blob: 5ece6ae4bdff04267cbac9a764eb026a56bb6746 [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 Wilson47650db2018-03-07 13:42:25 +0000581 rcu_read_lock();
582 if (engine->schedule)
Chris Wilsonb7268c52018-04-18 19:40:52 +0100583 engine->schedule(rq, attr);
Chris Wilson47650db2018-03-07 13:42:25 +0000584 rcu_read_unlock();
Chris Wilson6b5e90f2016-11-14 20:41:05 +0000585}
586
Chris Wilsonb7268c52018-04-18 19:40:52 +0100587static void fence_set_priority(struct dma_fence *fence,
588 const struct i915_sched_attr *attr)
Chris Wilson6b5e90f2016-11-14 20:41:05 +0000589{
590 /* Recurse once into a fence-array */
591 if (dma_fence_is_array(fence)) {
592 struct dma_fence_array *array = to_dma_fence_array(fence);
593 int i;
594
595 for (i = 0; i < array->num_fences; i++)
Chris Wilsonb7268c52018-04-18 19:40:52 +0100596 __fence_set_priority(array->fences[i], attr);
Chris Wilson6b5e90f2016-11-14 20:41:05 +0000597 } else {
Chris Wilsonb7268c52018-04-18 19:40:52 +0100598 __fence_set_priority(fence, attr);
Chris Wilson6b5e90f2016-11-14 20:41:05 +0000599 }
600}
601
602int
603i915_gem_object_wait_priority(struct drm_i915_gem_object *obj,
604 unsigned int flags,
Chris Wilsonb7268c52018-04-18 19:40:52 +0100605 const struct i915_sched_attr *attr)
Chris Wilson6b5e90f2016-11-14 20:41:05 +0000606{
607 struct dma_fence *excl;
608
609 if (flags & I915_WAIT_ALL) {
610 struct dma_fence **shared;
611 unsigned int count, i;
612 int ret;
613
614 ret = reservation_object_get_fences_rcu(obj->resv,
615 &excl, &count, &shared);
616 if (ret)
617 return ret;
618
619 for (i = 0; i < count; i++) {
Chris Wilsonb7268c52018-04-18 19:40:52 +0100620 fence_set_priority(shared[i], attr);
Chris Wilson6b5e90f2016-11-14 20:41:05 +0000621 dma_fence_put(shared[i]);
622 }
623
624 kfree(shared);
625 } else {
626 excl = reservation_object_get_excl_rcu(obj->resv);
627 }
628
629 if (excl) {
Chris Wilsonb7268c52018-04-18 19:40:52 +0100630 fence_set_priority(excl, attr);
Chris Wilson6b5e90f2016-11-14 20:41:05 +0000631 dma_fence_put(excl);
632 }
633 return 0;
634}
635
Chris Wilson00e60f22016-08-04 16:32:40 +0100636/**
Chris Wilsone95433c2016-10-28 13:58:27 +0100637 * Waits for rendering to the object to be completed
Chris Wilson00e60f22016-08-04 16:32:40 +0100638 * @obj: i915 gem object
Chris Wilsone95433c2016-10-28 13:58:27 +0100639 * @flags: how to wait (under a lock, for all rendering or just for writes etc)
640 * @timeout: how long to wait
Chris Wilsona0a8b1c2017-11-09 14:06:44 +0000641 * @rps_client: client (user process) to charge for any waitboosting
Chris Wilson00e60f22016-08-04 16:32:40 +0100642 */
643int
Chris Wilsone95433c2016-10-28 13:58:27 +0100644i915_gem_object_wait(struct drm_i915_gem_object *obj,
645 unsigned int flags,
646 long timeout,
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +0100647 struct intel_rps_client *rps_client)
Chris Wilson00e60f22016-08-04 16:32:40 +0100648{
Chris Wilsone95433c2016-10-28 13:58:27 +0100649 might_sleep();
650#if IS_ENABLED(CONFIG_LOCKDEP)
651 GEM_BUG_ON(debug_locks &&
652 !!lockdep_is_held(&obj->base.dev->struct_mutex) !=
653 !!(flags & I915_WAIT_LOCKED));
654#endif
655 GEM_BUG_ON(timeout < 0);
Chris Wilson00e60f22016-08-04 16:32:40 +0100656
Chris Wilsond07f0e52016-10-28 13:58:44 +0100657 timeout = i915_gem_object_wait_reservation(obj->resv,
658 flags, timeout,
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +0100659 rps_client);
Chris Wilsone95433c2016-10-28 13:58:27 +0100660 return timeout < 0 ? timeout : 0;
Chris Wilson00e60f22016-08-04 16:32:40 +0100661}
662
663static struct intel_rps_client *to_rps_client(struct drm_file *file)
664{
665 struct drm_i915_file_private *fpriv = file->driver_priv;
666
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +0100667 return &fpriv->rps_client;
Chris Wilson00e60f22016-08-04 16:32:40 +0100668}
669
Chris Wilson00731152014-05-21 12:42:56 +0100670static int
671i915_gem_phys_pwrite(struct drm_i915_gem_object *obj,
672 struct drm_i915_gem_pwrite *args,
Chris Wilson03ac84f2016-10-28 13:58:36 +0100673 struct drm_file *file)
Chris Wilson00731152014-05-21 12:42:56 +0100674{
Chris Wilson00731152014-05-21 12:42:56 +0100675 void *vaddr = obj->phys_handle->vaddr + args->offset;
Gustavo Padovan3ed605b2016-04-26 12:32:27 -0300676 char __user *user_data = u64_to_user_ptr(args->data_ptr);
Chris Wilson6a2c4232014-11-04 04:51:40 -0800677
678 /* We manually control the domain here and pretend that it
679 * remains coherent i.e. in the GTT domain, like shmem_pwrite.
680 */
Rodrigo Vivi77a0d1c2015-06-18 11:43:24 -0700681 intel_fb_obj_invalidate(obj, ORIGIN_CPU);
Chris Wilson10466d22017-01-06 15:22:38 +0000682 if (copy_from_user(vaddr, user_data, args->size))
683 return -EFAULT;
Chris Wilson00731152014-05-21 12:42:56 +0100684
Chris Wilson6a2c4232014-11-04 04:51:40 -0800685 drm_clflush_virt_range(vaddr, args->size);
Chris Wilson10466d22017-01-06 15:22:38 +0000686 i915_gem_chipset_flush(to_i915(obj->base.dev));
Paulo Zanoni063e4e62015-02-13 17:23:45 -0200687
Chris Wilsond59b21e2017-02-22 11:40:49 +0000688 intel_fb_obj_flush(obj, ORIGIN_CPU);
Chris Wilson10466d22017-01-06 15:22:38 +0000689 return 0;
Chris Wilson00731152014-05-21 12:42:56 +0100690}
691
Tvrtko Ursulin187685c2016-12-01 14:16:36 +0000692void *i915_gem_object_alloc(struct drm_i915_private *dev_priv)
Chris Wilson42dcedd2012-11-15 11:32:30 +0000693{
Chris Wilsonefab6d82015-04-07 16:20:57 +0100694 return kmem_cache_zalloc(dev_priv->objects, GFP_KERNEL);
Chris Wilson42dcedd2012-11-15 11:32:30 +0000695}
696
697void i915_gem_object_free(struct drm_i915_gem_object *obj)
698{
Chris Wilsonfac5e232016-07-04 11:34:36 +0100699 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
Chris Wilsonefab6d82015-04-07 16:20:57 +0100700 kmem_cache_free(dev_priv->objects, obj);
Chris Wilson42dcedd2012-11-15 11:32:30 +0000701}
702
Dave Airlieff72145b2011-02-07 12:16:14 +1000703static int
704i915_gem_create(struct drm_file *file,
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +0000705 struct drm_i915_private *dev_priv,
Dave Airlieff72145b2011-02-07 12:16:14 +1000706 uint64_t size,
707 uint32_t *handle_p)
Eric Anholt673a3942008-07-30 12:06:12 -0700708{
Chris Wilson05394f32010-11-08 19:18:58 +0000709 struct drm_i915_gem_object *obj;
Pekka Paalanena1a2d1d2009-08-23 12:40:55 +0300710 int ret;
711 u32 handle;
Eric Anholt673a3942008-07-30 12:06:12 -0700712
Dave Airlieff72145b2011-02-07 12:16:14 +1000713 size = roundup(size, PAGE_SIZE);
Chris Wilson8ffc0242011-09-14 14:14:28 +0200714 if (size == 0)
715 return -EINVAL;
Eric Anholt673a3942008-07-30 12:06:12 -0700716
717 /* Allocate the new object */
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +0000718 obj = i915_gem_object_create(dev_priv, size);
Chris Wilsonfe3db792016-04-25 13:32:13 +0100719 if (IS_ERR(obj))
720 return PTR_ERR(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700721
Chris Wilson05394f32010-11-08 19:18:58 +0000722 ret = drm_gem_handle_create(file, &obj->base, &handle);
Chris Wilson202f2fe2010-10-14 13:20:40 +0100723 /* drop reference from allocate - handle holds it now */
Chris Wilsonf0cd5182016-10-28 13:58:43 +0100724 i915_gem_object_put(obj);
Daniel Vetterd861e332013-07-24 23:25:03 +0200725 if (ret)
726 return ret;
Chris Wilson202f2fe2010-10-14 13:20:40 +0100727
Dave Airlieff72145b2011-02-07 12:16:14 +1000728 *handle_p = handle;
Eric Anholt673a3942008-07-30 12:06:12 -0700729 return 0;
730}
731
Dave Airlieff72145b2011-02-07 12:16:14 +1000732int
733i915_gem_dumb_create(struct drm_file *file,
734 struct drm_device *dev,
735 struct drm_mode_create_dumb *args)
736{
737 /* have to work out size/pitch and return them */
Paulo Zanonide45eaf2013-10-18 18:48:24 -0300738 args->pitch = ALIGN(args->width * DIV_ROUND_UP(args->bpp, 8), 64);
Dave Airlieff72145b2011-02-07 12:16:14 +1000739 args->size = args->pitch * args->height;
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +0000740 return i915_gem_create(file, to_i915(dev),
Dave Airlieda6b51d2014-12-24 13:11:17 +1000741 args->size, &args->handle);
Dave Airlieff72145b2011-02-07 12:16:14 +1000742}
743
Chris Wilsone27ab732017-06-15 13:38:49 +0100744static bool gpu_write_needs_clflush(struct drm_i915_gem_object *obj)
745{
746 return !(obj->cache_level == I915_CACHE_NONE ||
747 obj->cache_level == I915_CACHE_WT);
748}
749
Dave Airlieff72145b2011-02-07 12:16:14 +1000750/**
751 * Creates a new mm object and returns a handle to it.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +0100752 * @dev: drm device pointer
753 * @data: ioctl data blob
754 * @file: drm file pointer
Dave Airlieff72145b2011-02-07 12:16:14 +1000755 */
756int
757i915_gem_create_ioctl(struct drm_device *dev, void *data,
758 struct drm_file *file)
759{
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +0000760 struct drm_i915_private *dev_priv = to_i915(dev);
Dave Airlieff72145b2011-02-07 12:16:14 +1000761 struct drm_i915_gem_create *args = data;
Daniel Vetter63ed2cb2012-04-23 16:50:50 +0200762
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +0000763 i915_gem_flush_free_objects(dev_priv);
Chris Wilsonfbbd37b2016-10-28 13:58:42 +0100764
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +0000765 return i915_gem_create(file, dev_priv,
Dave Airlieda6b51d2014-12-24 13:11:17 +1000766 args->size, &args->handle);
Dave Airlieff72145b2011-02-07 12:16:14 +1000767}
768
Chris Wilsonef749212017-04-12 12:01:10 +0100769static inline enum fb_op_origin
770fb_write_origin(struct drm_i915_gem_object *obj, unsigned int domain)
771{
772 return (domain == I915_GEM_DOMAIN_GTT ?
773 obj->frontbuffer_ggtt_origin : ORIGIN_CPU);
774}
775
Chris Wilson71253972017-12-06 12:49:14 +0000776void i915_gem_flush_ggtt_writes(struct drm_i915_private *dev_priv)
Chris Wilsonef749212017-04-12 12:01:10 +0100777{
Chris Wilson71253972017-12-06 12:49:14 +0000778 /*
779 * No actual flushing is required for the GTT write domain for reads
780 * from the GTT domain. Writes to it "immediately" go to main memory
781 * as far as we know, so there's no chipset flush. It also doesn't
782 * land in the GPU render cache.
Chris Wilsonef749212017-04-12 12:01:10 +0100783 *
784 * However, we do have to enforce the order so that all writes through
785 * the GTT land before any writes to the device, such as updates to
786 * the GATT itself.
787 *
788 * We also have to wait a bit for the writes to land from the GTT.
789 * An uncached read (i.e. mmio) seems to be ideal for the round-trip
790 * timing. This issue has only been observed when switching quickly
791 * between GTT writes and CPU reads from inside the kernel on recent hw,
792 * and it appears to only affect discrete GTT blocks (i.e. on LLC
Chris Wilson71253972017-12-06 12:49:14 +0000793 * system agents we cannot reproduce this behaviour, until Cannonlake
794 * that was!).
Chris Wilsonef749212017-04-12 12:01:10 +0100795 */
Chris Wilson71253972017-12-06 12:49:14 +0000796
Chris Wilsonef749212017-04-12 12:01:10 +0100797 wmb();
798
Chris Wilson71253972017-12-06 12:49:14 +0000799 intel_runtime_pm_get(dev_priv);
800 spin_lock_irq(&dev_priv->uncore.lock);
801
802 POSTING_READ_FW(RING_HEAD(RENDER_RING_BASE));
803
804 spin_unlock_irq(&dev_priv->uncore.lock);
805 intel_runtime_pm_put(dev_priv);
806}
807
808static void
809flush_write_domain(struct drm_i915_gem_object *obj, unsigned int flush_domains)
810{
811 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
812 struct i915_vma *vma;
813
Christian Königc0a51fd2018-02-16 13:43:38 +0100814 if (!(obj->write_domain & flush_domains))
Chris Wilson71253972017-12-06 12:49:14 +0000815 return;
816
Christian Königc0a51fd2018-02-16 13:43:38 +0100817 switch (obj->write_domain) {
Chris Wilsonef749212017-04-12 12:01:10 +0100818 case I915_GEM_DOMAIN_GTT:
Chris Wilson71253972017-12-06 12:49:14 +0000819 i915_gem_flush_ggtt_writes(dev_priv);
Chris Wilsonef749212017-04-12 12:01:10 +0100820
821 intel_fb_obj_flush(obj,
822 fb_write_origin(obj, I915_GEM_DOMAIN_GTT));
Chris Wilson71253972017-12-06 12:49:14 +0000823
Chris Wilsone2189dd2017-12-07 21:14:07 +0000824 for_each_ggtt_vma(vma, obj) {
Chris Wilson71253972017-12-06 12:49:14 +0000825 if (vma->iomap)
826 continue;
827
828 i915_vma_unset_ggtt_write(vma);
829 }
Chris Wilsonef749212017-04-12 12:01:10 +0100830 break;
831
832 case I915_GEM_DOMAIN_CPU:
833 i915_gem_clflush_object(obj, I915_CLFLUSH_SYNC);
834 break;
Chris Wilsone27ab732017-06-15 13:38:49 +0100835
836 case I915_GEM_DOMAIN_RENDER:
837 if (gpu_write_needs_clflush(obj))
838 obj->cache_dirty = true;
839 break;
Chris Wilsonef749212017-04-12 12:01:10 +0100840 }
841
Christian Königc0a51fd2018-02-16 13:43:38 +0100842 obj->write_domain = 0;
Chris Wilsonef749212017-04-12 12:01:10 +0100843}
844
Daniel Vetter8c599672011-12-14 13:57:31 +0100845static inline int
Daniel Vetter8461d222011-12-14 13:57:32 +0100846__copy_to_user_swizzled(char __user *cpu_vaddr,
847 const char *gpu_vaddr, int gpu_offset,
848 int length)
849{
850 int ret, cpu_offset = 0;
851
852 while (length > 0) {
853 int cacheline_end = ALIGN(gpu_offset + 1, 64);
854 int this_length = min(cacheline_end - gpu_offset, length);
855 int swizzled_gpu_offset = gpu_offset ^ 64;
856
857 ret = __copy_to_user(cpu_vaddr + cpu_offset,
858 gpu_vaddr + swizzled_gpu_offset,
859 this_length);
860 if (ret)
861 return ret + length;
862
863 cpu_offset += this_length;
864 gpu_offset += this_length;
865 length -= this_length;
866 }
867
868 return 0;
869}
870
871static inline int
Ben Widawsky4f0c7cf2012-04-16 14:07:47 -0700872__copy_from_user_swizzled(char *gpu_vaddr, int gpu_offset,
873 const char __user *cpu_vaddr,
Daniel Vetter8c599672011-12-14 13:57:31 +0100874 int length)
875{
876 int ret, cpu_offset = 0;
877
878 while (length > 0) {
879 int cacheline_end = ALIGN(gpu_offset + 1, 64);
880 int this_length = min(cacheline_end - gpu_offset, length);
881 int swizzled_gpu_offset = gpu_offset ^ 64;
882
883 ret = __copy_from_user(gpu_vaddr + swizzled_gpu_offset,
884 cpu_vaddr + cpu_offset,
885 this_length);
886 if (ret)
887 return ret + length;
888
889 cpu_offset += this_length;
890 gpu_offset += this_length;
891 length -= this_length;
892 }
893
894 return 0;
895}
896
Brad Volkin4c914c02014-02-18 10:15:45 -0800897/*
898 * Pins the specified object's pages and synchronizes the object with
899 * GPU accesses. Sets needs_clflush to non-zero if the caller should
900 * flush the object from the CPU cache.
901 */
902int i915_gem_obj_prepare_shmem_read(struct drm_i915_gem_object *obj,
Chris Wilson43394c72016-08-18 17:16:47 +0100903 unsigned int *needs_clflush)
Brad Volkin4c914c02014-02-18 10:15:45 -0800904{
905 int ret;
906
Chris Wilsone95433c2016-10-28 13:58:27 +0100907 lockdep_assert_held(&obj->base.dev->struct_mutex);
Brad Volkin4c914c02014-02-18 10:15:45 -0800908
Chris Wilsone95433c2016-10-28 13:58:27 +0100909 *needs_clflush = 0;
Chris Wilson43394c72016-08-18 17:16:47 +0100910 if (!i915_gem_object_has_struct_page(obj))
911 return -ENODEV;
Brad Volkin4c914c02014-02-18 10:15:45 -0800912
Chris Wilsone95433c2016-10-28 13:58:27 +0100913 ret = i915_gem_object_wait(obj,
914 I915_WAIT_INTERRUPTIBLE |
915 I915_WAIT_LOCKED,
916 MAX_SCHEDULE_TIMEOUT,
917 NULL);
Chris Wilsonc13d87e2016-07-20 09:21:15 +0100918 if (ret)
919 return ret;
920
Chris Wilsona4f5ea62016-10-28 13:58:35 +0100921 ret = i915_gem_object_pin_pages(obj);
Chris Wilson97649512016-08-18 17:16:50 +0100922 if (ret)
923 return ret;
924
Chris Wilsonb8f55be2017-08-11 12:11:16 +0100925 if (obj->cache_coherent & I915_BO_CACHE_COHERENT_FOR_READ ||
926 !static_cpu_has(X86_FEATURE_CLFLUSH)) {
Chris Wilson7f5f95d2017-03-10 00:09:42 +0000927 ret = i915_gem_object_set_to_cpu_domain(obj, false);
928 if (ret)
929 goto err_unpin;
930 else
931 goto out;
932 }
933
Chris Wilsonef749212017-04-12 12:01:10 +0100934 flush_write_domain(obj, ~I915_GEM_DOMAIN_CPU);
Chris Wilsona314d5c2016-08-18 17:16:48 +0100935
Chris Wilson43394c72016-08-18 17:16:47 +0100936 /* If we're not in the cpu read domain, set ourself into the gtt
937 * read domain and manually flush cachelines (if required). This
938 * optimizes for the case when the gpu will dirty the data
939 * anyway again before the next pread happens.
940 */
Chris Wilsone27ab732017-06-15 13:38:49 +0100941 if (!obj->cache_dirty &&
Christian Königc0a51fd2018-02-16 13:43:38 +0100942 !(obj->read_domains & I915_GEM_DOMAIN_CPU))
Chris Wilson7f5f95d2017-03-10 00:09:42 +0000943 *needs_clflush = CLFLUSH_BEFORE;
Brad Volkin4c914c02014-02-18 10:15:45 -0800944
Chris Wilson7f5f95d2017-03-10 00:09:42 +0000945out:
Chris Wilson97649512016-08-18 17:16:50 +0100946 /* return with the pages pinned */
Chris Wilson43394c72016-08-18 17:16:47 +0100947 return 0;
Chris Wilson97649512016-08-18 17:16:50 +0100948
949err_unpin:
950 i915_gem_object_unpin_pages(obj);
951 return ret;
Chris Wilson43394c72016-08-18 17:16:47 +0100952}
953
954int i915_gem_obj_prepare_shmem_write(struct drm_i915_gem_object *obj,
955 unsigned int *needs_clflush)
956{
957 int ret;
958
Chris Wilsone95433c2016-10-28 13:58:27 +0100959 lockdep_assert_held(&obj->base.dev->struct_mutex);
960
Chris Wilson43394c72016-08-18 17:16:47 +0100961 *needs_clflush = 0;
962 if (!i915_gem_object_has_struct_page(obj))
963 return -ENODEV;
964
Chris Wilsone95433c2016-10-28 13:58:27 +0100965 ret = i915_gem_object_wait(obj,
966 I915_WAIT_INTERRUPTIBLE |
967 I915_WAIT_LOCKED |
968 I915_WAIT_ALL,
969 MAX_SCHEDULE_TIMEOUT,
970 NULL);
Chris Wilson43394c72016-08-18 17:16:47 +0100971 if (ret)
972 return ret;
973
Chris Wilsona4f5ea62016-10-28 13:58:35 +0100974 ret = i915_gem_object_pin_pages(obj);
Chris Wilson97649512016-08-18 17:16:50 +0100975 if (ret)
976 return ret;
977
Chris Wilsonb8f55be2017-08-11 12:11:16 +0100978 if (obj->cache_coherent & I915_BO_CACHE_COHERENT_FOR_WRITE ||
979 !static_cpu_has(X86_FEATURE_CLFLUSH)) {
Chris Wilson7f5f95d2017-03-10 00:09:42 +0000980 ret = i915_gem_object_set_to_cpu_domain(obj, true);
981 if (ret)
982 goto err_unpin;
983 else
984 goto out;
985 }
986
Chris Wilsonef749212017-04-12 12:01:10 +0100987 flush_write_domain(obj, ~I915_GEM_DOMAIN_CPU);
Chris Wilsona314d5c2016-08-18 17:16:48 +0100988
Chris Wilson43394c72016-08-18 17:16:47 +0100989 /* If we're not in the cpu write domain, set ourself into the
990 * gtt write domain and manually flush cachelines (as required).
991 * This optimizes for the case when the gpu will use the data
992 * right away and we therefore have to clflush anyway.
993 */
Chris Wilsone27ab732017-06-15 13:38:49 +0100994 if (!obj->cache_dirty) {
Chris Wilson7f5f95d2017-03-10 00:09:42 +0000995 *needs_clflush |= CLFLUSH_AFTER;
Chris Wilson43394c72016-08-18 17:16:47 +0100996
Chris Wilsone27ab732017-06-15 13:38:49 +0100997 /*
998 * Same trick applies to invalidate partially written
999 * cachelines read before writing.
1000 */
Christian Königc0a51fd2018-02-16 13:43:38 +01001001 if (!(obj->read_domains & I915_GEM_DOMAIN_CPU))
Chris Wilsone27ab732017-06-15 13:38:49 +01001002 *needs_clflush |= CLFLUSH_BEFORE;
1003 }
Chris Wilson43394c72016-08-18 17:16:47 +01001004
Chris Wilson7f5f95d2017-03-10 00:09:42 +00001005out:
Chris Wilson43394c72016-08-18 17:16:47 +01001006 intel_fb_obj_invalidate(obj, ORIGIN_CPU);
Chris Wilsona4f5ea62016-10-28 13:58:35 +01001007 obj->mm.dirty = true;
Chris Wilson97649512016-08-18 17:16:50 +01001008 /* return with the pages pinned */
Chris Wilson43394c72016-08-18 17:16:47 +01001009 return 0;
Chris Wilson97649512016-08-18 17:16:50 +01001010
1011err_unpin:
1012 i915_gem_object_unpin_pages(obj);
1013 return ret;
Brad Volkin4c914c02014-02-18 10:15:45 -08001014}
1015
Daniel Vetter23c18c72012-03-25 19:47:42 +02001016static void
1017shmem_clflush_swizzled_range(char *addr, unsigned long length,
1018 bool swizzled)
1019{
Daniel Vettere7e58eb2012-03-25 19:47:43 +02001020 if (unlikely(swizzled)) {
Daniel Vetter23c18c72012-03-25 19:47:42 +02001021 unsigned long start = (unsigned long) addr;
1022 unsigned long end = (unsigned long) addr + length;
1023
1024 /* For swizzling simply ensure that we always flush both
1025 * channels. Lame, but simple and it works. Swizzled
1026 * pwrite/pread is far from a hotpath - current userspace
1027 * doesn't use it at all. */
1028 start = round_down(start, 128);
1029 end = round_up(end, 128);
1030
1031 drm_clflush_virt_range((void *)start, end - start);
1032 } else {
1033 drm_clflush_virt_range(addr, length);
1034 }
1035
1036}
1037
Daniel Vetterd174bd62012-03-25 19:47:40 +02001038/* Only difference to the fast-path function is that this can handle bit17
1039 * and uses non-atomic copy and kmap functions. */
1040static int
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001041shmem_pread_slow(struct page *page, int offset, int length,
Daniel Vetterd174bd62012-03-25 19:47:40 +02001042 char __user *user_data,
1043 bool page_do_bit17_swizzling, bool needs_clflush)
1044{
1045 char *vaddr;
1046 int ret;
1047
1048 vaddr = kmap(page);
1049 if (needs_clflush)
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001050 shmem_clflush_swizzled_range(vaddr + offset, length,
Daniel Vetter23c18c72012-03-25 19:47:42 +02001051 page_do_bit17_swizzling);
Daniel Vetterd174bd62012-03-25 19:47:40 +02001052
1053 if (page_do_bit17_swizzling)
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001054 ret = __copy_to_user_swizzled(user_data, vaddr, offset, length);
Daniel Vetterd174bd62012-03-25 19:47:40 +02001055 else
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001056 ret = __copy_to_user(user_data, vaddr + offset, length);
Daniel Vetterd174bd62012-03-25 19:47:40 +02001057 kunmap(page);
1058
Chris Wilsonf60d7f02012-09-04 21:02:56 +01001059 return ret ? - EFAULT : 0;
Daniel Vetterd174bd62012-03-25 19:47:40 +02001060}
1061
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001062static int
1063shmem_pread(struct page *page, int offset, int length, char __user *user_data,
1064 bool page_do_bit17_swizzling, bool needs_clflush)
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301065{
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001066 int ret;
1067
1068 ret = -ENODEV;
1069 if (!page_do_bit17_swizzling) {
1070 char *vaddr = kmap_atomic(page);
1071
1072 if (needs_clflush)
1073 drm_clflush_virt_range(vaddr + offset, length);
1074 ret = __copy_to_user_inatomic(user_data, vaddr + offset, length);
1075 kunmap_atomic(vaddr);
1076 }
1077 if (ret == 0)
1078 return 0;
1079
1080 return shmem_pread_slow(page, offset, length, user_data,
1081 page_do_bit17_swizzling, needs_clflush);
1082}
1083
1084static int
1085i915_gem_shmem_pread(struct drm_i915_gem_object *obj,
1086 struct drm_i915_gem_pread *args)
1087{
1088 char __user *user_data;
1089 u64 remain;
1090 unsigned int obj_do_bit17_swizzling;
1091 unsigned int needs_clflush;
1092 unsigned int idx, offset;
1093 int ret;
1094
1095 obj_do_bit17_swizzling = 0;
1096 if (i915_gem_object_needs_bit17_swizzle(obj))
1097 obj_do_bit17_swizzling = BIT(17);
1098
1099 ret = mutex_lock_interruptible(&obj->base.dev->struct_mutex);
1100 if (ret)
1101 return ret;
1102
1103 ret = i915_gem_obj_prepare_shmem_read(obj, &needs_clflush);
1104 mutex_unlock(&obj->base.dev->struct_mutex);
1105 if (ret)
1106 return ret;
1107
1108 remain = args->size;
1109 user_data = u64_to_user_ptr(args->data_ptr);
1110 offset = offset_in_page(args->offset);
1111 for (idx = args->offset >> PAGE_SHIFT; remain; idx++) {
1112 struct page *page = i915_gem_object_get_page(obj, idx);
1113 int length;
1114
1115 length = remain;
1116 if (offset + length > PAGE_SIZE)
1117 length = PAGE_SIZE - offset;
1118
1119 ret = shmem_pread(page, offset, length, user_data,
1120 page_to_phys(page) & obj_do_bit17_swizzling,
1121 needs_clflush);
1122 if (ret)
1123 break;
1124
1125 remain -= length;
1126 user_data += length;
1127 offset = 0;
1128 }
1129
1130 i915_gem_obj_finish_shmem_access(obj);
1131 return ret;
1132}
1133
1134static inline bool
1135gtt_user_read(struct io_mapping *mapping,
1136 loff_t base, int offset,
1137 char __user *user_data, int length)
1138{
Ville Syrjäläafe722b2017-09-01 20:12:52 +03001139 void __iomem *vaddr;
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001140 unsigned long unwritten;
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301141
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301142 /* We can use the cpu mem copy function because this is X86. */
Ville Syrjäläafe722b2017-09-01 20:12:52 +03001143 vaddr = io_mapping_map_atomic_wc(mapping, base);
1144 unwritten = __copy_to_user_inatomic(user_data,
1145 (void __force *)vaddr + offset,
1146 length);
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001147 io_mapping_unmap_atomic(vaddr);
1148 if (unwritten) {
Ville Syrjäläafe722b2017-09-01 20:12:52 +03001149 vaddr = io_mapping_map_wc(mapping, base, PAGE_SIZE);
1150 unwritten = copy_to_user(user_data,
1151 (void __force *)vaddr + offset,
1152 length);
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001153 io_mapping_unmap(vaddr);
1154 }
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301155 return unwritten;
1156}
1157
1158static int
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001159i915_gem_gtt_pread(struct drm_i915_gem_object *obj,
1160 const struct drm_i915_gem_pread *args)
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301161{
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001162 struct drm_i915_private *i915 = to_i915(obj->base.dev);
1163 struct i915_ggtt *ggtt = &i915->ggtt;
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301164 struct drm_mm_node node;
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001165 struct i915_vma *vma;
1166 void __user *user_data;
1167 u64 remain, offset;
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301168 int ret;
1169
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001170 ret = mutex_lock_interruptible(&i915->drm.struct_mutex);
1171 if (ret)
1172 return ret;
1173
1174 intel_runtime_pm_get(i915);
1175 vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0,
Chris Wilsona3259ca2017-10-09 09:44:00 +01001176 PIN_MAPPABLE |
1177 PIN_NONFAULT |
1178 PIN_NONBLOCK);
Chris Wilson18034582016-08-18 17:16:45 +01001179 if (!IS_ERR(vma)) {
1180 node.start = i915_ggtt_offset(vma);
1181 node.allocated = false;
Chris Wilson49ef5292016-08-18 17:17:00 +01001182 ret = i915_vma_put_fence(vma);
Chris Wilson18034582016-08-18 17:16:45 +01001183 if (ret) {
1184 i915_vma_unpin(vma);
1185 vma = ERR_PTR(ret);
1186 }
1187 }
Chris Wilson058d88c2016-08-15 10:49:06 +01001188 if (IS_ERR(vma)) {
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001189 ret = insert_mappable_node(ggtt, &node, PAGE_SIZE);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301190 if (ret)
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001191 goto out_unlock;
1192 GEM_BUG_ON(!node.allocated);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301193 }
1194
1195 ret = i915_gem_object_set_to_gtt_domain(obj, false);
1196 if (ret)
1197 goto out_unpin;
1198
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001199 mutex_unlock(&i915->drm.struct_mutex);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301200
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001201 user_data = u64_to_user_ptr(args->data_ptr);
1202 remain = args->size;
1203 offset = args->offset;
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301204
1205 while (remain > 0) {
1206 /* Operation in this page
1207 *
1208 * page_base = page offset within aperture
1209 * page_offset = offset within page
1210 * page_length = bytes to copy for this page
1211 */
1212 u32 page_base = node.start;
1213 unsigned page_offset = offset_in_page(offset);
1214 unsigned page_length = PAGE_SIZE - page_offset;
1215 page_length = remain < page_length ? remain : page_length;
1216 if (node.allocated) {
1217 wmb();
1218 ggtt->base.insert_page(&ggtt->base,
1219 i915_gem_object_get_dma_address(obj, offset >> PAGE_SHIFT),
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001220 node.start, I915_CACHE_NONE, 0);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301221 wmb();
1222 } else {
1223 page_base += offset & PAGE_MASK;
1224 }
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001225
Matthew Auld73ebd502017-12-11 15:18:20 +00001226 if (gtt_user_read(&ggtt->iomap, page_base, page_offset,
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001227 user_data, page_length)) {
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301228 ret = -EFAULT;
1229 break;
1230 }
1231
1232 remain -= page_length;
1233 user_data += page_length;
1234 offset += page_length;
1235 }
1236
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001237 mutex_lock(&i915->drm.struct_mutex);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301238out_unpin:
1239 if (node.allocated) {
1240 wmb();
1241 ggtt->base.clear_range(&ggtt->base,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02001242 node.start, node.size);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301243 remove_mappable_node(&node);
1244 } else {
Chris Wilson058d88c2016-08-15 10:49:06 +01001245 i915_vma_unpin(vma);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301246 }
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001247out_unlock:
1248 intel_runtime_pm_put(i915);
1249 mutex_unlock(&i915->drm.struct_mutex);
Chris Wilsonf60d7f02012-09-04 21:02:56 +01001250
Eric Anholteb014592009-03-10 11:44:52 -07001251 return ret;
1252}
1253
Eric Anholt673a3942008-07-30 12:06:12 -07001254/**
1255 * Reads data from the object referenced by handle.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01001256 * @dev: drm device pointer
1257 * @data: ioctl data blob
1258 * @file: drm file pointer
Eric Anholt673a3942008-07-30 12:06:12 -07001259 *
1260 * On error, the contents of *data are undefined.
1261 */
1262int
1263i915_gem_pread_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00001264 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001265{
1266 struct drm_i915_gem_pread *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00001267 struct drm_i915_gem_object *obj;
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001268 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07001269
Chris Wilson51311d02010-11-17 09:10:42 +00001270 if (args->size == 0)
1271 return 0;
1272
1273 if (!access_ok(VERIFY_WRITE,
Gustavo Padovan3ed605b2016-04-26 12:32:27 -03001274 u64_to_user_ptr(args->data_ptr),
Chris Wilson51311d02010-11-17 09:10:42 +00001275 args->size))
1276 return -EFAULT;
1277
Chris Wilson03ac0642016-07-20 13:31:51 +01001278 obj = i915_gem_object_lookup(file, args->handle);
Chris Wilson258a5ed2016-08-05 10:14:16 +01001279 if (!obj)
1280 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07001281
Chris Wilson7dcd2492010-09-26 20:21:44 +01001282 /* Bounds check source. */
Matthew Auld966d5bf2016-12-13 20:32:22 +00001283 if (range_overflows_t(u64, args->offset, args->size, obj->base.size)) {
Chris Wilsonce9d4192010-09-26 20:50:05 +01001284 ret = -EINVAL;
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001285 goto out;
Chris Wilsonce9d4192010-09-26 20:50:05 +01001286 }
1287
Chris Wilsondb53a302011-02-03 11:57:46 +00001288 trace_i915_gem_object_pread(obj, args->offset, args->size);
1289
Chris Wilsone95433c2016-10-28 13:58:27 +01001290 ret = i915_gem_object_wait(obj,
1291 I915_WAIT_INTERRUPTIBLE,
1292 MAX_SCHEDULE_TIMEOUT,
1293 to_rps_client(file));
Chris Wilson258a5ed2016-08-05 10:14:16 +01001294 if (ret)
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001295 goto out;
Chris Wilson258a5ed2016-08-05 10:14:16 +01001296
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001297 ret = i915_gem_object_pin_pages(obj);
Chris Wilson258a5ed2016-08-05 10:14:16 +01001298 if (ret)
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001299 goto out;
Chris Wilson258a5ed2016-08-05 10:14:16 +01001300
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001301 ret = i915_gem_shmem_pread(obj, args);
Chris Wilson9c870d02016-10-24 13:42:15 +01001302 if (ret == -EFAULT || ret == -ENODEV)
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001303 ret = i915_gem_gtt_pread(obj, args);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301304
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001305 i915_gem_object_unpin_pages(obj);
1306out:
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001307 i915_gem_object_put(obj);
Eric Anholteb014592009-03-10 11:44:52 -07001308 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07001309}
1310
Keith Packard0839ccb2008-10-30 19:38:48 -07001311/* This is the fast write path which cannot handle
1312 * page faults in the source data
Linus Torvalds9b7530cc2008-10-20 14:16:43 -07001313 */
Linus Torvalds9b7530cc2008-10-20 14:16:43 -07001314
Chris Wilsonfe115622016-10-28 13:58:40 +01001315static inline bool
1316ggtt_write(struct io_mapping *mapping,
1317 loff_t base, int offset,
1318 char __user *user_data, int length)
Keith Packard0839ccb2008-10-30 19:38:48 -07001319{
Ville Syrjäläafe722b2017-09-01 20:12:52 +03001320 void __iomem *vaddr;
Keith Packard0839ccb2008-10-30 19:38:48 -07001321 unsigned long unwritten;
1322
Ben Widawsky4f0c7cf2012-04-16 14:07:47 -07001323 /* We can use the cpu mem copy function because this is X86. */
Ville Syrjäläafe722b2017-09-01 20:12:52 +03001324 vaddr = io_mapping_map_atomic_wc(mapping, base);
1325 unwritten = __copy_from_user_inatomic_nocache((void __force *)vaddr + offset,
Keith Packard0839ccb2008-10-30 19:38:48 -07001326 user_data, length);
Chris Wilsonfe115622016-10-28 13:58:40 +01001327 io_mapping_unmap_atomic(vaddr);
1328 if (unwritten) {
Ville Syrjäläafe722b2017-09-01 20:12:52 +03001329 vaddr = io_mapping_map_wc(mapping, base, PAGE_SIZE);
1330 unwritten = copy_from_user((void __force *)vaddr + offset,
1331 user_data, length);
Chris Wilsonfe115622016-10-28 13:58:40 +01001332 io_mapping_unmap(vaddr);
1333 }
Keith Packard0839ccb2008-10-30 19:38:48 -07001334
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001335 return unwritten;
1336}
1337
Eric Anholt3de09aa2009-03-09 09:42:23 -07001338/**
1339 * This is the fast pwrite path, where we copy the data directly from the
1340 * user into the GTT, uncached.
Chris Wilsonfe115622016-10-28 13:58:40 +01001341 * @obj: i915 GEM object
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01001342 * @args: pwrite arguments structure
Eric Anholt3de09aa2009-03-09 09:42:23 -07001343 */
Eric Anholt673a3942008-07-30 12:06:12 -07001344static int
Chris Wilsonfe115622016-10-28 13:58:40 +01001345i915_gem_gtt_pwrite_fast(struct drm_i915_gem_object *obj,
1346 const struct drm_i915_gem_pwrite *args)
Eric Anholt673a3942008-07-30 12:06:12 -07001347{
Chris Wilsonfe115622016-10-28 13:58:40 +01001348 struct drm_i915_private *i915 = to_i915(obj->base.dev);
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301349 struct i915_ggtt *ggtt = &i915->ggtt;
1350 struct drm_mm_node node;
Chris Wilsonfe115622016-10-28 13:58:40 +01001351 struct i915_vma *vma;
1352 u64 remain, offset;
1353 void __user *user_data;
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301354 int ret;
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301355
Chris Wilsonfe115622016-10-28 13:58:40 +01001356 ret = mutex_lock_interruptible(&i915->drm.struct_mutex);
1357 if (ret)
1358 return ret;
Daniel Vetter935aaa62012-03-25 19:47:35 +02001359
Chris Wilson8bd81812017-10-19 07:37:33 +01001360 if (i915_gem_object_has_struct_page(obj)) {
1361 /*
1362 * Avoid waking the device up if we can fallback, as
1363 * waking/resuming is very slow (worst-case 10-100 ms
1364 * depending on PCI sleeps and our own resume time).
1365 * This easily dwarfs any performance advantage from
1366 * using the cache bypass of indirect GGTT access.
1367 */
1368 if (!intel_runtime_pm_get_if_in_use(i915)) {
1369 ret = -EFAULT;
1370 goto out_unlock;
1371 }
1372 } else {
1373 /* No backing pages, no fallback, we must force GGTT access */
1374 intel_runtime_pm_get(i915);
1375 }
1376
Chris Wilson058d88c2016-08-15 10:49:06 +01001377 vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0,
Chris Wilsona3259ca2017-10-09 09:44:00 +01001378 PIN_MAPPABLE |
1379 PIN_NONFAULT |
1380 PIN_NONBLOCK);
Chris Wilson18034582016-08-18 17:16:45 +01001381 if (!IS_ERR(vma)) {
1382 node.start = i915_ggtt_offset(vma);
1383 node.allocated = false;
Chris Wilson49ef5292016-08-18 17:17:00 +01001384 ret = i915_vma_put_fence(vma);
Chris Wilson18034582016-08-18 17:16:45 +01001385 if (ret) {
1386 i915_vma_unpin(vma);
1387 vma = ERR_PTR(ret);
1388 }
1389 }
Chris Wilson058d88c2016-08-15 10:49:06 +01001390 if (IS_ERR(vma)) {
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001391 ret = insert_mappable_node(ggtt, &node, PAGE_SIZE);
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301392 if (ret)
Chris Wilson8bd81812017-10-19 07:37:33 +01001393 goto out_rpm;
Chris Wilsonfe115622016-10-28 13:58:40 +01001394 GEM_BUG_ON(!node.allocated);
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301395 }
Daniel Vetter935aaa62012-03-25 19:47:35 +02001396
1397 ret = i915_gem_object_set_to_gtt_domain(obj, true);
1398 if (ret)
1399 goto out_unpin;
1400
Chris Wilsonfe115622016-10-28 13:58:40 +01001401 mutex_unlock(&i915->drm.struct_mutex);
1402
Chris Wilsonb19482d2016-08-18 17:16:43 +01001403 intel_fb_obj_invalidate(obj, ORIGIN_CPU);
Paulo Zanoni063e4e62015-02-13 17:23:45 -02001404
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301405 user_data = u64_to_user_ptr(args->data_ptr);
1406 offset = args->offset;
1407 remain = args->size;
1408 while (remain) {
Eric Anholt673a3942008-07-30 12:06:12 -07001409 /* Operation in this page
1410 *
Keith Packard0839ccb2008-10-30 19:38:48 -07001411 * page_base = page offset within aperture
1412 * page_offset = offset within page
1413 * page_length = bytes to copy for this page
Eric Anholt673a3942008-07-30 12:06:12 -07001414 */
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301415 u32 page_base = node.start;
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001416 unsigned int page_offset = offset_in_page(offset);
1417 unsigned int page_length = PAGE_SIZE - page_offset;
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301418 page_length = remain < page_length ? remain : page_length;
1419 if (node.allocated) {
1420 wmb(); /* flush the write before we modify the GGTT */
1421 ggtt->base.insert_page(&ggtt->base,
1422 i915_gem_object_get_dma_address(obj, offset >> PAGE_SHIFT),
1423 node.start, I915_CACHE_NONE, 0);
1424 wmb(); /* flush modifications to the GGTT (insert_page) */
1425 } else {
1426 page_base += offset & PAGE_MASK;
1427 }
Keith Packard0839ccb2008-10-30 19:38:48 -07001428 /* If we get a fault while copying data, then (presumably) our
Eric Anholt3de09aa2009-03-09 09:42:23 -07001429 * source page isn't available. Return the error and we'll
1430 * retry in the slow path.
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301431 * If the object is non-shmem backed, we retry again with the
1432 * path that handles page fault.
Keith Packard0839ccb2008-10-30 19:38:48 -07001433 */
Matthew Auld73ebd502017-12-11 15:18:20 +00001434 if (ggtt_write(&ggtt->iomap, page_base, page_offset,
Chris Wilsonfe115622016-10-28 13:58:40 +01001435 user_data, page_length)) {
1436 ret = -EFAULT;
1437 break;
Daniel Vetter935aaa62012-03-25 19:47:35 +02001438 }
Eric Anholt673a3942008-07-30 12:06:12 -07001439
Keith Packard0839ccb2008-10-30 19:38:48 -07001440 remain -= page_length;
1441 user_data += page_length;
1442 offset += page_length;
Eric Anholt673a3942008-07-30 12:06:12 -07001443 }
Chris Wilsond59b21e2017-02-22 11:40:49 +00001444 intel_fb_obj_flush(obj, ORIGIN_CPU);
Chris Wilsonfe115622016-10-28 13:58:40 +01001445
1446 mutex_lock(&i915->drm.struct_mutex);
Daniel Vetter935aaa62012-03-25 19:47:35 +02001447out_unpin:
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301448 if (node.allocated) {
1449 wmb();
1450 ggtt->base.clear_range(&ggtt->base,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02001451 node.start, node.size);
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301452 remove_mappable_node(&node);
1453 } else {
Chris Wilson058d88c2016-08-15 10:49:06 +01001454 i915_vma_unpin(vma);
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301455 }
Chris Wilson8bd81812017-10-19 07:37:33 +01001456out_rpm:
Chris Wilson9c870d02016-10-24 13:42:15 +01001457 intel_runtime_pm_put(i915);
Chris Wilson8bd81812017-10-19 07:37:33 +01001458out_unlock:
Chris Wilsonfe115622016-10-28 13:58:40 +01001459 mutex_unlock(&i915->drm.struct_mutex);
Eric Anholt3de09aa2009-03-09 09:42:23 -07001460 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07001461}
1462
Eric Anholt673a3942008-07-30 12:06:12 -07001463static int
Chris Wilsonfe115622016-10-28 13:58:40 +01001464shmem_pwrite_slow(struct page *page, int offset, int length,
Daniel Vetterd174bd62012-03-25 19:47:40 +02001465 char __user *user_data,
1466 bool page_do_bit17_swizzling,
1467 bool needs_clflush_before,
1468 bool needs_clflush_after)
Eric Anholt673a3942008-07-30 12:06:12 -07001469{
Daniel Vetterd174bd62012-03-25 19:47:40 +02001470 char *vaddr;
1471 int ret;
Eric Anholt40123c12009-03-09 13:42:30 -07001472
Daniel Vetterd174bd62012-03-25 19:47:40 +02001473 vaddr = kmap(page);
Daniel Vettere7e58eb2012-03-25 19:47:43 +02001474 if (unlikely(needs_clflush_before || page_do_bit17_swizzling))
Chris Wilsonfe115622016-10-28 13:58:40 +01001475 shmem_clflush_swizzled_range(vaddr + offset, length,
Daniel Vetter23c18c72012-03-25 19:47:42 +02001476 page_do_bit17_swizzling);
Daniel Vetterd174bd62012-03-25 19:47:40 +02001477 if (page_do_bit17_swizzling)
Chris Wilsonfe115622016-10-28 13:58:40 +01001478 ret = __copy_from_user_swizzled(vaddr, offset, user_data,
1479 length);
Daniel Vetterd174bd62012-03-25 19:47:40 +02001480 else
Chris Wilsonfe115622016-10-28 13:58:40 +01001481 ret = __copy_from_user(vaddr + offset, user_data, length);
Daniel Vetterd174bd62012-03-25 19:47:40 +02001482 if (needs_clflush_after)
Chris Wilsonfe115622016-10-28 13:58:40 +01001483 shmem_clflush_swizzled_range(vaddr + offset, length,
Daniel Vetter23c18c72012-03-25 19:47:42 +02001484 page_do_bit17_swizzling);
Daniel Vetterd174bd62012-03-25 19:47:40 +02001485 kunmap(page);
Chris Wilsone5281cc2010-10-28 13:45:36 +01001486
Chris Wilson755d2212012-09-04 21:02:55 +01001487 return ret ? -EFAULT : 0;
Eric Anholt40123c12009-03-09 13:42:30 -07001488}
1489
Chris Wilsonfe115622016-10-28 13:58:40 +01001490/* Per-page copy function for the shmem pwrite fastpath.
1491 * Flushes invalid cachelines before writing to the target if
1492 * needs_clflush_before is set and flushes out any written cachelines after
1493 * writing if needs_clflush is set.
1494 */
Eric Anholt40123c12009-03-09 13:42:30 -07001495static int
Chris Wilsonfe115622016-10-28 13:58:40 +01001496shmem_pwrite(struct page *page, int offset, int len, char __user *user_data,
1497 bool page_do_bit17_swizzling,
1498 bool needs_clflush_before,
1499 bool needs_clflush_after)
Eric Anholt40123c12009-03-09 13:42:30 -07001500{
Chris Wilsonfe115622016-10-28 13:58:40 +01001501 int ret;
Eric Anholt40123c12009-03-09 13:42:30 -07001502
Chris Wilsonfe115622016-10-28 13:58:40 +01001503 ret = -ENODEV;
1504 if (!page_do_bit17_swizzling) {
1505 char *vaddr = kmap_atomic(page);
1506
1507 if (needs_clflush_before)
1508 drm_clflush_virt_range(vaddr + offset, len);
1509 ret = __copy_from_user_inatomic(vaddr + offset, user_data, len);
1510 if (needs_clflush_after)
1511 drm_clflush_virt_range(vaddr + offset, len);
1512
1513 kunmap_atomic(vaddr);
1514 }
1515 if (ret == 0)
1516 return ret;
1517
1518 return shmem_pwrite_slow(page, offset, len, user_data,
1519 page_do_bit17_swizzling,
1520 needs_clflush_before,
1521 needs_clflush_after);
1522}
1523
1524static int
1525i915_gem_shmem_pwrite(struct drm_i915_gem_object *obj,
1526 const struct drm_i915_gem_pwrite *args)
1527{
1528 struct drm_i915_private *i915 = to_i915(obj->base.dev);
1529 void __user *user_data;
1530 u64 remain;
1531 unsigned int obj_do_bit17_swizzling;
1532 unsigned int partial_cacheline_write;
1533 unsigned int needs_clflush;
1534 unsigned int offset, idx;
1535 int ret;
1536
1537 ret = mutex_lock_interruptible(&i915->drm.struct_mutex);
Chris Wilson43394c72016-08-18 17:16:47 +01001538 if (ret)
1539 return ret;
Eric Anholt40123c12009-03-09 13:42:30 -07001540
Chris Wilsonfe115622016-10-28 13:58:40 +01001541 ret = i915_gem_obj_prepare_shmem_write(obj, &needs_clflush);
1542 mutex_unlock(&i915->drm.struct_mutex);
1543 if (ret)
1544 return ret;
1545
1546 obj_do_bit17_swizzling = 0;
1547 if (i915_gem_object_needs_bit17_swizzle(obj))
1548 obj_do_bit17_swizzling = BIT(17);
1549
1550 /* If we don't overwrite a cacheline completely we need to be
1551 * careful to have up-to-date data by first clflushing. Don't
1552 * overcomplicate things and flush the entire patch.
1553 */
1554 partial_cacheline_write = 0;
1555 if (needs_clflush & CLFLUSH_BEFORE)
1556 partial_cacheline_write = boot_cpu_data.x86_clflush_size - 1;
1557
Chris Wilson43394c72016-08-18 17:16:47 +01001558 user_data = u64_to_user_ptr(args->data_ptr);
Chris Wilson43394c72016-08-18 17:16:47 +01001559 remain = args->size;
Chris Wilsonfe115622016-10-28 13:58:40 +01001560 offset = offset_in_page(args->offset);
1561 for (idx = args->offset >> PAGE_SHIFT; remain; idx++) {
1562 struct page *page = i915_gem_object_get_page(obj, idx);
1563 int length;
Eric Anholt40123c12009-03-09 13:42:30 -07001564
Chris Wilsonfe115622016-10-28 13:58:40 +01001565 length = remain;
1566 if (offset + length > PAGE_SIZE)
1567 length = PAGE_SIZE - offset;
Chris Wilsone5281cc2010-10-28 13:45:36 +01001568
Chris Wilsonfe115622016-10-28 13:58:40 +01001569 ret = shmem_pwrite(page, offset, length, user_data,
1570 page_to_phys(page) & obj_do_bit17_swizzling,
1571 (offset | length) & partial_cacheline_write,
1572 needs_clflush & CLFLUSH_AFTER);
1573 if (ret)
Chris Wilson9da3da62012-06-01 15:20:22 +01001574 break;
1575
Chris Wilsonfe115622016-10-28 13:58:40 +01001576 remain -= length;
1577 user_data += length;
1578 offset = 0;
Eric Anholt40123c12009-03-09 13:42:30 -07001579 }
1580
Chris Wilsond59b21e2017-02-22 11:40:49 +00001581 intel_fb_obj_flush(obj, ORIGIN_CPU);
Chris Wilsonfe115622016-10-28 13:58:40 +01001582 i915_gem_obj_finish_shmem_access(obj);
Eric Anholt40123c12009-03-09 13:42:30 -07001583 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07001584}
1585
1586/**
1587 * Writes data to the object referenced by handle.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01001588 * @dev: drm device
1589 * @data: ioctl data blob
1590 * @file: drm file
Eric Anholt673a3942008-07-30 12:06:12 -07001591 *
1592 * On error, the contents of the buffer that were to be modified are undefined.
1593 */
1594int
1595i915_gem_pwrite_ioctl(struct drm_device *dev, void *data,
Chris Wilsonfbd5a262010-10-14 15:03:58 +01001596 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001597{
1598 struct drm_i915_gem_pwrite *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00001599 struct drm_i915_gem_object *obj;
Chris Wilson51311d02010-11-17 09:10:42 +00001600 int ret;
1601
1602 if (args->size == 0)
1603 return 0;
1604
1605 if (!access_ok(VERIFY_READ,
Gustavo Padovan3ed605b2016-04-26 12:32:27 -03001606 u64_to_user_ptr(args->data_ptr),
Chris Wilson51311d02010-11-17 09:10:42 +00001607 args->size))
1608 return -EFAULT;
1609
Chris Wilson03ac0642016-07-20 13:31:51 +01001610 obj = i915_gem_object_lookup(file, args->handle);
Chris Wilson258a5ed2016-08-05 10:14:16 +01001611 if (!obj)
1612 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07001613
Chris Wilson7dcd2492010-09-26 20:21:44 +01001614 /* Bounds check destination. */
Matthew Auld966d5bf2016-12-13 20:32:22 +00001615 if (range_overflows_t(u64, args->offset, args->size, obj->base.size)) {
Chris Wilsonce9d4192010-09-26 20:50:05 +01001616 ret = -EINVAL;
Chris Wilson258a5ed2016-08-05 10:14:16 +01001617 goto err;
Chris Wilsonce9d4192010-09-26 20:50:05 +01001618 }
1619
Chris Wilsondb53a302011-02-03 11:57:46 +00001620 trace_i915_gem_object_pwrite(obj, args->offset, args->size);
1621
Chris Wilson7c55e2c2017-03-07 12:03:38 +00001622 ret = -ENODEV;
1623 if (obj->ops->pwrite)
1624 ret = obj->ops->pwrite(obj, args);
1625 if (ret != -ENODEV)
1626 goto err;
1627
Chris Wilsone95433c2016-10-28 13:58:27 +01001628 ret = i915_gem_object_wait(obj,
1629 I915_WAIT_INTERRUPTIBLE |
1630 I915_WAIT_ALL,
1631 MAX_SCHEDULE_TIMEOUT,
1632 to_rps_client(file));
Chris Wilson258a5ed2016-08-05 10:14:16 +01001633 if (ret)
1634 goto err;
1635
Chris Wilsonfe115622016-10-28 13:58:40 +01001636 ret = i915_gem_object_pin_pages(obj);
Chris Wilson258a5ed2016-08-05 10:14:16 +01001637 if (ret)
Chris Wilsonfe115622016-10-28 13:58:40 +01001638 goto err;
Chris Wilson258a5ed2016-08-05 10:14:16 +01001639
Daniel Vetter935aaa62012-03-25 19:47:35 +02001640 ret = -EFAULT;
Eric Anholt673a3942008-07-30 12:06:12 -07001641 /* We can only do the GTT pwrite on untiled buffers, as otherwise
1642 * it would end up going through the fenced access, and we'll get
1643 * different detiling behavior between reading and writing.
1644 * pread/pwrite currently are reading and writing from the CPU
1645 * perspective, requiring manual detiling by the client.
1646 */
Chris Wilson6eae0052016-06-20 15:05:52 +01001647 if (!i915_gem_object_has_struct_page(obj) ||
Chris Wilson9c870d02016-10-24 13:42:15 +01001648 cpu_write_needs_clflush(obj))
Daniel Vetter935aaa62012-03-25 19:47:35 +02001649 /* Note that the gtt paths might fail with non-page-backed user
1650 * pointers (e.g. gtt mappings when moving data between
Chris Wilson9c870d02016-10-24 13:42:15 +01001651 * textures). Fallback to the shmem path in that case.
1652 */
Chris Wilsonfe115622016-10-28 13:58:40 +01001653 ret = i915_gem_gtt_pwrite_fast(obj, args);
Eric Anholt673a3942008-07-30 12:06:12 -07001654
Chris Wilsond1054ee2016-07-16 18:42:36 +01001655 if (ret == -EFAULT || ret == -ENOSPC) {
Chris Wilson6a2c4232014-11-04 04:51:40 -08001656 if (obj->phys_handle)
1657 ret = i915_gem_phys_pwrite(obj, args, file);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301658 else
Chris Wilsonfe115622016-10-28 13:58:40 +01001659 ret = i915_gem_shmem_pwrite(obj, args);
Chris Wilson6a2c4232014-11-04 04:51:40 -08001660 }
Daniel Vetter5c0480f2011-12-14 13:57:30 +01001661
Chris Wilsonfe115622016-10-28 13:58:40 +01001662 i915_gem_object_unpin_pages(obj);
Chris Wilson258a5ed2016-08-05 10:14:16 +01001663err:
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001664 i915_gem_object_put(obj);
Chris Wilson258a5ed2016-08-05 10:14:16 +01001665 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07001666}
1667
Chris Wilson40e62d52016-10-28 13:58:41 +01001668static void i915_gem_object_bump_inactive_ggtt(struct drm_i915_gem_object *obj)
1669{
1670 struct drm_i915_private *i915;
1671 struct list_head *list;
1672 struct i915_vma *vma;
1673
Chris Wilsonf2123812017-10-16 12:40:37 +01001674 GEM_BUG_ON(!i915_gem_object_has_pinned_pages(obj));
1675
Chris Wilsone2189dd2017-12-07 21:14:07 +00001676 for_each_ggtt_vma(vma, obj) {
Chris Wilson40e62d52016-10-28 13:58:41 +01001677 if (i915_vma_is_active(vma))
1678 continue;
1679
1680 if (!drm_mm_node_allocated(&vma->node))
1681 continue;
1682
1683 list_move_tail(&vma->vm_link, &vma->vm->inactive_list);
1684 }
1685
1686 i915 = to_i915(obj->base.dev);
Chris Wilsonf2123812017-10-16 12:40:37 +01001687 spin_lock(&i915->mm.obj_lock);
Chris Wilson40e62d52016-10-28 13:58:41 +01001688 list = obj->bind_count ? &i915->mm.bound_list : &i915->mm.unbound_list;
Chris Wilsonf2123812017-10-16 12:40:37 +01001689 list_move_tail(&obj->mm.link, list);
1690 spin_unlock(&i915->mm.obj_lock);
Chris Wilson40e62d52016-10-28 13:58:41 +01001691}
1692
Eric Anholt673a3942008-07-30 12:06:12 -07001693/**
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001694 * Called when user space prepares to use an object with the CPU, either
1695 * through the mmap ioctl's mapping or a GTT mapping.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01001696 * @dev: drm device
1697 * @data: ioctl data blob
1698 * @file: drm file
Eric Anholt673a3942008-07-30 12:06:12 -07001699 */
1700int
1701i915_gem_set_domain_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00001702 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001703{
1704 struct drm_i915_gem_set_domain *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00001705 struct drm_i915_gem_object *obj;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001706 uint32_t read_domains = args->read_domains;
1707 uint32_t write_domain = args->write_domain;
Chris Wilson40e62d52016-10-28 13:58:41 +01001708 int err;
Eric Anholt673a3942008-07-30 12:06:12 -07001709
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001710 /* Only handle setting domains to types used by the CPU. */
Chris Wilsonb8f90962016-08-05 10:14:07 +01001711 if ((write_domain | read_domains) & I915_GEM_GPU_DOMAINS)
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001712 return -EINVAL;
1713
1714 /* Having something in the write domain implies it's in the read
1715 * domain, and only that read domain. Enforce that in the request.
1716 */
1717 if (write_domain != 0 && read_domains != write_domain)
1718 return -EINVAL;
1719
Chris Wilson03ac0642016-07-20 13:31:51 +01001720 obj = i915_gem_object_lookup(file, args->handle);
Chris Wilsonb8f90962016-08-05 10:14:07 +01001721 if (!obj)
1722 return -ENOENT;
Jesse Barnes652c3932009-08-17 13:31:43 -07001723
Chris Wilson3236f572012-08-24 09:35:09 +01001724 /* Try to flush the object off the GPU without holding the lock.
1725 * We will repeat the flush holding the lock in the normal manner
1726 * to catch cases where we are gazumped.
1727 */
Chris Wilson40e62d52016-10-28 13:58:41 +01001728 err = i915_gem_object_wait(obj,
Chris Wilsone95433c2016-10-28 13:58:27 +01001729 I915_WAIT_INTERRUPTIBLE |
1730 (write_domain ? I915_WAIT_ALL : 0),
1731 MAX_SCHEDULE_TIMEOUT,
1732 to_rps_client(file));
Chris Wilson40e62d52016-10-28 13:58:41 +01001733 if (err)
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001734 goto out;
Chris Wilsonb8f90962016-08-05 10:14:07 +01001735
Tina Zhanga03f3952017-11-14 10:25:13 +00001736 /*
1737 * Proxy objects do not control access to the backing storage, ergo
1738 * they cannot be used as a means to manipulate the cache domain
1739 * tracking for that backing storage. The proxy object is always
1740 * considered to be outside of any cache domain.
1741 */
1742 if (i915_gem_object_is_proxy(obj)) {
1743 err = -ENXIO;
1744 goto out;
1745 }
1746
1747 /*
1748 * Flush and acquire obj->pages so that we are coherent through
Chris Wilson40e62d52016-10-28 13:58:41 +01001749 * direct access in memory with previous cached writes through
1750 * shmemfs and that our cache domain tracking remains valid.
1751 * For example, if the obj->filp was moved to swap without us
1752 * being notified and releasing the pages, we would mistakenly
1753 * continue to assume that the obj remained out of the CPU cached
1754 * domain.
1755 */
1756 err = i915_gem_object_pin_pages(obj);
1757 if (err)
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001758 goto out;
Chris Wilson40e62d52016-10-28 13:58:41 +01001759
1760 err = i915_mutex_lock_interruptible(dev);
1761 if (err)
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001762 goto out_unpin;
Chris Wilson3236f572012-08-24 09:35:09 +01001763
Chris Wilsone22d8e32017-04-12 12:01:11 +01001764 if (read_domains & I915_GEM_DOMAIN_WC)
1765 err = i915_gem_object_set_to_wc_domain(obj, write_domain);
1766 else if (read_domains & I915_GEM_DOMAIN_GTT)
1767 err = i915_gem_object_set_to_gtt_domain(obj, write_domain);
Chris Wilson43566de2015-01-02 16:29:29 +05301768 else
Chris Wilsone22d8e32017-04-12 12:01:11 +01001769 err = i915_gem_object_set_to_cpu_domain(obj, write_domain);
Chris Wilson40e62d52016-10-28 13:58:41 +01001770
1771 /* And bump the LRU for this access */
1772 i915_gem_object_bump_inactive_ggtt(obj);
1773
1774 mutex_unlock(&dev->struct_mutex);
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001775
Daniel Vetter031b6982015-06-26 19:35:16 +02001776 if (write_domain != 0)
Chris Wilsonef749212017-04-12 12:01:10 +01001777 intel_fb_obj_invalidate(obj,
1778 fb_write_origin(obj, write_domain));
Daniel Vetter031b6982015-06-26 19:35:16 +02001779
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001780out_unpin:
Chris Wilson40e62d52016-10-28 13:58:41 +01001781 i915_gem_object_unpin_pages(obj);
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001782out:
1783 i915_gem_object_put(obj);
Chris Wilson40e62d52016-10-28 13:58:41 +01001784 return err;
Eric Anholt673a3942008-07-30 12:06:12 -07001785}
1786
1787/**
1788 * Called when user space has done writes to this buffer
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01001789 * @dev: drm device
1790 * @data: ioctl data blob
1791 * @file: drm file
Eric Anholt673a3942008-07-30 12:06:12 -07001792 */
1793int
1794i915_gem_sw_finish_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00001795 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001796{
1797 struct drm_i915_gem_sw_finish *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00001798 struct drm_i915_gem_object *obj;
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001799
Chris Wilson03ac0642016-07-20 13:31:51 +01001800 obj = i915_gem_object_lookup(file, args->handle);
Chris Wilsonc21724c2016-08-05 10:14:19 +01001801 if (!obj)
1802 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07001803
Tina Zhanga03f3952017-11-14 10:25:13 +00001804 /*
1805 * Proxy objects are barred from CPU access, so there is no
1806 * need to ban sw_finish as it is a nop.
1807 */
1808
Eric Anholt673a3942008-07-30 12:06:12 -07001809 /* Pinned buffers may be scanout, so flush the cache */
Chris Wilson5a97bcc2017-02-22 11:40:46 +00001810 i915_gem_object_flush_if_display(obj);
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001811 i915_gem_object_put(obj);
Chris Wilson5a97bcc2017-02-22 11:40:46 +00001812
1813 return 0;
Eric Anholt673a3942008-07-30 12:06:12 -07001814}
1815
1816/**
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01001817 * i915_gem_mmap_ioctl - Maps the contents of an object, returning the address
1818 * it is mapped to.
1819 * @dev: drm device
1820 * @data: ioctl data blob
1821 * @file: drm file
Eric Anholt673a3942008-07-30 12:06:12 -07001822 *
1823 * While the mapping holds a reference on the contents of the object, it doesn't
1824 * imply a ref on the object itself.
Daniel Vetter34367382014-10-16 12:28:18 +02001825 *
1826 * IMPORTANT:
1827 *
1828 * DRM driver writers who look a this function as an example for how to do GEM
1829 * mmap support, please don't implement mmap support like here. The modern way
1830 * to implement DRM mmap support is with an mmap offset ioctl (like
1831 * i915_gem_mmap_gtt) and then using the mmap syscall on the DRM fd directly.
1832 * That way debug tooling like valgrind will understand what's going on, hiding
1833 * the mmap call in a driver private ioctl will break that. The i915 driver only
1834 * does cpu mmaps this way because we didn't know better.
Eric Anholt673a3942008-07-30 12:06:12 -07001835 */
1836int
1837i915_gem_mmap_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00001838 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001839{
1840 struct drm_i915_gem_mmap *args = data;
Chris Wilson03ac0642016-07-20 13:31:51 +01001841 struct drm_i915_gem_object *obj;
Eric Anholt673a3942008-07-30 12:06:12 -07001842 unsigned long addr;
1843
Akash Goel1816f922015-01-02 16:29:30 +05301844 if (args->flags & ~(I915_MMAP_WC))
1845 return -EINVAL;
1846
Borislav Petkov568a58e2016-03-29 17:42:01 +02001847 if (args->flags & I915_MMAP_WC && !boot_cpu_has(X86_FEATURE_PAT))
Akash Goel1816f922015-01-02 16:29:30 +05301848 return -ENODEV;
1849
Chris Wilson03ac0642016-07-20 13:31:51 +01001850 obj = i915_gem_object_lookup(file, args->handle);
1851 if (!obj)
Chris Wilsonbf79cb92010-08-04 14:19:46 +01001852 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07001853
Daniel Vetter1286ff72012-05-10 15:25:09 +02001854 /* prime objects have no backing filp to GEM mmap
1855 * pages from.
1856 */
Chris Wilson03ac0642016-07-20 13:31:51 +01001857 if (!obj->base.filp) {
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001858 i915_gem_object_put(obj);
Tina Zhang274b2462017-11-14 10:25:12 +00001859 return -ENXIO;
Daniel Vetter1286ff72012-05-10 15:25:09 +02001860 }
1861
Chris Wilson03ac0642016-07-20 13:31:51 +01001862 addr = vm_mmap(obj->base.filp, 0, args->size,
Eric Anholt673a3942008-07-30 12:06:12 -07001863 PROT_READ | PROT_WRITE, MAP_SHARED,
1864 args->offset);
Akash Goel1816f922015-01-02 16:29:30 +05301865 if (args->flags & I915_MMAP_WC) {
1866 struct mm_struct *mm = current->mm;
1867 struct vm_area_struct *vma;
1868
Michal Hocko80a89a52016-05-23 16:26:11 -07001869 if (down_write_killable(&mm->mmap_sem)) {
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001870 i915_gem_object_put(obj);
Michal Hocko80a89a52016-05-23 16:26:11 -07001871 return -EINTR;
1872 }
Akash Goel1816f922015-01-02 16:29:30 +05301873 vma = find_vma(mm, addr);
1874 if (vma)
1875 vma->vm_page_prot =
1876 pgprot_writecombine(vm_get_page_prot(vma->vm_flags));
1877 else
1878 addr = -ENOMEM;
1879 up_write(&mm->mmap_sem);
Chris Wilsonaeecc962016-06-17 14:46:39 -03001880
1881 /* This may race, but that's ok, it only gets set */
Chris Wilson50349242016-08-18 17:17:04 +01001882 WRITE_ONCE(obj->frontbuffer_ggtt_origin, ORIGIN_CPU);
Akash Goel1816f922015-01-02 16:29:30 +05301883 }
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001884 i915_gem_object_put(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001885 if (IS_ERR((void *)addr))
1886 return addr;
1887
1888 args->addr_ptr = (uint64_t) addr;
1889
1890 return 0;
1891}
1892
Chris Wilson03af84f2016-08-18 17:17:01 +01001893static unsigned int tile_row_pages(struct drm_i915_gem_object *obj)
1894{
Chris Wilson6649a0b2017-01-09 16:16:08 +00001895 return i915_gem_object_get_tile_row_size(obj) >> PAGE_SHIFT;
Chris Wilson03af84f2016-08-18 17:17:01 +01001896}
1897
Jesse Barnesde151cf2008-11-12 10:03:55 -08001898/**
Chris Wilson4cc69072016-08-25 19:05:19 +01001899 * i915_gem_mmap_gtt_version - report the current feature set for GTT mmaps
1900 *
1901 * A history of the GTT mmap interface:
1902 *
1903 * 0 - Everything had to fit into the GTT. Both parties of a memcpy had to
1904 * aligned and suitable for fencing, and still fit into the available
1905 * mappable space left by the pinned display objects. A classic problem
1906 * we called the page-fault-of-doom where we would ping-pong between
1907 * two objects that could not fit inside the GTT and so the memcpy
1908 * would page one object in at the expense of the other between every
1909 * single byte.
1910 *
1911 * 1 - Objects can be any size, and have any compatible fencing (X Y, or none
1912 * as set via i915_gem_set_tiling() [DRM_I915_GEM_SET_TILING]). If the
1913 * object is too large for the available space (or simply too large
1914 * for the mappable aperture!), a view is created instead and faulted
1915 * into userspace. (This view is aligned and sized appropriately for
1916 * fenced access.)
1917 *
Chris Wilsone22d8e32017-04-12 12:01:11 +01001918 * 2 - Recognise WC as a separate cache domain so that we can flush the
1919 * delayed writes via GTT before performing direct access via WC.
1920 *
Chris Wilson4cc69072016-08-25 19:05:19 +01001921 * Restrictions:
1922 *
1923 * * snoopable objects cannot be accessed via the GTT. It can cause machine
1924 * hangs on some architectures, corruption on others. An attempt to service
1925 * a GTT page fault from a snoopable object will generate a SIGBUS.
1926 *
1927 * * the object must be able to fit into RAM (physical memory, though no
1928 * limited to the mappable aperture).
1929 *
1930 *
1931 * Caveats:
1932 *
1933 * * a new GTT page fault will synchronize rendering from the GPU and flush
1934 * all data to system memory. Subsequent access will not be synchronized.
1935 *
1936 * * all mappings are revoked on runtime device suspend.
1937 *
1938 * * there are only 8, 16 or 32 fence registers to share between all users
1939 * (older machines require fence register for display and blitter access
1940 * as well). Contention of the fence registers will cause the previous users
1941 * to be unmapped and any new access will generate new page faults.
1942 *
1943 * * running out of memory while servicing a fault may generate a SIGBUS,
1944 * rather than the expected SIGSEGV.
1945 */
1946int i915_gem_mmap_gtt_version(void)
1947{
Chris Wilsone22d8e32017-04-12 12:01:11 +01001948 return 2;
Chris Wilson4cc69072016-08-25 19:05:19 +01001949}
1950
Chris Wilson2d4281b2017-01-10 09:56:32 +00001951static inline struct i915_ggtt_view
1952compute_partial_view(struct drm_i915_gem_object *obj,
Chris Wilson2d4281b2017-01-10 09:56:32 +00001953 pgoff_t page_offset,
1954 unsigned int chunk)
1955{
1956 struct i915_ggtt_view view;
1957
1958 if (i915_gem_object_is_tiled(obj))
1959 chunk = roundup(chunk, tile_row_pages(obj));
1960
Chris Wilson2d4281b2017-01-10 09:56:32 +00001961 view.type = I915_GGTT_VIEW_PARTIAL;
Chris Wilson8bab11932017-01-14 00:28:25 +00001962 view.partial.offset = rounddown(page_offset, chunk);
1963 view.partial.size =
Chris Wilson2d4281b2017-01-10 09:56:32 +00001964 min_t(unsigned int, chunk,
Chris Wilson8bab11932017-01-14 00:28:25 +00001965 (obj->base.size >> PAGE_SHIFT) - view.partial.offset);
Chris Wilson2d4281b2017-01-10 09:56:32 +00001966
1967 /* If the partial covers the entire object, just create a normal VMA. */
1968 if (chunk >= obj->base.size >> PAGE_SHIFT)
1969 view.type = I915_GGTT_VIEW_NORMAL;
1970
1971 return view;
1972}
1973
Chris Wilson4cc69072016-08-25 19:05:19 +01001974/**
Jesse Barnesde151cf2008-11-12 10:03:55 -08001975 * i915_gem_fault - fault a page into the GTT
Geliang Tangd9072a32015-09-15 05:58:44 -07001976 * @vmf: fault info
Jesse Barnesde151cf2008-11-12 10:03:55 -08001977 *
1978 * The fault handler is set up by drm_gem_mmap() when a object is GTT mapped
1979 * from userspace. The fault handler takes care of binding the object to
1980 * the GTT (if needed), allocating and programming a fence register (again,
1981 * only if needed based on whether the old reg is still valid or the object
1982 * is tiled) and inserting a new PTE into the faulting process.
1983 *
1984 * Note that the faulting process may involve evicting existing objects
1985 * from the GTT and/or fence registers to make room. So performance may
1986 * suffer if the GTT working set is large or there are few fence registers
1987 * left.
Chris Wilson4cc69072016-08-25 19:05:19 +01001988 *
1989 * The current feature set supported by i915_gem_fault() and thus GTT mmaps
1990 * is exposed via I915_PARAM_MMAP_GTT_VERSION (see i915_gem_mmap_gtt_version).
Jesse Barnesde151cf2008-11-12 10:03:55 -08001991 */
Dave Jiang11bac802017-02-24 14:56:41 -08001992int i915_gem_fault(struct vm_fault *vmf)
Jesse Barnesde151cf2008-11-12 10:03:55 -08001993{
Chris Wilson03af84f2016-08-18 17:17:01 +01001994#define MIN_CHUNK_PAGES ((1 << 20) >> PAGE_SHIFT) /* 1 MiB */
Dave Jiang11bac802017-02-24 14:56:41 -08001995 struct vm_area_struct *area = vmf->vma;
Chris Wilson058d88c2016-08-15 10:49:06 +01001996 struct drm_i915_gem_object *obj = to_intel_bo(area->vm_private_data);
Chris Wilson05394f32010-11-08 19:18:58 +00001997 struct drm_device *dev = obj->base.dev;
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03001998 struct drm_i915_private *dev_priv = to_i915(dev);
1999 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Chris Wilsonb8f90962016-08-05 10:14:07 +01002000 bool write = !!(vmf->flags & FAULT_FLAG_WRITE);
Chris Wilson058d88c2016-08-15 10:49:06 +01002001 struct i915_vma *vma;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002002 pgoff_t page_offset;
Chris Wilson82118872016-08-18 17:17:05 +01002003 unsigned int flags;
Chris Wilsonb8f90962016-08-05 10:14:07 +01002004 int ret;
Paulo Zanonif65c9162013-11-27 18:20:34 -02002005
Jesse Barnesde151cf2008-11-12 10:03:55 -08002006 /* We don't use vmf->pgoff since that has the fake offset */
Jan Kara1a29d852016-12-14 15:07:01 -08002007 page_offset = (vmf->address - area->vm_start) >> PAGE_SHIFT;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002008
Chris Wilsondb53a302011-02-03 11:57:46 +00002009 trace_i915_gem_object_fault(obj, page_offset, true, write);
2010
Chris Wilson6e4930f2014-02-07 18:37:06 -02002011 /* Try to flush the object off the GPU first without holding the lock.
Chris Wilsonb8f90962016-08-05 10:14:07 +01002012 * Upon acquiring the lock, we will perform our sanity checks and then
Chris Wilson6e4930f2014-02-07 18:37:06 -02002013 * repeat the flush holding the lock in the normal manner to catch cases
2014 * where we are gazumped.
2015 */
Chris Wilsone95433c2016-10-28 13:58:27 +01002016 ret = i915_gem_object_wait(obj,
2017 I915_WAIT_INTERRUPTIBLE,
2018 MAX_SCHEDULE_TIMEOUT,
2019 NULL);
Chris Wilson6e4930f2014-02-07 18:37:06 -02002020 if (ret)
Chris Wilsonb8f90962016-08-05 10:14:07 +01002021 goto err;
2022
Chris Wilson40e62d52016-10-28 13:58:41 +01002023 ret = i915_gem_object_pin_pages(obj);
2024 if (ret)
2025 goto err;
2026
Chris Wilsonb8f90962016-08-05 10:14:07 +01002027 intel_runtime_pm_get(dev_priv);
2028
2029 ret = i915_mutex_lock_interruptible(dev);
2030 if (ret)
2031 goto err_rpm;
Chris Wilson6e4930f2014-02-07 18:37:06 -02002032
Chris Wilsoneb119bd2012-12-16 12:43:36 +00002033 /* Access to snoopable pages through the GTT is incoherent. */
Tvrtko Ursulin0031fb92016-11-04 14:42:44 +00002034 if (obj->cache_level != I915_CACHE_NONE && !HAS_LLC(dev_priv)) {
Chris Wilsonddeff6e2014-05-28 16:16:41 +01002035 ret = -EFAULT;
Chris Wilsonb8f90962016-08-05 10:14:07 +01002036 goto err_unlock;
Chris Wilsoneb119bd2012-12-16 12:43:36 +00002037 }
2038
Chris Wilson82118872016-08-18 17:17:05 +01002039 /* If the object is smaller than a couple of partial vma, it is
2040 * not worth only creating a single partial vma - we may as well
2041 * clear enough space for the full object.
2042 */
2043 flags = PIN_MAPPABLE;
2044 if (obj->base.size > 2 * MIN_CHUNK_PAGES << PAGE_SHIFT)
2045 flags |= PIN_NONBLOCK | PIN_NONFAULT;
2046
Chris Wilsona61007a2016-08-18 17:17:02 +01002047 /* Now pin it into the GTT as needed */
Chris Wilson82118872016-08-18 17:17:05 +01002048 vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0, flags);
Chris Wilsona61007a2016-08-18 17:17:02 +01002049 if (IS_ERR(vma)) {
Chris Wilsona61007a2016-08-18 17:17:02 +01002050 /* Use a partial view if it is bigger than available space */
Chris Wilson2d4281b2017-01-10 09:56:32 +00002051 struct i915_ggtt_view view =
Chris Wilson8201c1f2017-01-10 09:56:33 +00002052 compute_partial_view(obj, page_offset, MIN_CHUNK_PAGES);
Chris Wilsonaa136d92016-08-18 17:17:03 +01002053
Chris Wilson50349242016-08-18 17:17:04 +01002054 /* Userspace is now writing through an untracked VMA, abandon
2055 * all hope that the hardware is able to track future writes.
2056 */
2057 obj->frontbuffer_ggtt_origin = ORIGIN_CPU;
2058
Chris Wilsona61007a2016-08-18 17:17:02 +01002059 vma = i915_gem_object_ggtt_pin(obj, &view, 0, 0, PIN_MAPPABLE);
2060 }
Chris Wilson058d88c2016-08-15 10:49:06 +01002061 if (IS_ERR(vma)) {
2062 ret = PTR_ERR(vma);
Chris Wilsonb8f90962016-08-05 10:14:07 +01002063 goto err_unlock;
Chris Wilson058d88c2016-08-15 10:49:06 +01002064 }
Jesse Barnesde151cf2008-11-12 10:03:55 -08002065
Chris Wilsonc9839302012-11-20 10:45:17 +00002066 ret = i915_gem_object_set_to_gtt_domain(obj, write);
2067 if (ret)
Chris Wilsonb8f90962016-08-05 10:14:07 +01002068 goto err_unpin;
Chris Wilsonc9839302012-11-20 10:45:17 +00002069
Chris Wilson3bd40732017-10-09 09:43:56 +01002070 ret = i915_vma_pin_fence(vma);
Chris Wilsonc9839302012-11-20 10:45:17 +00002071 if (ret)
Chris Wilsonb8f90962016-08-05 10:14:07 +01002072 goto err_unpin;
Chris Wilson7d1c4802010-08-07 21:45:03 +01002073
Chris Wilsonb90b91d2014-06-10 12:14:40 +01002074 /* Finally, remap it using the new GTT offset */
Chris Wilsonc58305a2016-08-19 16:54:28 +01002075 ret = remap_io_mapping(area,
Chris Wilson8bab11932017-01-14 00:28:25 +00002076 area->vm_start + (vma->ggtt_view.partial.offset << PAGE_SHIFT),
Matthew Auld73ebd502017-12-11 15:18:20 +00002077 (ggtt->gmadr.start + vma->node.start) >> PAGE_SHIFT,
Chris Wilsonc58305a2016-08-19 16:54:28 +01002078 min_t(u64, vma->size, area->vm_end - area->vm_start),
Matthew Auld73ebd502017-12-11 15:18:20 +00002079 &ggtt->iomap);
Chris Wilsona65adaf2017-10-09 09:43:57 +01002080 if (ret)
2081 goto err_fence;
Chris Wilsona61007a2016-08-18 17:17:02 +01002082
Chris Wilsona65adaf2017-10-09 09:43:57 +01002083 /* Mark as being mmapped into userspace for later revocation */
2084 assert_rpm_wakelock_held(dev_priv);
2085 if (!i915_vma_set_userfault(vma) && !obj->userfault_count++)
2086 list_add(&obj->userfault_link, &dev_priv->mm.userfault_list);
2087 GEM_BUG_ON(!obj->userfault_count);
2088
Chris Wilson71253972017-12-06 12:49:14 +00002089 i915_vma_set_ggtt_write(vma);
2090
Chris Wilsona65adaf2017-10-09 09:43:57 +01002091err_fence:
Chris Wilson3bd40732017-10-09 09:43:56 +01002092 i915_vma_unpin_fence(vma);
Chris Wilsonb8f90962016-08-05 10:14:07 +01002093err_unpin:
Chris Wilson058d88c2016-08-15 10:49:06 +01002094 __i915_vma_unpin(vma);
Chris Wilsonb8f90962016-08-05 10:14:07 +01002095err_unlock:
Jesse Barnesde151cf2008-11-12 10:03:55 -08002096 mutex_unlock(&dev->struct_mutex);
Chris Wilsonb8f90962016-08-05 10:14:07 +01002097err_rpm:
2098 intel_runtime_pm_put(dev_priv);
Chris Wilson40e62d52016-10-28 13:58:41 +01002099 i915_gem_object_unpin_pages(obj);
Chris Wilsonb8f90962016-08-05 10:14:07 +01002100err:
Jesse Barnesde151cf2008-11-12 10:03:55 -08002101 switch (ret) {
Chris Wilsond9bc7e92011-02-07 13:09:31 +00002102 case -EIO:
Daniel Vetter2232f032014-09-04 09:36:18 +02002103 /*
2104 * We eat errors when the gpu is terminally wedged to avoid
2105 * userspace unduly crashing (gl has no provisions for mmaps to
2106 * fail). But any other -EIO isn't ours (e.g. swap in failure)
2107 * and so needs to be reported.
2108 */
2109 if (!i915_terminally_wedged(&dev_priv->gpu_error)) {
Paulo Zanonif65c9162013-11-27 18:20:34 -02002110 ret = VM_FAULT_SIGBUS;
2111 break;
2112 }
Chris Wilson045e7692010-11-07 09:18:22 +00002113 case -EAGAIN:
Daniel Vetter571c6082013-09-12 17:57:28 +02002114 /*
2115 * EAGAIN means the gpu is hung and we'll wait for the error
2116 * handler to reset everything when re-faulting in
2117 * i915_mutex_lock_interruptible.
Chris Wilsond9bc7e92011-02-07 13:09:31 +00002118 */
Chris Wilsonc7150892009-09-23 00:43:56 +01002119 case 0:
2120 case -ERESTARTSYS:
Chris Wilsonbed636a2011-02-11 20:31:19 +00002121 case -EINTR:
Dmitry Rogozhkine79e0fe2012-10-03 17:15:26 +03002122 case -EBUSY:
2123 /*
2124 * EBUSY is ok: this just means that another thread
2125 * already did the job.
2126 */
Paulo Zanonif65c9162013-11-27 18:20:34 -02002127 ret = VM_FAULT_NOPAGE;
2128 break;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002129 case -ENOMEM:
Paulo Zanonif65c9162013-11-27 18:20:34 -02002130 ret = VM_FAULT_OOM;
2131 break;
Daniel Vettera7c2e1a2012-10-17 11:17:16 +02002132 case -ENOSPC:
Chris Wilson45d67812014-01-31 11:34:57 +00002133 case -EFAULT:
Paulo Zanonif65c9162013-11-27 18:20:34 -02002134 ret = VM_FAULT_SIGBUS;
2135 break;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002136 default:
Daniel Vettera7c2e1a2012-10-17 11:17:16 +02002137 WARN_ONCE(ret, "unhandled error in i915_gem_fault: %i\n", ret);
Paulo Zanonif65c9162013-11-27 18:20:34 -02002138 ret = VM_FAULT_SIGBUS;
2139 break;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002140 }
Paulo Zanonif65c9162013-11-27 18:20:34 -02002141 return ret;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002142}
2143
Chris Wilsona65adaf2017-10-09 09:43:57 +01002144static void __i915_gem_object_release_mmap(struct drm_i915_gem_object *obj)
2145{
2146 struct i915_vma *vma;
2147
2148 GEM_BUG_ON(!obj->userfault_count);
2149
2150 obj->userfault_count = 0;
2151 list_del(&obj->userfault_link);
2152 drm_vma_node_unmap(&obj->base.vma_node,
2153 obj->base.dev->anon_inode->i_mapping);
2154
Chris Wilsone2189dd2017-12-07 21:14:07 +00002155 for_each_ggtt_vma(vma, obj)
Chris Wilsona65adaf2017-10-09 09:43:57 +01002156 i915_vma_unset_userfault(vma);
Chris Wilsona65adaf2017-10-09 09:43:57 +01002157}
2158
Jesse Barnesde151cf2008-11-12 10:03:55 -08002159/**
Chris Wilson901782b2009-07-10 08:18:50 +01002160 * i915_gem_release_mmap - remove physical page mappings
2161 * @obj: obj in question
2162 *
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02002163 * Preserve the reservation of the mmapping with the DRM core code, but
Chris Wilson901782b2009-07-10 08:18:50 +01002164 * relinquish ownership of the pages back to the system.
2165 *
2166 * It is vital that we remove the page mapping if we have mapped a tiled
2167 * object through the GTT and then lose the fence register due to
2168 * resource pressure. Similarly if the object has been moved out of the
2169 * aperture, than pages mapped into userspace must be revoked. Removing the
2170 * mapping will then trigger a page fault on the next user access, allowing
2171 * fixup by i915_gem_fault().
2172 */
Eric Anholtd05ca302009-07-10 13:02:26 -07002173void
Chris Wilson05394f32010-11-08 19:18:58 +00002174i915_gem_release_mmap(struct drm_i915_gem_object *obj)
Chris Wilson901782b2009-07-10 08:18:50 +01002175{
Chris Wilson275f0392016-10-24 13:42:14 +01002176 struct drm_i915_private *i915 = to_i915(obj->base.dev);
Chris Wilson275f0392016-10-24 13:42:14 +01002177
Chris Wilson349f2cc2016-04-13 17:35:12 +01002178 /* Serialisation between user GTT access and our code depends upon
2179 * revoking the CPU's PTE whilst the mutex is held. The next user
2180 * pagefault then has to wait until we release the mutex.
Chris Wilson9c870d02016-10-24 13:42:15 +01002181 *
2182 * Note that RPM complicates somewhat by adding an additional
2183 * requirement that operations to the GGTT be made holding the RPM
2184 * wakeref.
Chris Wilson349f2cc2016-04-13 17:35:12 +01002185 */
Chris Wilson275f0392016-10-24 13:42:14 +01002186 lockdep_assert_held(&i915->drm.struct_mutex);
Chris Wilson9c870d02016-10-24 13:42:15 +01002187 intel_runtime_pm_get(i915);
Chris Wilson349f2cc2016-04-13 17:35:12 +01002188
Chris Wilsona65adaf2017-10-09 09:43:57 +01002189 if (!obj->userfault_count)
Chris Wilson9c870d02016-10-24 13:42:15 +01002190 goto out;
Chris Wilson901782b2009-07-10 08:18:50 +01002191
Chris Wilsona65adaf2017-10-09 09:43:57 +01002192 __i915_gem_object_release_mmap(obj);
Chris Wilson349f2cc2016-04-13 17:35:12 +01002193
2194 /* Ensure that the CPU's PTE are revoked and there are not outstanding
2195 * memory transactions from userspace before we return. The TLB
2196 * flushing implied above by changing the PTE above *should* be
2197 * sufficient, an extra barrier here just provides us with a bit
2198 * of paranoid documentation about our requirement to serialise
2199 * memory writes before touching registers / GSM.
2200 */
2201 wmb();
Chris Wilson9c870d02016-10-24 13:42:15 +01002202
2203out:
2204 intel_runtime_pm_put(i915);
Chris Wilson901782b2009-07-10 08:18:50 +01002205}
2206
Chris Wilson7c108fd2016-10-24 13:42:18 +01002207void i915_gem_runtime_suspend(struct drm_i915_private *dev_priv)
Chris Wilsoneedd10f2014-06-16 08:57:44 +01002208{
Chris Wilson3594a3e2016-10-24 13:42:16 +01002209 struct drm_i915_gem_object *obj, *on;
Chris Wilson7c108fd2016-10-24 13:42:18 +01002210 int i;
Chris Wilsoneedd10f2014-06-16 08:57:44 +01002211
Chris Wilson3594a3e2016-10-24 13:42:16 +01002212 /*
2213 * Only called during RPM suspend. All users of the userfault_list
2214 * must be holding an RPM wakeref to ensure that this can not
2215 * run concurrently with themselves (and use the struct_mutex for
2216 * protection between themselves).
2217 */
2218
2219 list_for_each_entry_safe(obj, on,
Chris Wilsona65adaf2017-10-09 09:43:57 +01002220 &dev_priv->mm.userfault_list, userfault_link)
2221 __i915_gem_object_release_mmap(obj);
Chris Wilson7c108fd2016-10-24 13:42:18 +01002222
2223 /* The fence will be lost when the device powers down. If any were
2224 * in use by hardware (i.e. they are pinned), we should not be powering
2225 * down! All other fences will be reacquired by the user upon waking.
2226 */
2227 for (i = 0; i < dev_priv->num_fence_regs; i++) {
2228 struct drm_i915_fence_reg *reg = &dev_priv->fence_regs[i];
2229
Chris Wilsone0ec3ec2017-02-03 12:57:17 +00002230 /* Ideally we want to assert that the fence register is not
2231 * live at this point (i.e. that no piece of code will be
2232 * trying to write through fence + GTT, as that both violates
2233 * our tracking of activity and associated locking/barriers,
2234 * but also is illegal given that the hw is powered down).
2235 *
2236 * Previously we used reg->pin_count as a "liveness" indicator.
2237 * That is not sufficient, and we need a more fine-grained
2238 * tool if we want to have a sanity check here.
2239 */
Chris Wilson7c108fd2016-10-24 13:42:18 +01002240
2241 if (!reg->vma)
2242 continue;
2243
Chris Wilsona65adaf2017-10-09 09:43:57 +01002244 GEM_BUG_ON(i915_vma_has_userfault(reg->vma));
Chris Wilson7c108fd2016-10-24 13:42:18 +01002245 reg->dirty = true;
2246 }
Chris Wilsoneedd10f2014-06-16 08:57:44 +01002247}
2248
Chris Wilsond8cb5082012-08-11 15:41:03 +01002249static int i915_gem_object_create_mmap_offset(struct drm_i915_gem_object *obj)
2250{
Chris Wilsonfac5e232016-07-04 11:34:36 +01002251 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
Chris Wilsonf3f61842016-08-05 10:14:14 +01002252 int err;
Chris Wilsond8cb5082012-08-11 15:41:03 +01002253
Chris Wilsonf3f61842016-08-05 10:14:14 +01002254 err = drm_gem_create_mmap_offset(&obj->base);
Chris Wilsonb42a13d2017-01-06 15:22:40 +00002255 if (likely(!err))
Chris Wilsonf3f61842016-08-05 10:14:14 +01002256 return 0;
Daniel Vetterda494d72012-12-20 15:11:16 +01002257
Chris Wilsonb42a13d2017-01-06 15:22:40 +00002258 /* Attempt to reap some mmap space from dead objects */
2259 do {
2260 err = i915_gem_wait_for_idle(dev_priv, I915_WAIT_INTERRUPTIBLE);
2261 if (err)
2262 break;
Chris Wilsond8cb5082012-08-11 15:41:03 +01002263
Chris Wilsonb42a13d2017-01-06 15:22:40 +00002264 i915_gem_drain_freed_objects(dev_priv);
Chris Wilsonf3f61842016-08-05 10:14:14 +01002265 err = drm_gem_create_mmap_offset(&obj->base);
Chris Wilsonb42a13d2017-01-06 15:22:40 +00002266 if (!err)
2267 break;
2268
2269 } while (flush_delayed_work(&dev_priv->gt.retire_work));
Daniel Vetterda494d72012-12-20 15:11:16 +01002270
Chris Wilsonf3f61842016-08-05 10:14:14 +01002271 return err;
Chris Wilsond8cb5082012-08-11 15:41:03 +01002272}
2273
2274static void i915_gem_object_free_mmap_offset(struct drm_i915_gem_object *obj)
2275{
Chris Wilsond8cb5082012-08-11 15:41:03 +01002276 drm_gem_free_mmap_offset(&obj->base);
2277}
2278
Dave Airlieda6b51d2014-12-24 13:11:17 +10002279int
Dave Airlieff72145b2011-02-07 12:16:14 +10002280i915_gem_mmap_gtt(struct drm_file *file,
2281 struct drm_device *dev,
Dave Airlieda6b51d2014-12-24 13:11:17 +10002282 uint32_t handle,
Dave Airlieff72145b2011-02-07 12:16:14 +10002283 uint64_t *offset)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002284{
Chris Wilson05394f32010-11-08 19:18:58 +00002285 struct drm_i915_gem_object *obj;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002286 int ret;
2287
Chris Wilson03ac0642016-07-20 13:31:51 +01002288 obj = i915_gem_object_lookup(file, handle);
Chris Wilsonf3f61842016-08-05 10:14:14 +01002289 if (!obj)
2290 return -ENOENT;
Chris Wilsonab182822009-09-22 18:46:17 +01002291
Chris Wilsond8cb5082012-08-11 15:41:03 +01002292 ret = i915_gem_object_create_mmap_offset(obj);
Chris Wilsonf3f61842016-08-05 10:14:14 +01002293 if (ret == 0)
2294 *offset = drm_vma_node_offset_addr(&obj->base.vma_node);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002295
Chris Wilsonf0cd5182016-10-28 13:58:43 +01002296 i915_gem_object_put(obj);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01002297 return ret;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002298}
2299
Dave Airlieff72145b2011-02-07 12:16:14 +10002300/**
2301 * i915_gem_mmap_gtt_ioctl - prepare an object for GTT mmap'ing
2302 * @dev: DRM device
2303 * @data: GTT mapping ioctl data
2304 * @file: GEM object info
2305 *
2306 * Simply returns the fake offset to userspace so it can mmap it.
2307 * The mmap call will end up in drm_gem_mmap(), which will set things
2308 * up so we can get faults in the handler above.
2309 *
2310 * The fault handler will take care of binding the object into the GTT
2311 * (since it may have been evicted to make room for something), allocating
2312 * a fence register, and mapping the appropriate aperture address into
2313 * userspace.
2314 */
2315int
2316i915_gem_mmap_gtt_ioctl(struct drm_device *dev, void *data,
2317 struct drm_file *file)
2318{
2319 struct drm_i915_gem_mmap_gtt *args = data;
2320
Dave Airlieda6b51d2014-12-24 13:11:17 +10002321 return i915_gem_mmap_gtt(file, dev, args->handle, &args->offset);
Dave Airlieff72145b2011-02-07 12:16:14 +10002322}
2323
Daniel Vetter225067e2012-08-20 10:23:20 +02002324/* Immediately discard the backing storage */
2325static void
2326i915_gem_object_truncate(struct drm_i915_gem_object *obj)
Chris Wilsone5281cc2010-10-28 13:45:36 +01002327{
Chris Wilson4d6294bf2012-08-11 15:41:05 +01002328 i915_gem_object_free_mmap_offset(obj);
Daniel Vetter1286ff72012-05-10 15:25:09 +02002329
Chris Wilson4d6294bf2012-08-11 15:41:05 +01002330 if (obj->base.filp == NULL)
2331 return;
2332
Daniel Vetter225067e2012-08-20 10:23:20 +02002333 /* Our goal here is to return as much of the memory as
2334 * is possible back to the system as we are called from OOM.
2335 * To do this we must instruct the shmfs to drop all of its
2336 * backing pages, *now*.
Chris Wilsone5281cc2010-10-28 13:45:36 +01002337 */
Chris Wilson55372522014-03-25 13:23:06 +00002338 shmem_truncate_range(file_inode(obj->base.filp), 0, (loff_t)-1);
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002339 obj->mm.madv = __I915_MADV_PURGED;
Chris Wilson4e5462e2017-03-07 13:20:31 +00002340 obj->mm.pages = ERR_PTR(-EFAULT);
Chris Wilsone5281cc2010-10-28 13:45:36 +01002341}
Chris Wilsone5281cc2010-10-28 13:45:36 +01002342
Chris Wilson55372522014-03-25 13:23:06 +00002343/* Try to discard unwanted pages */
Chris Wilson03ac84f2016-10-28 13:58:36 +01002344void __i915_gem_object_invalidate(struct drm_i915_gem_object *obj)
Daniel Vetter225067e2012-08-20 10:23:20 +02002345{
Chris Wilson55372522014-03-25 13:23:06 +00002346 struct address_space *mapping;
2347
Chris Wilson1233e2d2016-10-28 13:58:37 +01002348 lockdep_assert_held(&obj->mm.lock);
Chris Wilsonf1fa4f42017-10-13 21:26:13 +01002349 GEM_BUG_ON(i915_gem_object_has_pages(obj));
Chris Wilson1233e2d2016-10-28 13:58:37 +01002350
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002351 switch (obj->mm.madv) {
Chris Wilson55372522014-03-25 13:23:06 +00002352 case I915_MADV_DONTNEED:
2353 i915_gem_object_truncate(obj);
2354 case __I915_MADV_PURGED:
2355 return;
2356 }
2357
2358 if (obj->base.filp == NULL)
2359 return;
2360
Al Viro93c76a32015-12-04 23:45:44 -05002361 mapping = obj->base.filp->f_mapping,
Chris Wilson55372522014-03-25 13:23:06 +00002362 invalidate_mapping_pages(mapping, 0, (loff_t)-1);
Chris Wilsone5281cc2010-10-28 13:45:36 +01002363}
2364
Chris Wilson5cdf5882010-09-27 15:51:07 +01002365static void
Chris Wilson03ac84f2016-10-28 13:58:36 +01002366i915_gem_object_put_pages_gtt(struct drm_i915_gem_object *obj,
2367 struct sg_table *pages)
Eric Anholt673a3942008-07-30 12:06:12 -07002368{
Dave Gordon85d12252016-05-20 11:54:06 +01002369 struct sgt_iter sgt_iter;
2370 struct page *page;
Daniel Vetter1286ff72012-05-10 15:25:09 +02002371
Chris Wilsone5facdf2016-12-23 14:57:57 +00002372 __i915_gem_object_release_shmem(obj, pages, true);
Eric Anholt856fa192009-03-19 14:10:50 -07002373
Chris Wilson03ac84f2016-10-28 13:58:36 +01002374 i915_gem_gtt_finish_pages(obj, pages);
Imre Deake2273302015-07-09 12:59:05 +03002375
Daniel Vetter6dacfd22011-09-12 21:30:02 +02002376 if (i915_gem_object_needs_bit17_swizzle(obj))
Chris Wilson03ac84f2016-10-28 13:58:36 +01002377 i915_gem_object_save_bit_17_swizzle(obj, pages);
Eric Anholt280b7132009-03-12 16:56:27 -07002378
Chris Wilson03ac84f2016-10-28 13:58:36 +01002379 for_each_sgt_page(page, sgt_iter, pages) {
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002380 if (obj->mm.dirty)
Chris Wilson9da3da62012-06-01 15:20:22 +01002381 set_page_dirty(page);
Chris Wilson3ef94da2009-09-14 16:50:29 +01002382
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002383 if (obj->mm.madv == I915_MADV_WILLNEED)
Chris Wilson9da3da62012-06-01 15:20:22 +01002384 mark_page_accessed(page);
Chris Wilson3ef94da2009-09-14 16:50:29 +01002385
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002386 put_page(page);
Chris Wilson3ef94da2009-09-14 16:50:29 +01002387 }
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002388 obj->mm.dirty = false;
Eric Anholt673a3942008-07-30 12:06:12 -07002389
Chris Wilson03ac84f2016-10-28 13:58:36 +01002390 sg_free_table(pages);
2391 kfree(pages);
Chris Wilson37e680a2012-06-07 15:38:42 +01002392}
2393
Chris Wilson96d77632016-10-28 13:58:33 +01002394static void __i915_gem_object_reset_page_iter(struct drm_i915_gem_object *obj)
2395{
2396 struct radix_tree_iter iter;
Ville Syrjäläc23aa712017-09-01 20:12:51 +03002397 void __rcu **slot;
Chris Wilson96d77632016-10-28 13:58:33 +01002398
Chris Wilsonbea6e982017-10-26 14:00:31 +01002399 rcu_read_lock();
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002400 radix_tree_for_each_slot(slot, &obj->mm.get_page.radix, &iter, 0)
2401 radix_tree_delete(&obj->mm.get_page.radix, iter.index);
Chris Wilsonbea6e982017-10-26 14:00:31 +01002402 rcu_read_unlock();
Chris Wilson96d77632016-10-28 13:58:33 +01002403}
2404
Chris Wilson548625e2016-11-01 12:11:34 +00002405void __i915_gem_object_put_pages(struct drm_i915_gem_object *obj,
2406 enum i915_mm_subclass subclass)
Chris Wilson37e680a2012-06-07 15:38:42 +01002407{
Chris Wilsonf2123812017-10-16 12:40:37 +01002408 struct drm_i915_private *i915 = to_i915(obj->base.dev);
Chris Wilson03ac84f2016-10-28 13:58:36 +01002409 struct sg_table *pages;
Chris Wilson37e680a2012-06-07 15:38:42 +01002410
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002411 if (i915_gem_object_has_pinned_pages(obj))
Chris Wilson03ac84f2016-10-28 13:58:36 +01002412 return;
Chris Wilsona5570172012-09-04 21:02:54 +01002413
Chris Wilson15717de2016-08-04 07:52:26 +01002414 GEM_BUG_ON(obj->bind_count);
Chris Wilsonf1fa4f42017-10-13 21:26:13 +01002415 if (!i915_gem_object_has_pages(obj))
Chris Wilson1233e2d2016-10-28 13:58:37 +01002416 return;
2417
2418 /* May be called by shrinker from within get_pages() (on another bo) */
Chris Wilson548625e2016-11-01 12:11:34 +00002419 mutex_lock_nested(&obj->mm.lock, subclass);
Chris Wilson1233e2d2016-10-28 13:58:37 +01002420 if (unlikely(atomic_read(&obj->mm.pages_pin_count)))
2421 goto unlock;
Ben Widawsky3e123022013-07-31 17:00:04 -07002422
Chris Wilsona2165e32012-12-03 11:49:00 +00002423 /* ->put_pages might need to allocate memory for the bit17 swizzle
2424 * array, hence protect them from being reaped by removing them from gtt
2425 * lists early. */
Chris Wilson03ac84f2016-10-28 13:58:36 +01002426 pages = fetch_and_zero(&obj->mm.pages);
2427 GEM_BUG_ON(!pages);
Chris Wilsona2165e32012-12-03 11:49:00 +00002428
Chris Wilsonf2123812017-10-16 12:40:37 +01002429 spin_lock(&i915->mm.obj_lock);
2430 list_del(&obj->mm.link);
2431 spin_unlock(&i915->mm.obj_lock);
2432
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002433 if (obj->mm.mapping) {
Chris Wilson4b30cb22016-08-18 17:16:42 +01002434 void *ptr;
2435
Chris Wilson0ce81782017-05-17 13:09:59 +01002436 ptr = page_mask_bits(obj->mm.mapping);
Chris Wilson4b30cb22016-08-18 17:16:42 +01002437 if (is_vmalloc_addr(ptr))
2438 vunmap(ptr);
Chris Wilsonfb8621d2016-04-08 12:11:14 +01002439 else
Chris Wilson4b30cb22016-08-18 17:16:42 +01002440 kunmap(kmap_to_page(ptr));
2441
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002442 obj->mm.mapping = NULL;
Chris Wilson0a798eb2016-04-08 12:11:11 +01002443 }
2444
Chris Wilson96d77632016-10-28 13:58:33 +01002445 __i915_gem_object_reset_page_iter(obj);
2446
Chris Wilson4e5462e2017-03-07 13:20:31 +00002447 if (!IS_ERR(pages))
2448 obj->ops->put_pages(obj, pages);
2449
Matthew Aulda5c081662017-10-06 23:18:18 +01002450 obj->mm.page_sizes.phys = obj->mm.page_sizes.sg = 0;
2451
Chris Wilson1233e2d2016-10-28 13:58:37 +01002452unlock:
2453 mutex_unlock(&obj->mm.lock);
Chris Wilson6c085a72012-08-20 11:40:46 +02002454}
2455
Chris Wilson935a2f72017-02-13 17:15:13 +00002456static bool i915_sg_trim(struct sg_table *orig_st)
Tvrtko Ursulin0c40ce12016-11-09 15:13:43 +00002457{
2458 struct sg_table new_st;
2459 struct scatterlist *sg, *new_sg;
2460 unsigned int i;
2461
2462 if (orig_st->nents == orig_st->orig_nents)
Chris Wilson935a2f72017-02-13 17:15:13 +00002463 return false;
Tvrtko Ursulin0c40ce12016-11-09 15:13:43 +00002464
Chris Wilson8bfc478f2016-12-23 14:57:58 +00002465 if (sg_alloc_table(&new_st, orig_st->nents, GFP_KERNEL | __GFP_NOWARN))
Chris Wilson935a2f72017-02-13 17:15:13 +00002466 return false;
Tvrtko Ursulin0c40ce12016-11-09 15:13:43 +00002467
2468 new_sg = new_st.sgl;
2469 for_each_sg(orig_st->sgl, sg, orig_st->nents, i) {
2470 sg_set_page(new_sg, sg_page(sg), sg->length, 0);
2471 /* called before being DMA mapped, no need to copy sg->dma_* */
2472 new_sg = sg_next(new_sg);
2473 }
Chris Wilsonc2dc6cc2016-12-19 12:43:46 +00002474 GEM_BUG_ON(new_sg); /* Should walk exactly nents and hit the end */
Tvrtko Ursulin0c40ce12016-11-09 15:13:43 +00002475
2476 sg_free_table(orig_st);
2477
2478 *orig_st = new_st;
Chris Wilson935a2f72017-02-13 17:15:13 +00002479 return true;
Tvrtko Ursulin0c40ce12016-11-09 15:13:43 +00002480}
2481
Matthew Auldb91b09e2017-10-06 23:18:17 +01002482static int i915_gem_object_get_pages_gtt(struct drm_i915_gem_object *obj)
Eric Anholt673a3942008-07-30 12:06:12 -07002483{
Chris Wilsonfac5e232016-07-04 11:34:36 +01002484 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
Chris Wilsond766ef52016-12-19 12:43:45 +00002485 const unsigned long page_count = obj->base.size / PAGE_SIZE;
2486 unsigned long i;
Eric Anholt673a3942008-07-30 12:06:12 -07002487 struct address_space *mapping;
Chris Wilson9da3da62012-06-01 15:20:22 +01002488 struct sg_table *st;
2489 struct scatterlist *sg;
Dave Gordon85d12252016-05-20 11:54:06 +01002490 struct sgt_iter sgt_iter;
Eric Anholt673a3942008-07-30 12:06:12 -07002491 struct page *page;
Imre Deak90797e62013-02-18 19:28:03 +02002492 unsigned long last_pfn = 0; /* suppress gcc warning */
Tvrtko Ursulin56024522017-08-03 10:14:17 +01002493 unsigned int max_segment = i915_sg_segment_size();
Matthew Auld84e89782017-10-09 12:00:24 +01002494 unsigned int sg_page_sizes;
Chris Wilson4846bf02017-06-09 12:03:46 +01002495 gfp_t noreclaim;
Imre Deake2273302015-07-09 12:59:05 +03002496 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07002497
Chris Wilson6c085a72012-08-20 11:40:46 +02002498 /* Assert that the object is not currently in any GPU domain. As it
2499 * wasn't in the GTT, there shouldn't be any way it could have been in
2500 * a GPU cache
2501 */
Christian Königc0a51fd2018-02-16 13:43:38 +01002502 GEM_BUG_ON(obj->read_domains & I915_GEM_GPU_DOMAINS);
2503 GEM_BUG_ON(obj->write_domain & I915_GEM_GPU_DOMAINS);
Chris Wilson6c085a72012-08-20 11:40:46 +02002504
Chris Wilson9da3da62012-06-01 15:20:22 +01002505 st = kmalloc(sizeof(*st), GFP_KERNEL);
2506 if (st == NULL)
Matthew Auldb91b09e2017-10-06 23:18:17 +01002507 return -ENOMEM;
Eric Anholt673a3942008-07-30 12:06:12 -07002508
Chris Wilsond766ef52016-12-19 12:43:45 +00002509rebuild_st:
Chris Wilson9da3da62012-06-01 15:20:22 +01002510 if (sg_alloc_table(st, page_count, GFP_KERNEL)) {
Chris Wilson9da3da62012-06-01 15:20:22 +01002511 kfree(st);
Matthew Auldb91b09e2017-10-06 23:18:17 +01002512 return -ENOMEM;
Chris Wilson9da3da62012-06-01 15:20:22 +01002513 }
2514
2515 /* Get the list of pages out of our struct file. They'll be pinned
2516 * at this point until we release them.
2517 *
2518 * Fail silently without starting the shrinker
2519 */
Al Viro93c76a32015-12-04 23:45:44 -05002520 mapping = obj->base.filp->f_mapping;
Chris Wilson0f6ab552017-06-09 12:03:48 +01002521 noreclaim = mapping_gfp_constraint(mapping, ~__GFP_RECLAIM);
Chris Wilson4846bf02017-06-09 12:03:46 +01002522 noreclaim |= __GFP_NORETRY | __GFP_NOWARN;
2523
Imre Deak90797e62013-02-18 19:28:03 +02002524 sg = st->sgl;
2525 st->nents = 0;
Matthew Auld84e89782017-10-09 12:00:24 +01002526 sg_page_sizes = 0;
Imre Deak90797e62013-02-18 19:28:03 +02002527 for (i = 0; i < page_count; i++) {
Chris Wilson4846bf02017-06-09 12:03:46 +01002528 const unsigned int shrink[] = {
2529 I915_SHRINK_BOUND | I915_SHRINK_UNBOUND | I915_SHRINK_PURGEABLE,
2530 0,
2531 }, *s = shrink;
2532 gfp_t gfp = noreclaim;
2533
2534 do {
Chris Wilson6c085a72012-08-20 11:40:46 +02002535 page = shmem_read_mapping_page_gfp(mapping, i, gfp);
Chris Wilson4846bf02017-06-09 12:03:46 +01002536 if (likely(!IS_ERR(page)))
2537 break;
2538
2539 if (!*s) {
2540 ret = PTR_ERR(page);
2541 goto err_sg;
2542 }
2543
Chris Wilson912d5722017-09-06 16:19:30 -07002544 i915_gem_shrink(dev_priv, 2 * page_count, NULL, *s++);
Chris Wilson4846bf02017-06-09 12:03:46 +01002545 cond_resched();
Chris Wilson24f8e002017-03-22 11:05:21 +00002546
Chris Wilson6c085a72012-08-20 11:40:46 +02002547 /* We've tried hard to allocate the memory by reaping
2548 * our own buffer, now let the real VM do its job and
2549 * go down in flames if truly OOM.
Chris Wilson24f8e002017-03-22 11:05:21 +00002550 *
2551 * However, since graphics tend to be disposable,
2552 * defer the oom here by reporting the ENOMEM back
2553 * to userspace.
Chris Wilson6c085a72012-08-20 11:40:46 +02002554 */
Chris Wilson4846bf02017-06-09 12:03:46 +01002555 if (!*s) {
2556 /* reclaim and warn, but no oom */
2557 gfp = mapping_gfp_mask(mapping);
Chris Wilsoneaf41802017-06-09 12:03:47 +01002558
2559 /* Our bo are always dirty and so we require
2560 * kswapd to reclaim our pages (direct reclaim
2561 * does not effectively begin pageout of our
2562 * buffers on its own). However, direct reclaim
2563 * only waits for kswapd when under allocation
2564 * congestion. So as a result __GFP_RECLAIM is
2565 * unreliable and fails to actually reclaim our
2566 * dirty pages -- unless you try over and over
2567 * again with !__GFP_NORETRY. However, we still
2568 * want to fail this allocation rather than
2569 * trigger the out-of-memory killer and for
Michal Hockodbb32952017-07-12 14:36:55 -07002570 * this we want __GFP_RETRY_MAYFAIL.
Chris Wilsoneaf41802017-06-09 12:03:47 +01002571 */
Michal Hockodbb32952017-07-12 14:36:55 -07002572 gfp |= __GFP_RETRY_MAYFAIL;
Imre Deake2273302015-07-09 12:59:05 +03002573 }
Chris Wilson4846bf02017-06-09 12:03:46 +01002574 } while (1);
2575
Chris Wilson871dfbd2016-10-11 09:20:21 +01002576 if (!i ||
2577 sg->length >= max_segment ||
2578 page_to_pfn(page) != last_pfn + 1) {
Matthew Aulda5c081662017-10-06 23:18:18 +01002579 if (i) {
Matthew Auld84e89782017-10-09 12:00:24 +01002580 sg_page_sizes |= sg->length;
Imre Deak90797e62013-02-18 19:28:03 +02002581 sg = sg_next(sg);
Matthew Aulda5c081662017-10-06 23:18:18 +01002582 }
Imre Deak90797e62013-02-18 19:28:03 +02002583 st->nents++;
2584 sg_set_page(sg, page, PAGE_SIZE, 0);
2585 } else {
2586 sg->length += PAGE_SIZE;
2587 }
2588 last_pfn = page_to_pfn(page);
Daniel Vetter3bbbe702013-10-07 17:15:45 -03002589
2590 /* Check that the i965g/gm workaround works. */
2591 WARN_ON((gfp & __GFP_DMA32) && (last_pfn >= 0x00100000UL));
Eric Anholt673a3942008-07-30 12:06:12 -07002592 }
Matthew Aulda5c081662017-10-06 23:18:18 +01002593 if (sg) { /* loop terminated early; short sg table */
Matthew Auld84e89782017-10-09 12:00:24 +01002594 sg_page_sizes |= sg->length;
Konrad Rzeszutek Wilk426729d2013-06-24 11:47:48 -04002595 sg_mark_end(sg);
Matthew Aulda5c081662017-10-06 23:18:18 +01002596 }
Chris Wilson74ce6b62012-10-19 15:51:06 +01002597
Tvrtko Ursulin0c40ce12016-11-09 15:13:43 +00002598 /* Trim unused sg entries to avoid wasting memory. */
2599 i915_sg_trim(st);
2600
Chris Wilson03ac84f2016-10-28 13:58:36 +01002601 ret = i915_gem_gtt_prepare_pages(obj, st);
Chris Wilsond766ef52016-12-19 12:43:45 +00002602 if (ret) {
2603 /* DMA remapping failed? One possible cause is that
2604 * it could not reserve enough large entries, asking
2605 * for PAGE_SIZE chunks instead may be helpful.
2606 */
2607 if (max_segment > PAGE_SIZE) {
2608 for_each_sgt_page(page, sgt_iter, st)
2609 put_page(page);
2610 sg_free_table(st);
2611
2612 max_segment = PAGE_SIZE;
2613 goto rebuild_st;
2614 } else {
2615 dev_warn(&dev_priv->drm.pdev->dev,
2616 "Failed to DMA remap %lu pages\n",
2617 page_count);
2618 goto err_pages;
2619 }
2620 }
Imre Deake2273302015-07-09 12:59:05 +03002621
Eric Anholt673a3942008-07-30 12:06:12 -07002622 if (i915_gem_object_needs_bit17_swizzle(obj))
Chris Wilson03ac84f2016-10-28 13:58:36 +01002623 i915_gem_object_do_bit_17_swizzle(obj, st);
Eric Anholt673a3942008-07-30 12:06:12 -07002624
Matthew Auld84e89782017-10-09 12:00:24 +01002625 __i915_gem_object_set_pages(obj, st, sg_page_sizes);
Matthew Auldb91b09e2017-10-06 23:18:17 +01002626
2627 return 0;
Eric Anholt673a3942008-07-30 12:06:12 -07002628
Chris Wilsonb17993b2016-11-14 11:29:30 +00002629err_sg:
Imre Deak90797e62013-02-18 19:28:03 +02002630 sg_mark_end(sg);
Chris Wilsonb17993b2016-11-14 11:29:30 +00002631err_pages:
Dave Gordon85d12252016-05-20 11:54:06 +01002632 for_each_sgt_page(page, sgt_iter, st)
2633 put_page(page);
Chris Wilson9da3da62012-06-01 15:20:22 +01002634 sg_free_table(st);
2635 kfree(st);
Chris Wilson0820baf2014-03-25 13:23:03 +00002636
2637 /* shmemfs first checks if there is enough memory to allocate the page
2638 * and reports ENOSPC should there be insufficient, along with the usual
2639 * ENOMEM for a genuine allocation failure.
2640 *
2641 * We use ENOSPC in our driver to mean that we have run out of aperture
2642 * space and so want to translate the error from shmemfs back to our
2643 * usual understanding of ENOMEM.
2644 */
Imre Deake2273302015-07-09 12:59:05 +03002645 if (ret == -ENOSPC)
2646 ret = -ENOMEM;
2647
Matthew Auldb91b09e2017-10-06 23:18:17 +01002648 return ret;
Chris Wilson03ac84f2016-10-28 13:58:36 +01002649}
2650
2651void __i915_gem_object_set_pages(struct drm_i915_gem_object *obj,
Matthew Aulda5c081662017-10-06 23:18:18 +01002652 struct sg_table *pages,
Matthew Auld84e89782017-10-09 12:00:24 +01002653 unsigned int sg_page_sizes)
Chris Wilson03ac84f2016-10-28 13:58:36 +01002654{
Matthew Aulda5c081662017-10-06 23:18:18 +01002655 struct drm_i915_private *i915 = to_i915(obj->base.dev);
2656 unsigned long supported = INTEL_INFO(i915)->page_sizes;
2657 int i;
2658
Chris Wilson1233e2d2016-10-28 13:58:37 +01002659 lockdep_assert_held(&obj->mm.lock);
Chris Wilson03ac84f2016-10-28 13:58:36 +01002660
2661 obj->mm.get_page.sg_pos = pages->sgl;
2662 obj->mm.get_page.sg_idx = 0;
2663
2664 obj->mm.pages = pages;
Chris Wilson2c3a3f42016-11-04 10:30:01 +00002665
2666 if (i915_gem_object_is_tiled(obj) &&
Chris Wilsonf2123812017-10-16 12:40:37 +01002667 i915->quirks & QUIRK_PIN_SWIZZLED_PAGES) {
Chris Wilson2c3a3f42016-11-04 10:30:01 +00002668 GEM_BUG_ON(obj->mm.quirked);
2669 __i915_gem_object_pin_pages(obj);
2670 obj->mm.quirked = true;
2671 }
Matthew Aulda5c081662017-10-06 23:18:18 +01002672
Matthew Auld84e89782017-10-09 12:00:24 +01002673 GEM_BUG_ON(!sg_page_sizes);
2674 obj->mm.page_sizes.phys = sg_page_sizes;
Matthew Aulda5c081662017-10-06 23:18:18 +01002675
2676 /*
Matthew Auld84e89782017-10-09 12:00:24 +01002677 * Calculate the supported page-sizes which fit into the given
2678 * sg_page_sizes. This will give us the page-sizes which we may be able
2679 * to use opportunistically when later inserting into the GTT. For
2680 * example if phys=2G, then in theory we should be able to use 1G, 2M,
2681 * 64K or 4K pages, although in practice this will depend on a number of
2682 * other factors.
Matthew Aulda5c081662017-10-06 23:18:18 +01002683 */
2684 obj->mm.page_sizes.sg = 0;
2685 for_each_set_bit(i, &supported, ilog2(I915_GTT_MAX_PAGE_SIZE) + 1) {
2686 if (obj->mm.page_sizes.phys & ~0u << i)
2687 obj->mm.page_sizes.sg |= BIT(i);
2688 }
Matthew Aulda5c081662017-10-06 23:18:18 +01002689 GEM_BUG_ON(!HAS_PAGE_SIZES(i915, obj->mm.page_sizes.sg));
Chris Wilsonf2123812017-10-16 12:40:37 +01002690
2691 spin_lock(&i915->mm.obj_lock);
2692 list_add(&obj->mm.link, &i915->mm.unbound_list);
2693 spin_unlock(&i915->mm.obj_lock);
Chris Wilson03ac84f2016-10-28 13:58:36 +01002694}
2695
2696static int ____i915_gem_object_get_pages(struct drm_i915_gem_object *obj)
2697{
Matthew Auldb91b09e2017-10-06 23:18:17 +01002698 int err;
Chris Wilson03ac84f2016-10-28 13:58:36 +01002699
2700 if (unlikely(obj->mm.madv != I915_MADV_WILLNEED)) {
2701 DRM_DEBUG("Attempting to obtain a purgeable object\n");
2702 return -EFAULT;
2703 }
2704
Matthew Auldb91b09e2017-10-06 23:18:17 +01002705 err = obj->ops->get_pages(obj);
Matthew Auldb65a9b92017-12-18 10:38:55 +00002706 GEM_BUG_ON(!err && !i915_gem_object_has_pages(obj));
Chris Wilson03ac84f2016-10-28 13:58:36 +01002707
Matthew Auldb91b09e2017-10-06 23:18:17 +01002708 return err;
Eric Anholt673a3942008-07-30 12:06:12 -07002709}
2710
Chris Wilson37e680a2012-06-07 15:38:42 +01002711/* Ensure that the associated pages are gathered from the backing storage
Chris Wilson1233e2d2016-10-28 13:58:37 +01002712 * and pinned into our object. i915_gem_object_pin_pages() may be called
Chris Wilson37e680a2012-06-07 15:38:42 +01002713 * multiple times before they are released by a single call to
Chris Wilson1233e2d2016-10-28 13:58:37 +01002714 * i915_gem_object_unpin_pages() - once the pages are no longer referenced
Chris Wilson37e680a2012-06-07 15:38:42 +01002715 * either as a result of memory pressure (reaping pages under the shrinker)
2716 * or as the object is itself released.
2717 */
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002718int __i915_gem_object_get_pages(struct drm_i915_gem_object *obj)
Chris Wilson37e680a2012-06-07 15:38:42 +01002719{
Chris Wilson03ac84f2016-10-28 13:58:36 +01002720 int err;
Chris Wilson37e680a2012-06-07 15:38:42 +01002721
Chris Wilson1233e2d2016-10-28 13:58:37 +01002722 err = mutex_lock_interruptible(&obj->mm.lock);
2723 if (err)
2724 return err;
Chris Wilson4c7d62c2016-10-28 13:58:32 +01002725
Chris Wilsonf1fa4f42017-10-13 21:26:13 +01002726 if (unlikely(!i915_gem_object_has_pages(obj))) {
Chris Wilson88c880b2017-09-06 14:52:20 +01002727 GEM_BUG_ON(i915_gem_object_has_pinned_pages(obj));
2728
Chris Wilson2c3a3f42016-11-04 10:30:01 +00002729 err = ____i915_gem_object_get_pages(obj);
2730 if (err)
2731 goto unlock;
2732
2733 smp_mb__before_atomic();
Chris Wilson1233e2d2016-10-28 13:58:37 +01002734 }
Chris Wilson2c3a3f42016-11-04 10:30:01 +00002735 atomic_inc(&obj->mm.pages_pin_count);
Chris Wilson43e28f02013-01-08 10:53:09 +00002736
Chris Wilson1233e2d2016-10-28 13:58:37 +01002737unlock:
2738 mutex_unlock(&obj->mm.lock);
Chris Wilson03ac84f2016-10-28 13:58:36 +01002739 return err;
Eric Anholt673a3942008-07-30 12:06:12 -07002740}
2741
Dave Gordondd6034c2016-05-20 11:54:04 +01002742/* The 'mapping' part of i915_gem_object_pin_map() below */
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002743static void *i915_gem_object_map(const struct drm_i915_gem_object *obj,
2744 enum i915_map_type type)
Dave Gordondd6034c2016-05-20 11:54:04 +01002745{
2746 unsigned long n_pages = obj->base.size >> PAGE_SHIFT;
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002747 struct sg_table *sgt = obj->mm.pages;
Dave Gordon85d12252016-05-20 11:54:06 +01002748 struct sgt_iter sgt_iter;
2749 struct page *page;
Dave Gordonb338fa42016-05-20 11:54:05 +01002750 struct page *stack_pages[32];
2751 struct page **pages = stack_pages;
Dave Gordondd6034c2016-05-20 11:54:04 +01002752 unsigned long i = 0;
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002753 pgprot_t pgprot;
Dave Gordondd6034c2016-05-20 11:54:04 +01002754 void *addr;
2755
2756 /* A single page can always be kmapped */
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002757 if (n_pages == 1 && type == I915_MAP_WB)
Dave Gordondd6034c2016-05-20 11:54:04 +01002758 return kmap(sg_page(sgt->sgl));
2759
Dave Gordonb338fa42016-05-20 11:54:05 +01002760 if (n_pages > ARRAY_SIZE(stack_pages)) {
2761 /* Too big for stack -- allocate temporary array instead */
Michal Hocko0ee931c2017-09-13 16:28:29 -07002762 pages = kvmalloc_array(n_pages, sizeof(*pages), GFP_KERNEL);
Dave Gordonb338fa42016-05-20 11:54:05 +01002763 if (!pages)
2764 return NULL;
2765 }
Dave Gordondd6034c2016-05-20 11:54:04 +01002766
Dave Gordon85d12252016-05-20 11:54:06 +01002767 for_each_sgt_page(page, sgt_iter, sgt)
2768 pages[i++] = page;
Dave Gordondd6034c2016-05-20 11:54:04 +01002769
2770 /* Check that we have the expected number of pages */
2771 GEM_BUG_ON(i != n_pages);
2772
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002773 switch (type) {
Chris Wilsona575c672017-08-28 11:46:31 +01002774 default:
2775 MISSING_CASE(type);
2776 /* fallthrough to use PAGE_KERNEL anyway */
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002777 case I915_MAP_WB:
2778 pgprot = PAGE_KERNEL;
2779 break;
2780 case I915_MAP_WC:
2781 pgprot = pgprot_writecombine(PAGE_KERNEL_IO);
2782 break;
2783 }
2784 addr = vmap(pages, n_pages, 0, pgprot);
Dave Gordondd6034c2016-05-20 11:54:04 +01002785
Dave Gordonb338fa42016-05-20 11:54:05 +01002786 if (pages != stack_pages)
Michal Hocko20981052017-05-17 14:23:12 +02002787 kvfree(pages);
Dave Gordondd6034c2016-05-20 11:54:04 +01002788
2789 return addr;
2790}
2791
2792/* get, pin, and map the pages of the object into kernel space */
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002793void *i915_gem_object_pin_map(struct drm_i915_gem_object *obj,
2794 enum i915_map_type type)
Chris Wilson0a798eb2016-04-08 12:11:11 +01002795{
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002796 enum i915_map_type has_type;
2797 bool pinned;
2798 void *ptr;
Chris Wilson0a798eb2016-04-08 12:11:11 +01002799 int ret;
2800
Tina Zhanga03f3952017-11-14 10:25:13 +00002801 if (unlikely(!i915_gem_object_has_struct_page(obj)))
2802 return ERR_PTR(-ENXIO);
Chris Wilson0a798eb2016-04-08 12:11:11 +01002803
Chris Wilson1233e2d2016-10-28 13:58:37 +01002804 ret = mutex_lock_interruptible(&obj->mm.lock);
Chris Wilson0a798eb2016-04-08 12:11:11 +01002805 if (ret)
2806 return ERR_PTR(ret);
2807
Chris Wilsona575c672017-08-28 11:46:31 +01002808 pinned = !(type & I915_MAP_OVERRIDE);
2809 type &= ~I915_MAP_OVERRIDE;
2810
Chris Wilson1233e2d2016-10-28 13:58:37 +01002811 if (!atomic_inc_not_zero(&obj->mm.pages_pin_count)) {
Chris Wilsonf1fa4f42017-10-13 21:26:13 +01002812 if (unlikely(!i915_gem_object_has_pages(obj))) {
Chris Wilson88c880b2017-09-06 14:52:20 +01002813 GEM_BUG_ON(i915_gem_object_has_pinned_pages(obj));
2814
Chris Wilson2c3a3f42016-11-04 10:30:01 +00002815 ret = ____i915_gem_object_get_pages(obj);
2816 if (ret)
2817 goto err_unlock;
Chris Wilson1233e2d2016-10-28 13:58:37 +01002818
Chris Wilson2c3a3f42016-11-04 10:30:01 +00002819 smp_mb__before_atomic();
2820 }
2821 atomic_inc(&obj->mm.pages_pin_count);
Chris Wilson1233e2d2016-10-28 13:58:37 +01002822 pinned = false;
2823 }
Chris Wilsonf1fa4f42017-10-13 21:26:13 +01002824 GEM_BUG_ON(!i915_gem_object_has_pages(obj));
Chris Wilson0a798eb2016-04-08 12:11:11 +01002825
Chris Wilson0ce81782017-05-17 13:09:59 +01002826 ptr = page_unpack_bits(obj->mm.mapping, &has_type);
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002827 if (ptr && has_type != type) {
2828 if (pinned) {
2829 ret = -EBUSY;
Chris Wilson1233e2d2016-10-28 13:58:37 +01002830 goto err_unpin;
Chris Wilson0a798eb2016-04-08 12:11:11 +01002831 }
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002832
2833 if (is_vmalloc_addr(ptr))
2834 vunmap(ptr);
2835 else
2836 kunmap(kmap_to_page(ptr));
2837
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002838 ptr = obj->mm.mapping = NULL;
Chris Wilson0a798eb2016-04-08 12:11:11 +01002839 }
2840
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002841 if (!ptr) {
2842 ptr = i915_gem_object_map(obj, type);
2843 if (!ptr) {
2844 ret = -ENOMEM;
Chris Wilson1233e2d2016-10-28 13:58:37 +01002845 goto err_unpin;
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002846 }
2847
Chris Wilson0ce81782017-05-17 13:09:59 +01002848 obj->mm.mapping = page_pack_bits(ptr, type);
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002849 }
2850
Chris Wilson1233e2d2016-10-28 13:58:37 +01002851out_unlock:
2852 mutex_unlock(&obj->mm.lock);
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002853 return ptr;
2854
Chris Wilson1233e2d2016-10-28 13:58:37 +01002855err_unpin:
2856 atomic_dec(&obj->mm.pages_pin_count);
2857err_unlock:
2858 ptr = ERR_PTR(ret);
2859 goto out_unlock;
Chris Wilson0a798eb2016-04-08 12:11:11 +01002860}
2861
Chris Wilson7c55e2c2017-03-07 12:03:38 +00002862static int
2863i915_gem_object_pwrite_gtt(struct drm_i915_gem_object *obj,
2864 const struct drm_i915_gem_pwrite *arg)
2865{
2866 struct address_space *mapping = obj->base.filp->f_mapping;
2867 char __user *user_data = u64_to_user_ptr(arg->data_ptr);
2868 u64 remain, offset;
2869 unsigned int pg;
2870
2871 /* Before we instantiate/pin the backing store for our use, we
2872 * can prepopulate the shmemfs filp efficiently using a write into
2873 * the pagecache. We avoid the penalty of instantiating all the
2874 * pages, important if the user is just writing to a few and never
2875 * uses the object on the GPU, and using a direct write into shmemfs
2876 * allows it to avoid the cost of retrieving a page (either swapin
2877 * or clearing-before-use) before it is overwritten.
2878 */
Chris Wilsonf1fa4f42017-10-13 21:26:13 +01002879 if (i915_gem_object_has_pages(obj))
Chris Wilson7c55e2c2017-03-07 12:03:38 +00002880 return -ENODEV;
2881
Chris Wilsona6d65e42017-10-16 21:27:32 +01002882 if (obj->mm.madv != I915_MADV_WILLNEED)
2883 return -EFAULT;
2884
Chris Wilson7c55e2c2017-03-07 12:03:38 +00002885 /* Before the pages are instantiated the object is treated as being
2886 * in the CPU domain. The pages will be clflushed as required before
2887 * use, and we can freely write into the pages directly. If userspace
2888 * races pwrite with any other operation; corruption will ensue -
2889 * that is userspace's prerogative!
2890 */
2891
2892 remain = arg->size;
2893 offset = arg->offset;
2894 pg = offset_in_page(offset);
2895
2896 do {
2897 unsigned int len, unwritten;
2898 struct page *page;
2899 void *data, *vaddr;
2900 int err;
2901
2902 len = PAGE_SIZE - pg;
2903 if (len > remain)
2904 len = remain;
2905
2906 err = pagecache_write_begin(obj->base.filp, mapping,
2907 offset, len, 0,
2908 &page, &data);
2909 if (err < 0)
2910 return err;
2911
2912 vaddr = kmap(page);
2913 unwritten = copy_from_user(vaddr + pg, user_data, len);
2914 kunmap(page);
2915
2916 err = pagecache_write_end(obj->base.filp, mapping,
2917 offset, len, len - unwritten,
2918 page, data);
2919 if (err < 0)
2920 return err;
2921
2922 if (unwritten)
2923 return -EFAULT;
2924
2925 remain -= len;
2926 user_data += len;
2927 offset += len;
2928 pg = 0;
2929 } while (remain);
2930
2931 return 0;
2932}
2933
Mika Kuoppalae5e1fc42016-11-16 17:20:31 +02002934static void i915_gem_context_mark_guilty(struct i915_gem_context *ctx)
Mika Kuoppalaaa60c662013-06-12 15:13:20 +03002935{
Chris Wilson77b25a92017-07-21 13:32:30 +01002936 bool banned;
Mika Kuoppalab083a082016-11-18 15:10:47 +02002937
Chris Wilson77b25a92017-07-21 13:32:30 +01002938 atomic_inc(&ctx->guilty_count);
2939
Chris Wilson24eae082018-02-05 09:22:01 +00002940 banned = false;
2941 if (i915_gem_context_is_bannable(ctx)) {
2942 unsigned int score;
2943
2944 score = atomic_add_return(CONTEXT_SCORE_GUILTY,
2945 &ctx->ban_score);
2946 banned = score >= CONTEXT_SCORE_BAN_THRESHOLD;
2947
2948 DRM_DEBUG_DRIVER("context %s marked guilty (score %d) banned? %s\n",
2949 ctx->name, score, yesno(banned));
2950 }
Chris Wilson77b25a92017-07-21 13:32:30 +01002951 if (!banned)
Mika Kuoppalab083a082016-11-18 15:10:47 +02002952 return;
2953
Chris Wilson77b25a92017-07-21 13:32:30 +01002954 i915_gem_context_set_banned(ctx);
2955 if (!IS_ERR_OR_NULL(ctx->file_priv)) {
2956 atomic_inc(&ctx->file_priv->context_bans);
2957 DRM_DEBUG_DRIVER("client %s has had %d context banned\n",
2958 ctx->name, atomic_read(&ctx->file_priv->context_bans));
2959 }
Mika Kuoppalae5e1fc42016-11-16 17:20:31 +02002960}
2961
2962static void i915_gem_context_mark_innocent(struct i915_gem_context *ctx)
2963{
Chris Wilson77b25a92017-07-21 13:32:30 +01002964 atomic_inc(&ctx->active_count);
Mika Kuoppalaaa60c662013-06-12 15:13:20 +03002965}
2966
Chris Wilsone61e0f52018-02-21 09:56:36 +00002967struct i915_request *
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00002968i915_gem_find_active_request(struct intel_engine_cs *engine)
Chris Wilson9375e442010-09-19 12:21:28 +01002969{
Chris Wilsone61e0f52018-02-21 09:56:36 +00002970 struct i915_request *request, *active = NULL;
Chris Wilson754c9fd2017-02-23 07:44:14 +00002971 unsigned long flags;
Mika Kuoppalaaa60c662013-06-12 15:13:20 +03002972
Chris Wilsonf69a02c2016-07-01 17:23:16 +01002973 /* We are called by the error capture and reset at a random
2974 * point in time. In particular, note that neither is crucially
2975 * ordered with an interrupt. After a hang, the GPU is dead and we
2976 * assume that no more writes can happen (we waited long enough for
2977 * all writes that were in transaction to be flushed) - adding an
2978 * extra delay for a recent interrupt is pointless. Hence, we do
2979 * not need an engine->irq_seqno_barrier() before the seqno reads.
2980 */
Chris Wilsona89d1f92018-05-02 17:38:39 +01002981 spin_lock_irqsave(&engine->timeline.lock, flags);
2982 list_for_each_entry(request, &engine->timeline.requests, link) {
Chris Wilsone61e0f52018-02-21 09:56:36 +00002983 if (__i915_request_completed(request, request->global_seqno))
Chris Wilson4db080f2013-12-04 11:37:09 +00002984 continue;
Mika Kuoppalaaa60c662013-06-12 15:13:20 +03002985
Mika Kuoppala36193ac2017-01-17 17:59:02 +02002986 GEM_BUG_ON(request->engine != engine);
Chris Wilsonc00122f32017-02-12 17:19:58 +00002987 GEM_BUG_ON(test_bit(DMA_FENCE_FLAG_SIGNALED_BIT,
2988 &request->fence.flags));
Mika Kuoppalab6b0fac2014-01-30 19:04:43 +02002989
Chris Wilson754c9fd2017-02-23 07:44:14 +00002990 active = request;
2991 break;
2992 }
Chris Wilsona89d1f92018-05-02 17:38:39 +01002993 spin_unlock_irqrestore(&engine->timeline.lock, flags);
Chris Wilson754c9fd2017-02-23 07:44:14 +00002994
2995 return active;
Mika Kuoppalab6b0fac2014-01-30 19:04:43 +02002996}
2997
Michel Thierrya1ef70e2017-06-20 10:57:47 +01002998/*
2999 * Ensure irq handler finishes, and not run again.
3000 * Also return the active request so that we only search for it once.
3001 */
Chris Wilsone61e0f52018-02-21 09:56:36 +00003002struct i915_request *
Michel Thierrya1ef70e2017-06-20 10:57:47 +01003003i915_gem_reset_prepare_engine(struct intel_engine_cs *engine)
3004{
Chris Wilsone61e0f52018-02-21 09:56:36 +00003005 struct i915_request *request = NULL;
Michel Thierrya1ef70e2017-06-20 10:57:47 +01003006
Chris Wilson1749d902017-10-09 12:02:59 +01003007 /*
3008 * During the reset sequence, we must prevent the engine from
3009 * entering RC6. As the context state is undefined until we restart
3010 * the engine, if it does enter RC6 during the reset, the state
3011 * written to the powercontext is undefined and so we may lose
3012 * GPU state upon resume, i.e. fail to restart after a reset.
3013 */
3014 intel_uncore_forcewake_get(engine->i915, FORCEWAKE_ALL);
3015
3016 /*
3017 * Prevent the signaler thread from updating the request
Michel Thierrya1ef70e2017-06-20 10:57:47 +01003018 * state (by calling dma_fence_signal) as we are processing
3019 * the reset. The write from the GPU of the seqno is
3020 * asynchronous and the signaler thread may see a different
3021 * value to us and declare the request complete, even though
3022 * the reset routine have picked that request as the active
3023 * (incomplete) request. This conflict is not handled
3024 * gracefully!
3025 */
3026 kthread_park(engine->breadcrumbs.signaler);
3027
Chris Wilson1749d902017-10-09 12:02:59 +01003028 /*
3029 * Prevent request submission to the hardware until we have
Michel Thierrya1ef70e2017-06-20 10:57:47 +01003030 * completed the reset in i915_gem_reset_finish(). If a request
3031 * is completed by one engine, it may then queue a request
Sagar Arun Kamblec6dce8f2017-11-16 19:02:37 +05303032 * to a second via its execlists->tasklet *just* as we are
Michel Thierrya1ef70e2017-06-20 10:57:47 +01003033 * calling engine->init_hw() and also writing the ELSP.
Sagar Arun Kamblec6dce8f2017-11-16 19:02:37 +05303034 * Turning off the execlists->tasklet until the reset is over
Michel Thierrya1ef70e2017-06-20 10:57:47 +01003035 * prevents the race.
Chris Wilson68ad3612018-03-07 13:42:26 +00003036 *
3037 * Note that this needs to be a single atomic operation on the
3038 * tasklet (flush existing tasks, prevent new tasks) to prevent
3039 * a race between reset and set-wedged. It is not, so we do the best
3040 * we can atm and make sure we don't lock the machine up in the more
3041 * common case of recursively being called from set-wedged from inside
3042 * i915_reset.
Michel Thierrya1ef70e2017-06-20 10:57:47 +01003043 */
Chris Wilson68ad3612018-03-07 13:42:26 +00003044 if (!atomic_read(&engine->execlists.tasklet.count))
3045 tasklet_kill(&engine->execlists.tasklet);
Sagar Arun Kamblec6dce8f2017-11-16 19:02:37 +05303046 tasklet_disable(&engine->execlists.tasklet);
Michel Thierrya1ef70e2017-06-20 10:57:47 +01003047
Michał Winiarskic41937f2017-10-26 15:35:58 +02003048 /*
3049 * We're using worker to queue preemption requests from the tasklet in
3050 * GuC submission mode.
3051 * Even though tasklet was disabled, we may still have a worker queued.
3052 * Let's make sure that all workers scheduled before disabling the
3053 * tasklet are completed before continuing with the reset.
3054 */
3055 if (engine->i915->guc.preempt_wq)
3056 flush_workqueue(engine->i915->guc.preempt_wq);
3057
Michel Thierrya1ef70e2017-06-20 10:57:47 +01003058 if (engine->irq_seqno_barrier)
3059 engine->irq_seqno_barrier(engine);
3060
Chris Wilsond1d1ebf42017-07-21 13:32:33 +01003061 request = i915_gem_find_active_request(engine);
3062 if (request && request->fence.error == -EIO)
3063 request = ERR_PTR(-EIO); /* Previous reset failed! */
Michel Thierrya1ef70e2017-06-20 10:57:47 +01003064
3065 return request;
3066}
3067
Chris Wilson0e178ae2017-01-17 17:59:06 +02003068int i915_gem_reset_prepare(struct drm_i915_private *dev_priv)
Chris Wilson4c965542017-01-17 17:59:01 +02003069{
3070 struct intel_engine_cs *engine;
Chris Wilsone61e0f52018-02-21 09:56:36 +00003071 struct i915_request *request;
Chris Wilson4c965542017-01-17 17:59:01 +02003072 enum intel_engine_id id;
Chris Wilson0e178ae2017-01-17 17:59:06 +02003073 int err = 0;
Chris Wilson4c965542017-01-17 17:59:01 +02003074
Chris Wilson0e178ae2017-01-17 17:59:06 +02003075 for_each_engine(engine, dev_priv, id) {
Michel Thierrya1ef70e2017-06-20 10:57:47 +01003076 request = i915_gem_reset_prepare_engine(engine);
3077 if (IS_ERR(request)) {
3078 err = PTR_ERR(request);
3079 continue;
Chris Wilson0e178ae2017-01-17 17:59:06 +02003080 }
Michel Thierryc64992e2017-06-20 10:57:44 +01003081
3082 engine->hangcheck.active_request = request;
Chris Wilson0e178ae2017-01-17 17:59:06 +02003083 }
3084
Chris Wilson4c965542017-01-17 17:59:01 +02003085 i915_gem_revoke_fences(dev_priv);
Michal Wajdeczkoc37d5722018-03-12 13:03:07 +00003086 intel_uc_sanitize(dev_priv);
Chris Wilson0e178ae2017-01-17 17:59:06 +02003087
3088 return err;
Chris Wilson4c965542017-01-17 17:59:01 +02003089}
3090
Chris Wilsone61e0f52018-02-21 09:56:36 +00003091static void skip_request(struct i915_request *request)
Mika Kuoppalab6b0fac2014-01-30 19:04:43 +02003092{
Chris Wilson821ed7d2016-09-09 14:11:53 +01003093 void *vaddr = request->ring->vaddr;
3094 u32 head;
Mika Kuoppalab6b0fac2014-01-30 19:04:43 +02003095
Chris Wilson821ed7d2016-09-09 14:11:53 +01003096 /* As this request likely depends on state from the lost
3097 * context, clear out all the user operations leaving the
3098 * breadcrumb at the end (so we get the fence notifications).
3099 */
3100 head = request->head;
3101 if (request->postfix < head) {
3102 memset(vaddr + head, 0, request->ring->size - head);
3103 head = 0;
3104 }
3105 memset(vaddr + head, 0, request->postfix - head);
Chris Wilsonc0d5f322017-01-10 17:22:43 +00003106
3107 dma_fence_set_error(&request->fence, -EIO);
Chris Wilson4db080f2013-12-04 11:37:09 +00003108}
3109
Chris Wilsone61e0f52018-02-21 09:56:36 +00003110static void engine_skip_context(struct i915_request *request)
Mika Kuoppala36193ac2017-01-17 17:59:02 +02003111{
3112 struct intel_engine_cs *engine = request->engine;
3113 struct i915_gem_context *hung_ctx = request->ctx;
Chris Wilsona89d1f92018-05-02 17:38:39 +01003114 struct i915_timeline *timeline = request->timeline;
Mika Kuoppala36193ac2017-01-17 17:59:02 +02003115 unsigned long flags;
3116
Chris Wilsona89d1f92018-05-02 17:38:39 +01003117 GEM_BUG_ON(timeline == &engine->timeline);
Mika Kuoppala36193ac2017-01-17 17:59:02 +02003118
Chris Wilsona89d1f92018-05-02 17:38:39 +01003119 spin_lock_irqsave(&engine->timeline.lock, flags);
Mika Kuoppala36193ac2017-01-17 17:59:02 +02003120 spin_lock(&timeline->lock);
3121
Chris Wilsona89d1f92018-05-02 17:38:39 +01003122 list_for_each_entry_continue(request, &engine->timeline.requests, link)
Mika Kuoppala36193ac2017-01-17 17:59:02 +02003123 if (request->ctx == hung_ctx)
3124 skip_request(request);
3125
3126 list_for_each_entry(request, &timeline->requests, link)
3127 skip_request(request);
3128
3129 spin_unlock(&timeline->lock);
Chris Wilsona89d1f92018-05-02 17:38:39 +01003130 spin_unlock_irqrestore(&engine->timeline.lock, flags);
Mika Kuoppala36193ac2017-01-17 17:59:02 +02003131}
3132
Chris Wilsond1d1ebf42017-07-21 13:32:33 +01003133/* Returns the request if it was guilty of the hang */
Chris Wilsone61e0f52018-02-21 09:56:36 +00003134static struct i915_request *
Chris Wilsond1d1ebf42017-07-21 13:32:33 +01003135i915_gem_reset_request(struct intel_engine_cs *engine,
Chris Wilsonbba08692018-04-06 23:03:53 +01003136 struct i915_request *request,
3137 bool stalled)
Mika Kuoppala61da5362017-01-17 17:59:05 +02003138{
Mika Kuoppala71895a02017-01-17 17:59:07 +02003139 /* The guilty request will get skipped on a hung engine.
3140 *
3141 * Users of client default contexts do not rely on logical
3142 * state preserved between batches so it is safe to execute
3143 * queued requests following the hang. Non default contexts
3144 * rely on preserved state, so skipping a batch loses the
3145 * evolution of the state and it needs to be considered corrupted.
3146 * Executing more queued batches on top of corrupted state is
3147 * risky. But we take the risk by trying to advance through
3148 * the queued requests in order to make the client behaviour
3149 * more predictable around resets, by not throwing away random
3150 * amount of batches it has prepared for execution. Sophisticated
3151 * clients can use gem_reset_stats_ioctl and dma fence status
3152 * (exported via sync_file info ioctl on explicit fences) to observe
3153 * when it loses the context state and should rebuild accordingly.
3154 *
3155 * The context ban, and ultimately the client ban, mechanism are safety
3156 * valves if client submission ends up resulting in nothing more than
3157 * subsequent hangs.
3158 */
3159
Chris Wilsonbba08692018-04-06 23:03:53 +01003160 if (i915_request_completed(request)) {
3161 GEM_TRACE("%s pardoned global=%d (fence %llx:%d), current %d\n",
3162 engine->name, request->global_seqno,
3163 request->fence.context, request->fence.seqno,
3164 intel_engine_get_seqno(engine));
3165 stalled = false;
3166 }
3167
3168 if (stalled) {
Mika Kuoppala61da5362017-01-17 17:59:05 +02003169 i915_gem_context_mark_guilty(request->ctx);
3170 skip_request(request);
Chris Wilsond1d1ebf42017-07-21 13:32:33 +01003171
3172 /* If this context is now banned, skip all pending requests. */
3173 if (i915_gem_context_is_banned(request->ctx))
3174 engine_skip_context(request);
Mika Kuoppala61da5362017-01-17 17:59:05 +02003175 } else {
Chris Wilsond1d1ebf42017-07-21 13:32:33 +01003176 /*
3177 * Since this is not the hung engine, it may have advanced
3178 * since the hang declaration. Double check by refinding
3179 * the active request at the time of the reset.
3180 */
3181 request = i915_gem_find_active_request(engine);
3182 if (request) {
3183 i915_gem_context_mark_innocent(request->ctx);
3184 dma_fence_set_error(&request->fence, -EAGAIN);
3185
3186 /* Rewind the engine to replay the incomplete rq */
Chris Wilsona89d1f92018-05-02 17:38:39 +01003187 spin_lock_irq(&engine->timeline.lock);
Chris Wilsond1d1ebf42017-07-21 13:32:33 +01003188 request = list_prev_entry(request, link);
Chris Wilsona89d1f92018-05-02 17:38:39 +01003189 if (&request->link == &engine->timeline.requests)
Chris Wilsond1d1ebf42017-07-21 13:32:33 +01003190 request = NULL;
Chris Wilsona89d1f92018-05-02 17:38:39 +01003191 spin_unlock_irq(&engine->timeline.lock);
Chris Wilsond1d1ebf42017-07-21 13:32:33 +01003192 }
Mika Kuoppala61da5362017-01-17 17:59:05 +02003193 }
3194
Chris Wilsond1d1ebf42017-07-21 13:32:33 +01003195 return request;
Mika Kuoppala61da5362017-01-17 17:59:05 +02003196}
3197
Michel Thierrya1ef70e2017-06-20 10:57:47 +01003198void i915_gem_reset_engine(struct intel_engine_cs *engine,
Chris Wilsonbba08692018-04-06 23:03:53 +01003199 struct i915_request *request,
3200 bool stalled)
Chris Wilson4db080f2013-12-04 11:37:09 +00003201{
Chris Wilsonfcb1de52017-12-19 09:01:10 +00003202 /*
3203 * Make sure this write is visible before we re-enable the interrupt
3204 * handlers on another CPU, as tasklet_enable() resolves to just
3205 * a compiler barrier which is insufficient for our purpose here.
3206 */
3207 smp_store_mb(engine->irq_posted, 0);
Chris Wilsoned454f22017-07-21 13:32:29 +01003208
Chris Wilsond1d1ebf42017-07-21 13:32:33 +01003209 if (request)
Chris Wilsonbba08692018-04-06 23:03:53 +01003210 request = i915_gem_reset_request(engine, request, stalled);
Chris Wilsond1d1ebf42017-07-21 13:32:33 +01003211
3212 if (request) {
Chris Wilsonc0dcb202017-02-07 15:24:37 +00003213 DRM_DEBUG_DRIVER("resetting %s to restart from tail of request 0x%x\n",
3214 engine->name, request->global_seqno);
Chris Wilsonc0dcb202017-02-07 15:24:37 +00003215 }
Chris Wilson821ed7d2016-09-09 14:11:53 +01003216
3217 /* Setup the CS to resume from the breadcrumb of the hung request */
3218 engine->reset_hw(engine, request);
Chris Wilson821ed7d2016-09-09 14:11:53 +01003219}
3220
Chris Wilsond0667e92018-04-06 23:03:54 +01003221void i915_gem_reset(struct drm_i915_private *dev_priv,
3222 unsigned int stalled_mask)
Chris Wilson821ed7d2016-09-09 14:11:53 +01003223{
3224 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05303225 enum intel_engine_id id;
Chris Wilson821ed7d2016-09-09 14:11:53 +01003226
Chris Wilson4c7d62c2016-10-28 13:58:32 +01003227 lockdep_assert_held(&dev_priv->drm.struct_mutex);
3228
Chris Wilsone61e0f52018-02-21 09:56:36 +00003229 i915_retire_requests(dev_priv);
Chris Wilson821ed7d2016-09-09 14:11:53 +01003230
Chris Wilson2ae55732017-02-12 17:20:02 +00003231 for_each_engine(engine, dev_priv, id) {
3232 struct i915_gem_context *ctx;
3233
Chris Wilsonbba08692018-04-06 23:03:53 +01003234 i915_gem_reset_engine(engine,
3235 engine->hangcheck.active_request,
Chris Wilsond0667e92018-04-06 23:03:54 +01003236 stalled_mask & ENGINE_MASK(id));
Chris Wilson2ae55732017-02-12 17:20:02 +00003237 ctx = fetch_and_zero(&engine->last_retired_context);
3238 if (ctx)
Chris Wilsonab82a062018-04-30 14:15:01 +01003239 intel_context_unpin(ctx, engine);
Chris Wilson7b6da812017-12-16 00:03:34 +00003240
3241 /*
3242 * Ostensibily, we always want a context loaded for powersaving,
3243 * so if the engine is idle after the reset, send a request
3244 * to load our scratch kernel_context.
3245 *
3246 * More mysteriously, if we leave the engine idle after a reset,
3247 * the next userspace batch may hang, with what appears to be
3248 * an incoherent read by the CS (presumably stale TLB). An
3249 * empty request appears sufficient to paper over the glitch.
3250 */
Chris Wilson01b8fdc2018-02-05 15:24:31 +00003251 if (intel_engine_is_idle(engine)) {
Chris Wilsone61e0f52018-02-21 09:56:36 +00003252 struct i915_request *rq;
Chris Wilson7b6da812017-12-16 00:03:34 +00003253
Chris Wilsone61e0f52018-02-21 09:56:36 +00003254 rq = i915_request_alloc(engine,
3255 dev_priv->kernel_context);
Chris Wilson7b6da812017-12-16 00:03:34 +00003256 if (!IS_ERR(rq))
Chris Wilsone61e0f52018-02-21 09:56:36 +00003257 __i915_request_add(rq, false);
Chris Wilson7b6da812017-12-16 00:03:34 +00003258 }
Chris Wilson2ae55732017-02-12 17:20:02 +00003259 }
Chris Wilson821ed7d2016-09-09 14:11:53 +01003260
Tvrtko Ursulin4362f4f2016-11-16 08:55:33 +00003261 i915_gem_restore_fences(dev_priv);
Chris Wilson821ed7d2016-09-09 14:11:53 +01003262}
3263
Michel Thierrya1ef70e2017-06-20 10:57:47 +01003264void i915_gem_reset_finish_engine(struct intel_engine_cs *engine)
3265{
Sagar Arun Kamblec6dce8f2017-11-16 19:02:37 +05303266 tasklet_enable(&engine->execlists.tasklet);
Michel Thierrya1ef70e2017-06-20 10:57:47 +01003267 kthread_unpark(engine->breadcrumbs.signaler);
Chris Wilson1749d902017-10-09 12:02:59 +01003268
3269 intel_uncore_forcewake_put(engine->i915, FORCEWAKE_ALL);
Michel Thierrya1ef70e2017-06-20 10:57:47 +01003270}
3271
Chris Wilsond8027092017-02-08 14:30:32 +00003272void i915_gem_reset_finish(struct drm_i915_private *dev_priv)
3273{
Chris Wilson1f7b8472017-02-08 14:30:33 +00003274 struct intel_engine_cs *engine;
3275 enum intel_engine_id id;
3276
Chris Wilsond8027092017-02-08 14:30:32 +00003277 lockdep_assert_held(&dev_priv->drm.struct_mutex);
Chris Wilson1f7b8472017-02-08 14:30:33 +00003278
Chris Wilsonfe3288b2017-02-12 17:20:01 +00003279 for_each_engine(engine, dev_priv, id) {
Michel Thierryc64992e2017-06-20 10:57:44 +01003280 engine->hangcheck.active_request = NULL;
Michel Thierrya1ef70e2017-06-20 10:57:47 +01003281 i915_gem_reset_finish_engine(engine);
Chris Wilsonfe3288b2017-02-12 17:20:01 +00003282 }
Chris Wilsond8027092017-02-08 14:30:32 +00003283}
3284
Chris Wilsone61e0f52018-02-21 09:56:36 +00003285static void nop_submit_request(struct i915_request *request)
Chris Wilson821ed7d2016-09-09 14:11:53 +01003286{
Chris Wilsond9b13c42018-03-15 13:14:50 +00003287 GEM_TRACE("%s fence %llx:%d -> -EIO\n",
3288 request->engine->name,
3289 request->fence.context, request->fence.seqno);
Daniel Vetteraf7a8ff2017-10-11 11:10:19 +02003290 dma_fence_set_error(&request->fence, -EIO);
3291
Chris Wilsone61e0f52018-02-21 09:56:36 +00003292 i915_request_submit(request);
Daniel Vetteraf7a8ff2017-10-11 11:10:19 +02003293}
3294
Chris Wilsone61e0f52018-02-21 09:56:36 +00003295static void nop_complete_submit_request(struct i915_request *request)
Daniel Vetteraf7a8ff2017-10-11 11:10:19 +02003296{
Chris Wilson8d550822017-10-06 12:56:17 +01003297 unsigned long flags;
3298
Chris Wilsond9b13c42018-03-15 13:14:50 +00003299 GEM_TRACE("%s fence %llx:%d -> -EIO\n",
3300 request->engine->name,
3301 request->fence.context, request->fence.seqno);
Chris Wilson3cd94422017-01-10 17:22:45 +00003302 dma_fence_set_error(&request->fence, -EIO);
Chris Wilson8d550822017-10-06 12:56:17 +01003303
Chris Wilsona89d1f92018-05-02 17:38:39 +01003304 spin_lock_irqsave(&request->engine->timeline.lock, flags);
Chris Wilsone61e0f52018-02-21 09:56:36 +00003305 __i915_request_submit(request);
Chris Wilson3dcf93f2016-11-22 14:41:20 +00003306 intel_engine_init_global_seqno(request->engine, request->global_seqno);
Chris Wilsona89d1f92018-05-02 17:38:39 +01003307 spin_unlock_irqrestore(&request->engine->timeline.lock, flags);
Chris Wilson821ed7d2016-09-09 14:11:53 +01003308}
3309
Daniel Vetteraf7a8ff2017-10-11 11:10:19 +02003310void i915_gem_set_wedged(struct drm_i915_private *i915)
Chris Wilson821ed7d2016-09-09 14:11:53 +01003311{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00003312 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05303313 enum intel_engine_id id;
Eric Anholt673a3942008-07-30 12:06:12 -07003314
Chris Wilsond9b13c42018-03-15 13:14:50 +00003315 GEM_TRACE("start\n");
3316
Chris Wilson7f961d72018-04-26 11:32:19 +01003317 if (GEM_SHOW_DEBUG()) {
Chris Wilson559e0402018-02-05 09:21:59 +00003318 struct drm_printer p = drm_debug_printer(__func__);
3319
3320 for_each_engine(engine, i915, id)
3321 intel_engine_dump(engine, &p, "%s\n", engine->name);
3322 }
3323
Chris Wilson0d73e7a2018-02-07 15:13:50 +00003324 set_bit(I915_WEDGED, &i915->gpu_error.flags);
3325 smp_mb__after_atomic();
3326
Daniel Vetteraf7a8ff2017-10-11 11:10:19 +02003327 /*
3328 * First, stop submission to hw, but do not yet complete requests by
3329 * rolling the global seqno forward (since this would complete requests
3330 * for which we haven't set the fence error to EIO yet).
3331 */
Chris Wilson963ddd62018-03-02 11:33:24 +00003332 for_each_engine(engine, i915, id) {
3333 i915_gem_reset_prepare_engine(engine);
Chris Wilson47650db2018-03-07 13:42:25 +00003334
Daniel Vetteraf7a8ff2017-10-11 11:10:19 +02003335 engine->submit_request = nop_submit_request;
Chris Wilson47650db2018-03-07 13:42:25 +00003336 engine->schedule = NULL;
Chris Wilson963ddd62018-03-02 11:33:24 +00003337 }
Chris Wilson47650db2018-03-07 13:42:25 +00003338 i915->caps.scheduler = 0;
Daniel Vetteraf7a8ff2017-10-11 11:10:19 +02003339
Chris Wilsonac697ae2018-03-15 15:10:15 +00003340 /* Even if the GPU reset fails, it should still stop the engines */
3341 intel_gpu_reset(i915, ALL_ENGINES);
3342
Daniel Vetteraf7a8ff2017-10-11 11:10:19 +02003343 /*
3344 * Make sure no one is running the old callback before we proceed with
3345 * cancelling requests and resetting the completion tracking. Otherwise
3346 * we might submit a request to the hardware which never completes.
3347 */
3348 synchronize_rcu();
3349
3350 for_each_engine(engine, i915, id) {
3351 /* Mark all executing requests as skipped */
3352 engine->cancel_requests(engine);
3353
3354 /*
3355 * Only once we've force-cancelled all in-flight requests can we
3356 * start to complete all requests.
3357 */
3358 engine->submit_request = nop_complete_submit_request;
3359 }
3360
3361 /*
3362 * Make sure no request can slip through without getting completed by
3363 * either this call here to intel_engine_init_global_seqno, or the one
3364 * in nop_complete_submit_request.
3365 */
3366 synchronize_rcu();
3367
3368 for_each_engine(engine, i915, id) {
3369 unsigned long flags;
3370
Chris Wilson0d73e7a2018-02-07 15:13:50 +00003371 /*
3372 * Mark all pending requests as complete so that any concurrent
Daniel Vetteraf7a8ff2017-10-11 11:10:19 +02003373 * (lockless) lookup doesn't try and wait upon the request as we
3374 * reset it.
3375 */
Chris Wilsona89d1f92018-05-02 17:38:39 +01003376 spin_lock_irqsave(&engine->timeline.lock, flags);
Daniel Vetteraf7a8ff2017-10-11 11:10:19 +02003377 intel_engine_init_global_seqno(engine,
3378 intel_engine_last_submit(engine));
Chris Wilsona89d1f92018-05-02 17:38:39 +01003379 spin_unlock_irqrestore(&engine->timeline.lock, flags);
Chris Wilson963ddd62018-03-02 11:33:24 +00003380
3381 i915_gem_reset_finish_engine(engine);
Daniel Vetteraf7a8ff2017-10-11 11:10:19 +02003382 }
Chris Wilson20e49332016-11-22 14:41:21 +00003383
Chris Wilsond9b13c42018-03-15 13:14:50 +00003384 GEM_TRACE("end\n");
3385
Chris Wilson3d7adbb2017-07-21 13:32:27 +01003386 wake_up_all(&i915->gpu_error.reset_queue);
Eric Anholt673a3942008-07-30 12:06:12 -07003387}
3388
Chris Wilson2e8f9d32017-03-16 17:13:04 +00003389bool i915_gem_unset_wedged(struct drm_i915_private *i915)
3390{
Chris Wilsona89d1f92018-05-02 17:38:39 +01003391 struct i915_timeline *tl;
Chris Wilson2e8f9d32017-03-16 17:13:04 +00003392
3393 lockdep_assert_held(&i915->drm.struct_mutex);
3394 if (!test_bit(I915_WEDGED, &i915->gpu_error.flags))
3395 return true;
3396
Chris Wilsond9b13c42018-03-15 13:14:50 +00003397 GEM_TRACE("start\n");
3398
Chris Wilson2d4ecac2018-03-07 13:42:21 +00003399 /*
3400 * Before unwedging, make sure that all pending operations
Chris Wilson2e8f9d32017-03-16 17:13:04 +00003401 * are flushed and errored out - we may have requests waiting upon
3402 * third party fences. We marked all inflight requests as EIO, and
3403 * every execbuf since returned EIO, for consistency we want all
3404 * the currently pending requests to also be marked as EIO, which
3405 * is done inside our nop_submit_request - and so we must wait.
3406 *
3407 * No more can be submitted until we reset the wedged bit.
3408 */
3409 list_for_each_entry(tl, &i915->gt.timelines, link) {
Chris Wilsona89d1f92018-05-02 17:38:39 +01003410 struct i915_request *rq;
Chris Wilson2e8f9d32017-03-16 17:13:04 +00003411
Chris Wilsona89d1f92018-05-02 17:38:39 +01003412 rq = i915_gem_active_peek(&tl->last_request,
3413 &i915->drm.struct_mutex);
3414 if (!rq)
3415 continue;
Chris Wilson2e8f9d32017-03-16 17:13:04 +00003416
Chris Wilsona89d1f92018-05-02 17:38:39 +01003417 /*
3418 * We can't use our normal waiter as we want to
3419 * avoid recursively trying to handle the current
3420 * reset. The basic dma_fence_default_wait() installs
3421 * a callback for dma_fence_signal(), which is
3422 * triggered by our nop handler (indirectly, the
3423 * callback enables the signaler thread which is
3424 * woken by the nop_submit_request() advancing the seqno
3425 * and when the seqno passes the fence, the signaler
3426 * then signals the fence waking us up).
3427 */
3428 if (dma_fence_default_wait(&rq->fence, true,
3429 MAX_SCHEDULE_TIMEOUT) < 0)
3430 return false;
Chris Wilson2e8f9d32017-03-16 17:13:04 +00003431 }
Chris Wilson2d4ecac2018-03-07 13:42:21 +00003432 i915_retire_requests(i915);
3433 GEM_BUG_ON(i915->gt.active_requests);
Chris Wilson2e8f9d32017-03-16 17:13:04 +00003434
Chris Wilson2d4ecac2018-03-07 13:42:21 +00003435 /*
3436 * Undo nop_submit_request. We prevent all new i915 requests from
Chris Wilson2e8f9d32017-03-16 17:13:04 +00003437 * being queued (by disallowing execbuf whilst wedged) so having
3438 * waited for all active requests above, we know the system is idle
3439 * and do not have to worry about a thread being inside
3440 * engine->submit_request() as we swap over. So unlike installing
3441 * the nop_submit_request on reset, we can do this from normal
3442 * context and do not require stop_machine().
3443 */
3444 intel_engines_reset_default_submission(i915);
Chris Wilson36703e72017-06-22 11:56:25 +01003445 i915_gem_contexts_lost(i915);
Chris Wilson2e8f9d32017-03-16 17:13:04 +00003446
Chris Wilsond9b13c42018-03-15 13:14:50 +00003447 GEM_TRACE("end\n");
3448
Chris Wilson2e8f9d32017-03-16 17:13:04 +00003449 smp_mb__before_atomic(); /* complete takeover before enabling execbuf */
3450 clear_bit(I915_WEDGED, &i915->gpu_error.flags);
3451
3452 return true;
3453}
3454
Daniel Vetter75ef9da2010-08-21 00:25:16 +02003455static void
Eric Anholt673a3942008-07-30 12:06:12 -07003456i915_gem_retire_work_handler(struct work_struct *work)
3457{
Chris Wilsonb29c19b2013-09-25 17:34:56 +01003458 struct drm_i915_private *dev_priv =
Chris Wilson67d97da2016-07-04 08:08:31 +01003459 container_of(work, typeof(*dev_priv), gt.retire_work.work);
Chris Wilson91c8a322016-07-05 10:40:23 +01003460 struct drm_device *dev = &dev_priv->drm;
Eric Anholt673a3942008-07-30 12:06:12 -07003461
Chris Wilson891b48c2010-09-29 12:26:37 +01003462 /* Come back later if the device is busy... */
Chris Wilsonb29c19b2013-09-25 17:34:56 +01003463 if (mutex_trylock(&dev->struct_mutex)) {
Chris Wilsone61e0f52018-02-21 09:56:36 +00003464 i915_retire_requests(dev_priv);
Chris Wilsonb29c19b2013-09-25 17:34:56 +01003465 mutex_unlock(&dev->struct_mutex);
3466 }
Chris Wilson67d97da2016-07-04 08:08:31 +01003467
Chris Wilson88923042018-01-29 14:41:04 +00003468 /*
3469 * Keep the retire handler running until we are finally idle.
Chris Wilson67d97da2016-07-04 08:08:31 +01003470 * We do not need to do this test under locking as in the worst-case
3471 * we queue the retire worker once too often.
3472 */
Chris Wilson88923042018-01-29 14:41:04 +00003473 if (READ_ONCE(dev_priv->gt.awake))
Chris Wilson67d97da2016-07-04 08:08:31 +01003474 queue_delayed_work(dev_priv->wq,
3475 &dev_priv->gt.retire_work,
Chris Wilsonbcb45082012-10-05 17:02:57 +01003476 round_jiffies_up_relative(HZ));
Chris Wilsonb29c19b2013-09-25 17:34:56 +01003477}
Chris Wilson891b48c2010-09-29 12:26:37 +01003478
Chris Wilson84a10742018-01-24 11:36:08 +00003479static void shrink_caches(struct drm_i915_private *i915)
3480{
3481 /*
3482 * kmem_cache_shrink() discards empty slabs and reorders partially
3483 * filled slabs to prioritise allocating from the mostly full slabs,
3484 * with the aim of reducing fragmentation.
3485 */
3486 kmem_cache_shrink(i915->priorities);
3487 kmem_cache_shrink(i915->dependencies);
3488 kmem_cache_shrink(i915->requests);
3489 kmem_cache_shrink(i915->luts);
3490 kmem_cache_shrink(i915->vmas);
3491 kmem_cache_shrink(i915->objects);
3492}
3493
3494struct sleep_rcu_work {
3495 union {
3496 struct rcu_head rcu;
3497 struct work_struct work;
3498 };
3499 struct drm_i915_private *i915;
3500 unsigned int epoch;
3501};
3502
3503static inline bool
3504same_epoch(struct drm_i915_private *i915, unsigned int epoch)
3505{
3506 /*
3507 * There is a small chance that the epoch wrapped since we started
3508 * sleeping. If we assume that epoch is at least a u32, then it will
3509 * take at least 2^32 * 100ms for it to wrap, or about 326 years.
3510 */
3511 return epoch == READ_ONCE(i915->gt.epoch);
3512}
3513
3514static void __sleep_work(struct work_struct *work)
3515{
3516 struct sleep_rcu_work *s = container_of(work, typeof(*s), work);
3517 struct drm_i915_private *i915 = s->i915;
3518 unsigned int epoch = s->epoch;
3519
3520 kfree(s);
3521 if (same_epoch(i915, epoch))
3522 shrink_caches(i915);
3523}
3524
3525static void __sleep_rcu(struct rcu_head *rcu)
3526{
3527 struct sleep_rcu_work *s = container_of(rcu, typeof(*s), rcu);
3528 struct drm_i915_private *i915 = s->i915;
3529
3530 if (same_epoch(i915, s->epoch)) {
3531 INIT_WORK(&s->work, __sleep_work);
3532 queue_work(i915->wq, &s->work);
3533 } else {
3534 kfree(s);
3535 }
3536}
3537
Chris Wilson5427f202017-10-23 22:32:34 +01003538static inline bool
3539new_requests_since_last_retire(const struct drm_i915_private *i915)
3540{
3541 return (READ_ONCE(i915->gt.active_requests) ||
3542 work_pending(&i915->gt.idle_work.work));
3543}
3544
Chris Wilsonb29c19b2013-09-25 17:34:56 +01003545static void
3546i915_gem_idle_work_handler(struct work_struct *work)
3547{
3548 struct drm_i915_private *dev_priv =
Chris Wilson67d97da2016-07-04 08:08:31 +01003549 container_of(work, typeof(*dev_priv), gt.idle_work.work);
Chris Wilson84a10742018-01-24 11:36:08 +00003550 unsigned int epoch = I915_EPOCH_INVALID;
Chris Wilson67d97da2016-07-04 08:08:31 +01003551 bool rearm_hangcheck;
3552
3553 if (!READ_ONCE(dev_priv->gt.awake))
3554 return;
3555
Imre Deak0cb56702016-11-07 11:20:04 +02003556 /*
3557 * Wait for last execlists context complete, but bail out in case a
Chris Wilsonffed7bd2018-03-01 10:33:38 +00003558 * new request is submitted. As we don't trust the hardware, we
3559 * continue on if the wait times out. This is necessary to allow
3560 * the machine to suspend even if the hardware dies, and we will
3561 * try to recover in resume (after depriving the hardware of power,
3562 * it may be in a better mmod).
Imre Deak0cb56702016-11-07 11:20:04 +02003563 */
Chris Wilsonffed7bd2018-03-01 10:33:38 +00003564 __wait_for(if (new_requests_since_last_retire(dev_priv)) return,
3565 intel_engines_are_idle(dev_priv),
3566 I915_IDLE_ENGINES_TIMEOUT * 1000,
3567 10, 500);
Chris Wilson67d97da2016-07-04 08:08:31 +01003568
3569 rearm_hangcheck =
3570 cancel_delayed_work_sync(&dev_priv->gpu_error.hangcheck_work);
3571
Chris Wilson5427f202017-10-23 22:32:34 +01003572 if (!mutex_trylock(&dev_priv->drm.struct_mutex)) {
Chris Wilson67d97da2016-07-04 08:08:31 +01003573 /* Currently busy, come back later */
3574 mod_delayed_work(dev_priv->wq,
3575 &dev_priv->gt.idle_work,
3576 msecs_to_jiffies(50));
3577 goto out_rearm;
3578 }
3579
Imre Deak93c97dc2016-11-07 11:20:03 +02003580 /*
3581 * New request retired after this work handler started, extend active
3582 * period until next instance of the work.
3583 */
Chris Wilson5427f202017-10-23 22:32:34 +01003584 if (new_requests_since_last_retire(dev_priv))
Imre Deak93c97dc2016-11-07 11:20:03 +02003585 goto out_unlock;
3586
Chris Wilsone4d20062018-04-06 16:51:44 +01003587 epoch = __i915_gem_park(dev_priv);
Chris Wilsonff320d62017-10-23 22:32:35 +01003588
Chris Wilson67d97da2016-07-04 08:08:31 +01003589 rearm_hangcheck = false;
Chris Wilson67d97da2016-07-04 08:08:31 +01003590out_unlock:
Chris Wilson5427f202017-10-23 22:32:34 +01003591 mutex_unlock(&dev_priv->drm.struct_mutex);
Chris Wilson35c94182015-04-07 16:20:37 +01003592
Chris Wilson67d97da2016-07-04 08:08:31 +01003593out_rearm:
3594 if (rearm_hangcheck) {
3595 GEM_BUG_ON(!dev_priv->gt.awake);
3596 i915_queue_hangcheck(dev_priv);
Chris Wilson35c94182015-04-07 16:20:37 +01003597 }
Chris Wilson84a10742018-01-24 11:36:08 +00003598
3599 /*
3600 * When we are idle, it is an opportune time to reap our caches.
3601 * However, we have many objects that utilise RCU and the ordered
3602 * i915->wq that this work is executing on. To try and flush any
3603 * pending frees now we are idle, we first wait for an RCU grace
3604 * period, and then queue a task (that will run last on the wq) to
3605 * shrink and re-optimize the caches.
3606 */
3607 if (same_epoch(dev_priv, epoch)) {
3608 struct sleep_rcu_work *s = kmalloc(sizeof(*s), GFP_KERNEL);
3609 if (s) {
3610 s->i915 = dev_priv;
3611 s->epoch = epoch;
3612 call_rcu(&s->rcu, __sleep_rcu);
3613 }
3614 }
Eric Anholt673a3942008-07-30 12:06:12 -07003615}
3616
Chris Wilsonb1f788c2016-08-04 07:52:45 +01003617void i915_gem_close_object(struct drm_gem_object *gem, struct drm_file *file)
3618{
Chris Wilsond1b48c12017-08-16 09:52:08 +01003619 struct drm_i915_private *i915 = to_i915(gem->dev);
Chris Wilsonb1f788c2016-08-04 07:52:45 +01003620 struct drm_i915_gem_object *obj = to_intel_bo(gem);
3621 struct drm_i915_file_private *fpriv = file->driver_priv;
Chris Wilsond1b48c12017-08-16 09:52:08 +01003622 struct i915_lut_handle *lut, *ln;
Chris Wilsonb1f788c2016-08-04 07:52:45 +01003623
Chris Wilsond1b48c12017-08-16 09:52:08 +01003624 mutex_lock(&i915->drm.struct_mutex);
3625
3626 list_for_each_entry_safe(lut, ln, &obj->lut_list, obj_link) {
3627 struct i915_gem_context *ctx = lut->ctx;
3628 struct i915_vma *vma;
3629
Chris Wilson432295d2017-08-22 12:05:15 +01003630 GEM_BUG_ON(ctx->file_priv == ERR_PTR(-EBADF));
Chris Wilsond1b48c12017-08-16 09:52:08 +01003631 if (ctx->file_priv != fpriv)
3632 continue;
3633
3634 vma = radix_tree_delete(&ctx->handles_vma, lut->handle);
Chris Wilson3ffff012017-08-22 12:05:17 +01003635 GEM_BUG_ON(vma->obj != obj);
3636
3637 /* We allow the process to have multiple handles to the same
3638 * vma, in the same fd namespace, by virtue of flink/open.
3639 */
3640 GEM_BUG_ON(!vma->open_count);
3641 if (!--vma->open_count && !i915_vma_is_ggtt(vma))
Chris Wilsonb1f788c2016-08-04 07:52:45 +01003642 i915_vma_close(vma);
Chris Wilsonf8a7fde2016-10-28 13:58:29 +01003643
Chris Wilsond1b48c12017-08-16 09:52:08 +01003644 list_del(&lut->obj_link);
3645 list_del(&lut->ctx_link);
Chris Wilson4ff4b442017-06-16 15:05:16 +01003646
Chris Wilsond1b48c12017-08-16 09:52:08 +01003647 kmem_cache_free(i915->luts, lut);
3648 __i915_gem_object_release_unless_active(obj);
Chris Wilsonf8a7fde2016-10-28 13:58:29 +01003649 }
Chris Wilsond1b48c12017-08-16 09:52:08 +01003650
3651 mutex_unlock(&i915->drm.struct_mutex);
Chris Wilsonb1f788c2016-08-04 07:52:45 +01003652}
3653
Chris Wilsone95433c2016-10-28 13:58:27 +01003654static unsigned long to_wait_timeout(s64 timeout_ns)
3655{
3656 if (timeout_ns < 0)
3657 return MAX_SCHEDULE_TIMEOUT;
3658
3659 if (timeout_ns == 0)
3660 return 0;
3661
3662 return nsecs_to_jiffies_timeout(timeout_ns);
3663}
3664
Ben Widawsky5816d642012-04-11 11:18:19 -07003665/**
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07003666 * i915_gem_wait_ioctl - implements DRM_IOCTL_I915_GEM_WAIT
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01003667 * @dev: drm device pointer
3668 * @data: ioctl data blob
3669 * @file: drm file pointer
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07003670 *
3671 * Returns 0 if successful, else an error is returned with the remaining time in
3672 * the timeout parameter.
3673 * -ETIME: object is still busy after timeout
3674 * -ERESTARTSYS: signal interrupted the wait
3675 * -ENONENT: object doesn't exist
3676 * Also possible, but rare:
Chris Wilsonb8050142017-08-11 11:57:31 +01003677 * -EAGAIN: incomplete, restart syscall
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07003678 * -ENOMEM: damn
3679 * -ENODEV: Internal IRQ fail
3680 * -E?: The add request failed
3681 *
3682 * The wait ioctl with a timeout of 0 reimplements the busy ioctl. With any
3683 * non-zero timeout parameter the wait ioctl will wait for the given number of
3684 * nanoseconds on an object becoming unbusy. Since the wait itself does so
3685 * without holding struct_mutex the object may become re-busied before this
3686 * function completes. A similar but shorter * race condition exists in the busy
3687 * ioctl
3688 */
3689int
3690i915_gem_wait_ioctl(struct drm_device *dev, void *data, struct drm_file *file)
3691{
3692 struct drm_i915_gem_wait *args = data;
3693 struct drm_i915_gem_object *obj;
Chris Wilsone95433c2016-10-28 13:58:27 +01003694 ktime_t start;
3695 long ret;
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07003696
Daniel Vetter11b5d512014-09-29 15:31:26 +02003697 if (args->flags != 0)
3698 return -EINVAL;
3699
Chris Wilson03ac0642016-07-20 13:31:51 +01003700 obj = i915_gem_object_lookup(file, args->bo_handle);
Chris Wilson033d5492016-08-05 10:14:17 +01003701 if (!obj)
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07003702 return -ENOENT;
Chris Wilson033d5492016-08-05 10:14:17 +01003703
Chris Wilsone95433c2016-10-28 13:58:27 +01003704 start = ktime_get();
3705
3706 ret = i915_gem_object_wait(obj,
3707 I915_WAIT_INTERRUPTIBLE | I915_WAIT_ALL,
3708 to_wait_timeout(args->timeout_ns),
3709 to_rps_client(file));
3710
3711 if (args->timeout_ns > 0) {
3712 args->timeout_ns -= ktime_to_ns(ktime_sub(ktime_get(), start));
3713 if (args->timeout_ns < 0)
3714 args->timeout_ns = 0;
Chris Wilsonc1d20612017-02-16 12:54:41 +00003715
3716 /*
3717 * Apparently ktime isn't accurate enough and occasionally has a
3718 * bit of mismatch in the jiffies<->nsecs<->ktime loop. So patch
3719 * things up to make the test happy. We allow up to 1 jiffy.
3720 *
3721 * This is a regression from the timespec->ktime conversion.
3722 */
3723 if (ret == -ETIME && !nsecs_to_jiffies(args->timeout_ns))
3724 args->timeout_ns = 0;
Chris Wilsonb8050142017-08-11 11:57:31 +01003725
3726 /* Asked to wait beyond the jiffie/scheduler precision? */
3727 if (ret == -ETIME && args->timeout_ns)
3728 ret = -EAGAIN;
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07003729 }
3730
Chris Wilsonf0cd5182016-10-28 13:58:43 +01003731 i915_gem_object_put(obj);
John Harrisonff865882014-11-24 18:49:28 +00003732 return ret;
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07003733}
3734
Chris Wilsona89d1f92018-05-02 17:38:39 +01003735static int wait_for_timeline(struct i915_timeline *tl, unsigned int flags)
Daniel Vetter4df2faf2010-02-19 11:52:00 +01003736{
Chris Wilsona89d1f92018-05-02 17:38:39 +01003737 return i915_gem_active_wait(&tl->last_request, flags);
Chris Wilson73cb9702016-10-28 13:58:46 +01003738}
3739
Chris Wilson25112b62017-03-30 15:50:39 +01003740static int wait_for_engines(struct drm_i915_private *i915)
3741{
Chris Wilsonee42c002017-12-11 19:41:34 +00003742 if (wait_for(intel_engines_are_idle(i915), I915_IDLE_ENGINES_TIMEOUT)) {
Chris Wilson59e4b192017-12-11 19:41:35 +00003743 dev_err(i915->drm.dev,
3744 "Failed to idle engines, declaring wedged!\n");
Chris Wilson629820f2018-03-09 10:11:14 +00003745 GEM_TRACE_DUMP();
Chris Wilsoncad99462017-08-26 12:09:33 +01003746 i915_gem_set_wedged(i915);
3747 return -EIO;
Chris Wilson25112b62017-03-30 15:50:39 +01003748 }
3749
3750 return 0;
3751}
3752
Chris Wilson73cb9702016-10-28 13:58:46 +01003753int i915_gem_wait_for_idle(struct drm_i915_private *i915, unsigned int flags)
3754{
Chris Wilson863e9fd2017-05-30 13:13:32 +01003755 /* If the device is asleep, we have no requests outstanding */
3756 if (!READ_ONCE(i915->gt.awake))
3757 return 0;
3758
Chris Wilson9caa34a2016-11-11 14:58:08 +00003759 if (flags & I915_WAIT_LOCKED) {
Chris Wilsona89d1f92018-05-02 17:38:39 +01003760 struct i915_timeline *tl;
3761 int err;
Chris Wilson9caa34a2016-11-11 14:58:08 +00003762
3763 lockdep_assert_held(&i915->drm.struct_mutex);
3764
3765 list_for_each_entry(tl, &i915->gt.timelines, link) {
Chris Wilsona89d1f92018-05-02 17:38:39 +01003766 err = wait_for_timeline(tl, flags);
3767 if (err)
3768 return err;
Chris Wilson9caa34a2016-11-11 14:58:08 +00003769 }
Chris Wilsone61e0f52018-02-21 09:56:36 +00003770 i915_retire_requests(i915);
Chris Wilson25112b62017-03-30 15:50:39 +01003771
Chris Wilsona89d1f92018-05-02 17:38:39 +01003772 return wait_for_engines(i915);
Chris Wilson9caa34a2016-11-11 14:58:08 +00003773 } else {
Chris Wilsona89d1f92018-05-02 17:38:39 +01003774 struct intel_engine_cs *engine;
3775 enum intel_engine_id id;
3776 int err;
Zou Nan haid1b851f2010-05-21 09:08:57 +08003777
Chris Wilsona89d1f92018-05-02 17:38:39 +01003778 for_each_engine(engine, i915, id) {
3779 err = wait_for_timeline(&engine->timeline, flags);
3780 if (err)
3781 return err;
3782 }
3783
3784 return 0;
3785 }
Daniel Vetter4df2faf2010-02-19 11:52:00 +01003786}
3787
Chris Wilson5a97bcc2017-02-22 11:40:46 +00003788static void __i915_gem_object_flush_for_display(struct drm_i915_gem_object *obj)
3789{
Chris Wilsone27ab732017-06-15 13:38:49 +01003790 /*
3791 * We manually flush the CPU domain so that we can override and
3792 * force the flush for the display, and perform it asyncrhonously.
3793 */
3794 flush_write_domain(obj, ~I915_GEM_DOMAIN_CPU);
3795 if (obj->cache_dirty)
3796 i915_gem_clflush_object(obj, I915_CLFLUSH_FORCE);
Christian Königc0a51fd2018-02-16 13:43:38 +01003797 obj->write_domain = 0;
Chris Wilson5a97bcc2017-02-22 11:40:46 +00003798}
3799
3800void i915_gem_object_flush_if_display(struct drm_i915_gem_object *obj)
3801{
Chris Wilsonbd3d2252017-10-13 21:26:14 +01003802 if (!READ_ONCE(obj->pin_global))
Chris Wilson5a97bcc2017-02-22 11:40:46 +00003803 return;
3804
3805 mutex_lock(&obj->base.dev->struct_mutex);
3806 __i915_gem_object_flush_for_display(obj);
3807 mutex_unlock(&obj->base.dev->struct_mutex);
3808}
3809
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003810/**
Chris Wilsone22d8e32017-04-12 12:01:11 +01003811 * Moves a single object to the WC read, and possibly write domain.
3812 * @obj: object to act on
3813 * @write: ask for write access or read only
3814 *
3815 * This function returns when the move is complete, including waiting on
3816 * flushes to occur.
3817 */
3818int
3819i915_gem_object_set_to_wc_domain(struct drm_i915_gem_object *obj, bool write)
3820{
3821 int ret;
3822
3823 lockdep_assert_held(&obj->base.dev->struct_mutex);
3824
3825 ret = i915_gem_object_wait(obj,
3826 I915_WAIT_INTERRUPTIBLE |
3827 I915_WAIT_LOCKED |
3828 (write ? I915_WAIT_ALL : 0),
3829 MAX_SCHEDULE_TIMEOUT,
3830 NULL);
3831 if (ret)
3832 return ret;
3833
Christian Königc0a51fd2018-02-16 13:43:38 +01003834 if (obj->write_domain == I915_GEM_DOMAIN_WC)
Chris Wilsone22d8e32017-04-12 12:01:11 +01003835 return 0;
3836
3837 /* Flush and acquire obj->pages so that we are coherent through
3838 * direct access in memory with previous cached writes through
3839 * shmemfs and that our cache domain tracking remains valid.
3840 * For example, if the obj->filp was moved to swap without us
3841 * being notified and releasing the pages, we would mistakenly
3842 * continue to assume that the obj remained out of the CPU cached
3843 * domain.
3844 */
3845 ret = i915_gem_object_pin_pages(obj);
3846 if (ret)
3847 return ret;
3848
3849 flush_write_domain(obj, ~I915_GEM_DOMAIN_WC);
3850
3851 /* Serialise direct access to this object with the barriers for
3852 * coherent writes from the GPU, by effectively invalidating the
3853 * WC domain upon first access.
3854 */
Christian Königc0a51fd2018-02-16 13:43:38 +01003855 if ((obj->read_domains & I915_GEM_DOMAIN_WC) == 0)
Chris Wilsone22d8e32017-04-12 12:01:11 +01003856 mb();
3857
3858 /* It should now be out of any other write domains, and we can update
3859 * the domain values for our changes.
3860 */
Christian Königc0a51fd2018-02-16 13:43:38 +01003861 GEM_BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_WC) != 0);
3862 obj->read_domains |= I915_GEM_DOMAIN_WC;
Chris Wilsone22d8e32017-04-12 12:01:11 +01003863 if (write) {
Christian Königc0a51fd2018-02-16 13:43:38 +01003864 obj->read_domains = I915_GEM_DOMAIN_WC;
3865 obj->write_domain = I915_GEM_DOMAIN_WC;
Chris Wilsone22d8e32017-04-12 12:01:11 +01003866 obj->mm.dirty = true;
3867 }
3868
3869 i915_gem_object_unpin_pages(obj);
3870 return 0;
3871}
3872
3873/**
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003874 * Moves a single object to the GTT read, and possibly write domain.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01003875 * @obj: object to act on
3876 * @write: ask for write access or read only
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003877 *
3878 * This function returns when the move is complete, including waiting on
3879 * flushes to occur.
3880 */
Jesse Barnes79e53942008-11-07 14:24:08 -08003881int
Chris Wilson20217462010-11-23 15:26:33 +00003882i915_gem_object_set_to_gtt_domain(struct drm_i915_gem_object *obj, bool write)
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003883{
Eric Anholte47c68e2008-11-14 13:35:19 -08003884 int ret;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003885
Chris Wilsone95433c2016-10-28 13:58:27 +01003886 lockdep_assert_held(&obj->base.dev->struct_mutex);
Chris Wilson4c7d62c2016-10-28 13:58:32 +01003887
Chris Wilsone95433c2016-10-28 13:58:27 +01003888 ret = i915_gem_object_wait(obj,
3889 I915_WAIT_INTERRUPTIBLE |
3890 I915_WAIT_LOCKED |
3891 (write ? I915_WAIT_ALL : 0),
3892 MAX_SCHEDULE_TIMEOUT,
3893 NULL);
Chris Wilson88241782011-01-07 17:09:48 +00003894 if (ret)
3895 return ret;
3896
Christian Königc0a51fd2018-02-16 13:43:38 +01003897 if (obj->write_domain == I915_GEM_DOMAIN_GTT)
Chris Wilsonc13d87e2016-07-20 09:21:15 +01003898 return 0;
3899
Chris Wilson43566de2015-01-02 16:29:29 +05303900 /* Flush and acquire obj->pages so that we are coherent through
3901 * direct access in memory with previous cached writes through
3902 * shmemfs and that our cache domain tracking remains valid.
3903 * For example, if the obj->filp was moved to swap without us
3904 * being notified and releasing the pages, we would mistakenly
3905 * continue to assume that the obj remained out of the CPU cached
3906 * domain.
3907 */
Chris Wilsona4f5ea62016-10-28 13:58:35 +01003908 ret = i915_gem_object_pin_pages(obj);
Chris Wilson43566de2015-01-02 16:29:29 +05303909 if (ret)
3910 return ret;
3911
Chris Wilsonef749212017-04-12 12:01:10 +01003912 flush_write_domain(obj, ~I915_GEM_DOMAIN_GTT);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003913
Chris Wilsond0a57782012-10-09 19:24:37 +01003914 /* Serialise direct access to this object with the barriers for
3915 * coherent writes from the GPU, by effectively invalidating the
3916 * GTT domain upon first access.
3917 */
Christian Königc0a51fd2018-02-16 13:43:38 +01003918 if ((obj->read_domains & I915_GEM_DOMAIN_GTT) == 0)
Chris Wilsond0a57782012-10-09 19:24:37 +01003919 mb();
3920
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003921 /* It should now be out of any other write domains, and we can update
3922 * the domain values for our changes.
3923 */
Christian Königc0a51fd2018-02-16 13:43:38 +01003924 GEM_BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_GTT) != 0);
3925 obj->read_domains |= I915_GEM_DOMAIN_GTT;
Eric Anholte47c68e2008-11-14 13:35:19 -08003926 if (write) {
Christian Königc0a51fd2018-02-16 13:43:38 +01003927 obj->read_domains = I915_GEM_DOMAIN_GTT;
3928 obj->write_domain = I915_GEM_DOMAIN_GTT;
Chris Wilsona4f5ea62016-10-28 13:58:35 +01003929 obj->mm.dirty = true;
Eric Anholte47c68e2008-11-14 13:35:19 -08003930 }
3931
Chris Wilsona4f5ea62016-10-28 13:58:35 +01003932 i915_gem_object_unpin_pages(obj);
Eric Anholte47c68e2008-11-14 13:35:19 -08003933 return 0;
3934}
3935
Chris Wilsonef55f922015-10-09 14:11:27 +01003936/**
3937 * Changes the cache-level of an object across all VMA.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01003938 * @obj: object to act on
3939 * @cache_level: new cache level to set for the object
Chris Wilsonef55f922015-10-09 14:11:27 +01003940 *
3941 * After this function returns, the object will be in the new cache-level
3942 * across all GTT and the contents of the backing storage will be coherent,
3943 * with respect to the new cache-level. In order to keep the backing storage
3944 * coherent for all users, we only allow a single cache level to be set
3945 * globally on the object and prevent it from being changed whilst the
3946 * hardware is reading from the object. That is if the object is currently
3947 * on the scanout it will be set to uncached (or equivalent display
3948 * cache coherency) and all non-MOCS GPU access will also be uncached so
3949 * that all direct access to the scanout remains coherent.
3950 */
Chris Wilsone4ffd172011-04-04 09:44:39 +01003951int i915_gem_object_set_cache_level(struct drm_i915_gem_object *obj,
3952 enum i915_cache_level cache_level)
3953{
Chris Wilsonaa653a62016-08-04 07:52:27 +01003954 struct i915_vma *vma;
Chris Wilsona6a7cc42016-11-18 21:17:46 +00003955 int ret;
Chris Wilsone4ffd172011-04-04 09:44:39 +01003956
Chris Wilson4c7d62c2016-10-28 13:58:32 +01003957 lockdep_assert_held(&obj->base.dev->struct_mutex);
3958
Chris Wilsone4ffd172011-04-04 09:44:39 +01003959 if (obj->cache_level == cache_level)
Chris Wilsona6a7cc42016-11-18 21:17:46 +00003960 return 0;
Chris Wilsone4ffd172011-04-04 09:44:39 +01003961
Chris Wilsonef55f922015-10-09 14:11:27 +01003962 /* Inspect the list of currently bound VMA and unbind any that would
3963 * be invalid given the new cache-level. This is principally to
3964 * catch the issue of the CS prefetch crossing page boundaries and
3965 * reading an invalid PTE on older architectures.
3966 */
Chris Wilsonaa653a62016-08-04 07:52:27 +01003967restart:
3968 list_for_each_entry(vma, &obj->vma_list, obj_link) {
Chris Wilsonef55f922015-10-09 14:11:27 +01003969 if (!drm_mm_node_allocated(&vma->node))
3970 continue;
3971
Chris Wilson20dfbde2016-08-04 16:32:30 +01003972 if (i915_vma_is_pinned(vma)) {
Chris Wilsonef55f922015-10-09 14:11:27 +01003973 DRM_DEBUG("can not change the cache level of pinned objects\n");
3974 return -EBUSY;
3975 }
3976
Chris Wilson010e3e62017-12-06 12:49:13 +00003977 if (!i915_vma_is_closed(vma) &&
3978 i915_gem_valid_gtt_space(vma, cache_level))
Chris Wilsonaa653a62016-08-04 07:52:27 +01003979 continue;
3980
3981 ret = i915_vma_unbind(vma);
3982 if (ret)
3983 return ret;
3984
3985 /* As unbinding may affect other elements in the
3986 * obj->vma_list (due to side-effects from retiring
3987 * an active vma), play safe and restart the iterator.
3988 */
3989 goto restart;
Chris Wilson42d6ab42012-07-26 11:49:32 +01003990 }
3991
Chris Wilsonef55f922015-10-09 14:11:27 +01003992 /* We can reuse the existing drm_mm nodes but need to change the
3993 * cache-level on the PTE. We could simply unbind them all and
3994 * rebind with the correct cache-level on next use. However since
3995 * we already have a valid slot, dma mapping, pages etc, we may as
3996 * rewrite the PTE in the belief that doing so tramples upon less
3997 * state and so involves less work.
3998 */
Chris Wilson15717de2016-08-04 07:52:26 +01003999 if (obj->bind_count) {
Chris Wilsonef55f922015-10-09 14:11:27 +01004000 /* Before we change the PTE, the GPU must not be accessing it.
4001 * If we wait upon the object, we know that all the bound
4002 * VMA are no longer active.
4003 */
Chris Wilsone95433c2016-10-28 13:58:27 +01004004 ret = i915_gem_object_wait(obj,
4005 I915_WAIT_INTERRUPTIBLE |
4006 I915_WAIT_LOCKED |
4007 I915_WAIT_ALL,
4008 MAX_SCHEDULE_TIMEOUT,
4009 NULL);
Chris Wilsone4ffd172011-04-04 09:44:39 +01004010 if (ret)
4011 return ret;
4012
Tvrtko Ursulin0031fb92016-11-04 14:42:44 +00004013 if (!HAS_LLC(to_i915(obj->base.dev)) &&
4014 cache_level != I915_CACHE_NONE) {
Chris Wilsonef55f922015-10-09 14:11:27 +01004015 /* Access to snoopable pages through the GTT is
4016 * incoherent and on some machines causes a hard
4017 * lockup. Relinquish the CPU mmaping to force
4018 * userspace to refault in the pages and we can
4019 * then double check if the GTT mapping is still
4020 * valid for that pointer access.
4021 */
4022 i915_gem_release_mmap(obj);
Chris Wilsone4ffd172011-04-04 09:44:39 +01004023
Chris Wilsonef55f922015-10-09 14:11:27 +01004024 /* As we no longer need a fence for GTT access,
4025 * we can relinquish it now (and so prevent having
4026 * to steal a fence from someone else on the next
4027 * fence request). Note GPU activity would have
4028 * dropped the fence as all snoopable access is
4029 * supposed to be linear.
4030 */
Chris Wilsone2189dd2017-12-07 21:14:07 +00004031 for_each_ggtt_vma(vma, obj) {
Chris Wilson49ef5292016-08-18 17:17:00 +01004032 ret = i915_vma_put_fence(vma);
4033 if (ret)
4034 return ret;
4035 }
Chris Wilsonef55f922015-10-09 14:11:27 +01004036 } else {
4037 /* We either have incoherent backing store and
4038 * so no GTT access or the architecture is fully
4039 * coherent. In such cases, existing GTT mmaps
4040 * ignore the cache bit in the PTE and we can
4041 * rewrite it without confusing the GPU or having
4042 * to force userspace to fault back in its mmaps.
4043 */
Chris Wilsone4ffd172011-04-04 09:44:39 +01004044 }
4045
Chris Wilson1c7f4bc2016-02-26 11:03:19 +00004046 list_for_each_entry(vma, &obj->vma_list, obj_link) {
Chris Wilsonef55f922015-10-09 14:11:27 +01004047 if (!drm_mm_node_allocated(&vma->node))
4048 continue;
4049
4050 ret = i915_vma_bind(vma, cache_level, PIN_UPDATE);
4051 if (ret)
4052 return ret;
4053 }
Chris Wilsone4ffd172011-04-04 09:44:39 +01004054 }
4055
Chris Wilson1c7f4bc2016-02-26 11:03:19 +00004056 list_for_each_entry(vma, &obj->vma_list, obj_link)
Chris Wilson2c225692013-08-09 12:26:45 +01004057 vma->node.color = cache_level;
Chris Wilsonb8f55be2017-08-11 12:11:16 +01004058 i915_gem_object_set_cache_coherency(obj, cache_level);
Chris Wilsone27ab732017-06-15 13:38:49 +01004059 obj->cache_dirty = true; /* Always invalidate stale cachelines */
Chris Wilson2c225692013-08-09 12:26:45 +01004060
Chris Wilsone4ffd172011-04-04 09:44:39 +01004061 return 0;
4062}
4063
Ben Widawsky199adf42012-09-21 17:01:20 -07004064int i915_gem_get_caching_ioctl(struct drm_device *dev, void *data,
4065 struct drm_file *file)
Chris Wilsone6994ae2012-07-10 10:27:08 +01004066{
Ben Widawsky199adf42012-09-21 17:01:20 -07004067 struct drm_i915_gem_caching *args = data;
Chris Wilsone6994ae2012-07-10 10:27:08 +01004068 struct drm_i915_gem_object *obj;
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004069 int err = 0;
Chris Wilsone6994ae2012-07-10 10:27:08 +01004070
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004071 rcu_read_lock();
4072 obj = i915_gem_object_lookup_rcu(file, args->handle);
4073 if (!obj) {
4074 err = -ENOENT;
4075 goto out;
4076 }
Chris Wilsone6994ae2012-07-10 10:27:08 +01004077
Chris Wilson651d7942013-08-08 14:41:10 +01004078 switch (obj->cache_level) {
4079 case I915_CACHE_LLC:
4080 case I915_CACHE_L3_LLC:
4081 args->caching = I915_CACHING_CACHED;
4082 break;
4083
Chris Wilson4257d3b2013-08-08 14:41:11 +01004084 case I915_CACHE_WT:
4085 args->caching = I915_CACHING_DISPLAY;
4086 break;
4087
Chris Wilson651d7942013-08-08 14:41:10 +01004088 default:
4089 args->caching = I915_CACHING_NONE;
4090 break;
4091 }
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004092out:
4093 rcu_read_unlock();
4094 return err;
Chris Wilsone6994ae2012-07-10 10:27:08 +01004095}
4096
Ben Widawsky199adf42012-09-21 17:01:20 -07004097int i915_gem_set_caching_ioctl(struct drm_device *dev, void *data,
4098 struct drm_file *file)
Chris Wilsone6994ae2012-07-10 10:27:08 +01004099{
Chris Wilson9c870d02016-10-24 13:42:15 +01004100 struct drm_i915_private *i915 = to_i915(dev);
Ben Widawsky199adf42012-09-21 17:01:20 -07004101 struct drm_i915_gem_caching *args = data;
Chris Wilsone6994ae2012-07-10 10:27:08 +01004102 struct drm_i915_gem_object *obj;
4103 enum i915_cache_level level;
Chris Wilsond65415d2017-01-19 08:22:10 +00004104 int ret = 0;
Chris Wilsone6994ae2012-07-10 10:27:08 +01004105
Ben Widawsky199adf42012-09-21 17:01:20 -07004106 switch (args->caching) {
4107 case I915_CACHING_NONE:
Chris Wilsone6994ae2012-07-10 10:27:08 +01004108 level = I915_CACHE_NONE;
4109 break;
Ben Widawsky199adf42012-09-21 17:01:20 -07004110 case I915_CACHING_CACHED:
Imre Deake5756c12015-08-14 18:43:30 +03004111 /*
4112 * Due to a HW issue on BXT A stepping, GPU stores via a
4113 * snooped mapping may leave stale data in a corresponding CPU
4114 * cacheline, whereas normally such cachelines would get
4115 * invalidated.
4116 */
Chris Wilson9c870d02016-10-24 13:42:15 +01004117 if (!HAS_LLC(i915) && !HAS_SNOOP(i915))
Imre Deake5756c12015-08-14 18:43:30 +03004118 return -ENODEV;
4119
Chris Wilsone6994ae2012-07-10 10:27:08 +01004120 level = I915_CACHE_LLC;
4121 break;
Chris Wilson4257d3b2013-08-08 14:41:11 +01004122 case I915_CACHING_DISPLAY:
Chris Wilson9c870d02016-10-24 13:42:15 +01004123 level = HAS_WT(i915) ? I915_CACHE_WT : I915_CACHE_NONE;
Chris Wilson4257d3b2013-08-08 14:41:11 +01004124 break;
Chris Wilsone6994ae2012-07-10 10:27:08 +01004125 default:
4126 return -EINVAL;
4127 }
4128
Chris Wilsond65415d2017-01-19 08:22:10 +00004129 obj = i915_gem_object_lookup(file, args->handle);
4130 if (!obj)
4131 return -ENOENT;
4132
Tina Zhanga03f3952017-11-14 10:25:13 +00004133 /*
4134 * The caching mode of proxy object is handled by its generator, and
4135 * not allowed to be changed by userspace.
4136 */
4137 if (i915_gem_object_is_proxy(obj)) {
4138 ret = -ENXIO;
4139 goto out;
4140 }
4141
Chris Wilsond65415d2017-01-19 08:22:10 +00004142 if (obj->cache_level == level)
4143 goto out;
4144
4145 ret = i915_gem_object_wait(obj,
4146 I915_WAIT_INTERRUPTIBLE,
4147 MAX_SCHEDULE_TIMEOUT,
4148 to_rps_client(file));
4149 if (ret)
4150 goto out;
4151
Ben Widawsky3bc29132012-09-26 16:15:20 -07004152 ret = i915_mutex_lock_interruptible(dev);
4153 if (ret)
Chris Wilsond65415d2017-01-19 08:22:10 +00004154 goto out;
Chris Wilsone6994ae2012-07-10 10:27:08 +01004155
4156 ret = i915_gem_object_set_cache_level(obj, level);
Chris Wilsone6994ae2012-07-10 10:27:08 +01004157 mutex_unlock(&dev->struct_mutex);
Chris Wilsond65415d2017-01-19 08:22:10 +00004158
4159out:
4160 i915_gem_object_put(obj);
Chris Wilsone6994ae2012-07-10 10:27:08 +01004161 return ret;
4162}
4163
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08004164/*
Dhinakaran Pandiyan07bcd992018-03-06 19:34:18 -08004165 * Prepare buffer for display plane (scanout, cursors, etc). Can be called from
4166 * an uninterruptible phase (modesetting) and allows any flushes to be pipelined
4167 * (for pageflips). We only flush the caches while preparing the buffer for
4168 * display, the callers are responsible for frontbuffer flush.
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08004169 */
Chris Wilson058d88c2016-08-15 10:49:06 +01004170struct i915_vma *
Chris Wilson2da3b9b2011-04-14 09:41:17 +01004171i915_gem_object_pin_to_display_plane(struct drm_i915_gem_object *obj,
4172 u32 alignment,
Chris Wilson59354852018-02-20 13:42:06 +00004173 const struct i915_ggtt_view *view,
4174 unsigned int flags)
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08004175{
Chris Wilson058d88c2016-08-15 10:49:06 +01004176 struct i915_vma *vma;
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08004177 int ret;
4178
Chris Wilson4c7d62c2016-10-28 13:58:32 +01004179 lockdep_assert_held(&obj->base.dev->struct_mutex);
4180
Chris Wilsonbd3d2252017-10-13 21:26:14 +01004181 /* Mark the global pin early so that we account for the
Chris Wilsoncc98b412013-08-09 12:25:09 +01004182 * display coherency whilst setting up the cache domains.
4183 */
Chris Wilsonbd3d2252017-10-13 21:26:14 +01004184 obj->pin_global++;
Chris Wilsoncc98b412013-08-09 12:25:09 +01004185
Eric Anholta7ef0642011-03-29 16:59:54 -07004186 /* The display engine is not coherent with the LLC cache on gen6. As
4187 * a result, we make sure that the pinning that is about to occur is
4188 * done with uncached PTEs. This is lowest common denominator for all
4189 * chipsets.
4190 *
4191 * However for gen6+, we could do better by using the GFDT bit instead
4192 * of uncaching, which would allow us to flush all the LLC-cached data
4193 * with that bit in the PTE to main memory with just one PIPE_CONTROL.
4194 */
Chris Wilson651d7942013-08-08 14:41:10 +01004195 ret = i915_gem_object_set_cache_level(obj,
Tvrtko Ursulin86527442016-10-13 11:03:00 +01004196 HAS_WT(to_i915(obj->base.dev)) ?
4197 I915_CACHE_WT : I915_CACHE_NONE);
Chris Wilson058d88c2016-08-15 10:49:06 +01004198 if (ret) {
4199 vma = ERR_PTR(ret);
Chris Wilsonbd3d2252017-10-13 21:26:14 +01004200 goto err_unpin_global;
Chris Wilson058d88c2016-08-15 10:49:06 +01004201 }
Eric Anholta7ef0642011-03-29 16:59:54 -07004202
Chris Wilson2da3b9b2011-04-14 09:41:17 +01004203 /* As the user may map the buffer once pinned in the display plane
4204 * (e.g. libkms for the bootup splash), we have to ensure that we
Chris Wilson2efb8132016-08-18 17:17:06 +01004205 * always use map_and_fenceable for all scanout buffers. However,
4206 * it may simply be too big to fit into mappable, in which case
4207 * put it anyway and hope that userspace can cope (but always first
4208 * try to preserve the existing ABI).
Chris Wilson2da3b9b2011-04-14 09:41:17 +01004209 */
Chris Wilson2efb8132016-08-18 17:17:06 +01004210 vma = ERR_PTR(-ENOSPC);
Chris Wilson59354852018-02-20 13:42:06 +00004211 if ((flags & PIN_MAPPABLE) == 0 &&
4212 (!view || view->type == I915_GGTT_VIEW_NORMAL))
Chris Wilson2efb8132016-08-18 17:17:06 +01004213 vma = i915_gem_object_ggtt_pin(obj, view, 0, alignment,
Chris Wilson59354852018-02-20 13:42:06 +00004214 flags |
4215 PIN_MAPPABLE |
4216 PIN_NONBLOCK);
4217 if (IS_ERR(vma))
Chris Wilson767a2222016-11-07 11:01:28 +00004218 vma = i915_gem_object_ggtt_pin(obj, view, 0, alignment, flags);
Chris Wilson058d88c2016-08-15 10:49:06 +01004219 if (IS_ERR(vma))
Chris Wilsonbd3d2252017-10-13 21:26:14 +01004220 goto err_unpin_global;
Chris Wilson2da3b9b2011-04-14 09:41:17 +01004221
Chris Wilsond8923dc2016-08-18 17:17:07 +01004222 vma->display_alignment = max_t(u64, vma->display_alignment, alignment);
4223
Chris Wilson5a97bcc2017-02-22 11:40:46 +00004224 __i915_gem_object_flush_for_display(obj);
Chris Wilsonb118c1e2010-05-27 13:18:14 +01004225
Chris Wilson2da3b9b2011-04-14 09:41:17 +01004226 /* It should now be out of any other write domains, and we can update
4227 * the domain values for our changes.
4228 */
Christian Königc0a51fd2018-02-16 13:43:38 +01004229 obj->read_domains |= I915_GEM_DOMAIN_GTT;
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08004230
Chris Wilson058d88c2016-08-15 10:49:06 +01004231 return vma;
Chris Wilsoncc98b412013-08-09 12:25:09 +01004232
Chris Wilsonbd3d2252017-10-13 21:26:14 +01004233err_unpin_global:
4234 obj->pin_global--;
Chris Wilson058d88c2016-08-15 10:49:06 +01004235 return vma;
Chris Wilsoncc98b412013-08-09 12:25:09 +01004236}
4237
4238void
Chris Wilson058d88c2016-08-15 10:49:06 +01004239i915_gem_object_unpin_from_display_plane(struct i915_vma *vma)
Chris Wilsoncc98b412013-08-09 12:25:09 +01004240{
Chris Wilson49d73912016-11-29 09:50:08 +00004241 lockdep_assert_held(&vma->vm->i915->drm.struct_mutex);
Chris Wilson4c7d62c2016-10-28 13:58:32 +01004242
Chris Wilsonbd3d2252017-10-13 21:26:14 +01004243 if (WARN_ON(vma->obj->pin_global == 0))
Tvrtko Ursulin8a0c39b2015-04-13 11:50:09 +01004244 return;
4245
Chris Wilsonbd3d2252017-10-13 21:26:14 +01004246 if (--vma->obj->pin_global == 0)
Chris Wilsonf51455d2017-01-10 14:47:34 +00004247 vma->display_alignment = I915_GTT_MIN_ALIGNMENT;
Tvrtko Ursuline6617332015-03-23 11:10:33 +00004248
Chris Wilson383d5822016-08-18 17:17:08 +01004249 /* Bump the LRU to try and avoid premature eviction whilst flipping */
Chris Wilsonbefedbb2017-01-19 19:26:55 +00004250 i915_gem_object_bump_inactive_ggtt(vma->obj);
Chris Wilson383d5822016-08-18 17:17:08 +01004251
Chris Wilson058d88c2016-08-15 10:49:06 +01004252 i915_vma_unpin(vma);
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08004253}
4254
Eric Anholte47c68e2008-11-14 13:35:19 -08004255/**
4256 * Moves a single object to the CPU read, and possibly write domain.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01004257 * @obj: object to act on
4258 * @write: requesting write or read-only access
Eric Anholte47c68e2008-11-14 13:35:19 -08004259 *
4260 * This function returns when the move is complete, including waiting on
4261 * flushes to occur.
4262 */
Chris Wilsondabdfe02012-03-26 10:10:27 +02004263int
Chris Wilson919926a2010-11-12 13:42:53 +00004264i915_gem_object_set_to_cpu_domain(struct drm_i915_gem_object *obj, bool write)
Eric Anholte47c68e2008-11-14 13:35:19 -08004265{
Eric Anholte47c68e2008-11-14 13:35:19 -08004266 int ret;
4267
Chris Wilsone95433c2016-10-28 13:58:27 +01004268 lockdep_assert_held(&obj->base.dev->struct_mutex);
Chris Wilson4c7d62c2016-10-28 13:58:32 +01004269
Chris Wilsone95433c2016-10-28 13:58:27 +01004270 ret = i915_gem_object_wait(obj,
4271 I915_WAIT_INTERRUPTIBLE |
4272 I915_WAIT_LOCKED |
4273 (write ? I915_WAIT_ALL : 0),
4274 MAX_SCHEDULE_TIMEOUT,
4275 NULL);
Chris Wilson88241782011-01-07 17:09:48 +00004276 if (ret)
4277 return ret;
4278
Chris Wilsonef749212017-04-12 12:01:10 +01004279 flush_write_domain(obj, ~I915_GEM_DOMAIN_CPU);
Eric Anholte47c68e2008-11-14 13:35:19 -08004280
Eric Anholte47c68e2008-11-14 13:35:19 -08004281 /* Flush the CPU cache if it's still invalid. */
Christian Königc0a51fd2018-02-16 13:43:38 +01004282 if ((obj->read_domains & I915_GEM_DOMAIN_CPU) == 0) {
Chris Wilson57822dc2017-02-22 11:40:48 +00004283 i915_gem_clflush_object(obj, I915_CLFLUSH_SYNC);
Christian Königc0a51fd2018-02-16 13:43:38 +01004284 obj->read_domains |= I915_GEM_DOMAIN_CPU;
Eric Anholte47c68e2008-11-14 13:35:19 -08004285 }
4286
4287 /* It should now be out of any other write domains, and we can update
4288 * the domain values for our changes.
4289 */
Christian Königc0a51fd2018-02-16 13:43:38 +01004290 GEM_BUG_ON(obj->write_domain & ~I915_GEM_DOMAIN_CPU);
Eric Anholte47c68e2008-11-14 13:35:19 -08004291
4292 /* If we're writing through the CPU, then the GPU read domains will
4293 * need to be invalidated at next use.
4294 */
Chris Wilsone27ab732017-06-15 13:38:49 +01004295 if (write)
4296 __start_cpu_write(obj);
Eric Anholt2ef7eea2008-11-10 10:53:25 -08004297
4298 return 0;
4299}
4300
Eric Anholt673a3942008-07-30 12:06:12 -07004301/* Throttle our rendering by waiting until the ring has completed our requests
4302 * emitted over 20 msec ago.
4303 *
Eric Anholtb9624422009-06-03 07:27:35 +00004304 * Note that if we were to use the current jiffies each time around the loop,
4305 * we wouldn't escape the function with any frames outstanding if the time to
4306 * render a frame was over 20ms.
4307 *
Eric Anholt673a3942008-07-30 12:06:12 -07004308 * This should get us reasonable parallelism between CPU and GPU but also
4309 * relatively low latency when blocking on a particular request to finish.
4310 */
4311static int
Chris Wilsonf787a5f2010-09-24 16:02:42 +01004312i915_gem_ring_throttle(struct drm_device *dev, struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07004313{
Chris Wilsonfac5e232016-07-04 11:34:36 +01004314 struct drm_i915_private *dev_priv = to_i915(dev);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01004315 struct drm_i915_file_private *file_priv = file->driver_priv;
Chris Wilsond0bc54f2015-05-21 21:01:48 +01004316 unsigned long recent_enough = jiffies - DRM_I915_THROTTLE_JIFFIES;
Chris Wilsone61e0f52018-02-21 09:56:36 +00004317 struct i915_request *request, *target = NULL;
Chris Wilsone95433c2016-10-28 13:58:27 +01004318 long ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004319
Chris Wilsonf4457ae2016-04-13 17:35:08 +01004320 /* ABI: return -EIO if already wedged */
4321 if (i915_terminally_wedged(&dev_priv->gpu_error))
4322 return -EIO;
Chris Wilsone110e8d2011-01-26 15:39:14 +00004323
Chris Wilson1c255952010-09-26 11:03:27 +01004324 spin_lock(&file_priv->mm.lock);
Chris Wilsonc8659ef2017-03-02 12:25:25 +00004325 list_for_each_entry(request, &file_priv->mm.request_list, client_link) {
Eric Anholtb9624422009-06-03 07:27:35 +00004326 if (time_after_eq(request->emitted_jiffies, recent_enough))
4327 break;
4328
Chris Wilsonc8659ef2017-03-02 12:25:25 +00004329 if (target) {
4330 list_del(&target->client_link);
4331 target->file_priv = NULL;
4332 }
John Harrisonfcfa423c2015-05-29 17:44:12 +01004333
John Harrison54fb2412014-11-24 18:49:27 +00004334 target = request;
Eric Anholtb9624422009-06-03 07:27:35 +00004335 }
John Harrisonff865882014-11-24 18:49:28 +00004336 if (target)
Chris Wilsone61e0f52018-02-21 09:56:36 +00004337 i915_request_get(target);
Chris Wilson1c255952010-09-26 11:03:27 +01004338 spin_unlock(&file_priv->mm.lock);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01004339
John Harrison54fb2412014-11-24 18:49:27 +00004340 if (target == NULL)
Chris Wilsonf787a5f2010-09-24 16:02:42 +01004341 return 0;
4342
Chris Wilsone61e0f52018-02-21 09:56:36 +00004343 ret = i915_request_wait(target,
Chris Wilsone95433c2016-10-28 13:58:27 +01004344 I915_WAIT_INTERRUPTIBLE,
4345 MAX_SCHEDULE_TIMEOUT);
Chris Wilsone61e0f52018-02-21 09:56:36 +00004346 i915_request_put(target);
John Harrisonff865882014-11-24 18:49:28 +00004347
Chris Wilsone95433c2016-10-28 13:58:27 +01004348 return ret < 0 ? ret : 0;
Eric Anholt673a3942008-07-30 12:06:12 -07004349}
4350
Chris Wilson058d88c2016-08-15 10:49:06 +01004351struct i915_vma *
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02004352i915_gem_object_ggtt_pin(struct drm_i915_gem_object *obj,
4353 const struct i915_ggtt_view *view,
Chris Wilson91b2db62016-08-04 16:32:23 +01004354 u64 size,
Chris Wilson2ffffd02016-08-04 16:32:22 +01004355 u64 alignment,
4356 u64 flags)
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02004357{
Chris Wilsonad16d2e2016-10-13 09:55:04 +01004358 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
4359 struct i915_address_space *vm = &dev_priv->ggtt.base;
Chris Wilson59bfa122016-08-04 16:32:31 +01004360 struct i915_vma *vma;
4361 int ret;
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03004362
Chris Wilson4c7d62c2016-10-28 13:58:32 +01004363 lockdep_assert_held(&obj->base.dev->struct_mutex);
4364
Chris Wilsonac87a6fd2018-02-20 13:42:05 +00004365 if (flags & PIN_MAPPABLE &&
4366 (!view || view->type == I915_GGTT_VIEW_NORMAL)) {
Chris Wilson43ae70d2017-10-09 09:44:01 +01004367 /* If the required space is larger than the available
4368 * aperture, we will not able to find a slot for the
4369 * object and unbinding the object now will be in
4370 * vain. Worse, doing so may cause us to ping-pong
4371 * the object in and out of the Global GTT and
4372 * waste a lot of cycles under the mutex.
4373 */
4374 if (obj->base.size > dev_priv->ggtt.mappable_end)
4375 return ERR_PTR(-E2BIG);
4376
4377 /* If NONBLOCK is set the caller is optimistically
4378 * trying to cache the full object within the mappable
4379 * aperture, and *must* have a fallback in place for
4380 * situations where we cannot bind the object. We
4381 * can be a little more lax here and use the fallback
4382 * more often to avoid costly migrations of ourselves
4383 * and other objects within the aperture.
4384 *
4385 * Half-the-aperture is used as a simple heuristic.
4386 * More interesting would to do search for a free
4387 * block prior to making the commitment to unbind.
4388 * That caters for the self-harm case, and with a
4389 * little more heuristics (e.g. NOFAULT, NOEVICT)
4390 * we could try to minimise harm to others.
4391 */
4392 if (flags & PIN_NONBLOCK &&
4393 obj->base.size > dev_priv->ggtt.mappable_end / 2)
4394 return ERR_PTR(-ENOSPC);
4395 }
4396
Chris Wilson718659a2017-01-16 15:21:28 +00004397 vma = i915_vma_instance(obj, vm, view);
Chris Wilsone0216b72017-01-19 19:26:57 +00004398 if (unlikely(IS_ERR(vma)))
Chris Wilson058d88c2016-08-15 10:49:06 +01004399 return vma;
Chris Wilson59bfa122016-08-04 16:32:31 +01004400
4401 if (i915_vma_misplaced(vma, size, alignment, flags)) {
Chris Wilson43ae70d2017-10-09 09:44:01 +01004402 if (flags & PIN_NONBLOCK) {
4403 if (i915_vma_is_pinned(vma) || i915_vma_is_active(vma))
4404 return ERR_PTR(-ENOSPC);
Chris Wilson59bfa122016-08-04 16:32:31 +01004405
Chris Wilson43ae70d2017-10-09 09:44:01 +01004406 if (flags & PIN_MAPPABLE &&
Chris Wilson944397f2017-01-09 16:16:11 +00004407 vma->fence_size > dev_priv->ggtt.mappable_end / 2)
Chris Wilsonad16d2e2016-10-13 09:55:04 +01004408 return ERR_PTR(-ENOSPC);
4409 }
4410
Chris Wilson59bfa122016-08-04 16:32:31 +01004411 WARN(i915_vma_is_pinned(vma),
4412 "bo is already pinned in ggtt with incorrect alignment:"
Chris Wilson05a20d02016-08-18 17:16:55 +01004413 " offset=%08x, req.alignment=%llx,"
4414 " req.map_and_fenceable=%d, vma->map_and_fenceable=%d\n",
4415 i915_ggtt_offset(vma), alignment,
Chris Wilson59bfa122016-08-04 16:32:31 +01004416 !!(flags & PIN_MAPPABLE),
Chris Wilson05a20d02016-08-18 17:16:55 +01004417 i915_vma_is_map_and_fenceable(vma));
Chris Wilson59bfa122016-08-04 16:32:31 +01004418 ret = i915_vma_unbind(vma);
4419 if (ret)
Chris Wilson058d88c2016-08-15 10:49:06 +01004420 return ERR_PTR(ret);
Chris Wilson59bfa122016-08-04 16:32:31 +01004421 }
4422
Chris Wilson058d88c2016-08-15 10:49:06 +01004423 ret = i915_vma_pin(vma, size, alignment, flags | PIN_GLOBAL);
4424 if (ret)
4425 return ERR_PTR(ret);
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02004426
Chris Wilson058d88c2016-08-15 10:49:06 +01004427 return vma;
Eric Anholt673a3942008-07-30 12:06:12 -07004428}
4429
Chris Wilsonedf6b762016-08-09 09:23:33 +01004430static __always_inline unsigned int __busy_read_flag(unsigned int id)
Chris Wilson3fdc13c2016-08-05 10:14:18 +01004431{
4432 /* Note that we could alias engines in the execbuf API, but
4433 * that would be very unwise as it prevents userspace from
4434 * fine control over engine selection. Ahem.
4435 *
4436 * This should be something like EXEC_MAX_ENGINE instead of
4437 * I915_NUM_ENGINES.
4438 */
4439 BUILD_BUG_ON(I915_NUM_ENGINES > 16);
4440 return 0x10000 << id;
4441}
4442
4443static __always_inline unsigned int __busy_write_id(unsigned int id)
4444{
Chris Wilson70cb4722016-08-09 18:08:25 +01004445 /* The uABI guarantees an active writer is also amongst the read
4446 * engines. This would be true if we accessed the activity tracking
4447 * under the lock, but as we perform the lookup of the object and
4448 * its activity locklessly we can not guarantee that the last_write
4449 * being active implies that we have set the same engine flag from
4450 * last_read - hence we always set both read and write busy for
4451 * last_write.
4452 */
4453 return id | __busy_read_flag(id);
Chris Wilson3fdc13c2016-08-05 10:14:18 +01004454}
4455
Chris Wilsonedf6b762016-08-09 09:23:33 +01004456static __always_inline unsigned int
Chris Wilsond07f0e52016-10-28 13:58:44 +01004457__busy_set_if_active(const struct dma_fence *fence,
Chris Wilson3fdc13c2016-08-05 10:14:18 +01004458 unsigned int (*flag)(unsigned int id))
4459{
Chris Wilsone61e0f52018-02-21 09:56:36 +00004460 struct i915_request *rq;
Chris Wilson12555012016-08-16 09:50:40 +01004461
Chris Wilsond07f0e52016-10-28 13:58:44 +01004462 /* We have to check the current hw status of the fence as the uABI
4463 * guarantees forward progress. We could rely on the idle worker
4464 * to eventually flush us, but to minimise latency just ask the
4465 * hardware.
4466 *
4467 * Note we only report on the status of native fences.
4468 */
4469 if (!dma_fence_is_i915(fence))
Chris Wilson12555012016-08-16 09:50:40 +01004470 return 0;
4471
Chris Wilsond07f0e52016-10-28 13:58:44 +01004472 /* opencode to_request() in order to avoid const warnings */
Chris Wilsone61e0f52018-02-21 09:56:36 +00004473 rq = container_of(fence, struct i915_request, fence);
4474 if (i915_request_completed(rq))
Chris Wilsond07f0e52016-10-28 13:58:44 +01004475 return 0;
4476
Chris Wilson1d39f282017-04-11 13:43:06 +01004477 return flag(rq->engine->uabi_id);
Chris Wilson3fdc13c2016-08-05 10:14:18 +01004478}
4479
Chris Wilsonedf6b762016-08-09 09:23:33 +01004480static __always_inline unsigned int
Chris Wilsond07f0e52016-10-28 13:58:44 +01004481busy_check_reader(const struct dma_fence *fence)
Chris Wilson3fdc13c2016-08-05 10:14:18 +01004482{
Chris Wilsond07f0e52016-10-28 13:58:44 +01004483 return __busy_set_if_active(fence, __busy_read_flag);
Chris Wilson3fdc13c2016-08-05 10:14:18 +01004484}
4485
Chris Wilsonedf6b762016-08-09 09:23:33 +01004486static __always_inline unsigned int
Chris Wilsond07f0e52016-10-28 13:58:44 +01004487busy_check_writer(const struct dma_fence *fence)
Chris Wilson3fdc13c2016-08-05 10:14:18 +01004488{
Chris Wilsond07f0e52016-10-28 13:58:44 +01004489 if (!fence)
4490 return 0;
4491
4492 return __busy_set_if_active(fence, __busy_write_id);
Chris Wilson3fdc13c2016-08-05 10:14:18 +01004493}
4494
Eric Anholt673a3942008-07-30 12:06:12 -07004495int
Eric Anholt673a3942008-07-30 12:06:12 -07004496i915_gem_busy_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00004497 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07004498{
4499 struct drm_i915_gem_busy *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00004500 struct drm_i915_gem_object *obj;
Chris Wilsond07f0e52016-10-28 13:58:44 +01004501 struct reservation_object_list *list;
4502 unsigned int seq;
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004503 int err;
Eric Anholt673a3942008-07-30 12:06:12 -07004504
Chris Wilsond07f0e52016-10-28 13:58:44 +01004505 err = -ENOENT;
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004506 rcu_read_lock();
4507 obj = i915_gem_object_lookup_rcu(file, args->handle);
Chris Wilsond07f0e52016-10-28 13:58:44 +01004508 if (!obj)
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004509 goto out;
Chris Wilsond07f0e52016-10-28 13:58:44 +01004510
4511 /* A discrepancy here is that we do not report the status of
4512 * non-i915 fences, i.e. even though we may report the object as idle,
4513 * a call to set-domain may still stall waiting for foreign rendering.
4514 * This also means that wait-ioctl may report an object as busy,
4515 * where busy-ioctl considers it idle.
4516 *
4517 * We trade the ability to warn of foreign fences to report on which
4518 * i915 engines are active for the object.
4519 *
4520 * Alternatively, we can trade that extra information on read/write
4521 * activity with
4522 * args->busy =
4523 * !reservation_object_test_signaled_rcu(obj->resv, true);
4524 * to report the overall busyness. This is what the wait-ioctl does.
4525 *
4526 */
4527retry:
4528 seq = raw_read_seqcount(&obj->resv->seq);
4529
4530 /* Translate the exclusive fence to the READ *and* WRITE engine */
4531 args->busy = busy_check_writer(rcu_dereference(obj->resv->fence_excl));
4532
4533 /* Translate shared fences to READ set of engines */
4534 list = rcu_dereference(obj->resv->fence);
4535 if (list) {
4536 unsigned int shared_count = list->shared_count, i;
4537
4538 for (i = 0; i < shared_count; ++i) {
4539 struct dma_fence *fence =
4540 rcu_dereference(list->shared[i]);
4541
4542 args->busy |= busy_check_reader(fence);
4543 }
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004544 }
Zou Nan haid1b851f2010-05-21 09:08:57 +08004545
Chris Wilsond07f0e52016-10-28 13:58:44 +01004546 if (args->busy && read_seqcount_retry(&obj->resv->seq, seq))
4547 goto retry;
Chris Wilson426960b2016-01-15 16:51:46 +00004548
Chris Wilsond07f0e52016-10-28 13:58:44 +01004549 err = 0;
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004550out:
4551 rcu_read_unlock();
4552 return err;
Eric Anholt673a3942008-07-30 12:06:12 -07004553}
4554
4555int
4556i915_gem_throttle_ioctl(struct drm_device *dev, void *data,
4557 struct drm_file *file_priv)
4558{
Akshay Joshi0206e352011-08-16 15:34:10 -04004559 return i915_gem_ring_throttle(dev, file_priv);
Eric Anholt673a3942008-07-30 12:06:12 -07004560}
4561
Chris Wilson3ef94da2009-09-14 16:50:29 +01004562int
4563i915_gem_madvise_ioctl(struct drm_device *dev, void *data,
4564 struct drm_file *file_priv)
4565{
Chris Wilsonfac5e232016-07-04 11:34:36 +01004566 struct drm_i915_private *dev_priv = to_i915(dev);
Chris Wilson3ef94da2009-09-14 16:50:29 +01004567 struct drm_i915_gem_madvise *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00004568 struct drm_i915_gem_object *obj;
Chris Wilson1233e2d2016-10-28 13:58:37 +01004569 int err;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004570
4571 switch (args->madv) {
4572 case I915_MADV_DONTNEED:
4573 case I915_MADV_WILLNEED:
4574 break;
4575 default:
4576 return -EINVAL;
4577 }
4578
Chris Wilson03ac0642016-07-20 13:31:51 +01004579 obj = i915_gem_object_lookup(file_priv, args->handle);
Chris Wilson1233e2d2016-10-28 13:58:37 +01004580 if (!obj)
4581 return -ENOENT;
4582
4583 err = mutex_lock_interruptible(&obj->mm.lock);
4584 if (err)
4585 goto out;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004586
Chris Wilsonf1fa4f42017-10-13 21:26:13 +01004587 if (i915_gem_object_has_pages(obj) &&
Chris Wilson3e510a82016-08-05 10:14:23 +01004588 i915_gem_object_is_tiled(obj) &&
Daniel Vetter656bfa32014-11-20 09:26:30 +01004589 dev_priv->quirks & QUIRK_PIN_SWIZZLED_PAGES) {
Chris Wilsonbc0629a2016-11-01 10:03:17 +00004590 if (obj->mm.madv == I915_MADV_WILLNEED) {
4591 GEM_BUG_ON(!obj->mm.quirked);
Chris Wilsona4f5ea62016-10-28 13:58:35 +01004592 __i915_gem_object_unpin_pages(obj);
Chris Wilsonbc0629a2016-11-01 10:03:17 +00004593 obj->mm.quirked = false;
4594 }
4595 if (args->madv == I915_MADV_WILLNEED) {
Chris Wilson2c3a3f42016-11-04 10:30:01 +00004596 GEM_BUG_ON(obj->mm.quirked);
Chris Wilsona4f5ea62016-10-28 13:58:35 +01004597 __i915_gem_object_pin_pages(obj);
Chris Wilsonbc0629a2016-11-01 10:03:17 +00004598 obj->mm.quirked = true;
4599 }
Daniel Vetter656bfa32014-11-20 09:26:30 +01004600 }
4601
Chris Wilsona4f5ea62016-10-28 13:58:35 +01004602 if (obj->mm.madv != __I915_MADV_PURGED)
4603 obj->mm.madv = args->madv;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004604
Chris Wilson6c085a72012-08-20 11:40:46 +02004605 /* if the object is no longer attached, discard its backing storage */
Chris Wilsonf1fa4f42017-10-13 21:26:13 +01004606 if (obj->mm.madv == I915_MADV_DONTNEED &&
4607 !i915_gem_object_has_pages(obj))
Chris Wilson2d7ef392009-09-20 23:13:10 +01004608 i915_gem_object_truncate(obj);
4609
Chris Wilsona4f5ea62016-10-28 13:58:35 +01004610 args->retained = obj->mm.madv != __I915_MADV_PURGED;
Chris Wilson1233e2d2016-10-28 13:58:37 +01004611 mutex_unlock(&obj->mm.lock);
Chris Wilsonbb6baf72009-09-22 14:24:13 +01004612
Chris Wilson1233e2d2016-10-28 13:58:37 +01004613out:
Chris Wilsonf8c417c2016-07-20 13:31:53 +01004614 i915_gem_object_put(obj);
Chris Wilson1233e2d2016-10-28 13:58:37 +01004615 return err;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004616}
4617
Chris Wilson5b8c8ae2016-11-16 19:07:04 +00004618static void
Chris Wilsone61e0f52018-02-21 09:56:36 +00004619frontbuffer_retire(struct i915_gem_active *active, struct i915_request *request)
Chris Wilson5b8c8ae2016-11-16 19:07:04 +00004620{
4621 struct drm_i915_gem_object *obj =
4622 container_of(active, typeof(*obj), frontbuffer_write);
4623
Chris Wilsond59b21e2017-02-22 11:40:49 +00004624 intel_fb_obj_flush(obj, ORIGIN_CS);
Chris Wilson5b8c8ae2016-11-16 19:07:04 +00004625}
4626
Chris Wilson37e680a2012-06-07 15:38:42 +01004627void i915_gem_object_init(struct drm_i915_gem_object *obj,
4628 const struct drm_i915_gem_object_ops *ops)
Chris Wilson0327d6b2012-08-11 15:41:06 +01004629{
Chris Wilson1233e2d2016-10-28 13:58:37 +01004630 mutex_init(&obj->mm.lock);
4631
Ben Widawsky2f633152013-07-17 12:19:03 -07004632 INIT_LIST_HEAD(&obj->vma_list);
Chris Wilsond1b48c12017-08-16 09:52:08 +01004633 INIT_LIST_HEAD(&obj->lut_list);
Chris Wilson8d9d5742015-04-07 16:20:38 +01004634 INIT_LIST_HEAD(&obj->batch_pool_link);
Chris Wilson0327d6b2012-08-11 15:41:06 +01004635
Chris Wilson37e680a2012-06-07 15:38:42 +01004636 obj->ops = ops;
4637
Chris Wilsond07f0e52016-10-28 13:58:44 +01004638 reservation_object_init(&obj->__builtin_resv);
4639 obj->resv = &obj->__builtin_resv;
4640
Chris Wilson50349242016-08-18 17:17:04 +01004641 obj->frontbuffer_ggtt_origin = ORIGIN_GTT;
Chris Wilson5b8c8ae2016-11-16 19:07:04 +00004642 init_request_active(&obj->frontbuffer_write, frontbuffer_retire);
Chris Wilsona4f5ea62016-10-28 13:58:35 +01004643
4644 obj->mm.madv = I915_MADV_WILLNEED;
4645 INIT_RADIX_TREE(&obj->mm.get_page.radix, GFP_KERNEL | __GFP_NOWARN);
4646 mutex_init(&obj->mm.get_page.lock);
Chris Wilson0327d6b2012-08-11 15:41:06 +01004647
Dave Gordonf19ec8c2016-07-04 11:34:37 +01004648 i915_gem_info_add_obj(to_i915(obj->base.dev), obj->base.size);
Chris Wilson0327d6b2012-08-11 15:41:06 +01004649}
4650
Chris Wilson37e680a2012-06-07 15:38:42 +01004651static const struct drm_i915_gem_object_ops i915_gem_object_ops = {
Tvrtko Ursulin3599a912016-11-01 14:44:10 +00004652 .flags = I915_GEM_OBJECT_HAS_STRUCT_PAGE |
4653 I915_GEM_OBJECT_IS_SHRINKABLE,
Chris Wilson7c55e2c2017-03-07 12:03:38 +00004654
Chris Wilson37e680a2012-06-07 15:38:42 +01004655 .get_pages = i915_gem_object_get_pages_gtt,
4656 .put_pages = i915_gem_object_put_pages_gtt,
Chris Wilson7c55e2c2017-03-07 12:03:38 +00004657
4658 .pwrite = i915_gem_object_pwrite_gtt,
Chris Wilson37e680a2012-06-07 15:38:42 +01004659};
4660
Matthew Auld465c4032017-10-06 23:18:14 +01004661static int i915_gem_object_create_shmem(struct drm_device *dev,
4662 struct drm_gem_object *obj,
4663 size_t size)
4664{
4665 struct drm_i915_private *i915 = to_i915(dev);
4666 unsigned long flags = VM_NORESERVE;
4667 struct file *filp;
4668
4669 drm_gem_private_object_init(dev, obj, size);
4670
4671 if (i915->mm.gemfs)
4672 filp = shmem_file_setup_with_mnt(i915->mm.gemfs, "i915", size,
4673 flags);
4674 else
4675 filp = shmem_file_setup("i915", size, flags);
4676
4677 if (IS_ERR(filp))
4678 return PTR_ERR(filp);
4679
4680 obj->filp = filp;
4681
4682 return 0;
4683}
4684
Chris Wilsonb4bcbe22016-10-18 13:02:49 +01004685struct drm_i915_gem_object *
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +00004686i915_gem_object_create(struct drm_i915_private *dev_priv, u64 size)
Daniel Vetterac52bc52010-04-09 19:05:06 +00004687{
Daniel Vetterc397b902010-04-09 19:05:07 +00004688 struct drm_i915_gem_object *obj;
Hugh Dickins5949eac2011-06-27 16:18:18 -07004689 struct address_space *mapping;
Chris Wilsonb8f55be2017-08-11 12:11:16 +01004690 unsigned int cache_level;
Daniel Vetter1a240d42012-11-29 22:18:51 +01004691 gfp_t mask;
Chris Wilsonfe3db792016-04-25 13:32:13 +01004692 int ret;
Daniel Vetterc397b902010-04-09 19:05:07 +00004693
Chris Wilsonb4bcbe22016-10-18 13:02:49 +01004694 /* There is a prevalence of the assumption that we fit the object's
4695 * page count inside a 32bit _signed_ variable. Let's document this and
4696 * catch if we ever need to fix it. In the meantime, if you do spot
4697 * such a local variable, please consider fixing!
4698 */
Tvrtko Ursulin7a3ee5d2017-03-30 17:31:30 +01004699 if (size >> PAGE_SHIFT > INT_MAX)
Chris Wilsonb4bcbe22016-10-18 13:02:49 +01004700 return ERR_PTR(-E2BIG);
4701
4702 if (overflows_type(size, obj->base.size))
4703 return ERR_PTR(-E2BIG);
4704
Tvrtko Ursulin187685c2016-12-01 14:16:36 +00004705 obj = i915_gem_object_alloc(dev_priv);
Daniel Vetterc397b902010-04-09 19:05:07 +00004706 if (obj == NULL)
Chris Wilsonfe3db792016-04-25 13:32:13 +01004707 return ERR_PTR(-ENOMEM);
Daniel Vetterc397b902010-04-09 19:05:07 +00004708
Matthew Auld465c4032017-10-06 23:18:14 +01004709 ret = i915_gem_object_create_shmem(&dev_priv->drm, &obj->base, size);
Chris Wilsonfe3db792016-04-25 13:32:13 +01004710 if (ret)
4711 goto fail;
Daniel Vetterc397b902010-04-09 19:05:07 +00004712
Chris Wilsonbed1ea92012-05-24 20:48:12 +01004713 mask = GFP_HIGHUSER | __GFP_RECLAIMABLE;
Jani Nikulac0f86832016-12-07 12:13:04 +02004714 if (IS_I965GM(dev_priv) || IS_I965G(dev_priv)) {
Chris Wilsonbed1ea92012-05-24 20:48:12 +01004715 /* 965gm cannot relocate objects above 4GiB. */
4716 mask &= ~__GFP_HIGHMEM;
4717 mask |= __GFP_DMA32;
4718 }
4719
Al Viro93c76a32015-12-04 23:45:44 -05004720 mapping = obj->base.filp->f_mapping;
Chris Wilsonbed1ea92012-05-24 20:48:12 +01004721 mapping_set_gfp_mask(mapping, mask);
Chris Wilson4846bf02017-06-09 12:03:46 +01004722 GEM_BUG_ON(!(mapping_gfp_mask(mapping) & __GFP_RECLAIM));
Hugh Dickins5949eac2011-06-27 16:18:18 -07004723
Chris Wilson37e680a2012-06-07 15:38:42 +01004724 i915_gem_object_init(obj, &i915_gem_object_ops);
Chris Wilson73aa8082010-09-30 11:46:12 +01004725
Christian Königc0a51fd2018-02-16 13:43:38 +01004726 obj->write_domain = I915_GEM_DOMAIN_CPU;
4727 obj->read_domains = I915_GEM_DOMAIN_CPU;
Daniel Vetterc397b902010-04-09 19:05:07 +00004728
Chris Wilsonb8f55be2017-08-11 12:11:16 +01004729 if (HAS_LLC(dev_priv))
Eugeni Dodonov3d29b842012-01-17 14:43:53 -02004730 /* On some devices, we can have the GPU use the LLC (the CPU
Eric Anholta1871112011-03-29 16:59:55 -07004731 * cache) for about a 10% performance improvement
4732 * compared to uncached. Graphics requests other than
4733 * display scanout are coherent with the CPU in
4734 * accessing this cache. This means in this mode we
4735 * don't need to clflush on the CPU side, and on the
4736 * GPU side we only need to flush internal caches to
4737 * get data visible to the CPU.
4738 *
4739 * However, we maintain the display planes as UC, and so
4740 * need to rebind when first used as such.
4741 */
Chris Wilsonb8f55be2017-08-11 12:11:16 +01004742 cache_level = I915_CACHE_LLC;
4743 else
4744 cache_level = I915_CACHE_NONE;
Eric Anholta1871112011-03-29 16:59:55 -07004745
Chris Wilsonb8f55be2017-08-11 12:11:16 +01004746 i915_gem_object_set_cache_coherency(obj, cache_level);
Chris Wilsone27ab732017-06-15 13:38:49 +01004747
Daniel Vetterd861e332013-07-24 23:25:03 +02004748 trace_i915_gem_object_create(obj);
4749
Chris Wilson05394f32010-11-08 19:18:58 +00004750 return obj;
Chris Wilsonfe3db792016-04-25 13:32:13 +01004751
4752fail:
4753 i915_gem_object_free(obj);
Chris Wilsonfe3db792016-04-25 13:32:13 +01004754 return ERR_PTR(ret);
Daniel Vetterac52bc52010-04-09 19:05:06 +00004755}
4756
Chris Wilson340fbd82014-05-22 09:16:52 +01004757static bool discard_backing_storage(struct drm_i915_gem_object *obj)
4758{
4759 /* If we are the last user of the backing storage (be it shmemfs
4760 * pages or stolen etc), we know that the pages are going to be
4761 * immediately released. In this case, we can then skip copying
4762 * back the contents from the GPU.
4763 */
4764
Chris Wilsona4f5ea62016-10-28 13:58:35 +01004765 if (obj->mm.madv != I915_MADV_WILLNEED)
Chris Wilson340fbd82014-05-22 09:16:52 +01004766 return false;
4767
4768 if (obj->base.filp == NULL)
4769 return true;
4770
4771 /* At first glance, this looks racy, but then again so would be
4772 * userspace racing mmap against close. However, the first external
4773 * reference to the filp can only be obtained through the
4774 * i915_gem_mmap_ioctl() which safeguards us against the user
4775 * acquiring such a reference whilst we are in the middle of
4776 * freeing the object.
4777 */
4778 return atomic_long_read(&obj->base.filp->f_count) == 1;
4779}
4780
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004781static void __i915_gem_free_objects(struct drm_i915_private *i915,
4782 struct llist_node *freed)
Chris Wilsonbe726152010-07-23 23:18:50 +01004783{
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004784 struct drm_i915_gem_object *obj, *on;
Chris Wilsonbe726152010-07-23 23:18:50 +01004785
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004786 intel_runtime_pm_get(i915);
Chris Wilsoncc731f52017-10-13 21:26:21 +01004787 llist_for_each_entry_safe(obj, on, freed, freed) {
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004788 struct i915_vma *vma, *vn;
Paulo Zanonif65c9162013-11-27 18:20:34 -02004789
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004790 trace_i915_gem_object_destroy(obj);
4791
Chris Wilsoncc731f52017-10-13 21:26:21 +01004792 mutex_lock(&i915->drm.struct_mutex);
4793
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004794 GEM_BUG_ON(i915_gem_object_is_active(obj));
4795 list_for_each_entry_safe(vma, vn,
4796 &obj->vma_list, obj_link) {
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004797 GEM_BUG_ON(i915_vma_is_active(vma));
4798 vma->flags &= ~I915_VMA_PIN_MASK;
Chris Wilson3365e222018-05-03 20:51:14 +01004799 i915_vma_destroy(vma);
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004800 }
Chris Wilsondb6c2b42016-11-01 11:54:00 +00004801 GEM_BUG_ON(!list_empty(&obj->vma_list));
4802 GEM_BUG_ON(!RB_EMPTY_ROOT(&obj->vma_tree));
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004803
Chris Wilsonf2123812017-10-16 12:40:37 +01004804 /* This serializes freeing with the shrinker. Since the free
4805 * is delayed, first by RCU then by the workqueue, we want the
4806 * shrinker to be able to free pages of unreferenced objects,
4807 * or else we may oom whilst there are plenty of deferred
4808 * freed objects.
4809 */
4810 if (i915_gem_object_has_pages(obj)) {
4811 spin_lock(&i915->mm.obj_lock);
4812 list_del_init(&obj->mm.link);
4813 spin_unlock(&i915->mm.obj_lock);
4814 }
4815
Chris Wilsoncc731f52017-10-13 21:26:21 +01004816 mutex_unlock(&i915->drm.struct_mutex);
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004817
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004818 GEM_BUG_ON(obj->bind_count);
Chris Wilsona65adaf2017-10-09 09:43:57 +01004819 GEM_BUG_ON(obj->userfault_count);
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004820 GEM_BUG_ON(atomic_read(&obj->frontbuffer_bits));
Chris Wilson67b48042017-08-22 12:05:16 +01004821 GEM_BUG_ON(!list_empty(&obj->lut_list));
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004822
4823 if (obj->ops->release)
4824 obj->ops->release(obj);
4825
4826 if (WARN_ON(i915_gem_object_has_pinned_pages(obj)))
4827 atomic_set(&obj->mm.pages_pin_count, 0);
Chris Wilson548625e2016-11-01 12:11:34 +00004828 __i915_gem_object_put_pages(obj, I915_MM_NORMAL);
Chris Wilsonf1fa4f42017-10-13 21:26:13 +01004829 GEM_BUG_ON(i915_gem_object_has_pages(obj));
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004830
4831 if (obj->base.import_attach)
4832 drm_prime_gem_destroy(&obj->base, NULL);
4833
Chris Wilsond07f0e52016-10-28 13:58:44 +01004834 reservation_object_fini(&obj->__builtin_resv);
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004835 drm_gem_object_release(&obj->base);
4836 i915_gem_info_remove_obj(i915, obj->base.size);
4837
4838 kfree(obj->bit_17);
4839 i915_gem_object_free(obj);
Chris Wilsoncc731f52017-10-13 21:26:21 +01004840
Chris Wilsonc9c70472018-02-19 22:06:31 +00004841 GEM_BUG_ON(!atomic_read(&i915->mm.free_count));
4842 atomic_dec(&i915->mm.free_count);
4843
Chris Wilsoncc731f52017-10-13 21:26:21 +01004844 if (on)
4845 cond_resched();
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004846 }
Chris Wilsoncc731f52017-10-13 21:26:21 +01004847 intel_runtime_pm_put(i915);
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004848}
4849
4850static void i915_gem_flush_free_objects(struct drm_i915_private *i915)
4851{
4852 struct llist_node *freed;
4853
Chris Wilson87701b42017-10-13 21:26:20 +01004854 /* Free the oldest, most stale object to keep the free_list short */
4855 freed = NULL;
4856 if (!llist_empty(&i915->mm.free_list)) { /* quick test for hotpath */
4857 /* Only one consumer of llist_del_first() allowed */
4858 spin_lock(&i915->mm.free_lock);
4859 freed = llist_del_first(&i915->mm.free_list);
4860 spin_unlock(&i915->mm.free_lock);
4861 }
4862 if (unlikely(freed)) {
4863 freed->next = NULL;
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004864 __i915_gem_free_objects(i915, freed);
Chris Wilson87701b42017-10-13 21:26:20 +01004865 }
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004866}
4867
4868static void __i915_gem_free_work(struct work_struct *work)
4869{
4870 struct drm_i915_private *i915 =
4871 container_of(work, struct drm_i915_private, mm.free_work);
4872 struct llist_node *freed;
Chris Wilson26e12f82011-03-20 11:20:19 +00004873
Chris Wilson2ef1e722018-01-15 20:57:59 +00004874 /*
4875 * All file-owned VMA should have been released by this point through
Chris Wilsonb1f788c2016-08-04 07:52:45 +01004876 * i915_gem_close_object(), or earlier by i915_gem_context_close().
4877 * However, the object may also be bound into the global GTT (e.g.
4878 * older GPUs without per-process support, or for direct access through
4879 * the GTT either for the user or for scanout). Those VMA still need to
4880 * unbound now.
4881 */
Chris Wilson1488fc02012-04-24 15:47:31 +01004882
Chris Wilsonf991c492017-11-06 11:15:08 +00004883 spin_lock(&i915->mm.free_lock);
Chris Wilson5ad08be2017-04-07 11:25:51 +01004884 while ((freed = llist_del_all(&i915->mm.free_list))) {
Chris Wilsonf991c492017-11-06 11:15:08 +00004885 spin_unlock(&i915->mm.free_lock);
4886
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004887 __i915_gem_free_objects(i915, freed);
Chris Wilson5ad08be2017-04-07 11:25:51 +01004888 if (need_resched())
Chris Wilsonf991c492017-11-06 11:15:08 +00004889 return;
4890
4891 spin_lock(&i915->mm.free_lock);
Chris Wilson5ad08be2017-04-07 11:25:51 +01004892 }
Chris Wilsonf991c492017-11-06 11:15:08 +00004893 spin_unlock(&i915->mm.free_lock);
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004894}
4895
4896static void __i915_gem_free_object_rcu(struct rcu_head *head)
4897{
4898 struct drm_i915_gem_object *obj =
4899 container_of(head, typeof(*obj), rcu);
4900 struct drm_i915_private *i915 = to_i915(obj->base.dev);
4901
Chris Wilson2ef1e722018-01-15 20:57:59 +00004902 /*
4903 * Since we require blocking on struct_mutex to unbind the freed
4904 * object from the GPU before releasing resources back to the
4905 * system, we can not do that directly from the RCU callback (which may
4906 * be a softirq context), but must instead then defer that work onto a
4907 * kthread. We use the RCU callback rather than move the freed object
4908 * directly onto the work queue so that we can mix between using the
4909 * worker and performing frees directly from subsequent allocations for
4910 * crude but effective memory throttling.
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004911 */
4912 if (llist_add(&obj->freed, &i915->mm.free_list))
Chris Wilsonbeacbd12018-01-15 12:28:45 +00004913 queue_work(i915->wq, &i915->mm.free_work);
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004914}
4915
4916void i915_gem_free_object(struct drm_gem_object *gem_obj)
4917{
4918 struct drm_i915_gem_object *obj = to_intel_bo(gem_obj);
4919
Chris Wilsonbc0629a2016-11-01 10:03:17 +00004920 if (obj->mm.quirked)
4921 __i915_gem_object_unpin_pages(obj);
4922
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004923 if (discard_backing_storage(obj))
4924 obj->mm.madv = I915_MADV_DONTNEED;
Daniel Vettera071fa02014-06-18 23:28:09 +02004925
Chris Wilson2ef1e722018-01-15 20:57:59 +00004926 /*
4927 * Before we free the object, make sure any pure RCU-only
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004928 * read-side critical sections are complete, e.g.
4929 * i915_gem_busy_ioctl(). For the corresponding synchronized
4930 * lookup see i915_gem_object_lookup_rcu().
4931 */
Chris Wilsonc9c70472018-02-19 22:06:31 +00004932 atomic_inc(&to_i915(obj->base.dev)->mm.free_count);
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004933 call_rcu(&obj->rcu, __i915_gem_free_object_rcu);
Chris Wilsonbe726152010-07-23 23:18:50 +01004934}
4935
Chris Wilsonf8a7fde2016-10-28 13:58:29 +01004936void __i915_gem_object_release_unless_active(struct drm_i915_gem_object *obj)
4937{
4938 lockdep_assert_held(&obj->base.dev->struct_mutex);
4939
Chris Wilsond1b48c12017-08-16 09:52:08 +01004940 if (!i915_gem_object_has_active_reference(obj) &&
4941 i915_gem_object_is_active(obj))
Chris Wilsonf8a7fde2016-10-28 13:58:29 +01004942 i915_gem_object_set_active_reference(obj);
4943 else
4944 i915_gem_object_put(obj);
4945}
4946
Chris Wilsonae6c4572017-11-10 14:26:28 +00004947static void assert_kernel_context_is_current(struct drm_i915_private *i915)
Chris Wilson3033aca2016-10-28 13:58:47 +01004948{
Chris Wilsonae6c4572017-11-10 14:26:28 +00004949 struct i915_gem_context *kernel_context = i915->kernel_context;
Chris Wilson3033aca2016-10-28 13:58:47 +01004950 struct intel_engine_cs *engine;
4951 enum intel_engine_id id;
4952
Chris Wilsonae6c4572017-11-10 14:26:28 +00004953 for_each_engine(engine, i915, id) {
Chris Wilsona89d1f92018-05-02 17:38:39 +01004954 GEM_BUG_ON(__i915_gem_active_peek(&engine->timeline.last_request));
Chris Wilsonae6c4572017-11-10 14:26:28 +00004955 GEM_BUG_ON(engine->last_retired_context != kernel_context);
4956 }
Chris Wilson3033aca2016-10-28 13:58:47 +01004957}
4958
Chris Wilson24145512017-01-24 11:01:35 +00004959void i915_gem_sanitize(struct drm_i915_private *i915)
4960{
Chris Wilsonf36325f2017-08-26 12:09:34 +01004961 if (i915_terminally_wedged(&i915->gpu_error)) {
4962 mutex_lock(&i915->drm.struct_mutex);
4963 i915_gem_unset_wedged(i915);
4964 mutex_unlock(&i915->drm.struct_mutex);
4965 }
4966
Chris Wilson24145512017-01-24 11:01:35 +00004967 /*
4968 * If we inherit context state from the BIOS or earlier occupants
4969 * of the GPU, the GPU may be in an inconsistent state when we
4970 * try to take over. The only way to remove the earlier state
4971 * is by resetting. However, resetting on earlier gen is tricky as
4972 * it may impact the display and we are uncertain about the stability
Joonas Lahtinenea117b82017-04-28 10:53:38 +03004973 * of the reset, so this could be applied to even earlier gen.
Chris Wilson24145512017-01-24 11:01:35 +00004974 */
Daniele Ceraolo Spurioce1599a2018-02-07 13:24:40 -08004975 if (INTEL_GEN(i915) >= 5 && intel_has_gpu_reset(i915))
4976 WARN_ON(intel_gpu_reset(i915, ALL_ENGINES));
Chris Wilson24145512017-01-24 11:01:35 +00004977}
4978
Tvrtko Ursulinbf9e8422016-12-01 14:16:38 +00004979int i915_gem_suspend(struct drm_i915_private *dev_priv)
Eric Anholt673a3942008-07-30 12:06:12 -07004980{
Tvrtko Ursulinbf9e8422016-12-01 14:16:38 +00004981 struct drm_device *dev = &dev_priv->drm;
Chris Wilsondcff85c2016-08-05 10:14:11 +01004982 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004983
Chris Wilsonc998e8a2017-03-02 08:30:29 +00004984 intel_runtime_pm_get(dev_priv);
Chris Wilson54b4f682016-07-21 21:16:19 +01004985 intel_suspend_gt_powersave(dev_priv);
4986
Chris Wilson45c5f202013-10-16 11:50:01 +01004987 mutex_lock(&dev->struct_mutex);
Chris Wilson5ab57c72016-07-15 14:56:20 +01004988
4989 /* We have to flush all the executing contexts to main memory so
4990 * that they can saved in the hibernation image. To ensure the last
4991 * context image is coherent, we have to switch away from it. That
4992 * leaves the dev_priv->kernel_context still active when
4993 * we actually suspend, and its image in memory may not match the GPU
4994 * state. Fortunately, the kernel_context is disposable and we do
4995 * not rely on its state.
4996 */
Chris Wilsonecf73eb2017-11-30 10:29:51 +00004997 if (!i915_terminally_wedged(&dev_priv->gpu_error)) {
4998 ret = i915_gem_switch_to_kernel_context(dev_priv);
4999 if (ret)
5000 goto err_unlock;
Chris Wilson5ab57c72016-07-15 14:56:20 +01005001
Chris Wilsonecf73eb2017-11-30 10:29:51 +00005002 ret = i915_gem_wait_for_idle(dev_priv,
5003 I915_WAIT_INTERRUPTIBLE |
5004 I915_WAIT_LOCKED);
5005 if (ret && ret != -EIO)
5006 goto err_unlock;
Chris Wilsonf7403342013-09-13 23:57:04 +01005007
Chris Wilsonecf73eb2017-11-30 10:29:51 +00005008 assert_kernel_context_is_current(dev_priv);
5009 }
Chris Wilson829a0af2017-06-20 12:05:45 +01005010 i915_gem_contexts_lost(dev_priv);
Chris Wilson45c5f202013-10-16 11:50:01 +01005011 mutex_unlock(&dev->struct_mutex);
5012
Michal Wajdeczko7cfca4a2018-03-02 11:15:49 +00005013 intel_uc_suspend(dev_priv);
Sagar Arun Kamble63987bf2017-04-05 15:51:50 +05305014
Chris Wilson737b1502015-01-26 18:03:03 +02005015 cancel_delayed_work_sync(&dev_priv->gpu_error.hangcheck_work);
Chris Wilson67d97da2016-07-04 08:08:31 +01005016 cancel_delayed_work_sync(&dev_priv->gt.retire_work);
Chris Wilsonbdeb9782016-12-23 14:57:56 +00005017
5018 /* As the idle_work is rearming if it detects a race, play safe and
5019 * repeat the flush until it is definitely idle.
5020 */
Chris Wilson7c262402017-10-06 11:40:38 +01005021 drain_delayed_work(&dev_priv->gt.idle_work);
Chris Wilsonbdeb9782016-12-23 14:57:56 +00005022
Chris Wilsonbdcf1202014-11-25 11:56:33 +00005023 /* Assert that we sucessfully flushed all the work and
5024 * reset the GPU back to its idle, low power state.
5025 */
Chris Wilson67d97da2016-07-04 08:08:31 +01005026 WARN_ON(dev_priv->gt.awake);
Chris Wilsonfc692bd2017-08-26 12:09:35 +01005027 if (WARN_ON(!intel_engines_are_idle(dev_priv)))
5028 i915_gem_set_wedged(dev_priv); /* no hope, discard everything */
Chris Wilsonbdcf1202014-11-25 11:56:33 +00005029
Imre Deak1c777c52016-10-12 17:46:37 +03005030 /*
5031 * Neither the BIOS, ourselves or any other kernel
5032 * expects the system to be in execlists mode on startup,
5033 * so we need to reset the GPU back to legacy mode. And the only
5034 * known way to disable logical contexts is through a GPU reset.
5035 *
5036 * So in order to leave the system in a known default configuration,
5037 * always reset the GPU upon unload and suspend. Afterwards we then
5038 * clean up the GEM state tracking, flushing off the requests and
5039 * leaving the system in a known idle state.
5040 *
5041 * Note that is of the upmost importance that the GPU is idle and
5042 * all stray writes are flushed *before* we dismantle the backing
5043 * storage for the pinned objects.
5044 *
5045 * However, since we are uncertain that resetting the GPU on older
5046 * machines is a good idea, we don't - just in case it leaves the
5047 * machine in an unusable condition.
5048 */
Michal Wajdeczkoc37d5722018-03-12 13:03:07 +00005049 intel_uc_sanitize(dev_priv);
Chris Wilson24145512017-01-24 11:01:35 +00005050 i915_gem_sanitize(dev_priv);
Chris Wilsoncad99462017-08-26 12:09:33 +01005051
5052 intel_runtime_pm_put(dev_priv);
5053 return 0;
Imre Deak1c777c52016-10-12 17:46:37 +03005054
Chris Wilsonc998e8a2017-03-02 08:30:29 +00005055err_unlock:
Chris Wilson45c5f202013-10-16 11:50:01 +01005056 mutex_unlock(&dev->struct_mutex);
Chris Wilsonc998e8a2017-03-02 08:30:29 +00005057 intel_runtime_pm_put(dev_priv);
Chris Wilson45c5f202013-10-16 11:50:01 +01005058 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07005059}
5060
Chris Wilson37cd3302017-11-12 11:27:38 +00005061void i915_gem_resume(struct drm_i915_private *i915)
Chris Wilson5ab57c72016-07-15 14:56:20 +01005062{
Chris Wilson37cd3302017-11-12 11:27:38 +00005063 WARN_ON(i915->gt.awake);
Chris Wilson5ab57c72016-07-15 14:56:20 +01005064
Chris Wilson37cd3302017-11-12 11:27:38 +00005065 mutex_lock(&i915->drm.struct_mutex);
5066 intel_uncore_forcewake_get(i915, FORCEWAKE_ALL);
Imre Deak31ab49a2016-11-07 11:20:05 +02005067
Chris Wilson37cd3302017-11-12 11:27:38 +00005068 i915_gem_restore_gtt_mappings(i915);
5069 i915_gem_restore_fences(i915);
Chris Wilson5ab57c72016-07-15 14:56:20 +01005070
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00005071 /*
5072 * As we didn't flush the kernel context before suspend, we cannot
Chris Wilson5ab57c72016-07-15 14:56:20 +01005073 * guarantee that the context image is complete. So let's just reset
5074 * it and start again.
5075 */
Chris Wilson37cd3302017-11-12 11:27:38 +00005076 i915->gt.resume(i915);
Chris Wilson5ab57c72016-07-15 14:56:20 +01005077
Chris Wilson37cd3302017-11-12 11:27:38 +00005078 if (i915_gem_init_hw(i915))
5079 goto err_wedged;
5080
Michal Wajdeczko7cfca4a2018-03-02 11:15:49 +00005081 intel_uc_resume(i915);
Chris Wilson7469c622017-11-14 13:03:00 +00005082
Chris Wilson37cd3302017-11-12 11:27:38 +00005083 /* Always reload a context for powersaving. */
5084 if (i915_gem_switch_to_kernel_context(i915))
5085 goto err_wedged;
5086
5087out_unlock:
5088 intel_uncore_forcewake_put(i915, FORCEWAKE_ALL);
5089 mutex_unlock(&i915->drm.struct_mutex);
5090 return;
5091
5092err_wedged:
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00005093 if (!i915_terminally_wedged(&i915->gpu_error)) {
5094 DRM_ERROR("failed to re-initialize GPU, declaring wedged!\n");
5095 i915_gem_set_wedged(i915);
5096 }
Chris Wilson37cd3302017-11-12 11:27:38 +00005097 goto out_unlock;
Chris Wilson5ab57c72016-07-15 14:56:20 +01005098}
5099
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00005100void i915_gem_init_swizzling(struct drm_i915_private *dev_priv)
Daniel Vetterf691e2f2012-02-02 09:58:12 +01005101{
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00005102 if (INTEL_GEN(dev_priv) < 5 ||
Daniel Vetterf691e2f2012-02-02 09:58:12 +01005103 dev_priv->mm.bit_6_swizzle_x == I915_BIT_6_SWIZZLE_NONE)
5104 return;
5105
5106 I915_WRITE(DISP_ARB_CTL, I915_READ(DISP_ARB_CTL) |
5107 DISP_TILE_SURFACE_SWIZZLING);
5108
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01005109 if (IS_GEN5(dev_priv))
Daniel Vetter11782b02012-01-31 16:47:55 +01005110 return;
5111
Daniel Vetterf691e2f2012-02-02 09:58:12 +01005112 I915_WRITE(TILECTL, I915_READ(TILECTL) | TILECTL_SWZCTL);
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01005113 if (IS_GEN6(dev_priv))
Daniel Vetter6b26c862012-04-24 14:04:12 +02005114 I915_WRITE(ARB_MODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_SNB));
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01005115 else if (IS_GEN7(dev_priv))
Daniel Vetter6b26c862012-04-24 14:04:12 +02005116 I915_WRITE(ARB_MODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_IVB));
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01005117 else if (IS_GEN8(dev_priv))
Ben Widawsky31a53362013-11-02 21:07:04 -07005118 I915_WRITE(GAMTARBMODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_BDW));
Ben Widawsky8782e262012-12-18 10:31:23 -08005119 else
5120 BUG();
Daniel Vetterf691e2f2012-02-02 09:58:12 +01005121}
Daniel Vettere21af882012-02-09 20:53:27 +01005122
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01005123static void init_unused_ring(struct drm_i915_private *dev_priv, u32 base)
Ville Syrjälä81e7f202014-08-15 01:21:55 +03005124{
Ville Syrjälä81e7f202014-08-15 01:21:55 +03005125 I915_WRITE(RING_CTL(base), 0);
5126 I915_WRITE(RING_HEAD(base), 0);
5127 I915_WRITE(RING_TAIL(base), 0);
5128 I915_WRITE(RING_START(base), 0);
5129}
5130
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01005131static void init_unused_rings(struct drm_i915_private *dev_priv)
Ville Syrjälä81e7f202014-08-15 01:21:55 +03005132{
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01005133 if (IS_I830(dev_priv)) {
5134 init_unused_ring(dev_priv, PRB1_BASE);
5135 init_unused_ring(dev_priv, SRB0_BASE);
5136 init_unused_ring(dev_priv, SRB1_BASE);
5137 init_unused_ring(dev_priv, SRB2_BASE);
5138 init_unused_ring(dev_priv, SRB3_BASE);
5139 } else if (IS_GEN2(dev_priv)) {
5140 init_unused_ring(dev_priv, SRB0_BASE);
5141 init_unused_ring(dev_priv, SRB1_BASE);
5142 } else if (IS_GEN3(dev_priv)) {
5143 init_unused_ring(dev_priv, PRB1_BASE);
5144 init_unused_ring(dev_priv, PRB2_BASE);
Ville Syrjälä81e7f202014-08-15 01:21:55 +03005145 }
5146}
5147
Chris Wilson20a8a742017-02-08 14:30:31 +00005148static int __i915_gem_restart_engines(void *data)
Ben Widawsky4fc7c972013-02-08 11:49:24 -08005149{
Chris Wilson20a8a742017-02-08 14:30:31 +00005150 struct drm_i915_private *i915 = data;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00005151 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05305152 enum intel_engine_id id;
Chris Wilson20a8a742017-02-08 14:30:31 +00005153 int err;
5154
5155 for_each_engine(engine, i915, id) {
5156 err = engine->init_hw(engine);
Chris Wilson8177e112018-02-07 11:15:45 +00005157 if (err) {
5158 DRM_ERROR("Failed to restart %s (%d)\n",
5159 engine->name, err);
Chris Wilson20a8a742017-02-08 14:30:31 +00005160 return err;
Chris Wilson8177e112018-02-07 11:15:45 +00005161 }
Chris Wilson20a8a742017-02-08 14:30:31 +00005162 }
5163
5164 return 0;
5165}
5166
5167int i915_gem_init_hw(struct drm_i915_private *dev_priv)
5168{
Chris Wilsond200cda2016-04-28 09:56:44 +01005169 int ret;
Ben Widawsky4fc7c972013-02-08 11:49:24 -08005170
Chris Wilsonde867c22016-10-25 13:16:02 +01005171 dev_priv->gt.last_init_time = ktime_get();
5172
Chris Wilson5e4f5182015-02-13 14:35:59 +00005173 /* Double layer security blanket, see i915_gem_init() */
5174 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
5175
Tvrtko Ursulin0031fb92016-11-04 14:42:44 +00005176 if (HAS_EDRAM(dev_priv) && INTEL_GEN(dev_priv) < 9)
Ben Widawsky05e21cc2013-07-04 11:02:04 -07005177 I915_WRITE(HSW_IDICR, I915_READ(HSW_IDICR) | IDIHASHMSK(0xf));
Ben Widawsky4fc7c972013-02-08 11:49:24 -08005178
Tvrtko Ursulin772c2a52016-10-13 11:03:01 +01005179 if (IS_HASWELL(dev_priv))
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01005180 I915_WRITE(MI_PREDICATE_RESULT_2, IS_HSW_GT3(dev_priv) ?
Ville Syrjälä0bf21342013-11-29 14:56:12 +02005181 LOWER_SLICE_ENABLED : LOWER_SLICE_DISABLED);
Rodrigo Vivi94353732013-08-28 16:45:46 -03005182
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01005183 if (HAS_PCH_NOP(dev_priv)) {
Tvrtko Ursulinfd6b8f42016-10-14 10:13:06 +01005184 if (IS_IVYBRIDGE(dev_priv)) {
Daniel Vetter6ba844b2014-01-22 23:39:30 +01005185 u32 temp = I915_READ(GEN7_MSG_CTL);
5186 temp &= ~(WAIT_FOR_PCH_FLR_ACK | WAIT_FOR_PCH_RESET_ACK);
5187 I915_WRITE(GEN7_MSG_CTL, temp);
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00005188 } else if (INTEL_GEN(dev_priv) >= 7) {
Daniel Vetter6ba844b2014-01-22 23:39:30 +01005189 u32 temp = I915_READ(HSW_NDE_RSTWRN_OPT);
5190 temp &= ~RESET_PCH_HANDSHAKE_ENABLE;
5191 I915_WRITE(HSW_NDE_RSTWRN_OPT, temp);
5192 }
Ben Widawsky88a2b2a2013-04-05 13:12:43 -07005193 }
5194
Oscar Mateo59b449d2018-04-10 09:12:47 -07005195 intel_gt_workarounds_apply(dev_priv);
5196
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00005197 i915_gem_init_swizzling(dev_priv);
Ben Widawsky4fc7c972013-02-08 11:49:24 -08005198
Daniel Vetterd5abdfd2014-11-20 09:45:19 +01005199 /*
5200 * At least 830 can leave some of the unused rings
5201 * "active" (ie. head != tail) after resume which
5202 * will prevent c3 entry. Makes sure all unused rings
5203 * are totally idle.
5204 */
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01005205 init_unused_rings(dev_priv);
Daniel Vetterd5abdfd2014-11-20 09:45:19 +01005206
Dave Gordoned54c1a2016-01-19 19:02:54 +00005207 BUG_ON(!dev_priv->kernel_context);
Chris Wilson6f74b362017-10-15 15:37:25 +01005208 if (i915_terminally_wedged(&dev_priv->gpu_error)) {
5209 ret = -EIO;
5210 goto out;
5211 }
John Harrison90638cc2015-05-29 17:43:37 +01005212
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00005213 ret = i915_ppgtt_init_hw(dev_priv);
John Harrison4ad2fd82015-06-18 13:11:20 +01005214 if (ret) {
Chris Wilson8177e112018-02-07 11:15:45 +00005215 DRM_ERROR("Enabling PPGTT failed (%d)\n", ret);
John Harrison4ad2fd82015-06-18 13:11:20 +01005216 goto out;
5217 }
5218
Jackie Lif08e2032018-03-13 17:32:53 -07005219 ret = intel_wopcm_init_hw(&dev_priv->wopcm);
5220 if (ret) {
5221 DRM_ERROR("Enabling WOPCM failed (%d)\n", ret);
5222 goto out;
5223 }
5224
Michał Winiarski9bdc3572017-10-25 18:25:19 +01005225 /* We can't enable contexts until all firmware is loaded */
5226 ret = intel_uc_init_hw(dev_priv);
Chris Wilson8177e112018-02-07 11:15:45 +00005227 if (ret) {
5228 DRM_ERROR("Enabling uc failed (%d)\n", ret);
Michał Winiarski9bdc3572017-10-25 18:25:19 +01005229 goto out;
Chris Wilson8177e112018-02-07 11:15:45 +00005230 }
Michał Winiarski9bdc3572017-10-25 18:25:19 +01005231
Tvrtko Ursulinbf9e8422016-12-01 14:16:38 +00005232 intel_mocs_init_l3cc_table(dev_priv);
Peter Antoine0ccdacf2016-04-13 15:03:25 +01005233
Chris Wilson136109c2017-11-02 13:14:30 +00005234 /* Only when the HW is re-initialised, can we replay the requests */
5235 ret = __i915_gem_restart_engines(dev_priv);
Chris Wilson5e4f5182015-02-13 14:35:59 +00005236out:
5237 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Ben Widawsky2fa48d82013-12-06 14:11:04 -08005238 return ret;
Zou Nan hai8187a2b2010-05-21 09:08:55 +08005239}
5240
Chris Wilsond2b4b972017-11-10 14:26:33 +00005241static int __intel_engines_record_defaults(struct drm_i915_private *i915)
5242{
5243 struct i915_gem_context *ctx;
5244 struct intel_engine_cs *engine;
5245 enum intel_engine_id id;
5246 int err;
5247
5248 /*
5249 * As we reset the gpu during very early sanitisation, the current
5250 * register state on the GPU should reflect its defaults values.
5251 * We load a context onto the hw (with restore-inhibit), then switch
5252 * over to a second context to save that default register state. We
5253 * can then prime every new context with that state so they all start
5254 * from the same default HW values.
5255 */
5256
5257 ctx = i915_gem_context_create_kernel(i915, 0);
5258 if (IS_ERR(ctx))
5259 return PTR_ERR(ctx);
5260
5261 for_each_engine(engine, i915, id) {
Chris Wilsone61e0f52018-02-21 09:56:36 +00005262 struct i915_request *rq;
Chris Wilsond2b4b972017-11-10 14:26:33 +00005263
Chris Wilsone61e0f52018-02-21 09:56:36 +00005264 rq = i915_request_alloc(engine, ctx);
Chris Wilsond2b4b972017-11-10 14:26:33 +00005265 if (IS_ERR(rq)) {
5266 err = PTR_ERR(rq);
5267 goto out_ctx;
5268 }
5269
Chris Wilson3fef5cd2017-11-20 10:20:02 +00005270 err = 0;
Chris Wilsond2b4b972017-11-10 14:26:33 +00005271 if (engine->init_context)
5272 err = engine->init_context(rq);
5273
Chris Wilsone61e0f52018-02-21 09:56:36 +00005274 __i915_request_add(rq, true);
Chris Wilsond2b4b972017-11-10 14:26:33 +00005275 if (err)
5276 goto err_active;
5277 }
5278
5279 err = i915_gem_switch_to_kernel_context(i915);
5280 if (err)
5281 goto err_active;
5282
5283 err = i915_gem_wait_for_idle(i915, I915_WAIT_LOCKED);
5284 if (err)
5285 goto err_active;
5286
5287 assert_kernel_context_is_current(i915);
5288
5289 for_each_engine(engine, i915, id) {
5290 struct i915_vma *state;
5291
Chris Wilsonab82a062018-04-30 14:15:01 +01005292 state = to_intel_context(ctx, engine)->state;
Chris Wilsond2b4b972017-11-10 14:26:33 +00005293 if (!state)
5294 continue;
5295
5296 /*
5297 * As we will hold a reference to the logical state, it will
5298 * not be torn down with the context, and importantly the
5299 * object will hold onto its vma (making it possible for a
5300 * stray GTT write to corrupt our defaults). Unmap the vma
5301 * from the GTT to prevent such accidents and reclaim the
5302 * space.
5303 */
5304 err = i915_vma_unbind(state);
5305 if (err)
5306 goto err_active;
5307
5308 err = i915_gem_object_set_to_cpu_domain(state->obj, false);
5309 if (err)
5310 goto err_active;
5311
5312 engine->default_state = i915_gem_object_get(state->obj);
5313 }
5314
5315 if (IS_ENABLED(CONFIG_DRM_I915_DEBUG_GEM)) {
5316 unsigned int found = intel_engines_has_context_isolation(i915);
5317
5318 /*
5319 * Make sure that classes with multiple engine instances all
5320 * share the same basic configuration.
5321 */
5322 for_each_engine(engine, i915, id) {
5323 unsigned int bit = BIT(engine->uabi_class);
5324 unsigned int expected = engine->default_state ? bit : 0;
5325
5326 if ((found & bit) != expected) {
5327 DRM_ERROR("mismatching default context state for class %d on engine %s\n",
5328 engine->uabi_class, engine->name);
5329 }
5330 }
5331 }
5332
5333out_ctx:
5334 i915_gem_context_set_closed(ctx);
5335 i915_gem_context_put(ctx);
5336 return err;
5337
5338err_active:
5339 /*
5340 * If we have to abandon now, we expect the engines to be idle
5341 * and ready to be torn-down. First try to flush any remaining
5342 * request, ensure we are pointing at the kernel context and
5343 * then remove it.
5344 */
5345 if (WARN_ON(i915_gem_switch_to_kernel_context(i915)))
5346 goto out_ctx;
5347
5348 if (WARN_ON(i915_gem_wait_for_idle(i915, I915_WAIT_LOCKED)))
5349 goto out_ctx;
5350
5351 i915_gem_contexts_lost(i915);
5352 goto out_ctx;
5353}
5354
Tvrtko Ursulinbf9e8422016-12-01 14:16:38 +00005355int i915_gem_init(struct drm_i915_private *dev_priv)
Chris Wilson1070a422012-04-24 15:47:41 +01005356{
Chris Wilson1070a422012-04-24 15:47:41 +01005357 int ret;
5358
Matthew Auldda9fe3f32017-10-06 23:18:31 +01005359 /*
5360 * We need to fallback to 4K pages since gvt gtt handling doesn't
5361 * support huge page entries - we will need to check either hypervisor
5362 * mm can support huge guest page or just do emulation in gvt.
5363 */
5364 if (intel_vgpu_active(dev_priv))
5365 mkwrite_device_info(dev_priv)->page_sizes =
5366 I915_GTT_PAGE_SIZE_4K;
5367
Chris Wilson94312822017-05-03 10:39:18 +01005368 dev_priv->mm.unordered_timeline = dma_fence_context_alloc(1);
Chris Wilson57822dc2017-02-22 11:40:48 +00005369
Chris Wilsonfb5c5512017-11-20 20:55:00 +00005370 if (HAS_LOGICAL_RING_CONTEXTS(dev_priv)) {
Chris Wilson821ed7d2016-09-09 14:11:53 +01005371 dev_priv->gt.resume = intel_lr_context_resume;
Tvrtko Ursulin117897f2016-03-16 11:00:40 +00005372 dev_priv->gt.cleanup_engine = intel_logical_ring_cleanup;
Chris Wilsonfb5c5512017-11-20 20:55:00 +00005373 } else {
5374 dev_priv->gt.resume = intel_legacy_submission_resume;
5375 dev_priv->gt.cleanup_engine = intel_engine_cleanup;
Oscar Mateoa83014d2014-07-24 17:04:21 +01005376 }
5377
Chris Wilsonee487002017-11-22 17:26:21 +00005378 ret = i915_gem_init_userptr(dev_priv);
5379 if (ret)
5380 return ret;
5381
Jackie Li6b0478f2018-03-13 17:32:50 -07005382 ret = intel_wopcm_init(&dev_priv->wopcm);
5383 if (ret)
5384 return ret;
5385
Sagar Arun Kamble70deead2018-01-24 21:16:58 +05305386 ret = intel_uc_init_misc(dev_priv);
Michał Winiarski3176ff42017-12-13 23:13:47 +01005387 if (ret)
5388 return ret;
5389
Chris Wilson5e4f5182015-02-13 14:35:59 +00005390 /* This is just a security blanket to placate dragons.
5391 * On some systems, we very sporadically observe that the first TLBs
5392 * used by the CS may be stale, despite us poking the TLB reset. If
5393 * we hold the forcewake during initialisation these problems
5394 * just magically go away.
5395 */
Chris Wilsonee487002017-11-22 17:26:21 +00005396 mutex_lock(&dev_priv->drm.struct_mutex);
Chris Wilson5e4f5182015-02-13 14:35:59 +00005397 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
5398
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01005399 ret = i915_gem_init_ggtt(dev_priv);
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00005400 if (ret) {
5401 GEM_BUG_ON(ret == -EIO);
5402 goto err_unlock;
5403 }
Jesse Barnesd62b4892013-03-08 10:45:53 -08005404
Chris Wilson829a0af2017-06-20 12:05:45 +01005405 ret = i915_gem_contexts_init(dev_priv);
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00005406 if (ret) {
5407 GEM_BUG_ON(ret == -EIO);
5408 goto err_ggtt;
5409 }
Ben Widawsky2fa48d82013-12-06 14:11:04 -08005410
Tvrtko Ursulinbf9e8422016-12-01 14:16:38 +00005411 ret = intel_engines_init(dev_priv);
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00005412 if (ret) {
5413 GEM_BUG_ON(ret == -EIO);
5414 goto err_context;
5415 }
Daniel Vetter53ca26c2012-04-26 23:28:03 +02005416
Chris Wilsonf58d13d2017-11-10 14:26:29 +00005417 intel_init_gt_powersave(dev_priv);
5418
Michał Winiarski61b5c152017-12-13 23:13:48 +01005419 ret = intel_uc_init(dev_priv);
Chris Wilsoncc6a8182017-11-10 14:26:30 +00005420 if (ret)
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00005421 goto err_pm;
Chris Wilsoncc6a8182017-11-10 14:26:30 +00005422
Michał Winiarski61b5c152017-12-13 23:13:48 +01005423 ret = i915_gem_init_hw(dev_priv);
5424 if (ret)
5425 goto err_uc_init;
5426
Chris Wilsoncc6a8182017-11-10 14:26:30 +00005427 /*
5428 * Despite its name intel_init_clock_gating applies both display
5429 * clock gating workarounds; GT mmio workarounds and the occasional
5430 * GT power context workaround. Worse, sometimes it includes a context
5431 * register workaround which we need to apply before we record the
5432 * default HW state for all contexts.
5433 *
5434 * FIXME: break up the workarounds and apply them at the right time!
5435 */
5436 intel_init_clock_gating(dev_priv);
5437
Chris Wilsond2b4b972017-11-10 14:26:33 +00005438 ret = __intel_engines_record_defaults(dev_priv);
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00005439 if (ret)
5440 goto err_init_hw;
5441
5442 if (i915_inject_load_failure()) {
5443 ret = -ENODEV;
5444 goto err_init_hw;
5445 }
5446
5447 if (i915_inject_load_failure()) {
5448 ret = -EIO;
5449 goto err_init_hw;
5450 }
5451
5452 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
5453 mutex_unlock(&dev_priv->drm.struct_mutex);
5454
5455 return 0;
5456
5457 /*
5458 * Unwinding is complicated by that we want to handle -EIO to mean
5459 * disable GPU submission but keep KMS alive. We want to mark the
5460 * HW as irrevisibly wedged, but keep enough state around that the
5461 * driver doesn't explode during runtime.
5462 */
5463err_init_hw:
5464 i915_gem_wait_for_idle(dev_priv, I915_WAIT_LOCKED);
5465 i915_gem_contexts_lost(dev_priv);
5466 intel_uc_fini_hw(dev_priv);
Michał Winiarski61b5c152017-12-13 23:13:48 +01005467err_uc_init:
5468 intel_uc_fini(dev_priv);
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00005469err_pm:
5470 if (ret != -EIO) {
5471 intel_cleanup_gt_powersave(dev_priv);
5472 i915_gem_cleanup_engines(dev_priv);
5473 }
5474err_context:
5475 if (ret != -EIO)
5476 i915_gem_contexts_fini(dev_priv);
5477err_ggtt:
5478err_unlock:
5479 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
5480 mutex_unlock(&dev_priv->drm.struct_mutex);
5481
Sagar Arun Kamble70deead2018-01-24 21:16:58 +05305482 intel_uc_fini_misc(dev_priv);
Sagar Arun Kambleda943b52018-01-10 18:24:16 +05305483
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00005484 if (ret != -EIO)
5485 i915_gem_cleanup_userptr(dev_priv);
5486
Chris Wilson60990322014-04-09 09:19:42 +01005487 if (ret == -EIO) {
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00005488 /*
5489 * Allow engine initialisation to fail by marking the GPU as
Chris Wilson60990322014-04-09 09:19:42 +01005490 * wedged. But we only want to do this where the GPU is angry,
5491 * for all other failure, such as an allocation failure, bail.
5492 */
Chris Wilson6f74b362017-10-15 15:37:25 +01005493 if (!i915_terminally_wedged(&dev_priv->gpu_error)) {
5494 DRM_ERROR("Failed to initialize GPU, declaring it wedged\n");
5495 i915_gem_set_wedged(dev_priv);
5496 }
Chris Wilson60990322014-04-09 09:19:42 +01005497 ret = 0;
Chris Wilson1070a422012-04-24 15:47:41 +01005498 }
5499
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00005500 i915_gem_drain_freed_objects(dev_priv);
Chris Wilson60990322014-04-09 09:19:42 +01005501 return ret;
Chris Wilson1070a422012-04-24 15:47:41 +01005502}
5503
Chris Wilson24145512017-01-24 11:01:35 +00005504void i915_gem_init_mmio(struct drm_i915_private *i915)
5505{
5506 i915_gem_sanitize(i915);
5507}
5508
Zou Nan hai8187a2b2010-05-21 09:08:55 +08005509void
Tvrtko Ursulincb15d9f2016-12-01 14:16:39 +00005510i915_gem_cleanup_engines(struct drm_i915_private *dev_priv)
Zou Nan hai8187a2b2010-05-21 09:08:55 +08005511{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00005512 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05305513 enum intel_engine_id id;
Zou Nan hai8187a2b2010-05-21 09:08:55 +08005514
Akash Goel3b3f1652016-10-13 22:44:48 +05305515 for_each_engine(engine, dev_priv, id)
Tvrtko Ursulin117897f2016-03-16 11:00:40 +00005516 dev_priv->gt.cleanup_engine(engine);
Zou Nan hai8187a2b2010-05-21 09:08:55 +08005517}
5518
Eric Anholt673a3942008-07-30 12:06:12 -07005519void
Imre Deak40ae4e12016-03-16 14:54:03 +02005520i915_gem_load_init_fences(struct drm_i915_private *dev_priv)
5521{
Chris Wilson49ef5292016-08-18 17:17:00 +01005522 int i;
Imre Deak40ae4e12016-03-16 14:54:03 +02005523
Tvrtko Ursulinc56b89f2018-02-09 21:58:46 +00005524 if (INTEL_GEN(dev_priv) >= 7 && !IS_VALLEYVIEW(dev_priv) &&
Imre Deak40ae4e12016-03-16 14:54:03 +02005525 !IS_CHERRYVIEW(dev_priv))
5526 dev_priv->num_fence_regs = 32;
Tvrtko Ursulinc56b89f2018-02-09 21:58:46 +00005527 else if (INTEL_GEN(dev_priv) >= 4 ||
Jani Nikula73f67aa2016-12-07 22:48:09 +02005528 IS_I945G(dev_priv) || IS_I945GM(dev_priv) ||
5529 IS_G33(dev_priv) || IS_PINEVIEW(dev_priv))
Imre Deak40ae4e12016-03-16 14:54:03 +02005530 dev_priv->num_fence_regs = 16;
5531 else
5532 dev_priv->num_fence_regs = 8;
5533
Chris Wilsonc0336662016-05-06 15:40:21 +01005534 if (intel_vgpu_active(dev_priv))
Imre Deak40ae4e12016-03-16 14:54:03 +02005535 dev_priv->num_fence_regs =
5536 I915_READ(vgtif_reg(avail_rs.fence_num));
5537
5538 /* Initialize fence registers to zero */
Chris Wilson49ef5292016-08-18 17:17:00 +01005539 for (i = 0; i < dev_priv->num_fence_regs; i++) {
5540 struct drm_i915_fence_reg *fence = &dev_priv->fence_regs[i];
5541
5542 fence->i915 = dev_priv;
5543 fence->id = i;
5544 list_add_tail(&fence->link, &dev_priv->mm.fence_list);
5545 }
Tvrtko Ursulin4362f4f2016-11-16 08:55:33 +00005546 i915_gem_restore_fences(dev_priv);
Imre Deak40ae4e12016-03-16 14:54:03 +02005547
Tvrtko Ursulin4362f4f2016-11-16 08:55:33 +00005548 i915_gem_detect_bit_6_swizzle(dev_priv);
Imre Deak40ae4e12016-03-16 14:54:03 +02005549}
5550
Chris Wilson9c52d1c2017-11-10 23:24:47 +00005551static void i915_gem_init__mm(struct drm_i915_private *i915)
5552{
5553 spin_lock_init(&i915->mm.object_stat_lock);
5554 spin_lock_init(&i915->mm.obj_lock);
5555 spin_lock_init(&i915->mm.free_lock);
5556
5557 init_llist_head(&i915->mm.free_list);
5558
5559 INIT_LIST_HEAD(&i915->mm.unbound_list);
5560 INIT_LIST_HEAD(&i915->mm.bound_list);
5561 INIT_LIST_HEAD(&i915->mm.fence_list);
5562 INIT_LIST_HEAD(&i915->mm.userfault_list);
5563
5564 INIT_WORK(&i915->mm.free_work, __i915_gem_free_work);
5565}
5566
Michal Wajdeczkoa0de9082018-03-23 12:34:49 +00005567int i915_gem_init_early(struct drm_i915_private *dev_priv)
Eric Anholt673a3942008-07-30 12:06:12 -07005568{
Tvrtko Ursulina9335682016-11-02 15:14:59 +00005569 int err = -ENOMEM;
Chris Wilson42dcedd2012-11-15 11:32:30 +00005570
Tvrtko Ursulina9335682016-11-02 15:14:59 +00005571 dev_priv->objects = KMEM_CACHE(drm_i915_gem_object, SLAB_HWCACHE_ALIGN);
5572 if (!dev_priv->objects)
Chris Wilson73cb9702016-10-28 13:58:46 +01005573 goto err_out;
Chris Wilson73cb9702016-10-28 13:58:46 +01005574
Tvrtko Ursulina9335682016-11-02 15:14:59 +00005575 dev_priv->vmas = KMEM_CACHE(i915_vma, SLAB_HWCACHE_ALIGN);
5576 if (!dev_priv->vmas)
Chris Wilson73cb9702016-10-28 13:58:46 +01005577 goto err_objects;
Chris Wilson73cb9702016-10-28 13:58:46 +01005578
Chris Wilsond1b48c12017-08-16 09:52:08 +01005579 dev_priv->luts = KMEM_CACHE(i915_lut_handle, 0);
5580 if (!dev_priv->luts)
5581 goto err_vmas;
5582
Chris Wilsone61e0f52018-02-21 09:56:36 +00005583 dev_priv->requests = KMEM_CACHE(i915_request,
Tvrtko Ursulina9335682016-11-02 15:14:59 +00005584 SLAB_HWCACHE_ALIGN |
5585 SLAB_RECLAIM_ACCOUNT |
Paul E. McKenney5f0d5a32017-01-18 02:53:44 -08005586 SLAB_TYPESAFE_BY_RCU);
Tvrtko Ursulina9335682016-11-02 15:14:59 +00005587 if (!dev_priv->requests)
Chris Wilsond1b48c12017-08-16 09:52:08 +01005588 goto err_luts;
Chris Wilson73cb9702016-10-28 13:58:46 +01005589
Chris Wilson52e54202016-11-14 20:41:02 +00005590 dev_priv->dependencies = KMEM_CACHE(i915_dependency,
5591 SLAB_HWCACHE_ALIGN |
5592 SLAB_RECLAIM_ACCOUNT);
5593 if (!dev_priv->dependencies)
5594 goto err_requests;
5595
Chris Wilsonc5cf9a92017-05-17 13:10:04 +01005596 dev_priv->priorities = KMEM_CACHE(i915_priolist, SLAB_HWCACHE_ALIGN);
5597 if (!dev_priv->priorities)
5598 goto err_dependencies;
5599
Chris Wilson73cb9702016-10-28 13:58:46 +01005600 INIT_LIST_HEAD(&dev_priv->gt.timelines);
Chris Wilson643b4502018-04-30 14:15:03 +01005601 INIT_LIST_HEAD(&dev_priv->gt.active_rings);
Chris Wilson3365e222018-05-03 20:51:14 +01005602 INIT_LIST_HEAD(&dev_priv->gt.closed_vma);
Chris Wilson643b4502018-04-30 14:15:03 +01005603
Chris Wilson9c52d1c2017-11-10 23:24:47 +00005604 i915_gem_init__mm(dev_priv);
Chris Wilsonf2123812017-10-16 12:40:37 +01005605
Chris Wilson67d97da2016-07-04 08:08:31 +01005606 INIT_DELAYED_WORK(&dev_priv->gt.retire_work,
Eric Anholt673a3942008-07-30 12:06:12 -07005607 i915_gem_retire_work_handler);
Chris Wilson67d97da2016-07-04 08:08:31 +01005608 INIT_DELAYED_WORK(&dev_priv->gt.idle_work,
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005609 i915_gem_idle_work_handler);
Chris Wilson1f15b762016-07-01 17:23:14 +01005610 init_waitqueue_head(&dev_priv->gpu_error.wait_queue);
Daniel Vetter1f83fee2012-11-15 17:17:22 +01005611 init_waitqueue_head(&dev_priv->gpu_error.reset_queue);
Chris Wilson31169712009-09-14 16:50:28 +01005612
Joonas Lahtinen6f633402016-09-01 14:58:21 +03005613 atomic_set(&dev_priv->mm.bsd_engine_dispatch_index, 0);
5614
Chris Wilsonb5add952016-08-04 16:32:36 +01005615 spin_lock_init(&dev_priv->fb_tracking.lock);
Chris Wilson73cb9702016-10-28 13:58:46 +01005616
Matthew Auld465c4032017-10-06 23:18:14 +01005617 err = i915_gemfs_init(dev_priv);
5618 if (err)
5619 DRM_NOTE("Unable to create a private tmpfs mount, hugepage support will be disabled(%d).\n", err);
5620
Chris Wilson73cb9702016-10-28 13:58:46 +01005621 return 0;
5622
Chris Wilson52e54202016-11-14 20:41:02 +00005623err_dependencies:
5624 kmem_cache_destroy(dev_priv->dependencies);
Chris Wilson73cb9702016-10-28 13:58:46 +01005625err_requests:
5626 kmem_cache_destroy(dev_priv->requests);
Chris Wilsond1b48c12017-08-16 09:52:08 +01005627err_luts:
5628 kmem_cache_destroy(dev_priv->luts);
Chris Wilson73cb9702016-10-28 13:58:46 +01005629err_vmas:
5630 kmem_cache_destroy(dev_priv->vmas);
5631err_objects:
5632 kmem_cache_destroy(dev_priv->objects);
5633err_out:
5634 return err;
Eric Anholt673a3942008-07-30 12:06:12 -07005635}
Dave Airlie71acb5e2008-12-30 20:31:46 +10005636
Michal Wajdeczkoa0de9082018-03-23 12:34:49 +00005637void i915_gem_cleanup_early(struct drm_i915_private *dev_priv)
Imre Deakd64aa092016-01-19 15:26:29 +02005638{
Chris Wilsonc4d4c1c2017-02-10 16:35:23 +00005639 i915_gem_drain_freed_objects(dev_priv);
Chris Wilsonc9c70472018-02-19 22:06:31 +00005640 GEM_BUG_ON(!llist_empty(&dev_priv->mm.free_list));
5641 GEM_BUG_ON(atomic_read(&dev_priv->mm.free_count));
Chris Wilsonc4d4c1c2017-02-10 16:35:23 +00005642 WARN_ON(dev_priv->mm.object_count);
Matthew Auldea84aa72016-11-17 21:04:11 +00005643 WARN_ON(!list_empty(&dev_priv->gt.timelines));
Matthew Auldea84aa72016-11-17 21:04:11 +00005644
Chris Wilsonc5cf9a92017-05-17 13:10:04 +01005645 kmem_cache_destroy(dev_priv->priorities);
Chris Wilson52e54202016-11-14 20:41:02 +00005646 kmem_cache_destroy(dev_priv->dependencies);
Imre Deakd64aa092016-01-19 15:26:29 +02005647 kmem_cache_destroy(dev_priv->requests);
Chris Wilsond1b48c12017-08-16 09:52:08 +01005648 kmem_cache_destroy(dev_priv->luts);
Imre Deakd64aa092016-01-19 15:26:29 +02005649 kmem_cache_destroy(dev_priv->vmas);
5650 kmem_cache_destroy(dev_priv->objects);
Chris Wilson0eafec62016-08-04 16:32:41 +01005651
5652 /* And ensure that our DESTROY_BY_RCU slabs are truly destroyed */
5653 rcu_barrier();
Matthew Auld465c4032017-10-06 23:18:14 +01005654
5655 i915_gemfs_fini(dev_priv);
Imre Deakd64aa092016-01-19 15:26:29 +02005656}
5657
Chris Wilson6a800ea2016-09-21 14:51:07 +01005658int i915_gem_freeze(struct drm_i915_private *dev_priv)
5659{
Chris Wilsond0aa3012017-04-07 11:25:49 +01005660 /* Discard all purgeable objects, let userspace recover those as
5661 * required after resuming.
5662 */
Chris Wilson6a800ea2016-09-21 14:51:07 +01005663 i915_gem_shrink_all(dev_priv);
Chris Wilson6a800ea2016-09-21 14:51:07 +01005664
Chris Wilson6a800ea2016-09-21 14:51:07 +01005665 return 0;
5666}
5667
Chris Wilson461fb992016-05-14 07:26:33 +01005668int i915_gem_freeze_late(struct drm_i915_private *dev_priv)
5669{
5670 struct drm_i915_gem_object *obj;
Chris Wilson7aab2d52016-09-09 20:02:18 +01005671 struct list_head *phases[] = {
5672 &dev_priv->mm.unbound_list,
5673 &dev_priv->mm.bound_list,
5674 NULL
5675 }, **p;
Chris Wilson461fb992016-05-14 07:26:33 +01005676
5677 /* Called just before we write the hibernation image.
5678 *
5679 * We need to update the domain tracking to reflect that the CPU
5680 * will be accessing all the pages to create and restore from the
5681 * hibernation, and so upon restoration those pages will be in the
5682 * CPU domain.
5683 *
5684 * To make sure the hibernation image contains the latest state,
5685 * we update that state just before writing out the image.
Chris Wilson7aab2d52016-09-09 20:02:18 +01005686 *
5687 * To try and reduce the hibernation image, we manually shrink
Chris Wilsond0aa3012017-04-07 11:25:49 +01005688 * the objects as well, see i915_gem_freeze()
Chris Wilson461fb992016-05-14 07:26:33 +01005689 */
5690
Chris Wilson912d5722017-09-06 16:19:30 -07005691 i915_gem_shrink(dev_priv, -1UL, NULL, I915_SHRINK_UNBOUND);
Chris Wilson17b93c42017-04-07 11:25:50 +01005692 i915_gem_drain_freed_objects(dev_priv);
Chris Wilson461fb992016-05-14 07:26:33 +01005693
Chris Wilsonf2123812017-10-16 12:40:37 +01005694 spin_lock(&dev_priv->mm.obj_lock);
Chris Wilson7aab2d52016-09-09 20:02:18 +01005695 for (p = phases; *p; p++) {
Chris Wilsonf2123812017-10-16 12:40:37 +01005696 list_for_each_entry(obj, *p, mm.link)
Chris Wilsone27ab732017-06-15 13:38:49 +01005697 __start_cpu_write(obj);
Chris Wilson461fb992016-05-14 07:26:33 +01005698 }
Chris Wilsonf2123812017-10-16 12:40:37 +01005699 spin_unlock(&dev_priv->mm.obj_lock);
Chris Wilson461fb992016-05-14 07:26:33 +01005700
5701 return 0;
5702}
5703
Chris Wilsonf787a5f2010-09-24 16:02:42 +01005704void i915_gem_release(struct drm_device *dev, struct drm_file *file)
Eric Anholtb9624422009-06-03 07:27:35 +00005705{
Chris Wilsonf787a5f2010-09-24 16:02:42 +01005706 struct drm_i915_file_private *file_priv = file->driver_priv;
Chris Wilsone61e0f52018-02-21 09:56:36 +00005707 struct i915_request *request;
Eric Anholtb9624422009-06-03 07:27:35 +00005708
5709 /* Clean up our request list when the client is going away, so that
5710 * later retire_requests won't dereference our soon-to-be-gone
5711 * file_priv.
5712 */
Chris Wilson1c255952010-09-26 11:03:27 +01005713 spin_lock(&file_priv->mm.lock);
Chris Wilsonc8659ef2017-03-02 12:25:25 +00005714 list_for_each_entry(request, &file_priv->mm.request_list, client_link)
Chris Wilsonf787a5f2010-09-24 16:02:42 +01005715 request->file_priv = NULL;
Chris Wilson1c255952010-09-26 11:03:27 +01005716 spin_unlock(&file_priv->mm.lock);
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005717}
5718
Chris Wilson829a0af2017-06-20 12:05:45 +01005719int i915_gem_open(struct drm_i915_private *i915, struct drm_file *file)
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005720{
5721 struct drm_i915_file_private *file_priv;
Ben Widawskye422b882013-12-06 14:10:58 -08005722 int ret;
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005723
Chris Wilsonc4c29d72016-11-09 10:45:07 +00005724 DRM_DEBUG("\n");
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005725
5726 file_priv = kzalloc(sizeof(*file_priv), GFP_KERNEL);
5727 if (!file_priv)
5728 return -ENOMEM;
5729
5730 file->driver_priv = file_priv;
Chris Wilson829a0af2017-06-20 12:05:45 +01005731 file_priv->dev_priv = i915;
Chris Wilsonab0e7ff2014-02-25 17:11:24 +02005732 file_priv->file = file;
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005733
5734 spin_lock_init(&file_priv->mm.lock);
5735 INIT_LIST_HEAD(&file_priv->mm.request_list);
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005736
Chris Wilsonc80ff162016-07-27 09:07:27 +01005737 file_priv->bsd_engine = -1;
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00005738
Chris Wilson829a0af2017-06-20 12:05:45 +01005739 ret = i915_gem_context_open(i915, file);
Ben Widawskye422b882013-12-06 14:10:58 -08005740 if (ret)
5741 kfree(file_priv);
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005742
Ben Widawskye422b882013-12-06 14:10:58 -08005743 return ret;
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005744}
5745
Daniel Vetterb680c372014-09-19 18:27:27 +02005746/**
5747 * i915_gem_track_fb - update frontbuffer tracking
Geliang Tangd9072a32015-09-15 05:58:44 -07005748 * @old: current GEM buffer for the frontbuffer slots
5749 * @new: new GEM buffer for the frontbuffer slots
5750 * @frontbuffer_bits: bitmask of frontbuffer slots
Daniel Vetterb680c372014-09-19 18:27:27 +02005751 *
5752 * This updates the frontbuffer tracking bits @frontbuffer_bits by clearing them
5753 * from @old and setting them in @new. Both @old and @new can be NULL.
5754 */
Daniel Vettera071fa02014-06-18 23:28:09 +02005755void i915_gem_track_fb(struct drm_i915_gem_object *old,
5756 struct drm_i915_gem_object *new,
5757 unsigned frontbuffer_bits)
5758{
Chris Wilsonfaf5bf02016-08-04 16:32:37 +01005759 /* Control of individual bits within the mask are guarded by
5760 * the owning plane->mutex, i.e. we can never see concurrent
5761 * manipulation of individual bits. But since the bitfield as a whole
5762 * is updated using RMW, we need to use atomics in order to update
5763 * the bits.
5764 */
5765 BUILD_BUG_ON(INTEL_FRONTBUFFER_BITS_PER_PIPE * I915_MAX_PIPES >
5766 sizeof(atomic_t) * BITS_PER_BYTE);
5767
Daniel Vettera071fa02014-06-18 23:28:09 +02005768 if (old) {
Chris Wilsonfaf5bf02016-08-04 16:32:37 +01005769 WARN_ON(!(atomic_read(&old->frontbuffer_bits) & frontbuffer_bits));
5770 atomic_andnot(frontbuffer_bits, &old->frontbuffer_bits);
Daniel Vettera071fa02014-06-18 23:28:09 +02005771 }
5772
5773 if (new) {
Chris Wilsonfaf5bf02016-08-04 16:32:37 +01005774 WARN_ON(atomic_read(&new->frontbuffer_bits) & frontbuffer_bits);
5775 atomic_or(frontbuffer_bits, &new->frontbuffer_bits);
Daniel Vettera071fa02014-06-18 23:28:09 +02005776 }
5777}
5778
Dave Gordonea702992015-07-09 19:29:02 +01005779/* Allocate a new GEM object and fill it with the supplied data */
5780struct drm_i915_gem_object *
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +00005781i915_gem_object_create_from_data(struct drm_i915_private *dev_priv,
Dave Gordonea702992015-07-09 19:29:02 +01005782 const void *data, size_t size)
5783{
5784 struct drm_i915_gem_object *obj;
Chris Wilsonbe062fa2017-03-17 19:46:48 +00005785 struct file *file;
5786 size_t offset;
5787 int err;
Dave Gordonea702992015-07-09 19:29:02 +01005788
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +00005789 obj = i915_gem_object_create(dev_priv, round_up(size, PAGE_SIZE));
Chris Wilsonfe3db792016-04-25 13:32:13 +01005790 if (IS_ERR(obj))
Dave Gordonea702992015-07-09 19:29:02 +01005791 return obj;
5792
Christian Königc0a51fd2018-02-16 13:43:38 +01005793 GEM_BUG_ON(obj->write_domain != I915_GEM_DOMAIN_CPU);
Dave Gordonea702992015-07-09 19:29:02 +01005794
Chris Wilsonbe062fa2017-03-17 19:46:48 +00005795 file = obj->base.filp;
5796 offset = 0;
5797 do {
5798 unsigned int len = min_t(typeof(size), size, PAGE_SIZE);
5799 struct page *page;
5800 void *pgdata, *vaddr;
Dave Gordonea702992015-07-09 19:29:02 +01005801
Chris Wilsonbe062fa2017-03-17 19:46:48 +00005802 err = pagecache_write_begin(file, file->f_mapping,
5803 offset, len, 0,
5804 &page, &pgdata);
5805 if (err < 0)
5806 goto fail;
Dave Gordonea702992015-07-09 19:29:02 +01005807
Chris Wilsonbe062fa2017-03-17 19:46:48 +00005808 vaddr = kmap(page);
5809 memcpy(vaddr, data, len);
5810 kunmap(page);
5811
5812 err = pagecache_write_end(file, file->f_mapping,
5813 offset, len, len,
5814 page, pgdata);
5815 if (err < 0)
5816 goto fail;
5817
5818 size -= len;
5819 data += len;
5820 offset += len;
5821 } while (size);
Dave Gordonea702992015-07-09 19:29:02 +01005822
5823 return obj;
5824
5825fail:
Chris Wilsonf8c417c2016-07-20 13:31:53 +01005826 i915_gem_object_put(obj);
Chris Wilsonbe062fa2017-03-17 19:46:48 +00005827 return ERR_PTR(err);
Dave Gordonea702992015-07-09 19:29:02 +01005828}
Chris Wilson96d77632016-10-28 13:58:33 +01005829
5830struct scatterlist *
5831i915_gem_object_get_sg(struct drm_i915_gem_object *obj,
5832 unsigned int n,
5833 unsigned int *offset)
5834{
Chris Wilsona4f5ea62016-10-28 13:58:35 +01005835 struct i915_gem_object_page_iter *iter = &obj->mm.get_page;
Chris Wilson96d77632016-10-28 13:58:33 +01005836 struct scatterlist *sg;
5837 unsigned int idx, count;
5838
5839 might_sleep();
5840 GEM_BUG_ON(n >= obj->base.size >> PAGE_SHIFT);
Chris Wilsona4f5ea62016-10-28 13:58:35 +01005841 GEM_BUG_ON(!i915_gem_object_has_pinned_pages(obj));
Chris Wilson96d77632016-10-28 13:58:33 +01005842
5843 /* As we iterate forward through the sg, we record each entry in a
5844 * radixtree for quick repeated (backwards) lookups. If we have seen
5845 * this index previously, we will have an entry for it.
5846 *
5847 * Initial lookup is O(N), but this is amortized to O(1) for
5848 * sequential page access (where each new request is consecutive
5849 * to the previous one). Repeated lookups are O(lg(obj->base.size)),
5850 * i.e. O(1) with a large constant!
5851 */
5852 if (n < READ_ONCE(iter->sg_idx))
5853 goto lookup;
5854
5855 mutex_lock(&iter->lock);
5856
5857 /* We prefer to reuse the last sg so that repeated lookup of this
5858 * (or the subsequent) sg are fast - comparing against the last
5859 * sg is faster than going through the radixtree.
5860 */
5861
5862 sg = iter->sg_pos;
5863 idx = iter->sg_idx;
5864 count = __sg_page_count(sg);
5865
5866 while (idx + count <= n) {
5867 unsigned long exception, i;
5868 int ret;
5869
5870 /* If we cannot allocate and insert this entry, or the
5871 * individual pages from this range, cancel updating the
5872 * sg_idx so that on this lookup we are forced to linearly
5873 * scan onwards, but on future lookups we will try the
5874 * insertion again (in which case we need to be careful of
5875 * the error return reporting that we have already inserted
5876 * this index).
5877 */
5878 ret = radix_tree_insert(&iter->radix, idx, sg);
5879 if (ret && ret != -EEXIST)
5880 goto scan;
5881
5882 exception =
5883 RADIX_TREE_EXCEPTIONAL_ENTRY |
5884 idx << RADIX_TREE_EXCEPTIONAL_SHIFT;
5885 for (i = 1; i < count; i++) {
5886 ret = radix_tree_insert(&iter->radix, idx + i,
5887 (void *)exception);
5888 if (ret && ret != -EEXIST)
5889 goto scan;
5890 }
5891
5892 idx += count;
5893 sg = ____sg_next(sg);
5894 count = __sg_page_count(sg);
5895 }
5896
5897scan:
5898 iter->sg_pos = sg;
5899 iter->sg_idx = idx;
5900
5901 mutex_unlock(&iter->lock);
5902
5903 if (unlikely(n < idx)) /* insertion completed by another thread */
5904 goto lookup;
5905
5906 /* In case we failed to insert the entry into the radixtree, we need
5907 * to look beyond the current sg.
5908 */
5909 while (idx + count <= n) {
5910 idx += count;
5911 sg = ____sg_next(sg);
5912 count = __sg_page_count(sg);
5913 }
5914
5915 *offset = n - idx;
5916 return sg;
5917
5918lookup:
5919 rcu_read_lock();
5920
5921 sg = radix_tree_lookup(&iter->radix, n);
5922 GEM_BUG_ON(!sg);
5923
5924 /* If this index is in the middle of multi-page sg entry,
5925 * the radixtree will contain an exceptional entry that points
5926 * to the start of that range. We will return the pointer to
5927 * the base page and the offset of this page within the
5928 * sg entry's range.
5929 */
5930 *offset = 0;
5931 if (unlikely(radix_tree_exception(sg))) {
5932 unsigned long base =
5933 (unsigned long)sg >> RADIX_TREE_EXCEPTIONAL_SHIFT;
5934
5935 sg = radix_tree_lookup(&iter->radix, base);
5936 GEM_BUG_ON(!sg);
5937
5938 *offset = n - base;
5939 }
5940
5941 rcu_read_unlock();
5942
5943 return sg;
5944}
5945
5946struct page *
5947i915_gem_object_get_page(struct drm_i915_gem_object *obj, unsigned int n)
5948{
5949 struct scatterlist *sg;
5950 unsigned int offset;
5951
5952 GEM_BUG_ON(!i915_gem_object_has_struct_page(obj));
5953
5954 sg = i915_gem_object_get_sg(obj, n, &offset);
5955 return nth_page(sg_page(sg), offset);
5956}
5957
5958/* Like i915_gem_object_get_page(), but mark the returned page dirty */
5959struct page *
5960i915_gem_object_get_dirty_page(struct drm_i915_gem_object *obj,
5961 unsigned int n)
5962{
5963 struct page *page;
5964
5965 page = i915_gem_object_get_page(obj, n);
Chris Wilsona4f5ea62016-10-28 13:58:35 +01005966 if (!obj->mm.dirty)
Chris Wilson96d77632016-10-28 13:58:33 +01005967 set_page_dirty(page);
5968
5969 return page;
5970}
5971
5972dma_addr_t
5973i915_gem_object_get_dma_address(struct drm_i915_gem_object *obj,
5974 unsigned long n)
5975{
5976 struct scatterlist *sg;
5977 unsigned int offset;
5978
5979 sg = i915_gem_object_get_sg(obj, n, &offset);
5980 return sg_dma_address(sg) + (offset << PAGE_SHIFT);
5981}
Chris Wilson935a2f72017-02-13 17:15:13 +00005982
Chris Wilson8eeb7902017-07-26 19:16:01 +01005983int i915_gem_object_attach_phys(struct drm_i915_gem_object *obj, int align)
5984{
5985 struct sg_table *pages;
5986 int err;
5987
5988 if (align > obj->base.size)
5989 return -EINVAL;
5990
5991 if (obj->ops == &i915_gem_phys_ops)
5992 return 0;
5993
5994 if (obj->ops != &i915_gem_object_ops)
5995 return -EINVAL;
5996
5997 err = i915_gem_object_unbind(obj);
5998 if (err)
5999 return err;
6000
6001 mutex_lock(&obj->mm.lock);
6002
6003 if (obj->mm.madv != I915_MADV_WILLNEED) {
6004 err = -EFAULT;
6005 goto err_unlock;
6006 }
6007
6008 if (obj->mm.quirked) {
6009 err = -EFAULT;
6010 goto err_unlock;
6011 }
6012
6013 if (obj->mm.mapping) {
6014 err = -EBUSY;
6015 goto err_unlock;
6016 }
6017
Chris Wilsonf2123812017-10-16 12:40:37 +01006018 pages = fetch_and_zero(&obj->mm.pages);
6019 if (pages) {
6020 struct drm_i915_private *i915 = to_i915(obj->base.dev);
6021
6022 __i915_gem_object_reset_page_iter(obj);
6023
6024 spin_lock(&i915->mm.obj_lock);
6025 list_del(&obj->mm.link);
6026 spin_unlock(&i915->mm.obj_lock);
6027 }
6028
Chris Wilson8eeb7902017-07-26 19:16:01 +01006029 obj->ops = &i915_gem_phys_ops;
6030
Chris Wilson8fb6a5d2017-07-26 19:16:02 +01006031 err = ____i915_gem_object_get_pages(obj);
Chris Wilson8eeb7902017-07-26 19:16:01 +01006032 if (err)
6033 goto err_xfer;
6034
6035 /* Perma-pin (until release) the physical set of pages */
6036 __i915_gem_object_pin_pages(obj);
6037
6038 if (!IS_ERR_OR_NULL(pages))
6039 i915_gem_object_ops.put_pages(obj, pages);
6040 mutex_unlock(&obj->mm.lock);
6041 return 0;
6042
6043err_xfer:
6044 obj->ops = &i915_gem_object_ops;
6045 obj->mm.pages = pages;
6046err_unlock:
6047 mutex_unlock(&obj->mm.lock);
6048 return err;
6049}
6050
Chris Wilson935a2f72017-02-13 17:15:13 +00006051#if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
6052#include "selftests/scatterlist.c"
Chris Wilson66d9cb52017-02-13 17:15:17 +00006053#include "selftests/mock_gem_device.c"
Chris Wilson44653982017-02-13 17:15:20 +00006054#include "selftests/huge_gem_object.c"
Matthew Auld40498662017-10-06 23:18:29 +01006055#include "selftests/huge_pages.c"
Chris Wilson8335fd62017-02-13 17:15:28 +00006056#include "selftests/i915_gem_object.c"
Chris Wilson17059452017-02-13 17:15:32 +00006057#include "selftests/i915_gem_coherency.c"
Chris Wilson935a2f72017-02-13 17:15:13 +00006058#endif