blob: fa1d94a4eb5fe49a1ac937c2e9b3b852716718a6 [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"
Oscar Mateo59b449d2018-04-10 09:12:47 -070038#include "intel_workarounds.h"
Matthew Auld465c4032017-10-06 23:18:14 +010039#include "i915_gemfs.h"
Chris Wilson6b5e90f2016-11-14 20:41:05 +000040#include <linux/dma-fence-array.h>
Chris Wilsonfe3288b2017-02-12 17:20:01 +000041#include <linux/kthread.h>
Chris Wilsonc13d87e2016-07-20 09:21:15 +010042#include <linux/reservation.h>
Hugh Dickins5949eac2011-06-27 16:18:18 -070043#include <linux/shmem_fs.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090044#include <linux/slab.h>
Chris Wilson20e49332016-11-22 14:41:21 +000045#include <linux/stop_machine.h>
Eric Anholt673a3942008-07-30 12:06:12 -070046#include <linux/swap.h>
Jesse Barnes79e53942008-11-07 14:24:08 -080047#include <linux/pci.h>
Daniel Vetter1286ff72012-05-10 15:25:09 +020048#include <linux/dma-buf.h>
Eric Anholt673a3942008-07-30 12:06:12 -070049
Chris Wilsonfbbd37b2016-10-28 13:58:42 +010050static void i915_gem_flush_free_objects(struct drm_i915_private *i915);
Chris Wilson61050802012-04-17 15:31:31 +010051
Chris Wilson2c225692013-08-09 12:26:45 +010052static bool cpu_write_needs_clflush(struct drm_i915_gem_object *obj)
53{
Chris Wilsone27ab732017-06-15 13:38:49 +010054 if (obj->cache_dirty)
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +053055 return false;
56
Chris Wilsonb8f55be2017-08-11 12:11:16 +010057 if (!(obj->cache_coherent & I915_BO_CACHE_COHERENT_FOR_WRITE))
Chris Wilson2c225692013-08-09 12:26:45 +010058 return true;
59
Chris Wilsonbd3d2252017-10-13 21:26:14 +010060 return obj->pin_global; /* currently in use by HW, keep flushed */
Chris Wilson2c225692013-08-09 12:26:45 +010061}
62
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +053063static int
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +010064insert_mappable_node(struct i915_ggtt *ggtt,
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +053065 struct drm_mm_node *node, u32 size)
66{
67 memset(node, 0, sizeof(*node));
Chris Wilson4e64e552017-02-02 21:04:38 +000068 return drm_mm_insert_node_in_range(&ggtt->base.mm, node,
69 size, 0, I915_COLOR_UNEVICTABLE,
70 0, ggtt->mappable_end,
71 DRM_MM_INSERT_LOW);
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +053072}
73
74static void
75remove_mappable_node(struct drm_mm_node *node)
76{
77 drm_mm_remove_node(node);
78}
79
Chris Wilson73aa8082010-09-30 11:46:12 +010080/* some bookkeeping */
81static void i915_gem_info_add_obj(struct drm_i915_private *dev_priv,
Chris Wilson3ef7f222016-10-18 13:02:48 +010082 u64 size)
Chris Wilson73aa8082010-09-30 11:46:12 +010083{
Daniel Vetterc20e8352013-07-24 22:40:23 +020084 spin_lock(&dev_priv->mm.object_stat_lock);
Chris Wilson73aa8082010-09-30 11:46:12 +010085 dev_priv->mm.object_count++;
86 dev_priv->mm.object_memory += size;
Daniel Vetterc20e8352013-07-24 22:40:23 +020087 spin_unlock(&dev_priv->mm.object_stat_lock);
Chris Wilson73aa8082010-09-30 11:46:12 +010088}
89
90static void i915_gem_info_remove_obj(struct drm_i915_private *dev_priv,
Chris Wilson3ef7f222016-10-18 13:02:48 +010091 u64 size)
Chris Wilson73aa8082010-09-30 11:46:12 +010092{
Daniel Vetterc20e8352013-07-24 22:40:23 +020093 spin_lock(&dev_priv->mm.object_stat_lock);
Chris Wilson73aa8082010-09-30 11:46:12 +010094 dev_priv->mm.object_count--;
95 dev_priv->mm.object_memory -= size;
Daniel Vetterc20e8352013-07-24 22:40:23 +020096 spin_unlock(&dev_priv->mm.object_stat_lock);
Chris Wilson73aa8082010-09-30 11:46:12 +010097}
98
Chris Wilson21dd3732011-01-26 15:55:56 +000099static int
Daniel Vetter33196de2012-11-14 17:14:05 +0100100i915_gem_wait_for_error(struct i915_gpu_error *error)
Chris Wilson30dbf0c2010-09-25 10:19:17 +0100101{
Chris Wilson30dbf0c2010-09-25 10:19:17 +0100102 int ret;
103
Chris Wilson4c7d62c2016-10-28 13:58:32 +0100104 might_sleep();
105
Daniel Vetter0a6759c2012-07-04 22:18:41 +0200106 /*
107 * Only wait 10 seconds for the gpu reset to complete to avoid hanging
108 * userspace. If it takes that long something really bad is going on and
109 * we should simply try to bail out and fail as gracefully as possible.
110 */
Daniel Vetter1f83fee2012-11-15 17:17:22 +0100111 ret = wait_event_interruptible_timeout(error->reset_queue,
Chris Wilson8c185ec2017-03-16 17:13:02 +0000112 !i915_reset_backoff(error),
Chris Wilsonb52992c2016-10-28 13:58:24 +0100113 I915_RESET_TIMEOUT);
Daniel Vetter0a6759c2012-07-04 22:18:41 +0200114 if (ret == 0) {
115 DRM_ERROR("Timed out waiting for the gpu reset to complete\n");
116 return -EIO;
117 } else if (ret < 0) {
Chris Wilson30dbf0c2010-09-25 10:19:17 +0100118 return ret;
Chris Wilsond98c52c2016-04-13 17:35:05 +0100119 } else {
120 return 0;
Daniel Vetter0a6759c2012-07-04 22:18:41 +0200121 }
Chris Wilson30dbf0c2010-09-25 10:19:17 +0100122}
123
Chris Wilson54cf91d2010-11-25 18:00:26 +0000124int i915_mutex_lock_interruptible(struct drm_device *dev)
Chris Wilson76c1dec2010-09-25 11:22:51 +0100125{
Chris Wilsonfac5e232016-07-04 11:34:36 +0100126 struct drm_i915_private *dev_priv = to_i915(dev);
Chris Wilson76c1dec2010-09-25 11:22:51 +0100127 int ret;
128
Daniel Vetter33196de2012-11-14 17:14:05 +0100129 ret = i915_gem_wait_for_error(&dev_priv->gpu_error);
Chris Wilson76c1dec2010-09-25 11:22:51 +0100130 if (ret)
131 return ret;
132
133 ret = mutex_lock_interruptible(&dev->struct_mutex);
134 if (ret)
135 return ret;
136
Chris Wilson76c1dec2010-09-25 11:22:51 +0100137 return 0;
138}
Chris Wilson30dbf0c2010-09-25 10:19:17 +0100139
Chris Wilsone4d20062018-04-06 16:51:44 +0100140static u32 __i915_gem_park(struct drm_i915_private *i915)
141{
142 lockdep_assert_held(&i915->drm.struct_mutex);
143 GEM_BUG_ON(i915->gt.active_requests);
Chris Wilson643b4502018-04-30 14:15:03 +0100144 GEM_BUG_ON(!list_empty(&i915->gt.active_rings));
Chris Wilsone4d20062018-04-06 16:51:44 +0100145
146 if (!i915->gt.awake)
147 return I915_EPOCH_INVALID;
148
149 GEM_BUG_ON(i915->gt.epoch == I915_EPOCH_INVALID);
150
151 /*
152 * Be paranoid and flush a concurrent interrupt to make sure
153 * we don't reactivate any irq tasklets after parking.
154 *
155 * FIXME: Note that even though we have waited for execlists to be idle,
156 * there may still be an in-flight interrupt even though the CSB
157 * is now empty. synchronize_irq() makes sure that a residual interrupt
158 * is completed before we continue, but it doesn't prevent the HW from
159 * raising a spurious interrupt later. To complete the shield we should
160 * coordinate disabling the CS irq with flushing the interrupts.
161 */
162 synchronize_irq(i915->drm.irq);
163
164 intel_engines_park(i915);
165 i915_gem_timelines_park(i915);
166
167 i915_pmu_gt_parked(i915);
168
169 i915->gt.awake = false;
170
171 if (INTEL_GEN(i915) >= 6)
172 gen6_rps_idle(i915);
173
174 intel_display_power_put(i915, POWER_DOMAIN_GT_IRQ);
175
176 intel_runtime_pm_put(i915);
177
178 return i915->gt.epoch;
179}
180
181void i915_gem_park(struct drm_i915_private *i915)
182{
183 lockdep_assert_held(&i915->drm.struct_mutex);
184 GEM_BUG_ON(i915->gt.active_requests);
185
186 if (!i915->gt.awake)
187 return;
188
189 /* Defer the actual call to __i915_gem_park() to prevent ping-pongs */
190 mod_delayed_work(i915->wq, &i915->gt.idle_work, msecs_to_jiffies(100));
191}
192
193void i915_gem_unpark(struct drm_i915_private *i915)
194{
195 lockdep_assert_held(&i915->drm.struct_mutex);
196 GEM_BUG_ON(!i915->gt.active_requests);
197
198 if (i915->gt.awake)
199 return;
200
201 intel_runtime_pm_get_noresume(i915);
202
203 /*
204 * It seems that the DMC likes to transition between the DC states a lot
205 * when there are no connected displays (no active power domains) during
206 * command submission.
207 *
208 * This activity has negative impact on the performance of the chip with
209 * huge latencies observed in the interrupt handler and elsewhere.
210 *
211 * Work around it by grabbing a GT IRQ power domain whilst there is any
212 * GT activity, preventing any DC state transitions.
213 */
214 intel_display_power_get(i915, POWER_DOMAIN_GT_IRQ);
215
216 i915->gt.awake = true;
217 if (unlikely(++i915->gt.epoch == 0)) /* keep 0 as invalid */
218 i915->gt.epoch = 1;
219
220 intel_enable_gt_powersave(i915);
221 i915_update_gfx_val(i915);
222 if (INTEL_GEN(i915) >= 6)
223 gen6_rps_busy(i915);
224 i915_pmu_gt_unparked(i915);
225
226 intel_engines_unpark(i915);
227
228 i915_queue_hangcheck(i915);
229
230 queue_delayed_work(i915->wq,
231 &i915->gt.retire_work,
232 round_jiffies_up_relative(HZ));
233}
234
Eric Anholt673a3942008-07-30 12:06:12 -0700235int
Eric Anholt5a125c32008-10-22 21:40:13 -0700236i915_gem_get_aperture_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +0000237 struct drm_file *file)
Eric Anholt5a125c32008-10-22 21:40:13 -0700238{
Joonas Lahtinen72e96d62016-03-30 16:57:10 +0300239 struct drm_i915_private *dev_priv = to_i915(dev);
Joonas Lahtinen62106b42016-03-18 10:42:57 +0200240 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Joonas Lahtinen72e96d62016-03-30 16:57:10 +0300241 struct drm_i915_gem_get_aperture *args = data;
Tvrtko Ursulinca1543b2015-07-01 11:51:10 +0100242 struct i915_vma *vma;
Weinan Liff8f7972017-05-31 10:35:52 +0800243 u64 pinned;
Eric Anholt5a125c32008-10-22 21:40:13 -0700244
Weinan Liff8f7972017-05-31 10:35:52 +0800245 pinned = ggtt->base.reserved;
Chris Wilson73aa8082010-09-30 11:46:12 +0100246 mutex_lock(&dev->struct_mutex);
Chris Wilson1c7f4bc2016-02-26 11:03:19 +0000247 list_for_each_entry(vma, &ggtt->base.active_list, vm_link)
Chris Wilson20dfbde2016-08-04 16:32:30 +0100248 if (i915_vma_is_pinned(vma))
Tvrtko Ursulinca1543b2015-07-01 11:51:10 +0100249 pinned += vma->node.size;
Chris Wilson1c7f4bc2016-02-26 11:03:19 +0000250 list_for_each_entry(vma, &ggtt->base.inactive_list, vm_link)
Chris Wilson20dfbde2016-08-04 16:32:30 +0100251 if (i915_vma_is_pinned(vma))
Tvrtko Ursulinca1543b2015-07-01 11:51:10 +0100252 pinned += vma->node.size;
Chris Wilson73aa8082010-09-30 11:46:12 +0100253 mutex_unlock(&dev->struct_mutex);
Eric Anholt5a125c32008-10-22 21:40:13 -0700254
Joonas Lahtinen72e96d62016-03-30 16:57:10 +0300255 args->aper_size = ggtt->base.total;
Akshay Joshi0206e352011-08-16 15:34:10 -0400256 args->aper_available_size = args->aper_size - pinned;
Chris Wilson6299f992010-11-24 12:23:44 +0000257
Eric Anholt5a125c32008-10-22 21:40:13 -0700258 return 0;
259}
260
Matthew Auldb91b09e2017-10-06 23:18:17 +0100261static int i915_gem_object_get_pages_phys(struct drm_i915_gem_object *obj)
Chris Wilson00731152014-05-21 12:42:56 +0100262{
Al Viro93c76a32015-12-04 23:45:44 -0500263 struct address_space *mapping = obj->base.filp->f_mapping;
Chris Wilsondbb43512016-12-07 13:34:11 +0000264 drm_dma_handle_t *phys;
Chris Wilson6a2c4232014-11-04 04:51:40 -0800265 struct sg_table *st;
266 struct scatterlist *sg;
Chris Wilsondbb43512016-12-07 13:34:11 +0000267 char *vaddr;
Chris Wilson6a2c4232014-11-04 04:51:40 -0800268 int i;
Matthew Auldb91b09e2017-10-06 23:18:17 +0100269 int err;
Chris Wilson00731152014-05-21 12:42:56 +0100270
Chris Wilson6a2c4232014-11-04 04:51:40 -0800271 if (WARN_ON(i915_gem_object_needs_bit17_swizzle(obj)))
Matthew Auldb91b09e2017-10-06 23:18:17 +0100272 return -EINVAL;
Chris Wilson00731152014-05-21 12:42:56 +0100273
Chris Wilsondbb43512016-12-07 13:34:11 +0000274 /* Always aligning to the object size, allows a single allocation
275 * to handle all possible callers, and given typical object sizes,
276 * the alignment of the buddy allocation will naturally match.
277 */
278 phys = drm_pci_alloc(obj->base.dev,
Ville Syrjälä750fae22017-09-07 17:32:03 +0300279 roundup_pow_of_two(obj->base.size),
Chris Wilsondbb43512016-12-07 13:34:11 +0000280 roundup_pow_of_two(obj->base.size));
281 if (!phys)
Matthew Auldb91b09e2017-10-06 23:18:17 +0100282 return -ENOMEM;
Chris Wilsondbb43512016-12-07 13:34:11 +0000283
284 vaddr = phys->vaddr;
Chris Wilson6a2c4232014-11-04 04:51:40 -0800285 for (i = 0; i < obj->base.size / PAGE_SIZE; i++) {
286 struct page *page;
287 char *src;
288
289 page = shmem_read_mapping_page(mapping, i);
Chris Wilsondbb43512016-12-07 13:34:11 +0000290 if (IS_ERR(page)) {
Matthew Auldb91b09e2017-10-06 23:18:17 +0100291 err = PTR_ERR(page);
Chris Wilsondbb43512016-12-07 13:34:11 +0000292 goto err_phys;
293 }
Chris Wilson6a2c4232014-11-04 04:51:40 -0800294
295 src = kmap_atomic(page);
296 memcpy(vaddr, src, PAGE_SIZE);
297 drm_clflush_virt_range(vaddr, PAGE_SIZE);
298 kunmap_atomic(src);
299
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300300 put_page(page);
Chris Wilson6a2c4232014-11-04 04:51:40 -0800301 vaddr += PAGE_SIZE;
302 }
303
Chris Wilsonc0336662016-05-06 15:40:21 +0100304 i915_gem_chipset_flush(to_i915(obj->base.dev));
Chris Wilson6a2c4232014-11-04 04:51:40 -0800305
306 st = kmalloc(sizeof(*st), GFP_KERNEL);
Chris Wilsondbb43512016-12-07 13:34:11 +0000307 if (!st) {
Matthew Auldb91b09e2017-10-06 23:18:17 +0100308 err = -ENOMEM;
Chris Wilsondbb43512016-12-07 13:34:11 +0000309 goto err_phys;
310 }
Chris Wilson6a2c4232014-11-04 04:51:40 -0800311
312 if (sg_alloc_table(st, 1, GFP_KERNEL)) {
313 kfree(st);
Matthew Auldb91b09e2017-10-06 23:18:17 +0100314 err = -ENOMEM;
Chris Wilsondbb43512016-12-07 13:34:11 +0000315 goto err_phys;
Chris Wilson6a2c4232014-11-04 04:51:40 -0800316 }
317
318 sg = st->sgl;
319 sg->offset = 0;
320 sg->length = obj->base.size;
321
Chris Wilsondbb43512016-12-07 13:34:11 +0000322 sg_dma_address(sg) = phys->busaddr;
Chris Wilson6a2c4232014-11-04 04:51:40 -0800323 sg_dma_len(sg) = obj->base.size;
324
Chris Wilsondbb43512016-12-07 13:34:11 +0000325 obj->phys_handle = phys;
Matthew Auldb91b09e2017-10-06 23:18:17 +0100326
Matthew Aulda5c081662017-10-06 23:18:18 +0100327 __i915_gem_object_set_pages(obj, st, sg->length);
Matthew Auldb91b09e2017-10-06 23:18:17 +0100328
329 return 0;
Chris Wilsondbb43512016-12-07 13:34:11 +0000330
331err_phys:
332 drm_pci_free(obj->base.dev, phys);
Matthew Auldb91b09e2017-10-06 23:18:17 +0100333
334 return err;
Chris Wilson6a2c4232014-11-04 04:51:40 -0800335}
336
Chris Wilsone27ab732017-06-15 13:38:49 +0100337static void __start_cpu_write(struct drm_i915_gem_object *obj)
338{
Christian Königc0a51fd2018-02-16 13:43:38 +0100339 obj->read_domains = I915_GEM_DOMAIN_CPU;
340 obj->write_domain = I915_GEM_DOMAIN_CPU;
Chris Wilsone27ab732017-06-15 13:38:49 +0100341 if (cpu_write_needs_clflush(obj))
342 obj->cache_dirty = true;
343}
344
Chris Wilson6a2c4232014-11-04 04:51:40 -0800345static void
Chris Wilson2b3c8312016-11-11 14:58:09 +0000346__i915_gem_object_release_shmem(struct drm_i915_gem_object *obj,
Chris Wilsone5facdf2016-12-23 14:57:57 +0000347 struct sg_table *pages,
348 bool needs_clflush)
Chris Wilson6a2c4232014-11-04 04:51:40 -0800349{
Chris Wilsona4f5ea62016-10-28 13:58:35 +0100350 GEM_BUG_ON(obj->mm.madv == __I915_MADV_PURGED);
Chris Wilson6a2c4232014-11-04 04:51:40 -0800351
Chris Wilsona4f5ea62016-10-28 13:58:35 +0100352 if (obj->mm.madv == I915_MADV_DONTNEED)
353 obj->mm.dirty = false;
Chris Wilson6a2c4232014-11-04 04:51:40 -0800354
Chris Wilsone5facdf2016-12-23 14:57:57 +0000355 if (needs_clflush &&
Christian Königc0a51fd2018-02-16 13:43:38 +0100356 (obj->read_domains & I915_GEM_DOMAIN_CPU) == 0 &&
Chris Wilsonb8f55be2017-08-11 12:11:16 +0100357 !(obj->cache_coherent & I915_BO_CACHE_COHERENT_FOR_READ))
Chris Wilson2b3c8312016-11-11 14:58:09 +0000358 drm_clflush_sg(pages);
Chris Wilson03ac84f2016-10-28 13:58:36 +0100359
Chris Wilsone27ab732017-06-15 13:38:49 +0100360 __start_cpu_write(obj);
Chris Wilson03ac84f2016-10-28 13:58:36 +0100361}
362
363static void
364i915_gem_object_put_pages_phys(struct drm_i915_gem_object *obj,
365 struct sg_table *pages)
366{
Chris Wilsone5facdf2016-12-23 14:57:57 +0000367 __i915_gem_object_release_shmem(obj, pages, false);
Chris Wilson03ac84f2016-10-28 13:58:36 +0100368
Chris Wilsona4f5ea62016-10-28 13:58:35 +0100369 if (obj->mm.dirty) {
Al Viro93c76a32015-12-04 23:45:44 -0500370 struct address_space *mapping = obj->base.filp->f_mapping;
Chris Wilson6a2c4232014-11-04 04:51:40 -0800371 char *vaddr = obj->phys_handle->vaddr;
Chris Wilson00731152014-05-21 12:42:56 +0100372 int i;
373
374 for (i = 0; i < obj->base.size / PAGE_SIZE; i++) {
Chris Wilson6a2c4232014-11-04 04:51:40 -0800375 struct page *page;
376 char *dst;
Chris Wilson00731152014-05-21 12:42:56 +0100377
Chris Wilson6a2c4232014-11-04 04:51:40 -0800378 page = shmem_read_mapping_page(mapping, i);
379 if (IS_ERR(page))
380 continue;
381
382 dst = kmap_atomic(page);
383 drm_clflush_virt_range(vaddr, PAGE_SIZE);
384 memcpy(dst, vaddr, PAGE_SIZE);
385 kunmap_atomic(dst);
386
387 set_page_dirty(page);
Chris Wilsona4f5ea62016-10-28 13:58:35 +0100388 if (obj->mm.madv == I915_MADV_WILLNEED)
Chris Wilson00731152014-05-21 12:42:56 +0100389 mark_page_accessed(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300390 put_page(page);
Chris Wilson00731152014-05-21 12:42:56 +0100391 vaddr += PAGE_SIZE;
392 }
Chris Wilsona4f5ea62016-10-28 13:58:35 +0100393 obj->mm.dirty = false;
Chris Wilson00731152014-05-21 12:42:56 +0100394 }
395
Chris Wilson03ac84f2016-10-28 13:58:36 +0100396 sg_free_table(pages);
397 kfree(pages);
Chris Wilsondbb43512016-12-07 13:34:11 +0000398
399 drm_pci_free(obj->base.dev, obj->phys_handle);
Chris Wilson6a2c4232014-11-04 04:51:40 -0800400}
401
402static void
403i915_gem_object_release_phys(struct drm_i915_gem_object *obj)
404{
Chris Wilsona4f5ea62016-10-28 13:58:35 +0100405 i915_gem_object_unpin_pages(obj);
Chris Wilson6a2c4232014-11-04 04:51:40 -0800406}
407
408static const struct drm_i915_gem_object_ops i915_gem_phys_ops = {
409 .get_pages = i915_gem_object_get_pages_phys,
410 .put_pages = i915_gem_object_put_pages_phys,
411 .release = i915_gem_object_release_phys,
412};
413
Chris Wilson581ab1f2017-02-15 16:39:00 +0000414static const struct drm_i915_gem_object_ops i915_gem_object_ops;
415
Chris Wilson35a96112016-08-14 18:44:40 +0100416int i915_gem_object_unbind(struct drm_i915_gem_object *obj)
Chris Wilsonaa653a62016-08-04 07:52:27 +0100417{
418 struct i915_vma *vma;
419 LIST_HEAD(still_in_list);
Chris Wilson02bef8f2016-08-14 18:44:41 +0100420 int ret;
Chris Wilsonaa653a62016-08-04 07:52:27 +0100421
Chris Wilson02bef8f2016-08-14 18:44:41 +0100422 lockdep_assert_held(&obj->base.dev->struct_mutex);
423
424 /* Closed vma are removed from the obj->vma_list - but they may
425 * still have an active binding on the object. To remove those we
426 * must wait for all rendering to complete to the object (as unbinding
427 * must anyway), and retire the requests.
Chris Wilsonaa653a62016-08-04 07:52:27 +0100428 */
Chris Wilson5888fc92017-12-04 13:25:13 +0000429 ret = i915_gem_object_set_to_cpu_domain(obj, false);
Chris Wilson02bef8f2016-08-14 18:44:41 +0100430 if (ret)
431 return ret;
432
Chris Wilsonaa653a62016-08-04 07:52:27 +0100433 while ((vma = list_first_entry_or_null(&obj->vma_list,
434 struct i915_vma,
435 obj_link))) {
436 list_move_tail(&vma->obj_link, &still_in_list);
437 ret = i915_vma_unbind(vma);
438 if (ret)
439 break;
440 }
441 list_splice(&still_in_list, &obj->vma_list);
442
443 return ret;
444}
445
Chris Wilsone95433c2016-10-28 13:58:27 +0100446static long
447i915_gem_object_wait_fence(struct dma_fence *fence,
448 unsigned int flags,
449 long timeout,
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +0100450 struct intel_rps_client *rps_client)
Chris Wilsone95433c2016-10-28 13:58:27 +0100451{
Chris Wilsone61e0f52018-02-21 09:56:36 +0000452 struct i915_request *rq;
Chris Wilsone95433c2016-10-28 13:58:27 +0100453
454 BUILD_BUG_ON(I915_WAIT_INTERRUPTIBLE != 0x1);
455
456 if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags))
457 return timeout;
458
459 if (!dma_fence_is_i915(fence))
460 return dma_fence_wait_timeout(fence,
461 flags & I915_WAIT_INTERRUPTIBLE,
462 timeout);
463
464 rq = to_request(fence);
Chris Wilsone61e0f52018-02-21 09:56:36 +0000465 if (i915_request_completed(rq))
Chris Wilsone95433c2016-10-28 13:58:27 +0100466 goto out;
467
Chris Wilsone9af4ea2018-01-18 13:16:09 +0000468 /*
469 * This client is about to stall waiting for the GPU. In many cases
Chris Wilsone95433c2016-10-28 13:58:27 +0100470 * this is undesirable and limits the throughput of the system, as
471 * many clients cannot continue processing user input/output whilst
472 * blocked. RPS autotuning may take tens of milliseconds to respond
473 * to the GPU load and thus incurs additional latency for the client.
474 * We can circumvent that by promoting the GPU frequency to maximum
475 * before we wait. This makes the GPU throttle up much more quickly
476 * (good for benchmarks and user experience, e.g. window animations),
477 * but at a cost of spending more power processing the workload
478 * (bad for battery). Not all clients even want their results
479 * immediately and for them we should just let the GPU select its own
480 * frequency to maximise efficiency. To prevent a single client from
481 * forcing the clocks too high for the whole system, we only allow
482 * each client to waitboost once in a busy period.
483 */
Chris Wilsone61e0f52018-02-21 09:56:36 +0000484 if (rps_client && !i915_request_started(rq)) {
Chris Wilsone95433c2016-10-28 13:58:27 +0100485 if (INTEL_GEN(rq->i915) >= 6)
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +0100486 gen6_rps_boost(rq, rps_client);
Chris Wilsone95433c2016-10-28 13:58:27 +0100487 }
488
Chris Wilsone61e0f52018-02-21 09:56:36 +0000489 timeout = i915_request_wait(rq, flags, timeout);
Chris Wilsone95433c2016-10-28 13:58:27 +0100490
491out:
Chris Wilsone61e0f52018-02-21 09:56:36 +0000492 if (flags & I915_WAIT_LOCKED && i915_request_completed(rq))
493 i915_request_retire_upto(rq);
Chris Wilsone95433c2016-10-28 13:58:27 +0100494
Chris Wilsone95433c2016-10-28 13:58:27 +0100495 return timeout;
496}
497
498static long
499i915_gem_object_wait_reservation(struct reservation_object *resv,
500 unsigned int flags,
501 long timeout,
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +0100502 struct intel_rps_client *rps_client)
Chris Wilsone95433c2016-10-28 13:58:27 +0100503{
Chris Wilsone54ca972017-02-17 15:13:04 +0000504 unsigned int seq = __read_seqcount_begin(&resv->seq);
Chris Wilsone95433c2016-10-28 13:58:27 +0100505 struct dma_fence *excl;
Chris Wilsone54ca972017-02-17 15:13:04 +0000506 bool prune_fences = false;
Chris Wilsone95433c2016-10-28 13:58:27 +0100507
508 if (flags & I915_WAIT_ALL) {
509 struct dma_fence **shared;
510 unsigned int count, i;
511 int ret;
512
513 ret = reservation_object_get_fences_rcu(resv,
514 &excl, &count, &shared);
515 if (ret)
516 return ret;
517
518 for (i = 0; i < count; i++) {
519 timeout = i915_gem_object_wait_fence(shared[i],
520 flags, timeout,
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +0100521 rps_client);
Chris Wilsond892e932017-02-12 21:53:43 +0000522 if (timeout < 0)
Chris Wilsone95433c2016-10-28 13:58:27 +0100523 break;
524
525 dma_fence_put(shared[i]);
526 }
527
528 for (; i < count; i++)
529 dma_fence_put(shared[i]);
530 kfree(shared);
Chris Wilsone54ca972017-02-17 15:13:04 +0000531
Chris Wilsonfa730552018-03-07 17:13:03 +0000532 /*
533 * If both shared fences and an exclusive fence exist,
534 * then by construction the shared fences must be later
535 * than the exclusive fence. If we successfully wait for
536 * all the shared fences, we know that the exclusive fence
537 * must all be signaled. If all the shared fences are
538 * signaled, we can prune the array and recover the
539 * floating references on the fences/requests.
540 */
Chris Wilsone54ca972017-02-17 15:13:04 +0000541 prune_fences = count && timeout >= 0;
Chris Wilsone95433c2016-10-28 13:58:27 +0100542 } else {
543 excl = reservation_object_get_excl_rcu(resv);
544 }
545
Chris Wilsonfa730552018-03-07 17:13:03 +0000546 if (excl && timeout >= 0)
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +0100547 timeout = i915_gem_object_wait_fence(excl, flags, timeout,
548 rps_client);
Chris Wilsone95433c2016-10-28 13:58:27 +0100549
550 dma_fence_put(excl);
551
Chris Wilsonfa730552018-03-07 17:13:03 +0000552 /*
553 * Opportunistically prune the fences iff we know they have *all* been
Chris Wilson03d1cac2017-03-08 13:26:28 +0000554 * signaled and that the reservation object has not been changed (i.e.
555 * no new fences have been added).
556 */
Chris Wilsone54ca972017-02-17 15:13:04 +0000557 if (prune_fences && !__read_seqcount_retry(&resv->seq, seq)) {
Chris Wilson03d1cac2017-03-08 13:26:28 +0000558 if (reservation_object_trylock(resv)) {
559 if (!__read_seqcount_retry(&resv->seq, seq))
560 reservation_object_add_excl_fence(resv, NULL);
561 reservation_object_unlock(resv);
562 }
Chris Wilsone54ca972017-02-17 15:13:04 +0000563 }
564
Chris Wilsone95433c2016-10-28 13:58:27 +0100565 return timeout;
566}
567
Chris Wilsonb7268c52018-04-18 19:40:52 +0100568static void __fence_set_priority(struct dma_fence *fence,
569 const struct i915_sched_attr *attr)
Chris Wilson6b5e90f2016-11-14 20:41:05 +0000570{
Chris Wilsone61e0f52018-02-21 09:56:36 +0000571 struct i915_request *rq;
Chris Wilson6b5e90f2016-11-14 20:41:05 +0000572 struct intel_engine_cs *engine;
573
Chris Wilsonc218ee02018-01-06 10:56:18 +0000574 if (dma_fence_is_signaled(fence) || !dma_fence_is_i915(fence))
Chris Wilson6b5e90f2016-11-14 20:41:05 +0000575 return;
576
577 rq = to_request(fence);
578 engine = rq->engine;
Chris Wilson6b5e90f2016-11-14 20:41:05 +0000579
Chris Wilson47650db2018-03-07 13:42:25 +0000580 rcu_read_lock();
581 if (engine->schedule)
Chris Wilsonb7268c52018-04-18 19:40:52 +0100582 engine->schedule(rq, attr);
Chris Wilson47650db2018-03-07 13:42:25 +0000583 rcu_read_unlock();
Chris Wilson6b5e90f2016-11-14 20:41:05 +0000584}
585
Chris Wilsonb7268c52018-04-18 19:40:52 +0100586static void fence_set_priority(struct dma_fence *fence,
587 const struct i915_sched_attr *attr)
Chris Wilson6b5e90f2016-11-14 20:41:05 +0000588{
589 /* Recurse once into a fence-array */
590 if (dma_fence_is_array(fence)) {
591 struct dma_fence_array *array = to_dma_fence_array(fence);
592 int i;
593
594 for (i = 0; i < array->num_fences; i++)
Chris Wilsonb7268c52018-04-18 19:40:52 +0100595 __fence_set_priority(array->fences[i], attr);
Chris Wilson6b5e90f2016-11-14 20:41:05 +0000596 } else {
Chris Wilsonb7268c52018-04-18 19:40:52 +0100597 __fence_set_priority(fence, attr);
Chris Wilson6b5e90f2016-11-14 20:41:05 +0000598 }
599}
600
601int
602i915_gem_object_wait_priority(struct drm_i915_gem_object *obj,
603 unsigned int flags,
Chris Wilsonb7268c52018-04-18 19:40:52 +0100604 const struct i915_sched_attr *attr)
Chris Wilson6b5e90f2016-11-14 20:41:05 +0000605{
606 struct dma_fence *excl;
607
608 if (flags & I915_WAIT_ALL) {
609 struct dma_fence **shared;
610 unsigned int count, i;
611 int ret;
612
613 ret = reservation_object_get_fences_rcu(obj->resv,
614 &excl, &count, &shared);
615 if (ret)
616 return ret;
617
618 for (i = 0; i < count; i++) {
Chris Wilsonb7268c52018-04-18 19:40:52 +0100619 fence_set_priority(shared[i], attr);
Chris Wilson6b5e90f2016-11-14 20:41:05 +0000620 dma_fence_put(shared[i]);
621 }
622
623 kfree(shared);
624 } else {
625 excl = reservation_object_get_excl_rcu(obj->resv);
626 }
627
628 if (excl) {
Chris Wilsonb7268c52018-04-18 19:40:52 +0100629 fence_set_priority(excl, attr);
Chris Wilson6b5e90f2016-11-14 20:41:05 +0000630 dma_fence_put(excl);
631 }
632 return 0;
633}
634
Chris Wilson00e60f22016-08-04 16:32:40 +0100635/**
Chris Wilsone95433c2016-10-28 13:58:27 +0100636 * Waits for rendering to the object to be completed
Chris Wilson00e60f22016-08-04 16:32:40 +0100637 * @obj: i915 gem object
Chris Wilsone95433c2016-10-28 13:58:27 +0100638 * @flags: how to wait (under a lock, for all rendering or just for writes etc)
639 * @timeout: how long to wait
Chris Wilsona0a8b1c2017-11-09 14:06:44 +0000640 * @rps_client: client (user process) to charge for any waitboosting
Chris Wilson00e60f22016-08-04 16:32:40 +0100641 */
642int
Chris Wilsone95433c2016-10-28 13:58:27 +0100643i915_gem_object_wait(struct drm_i915_gem_object *obj,
644 unsigned int flags,
645 long timeout,
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +0100646 struct intel_rps_client *rps_client)
Chris Wilson00e60f22016-08-04 16:32:40 +0100647{
Chris Wilsone95433c2016-10-28 13:58:27 +0100648 might_sleep();
649#if IS_ENABLED(CONFIG_LOCKDEP)
650 GEM_BUG_ON(debug_locks &&
651 !!lockdep_is_held(&obj->base.dev->struct_mutex) !=
652 !!(flags & I915_WAIT_LOCKED));
653#endif
654 GEM_BUG_ON(timeout < 0);
Chris Wilson00e60f22016-08-04 16:32:40 +0100655
Chris Wilsond07f0e52016-10-28 13:58:44 +0100656 timeout = i915_gem_object_wait_reservation(obj->resv,
657 flags, timeout,
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +0100658 rps_client);
Chris Wilsone95433c2016-10-28 13:58:27 +0100659 return timeout < 0 ? timeout : 0;
Chris Wilson00e60f22016-08-04 16:32:40 +0100660}
661
662static struct intel_rps_client *to_rps_client(struct drm_file *file)
663{
664 struct drm_i915_file_private *fpriv = file->driver_priv;
665
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +0100666 return &fpriv->rps_client;
Chris Wilson00e60f22016-08-04 16:32:40 +0100667}
668
Chris Wilson00731152014-05-21 12:42:56 +0100669static int
670i915_gem_phys_pwrite(struct drm_i915_gem_object *obj,
671 struct drm_i915_gem_pwrite *args,
Chris Wilson03ac84f2016-10-28 13:58:36 +0100672 struct drm_file *file)
Chris Wilson00731152014-05-21 12:42:56 +0100673{
Chris Wilson00731152014-05-21 12:42:56 +0100674 void *vaddr = obj->phys_handle->vaddr + args->offset;
Gustavo Padovan3ed605b2016-04-26 12:32:27 -0300675 char __user *user_data = u64_to_user_ptr(args->data_ptr);
Chris Wilson6a2c4232014-11-04 04:51:40 -0800676
677 /* We manually control the domain here and pretend that it
678 * remains coherent i.e. in the GTT domain, like shmem_pwrite.
679 */
Rodrigo Vivi77a0d1c2015-06-18 11:43:24 -0700680 intel_fb_obj_invalidate(obj, ORIGIN_CPU);
Chris Wilson10466d22017-01-06 15:22:38 +0000681 if (copy_from_user(vaddr, user_data, args->size))
682 return -EFAULT;
Chris Wilson00731152014-05-21 12:42:56 +0100683
Chris Wilson6a2c4232014-11-04 04:51:40 -0800684 drm_clflush_virt_range(vaddr, args->size);
Chris Wilson10466d22017-01-06 15:22:38 +0000685 i915_gem_chipset_flush(to_i915(obj->base.dev));
Paulo Zanoni063e4e62015-02-13 17:23:45 -0200686
Chris Wilsond59b21e2017-02-22 11:40:49 +0000687 intel_fb_obj_flush(obj, ORIGIN_CPU);
Chris Wilson10466d22017-01-06 15:22:38 +0000688 return 0;
Chris Wilson00731152014-05-21 12:42:56 +0100689}
690
Tvrtko Ursulin187685c2016-12-01 14:16:36 +0000691void *i915_gem_object_alloc(struct drm_i915_private *dev_priv)
Chris Wilson42dcedd2012-11-15 11:32:30 +0000692{
Chris Wilsonefab6d82015-04-07 16:20:57 +0100693 return kmem_cache_zalloc(dev_priv->objects, GFP_KERNEL);
Chris Wilson42dcedd2012-11-15 11:32:30 +0000694}
695
696void i915_gem_object_free(struct drm_i915_gem_object *obj)
697{
Chris Wilsonfac5e232016-07-04 11:34:36 +0100698 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
Chris Wilsonefab6d82015-04-07 16:20:57 +0100699 kmem_cache_free(dev_priv->objects, obj);
Chris Wilson42dcedd2012-11-15 11:32:30 +0000700}
701
Dave Airlieff72145b2011-02-07 12:16:14 +1000702static int
703i915_gem_create(struct drm_file *file,
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +0000704 struct drm_i915_private *dev_priv,
Dave Airlieff72145b2011-02-07 12:16:14 +1000705 uint64_t size,
706 uint32_t *handle_p)
Eric Anholt673a3942008-07-30 12:06:12 -0700707{
Chris Wilson05394f32010-11-08 19:18:58 +0000708 struct drm_i915_gem_object *obj;
Pekka Paalanena1a2d1d2009-08-23 12:40:55 +0300709 int ret;
710 u32 handle;
Eric Anholt673a3942008-07-30 12:06:12 -0700711
Dave Airlieff72145b2011-02-07 12:16:14 +1000712 size = roundup(size, PAGE_SIZE);
Chris Wilson8ffc0242011-09-14 14:14:28 +0200713 if (size == 0)
714 return -EINVAL;
Eric Anholt673a3942008-07-30 12:06:12 -0700715
716 /* Allocate the new object */
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +0000717 obj = i915_gem_object_create(dev_priv, size);
Chris Wilsonfe3db792016-04-25 13:32:13 +0100718 if (IS_ERR(obj))
719 return PTR_ERR(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700720
Chris Wilson05394f32010-11-08 19:18:58 +0000721 ret = drm_gem_handle_create(file, &obj->base, &handle);
Chris Wilson202f2fe2010-10-14 13:20:40 +0100722 /* drop reference from allocate - handle holds it now */
Chris Wilsonf0cd5182016-10-28 13:58:43 +0100723 i915_gem_object_put(obj);
Daniel Vetterd861e332013-07-24 23:25:03 +0200724 if (ret)
725 return ret;
Chris Wilson202f2fe2010-10-14 13:20:40 +0100726
Dave Airlieff72145b2011-02-07 12:16:14 +1000727 *handle_p = handle;
Eric Anholt673a3942008-07-30 12:06:12 -0700728 return 0;
729}
730
Dave Airlieff72145b2011-02-07 12:16:14 +1000731int
732i915_gem_dumb_create(struct drm_file *file,
733 struct drm_device *dev,
734 struct drm_mode_create_dumb *args)
735{
736 /* have to work out size/pitch and return them */
Paulo Zanonide45eaf2013-10-18 18:48:24 -0300737 args->pitch = ALIGN(args->width * DIV_ROUND_UP(args->bpp, 8), 64);
Dave Airlieff72145b2011-02-07 12:16:14 +1000738 args->size = args->pitch * args->height;
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +0000739 return i915_gem_create(file, to_i915(dev),
Dave Airlieda6b51d2014-12-24 13:11:17 +1000740 args->size, &args->handle);
Dave Airlieff72145b2011-02-07 12:16:14 +1000741}
742
Chris Wilsone27ab732017-06-15 13:38:49 +0100743static bool gpu_write_needs_clflush(struct drm_i915_gem_object *obj)
744{
745 return !(obj->cache_level == I915_CACHE_NONE ||
746 obj->cache_level == I915_CACHE_WT);
747}
748
Dave Airlieff72145b2011-02-07 12:16:14 +1000749/**
750 * Creates a new mm object and returns a handle to it.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +0100751 * @dev: drm device pointer
752 * @data: ioctl data blob
753 * @file: drm file pointer
Dave Airlieff72145b2011-02-07 12:16:14 +1000754 */
755int
756i915_gem_create_ioctl(struct drm_device *dev, void *data,
757 struct drm_file *file)
758{
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +0000759 struct drm_i915_private *dev_priv = to_i915(dev);
Dave Airlieff72145b2011-02-07 12:16:14 +1000760 struct drm_i915_gem_create *args = data;
Daniel Vetter63ed2cb2012-04-23 16:50:50 +0200761
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +0000762 i915_gem_flush_free_objects(dev_priv);
Chris Wilsonfbbd37b2016-10-28 13:58:42 +0100763
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +0000764 return i915_gem_create(file, dev_priv,
Dave Airlieda6b51d2014-12-24 13:11:17 +1000765 args->size, &args->handle);
Dave Airlieff72145b2011-02-07 12:16:14 +1000766}
767
Chris Wilsonef749212017-04-12 12:01:10 +0100768static inline enum fb_op_origin
769fb_write_origin(struct drm_i915_gem_object *obj, unsigned int domain)
770{
771 return (domain == I915_GEM_DOMAIN_GTT ?
772 obj->frontbuffer_ggtt_origin : ORIGIN_CPU);
773}
774
Chris Wilson7125397b2017-12-06 12:49:14 +0000775void i915_gem_flush_ggtt_writes(struct drm_i915_private *dev_priv)
Chris Wilsonef749212017-04-12 12:01:10 +0100776{
Chris Wilson7125397b2017-12-06 12:49:14 +0000777 /*
778 * No actual flushing is required for the GTT write domain for reads
779 * from the GTT domain. Writes to it "immediately" go to main memory
780 * as far as we know, so there's no chipset flush. It also doesn't
781 * land in the GPU render cache.
Chris Wilsonef749212017-04-12 12:01:10 +0100782 *
783 * However, we do have to enforce the order so that all writes through
784 * the GTT land before any writes to the device, such as updates to
785 * the GATT itself.
786 *
787 * We also have to wait a bit for the writes to land from the GTT.
788 * An uncached read (i.e. mmio) seems to be ideal for the round-trip
789 * timing. This issue has only been observed when switching quickly
790 * between GTT writes and CPU reads from inside the kernel on recent hw,
791 * and it appears to only affect discrete GTT blocks (i.e. on LLC
Chris Wilson7125397b2017-12-06 12:49:14 +0000792 * system agents we cannot reproduce this behaviour, until Cannonlake
793 * that was!).
Chris Wilsonef749212017-04-12 12:01:10 +0100794 */
Chris Wilson7125397b2017-12-06 12:49:14 +0000795
Chris Wilsonef749212017-04-12 12:01:10 +0100796 wmb();
797
Chris Wilson7125397b2017-12-06 12:49:14 +0000798 intel_runtime_pm_get(dev_priv);
799 spin_lock_irq(&dev_priv->uncore.lock);
800
801 POSTING_READ_FW(RING_HEAD(RENDER_RING_BASE));
802
803 spin_unlock_irq(&dev_priv->uncore.lock);
804 intel_runtime_pm_put(dev_priv);
805}
806
807static void
808flush_write_domain(struct drm_i915_gem_object *obj, unsigned int flush_domains)
809{
810 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
811 struct i915_vma *vma;
812
Christian Königc0a51fd2018-02-16 13:43:38 +0100813 if (!(obj->write_domain & flush_domains))
Chris Wilson7125397b2017-12-06 12:49:14 +0000814 return;
815
Christian Königc0a51fd2018-02-16 13:43:38 +0100816 switch (obj->write_domain) {
Chris Wilsonef749212017-04-12 12:01:10 +0100817 case I915_GEM_DOMAIN_GTT:
Chris Wilson7125397b2017-12-06 12:49:14 +0000818 i915_gem_flush_ggtt_writes(dev_priv);
Chris Wilsonef749212017-04-12 12:01:10 +0100819
820 intel_fb_obj_flush(obj,
821 fb_write_origin(obj, I915_GEM_DOMAIN_GTT));
Chris Wilson7125397b2017-12-06 12:49:14 +0000822
Chris Wilsone2189dd2017-12-07 21:14:07 +0000823 for_each_ggtt_vma(vma, obj) {
Chris Wilson7125397b2017-12-06 12:49:14 +0000824 if (vma->iomap)
825 continue;
826
827 i915_vma_unset_ggtt_write(vma);
828 }
Chris Wilsonef749212017-04-12 12:01:10 +0100829 break;
830
831 case I915_GEM_DOMAIN_CPU:
832 i915_gem_clflush_object(obj, I915_CLFLUSH_SYNC);
833 break;
Chris Wilsone27ab732017-06-15 13:38:49 +0100834
835 case I915_GEM_DOMAIN_RENDER:
836 if (gpu_write_needs_clflush(obj))
837 obj->cache_dirty = true;
838 break;
Chris Wilsonef749212017-04-12 12:01:10 +0100839 }
840
Christian Königc0a51fd2018-02-16 13:43:38 +0100841 obj->write_domain = 0;
Chris Wilsonef749212017-04-12 12:01:10 +0100842}
843
Daniel Vetter8c599672011-12-14 13:57:31 +0100844static inline int
Daniel Vetter8461d222011-12-14 13:57:32 +0100845__copy_to_user_swizzled(char __user *cpu_vaddr,
846 const char *gpu_vaddr, int gpu_offset,
847 int length)
848{
849 int ret, cpu_offset = 0;
850
851 while (length > 0) {
852 int cacheline_end = ALIGN(gpu_offset + 1, 64);
853 int this_length = min(cacheline_end - gpu_offset, length);
854 int swizzled_gpu_offset = gpu_offset ^ 64;
855
856 ret = __copy_to_user(cpu_vaddr + cpu_offset,
857 gpu_vaddr + swizzled_gpu_offset,
858 this_length);
859 if (ret)
860 return ret + length;
861
862 cpu_offset += this_length;
863 gpu_offset += this_length;
864 length -= this_length;
865 }
866
867 return 0;
868}
869
870static inline int
Ben Widawsky4f0c7cf2012-04-16 14:07:47 -0700871__copy_from_user_swizzled(char *gpu_vaddr, int gpu_offset,
872 const char __user *cpu_vaddr,
Daniel Vetter8c599672011-12-14 13:57:31 +0100873 int length)
874{
875 int ret, cpu_offset = 0;
876
877 while (length > 0) {
878 int cacheline_end = ALIGN(gpu_offset + 1, 64);
879 int this_length = min(cacheline_end - gpu_offset, length);
880 int swizzled_gpu_offset = gpu_offset ^ 64;
881
882 ret = __copy_from_user(gpu_vaddr + swizzled_gpu_offset,
883 cpu_vaddr + cpu_offset,
884 this_length);
885 if (ret)
886 return ret + length;
887
888 cpu_offset += this_length;
889 gpu_offset += this_length;
890 length -= this_length;
891 }
892
893 return 0;
894}
895
Brad Volkin4c914c02014-02-18 10:15:45 -0800896/*
897 * Pins the specified object's pages and synchronizes the object with
898 * GPU accesses. Sets needs_clflush to non-zero if the caller should
899 * flush the object from the CPU cache.
900 */
901int i915_gem_obj_prepare_shmem_read(struct drm_i915_gem_object *obj,
Chris Wilson43394c72016-08-18 17:16:47 +0100902 unsigned int *needs_clflush)
Brad Volkin4c914c02014-02-18 10:15:45 -0800903{
904 int ret;
905
Chris Wilsone95433c2016-10-28 13:58:27 +0100906 lockdep_assert_held(&obj->base.dev->struct_mutex);
Brad Volkin4c914c02014-02-18 10:15:45 -0800907
Chris Wilsone95433c2016-10-28 13:58:27 +0100908 *needs_clflush = 0;
Chris Wilson43394c72016-08-18 17:16:47 +0100909 if (!i915_gem_object_has_struct_page(obj))
910 return -ENODEV;
Brad Volkin4c914c02014-02-18 10:15:45 -0800911
Chris Wilsone95433c2016-10-28 13:58:27 +0100912 ret = i915_gem_object_wait(obj,
913 I915_WAIT_INTERRUPTIBLE |
914 I915_WAIT_LOCKED,
915 MAX_SCHEDULE_TIMEOUT,
916 NULL);
Chris Wilsonc13d87e2016-07-20 09:21:15 +0100917 if (ret)
918 return ret;
919
Chris Wilsona4f5ea62016-10-28 13:58:35 +0100920 ret = i915_gem_object_pin_pages(obj);
Chris Wilson97649512016-08-18 17:16:50 +0100921 if (ret)
922 return ret;
923
Chris Wilsonb8f55be2017-08-11 12:11:16 +0100924 if (obj->cache_coherent & I915_BO_CACHE_COHERENT_FOR_READ ||
925 !static_cpu_has(X86_FEATURE_CLFLUSH)) {
Chris Wilson7f5f95d2017-03-10 00:09:42 +0000926 ret = i915_gem_object_set_to_cpu_domain(obj, false);
927 if (ret)
928 goto err_unpin;
929 else
930 goto out;
931 }
932
Chris Wilsonef749212017-04-12 12:01:10 +0100933 flush_write_domain(obj, ~I915_GEM_DOMAIN_CPU);
Chris Wilsona314d5c2016-08-18 17:16:48 +0100934
Chris Wilson43394c72016-08-18 17:16:47 +0100935 /* If we're not in the cpu read domain, set ourself into the gtt
936 * read domain and manually flush cachelines (if required). This
937 * optimizes for the case when the gpu will dirty the data
938 * anyway again before the next pread happens.
939 */
Chris Wilsone27ab732017-06-15 13:38:49 +0100940 if (!obj->cache_dirty &&
Christian Königc0a51fd2018-02-16 13:43:38 +0100941 !(obj->read_domains & I915_GEM_DOMAIN_CPU))
Chris Wilson7f5f95d2017-03-10 00:09:42 +0000942 *needs_clflush = CLFLUSH_BEFORE;
Brad Volkin4c914c02014-02-18 10:15:45 -0800943
Chris Wilson7f5f95d2017-03-10 00:09:42 +0000944out:
Chris Wilson97649512016-08-18 17:16:50 +0100945 /* return with the pages pinned */
Chris Wilson43394c72016-08-18 17:16:47 +0100946 return 0;
Chris Wilson97649512016-08-18 17:16:50 +0100947
948err_unpin:
949 i915_gem_object_unpin_pages(obj);
950 return ret;
Chris Wilson43394c72016-08-18 17:16:47 +0100951}
952
953int i915_gem_obj_prepare_shmem_write(struct drm_i915_gem_object *obj,
954 unsigned int *needs_clflush)
955{
956 int ret;
957
Chris Wilsone95433c2016-10-28 13:58:27 +0100958 lockdep_assert_held(&obj->base.dev->struct_mutex);
959
Chris Wilson43394c72016-08-18 17:16:47 +0100960 *needs_clflush = 0;
961 if (!i915_gem_object_has_struct_page(obj))
962 return -ENODEV;
963
Chris Wilsone95433c2016-10-28 13:58:27 +0100964 ret = i915_gem_object_wait(obj,
965 I915_WAIT_INTERRUPTIBLE |
966 I915_WAIT_LOCKED |
967 I915_WAIT_ALL,
968 MAX_SCHEDULE_TIMEOUT,
969 NULL);
Chris Wilson43394c72016-08-18 17:16:47 +0100970 if (ret)
971 return ret;
972
Chris Wilsona4f5ea62016-10-28 13:58:35 +0100973 ret = i915_gem_object_pin_pages(obj);
Chris Wilson97649512016-08-18 17:16:50 +0100974 if (ret)
975 return ret;
976
Chris Wilsonb8f55be2017-08-11 12:11:16 +0100977 if (obj->cache_coherent & I915_BO_CACHE_COHERENT_FOR_WRITE ||
978 !static_cpu_has(X86_FEATURE_CLFLUSH)) {
Chris Wilson7f5f95d2017-03-10 00:09:42 +0000979 ret = i915_gem_object_set_to_cpu_domain(obj, true);
980 if (ret)
981 goto err_unpin;
982 else
983 goto out;
984 }
985
Chris Wilsonef749212017-04-12 12:01:10 +0100986 flush_write_domain(obj, ~I915_GEM_DOMAIN_CPU);
Chris Wilsona314d5c2016-08-18 17:16:48 +0100987
Chris Wilson43394c72016-08-18 17:16:47 +0100988 /* If we're not in the cpu write domain, set ourself into the
989 * gtt write domain and manually flush cachelines (as required).
990 * This optimizes for the case when the gpu will use the data
991 * right away and we therefore have to clflush anyway.
992 */
Chris Wilsone27ab732017-06-15 13:38:49 +0100993 if (!obj->cache_dirty) {
Chris Wilson7f5f95d2017-03-10 00:09:42 +0000994 *needs_clflush |= CLFLUSH_AFTER;
Chris Wilson43394c72016-08-18 17:16:47 +0100995
Chris Wilsone27ab732017-06-15 13:38:49 +0100996 /*
997 * Same trick applies to invalidate partially written
998 * cachelines read before writing.
999 */
Christian Königc0a51fd2018-02-16 13:43:38 +01001000 if (!(obj->read_domains & I915_GEM_DOMAIN_CPU))
Chris Wilsone27ab732017-06-15 13:38:49 +01001001 *needs_clflush |= CLFLUSH_BEFORE;
1002 }
Chris Wilson43394c72016-08-18 17:16:47 +01001003
Chris Wilson7f5f95d2017-03-10 00:09:42 +00001004out:
Chris Wilson43394c72016-08-18 17:16:47 +01001005 intel_fb_obj_invalidate(obj, ORIGIN_CPU);
Chris Wilsona4f5ea62016-10-28 13:58:35 +01001006 obj->mm.dirty = true;
Chris Wilson97649512016-08-18 17:16:50 +01001007 /* return with the pages pinned */
Chris Wilson43394c72016-08-18 17:16:47 +01001008 return 0;
Chris Wilson97649512016-08-18 17:16:50 +01001009
1010err_unpin:
1011 i915_gem_object_unpin_pages(obj);
1012 return ret;
Brad Volkin4c914c02014-02-18 10:15:45 -08001013}
1014
Daniel Vetter23c18c72012-03-25 19:47:42 +02001015static void
1016shmem_clflush_swizzled_range(char *addr, unsigned long length,
1017 bool swizzled)
1018{
Daniel Vettere7e58eb2012-03-25 19:47:43 +02001019 if (unlikely(swizzled)) {
Daniel Vetter23c18c72012-03-25 19:47:42 +02001020 unsigned long start = (unsigned long) addr;
1021 unsigned long end = (unsigned long) addr + length;
1022
1023 /* For swizzling simply ensure that we always flush both
1024 * channels. Lame, but simple and it works. Swizzled
1025 * pwrite/pread is far from a hotpath - current userspace
1026 * doesn't use it at all. */
1027 start = round_down(start, 128);
1028 end = round_up(end, 128);
1029
1030 drm_clflush_virt_range((void *)start, end - start);
1031 } else {
1032 drm_clflush_virt_range(addr, length);
1033 }
1034
1035}
1036
Daniel Vetterd174bd62012-03-25 19:47:40 +02001037/* Only difference to the fast-path function is that this can handle bit17
1038 * and uses non-atomic copy and kmap functions. */
1039static int
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001040shmem_pread_slow(struct page *page, int offset, int length,
Daniel Vetterd174bd62012-03-25 19:47:40 +02001041 char __user *user_data,
1042 bool page_do_bit17_swizzling, bool needs_clflush)
1043{
1044 char *vaddr;
1045 int ret;
1046
1047 vaddr = kmap(page);
1048 if (needs_clflush)
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001049 shmem_clflush_swizzled_range(vaddr + offset, length,
Daniel Vetter23c18c72012-03-25 19:47:42 +02001050 page_do_bit17_swizzling);
Daniel Vetterd174bd62012-03-25 19:47:40 +02001051
1052 if (page_do_bit17_swizzling)
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001053 ret = __copy_to_user_swizzled(user_data, vaddr, offset, length);
Daniel Vetterd174bd62012-03-25 19:47:40 +02001054 else
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001055 ret = __copy_to_user(user_data, vaddr + offset, length);
Daniel Vetterd174bd62012-03-25 19:47:40 +02001056 kunmap(page);
1057
Chris Wilsonf60d7f02012-09-04 21:02:56 +01001058 return ret ? - EFAULT : 0;
Daniel Vetterd174bd62012-03-25 19:47:40 +02001059}
1060
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001061static int
1062shmem_pread(struct page *page, int offset, int length, char __user *user_data,
1063 bool page_do_bit17_swizzling, bool needs_clflush)
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301064{
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001065 int ret;
1066
1067 ret = -ENODEV;
1068 if (!page_do_bit17_swizzling) {
1069 char *vaddr = kmap_atomic(page);
1070
1071 if (needs_clflush)
1072 drm_clflush_virt_range(vaddr + offset, length);
1073 ret = __copy_to_user_inatomic(user_data, vaddr + offset, length);
1074 kunmap_atomic(vaddr);
1075 }
1076 if (ret == 0)
1077 return 0;
1078
1079 return shmem_pread_slow(page, offset, length, user_data,
1080 page_do_bit17_swizzling, needs_clflush);
1081}
1082
1083static int
1084i915_gem_shmem_pread(struct drm_i915_gem_object *obj,
1085 struct drm_i915_gem_pread *args)
1086{
1087 char __user *user_data;
1088 u64 remain;
1089 unsigned int obj_do_bit17_swizzling;
1090 unsigned int needs_clflush;
1091 unsigned int idx, offset;
1092 int ret;
1093
1094 obj_do_bit17_swizzling = 0;
1095 if (i915_gem_object_needs_bit17_swizzle(obj))
1096 obj_do_bit17_swizzling = BIT(17);
1097
1098 ret = mutex_lock_interruptible(&obj->base.dev->struct_mutex);
1099 if (ret)
1100 return ret;
1101
1102 ret = i915_gem_obj_prepare_shmem_read(obj, &needs_clflush);
1103 mutex_unlock(&obj->base.dev->struct_mutex);
1104 if (ret)
1105 return ret;
1106
1107 remain = args->size;
1108 user_data = u64_to_user_ptr(args->data_ptr);
1109 offset = offset_in_page(args->offset);
1110 for (idx = args->offset >> PAGE_SHIFT; remain; idx++) {
1111 struct page *page = i915_gem_object_get_page(obj, idx);
1112 int length;
1113
1114 length = remain;
1115 if (offset + length > PAGE_SIZE)
1116 length = PAGE_SIZE - offset;
1117
1118 ret = shmem_pread(page, offset, length, user_data,
1119 page_to_phys(page) & obj_do_bit17_swizzling,
1120 needs_clflush);
1121 if (ret)
1122 break;
1123
1124 remain -= length;
1125 user_data += length;
1126 offset = 0;
1127 }
1128
1129 i915_gem_obj_finish_shmem_access(obj);
1130 return ret;
1131}
1132
1133static inline bool
1134gtt_user_read(struct io_mapping *mapping,
1135 loff_t base, int offset,
1136 char __user *user_data, int length)
1137{
Ville Syrjäläafe722b2017-09-01 20:12:52 +03001138 void __iomem *vaddr;
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001139 unsigned long unwritten;
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301140
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301141 /* We can use the cpu mem copy function because this is X86. */
Ville Syrjäläafe722b2017-09-01 20:12:52 +03001142 vaddr = io_mapping_map_atomic_wc(mapping, base);
1143 unwritten = __copy_to_user_inatomic(user_data,
1144 (void __force *)vaddr + offset,
1145 length);
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001146 io_mapping_unmap_atomic(vaddr);
1147 if (unwritten) {
Ville Syrjäläafe722b2017-09-01 20:12:52 +03001148 vaddr = io_mapping_map_wc(mapping, base, PAGE_SIZE);
1149 unwritten = copy_to_user(user_data,
1150 (void __force *)vaddr + offset,
1151 length);
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001152 io_mapping_unmap(vaddr);
1153 }
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301154 return unwritten;
1155}
1156
1157static int
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001158i915_gem_gtt_pread(struct drm_i915_gem_object *obj,
1159 const struct drm_i915_gem_pread *args)
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301160{
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001161 struct drm_i915_private *i915 = to_i915(obj->base.dev);
1162 struct i915_ggtt *ggtt = &i915->ggtt;
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301163 struct drm_mm_node node;
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001164 struct i915_vma *vma;
1165 void __user *user_data;
1166 u64 remain, offset;
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301167 int ret;
1168
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001169 ret = mutex_lock_interruptible(&i915->drm.struct_mutex);
1170 if (ret)
1171 return ret;
1172
1173 intel_runtime_pm_get(i915);
1174 vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0,
Chris Wilsona3259ca2017-10-09 09:44:00 +01001175 PIN_MAPPABLE |
1176 PIN_NONFAULT |
1177 PIN_NONBLOCK);
Chris Wilson18034582016-08-18 17:16:45 +01001178 if (!IS_ERR(vma)) {
1179 node.start = i915_ggtt_offset(vma);
1180 node.allocated = false;
Chris Wilson49ef5292016-08-18 17:17:00 +01001181 ret = i915_vma_put_fence(vma);
Chris Wilson18034582016-08-18 17:16:45 +01001182 if (ret) {
1183 i915_vma_unpin(vma);
1184 vma = ERR_PTR(ret);
1185 }
1186 }
Chris Wilson058d88c2016-08-15 10:49:06 +01001187 if (IS_ERR(vma)) {
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001188 ret = insert_mappable_node(ggtt, &node, PAGE_SIZE);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301189 if (ret)
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001190 goto out_unlock;
1191 GEM_BUG_ON(!node.allocated);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301192 }
1193
1194 ret = i915_gem_object_set_to_gtt_domain(obj, false);
1195 if (ret)
1196 goto out_unpin;
1197
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001198 mutex_unlock(&i915->drm.struct_mutex);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301199
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001200 user_data = u64_to_user_ptr(args->data_ptr);
1201 remain = args->size;
1202 offset = args->offset;
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301203
1204 while (remain > 0) {
1205 /* Operation in this page
1206 *
1207 * page_base = page offset within aperture
1208 * page_offset = offset within page
1209 * page_length = bytes to copy for this page
1210 */
1211 u32 page_base = node.start;
1212 unsigned page_offset = offset_in_page(offset);
1213 unsigned page_length = PAGE_SIZE - page_offset;
1214 page_length = remain < page_length ? remain : page_length;
1215 if (node.allocated) {
1216 wmb();
1217 ggtt->base.insert_page(&ggtt->base,
1218 i915_gem_object_get_dma_address(obj, offset >> PAGE_SHIFT),
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001219 node.start, I915_CACHE_NONE, 0);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301220 wmb();
1221 } else {
1222 page_base += offset & PAGE_MASK;
1223 }
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001224
Matthew Auld73ebd502017-12-11 15:18:20 +00001225 if (gtt_user_read(&ggtt->iomap, page_base, page_offset,
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001226 user_data, page_length)) {
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301227 ret = -EFAULT;
1228 break;
1229 }
1230
1231 remain -= page_length;
1232 user_data += page_length;
1233 offset += page_length;
1234 }
1235
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001236 mutex_lock(&i915->drm.struct_mutex);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301237out_unpin:
1238 if (node.allocated) {
1239 wmb();
1240 ggtt->base.clear_range(&ggtt->base,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02001241 node.start, node.size);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301242 remove_mappable_node(&node);
1243 } else {
Chris Wilson058d88c2016-08-15 10:49:06 +01001244 i915_vma_unpin(vma);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301245 }
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001246out_unlock:
1247 intel_runtime_pm_put(i915);
1248 mutex_unlock(&i915->drm.struct_mutex);
Chris Wilsonf60d7f02012-09-04 21:02:56 +01001249
Eric Anholteb014592009-03-10 11:44:52 -07001250 return ret;
1251}
1252
Eric Anholt673a3942008-07-30 12:06:12 -07001253/**
1254 * Reads data from the object referenced by handle.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01001255 * @dev: drm device pointer
1256 * @data: ioctl data blob
1257 * @file: drm file pointer
Eric Anholt673a3942008-07-30 12:06:12 -07001258 *
1259 * On error, the contents of *data are undefined.
1260 */
1261int
1262i915_gem_pread_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00001263 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001264{
1265 struct drm_i915_gem_pread *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00001266 struct drm_i915_gem_object *obj;
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001267 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07001268
Chris Wilson51311d02010-11-17 09:10:42 +00001269 if (args->size == 0)
1270 return 0;
1271
1272 if (!access_ok(VERIFY_WRITE,
Gustavo Padovan3ed605b2016-04-26 12:32:27 -03001273 u64_to_user_ptr(args->data_ptr),
Chris Wilson51311d02010-11-17 09:10:42 +00001274 args->size))
1275 return -EFAULT;
1276
Chris Wilson03ac0642016-07-20 13:31:51 +01001277 obj = i915_gem_object_lookup(file, args->handle);
Chris Wilson258a5ed2016-08-05 10:14:16 +01001278 if (!obj)
1279 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07001280
Chris Wilson7dcd2492010-09-26 20:21:44 +01001281 /* Bounds check source. */
Matthew Auld966d5bf2016-12-13 20:32:22 +00001282 if (range_overflows_t(u64, args->offset, args->size, obj->base.size)) {
Chris Wilsonce9d4192010-09-26 20:50:05 +01001283 ret = -EINVAL;
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001284 goto out;
Chris Wilsonce9d4192010-09-26 20:50:05 +01001285 }
1286
Chris Wilsondb53a302011-02-03 11:57:46 +00001287 trace_i915_gem_object_pread(obj, args->offset, args->size);
1288
Chris Wilsone95433c2016-10-28 13:58:27 +01001289 ret = i915_gem_object_wait(obj,
1290 I915_WAIT_INTERRUPTIBLE,
1291 MAX_SCHEDULE_TIMEOUT,
1292 to_rps_client(file));
Chris Wilson258a5ed2016-08-05 10:14:16 +01001293 if (ret)
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001294 goto out;
Chris Wilson258a5ed2016-08-05 10:14:16 +01001295
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001296 ret = i915_gem_object_pin_pages(obj);
Chris Wilson258a5ed2016-08-05 10:14:16 +01001297 if (ret)
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001298 goto out;
Chris Wilson258a5ed2016-08-05 10:14:16 +01001299
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001300 ret = i915_gem_shmem_pread(obj, args);
Chris Wilson9c870d02016-10-24 13:42:15 +01001301 if (ret == -EFAULT || ret == -ENODEV)
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001302 ret = i915_gem_gtt_pread(obj, args);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301303
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001304 i915_gem_object_unpin_pages(obj);
1305out:
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001306 i915_gem_object_put(obj);
Eric Anholteb014592009-03-10 11:44:52 -07001307 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07001308}
1309
Keith Packard0839ccb2008-10-30 19:38:48 -07001310/* This is the fast write path which cannot handle
1311 * page faults in the source data
Linus Torvalds9b7530cc2008-10-20 14:16:43 -07001312 */
Linus Torvalds9b7530cc2008-10-20 14:16:43 -07001313
Chris Wilsonfe115622016-10-28 13:58:40 +01001314static inline bool
1315ggtt_write(struct io_mapping *mapping,
1316 loff_t base, int offset,
1317 char __user *user_data, int length)
Keith Packard0839ccb2008-10-30 19:38:48 -07001318{
Ville Syrjäläafe722b2017-09-01 20:12:52 +03001319 void __iomem *vaddr;
Keith Packard0839ccb2008-10-30 19:38:48 -07001320 unsigned long unwritten;
1321
Ben Widawsky4f0c7cf2012-04-16 14:07:47 -07001322 /* We can use the cpu mem copy function because this is X86. */
Ville Syrjäläafe722b2017-09-01 20:12:52 +03001323 vaddr = io_mapping_map_atomic_wc(mapping, base);
1324 unwritten = __copy_from_user_inatomic_nocache((void __force *)vaddr + offset,
Keith Packard0839ccb2008-10-30 19:38:48 -07001325 user_data, length);
Chris Wilsonfe115622016-10-28 13:58:40 +01001326 io_mapping_unmap_atomic(vaddr);
1327 if (unwritten) {
Ville Syrjäläafe722b2017-09-01 20:12:52 +03001328 vaddr = io_mapping_map_wc(mapping, base, PAGE_SIZE);
1329 unwritten = copy_from_user((void __force *)vaddr + offset,
1330 user_data, length);
Chris Wilsonfe115622016-10-28 13:58:40 +01001331 io_mapping_unmap(vaddr);
1332 }
Keith Packard0839ccb2008-10-30 19:38:48 -07001333
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001334 return unwritten;
1335}
1336
Eric Anholt3de09aa2009-03-09 09:42:23 -07001337/**
1338 * This is the fast pwrite path, where we copy the data directly from the
1339 * user into the GTT, uncached.
Chris Wilsonfe115622016-10-28 13:58:40 +01001340 * @obj: i915 GEM object
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01001341 * @args: pwrite arguments structure
Eric Anholt3de09aa2009-03-09 09:42:23 -07001342 */
Eric Anholt673a3942008-07-30 12:06:12 -07001343static int
Chris Wilsonfe115622016-10-28 13:58:40 +01001344i915_gem_gtt_pwrite_fast(struct drm_i915_gem_object *obj,
1345 const struct drm_i915_gem_pwrite *args)
Eric Anholt673a3942008-07-30 12:06:12 -07001346{
Chris Wilsonfe115622016-10-28 13:58:40 +01001347 struct drm_i915_private *i915 = to_i915(obj->base.dev);
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301348 struct i915_ggtt *ggtt = &i915->ggtt;
1349 struct drm_mm_node node;
Chris Wilsonfe115622016-10-28 13:58:40 +01001350 struct i915_vma *vma;
1351 u64 remain, offset;
1352 void __user *user_data;
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301353 int ret;
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301354
Chris Wilsonfe115622016-10-28 13:58:40 +01001355 ret = mutex_lock_interruptible(&i915->drm.struct_mutex);
1356 if (ret)
1357 return ret;
Daniel Vetter935aaa62012-03-25 19:47:35 +02001358
Chris Wilson8bd818152017-10-19 07:37:33 +01001359 if (i915_gem_object_has_struct_page(obj)) {
1360 /*
1361 * Avoid waking the device up if we can fallback, as
1362 * waking/resuming is very slow (worst-case 10-100 ms
1363 * depending on PCI sleeps and our own resume time).
1364 * This easily dwarfs any performance advantage from
1365 * using the cache bypass of indirect GGTT access.
1366 */
1367 if (!intel_runtime_pm_get_if_in_use(i915)) {
1368 ret = -EFAULT;
1369 goto out_unlock;
1370 }
1371 } else {
1372 /* No backing pages, no fallback, we must force GGTT access */
1373 intel_runtime_pm_get(i915);
1374 }
1375
Chris Wilson058d88c2016-08-15 10:49:06 +01001376 vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0,
Chris Wilsona3259ca2017-10-09 09:44:00 +01001377 PIN_MAPPABLE |
1378 PIN_NONFAULT |
1379 PIN_NONBLOCK);
Chris Wilson18034582016-08-18 17:16:45 +01001380 if (!IS_ERR(vma)) {
1381 node.start = i915_ggtt_offset(vma);
1382 node.allocated = false;
Chris Wilson49ef5292016-08-18 17:17:00 +01001383 ret = i915_vma_put_fence(vma);
Chris Wilson18034582016-08-18 17:16:45 +01001384 if (ret) {
1385 i915_vma_unpin(vma);
1386 vma = ERR_PTR(ret);
1387 }
1388 }
Chris Wilson058d88c2016-08-15 10:49:06 +01001389 if (IS_ERR(vma)) {
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001390 ret = insert_mappable_node(ggtt, &node, PAGE_SIZE);
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301391 if (ret)
Chris Wilson8bd818152017-10-19 07:37:33 +01001392 goto out_rpm;
Chris Wilsonfe115622016-10-28 13:58:40 +01001393 GEM_BUG_ON(!node.allocated);
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301394 }
Daniel Vetter935aaa62012-03-25 19:47:35 +02001395
1396 ret = i915_gem_object_set_to_gtt_domain(obj, true);
1397 if (ret)
1398 goto out_unpin;
1399
Chris Wilsonfe115622016-10-28 13:58:40 +01001400 mutex_unlock(&i915->drm.struct_mutex);
1401
Chris Wilsonb19482d2016-08-18 17:16:43 +01001402 intel_fb_obj_invalidate(obj, ORIGIN_CPU);
Paulo Zanoni063e4e62015-02-13 17:23:45 -02001403
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301404 user_data = u64_to_user_ptr(args->data_ptr);
1405 offset = args->offset;
1406 remain = args->size;
1407 while (remain) {
Eric Anholt673a3942008-07-30 12:06:12 -07001408 /* Operation in this page
1409 *
Keith Packard0839ccb2008-10-30 19:38:48 -07001410 * page_base = page offset within aperture
1411 * page_offset = offset within page
1412 * page_length = bytes to copy for this page
Eric Anholt673a3942008-07-30 12:06:12 -07001413 */
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301414 u32 page_base = node.start;
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001415 unsigned int page_offset = offset_in_page(offset);
1416 unsigned int page_length = PAGE_SIZE - page_offset;
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301417 page_length = remain < page_length ? remain : page_length;
1418 if (node.allocated) {
1419 wmb(); /* flush the write before we modify the GGTT */
1420 ggtt->base.insert_page(&ggtt->base,
1421 i915_gem_object_get_dma_address(obj, offset >> PAGE_SHIFT),
1422 node.start, I915_CACHE_NONE, 0);
1423 wmb(); /* flush modifications to the GGTT (insert_page) */
1424 } else {
1425 page_base += offset & PAGE_MASK;
1426 }
Keith Packard0839ccb2008-10-30 19:38:48 -07001427 /* If we get a fault while copying data, then (presumably) our
Eric Anholt3de09aa2009-03-09 09:42:23 -07001428 * source page isn't available. Return the error and we'll
1429 * retry in the slow path.
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301430 * If the object is non-shmem backed, we retry again with the
1431 * path that handles page fault.
Keith Packard0839ccb2008-10-30 19:38:48 -07001432 */
Matthew Auld73ebd502017-12-11 15:18:20 +00001433 if (ggtt_write(&ggtt->iomap, page_base, page_offset,
Chris Wilsonfe115622016-10-28 13:58:40 +01001434 user_data, page_length)) {
1435 ret = -EFAULT;
1436 break;
Daniel Vetter935aaa62012-03-25 19:47:35 +02001437 }
Eric Anholt673a3942008-07-30 12:06:12 -07001438
Keith Packard0839ccb2008-10-30 19:38:48 -07001439 remain -= page_length;
1440 user_data += page_length;
1441 offset += page_length;
Eric Anholt673a3942008-07-30 12:06:12 -07001442 }
Chris Wilsond59b21e2017-02-22 11:40:49 +00001443 intel_fb_obj_flush(obj, ORIGIN_CPU);
Chris Wilsonfe115622016-10-28 13:58:40 +01001444
1445 mutex_lock(&i915->drm.struct_mutex);
Daniel Vetter935aaa62012-03-25 19:47:35 +02001446out_unpin:
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301447 if (node.allocated) {
1448 wmb();
1449 ggtt->base.clear_range(&ggtt->base,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02001450 node.start, node.size);
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301451 remove_mappable_node(&node);
1452 } else {
Chris Wilson058d88c2016-08-15 10:49:06 +01001453 i915_vma_unpin(vma);
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301454 }
Chris Wilson8bd818152017-10-19 07:37:33 +01001455out_rpm:
Chris Wilson9c870d02016-10-24 13:42:15 +01001456 intel_runtime_pm_put(i915);
Chris Wilson8bd818152017-10-19 07:37:33 +01001457out_unlock:
Chris Wilsonfe115622016-10-28 13:58:40 +01001458 mutex_unlock(&i915->drm.struct_mutex);
Eric Anholt3de09aa2009-03-09 09:42:23 -07001459 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07001460}
1461
Eric Anholt673a3942008-07-30 12:06:12 -07001462static int
Chris Wilsonfe115622016-10-28 13:58:40 +01001463shmem_pwrite_slow(struct page *page, int offset, int length,
Daniel Vetterd174bd62012-03-25 19:47:40 +02001464 char __user *user_data,
1465 bool page_do_bit17_swizzling,
1466 bool needs_clflush_before,
1467 bool needs_clflush_after)
Eric Anholt673a3942008-07-30 12:06:12 -07001468{
Daniel Vetterd174bd62012-03-25 19:47:40 +02001469 char *vaddr;
1470 int ret;
Eric Anholt40123c12009-03-09 13:42:30 -07001471
Daniel Vetterd174bd62012-03-25 19:47:40 +02001472 vaddr = kmap(page);
Daniel Vettere7e58eb2012-03-25 19:47:43 +02001473 if (unlikely(needs_clflush_before || page_do_bit17_swizzling))
Chris Wilsonfe115622016-10-28 13:58:40 +01001474 shmem_clflush_swizzled_range(vaddr + offset, length,
Daniel Vetter23c18c72012-03-25 19:47:42 +02001475 page_do_bit17_swizzling);
Daniel Vetterd174bd62012-03-25 19:47:40 +02001476 if (page_do_bit17_swizzling)
Chris Wilsonfe115622016-10-28 13:58:40 +01001477 ret = __copy_from_user_swizzled(vaddr, offset, user_data,
1478 length);
Daniel Vetterd174bd62012-03-25 19:47:40 +02001479 else
Chris Wilsonfe115622016-10-28 13:58:40 +01001480 ret = __copy_from_user(vaddr + offset, user_data, length);
Daniel Vetterd174bd62012-03-25 19:47:40 +02001481 if (needs_clflush_after)
Chris Wilsonfe115622016-10-28 13:58:40 +01001482 shmem_clflush_swizzled_range(vaddr + offset, length,
Daniel Vetter23c18c72012-03-25 19:47:42 +02001483 page_do_bit17_swizzling);
Daniel Vetterd174bd62012-03-25 19:47:40 +02001484 kunmap(page);
Chris Wilsone5281cc2010-10-28 13:45:36 +01001485
Chris Wilson755d2212012-09-04 21:02:55 +01001486 return ret ? -EFAULT : 0;
Eric Anholt40123c12009-03-09 13:42:30 -07001487}
1488
Chris Wilsonfe115622016-10-28 13:58:40 +01001489/* Per-page copy function for the shmem pwrite fastpath.
1490 * Flushes invalid cachelines before writing to the target if
1491 * needs_clflush_before is set and flushes out any written cachelines after
1492 * writing if needs_clflush is set.
1493 */
Eric Anholt40123c12009-03-09 13:42:30 -07001494static int
Chris Wilsonfe115622016-10-28 13:58:40 +01001495shmem_pwrite(struct page *page, int offset, int len, char __user *user_data,
1496 bool page_do_bit17_swizzling,
1497 bool needs_clflush_before,
1498 bool needs_clflush_after)
Eric Anholt40123c12009-03-09 13:42:30 -07001499{
Chris Wilsonfe115622016-10-28 13:58:40 +01001500 int ret;
Eric Anholt40123c12009-03-09 13:42:30 -07001501
Chris Wilsonfe115622016-10-28 13:58:40 +01001502 ret = -ENODEV;
1503 if (!page_do_bit17_swizzling) {
1504 char *vaddr = kmap_atomic(page);
1505
1506 if (needs_clflush_before)
1507 drm_clflush_virt_range(vaddr + offset, len);
1508 ret = __copy_from_user_inatomic(vaddr + offset, user_data, len);
1509 if (needs_clflush_after)
1510 drm_clflush_virt_range(vaddr + offset, len);
1511
1512 kunmap_atomic(vaddr);
1513 }
1514 if (ret == 0)
1515 return ret;
1516
1517 return shmem_pwrite_slow(page, offset, len, user_data,
1518 page_do_bit17_swizzling,
1519 needs_clflush_before,
1520 needs_clflush_after);
1521}
1522
1523static int
1524i915_gem_shmem_pwrite(struct drm_i915_gem_object *obj,
1525 const struct drm_i915_gem_pwrite *args)
1526{
1527 struct drm_i915_private *i915 = to_i915(obj->base.dev);
1528 void __user *user_data;
1529 u64 remain;
1530 unsigned int obj_do_bit17_swizzling;
1531 unsigned int partial_cacheline_write;
1532 unsigned int needs_clflush;
1533 unsigned int offset, idx;
1534 int ret;
1535
1536 ret = mutex_lock_interruptible(&i915->drm.struct_mutex);
Chris Wilson43394c72016-08-18 17:16:47 +01001537 if (ret)
1538 return ret;
Eric Anholt40123c12009-03-09 13:42:30 -07001539
Chris Wilsonfe115622016-10-28 13:58:40 +01001540 ret = i915_gem_obj_prepare_shmem_write(obj, &needs_clflush);
1541 mutex_unlock(&i915->drm.struct_mutex);
1542 if (ret)
1543 return ret;
1544
1545 obj_do_bit17_swizzling = 0;
1546 if (i915_gem_object_needs_bit17_swizzle(obj))
1547 obj_do_bit17_swizzling = BIT(17);
1548
1549 /* If we don't overwrite a cacheline completely we need to be
1550 * careful to have up-to-date data by first clflushing. Don't
1551 * overcomplicate things and flush the entire patch.
1552 */
1553 partial_cacheline_write = 0;
1554 if (needs_clflush & CLFLUSH_BEFORE)
1555 partial_cacheline_write = boot_cpu_data.x86_clflush_size - 1;
1556
Chris Wilson43394c72016-08-18 17:16:47 +01001557 user_data = u64_to_user_ptr(args->data_ptr);
Chris Wilson43394c72016-08-18 17:16:47 +01001558 remain = args->size;
Chris Wilsonfe115622016-10-28 13:58:40 +01001559 offset = offset_in_page(args->offset);
1560 for (idx = args->offset >> PAGE_SHIFT; remain; idx++) {
1561 struct page *page = i915_gem_object_get_page(obj, idx);
1562 int length;
Eric Anholt40123c12009-03-09 13:42:30 -07001563
Chris Wilsonfe115622016-10-28 13:58:40 +01001564 length = remain;
1565 if (offset + length > PAGE_SIZE)
1566 length = PAGE_SIZE - offset;
Chris Wilsone5281cc2010-10-28 13:45:36 +01001567
Chris Wilsonfe115622016-10-28 13:58:40 +01001568 ret = shmem_pwrite(page, offset, length, user_data,
1569 page_to_phys(page) & obj_do_bit17_swizzling,
1570 (offset | length) & partial_cacheline_write,
1571 needs_clflush & CLFLUSH_AFTER);
1572 if (ret)
Chris Wilson9da3da62012-06-01 15:20:22 +01001573 break;
1574
Chris Wilsonfe115622016-10-28 13:58:40 +01001575 remain -= length;
1576 user_data += length;
1577 offset = 0;
Eric Anholt40123c12009-03-09 13:42:30 -07001578 }
1579
Chris Wilsond59b21e2017-02-22 11:40:49 +00001580 intel_fb_obj_flush(obj, ORIGIN_CPU);
Chris Wilsonfe115622016-10-28 13:58:40 +01001581 i915_gem_obj_finish_shmem_access(obj);
Eric Anholt40123c12009-03-09 13:42:30 -07001582 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07001583}
1584
1585/**
1586 * Writes data to the object referenced by handle.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01001587 * @dev: drm device
1588 * @data: ioctl data blob
1589 * @file: drm file
Eric Anholt673a3942008-07-30 12:06:12 -07001590 *
1591 * On error, the contents of the buffer that were to be modified are undefined.
1592 */
1593int
1594i915_gem_pwrite_ioctl(struct drm_device *dev, void *data,
Chris Wilsonfbd5a262010-10-14 15:03:58 +01001595 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001596{
1597 struct drm_i915_gem_pwrite *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00001598 struct drm_i915_gem_object *obj;
Chris Wilson51311d02010-11-17 09:10:42 +00001599 int ret;
1600
1601 if (args->size == 0)
1602 return 0;
1603
1604 if (!access_ok(VERIFY_READ,
Gustavo Padovan3ed605b2016-04-26 12:32:27 -03001605 u64_to_user_ptr(args->data_ptr),
Chris Wilson51311d02010-11-17 09:10:42 +00001606 args->size))
1607 return -EFAULT;
1608
Chris Wilson03ac0642016-07-20 13:31:51 +01001609 obj = i915_gem_object_lookup(file, args->handle);
Chris Wilson258a5ed2016-08-05 10:14:16 +01001610 if (!obj)
1611 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07001612
Chris Wilson7dcd2492010-09-26 20:21:44 +01001613 /* Bounds check destination. */
Matthew Auld966d5bf2016-12-13 20:32:22 +00001614 if (range_overflows_t(u64, args->offset, args->size, obj->base.size)) {
Chris Wilsonce9d4192010-09-26 20:50:05 +01001615 ret = -EINVAL;
Chris Wilson258a5ed2016-08-05 10:14:16 +01001616 goto err;
Chris Wilsonce9d4192010-09-26 20:50:05 +01001617 }
1618
Chris Wilsondb53a302011-02-03 11:57:46 +00001619 trace_i915_gem_object_pwrite(obj, args->offset, args->size);
1620
Chris Wilson7c55e2c2017-03-07 12:03:38 +00001621 ret = -ENODEV;
1622 if (obj->ops->pwrite)
1623 ret = obj->ops->pwrite(obj, args);
1624 if (ret != -ENODEV)
1625 goto err;
1626
Chris Wilsone95433c2016-10-28 13:58:27 +01001627 ret = i915_gem_object_wait(obj,
1628 I915_WAIT_INTERRUPTIBLE |
1629 I915_WAIT_ALL,
1630 MAX_SCHEDULE_TIMEOUT,
1631 to_rps_client(file));
Chris Wilson258a5ed2016-08-05 10:14:16 +01001632 if (ret)
1633 goto err;
1634
Chris Wilsonfe115622016-10-28 13:58:40 +01001635 ret = i915_gem_object_pin_pages(obj);
Chris Wilson258a5ed2016-08-05 10:14:16 +01001636 if (ret)
Chris Wilsonfe115622016-10-28 13:58:40 +01001637 goto err;
Chris Wilson258a5ed2016-08-05 10:14:16 +01001638
Daniel Vetter935aaa62012-03-25 19:47:35 +02001639 ret = -EFAULT;
Eric Anholt673a3942008-07-30 12:06:12 -07001640 /* We can only do the GTT pwrite on untiled buffers, as otherwise
1641 * it would end up going through the fenced access, and we'll get
1642 * different detiling behavior between reading and writing.
1643 * pread/pwrite currently are reading and writing from the CPU
1644 * perspective, requiring manual detiling by the client.
1645 */
Chris Wilson6eae0052016-06-20 15:05:52 +01001646 if (!i915_gem_object_has_struct_page(obj) ||
Chris Wilson9c870d02016-10-24 13:42:15 +01001647 cpu_write_needs_clflush(obj))
Daniel Vetter935aaa62012-03-25 19:47:35 +02001648 /* Note that the gtt paths might fail with non-page-backed user
1649 * pointers (e.g. gtt mappings when moving data between
Chris Wilson9c870d02016-10-24 13:42:15 +01001650 * textures). Fallback to the shmem path in that case.
1651 */
Chris Wilsonfe115622016-10-28 13:58:40 +01001652 ret = i915_gem_gtt_pwrite_fast(obj, args);
Eric Anholt673a3942008-07-30 12:06:12 -07001653
Chris Wilsond1054ee2016-07-16 18:42:36 +01001654 if (ret == -EFAULT || ret == -ENOSPC) {
Chris Wilson6a2c4232014-11-04 04:51:40 -08001655 if (obj->phys_handle)
1656 ret = i915_gem_phys_pwrite(obj, args, file);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301657 else
Chris Wilsonfe115622016-10-28 13:58:40 +01001658 ret = i915_gem_shmem_pwrite(obj, args);
Chris Wilson6a2c4232014-11-04 04:51:40 -08001659 }
Daniel Vetter5c0480f2011-12-14 13:57:30 +01001660
Chris Wilsonfe115622016-10-28 13:58:40 +01001661 i915_gem_object_unpin_pages(obj);
Chris Wilson258a5ed2016-08-05 10:14:16 +01001662err:
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001663 i915_gem_object_put(obj);
Chris Wilson258a5ed2016-08-05 10:14:16 +01001664 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07001665}
1666
Chris Wilson40e62d52016-10-28 13:58:41 +01001667static void i915_gem_object_bump_inactive_ggtt(struct drm_i915_gem_object *obj)
1668{
1669 struct drm_i915_private *i915;
1670 struct list_head *list;
1671 struct i915_vma *vma;
1672
Chris Wilsonf2123812017-10-16 12:40:37 +01001673 GEM_BUG_ON(!i915_gem_object_has_pinned_pages(obj));
1674
Chris Wilsone2189dd2017-12-07 21:14:07 +00001675 for_each_ggtt_vma(vma, obj) {
Chris Wilson40e62d52016-10-28 13:58:41 +01001676 if (i915_vma_is_active(vma))
1677 continue;
1678
1679 if (!drm_mm_node_allocated(&vma->node))
1680 continue;
1681
1682 list_move_tail(&vma->vm_link, &vma->vm->inactive_list);
1683 }
1684
1685 i915 = to_i915(obj->base.dev);
Chris Wilsonf2123812017-10-16 12:40:37 +01001686 spin_lock(&i915->mm.obj_lock);
Chris Wilson40e62d52016-10-28 13:58:41 +01001687 list = obj->bind_count ? &i915->mm.bound_list : &i915->mm.unbound_list;
Chris Wilsonf2123812017-10-16 12:40:37 +01001688 list_move_tail(&obj->mm.link, list);
1689 spin_unlock(&i915->mm.obj_lock);
Chris Wilson40e62d52016-10-28 13:58:41 +01001690}
1691
Eric Anholt673a3942008-07-30 12:06:12 -07001692/**
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001693 * Called when user space prepares to use an object with the CPU, either
1694 * through the mmap ioctl's mapping or a GTT mapping.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01001695 * @dev: drm device
1696 * @data: ioctl data blob
1697 * @file: drm file
Eric Anholt673a3942008-07-30 12:06:12 -07001698 */
1699int
1700i915_gem_set_domain_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00001701 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001702{
1703 struct drm_i915_gem_set_domain *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00001704 struct drm_i915_gem_object *obj;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001705 uint32_t read_domains = args->read_domains;
1706 uint32_t write_domain = args->write_domain;
Chris Wilson40e62d52016-10-28 13:58:41 +01001707 int err;
Eric Anholt673a3942008-07-30 12:06:12 -07001708
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001709 /* Only handle setting domains to types used by the CPU. */
Chris Wilsonb8f90962016-08-05 10:14:07 +01001710 if ((write_domain | read_domains) & I915_GEM_GPU_DOMAINS)
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001711 return -EINVAL;
1712
1713 /* Having something in the write domain implies it's in the read
1714 * domain, and only that read domain. Enforce that in the request.
1715 */
1716 if (write_domain != 0 && read_domains != write_domain)
1717 return -EINVAL;
1718
Chris Wilson03ac0642016-07-20 13:31:51 +01001719 obj = i915_gem_object_lookup(file, args->handle);
Chris Wilsonb8f90962016-08-05 10:14:07 +01001720 if (!obj)
1721 return -ENOENT;
Jesse Barnes652c3932009-08-17 13:31:43 -07001722
Chris Wilson3236f572012-08-24 09:35:09 +01001723 /* Try to flush the object off the GPU without holding the lock.
1724 * We will repeat the flush holding the lock in the normal manner
1725 * to catch cases where we are gazumped.
1726 */
Chris Wilson40e62d52016-10-28 13:58:41 +01001727 err = i915_gem_object_wait(obj,
Chris Wilsone95433c2016-10-28 13:58:27 +01001728 I915_WAIT_INTERRUPTIBLE |
1729 (write_domain ? I915_WAIT_ALL : 0),
1730 MAX_SCHEDULE_TIMEOUT,
1731 to_rps_client(file));
Chris Wilson40e62d52016-10-28 13:58:41 +01001732 if (err)
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001733 goto out;
Chris Wilsonb8f90962016-08-05 10:14:07 +01001734
Tina Zhanga03f3952017-11-14 10:25:13 +00001735 /*
1736 * Proxy objects do not control access to the backing storage, ergo
1737 * they cannot be used as a means to manipulate the cache domain
1738 * tracking for that backing storage. The proxy object is always
1739 * considered to be outside of any cache domain.
1740 */
1741 if (i915_gem_object_is_proxy(obj)) {
1742 err = -ENXIO;
1743 goto out;
1744 }
1745
1746 /*
1747 * Flush and acquire obj->pages so that we are coherent through
Chris Wilson40e62d52016-10-28 13:58:41 +01001748 * direct access in memory with previous cached writes through
1749 * shmemfs and that our cache domain tracking remains valid.
1750 * For example, if the obj->filp was moved to swap without us
1751 * being notified and releasing the pages, we would mistakenly
1752 * continue to assume that the obj remained out of the CPU cached
1753 * domain.
1754 */
1755 err = i915_gem_object_pin_pages(obj);
1756 if (err)
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001757 goto out;
Chris Wilson40e62d52016-10-28 13:58:41 +01001758
1759 err = i915_mutex_lock_interruptible(dev);
1760 if (err)
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001761 goto out_unpin;
Chris Wilson3236f572012-08-24 09:35:09 +01001762
Chris Wilsone22d8e32017-04-12 12:01:11 +01001763 if (read_domains & I915_GEM_DOMAIN_WC)
1764 err = i915_gem_object_set_to_wc_domain(obj, write_domain);
1765 else if (read_domains & I915_GEM_DOMAIN_GTT)
1766 err = i915_gem_object_set_to_gtt_domain(obj, write_domain);
Chris Wilson43566de2015-01-02 16:29:29 +05301767 else
Chris Wilsone22d8e32017-04-12 12:01:11 +01001768 err = i915_gem_object_set_to_cpu_domain(obj, write_domain);
Chris Wilson40e62d52016-10-28 13:58:41 +01001769
1770 /* And bump the LRU for this access */
1771 i915_gem_object_bump_inactive_ggtt(obj);
1772
1773 mutex_unlock(&dev->struct_mutex);
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001774
Daniel Vetter031b6982015-06-26 19:35:16 +02001775 if (write_domain != 0)
Chris Wilsonef749212017-04-12 12:01:10 +01001776 intel_fb_obj_invalidate(obj,
1777 fb_write_origin(obj, write_domain));
Daniel Vetter031b6982015-06-26 19:35:16 +02001778
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001779out_unpin:
Chris Wilson40e62d52016-10-28 13:58:41 +01001780 i915_gem_object_unpin_pages(obj);
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001781out:
1782 i915_gem_object_put(obj);
Chris Wilson40e62d52016-10-28 13:58:41 +01001783 return err;
Eric Anholt673a3942008-07-30 12:06:12 -07001784}
1785
1786/**
1787 * Called when user space has done writes to this buffer
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01001788 * @dev: drm device
1789 * @data: ioctl data blob
1790 * @file: drm file
Eric Anholt673a3942008-07-30 12:06:12 -07001791 */
1792int
1793i915_gem_sw_finish_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00001794 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001795{
1796 struct drm_i915_gem_sw_finish *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00001797 struct drm_i915_gem_object *obj;
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001798
Chris Wilson03ac0642016-07-20 13:31:51 +01001799 obj = i915_gem_object_lookup(file, args->handle);
Chris Wilsonc21724c2016-08-05 10:14:19 +01001800 if (!obj)
1801 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07001802
Tina Zhanga03f3952017-11-14 10:25:13 +00001803 /*
1804 * Proxy objects are barred from CPU access, so there is no
1805 * need to ban sw_finish as it is a nop.
1806 */
1807
Eric Anholt673a3942008-07-30 12:06:12 -07001808 /* Pinned buffers may be scanout, so flush the cache */
Chris Wilson5a97bcc2017-02-22 11:40:46 +00001809 i915_gem_object_flush_if_display(obj);
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001810 i915_gem_object_put(obj);
Chris Wilson5a97bcc2017-02-22 11:40:46 +00001811
1812 return 0;
Eric Anholt673a3942008-07-30 12:06:12 -07001813}
1814
1815/**
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01001816 * i915_gem_mmap_ioctl - Maps the contents of an object, returning the address
1817 * it is mapped to.
1818 * @dev: drm device
1819 * @data: ioctl data blob
1820 * @file: drm file
Eric Anholt673a3942008-07-30 12:06:12 -07001821 *
1822 * While the mapping holds a reference on the contents of the object, it doesn't
1823 * imply a ref on the object itself.
Daniel Vetter34367382014-10-16 12:28:18 +02001824 *
1825 * IMPORTANT:
1826 *
1827 * DRM driver writers who look a this function as an example for how to do GEM
1828 * mmap support, please don't implement mmap support like here. The modern way
1829 * to implement DRM mmap support is with an mmap offset ioctl (like
1830 * i915_gem_mmap_gtt) and then using the mmap syscall on the DRM fd directly.
1831 * That way debug tooling like valgrind will understand what's going on, hiding
1832 * the mmap call in a driver private ioctl will break that. The i915 driver only
1833 * does cpu mmaps this way because we didn't know better.
Eric Anholt673a3942008-07-30 12:06:12 -07001834 */
1835int
1836i915_gem_mmap_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00001837 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001838{
1839 struct drm_i915_gem_mmap *args = data;
Chris Wilson03ac0642016-07-20 13:31:51 +01001840 struct drm_i915_gem_object *obj;
Eric Anholt673a3942008-07-30 12:06:12 -07001841 unsigned long addr;
1842
Akash Goel1816f922015-01-02 16:29:30 +05301843 if (args->flags & ~(I915_MMAP_WC))
1844 return -EINVAL;
1845
Borislav Petkov568a58e2016-03-29 17:42:01 +02001846 if (args->flags & I915_MMAP_WC && !boot_cpu_has(X86_FEATURE_PAT))
Akash Goel1816f922015-01-02 16:29:30 +05301847 return -ENODEV;
1848
Chris Wilson03ac0642016-07-20 13:31:51 +01001849 obj = i915_gem_object_lookup(file, args->handle);
1850 if (!obj)
Chris Wilsonbf79cb92010-08-04 14:19:46 +01001851 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07001852
Daniel Vetter1286ff72012-05-10 15:25:09 +02001853 /* prime objects have no backing filp to GEM mmap
1854 * pages from.
1855 */
Chris Wilson03ac0642016-07-20 13:31:51 +01001856 if (!obj->base.filp) {
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001857 i915_gem_object_put(obj);
Tina Zhang274b2462017-11-14 10:25:12 +00001858 return -ENXIO;
Daniel Vetter1286ff72012-05-10 15:25:09 +02001859 }
1860
Chris Wilson03ac0642016-07-20 13:31:51 +01001861 addr = vm_mmap(obj->base.filp, 0, args->size,
Eric Anholt673a3942008-07-30 12:06:12 -07001862 PROT_READ | PROT_WRITE, MAP_SHARED,
1863 args->offset);
Akash Goel1816f922015-01-02 16:29:30 +05301864 if (args->flags & I915_MMAP_WC) {
1865 struct mm_struct *mm = current->mm;
1866 struct vm_area_struct *vma;
1867
Michal Hocko80a89a52016-05-23 16:26:11 -07001868 if (down_write_killable(&mm->mmap_sem)) {
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001869 i915_gem_object_put(obj);
Michal Hocko80a89a52016-05-23 16:26:11 -07001870 return -EINTR;
1871 }
Akash Goel1816f922015-01-02 16:29:30 +05301872 vma = find_vma(mm, addr);
1873 if (vma)
1874 vma->vm_page_prot =
1875 pgprot_writecombine(vm_get_page_prot(vma->vm_flags));
1876 else
1877 addr = -ENOMEM;
1878 up_write(&mm->mmap_sem);
Chris Wilsonaeecc962016-06-17 14:46:39 -03001879
1880 /* This may race, but that's ok, it only gets set */
Chris Wilson50349242016-08-18 17:17:04 +01001881 WRITE_ONCE(obj->frontbuffer_ggtt_origin, ORIGIN_CPU);
Akash Goel1816f922015-01-02 16:29:30 +05301882 }
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001883 i915_gem_object_put(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001884 if (IS_ERR((void *)addr))
1885 return addr;
1886
1887 args->addr_ptr = (uint64_t) addr;
1888
1889 return 0;
1890}
1891
Chris Wilson03af84f2016-08-18 17:17:01 +01001892static unsigned int tile_row_pages(struct drm_i915_gem_object *obj)
1893{
Chris Wilson6649a0b2017-01-09 16:16:08 +00001894 return i915_gem_object_get_tile_row_size(obj) >> PAGE_SHIFT;
Chris Wilson03af84f2016-08-18 17:17:01 +01001895}
1896
Jesse Barnesde151cf2008-11-12 10:03:55 -08001897/**
Chris Wilson4cc69072016-08-25 19:05:19 +01001898 * i915_gem_mmap_gtt_version - report the current feature set for GTT mmaps
1899 *
1900 * A history of the GTT mmap interface:
1901 *
1902 * 0 - Everything had to fit into the GTT. Both parties of a memcpy had to
1903 * aligned and suitable for fencing, and still fit into the available
1904 * mappable space left by the pinned display objects. A classic problem
1905 * we called the page-fault-of-doom where we would ping-pong between
1906 * two objects that could not fit inside the GTT and so the memcpy
1907 * would page one object in at the expense of the other between every
1908 * single byte.
1909 *
1910 * 1 - Objects can be any size, and have any compatible fencing (X Y, or none
1911 * as set via i915_gem_set_tiling() [DRM_I915_GEM_SET_TILING]). If the
1912 * object is too large for the available space (or simply too large
1913 * for the mappable aperture!), a view is created instead and faulted
1914 * into userspace. (This view is aligned and sized appropriately for
1915 * fenced access.)
1916 *
Chris Wilsone22d8e32017-04-12 12:01:11 +01001917 * 2 - Recognise WC as a separate cache domain so that we can flush the
1918 * delayed writes via GTT before performing direct access via WC.
1919 *
Chris Wilson4cc69072016-08-25 19:05:19 +01001920 * Restrictions:
1921 *
1922 * * snoopable objects cannot be accessed via the GTT. It can cause machine
1923 * hangs on some architectures, corruption on others. An attempt to service
1924 * a GTT page fault from a snoopable object will generate a SIGBUS.
1925 *
1926 * * the object must be able to fit into RAM (physical memory, though no
1927 * limited to the mappable aperture).
1928 *
1929 *
1930 * Caveats:
1931 *
1932 * * a new GTT page fault will synchronize rendering from the GPU and flush
1933 * all data to system memory. Subsequent access will not be synchronized.
1934 *
1935 * * all mappings are revoked on runtime device suspend.
1936 *
1937 * * there are only 8, 16 or 32 fence registers to share between all users
1938 * (older machines require fence register for display and blitter access
1939 * as well). Contention of the fence registers will cause the previous users
1940 * to be unmapped and any new access will generate new page faults.
1941 *
1942 * * running out of memory while servicing a fault may generate a SIGBUS,
1943 * rather than the expected SIGSEGV.
1944 */
1945int i915_gem_mmap_gtt_version(void)
1946{
Chris Wilsone22d8e32017-04-12 12:01:11 +01001947 return 2;
Chris Wilson4cc69072016-08-25 19:05:19 +01001948}
1949
Chris Wilson2d4281b2017-01-10 09:56:32 +00001950static inline struct i915_ggtt_view
1951compute_partial_view(struct drm_i915_gem_object *obj,
Chris Wilson2d4281b2017-01-10 09:56:32 +00001952 pgoff_t page_offset,
1953 unsigned int chunk)
1954{
1955 struct i915_ggtt_view view;
1956
1957 if (i915_gem_object_is_tiled(obj))
1958 chunk = roundup(chunk, tile_row_pages(obj));
1959
Chris Wilson2d4281b2017-01-10 09:56:32 +00001960 view.type = I915_GGTT_VIEW_PARTIAL;
Chris Wilson8bab11932017-01-14 00:28:25 +00001961 view.partial.offset = rounddown(page_offset, chunk);
1962 view.partial.size =
Chris Wilson2d4281b2017-01-10 09:56:32 +00001963 min_t(unsigned int, chunk,
Chris Wilson8bab11932017-01-14 00:28:25 +00001964 (obj->base.size >> PAGE_SHIFT) - view.partial.offset);
Chris Wilson2d4281b2017-01-10 09:56:32 +00001965
1966 /* If the partial covers the entire object, just create a normal VMA. */
1967 if (chunk >= obj->base.size >> PAGE_SHIFT)
1968 view.type = I915_GGTT_VIEW_NORMAL;
1969
1970 return view;
1971}
1972
Chris Wilson4cc69072016-08-25 19:05:19 +01001973/**
Jesse Barnesde151cf2008-11-12 10:03:55 -08001974 * i915_gem_fault - fault a page into the GTT
Geliang Tangd9072a32015-09-15 05:58:44 -07001975 * @vmf: fault info
Jesse Barnesde151cf2008-11-12 10:03:55 -08001976 *
1977 * The fault handler is set up by drm_gem_mmap() when a object is GTT mapped
1978 * from userspace. The fault handler takes care of binding the object to
1979 * the GTT (if needed), allocating and programming a fence register (again,
1980 * only if needed based on whether the old reg is still valid or the object
1981 * is tiled) and inserting a new PTE into the faulting process.
1982 *
1983 * Note that the faulting process may involve evicting existing objects
1984 * from the GTT and/or fence registers to make room. So performance may
1985 * suffer if the GTT working set is large or there are few fence registers
1986 * left.
Chris Wilson4cc69072016-08-25 19:05:19 +01001987 *
1988 * The current feature set supported by i915_gem_fault() and thus GTT mmaps
1989 * is exposed via I915_PARAM_MMAP_GTT_VERSION (see i915_gem_mmap_gtt_version).
Jesse Barnesde151cf2008-11-12 10:03:55 -08001990 */
Dave Jiang11bac802017-02-24 14:56:41 -08001991int i915_gem_fault(struct vm_fault *vmf)
Jesse Barnesde151cf2008-11-12 10:03:55 -08001992{
Chris Wilson03af84f2016-08-18 17:17:01 +01001993#define MIN_CHUNK_PAGES ((1 << 20) >> PAGE_SHIFT) /* 1 MiB */
Dave Jiang11bac802017-02-24 14:56:41 -08001994 struct vm_area_struct *area = vmf->vma;
Chris Wilson058d88c2016-08-15 10:49:06 +01001995 struct drm_i915_gem_object *obj = to_intel_bo(area->vm_private_data);
Chris Wilson05394f32010-11-08 19:18:58 +00001996 struct drm_device *dev = obj->base.dev;
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03001997 struct drm_i915_private *dev_priv = to_i915(dev);
1998 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Chris Wilsonb8f90962016-08-05 10:14:07 +01001999 bool write = !!(vmf->flags & FAULT_FLAG_WRITE);
Chris Wilson058d88c2016-08-15 10:49:06 +01002000 struct i915_vma *vma;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002001 pgoff_t page_offset;
Chris Wilson82118872016-08-18 17:17:05 +01002002 unsigned int flags;
Chris Wilsonb8f90962016-08-05 10:14:07 +01002003 int ret;
Paulo Zanonif65c9162013-11-27 18:20:34 -02002004
Jesse Barnesde151cf2008-11-12 10:03:55 -08002005 /* We don't use vmf->pgoff since that has the fake offset */
Jan Kara1a29d852016-12-14 15:07:01 -08002006 page_offset = (vmf->address - area->vm_start) >> PAGE_SHIFT;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002007
Chris Wilsondb53a302011-02-03 11:57:46 +00002008 trace_i915_gem_object_fault(obj, page_offset, true, write);
2009
Chris Wilson6e4930f2014-02-07 18:37:06 -02002010 /* Try to flush the object off the GPU first without holding the lock.
Chris Wilsonb8f90962016-08-05 10:14:07 +01002011 * Upon acquiring the lock, we will perform our sanity checks and then
Chris Wilson6e4930f2014-02-07 18:37:06 -02002012 * repeat the flush holding the lock in the normal manner to catch cases
2013 * where we are gazumped.
2014 */
Chris Wilsone95433c2016-10-28 13:58:27 +01002015 ret = i915_gem_object_wait(obj,
2016 I915_WAIT_INTERRUPTIBLE,
2017 MAX_SCHEDULE_TIMEOUT,
2018 NULL);
Chris Wilson6e4930f2014-02-07 18:37:06 -02002019 if (ret)
Chris Wilsonb8f90962016-08-05 10:14:07 +01002020 goto err;
2021
Chris Wilson40e62d52016-10-28 13:58:41 +01002022 ret = i915_gem_object_pin_pages(obj);
2023 if (ret)
2024 goto err;
2025
Chris Wilsonb8f90962016-08-05 10:14:07 +01002026 intel_runtime_pm_get(dev_priv);
2027
2028 ret = i915_mutex_lock_interruptible(dev);
2029 if (ret)
2030 goto err_rpm;
Chris Wilson6e4930f2014-02-07 18:37:06 -02002031
Chris Wilsoneb119bd2012-12-16 12:43:36 +00002032 /* Access to snoopable pages through the GTT is incoherent. */
Tvrtko Ursulin0031fb92016-11-04 14:42:44 +00002033 if (obj->cache_level != I915_CACHE_NONE && !HAS_LLC(dev_priv)) {
Chris Wilsonddeff6e2014-05-28 16:16:41 +01002034 ret = -EFAULT;
Chris Wilsonb8f90962016-08-05 10:14:07 +01002035 goto err_unlock;
Chris Wilsoneb119bd2012-12-16 12:43:36 +00002036 }
2037
Chris Wilson82118872016-08-18 17:17:05 +01002038 /* If the object is smaller than a couple of partial vma, it is
2039 * not worth only creating a single partial vma - we may as well
2040 * clear enough space for the full object.
2041 */
2042 flags = PIN_MAPPABLE;
2043 if (obj->base.size > 2 * MIN_CHUNK_PAGES << PAGE_SHIFT)
2044 flags |= PIN_NONBLOCK | PIN_NONFAULT;
2045
Chris Wilsona61007a2016-08-18 17:17:02 +01002046 /* Now pin it into the GTT as needed */
Chris Wilson82118872016-08-18 17:17:05 +01002047 vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0, flags);
Chris Wilsona61007a2016-08-18 17:17:02 +01002048 if (IS_ERR(vma)) {
Chris Wilsona61007a2016-08-18 17:17:02 +01002049 /* Use a partial view if it is bigger than available space */
Chris Wilson2d4281b2017-01-10 09:56:32 +00002050 struct i915_ggtt_view view =
Chris Wilson8201c1f2017-01-10 09:56:33 +00002051 compute_partial_view(obj, page_offset, MIN_CHUNK_PAGES);
Chris Wilsonaa136d92016-08-18 17:17:03 +01002052
Chris Wilson50349242016-08-18 17:17:04 +01002053 /* Userspace is now writing through an untracked VMA, abandon
2054 * all hope that the hardware is able to track future writes.
2055 */
2056 obj->frontbuffer_ggtt_origin = ORIGIN_CPU;
2057
Chris Wilsona61007a2016-08-18 17:17:02 +01002058 vma = i915_gem_object_ggtt_pin(obj, &view, 0, 0, PIN_MAPPABLE);
2059 }
Chris Wilson058d88c2016-08-15 10:49:06 +01002060 if (IS_ERR(vma)) {
2061 ret = PTR_ERR(vma);
Chris Wilsonb8f90962016-08-05 10:14:07 +01002062 goto err_unlock;
Chris Wilson058d88c2016-08-15 10:49:06 +01002063 }
Jesse Barnesde151cf2008-11-12 10:03:55 -08002064
Chris Wilsonc9839302012-11-20 10:45:17 +00002065 ret = i915_gem_object_set_to_gtt_domain(obj, write);
2066 if (ret)
Chris Wilsonb8f90962016-08-05 10:14:07 +01002067 goto err_unpin;
Chris Wilsonc9839302012-11-20 10:45:17 +00002068
Chris Wilson3bd40732017-10-09 09:43:56 +01002069 ret = i915_vma_pin_fence(vma);
Chris Wilsonc9839302012-11-20 10:45:17 +00002070 if (ret)
Chris Wilsonb8f90962016-08-05 10:14:07 +01002071 goto err_unpin;
Chris Wilson7d1c4802010-08-07 21:45:03 +01002072
Chris Wilsonb90b91d2014-06-10 12:14:40 +01002073 /* Finally, remap it using the new GTT offset */
Chris Wilsonc58305a2016-08-19 16:54:28 +01002074 ret = remap_io_mapping(area,
Chris Wilson8bab11932017-01-14 00:28:25 +00002075 area->vm_start + (vma->ggtt_view.partial.offset << PAGE_SHIFT),
Matthew Auld73ebd502017-12-11 15:18:20 +00002076 (ggtt->gmadr.start + vma->node.start) >> PAGE_SHIFT,
Chris Wilsonc58305a2016-08-19 16:54:28 +01002077 min_t(u64, vma->size, area->vm_end - area->vm_start),
Matthew Auld73ebd502017-12-11 15:18:20 +00002078 &ggtt->iomap);
Chris Wilsona65adaf2017-10-09 09:43:57 +01002079 if (ret)
2080 goto err_fence;
Chris Wilsona61007a2016-08-18 17:17:02 +01002081
Chris Wilsona65adaf2017-10-09 09:43:57 +01002082 /* Mark as being mmapped into userspace for later revocation */
2083 assert_rpm_wakelock_held(dev_priv);
2084 if (!i915_vma_set_userfault(vma) && !obj->userfault_count++)
2085 list_add(&obj->userfault_link, &dev_priv->mm.userfault_list);
2086 GEM_BUG_ON(!obj->userfault_count);
2087
Chris Wilson7125397b2017-12-06 12:49:14 +00002088 i915_vma_set_ggtt_write(vma);
2089
Chris Wilsona65adaf2017-10-09 09:43:57 +01002090err_fence:
Chris Wilson3bd40732017-10-09 09:43:56 +01002091 i915_vma_unpin_fence(vma);
Chris Wilsonb8f90962016-08-05 10:14:07 +01002092err_unpin:
Chris Wilson058d88c2016-08-15 10:49:06 +01002093 __i915_vma_unpin(vma);
Chris Wilsonb8f90962016-08-05 10:14:07 +01002094err_unlock:
Jesse Barnesde151cf2008-11-12 10:03:55 -08002095 mutex_unlock(&dev->struct_mutex);
Chris Wilsonb8f90962016-08-05 10:14:07 +01002096err_rpm:
2097 intel_runtime_pm_put(dev_priv);
Chris Wilson40e62d52016-10-28 13:58:41 +01002098 i915_gem_object_unpin_pages(obj);
Chris Wilsonb8f90962016-08-05 10:14:07 +01002099err:
Jesse Barnesde151cf2008-11-12 10:03:55 -08002100 switch (ret) {
Chris Wilsond9bc7e92011-02-07 13:09:31 +00002101 case -EIO:
Daniel Vetter2232f032014-09-04 09:36:18 +02002102 /*
2103 * We eat errors when the gpu is terminally wedged to avoid
2104 * userspace unduly crashing (gl has no provisions for mmaps to
2105 * fail). But any other -EIO isn't ours (e.g. swap in failure)
2106 * and so needs to be reported.
2107 */
2108 if (!i915_terminally_wedged(&dev_priv->gpu_error)) {
Paulo Zanonif65c9162013-11-27 18:20:34 -02002109 ret = VM_FAULT_SIGBUS;
2110 break;
2111 }
Chris Wilson045e7692010-11-07 09:18:22 +00002112 case -EAGAIN:
Daniel Vetter571c6082013-09-12 17:57:28 +02002113 /*
2114 * EAGAIN means the gpu is hung and we'll wait for the error
2115 * handler to reset everything when re-faulting in
2116 * i915_mutex_lock_interruptible.
Chris Wilsond9bc7e92011-02-07 13:09:31 +00002117 */
Chris Wilsonc7150892009-09-23 00:43:56 +01002118 case 0:
2119 case -ERESTARTSYS:
Chris Wilsonbed636a2011-02-11 20:31:19 +00002120 case -EINTR:
Dmitry Rogozhkine79e0fe2012-10-03 17:15:26 +03002121 case -EBUSY:
2122 /*
2123 * EBUSY is ok: this just means that another thread
2124 * already did the job.
2125 */
Paulo Zanonif65c9162013-11-27 18:20:34 -02002126 ret = VM_FAULT_NOPAGE;
2127 break;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002128 case -ENOMEM:
Paulo Zanonif65c9162013-11-27 18:20:34 -02002129 ret = VM_FAULT_OOM;
2130 break;
Daniel Vettera7c2e1a2012-10-17 11:17:16 +02002131 case -ENOSPC:
Chris Wilson45d67812014-01-31 11:34:57 +00002132 case -EFAULT:
Paulo Zanonif65c9162013-11-27 18:20:34 -02002133 ret = VM_FAULT_SIGBUS;
2134 break;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002135 default:
Daniel Vettera7c2e1a2012-10-17 11:17:16 +02002136 WARN_ONCE(ret, "unhandled error in i915_gem_fault: %i\n", ret);
Paulo Zanonif65c9162013-11-27 18:20:34 -02002137 ret = VM_FAULT_SIGBUS;
2138 break;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002139 }
Paulo Zanonif65c9162013-11-27 18:20:34 -02002140 return ret;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002141}
2142
Chris Wilsona65adaf2017-10-09 09:43:57 +01002143static void __i915_gem_object_release_mmap(struct drm_i915_gem_object *obj)
2144{
2145 struct i915_vma *vma;
2146
2147 GEM_BUG_ON(!obj->userfault_count);
2148
2149 obj->userfault_count = 0;
2150 list_del(&obj->userfault_link);
2151 drm_vma_node_unmap(&obj->base.vma_node,
2152 obj->base.dev->anon_inode->i_mapping);
2153
Chris Wilsone2189dd2017-12-07 21:14:07 +00002154 for_each_ggtt_vma(vma, obj)
Chris Wilsona65adaf2017-10-09 09:43:57 +01002155 i915_vma_unset_userfault(vma);
Chris Wilsona65adaf2017-10-09 09:43:57 +01002156}
2157
Jesse Barnesde151cf2008-11-12 10:03:55 -08002158/**
Chris Wilson901782b2009-07-10 08:18:50 +01002159 * i915_gem_release_mmap - remove physical page mappings
2160 * @obj: obj in question
2161 *
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02002162 * Preserve the reservation of the mmapping with the DRM core code, but
Chris Wilson901782b2009-07-10 08:18:50 +01002163 * relinquish ownership of the pages back to the system.
2164 *
2165 * It is vital that we remove the page mapping if we have mapped a tiled
2166 * object through the GTT and then lose the fence register due to
2167 * resource pressure. Similarly if the object has been moved out of the
2168 * aperture, than pages mapped into userspace must be revoked. Removing the
2169 * mapping will then trigger a page fault on the next user access, allowing
2170 * fixup by i915_gem_fault().
2171 */
Eric Anholtd05ca302009-07-10 13:02:26 -07002172void
Chris Wilson05394f32010-11-08 19:18:58 +00002173i915_gem_release_mmap(struct drm_i915_gem_object *obj)
Chris Wilson901782b2009-07-10 08:18:50 +01002174{
Chris Wilson275f0392016-10-24 13:42:14 +01002175 struct drm_i915_private *i915 = to_i915(obj->base.dev);
Chris Wilson275f0392016-10-24 13:42:14 +01002176
Chris Wilson349f2cc2016-04-13 17:35:12 +01002177 /* Serialisation between user GTT access and our code depends upon
2178 * revoking the CPU's PTE whilst the mutex is held. The next user
2179 * pagefault then has to wait until we release the mutex.
Chris Wilson9c870d02016-10-24 13:42:15 +01002180 *
2181 * Note that RPM complicates somewhat by adding an additional
2182 * requirement that operations to the GGTT be made holding the RPM
2183 * wakeref.
Chris Wilson349f2cc2016-04-13 17:35:12 +01002184 */
Chris Wilson275f0392016-10-24 13:42:14 +01002185 lockdep_assert_held(&i915->drm.struct_mutex);
Chris Wilson9c870d02016-10-24 13:42:15 +01002186 intel_runtime_pm_get(i915);
Chris Wilson349f2cc2016-04-13 17:35:12 +01002187
Chris Wilsona65adaf2017-10-09 09:43:57 +01002188 if (!obj->userfault_count)
Chris Wilson9c870d02016-10-24 13:42:15 +01002189 goto out;
Chris Wilson901782b2009-07-10 08:18:50 +01002190
Chris Wilsona65adaf2017-10-09 09:43:57 +01002191 __i915_gem_object_release_mmap(obj);
Chris Wilson349f2cc2016-04-13 17:35:12 +01002192
2193 /* Ensure that the CPU's PTE are revoked and there are not outstanding
2194 * memory transactions from userspace before we return. The TLB
2195 * flushing implied above by changing the PTE above *should* be
2196 * sufficient, an extra barrier here just provides us with a bit
2197 * of paranoid documentation about our requirement to serialise
2198 * memory writes before touching registers / GSM.
2199 */
2200 wmb();
Chris Wilson9c870d02016-10-24 13:42:15 +01002201
2202out:
2203 intel_runtime_pm_put(i915);
Chris Wilson901782b2009-07-10 08:18:50 +01002204}
2205
Chris Wilson7c108fd2016-10-24 13:42:18 +01002206void i915_gem_runtime_suspend(struct drm_i915_private *dev_priv)
Chris Wilsoneedd10f2014-06-16 08:57:44 +01002207{
Chris Wilson3594a3e2016-10-24 13:42:16 +01002208 struct drm_i915_gem_object *obj, *on;
Chris Wilson7c108fd2016-10-24 13:42:18 +01002209 int i;
Chris Wilsoneedd10f2014-06-16 08:57:44 +01002210
Chris Wilson3594a3e2016-10-24 13:42:16 +01002211 /*
2212 * Only called during RPM suspend. All users of the userfault_list
2213 * must be holding an RPM wakeref to ensure that this can not
2214 * run concurrently with themselves (and use the struct_mutex for
2215 * protection between themselves).
2216 */
2217
2218 list_for_each_entry_safe(obj, on,
Chris Wilsona65adaf2017-10-09 09:43:57 +01002219 &dev_priv->mm.userfault_list, userfault_link)
2220 __i915_gem_object_release_mmap(obj);
Chris Wilson7c108fd2016-10-24 13:42:18 +01002221
2222 /* The fence will be lost when the device powers down. If any were
2223 * in use by hardware (i.e. they are pinned), we should not be powering
2224 * down! All other fences will be reacquired by the user upon waking.
2225 */
2226 for (i = 0; i < dev_priv->num_fence_regs; i++) {
2227 struct drm_i915_fence_reg *reg = &dev_priv->fence_regs[i];
2228
Chris Wilsone0ec3ec2017-02-03 12:57:17 +00002229 /* Ideally we want to assert that the fence register is not
2230 * live at this point (i.e. that no piece of code will be
2231 * trying to write through fence + GTT, as that both violates
2232 * our tracking of activity and associated locking/barriers,
2233 * but also is illegal given that the hw is powered down).
2234 *
2235 * Previously we used reg->pin_count as a "liveness" indicator.
2236 * That is not sufficient, and we need a more fine-grained
2237 * tool if we want to have a sanity check here.
2238 */
Chris Wilson7c108fd2016-10-24 13:42:18 +01002239
2240 if (!reg->vma)
2241 continue;
2242
Chris Wilsona65adaf2017-10-09 09:43:57 +01002243 GEM_BUG_ON(i915_vma_has_userfault(reg->vma));
Chris Wilson7c108fd2016-10-24 13:42:18 +01002244 reg->dirty = true;
2245 }
Chris Wilsoneedd10f2014-06-16 08:57:44 +01002246}
2247
Chris Wilsond8cb5082012-08-11 15:41:03 +01002248static int i915_gem_object_create_mmap_offset(struct drm_i915_gem_object *obj)
2249{
Chris Wilsonfac5e232016-07-04 11:34:36 +01002250 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
Chris Wilsonf3f61842016-08-05 10:14:14 +01002251 int err;
Chris Wilsond8cb5082012-08-11 15:41:03 +01002252
Chris Wilsonf3f61842016-08-05 10:14:14 +01002253 err = drm_gem_create_mmap_offset(&obj->base);
Chris Wilsonb42a13d2017-01-06 15:22:40 +00002254 if (likely(!err))
Chris Wilsonf3f61842016-08-05 10:14:14 +01002255 return 0;
Daniel Vetterda494d72012-12-20 15:11:16 +01002256
Chris Wilsonb42a13d2017-01-06 15:22:40 +00002257 /* Attempt to reap some mmap space from dead objects */
2258 do {
2259 err = i915_gem_wait_for_idle(dev_priv, I915_WAIT_INTERRUPTIBLE);
2260 if (err)
2261 break;
Chris Wilsond8cb5082012-08-11 15:41:03 +01002262
Chris Wilsonb42a13d2017-01-06 15:22:40 +00002263 i915_gem_drain_freed_objects(dev_priv);
Chris Wilsonf3f61842016-08-05 10:14:14 +01002264 err = drm_gem_create_mmap_offset(&obj->base);
Chris Wilsonb42a13d2017-01-06 15:22:40 +00002265 if (!err)
2266 break;
2267
2268 } while (flush_delayed_work(&dev_priv->gt.retire_work));
Daniel Vetterda494d72012-12-20 15:11:16 +01002269
Chris Wilsonf3f61842016-08-05 10:14:14 +01002270 return err;
Chris Wilsond8cb5082012-08-11 15:41:03 +01002271}
2272
2273static void i915_gem_object_free_mmap_offset(struct drm_i915_gem_object *obj)
2274{
Chris Wilsond8cb5082012-08-11 15:41:03 +01002275 drm_gem_free_mmap_offset(&obj->base);
2276}
2277
Dave Airlieda6b51d2014-12-24 13:11:17 +10002278int
Dave Airlieff72145b2011-02-07 12:16:14 +10002279i915_gem_mmap_gtt(struct drm_file *file,
2280 struct drm_device *dev,
Dave Airlieda6b51d2014-12-24 13:11:17 +10002281 uint32_t handle,
Dave Airlieff72145b2011-02-07 12:16:14 +10002282 uint64_t *offset)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002283{
Chris Wilson05394f32010-11-08 19:18:58 +00002284 struct drm_i915_gem_object *obj;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002285 int ret;
2286
Chris Wilson03ac0642016-07-20 13:31:51 +01002287 obj = i915_gem_object_lookup(file, handle);
Chris Wilsonf3f61842016-08-05 10:14:14 +01002288 if (!obj)
2289 return -ENOENT;
Chris Wilsonab182822009-09-22 18:46:17 +01002290
Chris Wilsond8cb5082012-08-11 15:41:03 +01002291 ret = i915_gem_object_create_mmap_offset(obj);
Chris Wilsonf3f61842016-08-05 10:14:14 +01002292 if (ret == 0)
2293 *offset = drm_vma_node_offset_addr(&obj->base.vma_node);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002294
Chris Wilsonf0cd5182016-10-28 13:58:43 +01002295 i915_gem_object_put(obj);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01002296 return ret;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002297}
2298
Dave Airlieff72145b2011-02-07 12:16:14 +10002299/**
2300 * i915_gem_mmap_gtt_ioctl - prepare an object for GTT mmap'ing
2301 * @dev: DRM device
2302 * @data: GTT mapping ioctl data
2303 * @file: GEM object info
2304 *
2305 * Simply returns the fake offset to userspace so it can mmap it.
2306 * The mmap call will end up in drm_gem_mmap(), which will set things
2307 * up so we can get faults in the handler above.
2308 *
2309 * The fault handler will take care of binding the object into the GTT
2310 * (since it may have been evicted to make room for something), allocating
2311 * a fence register, and mapping the appropriate aperture address into
2312 * userspace.
2313 */
2314int
2315i915_gem_mmap_gtt_ioctl(struct drm_device *dev, void *data,
2316 struct drm_file *file)
2317{
2318 struct drm_i915_gem_mmap_gtt *args = data;
2319
Dave Airlieda6b51d2014-12-24 13:11:17 +10002320 return i915_gem_mmap_gtt(file, dev, args->handle, &args->offset);
Dave Airlieff72145b2011-02-07 12:16:14 +10002321}
2322
Daniel Vetter225067e2012-08-20 10:23:20 +02002323/* Immediately discard the backing storage */
2324static void
2325i915_gem_object_truncate(struct drm_i915_gem_object *obj)
Chris Wilsone5281cc2010-10-28 13:45:36 +01002326{
Chris Wilson4d6294bf2012-08-11 15:41:05 +01002327 i915_gem_object_free_mmap_offset(obj);
Daniel Vetter1286ff72012-05-10 15:25:09 +02002328
Chris Wilson4d6294bf2012-08-11 15:41:05 +01002329 if (obj->base.filp == NULL)
2330 return;
2331
Daniel Vetter225067e2012-08-20 10:23:20 +02002332 /* Our goal here is to return as much of the memory as
2333 * is possible back to the system as we are called from OOM.
2334 * To do this we must instruct the shmfs to drop all of its
2335 * backing pages, *now*.
Chris Wilsone5281cc2010-10-28 13:45:36 +01002336 */
Chris Wilson55372522014-03-25 13:23:06 +00002337 shmem_truncate_range(file_inode(obj->base.filp), 0, (loff_t)-1);
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002338 obj->mm.madv = __I915_MADV_PURGED;
Chris Wilson4e5462e2017-03-07 13:20:31 +00002339 obj->mm.pages = ERR_PTR(-EFAULT);
Chris Wilsone5281cc2010-10-28 13:45:36 +01002340}
Chris Wilsone5281cc2010-10-28 13:45:36 +01002341
Chris Wilson55372522014-03-25 13:23:06 +00002342/* Try to discard unwanted pages */
Chris Wilson03ac84f2016-10-28 13:58:36 +01002343void __i915_gem_object_invalidate(struct drm_i915_gem_object *obj)
Daniel Vetter225067e2012-08-20 10:23:20 +02002344{
Chris Wilson55372522014-03-25 13:23:06 +00002345 struct address_space *mapping;
2346
Chris Wilson1233e2d2016-10-28 13:58:37 +01002347 lockdep_assert_held(&obj->mm.lock);
Chris Wilsonf1fa4f42017-10-13 21:26:13 +01002348 GEM_BUG_ON(i915_gem_object_has_pages(obj));
Chris Wilson1233e2d2016-10-28 13:58:37 +01002349
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002350 switch (obj->mm.madv) {
Chris Wilson55372522014-03-25 13:23:06 +00002351 case I915_MADV_DONTNEED:
2352 i915_gem_object_truncate(obj);
2353 case __I915_MADV_PURGED:
2354 return;
2355 }
2356
2357 if (obj->base.filp == NULL)
2358 return;
2359
Al Viro93c76a32015-12-04 23:45:44 -05002360 mapping = obj->base.filp->f_mapping,
Chris Wilson55372522014-03-25 13:23:06 +00002361 invalidate_mapping_pages(mapping, 0, (loff_t)-1);
Chris Wilsone5281cc2010-10-28 13:45:36 +01002362}
2363
Chris Wilson5cdf5882010-09-27 15:51:07 +01002364static void
Chris Wilson03ac84f2016-10-28 13:58:36 +01002365i915_gem_object_put_pages_gtt(struct drm_i915_gem_object *obj,
2366 struct sg_table *pages)
Eric Anholt673a3942008-07-30 12:06:12 -07002367{
Dave Gordon85d12252016-05-20 11:54:06 +01002368 struct sgt_iter sgt_iter;
2369 struct page *page;
Daniel Vetter1286ff72012-05-10 15:25:09 +02002370
Chris Wilsone5facdf2016-12-23 14:57:57 +00002371 __i915_gem_object_release_shmem(obj, pages, true);
Eric Anholt856fa192009-03-19 14:10:50 -07002372
Chris Wilson03ac84f2016-10-28 13:58:36 +01002373 i915_gem_gtt_finish_pages(obj, pages);
Imre Deake2273302015-07-09 12:59:05 +03002374
Daniel Vetter6dacfd22011-09-12 21:30:02 +02002375 if (i915_gem_object_needs_bit17_swizzle(obj))
Chris Wilson03ac84f2016-10-28 13:58:36 +01002376 i915_gem_object_save_bit_17_swizzle(obj, pages);
Eric Anholt280b7132009-03-12 16:56:27 -07002377
Chris Wilson03ac84f2016-10-28 13:58:36 +01002378 for_each_sgt_page(page, sgt_iter, pages) {
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002379 if (obj->mm.dirty)
Chris Wilson9da3da62012-06-01 15:20:22 +01002380 set_page_dirty(page);
Chris Wilson3ef94da2009-09-14 16:50:29 +01002381
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002382 if (obj->mm.madv == I915_MADV_WILLNEED)
Chris Wilson9da3da62012-06-01 15:20:22 +01002383 mark_page_accessed(page);
Chris Wilson3ef94da2009-09-14 16:50:29 +01002384
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002385 put_page(page);
Chris Wilson3ef94da2009-09-14 16:50:29 +01002386 }
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002387 obj->mm.dirty = false;
Eric Anholt673a3942008-07-30 12:06:12 -07002388
Chris Wilson03ac84f2016-10-28 13:58:36 +01002389 sg_free_table(pages);
2390 kfree(pages);
Chris Wilson37e680a2012-06-07 15:38:42 +01002391}
2392
Chris Wilson96d77632016-10-28 13:58:33 +01002393static void __i915_gem_object_reset_page_iter(struct drm_i915_gem_object *obj)
2394{
2395 struct radix_tree_iter iter;
Ville Syrjäläc23aa712017-09-01 20:12:51 +03002396 void __rcu **slot;
Chris Wilson96d77632016-10-28 13:58:33 +01002397
Chris Wilsonbea6e982017-10-26 14:00:31 +01002398 rcu_read_lock();
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002399 radix_tree_for_each_slot(slot, &obj->mm.get_page.radix, &iter, 0)
2400 radix_tree_delete(&obj->mm.get_page.radix, iter.index);
Chris Wilsonbea6e982017-10-26 14:00:31 +01002401 rcu_read_unlock();
Chris Wilson96d77632016-10-28 13:58:33 +01002402}
2403
Chris Wilson548625e2016-11-01 12:11:34 +00002404void __i915_gem_object_put_pages(struct drm_i915_gem_object *obj,
2405 enum i915_mm_subclass subclass)
Chris Wilson37e680a2012-06-07 15:38:42 +01002406{
Chris Wilsonf2123812017-10-16 12:40:37 +01002407 struct drm_i915_private *i915 = to_i915(obj->base.dev);
Chris Wilson03ac84f2016-10-28 13:58:36 +01002408 struct sg_table *pages;
Chris Wilson37e680a2012-06-07 15:38:42 +01002409
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002410 if (i915_gem_object_has_pinned_pages(obj))
Chris Wilson03ac84f2016-10-28 13:58:36 +01002411 return;
Chris Wilsona5570172012-09-04 21:02:54 +01002412
Chris Wilson15717de2016-08-04 07:52:26 +01002413 GEM_BUG_ON(obj->bind_count);
Chris Wilsonf1fa4f42017-10-13 21:26:13 +01002414 if (!i915_gem_object_has_pages(obj))
Chris Wilson1233e2d2016-10-28 13:58:37 +01002415 return;
2416
2417 /* May be called by shrinker from within get_pages() (on another bo) */
Chris Wilson548625e2016-11-01 12:11:34 +00002418 mutex_lock_nested(&obj->mm.lock, subclass);
Chris Wilson1233e2d2016-10-28 13:58:37 +01002419 if (unlikely(atomic_read(&obj->mm.pages_pin_count)))
2420 goto unlock;
Ben Widawsky3e123022013-07-31 17:00:04 -07002421
Chris Wilsona2165e32012-12-03 11:49:00 +00002422 /* ->put_pages might need to allocate memory for the bit17 swizzle
2423 * array, hence protect them from being reaped by removing them from gtt
2424 * lists early. */
Chris Wilson03ac84f2016-10-28 13:58:36 +01002425 pages = fetch_and_zero(&obj->mm.pages);
2426 GEM_BUG_ON(!pages);
Chris Wilsona2165e32012-12-03 11:49:00 +00002427
Chris Wilsonf2123812017-10-16 12:40:37 +01002428 spin_lock(&i915->mm.obj_lock);
2429 list_del(&obj->mm.link);
2430 spin_unlock(&i915->mm.obj_lock);
2431
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002432 if (obj->mm.mapping) {
Chris Wilson4b30cb22016-08-18 17:16:42 +01002433 void *ptr;
2434
Chris Wilson0ce81782017-05-17 13:09:59 +01002435 ptr = page_mask_bits(obj->mm.mapping);
Chris Wilson4b30cb22016-08-18 17:16:42 +01002436 if (is_vmalloc_addr(ptr))
2437 vunmap(ptr);
Chris Wilsonfb8621d2016-04-08 12:11:14 +01002438 else
Chris Wilson4b30cb22016-08-18 17:16:42 +01002439 kunmap(kmap_to_page(ptr));
2440
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002441 obj->mm.mapping = NULL;
Chris Wilson0a798eb2016-04-08 12:11:11 +01002442 }
2443
Chris Wilson96d77632016-10-28 13:58:33 +01002444 __i915_gem_object_reset_page_iter(obj);
2445
Chris Wilson4e5462e2017-03-07 13:20:31 +00002446 if (!IS_ERR(pages))
2447 obj->ops->put_pages(obj, pages);
2448
Matthew Aulda5c081662017-10-06 23:18:18 +01002449 obj->mm.page_sizes.phys = obj->mm.page_sizes.sg = 0;
2450
Chris Wilson1233e2d2016-10-28 13:58:37 +01002451unlock:
2452 mutex_unlock(&obj->mm.lock);
Chris Wilson6c085a72012-08-20 11:40:46 +02002453}
2454
Chris Wilson935a2f72017-02-13 17:15:13 +00002455static bool i915_sg_trim(struct sg_table *orig_st)
Tvrtko Ursulin0c40ce12016-11-09 15:13:43 +00002456{
2457 struct sg_table new_st;
2458 struct scatterlist *sg, *new_sg;
2459 unsigned int i;
2460
2461 if (orig_st->nents == orig_st->orig_nents)
Chris Wilson935a2f72017-02-13 17:15:13 +00002462 return false;
Tvrtko Ursulin0c40ce12016-11-09 15:13:43 +00002463
Chris Wilson8bfc478f2016-12-23 14:57:58 +00002464 if (sg_alloc_table(&new_st, orig_st->nents, GFP_KERNEL | __GFP_NOWARN))
Chris Wilson935a2f72017-02-13 17:15:13 +00002465 return false;
Tvrtko Ursulin0c40ce12016-11-09 15:13:43 +00002466
2467 new_sg = new_st.sgl;
2468 for_each_sg(orig_st->sgl, sg, orig_st->nents, i) {
2469 sg_set_page(new_sg, sg_page(sg), sg->length, 0);
2470 /* called before being DMA mapped, no need to copy sg->dma_* */
2471 new_sg = sg_next(new_sg);
2472 }
Chris Wilsonc2dc6cc2016-12-19 12:43:46 +00002473 GEM_BUG_ON(new_sg); /* Should walk exactly nents and hit the end */
Tvrtko Ursulin0c40ce12016-11-09 15:13:43 +00002474
2475 sg_free_table(orig_st);
2476
2477 *orig_st = new_st;
Chris Wilson935a2f72017-02-13 17:15:13 +00002478 return true;
Tvrtko Ursulin0c40ce12016-11-09 15:13:43 +00002479}
2480
Matthew Auldb91b09e2017-10-06 23:18:17 +01002481static int i915_gem_object_get_pages_gtt(struct drm_i915_gem_object *obj)
Eric Anholt673a3942008-07-30 12:06:12 -07002482{
Chris Wilsonfac5e232016-07-04 11:34:36 +01002483 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
Chris Wilsond766ef52016-12-19 12:43:45 +00002484 const unsigned long page_count = obj->base.size / PAGE_SIZE;
2485 unsigned long i;
Eric Anholt673a3942008-07-30 12:06:12 -07002486 struct address_space *mapping;
Chris Wilson9da3da62012-06-01 15:20:22 +01002487 struct sg_table *st;
2488 struct scatterlist *sg;
Dave Gordon85d12252016-05-20 11:54:06 +01002489 struct sgt_iter sgt_iter;
Eric Anholt673a3942008-07-30 12:06:12 -07002490 struct page *page;
Imre Deak90797e62013-02-18 19:28:03 +02002491 unsigned long last_pfn = 0; /* suppress gcc warning */
Tvrtko Ursulin56024522017-08-03 10:14:17 +01002492 unsigned int max_segment = i915_sg_segment_size();
Matthew Auld84e89782017-10-09 12:00:24 +01002493 unsigned int sg_page_sizes;
Chris Wilson4846bf02017-06-09 12:03:46 +01002494 gfp_t noreclaim;
Imre Deake2273302015-07-09 12:59:05 +03002495 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07002496
Chris Wilson6c085a72012-08-20 11:40:46 +02002497 /* Assert that the object is not currently in any GPU domain. As it
2498 * wasn't in the GTT, there shouldn't be any way it could have been in
2499 * a GPU cache
2500 */
Christian Königc0a51fd2018-02-16 13:43:38 +01002501 GEM_BUG_ON(obj->read_domains & I915_GEM_GPU_DOMAINS);
2502 GEM_BUG_ON(obj->write_domain & I915_GEM_GPU_DOMAINS);
Chris Wilson6c085a72012-08-20 11:40:46 +02002503
Chris Wilson9da3da62012-06-01 15:20:22 +01002504 st = kmalloc(sizeof(*st), GFP_KERNEL);
2505 if (st == NULL)
Matthew Auldb91b09e2017-10-06 23:18:17 +01002506 return -ENOMEM;
Eric Anholt673a3942008-07-30 12:06:12 -07002507
Chris Wilsond766ef52016-12-19 12:43:45 +00002508rebuild_st:
Chris Wilson9da3da62012-06-01 15:20:22 +01002509 if (sg_alloc_table(st, page_count, GFP_KERNEL)) {
Chris Wilson9da3da62012-06-01 15:20:22 +01002510 kfree(st);
Matthew Auldb91b09e2017-10-06 23:18:17 +01002511 return -ENOMEM;
Chris Wilson9da3da62012-06-01 15:20:22 +01002512 }
2513
2514 /* Get the list of pages out of our struct file. They'll be pinned
2515 * at this point until we release them.
2516 *
2517 * Fail silently without starting the shrinker
2518 */
Al Viro93c76a32015-12-04 23:45:44 -05002519 mapping = obj->base.filp->f_mapping;
Chris Wilson0f6ab552017-06-09 12:03:48 +01002520 noreclaim = mapping_gfp_constraint(mapping, ~__GFP_RECLAIM);
Chris Wilson4846bf02017-06-09 12:03:46 +01002521 noreclaim |= __GFP_NORETRY | __GFP_NOWARN;
2522
Imre Deak90797e62013-02-18 19:28:03 +02002523 sg = st->sgl;
2524 st->nents = 0;
Matthew Auld84e89782017-10-09 12:00:24 +01002525 sg_page_sizes = 0;
Imre Deak90797e62013-02-18 19:28:03 +02002526 for (i = 0; i < page_count; i++) {
Chris Wilson4846bf02017-06-09 12:03:46 +01002527 const unsigned int shrink[] = {
2528 I915_SHRINK_BOUND | I915_SHRINK_UNBOUND | I915_SHRINK_PURGEABLE,
2529 0,
2530 }, *s = shrink;
2531 gfp_t gfp = noreclaim;
2532
2533 do {
Chris Wilson6c085a72012-08-20 11:40:46 +02002534 page = shmem_read_mapping_page_gfp(mapping, i, gfp);
Chris Wilson4846bf02017-06-09 12:03:46 +01002535 if (likely(!IS_ERR(page)))
2536 break;
2537
2538 if (!*s) {
2539 ret = PTR_ERR(page);
2540 goto err_sg;
2541 }
2542
Chris Wilson912d5722017-09-06 16:19:30 -07002543 i915_gem_shrink(dev_priv, 2 * page_count, NULL, *s++);
Chris Wilson4846bf02017-06-09 12:03:46 +01002544 cond_resched();
Chris Wilson24f8e002017-03-22 11:05:21 +00002545
Chris Wilson6c085a72012-08-20 11:40:46 +02002546 /* We've tried hard to allocate the memory by reaping
2547 * our own buffer, now let the real VM do its job and
2548 * go down in flames if truly OOM.
Chris Wilson24f8e002017-03-22 11:05:21 +00002549 *
2550 * However, since graphics tend to be disposable,
2551 * defer the oom here by reporting the ENOMEM back
2552 * to userspace.
Chris Wilson6c085a72012-08-20 11:40:46 +02002553 */
Chris Wilson4846bf02017-06-09 12:03:46 +01002554 if (!*s) {
2555 /* reclaim and warn, but no oom */
2556 gfp = mapping_gfp_mask(mapping);
Chris Wilsoneaf41802017-06-09 12:03:47 +01002557
2558 /* Our bo are always dirty and so we require
2559 * kswapd to reclaim our pages (direct reclaim
2560 * does not effectively begin pageout of our
2561 * buffers on its own). However, direct reclaim
2562 * only waits for kswapd when under allocation
2563 * congestion. So as a result __GFP_RECLAIM is
2564 * unreliable and fails to actually reclaim our
2565 * dirty pages -- unless you try over and over
2566 * again with !__GFP_NORETRY. However, we still
2567 * want to fail this allocation rather than
2568 * trigger the out-of-memory killer and for
Michal Hockodbb32952017-07-12 14:36:55 -07002569 * this we want __GFP_RETRY_MAYFAIL.
Chris Wilsoneaf41802017-06-09 12:03:47 +01002570 */
Michal Hockodbb32952017-07-12 14:36:55 -07002571 gfp |= __GFP_RETRY_MAYFAIL;
Imre Deake2273302015-07-09 12:59:05 +03002572 }
Chris Wilson4846bf02017-06-09 12:03:46 +01002573 } while (1);
2574
Chris Wilson871dfbd2016-10-11 09:20:21 +01002575 if (!i ||
2576 sg->length >= max_segment ||
2577 page_to_pfn(page) != last_pfn + 1) {
Matthew Aulda5c081662017-10-06 23:18:18 +01002578 if (i) {
Matthew Auld84e89782017-10-09 12:00:24 +01002579 sg_page_sizes |= sg->length;
Imre Deak90797e62013-02-18 19:28:03 +02002580 sg = sg_next(sg);
Matthew Aulda5c081662017-10-06 23:18:18 +01002581 }
Imre Deak90797e62013-02-18 19:28:03 +02002582 st->nents++;
2583 sg_set_page(sg, page, PAGE_SIZE, 0);
2584 } else {
2585 sg->length += PAGE_SIZE;
2586 }
2587 last_pfn = page_to_pfn(page);
Daniel Vetter3bbbe702013-10-07 17:15:45 -03002588
2589 /* Check that the i965g/gm workaround works. */
2590 WARN_ON((gfp & __GFP_DMA32) && (last_pfn >= 0x00100000UL));
Eric Anholt673a3942008-07-30 12:06:12 -07002591 }
Matthew Aulda5c081662017-10-06 23:18:18 +01002592 if (sg) { /* loop terminated early; short sg table */
Matthew Auld84e89782017-10-09 12:00:24 +01002593 sg_page_sizes |= sg->length;
Konrad Rzeszutek Wilk426729d2013-06-24 11:47:48 -04002594 sg_mark_end(sg);
Matthew Aulda5c081662017-10-06 23:18:18 +01002595 }
Chris Wilson74ce6b62012-10-19 15:51:06 +01002596
Tvrtko Ursulin0c40ce12016-11-09 15:13:43 +00002597 /* Trim unused sg entries to avoid wasting memory. */
2598 i915_sg_trim(st);
2599
Chris Wilson03ac84f2016-10-28 13:58:36 +01002600 ret = i915_gem_gtt_prepare_pages(obj, st);
Chris Wilsond766ef52016-12-19 12:43:45 +00002601 if (ret) {
2602 /* DMA remapping failed? One possible cause is that
2603 * it could not reserve enough large entries, asking
2604 * for PAGE_SIZE chunks instead may be helpful.
2605 */
2606 if (max_segment > PAGE_SIZE) {
2607 for_each_sgt_page(page, sgt_iter, st)
2608 put_page(page);
2609 sg_free_table(st);
2610
2611 max_segment = PAGE_SIZE;
2612 goto rebuild_st;
2613 } else {
2614 dev_warn(&dev_priv->drm.pdev->dev,
2615 "Failed to DMA remap %lu pages\n",
2616 page_count);
2617 goto err_pages;
2618 }
2619 }
Imre Deake2273302015-07-09 12:59:05 +03002620
Eric Anholt673a3942008-07-30 12:06:12 -07002621 if (i915_gem_object_needs_bit17_swizzle(obj))
Chris Wilson03ac84f2016-10-28 13:58:36 +01002622 i915_gem_object_do_bit_17_swizzle(obj, st);
Eric Anholt673a3942008-07-30 12:06:12 -07002623
Matthew Auld84e89782017-10-09 12:00:24 +01002624 __i915_gem_object_set_pages(obj, st, sg_page_sizes);
Matthew Auldb91b09e2017-10-06 23:18:17 +01002625
2626 return 0;
Eric Anholt673a3942008-07-30 12:06:12 -07002627
Chris Wilsonb17993b2016-11-14 11:29:30 +00002628err_sg:
Imre Deak90797e62013-02-18 19:28:03 +02002629 sg_mark_end(sg);
Chris Wilsonb17993b2016-11-14 11:29:30 +00002630err_pages:
Dave Gordon85d12252016-05-20 11:54:06 +01002631 for_each_sgt_page(page, sgt_iter, st)
2632 put_page(page);
Chris Wilson9da3da62012-06-01 15:20:22 +01002633 sg_free_table(st);
2634 kfree(st);
Chris Wilson0820baf2014-03-25 13:23:03 +00002635
2636 /* shmemfs first checks if there is enough memory to allocate the page
2637 * and reports ENOSPC should there be insufficient, along with the usual
2638 * ENOMEM for a genuine allocation failure.
2639 *
2640 * We use ENOSPC in our driver to mean that we have run out of aperture
2641 * space and so want to translate the error from shmemfs back to our
2642 * usual understanding of ENOMEM.
2643 */
Imre Deake2273302015-07-09 12:59:05 +03002644 if (ret == -ENOSPC)
2645 ret = -ENOMEM;
2646
Matthew Auldb91b09e2017-10-06 23:18:17 +01002647 return ret;
Chris Wilson03ac84f2016-10-28 13:58:36 +01002648}
2649
2650void __i915_gem_object_set_pages(struct drm_i915_gem_object *obj,
Matthew Aulda5c081662017-10-06 23:18:18 +01002651 struct sg_table *pages,
Matthew Auld84e89782017-10-09 12:00:24 +01002652 unsigned int sg_page_sizes)
Chris Wilson03ac84f2016-10-28 13:58:36 +01002653{
Matthew Aulda5c081662017-10-06 23:18:18 +01002654 struct drm_i915_private *i915 = to_i915(obj->base.dev);
2655 unsigned long supported = INTEL_INFO(i915)->page_sizes;
2656 int i;
2657
Chris Wilson1233e2d2016-10-28 13:58:37 +01002658 lockdep_assert_held(&obj->mm.lock);
Chris Wilson03ac84f2016-10-28 13:58:36 +01002659
2660 obj->mm.get_page.sg_pos = pages->sgl;
2661 obj->mm.get_page.sg_idx = 0;
2662
2663 obj->mm.pages = pages;
Chris Wilson2c3a3f42016-11-04 10:30:01 +00002664
2665 if (i915_gem_object_is_tiled(obj) &&
Chris Wilsonf2123812017-10-16 12:40:37 +01002666 i915->quirks & QUIRK_PIN_SWIZZLED_PAGES) {
Chris Wilson2c3a3f42016-11-04 10:30:01 +00002667 GEM_BUG_ON(obj->mm.quirked);
2668 __i915_gem_object_pin_pages(obj);
2669 obj->mm.quirked = true;
2670 }
Matthew Aulda5c081662017-10-06 23:18:18 +01002671
Matthew Auld84e89782017-10-09 12:00:24 +01002672 GEM_BUG_ON(!sg_page_sizes);
2673 obj->mm.page_sizes.phys = sg_page_sizes;
Matthew Aulda5c081662017-10-06 23:18:18 +01002674
2675 /*
Matthew Auld84e89782017-10-09 12:00:24 +01002676 * Calculate the supported page-sizes which fit into the given
2677 * sg_page_sizes. This will give us the page-sizes which we may be able
2678 * to use opportunistically when later inserting into the GTT. For
2679 * example if phys=2G, then in theory we should be able to use 1G, 2M,
2680 * 64K or 4K pages, although in practice this will depend on a number of
2681 * other factors.
Matthew Aulda5c081662017-10-06 23:18:18 +01002682 */
2683 obj->mm.page_sizes.sg = 0;
2684 for_each_set_bit(i, &supported, ilog2(I915_GTT_MAX_PAGE_SIZE) + 1) {
2685 if (obj->mm.page_sizes.phys & ~0u << i)
2686 obj->mm.page_sizes.sg |= BIT(i);
2687 }
Matthew Aulda5c081662017-10-06 23:18:18 +01002688 GEM_BUG_ON(!HAS_PAGE_SIZES(i915, obj->mm.page_sizes.sg));
Chris Wilsonf2123812017-10-16 12:40:37 +01002689
2690 spin_lock(&i915->mm.obj_lock);
2691 list_add(&obj->mm.link, &i915->mm.unbound_list);
2692 spin_unlock(&i915->mm.obj_lock);
Chris Wilson03ac84f2016-10-28 13:58:36 +01002693}
2694
2695static int ____i915_gem_object_get_pages(struct drm_i915_gem_object *obj)
2696{
Matthew Auldb91b09e2017-10-06 23:18:17 +01002697 int err;
Chris Wilson03ac84f2016-10-28 13:58:36 +01002698
2699 if (unlikely(obj->mm.madv != I915_MADV_WILLNEED)) {
2700 DRM_DEBUG("Attempting to obtain a purgeable object\n");
2701 return -EFAULT;
2702 }
2703
Matthew Auldb91b09e2017-10-06 23:18:17 +01002704 err = obj->ops->get_pages(obj);
Matthew Auldb65a9b92017-12-18 10:38:55 +00002705 GEM_BUG_ON(!err && !i915_gem_object_has_pages(obj));
Chris Wilson03ac84f2016-10-28 13:58:36 +01002706
Matthew Auldb91b09e2017-10-06 23:18:17 +01002707 return err;
Eric Anholt673a3942008-07-30 12:06:12 -07002708}
2709
Chris Wilson37e680a2012-06-07 15:38:42 +01002710/* Ensure that the associated pages are gathered from the backing storage
Chris Wilson1233e2d2016-10-28 13:58:37 +01002711 * and pinned into our object. i915_gem_object_pin_pages() may be called
Chris Wilson37e680a2012-06-07 15:38:42 +01002712 * multiple times before they are released by a single call to
Chris Wilson1233e2d2016-10-28 13:58:37 +01002713 * i915_gem_object_unpin_pages() - once the pages are no longer referenced
Chris Wilson37e680a2012-06-07 15:38:42 +01002714 * either as a result of memory pressure (reaping pages under the shrinker)
2715 * or as the object is itself released.
2716 */
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002717int __i915_gem_object_get_pages(struct drm_i915_gem_object *obj)
Chris Wilson37e680a2012-06-07 15:38:42 +01002718{
Chris Wilson03ac84f2016-10-28 13:58:36 +01002719 int err;
Chris Wilson37e680a2012-06-07 15:38:42 +01002720
Chris Wilson1233e2d2016-10-28 13:58:37 +01002721 err = mutex_lock_interruptible(&obj->mm.lock);
2722 if (err)
2723 return err;
Chris Wilson4c7d62c2016-10-28 13:58:32 +01002724
Chris Wilsonf1fa4f42017-10-13 21:26:13 +01002725 if (unlikely(!i915_gem_object_has_pages(obj))) {
Chris Wilson88c880b2017-09-06 14:52:20 +01002726 GEM_BUG_ON(i915_gem_object_has_pinned_pages(obj));
2727
Chris Wilson2c3a3f42016-11-04 10:30:01 +00002728 err = ____i915_gem_object_get_pages(obj);
2729 if (err)
2730 goto unlock;
2731
2732 smp_mb__before_atomic();
Chris Wilson1233e2d2016-10-28 13:58:37 +01002733 }
Chris Wilson2c3a3f42016-11-04 10:30:01 +00002734 atomic_inc(&obj->mm.pages_pin_count);
Chris Wilson43e28f02013-01-08 10:53:09 +00002735
Chris Wilson1233e2d2016-10-28 13:58:37 +01002736unlock:
2737 mutex_unlock(&obj->mm.lock);
Chris Wilson03ac84f2016-10-28 13:58:36 +01002738 return err;
Eric Anholt673a3942008-07-30 12:06:12 -07002739}
2740
Dave Gordondd6034c2016-05-20 11:54:04 +01002741/* The 'mapping' part of i915_gem_object_pin_map() below */
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002742static void *i915_gem_object_map(const struct drm_i915_gem_object *obj,
2743 enum i915_map_type type)
Dave Gordondd6034c2016-05-20 11:54:04 +01002744{
2745 unsigned long n_pages = obj->base.size >> PAGE_SHIFT;
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002746 struct sg_table *sgt = obj->mm.pages;
Dave Gordon85d12252016-05-20 11:54:06 +01002747 struct sgt_iter sgt_iter;
2748 struct page *page;
Dave Gordonb338fa42016-05-20 11:54:05 +01002749 struct page *stack_pages[32];
2750 struct page **pages = stack_pages;
Dave Gordondd6034c2016-05-20 11:54:04 +01002751 unsigned long i = 0;
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002752 pgprot_t pgprot;
Dave Gordondd6034c2016-05-20 11:54:04 +01002753 void *addr;
2754
2755 /* A single page can always be kmapped */
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002756 if (n_pages == 1 && type == I915_MAP_WB)
Dave Gordondd6034c2016-05-20 11:54:04 +01002757 return kmap(sg_page(sgt->sgl));
2758
Dave Gordonb338fa42016-05-20 11:54:05 +01002759 if (n_pages > ARRAY_SIZE(stack_pages)) {
2760 /* Too big for stack -- allocate temporary array instead */
Michal Hocko0ee931c2017-09-13 16:28:29 -07002761 pages = kvmalloc_array(n_pages, sizeof(*pages), GFP_KERNEL);
Dave Gordonb338fa42016-05-20 11:54:05 +01002762 if (!pages)
2763 return NULL;
2764 }
Dave Gordondd6034c2016-05-20 11:54:04 +01002765
Dave Gordon85d12252016-05-20 11:54:06 +01002766 for_each_sgt_page(page, sgt_iter, sgt)
2767 pages[i++] = page;
Dave Gordondd6034c2016-05-20 11:54:04 +01002768
2769 /* Check that we have the expected number of pages */
2770 GEM_BUG_ON(i != n_pages);
2771
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002772 switch (type) {
Chris Wilsona575c672017-08-28 11:46:31 +01002773 default:
2774 MISSING_CASE(type);
2775 /* fallthrough to use PAGE_KERNEL anyway */
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002776 case I915_MAP_WB:
2777 pgprot = PAGE_KERNEL;
2778 break;
2779 case I915_MAP_WC:
2780 pgprot = pgprot_writecombine(PAGE_KERNEL_IO);
2781 break;
2782 }
2783 addr = vmap(pages, n_pages, 0, pgprot);
Dave Gordondd6034c2016-05-20 11:54:04 +01002784
Dave Gordonb338fa42016-05-20 11:54:05 +01002785 if (pages != stack_pages)
Michal Hocko20981052017-05-17 14:23:12 +02002786 kvfree(pages);
Dave Gordondd6034c2016-05-20 11:54:04 +01002787
2788 return addr;
2789}
2790
2791/* get, pin, and map the pages of the object into kernel space */
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002792void *i915_gem_object_pin_map(struct drm_i915_gem_object *obj,
2793 enum i915_map_type type)
Chris Wilson0a798eb2016-04-08 12:11:11 +01002794{
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002795 enum i915_map_type has_type;
2796 bool pinned;
2797 void *ptr;
Chris Wilson0a798eb2016-04-08 12:11:11 +01002798 int ret;
2799
Tina Zhanga03f3952017-11-14 10:25:13 +00002800 if (unlikely(!i915_gem_object_has_struct_page(obj)))
2801 return ERR_PTR(-ENXIO);
Chris Wilson0a798eb2016-04-08 12:11:11 +01002802
Chris Wilson1233e2d2016-10-28 13:58:37 +01002803 ret = mutex_lock_interruptible(&obj->mm.lock);
Chris Wilson0a798eb2016-04-08 12:11:11 +01002804 if (ret)
2805 return ERR_PTR(ret);
2806
Chris Wilsona575c672017-08-28 11:46:31 +01002807 pinned = !(type & I915_MAP_OVERRIDE);
2808 type &= ~I915_MAP_OVERRIDE;
2809
Chris Wilson1233e2d2016-10-28 13:58:37 +01002810 if (!atomic_inc_not_zero(&obj->mm.pages_pin_count)) {
Chris Wilsonf1fa4f42017-10-13 21:26:13 +01002811 if (unlikely(!i915_gem_object_has_pages(obj))) {
Chris Wilson88c880b2017-09-06 14:52:20 +01002812 GEM_BUG_ON(i915_gem_object_has_pinned_pages(obj));
2813
Chris Wilson2c3a3f42016-11-04 10:30:01 +00002814 ret = ____i915_gem_object_get_pages(obj);
2815 if (ret)
2816 goto err_unlock;
Chris Wilson1233e2d2016-10-28 13:58:37 +01002817
Chris Wilson2c3a3f42016-11-04 10:30:01 +00002818 smp_mb__before_atomic();
2819 }
2820 atomic_inc(&obj->mm.pages_pin_count);
Chris Wilson1233e2d2016-10-28 13:58:37 +01002821 pinned = false;
2822 }
Chris Wilsonf1fa4f42017-10-13 21:26:13 +01002823 GEM_BUG_ON(!i915_gem_object_has_pages(obj));
Chris Wilson0a798eb2016-04-08 12:11:11 +01002824
Chris Wilson0ce81782017-05-17 13:09:59 +01002825 ptr = page_unpack_bits(obj->mm.mapping, &has_type);
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002826 if (ptr && has_type != type) {
2827 if (pinned) {
2828 ret = -EBUSY;
Chris Wilson1233e2d2016-10-28 13:58:37 +01002829 goto err_unpin;
Chris Wilson0a798eb2016-04-08 12:11:11 +01002830 }
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002831
2832 if (is_vmalloc_addr(ptr))
2833 vunmap(ptr);
2834 else
2835 kunmap(kmap_to_page(ptr));
2836
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002837 ptr = obj->mm.mapping = NULL;
Chris Wilson0a798eb2016-04-08 12:11:11 +01002838 }
2839
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002840 if (!ptr) {
2841 ptr = i915_gem_object_map(obj, type);
2842 if (!ptr) {
2843 ret = -ENOMEM;
Chris Wilson1233e2d2016-10-28 13:58:37 +01002844 goto err_unpin;
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002845 }
2846
Chris Wilson0ce81782017-05-17 13:09:59 +01002847 obj->mm.mapping = page_pack_bits(ptr, type);
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002848 }
2849
Chris Wilson1233e2d2016-10-28 13:58:37 +01002850out_unlock:
2851 mutex_unlock(&obj->mm.lock);
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002852 return ptr;
2853
Chris Wilson1233e2d2016-10-28 13:58:37 +01002854err_unpin:
2855 atomic_dec(&obj->mm.pages_pin_count);
2856err_unlock:
2857 ptr = ERR_PTR(ret);
2858 goto out_unlock;
Chris Wilson0a798eb2016-04-08 12:11:11 +01002859}
2860
Chris Wilson7c55e2c2017-03-07 12:03:38 +00002861static int
2862i915_gem_object_pwrite_gtt(struct drm_i915_gem_object *obj,
2863 const struct drm_i915_gem_pwrite *arg)
2864{
2865 struct address_space *mapping = obj->base.filp->f_mapping;
2866 char __user *user_data = u64_to_user_ptr(arg->data_ptr);
2867 u64 remain, offset;
2868 unsigned int pg;
2869
2870 /* Before we instantiate/pin the backing store for our use, we
2871 * can prepopulate the shmemfs filp efficiently using a write into
2872 * the pagecache. We avoid the penalty of instantiating all the
2873 * pages, important if the user is just writing to a few and never
2874 * uses the object on the GPU, and using a direct write into shmemfs
2875 * allows it to avoid the cost of retrieving a page (either swapin
2876 * or clearing-before-use) before it is overwritten.
2877 */
Chris Wilsonf1fa4f42017-10-13 21:26:13 +01002878 if (i915_gem_object_has_pages(obj))
Chris Wilson7c55e2c2017-03-07 12:03:38 +00002879 return -ENODEV;
2880
Chris Wilsona6d65e42017-10-16 21:27:32 +01002881 if (obj->mm.madv != I915_MADV_WILLNEED)
2882 return -EFAULT;
2883
Chris Wilson7c55e2c2017-03-07 12:03:38 +00002884 /* Before the pages are instantiated the object is treated as being
2885 * in the CPU domain. The pages will be clflushed as required before
2886 * use, and we can freely write into the pages directly. If userspace
2887 * races pwrite with any other operation; corruption will ensue -
2888 * that is userspace's prerogative!
2889 */
2890
2891 remain = arg->size;
2892 offset = arg->offset;
2893 pg = offset_in_page(offset);
2894
2895 do {
2896 unsigned int len, unwritten;
2897 struct page *page;
2898 void *data, *vaddr;
2899 int err;
2900
2901 len = PAGE_SIZE - pg;
2902 if (len > remain)
2903 len = remain;
2904
2905 err = pagecache_write_begin(obj->base.filp, mapping,
2906 offset, len, 0,
2907 &page, &data);
2908 if (err < 0)
2909 return err;
2910
2911 vaddr = kmap(page);
2912 unwritten = copy_from_user(vaddr + pg, user_data, len);
2913 kunmap(page);
2914
2915 err = pagecache_write_end(obj->base.filp, mapping,
2916 offset, len, len - unwritten,
2917 page, data);
2918 if (err < 0)
2919 return err;
2920
2921 if (unwritten)
2922 return -EFAULT;
2923
2924 remain -= len;
2925 user_data += len;
2926 offset += len;
2927 pg = 0;
2928 } while (remain);
2929
2930 return 0;
2931}
2932
Mika Kuoppalae5e1fc42016-11-16 17:20:31 +02002933static void i915_gem_context_mark_guilty(struct i915_gem_context *ctx)
Mika Kuoppalaaa60c662013-06-12 15:13:20 +03002934{
Chris Wilson77b25a92017-07-21 13:32:30 +01002935 bool banned;
Mika Kuoppalab083a082016-11-18 15:10:47 +02002936
Chris Wilson77b25a92017-07-21 13:32:30 +01002937 atomic_inc(&ctx->guilty_count);
2938
Chris Wilson24eae082018-02-05 09:22:01 +00002939 banned = false;
2940 if (i915_gem_context_is_bannable(ctx)) {
2941 unsigned int score;
2942
2943 score = atomic_add_return(CONTEXT_SCORE_GUILTY,
2944 &ctx->ban_score);
2945 banned = score >= CONTEXT_SCORE_BAN_THRESHOLD;
2946
2947 DRM_DEBUG_DRIVER("context %s marked guilty (score %d) banned? %s\n",
2948 ctx->name, score, yesno(banned));
2949 }
Chris Wilson77b25a92017-07-21 13:32:30 +01002950 if (!banned)
Mika Kuoppalab083a082016-11-18 15:10:47 +02002951 return;
2952
Chris Wilson77b25a92017-07-21 13:32:30 +01002953 i915_gem_context_set_banned(ctx);
2954 if (!IS_ERR_OR_NULL(ctx->file_priv)) {
2955 atomic_inc(&ctx->file_priv->context_bans);
2956 DRM_DEBUG_DRIVER("client %s has had %d context banned\n",
2957 ctx->name, atomic_read(&ctx->file_priv->context_bans));
2958 }
Mika Kuoppalae5e1fc42016-11-16 17:20:31 +02002959}
2960
2961static void i915_gem_context_mark_innocent(struct i915_gem_context *ctx)
2962{
Chris Wilson77b25a92017-07-21 13:32:30 +01002963 atomic_inc(&ctx->active_count);
Mika Kuoppalaaa60c662013-06-12 15:13:20 +03002964}
2965
Chris Wilsone61e0f52018-02-21 09:56:36 +00002966struct i915_request *
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00002967i915_gem_find_active_request(struct intel_engine_cs *engine)
Chris Wilson9375e442010-09-19 12:21:28 +01002968{
Chris Wilsone61e0f52018-02-21 09:56:36 +00002969 struct i915_request *request, *active = NULL;
Chris Wilson754c9fd2017-02-23 07:44:14 +00002970 unsigned long flags;
Mika Kuoppalaaa60c662013-06-12 15:13:20 +03002971
Chris Wilsonf69a02c2016-07-01 17:23:16 +01002972 /* We are called by the error capture and reset at a random
2973 * point in time. In particular, note that neither is crucially
2974 * ordered with an interrupt. After a hang, the GPU is dead and we
2975 * assume that no more writes can happen (we waited long enough for
2976 * all writes that were in transaction to be flushed) - adding an
2977 * extra delay for a recent interrupt is pointless. Hence, we do
2978 * not need an engine->irq_seqno_barrier() before the seqno reads.
2979 */
Chris Wilson754c9fd2017-02-23 07:44:14 +00002980 spin_lock_irqsave(&engine->timeline->lock, flags);
Chris Wilson73cb9702016-10-28 13:58:46 +01002981 list_for_each_entry(request, &engine->timeline->requests, link) {
Chris Wilsone61e0f52018-02-21 09:56:36 +00002982 if (__i915_request_completed(request, request->global_seqno))
Chris Wilson4db080f2013-12-04 11:37:09 +00002983 continue;
Mika Kuoppalaaa60c662013-06-12 15:13:20 +03002984
Mika Kuoppala36193ac2017-01-17 17:59:02 +02002985 GEM_BUG_ON(request->engine != engine);
Chris Wilsonc00122f32017-02-12 17:19:58 +00002986 GEM_BUG_ON(test_bit(DMA_FENCE_FLAG_SIGNALED_BIT,
2987 &request->fence.flags));
Mika Kuoppalab6b0fac2014-01-30 19:04:43 +02002988
Chris Wilson754c9fd2017-02-23 07:44:14 +00002989 active = request;
2990 break;
2991 }
2992 spin_unlock_irqrestore(&engine->timeline->lock, flags);
2993
2994 return active;
Mika Kuoppalab6b0fac2014-01-30 19:04:43 +02002995}
2996
Michel Thierrya1ef70e2017-06-20 10:57:47 +01002997/*
2998 * Ensure irq handler finishes, and not run again.
2999 * Also return the active request so that we only search for it once.
3000 */
Chris Wilsone61e0f52018-02-21 09:56:36 +00003001struct i915_request *
Michel Thierrya1ef70e2017-06-20 10:57:47 +01003002i915_gem_reset_prepare_engine(struct intel_engine_cs *engine)
3003{
Chris Wilsone61e0f52018-02-21 09:56:36 +00003004 struct i915_request *request = NULL;
Michel Thierrya1ef70e2017-06-20 10:57:47 +01003005
Chris Wilson1749d902017-10-09 12:02:59 +01003006 /*
3007 * During the reset sequence, we must prevent the engine from
3008 * entering RC6. As the context state is undefined until we restart
3009 * the engine, if it does enter RC6 during the reset, the state
3010 * written to the powercontext is undefined and so we may lose
3011 * GPU state upon resume, i.e. fail to restart after a reset.
3012 */
3013 intel_uncore_forcewake_get(engine->i915, FORCEWAKE_ALL);
3014
3015 /*
3016 * Prevent the signaler thread from updating the request
Michel Thierrya1ef70e2017-06-20 10:57:47 +01003017 * state (by calling dma_fence_signal) as we are processing
3018 * the reset. The write from the GPU of the seqno is
3019 * asynchronous and the signaler thread may see a different
3020 * value to us and declare the request complete, even though
3021 * the reset routine have picked that request as the active
3022 * (incomplete) request. This conflict is not handled
3023 * gracefully!
3024 */
3025 kthread_park(engine->breadcrumbs.signaler);
3026
Chris Wilson1749d902017-10-09 12:02:59 +01003027 /*
3028 * Prevent request submission to the hardware until we have
Michel Thierrya1ef70e2017-06-20 10:57:47 +01003029 * completed the reset in i915_gem_reset_finish(). If a request
3030 * is completed by one engine, it may then queue a request
Sagar Arun Kamblec6dce8f2017-11-16 19:02:37 +05303031 * to a second via its execlists->tasklet *just* as we are
Michel Thierrya1ef70e2017-06-20 10:57:47 +01003032 * calling engine->init_hw() and also writing the ELSP.
Sagar Arun Kamblec6dce8f2017-11-16 19:02:37 +05303033 * Turning off the execlists->tasklet until the reset is over
Michel Thierrya1ef70e2017-06-20 10:57:47 +01003034 * prevents the race.
Chris Wilson68ad3612018-03-07 13:42:26 +00003035 *
3036 * Note that this needs to be a single atomic operation on the
3037 * tasklet (flush existing tasks, prevent new tasks) to prevent
3038 * a race between reset and set-wedged. It is not, so we do the best
3039 * we can atm and make sure we don't lock the machine up in the more
3040 * common case of recursively being called from set-wedged from inside
3041 * i915_reset.
Michel Thierrya1ef70e2017-06-20 10:57:47 +01003042 */
Chris Wilson68ad3612018-03-07 13:42:26 +00003043 if (!atomic_read(&engine->execlists.tasklet.count))
3044 tasklet_kill(&engine->execlists.tasklet);
Sagar Arun Kamblec6dce8f2017-11-16 19:02:37 +05303045 tasklet_disable(&engine->execlists.tasklet);
Michel Thierrya1ef70e2017-06-20 10:57:47 +01003046
Michał Winiarskic41937f2017-10-26 15:35:58 +02003047 /*
3048 * We're using worker to queue preemption requests from the tasklet in
3049 * GuC submission mode.
3050 * Even though tasklet was disabled, we may still have a worker queued.
3051 * Let's make sure that all workers scheduled before disabling the
3052 * tasklet are completed before continuing with the reset.
3053 */
3054 if (engine->i915->guc.preempt_wq)
3055 flush_workqueue(engine->i915->guc.preempt_wq);
3056
Michel Thierrya1ef70e2017-06-20 10:57:47 +01003057 if (engine->irq_seqno_barrier)
3058 engine->irq_seqno_barrier(engine);
3059
Chris Wilsond1d1ebf42017-07-21 13:32:33 +01003060 request = i915_gem_find_active_request(engine);
3061 if (request && request->fence.error == -EIO)
3062 request = ERR_PTR(-EIO); /* Previous reset failed! */
Michel Thierrya1ef70e2017-06-20 10:57:47 +01003063
3064 return request;
3065}
3066
Chris Wilson0e178ae2017-01-17 17:59:06 +02003067int i915_gem_reset_prepare(struct drm_i915_private *dev_priv)
Chris Wilson4c965542017-01-17 17:59:01 +02003068{
3069 struct intel_engine_cs *engine;
Chris Wilsone61e0f52018-02-21 09:56:36 +00003070 struct i915_request *request;
Chris Wilson4c965542017-01-17 17:59:01 +02003071 enum intel_engine_id id;
Chris Wilson0e178ae2017-01-17 17:59:06 +02003072 int err = 0;
Chris Wilson4c965542017-01-17 17:59:01 +02003073
Chris Wilson0e178ae2017-01-17 17:59:06 +02003074 for_each_engine(engine, dev_priv, id) {
Michel Thierrya1ef70e2017-06-20 10:57:47 +01003075 request = i915_gem_reset_prepare_engine(engine);
3076 if (IS_ERR(request)) {
3077 err = PTR_ERR(request);
3078 continue;
Chris Wilson0e178ae2017-01-17 17:59:06 +02003079 }
Michel Thierryc64992e2017-06-20 10:57:44 +01003080
3081 engine->hangcheck.active_request = request;
Chris Wilson0e178ae2017-01-17 17:59:06 +02003082 }
3083
Chris Wilson4c965542017-01-17 17:59:01 +02003084 i915_gem_revoke_fences(dev_priv);
Michal Wajdeczkoc37d5722018-03-12 13:03:07 +00003085 intel_uc_sanitize(dev_priv);
Chris Wilson0e178ae2017-01-17 17:59:06 +02003086
3087 return err;
Chris Wilson4c965542017-01-17 17:59:01 +02003088}
3089
Chris Wilsone61e0f52018-02-21 09:56:36 +00003090static void skip_request(struct i915_request *request)
Mika Kuoppalab6b0fac2014-01-30 19:04:43 +02003091{
Chris Wilson821ed7d2016-09-09 14:11:53 +01003092 void *vaddr = request->ring->vaddr;
3093 u32 head;
Mika Kuoppalab6b0fac2014-01-30 19:04:43 +02003094
Chris Wilson821ed7d2016-09-09 14:11:53 +01003095 /* As this request likely depends on state from the lost
3096 * context, clear out all the user operations leaving the
3097 * breadcrumb at the end (so we get the fence notifications).
3098 */
3099 head = request->head;
3100 if (request->postfix < head) {
3101 memset(vaddr + head, 0, request->ring->size - head);
3102 head = 0;
3103 }
3104 memset(vaddr + head, 0, request->postfix - head);
Chris Wilsonc0d5f322017-01-10 17:22:43 +00003105
3106 dma_fence_set_error(&request->fence, -EIO);
Chris Wilson4db080f2013-12-04 11:37:09 +00003107}
3108
Chris Wilsone61e0f52018-02-21 09:56:36 +00003109static void engine_skip_context(struct i915_request *request)
Mika Kuoppala36193ac2017-01-17 17:59:02 +02003110{
3111 struct intel_engine_cs *engine = request->engine;
3112 struct i915_gem_context *hung_ctx = request->ctx;
3113 struct intel_timeline *timeline;
3114 unsigned long flags;
3115
3116 timeline = i915_gem_context_lookup_timeline(hung_ctx, engine);
3117
3118 spin_lock_irqsave(&engine->timeline->lock, flags);
3119 spin_lock(&timeline->lock);
3120
3121 list_for_each_entry_continue(request, &engine->timeline->requests, link)
3122 if (request->ctx == hung_ctx)
3123 skip_request(request);
3124
3125 list_for_each_entry(request, &timeline->requests, link)
3126 skip_request(request);
3127
3128 spin_unlock(&timeline->lock);
3129 spin_unlock_irqrestore(&engine->timeline->lock, flags);
3130}
3131
Chris Wilsond1d1ebf42017-07-21 13:32:33 +01003132/* Returns the request if it was guilty of the hang */
Chris Wilsone61e0f52018-02-21 09:56:36 +00003133static struct i915_request *
Chris Wilsond1d1ebf42017-07-21 13:32:33 +01003134i915_gem_reset_request(struct intel_engine_cs *engine,
Chris Wilsonbba08692018-04-06 23:03:53 +01003135 struct i915_request *request,
3136 bool stalled)
Mika Kuoppala61da5362017-01-17 17:59:05 +02003137{
Mika Kuoppala71895a02017-01-17 17:59:07 +02003138 /* The guilty request will get skipped on a hung engine.
3139 *
3140 * Users of client default contexts do not rely on logical
3141 * state preserved between batches so it is safe to execute
3142 * queued requests following the hang. Non default contexts
3143 * rely on preserved state, so skipping a batch loses the
3144 * evolution of the state and it needs to be considered corrupted.
3145 * Executing more queued batches on top of corrupted state is
3146 * risky. But we take the risk by trying to advance through
3147 * the queued requests in order to make the client behaviour
3148 * more predictable around resets, by not throwing away random
3149 * amount of batches it has prepared for execution. Sophisticated
3150 * clients can use gem_reset_stats_ioctl and dma fence status
3151 * (exported via sync_file info ioctl on explicit fences) to observe
3152 * when it loses the context state and should rebuild accordingly.
3153 *
3154 * The context ban, and ultimately the client ban, mechanism are safety
3155 * valves if client submission ends up resulting in nothing more than
3156 * subsequent hangs.
3157 */
3158
Chris Wilsonbba08692018-04-06 23:03:53 +01003159 if (i915_request_completed(request)) {
3160 GEM_TRACE("%s pardoned global=%d (fence %llx:%d), current %d\n",
3161 engine->name, request->global_seqno,
3162 request->fence.context, request->fence.seqno,
3163 intel_engine_get_seqno(engine));
3164 stalled = false;
3165 }
3166
3167 if (stalled) {
Mika Kuoppala61da5362017-01-17 17:59:05 +02003168 i915_gem_context_mark_guilty(request->ctx);
3169 skip_request(request);
Chris Wilsond1d1ebf42017-07-21 13:32:33 +01003170
3171 /* If this context is now banned, skip all pending requests. */
3172 if (i915_gem_context_is_banned(request->ctx))
3173 engine_skip_context(request);
Mika Kuoppala61da5362017-01-17 17:59:05 +02003174 } else {
Chris Wilsond1d1ebf42017-07-21 13:32:33 +01003175 /*
3176 * Since this is not the hung engine, it may have advanced
3177 * since the hang declaration. Double check by refinding
3178 * the active request at the time of the reset.
3179 */
3180 request = i915_gem_find_active_request(engine);
3181 if (request) {
3182 i915_gem_context_mark_innocent(request->ctx);
3183 dma_fence_set_error(&request->fence, -EAGAIN);
3184
3185 /* Rewind the engine to replay the incomplete rq */
3186 spin_lock_irq(&engine->timeline->lock);
3187 request = list_prev_entry(request, link);
3188 if (&request->link == &engine->timeline->requests)
3189 request = NULL;
3190 spin_unlock_irq(&engine->timeline->lock);
3191 }
Mika Kuoppala61da5362017-01-17 17:59:05 +02003192 }
3193
Chris Wilsond1d1ebf42017-07-21 13:32:33 +01003194 return request;
Mika Kuoppala61da5362017-01-17 17:59:05 +02003195}
3196
Michel Thierrya1ef70e2017-06-20 10:57:47 +01003197void i915_gem_reset_engine(struct intel_engine_cs *engine,
Chris Wilsonbba08692018-04-06 23:03:53 +01003198 struct i915_request *request,
3199 bool stalled)
Chris Wilson4db080f2013-12-04 11:37:09 +00003200{
Chris Wilsonfcb1de52017-12-19 09:01:10 +00003201 /*
3202 * Make sure this write is visible before we re-enable the interrupt
3203 * handlers on another CPU, as tasklet_enable() resolves to just
3204 * a compiler barrier which is insufficient for our purpose here.
3205 */
3206 smp_store_mb(engine->irq_posted, 0);
Chris Wilsoned454f22017-07-21 13:32:29 +01003207
Chris Wilsond1d1ebf42017-07-21 13:32:33 +01003208 if (request)
Chris Wilsonbba08692018-04-06 23:03:53 +01003209 request = i915_gem_reset_request(engine, request, stalled);
Chris Wilsond1d1ebf42017-07-21 13:32:33 +01003210
3211 if (request) {
Chris Wilsonc0dcb202017-02-07 15:24:37 +00003212 DRM_DEBUG_DRIVER("resetting %s to restart from tail of request 0x%x\n",
3213 engine->name, request->global_seqno);
Chris Wilsonc0dcb202017-02-07 15:24:37 +00003214 }
Chris Wilson821ed7d2016-09-09 14:11:53 +01003215
3216 /* Setup the CS to resume from the breadcrumb of the hung request */
3217 engine->reset_hw(engine, request);
Chris Wilson821ed7d2016-09-09 14:11:53 +01003218}
3219
Chris Wilsond0667e92018-04-06 23:03:54 +01003220void i915_gem_reset(struct drm_i915_private *dev_priv,
3221 unsigned int stalled_mask)
Chris Wilson821ed7d2016-09-09 14:11:53 +01003222{
3223 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05303224 enum intel_engine_id id;
Chris Wilson821ed7d2016-09-09 14:11:53 +01003225
Chris Wilson4c7d62c2016-10-28 13:58:32 +01003226 lockdep_assert_held(&dev_priv->drm.struct_mutex);
3227
Chris Wilsone61e0f52018-02-21 09:56:36 +00003228 i915_retire_requests(dev_priv);
Chris Wilson821ed7d2016-09-09 14:11:53 +01003229
Chris Wilson2ae55732017-02-12 17:20:02 +00003230 for_each_engine(engine, dev_priv, id) {
3231 struct i915_gem_context *ctx;
3232
Chris Wilsonbba08692018-04-06 23:03:53 +01003233 i915_gem_reset_engine(engine,
3234 engine->hangcheck.active_request,
Chris Wilsond0667e92018-04-06 23:03:54 +01003235 stalled_mask & ENGINE_MASK(id));
Chris Wilson2ae55732017-02-12 17:20:02 +00003236 ctx = fetch_and_zero(&engine->last_retired_context);
3237 if (ctx)
Chris Wilsonab82a062018-04-30 14:15:01 +01003238 intel_context_unpin(ctx, engine);
Chris Wilson7b6da812017-12-16 00:03:34 +00003239
3240 /*
3241 * Ostensibily, we always want a context loaded for powersaving,
3242 * so if the engine is idle after the reset, send a request
3243 * to load our scratch kernel_context.
3244 *
3245 * More mysteriously, if we leave the engine idle after a reset,
3246 * the next userspace batch may hang, with what appears to be
3247 * an incoherent read by the CS (presumably stale TLB). An
3248 * empty request appears sufficient to paper over the glitch.
3249 */
Chris Wilson01b8fdc2018-02-05 15:24:31 +00003250 if (intel_engine_is_idle(engine)) {
Chris Wilsone61e0f52018-02-21 09:56:36 +00003251 struct i915_request *rq;
Chris Wilson7b6da812017-12-16 00:03:34 +00003252
Chris Wilsone61e0f52018-02-21 09:56:36 +00003253 rq = i915_request_alloc(engine,
3254 dev_priv->kernel_context);
Chris Wilson7b6da812017-12-16 00:03:34 +00003255 if (!IS_ERR(rq))
Chris Wilsone61e0f52018-02-21 09:56:36 +00003256 __i915_request_add(rq, false);
Chris Wilson7b6da812017-12-16 00:03:34 +00003257 }
Chris Wilson2ae55732017-02-12 17:20:02 +00003258 }
Chris Wilson821ed7d2016-09-09 14:11:53 +01003259
Tvrtko Ursulin4362f4f2016-11-16 08:55:33 +00003260 i915_gem_restore_fences(dev_priv);
Chris Wilson821ed7d2016-09-09 14:11:53 +01003261}
3262
Michel Thierrya1ef70e2017-06-20 10:57:47 +01003263void i915_gem_reset_finish_engine(struct intel_engine_cs *engine)
3264{
Sagar Arun Kamblec6dce8f2017-11-16 19:02:37 +05303265 tasklet_enable(&engine->execlists.tasklet);
Michel Thierrya1ef70e2017-06-20 10:57:47 +01003266 kthread_unpark(engine->breadcrumbs.signaler);
Chris Wilson1749d902017-10-09 12:02:59 +01003267
3268 intel_uncore_forcewake_put(engine->i915, FORCEWAKE_ALL);
Michel Thierrya1ef70e2017-06-20 10:57:47 +01003269}
3270
Chris Wilsond8027092017-02-08 14:30:32 +00003271void i915_gem_reset_finish(struct drm_i915_private *dev_priv)
3272{
Chris Wilson1f7b8472017-02-08 14:30:33 +00003273 struct intel_engine_cs *engine;
3274 enum intel_engine_id id;
3275
Chris Wilsond8027092017-02-08 14:30:32 +00003276 lockdep_assert_held(&dev_priv->drm.struct_mutex);
Chris Wilson1f7b8472017-02-08 14:30:33 +00003277
Chris Wilsonfe3288b2017-02-12 17:20:01 +00003278 for_each_engine(engine, dev_priv, id) {
Michel Thierryc64992e2017-06-20 10:57:44 +01003279 engine->hangcheck.active_request = NULL;
Michel Thierrya1ef70e2017-06-20 10:57:47 +01003280 i915_gem_reset_finish_engine(engine);
Chris Wilsonfe3288b2017-02-12 17:20:01 +00003281 }
Chris Wilsond8027092017-02-08 14:30:32 +00003282}
3283
Chris Wilsone61e0f52018-02-21 09:56:36 +00003284static void nop_submit_request(struct i915_request *request)
Chris Wilson821ed7d2016-09-09 14:11:53 +01003285{
Chris Wilsond9b13c42018-03-15 13:14:50 +00003286 GEM_TRACE("%s fence %llx:%d -> -EIO\n",
3287 request->engine->name,
3288 request->fence.context, request->fence.seqno);
Daniel Vetteraf7a8ff2017-10-11 11:10:19 +02003289 dma_fence_set_error(&request->fence, -EIO);
3290
Chris Wilsone61e0f52018-02-21 09:56:36 +00003291 i915_request_submit(request);
Daniel Vetteraf7a8ff2017-10-11 11:10:19 +02003292}
3293
Chris Wilsone61e0f52018-02-21 09:56:36 +00003294static void nop_complete_submit_request(struct i915_request *request)
Daniel Vetteraf7a8ff2017-10-11 11:10:19 +02003295{
Chris Wilson8d550822017-10-06 12:56:17 +01003296 unsigned long flags;
3297
Chris Wilsond9b13c42018-03-15 13:14:50 +00003298 GEM_TRACE("%s fence %llx:%d -> -EIO\n",
3299 request->engine->name,
3300 request->fence.context, request->fence.seqno);
Chris Wilson3cd94422017-01-10 17:22:45 +00003301 dma_fence_set_error(&request->fence, -EIO);
Chris Wilson8d550822017-10-06 12:56:17 +01003302
3303 spin_lock_irqsave(&request->engine->timeline->lock, flags);
Chris Wilsone61e0f52018-02-21 09:56:36 +00003304 __i915_request_submit(request);
Chris Wilson3dcf93f72016-11-22 14:41:20 +00003305 intel_engine_init_global_seqno(request->engine, request->global_seqno);
Chris Wilson8d550822017-10-06 12:56:17 +01003306 spin_unlock_irqrestore(&request->engine->timeline->lock, flags);
Chris Wilson821ed7d2016-09-09 14:11:53 +01003307}
3308
Daniel Vetteraf7a8ff2017-10-11 11:10:19 +02003309void i915_gem_set_wedged(struct drm_i915_private *i915)
Chris Wilson821ed7d2016-09-09 14:11:53 +01003310{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00003311 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05303312 enum intel_engine_id id;
Eric Anholt673a3942008-07-30 12:06:12 -07003313
Chris Wilsond9b13c42018-03-15 13:14:50 +00003314 GEM_TRACE("start\n");
3315
Chris Wilson7f961d72018-04-26 11:32:19 +01003316 if (GEM_SHOW_DEBUG()) {
Chris Wilson559e0402018-02-05 09:21:59 +00003317 struct drm_printer p = drm_debug_printer(__func__);
3318
3319 for_each_engine(engine, i915, id)
3320 intel_engine_dump(engine, &p, "%s\n", engine->name);
3321 }
3322
Chris Wilson0d73e7a2018-02-07 15:13:50 +00003323 set_bit(I915_WEDGED, &i915->gpu_error.flags);
3324 smp_mb__after_atomic();
3325
Daniel Vetteraf7a8ff2017-10-11 11:10:19 +02003326 /*
3327 * First, stop submission to hw, but do not yet complete requests by
3328 * rolling the global seqno forward (since this would complete requests
3329 * for which we haven't set the fence error to EIO yet).
3330 */
Chris Wilson963ddd62018-03-02 11:33:24 +00003331 for_each_engine(engine, i915, id) {
3332 i915_gem_reset_prepare_engine(engine);
Chris Wilson47650db2018-03-07 13:42:25 +00003333
Daniel Vetteraf7a8ff2017-10-11 11:10:19 +02003334 engine->submit_request = nop_submit_request;
Chris Wilson47650db2018-03-07 13:42:25 +00003335 engine->schedule = NULL;
Chris Wilson963ddd62018-03-02 11:33:24 +00003336 }
Chris Wilson47650db2018-03-07 13:42:25 +00003337 i915->caps.scheduler = 0;
Daniel Vetteraf7a8ff2017-10-11 11:10:19 +02003338
Chris Wilsonac697ae2018-03-15 15:10:15 +00003339 /* Even if the GPU reset fails, it should still stop the engines */
3340 intel_gpu_reset(i915, ALL_ENGINES);
3341
Daniel Vetteraf7a8ff2017-10-11 11:10:19 +02003342 /*
3343 * Make sure no one is running the old callback before we proceed with
3344 * cancelling requests and resetting the completion tracking. Otherwise
3345 * we might submit a request to the hardware which never completes.
3346 */
3347 synchronize_rcu();
3348
3349 for_each_engine(engine, i915, id) {
3350 /* Mark all executing requests as skipped */
3351 engine->cancel_requests(engine);
3352
3353 /*
3354 * Only once we've force-cancelled all in-flight requests can we
3355 * start to complete all requests.
3356 */
3357 engine->submit_request = nop_complete_submit_request;
3358 }
3359
3360 /*
3361 * Make sure no request can slip through without getting completed by
3362 * either this call here to intel_engine_init_global_seqno, or the one
3363 * in nop_complete_submit_request.
3364 */
3365 synchronize_rcu();
3366
3367 for_each_engine(engine, i915, id) {
3368 unsigned long flags;
3369
Chris Wilson0d73e7a2018-02-07 15:13:50 +00003370 /*
3371 * Mark all pending requests as complete so that any concurrent
Daniel Vetteraf7a8ff2017-10-11 11:10:19 +02003372 * (lockless) lookup doesn't try and wait upon the request as we
3373 * reset it.
3374 */
3375 spin_lock_irqsave(&engine->timeline->lock, flags);
3376 intel_engine_init_global_seqno(engine,
3377 intel_engine_last_submit(engine));
3378 spin_unlock_irqrestore(&engine->timeline->lock, flags);
Chris Wilson963ddd62018-03-02 11:33:24 +00003379
3380 i915_gem_reset_finish_engine(engine);
Daniel Vetteraf7a8ff2017-10-11 11:10:19 +02003381 }
Chris Wilson20e49332016-11-22 14:41:21 +00003382
Chris Wilsond9b13c42018-03-15 13:14:50 +00003383 GEM_TRACE("end\n");
3384
Chris Wilson3d7adbb2017-07-21 13:32:27 +01003385 wake_up_all(&i915->gpu_error.reset_queue);
Eric Anholt673a3942008-07-30 12:06:12 -07003386}
3387
Chris Wilson2e8f9d32017-03-16 17:13:04 +00003388bool i915_gem_unset_wedged(struct drm_i915_private *i915)
3389{
3390 struct i915_gem_timeline *tl;
3391 int i;
3392
3393 lockdep_assert_held(&i915->drm.struct_mutex);
3394 if (!test_bit(I915_WEDGED, &i915->gpu_error.flags))
3395 return true;
3396
Chris Wilsond9b13c42018-03-15 13:14:50 +00003397 GEM_TRACE("start\n");
3398
Chris Wilson2d4ecac2018-03-07 13:42:21 +00003399 /*
3400 * Before unwedging, make sure that all pending operations
Chris Wilson2e8f9d32017-03-16 17:13:04 +00003401 * are flushed and errored out - we may have requests waiting upon
3402 * third party fences. We marked all inflight requests as EIO, and
3403 * every execbuf since returned EIO, for consistency we want all
3404 * the currently pending requests to also be marked as EIO, which
3405 * is done inside our nop_submit_request - and so we must wait.
3406 *
3407 * No more can be submitted until we reset the wedged bit.
3408 */
3409 list_for_each_entry(tl, &i915->gt.timelines, link) {
3410 for (i = 0; i < ARRAY_SIZE(tl->engine); i++) {
Chris Wilsone61e0f52018-02-21 09:56:36 +00003411 struct i915_request *rq;
Chris Wilson2e8f9d32017-03-16 17:13:04 +00003412
3413 rq = i915_gem_active_peek(&tl->engine[i].last_request,
3414 &i915->drm.struct_mutex);
3415 if (!rq)
3416 continue;
3417
Chris Wilson2d4ecac2018-03-07 13:42:21 +00003418 /*
3419 * We can't use our normal waiter as we want to
Chris Wilson2e8f9d32017-03-16 17:13:04 +00003420 * avoid recursively trying to handle the current
3421 * reset. The basic dma_fence_default_wait() installs
3422 * a callback for dma_fence_signal(), which is
3423 * triggered by our nop handler (indirectly, the
3424 * callback enables the signaler thread which is
3425 * woken by the nop_submit_request() advancing the seqno
3426 * and when the seqno passes the fence, the signaler
3427 * then signals the fence waking us up).
3428 */
3429 if (dma_fence_default_wait(&rq->fence, true,
3430 MAX_SCHEDULE_TIMEOUT) < 0)
3431 return false;
3432 }
3433 }
Chris Wilson2d4ecac2018-03-07 13:42:21 +00003434 i915_retire_requests(i915);
3435 GEM_BUG_ON(i915->gt.active_requests);
Chris Wilson2e8f9d32017-03-16 17:13:04 +00003436
Chris Wilson2d4ecac2018-03-07 13:42:21 +00003437 /*
3438 * Undo nop_submit_request. We prevent all new i915 requests from
Chris Wilson2e8f9d32017-03-16 17:13:04 +00003439 * being queued (by disallowing execbuf whilst wedged) so having
3440 * waited for all active requests above, we know the system is idle
3441 * and do not have to worry about a thread being inside
3442 * engine->submit_request() as we swap over. So unlike installing
3443 * the nop_submit_request on reset, we can do this from normal
3444 * context and do not require stop_machine().
3445 */
3446 intel_engines_reset_default_submission(i915);
Chris Wilson36703e72017-06-22 11:56:25 +01003447 i915_gem_contexts_lost(i915);
Chris Wilson2e8f9d32017-03-16 17:13:04 +00003448
Chris Wilsond9b13c42018-03-15 13:14:50 +00003449 GEM_TRACE("end\n");
3450
Chris Wilson2e8f9d32017-03-16 17:13:04 +00003451 smp_mb__before_atomic(); /* complete takeover before enabling execbuf */
3452 clear_bit(I915_WEDGED, &i915->gpu_error.flags);
3453
3454 return true;
3455}
3456
Daniel Vetter75ef9da2010-08-21 00:25:16 +02003457static void
Eric Anholt673a3942008-07-30 12:06:12 -07003458i915_gem_retire_work_handler(struct work_struct *work)
3459{
Chris Wilsonb29c19b2013-09-25 17:34:56 +01003460 struct drm_i915_private *dev_priv =
Chris Wilson67d97da2016-07-04 08:08:31 +01003461 container_of(work, typeof(*dev_priv), gt.retire_work.work);
Chris Wilson91c8a322016-07-05 10:40:23 +01003462 struct drm_device *dev = &dev_priv->drm;
Eric Anholt673a3942008-07-30 12:06:12 -07003463
Chris Wilson891b48c2010-09-29 12:26:37 +01003464 /* Come back later if the device is busy... */
Chris Wilsonb29c19b2013-09-25 17:34:56 +01003465 if (mutex_trylock(&dev->struct_mutex)) {
Chris Wilsone61e0f52018-02-21 09:56:36 +00003466 i915_retire_requests(dev_priv);
Chris Wilsonb29c19b2013-09-25 17:34:56 +01003467 mutex_unlock(&dev->struct_mutex);
3468 }
Chris Wilson67d97da2016-07-04 08:08:31 +01003469
Chris Wilson88923042018-01-29 14:41:04 +00003470 /*
3471 * Keep the retire handler running until we are finally idle.
Chris Wilson67d97da2016-07-04 08:08:31 +01003472 * We do not need to do this test under locking as in the worst-case
3473 * we queue the retire worker once too often.
3474 */
Chris Wilson88923042018-01-29 14:41:04 +00003475 if (READ_ONCE(dev_priv->gt.awake))
Chris Wilson67d97da2016-07-04 08:08:31 +01003476 queue_delayed_work(dev_priv->wq,
3477 &dev_priv->gt.retire_work,
Chris Wilsonbcb45082012-10-05 17:02:57 +01003478 round_jiffies_up_relative(HZ));
Chris Wilsonb29c19b2013-09-25 17:34:56 +01003479}
Chris Wilson891b48c2010-09-29 12:26:37 +01003480
Chris Wilson84a10742018-01-24 11:36:08 +00003481static void shrink_caches(struct drm_i915_private *i915)
3482{
3483 /*
3484 * kmem_cache_shrink() discards empty slabs and reorders partially
3485 * filled slabs to prioritise allocating from the mostly full slabs,
3486 * with the aim of reducing fragmentation.
3487 */
3488 kmem_cache_shrink(i915->priorities);
3489 kmem_cache_shrink(i915->dependencies);
3490 kmem_cache_shrink(i915->requests);
3491 kmem_cache_shrink(i915->luts);
3492 kmem_cache_shrink(i915->vmas);
3493 kmem_cache_shrink(i915->objects);
3494}
3495
3496struct sleep_rcu_work {
3497 union {
3498 struct rcu_head rcu;
3499 struct work_struct work;
3500 };
3501 struct drm_i915_private *i915;
3502 unsigned int epoch;
3503};
3504
3505static inline bool
3506same_epoch(struct drm_i915_private *i915, unsigned int epoch)
3507{
3508 /*
3509 * There is a small chance that the epoch wrapped since we started
3510 * sleeping. If we assume that epoch is at least a u32, then it will
3511 * take at least 2^32 * 100ms for it to wrap, or about 326 years.
3512 */
3513 return epoch == READ_ONCE(i915->gt.epoch);
3514}
3515
3516static void __sleep_work(struct work_struct *work)
3517{
3518 struct sleep_rcu_work *s = container_of(work, typeof(*s), work);
3519 struct drm_i915_private *i915 = s->i915;
3520 unsigned int epoch = s->epoch;
3521
3522 kfree(s);
3523 if (same_epoch(i915, epoch))
3524 shrink_caches(i915);
3525}
3526
3527static void __sleep_rcu(struct rcu_head *rcu)
3528{
3529 struct sleep_rcu_work *s = container_of(rcu, typeof(*s), rcu);
3530 struct drm_i915_private *i915 = s->i915;
3531
3532 if (same_epoch(i915, s->epoch)) {
3533 INIT_WORK(&s->work, __sleep_work);
3534 queue_work(i915->wq, &s->work);
3535 } else {
3536 kfree(s);
3537 }
3538}
3539
Chris Wilson5427f202017-10-23 22:32:34 +01003540static inline bool
3541new_requests_since_last_retire(const struct drm_i915_private *i915)
3542{
3543 return (READ_ONCE(i915->gt.active_requests) ||
3544 work_pending(&i915->gt.idle_work.work));
3545}
3546
Chris Wilsonb29c19b2013-09-25 17:34:56 +01003547static void
3548i915_gem_idle_work_handler(struct work_struct *work)
3549{
3550 struct drm_i915_private *dev_priv =
Chris Wilson67d97da2016-07-04 08:08:31 +01003551 container_of(work, typeof(*dev_priv), gt.idle_work.work);
Chris Wilson84a10742018-01-24 11:36:08 +00003552 unsigned int epoch = I915_EPOCH_INVALID;
Chris Wilson67d97da2016-07-04 08:08:31 +01003553 bool rearm_hangcheck;
3554
3555 if (!READ_ONCE(dev_priv->gt.awake))
3556 return;
3557
Imre Deak0cb56702016-11-07 11:20:04 +02003558 /*
3559 * Wait for last execlists context complete, but bail out in case a
Chris Wilsonffed7bd2018-03-01 10:33:38 +00003560 * new request is submitted. As we don't trust the hardware, we
3561 * continue on if the wait times out. This is necessary to allow
3562 * the machine to suspend even if the hardware dies, and we will
3563 * try to recover in resume (after depriving the hardware of power,
3564 * it may be in a better mmod).
Imre Deak0cb56702016-11-07 11:20:04 +02003565 */
Chris Wilsonffed7bd2018-03-01 10:33:38 +00003566 __wait_for(if (new_requests_since_last_retire(dev_priv)) return,
3567 intel_engines_are_idle(dev_priv),
3568 I915_IDLE_ENGINES_TIMEOUT * 1000,
3569 10, 500);
Chris Wilson67d97da2016-07-04 08:08:31 +01003570
3571 rearm_hangcheck =
3572 cancel_delayed_work_sync(&dev_priv->gpu_error.hangcheck_work);
3573
Chris Wilson5427f202017-10-23 22:32:34 +01003574 if (!mutex_trylock(&dev_priv->drm.struct_mutex)) {
Chris Wilson67d97da2016-07-04 08:08:31 +01003575 /* Currently busy, come back later */
3576 mod_delayed_work(dev_priv->wq,
3577 &dev_priv->gt.idle_work,
3578 msecs_to_jiffies(50));
3579 goto out_rearm;
3580 }
3581
Imre Deak93c97dc2016-11-07 11:20:03 +02003582 /*
3583 * New request retired after this work handler started, extend active
3584 * period until next instance of the work.
3585 */
Chris Wilson5427f202017-10-23 22:32:34 +01003586 if (new_requests_since_last_retire(dev_priv))
Imre Deak93c97dc2016-11-07 11:20:03 +02003587 goto out_unlock;
3588
Chris Wilsone4d20062018-04-06 16:51:44 +01003589 epoch = __i915_gem_park(dev_priv);
Chris Wilsonff320d62017-10-23 22:32:35 +01003590
Chris Wilson67d97da2016-07-04 08:08:31 +01003591 rearm_hangcheck = false;
Chris Wilson67d97da2016-07-04 08:08:31 +01003592out_unlock:
Chris Wilson5427f202017-10-23 22:32:34 +01003593 mutex_unlock(&dev_priv->drm.struct_mutex);
Chris Wilson35c94182015-04-07 16:20:37 +01003594
Chris Wilson67d97da2016-07-04 08:08:31 +01003595out_rearm:
3596 if (rearm_hangcheck) {
3597 GEM_BUG_ON(!dev_priv->gt.awake);
3598 i915_queue_hangcheck(dev_priv);
Chris Wilson35c94182015-04-07 16:20:37 +01003599 }
Chris Wilson84a10742018-01-24 11:36:08 +00003600
3601 /*
3602 * When we are idle, it is an opportune time to reap our caches.
3603 * However, we have many objects that utilise RCU and the ordered
3604 * i915->wq that this work is executing on. To try and flush any
3605 * pending frees now we are idle, we first wait for an RCU grace
3606 * period, and then queue a task (that will run last on the wq) to
3607 * shrink and re-optimize the caches.
3608 */
3609 if (same_epoch(dev_priv, epoch)) {
3610 struct sleep_rcu_work *s = kmalloc(sizeof(*s), GFP_KERNEL);
3611 if (s) {
3612 s->i915 = dev_priv;
3613 s->epoch = epoch;
3614 call_rcu(&s->rcu, __sleep_rcu);
3615 }
3616 }
Eric Anholt673a3942008-07-30 12:06:12 -07003617}
3618
Chris Wilsonb1f788c2016-08-04 07:52:45 +01003619void i915_gem_close_object(struct drm_gem_object *gem, struct drm_file *file)
3620{
Chris Wilsond1b48c12017-08-16 09:52:08 +01003621 struct drm_i915_private *i915 = to_i915(gem->dev);
Chris Wilsonb1f788c2016-08-04 07:52:45 +01003622 struct drm_i915_gem_object *obj = to_intel_bo(gem);
3623 struct drm_i915_file_private *fpriv = file->driver_priv;
Chris Wilsond1b48c12017-08-16 09:52:08 +01003624 struct i915_lut_handle *lut, *ln;
Chris Wilsonb1f788c2016-08-04 07:52:45 +01003625
Chris Wilsond1b48c12017-08-16 09:52:08 +01003626 mutex_lock(&i915->drm.struct_mutex);
3627
3628 list_for_each_entry_safe(lut, ln, &obj->lut_list, obj_link) {
3629 struct i915_gem_context *ctx = lut->ctx;
3630 struct i915_vma *vma;
3631
Chris Wilson432295d2017-08-22 12:05:15 +01003632 GEM_BUG_ON(ctx->file_priv == ERR_PTR(-EBADF));
Chris Wilsond1b48c12017-08-16 09:52:08 +01003633 if (ctx->file_priv != fpriv)
3634 continue;
3635
3636 vma = radix_tree_delete(&ctx->handles_vma, lut->handle);
Chris Wilson3ffff012017-08-22 12:05:17 +01003637 GEM_BUG_ON(vma->obj != obj);
3638
3639 /* We allow the process to have multiple handles to the same
3640 * vma, in the same fd namespace, by virtue of flink/open.
3641 */
3642 GEM_BUG_ON(!vma->open_count);
3643 if (!--vma->open_count && !i915_vma_is_ggtt(vma))
Chris Wilsonb1f788c2016-08-04 07:52:45 +01003644 i915_vma_close(vma);
Chris Wilsonf8a7fde2016-10-28 13:58:29 +01003645
Chris Wilsond1b48c12017-08-16 09:52:08 +01003646 list_del(&lut->obj_link);
3647 list_del(&lut->ctx_link);
Chris Wilson4ff4b442017-06-16 15:05:16 +01003648
Chris Wilsond1b48c12017-08-16 09:52:08 +01003649 kmem_cache_free(i915->luts, lut);
3650 __i915_gem_object_release_unless_active(obj);
Chris Wilsonf8a7fde2016-10-28 13:58:29 +01003651 }
Chris Wilsond1b48c12017-08-16 09:52:08 +01003652
3653 mutex_unlock(&i915->drm.struct_mutex);
Chris Wilsonb1f788c2016-08-04 07:52:45 +01003654}
3655
Chris Wilsone95433c2016-10-28 13:58:27 +01003656static unsigned long to_wait_timeout(s64 timeout_ns)
3657{
3658 if (timeout_ns < 0)
3659 return MAX_SCHEDULE_TIMEOUT;
3660
3661 if (timeout_ns == 0)
3662 return 0;
3663
3664 return nsecs_to_jiffies_timeout(timeout_ns);
3665}
3666
Ben Widawsky5816d642012-04-11 11:18:19 -07003667/**
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07003668 * i915_gem_wait_ioctl - implements DRM_IOCTL_I915_GEM_WAIT
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01003669 * @dev: drm device pointer
3670 * @data: ioctl data blob
3671 * @file: drm file pointer
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07003672 *
3673 * Returns 0 if successful, else an error is returned with the remaining time in
3674 * the timeout parameter.
3675 * -ETIME: object is still busy after timeout
3676 * -ERESTARTSYS: signal interrupted the wait
3677 * -ENONENT: object doesn't exist
3678 * Also possible, but rare:
Chris Wilsonb8050142017-08-11 11:57:31 +01003679 * -EAGAIN: incomplete, restart syscall
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07003680 * -ENOMEM: damn
3681 * -ENODEV: Internal IRQ fail
3682 * -E?: The add request failed
3683 *
3684 * The wait ioctl with a timeout of 0 reimplements the busy ioctl. With any
3685 * non-zero timeout parameter the wait ioctl will wait for the given number of
3686 * nanoseconds on an object becoming unbusy. Since the wait itself does so
3687 * without holding struct_mutex the object may become re-busied before this
3688 * function completes. A similar but shorter * race condition exists in the busy
3689 * ioctl
3690 */
3691int
3692i915_gem_wait_ioctl(struct drm_device *dev, void *data, struct drm_file *file)
3693{
3694 struct drm_i915_gem_wait *args = data;
3695 struct drm_i915_gem_object *obj;
Chris Wilsone95433c2016-10-28 13:58:27 +01003696 ktime_t start;
3697 long ret;
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07003698
Daniel Vetter11b5d512014-09-29 15:31:26 +02003699 if (args->flags != 0)
3700 return -EINVAL;
3701
Chris Wilson03ac0642016-07-20 13:31:51 +01003702 obj = i915_gem_object_lookup(file, args->bo_handle);
Chris Wilson033d5492016-08-05 10:14:17 +01003703 if (!obj)
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07003704 return -ENOENT;
Chris Wilson033d5492016-08-05 10:14:17 +01003705
Chris Wilsone95433c2016-10-28 13:58:27 +01003706 start = ktime_get();
3707
3708 ret = i915_gem_object_wait(obj,
3709 I915_WAIT_INTERRUPTIBLE | I915_WAIT_ALL,
3710 to_wait_timeout(args->timeout_ns),
3711 to_rps_client(file));
3712
3713 if (args->timeout_ns > 0) {
3714 args->timeout_ns -= ktime_to_ns(ktime_sub(ktime_get(), start));
3715 if (args->timeout_ns < 0)
3716 args->timeout_ns = 0;
Chris Wilsonc1d20612017-02-16 12:54:41 +00003717
3718 /*
3719 * Apparently ktime isn't accurate enough and occasionally has a
3720 * bit of mismatch in the jiffies<->nsecs<->ktime loop. So patch
3721 * things up to make the test happy. We allow up to 1 jiffy.
3722 *
3723 * This is a regression from the timespec->ktime conversion.
3724 */
3725 if (ret == -ETIME && !nsecs_to_jiffies(args->timeout_ns))
3726 args->timeout_ns = 0;
Chris Wilsonb8050142017-08-11 11:57:31 +01003727
3728 /* Asked to wait beyond the jiffie/scheduler precision? */
3729 if (ret == -ETIME && args->timeout_ns)
3730 ret = -EAGAIN;
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07003731 }
3732
Chris Wilsonf0cd5182016-10-28 13:58:43 +01003733 i915_gem_object_put(obj);
John Harrisonff865882014-11-24 18:49:28 +00003734 return ret;
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07003735}
3736
Chris Wilson73cb9702016-10-28 13:58:46 +01003737static int wait_for_timeline(struct i915_gem_timeline *tl, unsigned int flags)
Daniel Vetter4df2faf2010-02-19 11:52:00 +01003738{
Chris Wilson73cb9702016-10-28 13:58:46 +01003739 int ret, i;
3740
3741 for (i = 0; i < ARRAY_SIZE(tl->engine); i++) {
3742 ret = i915_gem_active_wait(&tl->engine[i].last_request, flags);
3743 if (ret)
3744 return ret;
3745 }
3746
3747 return 0;
3748}
3749
Chris Wilson25112b62017-03-30 15:50:39 +01003750static int wait_for_engines(struct drm_i915_private *i915)
3751{
Chris Wilsonee42c002017-12-11 19:41:34 +00003752 if (wait_for(intel_engines_are_idle(i915), I915_IDLE_ENGINES_TIMEOUT)) {
Chris Wilson59e4b192017-12-11 19:41:35 +00003753 dev_err(i915->drm.dev,
3754 "Failed to idle engines, declaring wedged!\n");
Chris Wilson629820f2018-03-09 10:11:14 +00003755 GEM_TRACE_DUMP();
Chris Wilsoncad99462017-08-26 12:09:33 +01003756 i915_gem_set_wedged(i915);
3757 return -EIO;
Chris Wilson25112b62017-03-30 15:50:39 +01003758 }
3759
3760 return 0;
3761}
3762
Chris Wilson73cb9702016-10-28 13:58:46 +01003763int i915_gem_wait_for_idle(struct drm_i915_private *i915, unsigned int flags)
3764{
Dave Gordonb4ac5af2016-03-24 11:20:38 +00003765 int ret;
Daniel Vetter4df2faf2010-02-19 11:52:00 +01003766
Chris Wilson863e9fd2017-05-30 13:13:32 +01003767 /* If the device is asleep, we have no requests outstanding */
3768 if (!READ_ONCE(i915->gt.awake))
3769 return 0;
3770
Chris Wilson9caa34a2016-11-11 14:58:08 +00003771 if (flags & I915_WAIT_LOCKED) {
3772 struct i915_gem_timeline *tl;
3773
3774 lockdep_assert_held(&i915->drm.struct_mutex);
3775
3776 list_for_each_entry(tl, &i915->gt.timelines, link) {
3777 ret = wait_for_timeline(tl, flags);
3778 if (ret)
3779 return ret;
3780 }
Chris Wilsone61e0f52018-02-21 09:56:36 +00003781 i915_retire_requests(i915);
Chris Wilson25112b62017-03-30 15:50:39 +01003782
3783 ret = wait_for_engines(i915);
Chris Wilson9caa34a2016-11-11 14:58:08 +00003784 } else {
3785 ret = wait_for_timeline(&i915->gt.global_timeline, flags);
Chris Wilson1ec14ad2010-12-04 11:30:53 +00003786 }
Zou Nan haid1b851f2010-05-21 09:08:57 +08003787
Chris Wilson25112b62017-03-30 15:50:39 +01003788 return ret;
Daniel Vetter4df2faf2010-02-19 11:52:00 +01003789}
3790
Chris Wilson5a97bcc2017-02-22 11:40:46 +00003791static void __i915_gem_object_flush_for_display(struct drm_i915_gem_object *obj)
3792{
Chris Wilsone27ab732017-06-15 13:38:49 +01003793 /*
3794 * We manually flush the CPU domain so that we can override and
3795 * force the flush for the display, and perform it asyncrhonously.
3796 */
3797 flush_write_domain(obj, ~I915_GEM_DOMAIN_CPU);
3798 if (obj->cache_dirty)
3799 i915_gem_clflush_object(obj, I915_CLFLUSH_FORCE);
Christian Königc0a51fd2018-02-16 13:43:38 +01003800 obj->write_domain = 0;
Chris Wilson5a97bcc2017-02-22 11:40:46 +00003801}
3802
3803void i915_gem_object_flush_if_display(struct drm_i915_gem_object *obj)
3804{
Chris Wilsonbd3d2252017-10-13 21:26:14 +01003805 if (!READ_ONCE(obj->pin_global))
Chris Wilson5a97bcc2017-02-22 11:40:46 +00003806 return;
3807
3808 mutex_lock(&obj->base.dev->struct_mutex);
3809 __i915_gem_object_flush_for_display(obj);
3810 mutex_unlock(&obj->base.dev->struct_mutex);
3811}
3812
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003813/**
Chris Wilsone22d8e32017-04-12 12:01:11 +01003814 * Moves a single object to the WC read, and possibly write domain.
3815 * @obj: object to act on
3816 * @write: ask for write access or read only
3817 *
3818 * This function returns when the move is complete, including waiting on
3819 * flushes to occur.
3820 */
3821int
3822i915_gem_object_set_to_wc_domain(struct drm_i915_gem_object *obj, bool write)
3823{
3824 int ret;
3825
3826 lockdep_assert_held(&obj->base.dev->struct_mutex);
3827
3828 ret = i915_gem_object_wait(obj,
3829 I915_WAIT_INTERRUPTIBLE |
3830 I915_WAIT_LOCKED |
3831 (write ? I915_WAIT_ALL : 0),
3832 MAX_SCHEDULE_TIMEOUT,
3833 NULL);
3834 if (ret)
3835 return ret;
3836
Christian Königc0a51fd2018-02-16 13:43:38 +01003837 if (obj->write_domain == I915_GEM_DOMAIN_WC)
Chris Wilsone22d8e32017-04-12 12:01:11 +01003838 return 0;
3839
3840 /* Flush and acquire obj->pages so that we are coherent through
3841 * direct access in memory with previous cached writes through
3842 * shmemfs and that our cache domain tracking remains valid.
3843 * For example, if the obj->filp was moved to swap without us
3844 * being notified and releasing the pages, we would mistakenly
3845 * continue to assume that the obj remained out of the CPU cached
3846 * domain.
3847 */
3848 ret = i915_gem_object_pin_pages(obj);
3849 if (ret)
3850 return ret;
3851
3852 flush_write_domain(obj, ~I915_GEM_DOMAIN_WC);
3853
3854 /* Serialise direct access to this object with the barriers for
3855 * coherent writes from the GPU, by effectively invalidating the
3856 * WC domain upon first access.
3857 */
Christian Königc0a51fd2018-02-16 13:43:38 +01003858 if ((obj->read_domains & I915_GEM_DOMAIN_WC) == 0)
Chris Wilsone22d8e32017-04-12 12:01:11 +01003859 mb();
3860
3861 /* It should now be out of any other write domains, and we can update
3862 * the domain values for our changes.
3863 */
Christian Königc0a51fd2018-02-16 13:43:38 +01003864 GEM_BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_WC) != 0);
3865 obj->read_domains |= I915_GEM_DOMAIN_WC;
Chris Wilsone22d8e32017-04-12 12:01:11 +01003866 if (write) {
Christian Königc0a51fd2018-02-16 13:43:38 +01003867 obj->read_domains = I915_GEM_DOMAIN_WC;
3868 obj->write_domain = I915_GEM_DOMAIN_WC;
Chris Wilsone22d8e32017-04-12 12:01:11 +01003869 obj->mm.dirty = true;
3870 }
3871
3872 i915_gem_object_unpin_pages(obj);
3873 return 0;
3874}
3875
3876/**
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003877 * Moves a single object to the GTT read, and possibly write domain.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01003878 * @obj: object to act on
3879 * @write: ask for write access or read only
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003880 *
3881 * This function returns when the move is complete, including waiting on
3882 * flushes to occur.
3883 */
Jesse Barnes79e53942008-11-07 14:24:08 -08003884int
Chris Wilson20217462010-11-23 15:26:33 +00003885i915_gem_object_set_to_gtt_domain(struct drm_i915_gem_object *obj, bool write)
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003886{
Eric Anholte47c68e2008-11-14 13:35:19 -08003887 int ret;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003888
Chris Wilsone95433c2016-10-28 13:58:27 +01003889 lockdep_assert_held(&obj->base.dev->struct_mutex);
Chris Wilson4c7d62c2016-10-28 13:58:32 +01003890
Chris Wilsone95433c2016-10-28 13:58:27 +01003891 ret = i915_gem_object_wait(obj,
3892 I915_WAIT_INTERRUPTIBLE |
3893 I915_WAIT_LOCKED |
3894 (write ? I915_WAIT_ALL : 0),
3895 MAX_SCHEDULE_TIMEOUT,
3896 NULL);
Chris Wilson88241782011-01-07 17:09:48 +00003897 if (ret)
3898 return ret;
3899
Christian Königc0a51fd2018-02-16 13:43:38 +01003900 if (obj->write_domain == I915_GEM_DOMAIN_GTT)
Chris Wilsonc13d87e2016-07-20 09:21:15 +01003901 return 0;
3902
Chris Wilson43566de2015-01-02 16:29:29 +05303903 /* Flush and acquire obj->pages so that we are coherent through
3904 * direct access in memory with previous cached writes through
3905 * shmemfs and that our cache domain tracking remains valid.
3906 * For example, if the obj->filp was moved to swap without us
3907 * being notified and releasing the pages, we would mistakenly
3908 * continue to assume that the obj remained out of the CPU cached
3909 * domain.
3910 */
Chris Wilsona4f5ea62016-10-28 13:58:35 +01003911 ret = i915_gem_object_pin_pages(obj);
Chris Wilson43566de2015-01-02 16:29:29 +05303912 if (ret)
3913 return ret;
3914
Chris Wilsonef749212017-04-12 12:01:10 +01003915 flush_write_domain(obj, ~I915_GEM_DOMAIN_GTT);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003916
Chris Wilsond0a57782012-10-09 19:24:37 +01003917 /* Serialise direct access to this object with the barriers for
3918 * coherent writes from the GPU, by effectively invalidating the
3919 * GTT domain upon first access.
3920 */
Christian Königc0a51fd2018-02-16 13:43:38 +01003921 if ((obj->read_domains & I915_GEM_DOMAIN_GTT) == 0)
Chris Wilsond0a57782012-10-09 19:24:37 +01003922 mb();
3923
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003924 /* It should now be out of any other write domains, and we can update
3925 * the domain values for our changes.
3926 */
Christian Königc0a51fd2018-02-16 13:43:38 +01003927 GEM_BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_GTT) != 0);
3928 obj->read_domains |= I915_GEM_DOMAIN_GTT;
Eric Anholte47c68e2008-11-14 13:35:19 -08003929 if (write) {
Christian Königc0a51fd2018-02-16 13:43:38 +01003930 obj->read_domains = I915_GEM_DOMAIN_GTT;
3931 obj->write_domain = I915_GEM_DOMAIN_GTT;
Chris Wilsona4f5ea62016-10-28 13:58:35 +01003932 obj->mm.dirty = true;
Eric Anholte47c68e2008-11-14 13:35:19 -08003933 }
3934
Chris Wilsona4f5ea62016-10-28 13:58:35 +01003935 i915_gem_object_unpin_pages(obj);
Eric Anholte47c68e2008-11-14 13:35:19 -08003936 return 0;
3937}
3938
Chris Wilsonef55f922015-10-09 14:11:27 +01003939/**
3940 * Changes the cache-level of an object across all VMA.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01003941 * @obj: object to act on
3942 * @cache_level: new cache level to set for the object
Chris Wilsonef55f922015-10-09 14:11:27 +01003943 *
3944 * After this function returns, the object will be in the new cache-level
3945 * across all GTT and the contents of the backing storage will be coherent,
3946 * with respect to the new cache-level. In order to keep the backing storage
3947 * coherent for all users, we only allow a single cache level to be set
3948 * globally on the object and prevent it from being changed whilst the
3949 * hardware is reading from the object. That is if the object is currently
3950 * on the scanout it will be set to uncached (or equivalent display
3951 * cache coherency) and all non-MOCS GPU access will also be uncached so
3952 * that all direct access to the scanout remains coherent.
3953 */
Chris Wilsone4ffd172011-04-04 09:44:39 +01003954int i915_gem_object_set_cache_level(struct drm_i915_gem_object *obj,
3955 enum i915_cache_level cache_level)
3956{
Chris Wilsonaa653a62016-08-04 07:52:27 +01003957 struct i915_vma *vma;
Chris Wilsona6a7cc42016-11-18 21:17:46 +00003958 int ret;
Chris Wilsone4ffd172011-04-04 09:44:39 +01003959
Chris Wilson4c7d62c2016-10-28 13:58:32 +01003960 lockdep_assert_held(&obj->base.dev->struct_mutex);
3961
Chris Wilsone4ffd172011-04-04 09:44:39 +01003962 if (obj->cache_level == cache_level)
Chris Wilsona6a7cc42016-11-18 21:17:46 +00003963 return 0;
Chris Wilsone4ffd172011-04-04 09:44:39 +01003964
Chris Wilsonef55f922015-10-09 14:11:27 +01003965 /* Inspect the list of currently bound VMA and unbind any that would
3966 * be invalid given the new cache-level. This is principally to
3967 * catch the issue of the CS prefetch crossing page boundaries and
3968 * reading an invalid PTE on older architectures.
3969 */
Chris Wilsonaa653a62016-08-04 07:52:27 +01003970restart:
3971 list_for_each_entry(vma, &obj->vma_list, obj_link) {
Chris Wilsonef55f922015-10-09 14:11:27 +01003972 if (!drm_mm_node_allocated(&vma->node))
3973 continue;
3974
Chris Wilson20dfbde2016-08-04 16:32:30 +01003975 if (i915_vma_is_pinned(vma)) {
Chris Wilsonef55f922015-10-09 14:11:27 +01003976 DRM_DEBUG("can not change the cache level of pinned objects\n");
3977 return -EBUSY;
3978 }
3979
Chris Wilson010e3e62017-12-06 12:49:13 +00003980 if (!i915_vma_is_closed(vma) &&
3981 i915_gem_valid_gtt_space(vma, cache_level))
Chris Wilsonaa653a62016-08-04 07:52:27 +01003982 continue;
3983
3984 ret = i915_vma_unbind(vma);
3985 if (ret)
3986 return ret;
3987
3988 /* As unbinding may affect other elements in the
3989 * obj->vma_list (due to side-effects from retiring
3990 * an active vma), play safe and restart the iterator.
3991 */
3992 goto restart;
Chris Wilson42d6ab42012-07-26 11:49:32 +01003993 }
3994
Chris Wilsonef55f922015-10-09 14:11:27 +01003995 /* We can reuse the existing drm_mm nodes but need to change the
3996 * cache-level on the PTE. We could simply unbind them all and
3997 * rebind with the correct cache-level on next use. However since
3998 * we already have a valid slot, dma mapping, pages etc, we may as
3999 * rewrite the PTE in the belief that doing so tramples upon less
4000 * state and so involves less work.
4001 */
Chris Wilson15717de2016-08-04 07:52:26 +01004002 if (obj->bind_count) {
Chris Wilsonef55f922015-10-09 14:11:27 +01004003 /* Before we change the PTE, the GPU must not be accessing it.
4004 * If we wait upon the object, we know that all the bound
4005 * VMA are no longer active.
4006 */
Chris Wilsone95433c2016-10-28 13:58:27 +01004007 ret = i915_gem_object_wait(obj,
4008 I915_WAIT_INTERRUPTIBLE |
4009 I915_WAIT_LOCKED |
4010 I915_WAIT_ALL,
4011 MAX_SCHEDULE_TIMEOUT,
4012 NULL);
Chris Wilsone4ffd172011-04-04 09:44:39 +01004013 if (ret)
4014 return ret;
4015
Tvrtko Ursulin0031fb92016-11-04 14:42:44 +00004016 if (!HAS_LLC(to_i915(obj->base.dev)) &&
4017 cache_level != I915_CACHE_NONE) {
Chris Wilsonef55f922015-10-09 14:11:27 +01004018 /* Access to snoopable pages through the GTT is
4019 * incoherent and on some machines causes a hard
4020 * lockup. Relinquish the CPU mmaping to force
4021 * userspace to refault in the pages and we can
4022 * then double check if the GTT mapping is still
4023 * valid for that pointer access.
4024 */
4025 i915_gem_release_mmap(obj);
Chris Wilsone4ffd172011-04-04 09:44:39 +01004026
Chris Wilsonef55f922015-10-09 14:11:27 +01004027 /* As we no longer need a fence for GTT access,
4028 * we can relinquish it now (and so prevent having
4029 * to steal a fence from someone else on the next
4030 * fence request). Note GPU activity would have
4031 * dropped the fence as all snoopable access is
4032 * supposed to be linear.
4033 */
Chris Wilsone2189dd2017-12-07 21:14:07 +00004034 for_each_ggtt_vma(vma, obj) {
Chris Wilson49ef5292016-08-18 17:17:00 +01004035 ret = i915_vma_put_fence(vma);
4036 if (ret)
4037 return ret;
4038 }
Chris Wilsonef55f922015-10-09 14:11:27 +01004039 } else {
4040 /* We either have incoherent backing store and
4041 * so no GTT access or the architecture is fully
4042 * coherent. In such cases, existing GTT mmaps
4043 * ignore the cache bit in the PTE and we can
4044 * rewrite it without confusing the GPU or having
4045 * to force userspace to fault back in its mmaps.
4046 */
Chris Wilsone4ffd172011-04-04 09:44:39 +01004047 }
4048
Chris Wilson1c7f4bc2016-02-26 11:03:19 +00004049 list_for_each_entry(vma, &obj->vma_list, obj_link) {
Chris Wilsonef55f922015-10-09 14:11:27 +01004050 if (!drm_mm_node_allocated(&vma->node))
4051 continue;
4052
4053 ret = i915_vma_bind(vma, cache_level, PIN_UPDATE);
4054 if (ret)
4055 return ret;
4056 }
Chris Wilsone4ffd172011-04-04 09:44:39 +01004057 }
4058
Chris Wilson1c7f4bc2016-02-26 11:03:19 +00004059 list_for_each_entry(vma, &obj->vma_list, obj_link)
Chris Wilson2c225692013-08-09 12:26:45 +01004060 vma->node.color = cache_level;
Chris Wilsonb8f55be2017-08-11 12:11:16 +01004061 i915_gem_object_set_cache_coherency(obj, cache_level);
Chris Wilsone27ab732017-06-15 13:38:49 +01004062 obj->cache_dirty = true; /* Always invalidate stale cachelines */
Chris Wilson2c225692013-08-09 12:26:45 +01004063
Chris Wilsone4ffd172011-04-04 09:44:39 +01004064 return 0;
4065}
4066
Ben Widawsky199adf42012-09-21 17:01:20 -07004067int i915_gem_get_caching_ioctl(struct drm_device *dev, void *data,
4068 struct drm_file *file)
Chris Wilsone6994ae2012-07-10 10:27:08 +01004069{
Ben Widawsky199adf42012-09-21 17:01:20 -07004070 struct drm_i915_gem_caching *args = data;
Chris Wilsone6994ae2012-07-10 10:27:08 +01004071 struct drm_i915_gem_object *obj;
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004072 int err = 0;
Chris Wilsone6994ae2012-07-10 10:27:08 +01004073
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004074 rcu_read_lock();
4075 obj = i915_gem_object_lookup_rcu(file, args->handle);
4076 if (!obj) {
4077 err = -ENOENT;
4078 goto out;
4079 }
Chris Wilsone6994ae2012-07-10 10:27:08 +01004080
Chris Wilson651d7942013-08-08 14:41:10 +01004081 switch (obj->cache_level) {
4082 case I915_CACHE_LLC:
4083 case I915_CACHE_L3_LLC:
4084 args->caching = I915_CACHING_CACHED;
4085 break;
4086
Chris Wilson4257d3b2013-08-08 14:41:11 +01004087 case I915_CACHE_WT:
4088 args->caching = I915_CACHING_DISPLAY;
4089 break;
4090
Chris Wilson651d7942013-08-08 14:41:10 +01004091 default:
4092 args->caching = I915_CACHING_NONE;
4093 break;
4094 }
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004095out:
4096 rcu_read_unlock();
4097 return err;
Chris Wilsone6994ae2012-07-10 10:27:08 +01004098}
4099
Ben Widawsky199adf42012-09-21 17:01:20 -07004100int i915_gem_set_caching_ioctl(struct drm_device *dev, void *data,
4101 struct drm_file *file)
Chris Wilsone6994ae2012-07-10 10:27:08 +01004102{
Chris Wilson9c870d02016-10-24 13:42:15 +01004103 struct drm_i915_private *i915 = to_i915(dev);
Ben Widawsky199adf42012-09-21 17:01:20 -07004104 struct drm_i915_gem_caching *args = data;
Chris Wilsone6994ae2012-07-10 10:27:08 +01004105 struct drm_i915_gem_object *obj;
4106 enum i915_cache_level level;
Chris Wilsond65415d2017-01-19 08:22:10 +00004107 int ret = 0;
Chris Wilsone6994ae2012-07-10 10:27:08 +01004108
Ben Widawsky199adf42012-09-21 17:01:20 -07004109 switch (args->caching) {
4110 case I915_CACHING_NONE:
Chris Wilsone6994ae2012-07-10 10:27:08 +01004111 level = I915_CACHE_NONE;
4112 break;
Ben Widawsky199adf42012-09-21 17:01:20 -07004113 case I915_CACHING_CACHED:
Imre Deake5756c12015-08-14 18:43:30 +03004114 /*
4115 * Due to a HW issue on BXT A stepping, GPU stores via a
4116 * snooped mapping may leave stale data in a corresponding CPU
4117 * cacheline, whereas normally such cachelines would get
4118 * invalidated.
4119 */
Chris Wilson9c870d02016-10-24 13:42:15 +01004120 if (!HAS_LLC(i915) && !HAS_SNOOP(i915))
Imre Deake5756c12015-08-14 18:43:30 +03004121 return -ENODEV;
4122
Chris Wilsone6994ae2012-07-10 10:27:08 +01004123 level = I915_CACHE_LLC;
4124 break;
Chris Wilson4257d3b2013-08-08 14:41:11 +01004125 case I915_CACHING_DISPLAY:
Chris Wilson9c870d02016-10-24 13:42:15 +01004126 level = HAS_WT(i915) ? I915_CACHE_WT : I915_CACHE_NONE;
Chris Wilson4257d3b2013-08-08 14:41:11 +01004127 break;
Chris Wilsone6994ae2012-07-10 10:27:08 +01004128 default:
4129 return -EINVAL;
4130 }
4131
Chris Wilsond65415d2017-01-19 08:22:10 +00004132 obj = i915_gem_object_lookup(file, args->handle);
4133 if (!obj)
4134 return -ENOENT;
4135
Tina Zhanga03f3952017-11-14 10:25:13 +00004136 /*
4137 * The caching mode of proxy object is handled by its generator, and
4138 * not allowed to be changed by userspace.
4139 */
4140 if (i915_gem_object_is_proxy(obj)) {
4141 ret = -ENXIO;
4142 goto out;
4143 }
4144
Chris Wilsond65415d2017-01-19 08:22:10 +00004145 if (obj->cache_level == level)
4146 goto out;
4147
4148 ret = i915_gem_object_wait(obj,
4149 I915_WAIT_INTERRUPTIBLE,
4150 MAX_SCHEDULE_TIMEOUT,
4151 to_rps_client(file));
4152 if (ret)
4153 goto out;
4154
Ben Widawsky3bc29132012-09-26 16:15:20 -07004155 ret = i915_mutex_lock_interruptible(dev);
4156 if (ret)
Chris Wilsond65415d2017-01-19 08:22:10 +00004157 goto out;
Chris Wilsone6994ae2012-07-10 10:27:08 +01004158
4159 ret = i915_gem_object_set_cache_level(obj, level);
Chris Wilsone6994ae2012-07-10 10:27:08 +01004160 mutex_unlock(&dev->struct_mutex);
Chris Wilsond65415d2017-01-19 08:22:10 +00004161
4162out:
4163 i915_gem_object_put(obj);
Chris Wilsone6994ae2012-07-10 10:27:08 +01004164 return ret;
4165}
4166
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08004167/*
Dhinakaran Pandiyan07bcd992018-03-06 19:34:18 -08004168 * Prepare buffer for display plane (scanout, cursors, etc). Can be called from
4169 * an uninterruptible phase (modesetting) and allows any flushes to be pipelined
4170 * (for pageflips). We only flush the caches while preparing the buffer for
4171 * display, the callers are responsible for frontbuffer flush.
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08004172 */
Chris Wilson058d88c2016-08-15 10:49:06 +01004173struct i915_vma *
Chris Wilson2da3b9b2011-04-14 09:41:17 +01004174i915_gem_object_pin_to_display_plane(struct drm_i915_gem_object *obj,
4175 u32 alignment,
Chris Wilson59354852018-02-20 13:42:06 +00004176 const struct i915_ggtt_view *view,
4177 unsigned int flags)
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08004178{
Chris Wilson058d88c2016-08-15 10:49:06 +01004179 struct i915_vma *vma;
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08004180 int ret;
4181
Chris Wilson4c7d62c2016-10-28 13:58:32 +01004182 lockdep_assert_held(&obj->base.dev->struct_mutex);
4183
Chris Wilsonbd3d2252017-10-13 21:26:14 +01004184 /* Mark the global pin early so that we account for the
Chris Wilsoncc98b412013-08-09 12:25:09 +01004185 * display coherency whilst setting up the cache domains.
4186 */
Chris Wilsonbd3d2252017-10-13 21:26:14 +01004187 obj->pin_global++;
Chris Wilsoncc98b412013-08-09 12:25:09 +01004188
Eric Anholta7ef0642011-03-29 16:59:54 -07004189 /* The display engine is not coherent with the LLC cache on gen6. As
4190 * a result, we make sure that the pinning that is about to occur is
4191 * done with uncached PTEs. This is lowest common denominator for all
4192 * chipsets.
4193 *
4194 * However for gen6+, we could do better by using the GFDT bit instead
4195 * of uncaching, which would allow us to flush all the LLC-cached data
4196 * with that bit in the PTE to main memory with just one PIPE_CONTROL.
4197 */
Chris Wilson651d7942013-08-08 14:41:10 +01004198 ret = i915_gem_object_set_cache_level(obj,
Tvrtko Ursulin86527442016-10-13 11:03:00 +01004199 HAS_WT(to_i915(obj->base.dev)) ?
4200 I915_CACHE_WT : I915_CACHE_NONE);
Chris Wilson058d88c2016-08-15 10:49:06 +01004201 if (ret) {
4202 vma = ERR_PTR(ret);
Chris Wilsonbd3d2252017-10-13 21:26:14 +01004203 goto err_unpin_global;
Chris Wilson058d88c2016-08-15 10:49:06 +01004204 }
Eric Anholta7ef0642011-03-29 16:59:54 -07004205
Chris Wilson2da3b9b2011-04-14 09:41:17 +01004206 /* As the user may map the buffer once pinned in the display plane
4207 * (e.g. libkms for the bootup splash), we have to ensure that we
Chris Wilson2efb8132016-08-18 17:17:06 +01004208 * always use map_and_fenceable for all scanout buffers. However,
4209 * it may simply be too big to fit into mappable, in which case
4210 * put it anyway and hope that userspace can cope (but always first
4211 * try to preserve the existing ABI).
Chris Wilson2da3b9b2011-04-14 09:41:17 +01004212 */
Chris Wilson2efb8132016-08-18 17:17:06 +01004213 vma = ERR_PTR(-ENOSPC);
Chris Wilson59354852018-02-20 13:42:06 +00004214 if ((flags & PIN_MAPPABLE) == 0 &&
4215 (!view || view->type == I915_GGTT_VIEW_NORMAL))
Chris Wilson2efb8132016-08-18 17:17:06 +01004216 vma = i915_gem_object_ggtt_pin(obj, view, 0, alignment,
Chris Wilson59354852018-02-20 13:42:06 +00004217 flags |
4218 PIN_MAPPABLE |
4219 PIN_NONBLOCK);
4220 if (IS_ERR(vma))
Chris Wilson767a2222016-11-07 11:01:28 +00004221 vma = i915_gem_object_ggtt_pin(obj, view, 0, alignment, flags);
Chris Wilson058d88c2016-08-15 10:49:06 +01004222 if (IS_ERR(vma))
Chris Wilsonbd3d2252017-10-13 21:26:14 +01004223 goto err_unpin_global;
Chris Wilson2da3b9b2011-04-14 09:41:17 +01004224
Chris Wilsond8923dc2016-08-18 17:17:07 +01004225 vma->display_alignment = max_t(u64, vma->display_alignment, alignment);
4226
Chris Wilson5a97bcc2017-02-22 11:40:46 +00004227 __i915_gem_object_flush_for_display(obj);
Chris Wilsonb118c1e2010-05-27 13:18:14 +01004228
Chris Wilson2da3b9b2011-04-14 09:41:17 +01004229 /* It should now be out of any other write domains, and we can update
4230 * the domain values for our changes.
4231 */
Christian Königc0a51fd2018-02-16 13:43:38 +01004232 obj->read_domains |= I915_GEM_DOMAIN_GTT;
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08004233
Chris Wilson058d88c2016-08-15 10:49:06 +01004234 return vma;
Chris Wilsoncc98b412013-08-09 12:25:09 +01004235
Chris Wilsonbd3d2252017-10-13 21:26:14 +01004236err_unpin_global:
4237 obj->pin_global--;
Chris Wilson058d88c2016-08-15 10:49:06 +01004238 return vma;
Chris Wilsoncc98b412013-08-09 12:25:09 +01004239}
4240
4241void
Chris Wilson058d88c2016-08-15 10:49:06 +01004242i915_gem_object_unpin_from_display_plane(struct i915_vma *vma)
Chris Wilsoncc98b412013-08-09 12:25:09 +01004243{
Chris Wilson49d73912016-11-29 09:50:08 +00004244 lockdep_assert_held(&vma->vm->i915->drm.struct_mutex);
Chris Wilson4c7d62c2016-10-28 13:58:32 +01004245
Chris Wilsonbd3d2252017-10-13 21:26:14 +01004246 if (WARN_ON(vma->obj->pin_global == 0))
Tvrtko Ursulin8a0c39b2015-04-13 11:50:09 +01004247 return;
4248
Chris Wilsonbd3d2252017-10-13 21:26:14 +01004249 if (--vma->obj->pin_global == 0)
Chris Wilsonf51455d2017-01-10 14:47:34 +00004250 vma->display_alignment = I915_GTT_MIN_ALIGNMENT;
Tvrtko Ursuline6617332015-03-23 11:10:33 +00004251
Chris Wilson383d5822016-08-18 17:17:08 +01004252 /* Bump the LRU to try and avoid premature eviction whilst flipping */
Chris Wilsonbefedbb2017-01-19 19:26:55 +00004253 i915_gem_object_bump_inactive_ggtt(vma->obj);
Chris Wilson383d5822016-08-18 17:17:08 +01004254
Chris Wilson058d88c2016-08-15 10:49:06 +01004255 i915_vma_unpin(vma);
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08004256}
4257
Eric Anholte47c68e2008-11-14 13:35:19 -08004258/**
4259 * Moves a single object to the CPU read, and possibly write domain.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01004260 * @obj: object to act on
4261 * @write: requesting write or read-only access
Eric Anholte47c68e2008-11-14 13:35:19 -08004262 *
4263 * This function returns when the move is complete, including waiting on
4264 * flushes to occur.
4265 */
Chris Wilsondabdfe02012-03-26 10:10:27 +02004266int
Chris Wilson919926a2010-11-12 13:42:53 +00004267i915_gem_object_set_to_cpu_domain(struct drm_i915_gem_object *obj, bool write)
Eric Anholte47c68e2008-11-14 13:35:19 -08004268{
Eric Anholte47c68e2008-11-14 13:35:19 -08004269 int ret;
4270
Chris Wilsone95433c2016-10-28 13:58:27 +01004271 lockdep_assert_held(&obj->base.dev->struct_mutex);
Chris Wilson4c7d62c2016-10-28 13:58:32 +01004272
Chris Wilsone95433c2016-10-28 13:58:27 +01004273 ret = i915_gem_object_wait(obj,
4274 I915_WAIT_INTERRUPTIBLE |
4275 I915_WAIT_LOCKED |
4276 (write ? I915_WAIT_ALL : 0),
4277 MAX_SCHEDULE_TIMEOUT,
4278 NULL);
Chris Wilson88241782011-01-07 17:09:48 +00004279 if (ret)
4280 return ret;
4281
Chris Wilsonef749212017-04-12 12:01:10 +01004282 flush_write_domain(obj, ~I915_GEM_DOMAIN_CPU);
Eric Anholte47c68e2008-11-14 13:35:19 -08004283
Eric Anholte47c68e2008-11-14 13:35:19 -08004284 /* Flush the CPU cache if it's still invalid. */
Christian Königc0a51fd2018-02-16 13:43:38 +01004285 if ((obj->read_domains & I915_GEM_DOMAIN_CPU) == 0) {
Chris Wilson57822dc2017-02-22 11:40:48 +00004286 i915_gem_clflush_object(obj, I915_CLFLUSH_SYNC);
Christian Königc0a51fd2018-02-16 13:43:38 +01004287 obj->read_domains |= I915_GEM_DOMAIN_CPU;
Eric Anholte47c68e2008-11-14 13:35:19 -08004288 }
4289
4290 /* It should now be out of any other write domains, and we can update
4291 * the domain values for our changes.
4292 */
Christian Königc0a51fd2018-02-16 13:43:38 +01004293 GEM_BUG_ON(obj->write_domain & ~I915_GEM_DOMAIN_CPU);
Eric Anholte47c68e2008-11-14 13:35:19 -08004294
4295 /* If we're writing through the CPU, then the GPU read domains will
4296 * need to be invalidated at next use.
4297 */
Chris Wilsone27ab732017-06-15 13:38:49 +01004298 if (write)
4299 __start_cpu_write(obj);
Eric Anholt2ef7eea2008-11-10 10:53:25 -08004300
4301 return 0;
4302}
4303
Eric Anholt673a3942008-07-30 12:06:12 -07004304/* Throttle our rendering by waiting until the ring has completed our requests
4305 * emitted over 20 msec ago.
4306 *
Eric Anholtb9624422009-06-03 07:27:35 +00004307 * Note that if we were to use the current jiffies each time around the loop,
4308 * we wouldn't escape the function with any frames outstanding if the time to
4309 * render a frame was over 20ms.
4310 *
Eric Anholt673a3942008-07-30 12:06:12 -07004311 * This should get us reasonable parallelism between CPU and GPU but also
4312 * relatively low latency when blocking on a particular request to finish.
4313 */
4314static int
Chris Wilsonf787a5f2010-09-24 16:02:42 +01004315i915_gem_ring_throttle(struct drm_device *dev, struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07004316{
Chris Wilsonfac5e232016-07-04 11:34:36 +01004317 struct drm_i915_private *dev_priv = to_i915(dev);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01004318 struct drm_i915_file_private *file_priv = file->driver_priv;
Chris Wilsond0bc54f2015-05-21 21:01:48 +01004319 unsigned long recent_enough = jiffies - DRM_I915_THROTTLE_JIFFIES;
Chris Wilsone61e0f52018-02-21 09:56:36 +00004320 struct i915_request *request, *target = NULL;
Chris Wilsone95433c2016-10-28 13:58:27 +01004321 long ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004322
Chris Wilsonf4457ae2016-04-13 17:35:08 +01004323 /* ABI: return -EIO if already wedged */
4324 if (i915_terminally_wedged(&dev_priv->gpu_error))
4325 return -EIO;
Chris Wilsone110e8d2011-01-26 15:39:14 +00004326
Chris Wilson1c255952010-09-26 11:03:27 +01004327 spin_lock(&file_priv->mm.lock);
Chris Wilsonc8659ef2017-03-02 12:25:25 +00004328 list_for_each_entry(request, &file_priv->mm.request_list, client_link) {
Eric Anholtb9624422009-06-03 07:27:35 +00004329 if (time_after_eq(request->emitted_jiffies, recent_enough))
4330 break;
4331
Chris Wilsonc8659ef2017-03-02 12:25:25 +00004332 if (target) {
4333 list_del(&target->client_link);
4334 target->file_priv = NULL;
4335 }
John Harrisonfcfa423c2015-05-29 17:44:12 +01004336
John Harrison54fb2412014-11-24 18:49:27 +00004337 target = request;
Eric Anholtb9624422009-06-03 07:27:35 +00004338 }
John Harrisonff865882014-11-24 18:49:28 +00004339 if (target)
Chris Wilsone61e0f52018-02-21 09:56:36 +00004340 i915_request_get(target);
Chris Wilson1c255952010-09-26 11:03:27 +01004341 spin_unlock(&file_priv->mm.lock);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01004342
John Harrison54fb2412014-11-24 18:49:27 +00004343 if (target == NULL)
Chris Wilsonf787a5f2010-09-24 16:02:42 +01004344 return 0;
4345
Chris Wilsone61e0f52018-02-21 09:56:36 +00004346 ret = i915_request_wait(target,
Chris Wilsone95433c2016-10-28 13:58:27 +01004347 I915_WAIT_INTERRUPTIBLE,
4348 MAX_SCHEDULE_TIMEOUT);
Chris Wilsone61e0f52018-02-21 09:56:36 +00004349 i915_request_put(target);
John Harrisonff865882014-11-24 18:49:28 +00004350
Chris Wilsone95433c2016-10-28 13:58:27 +01004351 return ret < 0 ? ret : 0;
Eric Anholt673a3942008-07-30 12:06:12 -07004352}
4353
Chris Wilson058d88c2016-08-15 10:49:06 +01004354struct i915_vma *
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02004355i915_gem_object_ggtt_pin(struct drm_i915_gem_object *obj,
4356 const struct i915_ggtt_view *view,
Chris Wilson91b2db62016-08-04 16:32:23 +01004357 u64 size,
Chris Wilson2ffffd02016-08-04 16:32:22 +01004358 u64 alignment,
4359 u64 flags)
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02004360{
Chris Wilsonad16d2e2016-10-13 09:55:04 +01004361 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
4362 struct i915_address_space *vm = &dev_priv->ggtt.base;
Chris Wilson59bfa122016-08-04 16:32:31 +01004363 struct i915_vma *vma;
4364 int ret;
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03004365
Chris Wilson4c7d62c2016-10-28 13:58:32 +01004366 lockdep_assert_held(&obj->base.dev->struct_mutex);
4367
Chris Wilsonac87a6fd2018-02-20 13:42:05 +00004368 if (flags & PIN_MAPPABLE &&
4369 (!view || view->type == I915_GGTT_VIEW_NORMAL)) {
Chris Wilson43ae70d92017-10-09 09:44:01 +01004370 /* If the required space is larger than the available
4371 * aperture, we will not able to find a slot for the
4372 * object and unbinding the object now will be in
4373 * vain. Worse, doing so may cause us to ping-pong
4374 * the object in and out of the Global GTT and
4375 * waste a lot of cycles under the mutex.
4376 */
4377 if (obj->base.size > dev_priv->ggtt.mappable_end)
4378 return ERR_PTR(-E2BIG);
4379
4380 /* If NONBLOCK is set the caller is optimistically
4381 * trying to cache the full object within the mappable
4382 * aperture, and *must* have a fallback in place for
4383 * situations where we cannot bind the object. We
4384 * can be a little more lax here and use the fallback
4385 * more often to avoid costly migrations of ourselves
4386 * and other objects within the aperture.
4387 *
4388 * Half-the-aperture is used as a simple heuristic.
4389 * More interesting would to do search for a free
4390 * block prior to making the commitment to unbind.
4391 * That caters for the self-harm case, and with a
4392 * little more heuristics (e.g. NOFAULT, NOEVICT)
4393 * we could try to minimise harm to others.
4394 */
4395 if (flags & PIN_NONBLOCK &&
4396 obj->base.size > dev_priv->ggtt.mappable_end / 2)
4397 return ERR_PTR(-ENOSPC);
4398 }
4399
Chris Wilson718659a2017-01-16 15:21:28 +00004400 vma = i915_vma_instance(obj, vm, view);
Chris Wilsone0216b72017-01-19 19:26:57 +00004401 if (unlikely(IS_ERR(vma)))
Chris Wilson058d88c2016-08-15 10:49:06 +01004402 return vma;
Chris Wilson59bfa122016-08-04 16:32:31 +01004403
4404 if (i915_vma_misplaced(vma, size, alignment, flags)) {
Chris Wilson43ae70d92017-10-09 09:44:01 +01004405 if (flags & PIN_NONBLOCK) {
4406 if (i915_vma_is_pinned(vma) || i915_vma_is_active(vma))
4407 return ERR_PTR(-ENOSPC);
Chris Wilson59bfa122016-08-04 16:32:31 +01004408
Chris Wilson43ae70d92017-10-09 09:44:01 +01004409 if (flags & PIN_MAPPABLE &&
Chris Wilson944397f2017-01-09 16:16:11 +00004410 vma->fence_size > dev_priv->ggtt.mappable_end / 2)
Chris Wilsonad16d2e2016-10-13 09:55:04 +01004411 return ERR_PTR(-ENOSPC);
4412 }
4413
Chris Wilson59bfa122016-08-04 16:32:31 +01004414 WARN(i915_vma_is_pinned(vma),
4415 "bo is already pinned in ggtt with incorrect alignment:"
Chris Wilson05a20d02016-08-18 17:16:55 +01004416 " offset=%08x, req.alignment=%llx,"
4417 " req.map_and_fenceable=%d, vma->map_and_fenceable=%d\n",
4418 i915_ggtt_offset(vma), alignment,
Chris Wilson59bfa122016-08-04 16:32:31 +01004419 !!(flags & PIN_MAPPABLE),
Chris Wilson05a20d02016-08-18 17:16:55 +01004420 i915_vma_is_map_and_fenceable(vma));
Chris Wilson59bfa122016-08-04 16:32:31 +01004421 ret = i915_vma_unbind(vma);
4422 if (ret)
Chris Wilson058d88c2016-08-15 10:49:06 +01004423 return ERR_PTR(ret);
Chris Wilson59bfa122016-08-04 16:32:31 +01004424 }
4425
Chris Wilson058d88c2016-08-15 10:49:06 +01004426 ret = i915_vma_pin(vma, size, alignment, flags | PIN_GLOBAL);
4427 if (ret)
4428 return ERR_PTR(ret);
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02004429
Chris Wilson058d88c2016-08-15 10:49:06 +01004430 return vma;
Eric Anholt673a3942008-07-30 12:06:12 -07004431}
4432
Chris Wilsonedf6b762016-08-09 09:23:33 +01004433static __always_inline unsigned int __busy_read_flag(unsigned int id)
Chris Wilson3fdc13c2016-08-05 10:14:18 +01004434{
4435 /* Note that we could alias engines in the execbuf API, but
4436 * that would be very unwise as it prevents userspace from
4437 * fine control over engine selection. Ahem.
4438 *
4439 * This should be something like EXEC_MAX_ENGINE instead of
4440 * I915_NUM_ENGINES.
4441 */
4442 BUILD_BUG_ON(I915_NUM_ENGINES > 16);
4443 return 0x10000 << id;
4444}
4445
4446static __always_inline unsigned int __busy_write_id(unsigned int id)
4447{
Chris Wilson70cb4722016-08-09 18:08:25 +01004448 /* The uABI guarantees an active writer is also amongst the read
4449 * engines. This would be true if we accessed the activity tracking
4450 * under the lock, but as we perform the lookup of the object and
4451 * its activity locklessly we can not guarantee that the last_write
4452 * being active implies that we have set the same engine flag from
4453 * last_read - hence we always set both read and write busy for
4454 * last_write.
4455 */
4456 return id | __busy_read_flag(id);
Chris Wilson3fdc13c2016-08-05 10:14:18 +01004457}
4458
Chris Wilsonedf6b762016-08-09 09:23:33 +01004459static __always_inline unsigned int
Chris Wilsond07f0e52016-10-28 13:58:44 +01004460__busy_set_if_active(const struct dma_fence *fence,
Chris Wilson3fdc13c2016-08-05 10:14:18 +01004461 unsigned int (*flag)(unsigned int id))
4462{
Chris Wilsone61e0f52018-02-21 09:56:36 +00004463 struct i915_request *rq;
Chris Wilson12555012016-08-16 09:50:40 +01004464
Chris Wilsond07f0e52016-10-28 13:58:44 +01004465 /* We have to check the current hw status of the fence as the uABI
4466 * guarantees forward progress. We could rely on the idle worker
4467 * to eventually flush us, but to minimise latency just ask the
4468 * hardware.
4469 *
4470 * Note we only report on the status of native fences.
4471 */
4472 if (!dma_fence_is_i915(fence))
Chris Wilson12555012016-08-16 09:50:40 +01004473 return 0;
4474
Chris Wilsond07f0e52016-10-28 13:58:44 +01004475 /* opencode to_request() in order to avoid const warnings */
Chris Wilsone61e0f52018-02-21 09:56:36 +00004476 rq = container_of(fence, struct i915_request, fence);
4477 if (i915_request_completed(rq))
Chris Wilsond07f0e52016-10-28 13:58:44 +01004478 return 0;
4479
Chris Wilson1d39f282017-04-11 13:43:06 +01004480 return flag(rq->engine->uabi_id);
Chris Wilson3fdc13c2016-08-05 10:14:18 +01004481}
4482
Chris Wilsonedf6b762016-08-09 09:23:33 +01004483static __always_inline unsigned int
Chris Wilsond07f0e52016-10-28 13:58:44 +01004484busy_check_reader(const struct dma_fence *fence)
Chris Wilson3fdc13c2016-08-05 10:14:18 +01004485{
Chris Wilsond07f0e52016-10-28 13:58:44 +01004486 return __busy_set_if_active(fence, __busy_read_flag);
Chris Wilson3fdc13c2016-08-05 10:14:18 +01004487}
4488
Chris Wilsonedf6b762016-08-09 09:23:33 +01004489static __always_inline unsigned int
Chris Wilsond07f0e52016-10-28 13:58:44 +01004490busy_check_writer(const struct dma_fence *fence)
Chris Wilson3fdc13c2016-08-05 10:14:18 +01004491{
Chris Wilsond07f0e52016-10-28 13:58:44 +01004492 if (!fence)
4493 return 0;
4494
4495 return __busy_set_if_active(fence, __busy_write_id);
Chris Wilson3fdc13c2016-08-05 10:14:18 +01004496}
4497
Eric Anholt673a3942008-07-30 12:06:12 -07004498int
Eric Anholt673a3942008-07-30 12:06:12 -07004499i915_gem_busy_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00004500 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07004501{
4502 struct drm_i915_gem_busy *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00004503 struct drm_i915_gem_object *obj;
Chris Wilsond07f0e52016-10-28 13:58:44 +01004504 struct reservation_object_list *list;
4505 unsigned int seq;
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004506 int err;
Eric Anholt673a3942008-07-30 12:06:12 -07004507
Chris Wilsond07f0e52016-10-28 13:58:44 +01004508 err = -ENOENT;
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004509 rcu_read_lock();
4510 obj = i915_gem_object_lookup_rcu(file, args->handle);
Chris Wilsond07f0e52016-10-28 13:58:44 +01004511 if (!obj)
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004512 goto out;
Chris Wilsond07f0e52016-10-28 13:58:44 +01004513
4514 /* A discrepancy here is that we do not report the status of
4515 * non-i915 fences, i.e. even though we may report the object as idle,
4516 * a call to set-domain may still stall waiting for foreign rendering.
4517 * This also means that wait-ioctl may report an object as busy,
4518 * where busy-ioctl considers it idle.
4519 *
4520 * We trade the ability to warn of foreign fences to report on which
4521 * i915 engines are active for the object.
4522 *
4523 * Alternatively, we can trade that extra information on read/write
4524 * activity with
4525 * args->busy =
4526 * !reservation_object_test_signaled_rcu(obj->resv, true);
4527 * to report the overall busyness. This is what the wait-ioctl does.
4528 *
4529 */
4530retry:
4531 seq = raw_read_seqcount(&obj->resv->seq);
4532
4533 /* Translate the exclusive fence to the READ *and* WRITE engine */
4534 args->busy = busy_check_writer(rcu_dereference(obj->resv->fence_excl));
4535
4536 /* Translate shared fences to READ set of engines */
4537 list = rcu_dereference(obj->resv->fence);
4538 if (list) {
4539 unsigned int shared_count = list->shared_count, i;
4540
4541 for (i = 0; i < shared_count; ++i) {
4542 struct dma_fence *fence =
4543 rcu_dereference(list->shared[i]);
4544
4545 args->busy |= busy_check_reader(fence);
4546 }
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004547 }
Zou Nan haid1b851f2010-05-21 09:08:57 +08004548
Chris Wilsond07f0e52016-10-28 13:58:44 +01004549 if (args->busy && read_seqcount_retry(&obj->resv->seq, seq))
4550 goto retry;
Chris Wilson426960b2016-01-15 16:51:46 +00004551
Chris Wilsond07f0e52016-10-28 13:58:44 +01004552 err = 0;
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004553out:
4554 rcu_read_unlock();
4555 return err;
Eric Anholt673a3942008-07-30 12:06:12 -07004556}
4557
4558int
4559i915_gem_throttle_ioctl(struct drm_device *dev, void *data,
4560 struct drm_file *file_priv)
4561{
Akshay Joshi0206e352011-08-16 15:34:10 -04004562 return i915_gem_ring_throttle(dev, file_priv);
Eric Anholt673a3942008-07-30 12:06:12 -07004563}
4564
Chris Wilson3ef94da2009-09-14 16:50:29 +01004565int
4566i915_gem_madvise_ioctl(struct drm_device *dev, void *data,
4567 struct drm_file *file_priv)
4568{
Chris Wilsonfac5e232016-07-04 11:34:36 +01004569 struct drm_i915_private *dev_priv = to_i915(dev);
Chris Wilson3ef94da2009-09-14 16:50:29 +01004570 struct drm_i915_gem_madvise *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00004571 struct drm_i915_gem_object *obj;
Chris Wilson1233e2d2016-10-28 13:58:37 +01004572 int err;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004573
4574 switch (args->madv) {
4575 case I915_MADV_DONTNEED:
4576 case I915_MADV_WILLNEED:
4577 break;
4578 default:
4579 return -EINVAL;
4580 }
4581
Chris Wilson03ac0642016-07-20 13:31:51 +01004582 obj = i915_gem_object_lookup(file_priv, args->handle);
Chris Wilson1233e2d2016-10-28 13:58:37 +01004583 if (!obj)
4584 return -ENOENT;
4585
4586 err = mutex_lock_interruptible(&obj->mm.lock);
4587 if (err)
4588 goto out;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004589
Chris Wilsonf1fa4f42017-10-13 21:26:13 +01004590 if (i915_gem_object_has_pages(obj) &&
Chris Wilson3e510a82016-08-05 10:14:23 +01004591 i915_gem_object_is_tiled(obj) &&
Daniel Vetter656bfa32014-11-20 09:26:30 +01004592 dev_priv->quirks & QUIRK_PIN_SWIZZLED_PAGES) {
Chris Wilsonbc0629a2016-11-01 10:03:17 +00004593 if (obj->mm.madv == I915_MADV_WILLNEED) {
4594 GEM_BUG_ON(!obj->mm.quirked);
Chris Wilsona4f5ea62016-10-28 13:58:35 +01004595 __i915_gem_object_unpin_pages(obj);
Chris Wilsonbc0629a2016-11-01 10:03:17 +00004596 obj->mm.quirked = false;
4597 }
4598 if (args->madv == I915_MADV_WILLNEED) {
Chris Wilson2c3a3f42016-11-04 10:30:01 +00004599 GEM_BUG_ON(obj->mm.quirked);
Chris Wilsona4f5ea62016-10-28 13:58:35 +01004600 __i915_gem_object_pin_pages(obj);
Chris Wilsonbc0629a2016-11-01 10:03:17 +00004601 obj->mm.quirked = true;
4602 }
Daniel Vetter656bfa32014-11-20 09:26:30 +01004603 }
4604
Chris Wilsona4f5ea62016-10-28 13:58:35 +01004605 if (obj->mm.madv != __I915_MADV_PURGED)
4606 obj->mm.madv = args->madv;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004607
Chris Wilson6c085a72012-08-20 11:40:46 +02004608 /* if the object is no longer attached, discard its backing storage */
Chris Wilsonf1fa4f42017-10-13 21:26:13 +01004609 if (obj->mm.madv == I915_MADV_DONTNEED &&
4610 !i915_gem_object_has_pages(obj))
Chris Wilson2d7ef392009-09-20 23:13:10 +01004611 i915_gem_object_truncate(obj);
4612
Chris Wilsona4f5ea62016-10-28 13:58:35 +01004613 args->retained = obj->mm.madv != __I915_MADV_PURGED;
Chris Wilson1233e2d2016-10-28 13:58:37 +01004614 mutex_unlock(&obj->mm.lock);
Chris Wilsonbb6baf72009-09-22 14:24:13 +01004615
Chris Wilson1233e2d2016-10-28 13:58:37 +01004616out:
Chris Wilsonf8c417c2016-07-20 13:31:53 +01004617 i915_gem_object_put(obj);
Chris Wilson1233e2d2016-10-28 13:58:37 +01004618 return err;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004619}
4620
Chris Wilson5b8c8ae2016-11-16 19:07:04 +00004621static void
Chris Wilsone61e0f52018-02-21 09:56:36 +00004622frontbuffer_retire(struct i915_gem_active *active, struct i915_request *request)
Chris Wilson5b8c8ae2016-11-16 19:07:04 +00004623{
4624 struct drm_i915_gem_object *obj =
4625 container_of(active, typeof(*obj), frontbuffer_write);
4626
Chris Wilsond59b21e2017-02-22 11:40:49 +00004627 intel_fb_obj_flush(obj, ORIGIN_CS);
Chris Wilson5b8c8ae2016-11-16 19:07:04 +00004628}
4629
Chris Wilson37e680a2012-06-07 15:38:42 +01004630void i915_gem_object_init(struct drm_i915_gem_object *obj,
4631 const struct drm_i915_gem_object_ops *ops)
Chris Wilson0327d6b2012-08-11 15:41:06 +01004632{
Chris Wilson1233e2d2016-10-28 13:58:37 +01004633 mutex_init(&obj->mm.lock);
4634
Ben Widawsky2f633152013-07-17 12:19:03 -07004635 INIT_LIST_HEAD(&obj->vma_list);
Chris Wilsond1b48c12017-08-16 09:52:08 +01004636 INIT_LIST_HEAD(&obj->lut_list);
Chris Wilson8d9d5742015-04-07 16:20:38 +01004637 INIT_LIST_HEAD(&obj->batch_pool_link);
Chris Wilson0327d6b2012-08-11 15:41:06 +01004638
Chris Wilson37e680a2012-06-07 15:38:42 +01004639 obj->ops = ops;
4640
Chris Wilsond07f0e52016-10-28 13:58:44 +01004641 reservation_object_init(&obj->__builtin_resv);
4642 obj->resv = &obj->__builtin_resv;
4643
Chris Wilson50349242016-08-18 17:17:04 +01004644 obj->frontbuffer_ggtt_origin = ORIGIN_GTT;
Chris Wilson5b8c8ae2016-11-16 19:07:04 +00004645 init_request_active(&obj->frontbuffer_write, frontbuffer_retire);
Chris Wilsona4f5ea62016-10-28 13:58:35 +01004646
4647 obj->mm.madv = I915_MADV_WILLNEED;
4648 INIT_RADIX_TREE(&obj->mm.get_page.radix, GFP_KERNEL | __GFP_NOWARN);
4649 mutex_init(&obj->mm.get_page.lock);
Chris Wilson0327d6b2012-08-11 15:41:06 +01004650
Dave Gordonf19ec8c2016-07-04 11:34:37 +01004651 i915_gem_info_add_obj(to_i915(obj->base.dev), obj->base.size);
Chris Wilson0327d6b2012-08-11 15:41:06 +01004652}
4653
Chris Wilson37e680a2012-06-07 15:38:42 +01004654static const struct drm_i915_gem_object_ops i915_gem_object_ops = {
Tvrtko Ursulin3599a912016-11-01 14:44:10 +00004655 .flags = I915_GEM_OBJECT_HAS_STRUCT_PAGE |
4656 I915_GEM_OBJECT_IS_SHRINKABLE,
Chris Wilson7c55e2c2017-03-07 12:03:38 +00004657
Chris Wilson37e680a2012-06-07 15:38:42 +01004658 .get_pages = i915_gem_object_get_pages_gtt,
4659 .put_pages = i915_gem_object_put_pages_gtt,
Chris Wilson7c55e2c2017-03-07 12:03:38 +00004660
4661 .pwrite = i915_gem_object_pwrite_gtt,
Chris Wilson37e680a2012-06-07 15:38:42 +01004662};
4663
Matthew Auld465c4032017-10-06 23:18:14 +01004664static int i915_gem_object_create_shmem(struct drm_device *dev,
4665 struct drm_gem_object *obj,
4666 size_t size)
4667{
4668 struct drm_i915_private *i915 = to_i915(dev);
4669 unsigned long flags = VM_NORESERVE;
4670 struct file *filp;
4671
4672 drm_gem_private_object_init(dev, obj, size);
4673
4674 if (i915->mm.gemfs)
4675 filp = shmem_file_setup_with_mnt(i915->mm.gemfs, "i915", size,
4676 flags);
4677 else
4678 filp = shmem_file_setup("i915", size, flags);
4679
4680 if (IS_ERR(filp))
4681 return PTR_ERR(filp);
4682
4683 obj->filp = filp;
4684
4685 return 0;
4686}
4687
Chris Wilsonb4bcbe22016-10-18 13:02:49 +01004688struct drm_i915_gem_object *
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +00004689i915_gem_object_create(struct drm_i915_private *dev_priv, u64 size)
Daniel Vetterac52bc52010-04-09 19:05:06 +00004690{
Daniel Vetterc397b902010-04-09 19:05:07 +00004691 struct drm_i915_gem_object *obj;
Hugh Dickins5949eac2011-06-27 16:18:18 -07004692 struct address_space *mapping;
Chris Wilsonb8f55be2017-08-11 12:11:16 +01004693 unsigned int cache_level;
Daniel Vetter1a240d42012-11-29 22:18:51 +01004694 gfp_t mask;
Chris Wilsonfe3db792016-04-25 13:32:13 +01004695 int ret;
Daniel Vetterc397b902010-04-09 19:05:07 +00004696
Chris Wilsonb4bcbe22016-10-18 13:02:49 +01004697 /* There is a prevalence of the assumption that we fit the object's
4698 * page count inside a 32bit _signed_ variable. Let's document this and
4699 * catch if we ever need to fix it. In the meantime, if you do spot
4700 * such a local variable, please consider fixing!
4701 */
Tvrtko Ursulin7a3ee5d2017-03-30 17:31:30 +01004702 if (size >> PAGE_SHIFT > INT_MAX)
Chris Wilsonb4bcbe22016-10-18 13:02:49 +01004703 return ERR_PTR(-E2BIG);
4704
4705 if (overflows_type(size, obj->base.size))
4706 return ERR_PTR(-E2BIG);
4707
Tvrtko Ursulin187685c2016-12-01 14:16:36 +00004708 obj = i915_gem_object_alloc(dev_priv);
Daniel Vetterc397b902010-04-09 19:05:07 +00004709 if (obj == NULL)
Chris Wilsonfe3db792016-04-25 13:32:13 +01004710 return ERR_PTR(-ENOMEM);
Daniel Vetterc397b902010-04-09 19:05:07 +00004711
Matthew Auld465c4032017-10-06 23:18:14 +01004712 ret = i915_gem_object_create_shmem(&dev_priv->drm, &obj->base, size);
Chris Wilsonfe3db792016-04-25 13:32:13 +01004713 if (ret)
4714 goto fail;
Daniel Vetterc397b902010-04-09 19:05:07 +00004715
Chris Wilsonbed1ea92012-05-24 20:48:12 +01004716 mask = GFP_HIGHUSER | __GFP_RECLAIMABLE;
Jani Nikulac0f86832016-12-07 12:13:04 +02004717 if (IS_I965GM(dev_priv) || IS_I965G(dev_priv)) {
Chris Wilsonbed1ea92012-05-24 20:48:12 +01004718 /* 965gm cannot relocate objects above 4GiB. */
4719 mask &= ~__GFP_HIGHMEM;
4720 mask |= __GFP_DMA32;
4721 }
4722
Al Viro93c76a32015-12-04 23:45:44 -05004723 mapping = obj->base.filp->f_mapping;
Chris Wilsonbed1ea92012-05-24 20:48:12 +01004724 mapping_set_gfp_mask(mapping, mask);
Chris Wilson4846bf02017-06-09 12:03:46 +01004725 GEM_BUG_ON(!(mapping_gfp_mask(mapping) & __GFP_RECLAIM));
Hugh Dickins5949eac2011-06-27 16:18:18 -07004726
Chris Wilson37e680a2012-06-07 15:38:42 +01004727 i915_gem_object_init(obj, &i915_gem_object_ops);
Chris Wilson73aa8082010-09-30 11:46:12 +01004728
Christian Königc0a51fd2018-02-16 13:43:38 +01004729 obj->write_domain = I915_GEM_DOMAIN_CPU;
4730 obj->read_domains = I915_GEM_DOMAIN_CPU;
Daniel Vetterc397b902010-04-09 19:05:07 +00004731
Chris Wilsonb8f55be2017-08-11 12:11:16 +01004732 if (HAS_LLC(dev_priv))
Eugeni Dodonov3d29b842012-01-17 14:43:53 -02004733 /* On some devices, we can have the GPU use the LLC (the CPU
Eric Anholta1871112011-03-29 16:59:55 -07004734 * cache) for about a 10% performance improvement
4735 * compared to uncached. Graphics requests other than
4736 * display scanout are coherent with the CPU in
4737 * accessing this cache. This means in this mode we
4738 * don't need to clflush on the CPU side, and on the
4739 * GPU side we only need to flush internal caches to
4740 * get data visible to the CPU.
4741 *
4742 * However, we maintain the display planes as UC, and so
4743 * need to rebind when first used as such.
4744 */
Chris Wilsonb8f55be2017-08-11 12:11:16 +01004745 cache_level = I915_CACHE_LLC;
4746 else
4747 cache_level = I915_CACHE_NONE;
Eric Anholta1871112011-03-29 16:59:55 -07004748
Chris Wilsonb8f55be2017-08-11 12:11:16 +01004749 i915_gem_object_set_cache_coherency(obj, cache_level);
Chris Wilsone27ab732017-06-15 13:38:49 +01004750
Daniel Vetterd861e332013-07-24 23:25:03 +02004751 trace_i915_gem_object_create(obj);
4752
Chris Wilson05394f32010-11-08 19:18:58 +00004753 return obj;
Chris Wilsonfe3db792016-04-25 13:32:13 +01004754
4755fail:
4756 i915_gem_object_free(obj);
Chris Wilsonfe3db792016-04-25 13:32:13 +01004757 return ERR_PTR(ret);
Daniel Vetterac52bc52010-04-09 19:05:06 +00004758}
4759
Chris Wilson340fbd82014-05-22 09:16:52 +01004760static bool discard_backing_storage(struct drm_i915_gem_object *obj)
4761{
4762 /* If we are the last user of the backing storage (be it shmemfs
4763 * pages or stolen etc), we know that the pages are going to be
4764 * immediately released. In this case, we can then skip copying
4765 * back the contents from the GPU.
4766 */
4767
Chris Wilsona4f5ea62016-10-28 13:58:35 +01004768 if (obj->mm.madv != I915_MADV_WILLNEED)
Chris Wilson340fbd82014-05-22 09:16:52 +01004769 return false;
4770
4771 if (obj->base.filp == NULL)
4772 return true;
4773
4774 /* At first glance, this looks racy, but then again so would be
4775 * userspace racing mmap against close. However, the first external
4776 * reference to the filp can only be obtained through the
4777 * i915_gem_mmap_ioctl() which safeguards us against the user
4778 * acquiring such a reference whilst we are in the middle of
4779 * freeing the object.
4780 */
4781 return atomic_long_read(&obj->base.filp->f_count) == 1;
4782}
4783
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004784static void __i915_gem_free_objects(struct drm_i915_private *i915,
4785 struct llist_node *freed)
Chris Wilsonbe726152010-07-23 23:18:50 +01004786{
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004787 struct drm_i915_gem_object *obj, *on;
Chris Wilsonbe726152010-07-23 23:18:50 +01004788
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004789 intel_runtime_pm_get(i915);
Chris Wilsoncc731f52017-10-13 21:26:21 +01004790 llist_for_each_entry_safe(obj, on, freed, freed) {
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004791 struct i915_vma *vma, *vn;
Paulo Zanonif65c9162013-11-27 18:20:34 -02004792
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004793 trace_i915_gem_object_destroy(obj);
4794
Chris Wilsoncc731f52017-10-13 21:26:21 +01004795 mutex_lock(&i915->drm.struct_mutex);
4796
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004797 GEM_BUG_ON(i915_gem_object_is_active(obj));
4798 list_for_each_entry_safe(vma, vn,
4799 &obj->vma_list, obj_link) {
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004800 GEM_BUG_ON(i915_vma_is_active(vma));
4801 vma->flags &= ~I915_VMA_PIN_MASK;
4802 i915_vma_close(vma);
4803 }
Chris Wilsondb6c2b42016-11-01 11:54:00 +00004804 GEM_BUG_ON(!list_empty(&obj->vma_list));
4805 GEM_BUG_ON(!RB_EMPTY_ROOT(&obj->vma_tree));
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004806
Chris Wilsonf2123812017-10-16 12:40:37 +01004807 /* This serializes freeing with the shrinker. Since the free
4808 * is delayed, first by RCU then by the workqueue, we want the
4809 * shrinker to be able to free pages of unreferenced objects,
4810 * or else we may oom whilst there are plenty of deferred
4811 * freed objects.
4812 */
4813 if (i915_gem_object_has_pages(obj)) {
4814 spin_lock(&i915->mm.obj_lock);
4815 list_del_init(&obj->mm.link);
4816 spin_unlock(&i915->mm.obj_lock);
4817 }
4818
Chris Wilsoncc731f52017-10-13 21:26:21 +01004819 mutex_unlock(&i915->drm.struct_mutex);
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004820
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004821 GEM_BUG_ON(obj->bind_count);
Chris Wilsona65adaf2017-10-09 09:43:57 +01004822 GEM_BUG_ON(obj->userfault_count);
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004823 GEM_BUG_ON(atomic_read(&obj->frontbuffer_bits));
Chris Wilson67b48042017-08-22 12:05:16 +01004824 GEM_BUG_ON(!list_empty(&obj->lut_list));
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004825
4826 if (obj->ops->release)
4827 obj->ops->release(obj);
4828
4829 if (WARN_ON(i915_gem_object_has_pinned_pages(obj)))
4830 atomic_set(&obj->mm.pages_pin_count, 0);
Chris Wilson548625e2016-11-01 12:11:34 +00004831 __i915_gem_object_put_pages(obj, I915_MM_NORMAL);
Chris Wilsonf1fa4f42017-10-13 21:26:13 +01004832 GEM_BUG_ON(i915_gem_object_has_pages(obj));
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004833
4834 if (obj->base.import_attach)
4835 drm_prime_gem_destroy(&obj->base, NULL);
4836
Chris Wilsond07f0e52016-10-28 13:58:44 +01004837 reservation_object_fini(&obj->__builtin_resv);
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004838 drm_gem_object_release(&obj->base);
4839 i915_gem_info_remove_obj(i915, obj->base.size);
4840
4841 kfree(obj->bit_17);
4842 i915_gem_object_free(obj);
Chris Wilsoncc731f52017-10-13 21:26:21 +01004843
Chris Wilsonc9c704712018-02-19 22:06:31 +00004844 GEM_BUG_ON(!atomic_read(&i915->mm.free_count));
4845 atomic_dec(&i915->mm.free_count);
4846
Chris Wilsoncc731f52017-10-13 21:26:21 +01004847 if (on)
4848 cond_resched();
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004849 }
Chris Wilsoncc731f52017-10-13 21:26:21 +01004850 intel_runtime_pm_put(i915);
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004851}
4852
4853static void i915_gem_flush_free_objects(struct drm_i915_private *i915)
4854{
4855 struct llist_node *freed;
4856
Chris Wilson87701b42017-10-13 21:26:20 +01004857 /* Free the oldest, most stale object to keep the free_list short */
4858 freed = NULL;
4859 if (!llist_empty(&i915->mm.free_list)) { /* quick test for hotpath */
4860 /* Only one consumer of llist_del_first() allowed */
4861 spin_lock(&i915->mm.free_lock);
4862 freed = llist_del_first(&i915->mm.free_list);
4863 spin_unlock(&i915->mm.free_lock);
4864 }
4865 if (unlikely(freed)) {
4866 freed->next = NULL;
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004867 __i915_gem_free_objects(i915, freed);
Chris Wilson87701b42017-10-13 21:26:20 +01004868 }
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004869}
4870
4871static void __i915_gem_free_work(struct work_struct *work)
4872{
4873 struct drm_i915_private *i915 =
4874 container_of(work, struct drm_i915_private, mm.free_work);
4875 struct llist_node *freed;
Chris Wilson26e12f82011-03-20 11:20:19 +00004876
Chris Wilson2ef1e722018-01-15 20:57:59 +00004877 /*
4878 * All file-owned VMA should have been released by this point through
Chris Wilsonb1f788c2016-08-04 07:52:45 +01004879 * i915_gem_close_object(), or earlier by i915_gem_context_close().
4880 * However, the object may also be bound into the global GTT (e.g.
4881 * older GPUs without per-process support, or for direct access through
4882 * the GTT either for the user or for scanout). Those VMA still need to
4883 * unbound now.
4884 */
Chris Wilson1488fc02012-04-24 15:47:31 +01004885
Chris Wilsonf991c492017-11-06 11:15:08 +00004886 spin_lock(&i915->mm.free_lock);
Chris Wilson5ad08be2017-04-07 11:25:51 +01004887 while ((freed = llist_del_all(&i915->mm.free_list))) {
Chris Wilsonf991c492017-11-06 11:15:08 +00004888 spin_unlock(&i915->mm.free_lock);
4889
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004890 __i915_gem_free_objects(i915, freed);
Chris Wilson5ad08be2017-04-07 11:25:51 +01004891 if (need_resched())
Chris Wilsonf991c492017-11-06 11:15:08 +00004892 return;
4893
4894 spin_lock(&i915->mm.free_lock);
Chris Wilson5ad08be2017-04-07 11:25:51 +01004895 }
Chris Wilsonf991c492017-11-06 11:15:08 +00004896 spin_unlock(&i915->mm.free_lock);
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004897}
4898
4899static void __i915_gem_free_object_rcu(struct rcu_head *head)
4900{
4901 struct drm_i915_gem_object *obj =
4902 container_of(head, typeof(*obj), rcu);
4903 struct drm_i915_private *i915 = to_i915(obj->base.dev);
4904
Chris Wilson2ef1e722018-01-15 20:57:59 +00004905 /*
4906 * Since we require blocking on struct_mutex to unbind the freed
4907 * object from the GPU before releasing resources back to the
4908 * system, we can not do that directly from the RCU callback (which may
4909 * be a softirq context), but must instead then defer that work onto a
4910 * kthread. We use the RCU callback rather than move the freed object
4911 * directly onto the work queue so that we can mix between using the
4912 * worker and performing frees directly from subsequent allocations for
4913 * crude but effective memory throttling.
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004914 */
4915 if (llist_add(&obj->freed, &i915->mm.free_list))
Chris Wilsonbeacbd12018-01-15 12:28:45 +00004916 queue_work(i915->wq, &i915->mm.free_work);
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004917}
4918
4919void i915_gem_free_object(struct drm_gem_object *gem_obj)
4920{
4921 struct drm_i915_gem_object *obj = to_intel_bo(gem_obj);
4922
Chris Wilsonbc0629a2016-11-01 10:03:17 +00004923 if (obj->mm.quirked)
4924 __i915_gem_object_unpin_pages(obj);
4925
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004926 if (discard_backing_storage(obj))
4927 obj->mm.madv = I915_MADV_DONTNEED;
Daniel Vettera071fa02014-06-18 23:28:09 +02004928
Chris Wilson2ef1e722018-01-15 20:57:59 +00004929 /*
4930 * Before we free the object, make sure any pure RCU-only
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004931 * read-side critical sections are complete, e.g.
4932 * i915_gem_busy_ioctl(). For the corresponding synchronized
4933 * lookup see i915_gem_object_lookup_rcu().
4934 */
Chris Wilsonc9c704712018-02-19 22:06:31 +00004935 atomic_inc(&to_i915(obj->base.dev)->mm.free_count);
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004936 call_rcu(&obj->rcu, __i915_gem_free_object_rcu);
Chris Wilsonbe726152010-07-23 23:18:50 +01004937}
4938
Chris Wilsonf8a7fde2016-10-28 13:58:29 +01004939void __i915_gem_object_release_unless_active(struct drm_i915_gem_object *obj)
4940{
4941 lockdep_assert_held(&obj->base.dev->struct_mutex);
4942
Chris Wilsond1b48c12017-08-16 09:52:08 +01004943 if (!i915_gem_object_has_active_reference(obj) &&
4944 i915_gem_object_is_active(obj))
Chris Wilsonf8a7fde2016-10-28 13:58:29 +01004945 i915_gem_object_set_active_reference(obj);
4946 else
4947 i915_gem_object_put(obj);
4948}
4949
Chris Wilsonae6c4572017-11-10 14:26:28 +00004950static void assert_kernel_context_is_current(struct drm_i915_private *i915)
Chris Wilson3033aca2016-10-28 13:58:47 +01004951{
Chris Wilsonae6c4572017-11-10 14:26:28 +00004952 struct i915_gem_context *kernel_context = i915->kernel_context;
Chris Wilson3033aca2016-10-28 13:58:47 +01004953 struct intel_engine_cs *engine;
4954 enum intel_engine_id id;
4955
Chris Wilsonae6c4572017-11-10 14:26:28 +00004956 for_each_engine(engine, i915, id) {
4957 GEM_BUG_ON(__i915_gem_active_peek(&engine->timeline->last_request));
4958 GEM_BUG_ON(engine->last_retired_context != kernel_context);
4959 }
Chris Wilson3033aca2016-10-28 13:58:47 +01004960}
4961
Chris Wilson24145512017-01-24 11:01:35 +00004962void i915_gem_sanitize(struct drm_i915_private *i915)
4963{
Chris Wilsonf36325f2017-08-26 12:09:34 +01004964 if (i915_terminally_wedged(&i915->gpu_error)) {
4965 mutex_lock(&i915->drm.struct_mutex);
4966 i915_gem_unset_wedged(i915);
4967 mutex_unlock(&i915->drm.struct_mutex);
4968 }
4969
Chris Wilson24145512017-01-24 11:01:35 +00004970 /*
4971 * If we inherit context state from the BIOS or earlier occupants
4972 * of the GPU, the GPU may be in an inconsistent state when we
4973 * try to take over. The only way to remove the earlier state
4974 * is by resetting. However, resetting on earlier gen is tricky as
4975 * it may impact the display and we are uncertain about the stability
Joonas Lahtinenea117b82017-04-28 10:53:38 +03004976 * of the reset, so this could be applied to even earlier gen.
Chris Wilson24145512017-01-24 11:01:35 +00004977 */
Daniele Ceraolo Spurioce1599a2018-02-07 13:24:40 -08004978 if (INTEL_GEN(i915) >= 5 && intel_has_gpu_reset(i915))
4979 WARN_ON(intel_gpu_reset(i915, ALL_ENGINES));
Chris Wilson24145512017-01-24 11:01:35 +00004980}
4981
Tvrtko Ursulinbf9e8422016-12-01 14:16:38 +00004982int i915_gem_suspend(struct drm_i915_private *dev_priv)
Eric Anholt673a3942008-07-30 12:06:12 -07004983{
Tvrtko Ursulinbf9e8422016-12-01 14:16:38 +00004984 struct drm_device *dev = &dev_priv->drm;
Chris Wilsondcff85c2016-08-05 10:14:11 +01004985 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004986
Chris Wilsonc998e8a2017-03-02 08:30:29 +00004987 intel_runtime_pm_get(dev_priv);
Chris Wilson54b4f682016-07-21 21:16:19 +01004988 intel_suspend_gt_powersave(dev_priv);
4989
Chris Wilson45c5f202013-10-16 11:50:01 +01004990 mutex_lock(&dev->struct_mutex);
Chris Wilson5ab57c72016-07-15 14:56:20 +01004991
4992 /* We have to flush all the executing contexts to main memory so
4993 * that they can saved in the hibernation image. To ensure the last
4994 * context image is coherent, we have to switch away from it. That
4995 * leaves the dev_priv->kernel_context still active when
4996 * we actually suspend, and its image in memory may not match the GPU
4997 * state. Fortunately, the kernel_context is disposable and we do
4998 * not rely on its state.
4999 */
Chris Wilsonecf73eb2017-11-30 10:29:51 +00005000 if (!i915_terminally_wedged(&dev_priv->gpu_error)) {
5001 ret = i915_gem_switch_to_kernel_context(dev_priv);
5002 if (ret)
5003 goto err_unlock;
Chris Wilson5ab57c72016-07-15 14:56:20 +01005004
Chris Wilsonecf73eb2017-11-30 10:29:51 +00005005 ret = i915_gem_wait_for_idle(dev_priv,
5006 I915_WAIT_INTERRUPTIBLE |
5007 I915_WAIT_LOCKED);
5008 if (ret && ret != -EIO)
5009 goto err_unlock;
Chris Wilsonf7403342013-09-13 23:57:04 +01005010
Chris Wilsonecf73eb2017-11-30 10:29:51 +00005011 assert_kernel_context_is_current(dev_priv);
5012 }
Chris Wilson829a0af2017-06-20 12:05:45 +01005013 i915_gem_contexts_lost(dev_priv);
Chris Wilson45c5f202013-10-16 11:50:01 +01005014 mutex_unlock(&dev->struct_mutex);
5015
Michal Wajdeczko7cfca4a2018-03-02 11:15:49 +00005016 intel_uc_suspend(dev_priv);
Sagar Arun Kamble63987bf2017-04-05 15:51:50 +05305017
Chris Wilson737b1502015-01-26 18:03:03 +02005018 cancel_delayed_work_sync(&dev_priv->gpu_error.hangcheck_work);
Chris Wilson67d97da2016-07-04 08:08:31 +01005019 cancel_delayed_work_sync(&dev_priv->gt.retire_work);
Chris Wilsonbdeb9782016-12-23 14:57:56 +00005020
5021 /* As the idle_work is rearming if it detects a race, play safe and
5022 * repeat the flush until it is definitely idle.
5023 */
Chris Wilson7c262402017-10-06 11:40:38 +01005024 drain_delayed_work(&dev_priv->gt.idle_work);
Chris Wilsonbdeb9782016-12-23 14:57:56 +00005025
Chris Wilsonbdcf1202014-11-25 11:56:33 +00005026 /* Assert that we sucessfully flushed all the work and
5027 * reset the GPU back to its idle, low power state.
5028 */
Chris Wilson67d97da2016-07-04 08:08:31 +01005029 WARN_ON(dev_priv->gt.awake);
Chris Wilsonfc692bd2017-08-26 12:09:35 +01005030 if (WARN_ON(!intel_engines_are_idle(dev_priv)))
5031 i915_gem_set_wedged(dev_priv); /* no hope, discard everything */
Chris Wilsonbdcf1202014-11-25 11:56:33 +00005032
Imre Deak1c777c52016-10-12 17:46:37 +03005033 /*
5034 * Neither the BIOS, ourselves or any other kernel
5035 * expects the system to be in execlists mode on startup,
5036 * so we need to reset the GPU back to legacy mode. And the only
5037 * known way to disable logical contexts is through a GPU reset.
5038 *
5039 * So in order to leave the system in a known default configuration,
5040 * always reset the GPU upon unload and suspend. Afterwards we then
5041 * clean up the GEM state tracking, flushing off the requests and
5042 * leaving the system in a known idle state.
5043 *
5044 * Note that is of the upmost importance that the GPU is idle and
5045 * all stray writes are flushed *before* we dismantle the backing
5046 * storage for the pinned objects.
5047 *
5048 * However, since we are uncertain that resetting the GPU on older
5049 * machines is a good idea, we don't - just in case it leaves the
5050 * machine in an unusable condition.
5051 */
Michal Wajdeczkoc37d5722018-03-12 13:03:07 +00005052 intel_uc_sanitize(dev_priv);
Chris Wilson24145512017-01-24 11:01:35 +00005053 i915_gem_sanitize(dev_priv);
Chris Wilsoncad99462017-08-26 12:09:33 +01005054
5055 intel_runtime_pm_put(dev_priv);
5056 return 0;
Imre Deak1c777c52016-10-12 17:46:37 +03005057
Chris Wilsonc998e8a2017-03-02 08:30:29 +00005058err_unlock:
Chris Wilson45c5f202013-10-16 11:50:01 +01005059 mutex_unlock(&dev->struct_mutex);
Chris Wilsonc998e8a2017-03-02 08:30:29 +00005060 intel_runtime_pm_put(dev_priv);
Chris Wilson45c5f202013-10-16 11:50:01 +01005061 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07005062}
5063
Chris Wilson37cd3302017-11-12 11:27:38 +00005064void i915_gem_resume(struct drm_i915_private *i915)
Chris Wilson5ab57c72016-07-15 14:56:20 +01005065{
Chris Wilson37cd3302017-11-12 11:27:38 +00005066 WARN_ON(i915->gt.awake);
Chris Wilson5ab57c72016-07-15 14:56:20 +01005067
Chris Wilson37cd3302017-11-12 11:27:38 +00005068 mutex_lock(&i915->drm.struct_mutex);
5069 intel_uncore_forcewake_get(i915, FORCEWAKE_ALL);
Imre Deak31ab49a2016-11-07 11:20:05 +02005070
Chris Wilson37cd3302017-11-12 11:27:38 +00005071 i915_gem_restore_gtt_mappings(i915);
5072 i915_gem_restore_fences(i915);
Chris Wilson5ab57c72016-07-15 14:56:20 +01005073
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00005074 /*
5075 * As we didn't flush the kernel context before suspend, we cannot
Chris Wilson5ab57c72016-07-15 14:56:20 +01005076 * guarantee that the context image is complete. So let's just reset
5077 * it and start again.
5078 */
Chris Wilson37cd3302017-11-12 11:27:38 +00005079 i915->gt.resume(i915);
Chris Wilson5ab57c72016-07-15 14:56:20 +01005080
Chris Wilson37cd3302017-11-12 11:27:38 +00005081 if (i915_gem_init_hw(i915))
5082 goto err_wedged;
5083
Michal Wajdeczko7cfca4a2018-03-02 11:15:49 +00005084 intel_uc_resume(i915);
Chris Wilson7469c622017-11-14 13:03:00 +00005085
Chris Wilson37cd3302017-11-12 11:27:38 +00005086 /* Always reload a context for powersaving. */
5087 if (i915_gem_switch_to_kernel_context(i915))
5088 goto err_wedged;
5089
5090out_unlock:
5091 intel_uncore_forcewake_put(i915, FORCEWAKE_ALL);
5092 mutex_unlock(&i915->drm.struct_mutex);
5093 return;
5094
5095err_wedged:
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00005096 if (!i915_terminally_wedged(&i915->gpu_error)) {
5097 DRM_ERROR("failed to re-initialize GPU, declaring wedged!\n");
5098 i915_gem_set_wedged(i915);
5099 }
Chris Wilson37cd3302017-11-12 11:27:38 +00005100 goto out_unlock;
Chris Wilson5ab57c72016-07-15 14:56:20 +01005101}
5102
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00005103void i915_gem_init_swizzling(struct drm_i915_private *dev_priv)
Daniel Vetterf691e2f2012-02-02 09:58:12 +01005104{
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00005105 if (INTEL_GEN(dev_priv) < 5 ||
Daniel Vetterf691e2f2012-02-02 09:58:12 +01005106 dev_priv->mm.bit_6_swizzle_x == I915_BIT_6_SWIZZLE_NONE)
5107 return;
5108
5109 I915_WRITE(DISP_ARB_CTL, I915_READ(DISP_ARB_CTL) |
5110 DISP_TILE_SURFACE_SWIZZLING);
5111
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01005112 if (IS_GEN5(dev_priv))
Daniel Vetter11782b02012-01-31 16:47:55 +01005113 return;
5114
Daniel Vetterf691e2f2012-02-02 09:58:12 +01005115 I915_WRITE(TILECTL, I915_READ(TILECTL) | TILECTL_SWZCTL);
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01005116 if (IS_GEN6(dev_priv))
Daniel Vetter6b26c862012-04-24 14:04:12 +02005117 I915_WRITE(ARB_MODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_SNB));
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01005118 else if (IS_GEN7(dev_priv))
Daniel Vetter6b26c862012-04-24 14:04:12 +02005119 I915_WRITE(ARB_MODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_IVB));
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01005120 else if (IS_GEN8(dev_priv))
Ben Widawsky31a53362013-11-02 21:07:04 -07005121 I915_WRITE(GAMTARBMODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_BDW));
Ben Widawsky8782e262012-12-18 10:31:23 -08005122 else
5123 BUG();
Daniel Vetterf691e2f2012-02-02 09:58:12 +01005124}
Daniel Vettere21af882012-02-09 20:53:27 +01005125
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01005126static void init_unused_ring(struct drm_i915_private *dev_priv, u32 base)
Ville Syrjälä81e7f202014-08-15 01:21:55 +03005127{
Ville Syrjälä81e7f202014-08-15 01:21:55 +03005128 I915_WRITE(RING_CTL(base), 0);
5129 I915_WRITE(RING_HEAD(base), 0);
5130 I915_WRITE(RING_TAIL(base), 0);
5131 I915_WRITE(RING_START(base), 0);
5132}
5133
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01005134static void init_unused_rings(struct drm_i915_private *dev_priv)
Ville Syrjälä81e7f202014-08-15 01:21:55 +03005135{
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01005136 if (IS_I830(dev_priv)) {
5137 init_unused_ring(dev_priv, PRB1_BASE);
5138 init_unused_ring(dev_priv, SRB0_BASE);
5139 init_unused_ring(dev_priv, SRB1_BASE);
5140 init_unused_ring(dev_priv, SRB2_BASE);
5141 init_unused_ring(dev_priv, SRB3_BASE);
5142 } else if (IS_GEN2(dev_priv)) {
5143 init_unused_ring(dev_priv, SRB0_BASE);
5144 init_unused_ring(dev_priv, SRB1_BASE);
5145 } else if (IS_GEN3(dev_priv)) {
5146 init_unused_ring(dev_priv, PRB1_BASE);
5147 init_unused_ring(dev_priv, PRB2_BASE);
Ville Syrjälä81e7f202014-08-15 01:21:55 +03005148 }
5149}
5150
Chris Wilson20a8a742017-02-08 14:30:31 +00005151static int __i915_gem_restart_engines(void *data)
Ben Widawsky4fc7c972013-02-08 11:49:24 -08005152{
Chris Wilson20a8a742017-02-08 14:30:31 +00005153 struct drm_i915_private *i915 = data;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00005154 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05305155 enum intel_engine_id id;
Chris Wilson20a8a742017-02-08 14:30:31 +00005156 int err;
5157
5158 for_each_engine(engine, i915, id) {
5159 err = engine->init_hw(engine);
Chris Wilson8177e112018-02-07 11:15:45 +00005160 if (err) {
5161 DRM_ERROR("Failed to restart %s (%d)\n",
5162 engine->name, err);
Chris Wilson20a8a742017-02-08 14:30:31 +00005163 return err;
Chris Wilson8177e112018-02-07 11:15:45 +00005164 }
Chris Wilson20a8a742017-02-08 14:30:31 +00005165 }
5166
5167 return 0;
5168}
5169
5170int i915_gem_init_hw(struct drm_i915_private *dev_priv)
5171{
Chris Wilsond200cda2016-04-28 09:56:44 +01005172 int ret;
Ben Widawsky4fc7c972013-02-08 11:49:24 -08005173
Chris Wilsonde867c22016-10-25 13:16:02 +01005174 dev_priv->gt.last_init_time = ktime_get();
5175
Chris Wilson5e4f5182015-02-13 14:35:59 +00005176 /* Double layer security blanket, see i915_gem_init() */
5177 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
5178
Tvrtko Ursulin0031fb92016-11-04 14:42:44 +00005179 if (HAS_EDRAM(dev_priv) && INTEL_GEN(dev_priv) < 9)
Ben Widawsky05e21cc2013-07-04 11:02:04 -07005180 I915_WRITE(HSW_IDICR, I915_READ(HSW_IDICR) | IDIHASHMSK(0xf));
Ben Widawsky4fc7c972013-02-08 11:49:24 -08005181
Tvrtko Ursulin772c2a52016-10-13 11:03:01 +01005182 if (IS_HASWELL(dev_priv))
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01005183 I915_WRITE(MI_PREDICATE_RESULT_2, IS_HSW_GT3(dev_priv) ?
Ville Syrjälä0bf21342013-11-29 14:56:12 +02005184 LOWER_SLICE_ENABLED : LOWER_SLICE_DISABLED);
Rodrigo Vivi94353732013-08-28 16:45:46 -03005185
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01005186 if (HAS_PCH_NOP(dev_priv)) {
Tvrtko Ursulinfd6b8f42016-10-14 10:13:06 +01005187 if (IS_IVYBRIDGE(dev_priv)) {
Daniel Vetter6ba844b2014-01-22 23:39:30 +01005188 u32 temp = I915_READ(GEN7_MSG_CTL);
5189 temp &= ~(WAIT_FOR_PCH_FLR_ACK | WAIT_FOR_PCH_RESET_ACK);
5190 I915_WRITE(GEN7_MSG_CTL, temp);
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00005191 } else if (INTEL_GEN(dev_priv) >= 7) {
Daniel Vetter6ba844b2014-01-22 23:39:30 +01005192 u32 temp = I915_READ(HSW_NDE_RSTWRN_OPT);
5193 temp &= ~RESET_PCH_HANDSHAKE_ENABLE;
5194 I915_WRITE(HSW_NDE_RSTWRN_OPT, temp);
5195 }
Ben Widawsky88a2b2a2013-04-05 13:12:43 -07005196 }
5197
Oscar Mateo59b449d2018-04-10 09:12:47 -07005198 intel_gt_workarounds_apply(dev_priv);
5199
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00005200 i915_gem_init_swizzling(dev_priv);
Ben Widawsky4fc7c972013-02-08 11:49:24 -08005201
Daniel Vetterd5abdfd2014-11-20 09:45:19 +01005202 /*
5203 * At least 830 can leave some of the unused rings
5204 * "active" (ie. head != tail) after resume which
5205 * will prevent c3 entry. Makes sure all unused rings
5206 * are totally idle.
5207 */
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01005208 init_unused_rings(dev_priv);
Daniel Vetterd5abdfd2014-11-20 09:45:19 +01005209
Dave Gordoned54c1a2016-01-19 19:02:54 +00005210 BUG_ON(!dev_priv->kernel_context);
Chris Wilson6f74b362017-10-15 15:37:25 +01005211 if (i915_terminally_wedged(&dev_priv->gpu_error)) {
5212 ret = -EIO;
5213 goto out;
5214 }
John Harrison90638cc2015-05-29 17:43:37 +01005215
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00005216 ret = i915_ppgtt_init_hw(dev_priv);
John Harrison4ad2fd82015-06-18 13:11:20 +01005217 if (ret) {
Chris Wilson8177e112018-02-07 11:15:45 +00005218 DRM_ERROR("Enabling PPGTT failed (%d)\n", ret);
John Harrison4ad2fd82015-06-18 13:11:20 +01005219 goto out;
5220 }
5221
Jackie Lif08e2032018-03-13 17:32:53 -07005222 ret = intel_wopcm_init_hw(&dev_priv->wopcm);
5223 if (ret) {
5224 DRM_ERROR("Enabling WOPCM failed (%d)\n", ret);
5225 goto out;
5226 }
5227
Michał Winiarski9bdc3572017-10-25 18:25:19 +01005228 /* We can't enable contexts until all firmware is loaded */
5229 ret = intel_uc_init_hw(dev_priv);
Chris Wilson8177e112018-02-07 11:15:45 +00005230 if (ret) {
5231 DRM_ERROR("Enabling uc failed (%d)\n", ret);
Michał Winiarski9bdc3572017-10-25 18:25:19 +01005232 goto out;
Chris Wilson8177e112018-02-07 11:15:45 +00005233 }
Michał Winiarski9bdc3572017-10-25 18:25:19 +01005234
Tvrtko Ursulinbf9e8422016-12-01 14:16:38 +00005235 intel_mocs_init_l3cc_table(dev_priv);
Peter Antoine0ccdacf2016-04-13 15:03:25 +01005236
Chris Wilson136109c2017-11-02 13:14:30 +00005237 /* Only when the HW is re-initialised, can we replay the requests */
5238 ret = __i915_gem_restart_engines(dev_priv);
Chris Wilson5e4f5182015-02-13 14:35:59 +00005239out:
5240 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Ben Widawsky2fa48d82013-12-06 14:11:04 -08005241 return ret;
Zou Nan hai8187a2b2010-05-21 09:08:55 +08005242}
5243
Chris Wilsond2b4b972017-11-10 14:26:33 +00005244static int __intel_engines_record_defaults(struct drm_i915_private *i915)
5245{
5246 struct i915_gem_context *ctx;
5247 struct intel_engine_cs *engine;
5248 enum intel_engine_id id;
5249 int err;
5250
5251 /*
5252 * As we reset the gpu during very early sanitisation, the current
5253 * register state on the GPU should reflect its defaults values.
5254 * We load a context onto the hw (with restore-inhibit), then switch
5255 * over to a second context to save that default register state. We
5256 * can then prime every new context with that state so they all start
5257 * from the same default HW values.
5258 */
5259
5260 ctx = i915_gem_context_create_kernel(i915, 0);
5261 if (IS_ERR(ctx))
5262 return PTR_ERR(ctx);
5263
5264 for_each_engine(engine, i915, id) {
Chris Wilsone61e0f52018-02-21 09:56:36 +00005265 struct i915_request *rq;
Chris Wilsond2b4b972017-11-10 14:26:33 +00005266
Chris Wilsone61e0f52018-02-21 09:56:36 +00005267 rq = i915_request_alloc(engine, ctx);
Chris Wilsond2b4b972017-11-10 14:26:33 +00005268 if (IS_ERR(rq)) {
5269 err = PTR_ERR(rq);
5270 goto out_ctx;
5271 }
5272
Chris Wilson3fef5cd2017-11-20 10:20:02 +00005273 err = 0;
Chris Wilsond2b4b972017-11-10 14:26:33 +00005274 if (engine->init_context)
5275 err = engine->init_context(rq);
5276
Chris Wilsone61e0f52018-02-21 09:56:36 +00005277 __i915_request_add(rq, true);
Chris Wilsond2b4b972017-11-10 14:26:33 +00005278 if (err)
5279 goto err_active;
5280 }
5281
5282 err = i915_gem_switch_to_kernel_context(i915);
5283 if (err)
5284 goto err_active;
5285
5286 err = i915_gem_wait_for_idle(i915, I915_WAIT_LOCKED);
5287 if (err)
5288 goto err_active;
5289
5290 assert_kernel_context_is_current(i915);
5291
5292 for_each_engine(engine, i915, id) {
5293 struct i915_vma *state;
5294
Chris Wilsonab82a062018-04-30 14:15:01 +01005295 state = to_intel_context(ctx, engine)->state;
Chris Wilsond2b4b972017-11-10 14:26:33 +00005296 if (!state)
5297 continue;
5298
5299 /*
5300 * As we will hold a reference to the logical state, it will
5301 * not be torn down with the context, and importantly the
5302 * object will hold onto its vma (making it possible for a
5303 * stray GTT write to corrupt our defaults). Unmap the vma
5304 * from the GTT to prevent such accidents and reclaim the
5305 * space.
5306 */
5307 err = i915_vma_unbind(state);
5308 if (err)
5309 goto err_active;
5310
5311 err = i915_gem_object_set_to_cpu_domain(state->obj, false);
5312 if (err)
5313 goto err_active;
5314
5315 engine->default_state = i915_gem_object_get(state->obj);
5316 }
5317
5318 if (IS_ENABLED(CONFIG_DRM_I915_DEBUG_GEM)) {
5319 unsigned int found = intel_engines_has_context_isolation(i915);
5320
5321 /*
5322 * Make sure that classes with multiple engine instances all
5323 * share the same basic configuration.
5324 */
5325 for_each_engine(engine, i915, id) {
5326 unsigned int bit = BIT(engine->uabi_class);
5327 unsigned int expected = engine->default_state ? bit : 0;
5328
5329 if ((found & bit) != expected) {
5330 DRM_ERROR("mismatching default context state for class %d on engine %s\n",
5331 engine->uabi_class, engine->name);
5332 }
5333 }
5334 }
5335
5336out_ctx:
5337 i915_gem_context_set_closed(ctx);
5338 i915_gem_context_put(ctx);
5339 return err;
5340
5341err_active:
5342 /*
5343 * If we have to abandon now, we expect the engines to be idle
5344 * and ready to be torn-down. First try to flush any remaining
5345 * request, ensure we are pointing at the kernel context and
5346 * then remove it.
5347 */
5348 if (WARN_ON(i915_gem_switch_to_kernel_context(i915)))
5349 goto out_ctx;
5350
5351 if (WARN_ON(i915_gem_wait_for_idle(i915, I915_WAIT_LOCKED)))
5352 goto out_ctx;
5353
5354 i915_gem_contexts_lost(i915);
5355 goto out_ctx;
5356}
5357
Tvrtko Ursulinbf9e8422016-12-01 14:16:38 +00005358int i915_gem_init(struct drm_i915_private *dev_priv)
Chris Wilson1070a422012-04-24 15:47:41 +01005359{
Chris Wilson1070a422012-04-24 15:47:41 +01005360 int ret;
5361
Matthew Auldda9fe3f32017-10-06 23:18:31 +01005362 /*
5363 * We need to fallback to 4K pages since gvt gtt handling doesn't
5364 * support huge page entries - we will need to check either hypervisor
5365 * mm can support huge guest page or just do emulation in gvt.
5366 */
5367 if (intel_vgpu_active(dev_priv))
5368 mkwrite_device_info(dev_priv)->page_sizes =
5369 I915_GTT_PAGE_SIZE_4K;
5370
Chris Wilson94312822017-05-03 10:39:18 +01005371 dev_priv->mm.unordered_timeline = dma_fence_context_alloc(1);
Chris Wilson57822dc2017-02-22 11:40:48 +00005372
Chris Wilsonfb5c5512017-11-20 20:55:00 +00005373 if (HAS_LOGICAL_RING_CONTEXTS(dev_priv)) {
Chris Wilson821ed7d2016-09-09 14:11:53 +01005374 dev_priv->gt.resume = intel_lr_context_resume;
Tvrtko Ursulin117897f2016-03-16 11:00:40 +00005375 dev_priv->gt.cleanup_engine = intel_logical_ring_cleanup;
Chris Wilsonfb5c5512017-11-20 20:55:00 +00005376 } else {
5377 dev_priv->gt.resume = intel_legacy_submission_resume;
5378 dev_priv->gt.cleanup_engine = intel_engine_cleanup;
Oscar Mateoa83014d2014-07-24 17:04:21 +01005379 }
5380
Chris Wilsonee487002017-11-22 17:26:21 +00005381 ret = i915_gem_init_userptr(dev_priv);
5382 if (ret)
5383 return ret;
5384
Jackie Li6b0478f2018-03-13 17:32:50 -07005385 ret = intel_wopcm_init(&dev_priv->wopcm);
5386 if (ret)
5387 return ret;
5388
Sagar Arun Kamble70deead2018-01-24 21:16:58 +05305389 ret = intel_uc_init_misc(dev_priv);
Michał Winiarski3176ff42017-12-13 23:13:47 +01005390 if (ret)
5391 return ret;
5392
Chris Wilson5e4f5182015-02-13 14:35:59 +00005393 /* This is just a security blanket to placate dragons.
5394 * On some systems, we very sporadically observe that the first TLBs
5395 * used by the CS may be stale, despite us poking the TLB reset. If
5396 * we hold the forcewake during initialisation these problems
5397 * just magically go away.
5398 */
Chris Wilsonee487002017-11-22 17:26:21 +00005399 mutex_lock(&dev_priv->drm.struct_mutex);
Chris Wilson5e4f5182015-02-13 14:35:59 +00005400 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
5401
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01005402 ret = i915_gem_init_ggtt(dev_priv);
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00005403 if (ret) {
5404 GEM_BUG_ON(ret == -EIO);
5405 goto err_unlock;
5406 }
Jesse Barnesd62b4892013-03-08 10:45:53 -08005407
Chris Wilson829a0af2017-06-20 12:05:45 +01005408 ret = i915_gem_contexts_init(dev_priv);
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00005409 if (ret) {
5410 GEM_BUG_ON(ret == -EIO);
5411 goto err_ggtt;
5412 }
Ben Widawsky2fa48d82013-12-06 14:11:04 -08005413
Tvrtko Ursulinbf9e8422016-12-01 14:16:38 +00005414 ret = intel_engines_init(dev_priv);
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00005415 if (ret) {
5416 GEM_BUG_ON(ret == -EIO);
5417 goto err_context;
5418 }
Daniel Vetter53ca26c2012-04-26 23:28:03 +02005419
Chris Wilsonf58d13d2017-11-10 14:26:29 +00005420 intel_init_gt_powersave(dev_priv);
5421
Michał Winiarski61b5c152017-12-13 23:13:48 +01005422 ret = intel_uc_init(dev_priv);
Chris Wilsoncc6a8182017-11-10 14:26:30 +00005423 if (ret)
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00005424 goto err_pm;
Chris Wilsoncc6a8182017-11-10 14:26:30 +00005425
Michał Winiarski61b5c152017-12-13 23:13:48 +01005426 ret = i915_gem_init_hw(dev_priv);
5427 if (ret)
5428 goto err_uc_init;
5429
Chris Wilsoncc6a8182017-11-10 14:26:30 +00005430 /*
5431 * Despite its name intel_init_clock_gating applies both display
5432 * clock gating workarounds; GT mmio workarounds and the occasional
5433 * GT power context workaround. Worse, sometimes it includes a context
5434 * register workaround which we need to apply before we record the
5435 * default HW state for all contexts.
5436 *
5437 * FIXME: break up the workarounds and apply them at the right time!
5438 */
5439 intel_init_clock_gating(dev_priv);
5440
Chris Wilsond2b4b972017-11-10 14:26:33 +00005441 ret = __intel_engines_record_defaults(dev_priv);
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00005442 if (ret)
5443 goto err_init_hw;
5444
5445 if (i915_inject_load_failure()) {
5446 ret = -ENODEV;
5447 goto err_init_hw;
5448 }
5449
5450 if (i915_inject_load_failure()) {
5451 ret = -EIO;
5452 goto err_init_hw;
5453 }
5454
5455 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
5456 mutex_unlock(&dev_priv->drm.struct_mutex);
5457
5458 return 0;
5459
5460 /*
5461 * Unwinding is complicated by that we want to handle -EIO to mean
5462 * disable GPU submission but keep KMS alive. We want to mark the
5463 * HW as irrevisibly wedged, but keep enough state around that the
5464 * driver doesn't explode during runtime.
5465 */
5466err_init_hw:
5467 i915_gem_wait_for_idle(dev_priv, I915_WAIT_LOCKED);
5468 i915_gem_contexts_lost(dev_priv);
5469 intel_uc_fini_hw(dev_priv);
Michał Winiarski61b5c152017-12-13 23:13:48 +01005470err_uc_init:
5471 intel_uc_fini(dev_priv);
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00005472err_pm:
5473 if (ret != -EIO) {
5474 intel_cleanup_gt_powersave(dev_priv);
5475 i915_gem_cleanup_engines(dev_priv);
5476 }
5477err_context:
5478 if (ret != -EIO)
5479 i915_gem_contexts_fini(dev_priv);
5480err_ggtt:
5481err_unlock:
5482 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
5483 mutex_unlock(&dev_priv->drm.struct_mutex);
5484
Sagar Arun Kamble70deead2018-01-24 21:16:58 +05305485 intel_uc_fini_misc(dev_priv);
Sagar Arun Kambleda943b52018-01-10 18:24:16 +05305486
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00005487 if (ret != -EIO)
5488 i915_gem_cleanup_userptr(dev_priv);
5489
Chris Wilson60990322014-04-09 09:19:42 +01005490 if (ret == -EIO) {
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00005491 /*
5492 * Allow engine initialisation to fail by marking the GPU as
Chris Wilson60990322014-04-09 09:19:42 +01005493 * wedged. But we only want to do this where the GPU is angry,
5494 * for all other failure, such as an allocation failure, bail.
5495 */
Chris Wilson6f74b362017-10-15 15:37:25 +01005496 if (!i915_terminally_wedged(&dev_priv->gpu_error)) {
5497 DRM_ERROR("Failed to initialize GPU, declaring it wedged\n");
5498 i915_gem_set_wedged(dev_priv);
5499 }
Chris Wilson60990322014-04-09 09:19:42 +01005500 ret = 0;
Chris Wilson1070a422012-04-24 15:47:41 +01005501 }
5502
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00005503 i915_gem_drain_freed_objects(dev_priv);
Chris Wilson60990322014-04-09 09:19:42 +01005504 return ret;
Chris Wilson1070a422012-04-24 15:47:41 +01005505}
5506
Chris Wilson24145512017-01-24 11:01:35 +00005507void i915_gem_init_mmio(struct drm_i915_private *i915)
5508{
5509 i915_gem_sanitize(i915);
5510}
5511
Zou Nan hai8187a2b2010-05-21 09:08:55 +08005512void
Tvrtko Ursulincb15d9f2016-12-01 14:16:39 +00005513i915_gem_cleanup_engines(struct drm_i915_private *dev_priv)
Zou Nan hai8187a2b2010-05-21 09:08:55 +08005514{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00005515 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05305516 enum intel_engine_id id;
Zou Nan hai8187a2b2010-05-21 09:08:55 +08005517
Akash Goel3b3f1652016-10-13 22:44:48 +05305518 for_each_engine(engine, dev_priv, id)
Tvrtko Ursulin117897f2016-03-16 11:00:40 +00005519 dev_priv->gt.cleanup_engine(engine);
Zou Nan hai8187a2b2010-05-21 09:08:55 +08005520}
5521
Eric Anholt673a3942008-07-30 12:06:12 -07005522void
Imre Deak40ae4e12016-03-16 14:54:03 +02005523i915_gem_load_init_fences(struct drm_i915_private *dev_priv)
5524{
Chris Wilson49ef5292016-08-18 17:17:00 +01005525 int i;
Imre Deak40ae4e12016-03-16 14:54:03 +02005526
Tvrtko Ursulinc56b89f2018-02-09 21:58:46 +00005527 if (INTEL_GEN(dev_priv) >= 7 && !IS_VALLEYVIEW(dev_priv) &&
Imre Deak40ae4e12016-03-16 14:54:03 +02005528 !IS_CHERRYVIEW(dev_priv))
5529 dev_priv->num_fence_regs = 32;
Tvrtko Ursulinc56b89f2018-02-09 21:58:46 +00005530 else if (INTEL_GEN(dev_priv) >= 4 ||
Jani Nikula73f67aa2016-12-07 22:48:09 +02005531 IS_I945G(dev_priv) || IS_I945GM(dev_priv) ||
5532 IS_G33(dev_priv) || IS_PINEVIEW(dev_priv))
Imre Deak40ae4e12016-03-16 14:54:03 +02005533 dev_priv->num_fence_regs = 16;
5534 else
5535 dev_priv->num_fence_regs = 8;
5536
Chris Wilsonc0336662016-05-06 15:40:21 +01005537 if (intel_vgpu_active(dev_priv))
Imre Deak40ae4e12016-03-16 14:54:03 +02005538 dev_priv->num_fence_regs =
5539 I915_READ(vgtif_reg(avail_rs.fence_num));
5540
5541 /* Initialize fence registers to zero */
Chris Wilson49ef5292016-08-18 17:17:00 +01005542 for (i = 0; i < dev_priv->num_fence_regs; i++) {
5543 struct drm_i915_fence_reg *fence = &dev_priv->fence_regs[i];
5544
5545 fence->i915 = dev_priv;
5546 fence->id = i;
5547 list_add_tail(&fence->link, &dev_priv->mm.fence_list);
5548 }
Tvrtko Ursulin4362f4f2016-11-16 08:55:33 +00005549 i915_gem_restore_fences(dev_priv);
Imre Deak40ae4e12016-03-16 14:54:03 +02005550
Tvrtko Ursulin4362f4f2016-11-16 08:55:33 +00005551 i915_gem_detect_bit_6_swizzle(dev_priv);
Imre Deak40ae4e12016-03-16 14:54:03 +02005552}
5553
Chris Wilson9c52d1c2017-11-10 23:24:47 +00005554static void i915_gem_init__mm(struct drm_i915_private *i915)
5555{
5556 spin_lock_init(&i915->mm.object_stat_lock);
5557 spin_lock_init(&i915->mm.obj_lock);
5558 spin_lock_init(&i915->mm.free_lock);
5559
5560 init_llist_head(&i915->mm.free_list);
5561
5562 INIT_LIST_HEAD(&i915->mm.unbound_list);
5563 INIT_LIST_HEAD(&i915->mm.bound_list);
5564 INIT_LIST_HEAD(&i915->mm.fence_list);
5565 INIT_LIST_HEAD(&i915->mm.userfault_list);
5566
5567 INIT_WORK(&i915->mm.free_work, __i915_gem_free_work);
5568}
5569
Michal Wajdeczkoa0de9082018-03-23 12:34:49 +00005570int i915_gem_init_early(struct drm_i915_private *dev_priv)
Eric Anholt673a3942008-07-30 12:06:12 -07005571{
Tvrtko Ursulina9335682016-11-02 15:14:59 +00005572 int err = -ENOMEM;
Chris Wilson42dcedd2012-11-15 11:32:30 +00005573
Tvrtko Ursulina9335682016-11-02 15:14:59 +00005574 dev_priv->objects = KMEM_CACHE(drm_i915_gem_object, SLAB_HWCACHE_ALIGN);
5575 if (!dev_priv->objects)
Chris Wilson73cb9702016-10-28 13:58:46 +01005576 goto err_out;
Chris Wilson73cb9702016-10-28 13:58:46 +01005577
Tvrtko Ursulina9335682016-11-02 15:14:59 +00005578 dev_priv->vmas = KMEM_CACHE(i915_vma, SLAB_HWCACHE_ALIGN);
5579 if (!dev_priv->vmas)
Chris Wilson73cb9702016-10-28 13:58:46 +01005580 goto err_objects;
Chris Wilson73cb9702016-10-28 13:58:46 +01005581
Chris Wilsond1b48c12017-08-16 09:52:08 +01005582 dev_priv->luts = KMEM_CACHE(i915_lut_handle, 0);
5583 if (!dev_priv->luts)
5584 goto err_vmas;
5585
Chris Wilsone61e0f52018-02-21 09:56:36 +00005586 dev_priv->requests = KMEM_CACHE(i915_request,
Tvrtko Ursulina9335682016-11-02 15:14:59 +00005587 SLAB_HWCACHE_ALIGN |
5588 SLAB_RECLAIM_ACCOUNT |
Paul E. McKenney5f0d5a32017-01-18 02:53:44 -08005589 SLAB_TYPESAFE_BY_RCU);
Tvrtko Ursulina9335682016-11-02 15:14:59 +00005590 if (!dev_priv->requests)
Chris Wilsond1b48c12017-08-16 09:52:08 +01005591 goto err_luts;
Chris Wilson73cb9702016-10-28 13:58:46 +01005592
Chris Wilson52e54202016-11-14 20:41:02 +00005593 dev_priv->dependencies = KMEM_CACHE(i915_dependency,
5594 SLAB_HWCACHE_ALIGN |
5595 SLAB_RECLAIM_ACCOUNT);
5596 if (!dev_priv->dependencies)
5597 goto err_requests;
5598
Chris Wilsonc5cf9a92017-05-17 13:10:04 +01005599 dev_priv->priorities = KMEM_CACHE(i915_priolist, SLAB_HWCACHE_ALIGN);
5600 if (!dev_priv->priorities)
5601 goto err_dependencies;
5602
Chris Wilson73cb9702016-10-28 13:58:46 +01005603 INIT_LIST_HEAD(&dev_priv->gt.timelines);
Chris Wilson643b4502018-04-30 14:15:03 +01005604 INIT_LIST_HEAD(&dev_priv->gt.active_rings);
5605
5606 mutex_lock(&dev_priv->drm.struct_mutex);
Chris Wilsonbb894852016-11-14 20:40:57 +00005607 err = i915_gem_timeline_init__global(dev_priv);
Chris Wilson73cb9702016-10-28 13:58:46 +01005608 mutex_unlock(&dev_priv->drm.struct_mutex);
5609 if (err)
Chris Wilsonc5cf9a92017-05-17 13:10:04 +01005610 goto err_priorities;
Eric Anholt673a3942008-07-30 12:06:12 -07005611
Chris Wilson9c52d1c2017-11-10 23:24:47 +00005612 i915_gem_init__mm(dev_priv);
Chris Wilsonf2123812017-10-16 12:40:37 +01005613
Chris Wilson67d97da2016-07-04 08:08:31 +01005614 INIT_DELAYED_WORK(&dev_priv->gt.retire_work,
Eric Anholt673a3942008-07-30 12:06:12 -07005615 i915_gem_retire_work_handler);
Chris Wilson67d97da2016-07-04 08:08:31 +01005616 INIT_DELAYED_WORK(&dev_priv->gt.idle_work,
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005617 i915_gem_idle_work_handler);
Chris Wilson1f15b762016-07-01 17:23:14 +01005618 init_waitqueue_head(&dev_priv->gpu_error.wait_queue);
Daniel Vetter1f83fee2012-11-15 17:17:22 +01005619 init_waitqueue_head(&dev_priv->gpu_error.reset_queue);
Chris Wilson31169712009-09-14 16:50:28 +01005620
Joonas Lahtinen6f633402016-09-01 14:58:21 +03005621 atomic_set(&dev_priv->mm.bsd_engine_dispatch_index, 0);
5622
Chris Wilsonb5add952016-08-04 16:32:36 +01005623 spin_lock_init(&dev_priv->fb_tracking.lock);
Chris Wilson73cb9702016-10-28 13:58:46 +01005624
Matthew Auld465c4032017-10-06 23:18:14 +01005625 err = i915_gemfs_init(dev_priv);
5626 if (err)
5627 DRM_NOTE("Unable to create a private tmpfs mount, hugepage support will be disabled(%d).\n", err);
5628
Chris Wilson73cb9702016-10-28 13:58:46 +01005629 return 0;
5630
Chris Wilsonc5cf9a92017-05-17 13:10:04 +01005631err_priorities:
5632 kmem_cache_destroy(dev_priv->priorities);
Chris Wilson52e54202016-11-14 20:41:02 +00005633err_dependencies:
5634 kmem_cache_destroy(dev_priv->dependencies);
Chris Wilson73cb9702016-10-28 13:58:46 +01005635err_requests:
5636 kmem_cache_destroy(dev_priv->requests);
Chris Wilsond1b48c12017-08-16 09:52:08 +01005637err_luts:
5638 kmem_cache_destroy(dev_priv->luts);
Chris Wilson73cb9702016-10-28 13:58:46 +01005639err_vmas:
5640 kmem_cache_destroy(dev_priv->vmas);
5641err_objects:
5642 kmem_cache_destroy(dev_priv->objects);
5643err_out:
5644 return err;
Eric Anholt673a3942008-07-30 12:06:12 -07005645}
Dave Airlie71acb5e2008-12-30 20:31:46 +10005646
Michal Wajdeczkoa0de9082018-03-23 12:34:49 +00005647void i915_gem_cleanup_early(struct drm_i915_private *dev_priv)
Imre Deakd64aa092016-01-19 15:26:29 +02005648{
Chris Wilsonc4d4c1c2017-02-10 16:35:23 +00005649 i915_gem_drain_freed_objects(dev_priv);
Chris Wilsonc9c704712018-02-19 22:06:31 +00005650 GEM_BUG_ON(!llist_empty(&dev_priv->mm.free_list));
5651 GEM_BUG_ON(atomic_read(&dev_priv->mm.free_count));
Chris Wilsonc4d4c1c2017-02-10 16:35:23 +00005652 WARN_ON(dev_priv->mm.object_count);
Chris Wilson7d5d59e2016-11-01 08:48:41 +00005653
Matthew Auldea84aa72016-11-17 21:04:11 +00005654 mutex_lock(&dev_priv->drm.struct_mutex);
5655 i915_gem_timeline_fini(&dev_priv->gt.global_timeline);
5656 WARN_ON(!list_empty(&dev_priv->gt.timelines));
5657 mutex_unlock(&dev_priv->drm.struct_mutex);
5658
Chris Wilsonc5cf9a92017-05-17 13:10:04 +01005659 kmem_cache_destroy(dev_priv->priorities);
Chris Wilson52e54202016-11-14 20:41:02 +00005660 kmem_cache_destroy(dev_priv->dependencies);
Imre Deakd64aa092016-01-19 15:26:29 +02005661 kmem_cache_destroy(dev_priv->requests);
Chris Wilsond1b48c12017-08-16 09:52:08 +01005662 kmem_cache_destroy(dev_priv->luts);
Imre Deakd64aa092016-01-19 15:26:29 +02005663 kmem_cache_destroy(dev_priv->vmas);
5664 kmem_cache_destroy(dev_priv->objects);
Chris Wilson0eafec62016-08-04 16:32:41 +01005665
5666 /* And ensure that our DESTROY_BY_RCU slabs are truly destroyed */
5667 rcu_barrier();
Matthew Auld465c4032017-10-06 23:18:14 +01005668
5669 i915_gemfs_fini(dev_priv);
Imre Deakd64aa092016-01-19 15:26:29 +02005670}
5671
Chris Wilson6a800ea2016-09-21 14:51:07 +01005672int i915_gem_freeze(struct drm_i915_private *dev_priv)
5673{
Chris Wilsond0aa3012017-04-07 11:25:49 +01005674 /* Discard all purgeable objects, let userspace recover those as
5675 * required after resuming.
5676 */
Chris Wilson6a800ea2016-09-21 14:51:07 +01005677 i915_gem_shrink_all(dev_priv);
Chris Wilson6a800ea2016-09-21 14:51:07 +01005678
Chris Wilson6a800ea2016-09-21 14:51:07 +01005679 return 0;
5680}
5681
Chris Wilson461fb992016-05-14 07:26:33 +01005682int i915_gem_freeze_late(struct drm_i915_private *dev_priv)
5683{
5684 struct drm_i915_gem_object *obj;
Chris Wilson7aab2d52016-09-09 20:02:18 +01005685 struct list_head *phases[] = {
5686 &dev_priv->mm.unbound_list,
5687 &dev_priv->mm.bound_list,
5688 NULL
5689 }, **p;
Chris Wilson461fb992016-05-14 07:26:33 +01005690
5691 /* Called just before we write the hibernation image.
5692 *
5693 * We need to update the domain tracking to reflect that the CPU
5694 * will be accessing all the pages to create and restore from the
5695 * hibernation, and so upon restoration those pages will be in the
5696 * CPU domain.
5697 *
5698 * To make sure the hibernation image contains the latest state,
5699 * we update that state just before writing out the image.
Chris Wilson7aab2d52016-09-09 20:02:18 +01005700 *
5701 * To try and reduce the hibernation image, we manually shrink
Chris Wilsond0aa3012017-04-07 11:25:49 +01005702 * the objects as well, see i915_gem_freeze()
Chris Wilson461fb992016-05-14 07:26:33 +01005703 */
5704
Chris Wilson912d5722017-09-06 16:19:30 -07005705 i915_gem_shrink(dev_priv, -1UL, NULL, I915_SHRINK_UNBOUND);
Chris Wilson17b93c42017-04-07 11:25:50 +01005706 i915_gem_drain_freed_objects(dev_priv);
Chris Wilson461fb992016-05-14 07:26:33 +01005707
Chris Wilsonf2123812017-10-16 12:40:37 +01005708 spin_lock(&dev_priv->mm.obj_lock);
Chris Wilson7aab2d52016-09-09 20:02:18 +01005709 for (p = phases; *p; p++) {
Chris Wilsonf2123812017-10-16 12:40:37 +01005710 list_for_each_entry(obj, *p, mm.link)
Chris Wilsone27ab732017-06-15 13:38:49 +01005711 __start_cpu_write(obj);
Chris Wilson461fb992016-05-14 07:26:33 +01005712 }
Chris Wilsonf2123812017-10-16 12:40:37 +01005713 spin_unlock(&dev_priv->mm.obj_lock);
Chris Wilson461fb992016-05-14 07:26:33 +01005714
5715 return 0;
5716}
5717
Chris Wilsonf787a5f2010-09-24 16:02:42 +01005718void i915_gem_release(struct drm_device *dev, struct drm_file *file)
Eric Anholtb9624422009-06-03 07:27:35 +00005719{
Chris Wilsonf787a5f2010-09-24 16:02:42 +01005720 struct drm_i915_file_private *file_priv = file->driver_priv;
Chris Wilsone61e0f52018-02-21 09:56:36 +00005721 struct i915_request *request;
Eric Anholtb9624422009-06-03 07:27:35 +00005722
5723 /* Clean up our request list when the client is going away, so that
5724 * later retire_requests won't dereference our soon-to-be-gone
5725 * file_priv.
5726 */
Chris Wilson1c255952010-09-26 11:03:27 +01005727 spin_lock(&file_priv->mm.lock);
Chris Wilsonc8659ef2017-03-02 12:25:25 +00005728 list_for_each_entry(request, &file_priv->mm.request_list, client_link)
Chris Wilsonf787a5f2010-09-24 16:02:42 +01005729 request->file_priv = NULL;
Chris Wilson1c255952010-09-26 11:03:27 +01005730 spin_unlock(&file_priv->mm.lock);
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005731}
5732
Chris Wilson829a0af2017-06-20 12:05:45 +01005733int i915_gem_open(struct drm_i915_private *i915, struct drm_file *file)
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005734{
5735 struct drm_i915_file_private *file_priv;
Ben Widawskye422b882013-12-06 14:10:58 -08005736 int ret;
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005737
Chris Wilsonc4c29d72016-11-09 10:45:07 +00005738 DRM_DEBUG("\n");
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005739
5740 file_priv = kzalloc(sizeof(*file_priv), GFP_KERNEL);
5741 if (!file_priv)
5742 return -ENOMEM;
5743
5744 file->driver_priv = file_priv;
Chris Wilson829a0af2017-06-20 12:05:45 +01005745 file_priv->dev_priv = i915;
Chris Wilsonab0e7ff2014-02-25 17:11:24 +02005746 file_priv->file = file;
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005747
5748 spin_lock_init(&file_priv->mm.lock);
5749 INIT_LIST_HEAD(&file_priv->mm.request_list);
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005750
Chris Wilsonc80ff162016-07-27 09:07:27 +01005751 file_priv->bsd_engine = -1;
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00005752
Chris Wilson829a0af2017-06-20 12:05:45 +01005753 ret = i915_gem_context_open(i915, file);
Ben Widawskye422b882013-12-06 14:10:58 -08005754 if (ret)
5755 kfree(file_priv);
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005756
Ben Widawskye422b882013-12-06 14:10:58 -08005757 return ret;
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005758}
5759
Daniel Vetterb680c372014-09-19 18:27:27 +02005760/**
5761 * i915_gem_track_fb - update frontbuffer tracking
Geliang Tangd9072a32015-09-15 05:58:44 -07005762 * @old: current GEM buffer for the frontbuffer slots
5763 * @new: new GEM buffer for the frontbuffer slots
5764 * @frontbuffer_bits: bitmask of frontbuffer slots
Daniel Vetterb680c372014-09-19 18:27:27 +02005765 *
5766 * This updates the frontbuffer tracking bits @frontbuffer_bits by clearing them
5767 * from @old and setting them in @new. Both @old and @new can be NULL.
5768 */
Daniel Vettera071fa02014-06-18 23:28:09 +02005769void i915_gem_track_fb(struct drm_i915_gem_object *old,
5770 struct drm_i915_gem_object *new,
5771 unsigned frontbuffer_bits)
5772{
Chris Wilsonfaf5bf02016-08-04 16:32:37 +01005773 /* Control of individual bits within the mask are guarded by
5774 * the owning plane->mutex, i.e. we can never see concurrent
5775 * manipulation of individual bits. But since the bitfield as a whole
5776 * is updated using RMW, we need to use atomics in order to update
5777 * the bits.
5778 */
5779 BUILD_BUG_ON(INTEL_FRONTBUFFER_BITS_PER_PIPE * I915_MAX_PIPES >
5780 sizeof(atomic_t) * BITS_PER_BYTE);
5781
Daniel Vettera071fa02014-06-18 23:28:09 +02005782 if (old) {
Chris Wilsonfaf5bf02016-08-04 16:32:37 +01005783 WARN_ON(!(atomic_read(&old->frontbuffer_bits) & frontbuffer_bits));
5784 atomic_andnot(frontbuffer_bits, &old->frontbuffer_bits);
Daniel Vettera071fa02014-06-18 23:28:09 +02005785 }
5786
5787 if (new) {
Chris Wilsonfaf5bf02016-08-04 16:32:37 +01005788 WARN_ON(atomic_read(&new->frontbuffer_bits) & frontbuffer_bits);
5789 atomic_or(frontbuffer_bits, &new->frontbuffer_bits);
Daniel Vettera071fa02014-06-18 23:28:09 +02005790 }
5791}
5792
Dave Gordonea702992015-07-09 19:29:02 +01005793/* Allocate a new GEM object and fill it with the supplied data */
5794struct drm_i915_gem_object *
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +00005795i915_gem_object_create_from_data(struct drm_i915_private *dev_priv,
Dave Gordonea702992015-07-09 19:29:02 +01005796 const void *data, size_t size)
5797{
5798 struct drm_i915_gem_object *obj;
Chris Wilsonbe062fa2017-03-17 19:46:48 +00005799 struct file *file;
5800 size_t offset;
5801 int err;
Dave Gordonea702992015-07-09 19:29:02 +01005802
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +00005803 obj = i915_gem_object_create(dev_priv, round_up(size, PAGE_SIZE));
Chris Wilsonfe3db792016-04-25 13:32:13 +01005804 if (IS_ERR(obj))
Dave Gordonea702992015-07-09 19:29:02 +01005805 return obj;
5806
Christian Königc0a51fd2018-02-16 13:43:38 +01005807 GEM_BUG_ON(obj->write_domain != I915_GEM_DOMAIN_CPU);
Dave Gordonea702992015-07-09 19:29:02 +01005808
Chris Wilsonbe062fa2017-03-17 19:46:48 +00005809 file = obj->base.filp;
5810 offset = 0;
5811 do {
5812 unsigned int len = min_t(typeof(size), size, PAGE_SIZE);
5813 struct page *page;
5814 void *pgdata, *vaddr;
Dave Gordonea702992015-07-09 19:29:02 +01005815
Chris Wilsonbe062fa2017-03-17 19:46:48 +00005816 err = pagecache_write_begin(file, file->f_mapping,
5817 offset, len, 0,
5818 &page, &pgdata);
5819 if (err < 0)
5820 goto fail;
Dave Gordonea702992015-07-09 19:29:02 +01005821
Chris Wilsonbe062fa2017-03-17 19:46:48 +00005822 vaddr = kmap(page);
5823 memcpy(vaddr, data, len);
5824 kunmap(page);
5825
5826 err = pagecache_write_end(file, file->f_mapping,
5827 offset, len, len,
5828 page, pgdata);
5829 if (err < 0)
5830 goto fail;
5831
5832 size -= len;
5833 data += len;
5834 offset += len;
5835 } while (size);
Dave Gordonea702992015-07-09 19:29:02 +01005836
5837 return obj;
5838
5839fail:
Chris Wilsonf8c417c2016-07-20 13:31:53 +01005840 i915_gem_object_put(obj);
Chris Wilsonbe062fa2017-03-17 19:46:48 +00005841 return ERR_PTR(err);
Dave Gordonea702992015-07-09 19:29:02 +01005842}
Chris Wilson96d77632016-10-28 13:58:33 +01005843
5844struct scatterlist *
5845i915_gem_object_get_sg(struct drm_i915_gem_object *obj,
5846 unsigned int n,
5847 unsigned int *offset)
5848{
Chris Wilsona4f5ea62016-10-28 13:58:35 +01005849 struct i915_gem_object_page_iter *iter = &obj->mm.get_page;
Chris Wilson96d77632016-10-28 13:58:33 +01005850 struct scatterlist *sg;
5851 unsigned int idx, count;
5852
5853 might_sleep();
5854 GEM_BUG_ON(n >= obj->base.size >> PAGE_SHIFT);
Chris Wilsona4f5ea62016-10-28 13:58:35 +01005855 GEM_BUG_ON(!i915_gem_object_has_pinned_pages(obj));
Chris Wilson96d77632016-10-28 13:58:33 +01005856
5857 /* As we iterate forward through the sg, we record each entry in a
5858 * radixtree for quick repeated (backwards) lookups. If we have seen
5859 * this index previously, we will have an entry for it.
5860 *
5861 * Initial lookup is O(N), but this is amortized to O(1) for
5862 * sequential page access (where each new request is consecutive
5863 * to the previous one). Repeated lookups are O(lg(obj->base.size)),
5864 * i.e. O(1) with a large constant!
5865 */
5866 if (n < READ_ONCE(iter->sg_idx))
5867 goto lookup;
5868
5869 mutex_lock(&iter->lock);
5870
5871 /* We prefer to reuse the last sg so that repeated lookup of this
5872 * (or the subsequent) sg are fast - comparing against the last
5873 * sg is faster than going through the radixtree.
5874 */
5875
5876 sg = iter->sg_pos;
5877 idx = iter->sg_idx;
5878 count = __sg_page_count(sg);
5879
5880 while (idx + count <= n) {
5881 unsigned long exception, i;
5882 int ret;
5883
5884 /* If we cannot allocate and insert this entry, or the
5885 * individual pages from this range, cancel updating the
5886 * sg_idx so that on this lookup we are forced to linearly
5887 * scan onwards, but on future lookups we will try the
5888 * insertion again (in which case we need to be careful of
5889 * the error return reporting that we have already inserted
5890 * this index).
5891 */
5892 ret = radix_tree_insert(&iter->radix, idx, sg);
5893 if (ret && ret != -EEXIST)
5894 goto scan;
5895
5896 exception =
5897 RADIX_TREE_EXCEPTIONAL_ENTRY |
5898 idx << RADIX_TREE_EXCEPTIONAL_SHIFT;
5899 for (i = 1; i < count; i++) {
5900 ret = radix_tree_insert(&iter->radix, idx + i,
5901 (void *)exception);
5902 if (ret && ret != -EEXIST)
5903 goto scan;
5904 }
5905
5906 idx += count;
5907 sg = ____sg_next(sg);
5908 count = __sg_page_count(sg);
5909 }
5910
5911scan:
5912 iter->sg_pos = sg;
5913 iter->sg_idx = idx;
5914
5915 mutex_unlock(&iter->lock);
5916
5917 if (unlikely(n < idx)) /* insertion completed by another thread */
5918 goto lookup;
5919
5920 /* In case we failed to insert the entry into the radixtree, we need
5921 * to look beyond the current sg.
5922 */
5923 while (idx + count <= n) {
5924 idx += count;
5925 sg = ____sg_next(sg);
5926 count = __sg_page_count(sg);
5927 }
5928
5929 *offset = n - idx;
5930 return sg;
5931
5932lookup:
5933 rcu_read_lock();
5934
5935 sg = radix_tree_lookup(&iter->radix, n);
5936 GEM_BUG_ON(!sg);
5937
5938 /* If this index is in the middle of multi-page sg entry,
5939 * the radixtree will contain an exceptional entry that points
5940 * to the start of that range. We will return the pointer to
5941 * the base page and the offset of this page within the
5942 * sg entry's range.
5943 */
5944 *offset = 0;
5945 if (unlikely(radix_tree_exception(sg))) {
5946 unsigned long base =
5947 (unsigned long)sg >> RADIX_TREE_EXCEPTIONAL_SHIFT;
5948
5949 sg = radix_tree_lookup(&iter->radix, base);
5950 GEM_BUG_ON(!sg);
5951
5952 *offset = n - base;
5953 }
5954
5955 rcu_read_unlock();
5956
5957 return sg;
5958}
5959
5960struct page *
5961i915_gem_object_get_page(struct drm_i915_gem_object *obj, unsigned int n)
5962{
5963 struct scatterlist *sg;
5964 unsigned int offset;
5965
5966 GEM_BUG_ON(!i915_gem_object_has_struct_page(obj));
5967
5968 sg = i915_gem_object_get_sg(obj, n, &offset);
5969 return nth_page(sg_page(sg), offset);
5970}
5971
5972/* Like i915_gem_object_get_page(), but mark the returned page dirty */
5973struct page *
5974i915_gem_object_get_dirty_page(struct drm_i915_gem_object *obj,
5975 unsigned int n)
5976{
5977 struct page *page;
5978
5979 page = i915_gem_object_get_page(obj, n);
Chris Wilsona4f5ea62016-10-28 13:58:35 +01005980 if (!obj->mm.dirty)
Chris Wilson96d77632016-10-28 13:58:33 +01005981 set_page_dirty(page);
5982
5983 return page;
5984}
5985
5986dma_addr_t
5987i915_gem_object_get_dma_address(struct drm_i915_gem_object *obj,
5988 unsigned long n)
5989{
5990 struct scatterlist *sg;
5991 unsigned int offset;
5992
5993 sg = i915_gem_object_get_sg(obj, n, &offset);
5994 return sg_dma_address(sg) + (offset << PAGE_SHIFT);
5995}
Chris Wilson935a2f72017-02-13 17:15:13 +00005996
Chris Wilson8eeb7902017-07-26 19:16:01 +01005997int i915_gem_object_attach_phys(struct drm_i915_gem_object *obj, int align)
5998{
5999 struct sg_table *pages;
6000 int err;
6001
6002 if (align > obj->base.size)
6003 return -EINVAL;
6004
6005 if (obj->ops == &i915_gem_phys_ops)
6006 return 0;
6007
6008 if (obj->ops != &i915_gem_object_ops)
6009 return -EINVAL;
6010
6011 err = i915_gem_object_unbind(obj);
6012 if (err)
6013 return err;
6014
6015 mutex_lock(&obj->mm.lock);
6016
6017 if (obj->mm.madv != I915_MADV_WILLNEED) {
6018 err = -EFAULT;
6019 goto err_unlock;
6020 }
6021
6022 if (obj->mm.quirked) {
6023 err = -EFAULT;
6024 goto err_unlock;
6025 }
6026
6027 if (obj->mm.mapping) {
6028 err = -EBUSY;
6029 goto err_unlock;
6030 }
6031
Chris Wilsonf2123812017-10-16 12:40:37 +01006032 pages = fetch_and_zero(&obj->mm.pages);
6033 if (pages) {
6034 struct drm_i915_private *i915 = to_i915(obj->base.dev);
6035
6036 __i915_gem_object_reset_page_iter(obj);
6037
6038 spin_lock(&i915->mm.obj_lock);
6039 list_del(&obj->mm.link);
6040 spin_unlock(&i915->mm.obj_lock);
6041 }
6042
Chris Wilson8eeb7902017-07-26 19:16:01 +01006043 obj->ops = &i915_gem_phys_ops;
6044
Chris Wilson8fb6a5d2017-07-26 19:16:02 +01006045 err = ____i915_gem_object_get_pages(obj);
Chris Wilson8eeb7902017-07-26 19:16:01 +01006046 if (err)
6047 goto err_xfer;
6048
6049 /* Perma-pin (until release) the physical set of pages */
6050 __i915_gem_object_pin_pages(obj);
6051
6052 if (!IS_ERR_OR_NULL(pages))
6053 i915_gem_object_ops.put_pages(obj, pages);
6054 mutex_unlock(&obj->mm.lock);
6055 return 0;
6056
6057err_xfer:
6058 obj->ops = &i915_gem_object_ops;
6059 obj->mm.pages = pages;
6060err_unlock:
6061 mutex_unlock(&obj->mm.lock);
6062 return err;
6063}
6064
Chris Wilson935a2f72017-02-13 17:15:13 +00006065#if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
6066#include "selftests/scatterlist.c"
Chris Wilson66d9cb52017-02-13 17:15:17 +00006067#include "selftests/mock_gem_device.c"
Chris Wilson44653982017-02-13 17:15:20 +00006068#include "selftests/huge_gem_object.c"
Matthew Auld40498662017-10-06 23:18:29 +01006069#include "selftests/huge_pages.c"
Chris Wilson8335fd62017-02-13 17:15:28 +00006070#include "selftests/i915_gem_object.c"
Chris Wilson17059452017-02-13 17:15:32 +00006071#include "selftests/i915_gem_coherency.c"
Chris Wilson935a2f72017-02-13 17:15:13 +00006072#endif