blob: 05f44ca35a06286cb6df02376d7ad3b0e23bde3e [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);
Chris Wilsona89d1f92018-05-02 17:38:39 +0100165 i915_timelines_park(i915);
Chris Wilsone4d20062018-04-06 16:51:44 +0100166
167 i915_pmu_gt_parked(i915);
Chris Wilson3365e222018-05-03 20:51:14 +0100168 i915_vma_parked(i915);
Chris Wilsone4d20062018-04-06 16:51:44 +0100169
170 i915->gt.awake = false;
171
172 if (INTEL_GEN(i915) >= 6)
173 gen6_rps_idle(i915);
174
175 intel_display_power_put(i915, POWER_DOMAIN_GT_IRQ);
176
177 intel_runtime_pm_put(i915);
178
179 return i915->gt.epoch;
180}
181
182void i915_gem_park(struct drm_i915_private *i915)
183{
184 lockdep_assert_held(&i915->drm.struct_mutex);
185 GEM_BUG_ON(i915->gt.active_requests);
186
187 if (!i915->gt.awake)
188 return;
189
190 /* Defer the actual call to __i915_gem_park() to prevent ping-pongs */
191 mod_delayed_work(i915->wq, &i915->gt.idle_work, msecs_to_jiffies(100));
192}
193
194void i915_gem_unpark(struct drm_i915_private *i915)
195{
196 lockdep_assert_held(&i915->drm.struct_mutex);
197 GEM_BUG_ON(!i915->gt.active_requests);
198
199 if (i915->gt.awake)
200 return;
201
202 intel_runtime_pm_get_noresume(i915);
203
204 /*
205 * It seems that the DMC likes to transition between the DC states a lot
206 * when there are no connected displays (no active power domains) during
207 * command submission.
208 *
209 * This activity has negative impact on the performance of the chip with
210 * huge latencies observed in the interrupt handler and elsewhere.
211 *
212 * Work around it by grabbing a GT IRQ power domain whilst there is any
213 * GT activity, preventing any DC state transitions.
214 */
215 intel_display_power_get(i915, POWER_DOMAIN_GT_IRQ);
216
217 i915->gt.awake = true;
218 if (unlikely(++i915->gt.epoch == 0)) /* keep 0 as invalid */
219 i915->gt.epoch = 1;
220
221 intel_enable_gt_powersave(i915);
222 i915_update_gfx_val(i915);
223 if (INTEL_GEN(i915) >= 6)
224 gen6_rps_busy(i915);
225 i915_pmu_gt_unparked(i915);
226
227 intel_engines_unpark(i915);
228
229 i915_queue_hangcheck(i915);
230
231 queue_delayed_work(i915->wq,
232 &i915->gt.retire_work,
233 round_jiffies_up_relative(HZ));
234}
235
Eric Anholt673a3942008-07-30 12:06:12 -0700236int
Eric Anholt5a125c32008-10-22 21:40:13 -0700237i915_gem_get_aperture_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +0000238 struct drm_file *file)
Eric Anholt5a125c32008-10-22 21:40:13 -0700239{
Joonas Lahtinen72e96d62016-03-30 16:57:10 +0300240 struct drm_i915_private *dev_priv = to_i915(dev);
Joonas Lahtinen62106b42016-03-18 10:42:57 +0200241 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Joonas Lahtinen72e96d62016-03-30 16:57:10 +0300242 struct drm_i915_gem_get_aperture *args = data;
Tvrtko Ursulinca1543b2015-07-01 11:51:10 +0100243 struct i915_vma *vma;
Weinan Liff8f7972017-05-31 10:35:52 +0800244 u64 pinned;
Eric Anholt5a125c32008-10-22 21:40:13 -0700245
Weinan Liff8f7972017-05-31 10:35:52 +0800246 pinned = ggtt->base.reserved;
Chris Wilson73aa8082010-09-30 11:46:12 +0100247 mutex_lock(&dev->struct_mutex);
Chris Wilson1c7f4bc2016-02-26 11:03:19 +0000248 list_for_each_entry(vma, &ggtt->base.active_list, vm_link)
Chris Wilson20dfbde2016-08-04 16:32:30 +0100249 if (i915_vma_is_pinned(vma))
Tvrtko Ursulinca1543b2015-07-01 11:51:10 +0100250 pinned += vma->node.size;
Chris Wilson1c7f4bc2016-02-26 11:03:19 +0000251 list_for_each_entry(vma, &ggtt->base.inactive_list, vm_link)
Chris Wilson20dfbde2016-08-04 16:32:30 +0100252 if (i915_vma_is_pinned(vma))
Tvrtko Ursulinca1543b2015-07-01 11:51:10 +0100253 pinned += vma->node.size;
Chris Wilson73aa8082010-09-30 11:46:12 +0100254 mutex_unlock(&dev->struct_mutex);
Eric Anholt5a125c32008-10-22 21:40:13 -0700255
Joonas Lahtinen72e96d62016-03-30 16:57:10 +0300256 args->aper_size = ggtt->base.total;
Akshay Joshi0206e352011-08-16 15:34:10 -0400257 args->aper_available_size = args->aper_size - pinned;
Chris Wilson6299f992010-11-24 12:23:44 +0000258
Eric Anholt5a125c32008-10-22 21:40:13 -0700259 return 0;
260}
261
Matthew Auldb91b09e2017-10-06 23:18:17 +0100262static int i915_gem_object_get_pages_phys(struct drm_i915_gem_object *obj)
Chris Wilson00731152014-05-21 12:42:56 +0100263{
Al Viro93c76a32015-12-04 23:45:44 -0500264 struct address_space *mapping = obj->base.filp->f_mapping;
Chris Wilsondbb43512016-12-07 13:34:11 +0000265 drm_dma_handle_t *phys;
Chris Wilson6a2c4232014-11-04 04:51:40 -0800266 struct sg_table *st;
267 struct scatterlist *sg;
Chris Wilsondbb43512016-12-07 13:34:11 +0000268 char *vaddr;
Chris Wilson6a2c4232014-11-04 04:51:40 -0800269 int i;
Matthew Auldb91b09e2017-10-06 23:18:17 +0100270 int err;
Chris Wilson00731152014-05-21 12:42:56 +0100271
Chris Wilson6a2c4232014-11-04 04:51:40 -0800272 if (WARN_ON(i915_gem_object_needs_bit17_swizzle(obj)))
Matthew Auldb91b09e2017-10-06 23:18:17 +0100273 return -EINVAL;
Chris Wilson00731152014-05-21 12:42:56 +0100274
Chris Wilsondbb43512016-12-07 13:34:11 +0000275 /* Always aligning to the object size, allows a single allocation
276 * to handle all possible callers, and given typical object sizes,
277 * the alignment of the buddy allocation will naturally match.
278 */
279 phys = drm_pci_alloc(obj->base.dev,
Ville Syrjälä750fae22017-09-07 17:32:03 +0300280 roundup_pow_of_two(obj->base.size),
Chris Wilsondbb43512016-12-07 13:34:11 +0000281 roundup_pow_of_two(obj->base.size));
282 if (!phys)
Matthew Auldb91b09e2017-10-06 23:18:17 +0100283 return -ENOMEM;
Chris Wilsondbb43512016-12-07 13:34:11 +0000284
285 vaddr = phys->vaddr;
Chris Wilson6a2c4232014-11-04 04:51:40 -0800286 for (i = 0; i < obj->base.size / PAGE_SIZE; i++) {
287 struct page *page;
288 char *src;
289
290 page = shmem_read_mapping_page(mapping, i);
Chris Wilsondbb43512016-12-07 13:34:11 +0000291 if (IS_ERR(page)) {
Matthew Auldb91b09e2017-10-06 23:18:17 +0100292 err = PTR_ERR(page);
Chris Wilsondbb43512016-12-07 13:34:11 +0000293 goto err_phys;
294 }
Chris Wilson6a2c4232014-11-04 04:51:40 -0800295
296 src = kmap_atomic(page);
297 memcpy(vaddr, src, PAGE_SIZE);
298 drm_clflush_virt_range(vaddr, PAGE_SIZE);
299 kunmap_atomic(src);
300
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300301 put_page(page);
Chris Wilson6a2c4232014-11-04 04:51:40 -0800302 vaddr += PAGE_SIZE;
303 }
304
Chris Wilsonc0336662016-05-06 15:40:21 +0100305 i915_gem_chipset_flush(to_i915(obj->base.dev));
Chris Wilson6a2c4232014-11-04 04:51:40 -0800306
307 st = kmalloc(sizeof(*st), GFP_KERNEL);
Chris Wilsondbb43512016-12-07 13:34:11 +0000308 if (!st) {
Matthew Auldb91b09e2017-10-06 23:18:17 +0100309 err = -ENOMEM;
Chris Wilsondbb43512016-12-07 13:34:11 +0000310 goto err_phys;
311 }
Chris Wilson6a2c4232014-11-04 04:51:40 -0800312
313 if (sg_alloc_table(st, 1, GFP_KERNEL)) {
314 kfree(st);
Matthew Auldb91b09e2017-10-06 23:18:17 +0100315 err = -ENOMEM;
Chris Wilsondbb43512016-12-07 13:34:11 +0000316 goto err_phys;
Chris Wilson6a2c4232014-11-04 04:51:40 -0800317 }
318
319 sg = st->sgl;
320 sg->offset = 0;
321 sg->length = obj->base.size;
322
Chris Wilsondbb43512016-12-07 13:34:11 +0000323 sg_dma_address(sg) = phys->busaddr;
Chris Wilson6a2c4232014-11-04 04:51:40 -0800324 sg_dma_len(sg) = obj->base.size;
325
Chris Wilsondbb43512016-12-07 13:34:11 +0000326 obj->phys_handle = phys;
Matthew Auldb91b09e2017-10-06 23:18:17 +0100327
Matthew Aulda5c081662017-10-06 23:18:18 +0100328 __i915_gem_object_set_pages(obj, st, sg->length);
Matthew Auldb91b09e2017-10-06 23:18:17 +0100329
330 return 0;
Chris Wilsondbb43512016-12-07 13:34:11 +0000331
332err_phys:
333 drm_pci_free(obj->base.dev, phys);
Matthew Auldb91b09e2017-10-06 23:18:17 +0100334
335 return err;
Chris Wilson6a2c4232014-11-04 04:51:40 -0800336}
337
Chris Wilsone27ab732017-06-15 13:38:49 +0100338static void __start_cpu_write(struct drm_i915_gem_object *obj)
339{
Christian Königc0a51fd2018-02-16 13:43:38 +0100340 obj->read_domains = I915_GEM_DOMAIN_CPU;
341 obj->write_domain = I915_GEM_DOMAIN_CPU;
Chris Wilsone27ab732017-06-15 13:38:49 +0100342 if (cpu_write_needs_clflush(obj))
343 obj->cache_dirty = true;
344}
345
Chris Wilson6a2c4232014-11-04 04:51:40 -0800346static void
Chris Wilson2b3c8312016-11-11 14:58:09 +0000347__i915_gem_object_release_shmem(struct drm_i915_gem_object *obj,
Chris Wilsone5facdf2016-12-23 14:57:57 +0000348 struct sg_table *pages,
349 bool needs_clflush)
Chris Wilson6a2c4232014-11-04 04:51:40 -0800350{
Chris Wilsona4f5ea62016-10-28 13:58:35 +0100351 GEM_BUG_ON(obj->mm.madv == __I915_MADV_PURGED);
Chris Wilson6a2c4232014-11-04 04:51:40 -0800352
Chris Wilsona4f5ea62016-10-28 13:58:35 +0100353 if (obj->mm.madv == I915_MADV_DONTNEED)
354 obj->mm.dirty = false;
Chris Wilson6a2c4232014-11-04 04:51:40 -0800355
Chris Wilsone5facdf2016-12-23 14:57:57 +0000356 if (needs_clflush &&
Christian Königc0a51fd2018-02-16 13:43:38 +0100357 (obj->read_domains & I915_GEM_DOMAIN_CPU) == 0 &&
Chris Wilsonb8f55be2017-08-11 12:11:16 +0100358 !(obj->cache_coherent & I915_BO_CACHE_COHERENT_FOR_READ))
Chris Wilson2b3c8312016-11-11 14:58:09 +0000359 drm_clflush_sg(pages);
Chris Wilson03ac84f2016-10-28 13:58:36 +0100360
Chris Wilsone27ab732017-06-15 13:38:49 +0100361 __start_cpu_write(obj);
Chris Wilson03ac84f2016-10-28 13:58:36 +0100362}
363
364static void
365i915_gem_object_put_pages_phys(struct drm_i915_gem_object *obj,
366 struct sg_table *pages)
367{
Chris Wilsone5facdf2016-12-23 14:57:57 +0000368 __i915_gem_object_release_shmem(obj, pages, false);
Chris Wilson03ac84f2016-10-28 13:58:36 +0100369
Chris Wilsona4f5ea62016-10-28 13:58:35 +0100370 if (obj->mm.dirty) {
Al Viro93c76a32015-12-04 23:45:44 -0500371 struct address_space *mapping = obj->base.filp->f_mapping;
Chris Wilson6a2c4232014-11-04 04:51:40 -0800372 char *vaddr = obj->phys_handle->vaddr;
Chris Wilson00731152014-05-21 12:42:56 +0100373 int i;
374
375 for (i = 0; i < obj->base.size / PAGE_SIZE; i++) {
Chris Wilson6a2c4232014-11-04 04:51:40 -0800376 struct page *page;
377 char *dst;
Chris Wilson00731152014-05-21 12:42:56 +0100378
Chris Wilson6a2c4232014-11-04 04:51:40 -0800379 page = shmem_read_mapping_page(mapping, i);
380 if (IS_ERR(page))
381 continue;
382
383 dst = kmap_atomic(page);
384 drm_clflush_virt_range(vaddr, PAGE_SIZE);
385 memcpy(dst, vaddr, PAGE_SIZE);
386 kunmap_atomic(dst);
387
388 set_page_dirty(page);
Chris Wilsona4f5ea62016-10-28 13:58:35 +0100389 if (obj->mm.madv == I915_MADV_WILLNEED)
Chris Wilson00731152014-05-21 12:42:56 +0100390 mark_page_accessed(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300391 put_page(page);
Chris Wilson00731152014-05-21 12:42:56 +0100392 vaddr += PAGE_SIZE;
393 }
Chris Wilsona4f5ea62016-10-28 13:58:35 +0100394 obj->mm.dirty = false;
Chris Wilson00731152014-05-21 12:42:56 +0100395 }
396
Chris Wilson03ac84f2016-10-28 13:58:36 +0100397 sg_free_table(pages);
398 kfree(pages);
Chris Wilsondbb43512016-12-07 13:34:11 +0000399
400 drm_pci_free(obj->base.dev, obj->phys_handle);
Chris Wilson6a2c4232014-11-04 04:51:40 -0800401}
402
403static void
404i915_gem_object_release_phys(struct drm_i915_gem_object *obj)
405{
Chris Wilsona4f5ea62016-10-28 13:58:35 +0100406 i915_gem_object_unpin_pages(obj);
Chris Wilson6a2c4232014-11-04 04:51:40 -0800407}
408
409static const struct drm_i915_gem_object_ops i915_gem_phys_ops = {
410 .get_pages = i915_gem_object_get_pages_phys,
411 .put_pages = i915_gem_object_put_pages_phys,
412 .release = i915_gem_object_release_phys,
413};
414
Chris Wilson581ab1f2017-02-15 16:39:00 +0000415static const struct drm_i915_gem_object_ops i915_gem_object_ops;
416
Chris Wilson35a96112016-08-14 18:44:40 +0100417int i915_gem_object_unbind(struct drm_i915_gem_object *obj)
Chris Wilsonaa653a62016-08-04 07:52:27 +0100418{
419 struct i915_vma *vma;
420 LIST_HEAD(still_in_list);
Chris Wilson02bef8f2016-08-14 18:44:41 +0100421 int ret;
Chris Wilsonaa653a62016-08-04 07:52:27 +0100422
Chris Wilson02bef8f2016-08-14 18:44:41 +0100423 lockdep_assert_held(&obj->base.dev->struct_mutex);
424
425 /* Closed vma are removed from the obj->vma_list - but they may
426 * still have an active binding on the object. To remove those we
427 * must wait for all rendering to complete to the object (as unbinding
428 * must anyway), and retire the requests.
Chris Wilsonaa653a62016-08-04 07:52:27 +0100429 */
Chris Wilson5888fc92017-12-04 13:25:13 +0000430 ret = i915_gem_object_set_to_cpu_domain(obj, false);
Chris Wilson02bef8f2016-08-14 18:44:41 +0100431 if (ret)
432 return ret;
433
Chris Wilsonaa653a62016-08-04 07:52:27 +0100434 while ((vma = list_first_entry_or_null(&obj->vma_list,
435 struct i915_vma,
436 obj_link))) {
437 list_move_tail(&vma->obj_link, &still_in_list);
438 ret = i915_vma_unbind(vma);
439 if (ret)
440 break;
441 }
442 list_splice(&still_in_list, &obj->vma_list);
443
444 return ret;
445}
446
Chris Wilsone95433c2016-10-28 13:58:27 +0100447static long
448i915_gem_object_wait_fence(struct dma_fence *fence,
449 unsigned int flags,
450 long timeout,
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +0100451 struct intel_rps_client *rps_client)
Chris Wilsone95433c2016-10-28 13:58:27 +0100452{
Chris Wilsone61e0f52018-02-21 09:56:36 +0000453 struct i915_request *rq;
Chris Wilsone95433c2016-10-28 13:58:27 +0100454
455 BUILD_BUG_ON(I915_WAIT_INTERRUPTIBLE != 0x1);
456
457 if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags))
458 return timeout;
459
460 if (!dma_fence_is_i915(fence))
461 return dma_fence_wait_timeout(fence,
462 flags & I915_WAIT_INTERRUPTIBLE,
463 timeout);
464
465 rq = to_request(fence);
Chris Wilsone61e0f52018-02-21 09:56:36 +0000466 if (i915_request_completed(rq))
Chris Wilsone95433c2016-10-28 13:58:27 +0100467 goto out;
468
Chris Wilsone9af4ea2018-01-18 13:16:09 +0000469 /*
470 * This client is about to stall waiting for the GPU. In many cases
Chris Wilsone95433c2016-10-28 13:58:27 +0100471 * this is undesirable and limits the throughput of the system, as
472 * many clients cannot continue processing user input/output whilst
473 * blocked. RPS autotuning may take tens of milliseconds to respond
474 * to the GPU load and thus incurs additional latency for the client.
475 * We can circumvent that by promoting the GPU frequency to maximum
476 * before we wait. This makes the GPU throttle up much more quickly
477 * (good for benchmarks and user experience, e.g. window animations),
478 * but at a cost of spending more power processing the workload
479 * (bad for battery). Not all clients even want their results
480 * immediately and for them we should just let the GPU select its own
481 * frequency to maximise efficiency. To prevent a single client from
482 * forcing the clocks too high for the whole system, we only allow
483 * each client to waitboost once in a busy period.
484 */
Chris Wilsone61e0f52018-02-21 09:56:36 +0000485 if (rps_client && !i915_request_started(rq)) {
Chris Wilsone95433c2016-10-28 13:58:27 +0100486 if (INTEL_GEN(rq->i915) >= 6)
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +0100487 gen6_rps_boost(rq, rps_client);
Chris Wilsone95433c2016-10-28 13:58:27 +0100488 }
489
Chris Wilsone61e0f52018-02-21 09:56:36 +0000490 timeout = i915_request_wait(rq, flags, timeout);
Chris Wilsone95433c2016-10-28 13:58:27 +0100491
492out:
Chris Wilsone61e0f52018-02-21 09:56:36 +0000493 if (flags & I915_WAIT_LOCKED && i915_request_completed(rq))
494 i915_request_retire_upto(rq);
Chris Wilsone95433c2016-10-28 13:58:27 +0100495
Chris Wilsone95433c2016-10-28 13:58:27 +0100496 return timeout;
497}
498
499static long
500i915_gem_object_wait_reservation(struct reservation_object *resv,
501 unsigned int flags,
502 long timeout,
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +0100503 struct intel_rps_client *rps_client)
Chris Wilsone95433c2016-10-28 13:58:27 +0100504{
Chris Wilsone54ca972017-02-17 15:13:04 +0000505 unsigned int seq = __read_seqcount_begin(&resv->seq);
Chris Wilsone95433c2016-10-28 13:58:27 +0100506 struct dma_fence *excl;
Chris Wilsone54ca972017-02-17 15:13:04 +0000507 bool prune_fences = false;
Chris Wilsone95433c2016-10-28 13:58:27 +0100508
509 if (flags & I915_WAIT_ALL) {
510 struct dma_fence **shared;
511 unsigned int count, i;
512 int ret;
513
514 ret = reservation_object_get_fences_rcu(resv,
515 &excl, &count, &shared);
516 if (ret)
517 return ret;
518
519 for (i = 0; i < count; i++) {
520 timeout = i915_gem_object_wait_fence(shared[i],
521 flags, timeout,
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +0100522 rps_client);
Chris Wilsond892e932017-02-12 21:53:43 +0000523 if (timeout < 0)
Chris Wilsone95433c2016-10-28 13:58:27 +0100524 break;
525
526 dma_fence_put(shared[i]);
527 }
528
529 for (; i < count; i++)
530 dma_fence_put(shared[i]);
531 kfree(shared);
Chris Wilsone54ca972017-02-17 15:13:04 +0000532
Chris Wilsonfa730552018-03-07 17:13:03 +0000533 /*
534 * If both shared fences and an exclusive fence exist,
535 * then by construction the shared fences must be later
536 * than the exclusive fence. If we successfully wait for
537 * all the shared fences, we know that the exclusive fence
538 * must all be signaled. If all the shared fences are
539 * signaled, we can prune the array and recover the
540 * floating references on the fences/requests.
541 */
Chris Wilsone54ca972017-02-17 15:13:04 +0000542 prune_fences = count && timeout >= 0;
Chris Wilsone95433c2016-10-28 13:58:27 +0100543 } else {
544 excl = reservation_object_get_excl_rcu(resv);
545 }
546
Chris Wilsonfa730552018-03-07 17:13:03 +0000547 if (excl && timeout >= 0)
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +0100548 timeout = i915_gem_object_wait_fence(excl, flags, timeout,
549 rps_client);
Chris Wilsone95433c2016-10-28 13:58:27 +0100550
551 dma_fence_put(excl);
552
Chris Wilsonfa730552018-03-07 17:13:03 +0000553 /*
554 * Opportunistically prune the fences iff we know they have *all* been
Chris Wilson03d1cac2017-03-08 13:26:28 +0000555 * signaled and that the reservation object has not been changed (i.e.
556 * no new fences have been added).
557 */
Chris Wilsone54ca972017-02-17 15:13:04 +0000558 if (prune_fences && !__read_seqcount_retry(&resv->seq, seq)) {
Chris Wilson03d1cac2017-03-08 13:26:28 +0000559 if (reservation_object_trylock(resv)) {
560 if (!__read_seqcount_retry(&resv->seq, seq))
561 reservation_object_add_excl_fence(resv, NULL);
562 reservation_object_unlock(resv);
563 }
Chris Wilsone54ca972017-02-17 15:13:04 +0000564 }
565
Chris Wilsone95433c2016-10-28 13:58:27 +0100566 return timeout;
567}
568
Chris Wilsonb7268c52018-04-18 19:40:52 +0100569static void __fence_set_priority(struct dma_fence *fence,
570 const struct i915_sched_attr *attr)
Chris Wilson6b5e90f2016-11-14 20:41:05 +0000571{
Chris Wilsone61e0f52018-02-21 09:56:36 +0000572 struct i915_request *rq;
Chris Wilson6b5e90f2016-11-14 20:41:05 +0000573 struct intel_engine_cs *engine;
574
Chris Wilsonc218ee02018-01-06 10:56:18 +0000575 if (dma_fence_is_signaled(fence) || !dma_fence_is_i915(fence))
Chris Wilson6b5e90f2016-11-14 20:41:05 +0000576 return;
577
578 rq = to_request(fence);
579 engine = rq->engine;
Chris Wilson6b5e90f2016-11-14 20:41:05 +0000580
Chris Wilson4f6d8fc2018-05-07 14:57:25 +0100581 local_bh_disable();
582 rcu_read_lock(); /* RCU serialisation for set-wedged protection */
Chris Wilson47650db2018-03-07 13:42:25 +0000583 if (engine->schedule)
Chris Wilsonb7268c52018-04-18 19:40:52 +0100584 engine->schedule(rq, attr);
Chris Wilson47650db2018-03-07 13:42:25 +0000585 rcu_read_unlock();
Chris Wilson4f6d8fc2018-05-07 14:57:25 +0100586 local_bh_enable(); /* kick the tasklets if queues were reprioritised */
Chris Wilson6b5e90f2016-11-14 20:41:05 +0000587}
588
Chris Wilsonb7268c52018-04-18 19:40:52 +0100589static void fence_set_priority(struct dma_fence *fence,
590 const struct i915_sched_attr *attr)
Chris Wilson6b5e90f2016-11-14 20:41:05 +0000591{
592 /* Recurse once into a fence-array */
593 if (dma_fence_is_array(fence)) {
594 struct dma_fence_array *array = to_dma_fence_array(fence);
595 int i;
596
597 for (i = 0; i < array->num_fences; i++)
Chris Wilsonb7268c52018-04-18 19:40:52 +0100598 __fence_set_priority(array->fences[i], attr);
Chris Wilson6b5e90f2016-11-14 20:41:05 +0000599 } else {
Chris Wilsonb7268c52018-04-18 19:40:52 +0100600 __fence_set_priority(fence, attr);
Chris Wilson6b5e90f2016-11-14 20:41:05 +0000601 }
602}
603
604int
605i915_gem_object_wait_priority(struct drm_i915_gem_object *obj,
606 unsigned int flags,
Chris Wilsonb7268c52018-04-18 19:40:52 +0100607 const struct i915_sched_attr *attr)
Chris Wilson6b5e90f2016-11-14 20:41:05 +0000608{
609 struct dma_fence *excl;
610
611 if (flags & I915_WAIT_ALL) {
612 struct dma_fence **shared;
613 unsigned int count, i;
614 int ret;
615
616 ret = reservation_object_get_fences_rcu(obj->resv,
617 &excl, &count, &shared);
618 if (ret)
619 return ret;
620
621 for (i = 0; i < count; i++) {
Chris Wilsonb7268c52018-04-18 19:40:52 +0100622 fence_set_priority(shared[i], attr);
Chris Wilson6b5e90f2016-11-14 20:41:05 +0000623 dma_fence_put(shared[i]);
624 }
625
626 kfree(shared);
627 } else {
628 excl = reservation_object_get_excl_rcu(obj->resv);
629 }
630
631 if (excl) {
Chris Wilsonb7268c52018-04-18 19:40:52 +0100632 fence_set_priority(excl, attr);
Chris Wilson6b5e90f2016-11-14 20:41:05 +0000633 dma_fence_put(excl);
634 }
635 return 0;
636}
637
Chris Wilson00e60f22016-08-04 16:32:40 +0100638/**
Chris Wilsone95433c2016-10-28 13:58:27 +0100639 * Waits for rendering to the object to be completed
Chris Wilson00e60f22016-08-04 16:32:40 +0100640 * @obj: i915 gem object
Chris Wilsone95433c2016-10-28 13:58:27 +0100641 * @flags: how to wait (under a lock, for all rendering or just for writes etc)
642 * @timeout: how long to wait
Chris Wilsona0a8b1c2017-11-09 14:06:44 +0000643 * @rps_client: client (user process) to charge for any waitboosting
Chris Wilson00e60f22016-08-04 16:32:40 +0100644 */
645int
Chris Wilsone95433c2016-10-28 13:58:27 +0100646i915_gem_object_wait(struct drm_i915_gem_object *obj,
647 unsigned int flags,
648 long timeout,
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +0100649 struct intel_rps_client *rps_client)
Chris Wilson00e60f22016-08-04 16:32:40 +0100650{
Chris Wilsone95433c2016-10-28 13:58:27 +0100651 might_sleep();
652#if IS_ENABLED(CONFIG_LOCKDEP)
653 GEM_BUG_ON(debug_locks &&
654 !!lockdep_is_held(&obj->base.dev->struct_mutex) !=
655 !!(flags & I915_WAIT_LOCKED));
656#endif
657 GEM_BUG_ON(timeout < 0);
Chris Wilson00e60f22016-08-04 16:32:40 +0100658
Chris Wilsond07f0e52016-10-28 13:58:44 +0100659 timeout = i915_gem_object_wait_reservation(obj->resv,
660 flags, timeout,
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +0100661 rps_client);
Chris Wilsone95433c2016-10-28 13:58:27 +0100662 return timeout < 0 ? timeout : 0;
Chris Wilson00e60f22016-08-04 16:32:40 +0100663}
664
665static struct intel_rps_client *to_rps_client(struct drm_file *file)
666{
667 struct drm_i915_file_private *fpriv = file->driver_priv;
668
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +0100669 return &fpriv->rps_client;
Chris Wilson00e60f22016-08-04 16:32:40 +0100670}
671
Chris Wilson00731152014-05-21 12:42:56 +0100672static int
673i915_gem_phys_pwrite(struct drm_i915_gem_object *obj,
674 struct drm_i915_gem_pwrite *args,
Chris Wilson03ac84f2016-10-28 13:58:36 +0100675 struct drm_file *file)
Chris Wilson00731152014-05-21 12:42:56 +0100676{
Chris Wilson00731152014-05-21 12:42:56 +0100677 void *vaddr = obj->phys_handle->vaddr + args->offset;
Gustavo Padovan3ed605b2016-04-26 12:32:27 -0300678 char __user *user_data = u64_to_user_ptr(args->data_ptr);
Chris Wilson6a2c4232014-11-04 04:51:40 -0800679
680 /* We manually control the domain here and pretend that it
681 * remains coherent i.e. in the GTT domain, like shmem_pwrite.
682 */
Rodrigo Vivi77a0d1c2015-06-18 11:43:24 -0700683 intel_fb_obj_invalidate(obj, ORIGIN_CPU);
Chris Wilson10466d22017-01-06 15:22:38 +0000684 if (copy_from_user(vaddr, user_data, args->size))
685 return -EFAULT;
Chris Wilson00731152014-05-21 12:42:56 +0100686
Chris Wilson6a2c4232014-11-04 04:51:40 -0800687 drm_clflush_virt_range(vaddr, args->size);
Chris Wilson10466d22017-01-06 15:22:38 +0000688 i915_gem_chipset_flush(to_i915(obj->base.dev));
Paulo Zanoni063e4e62015-02-13 17:23:45 -0200689
Chris Wilsond59b21e2017-02-22 11:40:49 +0000690 intel_fb_obj_flush(obj, ORIGIN_CPU);
Chris Wilson10466d22017-01-06 15:22:38 +0000691 return 0;
Chris Wilson00731152014-05-21 12:42:56 +0100692}
693
Tvrtko Ursulin187685c2016-12-01 14:16:36 +0000694void *i915_gem_object_alloc(struct drm_i915_private *dev_priv)
Chris Wilson42dcedd2012-11-15 11:32:30 +0000695{
Chris Wilsonefab6d82015-04-07 16:20:57 +0100696 return kmem_cache_zalloc(dev_priv->objects, GFP_KERNEL);
Chris Wilson42dcedd2012-11-15 11:32:30 +0000697}
698
699void i915_gem_object_free(struct drm_i915_gem_object *obj)
700{
Chris Wilsonfac5e232016-07-04 11:34:36 +0100701 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
Chris Wilsonefab6d82015-04-07 16:20:57 +0100702 kmem_cache_free(dev_priv->objects, obj);
Chris Wilson42dcedd2012-11-15 11:32:30 +0000703}
704
Dave Airlieff72145b2011-02-07 12:16:14 +1000705static int
706i915_gem_create(struct drm_file *file,
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +0000707 struct drm_i915_private *dev_priv,
Dave Airlieff72145b2011-02-07 12:16:14 +1000708 uint64_t size,
709 uint32_t *handle_p)
Eric Anholt673a3942008-07-30 12:06:12 -0700710{
Chris Wilson05394f32010-11-08 19:18:58 +0000711 struct drm_i915_gem_object *obj;
Pekka Paalanena1a2d1d2009-08-23 12:40:55 +0300712 int ret;
713 u32 handle;
Eric Anholt673a3942008-07-30 12:06:12 -0700714
Dave Airlieff72145b2011-02-07 12:16:14 +1000715 size = roundup(size, PAGE_SIZE);
Chris Wilson8ffc0242011-09-14 14:14:28 +0200716 if (size == 0)
717 return -EINVAL;
Eric Anholt673a3942008-07-30 12:06:12 -0700718
719 /* Allocate the new object */
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +0000720 obj = i915_gem_object_create(dev_priv, size);
Chris Wilsonfe3db792016-04-25 13:32:13 +0100721 if (IS_ERR(obj))
722 return PTR_ERR(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700723
Chris Wilson05394f32010-11-08 19:18:58 +0000724 ret = drm_gem_handle_create(file, &obj->base, &handle);
Chris Wilson202f2fe2010-10-14 13:20:40 +0100725 /* drop reference from allocate - handle holds it now */
Chris Wilsonf0cd5182016-10-28 13:58:43 +0100726 i915_gem_object_put(obj);
Daniel Vetterd861e332013-07-24 23:25:03 +0200727 if (ret)
728 return ret;
Chris Wilson202f2fe2010-10-14 13:20:40 +0100729
Dave Airlieff72145b2011-02-07 12:16:14 +1000730 *handle_p = handle;
Eric Anholt673a3942008-07-30 12:06:12 -0700731 return 0;
732}
733
Dave Airlieff72145b2011-02-07 12:16:14 +1000734int
735i915_gem_dumb_create(struct drm_file *file,
736 struct drm_device *dev,
737 struct drm_mode_create_dumb *args)
738{
739 /* have to work out size/pitch and return them */
Paulo Zanonide45eaf2013-10-18 18:48:24 -0300740 args->pitch = ALIGN(args->width * DIV_ROUND_UP(args->bpp, 8), 64);
Dave Airlieff72145b2011-02-07 12:16:14 +1000741 args->size = args->pitch * args->height;
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +0000742 return i915_gem_create(file, to_i915(dev),
Dave Airlieda6b51d2014-12-24 13:11:17 +1000743 args->size, &args->handle);
Dave Airlieff72145b2011-02-07 12:16:14 +1000744}
745
Chris Wilsone27ab732017-06-15 13:38:49 +0100746static bool gpu_write_needs_clflush(struct drm_i915_gem_object *obj)
747{
748 return !(obj->cache_level == I915_CACHE_NONE ||
749 obj->cache_level == I915_CACHE_WT);
750}
751
Dave Airlieff72145b2011-02-07 12:16:14 +1000752/**
753 * Creates a new mm object and returns a handle to it.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +0100754 * @dev: drm device pointer
755 * @data: ioctl data blob
756 * @file: drm file pointer
Dave Airlieff72145b2011-02-07 12:16:14 +1000757 */
758int
759i915_gem_create_ioctl(struct drm_device *dev, void *data,
760 struct drm_file *file)
761{
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +0000762 struct drm_i915_private *dev_priv = to_i915(dev);
Dave Airlieff72145b2011-02-07 12:16:14 +1000763 struct drm_i915_gem_create *args = data;
Daniel Vetter63ed2cb2012-04-23 16:50:50 +0200764
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +0000765 i915_gem_flush_free_objects(dev_priv);
Chris Wilsonfbbd37b2016-10-28 13:58:42 +0100766
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +0000767 return i915_gem_create(file, dev_priv,
Dave Airlieda6b51d2014-12-24 13:11:17 +1000768 args->size, &args->handle);
Dave Airlieff72145b2011-02-07 12:16:14 +1000769}
770
Chris Wilsonef749212017-04-12 12:01:10 +0100771static inline enum fb_op_origin
772fb_write_origin(struct drm_i915_gem_object *obj, unsigned int domain)
773{
774 return (domain == I915_GEM_DOMAIN_GTT ?
775 obj->frontbuffer_ggtt_origin : ORIGIN_CPU);
776}
777
Chris Wilson7125397b2017-12-06 12:49:14 +0000778void i915_gem_flush_ggtt_writes(struct drm_i915_private *dev_priv)
Chris Wilsonef749212017-04-12 12:01:10 +0100779{
Chris Wilson7125397b2017-12-06 12:49:14 +0000780 /*
781 * No actual flushing is required for the GTT write domain for reads
782 * from the GTT domain. Writes to it "immediately" go to main memory
783 * as far as we know, so there's no chipset flush. It also doesn't
784 * land in the GPU render cache.
Chris Wilsonef749212017-04-12 12:01:10 +0100785 *
786 * However, we do have to enforce the order so that all writes through
787 * the GTT land before any writes to the device, such as updates to
788 * the GATT itself.
789 *
790 * We also have to wait a bit for the writes to land from the GTT.
791 * An uncached read (i.e. mmio) seems to be ideal for the round-trip
792 * timing. This issue has only been observed when switching quickly
793 * between GTT writes and CPU reads from inside the kernel on recent hw,
794 * and it appears to only affect discrete GTT blocks (i.e. on LLC
Chris Wilson7125397b2017-12-06 12:49:14 +0000795 * system agents we cannot reproduce this behaviour, until Cannonlake
796 * that was!).
Chris Wilsonef749212017-04-12 12:01:10 +0100797 */
Chris Wilson7125397b2017-12-06 12:49:14 +0000798
Chris Wilsonef749212017-04-12 12:01:10 +0100799 wmb();
800
Chris Wilson7125397b2017-12-06 12:49:14 +0000801 intel_runtime_pm_get(dev_priv);
802 spin_lock_irq(&dev_priv->uncore.lock);
803
804 POSTING_READ_FW(RING_HEAD(RENDER_RING_BASE));
805
806 spin_unlock_irq(&dev_priv->uncore.lock);
807 intel_runtime_pm_put(dev_priv);
808}
809
810static void
811flush_write_domain(struct drm_i915_gem_object *obj, unsigned int flush_domains)
812{
813 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
814 struct i915_vma *vma;
815
Christian Königc0a51fd2018-02-16 13:43:38 +0100816 if (!(obj->write_domain & flush_domains))
Chris Wilson7125397b2017-12-06 12:49:14 +0000817 return;
818
Christian Königc0a51fd2018-02-16 13:43:38 +0100819 switch (obj->write_domain) {
Chris Wilsonef749212017-04-12 12:01:10 +0100820 case I915_GEM_DOMAIN_GTT:
Chris Wilson7125397b2017-12-06 12:49:14 +0000821 i915_gem_flush_ggtt_writes(dev_priv);
Chris Wilsonef749212017-04-12 12:01:10 +0100822
823 intel_fb_obj_flush(obj,
824 fb_write_origin(obj, I915_GEM_DOMAIN_GTT));
Chris Wilson7125397b2017-12-06 12:49:14 +0000825
Chris Wilsone2189dd2017-12-07 21:14:07 +0000826 for_each_ggtt_vma(vma, obj) {
Chris Wilson7125397b2017-12-06 12:49:14 +0000827 if (vma->iomap)
828 continue;
829
830 i915_vma_unset_ggtt_write(vma);
831 }
Chris Wilsonef749212017-04-12 12:01:10 +0100832 break;
833
834 case I915_GEM_DOMAIN_CPU:
835 i915_gem_clflush_object(obj, I915_CLFLUSH_SYNC);
836 break;
Chris Wilsone27ab732017-06-15 13:38:49 +0100837
838 case I915_GEM_DOMAIN_RENDER:
839 if (gpu_write_needs_clflush(obj))
840 obj->cache_dirty = true;
841 break;
Chris Wilsonef749212017-04-12 12:01:10 +0100842 }
843
Christian Königc0a51fd2018-02-16 13:43:38 +0100844 obj->write_domain = 0;
Chris Wilsonef749212017-04-12 12:01:10 +0100845}
846
Daniel Vetter8c599672011-12-14 13:57:31 +0100847static inline int
Daniel Vetter8461d222011-12-14 13:57:32 +0100848__copy_to_user_swizzled(char __user *cpu_vaddr,
849 const char *gpu_vaddr, int gpu_offset,
850 int length)
851{
852 int ret, cpu_offset = 0;
853
854 while (length > 0) {
855 int cacheline_end = ALIGN(gpu_offset + 1, 64);
856 int this_length = min(cacheline_end - gpu_offset, length);
857 int swizzled_gpu_offset = gpu_offset ^ 64;
858
859 ret = __copy_to_user(cpu_vaddr + cpu_offset,
860 gpu_vaddr + swizzled_gpu_offset,
861 this_length);
862 if (ret)
863 return ret + length;
864
865 cpu_offset += this_length;
866 gpu_offset += this_length;
867 length -= this_length;
868 }
869
870 return 0;
871}
872
873static inline int
Ben Widawsky4f0c7cf2012-04-16 14:07:47 -0700874__copy_from_user_swizzled(char *gpu_vaddr, int gpu_offset,
875 const char __user *cpu_vaddr,
Daniel Vetter8c599672011-12-14 13:57:31 +0100876 int length)
877{
878 int ret, cpu_offset = 0;
879
880 while (length > 0) {
881 int cacheline_end = ALIGN(gpu_offset + 1, 64);
882 int this_length = min(cacheline_end - gpu_offset, length);
883 int swizzled_gpu_offset = gpu_offset ^ 64;
884
885 ret = __copy_from_user(gpu_vaddr + swizzled_gpu_offset,
886 cpu_vaddr + cpu_offset,
887 this_length);
888 if (ret)
889 return ret + length;
890
891 cpu_offset += this_length;
892 gpu_offset += this_length;
893 length -= this_length;
894 }
895
896 return 0;
897}
898
Brad Volkin4c914c02014-02-18 10:15:45 -0800899/*
900 * Pins the specified object's pages and synchronizes the object with
901 * GPU accesses. Sets needs_clflush to non-zero if the caller should
902 * flush the object from the CPU cache.
903 */
904int i915_gem_obj_prepare_shmem_read(struct drm_i915_gem_object *obj,
Chris Wilson43394c72016-08-18 17:16:47 +0100905 unsigned int *needs_clflush)
Brad Volkin4c914c02014-02-18 10:15:45 -0800906{
907 int ret;
908
Chris Wilsone95433c2016-10-28 13:58:27 +0100909 lockdep_assert_held(&obj->base.dev->struct_mutex);
Brad Volkin4c914c02014-02-18 10:15:45 -0800910
Chris Wilsone95433c2016-10-28 13:58:27 +0100911 *needs_clflush = 0;
Chris Wilson43394c72016-08-18 17:16:47 +0100912 if (!i915_gem_object_has_struct_page(obj))
913 return -ENODEV;
Brad Volkin4c914c02014-02-18 10:15:45 -0800914
Chris Wilsone95433c2016-10-28 13:58:27 +0100915 ret = i915_gem_object_wait(obj,
916 I915_WAIT_INTERRUPTIBLE |
917 I915_WAIT_LOCKED,
918 MAX_SCHEDULE_TIMEOUT,
919 NULL);
Chris Wilsonc13d87e2016-07-20 09:21:15 +0100920 if (ret)
921 return ret;
922
Chris Wilsona4f5ea62016-10-28 13:58:35 +0100923 ret = i915_gem_object_pin_pages(obj);
Chris Wilson97649512016-08-18 17:16:50 +0100924 if (ret)
925 return ret;
926
Chris Wilsonb8f55be2017-08-11 12:11:16 +0100927 if (obj->cache_coherent & I915_BO_CACHE_COHERENT_FOR_READ ||
928 !static_cpu_has(X86_FEATURE_CLFLUSH)) {
Chris Wilson7f5f95d2017-03-10 00:09:42 +0000929 ret = i915_gem_object_set_to_cpu_domain(obj, false);
930 if (ret)
931 goto err_unpin;
932 else
933 goto out;
934 }
935
Chris Wilsonef749212017-04-12 12:01:10 +0100936 flush_write_domain(obj, ~I915_GEM_DOMAIN_CPU);
Chris Wilsona314d5c2016-08-18 17:16:48 +0100937
Chris Wilson43394c72016-08-18 17:16:47 +0100938 /* If we're not in the cpu read domain, set ourself into the gtt
939 * read domain and manually flush cachelines (if required). This
940 * optimizes for the case when the gpu will dirty the data
941 * anyway again before the next pread happens.
942 */
Chris Wilsone27ab732017-06-15 13:38:49 +0100943 if (!obj->cache_dirty &&
Christian Königc0a51fd2018-02-16 13:43:38 +0100944 !(obj->read_domains & I915_GEM_DOMAIN_CPU))
Chris Wilson7f5f95d2017-03-10 00:09:42 +0000945 *needs_clflush = CLFLUSH_BEFORE;
Brad Volkin4c914c02014-02-18 10:15:45 -0800946
Chris Wilson7f5f95d2017-03-10 00:09:42 +0000947out:
Chris Wilson97649512016-08-18 17:16:50 +0100948 /* return with the pages pinned */
Chris Wilson43394c72016-08-18 17:16:47 +0100949 return 0;
Chris Wilson97649512016-08-18 17:16:50 +0100950
951err_unpin:
952 i915_gem_object_unpin_pages(obj);
953 return ret;
Chris Wilson43394c72016-08-18 17:16:47 +0100954}
955
956int i915_gem_obj_prepare_shmem_write(struct drm_i915_gem_object *obj,
957 unsigned int *needs_clflush)
958{
959 int ret;
960
Chris Wilsone95433c2016-10-28 13:58:27 +0100961 lockdep_assert_held(&obj->base.dev->struct_mutex);
962
Chris Wilson43394c72016-08-18 17:16:47 +0100963 *needs_clflush = 0;
964 if (!i915_gem_object_has_struct_page(obj))
965 return -ENODEV;
966
Chris Wilsone95433c2016-10-28 13:58:27 +0100967 ret = i915_gem_object_wait(obj,
968 I915_WAIT_INTERRUPTIBLE |
969 I915_WAIT_LOCKED |
970 I915_WAIT_ALL,
971 MAX_SCHEDULE_TIMEOUT,
972 NULL);
Chris Wilson43394c72016-08-18 17:16:47 +0100973 if (ret)
974 return ret;
975
Chris Wilsona4f5ea62016-10-28 13:58:35 +0100976 ret = i915_gem_object_pin_pages(obj);
Chris Wilson97649512016-08-18 17:16:50 +0100977 if (ret)
978 return ret;
979
Chris Wilsonb8f55be2017-08-11 12:11:16 +0100980 if (obj->cache_coherent & I915_BO_CACHE_COHERENT_FOR_WRITE ||
981 !static_cpu_has(X86_FEATURE_CLFLUSH)) {
Chris Wilson7f5f95d2017-03-10 00:09:42 +0000982 ret = i915_gem_object_set_to_cpu_domain(obj, true);
983 if (ret)
984 goto err_unpin;
985 else
986 goto out;
987 }
988
Chris Wilsonef749212017-04-12 12:01:10 +0100989 flush_write_domain(obj, ~I915_GEM_DOMAIN_CPU);
Chris Wilsona314d5c2016-08-18 17:16:48 +0100990
Chris Wilson43394c72016-08-18 17:16:47 +0100991 /* If we're not in the cpu write domain, set ourself into the
992 * gtt write domain and manually flush cachelines (as required).
993 * This optimizes for the case when the gpu will use the data
994 * right away and we therefore have to clflush anyway.
995 */
Chris Wilsone27ab732017-06-15 13:38:49 +0100996 if (!obj->cache_dirty) {
Chris Wilson7f5f95d2017-03-10 00:09:42 +0000997 *needs_clflush |= CLFLUSH_AFTER;
Chris Wilson43394c72016-08-18 17:16:47 +0100998
Chris Wilsone27ab732017-06-15 13:38:49 +0100999 /*
1000 * Same trick applies to invalidate partially written
1001 * cachelines read before writing.
1002 */
Christian Königc0a51fd2018-02-16 13:43:38 +01001003 if (!(obj->read_domains & I915_GEM_DOMAIN_CPU))
Chris Wilsone27ab732017-06-15 13:38:49 +01001004 *needs_clflush |= CLFLUSH_BEFORE;
1005 }
Chris Wilson43394c72016-08-18 17:16:47 +01001006
Chris Wilson7f5f95d2017-03-10 00:09:42 +00001007out:
Chris Wilson43394c72016-08-18 17:16:47 +01001008 intel_fb_obj_invalidate(obj, ORIGIN_CPU);
Chris Wilsona4f5ea62016-10-28 13:58:35 +01001009 obj->mm.dirty = true;
Chris Wilson97649512016-08-18 17:16:50 +01001010 /* return with the pages pinned */
Chris Wilson43394c72016-08-18 17:16:47 +01001011 return 0;
Chris Wilson97649512016-08-18 17:16:50 +01001012
1013err_unpin:
1014 i915_gem_object_unpin_pages(obj);
1015 return ret;
Brad Volkin4c914c02014-02-18 10:15:45 -08001016}
1017
Daniel Vetter23c18c72012-03-25 19:47:42 +02001018static void
1019shmem_clflush_swizzled_range(char *addr, unsigned long length,
1020 bool swizzled)
1021{
Daniel Vettere7e58eb2012-03-25 19:47:43 +02001022 if (unlikely(swizzled)) {
Daniel Vetter23c18c72012-03-25 19:47:42 +02001023 unsigned long start = (unsigned long) addr;
1024 unsigned long end = (unsigned long) addr + length;
1025
1026 /* For swizzling simply ensure that we always flush both
1027 * channels. Lame, but simple and it works. Swizzled
1028 * pwrite/pread is far from a hotpath - current userspace
1029 * doesn't use it at all. */
1030 start = round_down(start, 128);
1031 end = round_up(end, 128);
1032
1033 drm_clflush_virt_range((void *)start, end - start);
1034 } else {
1035 drm_clflush_virt_range(addr, length);
1036 }
1037
1038}
1039
Daniel Vetterd174bd62012-03-25 19:47:40 +02001040/* Only difference to the fast-path function is that this can handle bit17
1041 * and uses non-atomic copy and kmap functions. */
1042static int
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001043shmem_pread_slow(struct page *page, int offset, int length,
Daniel Vetterd174bd62012-03-25 19:47:40 +02001044 char __user *user_data,
1045 bool page_do_bit17_swizzling, bool needs_clflush)
1046{
1047 char *vaddr;
1048 int ret;
1049
1050 vaddr = kmap(page);
1051 if (needs_clflush)
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001052 shmem_clflush_swizzled_range(vaddr + offset, length,
Daniel Vetter23c18c72012-03-25 19:47:42 +02001053 page_do_bit17_swizzling);
Daniel Vetterd174bd62012-03-25 19:47:40 +02001054
1055 if (page_do_bit17_swizzling)
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001056 ret = __copy_to_user_swizzled(user_data, vaddr, offset, length);
Daniel Vetterd174bd62012-03-25 19:47:40 +02001057 else
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001058 ret = __copy_to_user(user_data, vaddr + offset, length);
Daniel Vetterd174bd62012-03-25 19:47:40 +02001059 kunmap(page);
1060
Chris Wilsonf60d7f02012-09-04 21:02:56 +01001061 return ret ? - EFAULT : 0;
Daniel Vetterd174bd62012-03-25 19:47:40 +02001062}
1063
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001064static int
1065shmem_pread(struct page *page, int offset, int length, char __user *user_data,
1066 bool page_do_bit17_swizzling, bool needs_clflush)
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301067{
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001068 int ret;
1069
1070 ret = -ENODEV;
1071 if (!page_do_bit17_swizzling) {
1072 char *vaddr = kmap_atomic(page);
1073
1074 if (needs_clflush)
1075 drm_clflush_virt_range(vaddr + offset, length);
1076 ret = __copy_to_user_inatomic(user_data, vaddr + offset, length);
1077 kunmap_atomic(vaddr);
1078 }
1079 if (ret == 0)
1080 return 0;
1081
1082 return shmem_pread_slow(page, offset, length, user_data,
1083 page_do_bit17_swizzling, needs_clflush);
1084}
1085
1086static int
1087i915_gem_shmem_pread(struct drm_i915_gem_object *obj,
1088 struct drm_i915_gem_pread *args)
1089{
1090 char __user *user_data;
1091 u64 remain;
1092 unsigned int obj_do_bit17_swizzling;
1093 unsigned int needs_clflush;
1094 unsigned int idx, offset;
1095 int ret;
1096
1097 obj_do_bit17_swizzling = 0;
1098 if (i915_gem_object_needs_bit17_swizzle(obj))
1099 obj_do_bit17_swizzling = BIT(17);
1100
1101 ret = mutex_lock_interruptible(&obj->base.dev->struct_mutex);
1102 if (ret)
1103 return ret;
1104
1105 ret = i915_gem_obj_prepare_shmem_read(obj, &needs_clflush);
1106 mutex_unlock(&obj->base.dev->struct_mutex);
1107 if (ret)
1108 return ret;
1109
1110 remain = args->size;
1111 user_data = u64_to_user_ptr(args->data_ptr);
1112 offset = offset_in_page(args->offset);
1113 for (idx = args->offset >> PAGE_SHIFT; remain; idx++) {
1114 struct page *page = i915_gem_object_get_page(obj, idx);
1115 int length;
1116
1117 length = remain;
1118 if (offset + length > PAGE_SIZE)
1119 length = PAGE_SIZE - offset;
1120
1121 ret = shmem_pread(page, offset, length, user_data,
1122 page_to_phys(page) & obj_do_bit17_swizzling,
1123 needs_clflush);
1124 if (ret)
1125 break;
1126
1127 remain -= length;
1128 user_data += length;
1129 offset = 0;
1130 }
1131
1132 i915_gem_obj_finish_shmem_access(obj);
1133 return ret;
1134}
1135
1136static inline bool
1137gtt_user_read(struct io_mapping *mapping,
1138 loff_t base, int offset,
1139 char __user *user_data, int length)
1140{
Ville Syrjäläafe722b2017-09-01 20:12:52 +03001141 void __iomem *vaddr;
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001142 unsigned long unwritten;
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301143
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301144 /* We can use the cpu mem copy function because this is X86. */
Ville Syrjäläafe722b2017-09-01 20:12:52 +03001145 vaddr = io_mapping_map_atomic_wc(mapping, base);
1146 unwritten = __copy_to_user_inatomic(user_data,
1147 (void __force *)vaddr + offset,
1148 length);
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001149 io_mapping_unmap_atomic(vaddr);
1150 if (unwritten) {
Ville Syrjäläafe722b2017-09-01 20:12:52 +03001151 vaddr = io_mapping_map_wc(mapping, base, PAGE_SIZE);
1152 unwritten = copy_to_user(user_data,
1153 (void __force *)vaddr + offset,
1154 length);
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001155 io_mapping_unmap(vaddr);
1156 }
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301157 return unwritten;
1158}
1159
1160static int
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001161i915_gem_gtt_pread(struct drm_i915_gem_object *obj,
1162 const struct drm_i915_gem_pread *args)
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301163{
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001164 struct drm_i915_private *i915 = to_i915(obj->base.dev);
1165 struct i915_ggtt *ggtt = &i915->ggtt;
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301166 struct drm_mm_node node;
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001167 struct i915_vma *vma;
1168 void __user *user_data;
1169 u64 remain, offset;
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301170 int ret;
1171
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001172 ret = mutex_lock_interruptible(&i915->drm.struct_mutex);
1173 if (ret)
1174 return ret;
1175
1176 intel_runtime_pm_get(i915);
1177 vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0,
Chris Wilsona3259ca2017-10-09 09:44:00 +01001178 PIN_MAPPABLE |
1179 PIN_NONFAULT |
1180 PIN_NONBLOCK);
Chris Wilson18034582016-08-18 17:16:45 +01001181 if (!IS_ERR(vma)) {
1182 node.start = i915_ggtt_offset(vma);
1183 node.allocated = false;
Chris Wilson49ef5292016-08-18 17:17:00 +01001184 ret = i915_vma_put_fence(vma);
Chris Wilson18034582016-08-18 17:16:45 +01001185 if (ret) {
1186 i915_vma_unpin(vma);
1187 vma = ERR_PTR(ret);
1188 }
1189 }
Chris Wilson058d88c2016-08-15 10:49:06 +01001190 if (IS_ERR(vma)) {
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001191 ret = insert_mappable_node(ggtt, &node, PAGE_SIZE);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301192 if (ret)
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001193 goto out_unlock;
1194 GEM_BUG_ON(!node.allocated);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301195 }
1196
1197 ret = i915_gem_object_set_to_gtt_domain(obj, false);
1198 if (ret)
1199 goto out_unpin;
1200
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001201 mutex_unlock(&i915->drm.struct_mutex);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301202
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001203 user_data = u64_to_user_ptr(args->data_ptr);
1204 remain = args->size;
1205 offset = args->offset;
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301206
1207 while (remain > 0) {
1208 /* Operation in this page
1209 *
1210 * page_base = page offset within aperture
1211 * page_offset = offset within page
1212 * page_length = bytes to copy for this page
1213 */
1214 u32 page_base = node.start;
1215 unsigned page_offset = offset_in_page(offset);
1216 unsigned page_length = PAGE_SIZE - page_offset;
1217 page_length = remain < page_length ? remain : page_length;
1218 if (node.allocated) {
1219 wmb();
1220 ggtt->base.insert_page(&ggtt->base,
1221 i915_gem_object_get_dma_address(obj, offset >> PAGE_SHIFT),
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001222 node.start, I915_CACHE_NONE, 0);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301223 wmb();
1224 } else {
1225 page_base += offset & PAGE_MASK;
1226 }
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001227
Matthew Auld73ebd502017-12-11 15:18:20 +00001228 if (gtt_user_read(&ggtt->iomap, page_base, page_offset,
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001229 user_data, page_length)) {
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301230 ret = -EFAULT;
1231 break;
1232 }
1233
1234 remain -= page_length;
1235 user_data += page_length;
1236 offset += page_length;
1237 }
1238
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001239 mutex_lock(&i915->drm.struct_mutex);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301240out_unpin:
1241 if (node.allocated) {
1242 wmb();
1243 ggtt->base.clear_range(&ggtt->base,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02001244 node.start, node.size);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301245 remove_mappable_node(&node);
1246 } else {
Chris Wilson058d88c2016-08-15 10:49:06 +01001247 i915_vma_unpin(vma);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301248 }
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001249out_unlock:
1250 intel_runtime_pm_put(i915);
1251 mutex_unlock(&i915->drm.struct_mutex);
Chris Wilsonf60d7f02012-09-04 21:02:56 +01001252
Eric Anholteb014592009-03-10 11:44:52 -07001253 return ret;
1254}
1255
Eric Anholt673a3942008-07-30 12:06:12 -07001256/**
1257 * Reads data from the object referenced by handle.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01001258 * @dev: drm device pointer
1259 * @data: ioctl data blob
1260 * @file: drm file pointer
Eric Anholt673a3942008-07-30 12:06:12 -07001261 *
1262 * On error, the contents of *data are undefined.
1263 */
1264int
1265i915_gem_pread_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00001266 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001267{
1268 struct drm_i915_gem_pread *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00001269 struct drm_i915_gem_object *obj;
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001270 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07001271
Chris Wilson51311d02010-11-17 09:10:42 +00001272 if (args->size == 0)
1273 return 0;
1274
1275 if (!access_ok(VERIFY_WRITE,
Gustavo Padovan3ed605b2016-04-26 12:32:27 -03001276 u64_to_user_ptr(args->data_ptr),
Chris Wilson51311d02010-11-17 09:10:42 +00001277 args->size))
1278 return -EFAULT;
1279
Chris Wilson03ac0642016-07-20 13:31:51 +01001280 obj = i915_gem_object_lookup(file, args->handle);
Chris Wilson258a5ed2016-08-05 10:14:16 +01001281 if (!obj)
1282 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07001283
Chris Wilson7dcd2492010-09-26 20:21:44 +01001284 /* Bounds check source. */
Matthew Auld966d5bf2016-12-13 20:32:22 +00001285 if (range_overflows_t(u64, args->offset, args->size, obj->base.size)) {
Chris Wilsonce9d4192010-09-26 20:50:05 +01001286 ret = -EINVAL;
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001287 goto out;
Chris Wilsonce9d4192010-09-26 20:50:05 +01001288 }
1289
Chris Wilsondb53a302011-02-03 11:57:46 +00001290 trace_i915_gem_object_pread(obj, args->offset, args->size);
1291
Chris Wilsone95433c2016-10-28 13:58:27 +01001292 ret = i915_gem_object_wait(obj,
1293 I915_WAIT_INTERRUPTIBLE,
1294 MAX_SCHEDULE_TIMEOUT,
1295 to_rps_client(file));
Chris Wilson258a5ed2016-08-05 10:14:16 +01001296 if (ret)
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001297 goto out;
Chris Wilson258a5ed2016-08-05 10:14:16 +01001298
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001299 ret = i915_gem_object_pin_pages(obj);
Chris Wilson258a5ed2016-08-05 10:14:16 +01001300 if (ret)
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001301 goto out;
Chris Wilson258a5ed2016-08-05 10:14:16 +01001302
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001303 ret = i915_gem_shmem_pread(obj, args);
Chris Wilson9c870d02016-10-24 13:42:15 +01001304 if (ret == -EFAULT || ret == -ENODEV)
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001305 ret = i915_gem_gtt_pread(obj, args);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301306
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001307 i915_gem_object_unpin_pages(obj);
1308out:
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001309 i915_gem_object_put(obj);
Eric Anholteb014592009-03-10 11:44:52 -07001310 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07001311}
1312
Keith Packard0839ccb2008-10-30 19:38:48 -07001313/* This is the fast write path which cannot handle
1314 * page faults in the source data
Linus Torvalds9b7530cc2008-10-20 14:16:43 -07001315 */
Linus Torvalds9b7530cc2008-10-20 14:16:43 -07001316
Chris Wilsonfe115622016-10-28 13:58:40 +01001317static inline bool
1318ggtt_write(struct io_mapping *mapping,
1319 loff_t base, int offset,
1320 char __user *user_data, int length)
Keith Packard0839ccb2008-10-30 19:38:48 -07001321{
Ville Syrjäläafe722b2017-09-01 20:12:52 +03001322 void __iomem *vaddr;
Keith Packard0839ccb2008-10-30 19:38:48 -07001323 unsigned long unwritten;
1324
Ben Widawsky4f0c7cf2012-04-16 14:07:47 -07001325 /* We can use the cpu mem copy function because this is X86. */
Ville Syrjäläafe722b2017-09-01 20:12:52 +03001326 vaddr = io_mapping_map_atomic_wc(mapping, base);
1327 unwritten = __copy_from_user_inatomic_nocache((void __force *)vaddr + offset,
Keith Packard0839ccb2008-10-30 19:38:48 -07001328 user_data, length);
Chris Wilsonfe115622016-10-28 13:58:40 +01001329 io_mapping_unmap_atomic(vaddr);
1330 if (unwritten) {
Ville Syrjäläafe722b2017-09-01 20:12:52 +03001331 vaddr = io_mapping_map_wc(mapping, base, PAGE_SIZE);
1332 unwritten = copy_from_user((void __force *)vaddr + offset,
1333 user_data, length);
Chris Wilsonfe115622016-10-28 13:58:40 +01001334 io_mapping_unmap(vaddr);
1335 }
Keith Packard0839ccb2008-10-30 19:38:48 -07001336
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001337 return unwritten;
1338}
1339
Eric Anholt3de09aa2009-03-09 09:42:23 -07001340/**
1341 * This is the fast pwrite path, where we copy the data directly from the
1342 * user into the GTT, uncached.
Chris Wilsonfe115622016-10-28 13:58:40 +01001343 * @obj: i915 GEM object
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01001344 * @args: pwrite arguments structure
Eric Anholt3de09aa2009-03-09 09:42:23 -07001345 */
Eric Anholt673a3942008-07-30 12:06:12 -07001346static int
Chris Wilsonfe115622016-10-28 13:58:40 +01001347i915_gem_gtt_pwrite_fast(struct drm_i915_gem_object *obj,
1348 const struct drm_i915_gem_pwrite *args)
Eric Anholt673a3942008-07-30 12:06:12 -07001349{
Chris Wilsonfe115622016-10-28 13:58:40 +01001350 struct drm_i915_private *i915 = to_i915(obj->base.dev);
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301351 struct i915_ggtt *ggtt = &i915->ggtt;
1352 struct drm_mm_node node;
Chris Wilsonfe115622016-10-28 13:58:40 +01001353 struct i915_vma *vma;
1354 u64 remain, offset;
1355 void __user *user_data;
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301356 int ret;
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301357
Chris Wilsonfe115622016-10-28 13:58:40 +01001358 ret = mutex_lock_interruptible(&i915->drm.struct_mutex);
1359 if (ret)
1360 return ret;
Daniel Vetter935aaa62012-03-25 19:47:35 +02001361
Chris Wilson8bd818152017-10-19 07:37:33 +01001362 if (i915_gem_object_has_struct_page(obj)) {
1363 /*
1364 * Avoid waking the device up if we can fallback, as
1365 * waking/resuming is very slow (worst-case 10-100 ms
1366 * depending on PCI sleeps and our own resume time).
1367 * This easily dwarfs any performance advantage from
1368 * using the cache bypass of indirect GGTT access.
1369 */
1370 if (!intel_runtime_pm_get_if_in_use(i915)) {
1371 ret = -EFAULT;
1372 goto out_unlock;
1373 }
1374 } else {
1375 /* No backing pages, no fallback, we must force GGTT access */
1376 intel_runtime_pm_get(i915);
1377 }
1378
Chris Wilson058d88c2016-08-15 10:49:06 +01001379 vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0,
Chris Wilsona3259ca2017-10-09 09:44:00 +01001380 PIN_MAPPABLE |
1381 PIN_NONFAULT |
1382 PIN_NONBLOCK);
Chris Wilson18034582016-08-18 17:16:45 +01001383 if (!IS_ERR(vma)) {
1384 node.start = i915_ggtt_offset(vma);
1385 node.allocated = false;
Chris Wilson49ef5292016-08-18 17:17:00 +01001386 ret = i915_vma_put_fence(vma);
Chris Wilson18034582016-08-18 17:16:45 +01001387 if (ret) {
1388 i915_vma_unpin(vma);
1389 vma = ERR_PTR(ret);
1390 }
1391 }
Chris Wilson058d88c2016-08-15 10:49:06 +01001392 if (IS_ERR(vma)) {
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001393 ret = insert_mappable_node(ggtt, &node, PAGE_SIZE);
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301394 if (ret)
Chris Wilson8bd818152017-10-19 07:37:33 +01001395 goto out_rpm;
Chris Wilsonfe115622016-10-28 13:58:40 +01001396 GEM_BUG_ON(!node.allocated);
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301397 }
Daniel Vetter935aaa62012-03-25 19:47:35 +02001398
1399 ret = i915_gem_object_set_to_gtt_domain(obj, true);
1400 if (ret)
1401 goto out_unpin;
1402
Chris Wilsonfe115622016-10-28 13:58:40 +01001403 mutex_unlock(&i915->drm.struct_mutex);
1404
Chris Wilsonb19482d2016-08-18 17:16:43 +01001405 intel_fb_obj_invalidate(obj, ORIGIN_CPU);
Paulo Zanoni063e4e62015-02-13 17:23:45 -02001406
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301407 user_data = u64_to_user_ptr(args->data_ptr);
1408 offset = args->offset;
1409 remain = args->size;
1410 while (remain) {
Eric Anholt673a3942008-07-30 12:06:12 -07001411 /* Operation in this page
1412 *
Keith Packard0839ccb2008-10-30 19:38:48 -07001413 * page_base = page offset within aperture
1414 * page_offset = offset within page
1415 * page_length = bytes to copy for this page
Eric Anholt673a3942008-07-30 12:06:12 -07001416 */
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301417 u32 page_base = node.start;
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001418 unsigned int page_offset = offset_in_page(offset);
1419 unsigned int page_length = PAGE_SIZE - page_offset;
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301420 page_length = remain < page_length ? remain : page_length;
1421 if (node.allocated) {
1422 wmb(); /* flush the write before we modify the GGTT */
1423 ggtt->base.insert_page(&ggtt->base,
1424 i915_gem_object_get_dma_address(obj, offset >> PAGE_SHIFT),
1425 node.start, I915_CACHE_NONE, 0);
1426 wmb(); /* flush modifications to the GGTT (insert_page) */
1427 } else {
1428 page_base += offset & PAGE_MASK;
1429 }
Keith Packard0839ccb2008-10-30 19:38:48 -07001430 /* If we get a fault while copying data, then (presumably) our
Eric Anholt3de09aa2009-03-09 09:42:23 -07001431 * source page isn't available. Return the error and we'll
1432 * retry in the slow path.
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301433 * If the object is non-shmem backed, we retry again with the
1434 * path that handles page fault.
Keith Packard0839ccb2008-10-30 19:38:48 -07001435 */
Matthew Auld73ebd502017-12-11 15:18:20 +00001436 if (ggtt_write(&ggtt->iomap, page_base, page_offset,
Chris Wilsonfe115622016-10-28 13:58:40 +01001437 user_data, page_length)) {
1438 ret = -EFAULT;
1439 break;
Daniel Vetter935aaa62012-03-25 19:47:35 +02001440 }
Eric Anholt673a3942008-07-30 12:06:12 -07001441
Keith Packard0839ccb2008-10-30 19:38:48 -07001442 remain -= page_length;
1443 user_data += page_length;
1444 offset += page_length;
Eric Anholt673a3942008-07-30 12:06:12 -07001445 }
Chris Wilsond59b21e2017-02-22 11:40:49 +00001446 intel_fb_obj_flush(obj, ORIGIN_CPU);
Chris Wilsonfe115622016-10-28 13:58:40 +01001447
1448 mutex_lock(&i915->drm.struct_mutex);
Daniel Vetter935aaa62012-03-25 19:47:35 +02001449out_unpin:
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301450 if (node.allocated) {
1451 wmb();
1452 ggtt->base.clear_range(&ggtt->base,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02001453 node.start, node.size);
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301454 remove_mappable_node(&node);
1455 } else {
Chris Wilson058d88c2016-08-15 10:49:06 +01001456 i915_vma_unpin(vma);
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301457 }
Chris Wilson8bd818152017-10-19 07:37:33 +01001458out_rpm:
Chris Wilson9c870d02016-10-24 13:42:15 +01001459 intel_runtime_pm_put(i915);
Chris Wilson8bd818152017-10-19 07:37:33 +01001460out_unlock:
Chris Wilsonfe115622016-10-28 13:58:40 +01001461 mutex_unlock(&i915->drm.struct_mutex);
Eric Anholt3de09aa2009-03-09 09:42:23 -07001462 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07001463}
1464
Eric Anholt673a3942008-07-30 12:06:12 -07001465static int
Chris Wilsonfe115622016-10-28 13:58:40 +01001466shmem_pwrite_slow(struct page *page, int offset, int length,
Daniel Vetterd174bd62012-03-25 19:47:40 +02001467 char __user *user_data,
1468 bool page_do_bit17_swizzling,
1469 bool needs_clflush_before,
1470 bool needs_clflush_after)
Eric Anholt673a3942008-07-30 12:06:12 -07001471{
Daniel Vetterd174bd62012-03-25 19:47:40 +02001472 char *vaddr;
1473 int ret;
Eric Anholt40123c12009-03-09 13:42:30 -07001474
Daniel Vetterd174bd62012-03-25 19:47:40 +02001475 vaddr = kmap(page);
Daniel Vettere7e58eb2012-03-25 19:47:43 +02001476 if (unlikely(needs_clflush_before || page_do_bit17_swizzling))
Chris Wilsonfe115622016-10-28 13:58:40 +01001477 shmem_clflush_swizzled_range(vaddr + offset, length,
Daniel Vetter23c18c72012-03-25 19:47:42 +02001478 page_do_bit17_swizzling);
Daniel Vetterd174bd62012-03-25 19:47:40 +02001479 if (page_do_bit17_swizzling)
Chris Wilsonfe115622016-10-28 13:58:40 +01001480 ret = __copy_from_user_swizzled(vaddr, offset, user_data,
1481 length);
Daniel Vetterd174bd62012-03-25 19:47:40 +02001482 else
Chris Wilsonfe115622016-10-28 13:58:40 +01001483 ret = __copy_from_user(vaddr + offset, user_data, length);
Daniel Vetterd174bd62012-03-25 19:47:40 +02001484 if (needs_clflush_after)
Chris Wilsonfe115622016-10-28 13:58:40 +01001485 shmem_clflush_swizzled_range(vaddr + offset, length,
Daniel Vetter23c18c72012-03-25 19:47:42 +02001486 page_do_bit17_swizzling);
Daniel Vetterd174bd62012-03-25 19:47:40 +02001487 kunmap(page);
Chris Wilsone5281cc2010-10-28 13:45:36 +01001488
Chris Wilson755d2212012-09-04 21:02:55 +01001489 return ret ? -EFAULT : 0;
Eric Anholt40123c12009-03-09 13:42:30 -07001490}
1491
Chris Wilsonfe115622016-10-28 13:58:40 +01001492/* Per-page copy function for the shmem pwrite fastpath.
1493 * Flushes invalid cachelines before writing to the target if
1494 * needs_clflush_before is set and flushes out any written cachelines after
1495 * writing if needs_clflush is set.
1496 */
Eric Anholt40123c12009-03-09 13:42:30 -07001497static int
Chris Wilsonfe115622016-10-28 13:58:40 +01001498shmem_pwrite(struct page *page, int offset, int len, char __user *user_data,
1499 bool page_do_bit17_swizzling,
1500 bool needs_clflush_before,
1501 bool needs_clflush_after)
Eric Anholt40123c12009-03-09 13:42:30 -07001502{
Chris Wilsonfe115622016-10-28 13:58:40 +01001503 int ret;
Eric Anholt40123c12009-03-09 13:42:30 -07001504
Chris Wilsonfe115622016-10-28 13:58:40 +01001505 ret = -ENODEV;
1506 if (!page_do_bit17_swizzling) {
1507 char *vaddr = kmap_atomic(page);
1508
1509 if (needs_clflush_before)
1510 drm_clflush_virt_range(vaddr + offset, len);
1511 ret = __copy_from_user_inatomic(vaddr + offset, user_data, len);
1512 if (needs_clflush_after)
1513 drm_clflush_virt_range(vaddr + offset, len);
1514
1515 kunmap_atomic(vaddr);
1516 }
1517 if (ret == 0)
1518 return ret;
1519
1520 return shmem_pwrite_slow(page, offset, len, user_data,
1521 page_do_bit17_swizzling,
1522 needs_clflush_before,
1523 needs_clflush_after);
1524}
1525
1526static int
1527i915_gem_shmem_pwrite(struct drm_i915_gem_object *obj,
1528 const struct drm_i915_gem_pwrite *args)
1529{
1530 struct drm_i915_private *i915 = to_i915(obj->base.dev);
1531 void __user *user_data;
1532 u64 remain;
1533 unsigned int obj_do_bit17_swizzling;
1534 unsigned int partial_cacheline_write;
1535 unsigned int needs_clflush;
1536 unsigned int offset, idx;
1537 int ret;
1538
1539 ret = mutex_lock_interruptible(&i915->drm.struct_mutex);
Chris Wilson43394c72016-08-18 17:16:47 +01001540 if (ret)
1541 return ret;
Eric Anholt40123c12009-03-09 13:42:30 -07001542
Chris Wilsonfe115622016-10-28 13:58:40 +01001543 ret = i915_gem_obj_prepare_shmem_write(obj, &needs_clflush);
1544 mutex_unlock(&i915->drm.struct_mutex);
1545 if (ret)
1546 return ret;
1547
1548 obj_do_bit17_swizzling = 0;
1549 if (i915_gem_object_needs_bit17_swizzle(obj))
1550 obj_do_bit17_swizzling = BIT(17);
1551
1552 /* If we don't overwrite a cacheline completely we need to be
1553 * careful to have up-to-date data by first clflushing. Don't
1554 * overcomplicate things and flush the entire patch.
1555 */
1556 partial_cacheline_write = 0;
1557 if (needs_clflush & CLFLUSH_BEFORE)
1558 partial_cacheline_write = boot_cpu_data.x86_clflush_size - 1;
1559
Chris Wilson43394c72016-08-18 17:16:47 +01001560 user_data = u64_to_user_ptr(args->data_ptr);
Chris Wilson43394c72016-08-18 17:16:47 +01001561 remain = args->size;
Chris Wilsonfe115622016-10-28 13:58:40 +01001562 offset = offset_in_page(args->offset);
1563 for (idx = args->offset >> PAGE_SHIFT; remain; idx++) {
1564 struct page *page = i915_gem_object_get_page(obj, idx);
1565 int length;
Eric Anholt40123c12009-03-09 13:42:30 -07001566
Chris Wilsonfe115622016-10-28 13:58:40 +01001567 length = remain;
1568 if (offset + length > PAGE_SIZE)
1569 length = PAGE_SIZE - offset;
Chris Wilsone5281cc2010-10-28 13:45:36 +01001570
Chris Wilsonfe115622016-10-28 13:58:40 +01001571 ret = shmem_pwrite(page, offset, length, user_data,
1572 page_to_phys(page) & obj_do_bit17_swizzling,
1573 (offset | length) & partial_cacheline_write,
1574 needs_clflush & CLFLUSH_AFTER);
1575 if (ret)
Chris Wilson9da3da62012-06-01 15:20:22 +01001576 break;
1577
Chris Wilsonfe115622016-10-28 13:58:40 +01001578 remain -= length;
1579 user_data += length;
1580 offset = 0;
Eric Anholt40123c12009-03-09 13:42:30 -07001581 }
1582
Chris Wilsond59b21e2017-02-22 11:40:49 +00001583 intel_fb_obj_flush(obj, ORIGIN_CPU);
Chris Wilsonfe115622016-10-28 13:58:40 +01001584 i915_gem_obj_finish_shmem_access(obj);
Eric Anholt40123c12009-03-09 13:42:30 -07001585 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07001586}
1587
1588/**
1589 * Writes data to the object referenced by handle.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01001590 * @dev: drm device
1591 * @data: ioctl data blob
1592 * @file: drm file
Eric Anholt673a3942008-07-30 12:06:12 -07001593 *
1594 * On error, the contents of the buffer that were to be modified are undefined.
1595 */
1596int
1597i915_gem_pwrite_ioctl(struct drm_device *dev, void *data,
Chris Wilsonfbd5a262010-10-14 15:03:58 +01001598 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001599{
1600 struct drm_i915_gem_pwrite *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00001601 struct drm_i915_gem_object *obj;
Chris Wilson51311d02010-11-17 09:10:42 +00001602 int ret;
1603
1604 if (args->size == 0)
1605 return 0;
1606
1607 if (!access_ok(VERIFY_READ,
Gustavo Padovan3ed605b2016-04-26 12:32:27 -03001608 u64_to_user_ptr(args->data_ptr),
Chris Wilson51311d02010-11-17 09:10:42 +00001609 args->size))
1610 return -EFAULT;
1611
Chris Wilson03ac0642016-07-20 13:31:51 +01001612 obj = i915_gem_object_lookup(file, args->handle);
Chris Wilson258a5ed2016-08-05 10:14:16 +01001613 if (!obj)
1614 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07001615
Chris Wilson7dcd2492010-09-26 20:21:44 +01001616 /* Bounds check destination. */
Matthew Auld966d5bf2016-12-13 20:32:22 +00001617 if (range_overflows_t(u64, args->offset, args->size, obj->base.size)) {
Chris Wilsonce9d4192010-09-26 20:50:05 +01001618 ret = -EINVAL;
Chris Wilson258a5ed2016-08-05 10:14:16 +01001619 goto err;
Chris Wilsonce9d4192010-09-26 20:50:05 +01001620 }
1621
Chris Wilsondb53a302011-02-03 11:57:46 +00001622 trace_i915_gem_object_pwrite(obj, args->offset, args->size);
1623
Chris Wilson7c55e2c2017-03-07 12:03:38 +00001624 ret = -ENODEV;
1625 if (obj->ops->pwrite)
1626 ret = obj->ops->pwrite(obj, args);
1627 if (ret != -ENODEV)
1628 goto err;
1629
Chris Wilsone95433c2016-10-28 13:58:27 +01001630 ret = i915_gem_object_wait(obj,
1631 I915_WAIT_INTERRUPTIBLE |
1632 I915_WAIT_ALL,
1633 MAX_SCHEDULE_TIMEOUT,
1634 to_rps_client(file));
Chris Wilson258a5ed2016-08-05 10:14:16 +01001635 if (ret)
1636 goto err;
1637
Chris Wilsonfe115622016-10-28 13:58:40 +01001638 ret = i915_gem_object_pin_pages(obj);
Chris Wilson258a5ed2016-08-05 10:14:16 +01001639 if (ret)
Chris Wilsonfe115622016-10-28 13:58:40 +01001640 goto err;
Chris Wilson258a5ed2016-08-05 10:14:16 +01001641
Daniel Vetter935aaa62012-03-25 19:47:35 +02001642 ret = -EFAULT;
Eric Anholt673a3942008-07-30 12:06:12 -07001643 /* We can only do the GTT pwrite on untiled buffers, as otherwise
1644 * it would end up going through the fenced access, and we'll get
1645 * different detiling behavior between reading and writing.
1646 * pread/pwrite currently are reading and writing from the CPU
1647 * perspective, requiring manual detiling by the client.
1648 */
Chris Wilson6eae0052016-06-20 15:05:52 +01001649 if (!i915_gem_object_has_struct_page(obj) ||
Chris Wilson9c870d02016-10-24 13:42:15 +01001650 cpu_write_needs_clflush(obj))
Daniel Vetter935aaa62012-03-25 19:47:35 +02001651 /* Note that the gtt paths might fail with non-page-backed user
1652 * pointers (e.g. gtt mappings when moving data between
Chris Wilson9c870d02016-10-24 13:42:15 +01001653 * textures). Fallback to the shmem path in that case.
1654 */
Chris Wilsonfe115622016-10-28 13:58:40 +01001655 ret = i915_gem_gtt_pwrite_fast(obj, args);
Eric Anholt673a3942008-07-30 12:06:12 -07001656
Chris Wilsond1054ee2016-07-16 18:42:36 +01001657 if (ret == -EFAULT || ret == -ENOSPC) {
Chris Wilson6a2c4232014-11-04 04:51:40 -08001658 if (obj->phys_handle)
1659 ret = i915_gem_phys_pwrite(obj, args, file);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301660 else
Chris Wilsonfe115622016-10-28 13:58:40 +01001661 ret = i915_gem_shmem_pwrite(obj, args);
Chris Wilson6a2c4232014-11-04 04:51:40 -08001662 }
Daniel Vetter5c0480f2011-12-14 13:57:30 +01001663
Chris Wilsonfe115622016-10-28 13:58:40 +01001664 i915_gem_object_unpin_pages(obj);
Chris Wilson258a5ed2016-08-05 10:14:16 +01001665err:
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001666 i915_gem_object_put(obj);
Chris Wilson258a5ed2016-08-05 10:14:16 +01001667 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07001668}
1669
Chris Wilson40e62d52016-10-28 13:58:41 +01001670static void i915_gem_object_bump_inactive_ggtt(struct drm_i915_gem_object *obj)
1671{
1672 struct drm_i915_private *i915;
1673 struct list_head *list;
1674 struct i915_vma *vma;
1675
Chris Wilsonf2123812017-10-16 12:40:37 +01001676 GEM_BUG_ON(!i915_gem_object_has_pinned_pages(obj));
1677
Chris Wilsone2189dd2017-12-07 21:14:07 +00001678 for_each_ggtt_vma(vma, obj) {
Chris Wilson40e62d52016-10-28 13:58:41 +01001679 if (i915_vma_is_active(vma))
1680 continue;
1681
1682 if (!drm_mm_node_allocated(&vma->node))
1683 continue;
1684
1685 list_move_tail(&vma->vm_link, &vma->vm->inactive_list);
1686 }
1687
1688 i915 = to_i915(obj->base.dev);
Chris Wilsonf2123812017-10-16 12:40:37 +01001689 spin_lock(&i915->mm.obj_lock);
Chris Wilson40e62d52016-10-28 13:58:41 +01001690 list = obj->bind_count ? &i915->mm.bound_list : &i915->mm.unbound_list;
Chris Wilsonf2123812017-10-16 12:40:37 +01001691 list_move_tail(&obj->mm.link, list);
1692 spin_unlock(&i915->mm.obj_lock);
Chris Wilson40e62d52016-10-28 13:58:41 +01001693}
1694
Eric Anholt673a3942008-07-30 12:06:12 -07001695/**
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001696 * Called when user space prepares to use an object with the CPU, either
1697 * through the mmap ioctl's mapping or a GTT mapping.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01001698 * @dev: drm device
1699 * @data: ioctl data blob
1700 * @file: drm file
Eric Anholt673a3942008-07-30 12:06:12 -07001701 */
1702int
1703i915_gem_set_domain_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00001704 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001705{
1706 struct drm_i915_gem_set_domain *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00001707 struct drm_i915_gem_object *obj;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001708 uint32_t read_domains = args->read_domains;
1709 uint32_t write_domain = args->write_domain;
Chris Wilson40e62d52016-10-28 13:58:41 +01001710 int err;
Eric Anholt673a3942008-07-30 12:06:12 -07001711
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001712 /* Only handle setting domains to types used by the CPU. */
Chris Wilsonb8f90962016-08-05 10:14:07 +01001713 if ((write_domain | read_domains) & I915_GEM_GPU_DOMAINS)
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001714 return -EINVAL;
1715
1716 /* Having something in the write domain implies it's in the read
1717 * domain, and only that read domain. Enforce that in the request.
1718 */
1719 if (write_domain != 0 && read_domains != write_domain)
1720 return -EINVAL;
1721
Chris Wilson03ac0642016-07-20 13:31:51 +01001722 obj = i915_gem_object_lookup(file, args->handle);
Chris Wilsonb8f90962016-08-05 10:14:07 +01001723 if (!obj)
1724 return -ENOENT;
Jesse Barnes652c3932009-08-17 13:31:43 -07001725
Chris Wilson3236f572012-08-24 09:35:09 +01001726 /* Try to flush the object off the GPU without holding the lock.
1727 * We will repeat the flush holding the lock in the normal manner
1728 * to catch cases where we are gazumped.
1729 */
Chris Wilson40e62d52016-10-28 13:58:41 +01001730 err = i915_gem_object_wait(obj,
Chris Wilsone95433c2016-10-28 13:58:27 +01001731 I915_WAIT_INTERRUPTIBLE |
1732 (write_domain ? I915_WAIT_ALL : 0),
1733 MAX_SCHEDULE_TIMEOUT,
1734 to_rps_client(file));
Chris Wilson40e62d52016-10-28 13:58:41 +01001735 if (err)
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001736 goto out;
Chris Wilsonb8f90962016-08-05 10:14:07 +01001737
Tina Zhanga03f3952017-11-14 10:25:13 +00001738 /*
1739 * Proxy objects do not control access to the backing storage, ergo
1740 * they cannot be used as a means to manipulate the cache domain
1741 * tracking for that backing storage. The proxy object is always
1742 * considered to be outside of any cache domain.
1743 */
1744 if (i915_gem_object_is_proxy(obj)) {
1745 err = -ENXIO;
1746 goto out;
1747 }
1748
1749 /*
1750 * Flush and acquire obj->pages so that we are coherent through
Chris Wilson40e62d52016-10-28 13:58:41 +01001751 * direct access in memory with previous cached writes through
1752 * shmemfs and that our cache domain tracking remains valid.
1753 * For example, if the obj->filp was moved to swap without us
1754 * being notified and releasing the pages, we would mistakenly
1755 * continue to assume that the obj remained out of the CPU cached
1756 * domain.
1757 */
1758 err = i915_gem_object_pin_pages(obj);
1759 if (err)
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001760 goto out;
Chris Wilson40e62d52016-10-28 13:58:41 +01001761
1762 err = i915_mutex_lock_interruptible(dev);
1763 if (err)
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001764 goto out_unpin;
Chris Wilson3236f572012-08-24 09:35:09 +01001765
Chris Wilsone22d8e32017-04-12 12:01:11 +01001766 if (read_domains & I915_GEM_DOMAIN_WC)
1767 err = i915_gem_object_set_to_wc_domain(obj, write_domain);
1768 else if (read_domains & I915_GEM_DOMAIN_GTT)
1769 err = i915_gem_object_set_to_gtt_domain(obj, write_domain);
Chris Wilson43566de2015-01-02 16:29:29 +05301770 else
Chris Wilsone22d8e32017-04-12 12:01:11 +01001771 err = i915_gem_object_set_to_cpu_domain(obj, write_domain);
Chris Wilson40e62d52016-10-28 13:58:41 +01001772
1773 /* And bump the LRU for this access */
1774 i915_gem_object_bump_inactive_ggtt(obj);
1775
1776 mutex_unlock(&dev->struct_mutex);
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001777
Daniel Vetter031b6982015-06-26 19:35:16 +02001778 if (write_domain != 0)
Chris Wilsonef749212017-04-12 12:01:10 +01001779 intel_fb_obj_invalidate(obj,
1780 fb_write_origin(obj, write_domain));
Daniel Vetter031b6982015-06-26 19:35:16 +02001781
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001782out_unpin:
Chris Wilson40e62d52016-10-28 13:58:41 +01001783 i915_gem_object_unpin_pages(obj);
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001784out:
1785 i915_gem_object_put(obj);
Chris Wilson40e62d52016-10-28 13:58:41 +01001786 return err;
Eric Anholt673a3942008-07-30 12:06:12 -07001787}
1788
1789/**
1790 * Called when user space has done writes to this buffer
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01001791 * @dev: drm device
1792 * @data: ioctl data blob
1793 * @file: drm file
Eric Anholt673a3942008-07-30 12:06:12 -07001794 */
1795int
1796i915_gem_sw_finish_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00001797 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001798{
1799 struct drm_i915_gem_sw_finish *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00001800 struct drm_i915_gem_object *obj;
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001801
Chris Wilson03ac0642016-07-20 13:31:51 +01001802 obj = i915_gem_object_lookup(file, args->handle);
Chris Wilsonc21724c2016-08-05 10:14:19 +01001803 if (!obj)
1804 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07001805
Tina Zhanga03f3952017-11-14 10:25:13 +00001806 /*
1807 * Proxy objects are barred from CPU access, so there is no
1808 * need to ban sw_finish as it is a nop.
1809 */
1810
Eric Anholt673a3942008-07-30 12:06:12 -07001811 /* Pinned buffers may be scanout, so flush the cache */
Chris Wilson5a97bcc2017-02-22 11:40:46 +00001812 i915_gem_object_flush_if_display(obj);
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001813 i915_gem_object_put(obj);
Chris Wilson5a97bcc2017-02-22 11:40:46 +00001814
1815 return 0;
Eric Anholt673a3942008-07-30 12:06:12 -07001816}
1817
1818/**
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01001819 * i915_gem_mmap_ioctl - Maps the contents of an object, returning the address
1820 * it is mapped to.
1821 * @dev: drm device
1822 * @data: ioctl data blob
1823 * @file: drm file
Eric Anholt673a3942008-07-30 12:06:12 -07001824 *
1825 * While the mapping holds a reference on the contents of the object, it doesn't
1826 * imply a ref on the object itself.
Daniel Vetter34367382014-10-16 12:28:18 +02001827 *
1828 * IMPORTANT:
1829 *
1830 * DRM driver writers who look a this function as an example for how to do GEM
1831 * mmap support, please don't implement mmap support like here. The modern way
1832 * to implement DRM mmap support is with an mmap offset ioctl (like
1833 * i915_gem_mmap_gtt) and then using the mmap syscall on the DRM fd directly.
1834 * That way debug tooling like valgrind will understand what's going on, hiding
1835 * the mmap call in a driver private ioctl will break that. The i915 driver only
1836 * does cpu mmaps this way because we didn't know better.
Eric Anholt673a3942008-07-30 12:06:12 -07001837 */
1838int
1839i915_gem_mmap_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00001840 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001841{
1842 struct drm_i915_gem_mmap *args = data;
Chris Wilson03ac0642016-07-20 13:31:51 +01001843 struct drm_i915_gem_object *obj;
Eric Anholt673a3942008-07-30 12:06:12 -07001844 unsigned long addr;
1845
Akash Goel1816f922015-01-02 16:29:30 +05301846 if (args->flags & ~(I915_MMAP_WC))
1847 return -EINVAL;
1848
Borislav Petkov568a58e2016-03-29 17:42:01 +02001849 if (args->flags & I915_MMAP_WC && !boot_cpu_has(X86_FEATURE_PAT))
Akash Goel1816f922015-01-02 16:29:30 +05301850 return -ENODEV;
1851
Chris Wilson03ac0642016-07-20 13:31:51 +01001852 obj = i915_gem_object_lookup(file, args->handle);
1853 if (!obj)
Chris Wilsonbf79cb92010-08-04 14:19:46 +01001854 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07001855
Daniel Vetter1286ff72012-05-10 15:25:09 +02001856 /* prime objects have no backing filp to GEM mmap
1857 * pages from.
1858 */
Chris Wilson03ac0642016-07-20 13:31:51 +01001859 if (!obj->base.filp) {
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001860 i915_gem_object_put(obj);
Tina Zhang274b2462017-11-14 10:25:12 +00001861 return -ENXIO;
Daniel Vetter1286ff72012-05-10 15:25:09 +02001862 }
1863
Chris Wilson03ac0642016-07-20 13:31:51 +01001864 addr = vm_mmap(obj->base.filp, 0, args->size,
Eric Anholt673a3942008-07-30 12:06:12 -07001865 PROT_READ | PROT_WRITE, MAP_SHARED,
1866 args->offset);
Akash Goel1816f922015-01-02 16:29:30 +05301867 if (args->flags & I915_MMAP_WC) {
1868 struct mm_struct *mm = current->mm;
1869 struct vm_area_struct *vma;
1870
Michal Hocko80a89a52016-05-23 16:26:11 -07001871 if (down_write_killable(&mm->mmap_sem)) {
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001872 i915_gem_object_put(obj);
Michal Hocko80a89a52016-05-23 16:26:11 -07001873 return -EINTR;
1874 }
Akash Goel1816f922015-01-02 16:29:30 +05301875 vma = find_vma(mm, addr);
1876 if (vma)
1877 vma->vm_page_prot =
1878 pgprot_writecombine(vm_get_page_prot(vma->vm_flags));
1879 else
1880 addr = -ENOMEM;
1881 up_write(&mm->mmap_sem);
Chris Wilsonaeecc962016-06-17 14:46:39 -03001882
1883 /* This may race, but that's ok, it only gets set */
Chris Wilson50349242016-08-18 17:17:04 +01001884 WRITE_ONCE(obj->frontbuffer_ggtt_origin, ORIGIN_CPU);
Akash Goel1816f922015-01-02 16:29:30 +05301885 }
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001886 i915_gem_object_put(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001887 if (IS_ERR((void *)addr))
1888 return addr;
1889
1890 args->addr_ptr = (uint64_t) addr;
1891
1892 return 0;
1893}
1894
Chris Wilson03af84f2016-08-18 17:17:01 +01001895static unsigned int tile_row_pages(struct drm_i915_gem_object *obj)
1896{
Chris Wilson6649a0b2017-01-09 16:16:08 +00001897 return i915_gem_object_get_tile_row_size(obj) >> PAGE_SHIFT;
Chris Wilson03af84f2016-08-18 17:17:01 +01001898}
1899
Jesse Barnesde151cf2008-11-12 10:03:55 -08001900/**
Chris Wilson4cc69072016-08-25 19:05:19 +01001901 * i915_gem_mmap_gtt_version - report the current feature set for GTT mmaps
1902 *
1903 * A history of the GTT mmap interface:
1904 *
1905 * 0 - Everything had to fit into the GTT. Both parties of a memcpy had to
1906 * aligned and suitable for fencing, and still fit into the available
1907 * mappable space left by the pinned display objects. A classic problem
1908 * we called the page-fault-of-doom where we would ping-pong between
1909 * two objects that could not fit inside the GTT and so the memcpy
1910 * would page one object in at the expense of the other between every
1911 * single byte.
1912 *
1913 * 1 - Objects can be any size, and have any compatible fencing (X Y, or none
1914 * as set via i915_gem_set_tiling() [DRM_I915_GEM_SET_TILING]). If the
1915 * object is too large for the available space (or simply too large
1916 * for the mappable aperture!), a view is created instead and faulted
1917 * into userspace. (This view is aligned and sized appropriately for
1918 * fenced access.)
1919 *
Chris Wilsone22d8e32017-04-12 12:01:11 +01001920 * 2 - Recognise WC as a separate cache domain so that we can flush the
1921 * delayed writes via GTT before performing direct access via WC.
1922 *
Chris Wilson4cc69072016-08-25 19:05:19 +01001923 * Restrictions:
1924 *
1925 * * snoopable objects cannot be accessed via the GTT. It can cause machine
1926 * hangs on some architectures, corruption on others. An attempt to service
1927 * a GTT page fault from a snoopable object will generate a SIGBUS.
1928 *
1929 * * the object must be able to fit into RAM (physical memory, though no
1930 * limited to the mappable aperture).
1931 *
1932 *
1933 * Caveats:
1934 *
1935 * * a new GTT page fault will synchronize rendering from the GPU and flush
1936 * all data to system memory. Subsequent access will not be synchronized.
1937 *
1938 * * all mappings are revoked on runtime device suspend.
1939 *
1940 * * there are only 8, 16 or 32 fence registers to share between all users
1941 * (older machines require fence register for display and blitter access
1942 * as well). Contention of the fence registers will cause the previous users
1943 * to be unmapped and any new access will generate new page faults.
1944 *
1945 * * running out of memory while servicing a fault may generate a SIGBUS,
1946 * rather than the expected SIGSEGV.
1947 */
1948int i915_gem_mmap_gtt_version(void)
1949{
Chris Wilsone22d8e32017-04-12 12:01:11 +01001950 return 2;
Chris Wilson4cc69072016-08-25 19:05:19 +01001951}
1952
Chris Wilson2d4281b2017-01-10 09:56:32 +00001953static inline struct i915_ggtt_view
1954compute_partial_view(struct drm_i915_gem_object *obj,
Chris Wilson2d4281b2017-01-10 09:56:32 +00001955 pgoff_t page_offset,
1956 unsigned int chunk)
1957{
1958 struct i915_ggtt_view view;
1959
1960 if (i915_gem_object_is_tiled(obj))
1961 chunk = roundup(chunk, tile_row_pages(obj));
1962
Chris Wilson2d4281b2017-01-10 09:56:32 +00001963 view.type = I915_GGTT_VIEW_PARTIAL;
Chris Wilson8bab11932017-01-14 00:28:25 +00001964 view.partial.offset = rounddown(page_offset, chunk);
1965 view.partial.size =
Chris Wilson2d4281b2017-01-10 09:56:32 +00001966 min_t(unsigned int, chunk,
Chris Wilson8bab11932017-01-14 00:28:25 +00001967 (obj->base.size >> PAGE_SHIFT) - view.partial.offset);
Chris Wilson2d4281b2017-01-10 09:56:32 +00001968
1969 /* If the partial covers the entire object, just create a normal VMA. */
1970 if (chunk >= obj->base.size >> PAGE_SHIFT)
1971 view.type = I915_GGTT_VIEW_NORMAL;
1972
1973 return view;
1974}
1975
Chris Wilson4cc69072016-08-25 19:05:19 +01001976/**
Jesse Barnesde151cf2008-11-12 10:03:55 -08001977 * i915_gem_fault - fault a page into the GTT
Geliang Tangd9072a32015-09-15 05:58:44 -07001978 * @vmf: fault info
Jesse Barnesde151cf2008-11-12 10:03:55 -08001979 *
1980 * The fault handler is set up by drm_gem_mmap() when a object is GTT mapped
1981 * from userspace. The fault handler takes care of binding the object to
1982 * the GTT (if needed), allocating and programming a fence register (again,
1983 * only if needed based on whether the old reg is still valid or the object
1984 * is tiled) and inserting a new PTE into the faulting process.
1985 *
1986 * Note that the faulting process may involve evicting existing objects
1987 * from the GTT and/or fence registers to make room. So performance may
1988 * suffer if the GTT working set is large or there are few fence registers
1989 * left.
Chris Wilson4cc69072016-08-25 19:05:19 +01001990 *
1991 * The current feature set supported by i915_gem_fault() and thus GTT mmaps
1992 * is exposed via I915_PARAM_MMAP_GTT_VERSION (see i915_gem_mmap_gtt_version).
Jesse Barnesde151cf2008-11-12 10:03:55 -08001993 */
Dave Jiang11bac802017-02-24 14:56:41 -08001994int i915_gem_fault(struct vm_fault *vmf)
Jesse Barnesde151cf2008-11-12 10:03:55 -08001995{
Chris Wilson03af84f2016-08-18 17:17:01 +01001996#define MIN_CHUNK_PAGES ((1 << 20) >> PAGE_SHIFT) /* 1 MiB */
Dave Jiang11bac802017-02-24 14:56:41 -08001997 struct vm_area_struct *area = vmf->vma;
Chris Wilson058d88c2016-08-15 10:49:06 +01001998 struct drm_i915_gem_object *obj = to_intel_bo(area->vm_private_data);
Chris Wilson05394f32010-11-08 19:18:58 +00001999 struct drm_device *dev = obj->base.dev;
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002000 struct drm_i915_private *dev_priv = to_i915(dev);
2001 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Chris Wilsonb8f90962016-08-05 10:14:07 +01002002 bool write = !!(vmf->flags & FAULT_FLAG_WRITE);
Chris Wilson058d88c2016-08-15 10:49:06 +01002003 struct i915_vma *vma;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002004 pgoff_t page_offset;
Chris Wilson82118872016-08-18 17:17:05 +01002005 unsigned int flags;
Chris Wilsonb8f90962016-08-05 10:14:07 +01002006 int ret;
Paulo Zanonif65c9162013-11-27 18:20:34 -02002007
Jesse Barnesde151cf2008-11-12 10:03:55 -08002008 /* We don't use vmf->pgoff since that has the fake offset */
Jan Kara1a29d852016-12-14 15:07:01 -08002009 page_offset = (vmf->address - area->vm_start) >> PAGE_SHIFT;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002010
Chris Wilsondb53a302011-02-03 11:57:46 +00002011 trace_i915_gem_object_fault(obj, page_offset, true, write);
2012
Chris Wilson6e4930f2014-02-07 18:37:06 -02002013 /* Try to flush the object off the GPU first without holding the lock.
Chris Wilsonb8f90962016-08-05 10:14:07 +01002014 * Upon acquiring the lock, we will perform our sanity checks and then
Chris Wilson6e4930f2014-02-07 18:37:06 -02002015 * repeat the flush holding the lock in the normal manner to catch cases
2016 * where we are gazumped.
2017 */
Chris Wilsone95433c2016-10-28 13:58:27 +01002018 ret = i915_gem_object_wait(obj,
2019 I915_WAIT_INTERRUPTIBLE,
2020 MAX_SCHEDULE_TIMEOUT,
2021 NULL);
Chris Wilson6e4930f2014-02-07 18:37:06 -02002022 if (ret)
Chris Wilsonb8f90962016-08-05 10:14:07 +01002023 goto err;
2024
Chris Wilson40e62d52016-10-28 13:58:41 +01002025 ret = i915_gem_object_pin_pages(obj);
2026 if (ret)
2027 goto err;
2028
Chris Wilsonb8f90962016-08-05 10:14:07 +01002029 intel_runtime_pm_get(dev_priv);
2030
2031 ret = i915_mutex_lock_interruptible(dev);
2032 if (ret)
2033 goto err_rpm;
Chris Wilson6e4930f2014-02-07 18:37:06 -02002034
Chris Wilsoneb119bd2012-12-16 12:43:36 +00002035 /* Access to snoopable pages through the GTT is incoherent. */
Tvrtko Ursulin0031fb92016-11-04 14:42:44 +00002036 if (obj->cache_level != I915_CACHE_NONE && !HAS_LLC(dev_priv)) {
Chris Wilsonddeff6e2014-05-28 16:16:41 +01002037 ret = -EFAULT;
Chris Wilsonb8f90962016-08-05 10:14:07 +01002038 goto err_unlock;
Chris Wilsoneb119bd2012-12-16 12:43:36 +00002039 }
2040
Chris Wilson82118872016-08-18 17:17:05 +01002041 /* If the object is smaller than a couple of partial vma, it is
2042 * not worth only creating a single partial vma - we may as well
2043 * clear enough space for the full object.
2044 */
2045 flags = PIN_MAPPABLE;
2046 if (obj->base.size > 2 * MIN_CHUNK_PAGES << PAGE_SHIFT)
2047 flags |= PIN_NONBLOCK | PIN_NONFAULT;
2048
Chris Wilsona61007a2016-08-18 17:17:02 +01002049 /* Now pin it into the GTT as needed */
Chris Wilson82118872016-08-18 17:17:05 +01002050 vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0, flags);
Chris Wilsona61007a2016-08-18 17:17:02 +01002051 if (IS_ERR(vma)) {
Chris Wilsona61007a2016-08-18 17:17:02 +01002052 /* Use a partial view if it is bigger than available space */
Chris Wilson2d4281b2017-01-10 09:56:32 +00002053 struct i915_ggtt_view view =
Chris Wilson8201c1f2017-01-10 09:56:33 +00002054 compute_partial_view(obj, page_offset, MIN_CHUNK_PAGES);
Chris Wilsonaa136d92016-08-18 17:17:03 +01002055
Chris Wilson50349242016-08-18 17:17:04 +01002056 /* Userspace is now writing through an untracked VMA, abandon
2057 * all hope that the hardware is able to track future writes.
2058 */
2059 obj->frontbuffer_ggtt_origin = ORIGIN_CPU;
2060
Chris Wilsona61007a2016-08-18 17:17:02 +01002061 vma = i915_gem_object_ggtt_pin(obj, &view, 0, 0, PIN_MAPPABLE);
2062 }
Chris Wilson058d88c2016-08-15 10:49:06 +01002063 if (IS_ERR(vma)) {
2064 ret = PTR_ERR(vma);
Chris Wilsonb8f90962016-08-05 10:14:07 +01002065 goto err_unlock;
Chris Wilson058d88c2016-08-15 10:49:06 +01002066 }
Jesse Barnesde151cf2008-11-12 10:03:55 -08002067
Chris Wilsonc9839302012-11-20 10:45:17 +00002068 ret = i915_gem_object_set_to_gtt_domain(obj, write);
2069 if (ret)
Chris Wilsonb8f90962016-08-05 10:14:07 +01002070 goto err_unpin;
Chris Wilsonc9839302012-11-20 10:45:17 +00002071
Chris Wilson3bd40732017-10-09 09:43:56 +01002072 ret = i915_vma_pin_fence(vma);
Chris Wilsonc9839302012-11-20 10:45:17 +00002073 if (ret)
Chris Wilsonb8f90962016-08-05 10:14:07 +01002074 goto err_unpin;
Chris Wilson7d1c4802010-08-07 21:45:03 +01002075
Chris Wilsonb90b91d2014-06-10 12:14:40 +01002076 /* Finally, remap it using the new GTT offset */
Chris Wilsonc58305a2016-08-19 16:54:28 +01002077 ret = remap_io_mapping(area,
Chris Wilson8bab11932017-01-14 00:28:25 +00002078 area->vm_start + (vma->ggtt_view.partial.offset << PAGE_SHIFT),
Matthew Auld73ebd502017-12-11 15:18:20 +00002079 (ggtt->gmadr.start + vma->node.start) >> PAGE_SHIFT,
Chris Wilsonc58305a2016-08-19 16:54:28 +01002080 min_t(u64, vma->size, area->vm_end - area->vm_start),
Matthew Auld73ebd502017-12-11 15:18:20 +00002081 &ggtt->iomap);
Chris Wilsona65adaf2017-10-09 09:43:57 +01002082 if (ret)
2083 goto err_fence;
Chris Wilsona61007a2016-08-18 17:17:02 +01002084
Chris Wilsona65adaf2017-10-09 09:43:57 +01002085 /* Mark as being mmapped into userspace for later revocation */
2086 assert_rpm_wakelock_held(dev_priv);
2087 if (!i915_vma_set_userfault(vma) && !obj->userfault_count++)
2088 list_add(&obj->userfault_link, &dev_priv->mm.userfault_list);
2089 GEM_BUG_ON(!obj->userfault_count);
2090
Chris Wilson7125397b2017-12-06 12:49:14 +00002091 i915_vma_set_ggtt_write(vma);
2092
Chris Wilsona65adaf2017-10-09 09:43:57 +01002093err_fence:
Chris Wilson3bd40732017-10-09 09:43:56 +01002094 i915_vma_unpin_fence(vma);
Chris Wilsonb8f90962016-08-05 10:14:07 +01002095err_unpin:
Chris Wilson058d88c2016-08-15 10:49:06 +01002096 __i915_vma_unpin(vma);
Chris Wilsonb8f90962016-08-05 10:14:07 +01002097err_unlock:
Jesse Barnesde151cf2008-11-12 10:03:55 -08002098 mutex_unlock(&dev->struct_mutex);
Chris Wilsonb8f90962016-08-05 10:14:07 +01002099err_rpm:
2100 intel_runtime_pm_put(dev_priv);
Chris Wilson40e62d52016-10-28 13:58:41 +01002101 i915_gem_object_unpin_pages(obj);
Chris Wilsonb8f90962016-08-05 10:14:07 +01002102err:
Jesse Barnesde151cf2008-11-12 10:03:55 -08002103 switch (ret) {
Chris Wilsond9bc7e92011-02-07 13:09:31 +00002104 case -EIO:
Daniel Vetter2232f032014-09-04 09:36:18 +02002105 /*
2106 * We eat errors when the gpu is terminally wedged to avoid
2107 * userspace unduly crashing (gl has no provisions for mmaps to
2108 * fail). But any other -EIO isn't ours (e.g. swap in failure)
2109 * and so needs to be reported.
2110 */
2111 if (!i915_terminally_wedged(&dev_priv->gpu_error)) {
Paulo Zanonif65c9162013-11-27 18:20:34 -02002112 ret = VM_FAULT_SIGBUS;
2113 break;
2114 }
Chris Wilson045e7692010-11-07 09:18:22 +00002115 case -EAGAIN:
Daniel Vetter571c6082013-09-12 17:57:28 +02002116 /*
2117 * EAGAIN means the gpu is hung and we'll wait for the error
2118 * handler to reset everything when re-faulting in
2119 * i915_mutex_lock_interruptible.
Chris Wilsond9bc7e92011-02-07 13:09:31 +00002120 */
Chris Wilsonc7150892009-09-23 00:43:56 +01002121 case 0:
2122 case -ERESTARTSYS:
Chris Wilsonbed636a2011-02-11 20:31:19 +00002123 case -EINTR:
Dmitry Rogozhkine79e0fe2012-10-03 17:15:26 +03002124 case -EBUSY:
2125 /*
2126 * EBUSY is ok: this just means that another thread
2127 * already did the job.
2128 */
Paulo Zanonif65c9162013-11-27 18:20:34 -02002129 ret = VM_FAULT_NOPAGE;
2130 break;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002131 case -ENOMEM:
Paulo Zanonif65c9162013-11-27 18:20:34 -02002132 ret = VM_FAULT_OOM;
2133 break;
Daniel Vettera7c2e1a2012-10-17 11:17:16 +02002134 case -ENOSPC:
Chris Wilson45d67812014-01-31 11:34:57 +00002135 case -EFAULT:
Paulo Zanonif65c9162013-11-27 18:20:34 -02002136 ret = VM_FAULT_SIGBUS;
2137 break;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002138 default:
Daniel Vettera7c2e1a2012-10-17 11:17:16 +02002139 WARN_ONCE(ret, "unhandled error in i915_gem_fault: %i\n", ret);
Paulo Zanonif65c9162013-11-27 18:20:34 -02002140 ret = VM_FAULT_SIGBUS;
2141 break;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002142 }
Paulo Zanonif65c9162013-11-27 18:20:34 -02002143 return ret;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002144}
2145
Chris Wilsona65adaf2017-10-09 09:43:57 +01002146static void __i915_gem_object_release_mmap(struct drm_i915_gem_object *obj)
2147{
2148 struct i915_vma *vma;
2149
2150 GEM_BUG_ON(!obj->userfault_count);
2151
2152 obj->userfault_count = 0;
2153 list_del(&obj->userfault_link);
2154 drm_vma_node_unmap(&obj->base.vma_node,
2155 obj->base.dev->anon_inode->i_mapping);
2156
Chris Wilsone2189dd2017-12-07 21:14:07 +00002157 for_each_ggtt_vma(vma, obj)
Chris Wilsona65adaf2017-10-09 09:43:57 +01002158 i915_vma_unset_userfault(vma);
Chris Wilsona65adaf2017-10-09 09:43:57 +01002159}
2160
Jesse Barnesde151cf2008-11-12 10:03:55 -08002161/**
Chris Wilson901782b2009-07-10 08:18:50 +01002162 * i915_gem_release_mmap - remove physical page mappings
2163 * @obj: obj in question
2164 *
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02002165 * Preserve the reservation of the mmapping with the DRM core code, but
Chris Wilson901782b2009-07-10 08:18:50 +01002166 * relinquish ownership of the pages back to the system.
2167 *
2168 * It is vital that we remove the page mapping if we have mapped a tiled
2169 * object through the GTT and then lose the fence register due to
2170 * resource pressure. Similarly if the object has been moved out of the
2171 * aperture, than pages mapped into userspace must be revoked. Removing the
2172 * mapping will then trigger a page fault on the next user access, allowing
2173 * fixup by i915_gem_fault().
2174 */
Eric Anholtd05ca302009-07-10 13:02:26 -07002175void
Chris Wilson05394f32010-11-08 19:18:58 +00002176i915_gem_release_mmap(struct drm_i915_gem_object *obj)
Chris Wilson901782b2009-07-10 08:18:50 +01002177{
Chris Wilson275f0392016-10-24 13:42:14 +01002178 struct drm_i915_private *i915 = to_i915(obj->base.dev);
Chris Wilson275f0392016-10-24 13:42:14 +01002179
Chris Wilson349f2cc2016-04-13 17:35:12 +01002180 /* Serialisation between user GTT access and our code depends upon
2181 * revoking the CPU's PTE whilst the mutex is held. The next user
2182 * pagefault then has to wait until we release the mutex.
Chris Wilson9c870d02016-10-24 13:42:15 +01002183 *
2184 * Note that RPM complicates somewhat by adding an additional
2185 * requirement that operations to the GGTT be made holding the RPM
2186 * wakeref.
Chris Wilson349f2cc2016-04-13 17:35:12 +01002187 */
Chris Wilson275f0392016-10-24 13:42:14 +01002188 lockdep_assert_held(&i915->drm.struct_mutex);
Chris Wilson9c870d02016-10-24 13:42:15 +01002189 intel_runtime_pm_get(i915);
Chris Wilson349f2cc2016-04-13 17:35:12 +01002190
Chris Wilsona65adaf2017-10-09 09:43:57 +01002191 if (!obj->userfault_count)
Chris Wilson9c870d02016-10-24 13:42:15 +01002192 goto out;
Chris Wilson901782b2009-07-10 08:18:50 +01002193
Chris Wilsona65adaf2017-10-09 09:43:57 +01002194 __i915_gem_object_release_mmap(obj);
Chris Wilson349f2cc2016-04-13 17:35:12 +01002195
2196 /* Ensure that the CPU's PTE are revoked and there are not outstanding
2197 * memory transactions from userspace before we return. The TLB
2198 * flushing implied above by changing the PTE above *should* be
2199 * sufficient, an extra barrier here just provides us with a bit
2200 * of paranoid documentation about our requirement to serialise
2201 * memory writes before touching registers / GSM.
2202 */
2203 wmb();
Chris Wilson9c870d02016-10-24 13:42:15 +01002204
2205out:
2206 intel_runtime_pm_put(i915);
Chris Wilson901782b2009-07-10 08:18:50 +01002207}
2208
Chris Wilson7c108fd2016-10-24 13:42:18 +01002209void i915_gem_runtime_suspend(struct drm_i915_private *dev_priv)
Chris Wilsoneedd10f2014-06-16 08:57:44 +01002210{
Chris Wilson3594a3e2016-10-24 13:42:16 +01002211 struct drm_i915_gem_object *obj, *on;
Chris Wilson7c108fd2016-10-24 13:42:18 +01002212 int i;
Chris Wilsoneedd10f2014-06-16 08:57:44 +01002213
Chris Wilson3594a3e2016-10-24 13:42:16 +01002214 /*
2215 * Only called during RPM suspend. All users of the userfault_list
2216 * must be holding an RPM wakeref to ensure that this can not
2217 * run concurrently with themselves (and use the struct_mutex for
2218 * protection between themselves).
2219 */
2220
2221 list_for_each_entry_safe(obj, on,
Chris Wilsona65adaf2017-10-09 09:43:57 +01002222 &dev_priv->mm.userfault_list, userfault_link)
2223 __i915_gem_object_release_mmap(obj);
Chris Wilson7c108fd2016-10-24 13:42:18 +01002224
2225 /* The fence will be lost when the device powers down. If any were
2226 * in use by hardware (i.e. they are pinned), we should not be powering
2227 * down! All other fences will be reacquired by the user upon waking.
2228 */
2229 for (i = 0; i < dev_priv->num_fence_regs; i++) {
2230 struct drm_i915_fence_reg *reg = &dev_priv->fence_regs[i];
2231
Chris Wilsone0ec3ec2017-02-03 12:57:17 +00002232 /* Ideally we want to assert that the fence register is not
2233 * live at this point (i.e. that no piece of code will be
2234 * trying to write through fence + GTT, as that both violates
2235 * our tracking of activity and associated locking/barriers,
2236 * but also is illegal given that the hw is powered down).
2237 *
2238 * Previously we used reg->pin_count as a "liveness" indicator.
2239 * That is not sufficient, and we need a more fine-grained
2240 * tool if we want to have a sanity check here.
2241 */
Chris Wilson7c108fd2016-10-24 13:42:18 +01002242
2243 if (!reg->vma)
2244 continue;
2245
Chris Wilsona65adaf2017-10-09 09:43:57 +01002246 GEM_BUG_ON(i915_vma_has_userfault(reg->vma));
Chris Wilson7c108fd2016-10-24 13:42:18 +01002247 reg->dirty = true;
2248 }
Chris Wilsoneedd10f2014-06-16 08:57:44 +01002249}
2250
Chris Wilsond8cb5082012-08-11 15:41:03 +01002251static int i915_gem_object_create_mmap_offset(struct drm_i915_gem_object *obj)
2252{
Chris Wilsonfac5e232016-07-04 11:34:36 +01002253 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
Chris Wilsonf3f61842016-08-05 10:14:14 +01002254 int err;
Chris Wilsond8cb5082012-08-11 15:41:03 +01002255
Chris Wilsonf3f61842016-08-05 10:14:14 +01002256 err = drm_gem_create_mmap_offset(&obj->base);
Chris Wilsonb42a13d2017-01-06 15:22:40 +00002257 if (likely(!err))
Chris Wilsonf3f61842016-08-05 10:14:14 +01002258 return 0;
Daniel Vetterda494d72012-12-20 15:11:16 +01002259
Chris Wilsonb42a13d2017-01-06 15:22:40 +00002260 /* Attempt to reap some mmap space from dead objects */
2261 do {
2262 err = i915_gem_wait_for_idle(dev_priv, I915_WAIT_INTERRUPTIBLE);
2263 if (err)
2264 break;
Chris Wilsond8cb5082012-08-11 15:41:03 +01002265
Chris Wilsonb42a13d2017-01-06 15:22:40 +00002266 i915_gem_drain_freed_objects(dev_priv);
Chris Wilsonf3f61842016-08-05 10:14:14 +01002267 err = drm_gem_create_mmap_offset(&obj->base);
Chris Wilsonb42a13d2017-01-06 15:22:40 +00002268 if (!err)
2269 break;
2270
2271 } while (flush_delayed_work(&dev_priv->gt.retire_work));
Daniel Vetterda494d72012-12-20 15:11:16 +01002272
Chris Wilsonf3f61842016-08-05 10:14:14 +01002273 return err;
Chris Wilsond8cb5082012-08-11 15:41:03 +01002274}
2275
2276static void i915_gem_object_free_mmap_offset(struct drm_i915_gem_object *obj)
2277{
Chris Wilsond8cb5082012-08-11 15:41:03 +01002278 drm_gem_free_mmap_offset(&obj->base);
2279}
2280
Dave Airlieda6b51d2014-12-24 13:11:17 +10002281int
Dave Airlieff72145b2011-02-07 12:16:14 +10002282i915_gem_mmap_gtt(struct drm_file *file,
2283 struct drm_device *dev,
Dave Airlieda6b51d2014-12-24 13:11:17 +10002284 uint32_t handle,
Dave Airlieff72145b2011-02-07 12:16:14 +10002285 uint64_t *offset)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002286{
Chris Wilson05394f32010-11-08 19:18:58 +00002287 struct drm_i915_gem_object *obj;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002288 int ret;
2289
Chris Wilson03ac0642016-07-20 13:31:51 +01002290 obj = i915_gem_object_lookup(file, handle);
Chris Wilsonf3f61842016-08-05 10:14:14 +01002291 if (!obj)
2292 return -ENOENT;
Chris Wilsonab182822009-09-22 18:46:17 +01002293
Chris Wilsond8cb5082012-08-11 15:41:03 +01002294 ret = i915_gem_object_create_mmap_offset(obj);
Chris Wilsonf3f61842016-08-05 10:14:14 +01002295 if (ret == 0)
2296 *offset = drm_vma_node_offset_addr(&obj->base.vma_node);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002297
Chris Wilsonf0cd5182016-10-28 13:58:43 +01002298 i915_gem_object_put(obj);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01002299 return ret;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002300}
2301
Dave Airlieff72145b2011-02-07 12:16:14 +10002302/**
2303 * i915_gem_mmap_gtt_ioctl - prepare an object for GTT mmap'ing
2304 * @dev: DRM device
2305 * @data: GTT mapping ioctl data
2306 * @file: GEM object info
2307 *
2308 * Simply returns the fake offset to userspace so it can mmap it.
2309 * The mmap call will end up in drm_gem_mmap(), which will set things
2310 * up so we can get faults in the handler above.
2311 *
2312 * The fault handler will take care of binding the object into the GTT
2313 * (since it may have been evicted to make room for something), allocating
2314 * a fence register, and mapping the appropriate aperture address into
2315 * userspace.
2316 */
2317int
2318i915_gem_mmap_gtt_ioctl(struct drm_device *dev, void *data,
2319 struct drm_file *file)
2320{
2321 struct drm_i915_gem_mmap_gtt *args = data;
2322
Dave Airlieda6b51d2014-12-24 13:11:17 +10002323 return i915_gem_mmap_gtt(file, dev, args->handle, &args->offset);
Dave Airlieff72145b2011-02-07 12:16:14 +10002324}
2325
Daniel Vetter225067e2012-08-20 10:23:20 +02002326/* Immediately discard the backing storage */
2327static void
2328i915_gem_object_truncate(struct drm_i915_gem_object *obj)
Chris Wilsone5281cc2010-10-28 13:45:36 +01002329{
Chris Wilson4d6294bf2012-08-11 15:41:05 +01002330 i915_gem_object_free_mmap_offset(obj);
Daniel Vetter1286ff72012-05-10 15:25:09 +02002331
Chris Wilson4d6294bf2012-08-11 15:41:05 +01002332 if (obj->base.filp == NULL)
2333 return;
2334
Daniel Vetter225067e2012-08-20 10:23:20 +02002335 /* Our goal here is to return as much of the memory as
2336 * is possible back to the system as we are called from OOM.
2337 * To do this we must instruct the shmfs to drop all of its
2338 * backing pages, *now*.
Chris Wilsone5281cc2010-10-28 13:45:36 +01002339 */
Chris Wilson55372522014-03-25 13:23:06 +00002340 shmem_truncate_range(file_inode(obj->base.filp), 0, (loff_t)-1);
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002341 obj->mm.madv = __I915_MADV_PURGED;
Chris Wilson4e5462e2017-03-07 13:20:31 +00002342 obj->mm.pages = ERR_PTR(-EFAULT);
Chris Wilsone5281cc2010-10-28 13:45:36 +01002343}
Chris Wilsone5281cc2010-10-28 13:45:36 +01002344
Chris Wilson55372522014-03-25 13:23:06 +00002345/* Try to discard unwanted pages */
Chris Wilson03ac84f2016-10-28 13:58:36 +01002346void __i915_gem_object_invalidate(struct drm_i915_gem_object *obj)
Daniel Vetter225067e2012-08-20 10:23:20 +02002347{
Chris Wilson55372522014-03-25 13:23:06 +00002348 struct address_space *mapping;
2349
Chris Wilson1233e2d2016-10-28 13:58:37 +01002350 lockdep_assert_held(&obj->mm.lock);
Chris Wilsonf1fa4f42017-10-13 21:26:13 +01002351 GEM_BUG_ON(i915_gem_object_has_pages(obj));
Chris Wilson1233e2d2016-10-28 13:58:37 +01002352
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002353 switch (obj->mm.madv) {
Chris Wilson55372522014-03-25 13:23:06 +00002354 case I915_MADV_DONTNEED:
2355 i915_gem_object_truncate(obj);
2356 case __I915_MADV_PURGED:
2357 return;
2358 }
2359
2360 if (obj->base.filp == NULL)
2361 return;
2362
Al Viro93c76a32015-12-04 23:45:44 -05002363 mapping = obj->base.filp->f_mapping,
Chris Wilson55372522014-03-25 13:23:06 +00002364 invalidate_mapping_pages(mapping, 0, (loff_t)-1);
Chris Wilsone5281cc2010-10-28 13:45:36 +01002365}
2366
Chris Wilson5cdf5882010-09-27 15:51:07 +01002367static void
Chris Wilson03ac84f2016-10-28 13:58:36 +01002368i915_gem_object_put_pages_gtt(struct drm_i915_gem_object *obj,
2369 struct sg_table *pages)
Eric Anholt673a3942008-07-30 12:06:12 -07002370{
Dave Gordon85d12252016-05-20 11:54:06 +01002371 struct sgt_iter sgt_iter;
2372 struct page *page;
Daniel Vetter1286ff72012-05-10 15:25:09 +02002373
Chris Wilsone5facdf2016-12-23 14:57:57 +00002374 __i915_gem_object_release_shmem(obj, pages, true);
Eric Anholt856fa192009-03-19 14:10:50 -07002375
Chris Wilson03ac84f2016-10-28 13:58:36 +01002376 i915_gem_gtt_finish_pages(obj, pages);
Imre Deake2273302015-07-09 12:59:05 +03002377
Daniel Vetter6dacfd22011-09-12 21:30:02 +02002378 if (i915_gem_object_needs_bit17_swizzle(obj))
Chris Wilson03ac84f2016-10-28 13:58:36 +01002379 i915_gem_object_save_bit_17_swizzle(obj, pages);
Eric Anholt280b7132009-03-12 16:56:27 -07002380
Chris Wilson03ac84f2016-10-28 13:58:36 +01002381 for_each_sgt_page(page, sgt_iter, pages) {
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002382 if (obj->mm.dirty)
Chris Wilson9da3da62012-06-01 15:20:22 +01002383 set_page_dirty(page);
Chris Wilson3ef94da2009-09-14 16:50:29 +01002384
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002385 if (obj->mm.madv == I915_MADV_WILLNEED)
Chris Wilson9da3da62012-06-01 15:20:22 +01002386 mark_page_accessed(page);
Chris Wilson3ef94da2009-09-14 16:50:29 +01002387
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002388 put_page(page);
Chris Wilson3ef94da2009-09-14 16:50:29 +01002389 }
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002390 obj->mm.dirty = false;
Eric Anholt673a3942008-07-30 12:06:12 -07002391
Chris Wilson03ac84f2016-10-28 13:58:36 +01002392 sg_free_table(pages);
2393 kfree(pages);
Chris Wilson37e680a2012-06-07 15:38:42 +01002394}
2395
Chris Wilson96d77632016-10-28 13:58:33 +01002396static void __i915_gem_object_reset_page_iter(struct drm_i915_gem_object *obj)
2397{
2398 struct radix_tree_iter iter;
Ville Syrjäläc23aa712017-09-01 20:12:51 +03002399 void __rcu **slot;
Chris Wilson96d77632016-10-28 13:58:33 +01002400
Chris Wilsonbea6e982017-10-26 14:00:31 +01002401 rcu_read_lock();
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002402 radix_tree_for_each_slot(slot, &obj->mm.get_page.radix, &iter, 0)
2403 radix_tree_delete(&obj->mm.get_page.radix, iter.index);
Chris Wilsonbea6e982017-10-26 14:00:31 +01002404 rcu_read_unlock();
Chris Wilson96d77632016-10-28 13:58:33 +01002405}
2406
Chris Wilson548625e2016-11-01 12:11:34 +00002407void __i915_gem_object_put_pages(struct drm_i915_gem_object *obj,
2408 enum i915_mm_subclass subclass)
Chris Wilson37e680a2012-06-07 15:38:42 +01002409{
Chris Wilsonf2123812017-10-16 12:40:37 +01002410 struct drm_i915_private *i915 = to_i915(obj->base.dev);
Chris Wilson03ac84f2016-10-28 13:58:36 +01002411 struct sg_table *pages;
Chris Wilson37e680a2012-06-07 15:38:42 +01002412
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002413 if (i915_gem_object_has_pinned_pages(obj))
Chris Wilson03ac84f2016-10-28 13:58:36 +01002414 return;
Chris Wilsona5570172012-09-04 21:02:54 +01002415
Chris Wilson15717de2016-08-04 07:52:26 +01002416 GEM_BUG_ON(obj->bind_count);
Chris Wilsonf1fa4f42017-10-13 21:26:13 +01002417 if (!i915_gem_object_has_pages(obj))
Chris Wilson1233e2d2016-10-28 13:58:37 +01002418 return;
2419
2420 /* May be called by shrinker from within get_pages() (on another bo) */
Chris Wilson548625e2016-11-01 12:11:34 +00002421 mutex_lock_nested(&obj->mm.lock, subclass);
Chris Wilson1233e2d2016-10-28 13:58:37 +01002422 if (unlikely(atomic_read(&obj->mm.pages_pin_count)))
2423 goto unlock;
Ben Widawsky3e123022013-07-31 17:00:04 -07002424
Chris Wilsona2165e32012-12-03 11:49:00 +00002425 /* ->put_pages might need to allocate memory for the bit17 swizzle
2426 * array, hence protect them from being reaped by removing them from gtt
2427 * lists early. */
Chris Wilson03ac84f2016-10-28 13:58:36 +01002428 pages = fetch_and_zero(&obj->mm.pages);
2429 GEM_BUG_ON(!pages);
Chris Wilsona2165e32012-12-03 11:49:00 +00002430
Chris Wilsonf2123812017-10-16 12:40:37 +01002431 spin_lock(&i915->mm.obj_lock);
2432 list_del(&obj->mm.link);
2433 spin_unlock(&i915->mm.obj_lock);
2434
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002435 if (obj->mm.mapping) {
Chris Wilson4b30cb22016-08-18 17:16:42 +01002436 void *ptr;
2437
Chris Wilson0ce81782017-05-17 13:09:59 +01002438 ptr = page_mask_bits(obj->mm.mapping);
Chris Wilson4b30cb22016-08-18 17:16:42 +01002439 if (is_vmalloc_addr(ptr))
2440 vunmap(ptr);
Chris Wilsonfb8621d2016-04-08 12:11:14 +01002441 else
Chris Wilson4b30cb22016-08-18 17:16:42 +01002442 kunmap(kmap_to_page(ptr));
2443
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002444 obj->mm.mapping = NULL;
Chris Wilson0a798eb2016-04-08 12:11:11 +01002445 }
2446
Chris Wilson96d77632016-10-28 13:58:33 +01002447 __i915_gem_object_reset_page_iter(obj);
2448
Chris Wilson4e5462e2017-03-07 13:20:31 +00002449 if (!IS_ERR(pages))
2450 obj->ops->put_pages(obj, pages);
2451
Matthew Aulda5c081662017-10-06 23:18:18 +01002452 obj->mm.page_sizes.phys = obj->mm.page_sizes.sg = 0;
2453
Chris Wilson1233e2d2016-10-28 13:58:37 +01002454unlock:
2455 mutex_unlock(&obj->mm.lock);
Chris Wilson6c085a72012-08-20 11:40:46 +02002456}
2457
Chris Wilson935a2f72017-02-13 17:15:13 +00002458static bool i915_sg_trim(struct sg_table *orig_st)
Tvrtko Ursulin0c40ce12016-11-09 15:13:43 +00002459{
2460 struct sg_table new_st;
2461 struct scatterlist *sg, *new_sg;
2462 unsigned int i;
2463
2464 if (orig_st->nents == orig_st->orig_nents)
Chris Wilson935a2f72017-02-13 17:15:13 +00002465 return false;
Tvrtko Ursulin0c40ce12016-11-09 15:13:43 +00002466
Chris Wilson8bfc478f2016-12-23 14:57:58 +00002467 if (sg_alloc_table(&new_st, orig_st->nents, GFP_KERNEL | __GFP_NOWARN))
Chris Wilson935a2f72017-02-13 17:15:13 +00002468 return false;
Tvrtko Ursulin0c40ce12016-11-09 15:13:43 +00002469
2470 new_sg = new_st.sgl;
2471 for_each_sg(orig_st->sgl, sg, orig_st->nents, i) {
2472 sg_set_page(new_sg, sg_page(sg), sg->length, 0);
2473 /* called before being DMA mapped, no need to copy sg->dma_* */
2474 new_sg = sg_next(new_sg);
2475 }
Chris Wilsonc2dc6cc2016-12-19 12:43:46 +00002476 GEM_BUG_ON(new_sg); /* Should walk exactly nents and hit the end */
Tvrtko Ursulin0c40ce12016-11-09 15:13:43 +00002477
2478 sg_free_table(orig_st);
2479
2480 *orig_st = new_st;
Chris Wilson935a2f72017-02-13 17:15:13 +00002481 return true;
Tvrtko Ursulin0c40ce12016-11-09 15:13:43 +00002482}
2483
Matthew Auldb91b09e2017-10-06 23:18:17 +01002484static int i915_gem_object_get_pages_gtt(struct drm_i915_gem_object *obj)
Eric Anholt673a3942008-07-30 12:06:12 -07002485{
Chris Wilsonfac5e232016-07-04 11:34:36 +01002486 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
Chris Wilsond766ef52016-12-19 12:43:45 +00002487 const unsigned long page_count = obj->base.size / PAGE_SIZE;
2488 unsigned long i;
Eric Anholt673a3942008-07-30 12:06:12 -07002489 struct address_space *mapping;
Chris Wilson9da3da62012-06-01 15:20:22 +01002490 struct sg_table *st;
2491 struct scatterlist *sg;
Dave Gordon85d12252016-05-20 11:54:06 +01002492 struct sgt_iter sgt_iter;
Eric Anholt673a3942008-07-30 12:06:12 -07002493 struct page *page;
Imre Deak90797e62013-02-18 19:28:03 +02002494 unsigned long last_pfn = 0; /* suppress gcc warning */
Tvrtko Ursulin56024522017-08-03 10:14:17 +01002495 unsigned int max_segment = i915_sg_segment_size();
Matthew Auld84e89782017-10-09 12:00:24 +01002496 unsigned int sg_page_sizes;
Chris Wilson4846bf02017-06-09 12:03:46 +01002497 gfp_t noreclaim;
Imre Deake2273302015-07-09 12:59:05 +03002498 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07002499
Chris Wilson6c085a72012-08-20 11:40:46 +02002500 /* Assert that the object is not currently in any GPU domain. As it
2501 * wasn't in the GTT, there shouldn't be any way it could have been in
2502 * a GPU cache
2503 */
Christian Königc0a51fd2018-02-16 13:43:38 +01002504 GEM_BUG_ON(obj->read_domains & I915_GEM_GPU_DOMAINS);
2505 GEM_BUG_ON(obj->write_domain & I915_GEM_GPU_DOMAINS);
Chris Wilson6c085a72012-08-20 11:40:46 +02002506
Chris Wilson9da3da62012-06-01 15:20:22 +01002507 st = kmalloc(sizeof(*st), GFP_KERNEL);
2508 if (st == NULL)
Matthew Auldb91b09e2017-10-06 23:18:17 +01002509 return -ENOMEM;
Eric Anholt673a3942008-07-30 12:06:12 -07002510
Chris Wilsond766ef52016-12-19 12:43:45 +00002511rebuild_st:
Chris Wilson9da3da62012-06-01 15:20:22 +01002512 if (sg_alloc_table(st, page_count, GFP_KERNEL)) {
Chris Wilson9da3da62012-06-01 15:20:22 +01002513 kfree(st);
Matthew Auldb91b09e2017-10-06 23:18:17 +01002514 return -ENOMEM;
Chris Wilson9da3da62012-06-01 15:20:22 +01002515 }
2516
2517 /* Get the list of pages out of our struct file. They'll be pinned
2518 * at this point until we release them.
2519 *
2520 * Fail silently without starting the shrinker
2521 */
Al Viro93c76a32015-12-04 23:45:44 -05002522 mapping = obj->base.filp->f_mapping;
Chris Wilson0f6ab552017-06-09 12:03:48 +01002523 noreclaim = mapping_gfp_constraint(mapping, ~__GFP_RECLAIM);
Chris Wilson4846bf02017-06-09 12:03:46 +01002524 noreclaim |= __GFP_NORETRY | __GFP_NOWARN;
2525
Imre Deak90797e62013-02-18 19:28:03 +02002526 sg = st->sgl;
2527 st->nents = 0;
Matthew Auld84e89782017-10-09 12:00:24 +01002528 sg_page_sizes = 0;
Imre Deak90797e62013-02-18 19:28:03 +02002529 for (i = 0; i < page_count; i++) {
Chris Wilson4846bf02017-06-09 12:03:46 +01002530 const unsigned int shrink[] = {
2531 I915_SHRINK_BOUND | I915_SHRINK_UNBOUND | I915_SHRINK_PURGEABLE,
2532 0,
2533 }, *s = shrink;
2534 gfp_t gfp = noreclaim;
2535
2536 do {
Chris Wilson6c085a72012-08-20 11:40:46 +02002537 page = shmem_read_mapping_page_gfp(mapping, i, gfp);
Chris Wilson4846bf02017-06-09 12:03:46 +01002538 if (likely(!IS_ERR(page)))
2539 break;
2540
2541 if (!*s) {
2542 ret = PTR_ERR(page);
2543 goto err_sg;
2544 }
2545
Chris Wilson912d5722017-09-06 16:19:30 -07002546 i915_gem_shrink(dev_priv, 2 * page_count, NULL, *s++);
Chris Wilson4846bf02017-06-09 12:03:46 +01002547 cond_resched();
Chris Wilson24f8e002017-03-22 11:05:21 +00002548
Chris Wilson6c085a72012-08-20 11:40:46 +02002549 /* We've tried hard to allocate the memory by reaping
2550 * our own buffer, now let the real VM do its job and
2551 * go down in flames if truly OOM.
Chris Wilson24f8e002017-03-22 11:05:21 +00002552 *
2553 * However, since graphics tend to be disposable,
2554 * defer the oom here by reporting the ENOMEM back
2555 * to userspace.
Chris Wilson6c085a72012-08-20 11:40:46 +02002556 */
Chris Wilson4846bf02017-06-09 12:03:46 +01002557 if (!*s) {
2558 /* reclaim and warn, but no oom */
2559 gfp = mapping_gfp_mask(mapping);
Chris Wilsoneaf41802017-06-09 12:03:47 +01002560
2561 /* Our bo are always dirty and so we require
2562 * kswapd to reclaim our pages (direct reclaim
2563 * does not effectively begin pageout of our
2564 * buffers on its own). However, direct reclaim
2565 * only waits for kswapd when under allocation
2566 * congestion. So as a result __GFP_RECLAIM is
2567 * unreliable and fails to actually reclaim our
2568 * dirty pages -- unless you try over and over
2569 * again with !__GFP_NORETRY. However, we still
2570 * want to fail this allocation rather than
2571 * trigger the out-of-memory killer and for
Michal Hockodbb32952017-07-12 14:36:55 -07002572 * this we want __GFP_RETRY_MAYFAIL.
Chris Wilsoneaf41802017-06-09 12:03:47 +01002573 */
Michal Hockodbb32952017-07-12 14:36:55 -07002574 gfp |= __GFP_RETRY_MAYFAIL;
Imre Deake2273302015-07-09 12:59:05 +03002575 }
Chris Wilson4846bf02017-06-09 12:03:46 +01002576 } while (1);
2577
Chris Wilson871dfbd2016-10-11 09:20:21 +01002578 if (!i ||
2579 sg->length >= max_segment ||
2580 page_to_pfn(page) != last_pfn + 1) {
Matthew Aulda5c081662017-10-06 23:18:18 +01002581 if (i) {
Matthew Auld84e89782017-10-09 12:00:24 +01002582 sg_page_sizes |= sg->length;
Imre Deak90797e62013-02-18 19:28:03 +02002583 sg = sg_next(sg);
Matthew Aulda5c081662017-10-06 23:18:18 +01002584 }
Imre Deak90797e62013-02-18 19:28:03 +02002585 st->nents++;
2586 sg_set_page(sg, page, PAGE_SIZE, 0);
2587 } else {
2588 sg->length += PAGE_SIZE;
2589 }
2590 last_pfn = page_to_pfn(page);
Daniel Vetter3bbbe702013-10-07 17:15:45 -03002591
2592 /* Check that the i965g/gm workaround works. */
2593 WARN_ON((gfp & __GFP_DMA32) && (last_pfn >= 0x00100000UL));
Eric Anholt673a3942008-07-30 12:06:12 -07002594 }
Matthew Aulda5c081662017-10-06 23:18:18 +01002595 if (sg) { /* loop terminated early; short sg table */
Matthew Auld84e89782017-10-09 12:00:24 +01002596 sg_page_sizes |= sg->length;
Konrad Rzeszutek Wilk426729d2013-06-24 11:47:48 -04002597 sg_mark_end(sg);
Matthew Aulda5c081662017-10-06 23:18:18 +01002598 }
Chris Wilson74ce6b62012-10-19 15:51:06 +01002599
Tvrtko Ursulin0c40ce12016-11-09 15:13:43 +00002600 /* Trim unused sg entries to avoid wasting memory. */
2601 i915_sg_trim(st);
2602
Chris Wilson03ac84f2016-10-28 13:58:36 +01002603 ret = i915_gem_gtt_prepare_pages(obj, st);
Chris Wilsond766ef52016-12-19 12:43:45 +00002604 if (ret) {
2605 /* DMA remapping failed? One possible cause is that
2606 * it could not reserve enough large entries, asking
2607 * for PAGE_SIZE chunks instead may be helpful.
2608 */
2609 if (max_segment > PAGE_SIZE) {
2610 for_each_sgt_page(page, sgt_iter, st)
2611 put_page(page);
2612 sg_free_table(st);
2613
2614 max_segment = PAGE_SIZE;
2615 goto rebuild_st;
2616 } else {
2617 dev_warn(&dev_priv->drm.pdev->dev,
2618 "Failed to DMA remap %lu pages\n",
2619 page_count);
2620 goto err_pages;
2621 }
2622 }
Imre Deake2273302015-07-09 12:59:05 +03002623
Eric Anholt673a3942008-07-30 12:06:12 -07002624 if (i915_gem_object_needs_bit17_swizzle(obj))
Chris Wilson03ac84f2016-10-28 13:58:36 +01002625 i915_gem_object_do_bit_17_swizzle(obj, st);
Eric Anholt673a3942008-07-30 12:06:12 -07002626
Matthew Auld84e89782017-10-09 12:00:24 +01002627 __i915_gem_object_set_pages(obj, st, sg_page_sizes);
Matthew Auldb91b09e2017-10-06 23:18:17 +01002628
2629 return 0;
Eric Anholt673a3942008-07-30 12:06:12 -07002630
Chris Wilsonb17993b2016-11-14 11:29:30 +00002631err_sg:
Imre Deak90797e62013-02-18 19:28:03 +02002632 sg_mark_end(sg);
Chris Wilsonb17993b2016-11-14 11:29:30 +00002633err_pages:
Dave Gordon85d12252016-05-20 11:54:06 +01002634 for_each_sgt_page(page, sgt_iter, st)
2635 put_page(page);
Chris Wilson9da3da62012-06-01 15:20:22 +01002636 sg_free_table(st);
2637 kfree(st);
Chris Wilson0820baf2014-03-25 13:23:03 +00002638
2639 /* shmemfs first checks if there is enough memory to allocate the page
2640 * and reports ENOSPC should there be insufficient, along with the usual
2641 * ENOMEM for a genuine allocation failure.
2642 *
2643 * We use ENOSPC in our driver to mean that we have run out of aperture
2644 * space and so want to translate the error from shmemfs back to our
2645 * usual understanding of ENOMEM.
2646 */
Imre Deake2273302015-07-09 12:59:05 +03002647 if (ret == -ENOSPC)
2648 ret = -ENOMEM;
2649
Matthew Auldb91b09e2017-10-06 23:18:17 +01002650 return ret;
Chris Wilson03ac84f2016-10-28 13:58:36 +01002651}
2652
2653void __i915_gem_object_set_pages(struct drm_i915_gem_object *obj,
Matthew Aulda5c081662017-10-06 23:18:18 +01002654 struct sg_table *pages,
Matthew Auld84e89782017-10-09 12:00:24 +01002655 unsigned int sg_page_sizes)
Chris Wilson03ac84f2016-10-28 13:58:36 +01002656{
Matthew Aulda5c081662017-10-06 23:18:18 +01002657 struct drm_i915_private *i915 = to_i915(obj->base.dev);
2658 unsigned long supported = INTEL_INFO(i915)->page_sizes;
2659 int i;
2660
Chris Wilson1233e2d2016-10-28 13:58:37 +01002661 lockdep_assert_held(&obj->mm.lock);
Chris Wilson03ac84f2016-10-28 13:58:36 +01002662
2663 obj->mm.get_page.sg_pos = pages->sgl;
2664 obj->mm.get_page.sg_idx = 0;
2665
2666 obj->mm.pages = pages;
Chris Wilson2c3a3f42016-11-04 10:30:01 +00002667
2668 if (i915_gem_object_is_tiled(obj) &&
Chris Wilsonf2123812017-10-16 12:40:37 +01002669 i915->quirks & QUIRK_PIN_SWIZZLED_PAGES) {
Chris Wilson2c3a3f42016-11-04 10:30:01 +00002670 GEM_BUG_ON(obj->mm.quirked);
2671 __i915_gem_object_pin_pages(obj);
2672 obj->mm.quirked = true;
2673 }
Matthew Aulda5c081662017-10-06 23:18:18 +01002674
Matthew Auld84e89782017-10-09 12:00:24 +01002675 GEM_BUG_ON(!sg_page_sizes);
2676 obj->mm.page_sizes.phys = sg_page_sizes;
Matthew Aulda5c081662017-10-06 23:18:18 +01002677
2678 /*
Matthew Auld84e89782017-10-09 12:00:24 +01002679 * Calculate the supported page-sizes which fit into the given
2680 * sg_page_sizes. This will give us the page-sizes which we may be able
2681 * to use opportunistically when later inserting into the GTT. For
2682 * example if phys=2G, then in theory we should be able to use 1G, 2M,
2683 * 64K or 4K pages, although in practice this will depend on a number of
2684 * other factors.
Matthew Aulda5c081662017-10-06 23:18:18 +01002685 */
2686 obj->mm.page_sizes.sg = 0;
2687 for_each_set_bit(i, &supported, ilog2(I915_GTT_MAX_PAGE_SIZE) + 1) {
2688 if (obj->mm.page_sizes.phys & ~0u << i)
2689 obj->mm.page_sizes.sg |= BIT(i);
2690 }
Matthew Aulda5c081662017-10-06 23:18:18 +01002691 GEM_BUG_ON(!HAS_PAGE_SIZES(i915, obj->mm.page_sizes.sg));
Chris Wilsonf2123812017-10-16 12:40:37 +01002692
2693 spin_lock(&i915->mm.obj_lock);
2694 list_add(&obj->mm.link, &i915->mm.unbound_list);
2695 spin_unlock(&i915->mm.obj_lock);
Chris Wilson03ac84f2016-10-28 13:58:36 +01002696}
2697
2698static int ____i915_gem_object_get_pages(struct drm_i915_gem_object *obj)
2699{
Matthew Auldb91b09e2017-10-06 23:18:17 +01002700 int err;
Chris Wilson03ac84f2016-10-28 13:58:36 +01002701
2702 if (unlikely(obj->mm.madv != I915_MADV_WILLNEED)) {
2703 DRM_DEBUG("Attempting to obtain a purgeable object\n");
2704 return -EFAULT;
2705 }
2706
Matthew Auldb91b09e2017-10-06 23:18:17 +01002707 err = obj->ops->get_pages(obj);
Matthew Auldb65a9b92017-12-18 10:38:55 +00002708 GEM_BUG_ON(!err && !i915_gem_object_has_pages(obj));
Chris Wilson03ac84f2016-10-28 13:58:36 +01002709
Matthew Auldb91b09e2017-10-06 23:18:17 +01002710 return err;
Eric Anholt673a3942008-07-30 12:06:12 -07002711}
2712
Chris Wilson37e680a2012-06-07 15:38:42 +01002713/* Ensure that the associated pages are gathered from the backing storage
Chris Wilson1233e2d2016-10-28 13:58:37 +01002714 * and pinned into our object. i915_gem_object_pin_pages() may be called
Chris Wilson37e680a2012-06-07 15:38:42 +01002715 * multiple times before they are released by a single call to
Chris Wilson1233e2d2016-10-28 13:58:37 +01002716 * i915_gem_object_unpin_pages() - once the pages are no longer referenced
Chris Wilson37e680a2012-06-07 15:38:42 +01002717 * either as a result of memory pressure (reaping pages under the shrinker)
2718 * or as the object is itself released.
2719 */
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002720int __i915_gem_object_get_pages(struct drm_i915_gem_object *obj)
Chris Wilson37e680a2012-06-07 15:38:42 +01002721{
Chris Wilson03ac84f2016-10-28 13:58:36 +01002722 int err;
Chris Wilson37e680a2012-06-07 15:38:42 +01002723
Chris Wilson1233e2d2016-10-28 13:58:37 +01002724 err = mutex_lock_interruptible(&obj->mm.lock);
2725 if (err)
2726 return err;
Chris Wilson4c7d62c2016-10-28 13:58:32 +01002727
Chris Wilsonf1fa4f42017-10-13 21:26:13 +01002728 if (unlikely(!i915_gem_object_has_pages(obj))) {
Chris Wilson88c880b2017-09-06 14:52:20 +01002729 GEM_BUG_ON(i915_gem_object_has_pinned_pages(obj));
2730
Chris Wilson2c3a3f42016-11-04 10:30:01 +00002731 err = ____i915_gem_object_get_pages(obj);
2732 if (err)
2733 goto unlock;
2734
2735 smp_mb__before_atomic();
Chris Wilson1233e2d2016-10-28 13:58:37 +01002736 }
Chris Wilson2c3a3f42016-11-04 10:30:01 +00002737 atomic_inc(&obj->mm.pages_pin_count);
Chris Wilson43e28f02013-01-08 10:53:09 +00002738
Chris Wilson1233e2d2016-10-28 13:58:37 +01002739unlock:
2740 mutex_unlock(&obj->mm.lock);
Chris Wilson03ac84f2016-10-28 13:58:36 +01002741 return err;
Eric Anholt673a3942008-07-30 12:06:12 -07002742}
2743
Dave Gordondd6034c2016-05-20 11:54:04 +01002744/* The 'mapping' part of i915_gem_object_pin_map() below */
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002745static void *i915_gem_object_map(const struct drm_i915_gem_object *obj,
2746 enum i915_map_type type)
Dave Gordondd6034c2016-05-20 11:54:04 +01002747{
2748 unsigned long n_pages = obj->base.size >> PAGE_SHIFT;
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002749 struct sg_table *sgt = obj->mm.pages;
Dave Gordon85d12252016-05-20 11:54:06 +01002750 struct sgt_iter sgt_iter;
2751 struct page *page;
Dave Gordonb338fa42016-05-20 11:54:05 +01002752 struct page *stack_pages[32];
2753 struct page **pages = stack_pages;
Dave Gordondd6034c2016-05-20 11:54:04 +01002754 unsigned long i = 0;
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002755 pgprot_t pgprot;
Dave Gordondd6034c2016-05-20 11:54:04 +01002756 void *addr;
2757
2758 /* A single page can always be kmapped */
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002759 if (n_pages == 1 && type == I915_MAP_WB)
Dave Gordondd6034c2016-05-20 11:54:04 +01002760 return kmap(sg_page(sgt->sgl));
2761
Dave Gordonb338fa42016-05-20 11:54:05 +01002762 if (n_pages > ARRAY_SIZE(stack_pages)) {
2763 /* Too big for stack -- allocate temporary array instead */
Michal Hocko0ee931c2017-09-13 16:28:29 -07002764 pages = kvmalloc_array(n_pages, sizeof(*pages), GFP_KERNEL);
Dave Gordonb338fa42016-05-20 11:54:05 +01002765 if (!pages)
2766 return NULL;
2767 }
Dave Gordondd6034c2016-05-20 11:54:04 +01002768
Dave Gordon85d12252016-05-20 11:54:06 +01002769 for_each_sgt_page(page, sgt_iter, sgt)
2770 pages[i++] = page;
Dave Gordondd6034c2016-05-20 11:54:04 +01002771
2772 /* Check that we have the expected number of pages */
2773 GEM_BUG_ON(i != n_pages);
2774
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002775 switch (type) {
Chris Wilsona575c672017-08-28 11:46:31 +01002776 default:
2777 MISSING_CASE(type);
2778 /* fallthrough to use PAGE_KERNEL anyway */
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002779 case I915_MAP_WB:
2780 pgprot = PAGE_KERNEL;
2781 break;
2782 case I915_MAP_WC:
2783 pgprot = pgprot_writecombine(PAGE_KERNEL_IO);
2784 break;
2785 }
2786 addr = vmap(pages, n_pages, 0, pgprot);
Dave Gordondd6034c2016-05-20 11:54:04 +01002787
Dave Gordonb338fa42016-05-20 11:54:05 +01002788 if (pages != stack_pages)
Michal Hocko20981052017-05-17 14:23:12 +02002789 kvfree(pages);
Dave Gordondd6034c2016-05-20 11:54:04 +01002790
2791 return addr;
2792}
2793
2794/* get, pin, and map the pages of the object into kernel space */
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002795void *i915_gem_object_pin_map(struct drm_i915_gem_object *obj,
2796 enum i915_map_type type)
Chris Wilson0a798eb2016-04-08 12:11:11 +01002797{
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002798 enum i915_map_type has_type;
2799 bool pinned;
2800 void *ptr;
Chris Wilson0a798eb2016-04-08 12:11:11 +01002801 int ret;
2802
Tina Zhanga03f3952017-11-14 10:25:13 +00002803 if (unlikely(!i915_gem_object_has_struct_page(obj)))
2804 return ERR_PTR(-ENXIO);
Chris Wilson0a798eb2016-04-08 12:11:11 +01002805
Chris Wilson1233e2d2016-10-28 13:58:37 +01002806 ret = mutex_lock_interruptible(&obj->mm.lock);
Chris Wilson0a798eb2016-04-08 12:11:11 +01002807 if (ret)
2808 return ERR_PTR(ret);
2809
Chris Wilsona575c672017-08-28 11:46:31 +01002810 pinned = !(type & I915_MAP_OVERRIDE);
2811 type &= ~I915_MAP_OVERRIDE;
2812
Chris Wilson1233e2d2016-10-28 13:58:37 +01002813 if (!atomic_inc_not_zero(&obj->mm.pages_pin_count)) {
Chris Wilsonf1fa4f42017-10-13 21:26:13 +01002814 if (unlikely(!i915_gem_object_has_pages(obj))) {
Chris Wilson88c880b2017-09-06 14:52:20 +01002815 GEM_BUG_ON(i915_gem_object_has_pinned_pages(obj));
2816
Chris Wilson2c3a3f42016-11-04 10:30:01 +00002817 ret = ____i915_gem_object_get_pages(obj);
2818 if (ret)
2819 goto err_unlock;
Chris Wilson1233e2d2016-10-28 13:58:37 +01002820
Chris Wilson2c3a3f42016-11-04 10:30:01 +00002821 smp_mb__before_atomic();
2822 }
2823 atomic_inc(&obj->mm.pages_pin_count);
Chris Wilson1233e2d2016-10-28 13:58:37 +01002824 pinned = false;
2825 }
Chris Wilsonf1fa4f42017-10-13 21:26:13 +01002826 GEM_BUG_ON(!i915_gem_object_has_pages(obj));
Chris Wilson0a798eb2016-04-08 12:11:11 +01002827
Chris Wilson0ce81782017-05-17 13:09:59 +01002828 ptr = page_unpack_bits(obj->mm.mapping, &has_type);
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002829 if (ptr && has_type != type) {
2830 if (pinned) {
2831 ret = -EBUSY;
Chris Wilson1233e2d2016-10-28 13:58:37 +01002832 goto err_unpin;
Chris Wilson0a798eb2016-04-08 12:11:11 +01002833 }
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002834
2835 if (is_vmalloc_addr(ptr))
2836 vunmap(ptr);
2837 else
2838 kunmap(kmap_to_page(ptr));
2839
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002840 ptr = obj->mm.mapping = NULL;
Chris Wilson0a798eb2016-04-08 12:11:11 +01002841 }
2842
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002843 if (!ptr) {
2844 ptr = i915_gem_object_map(obj, type);
2845 if (!ptr) {
2846 ret = -ENOMEM;
Chris Wilson1233e2d2016-10-28 13:58:37 +01002847 goto err_unpin;
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002848 }
2849
Chris Wilson0ce81782017-05-17 13:09:59 +01002850 obj->mm.mapping = page_pack_bits(ptr, type);
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002851 }
2852
Chris Wilson1233e2d2016-10-28 13:58:37 +01002853out_unlock:
2854 mutex_unlock(&obj->mm.lock);
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002855 return ptr;
2856
Chris Wilson1233e2d2016-10-28 13:58:37 +01002857err_unpin:
2858 atomic_dec(&obj->mm.pages_pin_count);
2859err_unlock:
2860 ptr = ERR_PTR(ret);
2861 goto out_unlock;
Chris Wilson0a798eb2016-04-08 12:11:11 +01002862}
2863
Chris Wilson7c55e2c2017-03-07 12:03:38 +00002864static int
2865i915_gem_object_pwrite_gtt(struct drm_i915_gem_object *obj,
2866 const struct drm_i915_gem_pwrite *arg)
2867{
2868 struct address_space *mapping = obj->base.filp->f_mapping;
2869 char __user *user_data = u64_to_user_ptr(arg->data_ptr);
2870 u64 remain, offset;
2871 unsigned int pg;
2872
2873 /* Before we instantiate/pin the backing store for our use, we
2874 * can prepopulate the shmemfs filp efficiently using a write into
2875 * the pagecache. We avoid the penalty of instantiating all the
2876 * pages, important if the user is just writing to a few and never
2877 * uses the object on the GPU, and using a direct write into shmemfs
2878 * allows it to avoid the cost of retrieving a page (either swapin
2879 * or clearing-before-use) before it is overwritten.
2880 */
Chris Wilsonf1fa4f42017-10-13 21:26:13 +01002881 if (i915_gem_object_has_pages(obj))
Chris Wilson7c55e2c2017-03-07 12:03:38 +00002882 return -ENODEV;
2883
Chris Wilsona6d65e42017-10-16 21:27:32 +01002884 if (obj->mm.madv != I915_MADV_WILLNEED)
2885 return -EFAULT;
2886
Chris Wilson7c55e2c2017-03-07 12:03:38 +00002887 /* Before the pages are instantiated the object is treated as being
2888 * in the CPU domain. The pages will be clflushed as required before
2889 * use, and we can freely write into the pages directly. If userspace
2890 * races pwrite with any other operation; corruption will ensue -
2891 * that is userspace's prerogative!
2892 */
2893
2894 remain = arg->size;
2895 offset = arg->offset;
2896 pg = offset_in_page(offset);
2897
2898 do {
2899 unsigned int len, unwritten;
2900 struct page *page;
2901 void *data, *vaddr;
2902 int err;
2903
2904 len = PAGE_SIZE - pg;
2905 if (len > remain)
2906 len = remain;
2907
2908 err = pagecache_write_begin(obj->base.filp, mapping,
2909 offset, len, 0,
2910 &page, &data);
2911 if (err < 0)
2912 return err;
2913
2914 vaddr = kmap(page);
2915 unwritten = copy_from_user(vaddr + pg, user_data, len);
2916 kunmap(page);
2917
2918 err = pagecache_write_end(obj->base.filp, mapping,
2919 offset, len, len - unwritten,
2920 page, data);
2921 if (err < 0)
2922 return err;
2923
2924 if (unwritten)
2925 return -EFAULT;
2926
2927 remain -= len;
2928 user_data += len;
2929 offset += len;
2930 pg = 0;
2931 } while (remain);
2932
2933 return 0;
2934}
2935
Mika Kuoppalae5e1fc42016-11-16 17:20:31 +02002936static void i915_gem_context_mark_guilty(struct i915_gem_context *ctx)
Mika Kuoppalaaa60c662013-06-12 15:13:20 +03002937{
Chris Wilson77b25a92017-07-21 13:32:30 +01002938 bool banned;
Mika Kuoppalab083a082016-11-18 15:10:47 +02002939
Chris Wilson77b25a92017-07-21 13:32:30 +01002940 atomic_inc(&ctx->guilty_count);
2941
Chris Wilson24eae082018-02-05 09:22:01 +00002942 banned = false;
2943 if (i915_gem_context_is_bannable(ctx)) {
2944 unsigned int score;
2945
2946 score = atomic_add_return(CONTEXT_SCORE_GUILTY,
2947 &ctx->ban_score);
2948 banned = score >= CONTEXT_SCORE_BAN_THRESHOLD;
2949
2950 DRM_DEBUG_DRIVER("context %s marked guilty (score %d) banned? %s\n",
2951 ctx->name, score, yesno(banned));
2952 }
Chris Wilson77b25a92017-07-21 13:32:30 +01002953 if (!banned)
Mika Kuoppalab083a082016-11-18 15:10:47 +02002954 return;
2955
Chris Wilson77b25a92017-07-21 13:32:30 +01002956 i915_gem_context_set_banned(ctx);
2957 if (!IS_ERR_OR_NULL(ctx->file_priv)) {
2958 atomic_inc(&ctx->file_priv->context_bans);
2959 DRM_DEBUG_DRIVER("client %s has had %d context banned\n",
2960 ctx->name, atomic_read(&ctx->file_priv->context_bans));
2961 }
Mika Kuoppalae5e1fc42016-11-16 17:20:31 +02002962}
2963
2964static void i915_gem_context_mark_innocent(struct i915_gem_context *ctx)
2965{
Chris Wilson77b25a92017-07-21 13:32:30 +01002966 atomic_inc(&ctx->active_count);
Mika Kuoppalaaa60c662013-06-12 15:13:20 +03002967}
2968
Chris Wilsone61e0f52018-02-21 09:56:36 +00002969struct i915_request *
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00002970i915_gem_find_active_request(struct intel_engine_cs *engine)
Chris Wilson9375e442010-09-19 12:21:28 +01002971{
Chris Wilsone61e0f52018-02-21 09:56:36 +00002972 struct i915_request *request, *active = NULL;
Chris Wilson754c9fd2017-02-23 07:44:14 +00002973 unsigned long flags;
Mika Kuoppalaaa60c662013-06-12 15:13:20 +03002974
Chris Wilsonf69a02c2016-07-01 17:23:16 +01002975 /* We are called by the error capture and reset at a random
2976 * point in time. In particular, note that neither is crucially
2977 * ordered with an interrupt. After a hang, the GPU is dead and we
2978 * assume that no more writes can happen (we waited long enough for
2979 * all writes that were in transaction to be flushed) - adding an
2980 * extra delay for a recent interrupt is pointless. Hence, we do
2981 * not need an engine->irq_seqno_barrier() before the seqno reads.
2982 */
Chris Wilsona89d1f92018-05-02 17:38:39 +01002983 spin_lock_irqsave(&engine->timeline.lock, flags);
2984 list_for_each_entry(request, &engine->timeline.requests, link) {
Chris Wilsone61e0f52018-02-21 09:56:36 +00002985 if (__i915_request_completed(request, request->global_seqno))
Chris Wilson4db080f2013-12-04 11:37:09 +00002986 continue;
Mika Kuoppalaaa60c662013-06-12 15:13:20 +03002987
Mika Kuoppala36193ac2017-01-17 17:59:02 +02002988 GEM_BUG_ON(request->engine != engine);
Chris Wilsonc00122f32017-02-12 17:19:58 +00002989 GEM_BUG_ON(test_bit(DMA_FENCE_FLAG_SIGNALED_BIT,
2990 &request->fence.flags));
Mika Kuoppalab6b0fac2014-01-30 19:04:43 +02002991
Chris Wilson754c9fd2017-02-23 07:44:14 +00002992 active = request;
2993 break;
2994 }
Chris Wilsona89d1f92018-05-02 17:38:39 +01002995 spin_unlock_irqrestore(&engine->timeline.lock, flags);
Chris Wilson754c9fd2017-02-23 07:44:14 +00002996
2997 return active;
Mika Kuoppalab6b0fac2014-01-30 19:04:43 +02002998}
2999
Michel Thierrya1ef70e2017-06-20 10:57:47 +01003000/*
3001 * Ensure irq handler finishes, and not run again.
3002 * Also return the active request so that we only search for it once.
3003 */
Chris Wilsone61e0f52018-02-21 09:56:36 +00003004struct i915_request *
Michel Thierrya1ef70e2017-06-20 10:57:47 +01003005i915_gem_reset_prepare_engine(struct intel_engine_cs *engine)
3006{
Chris Wilson5adfb772018-05-16 19:33:51 +01003007 struct i915_request *request;
Michel Thierrya1ef70e2017-06-20 10:57:47 +01003008
Chris Wilson1749d902017-10-09 12:02:59 +01003009 /*
3010 * During the reset sequence, we must prevent the engine from
3011 * entering RC6. As the context state is undefined until we restart
3012 * the engine, if it does enter RC6 during the reset, the state
3013 * written to the powercontext is undefined and so we may lose
3014 * GPU state upon resume, i.e. fail to restart after a reset.
3015 */
3016 intel_uncore_forcewake_get(engine->i915, FORCEWAKE_ALL);
3017
Chris Wilson5adfb772018-05-16 19:33:51 +01003018 request = engine->reset.prepare(engine);
Chris Wilsond1d1ebf42017-07-21 13:32:33 +01003019 if (request && request->fence.error == -EIO)
3020 request = ERR_PTR(-EIO); /* Previous reset failed! */
Michel Thierrya1ef70e2017-06-20 10:57:47 +01003021
3022 return request;
3023}
3024
Chris Wilson0e178ae2017-01-17 17:59:06 +02003025int i915_gem_reset_prepare(struct drm_i915_private *dev_priv)
Chris Wilson4c965542017-01-17 17:59:01 +02003026{
3027 struct intel_engine_cs *engine;
Chris Wilsone61e0f52018-02-21 09:56:36 +00003028 struct i915_request *request;
Chris Wilson4c965542017-01-17 17:59:01 +02003029 enum intel_engine_id id;
Chris Wilson0e178ae2017-01-17 17:59:06 +02003030 int err = 0;
Chris Wilson4c965542017-01-17 17:59:01 +02003031
Chris Wilson0e178ae2017-01-17 17:59:06 +02003032 for_each_engine(engine, dev_priv, id) {
Michel Thierrya1ef70e2017-06-20 10:57:47 +01003033 request = i915_gem_reset_prepare_engine(engine);
3034 if (IS_ERR(request)) {
3035 err = PTR_ERR(request);
3036 continue;
Chris Wilson0e178ae2017-01-17 17:59:06 +02003037 }
Michel Thierryc64992e2017-06-20 10:57:44 +01003038
3039 engine->hangcheck.active_request = request;
Chris Wilson0e178ae2017-01-17 17:59:06 +02003040 }
3041
Chris Wilson4c965542017-01-17 17:59:01 +02003042 i915_gem_revoke_fences(dev_priv);
Michal Wajdeczkoc37d5722018-03-12 13:03:07 +00003043 intel_uc_sanitize(dev_priv);
Chris Wilson0e178ae2017-01-17 17:59:06 +02003044
3045 return err;
Chris Wilson4c965542017-01-17 17:59:01 +02003046}
3047
Chris Wilsone61e0f52018-02-21 09:56:36 +00003048static void skip_request(struct i915_request *request)
Mika Kuoppalab6b0fac2014-01-30 19:04:43 +02003049{
Chris Wilson821ed7d2016-09-09 14:11:53 +01003050 void *vaddr = request->ring->vaddr;
3051 u32 head;
Mika Kuoppalab6b0fac2014-01-30 19:04:43 +02003052
Chris Wilson821ed7d2016-09-09 14:11:53 +01003053 /* As this request likely depends on state from the lost
3054 * context, clear out all the user operations leaving the
3055 * breadcrumb at the end (so we get the fence notifications).
3056 */
3057 head = request->head;
3058 if (request->postfix < head) {
3059 memset(vaddr + head, 0, request->ring->size - head);
3060 head = 0;
3061 }
3062 memset(vaddr + head, 0, request->postfix - head);
Chris Wilsonc0d5f322017-01-10 17:22:43 +00003063
3064 dma_fence_set_error(&request->fence, -EIO);
Chris Wilson4db080f2013-12-04 11:37:09 +00003065}
3066
Chris Wilsone61e0f52018-02-21 09:56:36 +00003067static void engine_skip_context(struct i915_request *request)
Mika Kuoppala36193ac2017-01-17 17:59:02 +02003068{
3069 struct intel_engine_cs *engine = request->engine;
Chris Wilson4e0d64d2018-05-17 22:26:30 +01003070 struct i915_gem_context *hung_ctx = request->gem_context;
Chris Wilsona89d1f92018-05-02 17:38:39 +01003071 struct i915_timeline *timeline = request->timeline;
Mika Kuoppala36193ac2017-01-17 17:59:02 +02003072 unsigned long flags;
3073
Chris Wilsona89d1f92018-05-02 17:38:39 +01003074 GEM_BUG_ON(timeline == &engine->timeline);
Mika Kuoppala36193ac2017-01-17 17:59:02 +02003075
Chris Wilsona89d1f92018-05-02 17:38:39 +01003076 spin_lock_irqsave(&engine->timeline.lock, flags);
Chris Wilson0c591a42018-05-12 09:49:57 +01003077 spin_lock_nested(&timeline->lock, SINGLE_DEPTH_NESTING);
Mika Kuoppala36193ac2017-01-17 17:59:02 +02003078
Chris Wilsona89d1f92018-05-02 17:38:39 +01003079 list_for_each_entry_continue(request, &engine->timeline.requests, link)
Chris Wilson4e0d64d2018-05-17 22:26:30 +01003080 if (request->gem_context == hung_ctx)
Mika Kuoppala36193ac2017-01-17 17:59:02 +02003081 skip_request(request);
3082
3083 list_for_each_entry(request, &timeline->requests, link)
3084 skip_request(request);
3085
3086 spin_unlock(&timeline->lock);
Chris Wilsona89d1f92018-05-02 17:38:39 +01003087 spin_unlock_irqrestore(&engine->timeline.lock, flags);
Mika Kuoppala36193ac2017-01-17 17:59:02 +02003088}
3089
Chris Wilsond1d1ebf42017-07-21 13:32:33 +01003090/* Returns the request if it was guilty of the hang */
Chris Wilsone61e0f52018-02-21 09:56:36 +00003091static struct i915_request *
Chris Wilsond1d1ebf42017-07-21 13:32:33 +01003092i915_gem_reset_request(struct intel_engine_cs *engine,
Chris Wilsonbba08692018-04-06 23:03:53 +01003093 struct i915_request *request,
3094 bool stalled)
Mika Kuoppala61da5362017-01-17 17:59:05 +02003095{
Mika Kuoppala71895a02017-01-17 17:59:07 +02003096 /* The guilty request will get skipped on a hung engine.
3097 *
3098 * Users of client default contexts do not rely on logical
3099 * state preserved between batches so it is safe to execute
3100 * queued requests following the hang. Non default contexts
3101 * rely on preserved state, so skipping a batch loses the
3102 * evolution of the state and it needs to be considered corrupted.
3103 * Executing more queued batches on top of corrupted state is
3104 * risky. But we take the risk by trying to advance through
3105 * the queued requests in order to make the client behaviour
3106 * more predictable around resets, by not throwing away random
3107 * amount of batches it has prepared for execution. Sophisticated
3108 * clients can use gem_reset_stats_ioctl and dma fence status
3109 * (exported via sync_file info ioctl on explicit fences) to observe
3110 * when it loses the context state and should rebuild accordingly.
3111 *
3112 * The context ban, and ultimately the client ban, mechanism are safety
3113 * valves if client submission ends up resulting in nothing more than
3114 * subsequent hangs.
3115 */
3116
Chris Wilsonbba08692018-04-06 23:03:53 +01003117 if (i915_request_completed(request)) {
3118 GEM_TRACE("%s pardoned global=%d (fence %llx:%d), current %d\n",
3119 engine->name, request->global_seqno,
3120 request->fence.context, request->fence.seqno,
3121 intel_engine_get_seqno(engine));
3122 stalled = false;
3123 }
3124
3125 if (stalled) {
Chris Wilson4e0d64d2018-05-17 22:26:30 +01003126 i915_gem_context_mark_guilty(request->gem_context);
Mika Kuoppala61da5362017-01-17 17:59:05 +02003127 skip_request(request);
Chris Wilsond1d1ebf42017-07-21 13:32:33 +01003128
3129 /* If this context is now banned, skip all pending requests. */
Chris Wilson4e0d64d2018-05-17 22:26:30 +01003130 if (i915_gem_context_is_banned(request->gem_context))
Chris Wilsond1d1ebf42017-07-21 13:32:33 +01003131 engine_skip_context(request);
Mika Kuoppala61da5362017-01-17 17:59:05 +02003132 } else {
Chris Wilsond1d1ebf42017-07-21 13:32:33 +01003133 /*
3134 * Since this is not the hung engine, it may have advanced
3135 * since the hang declaration. Double check by refinding
3136 * the active request at the time of the reset.
3137 */
3138 request = i915_gem_find_active_request(engine);
3139 if (request) {
Chris Wilson4e0d64d2018-05-17 22:26:30 +01003140 i915_gem_context_mark_innocent(request->gem_context);
Chris Wilsond1d1ebf42017-07-21 13:32:33 +01003141 dma_fence_set_error(&request->fence, -EAGAIN);
3142
3143 /* Rewind the engine to replay the incomplete rq */
Chris Wilsona89d1f92018-05-02 17:38:39 +01003144 spin_lock_irq(&engine->timeline.lock);
Chris Wilsond1d1ebf42017-07-21 13:32:33 +01003145 request = list_prev_entry(request, link);
Chris Wilsona89d1f92018-05-02 17:38:39 +01003146 if (&request->link == &engine->timeline.requests)
Chris Wilsond1d1ebf42017-07-21 13:32:33 +01003147 request = NULL;
Chris Wilsona89d1f92018-05-02 17:38:39 +01003148 spin_unlock_irq(&engine->timeline.lock);
Chris Wilsond1d1ebf42017-07-21 13:32:33 +01003149 }
Mika Kuoppala61da5362017-01-17 17:59:05 +02003150 }
3151
Chris Wilsond1d1ebf42017-07-21 13:32:33 +01003152 return request;
Mika Kuoppala61da5362017-01-17 17:59:05 +02003153}
3154
Michel Thierrya1ef70e2017-06-20 10:57:47 +01003155void i915_gem_reset_engine(struct intel_engine_cs *engine,
Chris Wilsonbba08692018-04-06 23:03:53 +01003156 struct i915_request *request,
3157 bool stalled)
Chris Wilson4db080f2013-12-04 11:37:09 +00003158{
Chris Wilsonfcb1de52017-12-19 09:01:10 +00003159 /*
3160 * Make sure this write is visible before we re-enable the interrupt
3161 * handlers on another CPU, as tasklet_enable() resolves to just
3162 * a compiler barrier which is insufficient for our purpose here.
3163 */
3164 smp_store_mb(engine->irq_posted, 0);
Chris Wilsoned454f22017-07-21 13:32:29 +01003165
Chris Wilsond1d1ebf42017-07-21 13:32:33 +01003166 if (request)
Chris Wilsonbba08692018-04-06 23:03:53 +01003167 request = i915_gem_reset_request(engine, request, stalled);
Chris Wilsond1d1ebf42017-07-21 13:32:33 +01003168
Chris Wilson821ed7d2016-09-09 14:11:53 +01003169 /* Setup the CS to resume from the breadcrumb of the hung request */
Chris Wilson5adfb772018-05-16 19:33:51 +01003170 engine->reset.reset(engine, request);
Chris Wilson821ed7d2016-09-09 14:11:53 +01003171}
3172
Chris Wilsond0667e92018-04-06 23:03:54 +01003173void i915_gem_reset(struct drm_i915_private *dev_priv,
3174 unsigned int stalled_mask)
Chris Wilson821ed7d2016-09-09 14:11:53 +01003175{
3176 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05303177 enum intel_engine_id id;
Chris Wilson821ed7d2016-09-09 14:11:53 +01003178
Chris Wilson4c7d62c2016-10-28 13:58:32 +01003179 lockdep_assert_held(&dev_priv->drm.struct_mutex);
3180
Chris Wilsone61e0f52018-02-21 09:56:36 +00003181 i915_retire_requests(dev_priv);
Chris Wilson821ed7d2016-09-09 14:11:53 +01003182
Chris Wilson2ae55732017-02-12 17:20:02 +00003183 for_each_engine(engine, dev_priv, id) {
Chris Wilson1fc44d92018-05-17 22:26:32 +01003184 struct intel_context *ce;
Chris Wilson2ae55732017-02-12 17:20:02 +00003185
Chris Wilsonbba08692018-04-06 23:03:53 +01003186 i915_gem_reset_engine(engine,
3187 engine->hangcheck.active_request,
Chris Wilsond0667e92018-04-06 23:03:54 +01003188 stalled_mask & ENGINE_MASK(id));
Chris Wilson1fc44d92018-05-17 22:26:32 +01003189 ce = fetch_and_zero(&engine->last_retired_context);
3190 if (ce)
3191 intel_context_unpin(ce);
Chris Wilson7b6da812017-12-16 00:03:34 +00003192
3193 /*
3194 * Ostensibily, we always want a context loaded for powersaving,
3195 * so if the engine is idle after the reset, send a request
3196 * to load our scratch kernel_context.
3197 *
3198 * More mysteriously, if we leave the engine idle after a reset,
3199 * the next userspace batch may hang, with what appears to be
3200 * an incoherent read by the CS (presumably stale TLB). An
3201 * empty request appears sufficient to paper over the glitch.
3202 */
Chris Wilson01b8fdc2018-02-05 15:24:31 +00003203 if (intel_engine_is_idle(engine)) {
Chris Wilsone61e0f52018-02-21 09:56:36 +00003204 struct i915_request *rq;
Chris Wilson7b6da812017-12-16 00:03:34 +00003205
Chris Wilsone61e0f52018-02-21 09:56:36 +00003206 rq = i915_request_alloc(engine,
3207 dev_priv->kernel_context);
Chris Wilson7b6da812017-12-16 00:03:34 +00003208 if (!IS_ERR(rq))
Chris Wilsone61e0f52018-02-21 09:56:36 +00003209 __i915_request_add(rq, false);
Chris Wilson7b6da812017-12-16 00:03:34 +00003210 }
Chris Wilson2ae55732017-02-12 17:20:02 +00003211 }
Chris Wilson821ed7d2016-09-09 14:11:53 +01003212
Tvrtko Ursulin4362f4f2016-11-16 08:55:33 +00003213 i915_gem_restore_fences(dev_priv);
Chris Wilson821ed7d2016-09-09 14:11:53 +01003214}
3215
Michel Thierrya1ef70e2017-06-20 10:57:47 +01003216void i915_gem_reset_finish_engine(struct intel_engine_cs *engine)
3217{
Chris Wilson5adfb772018-05-16 19:33:51 +01003218 engine->reset.finish(engine);
3219
Chris Wilson1749d902017-10-09 12:02:59 +01003220 intel_uncore_forcewake_put(engine->i915, FORCEWAKE_ALL);
Michel Thierrya1ef70e2017-06-20 10:57:47 +01003221}
3222
Chris Wilsond8027092017-02-08 14:30:32 +00003223void i915_gem_reset_finish(struct drm_i915_private *dev_priv)
3224{
Chris Wilson1f7b8472017-02-08 14:30:33 +00003225 struct intel_engine_cs *engine;
3226 enum intel_engine_id id;
3227
Chris Wilsond8027092017-02-08 14:30:32 +00003228 lockdep_assert_held(&dev_priv->drm.struct_mutex);
Chris Wilson1f7b8472017-02-08 14:30:33 +00003229
Chris Wilsonfe3288b2017-02-12 17:20:01 +00003230 for_each_engine(engine, dev_priv, id) {
Michel Thierryc64992e2017-06-20 10:57:44 +01003231 engine->hangcheck.active_request = NULL;
Michel Thierrya1ef70e2017-06-20 10:57:47 +01003232 i915_gem_reset_finish_engine(engine);
Chris Wilsonfe3288b2017-02-12 17:20:01 +00003233 }
Chris Wilsond8027092017-02-08 14:30:32 +00003234}
3235
Chris Wilsone61e0f52018-02-21 09:56:36 +00003236static void nop_submit_request(struct i915_request *request)
Chris Wilson821ed7d2016-09-09 14:11:53 +01003237{
Chris Wilsond9b13c42018-03-15 13:14:50 +00003238 GEM_TRACE("%s fence %llx:%d -> -EIO\n",
3239 request->engine->name,
3240 request->fence.context, request->fence.seqno);
Daniel Vetteraf7a8ff2017-10-11 11:10:19 +02003241 dma_fence_set_error(&request->fence, -EIO);
3242
Chris Wilsone61e0f52018-02-21 09:56:36 +00003243 i915_request_submit(request);
Daniel Vetteraf7a8ff2017-10-11 11:10:19 +02003244}
3245
Chris Wilsone61e0f52018-02-21 09:56:36 +00003246static void nop_complete_submit_request(struct i915_request *request)
Daniel Vetteraf7a8ff2017-10-11 11:10:19 +02003247{
Chris Wilson8d550822017-10-06 12:56:17 +01003248 unsigned long flags;
3249
Chris Wilsond9b13c42018-03-15 13:14:50 +00003250 GEM_TRACE("%s fence %llx:%d -> -EIO\n",
3251 request->engine->name,
3252 request->fence.context, request->fence.seqno);
Chris Wilson3cd94422017-01-10 17:22:45 +00003253 dma_fence_set_error(&request->fence, -EIO);
Chris Wilson8d550822017-10-06 12:56:17 +01003254
Chris Wilsona89d1f92018-05-02 17:38:39 +01003255 spin_lock_irqsave(&request->engine->timeline.lock, flags);
Chris Wilsone61e0f52018-02-21 09:56:36 +00003256 __i915_request_submit(request);
Chris Wilson3dcf93f72016-11-22 14:41:20 +00003257 intel_engine_init_global_seqno(request->engine, request->global_seqno);
Chris Wilsona89d1f92018-05-02 17:38:39 +01003258 spin_unlock_irqrestore(&request->engine->timeline.lock, flags);
Chris Wilson821ed7d2016-09-09 14:11:53 +01003259}
3260
Daniel Vetteraf7a8ff2017-10-11 11:10:19 +02003261void i915_gem_set_wedged(struct drm_i915_private *i915)
Chris Wilson821ed7d2016-09-09 14:11:53 +01003262{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00003263 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05303264 enum intel_engine_id id;
Eric Anholt673a3942008-07-30 12:06:12 -07003265
Chris Wilsond9b13c42018-03-15 13:14:50 +00003266 GEM_TRACE("start\n");
3267
Chris Wilson7f961d72018-04-26 11:32:19 +01003268 if (GEM_SHOW_DEBUG()) {
Chris Wilson559e0402018-02-05 09:21:59 +00003269 struct drm_printer p = drm_debug_printer(__func__);
3270
3271 for_each_engine(engine, i915, id)
3272 intel_engine_dump(engine, &p, "%s\n", engine->name);
3273 }
3274
Chris Wilson0d73e7a2018-02-07 15:13:50 +00003275 set_bit(I915_WEDGED, &i915->gpu_error.flags);
3276 smp_mb__after_atomic();
3277
Daniel Vetteraf7a8ff2017-10-11 11:10:19 +02003278 /*
3279 * First, stop submission to hw, but do not yet complete requests by
3280 * rolling the global seqno forward (since this would complete requests
3281 * for which we haven't set the fence error to EIO yet).
3282 */
Chris Wilson963ddd62018-03-02 11:33:24 +00003283 for_each_engine(engine, i915, id) {
3284 i915_gem_reset_prepare_engine(engine);
Chris Wilson47650db2018-03-07 13:42:25 +00003285
Daniel Vetteraf7a8ff2017-10-11 11:10:19 +02003286 engine->submit_request = nop_submit_request;
Chris Wilson47650db2018-03-07 13:42:25 +00003287 engine->schedule = NULL;
Chris Wilson963ddd62018-03-02 11:33:24 +00003288 }
Chris Wilson47650db2018-03-07 13:42:25 +00003289 i915->caps.scheduler = 0;
Daniel Vetteraf7a8ff2017-10-11 11:10:19 +02003290
Chris Wilsonac697ae2018-03-15 15:10:15 +00003291 /* Even if the GPU reset fails, it should still stop the engines */
3292 intel_gpu_reset(i915, ALL_ENGINES);
3293
Daniel Vetteraf7a8ff2017-10-11 11:10:19 +02003294 /*
3295 * Make sure no one is running the old callback before we proceed with
3296 * cancelling requests and resetting the completion tracking. Otherwise
3297 * we might submit a request to the hardware which never completes.
3298 */
3299 synchronize_rcu();
3300
3301 for_each_engine(engine, i915, id) {
3302 /* Mark all executing requests as skipped */
3303 engine->cancel_requests(engine);
3304
3305 /*
3306 * Only once we've force-cancelled all in-flight requests can we
3307 * start to complete all requests.
3308 */
3309 engine->submit_request = nop_complete_submit_request;
3310 }
3311
3312 /*
3313 * Make sure no request can slip through without getting completed by
3314 * either this call here to intel_engine_init_global_seqno, or the one
3315 * in nop_complete_submit_request.
3316 */
3317 synchronize_rcu();
3318
3319 for_each_engine(engine, i915, id) {
3320 unsigned long flags;
3321
Chris Wilson0d73e7a2018-02-07 15:13:50 +00003322 /*
3323 * Mark all pending requests as complete so that any concurrent
Daniel Vetteraf7a8ff2017-10-11 11:10:19 +02003324 * (lockless) lookup doesn't try and wait upon the request as we
3325 * reset it.
3326 */
Chris Wilsona89d1f92018-05-02 17:38:39 +01003327 spin_lock_irqsave(&engine->timeline.lock, flags);
Daniel Vetteraf7a8ff2017-10-11 11:10:19 +02003328 intel_engine_init_global_seqno(engine,
3329 intel_engine_last_submit(engine));
Chris Wilsona89d1f92018-05-02 17:38:39 +01003330 spin_unlock_irqrestore(&engine->timeline.lock, flags);
Chris Wilson963ddd62018-03-02 11:33:24 +00003331
3332 i915_gem_reset_finish_engine(engine);
Daniel Vetteraf7a8ff2017-10-11 11:10:19 +02003333 }
Chris Wilson20e49332016-11-22 14:41:21 +00003334
Chris Wilsond9b13c42018-03-15 13:14:50 +00003335 GEM_TRACE("end\n");
3336
Chris Wilson3d7adbb2017-07-21 13:32:27 +01003337 wake_up_all(&i915->gpu_error.reset_queue);
Eric Anholt673a3942008-07-30 12:06:12 -07003338}
3339
Chris Wilson2e8f9d32017-03-16 17:13:04 +00003340bool i915_gem_unset_wedged(struct drm_i915_private *i915)
3341{
Chris Wilsona89d1f92018-05-02 17:38:39 +01003342 struct i915_timeline *tl;
Chris Wilson2e8f9d32017-03-16 17:13:04 +00003343
3344 lockdep_assert_held(&i915->drm.struct_mutex);
3345 if (!test_bit(I915_WEDGED, &i915->gpu_error.flags))
3346 return true;
3347
Chris Wilsond9b13c42018-03-15 13:14:50 +00003348 GEM_TRACE("start\n");
3349
Chris Wilson2d4ecac2018-03-07 13:42:21 +00003350 /*
3351 * Before unwedging, make sure that all pending operations
Chris Wilson2e8f9d32017-03-16 17:13:04 +00003352 * are flushed and errored out - we may have requests waiting upon
3353 * third party fences. We marked all inflight requests as EIO, and
3354 * every execbuf since returned EIO, for consistency we want all
3355 * the currently pending requests to also be marked as EIO, which
3356 * is done inside our nop_submit_request - and so we must wait.
3357 *
3358 * No more can be submitted until we reset the wedged bit.
3359 */
3360 list_for_each_entry(tl, &i915->gt.timelines, link) {
Chris Wilsona89d1f92018-05-02 17:38:39 +01003361 struct i915_request *rq;
Chris Wilson2e8f9d32017-03-16 17:13:04 +00003362
Chris Wilsona89d1f92018-05-02 17:38:39 +01003363 rq = i915_gem_active_peek(&tl->last_request,
3364 &i915->drm.struct_mutex);
3365 if (!rq)
3366 continue;
Chris Wilson2e8f9d32017-03-16 17:13:04 +00003367
Chris Wilsona89d1f92018-05-02 17:38:39 +01003368 /*
3369 * We can't use our normal waiter as we want to
3370 * avoid recursively trying to handle the current
3371 * reset. The basic dma_fence_default_wait() installs
3372 * a callback for dma_fence_signal(), which is
3373 * triggered by our nop handler (indirectly, the
3374 * callback enables the signaler thread which is
3375 * woken by the nop_submit_request() advancing the seqno
3376 * and when the seqno passes the fence, the signaler
3377 * then signals the fence waking us up).
3378 */
3379 if (dma_fence_default_wait(&rq->fence, true,
3380 MAX_SCHEDULE_TIMEOUT) < 0)
3381 return false;
Chris Wilson2e8f9d32017-03-16 17:13:04 +00003382 }
Chris Wilson2d4ecac2018-03-07 13:42:21 +00003383 i915_retire_requests(i915);
3384 GEM_BUG_ON(i915->gt.active_requests);
Chris Wilson2e8f9d32017-03-16 17:13:04 +00003385
Chris Wilson2d4ecac2018-03-07 13:42:21 +00003386 /*
3387 * Undo nop_submit_request. We prevent all new i915 requests from
Chris Wilson2e8f9d32017-03-16 17:13:04 +00003388 * being queued (by disallowing execbuf whilst wedged) so having
3389 * waited for all active requests above, we know the system is idle
3390 * and do not have to worry about a thread being inside
3391 * engine->submit_request() as we swap over. So unlike installing
3392 * the nop_submit_request on reset, we can do this from normal
3393 * context and do not require stop_machine().
3394 */
3395 intel_engines_reset_default_submission(i915);
Chris Wilson36703e72017-06-22 11:56:25 +01003396 i915_gem_contexts_lost(i915);
Chris Wilson2e8f9d32017-03-16 17:13:04 +00003397
Chris Wilsond9b13c42018-03-15 13:14:50 +00003398 GEM_TRACE("end\n");
3399
Chris Wilson2e8f9d32017-03-16 17:13:04 +00003400 smp_mb__before_atomic(); /* complete takeover before enabling execbuf */
3401 clear_bit(I915_WEDGED, &i915->gpu_error.flags);
3402
3403 return true;
3404}
3405
Daniel Vetter75ef9da2010-08-21 00:25:16 +02003406static void
Eric Anholt673a3942008-07-30 12:06:12 -07003407i915_gem_retire_work_handler(struct work_struct *work)
3408{
Chris Wilsonb29c19b2013-09-25 17:34:56 +01003409 struct drm_i915_private *dev_priv =
Chris Wilson67d97da2016-07-04 08:08:31 +01003410 container_of(work, typeof(*dev_priv), gt.retire_work.work);
Chris Wilson91c8a322016-07-05 10:40:23 +01003411 struct drm_device *dev = &dev_priv->drm;
Eric Anholt673a3942008-07-30 12:06:12 -07003412
Chris Wilson891b48c2010-09-29 12:26:37 +01003413 /* Come back later if the device is busy... */
Chris Wilsonb29c19b2013-09-25 17:34:56 +01003414 if (mutex_trylock(&dev->struct_mutex)) {
Chris Wilsone61e0f52018-02-21 09:56:36 +00003415 i915_retire_requests(dev_priv);
Chris Wilsonb29c19b2013-09-25 17:34:56 +01003416 mutex_unlock(&dev->struct_mutex);
3417 }
Chris Wilson67d97da2016-07-04 08:08:31 +01003418
Chris Wilson88923042018-01-29 14:41:04 +00003419 /*
3420 * Keep the retire handler running until we are finally idle.
Chris Wilson67d97da2016-07-04 08:08:31 +01003421 * We do not need to do this test under locking as in the worst-case
3422 * we queue the retire worker once too often.
3423 */
Chris Wilson88923042018-01-29 14:41:04 +00003424 if (READ_ONCE(dev_priv->gt.awake))
Chris Wilson67d97da2016-07-04 08:08:31 +01003425 queue_delayed_work(dev_priv->wq,
3426 &dev_priv->gt.retire_work,
Chris Wilsonbcb45082012-10-05 17:02:57 +01003427 round_jiffies_up_relative(HZ));
Chris Wilsonb29c19b2013-09-25 17:34:56 +01003428}
Chris Wilson891b48c2010-09-29 12:26:37 +01003429
Chris Wilson84a10742018-01-24 11:36:08 +00003430static void shrink_caches(struct drm_i915_private *i915)
3431{
3432 /*
3433 * kmem_cache_shrink() discards empty slabs and reorders partially
3434 * filled slabs to prioritise allocating from the mostly full slabs,
3435 * with the aim of reducing fragmentation.
3436 */
3437 kmem_cache_shrink(i915->priorities);
3438 kmem_cache_shrink(i915->dependencies);
3439 kmem_cache_shrink(i915->requests);
3440 kmem_cache_shrink(i915->luts);
3441 kmem_cache_shrink(i915->vmas);
3442 kmem_cache_shrink(i915->objects);
3443}
3444
3445struct sleep_rcu_work {
3446 union {
3447 struct rcu_head rcu;
3448 struct work_struct work;
3449 };
3450 struct drm_i915_private *i915;
3451 unsigned int epoch;
3452};
3453
3454static inline bool
3455same_epoch(struct drm_i915_private *i915, unsigned int epoch)
3456{
3457 /*
3458 * There is a small chance that the epoch wrapped since we started
3459 * sleeping. If we assume that epoch is at least a u32, then it will
3460 * take at least 2^32 * 100ms for it to wrap, or about 326 years.
3461 */
3462 return epoch == READ_ONCE(i915->gt.epoch);
3463}
3464
3465static void __sleep_work(struct work_struct *work)
3466{
3467 struct sleep_rcu_work *s = container_of(work, typeof(*s), work);
3468 struct drm_i915_private *i915 = s->i915;
3469 unsigned int epoch = s->epoch;
3470
3471 kfree(s);
3472 if (same_epoch(i915, epoch))
3473 shrink_caches(i915);
3474}
3475
3476static void __sleep_rcu(struct rcu_head *rcu)
3477{
3478 struct sleep_rcu_work *s = container_of(rcu, typeof(*s), rcu);
3479 struct drm_i915_private *i915 = s->i915;
3480
3481 if (same_epoch(i915, s->epoch)) {
3482 INIT_WORK(&s->work, __sleep_work);
3483 queue_work(i915->wq, &s->work);
3484 } else {
3485 kfree(s);
3486 }
3487}
3488
Chris Wilson5427f202017-10-23 22:32:34 +01003489static inline bool
3490new_requests_since_last_retire(const struct drm_i915_private *i915)
3491{
3492 return (READ_ONCE(i915->gt.active_requests) ||
3493 work_pending(&i915->gt.idle_work.work));
3494}
3495
Chris Wilsonb29c19b2013-09-25 17:34:56 +01003496static void
3497i915_gem_idle_work_handler(struct work_struct *work)
3498{
3499 struct drm_i915_private *dev_priv =
Chris Wilson67d97da2016-07-04 08:08:31 +01003500 container_of(work, typeof(*dev_priv), gt.idle_work.work);
Chris Wilson84a10742018-01-24 11:36:08 +00003501 unsigned int epoch = I915_EPOCH_INVALID;
Chris Wilson67d97da2016-07-04 08:08:31 +01003502 bool rearm_hangcheck;
3503
3504 if (!READ_ONCE(dev_priv->gt.awake))
3505 return;
3506
Imre Deak0cb56702016-11-07 11:20:04 +02003507 /*
3508 * Wait for last execlists context complete, but bail out in case a
Chris Wilsonffed7bd2018-03-01 10:33:38 +00003509 * new request is submitted. As we don't trust the hardware, we
3510 * continue on if the wait times out. This is necessary to allow
3511 * the machine to suspend even if the hardware dies, and we will
3512 * try to recover in resume (after depriving the hardware of power,
3513 * it may be in a better mmod).
Imre Deak0cb56702016-11-07 11:20:04 +02003514 */
Chris Wilsonffed7bd2018-03-01 10:33:38 +00003515 __wait_for(if (new_requests_since_last_retire(dev_priv)) return,
3516 intel_engines_are_idle(dev_priv),
3517 I915_IDLE_ENGINES_TIMEOUT * 1000,
3518 10, 500);
Chris Wilson67d97da2016-07-04 08:08:31 +01003519
3520 rearm_hangcheck =
3521 cancel_delayed_work_sync(&dev_priv->gpu_error.hangcheck_work);
3522
Chris Wilson5427f202017-10-23 22:32:34 +01003523 if (!mutex_trylock(&dev_priv->drm.struct_mutex)) {
Chris Wilson67d97da2016-07-04 08:08:31 +01003524 /* Currently busy, come back later */
3525 mod_delayed_work(dev_priv->wq,
3526 &dev_priv->gt.idle_work,
3527 msecs_to_jiffies(50));
3528 goto out_rearm;
3529 }
3530
Imre Deak93c97dc2016-11-07 11:20:03 +02003531 /*
3532 * New request retired after this work handler started, extend active
3533 * period until next instance of the work.
3534 */
Chris Wilson5427f202017-10-23 22:32:34 +01003535 if (new_requests_since_last_retire(dev_priv))
Imre Deak93c97dc2016-11-07 11:20:03 +02003536 goto out_unlock;
3537
Chris Wilsone4d20062018-04-06 16:51:44 +01003538 epoch = __i915_gem_park(dev_priv);
Chris Wilsonff320d62017-10-23 22:32:35 +01003539
Chris Wilson67d97da2016-07-04 08:08:31 +01003540 rearm_hangcheck = false;
Chris Wilson67d97da2016-07-04 08:08:31 +01003541out_unlock:
Chris Wilson5427f202017-10-23 22:32:34 +01003542 mutex_unlock(&dev_priv->drm.struct_mutex);
Chris Wilson35c94182015-04-07 16:20:37 +01003543
Chris Wilson67d97da2016-07-04 08:08:31 +01003544out_rearm:
3545 if (rearm_hangcheck) {
3546 GEM_BUG_ON(!dev_priv->gt.awake);
3547 i915_queue_hangcheck(dev_priv);
Chris Wilson35c94182015-04-07 16:20:37 +01003548 }
Chris Wilson84a10742018-01-24 11:36:08 +00003549
3550 /*
3551 * When we are idle, it is an opportune time to reap our caches.
3552 * However, we have many objects that utilise RCU and the ordered
3553 * i915->wq that this work is executing on. To try and flush any
3554 * pending frees now we are idle, we first wait for an RCU grace
3555 * period, and then queue a task (that will run last on the wq) to
3556 * shrink and re-optimize the caches.
3557 */
3558 if (same_epoch(dev_priv, epoch)) {
3559 struct sleep_rcu_work *s = kmalloc(sizeof(*s), GFP_KERNEL);
3560 if (s) {
3561 s->i915 = dev_priv;
3562 s->epoch = epoch;
3563 call_rcu(&s->rcu, __sleep_rcu);
3564 }
3565 }
Eric Anholt673a3942008-07-30 12:06:12 -07003566}
3567
Chris Wilsonb1f788c2016-08-04 07:52:45 +01003568void i915_gem_close_object(struct drm_gem_object *gem, struct drm_file *file)
3569{
Chris Wilsond1b48c12017-08-16 09:52:08 +01003570 struct drm_i915_private *i915 = to_i915(gem->dev);
Chris Wilsonb1f788c2016-08-04 07:52:45 +01003571 struct drm_i915_gem_object *obj = to_intel_bo(gem);
3572 struct drm_i915_file_private *fpriv = file->driver_priv;
Chris Wilsond1b48c12017-08-16 09:52:08 +01003573 struct i915_lut_handle *lut, *ln;
Chris Wilsonb1f788c2016-08-04 07:52:45 +01003574
Chris Wilsond1b48c12017-08-16 09:52:08 +01003575 mutex_lock(&i915->drm.struct_mutex);
3576
3577 list_for_each_entry_safe(lut, ln, &obj->lut_list, obj_link) {
3578 struct i915_gem_context *ctx = lut->ctx;
3579 struct i915_vma *vma;
3580
Chris Wilson432295d2017-08-22 12:05:15 +01003581 GEM_BUG_ON(ctx->file_priv == ERR_PTR(-EBADF));
Chris Wilsond1b48c12017-08-16 09:52:08 +01003582 if (ctx->file_priv != fpriv)
3583 continue;
3584
3585 vma = radix_tree_delete(&ctx->handles_vma, lut->handle);
Chris Wilson3ffff012017-08-22 12:05:17 +01003586 GEM_BUG_ON(vma->obj != obj);
3587
3588 /* We allow the process to have multiple handles to the same
3589 * vma, in the same fd namespace, by virtue of flink/open.
3590 */
3591 GEM_BUG_ON(!vma->open_count);
3592 if (!--vma->open_count && !i915_vma_is_ggtt(vma))
Chris Wilsonb1f788c2016-08-04 07:52:45 +01003593 i915_vma_close(vma);
Chris Wilsonf8a7fde2016-10-28 13:58:29 +01003594
Chris Wilsond1b48c12017-08-16 09:52:08 +01003595 list_del(&lut->obj_link);
3596 list_del(&lut->ctx_link);
Chris Wilson4ff4b442017-06-16 15:05:16 +01003597
Chris Wilsond1b48c12017-08-16 09:52:08 +01003598 kmem_cache_free(i915->luts, lut);
3599 __i915_gem_object_release_unless_active(obj);
Chris Wilsonf8a7fde2016-10-28 13:58:29 +01003600 }
Chris Wilsond1b48c12017-08-16 09:52:08 +01003601
3602 mutex_unlock(&i915->drm.struct_mutex);
Chris Wilsonb1f788c2016-08-04 07:52:45 +01003603}
3604
Chris Wilsone95433c2016-10-28 13:58:27 +01003605static unsigned long to_wait_timeout(s64 timeout_ns)
3606{
3607 if (timeout_ns < 0)
3608 return MAX_SCHEDULE_TIMEOUT;
3609
3610 if (timeout_ns == 0)
3611 return 0;
3612
3613 return nsecs_to_jiffies_timeout(timeout_ns);
3614}
3615
Ben Widawsky5816d642012-04-11 11:18:19 -07003616/**
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07003617 * i915_gem_wait_ioctl - implements DRM_IOCTL_I915_GEM_WAIT
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01003618 * @dev: drm device pointer
3619 * @data: ioctl data blob
3620 * @file: drm file pointer
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07003621 *
3622 * Returns 0 if successful, else an error is returned with the remaining time in
3623 * the timeout parameter.
3624 * -ETIME: object is still busy after timeout
3625 * -ERESTARTSYS: signal interrupted the wait
3626 * -ENONENT: object doesn't exist
3627 * Also possible, but rare:
Chris Wilsonb8050142017-08-11 11:57:31 +01003628 * -EAGAIN: incomplete, restart syscall
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07003629 * -ENOMEM: damn
3630 * -ENODEV: Internal IRQ fail
3631 * -E?: The add request failed
3632 *
3633 * The wait ioctl with a timeout of 0 reimplements the busy ioctl. With any
3634 * non-zero timeout parameter the wait ioctl will wait for the given number of
3635 * nanoseconds on an object becoming unbusy. Since the wait itself does so
3636 * without holding struct_mutex the object may become re-busied before this
3637 * function completes. A similar but shorter * race condition exists in the busy
3638 * ioctl
3639 */
3640int
3641i915_gem_wait_ioctl(struct drm_device *dev, void *data, struct drm_file *file)
3642{
3643 struct drm_i915_gem_wait *args = data;
3644 struct drm_i915_gem_object *obj;
Chris Wilsone95433c2016-10-28 13:58:27 +01003645 ktime_t start;
3646 long ret;
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07003647
Daniel Vetter11b5d512014-09-29 15:31:26 +02003648 if (args->flags != 0)
3649 return -EINVAL;
3650
Chris Wilson03ac0642016-07-20 13:31:51 +01003651 obj = i915_gem_object_lookup(file, args->bo_handle);
Chris Wilson033d5492016-08-05 10:14:17 +01003652 if (!obj)
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07003653 return -ENOENT;
Chris Wilson033d5492016-08-05 10:14:17 +01003654
Chris Wilsone95433c2016-10-28 13:58:27 +01003655 start = ktime_get();
3656
3657 ret = i915_gem_object_wait(obj,
3658 I915_WAIT_INTERRUPTIBLE | I915_WAIT_ALL,
3659 to_wait_timeout(args->timeout_ns),
3660 to_rps_client(file));
3661
3662 if (args->timeout_ns > 0) {
3663 args->timeout_ns -= ktime_to_ns(ktime_sub(ktime_get(), start));
3664 if (args->timeout_ns < 0)
3665 args->timeout_ns = 0;
Chris Wilsonc1d20612017-02-16 12:54:41 +00003666
3667 /*
3668 * Apparently ktime isn't accurate enough and occasionally has a
3669 * bit of mismatch in the jiffies<->nsecs<->ktime loop. So patch
3670 * things up to make the test happy. We allow up to 1 jiffy.
3671 *
3672 * This is a regression from the timespec->ktime conversion.
3673 */
3674 if (ret == -ETIME && !nsecs_to_jiffies(args->timeout_ns))
3675 args->timeout_ns = 0;
Chris Wilsonb8050142017-08-11 11:57:31 +01003676
3677 /* Asked to wait beyond the jiffie/scheduler precision? */
3678 if (ret == -ETIME && args->timeout_ns)
3679 ret = -EAGAIN;
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07003680 }
3681
Chris Wilsonf0cd5182016-10-28 13:58:43 +01003682 i915_gem_object_put(obj);
John Harrisonff865882014-11-24 18:49:28 +00003683 return ret;
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07003684}
3685
Chris Wilsona89d1f92018-05-02 17:38:39 +01003686static int wait_for_timeline(struct i915_timeline *tl, unsigned int flags)
Daniel Vetter4df2faf2010-02-19 11:52:00 +01003687{
Chris Wilsona89d1f92018-05-02 17:38:39 +01003688 return i915_gem_active_wait(&tl->last_request, flags);
Chris Wilson73cb9702016-10-28 13:58:46 +01003689}
3690
Chris Wilson25112b62017-03-30 15:50:39 +01003691static int wait_for_engines(struct drm_i915_private *i915)
3692{
Chris Wilsonee42c002017-12-11 19:41:34 +00003693 if (wait_for(intel_engines_are_idle(i915), I915_IDLE_ENGINES_TIMEOUT)) {
Chris Wilson59e4b192017-12-11 19:41:35 +00003694 dev_err(i915->drm.dev,
3695 "Failed to idle engines, declaring wedged!\n");
Chris Wilson629820f2018-03-09 10:11:14 +00003696 GEM_TRACE_DUMP();
Chris Wilsoncad99462017-08-26 12:09:33 +01003697 i915_gem_set_wedged(i915);
3698 return -EIO;
Chris Wilson25112b62017-03-30 15:50:39 +01003699 }
3700
3701 return 0;
3702}
3703
Chris Wilson73cb9702016-10-28 13:58:46 +01003704int i915_gem_wait_for_idle(struct drm_i915_private *i915, unsigned int flags)
3705{
Chris Wilson09a4c022018-05-24 09:11:35 +01003706 GEM_TRACE("flags=%x (%s)\n",
3707 flags, flags & I915_WAIT_LOCKED ? "locked" : "unlocked");
3708
Chris Wilson863e9fd2017-05-30 13:13:32 +01003709 /* If the device is asleep, we have no requests outstanding */
3710 if (!READ_ONCE(i915->gt.awake))
3711 return 0;
3712
Chris Wilson9caa34a2016-11-11 14:58:08 +00003713 if (flags & I915_WAIT_LOCKED) {
Chris Wilsona89d1f92018-05-02 17:38:39 +01003714 struct i915_timeline *tl;
3715 int err;
Chris Wilson9caa34a2016-11-11 14:58:08 +00003716
3717 lockdep_assert_held(&i915->drm.struct_mutex);
3718
3719 list_for_each_entry(tl, &i915->gt.timelines, link) {
Chris Wilsona89d1f92018-05-02 17:38:39 +01003720 err = wait_for_timeline(tl, flags);
3721 if (err)
3722 return err;
Chris Wilson9caa34a2016-11-11 14:58:08 +00003723 }
Chris Wilsone61e0f52018-02-21 09:56:36 +00003724 i915_retire_requests(i915);
Chris Wilson09a4c022018-05-24 09:11:35 +01003725 GEM_BUG_ON(i915->gt.active_requests);
Chris Wilson25112b62017-03-30 15:50:39 +01003726
Chris Wilsona89d1f92018-05-02 17:38:39 +01003727 return wait_for_engines(i915);
Chris Wilson9caa34a2016-11-11 14:58:08 +00003728 } else {
Chris Wilsona89d1f92018-05-02 17:38:39 +01003729 struct intel_engine_cs *engine;
3730 enum intel_engine_id id;
3731 int err;
Zou Nan haid1b851f2010-05-21 09:08:57 +08003732
Chris Wilsona89d1f92018-05-02 17:38:39 +01003733 for_each_engine(engine, i915, id) {
3734 err = wait_for_timeline(&engine->timeline, flags);
3735 if (err)
3736 return err;
3737 }
3738
3739 return 0;
3740 }
Daniel Vetter4df2faf2010-02-19 11:52:00 +01003741}
3742
Chris Wilson5a97bcc2017-02-22 11:40:46 +00003743static void __i915_gem_object_flush_for_display(struct drm_i915_gem_object *obj)
3744{
Chris Wilsone27ab732017-06-15 13:38:49 +01003745 /*
3746 * We manually flush the CPU domain so that we can override and
3747 * force the flush for the display, and perform it asyncrhonously.
3748 */
3749 flush_write_domain(obj, ~I915_GEM_DOMAIN_CPU);
3750 if (obj->cache_dirty)
3751 i915_gem_clflush_object(obj, I915_CLFLUSH_FORCE);
Christian Königc0a51fd2018-02-16 13:43:38 +01003752 obj->write_domain = 0;
Chris Wilson5a97bcc2017-02-22 11:40:46 +00003753}
3754
3755void i915_gem_object_flush_if_display(struct drm_i915_gem_object *obj)
3756{
Chris Wilsonbd3d2252017-10-13 21:26:14 +01003757 if (!READ_ONCE(obj->pin_global))
Chris Wilson5a97bcc2017-02-22 11:40:46 +00003758 return;
3759
3760 mutex_lock(&obj->base.dev->struct_mutex);
3761 __i915_gem_object_flush_for_display(obj);
3762 mutex_unlock(&obj->base.dev->struct_mutex);
3763}
3764
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003765/**
Chris Wilsone22d8e32017-04-12 12:01:11 +01003766 * Moves a single object to the WC read, and possibly write domain.
3767 * @obj: object to act on
3768 * @write: ask for write access or read only
3769 *
3770 * This function returns when the move is complete, including waiting on
3771 * flushes to occur.
3772 */
3773int
3774i915_gem_object_set_to_wc_domain(struct drm_i915_gem_object *obj, bool write)
3775{
3776 int ret;
3777
3778 lockdep_assert_held(&obj->base.dev->struct_mutex);
3779
3780 ret = i915_gem_object_wait(obj,
3781 I915_WAIT_INTERRUPTIBLE |
3782 I915_WAIT_LOCKED |
3783 (write ? I915_WAIT_ALL : 0),
3784 MAX_SCHEDULE_TIMEOUT,
3785 NULL);
3786 if (ret)
3787 return ret;
3788
Christian Königc0a51fd2018-02-16 13:43:38 +01003789 if (obj->write_domain == I915_GEM_DOMAIN_WC)
Chris Wilsone22d8e32017-04-12 12:01:11 +01003790 return 0;
3791
3792 /* Flush and acquire obj->pages so that we are coherent through
3793 * direct access in memory with previous cached writes through
3794 * shmemfs and that our cache domain tracking remains valid.
3795 * For example, if the obj->filp was moved to swap without us
3796 * being notified and releasing the pages, we would mistakenly
3797 * continue to assume that the obj remained out of the CPU cached
3798 * domain.
3799 */
3800 ret = i915_gem_object_pin_pages(obj);
3801 if (ret)
3802 return ret;
3803
3804 flush_write_domain(obj, ~I915_GEM_DOMAIN_WC);
3805
3806 /* Serialise direct access to this object with the barriers for
3807 * coherent writes from the GPU, by effectively invalidating the
3808 * WC domain upon first access.
3809 */
Christian Königc0a51fd2018-02-16 13:43:38 +01003810 if ((obj->read_domains & I915_GEM_DOMAIN_WC) == 0)
Chris Wilsone22d8e32017-04-12 12:01:11 +01003811 mb();
3812
3813 /* It should now be out of any other write domains, and we can update
3814 * the domain values for our changes.
3815 */
Christian Königc0a51fd2018-02-16 13:43:38 +01003816 GEM_BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_WC) != 0);
3817 obj->read_domains |= I915_GEM_DOMAIN_WC;
Chris Wilsone22d8e32017-04-12 12:01:11 +01003818 if (write) {
Christian Königc0a51fd2018-02-16 13:43:38 +01003819 obj->read_domains = I915_GEM_DOMAIN_WC;
3820 obj->write_domain = I915_GEM_DOMAIN_WC;
Chris Wilsone22d8e32017-04-12 12:01:11 +01003821 obj->mm.dirty = true;
3822 }
3823
3824 i915_gem_object_unpin_pages(obj);
3825 return 0;
3826}
3827
3828/**
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003829 * Moves a single object to the GTT read, and possibly write domain.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01003830 * @obj: object to act on
3831 * @write: ask for write access or read only
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003832 *
3833 * This function returns when the move is complete, including waiting on
3834 * flushes to occur.
3835 */
Jesse Barnes79e53942008-11-07 14:24:08 -08003836int
Chris Wilson20217462010-11-23 15:26:33 +00003837i915_gem_object_set_to_gtt_domain(struct drm_i915_gem_object *obj, bool write)
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003838{
Eric Anholte47c68e2008-11-14 13:35:19 -08003839 int ret;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003840
Chris Wilsone95433c2016-10-28 13:58:27 +01003841 lockdep_assert_held(&obj->base.dev->struct_mutex);
Chris Wilson4c7d62c2016-10-28 13:58:32 +01003842
Chris Wilsone95433c2016-10-28 13:58:27 +01003843 ret = i915_gem_object_wait(obj,
3844 I915_WAIT_INTERRUPTIBLE |
3845 I915_WAIT_LOCKED |
3846 (write ? I915_WAIT_ALL : 0),
3847 MAX_SCHEDULE_TIMEOUT,
3848 NULL);
Chris Wilson88241782011-01-07 17:09:48 +00003849 if (ret)
3850 return ret;
3851
Christian Königc0a51fd2018-02-16 13:43:38 +01003852 if (obj->write_domain == I915_GEM_DOMAIN_GTT)
Chris Wilsonc13d87e2016-07-20 09:21:15 +01003853 return 0;
3854
Chris Wilson43566de2015-01-02 16:29:29 +05303855 /* Flush and acquire obj->pages so that we are coherent through
3856 * direct access in memory with previous cached writes through
3857 * shmemfs and that our cache domain tracking remains valid.
3858 * For example, if the obj->filp was moved to swap without us
3859 * being notified and releasing the pages, we would mistakenly
3860 * continue to assume that the obj remained out of the CPU cached
3861 * domain.
3862 */
Chris Wilsona4f5ea62016-10-28 13:58:35 +01003863 ret = i915_gem_object_pin_pages(obj);
Chris Wilson43566de2015-01-02 16:29:29 +05303864 if (ret)
3865 return ret;
3866
Chris Wilsonef749212017-04-12 12:01:10 +01003867 flush_write_domain(obj, ~I915_GEM_DOMAIN_GTT);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003868
Chris Wilsond0a57782012-10-09 19:24:37 +01003869 /* Serialise direct access to this object with the barriers for
3870 * coherent writes from the GPU, by effectively invalidating the
3871 * GTT domain upon first access.
3872 */
Christian Königc0a51fd2018-02-16 13:43:38 +01003873 if ((obj->read_domains & I915_GEM_DOMAIN_GTT) == 0)
Chris Wilsond0a57782012-10-09 19:24:37 +01003874 mb();
3875
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003876 /* It should now be out of any other write domains, and we can update
3877 * the domain values for our changes.
3878 */
Christian Königc0a51fd2018-02-16 13:43:38 +01003879 GEM_BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_GTT) != 0);
3880 obj->read_domains |= I915_GEM_DOMAIN_GTT;
Eric Anholte47c68e2008-11-14 13:35:19 -08003881 if (write) {
Christian Königc0a51fd2018-02-16 13:43:38 +01003882 obj->read_domains = I915_GEM_DOMAIN_GTT;
3883 obj->write_domain = I915_GEM_DOMAIN_GTT;
Chris Wilsona4f5ea62016-10-28 13:58:35 +01003884 obj->mm.dirty = true;
Eric Anholte47c68e2008-11-14 13:35:19 -08003885 }
3886
Chris Wilsona4f5ea62016-10-28 13:58:35 +01003887 i915_gem_object_unpin_pages(obj);
Eric Anholte47c68e2008-11-14 13:35:19 -08003888 return 0;
3889}
3890
Chris Wilsonef55f922015-10-09 14:11:27 +01003891/**
3892 * Changes the cache-level of an object across all VMA.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01003893 * @obj: object to act on
3894 * @cache_level: new cache level to set for the object
Chris Wilsonef55f922015-10-09 14:11:27 +01003895 *
3896 * After this function returns, the object will be in the new cache-level
3897 * across all GTT and the contents of the backing storage will be coherent,
3898 * with respect to the new cache-level. In order to keep the backing storage
3899 * coherent for all users, we only allow a single cache level to be set
3900 * globally on the object and prevent it from being changed whilst the
3901 * hardware is reading from the object. That is if the object is currently
3902 * on the scanout it will be set to uncached (or equivalent display
3903 * cache coherency) and all non-MOCS GPU access will also be uncached so
3904 * that all direct access to the scanout remains coherent.
3905 */
Chris Wilsone4ffd172011-04-04 09:44:39 +01003906int i915_gem_object_set_cache_level(struct drm_i915_gem_object *obj,
3907 enum i915_cache_level cache_level)
3908{
Chris Wilsonaa653a62016-08-04 07:52:27 +01003909 struct i915_vma *vma;
Chris Wilsona6a7cc42016-11-18 21:17:46 +00003910 int ret;
Chris Wilsone4ffd172011-04-04 09:44:39 +01003911
Chris Wilson4c7d62c2016-10-28 13:58:32 +01003912 lockdep_assert_held(&obj->base.dev->struct_mutex);
3913
Chris Wilsone4ffd172011-04-04 09:44:39 +01003914 if (obj->cache_level == cache_level)
Chris Wilsona6a7cc42016-11-18 21:17:46 +00003915 return 0;
Chris Wilsone4ffd172011-04-04 09:44:39 +01003916
Chris Wilsonef55f922015-10-09 14:11:27 +01003917 /* Inspect the list of currently bound VMA and unbind any that would
3918 * be invalid given the new cache-level. This is principally to
3919 * catch the issue of the CS prefetch crossing page boundaries and
3920 * reading an invalid PTE on older architectures.
3921 */
Chris Wilsonaa653a62016-08-04 07:52:27 +01003922restart:
3923 list_for_each_entry(vma, &obj->vma_list, obj_link) {
Chris Wilsonef55f922015-10-09 14:11:27 +01003924 if (!drm_mm_node_allocated(&vma->node))
3925 continue;
3926
Chris Wilson20dfbde2016-08-04 16:32:30 +01003927 if (i915_vma_is_pinned(vma)) {
Chris Wilsonef55f922015-10-09 14:11:27 +01003928 DRM_DEBUG("can not change the cache level of pinned objects\n");
3929 return -EBUSY;
3930 }
3931
Chris Wilson010e3e62017-12-06 12:49:13 +00003932 if (!i915_vma_is_closed(vma) &&
3933 i915_gem_valid_gtt_space(vma, cache_level))
Chris Wilsonaa653a62016-08-04 07:52:27 +01003934 continue;
3935
3936 ret = i915_vma_unbind(vma);
3937 if (ret)
3938 return ret;
3939
3940 /* As unbinding may affect other elements in the
3941 * obj->vma_list (due to side-effects from retiring
3942 * an active vma), play safe and restart the iterator.
3943 */
3944 goto restart;
Chris Wilson42d6ab42012-07-26 11:49:32 +01003945 }
3946
Chris Wilsonef55f922015-10-09 14:11:27 +01003947 /* We can reuse the existing drm_mm nodes but need to change the
3948 * cache-level on the PTE. We could simply unbind them all and
3949 * rebind with the correct cache-level on next use. However since
3950 * we already have a valid slot, dma mapping, pages etc, we may as
3951 * rewrite the PTE in the belief that doing so tramples upon less
3952 * state and so involves less work.
3953 */
Chris Wilson15717de2016-08-04 07:52:26 +01003954 if (obj->bind_count) {
Chris Wilsonef55f922015-10-09 14:11:27 +01003955 /* Before we change the PTE, the GPU must not be accessing it.
3956 * If we wait upon the object, we know that all the bound
3957 * VMA are no longer active.
3958 */
Chris Wilsone95433c2016-10-28 13:58:27 +01003959 ret = i915_gem_object_wait(obj,
3960 I915_WAIT_INTERRUPTIBLE |
3961 I915_WAIT_LOCKED |
3962 I915_WAIT_ALL,
3963 MAX_SCHEDULE_TIMEOUT,
3964 NULL);
Chris Wilsone4ffd172011-04-04 09:44:39 +01003965 if (ret)
3966 return ret;
3967
Tvrtko Ursulin0031fb92016-11-04 14:42:44 +00003968 if (!HAS_LLC(to_i915(obj->base.dev)) &&
3969 cache_level != I915_CACHE_NONE) {
Chris Wilsonef55f922015-10-09 14:11:27 +01003970 /* Access to snoopable pages through the GTT is
3971 * incoherent and on some machines causes a hard
3972 * lockup. Relinquish the CPU mmaping to force
3973 * userspace to refault in the pages and we can
3974 * then double check if the GTT mapping is still
3975 * valid for that pointer access.
3976 */
3977 i915_gem_release_mmap(obj);
Chris Wilsone4ffd172011-04-04 09:44:39 +01003978
Chris Wilsonef55f922015-10-09 14:11:27 +01003979 /* As we no longer need a fence for GTT access,
3980 * we can relinquish it now (and so prevent having
3981 * to steal a fence from someone else on the next
3982 * fence request). Note GPU activity would have
3983 * dropped the fence as all snoopable access is
3984 * supposed to be linear.
3985 */
Chris Wilsone2189dd2017-12-07 21:14:07 +00003986 for_each_ggtt_vma(vma, obj) {
Chris Wilson49ef5292016-08-18 17:17:00 +01003987 ret = i915_vma_put_fence(vma);
3988 if (ret)
3989 return ret;
3990 }
Chris Wilsonef55f922015-10-09 14:11:27 +01003991 } else {
3992 /* We either have incoherent backing store and
3993 * so no GTT access or the architecture is fully
3994 * coherent. In such cases, existing GTT mmaps
3995 * ignore the cache bit in the PTE and we can
3996 * rewrite it without confusing the GPU or having
3997 * to force userspace to fault back in its mmaps.
3998 */
Chris Wilsone4ffd172011-04-04 09:44:39 +01003999 }
4000
Chris Wilson1c7f4bc2016-02-26 11:03:19 +00004001 list_for_each_entry(vma, &obj->vma_list, obj_link) {
Chris Wilsonef55f922015-10-09 14:11:27 +01004002 if (!drm_mm_node_allocated(&vma->node))
4003 continue;
4004
4005 ret = i915_vma_bind(vma, cache_level, PIN_UPDATE);
4006 if (ret)
4007 return ret;
4008 }
Chris Wilsone4ffd172011-04-04 09:44:39 +01004009 }
4010
Chris Wilson1c7f4bc2016-02-26 11:03:19 +00004011 list_for_each_entry(vma, &obj->vma_list, obj_link)
Chris Wilson2c225692013-08-09 12:26:45 +01004012 vma->node.color = cache_level;
Chris Wilsonb8f55be2017-08-11 12:11:16 +01004013 i915_gem_object_set_cache_coherency(obj, cache_level);
Chris Wilsone27ab732017-06-15 13:38:49 +01004014 obj->cache_dirty = true; /* Always invalidate stale cachelines */
Chris Wilson2c225692013-08-09 12:26:45 +01004015
Chris Wilsone4ffd172011-04-04 09:44:39 +01004016 return 0;
4017}
4018
Ben Widawsky199adf42012-09-21 17:01:20 -07004019int i915_gem_get_caching_ioctl(struct drm_device *dev, void *data,
4020 struct drm_file *file)
Chris Wilsone6994ae2012-07-10 10:27:08 +01004021{
Ben Widawsky199adf42012-09-21 17:01:20 -07004022 struct drm_i915_gem_caching *args = data;
Chris Wilsone6994ae2012-07-10 10:27:08 +01004023 struct drm_i915_gem_object *obj;
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004024 int err = 0;
Chris Wilsone6994ae2012-07-10 10:27:08 +01004025
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004026 rcu_read_lock();
4027 obj = i915_gem_object_lookup_rcu(file, args->handle);
4028 if (!obj) {
4029 err = -ENOENT;
4030 goto out;
4031 }
Chris Wilsone6994ae2012-07-10 10:27:08 +01004032
Chris Wilson651d7942013-08-08 14:41:10 +01004033 switch (obj->cache_level) {
4034 case I915_CACHE_LLC:
4035 case I915_CACHE_L3_LLC:
4036 args->caching = I915_CACHING_CACHED;
4037 break;
4038
Chris Wilson4257d3b2013-08-08 14:41:11 +01004039 case I915_CACHE_WT:
4040 args->caching = I915_CACHING_DISPLAY;
4041 break;
4042
Chris Wilson651d7942013-08-08 14:41:10 +01004043 default:
4044 args->caching = I915_CACHING_NONE;
4045 break;
4046 }
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004047out:
4048 rcu_read_unlock();
4049 return err;
Chris Wilsone6994ae2012-07-10 10:27:08 +01004050}
4051
Ben Widawsky199adf42012-09-21 17:01:20 -07004052int i915_gem_set_caching_ioctl(struct drm_device *dev, void *data,
4053 struct drm_file *file)
Chris Wilsone6994ae2012-07-10 10:27:08 +01004054{
Chris Wilson9c870d02016-10-24 13:42:15 +01004055 struct drm_i915_private *i915 = to_i915(dev);
Ben Widawsky199adf42012-09-21 17:01:20 -07004056 struct drm_i915_gem_caching *args = data;
Chris Wilsone6994ae2012-07-10 10:27:08 +01004057 struct drm_i915_gem_object *obj;
4058 enum i915_cache_level level;
Chris Wilsond65415d2017-01-19 08:22:10 +00004059 int ret = 0;
Chris Wilsone6994ae2012-07-10 10:27:08 +01004060
Ben Widawsky199adf42012-09-21 17:01:20 -07004061 switch (args->caching) {
4062 case I915_CACHING_NONE:
Chris Wilsone6994ae2012-07-10 10:27:08 +01004063 level = I915_CACHE_NONE;
4064 break;
Ben Widawsky199adf42012-09-21 17:01:20 -07004065 case I915_CACHING_CACHED:
Imre Deake5756c12015-08-14 18:43:30 +03004066 /*
4067 * Due to a HW issue on BXT A stepping, GPU stores via a
4068 * snooped mapping may leave stale data in a corresponding CPU
4069 * cacheline, whereas normally such cachelines would get
4070 * invalidated.
4071 */
Chris Wilson9c870d02016-10-24 13:42:15 +01004072 if (!HAS_LLC(i915) && !HAS_SNOOP(i915))
Imre Deake5756c12015-08-14 18:43:30 +03004073 return -ENODEV;
4074
Chris Wilsone6994ae2012-07-10 10:27:08 +01004075 level = I915_CACHE_LLC;
4076 break;
Chris Wilson4257d3b2013-08-08 14:41:11 +01004077 case I915_CACHING_DISPLAY:
Chris Wilson9c870d02016-10-24 13:42:15 +01004078 level = HAS_WT(i915) ? I915_CACHE_WT : I915_CACHE_NONE;
Chris Wilson4257d3b2013-08-08 14:41:11 +01004079 break;
Chris Wilsone6994ae2012-07-10 10:27:08 +01004080 default:
4081 return -EINVAL;
4082 }
4083
Chris Wilsond65415d2017-01-19 08:22:10 +00004084 obj = i915_gem_object_lookup(file, args->handle);
4085 if (!obj)
4086 return -ENOENT;
4087
Tina Zhanga03f3952017-11-14 10:25:13 +00004088 /*
4089 * The caching mode of proxy object is handled by its generator, and
4090 * not allowed to be changed by userspace.
4091 */
4092 if (i915_gem_object_is_proxy(obj)) {
4093 ret = -ENXIO;
4094 goto out;
4095 }
4096
Chris Wilsond65415d2017-01-19 08:22:10 +00004097 if (obj->cache_level == level)
4098 goto out;
4099
4100 ret = i915_gem_object_wait(obj,
4101 I915_WAIT_INTERRUPTIBLE,
4102 MAX_SCHEDULE_TIMEOUT,
4103 to_rps_client(file));
4104 if (ret)
4105 goto out;
4106
Ben Widawsky3bc29132012-09-26 16:15:20 -07004107 ret = i915_mutex_lock_interruptible(dev);
4108 if (ret)
Chris Wilsond65415d2017-01-19 08:22:10 +00004109 goto out;
Chris Wilsone6994ae2012-07-10 10:27:08 +01004110
4111 ret = i915_gem_object_set_cache_level(obj, level);
Chris Wilsone6994ae2012-07-10 10:27:08 +01004112 mutex_unlock(&dev->struct_mutex);
Chris Wilsond65415d2017-01-19 08:22:10 +00004113
4114out:
4115 i915_gem_object_put(obj);
Chris Wilsone6994ae2012-07-10 10:27:08 +01004116 return ret;
4117}
4118
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08004119/*
Dhinakaran Pandiyan07bcd992018-03-06 19:34:18 -08004120 * Prepare buffer for display plane (scanout, cursors, etc). Can be called from
4121 * an uninterruptible phase (modesetting) and allows any flushes to be pipelined
4122 * (for pageflips). We only flush the caches while preparing the buffer for
4123 * display, the callers are responsible for frontbuffer flush.
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08004124 */
Chris Wilson058d88c2016-08-15 10:49:06 +01004125struct i915_vma *
Chris Wilson2da3b9b2011-04-14 09:41:17 +01004126i915_gem_object_pin_to_display_plane(struct drm_i915_gem_object *obj,
4127 u32 alignment,
Chris Wilson59354852018-02-20 13:42:06 +00004128 const struct i915_ggtt_view *view,
4129 unsigned int flags)
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08004130{
Chris Wilson058d88c2016-08-15 10:49:06 +01004131 struct i915_vma *vma;
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08004132 int ret;
4133
Chris Wilson4c7d62c2016-10-28 13:58:32 +01004134 lockdep_assert_held(&obj->base.dev->struct_mutex);
4135
Chris Wilsonbd3d2252017-10-13 21:26:14 +01004136 /* Mark the global pin early so that we account for the
Chris Wilsoncc98b412013-08-09 12:25:09 +01004137 * display coherency whilst setting up the cache domains.
4138 */
Chris Wilsonbd3d2252017-10-13 21:26:14 +01004139 obj->pin_global++;
Chris Wilsoncc98b412013-08-09 12:25:09 +01004140
Eric Anholta7ef0642011-03-29 16:59:54 -07004141 /* The display engine is not coherent with the LLC cache on gen6. As
4142 * a result, we make sure that the pinning that is about to occur is
4143 * done with uncached PTEs. This is lowest common denominator for all
4144 * chipsets.
4145 *
4146 * However for gen6+, we could do better by using the GFDT bit instead
4147 * of uncaching, which would allow us to flush all the LLC-cached data
4148 * with that bit in the PTE to main memory with just one PIPE_CONTROL.
4149 */
Chris Wilson651d7942013-08-08 14:41:10 +01004150 ret = i915_gem_object_set_cache_level(obj,
Tvrtko Ursulin86527442016-10-13 11:03:00 +01004151 HAS_WT(to_i915(obj->base.dev)) ?
4152 I915_CACHE_WT : I915_CACHE_NONE);
Chris Wilson058d88c2016-08-15 10:49:06 +01004153 if (ret) {
4154 vma = ERR_PTR(ret);
Chris Wilsonbd3d2252017-10-13 21:26:14 +01004155 goto err_unpin_global;
Chris Wilson058d88c2016-08-15 10:49:06 +01004156 }
Eric Anholta7ef0642011-03-29 16:59:54 -07004157
Chris Wilson2da3b9b2011-04-14 09:41:17 +01004158 /* As the user may map the buffer once pinned in the display plane
4159 * (e.g. libkms for the bootup splash), we have to ensure that we
Chris Wilson2efb8132016-08-18 17:17:06 +01004160 * always use map_and_fenceable for all scanout buffers. However,
4161 * it may simply be too big to fit into mappable, in which case
4162 * put it anyway and hope that userspace can cope (but always first
4163 * try to preserve the existing ABI).
Chris Wilson2da3b9b2011-04-14 09:41:17 +01004164 */
Chris Wilson2efb8132016-08-18 17:17:06 +01004165 vma = ERR_PTR(-ENOSPC);
Chris Wilson59354852018-02-20 13:42:06 +00004166 if ((flags & PIN_MAPPABLE) == 0 &&
4167 (!view || view->type == I915_GGTT_VIEW_NORMAL))
Chris Wilson2efb8132016-08-18 17:17:06 +01004168 vma = i915_gem_object_ggtt_pin(obj, view, 0, alignment,
Chris Wilson59354852018-02-20 13:42:06 +00004169 flags |
4170 PIN_MAPPABLE |
4171 PIN_NONBLOCK);
4172 if (IS_ERR(vma))
Chris Wilson767a2222016-11-07 11:01:28 +00004173 vma = i915_gem_object_ggtt_pin(obj, view, 0, alignment, flags);
Chris Wilson058d88c2016-08-15 10:49:06 +01004174 if (IS_ERR(vma))
Chris Wilsonbd3d2252017-10-13 21:26:14 +01004175 goto err_unpin_global;
Chris Wilson2da3b9b2011-04-14 09:41:17 +01004176
Chris Wilsond8923dc2016-08-18 17:17:07 +01004177 vma->display_alignment = max_t(u64, vma->display_alignment, alignment);
4178
Chris Wilson5a97bcc2017-02-22 11:40:46 +00004179 __i915_gem_object_flush_for_display(obj);
Chris Wilsonb118c1e2010-05-27 13:18:14 +01004180
Chris Wilson2da3b9b2011-04-14 09:41:17 +01004181 /* It should now be out of any other write domains, and we can update
4182 * the domain values for our changes.
4183 */
Christian Königc0a51fd2018-02-16 13:43:38 +01004184 obj->read_domains |= I915_GEM_DOMAIN_GTT;
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08004185
Chris Wilson058d88c2016-08-15 10:49:06 +01004186 return vma;
Chris Wilsoncc98b412013-08-09 12:25:09 +01004187
Chris Wilsonbd3d2252017-10-13 21:26:14 +01004188err_unpin_global:
4189 obj->pin_global--;
Chris Wilson058d88c2016-08-15 10:49:06 +01004190 return vma;
Chris Wilsoncc98b412013-08-09 12:25:09 +01004191}
4192
4193void
Chris Wilson058d88c2016-08-15 10:49:06 +01004194i915_gem_object_unpin_from_display_plane(struct i915_vma *vma)
Chris Wilsoncc98b412013-08-09 12:25:09 +01004195{
Chris Wilson49d73912016-11-29 09:50:08 +00004196 lockdep_assert_held(&vma->vm->i915->drm.struct_mutex);
Chris Wilson4c7d62c2016-10-28 13:58:32 +01004197
Chris Wilsonbd3d2252017-10-13 21:26:14 +01004198 if (WARN_ON(vma->obj->pin_global == 0))
Tvrtko Ursulin8a0c39b2015-04-13 11:50:09 +01004199 return;
4200
Chris Wilsonbd3d2252017-10-13 21:26:14 +01004201 if (--vma->obj->pin_global == 0)
Chris Wilsonf51455d2017-01-10 14:47:34 +00004202 vma->display_alignment = I915_GTT_MIN_ALIGNMENT;
Tvrtko Ursuline6617332015-03-23 11:10:33 +00004203
Chris Wilson383d5822016-08-18 17:17:08 +01004204 /* Bump the LRU to try and avoid premature eviction whilst flipping */
Chris Wilsonbefedbb2017-01-19 19:26:55 +00004205 i915_gem_object_bump_inactive_ggtt(vma->obj);
Chris Wilson383d5822016-08-18 17:17:08 +01004206
Chris Wilson058d88c2016-08-15 10:49:06 +01004207 i915_vma_unpin(vma);
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08004208}
4209
Eric Anholte47c68e2008-11-14 13:35:19 -08004210/**
4211 * Moves a single object to the CPU read, and possibly write domain.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01004212 * @obj: object to act on
4213 * @write: requesting write or read-only access
Eric Anholte47c68e2008-11-14 13:35:19 -08004214 *
4215 * This function returns when the move is complete, including waiting on
4216 * flushes to occur.
4217 */
Chris Wilsondabdfe02012-03-26 10:10:27 +02004218int
Chris Wilson919926a2010-11-12 13:42:53 +00004219i915_gem_object_set_to_cpu_domain(struct drm_i915_gem_object *obj, bool write)
Eric Anholte47c68e2008-11-14 13:35:19 -08004220{
Eric Anholte47c68e2008-11-14 13:35:19 -08004221 int ret;
4222
Chris Wilsone95433c2016-10-28 13:58:27 +01004223 lockdep_assert_held(&obj->base.dev->struct_mutex);
Chris Wilson4c7d62c2016-10-28 13:58:32 +01004224
Chris Wilsone95433c2016-10-28 13:58:27 +01004225 ret = i915_gem_object_wait(obj,
4226 I915_WAIT_INTERRUPTIBLE |
4227 I915_WAIT_LOCKED |
4228 (write ? I915_WAIT_ALL : 0),
4229 MAX_SCHEDULE_TIMEOUT,
4230 NULL);
Chris Wilson88241782011-01-07 17:09:48 +00004231 if (ret)
4232 return ret;
4233
Chris Wilsonef749212017-04-12 12:01:10 +01004234 flush_write_domain(obj, ~I915_GEM_DOMAIN_CPU);
Eric Anholte47c68e2008-11-14 13:35:19 -08004235
Eric Anholte47c68e2008-11-14 13:35:19 -08004236 /* Flush the CPU cache if it's still invalid. */
Christian Königc0a51fd2018-02-16 13:43:38 +01004237 if ((obj->read_domains & I915_GEM_DOMAIN_CPU) == 0) {
Chris Wilson57822dc2017-02-22 11:40:48 +00004238 i915_gem_clflush_object(obj, I915_CLFLUSH_SYNC);
Christian Königc0a51fd2018-02-16 13:43:38 +01004239 obj->read_domains |= I915_GEM_DOMAIN_CPU;
Eric Anholte47c68e2008-11-14 13:35:19 -08004240 }
4241
4242 /* It should now be out of any other write domains, and we can update
4243 * the domain values for our changes.
4244 */
Christian Königc0a51fd2018-02-16 13:43:38 +01004245 GEM_BUG_ON(obj->write_domain & ~I915_GEM_DOMAIN_CPU);
Eric Anholte47c68e2008-11-14 13:35:19 -08004246
4247 /* If we're writing through the CPU, then the GPU read domains will
4248 * need to be invalidated at next use.
4249 */
Chris Wilsone27ab732017-06-15 13:38:49 +01004250 if (write)
4251 __start_cpu_write(obj);
Eric Anholt2ef7eea2008-11-10 10:53:25 -08004252
4253 return 0;
4254}
4255
Eric Anholt673a3942008-07-30 12:06:12 -07004256/* Throttle our rendering by waiting until the ring has completed our requests
4257 * emitted over 20 msec ago.
4258 *
Eric Anholtb9624422009-06-03 07:27:35 +00004259 * Note that if we were to use the current jiffies each time around the loop,
4260 * we wouldn't escape the function with any frames outstanding if the time to
4261 * render a frame was over 20ms.
4262 *
Eric Anholt673a3942008-07-30 12:06:12 -07004263 * This should get us reasonable parallelism between CPU and GPU but also
4264 * relatively low latency when blocking on a particular request to finish.
4265 */
4266static int
Chris Wilsonf787a5f2010-09-24 16:02:42 +01004267i915_gem_ring_throttle(struct drm_device *dev, struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07004268{
Chris Wilsonfac5e232016-07-04 11:34:36 +01004269 struct drm_i915_private *dev_priv = to_i915(dev);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01004270 struct drm_i915_file_private *file_priv = file->driver_priv;
Chris Wilsond0bc54f2015-05-21 21:01:48 +01004271 unsigned long recent_enough = jiffies - DRM_I915_THROTTLE_JIFFIES;
Chris Wilsone61e0f52018-02-21 09:56:36 +00004272 struct i915_request *request, *target = NULL;
Chris Wilsone95433c2016-10-28 13:58:27 +01004273 long ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004274
Chris Wilsonf4457ae2016-04-13 17:35:08 +01004275 /* ABI: return -EIO if already wedged */
4276 if (i915_terminally_wedged(&dev_priv->gpu_error))
4277 return -EIO;
Chris Wilsone110e8d2011-01-26 15:39:14 +00004278
Chris Wilson1c255952010-09-26 11:03:27 +01004279 spin_lock(&file_priv->mm.lock);
Chris Wilsonc8659ef2017-03-02 12:25:25 +00004280 list_for_each_entry(request, &file_priv->mm.request_list, client_link) {
Eric Anholtb9624422009-06-03 07:27:35 +00004281 if (time_after_eq(request->emitted_jiffies, recent_enough))
4282 break;
4283
Chris Wilsonc8659ef2017-03-02 12:25:25 +00004284 if (target) {
4285 list_del(&target->client_link);
4286 target->file_priv = NULL;
4287 }
John Harrisonfcfa423c2015-05-29 17:44:12 +01004288
John Harrison54fb2412014-11-24 18:49:27 +00004289 target = request;
Eric Anholtb9624422009-06-03 07:27:35 +00004290 }
John Harrisonff865882014-11-24 18:49:28 +00004291 if (target)
Chris Wilsone61e0f52018-02-21 09:56:36 +00004292 i915_request_get(target);
Chris Wilson1c255952010-09-26 11:03:27 +01004293 spin_unlock(&file_priv->mm.lock);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01004294
John Harrison54fb2412014-11-24 18:49:27 +00004295 if (target == NULL)
Chris Wilsonf787a5f2010-09-24 16:02:42 +01004296 return 0;
4297
Chris Wilsone61e0f52018-02-21 09:56:36 +00004298 ret = i915_request_wait(target,
Chris Wilsone95433c2016-10-28 13:58:27 +01004299 I915_WAIT_INTERRUPTIBLE,
4300 MAX_SCHEDULE_TIMEOUT);
Chris Wilsone61e0f52018-02-21 09:56:36 +00004301 i915_request_put(target);
John Harrisonff865882014-11-24 18:49:28 +00004302
Chris Wilsone95433c2016-10-28 13:58:27 +01004303 return ret < 0 ? ret : 0;
Eric Anholt673a3942008-07-30 12:06:12 -07004304}
4305
Chris Wilson058d88c2016-08-15 10:49:06 +01004306struct i915_vma *
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02004307i915_gem_object_ggtt_pin(struct drm_i915_gem_object *obj,
4308 const struct i915_ggtt_view *view,
Chris Wilson91b2db62016-08-04 16:32:23 +01004309 u64 size,
Chris Wilson2ffffd02016-08-04 16:32:22 +01004310 u64 alignment,
4311 u64 flags)
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02004312{
Chris Wilsonad16d2e2016-10-13 09:55:04 +01004313 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
4314 struct i915_address_space *vm = &dev_priv->ggtt.base;
Chris Wilson59bfa122016-08-04 16:32:31 +01004315 struct i915_vma *vma;
4316 int ret;
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03004317
Chris Wilson4c7d62c2016-10-28 13:58:32 +01004318 lockdep_assert_held(&obj->base.dev->struct_mutex);
4319
Chris Wilsonac87a6fd2018-02-20 13:42:05 +00004320 if (flags & PIN_MAPPABLE &&
4321 (!view || view->type == I915_GGTT_VIEW_NORMAL)) {
Chris Wilson43ae70d92017-10-09 09:44:01 +01004322 /* If the required space is larger than the available
4323 * aperture, we will not able to find a slot for the
4324 * object and unbinding the object now will be in
4325 * vain. Worse, doing so may cause us to ping-pong
4326 * the object in and out of the Global GTT and
4327 * waste a lot of cycles under the mutex.
4328 */
4329 if (obj->base.size > dev_priv->ggtt.mappable_end)
4330 return ERR_PTR(-E2BIG);
4331
4332 /* If NONBLOCK is set the caller is optimistically
4333 * trying to cache the full object within the mappable
4334 * aperture, and *must* have a fallback in place for
4335 * situations where we cannot bind the object. We
4336 * can be a little more lax here and use the fallback
4337 * more often to avoid costly migrations of ourselves
4338 * and other objects within the aperture.
4339 *
4340 * Half-the-aperture is used as a simple heuristic.
4341 * More interesting would to do search for a free
4342 * block prior to making the commitment to unbind.
4343 * That caters for the self-harm case, and with a
4344 * little more heuristics (e.g. NOFAULT, NOEVICT)
4345 * we could try to minimise harm to others.
4346 */
4347 if (flags & PIN_NONBLOCK &&
4348 obj->base.size > dev_priv->ggtt.mappable_end / 2)
4349 return ERR_PTR(-ENOSPC);
4350 }
4351
Chris Wilson718659a2017-01-16 15:21:28 +00004352 vma = i915_vma_instance(obj, vm, view);
Chris Wilsone0216b72017-01-19 19:26:57 +00004353 if (unlikely(IS_ERR(vma)))
Chris Wilson058d88c2016-08-15 10:49:06 +01004354 return vma;
Chris Wilson59bfa122016-08-04 16:32:31 +01004355
4356 if (i915_vma_misplaced(vma, size, alignment, flags)) {
Chris Wilson43ae70d92017-10-09 09:44:01 +01004357 if (flags & PIN_NONBLOCK) {
4358 if (i915_vma_is_pinned(vma) || i915_vma_is_active(vma))
4359 return ERR_PTR(-ENOSPC);
Chris Wilson59bfa122016-08-04 16:32:31 +01004360
Chris Wilson43ae70d92017-10-09 09:44:01 +01004361 if (flags & PIN_MAPPABLE &&
Chris Wilson944397f2017-01-09 16:16:11 +00004362 vma->fence_size > dev_priv->ggtt.mappable_end / 2)
Chris Wilsonad16d2e2016-10-13 09:55:04 +01004363 return ERR_PTR(-ENOSPC);
4364 }
4365
Chris Wilson59bfa122016-08-04 16:32:31 +01004366 WARN(i915_vma_is_pinned(vma),
4367 "bo is already pinned in ggtt with incorrect alignment:"
Chris Wilson05a20d02016-08-18 17:16:55 +01004368 " offset=%08x, req.alignment=%llx,"
4369 " req.map_and_fenceable=%d, vma->map_and_fenceable=%d\n",
4370 i915_ggtt_offset(vma), alignment,
Chris Wilson59bfa122016-08-04 16:32:31 +01004371 !!(flags & PIN_MAPPABLE),
Chris Wilson05a20d02016-08-18 17:16:55 +01004372 i915_vma_is_map_and_fenceable(vma));
Chris Wilson59bfa122016-08-04 16:32:31 +01004373 ret = i915_vma_unbind(vma);
4374 if (ret)
Chris Wilson058d88c2016-08-15 10:49:06 +01004375 return ERR_PTR(ret);
Chris Wilson59bfa122016-08-04 16:32:31 +01004376 }
4377
Chris Wilson058d88c2016-08-15 10:49:06 +01004378 ret = i915_vma_pin(vma, size, alignment, flags | PIN_GLOBAL);
4379 if (ret)
4380 return ERR_PTR(ret);
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02004381
Chris Wilson058d88c2016-08-15 10:49:06 +01004382 return vma;
Eric Anholt673a3942008-07-30 12:06:12 -07004383}
4384
Chris Wilsonedf6b762016-08-09 09:23:33 +01004385static __always_inline unsigned int __busy_read_flag(unsigned int id)
Chris Wilson3fdc13c2016-08-05 10:14:18 +01004386{
4387 /* Note that we could alias engines in the execbuf API, but
4388 * that would be very unwise as it prevents userspace from
4389 * fine control over engine selection. Ahem.
4390 *
4391 * This should be something like EXEC_MAX_ENGINE instead of
4392 * I915_NUM_ENGINES.
4393 */
4394 BUILD_BUG_ON(I915_NUM_ENGINES > 16);
4395 return 0x10000 << id;
4396}
4397
4398static __always_inline unsigned int __busy_write_id(unsigned int id)
4399{
Chris Wilson70cb4722016-08-09 18:08:25 +01004400 /* The uABI guarantees an active writer is also amongst the read
4401 * engines. This would be true if we accessed the activity tracking
4402 * under the lock, but as we perform the lookup of the object and
4403 * its activity locklessly we can not guarantee that the last_write
4404 * being active implies that we have set the same engine flag from
4405 * last_read - hence we always set both read and write busy for
4406 * last_write.
4407 */
4408 return id | __busy_read_flag(id);
Chris Wilson3fdc13c2016-08-05 10:14:18 +01004409}
4410
Chris Wilsonedf6b762016-08-09 09:23:33 +01004411static __always_inline unsigned int
Chris Wilsond07f0e52016-10-28 13:58:44 +01004412__busy_set_if_active(const struct dma_fence *fence,
Chris Wilson3fdc13c2016-08-05 10:14:18 +01004413 unsigned int (*flag)(unsigned int id))
4414{
Chris Wilsone61e0f52018-02-21 09:56:36 +00004415 struct i915_request *rq;
Chris Wilson12555012016-08-16 09:50:40 +01004416
Chris Wilsond07f0e52016-10-28 13:58:44 +01004417 /* We have to check the current hw status of the fence as the uABI
4418 * guarantees forward progress. We could rely on the idle worker
4419 * to eventually flush us, but to minimise latency just ask the
4420 * hardware.
4421 *
4422 * Note we only report on the status of native fences.
4423 */
4424 if (!dma_fence_is_i915(fence))
Chris Wilson12555012016-08-16 09:50:40 +01004425 return 0;
4426
Chris Wilsond07f0e52016-10-28 13:58:44 +01004427 /* opencode to_request() in order to avoid const warnings */
Chris Wilsone61e0f52018-02-21 09:56:36 +00004428 rq = container_of(fence, struct i915_request, fence);
4429 if (i915_request_completed(rq))
Chris Wilsond07f0e52016-10-28 13:58:44 +01004430 return 0;
4431
Chris Wilson1d39f282017-04-11 13:43:06 +01004432 return flag(rq->engine->uabi_id);
Chris Wilson3fdc13c2016-08-05 10:14:18 +01004433}
4434
Chris Wilsonedf6b762016-08-09 09:23:33 +01004435static __always_inline unsigned int
Chris Wilsond07f0e52016-10-28 13:58:44 +01004436busy_check_reader(const struct dma_fence *fence)
Chris Wilson3fdc13c2016-08-05 10:14:18 +01004437{
Chris Wilsond07f0e52016-10-28 13:58:44 +01004438 return __busy_set_if_active(fence, __busy_read_flag);
Chris Wilson3fdc13c2016-08-05 10:14:18 +01004439}
4440
Chris Wilsonedf6b762016-08-09 09:23:33 +01004441static __always_inline unsigned int
Chris Wilsond07f0e52016-10-28 13:58:44 +01004442busy_check_writer(const struct dma_fence *fence)
Chris Wilson3fdc13c2016-08-05 10:14:18 +01004443{
Chris Wilsond07f0e52016-10-28 13:58:44 +01004444 if (!fence)
4445 return 0;
4446
4447 return __busy_set_if_active(fence, __busy_write_id);
Chris Wilson3fdc13c2016-08-05 10:14:18 +01004448}
4449
Eric Anholt673a3942008-07-30 12:06:12 -07004450int
Eric Anholt673a3942008-07-30 12:06:12 -07004451i915_gem_busy_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00004452 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07004453{
4454 struct drm_i915_gem_busy *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00004455 struct drm_i915_gem_object *obj;
Chris Wilsond07f0e52016-10-28 13:58:44 +01004456 struct reservation_object_list *list;
4457 unsigned int seq;
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004458 int err;
Eric Anholt673a3942008-07-30 12:06:12 -07004459
Chris Wilsond07f0e52016-10-28 13:58:44 +01004460 err = -ENOENT;
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004461 rcu_read_lock();
4462 obj = i915_gem_object_lookup_rcu(file, args->handle);
Chris Wilsond07f0e52016-10-28 13:58:44 +01004463 if (!obj)
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004464 goto out;
Chris Wilsond07f0e52016-10-28 13:58:44 +01004465
4466 /* A discrepancy here is that we do not report the status of
4467 * non-i915 fences, i.e. even though we may report the object as idle,
4468 * a call to set-domain may still stall waiting for foreign rendering.
4469 * This also means that wait-ioctl may report an object as busy,
4470 * where busy-ioctl considers it idle.
4471 *
4472 * We trade the ability to warn of foreign fences to report on which
4473 * i915 engines are active for the object.
4474 *
4475 * Alternatively, we can trade that extra information on read/write
4476 * activity with
4477 * args->busy =
4478 * !reservation_object_test_signaled_rcu(obj->resv, true);
4479 * to report the overall busyness. This is what the wait-ioctl does.
4480 *
4481 */
4482retry:
4483 seq = raw_read_seqcount(&obj->resv->seq);
4484
4485 /* Translate the exclusive fence to the READ *and* WRITE engine */
4486 args->busy = busy_check_writer(rcu_dereference(obj->resv->fence_excl));
4487
4488 /* Translate shared fences to READ set of engines */
4489 list = rcu_dereference(obj->resv->fence);
4490 if (list) {
4491 unsigned int shared_count = list->shared_count, i;
4492
4493 for (i = 0; i < shared_count; ++i) {
4494 struct dma_fence *fence =
4495 rcu_dereference(list->shared[i]);
4496
4497 args->busy |= busy_check_reader(fence);
4498 }
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004499 }
Zou Nan haid1b851f2010-05-21 09:08:57 +08004500
Chris Wilsond07f0e52016-10-28 13:58:44 +01004501 if (args->busy && read_seqcount_retry(&obj->resv->seq, seq))
4502 goto retry;
Chris Wilson426960b2016-01-15 16:51:46 +00004503
Chris Wilsond07f0e52016-10-28 13:58:44 +01004504 err = 0;
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004505out:
4506 rcu_read_unlock();
4507 return err;
Eric Anholt673a3942008-07-30 12:06:12 -07004508}
4509
4510int
4511i915_gem_throttle_ioctl(struct drm_device *dev, void *data,
4512 struct drm_file *file_priv)
4513{
Akshay Joshi0206e352011-08-16 15:34:10 -04004514 return i915_gem_ring_throttle(dev, file_priv);
Eric Anholt673a3942008-07-30 12:06:12 -07004515}
4516
Chris Wilson3ef94da2009-09-14 16:50:29 +01004517int
4518i915_gem_madvise_ioctl(struct drm_device *dev, void *data,
4519 struct drm_file *file_priv)
4520{
Chris Wilsonfac5e232016-07-04 11:34:36 +01004521 struct drm_i915_private *dev_priv = to_i915(dev);
Chris Wilson3ef94da2009-09-14 16:50:29 +01004522 struct drm_i915_gem_madvise *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00004523 struct drm_i915_gem_object *obj;
Chris Wilson1233e2d2016-10-28 13:58:37 +01004524 int err;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004525
4526 switch (args->madv) {
4527 case I915_MADV_DONTNEED:
4528 case I915_MADV_WILLNEED:
4529 break;
4530 default:
4531 return -EINVAL;
4532 }
4533
Chris Wilson03ac0642016-07-20 13:31:51 +01004534 obj = i915_gem_object_lookup(file_priv, args->handle);
Chris Wilson1233e2d2016-10-28 13:58:37 +01004535 if (!obj)
4536 return -ENOENT;
4537
4538 err = mutex_lock_interruptible(&obj->mm.lock);
4539 if (err)
4540 goto out;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004541
Chris Wilsonf1fa4f42017-10-13 21:26:13 +01004542 if (i915_gem_object_has_pages(obj) &&
Chris Wilson3e510a82016-08-05 10:14:23 +01004543 i915_gem_object_is_tiled(obj) &&
Daniel Vetter656bfa32014-11-20 09:26:30 +01004544 dev_priv->quirks & QUIRK_PIN_SWIZZLED_PAGES) {
Chris Wilsonbc0629a2016-11-01 10:03:17 +00004545 if (obj->mm.madv == I915_MADV_WILLNEED) {
4546 GEM_BUG_ON(!obj->mm.quirked);
Chris Wilsona4f5ea62016-10-28 13:58:35 +01004547 __i915_gem_object_unpin_pages(obj);
Chris Wilsonbc0629a2016-11-01 10:03:17 +00004548 obj->mm.quirked = false;
4549 }
4550 if (args->madv == I915_MADV_WILLNEED) {
Chris Wilson2c3a3f42016-11-04 10:30:01 +00004551 GEM_BUG_ON(obj->mm.quirked);
Chris Wilsona4f5ea62016-10-28 13:58:35 +01004552 __i915_gem_object_pin_pages(obj);
Chris Wilsonbc0629a2016-11-01 10:03:17 +00004553 obj->mm.quirked = true;
4554 }
Daniel Vetter656bfa32014-11-20 09:26:30 +01004555 }
4556
Chris Wilsona4f5ea62016-10-28 13:58:35 +01004557 if (obj->mm.madv != __I915_MADV_PURGED)
4558 obj->mm.madv = args->madv;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004559
Chris Wilson6c085a72012-08-20 11:40:46 +02004560 /* if the object is no longer attached, discard its backing storage */
Chris Wilsonf1fa4f42017-10-13 21:26:13 +01004561 if (obj->mm.madv == I915_MADV_DONTNEED &&
4562 !i915_gem_object_has_pages(obj))
Chris Wilson2d7ef392009-09-20 23:13:10 +01004563 i915_gem_object_truncate(obj);
4564
Chris Wilsona4f5ea62016-10-28 13:58:35 +01004565 args->retained = obj->mm.madv != __I915_MADV_PURGED;
Chris Wilson1233e2d2016-10-28 13:58:37 +01004566 mutex_unlock(&obj->mm.lock);
Chris Wilsonbb6baf72009-09-22 14:24:13 +01004567
Chris Wilson1233e2d2016-10-28 13:58:37 +01004568out:
Chris Wilsonf8c417c2016-07-20 13:31:53 +01004569 i915_gem_object_put(obj);
Chris Wilson1233e2d2016-10-28 13:58:37 +01004570 return err;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004571}
4572
Chris Wilson5b8c8ae2016-11-16 19:07:04 +00004573static void
Chris Wilsone61e0f52018-02-21 09:56:36 +00004574frontbuffer_retire(struct i915_gem_active *active, struct i915_request *request)
Chris Wilson5b8c8ae2016-11-16 19:07:04 +00004575{
4576 struct drm_i915_gem_object *obj =
4577 container_of(active, typeof(*obj), frontbuffer_write);
4578
Chris Wilsond59b21e2017-02-22 11:40:49 +00004579 intel_fb_obj_flush(obj, ORIGIN_CS);
Chris Wilson5b8c8ae2016-11-16 19:07:04 +00004580}
4581
Chris Wilson37e680a2012-06-07 15:38:42 +01004582void i915_gem_object_init(struct drm_i915_gem_object *obj,
4583 const struct drm_i915_gem_object_ops *ops)
Chris Wilson0327d6b2012-08-11 15:41:06 +01004584{
Chris Wilson1233e2d2016-10-28 13:58:37 +01004585 mutex_init(&obj->mm.lock);
4586
Ben Widawsky2f633152013-07-17 12:19:03 -07004587 INIT_LIST_HEAD(&obj->vma_list);
Chris Wilsond1b48c12017-08-16 09:52:08 +01004588 INIT_LIST_HEAD(&obj->lut_list);
Chris Wilson8d9d5742015-04-07 16:20:38 +01004589 INIT_LIST_HEAD(&obj->batch_pool_link);
Chris Wilson0327d6b2012-08-11 15:41:06 +01004590
Chris Wilson37e680a2012-06-07 15:38:42 +01004591 obj->ops = ops;
4592
Chris Wilsond07f0e52016-10-28 13:58:44 +01004593 reservation_object_init(&obj->__builtin_resv);
4594 obj->resv = &obj->__builtin_resv;
4595
Chris Wilson50349242016-08-18 17:17:04 +01004596 obj->frontbuffer_ggtt_origin = ORIGIN_GTT;
Chris Wilson5b8c8ae2016-11-16 19:07:04 +00004597 init_request_active(&obj->frontbuffer_write, frontbuffer_retire);
Chris Wilsona4f5ea62016-10-28 13:58:35 +01004598
4599 obj->mm.madv = I915_MADV_WILLNEED;
4600 INIT_RADIX_TREE(&obj->mm.get_page.radix, GFP_KERNEL | __GFP_NOWARN);
4601 mutex_init(&obj->mm.get_page.lock);
Chris Wilson0327d6b2012-08-11 15:41:06 +01004602
Dave Gordonf19ec8c2016-07-04 11:34:37 +01004603 i915_gem_info_add_obj(to_i915(obj->base.dev), obj->base.size);
Chris Wilson0327d6b2012-08-11 15:41:06 +01004604}
4605
Chris Wilson37e680a2012-06-07 15:38:42 +01004606static const struct drm_i915_gem_object_ops i915_gem_object_ops = {
Tvrtko Ursulin3599a912016-11-01 14:44:10 +00004607 .flags = I915_GEM_OBJECT_HAS_STRUCT_PAGE |
4608 I915_GEM_OBJECT_IS_SHRINKABLE,
Chris Wilson7c55e2c2017-03-07 12:03:38 +00004609
Chris Wilson37e680a2012-06-07 15:38:42 +01004610 .get_pages = i915_gem_object_get_pages_gtt,
4611 .put_pages = i915_gem_object_put_pages_gtt,
Chris Wilson7c55e2c2017-03-07 12:03:38 +00004612
4613 .pwrite = i915_gem_object_pwrite_gtt,
Chris Wilson37e680a2012-06-07 15:38:42 +01004614};
4615
Matthew Auld465c4032017-10-06 23:18:14 +01004616static int i915_gem_object_create_shmem(struct drm_device *dev,
4617 struct drm_gem_object *obj,
4618 size_t size)
4619{
4620 struct drm_i915_private *i915 = to_i915(dev);
4621 unsigned long flags = VM_NORESERVE;
4622 struct file *filp;
4623
4624 drm_gem_private_object_init(dev, obj, size);
4625
4626 if (i915->mm.gemfs)
4627 filp = shmem_file_setup_with_mnt(i915->mm.gemfs, "i915", size,
4628 flags);
4629 else
4630 filp = shmem_file_setup("i915", size, flags);
4631
4632 if (IS_ERR(filp))
4633 return PTR_ERR(filp);
4634
4635 obj->filp = filp;
4636
4637 return 0;
4638}
4639
Chris Wilsonb4bcbe22016-10-18 13:02:49 +01004640struct drm_i915_gem_object *
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +00004641i915_gem_object_create(struct drm_i915_private *dev_priv, u64 size)
Daniel Vetterac52bc52010-04-09 19:05:06 +00004642{
Daniel Vetterc397b902010-04-09 19:05:07 +00004643 struct drm_i915_gem_object *obj;
Hugh Dickins5949eac2011-06-27 16:18:18 -07004644 struct address_space *mapping;
Chris Wilsonb8f55be2017-08-11 12:11:16 +01004645 unsigned int cache_level;
Daniel Vetter1a240d42012-11-29 22:18:51 +01004646 gfp_t mask;
Chris Wilsonfe3db792016-04-25 13:32:13 +01004647 int ret;
Daniel Vetterc397b902010-04-09 19:05:07 +00004648
Chris Wilsonb4bcbe22016-10-18 13:02:49 +01004649 /* There is a prevalence of the assumption that we fit the object's
4650 * page count inside a 32bit _signed_ variable. Let's document this and
4651 * catch if we ever need to fix it. In the meantime, if you do spot
4652 * such a local variable, please consider fixing!
4653 */
Tvrtko Ursulin7a3ee5d2017-03-30 17:31:30 +01004654 if (size >> PAGE_SHIFT > INT_MAX)
Chris Wilsonb4bcbe22016-10-18 13:02:49 +01004655 return ERR_PTR(-E2BIG);
4656
4657 if (overflows_type(size, obj->base.size))
4658 return ERR_PTR(-E2BIG);
4659
Tvrtko Ursulin187685c2016-12-01 14:16:36 +00004660 obj = i915_gem_object_alloc(dev_priv);
Daniel Vetterc397b902010-04-09 19:05:07 +00004661 if (obj == NULL)
Chris Wilsonfe3db792016-04-25 13:32:13 +01004662 return ERR_PTR(-ENOMEM);
Daniel Vetterc397b902010-04-09 19:05:07 +00004663
Matthew Auld465c4032017-10-06 23:18:14 +01004664 ret = i915_gem_object_create_shmem(&dev_priv->drm, &obj->base, size);
Chris Wilsonfe3db792016-04-25 13:32:13 +01004665 if (ret)
4666 goto fail;
Daniel Vetterc397b902010-04-09 19:05:07 +00004667
Chris Wilsonbed1ea92012-05-24 20:48:12 +01004668 mask = GFP_HIGHUSER | __GFP_RECLAIMABLE;
Jani Nikulac0f86832016-12-07 12:13:04 +02004669 if (IS_I965GM(dev_priv) || IS_I965G(dev_priv)) {
Chris Wilsonbed1ea92012-05-24 20:48:12 +01004670 /* 965gm cannot relocate objects above 4GiB. */
4671 mask &= ~__GFP_HIGHMEM;
4672 mask |= __GFP_DMA32;
4673 }
4674
Al Viro93c76a32015-12-04 23:45:44 -05004675 mapping = obj->base.filp->f_mapping;
Chris Wilsonbed1ea92012-05-24 20:48:12 +01004676 mapping_set_gfp_mask(mapping, mask);
Chris Wilson4846bf02017-06-09 12:03:46 +01004677 GEM_BUG_ON(!(mapping_gfp_mask(mapping) & __GFP_RECLAIM));
Hugh Dickins5949eac2011-06-27 16:18:18 -07004678
Chris Wilson37e680a2012-06-07 15:38:42 +01004679 i915_gem_object_init(obj, &i915_gem_object_ops);
Chris Wilson73aa8082010-09-30 11:46:12 +01004680
Christian Königc0a51fd2018-02-16 13:43:38 +01004681 obj->write_domain = I915_GEM_DOMAIN_CPU;
4682 obj->read_domains = I915_GEM_DOMAIN_CPU;
Daniel Vetterc397b902010-04-09 19:05:07 +00004683
Chris Wilsonb8f55be2017-08-11 12:11:16 +01004684 if (HAS_LLC(dev_priv))
Eugeni Dodonov3d29b842012-01-17 14:43:53 -02004685 /* On some devices, we can have the GPU use the LLC (the CPU
Eric Anholta1871112011-03-29 16:59:55 -07004686 * cache) for about a 10% performance improvement
4687 * compared to uncached. Graphics requests other than
4688 * display scanout are coherent with the CPU in
4689 * accessing this cache. This means in this mode we
4690 * don't need to clflush on the CPU side, and on the
4691 * GPU side we only need to flush internal caches to
4692 * get data visible to the CPU.
4693 *
4694 * However, we maintain the display planes as UC, and so
4695 * need to rebind when first used as such.
4696 */
Chris Wilsonb8f55be2017-08-11 12:11:16 +01004697 cache_level = I915_CACHE_LLC;
4698 else
4699 cache_level = I915_CACHE_NONE;
Eric Anholta1871112011-03-29 16:59:55 -07004700
Chris Wilsonb8f55be2017-08-11 12:11:16 +01004701 i915_gem_object_set_cache_coherency(obj, cache_level);
Chris Wilsone27ab732017-06-15 13:38:49 +01004702
Daniel Vetterd861e332013-07-24 23:25:03 +02004703 trace_i915_gem_object_create(obj);
4704
Chris Wilson05394f32010-11-08 19:18:58 +00004705 return obj;
Chris Wilsonfe3db792016-04-25 13:32:13 +01004706
4707fail:
4708 i915_gem_object_free(obj);
Chris Wilsonfe3db792016-04-25 13:32:13 +01004709 return ERR_PTR(ret);
Daniel Vetterac52bc52010-04-09 19:05:06 +00004710}
4711
Chris Wilson340fbd82014-05-22 09:16:52 +01004712static bool discard_backing_storage(struct drm_i915_gem_object *obj)
4713{
4714 /* If we are the last user of the backing storage (be it shmemfs
4715 * pages or stolen etc), we know that the pages are going to be
4716 * immediately released. In this case, we can then skip copying
4717 * back the contents from the GPU.
4718 */
4719
Chris Wilsona4f5ea62016-10-28 13:58:35 +01004720 if (obj->mm.madv != I915_MADV_WILLNEED)
Chris Wilson340fbd82014-05-22 09:16:52 +01004721 return false;
4722
4723 if (obj->base.filp == NULL)
4724 return true;
4725
4726 /* At first glance, this looks racy, but then again so would be
4727 * userspace racing mmap against close. However, the first external
4728 * reference to the filp can only be obtained through the
4729 * i915_gem_mmap_ioctl() which safeguards us against the user
4730 * acquiring such a reference whilst we are in the middle of
4731 * freeing the object.
4732 */
4733 return atomic_long_read(&obj->base.filp->f_count) == 1;
4734}
4735
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004736static void __i915_gem_free_objects(struct drm_i915_private *i915,
4737 struct llist_node *freed)
Chris Wilsonbe726152010-07-23 23:18:50 +01004738{
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004739 struct drm_i915_gem_object *obj, *on;
Chris Wilsonbe726152010-07-23 23:18:50 +01004740
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004741 intel_runtime_pm_get(i915);
Chris Wilsoncc731f52017-10-13 21:26:21 +01004742 llist_for_each_entry_safe(obj, on, freed, freed) {
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004743 struct i915_vma *vma, *vn;
Paulo Zanonif65c9162013-11-27 18:20:34 -02004744
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004745 trace_i915_gem_object_destroy(obj);
4746
Chris Wilsoncc731f52017-10-13 21:26:21 +01004747 mutex_lock(&i915->drm.struct_mutex);
4748
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004749 GEM_BUG_ON(i915_gem_object_is_active(obj));
4750 list_for_each_entry_safe(vma, vn,
4751 &obj->vma_list, obj_link) {
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004752 GEM_BUG_ON(i915_vma_is_active(vma));
4753 vma->flags &= ~I915_VMA_PIN_MASK;
Chris Wilson3365e222018-05-03 20:51:14 +01004754 i915_vma_destroy(vma);
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004755 }
Chris Wilsondb6c2b42016-11-01 11:54:00 +00004756 GEM_BUG_ON(!list_empty(&obj->vma_list));
4757 GEM_BUG_ON(!RB_EMPTY_ROOT(&obj->vma_tree));
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004758
Chris Wilsonf2123812017-10-16 12:40:37 +01004759 /* This serializes freeing with the shrinker. Since the free
4760 * is delayed, first by RCU then by the workqueue, we want the
4761 * shrinker to be able to free pages of unreferenced objects,
4762 * or else we may oom whilst there are plenty of deferred
4763 * freed objects.
4764 */
4765 if (i915_gem_object_has_pages(obj)) {
4766 spin_lock(&i915->mm.obj_lock);
4767 list_del_init(&obj->mm.link);
4768 spin_unlock(&i915->mm.obj_lock);
4769 }
4770
Chris Wilsoncc731f52017-10-13 21:26:21 +01004771 mutex_unlock(&i915->drm.struct_mutex);
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004772
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004773 GEM_BUG_ON(obj->bind_count);
Chris Wilsona65adaf2017-10-09 09:43:57 +01004774 GEM_BUG_ON(obj->userfault_count);
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004775 GEM_BUG_ON(atomic_read(&obj->frontbuffer_bits));
Chris Wilson67b48042017-08-22 12:05:16 +01004776 GEM_BUG_ON(!list_empty(&obj->lut_list));
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004777
4778 if (obj->ops->release)
4779 obj->ops->release(obj);
4780
4781 if (WARN_ON(i915_gem_object_has_pinned_pages(obj)))
4782 atomic_set(&obj->mm.pages_pin_count, 0);
Chris Wilson548625e2016-11-01 12:11:34 +00004783 __i915_gem_object_put_pages(obj, I915_MM_NORMAL);
Chris Wilsonf1fa4f42017-10-13 21:26:13 +01004784 GEM_BUG_ON(i915_gem_object_has_pages(obj));
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004785
4786 if (obj->base.import_attach)
4787 drm_prime_gem_destroy(&obj->base, NULL);
4788
Chris Wilsond07f0e52016-10-28 13:58:44 +01004789 reservation_object_fini(&obj->__builtin_resv);
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004790 drm_gem_object_release(&obj->base);
4791 i915_gem_info_remove_obj(i915, obj->base.size);
4792
4793 kfree(obj->bit_17);
4794 i915_gem_object_free(obj);
Chris Wilsoncc731f52017-10-13 21:26:21 +01004795
Chris Wilsonc9c704712018-02-19 22:06:31 +00004796 GEM_BUG_ON(!atomic_read(&i915->mm.free_count));
4797 atomic_dec(&i915->mm.free_count);
4798
Chris Wilsoncc731f52017-10-13 21:26:21 +01004799 if (on)
4800 cond_resched();
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004801 }
Chris Wilsoncc731f52017-10-13 21:26:21 +01004802 intel_runtime_pm_put(i915);
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004803}
4804
4805static void i915_gem_flush_free_objects(struct drm_i915_private *i915)
4806{
4807 struct llist_node *freed;
4808
Chris Wilson87701b42017-10-13 21:26:20 +01004809 /* Free the oldest, most stale object to keep the free_list short */
4810 freed = NULL;
4811 if (!llist_empty(&i915->mm.free_list)) { /* quick test for hotpath */
4812 /* Only one consumer of llist_del_first() allowed */
4813 spin_lock(&i915->mm.free_lock);
4814 freed = llist_del_first(&i915->mm.free_list);
4815 spin_unlock(&i915->mm.free_lock);
4816 }
4817 if (unlikely(freed)) {
4818 freed->next = NULL;
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004819 __i915_gem_free_objects(i915, freed);
Chris Wilson87701b42017-10-13 21:26:20 +01004820 }
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004821}
4822
4823static void __i915_gem_free_work(struct work_struct *work)
4824{
4825 struct drm_i915_private *i915 =
4826 container_of(work, struct drm_i915_private, mm.free_work);
4827 struct llist_node *freed;
Chris Wilson26e12f82011-03-20 11:20:19 +00004828
Chris Wilson2ef1e722018-01-15 20:57:59 +00004829 /*
4830 * All file-owned VMA should have been released by this point through
Chris Wilsonb1f788c2016-08-04 07:52:45 +01004831 * i915_gem_close_object(), or earlier by i915_gem_context_close().
4832 * However, the object may also be bound into the global GTT (e.g.
4833 * older GPUs without per-process support, or for direct access through
4834 * the GTT either for the user or for scanout). Those VMA still need to
4835 * unbound now.
4836 */
Chris Wilson1488fc02012-04-24 15:47:31 +01004837
Chris Wilsonf991c492017-11-06 11:15:08 +00004838 spin_lock(&i915->mm.free_lock);
Chris Wilson5ad08be2017-04-07 11:25:51 +01004839 while ((freed = llist_del_all(&i915->mm.free_list))) {
Chris Wilsonf991c492017-11-06 11:15:08 +00004840 spin_unlock(&i915->mm.free_lock);
4841
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004842 __i915_gem_free_objects(i915, freed);
Chris Wilson5ad08be2017-04-07 11:25:51 +01004843 if (need_resched())
Chris Wilsonf991c492017-11-06 11:15:08 +00004844 return;
4845
4846 spin_lock(&i915->mm.free_lock);
Chris Wilson5ad08be2017-04-07 11:25:51 +01004847 }
Chris Wilsonf991c492017-11-06 11:15:08 +00004848 spin_unlock(&i915->mm.free_lock);
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004849}
4850
4851static void __i915_gem_free_object_rcu(struct rcu_head *head)
4852{
4853 struct drm_i915_gem_object *obj =
4854 container_of(head, typeof(*obj), rcu);
4855 struct drm_i915_private *i915 = to_i915(obj->base.dev);
4856
Chris Wilson2ef1e722018-01-15 20:57:59 +00004857 /*
4858 * Since we require blocking on struct_mutex to unbind the freed
4859 * object from the GPU before releasing resources back to the
4860 * system, we can not do that directly from the RCU callback (which may
4861 * be a softirq context), but must instead then defer that work onto a
4862 * kthread. We use the RCU callback rather than move the freed object
4863 * directly onto the work queue so that we can mix between using the
4864 * worker and performing frees directly from subsequent allocations for
4865 * crude but effective memory throttling.
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004866 */
4867 if (llist_add(&obj->freed, &i915->mm.free_list))
Chris Wilsonbeacbd12018-01-15 12:28:45 +00004868 queue_work(i915->wq, &i915->mm.free_work);
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004869}
4870
4871void i915_gem_free_object(struct drm_gem_object *gem_obj)
4872{
4873 struct drm_i915_gem_object *obj = to_intel_bo(gem_obj);
4874
Chris Wilsonbc0629a2016-11-01 10:03:17 +00004875 if (obj->mm.quirked)
4876 __i915_gem_object_unpin_pages(obj);
4877
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004878 if (discard_backing_storage(obj))
4879 obj->mm.madv = I915_MADV_DONTNEED;
Daniel Vettera071fa02014-06-18 23:28:09 +02004880
Chris Wilson2ef1e722018-01-15 20:57:59 +00004881 /*
4882 * Before we free the object, make sure any pure RCU-only
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004883 * read-side critical sections are complete, e.g.
4884 * i915_gem_busy_ioctl(). For the corresponding synchronized
4885 * lookup see i915_gem_object_lookup_rcu().
4886 */
Chris Wilsonc9c704712018-02-19 22:06:31 +00004887 atomic_inc(&to_i915(obj->base.dev)->mm.free_count);
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004888 call_rcu(&obj->rcu, __i915_gem_free_object_rcu);
Chris Wilsonbe726152010-07-23 23:18:50 +01004889}
4890
Chris Wilsonf8a7fde2016-10-28 13:58:29 +01004891void __i915_gem_object_release_unless_active(struct drm_i915_gem_object *obj)
4892{
4893 lockdep_assert_held(&obj->base.dev->struct_mutex);
4894
Chris Wilsond1b48c12017-08-16 09:52:08 +01004895 if (!i915_gem_object_has_active_reference(obj) &&
4896 i915_gem_object_is_active(obj))
Chris Wilsonf8a7fde2016-10-28 13:58:29 +01004897 i915_gem_object_set_active_reference(obj);
4898 else
4899 i915_gem_object_put(obj);
4900}
4901
Chris Wilsonae6c4572017-11-10 14:26:28 +00004902static void assert_kernel_context_is_current(struct drm_i915_private *i915)
Chris Wilson3033aca2016-10-28 13:58:47 +01004903{
Chris Wilson1fc44d92018-05-17 22:26:32 +01004904 struct i915_gem_context *kctx = i915->kernel_context;
Chris Wilson3033aca2016-10-28 13:58:47 +01004905 struct intel_engine_cs *engine;
4906 enum intel_engine_id id;
4907
Chris Wilson09a4c022018-05-24 09:11:35 +01004908 GEM_BUG_ON(i915->gt.active_requests);
Chris Wilsonae6c4572017-11-10 14:26:28 +00004909 for_each_engine(engine, i915, id) {
Chris Wilsona89d1f92018-05-02 17:38:39 +01004910 GEM_BUG_ON(__i915_gem_active_peek(&engine->timeline.last_request));
Chris Wilson1fc44d92018-05-17 22:26:32 +01004911 GEM_BUG_ON(engine->last_retired_context->gem_context != kctx);
Chris Wilsonae6c4572017-11-10 14:26:28 +00004912 }
Chris Wilson3033aca2016-10-28 13:58:47 +01004913}
4914
Chris Wilson24145512017-01-24 11:01:35 +00004915void i915_gem_sanitize(struct drm_i915_private *i915)
4916{
Chris Wilsonf36325f2017-08-26 12:09:34 +01004917 if (i915_terminally_wedged(&i915->gpu_error)) {
4918 mutex_lock(&i915->drm.struct_mutex);
4919 i915_gem_unset_wedged(i915);
4920 mutex_unlock(&i915->drm.struct_mutex);
4921 }
4922
Chris Wilson24145512017-01-24 11:01:35 +00004923 /*
4924 * If we inherit context state from the BIOS or earlier occupants
4925 * of the GPU, the GPU may be in an inconsistent state when we
4926 * try to take over. The only way to remove the earlier state
4927 * is by resetting. However, resetting on earlier gen is tricky as
4928 * it may impact the display and we are uncertain about the stability
Joonas Lahtinenea117b82017-04-28 10:53:38 +03004929 * of the reset, so this could be applied to even earlier gen.
Chris Wilson24145512017-01-24 11:01:35 +00004930 */
Daniele Ceraolo Spurioce1599a2018-02-07 13:24:40 -08004931 if (INTEL_GEN(i915) >= 5 && intel_has_gpu_reset(i915))
4932 WARN_ON(intel_gpu_reset(i915, ALL_ENGINES));
Chris Wilson24145512017-01-24 11:01:35 +00004933}
4934
Tvrtko Ursulinbf9e8422016-12-01 14:16:38 +00004935int i915_gem_suspend(struct drm_i915_private *dev_priv)
Eric Anholt673a3942008-07-30 12:06:12 -07004936{
Tvrtko Ursulinbf9e8422016-12-01 14:16:38 +00004937 struct drm_device *dev = &dev_priv->drm;
Chris Wilsondcff85c2016-08-05 10:14:11 +01004938 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004939
Chris Wilson09a4c022018-05-24 09:11:35 +01004940 GEM_TRACE("\n");
4941
Chris Wilsonc998e8a2017-03-02 08:30:29 +00004942 intel_runtime_pm_get(dev_priv);
Chris Wilson54b4f682016-07-21 21:16:19 +01004943 intel_suspend_gt_powersave(dev_priv);
4944
Chris Wilson45c5f202013-10-16 11:50:01 +01004945 mutex_lock(&dev->struct_mutex);
Chris Wilson5ab57c72016-07-15 14:56:20 +01004946
4947 /* We have to flush all the executing contexts to main memory so
4948 * that they can saved in the hibernation image. To ensure the last
4949 * context image is coherent, we have to switch away from it. That
4950 * leaves the dev_priv->kernel_context still active when
4951 * we actually suspend, and its image in memory may not match the GPU
4952 * state. Fortunately, the kernel_context is disposable and we do
4953 * not rely on its state.
4954 */
Chris Wilsonecf73eb2017-11-30 10:29:51 +00004955 if (!i915_terminally_wedged(&dev_priv->gpu_error)) {
4956 ret = i915_gem_switch_to_kernel_context(dev_priv);
4957 if (ret)
4958 goto err_unlock;
Chris Wilson5ab57c72016-07-15 14:56:20 +01004959
Chris Wilsonecf73eb2017-11-30 10:29:51 +00004960 ret = i915_gem_wait_for_idle(dev_priv,
4961 I915_WAIT_INTERRUPTIBLE |
4962 I915_WAIT_LOCKED);
4963 if (ret && ret != -EIO)
4964 goto err_unlock;
Chris Wilsonf7403342013-09-13 23:57:04 +01004965
Chris Wilsonecf73eb2017-11-30 10:29:51 +00004966 assert_kernel_context_is_current(dev_priv);
4967 }
Chris Wilson829a0af2017-06-20 12:05:45 +01004968 i915_gem_contexts_lost(dev_priv);
Chris Wilson45c5f202013-10-16 11:50:01 +01004969 mutex_unlock(&dev->struct_mutex);
4970
Michal Wajdeczko7cfca4a2018-03-02 11:15:49 +00004971 intel_uc_suspend(dev_priv);
Sagar Arun Kamble63987bf2017-04-05 15:51:50 +05304972
Chris Wilson737b1502015-01-26 18:03:03 +02004973 cancel_delayed_work_sync(&dev_priv->gpu_error.hangcheck_work);
Chris Wilson67d97da2016-07-04 08:08:31 +01004974 cancel_delayed_work_sync(&dev_priv->gt.retire_work);
Chris Wilsonbdeb9782016-12-23 14:57:56 +00004975
4976 /* As the idle_work is rearming if it detects a race, play safe and
4977 * repeat the flush until it is definitely idle.
4978 */
Chris Wilson7c262402017-10-06 11:40:38 +01004979 drain_delayed_work(&dev_priv->gt.idle_work);
Chris Wilsonbdeb9782016-12-23 14:57:56 +00004980
Chris Wilsonbdcf1202014-11-25 11:56:33 +00004981 /* Assert that we sucessfully flushed all the work and
4982 * reset the GPU back to its idle, low power state.
4983 */
Chris Wilson67d97da2016-07-04 08:08:31 +01004984 WARN_ON(dev_priv->gt.awake);
Chris Wilsonfc692bd2017-08-26 12:09:35 +01004985 if (WARN_ON(!intel_engines_are_idle(dev_priv)))
4986 i915_gem_set_wedged(dev_priv); /* no hope, discard everything */
Chris Wilsonbdcf1202014-11-25 11:56:33 +00004987
Imre Deak1c777c52016-10-12 17:46:37 +03004988 /*
4989 * Neither the BIOS, ourselves or any other kernel
4990 * expects the system to be in execlists mode on startup,
4991 * so we need to reset the GPU back to legacy mode. And the only
4992 * known way to disable logical contexts is through a GPU reset.
4993 *
4994 * So in order to leave the system in a known default configuration,
4995 * always reset the GPU upon unload and suspend. Afterwards we then
4996 * clean up the GEM state tracking, flushing off the requests and
4997 * leaving the system in a known idle state.
4998 *
4999 * Note that is of the upmost importance that the GPU is idle and
5000 * all stray writes are flushed *before* we dismantle the backing
5001 * storage for the pinned objects.
5002 *
5003 * However, since we are uncertain that resetting the GPU on older
5004 * machines is a good idea, we don't - just in case it leaves the
5005 * machine in an unusable condition.
5006 */
Michal Wajdeczkoc37d5722018-03-12 13:03:07 +00005007 intel_uc_sanitize(dev_priv);
Chris Wilson24145512017-01-24 11:01:35 +00005008 i915_gem_sanitize(dev_priv);
Chris Wilsoncad99462017-08-26 12:09:33 +01005009
5010 intel_runtime_pm_put(dev_priv);
5011 return 0;
Imre Deak1c777c52016-10-12 17:46:37 +03005012
Chris Wilsonc998e8a2017-03-02 08:30:29 +00005013err_unlock:
Chris Wilson45c5f202013-10-16 11:50:01 +01005014 mutex_unlock(&dev->struct_mutex);
Chris Wilsonc998e8a2017-03-02 08:30:29 +00005015 intel_runtime_pm_put(dev_priv);
Chris Wilson45c5f202013-10-16 11:50:01 +01005016 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07005017}
5018
Chris Wilson37cd3302017-11-12 11:27:38 +00005019void i915_gem_resume(struct drm_i915_private *i915)
Chris Wilson5ab57c72016-07-15 14:56:20 +01005020{
Chris Wilson37cd3302017-11-12 11:27:38 +00005021 WARN_ON(i915->gt.awake);
Chris Wilson5ab57c72016-07-15 14:56:20 +01005022
Chris Wilson37cd3302017-11-12 11:27:38 +00005023 mutex_lock(&i915->drm.struct_mutex);
5024 intel_uncore_forcewake_get(i915, FORCEWAKE_ALL);
Imre Deak31ab49a2016-11-07 11:20:05 +02005025
Chris Wilson37cd3302017-11-12 11:27:38 +00005026 i915_gem_restore_gtt_mappings(i915);
5027 i915_gem_restore_fences(i915);
Chris Wilson5ab57c72016-07-15 14:56:20 +01005028
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00005029 /*
5030 * As we didn't flush the kernel context before suspend, we cannot
Chris Wilson5ab57c72016-07-15 14:56:20 +01005031 * guarantee that the context image is complete. So let's just reset
5032 * it and start again.
5033 */
Chris Wilson37cd3302017-11-12 11:27:38 +00005034 i915->gt.resume(i915);
Chris Wilson5ab57c72016-07-15 14:56:20 +01005035
Chris Wilson37cd3302017-11-12 11:27:38 +00005036 if (i915_gem_init_hw(i915))
5037 goto err_wedged;
5038
Michal Wajdeczko7cfca4a2018-03-02 11:15:49 +00005039 intel_uc_resume(i915);
Chris Wilson7469c622017-11-14 13:03:00 +00005040
Chris Wilson37cd3302017-11-12 11:27:38 +00005041 /* Always reload a context for powersaving. */
5042 if (i915_gem_switch_to_kernel_context(i915))
5043 goto err_wedged;
5044
5045out_unlock:
5046 intel_uncore_forcewake_put(i915, FORCEWAKE_ALL);
5047 mutex_unlock(&i915->drm.struct_mutex);
5048 return;
5049
5050err_wedged:
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00005051 if (!i915_terminally_wedged(&i915->gpu_error)) {
5052 DRM_ERROR("failed to re-initialize GPU, declaring wedged!\n");
5053 i915_gem_set_wedged(i915);
5054 }
Chris Wilson37cd3302017-11-12 11:27:38 +00005055 goto out_unlock;
Chris Wilson5ab57c72016-07-15 14:56:20 +01005056}
5057
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00005058void i915_gem_init_swizzling(struct drm_i915_private *dev_priv)
Daniel Vetterf691e2f2012-02-02 09:58:12 +01005059{
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00005060 if (INTEL_GEN(dev_priv) < 5 ||
Daniel Vetterf691e2f2012-02-02 09:58:12 +01005061 dev_priv->mm.bit_6_swizzle_x == I915_BIT_6_SWIZZLE_NONE)
5062 return;
5063
5064 I915_WRITE(DISP_ARB_CTL, I915_READ(DISP_ARB_CTL) |
5065 DISP_TILE_SURFACE_SWIZZLING);
5066
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01005067 if (IS_GEN5(dev_priv))
Daniel Vetter11782b02012-01-31 16:47:55 +01005068 return;
5069
Daniel Vetterf691e2f2012-02-02 09:58:12 +01005070 I915_WRITE(TILECTL, I915_READ(TILECTL) | TILECTL_SWZCTL);
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01005071 if (IS_GEN6(dev_priv))
Daniel Vetter6b26c862012-04-24 14:04:12 +02005072 I915_WRITE(ARB_MODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_SNB));
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01005073 else if (IS_GEN7(dev_priv))
Daniel Vetter6b26c862012-04-24 14:04:12 +02005074 I915_WRITE(ARB_MODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_IVB));
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01005075 else if (IS_GEN8(dev_priv))
Ben Widawsky31a53362013-11-02 21:07:04 -07005076 I915_WRITE(GAMTARBMODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_BDW));
Ben Widawsky8782e262012-12-18 10:31:23 -08005077 else
5078 BUG();
Daniel Vetterf691e2f2012-02-02 09:58:12 +01005079}
Daniel Vettere21af882012-02-09 20:53:27 +01005080
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01005081static void init_unused_ring(struct drm_i915_private *dev_priv, u32 base)
Ville Syrjälä81e7f202014-08-15 01:21:55 +03005082{
Ville Syrjälä81e7f202014-08-15 01:21:55 +03005083 I915_WRITE(RING_CTL(base), 0);
5084 I915_WRITE(RING_HEAD(base), 0);
5085 I915_WRITE(RING_TAIL(base), 0);
5086 I915_WRITE(RING_START(base), 0);
5087}
5088
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01005089static void init_unused_rings(struct drm_i915_private *dev_priv)
Ville Syrjälä81e7f202014-08-15 01:21:55 +03005090{
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01005091 if (IS_I830(dev_priv)) {
5092 init_unused_ring(dev_priv, PRB1_BASE);
5093 init_unused_ring(dev_priv, SRB0_BASE);
5094 init_unused_ring(dev_priv, SRB1_BASE);
5095 init_unused_ring(dev_priv, SRB2_BASE);
5096 init_unused_ring(dev_priv, SRB3_BASE);
5097 } else if (IS_GEN2(dev_priv)) {
5098 init_unused_ring(dev_priv, SRB0_BASE);
5099 init_unused_ring(dev_priv, SRB1_BASE);
5100 } else if (IS_GEN3(dev_priv)) {
5101 init_unused_ring(dev_priv, PRB1_BASE);
5102 init_unused_ring(dev_priv, PRB2_BASE);
Ville Syrjälä81e7f202014-08-15 01:21:55 +03005103 }
5104}
5105
Chris Wilson20a8a742017-02-08 14:30:31 +00005106static int __i915_gem_restart_engines(void *data)
Ben Widawsky4fc7c972013-02-08 11:49:24 -08005107{
Chris Wilson20a8a742017-02-08 14:30:31 +00005108 struct drm_i915_private *i915 = data;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00005109 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05305110 enum intel_engine_id id;
Chris Wilson20a8a742017-02-08 14:30:31 +00005111 int err;
5112
5113 for_each_engine(engine, i915, id) {
5114 err = engine->init_hw(engine);
Chris Wilson8177e112018-02-07 11:15:45 +00005115 if (err) {
5116 DRM_ERROR("Failed to restart %s (%d)\n",
5117 engine->name, err);
Chris Wilson20a8a742017-02-08 14:30:31 +00005118 return err;
Chris Wilson8177e112018-02-07 11:15:45 +00005119 }
Chris Wilson20a8a742017-02-08 14:30:31 +00005120 }
5121
5122 return 0;
5123}
5124
5125int i915_gem_init_hw(struct drm_i915_private *dev_priv)
5126{
Chris Wilsond200cda2016-04-28 09:56:44 +01005127 int ret;
Ben Widawsky4fc7c972013-02-08 11:49:24 -08005128
Chris Wilsonde867c22016-10-25 13:16:02 +01005129 dev_priv->gt.last_init_time = ktime_get();
5130
Chris Wilson5e4f5182015-02-13 14:35:59 +00005131 /* Double layer security blanket, see i915_gem_init() */
5132 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
5133
Tvrtko Ursulin0031fb92016-11-04 14:42:44 +00005134 if (HAS_EDRAM(dev_priv) && INTEL_GEN(dev_priv) < 9)
Ben Widawsky05e21cc2013-07-04 11:02:04 -07005135 I915_WRITE(HSW_IDICR, I915_READ(HSW_IDICR) | IDIHASHMSK(0xf));
Ben Widawsky4fc7c972013-02-08 11:49:24 -08005136
Tvrtko Ursulin772c2a52016-10-13 11:03:01 +01005137 if (IS_HASWELL(dev_priv))
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01005138 I915_WRITE(MI_PREDICATE_RESULT_2, IS_HSW_GT3(dev_priv) ?
Ville Syrjälä0bf21342013-11-29 14:56:12 +02005139 LOWER_SLICE_ENABLED : LOWER_SLICE_DISABLED);
Rodrigo Vivi94353732013-08-28 16:45:46 -03005140
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01005141 if (HAS_PCH_NOP(dev_priv)) {
Tvrtko Ursulinfd6b8f42016-10-14 10:13:06 +01005142 if (IS_IVYBRIDGE(dev_priv)) {
Daniel Vetter6ba844b2014-01-22 23:39:30 +01005143 u32 temp = I915_READ(GEN7_MSG_CTL);
5144 temp &= ~(WAIT_FOR_PCH_FLR_ACK | WAIT_FOR_PCH_RESET_ACK);
5145 I915_WRITE(GEN7_MSG_CTL, temp);
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00005146 } else if (INTEL_GEN(dev_priv) >= 7) {
Daniel Vetter6ba844b2014-01-22 23:39:30 +01005147 u32 temp = I915_READ(HSW_NDE_RSTWRN_OPT);
5148 temp &= ~RESET_PCH_HANDSHAKE_ENABLE;
5149 I915_WRITE(HSW_NDE_RSTWRN_OPT, temp);
5150 }
Ben Widawsky88a2b2a2013-04-05 13:12:43 -07005151 }
5152
Oscar Mateo59b449d2018-04-10 09:12:47 -07005153 intel_gt_workarounds_apply(dev_priv);
5154
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00005155 i915_gem_init_swizzling(dev_priv);
Ben Widawsky4fc7c972013-02-08 11:49:24 -08005156
Daniel Vetterd5abdfd2014-11-20 09:45:19 +01005157 /*
5158 * At least 830 can leave some of the unused rings
5159 * "active" (ie. head != tail) after resume which
5160 * will prevent c3 entry. Makes sure all unused rings
5161 * are totally idle.
5162 */
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01005163 init_unused_rings(dev_priv);
Daniel Vetterd5abdfd2014-11-20 09:45:19 +01005164
Dave Gordoned54c1a2016-01-19 19:02:54 +00005165 BUG_ON(!dev_priv->kernel_context);
Chris Wilson6f74b362017-10-15 15:37:25 +01005166 if (i915_terminally_wedged(&dev_priv->gpu_error)) {
5167 ret = -EIO;
5168 goto out;
5169 }
John Harrison90638cc2015-05-29 17:43:37 +01005170
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00005171 ret = i915_ppgtt_init_hw(dev_priv);
John Harrison4ad2fd82015-06-18 13:11:20 +01005172 if (ret) {
Chris Wilson8177e112018-02-07 11:15:45 +00005173 DRM_ERROR("Enabling PPGTT failed (%d)\n", ret);
John Harrison4ad2fd82015-06-18 13:11:20 +01005174 goto out;
5175 }
5176
Jackie Lif08e2032018-03-13 17:32:53 -07005177 ret = intel_wopcm_init_hw(&dev_priv->wopcm);
5178 if (ret) {
5179 DRM_ERROR("Enabling WOPCM failed (%d)\n", ret);
5180 goto out;
5181 }
5182
Michał Winiarski9bdc3572017-10-25 18:25:19 +01005183 /* We can't enable contexts until all firmware is loaded */
5184 ret = intel_uc_init_hw(dev_priv);
Chris Wilson8177e112018-02-07 11:15:45 +00005185 if (ret) {
5186 DRM_ERROR("Enabling uc failed (%d)\n", ret);
Michał Winiarski9bdc3572017-10-25 18:25:19 +01005187 goto out;
Chris Wilson8177e112018-02-07 11:15:45 +00005188 }
Michał Winiarski9bdc3572017-10-25 18:25:19 +01005189
Tvrtko Ursulinbf9e8422016-12-01 14:16:38 +00005190 intel_mocs_init_l3cc_table(dev_priv);
Peter Antoine0ccdacf2016-04-13 15:03:25 +01005191
Chris Wilson136109c2017-11-02 13:14:30 +00005192 /* Only when the HW is re-initialised, can we replay the requests */
5193 ret = __i915_gem_restart_engines(dev_priv);
Chris Wilson5e4f5182015-02-13 14:35:59 +00005194out:
5195 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Ben Widawsky2fa48d82013-12-06 14:11:04 -08005196 return ret;
Zou Nan hai8187a2b2010-05-21 09:08:55 +08005197}
5198
Chris Wilsond2b4b972017-11-10 14:26:33 +00005199static int __intel_engines_record_defaults(struct drm_i915_private *i915)
5200{
5201 struct i915_gem_context *ctx;
5202 struct intel_engine_cs *engine;
5203 enum intel_engine_id id;
5204 int err;
5205
5206 /*
5207 * As we reset the gpu during very early sanitisation, the current
5208 * register state on the GPU should reflect its defaults values.
5209 * We load a context onto the hw (with restore-inhibit), then switch
5210 * over to a second context to save that default register state. We
5211 * can then prime every new context with that state so they all start
5212 * from the same default HW values.
5213 */
5214
5215 ctx = i915_gem_context_create_kernel(i915, 0);
5216 if (IS_ERR(ctx))
5217 return PTR_ERR(ctx);
5218
5219 for_each_engine(engine, i915, id) {
Chris Wilsone61e0f52018-02-21 09:56:36 +00005220 struct i915_request *rq;
Chris Wilsond2b4b972017-11-10 14:26:33 +00005221
Chris Wilsone61e0f52018-02-21 09:56:36 +00005222 rq = i915_request_alloc(engine, ctx);
Chris Wilsond2b4b972017-11-10 14:26:33 +00005223 if (IS_ERR(rq)) {
5224 err = PTR_ERR(rq);
5225 goto out_ctx;
5226 }
5227
Chris Wilson3fef5cd2017-11-20 10:20:02 +00005228 err = 0;
Chris Wilsond2b4b972017-11-10 14:26:33 +00005229 if (engine->init_context)
5230 err = engine->init_context(rq);
5231
Chris Wilsone61e0f52018-02-21 09:56:36 +00005232 __i915_request_add(rq, true);
Chris Wilsond2b4b972017-11-10 14:26:33 +00005233 if (err)
5234 goto err_active;
5235 }
5236
5237 err = i915_gem_switch_to_kernel_context(i915);
5238 if (err)
5239 goto err_active;
5240
5241 err = i915_gem_wait_for_idle(i915, I915_WAIT_LOCKED);
5242 if (err)
5243 goto err_active;
5244
5245 assert_kernel_context_is_current(i915);
5246
5247 for_each_engine(engine, i915, id) {
5248 struct i915_vma *state;
5249
Chris Wilsonab82a062018-04-30 14:15:01 +01005250 state = to_intel_context(ctx, engine)->state;
Chris Wilsond2b4b972017-11-10 14:26:33 +00005251 if (!state)
5252 continue;
5253
5254 /*
5255 * As we will hold a reference to the logical state, it will
5256 * not be torn down with the context, and importantly the
5257 * object will hold onto its vma (making it possible for a
5258 * stray GTT write to corrupt our defaults). Unmap the vma
5259 * from the GTT to prevent such accidents and reclaim the
5260 * space.
5261 */
5262 err = i915_vma_unbind(state);
5263 if (err)
5264 goto err_active;
5265
5266 err = i915_gem_object_set_to_cpu_domain(state->obj, false);
5267 if (err)
5268 goto err_active;
5269
5270 engine->default_state = i915_gem_object_get(state->obj);
5271 }
5272
5273 if (IS_ENABLED(CONFIG_DRM_I915_DEBUG_GEM)) {
5274 unsigned int found = intel_engines_has_context_isolation(i915);
5275
5276 /*
5277 * Make sure that classes with multiple engine instances all
5278 * share the same basic configuration.
5279 */
5280 for_each_engine(engine, i915, id) {
5281 unsigned int bit = BIT(engine->uabi_class);
5282 unsigned int expected = engine->default_state ? bit : 0;
5283
5284 if ((found & bit) != expected) {
5285 DRM_ERROR("mismatching default context state for class %d on engine %s\n",
5286 engine->uabi_class, engine->name);
5287 }
5288 }
5289 }
5290
5291out_ctx:
5292 i915_gem_context_set_closed(ctx);
5293 i915_gem_context_put(ctx);
5294 return err;
5295
5296err_active:
5297 /*
5298 * If we have to abandon now, we expect the engines to be idle
5299 * and ready to be torn-down. First try to flush any remaining
5300 * request, ensure we are pointing at the kernel context and
5301 * then remove it.
5302 */
5303 if (WARN_ON(i915_gem_switch_to_kernel_context(i915)))
5304 goto out_ctx;
5305
5306 if (WARN_ON(i915_gem_wait_for_idle(i915, I915_WAIT_LOCKED)))
5307 goto out_ctx;
5308
5309 i915_gem_contexts_lost(i915);
5310 goto out_ctx;
5311}
5312
Tvrtko Ursulinbf9e8422016-12-01 14:16:38 +00005313int i915_gem_init(struct drm_i915_private *dev_priv)
Chris Wilson1070a422012-04-24 15:47:41 +01005314{
Chris Wilson1070a422012-04-24 15:47:41 +01005315 int ret;
5316
Matthew Auldda9fe3f32017-10-06 23:18:31 +01005317 /*
5318 * We need to fallback to 4K pages since gvt gtt handling doesn't
5319 * support huge page entries - we will need to check either hypervisor
5320 * mm can support huge guest page or just do emulation in gvt.
5321 */
5322 if (intel_vgpu_active(dev_priv))
5323 mkwrite_device_info(dev_priv)->page_sizes =
5324 I915_GTT_PAGE_SIZE_4K;
5325
Chris Wilson94312822017-05-03 10:39:18 +01005326 dev_priv->mm.unordered_timeline = dma_fence_context_alloc(1);
Chris Wilson57822dc2017-02-22 11:40:48 +00005327
Chris Wilsonfb5c5512017-11-20 20:55:00 +00005328 if (HAS_LOGICAL_RING_CONTEXTS(dev_priv)) {
Chris Wilson821ed7d2016-09-09 14:11:53 +01005329 dev_priv->gt.resume = intel_lr_context_resume;
Tvrtko Ursulin117897f2016-03-16 11:00:40 +00005330 dev_priv->gt.cleanup_engine = intel_logical_ring_cleanup;
Chris Wilsonfb5c5512017-11-20 20:55:00 +00005331 } else {
5332 dev_priv->gt.resume = intel_legacy_submission_resume;
5333 dev_priv->gt.cleanup_engine = intel_engine_cleanup;
Oscar Mateoa83014d2014-07-24 17:04:21 +01005334 }
5335
Chris Wilsonee487002017-11-22 17:26:21 +00005336 ret = i915_gem_init_userptr(dev_priv);
5337 if (ret)
5338 return ret;
5339
Jackie Li6b0478f2018-03-13 17:32:50 -07005340 ret = intel_wopcm_init(&dev_priv->wopcm);
5341 if (ret)
5342 return ret;
5343
Sagar Arun Kamble70deead2018-01-24 21:16:58 +05305344 ret = intel_uc_init_misc(dev_priv);
Michał Winiarski3176ff42017-12-13 23:13:47 +01005345 if (ret)
5346 return ret;
5347
Chris Wilson5e4f5182015-02-13 14:35:59 +00005348 /* This is just a security blanket to placate dragons.
5349 * On some systems, we very sporadically observe that the first TLBs
5350 * used by the CS may be stale, despite us poking the TLB reset. If
5351 * we hold the forcewake during initialisation these problems
5352 * just magically go away.
5353 */
Chris Wilsonee487002017-11-22 17:26:21 +00005354 mutex_lock(&dev_priv->drm.struct_mutex);
Chris Wilson5e4f5182015-02-13 14:35:59 +00005355 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
5356
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01005357 ret = i915_gem_init_ggtt(dev_priv);
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00005358 if (ret) {
5359 GEM_BUG_ON(ret == -EIO);
5360 goto err_unlock;
5361 }
Jesse Barnesd62b4892013-03-08 10:45:53 -08005362
Chris Wilson829a0af2017-06-20 12:05:45 +01005363 ret = i915_gem_contexts_init(dev_priv);
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00005364 if (ret) {
5365 GEM_BUG_ON(ret == -EIO);
5366 goto err_ggtt;
5367 }
Ben Widawsky2fa48d82013-12-06 14:11:04 -08005368
Tvrtko Ursulinbf9e8422016-12-01 14:16:38 +00005369 ret = intel_engines_init(dev_priv);
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00005370 if (ret) {
5371 GEM_BUG_ON(ret == -EIO);
5372 goto err_context;
5373 }
Daniel Vetter53ca26c2012-04-26 23:28:03 +02005374
Chris Wilsonf58d13d2017-11-10 14:26:29 +00005375 intel_init_gt_powersave(dev_priv);
5376
Michał Winiarski61b5c152017-12-13 23:13:48 +01005377 ret = intel_uc_init(dev_priv);
Chris Wilsoncc6a8182017-11-10 14:26:30 +00005378 if (ret)
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00005379 goto err_pm;
Chris Wilsoncc6a8182017-11-10 14:26:30 +00005380
Michał Winiarski61b5c152017-12-13 23:13:48 +01005381 ret = i915_gem_init_hw(dev_priv);
5382 if (ret)
5383 goto err_uc_init;
5384
Chris Wilsoncc6a8182017-11-10 14:26:30 +00005385 /*
5386 * Despite its name intel_init_clock_gating applies both display
5387 * clock gating workarounds; GT mmio workarounds and the occasional
5388 * GT power context workaround. Worse, sometimes it includes a context
5389 * register workaround which we need to apply before we record the
5390 * default HW state for all contexts.
5391 *
5392 * FIXME: break up the workarounds and apply them at the right time!
5393 */
5394 intel_init_clock_gating(dev_priv);
5395
Chris Wilsond2b4b972017-11-10 14:26:33 +00005396 ret = __intel_engines_record_defaults(dev_priv);
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00005397 if (ret)
5398 goto err_init_hw;
5399
5400 if (i915_inject_load_failure()) {
5401 ret = -ENODEV;
5402 goto err_init_hw;
5403 }
5404
5405 if (i915_inject_load_failure()) {
5406 ret = -EIO;
5407 goto err_init_hw;
5408 }
5409
5410 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
5411 mutex_unlock(&dev_priv->drm.struct_mutex);
5412
5413 return 0;
5414
5415 /*
5416 * Unwinding is complicated by that we want to handle -EIO to mean
5417 * disable GPU submission but keep KMS alive. We want to mark the
5418 * HW as irrevisibly wedged, but keep enough state around that the
5419 * driver doesn't explode during runtime.
5420 */
5421err_init_hw:
5422 i915_gem_wait_for_idle(dev_priv, I915_WAIT_LOCKED);
5423 i915_gem_contexts_lost(dev_priv);
5424 intel_uc_fini_hw(dev_priv);
Michał Winiarski61b5c152017-12-13 23:13:48 +01005425err_uc_init:
5426 intel_uc_fini(dev_priv);
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00005427err_pm:
5428 if (ret != -EIO) {
5429 intel_cleanup_gt_powersave(dev_priv);
5430 i915_gem_cleanup_engines(dev_priv);
5431 }
5432err_context:
5433 if (ret != -EIO)
5434 i915_gem_contexts_fini(dev_priv);
5435err_ggtt:
5436err_unlock:
5437 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
5438 mutex_unlock(&dev_priv->drm.struct_mutex);
5439
Sagar Arun Kamble70deead2018-01-24 21:16:58 +05305440 intel_uc_fini_misc(dev_priv);
Sagar Arun Kambleda943b52018-01-10 18:24:16 +05305441
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00005442 if (ret != -EIO)
5443 i915_gem_cleanup_userptr(dev_priv);
5444
Chris Wilson60990322014-04-09 09:19:42 +01005445 if (ret == -EIO) {
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00005446 /*
5447 * Allow engine initialisation to fail by marking the GPU as
Chris Wilson60990322014-04-09 09:19:42 +01005448 * wedged. But we only want to do this where the GPU is angry,
5449 * for all other failure, such as an allocation failure, bail.
5450 */
Chris Wilson6f74b362017-10-15 15:37:25 +01005451 if (!i915_terminally_wedged(&dev_priv->gpu_error)) {
5452 DRM_ERROR("Failed to initialize GPU, declaring it wedged\n");
5453 i915_gem_set_wedged(dev_priv);
5454 }
Chris Wilson60990322014-04-09 09:19:42 +01005455 ret = 0;
Chris Wilson1070a422012-04-24 15:47:41 +01005456 }
5457
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00005458 i915_gem_drain_freed_objects(dev_priv);
Chris Wilson60990322014-04-09 09:19:42 +01005459 return ret;
Chris Wilson1070a422012-04-24 15:47:41 +01005460}
5461
Chris Wilson24145512017-01-24 11:01:35 +00005462void i915_gem_init_mmio(struct drm_i915_private *i915)
5463{
5464 i915_gem_sanitize(i915);
5465}
5466
Zou Nan hai8187a2b2010-05-21 09:08:55 +08005467void
Tvrtko Ursulincb15d9f2016-12-01 14:16:39 +00005468i915_gem_cleanup_engines(struct drm_i915_private *dev_priv)
Zou Nan hai8187a2b2010-05-21 09:08:55 +08005469{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00005470 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05305471 enum intel_engine_id id;
Zou Nan hai8187a2b2010-05-21 09:08:55 +08005472
Akash Goel3b3f1652016-10-13 22:44:48 +05305473 for_each_engine(engine, dev_priv, id)
Tvrtko Ursulin117897f2016-03-16 11:00:40 +00005474 dev_priv->gt.cleanup_engine(engine);
Zou Nan hai8187a2b2010-05-21 09:08:55 +08005475}
5476
Eric Anholt673a3942008-07-30 12:06:12 -07005477void
Imre Deak40ae4e12016-03-16 14:54:03 +02005478i915_gem_load_init_fences(struct drm_i915_private *dev_priv)
5479{
Chris Wilson49ef5292016-08-18 17:17:00 +01005480 int i;
Imre Deak40ae4e12016-03-16 14:54:03 +02005481
Tvrtko Ursulinc56b89f2018-02-09 21:58:46 +00005482 if (INTEL_GEN(dev_priv) >= 7 && !IS_VALLEYVIEW(dev_priv) &&
Imre Deak40ae4e12016-03-16 14:54:03 +02005483 !IS_CHERRYVIEW(dev_priv))
5484 dev_priv->num_fence_regs = 32;
Tvrtko Ursulinc56b89f2018-02-09 21:58:46 +00005485 else if (INTEL_GEN(dev_priv) >= 4 ||
Jani Nikula73f67aa2016-12-07 22:48:09 +02005486 IS_I945G(dev_priv) || IS_I945GM(dev_priv) ||
5487 IS_G33(dev_priv) || IS_PINEVIEW(dev_priv))
Imre Deak40ae4e12016-03-16 14:54:03 +02005488 dev_priv->num_fence_regs = 16;
5489 else
5490 dev_priv->num_fence_regs = 8;
5491
Chris Wilsonc0336662016-05-06 15:40:21 +01005492 if (intel_vgpu_active(dev_priv))
Imre Deak40ae4e12016-03-16 14:54:03 +02005493 dev_priv->num_fence_regs =
5494 I915_READ(vgtif_reg(avail_rs.fence_num));
5495
5496 /* Initialize fence registers to zero */
Chris Wilson49ef5292016-08-18 17:17:00 +01005497 for (i = 0; i < dev_priv->num_fence_regs; i++) {
5498 struct drm_i915_fence_reg *fence = &dev_priv->fence_regs[i];
5499
5500 fence->i915 = dev_priv;
5501 fence->id = i;
5502 list_add_tail(&fence->link, &dev_priv->mm.fence_list);
5503 }
Tvrtko Ursulin4362f4f2016-11-16 08:55:33 +00005504 i915_gem_restore_fences(dev_priv);
Imre Deak40ae4e12016-03-16 14:54:03 +02005505
Tvrtko Ursulin4362f4f2016-11-16 08:55:33 +00005506 i915_gem_detect_bit_6_swizzle(dev_priv);
Imre Deak40ae4e12016-03-16 14:54:03 +02005507}
5508
Chris Wilson9c52d1c2017-11-10 23:24:47 +00005509static void i915_gem_init__mm(struct drm_i915_private *i915)
5510{
5511 spin_lock_init(&i915->mm.object_stat_lock);
5512 spin_lock_init(&i915->mm.obj_lock);
5513 spin_lock_init(&i915->mm.free_lock);
5514
5515 init_llist_head(&i915->mm.free_list);
5516
5517 INIT_LIST_HEAD(&i915->mm.unbound_list);
5518 INIT_LIST_HEAD(&i915->mm.bound_list);
5519 INIT_LIST_HEAD(&i915->mm.fence_list);
5520 INIT_LIST_HEAD(&i915->mm.userfault_list);
5521
5522 INIT_WORK(&i915->mm.free_work, __i915_gem_free_work);
5523}
5524
Michal Wajdeczkoa0de9082018-03-23 12:34:49 +00005525int i915_gem_init_early(struct drm_i915_private *dev_priv)
Eric Anholt673a3942008-07-30 12:06:12 -07005526{
Tvrtko Ursulina9335682016-11-02 15:14:59 +00005527 int err = -ENOMEM;
Chris Wilson42dcedd2012-11-15 11:32:30 +00005528
Tvrtko Ursulina9335682016-11-02 15:14:59 +00005529 dev_priv->objects = KMEM_CACHE(drm_i915_gem_object, SLAB_HWCACHE_ALIGN);
5530 if (!dev_priv->objects)
Chris Wilson73cb9702016-10-28 13:58:46 +01005531 goto err_out;
Chris Wilson73cb9702016-10-28 13:58:46 +01005532
Tvrtko Ursulina9335682016-11-02 15:14:59 +00005533 dev_priv->vmas = KMEM_CACHE(i915_vma, SLAB_HWCACHE_ALIGN);
5534 if (!dev_priv->vmas)
Chris Wilson73cb9702016-10-28 13:58:46 +01005535 goto err_objects;
Chris Wilson73cb9702016-10-28 13:58:46 +01005536
Chris Wilsond1b48c12017-08-16 09:52:08 +01005537 dev_priv->luts = KMEM_CACHE(i915_lut_handle, 0);
5538 if (!dev_priv->luts)
5539 goto err_vmas;
5540
Chris Wilsone61e0f52018-02-21 09:56:36 +00005541 dev_priv->requests = KMEM_CACHE(i915_request,
Tvrtko Ursulina9335682016-11-02 15:14:59 +00005542 SLAB_HWCACHE_ALIGN |
5543 SLAB_RECLAIM_ACCOUNT |
Paul E. McKenney5f0d5a32017-01-18 02:53:44 -08005544 SLAB_TYPESAFE_BY_RCU);
Tvrtko Ursulina9335682016-11-02 15:14:59 +00005545 if (!dev_priv->requests)
Chris Wilsond1b48c12017-08-16 09:52:08 +01005546 goto err_luts;
Chris Wilson73cb9702016-10-28 13:58:46 +01005547
Chris Wilson52e54202016-11-14 20:41:02 +00005548 dev_priv->dependencies = KMEM_CACHE(i915_dependency,
5549 SLAB_HWCACHE_ALIGN |
5550 SLAB_RECLAIM_ACCOUNT);
5551 if (!dev_priv->dependencies)
5552 goto err_requests;
5553
Chris Wilsonc5cf9a92017-05-17 13:10:04 +01005554 dev_priv->priorities = KMEM_CACHE(i915_priolist, SLAB_HWCACHE_ALIGN);
5555 if (!dev_priv->priorities)
5556 goto err_dependencies;
5557
Chris Wilson73cb9702016-10-28 13:58:46 +01005558 INIT_LIST_HEAD(&dev_priv->gt.timelines);
Chris Wilson643b4502018-04-30 14:15:03 +01005559 INIT_LIST_HEAD(&dev_priv->gt.active_rings);
Chris Wilson3365e222018-05-03 20:51:14 +01005560 INIT_LIST_HEAD(&dev_priv->gt.closed_vma);
Chris Wilson643b4502018-04-30 14:15:03 +01005561
Chris Wilson9c52d1c2017-11-10 23:24:47 +00005562 i915_gem_init__mm(dev_priv);
Chris Wilsonf2123812017-10-16 12:40:37 +01005563
Chris Wilson67d97da2016-07-04 08:08:31 +01005564 INIT_DELAYED_WORK(&dev_priv->gt.retire_work,
Eric Anholt673a3942008-07-30 12:06:12 -07005565 i915_gem_retire_work_handler);
Chris Wilson67d97da2016-07-04 08:08:31 +01005566 INIT_DELAYED_WORK(&dev_priv->gt.idle_work,
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005567 i915_gem_idle_work_handler);
Chris Wilson1f15b762016-07-01 17:23:14 +01005568 init_waitqueue_head(&dev_priv->gpu_error.wait_queue);
Daniel Vetter1f83fee2012-11-15 17:17:22 +01005569 init_waitqueue_head(&dev_priv->gpu_error.reset_queue);
Chris Wilson31169712009-09-14 16:50:28 +01005570
Joonas Lahtinen6f633402016-09-01 14:58:21 +03005571 atomic_set(&dev_priv->mm.bsd_engine_dispatch_index, 0);
5572
Chris Wilsonb5add952016-08-04 16:32:36 +01005573 spin_lock_init(&dev_priv->fb_tracking.lock);
Chris Wilson73cb9702016-10-28 13:58:46 +01005574
Matthew Auld465c4032017-10-06 23:18:14 +01005575 err = i915_gemfs_init(dev_priv);
5576 if (err)
5577 DRM_NOTE("Unable to create a private tmpfs mount, hugepage support will be disabled(%d).\n", err);
5578
Chris Wilson73cb9702016-10-28 13:58:46 +01005579 return 0;
5580
Chris Wilson52e54202016-11-14 20:41:02 +00005581err_dependencies:
5582 kmem_cache_destroy(dev_priv->dependencies);
Chris Wilson73cb9702016-10-28 13:58:46 +01005583err_requests:
5584 kmem_cache_destroy(dev_priv->requests);
Chris Wilsond1b48c12017-08-16 09:52:08 +01005585err_luts:
5586 kmem_cache_destroy(dev_priv->luts);
Chris Wilson73cb9702016-10-28 13:58:46 +01005587err_vmas:
5588 kmem_cache_destroy(dev_priv->vmas);
5589err_objects:
5590 kmem_cache_destroy(dev_priv->objects);
5591err_out:
5592 return err;
Eric Anholt673a3942008-07-30 12:06:12 -07005593}
Dave Airlie71acb5e2008-12-30 20:31:46 +10005594
Michal Wajdeczkoa0de9082018-03-23 12:34:49 +00005595void i915_gem_cleanup_early(struct drm_i915_private *dev_priv)
Imre Deakd64aa092016-01-19 15:26:29 +02005596{
Chris Wilsonc4d4c1c2017-02-10 16:35:23 +00005597 i915_gem_drain_freed_objects(dev_priv);
Chris Wilsonc9c704712018-02-19 22:06:31 +00005598 GEM_BUG_ON(!llist_empty(&dev_priv->mm.free_list));
5599 GEM_BUG_ON(atomic_read(&dev_priv->mm.free_count));
Chris Wilsonc4d4c1c2017-02-10 16:35:23 +00005600 WARN_ON(dev_priv->mm.object_count);
Matthew Auldea84aa72016-11-17 21:04:11 +00005601 WARN_ON(!list_empty(&dev_priv->gt.timelines));
Matthew Auldea84aa72016-11-17 21:04:11 +00005602
Chris Wilsonc5cf9a92017-05-17 13:10:04 +01005603 kmem_cache_destroy(dev_priv->priorities);
Chris Wilson52e54202016-11-14 20:41:02 +00005604 kmem_cache_destroy(dev_priv->dependencies);
Imre Deakd64aa092016-01-19 15:26:29 +02005605 kmem_cache_destroy(dev_priv->requests);
Chris Wilsond1b48c12017-08-16 09:52:08 +01005606 kmem_cache_destroy(dev_priv->luts);
Imre Deakd64aa092016-01-19 15:26:29 +02005607 kmem_cache_destroy(dev_priv->vmas);
5608 kmem_cache_destroy(dev_priv->objects);
Chris Wilson0eafec62016-08-04 16:32:41 +01005609
5610 /* And ensure that our DESTROY_BY_RCU slabs are truly destroyed */
5611 rcu_barrier();
Matthew Auld465c4032017-10-06 23:18:14 +01005612
5613 i915_gemfs_fini(dev_priv);
Imre Deakd64aa092016-01-19 15:26:29 +02005614}
5615
Chris Wilson6a800ea2016-09-21 14:51:07 +01005616int i915_gem_freeze(struct drm_i915_private *dev_priv)
5617{
Chris Wilsond0aa3012017-04-07 11:25:49 +01005618 /* Discard all purgeable objects, let userspace recover those as
5619 * required after resuming.
5620 */
Chris Wilson6a800ea2016-09-21 14:51:07 +01005621 i915_gem_shrink_all(dev_priv);
Chris Wilson6a800ea2016-09-21 14:51:07 +01005622
Chris Wilson6a800ea2016-09-21 14:51:07 +01005623 return 0;
5624}
5625
Chris Wilson461fb992016-05-14 07:26:33 +01005626int i915_gem_freeze_late(struct drm_i915_private *dev_priv)
5627{
5628 struct drm_i915_gem_object *obj;
Chris Wilson7aab2d52016-09-09 20:02:18 +01005629 struct list_head *phases[] = {
5630 &dev_priv->mm.unbound_list,
5631 &dev_priv->mm.bound_list,
5632 NULL
5633 }, **p;
Chris Wilson461fb992016-05-14 07:26:33 +01005634
5635 /* Called just before we write the hibernation image.
5636 *
5637 * We need to update the domain tracking to reflect that the CPU
5638 * will be accessing all the pages to create and restore from the
5639 * hibernation, and so upon restoration those pages will be in the
5640 * CPU domain.
5641 *
5642 * To make sure the hibernation image contains the latest state,
5643 * we update that state just before writing out the image.
Chris Wilson7aab2d52016-09-09 20:02:18 +01005644 *
5645 * To try and reduce the hibernation image, we manually shrink
Chris Wilsond0aa3012017-04-07 11:25:49 +01005646 * the objects as well, see i915_gem_freeze()
Chris Wilson461fb992016-05-14 07:26:33 +01005647 */
5648
Chris Wilson912d5722017-09-06 16:19:30 -07005649 i915_gem_shrink(dev_priv, -1UL, NULL, I915_SHRINK_UNBOUND);
Chris Wilson17b93c42017-04-07 11:25:50 +01005650 i915_gem_drain_freed_objects(dev_priv);
Chris Wilson461fb992016-05-14 07:26:33 +01005651
Chris Wilsonf2123812017-10-16 12:40:37 +01005652 spin_lock(&dev_priv->mm.obj_lock);
Chris Wilson7aab2d52016-09-09 20:02:18 +01005653 for (p = phases; *p; p++) {
Chris Wilsonf2123812017-10-16 12:40:37 +01005654 list_for_each_entry(obj, *p, mm.link)
Chris Wilsone27ab732017-06-15 13:38:49 +01005655 __start_cpu_write(obj);
Chris Wilson461fb992016-05-14 07:26:33 +01005656 }
Chris Wilsonf2123812017-10-16 12:40:37 +01005657 spin_unlock(&dev_priv->mm.obj_lock);
Chris Wilson461fb992016-05-14 07:26:33 +01005658
5659 return 0;
5660}
5661
Chris Wilsonf787a5f2010-09-24 16:02:42 +01005662void i915_gem_release(struct drm_device *dev, struct drm_file *file)
Eric Anholtb9624422009-06-03 07:27:35 +00005663{
Chris Wilsonf787a5f2010-09-24 16:02:42 +01005664 struct drm_i915_file_private *file_priv = file->driver_priv;
Chris Wilsone61e0f52018-02-21 09:56:36 +00005665 struct i915_request *request;
Eric Anholtb9624422009-06-03 07:27:35 +00005666
5667 /* Clean up our request list when the client is going away, so that
5668 * later retire_requests won't dereference our soon-to-be-gone
5669 * file_priv.
5670 */
Chris Wilson1c255952010-09-26 11:03:27 +01005671 spin_lock(&file_priv->mm.lock);
Chris Wilsonc8659ef2017-03-02 12:25:25 +00005672 list_for_each_entry(request, &file_priv->mm.request_list, client_link)
Chris Wilsonf787a5f2010-09-24 16:02:42 +01005673 request->file_priv = NULL;
Chris Wilson1c255952010-09-26 11:03:27 +01005674 spin_unlock(&file_priv->mm.lock);
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005675}
5676
Chris Wilson829a0af2017-06-20 12:05:45 +01005677int i915_gem_open(struct drm_i915_private *i915, struct drm_file *file)
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005678{
5679 struct drm_i915_file_private *file_priv;
Ben Widawskye422b882013-12-06 14:10:58 -08005680 int ret;
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005681
Chris Wilsonc4c29d72016-11-09 10:45:07 +00005682 DRM_DEBUG("\n");
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005683
5684 file_priv = kzalloc(sizeof(*file_priv), GFP_KERNEL);
5685 if (!file_priv)
5686 return -ENOMEM;
5687
5688 file->driver_priv = file_priv;
Chris Wilson829a0af2017-06-20 12:05:45 +01005689 file_priv->dev_priv = i915;
Chris Wilsonab0e7ff2014-02-25 17:11:24 +02005690 file_priv->file = file;
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005691
5692 spin_lock_init(&file_priv->mm.lock);
5693 INIT_LIST_HEAD(&file_priv->mm.request_list);
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005694
Chris Wilsonc80ff162016-07-27 09:07:27 +01005695 file_priv->bsd_engine = -1;
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00005696
Chris Wilson829a0af2017-06-20 12:05:45 +01005697 ret = i915_gem_context_open(i915, file);
Ben Widawskye422b882013-12-06 14:10:58 -08005698 if (ret)
5699 kfree(file_priv);
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005700
Ben Widawskye422b882013-12-06 14:10:58 -08005701 return ret;
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005702}
5703
Daniel Vetterb680c372014-09-19 18:27:27 +02005704/**
5705 * i915_gem_track_fb - update frontbuffer tracking
Geliang Tangd9072a32015-09-15 05:58:44 -07005706 * @old: current GEM buffer for the frontbuffer slots
5707 * @new: new GEM buffer for the frontbuffer slots
5708 * @frontbuffer_bits: bitmask of frontbuffer slots
Daniel Vetterb680c372014-09-19 18:27:27 +02005709 *
5710 * This updates the frontbuffer tracking bits @frontbuffer_bits by clearing them
5711 * from @old and setting them in @new. Both @old and @new can be NULL.
5712 */
Daniel Vettera071fa02014-06-18 23:28:09 +02005713void i915_gem_track_fb(struct drm_i915_gem_object *old,
5714 struct drm_i915_gem_object *new,
5715 unsigned frontbuffer_bits)
5716{
Chris Wilsonfaf5bf02016-08-04 16:32:37 +01005717 /* Control of individual bits within the mask are guarded by
5718 * the owning plane->mutex, i.e. we can never see concurrent
5719 * manipulation of individual bits. But since the bitfield as a whole
5720 * is updated using RMW, we need to use atomics in order to update
5721 * the bits.
5722 */
5723 BUILD_BUG_ON(INTEL_FRONTBUFFER_BITS_PER_PIPE * I915_MAX_PIPES >
5724 sizeof(atomic_t) * BITS_PER_BYTE);
5725
Daniel Vettera071fa02014-06-18 23:28:09 +02005726 if (old) {
Chris Wilsonfaf5bf02016-08-04 16:32:37 +01005727 WARN_ON(!(atomic_read(&old->frontbuffer_bits) & frontbuffer_bits));
5728 atomic_andnot(frontbuffer_bits, &old->frontbuffer_bits);
Daniel Vettera071fa02014-06-18 23:28:09 +02005729 }
5730
5731 if (new) {
Chris Wilsonfaf5bf02016-08-04 16:32:37 +01005732 WARN_ON(atomic_read(&new->frontbuffer_bits) & frontbuffer_bits);
5733 atomic_or(frontbuffer_bits, &new->frontbuffer_bits);
Daniel Vettera071fa02014-06-18 23:28:09 +02005734 }
5735}
5736
Dave Gordonea702992015-07-09 19:29:02 +01005737/* Allocate a new GEM object and fill it with the supplied data */
5738struct drm_i915_gem_object *
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +00005739i915_gem_object_create_from_data(struct drm_i915_private *dev_priv,
Dave Gordonea702992015-07-09 19:29:02 +01005740 const void *data, size_t size)
5741{
5742 struct drm_i915_gem_object *obj;
Chris Wilsonbe062fa2017-03-17 19:46:48 +00005743 struct file *file;
5744 size_t offset;
5745 int err;
Dave Gordonea702992015-07-09 19:29:02 +01005746
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +00005747 obj = i915_gem_object_create(dev_priv, round_up(size, PAGE_SIZE));
Chris Wilsonfe3db792016-04-25 13:32:13 +01005748 if (IS_ERR(obj))
Dave Gordonea702992015-07-09 19:29:02 +01005749 return obj;
5750
Christian Königc0a51fd2018-02-16 13:43:38 +01005751 GEM_BUG_ON(obj->write_domain != I915_GEM_DOMAIN_CPU);
Dave Gordonea702992015-07-09 19:29:02 +01005752
Chris Wilsonbe062fa2017-03-17 19:46:48 +00005753 file = obj->base.filp;
5754 offset = 0;
5755 do {
5756 unsigned int len = min_t(typeof(size), size, PAGE_SIZE);
5757 struct page *page;
5758 void *pgdata, *vaddr;
Dave Gordonea702992015-07-09 19:29:02 +01005759
Chris Wilsonbe062fa2017-03-17 19:46:48 +00005760 err = pagecache_write_begin(file, file->f_mapping,
5761 offset, len, 0,
5762 &page, &pgdata);
5763 if (err < 0)
5764 goto fail;
Dave Gordonea702992015-07-09 19:29:02 +01005765
Chris Wilsonbe062fa2017-03-17 19:46:48 +00005766 vaddr = kmap(page);
5767 memcpy(vaddr, data, len);
5768 kunmap(page);
5769
5770 err = pagecache_write_end(file, file->f_mapping,
5771 offset, len, len,
5772 page, pgdata);
5773 if (err < 0)
5774 goto fail;
5775
5776 size -= len;
5777 data += len;
5778 offset += len;
5779 } while (size);
Dave Gordonea702992015-07-09 19:29:02 +01005780
5781 return obj;
5782
5783fail:
Chris Wilsonf8c417c2016-07-20 13:31:53 +01005784 i915_gem_object_put(obj);
Chris Wilsonbe062fa2017-03-17 19:46:48 +00005785 return ERR_PTR(err);
Dave Gordonea702992015-07-09 19:29:02 +01005786}
Chris Wilson96d77632016-10-28 13:58:33 +01005787
5788struct scatterlist *
5789i915_gem_object_get_sg(struct drm_i915_gem_object *obj,
5790 unsigned int n,
5791 unsigned int *offset)
5792{
Chris Wilsona4f5ea62016-10-28 13:58:35 +01005793 struct i915_gem_object_page_iter *iter = &obj->mm.get_page;
Chris Wilson96d77632016-10-28 13:58:33 +01005794 struct scatterlist *sg;
5795 unsigned int idx, count;
5796
5797 might_sleep();
5798 GEM_BUG_ON(n >= obj->base.size >> PAGE_SHIFT);
Chris Wilsona4f5ea62016-10-28 13:58:35 +01005799 GEM_BUG_ON(!i915_gem_object_has_pinned_pages(obj));
Chris Wilson96d77632016-10-28 13:58:33 +01005800
5801 /* As we iterate forward through the sg, we record each entry in a
5802 * radixtree for quick repeated (backwards) lookups. If we have seen
5803 * this index previously, we will have an entry for it.
5804 *
5805 * Initial lookup is O(N), but this is amortized to O(1) for
5806 * sequential page access (where each new request is consecutive
5807 * to the previous one). Repeated lookups are O(lg(obj->base.size)),
5808 * i.e. O(1) with a large constant!
5809 */
5810 if (n < READ_ONCE(iter->sg_idx))
5811 goto lookup;
5812
5813 mutex_lock(&iter->lock);
5814
5815 /* We prefer to reuse the last sg so that repeated lookup of this
5816 * (or the subsequent) sg are fast - comparing against the last
5817 * sg is faster than going through the radixtree.
5818 */
5819
5820 sg = iter->sg_pos;
5821 idx = iter->sg_idx;
5822 count = __sg_page_count(sg);
5823
5824 while (idx + count <= n) {
5825 unsigned long exception, i;
5826 int ret;
5827
5828 /* If we cannot allocate and insert this entry, or the
5829 * individual pages from this range, cancel updating the
5830 * sg_idx so that on this lookup we are forced to linearly
5831 * scan onwards, but on future lookups we will try the
5832 * insertion again (in which case we need to be careful of
5833 * the error return reporting that we have already inserted
5834 * this index).
5835 */
5836 ret = radix_tree_insert(&iter->radix, idx, sg);
5837 if (ret && ret != -EEXIST)
5838 goto scan;
5839
5840 exception =
5841 RADIX_TREE_EXCEPTIONAL_ENTRY |
5842 idx << RADIX_TREE_EXCEPTIONAL_SHIFT;
5843 for (i = 1; i < count; i++) {
5844 ret = radix_tree_insert(&iter->radix, idx + i,
5845 (void *)exception);
5846 if (ret && ret != -EEXIST)
5847 goto scan;
5848 }
5849
5850 idx += count;
5851 sg = ____sg_next(sg);
5852 count = __sg_page_count(sg);
5853 }
5854
5855scan:
5856 iter->sg_pos = sg;
5857 iter->sg_idx = idx;
5858
5859 mutex_unlock(&iter->lock);
5860
5861 if (unlikely(n < idx)) /* insertion completed by another thread */
5862 goto lookup;
5863
5864 /* In case we failed to insert the entry into the radixtree, we need
5865 * to look beyond the current sg.
5866 */
5867 while (idx + count <= n) {
5868 idx += count;
5869 sg = ____sg_next(sg);
5870 count = __sg_page_count(sg);
5871 }
5872
5873 *offset = n - idx;
5874 return sg;
5875
5876lookup:
5877 rcu_read_lock();
5878
5879 sg = radix_tree_lookup(&iter->radix, n);
5880 GEM_BUG_ON(!sg);
5881
5882 /* If this index is in the middle of multi-page sg entry,
5883 * the radixtree will contain an exceptional entry that points
5884 * to the start of that range. We will return the pointer to
5885 * the base page and the offset of this page within the
5886 * sg entry's range.
5887 */
5888 *offset = 0;
5889 if (unlikely(radix_tree_exception(sg))) {
5890 unsigned long base =
5891 (unsigned long)sg >> RADIX_TREE_EXCEPTIONAL_SHIFT;
5892
5893 sg = radix_tree_lookup(&iter->radix, base);
5894 GEM_BUG_ON(!sg);
5895
5896 *offset = n - base;
5897 }
5898
5899 rcu_read_unlock();
5900
5901 return sg;
5902}
5903
5904struct page *
5905i915_gem_object_get_page(struct drm_i915_gem_object *obj, unsigned int n)
5906{
5907 struct scatterlist *sg;
5908 unsigned int offset;
5909
5910 GEM_BUG_ON(!i915_gem_object_has_struct_page(obj));
5911
5912 sg = i915_gem_object_get_sg(obj, n, &offset);
5913 return nth_page(sg_page(sg), offset);
5914}
5915
5916/* Like i915_gem_object_get_page(), but mark the returned page dirty */
5917struct page *
5918i915_gem_object_get_dirty_page(struct drm_i915_gem_object *obj,
5919 unsigned int n)
5920{
5921 struct page *page;
5922
5923 page = i915_gem_object_get_page(obj, n);
Chris Wilsona4f5ea62016-10-28 13:58:35 +01005924 if (!obj->mm.dirty)
Chris Wilson96d77632016-10-28 13:58:33 +01005925 set_page_dirty(page);
5926
5927 return page;
5928}
5929
5930dma_addr_t
5931i915_gem_object_get_dma_address(struct drm_i915_gem_object *obj,
5932 unsigned long n)
5933{
5934 struct scatterlist *sg;
5935 unsigned int offset;
5936
5937 sg = i915_gem_object_get_sg(obj, n, &offset);
5938 return sg_dma_address(sg) + (offset << PAGE_SHIFT);
5939}
Chris Wilson935a2f72017-02-13 17:15:13 +00005940
Chris Wilson8eeb7902017-07-26 19:16:01 +01005941int i915_gem_object_attach_phys(struct drm_i915_gem_object *obj, int align)
5942{
5943 struct sg_table *pages;
5944 int err;
5945
5946 if (align > obj->base.size)
5947 return -EINVAL;
5948
5949 if (obj->ops == &i915_gem_phys_ops)
5950 return 0;
5951
5952 if (obj->ops != &i915_gem_object_ops)
5953 return -EINVAL;
5954
5955 err = i915_gem_object_unbind(obj);
5956 if (err)
5957 return err;
5958
5959 mutex_lock(&obj->mm.lock);
5960
5961 if (obj->mm.madv != I915_MADV_WILLNEED) {
5962 err = -EFAULT;
5963 goto err_unlock;
5964 }
5965
5966 if (obj->mm.quirked) {
5967 err = -EFAULT;
5968 goto err_unlock;
5969 }
5970
5971 if (obj->mm.mapping) {
5972 err = -EBUSY;
5973 goto err_unlock;
5974 }
5975
Chris Wilsonf2123812017-10-16 12:40:37 +01005976 pages = fetch_and_zero(&obj->mm.pages);
5977 if (pages) {
5978 struct drm_i915_private *i915 = to_i915(obj->base.dev);
5979
5980 __i915_gem_object_reset_page_iter(obj);
5981
5982 spin_lock(&i915->mm.obj_lock);
5983 list_del(&obj->mm.link);
5984 spin_unlock(&i915->mm.obj_lock);
5985 }
5986
Chris Wilson8eeb7902017-07-26 19:16:01 +01005987 obj->ops = &i915_gem_phys_ops;
5988
Chris Wilson8fb6a5d2017-07-26 19:16:02 +01005989 err = ____i915_gem_object_get_pages(obj);
Chris Wilson8eeb7902017-07-26 19:16:01 +01005990 if (err)
5991 goto err_xfer;
5992
5993 /* Perma-pin (until release) the physical set of pages */
5994 __i915_gem_object_pin_pages(obj);
5995
5996 if (!IS_ERR_OR_NULL(pages))
5997 i915_gem_object_ops.put_pages(obj, pages);
5998 mutex_unlock(&obj->mm.lock);
5999 return 0;
6000
6001err_xfer:
6002 obj->ops = &i915_gem_object_ops;
6003 obj->mm.pages = pages;
6004err_unlock:
6005 mutex_unlock(&obj->mm.lock);
6006 return err;
6007}
6008
Chris Wilson935a2f72017-02-13 17:15:13 +00006009#if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
6010#include "selftests/scatterlist.c"
Chris Wilson66d9cb52017-02-13 17:15:17 +00006011#include "selftests/mock_gem_device.c"
Chris Wilson44653982017-02-13 17:15:20 +00006012#include "selftests/huge_gem_object.c"
Matthew Auld40498662017-10-06 23:18:29 +01006013#include "selftests/huge_pages.c"
Chris Wilson8335fd62017-02-13 17:15:28 +00006014#include "selftests/i915_gem_object.c"
Chris Wilson17059452017-02-13 17:15:32 +00006015#include "selftests/i915_gem_coherency.c"
Chris Wilson935a2f72017-02-13 17:15:13 +00006016#endif