blob: 25534d17496ff14aee1b406b6a3d64206be5b54d [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 Wilson82ad6442018-06-05 16:37:58 +010068 return drm_mm_insert_node_in_range(&ggtt->vm.mm, node,
Chris Wilson4e64e552017-02-02 21:04:38 +000069 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{
Chris Wilson4dfacb02018-05-31 09:22:43 +0100142 GEM_TRACE("\n");
143
Chris Wilsone4d20062018-04-06 16:51:44 +0100144 lockdep_assert_held(&i915->drm.struct_mutex);
145 GEM_BUG_ON(i915->gt.active_requests);
Chris Wilson643b4502018-04-30 14:15:03 +0100146 GEM_BUG_ON(!list_empty(&i915->gt.active_rings));
Chris Wilsone4d20062018-04-06 16:51:44 +0100147
148 if (!i915->gt.awake)
149 return I915_EPOCH_INVALID;
150
151 GEM_BUG_ON(i915->gt.epoch == I915_EPOCH_INVALID);
152
153 /*
154 * Be paranoid and flush a concurrent interrupt to make sure
155 * we don't reactivate any irq tasklets after parking.
156 *
157 * FIXME: Note that even though we have waited for execlists to be idle,
158 * there may still be an in-flight interrupt even though the CSB
159 * is now empty. synchronize_irq() makes sure that a residual interrupt
160 * is completed before we continue, but it doesn't prevent the HW from
161 * raising a spurious interrupt later. To complete the shield we should
162 * coordinate disabling the CS irq with flushing the interrupts.
163 */
164 synchronize_irq(i915->drm.irq);
165
166 intel_engines_park(i915);
Chris Wilsona89d1f92018-05-02 17:38:39 +0100167 i915_timelines_park(i915);
Chris Wilsone4d20062018-04-06 16:51:44 +0100168
169 i915_pmu_gt_parked(i915);
Chris Wilson3365e222018-05-03 20:51:14 +0100170 i915_vma_parked(i915);
Chris Wilsone4d20062018-04-06 16:51:44 +0100171
172 i915->gt.awake = false;
173
174 if (INTEL_GEN(i915) >= 6)
175 gen6_rps_idle(i915);
176
177 intel_display_power_put(i915, POWER_DOMAIN_GT_IRQ);
178
179 intel_runtime_pm_put(i915);
180
181 return i915->gt.epoch;
182}
183
184void i915_gem_park(struct drm_i915_private *i915)
185{
Chris Wilson4dfacb02018-05-31 09:22:43 +0100186 GEM_TRACE("\n");
187
Chris Wilsone4d20062018-04-06 16:51:44 +0100188 lockdep_assert_held(&i915->drm.struct_mutex);
189 GEM_BUG_ON(i915->gt.active_requests);
190
191 if (!i915->gt.awake)
192 return;
193
194 /* Defer the actual call to __i915_gem_park() to prevent ping-pongs */
195 mod_delayed_work(i915->wq, &i915->gt.idle_work, msecs_to_jiffies(100));
196}
197
198void i915_gem_unpark(struct drm_i915_private *i915)
199{
Chris Wilson4dfacb02018-05-31 09:22:43 +0100200 GEM_TRACE("\n");
201
Chris Wilsone4d20062018-04-06 16:51:44 +0100202 lockdep_assert_held(&i915->drm.struct_mutex);
203 GEM_BUG_ON(!i915->gt.active_requests);
204
205 if (i915->gt.awake)
206 return;
207
208 intel_runtime_pm_get_noresume(i915);
209
210 /*
211 * It seems that the DMC likes to transition between the DC states a lot
212 * when there are no connected displays (no active power domains) during
213 * command submission.
214 *
215 * This activity has negative impact on the performance of the chip with
216 * huge latencies observed in the interrupt handler and elsewhere.
217 *
218 * Work around it by grabbing a GT IRQ power domain whilst there is any
219 * GT activity, preventing any DC state transitions.
220 */
221 intel_display_power_get(i915, POWER_DOMAIN_GT_IRQ);
222
223 i915->gt.awake = true;
224 if (unlikely(++i915->gt.epoch == 0)) /* keep 0 as invalid */
225 i915->gt.epoch = 1;
226
227 intel_enable_gt_powersave(i915);
228 i915_update_gfx_val(i915);
229 if (INTEL_GEN(i915) >= 6)
230 gen6_rps_busy(i915);
231 i915_pmu_gt_unparked(i915);
232
233 intel_engines_unpark(i915);
234
235 i915_queue_hangcheck(i915);
236
237 queue_delayed_work(i915->wq,
238 &i915->gt.retire_work,
239 round_jiffies_up_relative(HZ));
240}
241
Eric Anholt673a3942008-07-30 12:06:12 -0700242int
Eric Anholt5a125c32008-10-22 21:40:13 -0700243i915_gem_get_aperture_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +0000244 struct drm_file *file)
Eric Anholt5a125c32008-10-22 21:40:13 -0700245{
Joonas Lahtinen72e96d62016-03-30 16:57:10 +0300246 struct drm_i915_private *dev_priv = to_i915(dev);
Joonas Lahtinen62106b42016-03-18 10:42:57 +0200247 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Joonas Lahtinen72e96d62016-03-30 16:57:10 +0300248 struct drm_i915_gem_get_aperture *args = data;
Tvrtko Ursulinca1543b2015-07-01 11:51:10 +0100249 struct i915_vma *vma;
Weinan Liff8f7972017-05-31 10:35:52 +0800250 u64 pinned;
Eric Anholt5a125c32008-10-22 21:40:13 -0700251
Chris Wilson82ad6442018-06-05 16:37:58 +0100252 pinned = ggtt->vm.reserved;
Chris Wilson73aa8082010-09-30 11:46:12 +0100253 mutex_lock(&dev->struct_mutex);
Chris Wilson82ad6442018-06-05 16:37:58 +0100254 list_for_each_entry(vma, &ggtt->vm.active_list, vm_link)
Chris Wilson20dfbde2016-08-04 16:32:30 +0100255 if (i915_vma_is_pinned(vma))
Tvrtko Ursulinca1543b2015-07-01 11:51:10 +0100256 pinned += vma->node.size;
Chris Wilson82ad6442018-06-05 16:37:58 +0100257 list_for_each_entry(vma, &ggtt->vm.inactive_list, vm_link)
Chris Wilson20dfbde2016-08-04 16:32:30 +0100258 if (i915_vma_is_pinned(vma))
Tvrtko Ursulinca1543b2015-07-01 11:51:10 +0100259 pinned += vma->node.size;
Chris Wilson73aa8082010-09-30 11:46:12 +0100260 mutex_unlock(&dev->struct_mutex);
Eric Anholt5a125c32008-10-22 21:40:13 -0700261
Chris Wilson82ad6442018-06-05 16:37:58 +0100262 args->aper_size = ggtt->vm.total;
Akshay Joshi0206e352011-08-16 15:34:10 -0400263 args->aper_available_size = args->aper_size - pinned;
Chris Wilson6299f992010-11-24 12:23:44 +0000264
Eric Anholt5a125c32008-10-22 21:40:13 -0700265 return 0;
266}
267
Matthew Auldb91b09e2017-10-06 23:18:17 +0100268static int i915_gem_object_get_pages_phys(struct drm_i915_gem_object *obj)
Chris Wilson00731152014-05-21 12:42:56 +0100269{
Al Viro93c76a32015-12-04 23:45:44 -0500270 struct address_space *mapping = obj->base.filp->f_mapping;
Chris Wilsondbb43512016-12-07 13:34:11 +0000271 drm_dma_handle_t *phys;
Chris Wilson6a2c4232014-11-04 04:51:40 -0800272 struct sg_table *st;
273 struct scatterlist *sg;
Chris Wilsondbb43512016-12-07 13:34:11 +0000274 char *vaddr;
Chris Wilson6a2c4232014-11-04 04:51:40 -0800275 int i;
Matthew Auldb91b09e2017-10-06 23:18:17 +0100276 int err;
Chris Wilson00731152014-05-21 12:42:56 +0100277
Chris Wilson6a2c4232014-11-04 04:51:40 -0800278 if (WARN_ON(i915_gem_object_needs_bit17_swizzle(obj)))
Matthew Auldb91b09e2017-10-06 23:18:17 +0100279 return -EINVAL;
Chris Wilson00731152014-05-21 12:42:56 +0100280
Chris Wilsondbb43512016-12-07 13:34:11 +0000281 /* Always aligning to the object size, allows a single allocation
282 * to handle all possible callers, and given typical object sizes,
283 * the alignment of the buddy allocation will naturally match.
284 */
285 phys = drm_pci_alloc(obj->base.dev,
Ville Syrjälä750fae22017-09-07 17:32:03 +0300286 roundup_pow_of_two(obj->base.size),
Chris Wilsondbb43512016-12-07 13:34:11 +0000287 roundup_pow_of_two(obj->base.size));
288 if (!phys)
Matthew Auldb91b09e2017-10-06 23:18:17 +0100289 return -ENOMEM;
Chris Wilsondbb43512016-12-07 13:34:11 +0000290
291 vaddr = phys->vaddr;
Chris Wilson6a2c4232014-11-04 04:51:40 -0800292 for (i = 0; i < obj->base.size / PAGE_SIZE; i++) {
293 struct page *page;
294 char *src;
295
296 page = shmem_read_mapping_page(mapping, i);
Chris Wilsondbb43512016-12-07 13:34:11 +0000297 if (IS_ERR(page)) {
Matthew Auldb91b09e2017-10-06 23:18:17 +0100298 err = PTR_ERR(page);
Chris Wilsondbb43512016-12-07 13:34:11 +0000299 goto err_phys;
300 }
Chris Wilson6a2c4232014-11-04 04:51:40 -0800301
302 src = kmap_atomic(page);
303 memcpy(vaddr, src, PAGE_SIZE);
304 drm_clflush_virt_range(vaddr, PAGE_SIZE);
305 kunmap_atomic(src);
306
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300307 put_page(page);
Chris Wilson6a2c4232014-11-04 04:51:40 -0800308 vaddr += PAGE_SIZE;
309 }
310
Chris Wilsonc0336662016-05-06 15:40:21 +0100311 i915_gem_chipset_flush(to_i915(obj->base.dev));
Chris Wilson6a2c4232014-11-04 04:51:40 -0800312
313 st = kmalloc(sizeof(*st), GFP_KERNEL);
Chris Wilsondbb43512016-12-07 13:34:11 +0000314 if (!st) {
Matthew Auldb91b09e2017-10-06 23:18:17 +0100315 err = -ENOMEM;
Chris Wilsondbb43512016-12-07 13:34:11 +0000316 goto err_phys;
317 }
Chris Wilson6a2c4232014-11-04 04:51:40 -0800318
319 if (sg_alloc_table(st, 1, GFP_KERNEL)) {
320 kfree(st);
Matthew Auldb91b09e2017-10-06 23:18:17 +0100321 err = -ENOMEM;
Chris Wilsondbb43512016-12-07 13:34:11 +0000322 goto err_phys;
Chris Wilson6a2c4232014-11-04 04:51:40 -0800323 }
324
325 sg = st->sgl;
326 sg->offset = 0;
327 sg->length = obj->base.size;
328
Chris Wilsondbb43512016-12-07 13:34:11 +0000329 sg_dma_address(sg) = phys->busaddr;
Chris Wilson6a2c4232014-11-04 04:51:40 -0800330 sg_dma_len(sg) = obj->base.size;
331
Chris Wilsondbb43512016-12-07 13:34:11 +0000332 obj->phys_handle = phys;
Matthew Auldb91b09e2017-10-06 23:18:17 +0100333
Matthew Aulda5c081662017-10-06 23:18:18 +0100334 __i915_gem_object_set_pages(obj, st, sg->length);
Matthew Auldb91b09e2017-10-06 23:18:17 +0100335
336 return 0;
Chris Wilsondbb43512016-12-07 13:34:11 +0000337
338err_phys:
339 drm_pci_free(obj->base.dev, phys);
Matthew Auldb91b09e2017-10-06 23:18:17 +0100340
341 return err;
Chris Wilson6a2c4232014-11-04 04:51:40 -0800342}
343
Chris Wilsone27ab732017-06-15 13:38:49 +0100344static void __start_cpu_write(struct drm_i915_gem_object *obj)
345{
Christian Königc0a51fd2018-02-16 13:43:38 +0100346 obj->read_domains = I915_GEM_DOMAIN_CPU;
347 obj->write_domain = I915_GEM_DOMAIN_CPU;
Chris Wilsone27ab732017-06-15 13:38:49 +0100348 if (cpu_write_needs_clflush(obj))
349 obj->cache_dirty = true;
350}
351
Chris Wilson6a2c4232014-11-04 04:51:40 -0800352static void
Chris Wilson2b3c8312016-11-11 14:58:09 +0000353__i915_gem_object_release_shmem(struct drm_i915_gem_object *obj,
Chris Wilsone5facdf2016-12-23 14:57:57 +0000354 struct sg_table *pages,
355 bool needs_clflush)
Chris Wilson6a2c4232014-11-04 04:51:40 -0800356{
Chris Wilsona4f5ea62016-10-28 13:58:35 +0100357 GEM_BUG_ON(obj->mm.madv == __I915_MADV_PURGED);
Chris Wilson6a2c4232014-11-04 04:51:40 -0800358
Chris Wilsona4f5ea62016-10-28 13:58:35 +0100359 if (obj->mm.madv == I915_MADV_DONTNEED)
360 obj->mm.dirty = false;
Chris Wilson6a2c4232014-11-04 04:51:40 -0800361
Chris Wilsone5facdf2016-12-23 14:57:57 +0000362 if (needs_clflush &&
Christian Königc0a51fd2018-02-16 13:43:38 +0100363 (obj->read_domains & I915_GEM_DOMAIN_CPU) == 0 &&
Chris Wilsonb8f55be2017-08-11 12:11:16 +0100364 !(obj->cache_coherent & I915_BO_CACHE_COHERENT_FOR_READ))
Chris Wilson2b3c8312016-11-11 14:58:09 +0000365 drm_clflush_sg(pages);
Chris Wilson03ac84f2016-10-28 13:58:36 +0100366
Chris Wilsone27ab732017-06-15 13:38:49 +0100367 __start_cpu_write(obj);
Chris Wilson03ac84f2016-10-28 13:58:36 +0100368}
369
370static void
371i915_gem_object_put_pages_phys(struct drm_i915_gem_object *obj,
372 struct sg_table *pages)
373{
Chris Wilsone5facdf2016-12-23 14:57:57 +0000374 __i915_gem_object_release_shmem(obj, pages, false);
Chris Wilson03ac84f2016-10-28 13:58:36 +0100375
Chris Wilsona4f5ea62016-10-28 13:58:35 +0100376 if (obj->mm.dirty) {
Al Viro93c76a32015-12-04 23:45:44 -0500377 struct address_space *mapping = obj->base.filp->f_mapping;
Chris Wilson6a2c4232014-11-04 04:51:40 -0800378 char *vaddr = obj->phys_handle->vaddr;
Chris Wilson00731152014-05-21 12:42:56 +0100379 int i;
380
381 for (i = 0; i < obj->base.size / PAGE_SIZE; i++) {
Chris Wilson6a2c4232014-11-04 04:51:40 -0800382 struct page *page;
383 char *dst;
Chris Wilson00731152014-05-21 12:42:56 +0100384
Chris Wilson6a2c4232014-11-04 04:51:40 -0800385 page = shmem_read_mapping_page(mapping, i);
386 if (IS_ERR(page))
387 continue;
388
389 dst = kmap_atomic(page);
390 drm_clflush_virt_range(vaddr, PAGE_SIZE);
391 memcpy(dst, vaddr, PAGE_SIZE);
392 kunmap_atomic(dst);
393
394 set_page_dirty(page);
Chris Wilsona4f5ea62016-10-28 13:58:35 +0100395 if (obj->mm.madv == I915_MADV_WILLNEED)
Chris Wilson00731152014-05-21 12:42:56 +0100396 mark_page_accessed(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300397 put_page(page);
Chris Wilson00731152014-05-21 12:42:56 +0100398 vaddr += PAGE_SIZE;
399 }
Chris Wilsona4f5ea62016-10-28 13:58:35 +0100400 obj->mm.dirty = false;
Chris Wilson00731152014-05-21 12:42:56 +0100401 }
402
Chris Wilson03ac84f2016-10-28 13:58:36 +0100403 sg_free_table(pages);
404 kfree(pages);
Chris Wilsondbb43512016-12-07 13:34:11 +0000405
406 drm_pci_free(obj->base.dev, obj->phys_handle);
Chris Wilson6a2c4232014-11-04 04:51:40 -0800407}
408
409static void
410i915_gem_object_release_phys(struct drm_i915_gem_object *obj)
411{
Chris Wilsona4f5ea62016-10-28 13:58:35 +0100412 i915_gem_object_unpin_pages(obj);
Chris Wilson6a2c4232014-11-04 04:51:40 -0800413}
414
415static const struct drm_i915_gem_object_ops i915_gem_phys_ops = {
416 .get_pages = i915_gem_object_get_pages_phys,
417 .put_pages = i915_gem_object_put_pages_phys,
418 .release = i915_gem_object_release_phys,
419};
420
Chris Wilson581ab1f2017-02-15 16:39:00 +0000421static const struct drm_i915_gem_object_ops i915_gem_object_ops;
422
Chris Wilson35a96112016-08-14 18:44:40 +0100423int i915_gem_object_unbind(struct drm_i915_gem_object *obj)
Chris Wilsonaa653a62016-08-04 07:52:27 +0100424{
425 struct i915_vma *vma;
426 LIST_HEAD(still_in_list);
Chris Wilson02bef8f2016-08-14 18:44:41 +0100427 int ret;
Chris Wilsonaa653a62016-08-04 07:52:27 +0100428
Chris Wilson02bef8f2016-08-14 18:44:41 +0100429 lockdep_assert_held(&obj->base.dev->struct_mutex);
430
431 /* Closed vma are removed from the obj->vma_list - but they may
432 * still have an active binding on the object. To remove those we
433 * must wait for all rendering to complete to the object (as unbinding
434 * must anyway), and retire the requests.
Chris Wilsonaa653a62016-08-04 07:52:27 +0100435 */
Chris Wilson5888fc92017-12-04 13:25:13 +0000436 ret = i915_gem_object_set_to_cpu_domain(obj, false);
Chris Wilson02bef8f2016-08-14 18:44:41 +0100437 if (ret)
438 return ret;
439
Chris Wilsonaa653a62016-08-04 07:52:27 +0100440 while ((vma = list_first_entry_or_null(&obj->vma_list,
441 struct i915_vma,
442 obj_link))) {
443 list_move_tail(&vma->obj_link, &still_in_list);
444 ret = i915_vma_unbind(vma);
445 if (ret)
446 break;
447 }
448 list_splice(&still_in_list, &obj->vma_list);
449
450 return ret;
451}
452
Chris Wilsone95433c2016-10-28 13:58:27 +0100453static long
454i915_gem_object_wait_fence(struct dma_fence *fence,
455 unsigned int flags,
456 long timeout,
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +0100457 struct intel_rps_client *rps_client)
Chris Wilsone95433c2016-10-28 13:58:27 +0100458{
Chris Wilsone61e0f52018-02-21 09:56:36 +0000459 struct i915_request *rq;
Chris Wilsone95433c2016-10-28 13:58:27 +0100460
461 BUILD_BUG_ON(I915_WAIT_INTERRUPTIBLE != 0x1);
462
463 if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags))
464 return timeout;
465
466 if (!dma_fence_is_i915(fence))
467 return dma_fence_wait_timeout(fence,
468 flags & I915_WAIT_INTERRUPTIBLE,
469 timeout);
470
471 rq = to_request(fence);
Chris Wilsone61e0f52018-02-21 09:56:36 +0000472 if (i915_request_completed(rq))
Chris Wilsone95433c2016-10-28 13:58:27 +0100473 goto out;
474
Chris Wilsone9af4ea2018-01-18 13:16:09 +0000475 /*
476 * This client is about to stall waiting for the GPU. In many cases
Chris Wilsone95433c2016-10-28 13:58:27 +0100477 * this is undesirable and limits the throughput of the system, as
478 * many clients cannot continue processing user input/output whilst
479 * blocked. RPS autotuning may take tens of milliseconds to respond
480 * to the GPU load and thus incurs additional latency for the client.
481 * We can circumvent that by promoting the GPU frequency to maximum
482 * before we wait. This makes the GPU throttle up much more quickly
483 * (good for benchmarks and user experience, e.g. window animations),
484 * but at a cost of spending more power processing the workload
485 * (bad for battery). Not all clients even want their results
486 * immediately and for them we should just let the GPU select its own
487 * frequency to maximise efficiency. To prevent a single client from
488 * forcing the clocks too high for the whole system, we only allow
489 * each client to waitboost once in a busy period.
490 */
Chris Wilsone61e0f52018-02-21 09:56:36 +0000491 if (rps_client && !i915_request_started(rq)) {
Chris Wilsone95433c2016-10-28 13:58:27 +0100492 if (INTEL_GEN(rq->i915) >= 6)
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +0100493 gen6_rps_boost(rq, rps_client);
Chris Wilsone95433c2016-10-28 13:58:27 +0100494 }
495
Chris Wilsone61e0f52018-02-21 09:56:36 +0000496 timeout = i915_request_wait(rq, flags, timeout);
Chris Wilsone95433c2016-10-28 13:58:27 +0100497
498out:
Chris Wilsone61e0f52018-02-21 09:56:36 +0000499 if (flags & I915_WAIT_LOCKED && i915_request_completed(rq))
500 i915_request_retire_upto(rq);
Chris Wilsone95433c2016-10-28 13:58:27 +0100501
Chris Wilsone95433c2016-10-28 13:58:27 +0100502 return timeout;
503}
504
505static long
506i915_gem_object_wait_reservation(struct reservation_object *resv,
507 unsigned int flags,
508 long timeout,
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +0100509 struct intel_rps_client *rps_client)
Chris Wilsone95433c2016-10-28 13:58:27 +0100510{
Chris Wilsone54ca972017-02-17 15:13:04 +0000511 unsigned int seq = __read_seqcount_begin(&resv->seq);
Chris Wilsone95433c2016-10-28 13:58:27 +0100512 struct dma_fence *excl;
Chris Wilsone54ca972017-02-17 15:13:04 +0000513 bool prune_fences = false;
Chris Wilsone95433c2016-10-28 13:58:27 +0100514
515 if (flags & I915_WAIT_ALL) {
516 struct dma_fence **shared;
517 unsigned int count, i;
518 int ret;
519
520 ret = reservation_object_get_fences_rcu(resv,
521 &excl, &count, &shared);
522 if (ret)
523 return ret;
524
525 for (i = 0; i < count; i++) {
526 timeout = i915_gem_object_wait_fence(shared[i],
527 flags, timeout,
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +0100528 rps_client);
Chris Wilsond892e932017-02-12 21:53:43 +0000529 if (timeout < 0)
Chris Wilsone95433c2016-10-28 13:58:27 +0100530 break;
531
532 dma_fence_put(shared[i]);
533 }
534
535 for (; i < count; i++)
536 dma_fence_put(shared[i]);
537 kfree(shared);
Chris Wilsone54ca972017-02-17 15:13:04 +0000538
Chris Wilsonfa730552018-03-07 17:13:03 +0000539 /*
540 * If both shared fences and an exclusive fence exist,
541 * then by construction the shared fences must be later
542 * than the exclusive fence. If we successfully wait for
543 * all the shared fences, we know that the exclusive fence
544 * must all be signaled. If all the shared fences are
545 * signaled, we can prune the array and recover the
546 * floating references on the fences/requests.
547 */
Chris Wilsone54ca972017-02-17 15:13:04 +0000548 prune_fences = count && timeout >= 0;
Chris Wilsone95433c2016-10-28 13:58:27 +0100549 } else {
550 excl = reservation_object_get_excl_rcu(resv);
551 }
552
Chris Wilsonfa730552018-03-07 17:13:03 +0000553 if (excl && timeout >= 0)
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +0100554 timeout = i915_gem_object_wait_fence(excl, flags, timeout,
555 rps_client);
Chris Wilsone95433c2016-10-28 13:58:27 +0100556
557 dma_fence_put(excl);
558
Chris Wilsonfa730552018-03-07 17:13:03 +0000559 /*
560 * Opportunistically prune the fences iff we know they have *all* been
Chris Wilson03d1cac2017-03-08 13:26:28 +0000561 * signaled and that the reservation object has not been changed (i.e.
562 * no new fences have been added).
563 */
Chris Wilsone54ca972017-02-17 15:13:04 +0000564 if (prune_fences && !__read_seqcount_retry(&resv->seq, seq)) {
Chris Wilson03d1cac2017-03-08 13:26:28 +0000565 if (reservation_object_trylock(resv)) {
566 if (!__read_seqcount_retry(&resv->seq, seq))
567 reservation_object_add_excl_fence(resv, NULL);
568 reservation_object_unlock(resv);
569 }
Chris Wilsone54ca972017-02-17 15:13:04 +0000570 }
571
Chris Wilsone95433c2016-10-28 13:58:27 +0100572 return timeout;
573}
574
Chris Wilsonb7268c52018-04-18 19:40:52 +0100575static void __fence_set_priority(struct dma_fence *fence,
576 const struct i915_sched_attr *attr)
Chris Wilson6b5e90f2016-11-14 20:41:05 +0000577{
Chris Wilsone61e0f52018-02-21 09:56:36 +0000578 struct i915_request *rq;
Chris Wilson6b5e90f2016-11-14 20:41:05 +0000579 struct intel_engine_cs *engine;
580
Chris Wilsonc218ee02018-01-06 10:56:18 +0000581 if (dma_fence_is_signaled(fence) || !dma_fence_is_i915(fence))
Chris Wilson6b5e90f2016-11-14 20:41:05 +0000582 return;
583
584 rq = to_request(fence);
585 engine = rq->engine;
Chris Wilson6b5e90f2016-11-14 20:41:05 +0000586
Chris Wilson4f6d8fc2018-05-07 14:57:25 +0100587 local_bh_disable();
588 rcu_read_lock(); /* RCU serialisation for set-wedged protection */
Chris Wilson47650db2018-03-07 13:42:25 +0000589 if (engine->schedule)
Chris Wilsonb7268c52018-04-18 19:40:52 +0100590 engine->schedule(rq, attr);
Chris Wilson47650db2018-03-07 13:42:25 +0000591 rcu_read_unlock();
Chris Wilson4f6d8fc2018-05-07 14:57:25 +0100592 local_bh_enable(); /* kick the tasklets if queues were reprioritised */
Chris Wilson6b5e90f2016-11-14 20:41:05 +0000593}
594
Chris Wilsonb7268c52018-04-18 19:40:52 +0100595static void fence_set_priority(struct dma_fence *fence,
596 const struct i915_sched_attr *attr)
Chris Wilson6b5e90f2016-11-14 20:41:05 +0000597{
598 /* Recurse once into a fence-array */
599 if (dma_fence_is_array(fence)) {
600 struct dma_fence_array *array = to_dma_fence_array(fence);
601 int i;
602
603 for (i = 0; i < array->num_fences; i++)
Chris Wilsonb7268c52018-04-18 19:40:52 +0100604 __fence_set_priority(array->fences[i], attr);
Chris Wilson6b5e90f2016-11-14 20:41:05 +0000605 } else {
Chris Wilsonb7268c52018-04-18 19:40:52 +0100606 __fence_set_priority(fence, attr);
Chris Wilson6b5e90f2016-11-14 20:41:05 +0000607 }
608}
609
610int
611i915_gem_object_wait_priority(struct drm_i915_gem_object *obj,
612 unsigned int flags,
Chris Wilsonb7268c52018-04-18 19:40:52 +0100613 const struct i915_sched_attr *attr)
Chris Wilson6b5e90f2016-11-14 20:41:05 +0000614{
615 struct dma_fence *excl;
616
617 if (flags & I915_WAIT_ALL) {
618 struct dma_fence **shared;
619 unsigned int count, i;
620 int ret;
621
622 ret = reservation_object_get_fences_rcu(obj->resv,
623 &excl, &count, &shared);
624 if (ret)
625 return ret;
626
627 for (i = 0; i < count; i++) {
Chris Wilsonb7268c52018-04-18 19:40:52 +0100628 fence_set_priority(shared[i], attr);
Chris Wilson6b5e90f2016-11-14 20:41:05 +0000629 dma_fence_put(shared[i]);
630 }
631
632 kfree(shared);
633 } else {
634 excl = reservation_object_get_excl_rcu(obj->resv);
635 }
636
637 if (excl) {
Chris Wilsonb7268c52018-04-18 19:40:52 +0100638 fence_set_priority(excl, attr);
Chris Wilson6b5e90f2016-11-14 20:41:05 +0000639 dma_fence_put(excl);
640 }
641 return 0;
642}
643
Chris Wilson00e60f22016-08-04 16:32:40 +0100644/**
Chris Wilsone95433c2016-10-28 13:58:27 +0100645 * Waits for rendering to the object to be completed
Chris Wilson00e60f22016-08-04 16:32:40 +0100646 * @obj: i915 gem object
Chris Wilsone95433c2016-10-28 13:58:27 +0100647 * @flags: how to wait (under a lock, for all rendering or just for writes etc)
648 * @timeout: how long to wait
Chris Wilsona0a8b1c2017-11-09 14:06:44 +0000649 * @rps_client: client (user process) to charge for any waitboosting
Chris Wilson00e60f22016-08-04 16:32:40 +0100650 */
651int
Chris Wilsone95433c2016-10-28 13:58:27 +0100652i915_gem_object_wait(struct drm_i915_gem_object *obj,
653 unsigned int flags,
654 long timeout,
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +0100655 struct intel_rps_client *rps_client)
Chris Wilson00e60f22016-08-04 16:32:40 +0100656{
Chris Wilsone95433c2016-10-28 13:58:27 +0100657 might_sleep();
658#if IS_ENABLED(CONFIG_LOCKDEP)
659 GEM_BUG_ON(debug_locks &&
660 !!lockdep_is_held(&obj->base.dev->struct_mutex) !=
661 !!(flags & I915_WAIT_LOCKED));
662#endif
663 GEM_BUG_ON(timeout < 0);
Chris Wilson00e60f22016-08-04 16:32:40 +0100664
Chris Wilsond07f0e52016-10-28 13:58:44 +0100665 timeout = i915_gem_object_wait_reservation(obj->resv,
666 flags, timeout,
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +0100667 rps_client);
Chris Wilsone95433c2016-10-28 13:58:27 +0100668 return timeout < 0 ? timeout : 0;
Chris Wilson00e60f22016-08-04 16:32:40 +0100669}
670
671static struct intel_rps_client *to_rps_client(struct drm_file *file)
672{
673 struct drm_i915_file_private *fpriv = file->driver_priv;
674
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +0100675 return &fpriv->rps_client;
Chris Wilson00e60f22016-08-04 16:32:40 +0100676}
677
Chris Wilson00731152014-05-21 12:42:56 +0100678static int
679i915_gem_phys_pwrite(struct drm_i915_gem_object *obj,
680 struct drm_i915_gem_pwrite *args,
Chris Wilson03ac84f2016-10-28 13:58:36 +0100681 struct drm_file *file)
Chris Wilson00731152014-05-21 12:42:56 +0100682{
Chris Wilson00731152014-05-21 12:42:56 +0100683 void *vaddr = obj->phys_handle->vaddr + args->offset;
Gustavo Padovan3ed605b2016-04-26 12:32:27 -0300684 char __user *user_data = u64_to_user_ptr(args->data_ptr);
Chris Wilson6a2c4232014-11-04 04:51:40 -0800685
686 /* We manually control the domain here and pretend that it
687 * remains coherent i.e. in the GTT domain, like shmem_pwrite.
688 */
Rodrigo Vivi77a0d1c2015-06-18 11:43:24 -0700689 intel_fb_obj_invalidate(obj, ORIGIN_CPU);
Chris Wilson10466d22017-01-06 15:22:38 +0000690 if (copy_from_user(vaddr, user_data, args->size))
691 return -EFAULT;
Chris Wilson00731152014-05-21 12:42:56 +0100692
Chris Wilson6a2c4232014-11-04 04:51:40 -0800693 drm_clflush_virt_range(vaddr, args->size);
Chris Wilson10466d22017-01-06 15:22:38 +0000694 i915_gem_chipset_flush(to_i915(obj->base.dev));
Paulo Zanoni063e4e62015-02-13 17:23:45 -0200695
Chris Wilsond59b21e2017-02-22 11:40:49 +0000696 intel_fb_obj_flush(obj, ORIGIN_CPU);
Chris Wilson10466d22017-01-06 15:22:38 +0000697 return 0;
Chris Wilson00731152014-05-21 12:42:56 +0100698}
699
Tvrtko Ursulin187685c2016-12-01 14:16:36 +0000700void *i915_gem_object_alloc(struct drm_i915_private *dev_priv)
Chris Wilson42dcedd2012-11-15 11:32:30 +0000701{
Chris Wilsonefab6d82015-04-07 16:20:57 +0100702 return kmem_cache_zalloc(dev_priv->objects, GFP_KERNEL);
Chris Wilson42dcedd2012-11-15 11:32:30 +0000703}
704
705void i915_gem_object_free(struct drm_i915_gem_object *obj)
706{
Chris Wilsonfac5e232016-07-04 11:34:36 +0100707 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
Chris Wilsonefab6d82015-04-07 16:20:57 +0100708 kmem_cache_free(dev_priv->objects, obj);
Chris Wilson42dcedd2012-11-15 11:32:30 +0000709}
710
Dave Airlieff72145b2011-02-07 12:16:14 +1000711static int
712i915_gem_create(struct drm_file *file,
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +0000713 struct drm_i915_private *dev_priv,
Dave Airlieff72145b2011-02-07 12:16:14 +1000714 uint64_t size,
715 uint32_t *handle_p)
Eric Anholt673a3942008-07-30 12:06:12 -0700716{
Chris Wilson05394f32010-11-08 19:18:58 +0000717 struct drm_i915_gem_object *obj;
Pekka Paalanena1a2d1d2009-08-23 12:40:55 +0300718 int ret;
719 u32 handle;
Eric Anholt673a3942008-07-30 12:06:12 -0700720
Dave Airlieff72145b2011-02-07 12:16:14 +1000721 size = roundup(size, PAGE_SIZE);
Chris Wilson8ffc0242011-09-14 14:14:28 +0200722 if (size == 0)
723 return -EINVAL;
Eric Anholt673a3942008-07-30 12:06:12 -0700724
725 /* Allocate the new object */
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +0000726 obj = i915_gem_object_create(dev_priv, size);
Chris Wilsonfe3db792016-04-25 13:32:13 +0100727 if (IS_ERR(obj))
728 return PTR_ERR(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700729
Chris Wilson05394f32010-11-08 19:18:58 +0000730 ret = drm_gem_handle_create(file, &obj->base, &handle);
Chris Wilson202f2fe2010-10-14 13:20:40 +0100731 /* drop reference from allocate - handle holds it now */
Chris Wilsonf0cd5182016-10-28 13:58:43 +0100732 i915_gem_object_put(obj);
Daniel Vetterd861e332013-07-24 23:25:03 +0200733 if (ret)
734 return ret;
Chris Wilson202f2fe2010-10-14 13:20:40 +0100735
Dave Airlieff72145b2011-02-07 12:16:14 +1000736 *handle_p = handle;
Eric Anholt673a3942008-07-30 12:06:12 -0700737 return 0;
738}
739
Dave Airlieff72145b2011-02-07 12:16:14 +1000740int
741i915_gem_dumb_create(struct drm_file *file,
742 struct drm_device *dev,
743 struct drm_mode_create_dumb *args)
744{
745 /* have to work out size/pitch and return them */
Paulo Zanonide45eaf2013-10-18 18:48:24 -0300746 args->pitch = ALIGN(args->width * DIV_ROUND_UP(args->bpp, 8), 64);
Dave Airlieff72145b2011-02-07 12:16:14 +1000747 args->size = args->pitch * args->height;
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +0000748 return i915_gem_create(file, to_i915(dev),
Dave Airlieda6b51d2014-12-24 13:11:17 +1000749 args->size, &args->handle);
Dave Airlieff72145b2011-02-07 12:16:14 +1000750}
751
Chris Wilsone27ab732017-06-15 13:38:49 +0100752static bool gpu_write_needs_clflush(struct drm_i915_gem_object *obj)
753{
754 return !(obj->cache_level == I915_CACHE_NONE ||
755 obj->cache_level == I915_CACHE_WT);
756}
757
Dave Airlieff72145b2011-02-07 12:16:14 +1000758/**
759 * Creates a new mm object and returns a handle to it.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +0100760 * @dev: drm device pointer
761 * @data: ioctl data blob
762 * @file: drm file pointer
Dave Airlieff72145b2011-02-07 12:16:14 +1000763 */
764int
765i915_gem_create_ioctl(struct drm_device *dev, void *data,
766 struct drm_file *file)
767{
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +0000768 struct drm_i915_private *dev_priv = to_i915(dev);
Dave Airlieff72145b2011-02-07 12:16:14 +1000769 struct drm_i915_gem_create *args = data;
Daniel Vetter63ed2cb2012-04-23 16:50:50 +0200770
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +0000771 i915_gem_flush_free_objects(dev_priv);
Chris Wilsonfbbd37b2016-10-28 13:58:42 +0100772
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +0000773 return i915_gem_create(file, dev_priv,
Dave Airlieda6b51d2014-12-24 13:11:17 +1000774 args->size, &args->handle);
Dave Airlieff72145b2011-02-07 12:16:14 +1000775}
776
Chris Wilsonef749212017-04-12 12:01:10 +0100777static inline enum fb_op_origin
778fb_write_origin(struct drm_i915_gem_object *obj, unsigned int domain)
779{
780 return (domain == I915_GEM_DOMAIN_GTT ?
781 obj->frontbuffer_ggtt_origin : ORIGIN_CPU);
782}
783
Chris Wilson7125397b2017-12-06 12:49:14 +0000784void i915_gem_flush_ggtt_writes(struct drm_i915_private *dev_priv)
Chris Wilsonef749212017-04-12 12:01:10 +0100785{
Chris Wilson7125397b2017-12-06 12:49:14 +0000786 /*
787 * No actual flushing is required for the GTT write domain for reads
788 * from the GTT domain. Writes to it "immediately" go to main memory
789 * as far as we know, so there's no chipset flush. It also doesn't
790 * land in the GPU render cache.
Chris Wilsonef749212017-04-12 12:01:10 +0100791 *
792 * However, we do have to enforce the order so that all writes through
793 * the GTT land before any writes to the device, such as updates to
794 * the GATT itself.
795 *
796 * We also have to wait a bit for the writes to land from the GTT.
797 * An uncached read (i.e. mmio) seems to be ideal for the round-trip
798 * timing. This issue has only been observed when switching quickly
799 * between GTT writes and CPU reads from inside the kernel on recent hw,
800 * and it appears to only affect discrete GTT blocks (i.e. on LLC
Chris Wilson7125397b2017-12-06 12:49:14 +0000801 * system agents we cannot reproduce this behaviour, until Cannonlake
802 * that was!).
Chris Wilsonef749212017-04-12 12:01:10 +0100803 */
Chris Wilson7125397b2017-12-06 12:49:14 +0000804
Chris Wilson900ccf32018-07-20 11:19:10 +0100805 wmb();
806
807 if (INTEL_INFO(dev_priv)->has_coherent_ggtt)
808 return;
809
Chris Wilsona8bd3b82018-07-17 10:26:55 +0100810 i915_gem_chipset_flush(dev_priv);
Chris Wilsonef749212017-04-12 12:01:10 +0100811
Chris Wilson7125397b2017-12-06 12:49:14 +0000812 intel_runtime_pm_get(dev_priv);
813 spin_lock_irq(&dev_priv->uncore.lock);
814
815 POSTING_READ_FW(RING_HEAD(RENDER_RING_BASE));
816
817 spin_unlock_irq(&dev_priv->uncore.lock);
818 intel_runtime_pm_put(dev_priv);
819}
820
821static void
822flush_write_domain(struct drm_i915_gem_object *obj, unsigned int flush_domains)
823{
824 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
825 struct i915_vma *vma;
826
Christian Königc0a51fd2018-02-16 13:43:38 +0100827 if (!(obj->write_domain & flush_domains))
Chris Wilson7125397b2017-12-06 12:49:14 +0000828 return;
829
Christian Königc0a51fd2018-02-16 13:43:38 +0100830 switch (obj->write_domain) {
Chris Wilsonef749212017-04-12 12:01:10 +0100831 case I915_GEM_DOMAIN_GTT:
Chris Wilson7125397b2017-12-06 12:49:14 +0000832 i915_gem_flush_ggtt_writes(dev_priv);
Chris Wilsonef749212017-04-12 12:01:10 +0100833
834 intel_fb_obj_flush(obj,
835 fb_write_origin(obj, I915_GEM_DOMAIN_GTT));
Chris Wilson7125397b2017-12-06 12:49:14 +0000836
Chris Wilsone2189dd2017-12-07 21:14:07 +0000837 for_each_ggtt_vma(vma, obj) {
Chris Wilson7125397b2017-12-06 12:49:14 +0000838 if (vma->iomap)
839 continue;
840
841 i915_vma_unset_ggtt_write(vma);
842 }
Chris Wilsonef749212017-04-12 12:01:10 +0100843 break;
844
Chris Wilsonadd00e62018-07-06 12:54:02 +0100845 case I915_GEM_DOMAIN_WC:
846 wmb();
847 break;
848
Chris Wilsonef749212017-04-12 12:01:10 +0100849 case I915_GEM_DOMAIN_CPU:
850 i915_gem_clflush_object(obj, I915_CLFLUSH_SYNC);
851 break;
Chris Wilsone27ab732017-06-15 13:38:49 +0100852
853 case I915_GEM_DOMAIN_RENDER:
854 if (gpu_write_needs_clflush(obj))
855 obj->cache_dirty = true;
856 break;
Chris Wilsonef749212017-04-12 12:01:10 +0100857 }
858
Christian Königc0a51fd2018-02-16 13:43:38 +0100859 obj->write_domain = 0;
Chris Wilsonef749212017-04-12 12:01:10 +0100860}
861
Brad Volkin4c914c02014-02-18 10:15:45 -0800862/*
863 * Pins the specified object's pages and synchronizes the object with
864 * GPU accesses. Sets needs_clflush to non-zero if the caller should
865 * flush the object from the CPU cache.
866 */
867int i915_gem_obj_prepare_shmem_read(struct drm_i915_gem_object *obj,
Chris Wilson43394c72016-08-18 17:16:47 +0100868 unsigned int *needs_clflush)
Brad Volkin4c914c02014-02-18 10:15:45 -0800869{
870 int ret;
871
Chris Wilsone95433c2016-10-28 13:58:27 +0100872 lockdep_assert_held(&obj->base.dev->struct_mutex);
Brad Volkin4c914c02014-02-18 10:15:45 -0800873
Chris Wilsone95433c2016-10-28 13:58:27 +0100874 *needs_clflush = 0;
Chris Wilson43394c72016-08-18 17:16:47 +0100875 if (!i915_gem_object_has_struct_page(obj))
876 return -ENODEV;
Brad Volkin4c914c02014-02-18 10:15:45 -0800877
Chris Wilsone95433c2016-10-28 13:58:27 +0100878 ret = i915_gem_object_wait(obj,
879 I915_WAIT_INTERRUPTIBLE |
880 I915_WAIT_LOCKED,
881 MAX_SCHEDULE_TIMEOUT,
882 NULL);
Chris Wilsonc13d87e2016-07-20 09:21:15 +0100883 if (ret)
884 return ret;
885
Chris Wilsona4f5ea62016-10-28 13:58:35 +0100886 ret = i915_gem_object_pin_pages(obj);
Chris Wilson97649512016-08-18 17:16:50 +0100887 if (ret)
888 return ret;
889
Chris Wilsonb8f55be2017-08-11 12:11:16 +0100890 if (obj->cache_coherent & I915_BO_CACHE_COHERENT_FOR_READ ||
891 !static_cpu_has(X86_FEATURE_CLFLUSH)) {
Chris Wilson7f5f95d2017-03-10 00:09:42 +0000892 ret = i915_gem_object_set_to_cpu_domain(obj, false);
893 if (ret)
894 goto err_unpin;
895 else
896 goto out;
897 }
898
Chris Wilsonef749212017-04-12 12:01:10 +0100899 flush_write_domain(obj, ~I915_GEM_DOMAIN_CPU);
Chris Wilsona314d5c2016-08-18 17:16:48 +0100900
Chris Wilson43394c72016-08-18 17:16:47 +0100901 /* If we're not in the cpu read domain, set ourself into the gtt
902 * read domain and manually flush cachelines (if required). This
903 * optimizes for the case when the gpu will dirty the data
904 * anyway again before the next pread happens.
905 */
Chris Wilsone27ab732017-06-15 13:38:49 +0100906 if (!obj->cache_dirty &&
Christian Königc0a51fd2018-02-16 13:43:38 +0100907 !(obj->read_domains & I915_GEM_DOMAIN_CPU))
Chris Wilson7f5f95d2017-03-10 00:09:42 +0000908 *needs_clflush = CLFLUSH_BEFORE;
Brad Volkin4c914c02014-02-18 10:15:45 -0800909
Chris Wilson7f5f95d2017-03-10 00:09:42 +0000910out:
Chris Wilson97649512016-08-18 17:16:50 +0100911 /* return with the pages pinned */
Chris Wilson43394c72016-08-18 17:16:47 +0100912 return 0;
Chris Wilson97649512016-08-18 17:16:50 +0100913
914err_unpin:
915 i915_gem_object_unpin_pages(obj);
916 return ret;
Chris Wilson43394c72016-08-18 17:16:47 +0100917}
918
919int i915_gem_obj_prepare_shmem_write(struct drm_i915_gem_object *obj,
920 unsigned int *needs_clflush)
921{
922 int ret;
923
Chris Wilsone95433c2016-10-28 13:58:27 +0100924 lockdep_assert_held(&obj->base.dev->struct_mutex);
925
Chris Wilson43394c72016-08-18 17:16:47 +0100926 *needs_clflush = 0;
927 if (!i915_gem_object_has_struct_page(obj))
928 return -ENODEV;
929
Chris Wilsone95433c2016-10-28 13:58:27 +0100930 ret = i915_gem_object_wait(obj,
931 I915_WAIT_INTERRUPTIBLE |
932 I915_WAIT_LOCKED |
933 I915_WAIT_ALL,
934 MAX_SCHEDULE_TIMEOUT,
935 NULL);
Chris Wilson43394c72016-08-18 17:16:47 +0100936 if (ret)
937 return ret;
938
Chris Wilsona4f5ea62016-10-28 13:58:35 +0100939 ret = i915_gem_object_pin_pages(obj);
Chris Wilson97649512016-08-18 17:16:50 +0100940 if (ret)
941 return ret;
942
Chris Wilsonb8f55be2017-08-11 12:11:16 +0100943 if (obj->cache_coherent & I915_BO_CACHE_COHERENT_FOR_WRITE ||
944 !static_cpu_has(X86_FEATURE_CLFLUSH)) {
Chris Wilson7f5f95d2017-03-10 00:09:42 +0000945 ret = i915_gem_object_set_to_cpu_domain(obj, true);
946 if (ret)
947 goto err_unpin;
948 else
949 goto out;
950 }
951
Chris Wilsonef749212017-04-12 12:01:10 +0100952 flush_write_domain(obj, ~I915_GEM_DOMAIN_CPU);
Chris Wilsona314d5c2016-08-18 17:16:48 +0100953
Chris Wilson43394c72016-08-18 17:16:47 +0100954 /* If we're not in the cpu write domain, set ourself into the
955 * gtt write domain and manually flush cachelines (as required).
956 * This optimizes for the case when the gpu will use the data
957 * right away and we therefore have to clflush anyway.
958 */
Chris Wilsone27ab732017-06-15 13:38:49 +0100959 if (!obj->cache_dirty) {
Chris Wilson7f5f95d2017-03-10 00:09:42 +0000960 *needs_clflush |= CLFLUSH_AFTER;
Chris Wilson43394c72016-08-18 17:16:47 +0100961
Chris Wilsone27ab732017-06-15 13:38:49 +0100962 /*
963 * Same trick applies to invalidate partially written
964 * cachelines read before writing.
965 */
Christian Königc0a51fd2018-02-16 13:43:38 +0100966 if (!(obj->read_domains & I915_GEM_DOMAIN_CPU))
Chris Wilsone27ab732017-06-15 13:38:49 +0100967 *needs_clflush |= CLFLUSH_BEFORE;
968 }
Chris Wilson43394c72016-08-18 17:16:47 +0100969
Chris Wilson7f5f95d2017-03-10 00:09:42 +0000970out:
Chris Wilson43394c72016-08-18 17:16:47 +0100971 intel_fb_obj_invalidate(obj, ORIGIN_CPU);
Chris Wilsona4f5ea62016-10-28 13:58:35 +0100972 obj->mm.dirty = true;
Chris Wilson97649512016-08-18 17:16:50 +0100973 /* return with the pages pinned */
Chris Wilson43394c72016-08-18 17:16:47 +0100974 return 0;
Chris Wilson97649512016-08-18 17:16:50 +0100975
976err_unpin:
977 i915_gem_object_unpin_pages(obj);
978 return ret;
Brad Volkin4c914c02014-02-18 10:15:45 -0800979}
980
Daniel Vetterd174bd62012-03-25 19:47:40 +0200981static int
Chris Wilsonb9d126e2019-01-05 12:07:58 +0000982shmem_pread(struct page *page, int offset, int len, char __user *user_data,
983 bool needs_clflush)
Daniel Vetterd174bd62012-03-25 19:47:40 +0200984{
985 char *vaddr;
986 int ret;
987
988 vaddr = kmap(page);
Daniel Vetterd174bd62012-03-25 19:47:40 +0200989
Chris Wilsonb9d126e2019-01-05 12:07:58 +0000990 if (needs_clflush)
991 drm_clflush_virt_range(vaddr + offset, len);
992
993 ret = __copy_to_user(user_data, vaddr + offset, len);
994
Daniel Vetterd174bd62012-03-25 19:47:40 +0200995 kunmap(page);
996
Chris Wilsonb9d126e2019-01-05 12:07:58 +0000997 return ret ? -EFAULT : 0;
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100998}
999
1000static int
1001i915_gem_shmem_pread(struct drm_i915_gem_object *obj,
1002 struct drm_i915_gem_pread *args)
1003{
1004 char __user *user_data;
1005 u64 remain;
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001006 unsigned int needs_clflush;
1007 unsigned int idx, offset;
1008 int ret;
1009
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001010 ret = mutex_lock_interruptible(&obj->base.dev->struct_mutex);
1011 if (ret)
1012 return ret;
1013
1014 ret = i915_gem_obj_prepare_shmem_read(obj, &needs_clflush);
1015 mutex_unlock(&obj->base.dev->struct_mutex);
1016 if (ret)
1017 return ret;
1018
1019 remain = args->size;
1020 user_data = u64_to_user_ptr(args->data_ptr);
1021 offset = offset_in_page(args->offset);
1022 for (idx = args->offset >> PAGE_SHIFT; remain; idx++) {
1023 struct page *page = i915_gem_object_get_page(obj, idx);
Chris Wilsona5e856a52018-10-12 15:02:28 +01001024 unsigned int length = min_t(u64, remain, PAGE_SIZE - offset);
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001025
1026 ret = shmem_pread(page, offset, length, user_data,
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001027 needs_clflush);
1028 if (ret)
1029 break;
1030
1031 remain -= length;
1032 user_data += length;
1033 offset = 0;
1034 }
1035
1036 i915_gem_obj_finish_shmem_access(obj);
1037 return ret;
1038}
1039
1040static inline bool
1041gtt_user_read(struct io_mapping *mapping,
1042 loff_t base, int offset,
1043 char __user *user_data, int length)
1044{
Ville Syrjäläafe722b2017-09-01 20:12:52 +03001045 void __iomem *vaddr;
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001046 unsigned long unwritten;
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301047
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301048 /* We can use the cpu mem copy function because this is X86. */
Ville Syrjäläafe722b2017-09-01 20:12:52 +03001049 vaddr = io_mapping_map_atomic_wc(mapping, base);
1050 unwritten = __copy_to_user_inatomic(user_data,
1051 (void __force *)vaddr + offset,
1052 length);
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001053 io_mapping_unmap_atomic(vaddr);
1054 if (unwritten) {
Ville Syrjäläafe722b2017-09-01 20:12:52 +03001055 vaddr = io_mapping_map_wc(mapping, base, PAGE_SIZE);
1056 unwritten = copy_to_user(user_data,
1057 (void __force *)vaddr + offset,
1058 length);
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001059 io_mapping_unmap(vaddr);
1060 }
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301061 return unwritten;
1062}
1063
1064static int
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001065i915_gem_gtt_pread(struct drm_i915_gem_object *obj,
1066 const struct drm_i915_gem_pread *args)
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301067{
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001068 struct drm_i915_private *i915 = to_i915(obj->base.dev);
1069 struct i915_ggtt *ggtt = &i915->ggtt;
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301070 struct drm_mm_node node;
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001071 struct i915_vma *vma;
1072 void __user *user_data;
1073 u64 remain, offset;
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301074 int ret;
1075
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001076 ret = mutex_lock_interruptible(&i915->drm.struct_mutex);
1077 if (ret)
1078 return ret;
1079
1080 intel_runtime_pm_get(i915);
1081 vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0,
Chris Wilsona3259ca2017-10-09 09:44:00 +01001082 PIN_MAPPABLE |
1083 PIN_NONFAULT |
1084 PIN_NONBLOCK);
Chris Wilson18034582016-08-18 17:16:45 +01001085 if (!IS_ERR(vma)) {
1086 node.start = i915_ggtt_offset(vma);
1087 node.allocated = false;
Chris Wilson49ef5292016-08-18 17:17:00 +01001088 ret = i915_vma_put_fence(vma);
Chris Wilson18034582016-08-18 17:16:45 +01001089 if (ret) {
1090 i915_vma_unpin(vma);
1091 vma = ERR_PTR(ret);
1092 }
1093 }
Chris Wilson058d88c2016-08-15 10:49:06 +01001094 if (IS_ERR(vma)) {
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001095 ret = insert_mappable_node(ggtt, &node, PAGE_SIZE);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301096 if (ret)
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001097 goto out_unlock;
1098 GEM_BUG_ON(!node.allocated);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301099 }
1100
1101 ret = i915_gem_object_set_to_gtt_domain(obj, false);
1102 if (ret)
1103 goto out_unpin;
1104
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001105 mutex_unlock(&i915->drm.struct_mutex);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301106
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001107 user_data = u64_to_user_ptr(args->data_ptr);
1108 remain = args->size;
1109 offset = args->offset;
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301110
1111 while (remain > 0) {
1112 /* Operation in this page
1113 *
1114 * page_base = page offset within aperture
1115 * page_offset = offset within page
1116 * page_length = bytes to copy for this page
1117 */
1118 u32 page_base = node.start;
1119 unsigned page_offset = offset_in_page(offset);
1120 unsigned page_length = PAGE_SIZE - page_offset;
1121 page_length = remain < page_length ? remain : page_length;
1122 if (node.allocated) {
1123 wmb();
Chris Wilson82ad6442018-06-05 16:37:58 +01001124 ggtt->vm.insert_page(&ggtt->vm,
1125 i915_gem_object_get_dma_address(obj, offset >> PAGE_SHIFT),
1126 node.start, I915_CACHE_NONE, 0);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301127 wmb();
1128 } else {
1129 page_base += offset & PAGE_MASK;
1130 }
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001131
Matthew Auld73ebd502017-12-11 15:18:20 +00001132 if (gtt_user_read(&ggtt->iomap, page_base, page_offset,
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001133 user_data, page_length)) {
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301134 ret = -EFAULT;
1135 break;
1136 }
1137
1138 remain -= page_length;
1139 user_data += page_length;
1140 offset += page_length;
1141 }
1142
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001143 mutex_lock(&i915->drm.struct_mutex);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301144out_unpin:
1145 if (node.allocated) {
1146 wmb();
Chris Wilson82ad6442018-06-05 16:37:58 +01001147 ggtt->vm.clear_range(&ggtt->vm, node.start, node.size);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301148 remove_mappable_node(&node);
1149 } else {
Chris Wilson058d88c2016-08-15 10:49:06 +01001150 i915_vma_unpin(vma);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301151 }
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001152out_unlock:
1153 intel_runtime_pm_put(i915);
1154 mutex_unlock(&i915->drm.struct_mutex);
Chris Wilsonf60d7f02012-09-04 21:02:56 +01001155
Eric Anholteb014592009-03-10 11:44:52 -07001156 return ret;
1157}
1158
Eric Anholt673a3942008-07-30 12:06:12 -07001159/**
1160 * Reads data from the object referenced by handle.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01001161 * @dev: drm device pointer
1162 * @data: ioctl data blob
1163 * @file: drm file pointer
Eric Anholt673a3942008-07-30 12:06:12 -07001164 *
1165 * On error, the contents of *data are undefined.
1166 */
1167int
1168i915_gem_pread_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00001169 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001170{
1171 struct drm_i915_gem_pread *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00001172 struct drm_i915_gem_object *obj;
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001173 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07001174
Chris Wilson51311d02010-11-17 09:10:42 +00001175 if (args->size == 0)
1176 return 0;
1177
1178 if (!access_ok(VERIFY_WRITE,
Gustavo Padovan3ed605b2016-04-26 12:32:27 -03001179 u64_to_user_ptr(args->data_ptr),
Chris Wilson51311d02010-11-17 09:10:42 +00001180 args->size))
1181 return -EFAULT;
1182
Chris Wilson03ac0642016-07-20 13:31:51 +01001183 obj = i915_gem_object_lookup(file, args->handle);
Chris Wilson258a5ed2016-08-05 10:14:16 +01001184 if (!obj)
1185 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07001186
Chris Wilson7dcd2492010-09-26 20:21:44 +01001187 /* Bounds check source. */
Matthew Auld966d5bf2016-12-13 20:32:22 +00001188 if (range_overflows_t(u64, args->offset, args->size, obj->base.size)) {
Chris Wilsonce9d4192010-09-26 20:50:05 +01001189 ret = -EINVAL;
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001190 goto out;
Chris Wilsonce9d4192010-09-26 20:50:05 +01001191 }
1192
Chris Wilsondb53a302011-02-03 11:57:46 +00001193 trace_i915_gem_object_pread(obj, args->offset, args->size);
1194
Chris Wilsone95433c2016-10-28 13:58:27 +01001195 ret = i915_gem_object_wait(obj,
1196 I915_WAIT_INTERRUPTIBLE,
1197 MAX_SCHEDULE_TIMEOUT,
1198 to_rps_client(file));
Chris Wilson258a5ed2016-08-05 10:14:16 +01001199 if (ret)
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001200 goto out;
Chris Wilson258a5ed2016-08-05 10:14:16 +01001201
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001202 ret = i915_gem_object_pin_pages(obj);
Chris Wilson258a5ed2016-08-05 10:14:16 +01001203 if (ret)
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001204 goto out;
Chris Wilson258a5ed2016-08-05 10:14:16 +01001205
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001206 ret = i915_gem_shmem_pread(obj, args);
Chris Wilson9c870d02016-10-24 13:42:15 +01001207 if (ret == -EFAULT || ret == -ENODEV)
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001208 ret = i915_gem_gtt_pread(obj, args);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301209
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001210 i915_gem_object_unpin_pages(obj);
1211out:
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001212 i915_gem_object_put(obj);
Eric Anholteb014592009-03-10 11:44:52 -07001213 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07001214}
1215
Keith Packard0839ccb2008-10-30 19:38:48 -07001216/* This is the fast write path which cannot handle
1217 * page faults in the source data
Linus Torvalds9b7530cc2008-10-20 14:16:43 -07001218 */
Linus Torvalds9b7530cc2008-10-20 14:16:43 -07001219
Chris Wilsonfe115622016-10-28 13:58:40 +01001220static inline bool
1221ggtt_write(struct io_mapping *mapping,
1222 loff_t base, int offset,
1223 char __user *user_data, int length)
Keith Packard0839ccb2008-10-30 19:38:48 -07001224{
Ville Syrjäläafe722b2017-09-01 20:12:52 +03001225 void __iomem *vaddr;
Keith Packard0839ccb2008-10-30 19:38:48 -07001226 unsigned long unwritten;
1227
Ben Widawsky4f0c7cf2012-04-16 14:07:47 -07001228 /* We can use the cpu mem copy function because this is X86. */
Ville Syrjäläafe722b2017-09-01 20:12:52 +03001229 vaddr = io_mapping_map_atomic_wc(mapping, base);
1230 unwritten = __copy_from_user_inatomic_nocache((void __force *)vaddr + offset,
Keith Packard0839ccb2008-10-30 19:38:48 -07001231 user_data, length);
Chris Wilsonfe115622016-10-28 13:58:40 +01001232 io_mapping_unmap_atomic(vaddr);
1233 if (unwritten) {
Ville Syrjäläafe722b2017-09-01 20:12:52 +03001234 vaddr = io_mapping_map_wc(mapping, base, PAGE_SIZE);
1235 unwritten = copy_from_user((void __force *)vaddr + offset,
1236 user_data, length);
Chris Wilsonfe115622016-10-28 13:58:40 +01001237 io_mapping_unmap(vaddr);
1238 }
Keith Packard0839ccb2008-10-30 19:38:48 -07001239
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001240 return unwritten;
1241}
1242
Eric Anholt3de09aa2009-03-09 09:42:23 -07001243/**
1244 * This is the fast pwrite path, where we copy the data directly from the
1245 * user into the GTT, uncached.
Chris Wilsonfe115622016-10-28 13:58:40 +01001246 * @obj: i915 GEM object
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01001247 * @args: pwrite arguments structure
Eric Anholt3de09aa2009-03-09 09:42:23 -07001248 */
Eric Anholt673a3942008-07-30 12:06:12 -07001249static int
Chris Wilsonfe115622016-10-28 13:58:40 +01001250i915_gem_gtt_pwrite_fast(struct drm_i915_gem_object *obj,
1251 const struct drm_i915_gem_pwrite *args)
Eric Anholt673a3942008-07-30 12:06:12 -07001252{
Chris Wilsonfe115622016-10-28 13:58:40 +01001253 struct drm_i915_private *i915 = to_i915(obj->base.dev);
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301254 struct i915_ggtt *ggtt = &i915->ggtt;
1255 struct drm_mm_node node;
Chris Wilsonfe115622016-10-28 13:58:40 +01001256 struct i915_vma *vma;
1257 u64 remain, offset;
1258 void __user *user_data;
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301259 int ret;
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301260
Chris Wilsonfe115622016-10-28 13:58:40 +01001261 ret = mutex_lock_interruptible(&i915->drm.struct_mutex);
1262 if (ret)
1263 return ret;
Daniel Vetter935aaa62012-03-25 19:47:35 +02001264
Chris Wilson8bd818152017-10-19 07:37:33 +01001265 if (i915_gem_object_has_struct_page(obj)) {
1266 /*
1267 * Avoid waking the device up if we can fallback, as
1268 * waking/resuming is very slow (worst-case 10-100 ms
1269 * depending on PCI sleeps and our own resume time).
1270 * This easily dwarfs any performance advantage from
1271 * using the cache bypass of indirect GGTT access.
1272 */
1273 if (!intel_runtime_pm_get_if_in_use(i915)) {
1274 ret = -EFAULT;
1275 goto out_unlock;
1276 }
1277 } else {
1278 /* No backing pages, no fallback, we must force GGTT access */
1279 intel_runtime_pm_get(i915);
1280 }
1281
Chris Wilson058d88c2016-08-15 10:49:06 +01001282 vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0,
Chris Wilsona3259ca2017-10-09 09:44:00 +01001283 PIN_MAPPABLE |
1284 PIN_NONFAULT |
1285 PIN_NONBLOCK);
Chris Wilson18034582016-08-18 17:16:45 +01001286 if (!IS_ERR(vma)) {
1287 node.start = i915_ggtt_offset(vma);
1288 node.allocated = false;
Chris Wilson49ef5292016-08-18 17:17:00 +01001289 ret = i915_vma_put_fence(vma);
Chris Wilson18034582016-08-18 17:16:45 +01001290 if (ret) {
1291 i915_vma_unpin(vma);
1292 vma = ERR_PTR(ret);
1293 }
1294 }
Chris Wilson058d88c2016-08-15 10:49:06 +01001295 if (IS_ERR(vma)) {
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001296 ret = insert_mappable_node(ggtt, &node, PAGE_SIZE);
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301297 if (ret)
Chris Wilson8bd818152017-10-19 07:37:33 +01001298 goto out_rpm;
Chris Wilsonfe115622016-10-28 13:58:40 +01001299 GEM_BUG_ON(!node.allocated);
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301300 }
Daniel Vetter935aaa62012-03-25 19:47:35 +02001301
1302 ret = i915_gem_object_set_to_gtt_domain(obj, true);
1303 if (ret)
1304 goto out_unpin;
1305
Chris Wilsonfe115622016-10-28 13:58:40 +01001306 mutex_unlock(&i915->drm.struct_mutex);
1307
Chris Wilsonb19482d2016-08-18 17:16:43 +01001308 intel_fb_obj_invalidate(obj, ORIGIN_CPU);
Paulo Zanoni063e4e62015-02-13 17:23:45 -02001309
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301310 user_data = u64_to_user_ptr(args->data_ptr);
1311 offset = args->offset;
1312 remain = args->size;
1313 while (remain) {
Eric Anholt673a3942008-07-30 12:06:12 -07001314 /* Operation in this page
1315 *
Keith Packard0839ccb2008-10-30 19:38:48 -07001316 * page_base = page offset within aperture
1317 * page_offset = offset within page
1318 * page_length = bytes to copy for this page
Eric Anholt673a3942008-07-30 12:06:12 -07001319 */
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301320 u32 page_base = node.start;
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001321 unsigned int page_offset = offset_in_page(offset);
1322 unsigned int page_length = PAGE_SIZE - page_offset;
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301323 page_length = remain < page_length ? remain : page_length;
1324 if (node.allocated) {
1325 wmb(); /* flush the write before we modify the GGTT */
Chris Wilson82ad6442018-06-05 16:37:58 +01001326 ggtt->vm.insert_page(&ggtt->vm,
1327 i915_gem_object_get_dma_address(obj, offset >> PAGE_SHIFT),
1328 node.start, I915_CACHE_NONE, 0);
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301329 wmb(); /* flush modifications to the GGTT (insert_page) */
1330 } else {
1331 page_base += offset & PAGE_MASK;
1332 }
Keith Packard0839ccb2008-10-30 19:38:48 -07001333 /* If we get a fault while copying data, then (presumably) our
Eric Anholt3de09aa2009-03-09 09:42:23 -07001334 * source page isn't available. Return the error and we'll
1335 * retry in the slow path.
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301336 * If the object is non-shmem backed, we retry again with the
1337 * path that handles page fault.
Keith Packard0839ccb2008-10-30 19:38:48 -07001338 */
Matthew Auld73ebd502017-12-11 15:18:20 +00001339 if (ggtt_write(&ggtt->iomap, page_base, page_offset,
Chris Wilsonfe115622016-10-28 13:58:40 +01001340 user_data, page_length)) {
1341 ret = -EFAULT;
1342 break;
Daniel Vetter935aaa62012-03-25 19:47:35 +02001343 }
Eric Anholt673a3942008-07-30 12:06:12 -07001344
Keith Packard0839ccb2008-10-30 19:38:48 -07001345 remain -= page_length;
1346 user_data += page_length;
1347 offset += page_length;
Eric Anholt673a3942008-07-30 12:06:12 -07001348 }
Chris Wilsond59b21e2017-02-22 11:40:49 +00001349 intel_fb_obj_flush(obj, ORIGIN_CPU);
Chris Wilsonfe115622016-10-28 13:58:40 +01001350
1351 mutex_lock(&i915->drm.struct_mutex);
Daniel Vetter935aaa62012-03-25 19:47:35 +02001352out_unpin:
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301353 if (node.allocated) {
1354 wmb();
Chris Wilson82ad6442018-06-05 16:37:58 +01001355 ggtt->vm.clear_range(&ggtt->vm, node.start, node.size);
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301356 remove_mappable_node(&node);
1357 } else {
Chris Wilson058d88c2016-08-15 10:49:06 +01001358 i915_vma_unpin(vma);
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301359 }
Chris Wilson8bd818152017-10-19 07:37:33 +01001360out_rpm:
Chris Wilson9c870d02016-10-24 13:42:15 +01001361 intel_runtime_pm_put(i915);
Chris Wilson8bd818152017-10-19 07:37:33 +01001362out_unlock:
Chris Wilsonfe115622016-10-28 13:58:40 +01001363 mutex_unlock(&i915->drm.struct_mutex);
Eric Anholt3de09aa2009-03-09 09:42:23 -07001364 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07001365}
1366
Chris Wilsonfe115622016-10-28 13:58:40 +01001367/* Per-page copy function for the shmem pwrite fastpath.
1368 * Flushes invalid cachelines before writing to the target if
1369 * needs_clflush_before is set and flushes out any written cachelines after
1370 * writing if needs_clflush is set.
1371 */
Eric Anholt40123c12009-03-09 13:42:30 -07001372static int
Chris Wilsonfe115622016-10-28 13:58:40 +01001373shmem_pwrite(struct page *page, int offset, int len, char __user *user_data,
Chris Wilsonfe115622016-10-28 13:58:40 +01001374 bool needs_clflush_before,
1375 bool needs_clflush_after)
Eric Anholt40123c12009-03-09 13:42:30 -07001376{
Chris Wilsonb9d126e2019-01-05 12:07:58 +00001377 char *vaddr;
Chris Wilsonfe115622016-10-28 13:58:40 +01001378 int ret;
Eric Anholt40123c12009-03-09 13:42:30 -07001379
Chris Wilsonb9d126e2019-01-05 12:07:58 +00001380 vaddr = kmap(page);
Chris Wilsonfe115622016-10-28 13:58:40 +01001381
Chris Wilsonb9d126e2019-01-05 12:07:58 +00001382 if (needs_clflush_before)
1383 drm_clflush_virt_range(vaddr + offset, len);
Chris Wilsonfe115622016-10-28 13:58:40 +01001384
Chris Wilsonb9d126e2019-01-05 12:07:58 +00001385 ret = __copy_from_user(vaddr + offset, user_data, len);
1386 if (!ret && needs_clflush_after)
1387 drm_clflush_virt_range(vaddr + offset, len);
Chris Wilsonfe115622016-10-28 13:58:40 +01001388
Chris Wilsonb9d126e2019-01-05 12:07:58 +00001389 kunmap(page);
1390
1391 return ret ? -EFAULT : 0;
Chris Wilsonfe115622016-10-28 13:58:40 +01001392}
1393
1394static int
1395i915_gem_shmem_pwrite(struct drm_i915_gem_object *obj,
1396 const struct drm_i915_gem_pwrite *args)
1397{
1398 struct drm_i915_private *i915 = to_i915(obj->base.dev);
1399 void __user *user_data;
1400 u64 remain;
Chris Wilsonfe115622016-10-28 13:58:40 +01001401 unsigned int partial_cacheline_write;
1402 unsigned int needs_clflush;
1403 unsigned int offset, idx;
1404 int ret;
1405
1406 ret = mutex_lock_interruptible(&i915->drm.struct_mutex);
Chris Wilson43394c72016-08-18 17:16:47 +01001407 if (ret)
1408 return ret;
Eric Anholt40123c12009-03-09 13:42:30 -07001409
Chris Wilsonfe115622016-10-28 13:58:40 +01001410 ret = i915_gem_obj_prepare_shmem_write(obj, &needs_clflush);
1411 mutex_unlock(&i915->drm.struct_mutex);
1412 if (ret)
1413 return ret;
1414
Chris Wilsonfe115622016-10-28 13:58:40 +01001415 /* If we don't overwrite a cacheline completely we need to be
1416 * careful to have up-to-date data by first clflushing. Don't
1417 * overcomplicate things and flush the entire patch.
1418 */
1419 partial_cacheline_write = 0;
1420 if (needs_clflush & CLFLUSH_BEFORE)
1421 partial_cacheline_write = boot_cpu_data.x86_clflush_size - 1;
1422
Chris Wilson43394c72016-08-18 17:16:47 +01001423 user_data = u64_to_user_ptr(args->data_ptr);
Chris Wilson43394c72016-08-18 17:16:47 +01001424 remain = args->size;
Chris Wilsonfe115622016-10-28 13:58:40 +01001425 offset = offset_in_page(args->offset);
1426 for (idx = args->offset >> PAGE_SHIFT; remain; idx++) {
1427 struct page *page = i915_gem_object_get_page(obj, idx);
Chris Wilsona5e856a52018-10-12 15:02:28 +01001428 unsigned int length = min_t(u64, remain, PAGE_SIZE - offset);
Chris Wilsone5281cc2010-10-28 13:45:36 +01001429
Chris Wilsonfe115622016-10-28 13:58:40 +01001430 ret = shmem_pwrite(page, offset, length, user_data,
Chris Wilsonfe115622016-10-28 13:58:40 +01001431 (offset | length) & partial_cacheline_write,
1432 needs_clflush & CLFLUSH_AFTER);
1433 if (ret)
Chris Wilson9da3da62012-06-01 15:20:22 +01001434 break;
1435
Chris Wilsonfe115622016-10-28 13:58:40 +01001436 remain -= length;
1437 user_data += length;
1438 offset = 0;
Eric Anholt40123c12009-03-09 13:42:30 -07001439 }
1440
Chris Wilsond59b21e2017-02-22 11:40:49 +00001441 intel_fb_obj_flush(obj, ORIGIN_CPU);
Chris Wilsonfe115622016-10-28 13:58:40 +01001442 i915_gem_obj_finish_shmem_access(obj);
Eric Anholt40123c12009-03-09 13:42:30 -07001443 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07001444}
1445
1446/**
1447 * Writes data to the object referenced by handle.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01001448 * @dev: drm device
1449 * @data: ioctl data blob
1450 * @file: drm file
Eric Anholt673a3942008-07-30 12:06:12 -07001451 *
1452 * On error, the contents of the buffer that were to be modified are undefined.
1453 */
1454int
1455i915_gem_pwrite_ioctl(struct drm_device *dev, void *data,
Chris Wilsonfbd5a262010-10-14 15:03:58 +01001456 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001457{
1458 struct drm_i915_gem_pwrite *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00001459 struct drm_i915_gem_object *obj;
Chris Wilson51311d02010-11-17 09:10:42 +00001460 int ret;
1461
1462 if (args->size == 0)
1463 return 0;
1464
1465 if (!access_ok(VERIFY_READ,
Gustavo Padovan3ed605b2016-04-26 12:32:27 -03001466 u64_to_user_ptr(args->data_ptr),
Chris Wilson51311d02010-11-17 09:10:42 +00001467 args->size))
1468 return -EFAULT;
1469
Chris Wilson03ac0642016-07-20 13:31:51 +01001470 obj = i915_gem_object_lookup(file, args->handle);
Chris Wilson258a5ed2016-08-05 10:14:16 +01001471 if (!obj)
1472 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07001473
Chris Wilson7dcd2492010-09-26 20:21:44 +01001474 /* Bounds check destination. */
Matthew Auld966d5bf2016-12-13 20:32:22 +00001475 if (range_overflows_t(u64, args->offset, args->size, obj->base.size)) {
Chris Wilsonce9d4192010-09-26 20:50:05 +01001476 ret = -EINVAL;
Chris Wilson258a5ed2016-08-05 10:14:16 +01001477 goto err;
Chris Wilsonce9d4192010-09-26 20:50:05 +01001478 }
1479
Chris Wilsonf8c1cce2018-07-12 19:53:14 +01001480 /* Writes not allowed into this read-only object */
1481 if (i915_gem_object_is_readonly(obj)) {
1482 ret = -EINVAL;
1483 goto err;
1484 }
1485
Chris Wilsondb53a302011-02-03 11:57:46 +00001486 trace_i915_gem_object_pwrite(obj, args->offset, args->size);
1487
Chris Wilson7c55e2c2017-03-07 12:03:38 +00001488 ret = -ENODEV;
1489 if (obj->ops->pwrite)
1490 ret = obj->ops->pwrite(obj, args);
1491 if (ret != -ENODEV)
1492 goto err;
1493
Chris Wilsone95433c2016-10-28 13:58:27 +01001494 ret = i915_gem_object_wait(obj,
1495 I915_WAIT_INTERRUPTIBLE |
1496 I915_WAIT_ALL,
1497 MAX_SCHEDULE_TIMEOUT,
1498 to_rps_client(file));
Chris Wilson258a5ed2016-08-05 10:14:16 +01001499 if (ret)
1500 goto err;
1501
Chris Wilsonfe115622016-10-28 13:58:40 +01001502 ret = i915_gem_object_pin_pages(obj);
Chris Wilson258a5ed2016-08-05 10:14:16 +01001503 if (ret)
Chris Wilsonfe115622016-10-28 13:58:40 +01001504 goto err;
Chris Wilson258a5ed2016-08-05 10:14:16 +01001505
Daniel Vetter935aaa62012-03-25 19:47:35 +02001506 ret = -EFAULT;
Eric Anholt673a3942008-07-30 12:06:12 -07001507 /* We can only do the GTT pwrite on untiled buffers, as otherwise
1508 * it would end up going through the fenced access, and we'll get
1509 * different detiling behavior between reading and writing.
1510 * pread/pwrite currently are reading and writing from the CPU
1511 * perspective, requiring manual detiling by the client.
1512 */
Chris Wilson6eae0052016-06-20 15:05:52 +01001513 if (!i915_gem_object_has_struct_page(obj) ||
Chris Wilson9c870d02016-10-24 13:42:15 +01001514 cpu_write_needs_clflush(obj))
Daniel Vetter935aaa62012-03-25 19:47:35 +02001515 /* Note that the gtt paths might fail with non-page-backed user
1516 * pointers (e.g. gtt mappings when moving data between
Chris Wilson9c870d02016-10-24 13:42:15 +01001517 * textures). Fallback to the shmem path in that case.
1518 */
Chris Wilsonfe115622016-10-28 13:58:40 +01001519 ret = i915_gem_gtt_pwrite_fast(obj, args);
Eric Anholt673a3942008-07-30 12:06:12 -07001520
Chris Wilsond1054ee2016-07-16 18:42:36 +01001521 if (ret == -EFAULT || ret == -ENOSPC) {
Chris Wilson6a2c4232014-11-04 04:51:40 -08001522 if (obj->phys_handle)
1523 ret = i915_gem_phys_pwrite(obj, args, file);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301524 else
Chris Wilsonfe115622016-10-28 13:58:40 +01001525 ret = i915_gem_shmem_pwrite(obj, args);
Chris Wilson6a2c4232014-11-04 04:51:40 -08001526 }
Daniel Vetter5c0480f2011-12-14 13:57:30 +01001527
Chris Wilsonfe115622016-10-28 13:58:40 +01001528 i915_gem_object_unpin_pages(obj);
Chris Wilson258a5ed2016-08-05 10:14:16 +01001529err:
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001530 i915_gem_object_put(obj);
Chris Wilson258a5ed2016-08-05 10:14:16 +01001531 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07001532}
1533
Chris Wilson40e62d52016-10-28 13:58:41 +01001534static void i915_gem_object_bump_inactive_ggtt(struct drm_i915_gem_object *obj)
1535{
1536 struct drm_i915_private *i915;
1537 struct list_head *list;
1538 struct i915_vma *vma;
1539
Chris Wilsonf2123812017-10-16 12:40:37 +01001540 GEM_BUG_ON(!i915_gem_object_has_pinned_pages(obj));
1541
Chris Wilsone2189dd2017-12-07 21:14:07 +00001542 for_each_ggtt_vma(vma, obj) {
Chris Wilson40e62d52016-10-28 13:58:41 +01001543 if (i915_vma_is_active(vma))
1544 continue;
1545
1546 if (!drm_mm_node_allocated(&vma->node))
1547 continue;
1548
1549 list_move_tail(&vma->vm_link, &vma->vm->inactive_list);
1550 }
1551
1552 i915 = to_i915(obj->base.dev);
Chris Wilsonf2123812017-10-16 12:40:37 +01001553 spin_lock(&i915->mm.obj_lock);
Chris Wilson40e62d52016-10-28 13:58:41 +01001554 list = obj->bind_count ? &i915->mm.bound_list : &i915->mm.unbound_list;
Chris Wilsonf2123812017-10-16 12:40:37 +01001555 list_move_tail(&obj->mm.link, list);
1556 spin_unlock(&i915->mm.obj_lock);
Chris Wilson40e62d52016-10-28 13:58:41 +01001557}
1558
Eric Anholt673a3942008-07-30 12:06:12 -07001559/**
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001560 * Called when user space prepares to use an object with the CPU, either
1561 * through the mmap ioctl's mapping or a GTT mapping.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01001562 * @dev: drm device
1563 * @data: ioctl data blob
1564 * @file: drm file
Eric Anholt673a3942008-07-30 12:06:12 -07001565 */
1566int
1567i915_gem_set_domain_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00001568 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001569{
1570 struct drm_i915_gem_set_domain *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00001571 struct drm_i915_gem_object *obj;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001572 uint32_t read_domains = args->read_domains;
1573 uint32_t write_domain = args->write_domain;
Chris Wilson40e62d52016-10-28 13:58:41 +01001574 int err;
Eric Anholt673a3942008-07-30 12:06:12 -07001575
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001576 /* Only handle setting domains to types used by the CPU. */
Chris Wilsonb8f90962016-08-05 10:14:07 +01001577 if ((write_domain | read_domains) & I915_GEM_GPU_DOMAINS)
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001578 return -EINVAL;
1579
1580 /* Having something in the write domain implies it's in the read
1581 * domain, and only that read domain. Enforce that in the request.
1582 */
1583 if (write_domain != 0 && read_domains != write_domain)
1584 return -EINVAL;
1585
Chris Wilson03ac0642016-07-20 13:31:51 +01001586 obj = i915_gem_object_lookup(file, args->handle);
Chris Wilsonb8f90962016-08-05 10:14:07 +01001587 if (!obj)
1588 return -ENOENT;
Jesse Barnes652c3932009-08-17 13:31:43 -07001589
Chris Wilson3236f572012-08-24 09:35:09 +01001590 /* Try to flush the object off the GPU without holding the lock.
1591 * We will repeat the flush holding the lock in the normal manner
1592 * to catch cases where we are gazumped.
1593 */
Chris Wilson40e62d52016-10-28 13:58:41 +01001594 err = i915_gem_object_wait(obj,
Chris Wilsone95433c2016-10-28 13:58:27 +01001595 I915_WAIT_INTERRUPTIBLE |
Chris Wilsone9eaf822018-10-01 15:47:55 +01001596 I915_WAIT_PRIORITY |
Chris Wilsone95433c2016-10-28 13:58:27 +01001597 (write_domain ? I915_WAIT_ALL : 0),
1598 MAX_SCHEDULE_TIMEOUT,
1599 to_rps_client(file));
Chris Wilson40e62d52016-10-28 13:58:41 +01001600 if (err)
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001601 goto out;
Chris Wilsonb8f90962016-08-05 10:14:07 +01001602
Tina Zhanga03f3952017-11-14 10:25:13 +00001603 /*
1604 * Proxy objects do not control access to the backing storage, ergo
1605 * they cannot be used as a means to manipulate the cache domain
1606 * tracking for that backing storage. The proxy object is always
1607 * considered to be outside of any cache domain.
1608 */
1609 if (i915_gem_object_is_proxy(obj)) {
1610 err = -ENXIO;
1611 goto out;
1612 }
1613
1614 /*
1615 * Flush and acquire obj->pages so that we are coherent through
Chris Wilson40e62d52016-10-28 13:58:41 +01001616 * direct access in memory with previous cached writes through
1617 * shmemfs and that our cache domain tracking remains valid.
1618 * For example, if the obj->filp was moved to swap without us
1619 * being notified and releasing the pages, we would mistakenly
1620 * continue to assume that the obj remained out of the CPU cached
1621 * domain.
1622 */
1623 err = i915_gem_object_pin_pages(obj);
1624 if (err)
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001625 goto out;
Chris Wilson40e62d52016-10-28 13:58:41 +01001626
1627 err = i915_mutex_lock_interruptible(dev);
1628 if (err)
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001629 goto out_unpin;
Chris Wilson3236f572012-08-24 09:35:09 +01001630
Chris Wilsone22d8e32017-04-12 12:01:11 +01001631 if (read_domains & I915_GEM_DOMAIN_WC)
1632 err = i915_gem_object_set_to_wc_domain(obj, write_domain);
1633 else if (read_domains & I915_GEM_DOMAIN_GTT)
1634 err = i915_gem_object_set_to_gtt_domain(obj, write_domain);
Chris Wilson43566de2015-01-02 16:29:29 +05301635 else
Chris Wilsone22d8e32017-04-12 12:01:11 +01001636 err = i915_gem_object_set_to_cpu_domain(obj, write_domain);
Chris Wilson40e62d52016-10-28 13:58:41 +01001637
1638 /* And bump the LRU for this access */
1639 i915_gem_object_bump_inactive_ggtt(obj);
1640
1641 mutex_unlock(&dev->struct_mutex);
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001642
Daniel Vetter031b6982015-06-26 19:35:16 +02001643 if (write_domain != 0)
Chris Wilsonef749212017-04-12 12:01:10 +01001644 intel_fb_obj_invalidate(obj,
1645 fb_write_origin(obj, write_domain));
Daniel Vetter031b6982015-06-26 19:35:16 +02001646
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001647out_unpin:
Chris Wilson40e62d52016-10-28 13:58:41 +01001648 i915_gem_object_unpin_pages(obj);
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001649out:
1650 i915_gem_object_put(obj);
Chris Wilson40e62d52016-10-28 13:58:41 +01001651 return err;
Eric Anholt673a3942008-07-30 12:06:12 -07001652}
1653
1654/**
1655 * Called when user space has done writes to this buffer
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01001656 * @dev: drm device
1657 * @data: ioctl data blob
1658 * @file: drm file
Eric Anholt673a3942008-07-30 12:06:12 -07001659 */
1660int
1661i915_gem_sw_finish_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00001662 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001663{
1664 struct drm_i915_gem_sw_finish *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00001665 struct drm_i915_gem_object *obj;
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001666
Chris Wilson03ac0642016-07-20 13:31:51 +01001667 obj = i915_gem_object_lookup(file, args->handle);
Chris Wilsonc21724c2016-08-05 10:14:19 +01001668 if (!obj)
1669 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07001670
Tina Zhanga03f3952017-11-14 10:25:13 +00001671 /*
1672 * Proxy objects are barred from CPU access, so there is no
1673 * need to ban sw_finish as it is a nop.
1674 */
1675
Eric Anholt673a3942008-07-30 12:06:12 -07001676 /* Pinned buffers may be scanout, so flush the cache */
Chris Wilson5a97bcc2017-02-22 11:40:46 +00001677 i915_gem_object_flush_if_display(obj);
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001678 i915_gem_object_put(obj);
Chris Wilson5a97bcc2017-02-22 11:40:46 +00001679
1680 return 0;
Eric Anholt673a3942008-07-30 12:06:12 -07001681}
1682
1683/**
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01001684 * i915_gem_mmap_ioctl - Maps the contents of an object, returning the address
1685 * it is mapped to.
1686 * @dev: drm device
1687 * @data: ioctl data blob
1688 * @file: drm file
Eric Anholt673a3942008-07-30 12:06:12 -07001689 *
1690 * While the mapping holds a reference on the contents of the object, it doesn't
1691 * imply a ref on the object itself.
Daniel Vetter34367382014-10-16 12:28:18 +02001692 *
1693 * IMPORTANT:
1694 *
1695 * DRM driver writers who look a this function as an example for how to do GEM
1696 * mmap support, please don't implement mmap support like here. The modern way
1697 * to implement DRM mmap support is with an mmap offset ioctl (like
1698 * i915_gem_mmap_gtt) and then using the mmap syscall on the DRM fd directly.
1699 * That way debug tooling like valgrind will understand what's going on, hiding
1700 * the mmap call in a driver private ioctl will break that. The i915 driver only
1701 * does cpu mmaps this way because we didn't know better.
Eric Anholt673a3942008-07-30 12:06:12 -07001702 */
1703int
1704i915_gem_mmap_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00001705 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001706{
1707 struct drm_i915_gem_mmap *args = data;
Chris Wilson03ac0642016-07-20 13:31:51 +01001708 struct drm_i915_gem_object *obj;
Eric Anholt673a3942008-07-30 12:06:12 -07001709 unsigned long addr;
1710
Akash Goel1816f922015-01-02 16:29:30 +05301711 if (args->flags & ~(I915_MMAP_WC))
1712 return -EINVAL;
1713
Borislav Petkov568a58e2016-03-29 17:42:01 +02001714 if (args->flags & I915_MMAP_WC && !boot_cpu_has(X86_FEATURE_PAT))
Akash Goel1816f922015-01-02 16:29:30 +05301715 return -ENODEV;
1716
Chris Wilson03ac0642016-07-20 13:31:51 +01001717 obj = i915_gem_object_lookup(file, args->handle);
1718 if (!obj)
Chris Wilsonbf79cb92010-08-04 14:19:46 +01001719 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07001720
Daniel Vetter1286ff72012-05-10 15:25:09 +02001721 /* prime objects have no backing filp to GEM mmap
1722 * pages from.
1723 */
Chris Wilson03ac0642016-07-20 13:31:51 +01001724 if (!obj->base.filp) {
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001725 i915_gem_object_put(obj);
Tina Zhang274b2462017-11-14 10:25:12 +00001726 return -ENXIO;
Daniel Vetter1286ff72012-05-10 15:25:09 +02001727 }
1728
Chris Wilson03ac0642016-07-20 13:31:51 +01001729 addr = vm_mmap(obj->base.filp, 0, args->size,
Eric Anholt673a3942008-07-30 12:06:12 -07001730 PROT_READ | PROT_WRITE, MAP_SHARED,
1731 args->offset);
Akash Goel1816f922015-01-02 16:29:30 +05301732 if (args->flags & I915_MMAP_WC) {
1733 struct mm_struct *mm = current->mm;
1734 struct vm_area_struct *vma;
1735
Michal Hocko80a89a52016-05-23 16:26:11 -07001736 if (down_write_killable(&mm->mmap_sem)) {
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001737 i915_gem_object_put(obj);
Michal Hocko80a89a52016-05-23 16:26:11 -07001738 return -EINTR;
1739 }
Akash Goel1816f922015-01-02 16:29:30 +05301740 vma = find_vma(mm, addr);
1741 if (vma)
1742 vma->vm_page_prot =
1743 pgprot_writecombine(vm_get_page_prot(vma->vm_flags));
1744 else
1745 addr = -ENOMEM;
1746 up_write(&mm->mmap_sem);
Chris Wilsonaeecc962016-06-17 14:46:39 -03001747
1748 /* This may race, but that's ok, it only gets set */
Chris Wilson50349242016-08-18 17:17:04 +01001749 WRITE_ONCE(obj->frontbuffer_ggtt_origin, ORIGIN_CPU);
Akash Goel1816f922015-01-02 16:29:30 +05301750 }
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001751 i915_gem_object_put(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001752 if (IS_ERR((void *)addr))
1753 return addr;
1754
1755 args->addr_ptr = (uint64_t) addr;
1756
1757 return 0;
1758}
1759
Chris Wilsond899ace2018-07-25 16:54:47 +01001760static unsigned int tile_row_pages(const struct drm_i915_gem_object *obj)
Chris Wilson03af84f2016-08-18 17:17:01 +01001761{
Chris Wilson6649a0b2017-01-09 16:16:08 +00001762 return i915_gem_object_get_tile_row_size(obj) >> PAGE_SHIFT;
Chris Wilson03af84f2016-08-18 17:17:01 +01001763}
1764
Jesse Barnesde151cf2008-11-12 10:03:55 -08001765/**
Chris Wilson4cc69072016-08-25 19:05:19 +01001766 * i915_gem_mmap_gtt_version - report the current feature set for GTT mmaps
1767 *
1768 * A history of the GTT mmap interface:
1769 *
1770 * 0 - Everything had to fit into the GTT. Both parties of a memcpy had to
1771 * aligned and suitable for fencing, and still fit into the available
1772 * mappable space left by the pinned display objects. A classic problem
1773 * we called the page-fault-of-doom where we would ping-pong between
1774 * two objects that could not fit inside the GTT and so the memcpy
1775 * would page one object in at the expense of the other between every
1776 * single byte.
1777 *
1778 * 1 - Objects can be any size, and have any compatible fencing (X Y, or none
1779 * as set via i915_gem_set_tiling() [DRM_I915_GEM_SET_TILING]). If the
1780 * object is too large for the available space (or simply too large
1781 * for the mappable aperture!), a view is created instead and faulted
1782 * into userspace. (This view is aligned and sized appropriately for
1783 * fenced access.)
1784 *
Chris Wilsone22d8e32017-04-12 12:01:11 +01001785 * 2 - Recognise WC as a separate cache domain so that we can flush the
1786 * delayed writes via GTT before performing direct access via WC.
1787 *
Chris Wilson4cc69072016-08-25 19:05:19 +01001788 * Restrictions:
1789 *
1790 * * snoopable objects cannot be accessed via the GTT. It can cause machine
1791 * hangs on some architectures, corruption on others. An attempt to service
1792 * a GTT page fault from a snoopable object will generate a SIGBUS.
1793 *
1794 * * the object must be able to fit into RAM (physical memory, though no
1795 * limited to the mappable aperture).
1796 *
1797 *
1798 * Caveats:
1799 *
1800 * * a new GTT page fault will synchronize rendering from the GPU and flush
1801 * all data to system memory. Subsequent access will not be synchronized.
1802 *
1803 * * all mappings are revoked on runtime device suspend.
1804 *
1805 * * there are only 8, 16 or 32 fence registers to share between all users
1806 * (older machines require fence register for display and blitter access
1807 * as well). Contention of the fence registers will cause the previous users
1808 * to be unmapped and any new access will generate new page faults.
1809 *
1810 * * running out of memory while servicing a fault may generate a SIGBUS,
1811 * rather than the expected SIGSEGV.
1812 */
1813int i915_gem_mmap_gtt_version(void)
1814{
Chris Wilsone22d8e32017-04-12 12:01:11 +01001815 return 2;
Chris Wilson4cc69072016-08-25 19:05:19 +01001816}
1817
Chris Wilson2d4281b2017-01-10 09:56:32 +00001818static inline struct i915_ggtt_view
Chris Wilsond899ace2018-07-25 16:54:47 +01001819compute_partial_view(const struct drm_i915_gem_object *obj,
Chris Wilson2d4281b2017-01-10 09:56:32 +00001820 pgoff_t page_offset,
1821 unsigned int chunk)
1822{
1823 struct i915_ggtt_view view;
1824
1825 if (i915_gem_object_is_tiled(obj))
1826 chunk = roundup(chunk, tile_row_pages(obj));
1827
Chris Wilson2d4281b2017-01-10 09:56:32 +00001828 view.type = I915_GGTT_VIEW_PARTIAL;
Chris Wilson8bab11932017-01-14 00:28:25 +00001829 view.partial.offset = rounddown(page_offset, chunk);
1830 view.partial.size =
Chris Wilson2d4281b2017-01-10 09:56:32 +00001831 min_t(unsigned int, chunk,
Chris Wilson8bab11932017-01-14 00:28:25 +00001832 (obj->base.size >> PAGE_SHIFT) - view.partial.offset);
Chris Wilson2d4281b2017-01-10 09:56:32 +00001833
1834 /* If the partial covers the entire object, just create a normal VMA. */
1835 if (chunk >= obj->base.size >> PAGE_SHIFT)
1836 view.type = I915_GGTT_VIEW_NORMAL;
1837
1838 return view;
1839}
1840
Chris Wilson4cc69072016-08-25 19:05:19 +01001841/**
Jesse Barnesde151cf2008-11-12 10:03:55 -08001842 * i915_gem_fault - fault a page into the GTT
Geliang Tangd9072a32015-09-15 05:58:44 -07001843 * @vmf: fault info
Jesse Barnesde151cf2008-11-12 10:03:55 -08001844 *
1845 * The fault handler is set up by drm_gem_mmap() when a object is GTT mapped
1846 * from userspace. The fault handler takes care of binding the object to
1847 * the GTT (if needed), allocating and programming a fence register (again,
1848 * only if needed based on whether the old reg is still valid or the object
1849 * is tiled) and inserting a new PTE into the faulting process.
1850 *
1851 * Note that the faulting process may involve evicting existing objects
1852 * from the GTT and/or fence registers to make room. So performance may
1853 * suffer if the GTT working set is large or there are few fence registers
1854 * left.
Chris Wilson4cc69072016-08-25 19:05:19 +01001855 *
1856 * The current feature set supported by i915_gem_fault() and thus GTT mmaps
1857 * is exposed via I915_PARAM_MMAP_GTT_VERSION (see i915_gem_mmap_gtt_version).
Jesse Barnesde151cf2008-11-12 10:03:55 -08001858 */
Chris Wilson52137012018-06-06 22:45:20 +01001859vm_fault_t i915_gem_fault(struct vm_fault *vmf)
Jesse Barnesde151cf2008-11-12 10:03:55 -08001860{
Chris Wilson420980c2018-06-05 14:57:46 +01001861#define MIN_CHUNK_PAGES (SZ_1M >> PAGE_SHIFT)
Dave Jiang11bac802017-02-24 14:56:41 -08001862 struct vm_area_struct *area = vmf->vma;
Chris Wilson058d88c2016-08-15 10:49:06 +01001863 struct drm_i915_gem_object *obj = to_intel_bo(area->vm_private_data);
Chris Wilson05394f32010-11-08 19:18:58 +00001864 struct drm_device *dev = obj->base.dev;
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03001865 struct drm_i915_private *dev_priv = to_i915(dev);
1866 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Chris Wilsonaae7c062018-09-03 09:33:34 +01001867 bool write = area->vm_flags & VM_WRITE;
Chris Wilson058d88c2016-08-15 10:49:06 +01001868 struct i915_vma *vma;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001869 pgoff_t page_offset;
Chris Wilsonb8f90962016-08-05 10:14:07 +01001870 int ret;
Paulo Zanonif65c9162013-11-27 18:20:34 -02001871
Chris Wilson3e977ac2018-07-12 19:53:13 +01001872 /* Sanity check that we allow writing into this object */
1873 if (i915_gem_object_is_readonly(obj) && write)
1874 return VM_FAULT_SIGBUS;
1875
Jesse Barnesde151cf2008-11-12 10:03:55 -08001876 /* We don't use vmf->pgoff since that has the fake offset */
Jan Kara1a29d852016-12-14 15:07:01 -08001877 page_offset = (vmf->address - area->vm_start) >> PAGE_SHIFT;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001878
Chris Wilsondb53a302011-02-03 11:57:46 +00001879 trace_i915_gem_object_fault(obj, page_offset, true, write);
1880
Chris Wilson6e4930f2014-02-07 18:37:06 -02001881 /* Try to flush the object off the GPU first without holding the lock.
Chris Wilsonb8f90962016-08-05 10:14:07 +01001882 * Upon acquiring the lock, we will perform our sanity checks and then
Chris Wilson6e4930f2014-02-07 18:37:06 -02001883 * repeat the flush holding the lock in the normal manner to catch cases
1884 * where we are gazumped.
1885 */
Chris Wilsone95433c2016-10-28 13:58:27 +01001886 ret = i915_gem_object_wait(obj,
1887 I915_WAIT_INTERRUPTIBLE,
1888 MAX_SCHEDULE_TIMEOUT,
1889 NULL);
Chris Wilson6e4930f2014-02-07 18:37:06 -02001890 if (ret)
Chris Wilsonb8f90962016-08-05 10:14:07 +01001891 goto err;
1892
Chris Wilson40e62d52016-10-28 13:58:41 +01001893 ret = i915_gem_object_pin_pages(obj);
1894 if (ret)
1895 goto err;
1896
Chris Wilsonb8f90962016-08-05 10:14:07 +01001897 intel_runtime_pm_get(dev_priv);
1898
1899 ret = i915_mutex_lock_interruptible(dev);
1900 if (ret)
1901 goto err_rpm;
Chris Wilson6e4930f2014-02-07 18:37:06 -02001902
Chris Wilsoneb119bd2012-12-16 12:43:36 +00001903 /* Access to snoopable pages through the GTT is incoherent. */
Tvrtko Ursulin0031fb92016-11-04 14:42:44 +00001904 if (obj->cache_level != I915_CACHE_NONE && !HAS_LLC(dev_priv)) {
Chris Wilsonddeff6e2014-05-28 16:16:41 +01001905 ret = -EFAULT;
Chris Wilsonb8f90962016-08-05 10:14:07 +01001906 goto err_unlock;
Chris Wilsoneb119bd2012-12-16 12:43:36 +00001907 }
1908
Chris Wilson82118872016-08-18 17:17:05 +01001909
Chris Wilsona61007a2016-08-18 17:17:02 +01001910 /* Now pin it into the GTT as needed */
Chris Wilson7e7367d2018-06-30 10:05:09 +01001911 vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0,
1912 PIN_MAPPABLE |
1913 PIN_NONBLOCK |
1914 PIN_NONFAULT);
Chris Wilsona61007a2016-08-18 17:17:02 +01001915 if (IS_ERR(vma)) {
Chris Wilsona61007a2016-08-18 17:17:02 +01001916 /* Use a partial view if it is bigger than available space */
Chris Wilson2d4281b2017-01-10 09:56:32 +00001917 struct i915_ggtt_view view =
Chris Wilson8201c1f2017-01-10 09:56:33 +00001918 compute_partial_view(obj, page_offset, MIN_CHUNK_PAGES);
Chris Wilson7e7367d2018-06-30 10:05:09 +01001919 unsigned int flags;
Chris Wilsonaa136d92016-08-18 17:17:03 +01001920
Chris Wilson7e7367d2018-06-30 10:05:09 +01001921 flags = PIN_MAPPABLE;
1922 if (view.type == I915_GGTT_VIEW_NORMAL)
1923 flags |= PIN_NONBLOCK; /* avoid warnings for pinned */
1924
1925 /*
1926 * Userspace is now writing through an untracked VMA, abandon
Chris Wilson50349242016-08-18 17:17:04 +01001927 * all hope that the hardware is able to track future writes.
1928 */
1929 obj->frontbuffer_ggtt_origin = ORIGIN_CPU;
1930
Chris Wilson7e7367d2018-06-30 10:05:09 +01001931 vma = i915_gem_object_ggtt_pin(obj, &view, 0, 0, flags);
1932 if (IS_ERR(vma) && !view.type) {
1933 flags = PIN_MAPPABLE;
1934 view.type = I915_GGTT_VIEW_PARTIAL;
1935 vma = i915_gem_object_ggtt_pin(obj, &view, 0, 0, flags);
1936 }
Chris Wilsona61007a2016-08-18 17:17:02 +01001937 }
Chris Wilson058d88c2016-08-15 10:49:06 +01001938 if (IS_ERR(vma)) {
1939 ret = PTR_ERR(vma);
Chris Wilsonb8f90962016-08-05 10:14:07 +01001940 goto err_unlock;
Chris Wilson058d88c2016-08-15 10:49:06 +01001941 }
Jesse Barnesde151cf2008-11-12 10:03:55 -08001942
Chris Wilsonc9839302012-11-20 10:45:17 +00001943 ret = i915_gem_object_set_to_gtt_domain(obj, write);
1944 if (ret)
Chris Wilsonb8f90962016-08-05 10:14:07 +01001945 goto err_unpin;
Chris Wilsonc9839302012-11-20 10:45:17 +00001946
Chris Wilson3bd40732017-10-09 09:43:56 +01001947 ret = i915_vma_pin_fence(vma);
Chris Wilsonc9839302012-11-20 10:45:17 +00001948 if (ret)
Chris Wilsonb8f90962016-08-05 10:14:07 +01001949 goto err_unpin;
Chris Wilson7d1c4802010-08-07 21:45:03 +01001950
Chris Wilsonb90b91d2014-06-10 12:14:40 +01001951 /* Finally, remap it using the new GTT offset */
Chris Wilsonc58305a2016-08-19 16:54:28 +01001952 ret = remap_io_mapping(area,
Chris Wilson8bab11932017-01-14 00:28:25 +00001953 area->vm_start + (vma->ggtt_view.partial.offset << PAGE_SHIFT),
Matthew Auld73ebd502017-12-11 15:18:20 +00001954 (ggtt->gmadr.start + vma->node.start) >> PAGE_SHIFT,
Chris Wilsonc58305a2016-08-19 16:54:28 +01001955 min_t(u64, vma->size, area->vm_end - area->vm_start),
Matthew Auld73ebd502017-12-11 15:18:20 +00001956 &ggtt->iomap);
Chris Wilsona65adaf2017-10-09 09:43:57 +01001957 if (ret)
1958 goto err_fence;
Chris Wilsona61007a2016-08-18 17:17:02 +01001959
Chris Wilsona65adaf2017-10-09 09:43:57 +01001960 /* Mark as being mmapped into userspace for later revocation */
1961 assert_rpm_wakelock_held(dev_priv);
1962 if (!i915_vma_set_userfault(vma) && !obj->userfault_count++)
1963 list_add(&obj->userfault_link, &dev_priv->mm.userfault_list);
1964 GEM_BUG_ON(!obj->userfault_count);
1965
Chris Wilson7125397b2017-12-06 12:49:14 +00001966 i915_vma_set_ggtt_write(vma);
1967
Chris Wilsona65adaf2017-10-09 09:43:57 +01001968err_fence:
Chris Wilson3bd40732017-10-09 09:43:56 +01001969 i915_vma_unpin_fence(vma);
Chris Wilsonb8f90962016-08-05 10:14:07 +01001970err_unpin:
Chris Wilson058d88c2016-08-15 10:49:06 +01001971 __i915_vma_unpin(vma);
Chris Wilsonb8f90962016-08-05 10:14:07 +01001972err_unlock:
Jesse Barnesde151cf2008-11-12 10:03:55 -08001973 mutex_unlock(&dev->struct_mutex);
Chris Wilsonb8f90962016-08-05 10:14:07 +01001974err_rpm:
1975 intel_runtime_pm_put(dev_priv);
Chris Wilson40e62d52016-10-28 13:58:41 +01001976 i915_gem_object_unpin_pages(obj);
Chris Wilsonb8f90962016-08-05 10:14:07 +01001977err:
Jesse Barnesde151cf2008-11-12 10:03:55 -08001978 switch (ret) {
Chris Wilsond9bc7e92011-02-07 13:09:31 +00001979 case -EIO:
Daniel Vetter2232f032014-09-04 09:36:18 +02001980 /*
1981 * We eat errors when the gpu is terminally wedged to avoid
1982 * userspace unduly crashing (gl has no provisions for mmaps to
1983 * fail). But any other -EIO isn't ours (e.g. swap in failure)
1984 * and so needs to be reported.
1985 */
Chris Wilson52137012018-06-06 22:45:20 +01001986 if (!i915_terminally_wedged(&dev_priv->gpu_error))
1987 return VM_FAULT_SIGBUS;
Gustavo A. R. Silvaf0d759f2018-06-28 17:35:41 -05001988 /* else: fall through */
Chris Wilson045e7692010-11-07 09:18:22 +00001989 case -EAGAIN:
Daniel Vetter571c6082013-09-12 17:57:28 +02001990 /*
1991 * EAGAIN means the gpu is hung and we'll wait for the error
1992 * handler to reset everything when re-faulting in
1993 * i915_mutex_lock_interruptible.
Chris Wilsond9bc7e92011-02-07 13:09:31 +00001994 */
Chris Wilsonc7150892009-09-23 00:43:56 +01001995 case 0:
1996 case -ERESTARTSYS:
Chris Wilsonbed636a2011-02-11 20:31:19 +00001997 case -EINTR:
Dmitry Rogozhkine79e0fe2012-10-03 17:15:26 +03001998 case -EBUSY:
1999 /*
2000 * EBUSY is ok: this just means that another thread
2001 * already did the job.
2002 */
Chris Wilson52137012018-06-06 22:45:20 +01002003 return VM_FAULT_NOPAGE;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002004 case -ENOMEM:
Chris Wilson52137012018-06-06 22:45:20 +01002005 return VM_FAULT_OOM;
Daniel Vettera7c2e1a2012-10-17 11:17:16 +02002006 case -ENOSPC:
Chris Wilson45d67812014-01-31 11:34:57 +00002007 case -EFAULT:
Chris Wilson52137012018-06-06 22:45:20 +01002008 return VM_FAULT_SIGBUS;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002009 default:
Daniel Vettera7c2e1a2012-10-17 11:17:16 +02002010 WARN_ONCE(ret, "unhandled error in i915_gem_fault: %i\n", ret);
Chris Wilson52137012018-06-06 22:45:20 +01002011 return VM_FAULT_SIGBUS;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002012 }
2013}
2014
Chris Wilsona65adaf2017-10-09 09:43:57 +01002015static void __i915_gem_object_release_mmap(struct drm_i915_gem_object *obj)
2016{
2017 struct i915_vma *vma;
2018
2019 GEM_BUG_ON(!obj->userfault_count);
2020
2021 obj->userfault_count = 0;
2022 list_del(&obj->userfault_link);
2023 drm_vma_node_unmap(&obj->base.vma_node,
2024 obj->base.dev->anon_inode->i_mapping);
2025
Chris Wilsone2189dd2017-12-07 21:14:07 +00002026 for_each_ggtt_vma(vma, obj)
Chris Wilsona65adaf2017-10-09 09:43:57 +01002027 i915_vma_unset_userfault(vma);
Chris Wilsona65adaf2017-10-09 09:43:57 +01002028}
2029
Jesse Barnesde151cf2008-11-12 10:03:55 -08002030/**
Chris Wilson901782b2009-07-10 08:18:50 +01002031 * i915_gem_release_mmap - remove physical page mappings
2032 * @obj: obj in question
2033 *
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02002034 * Preserve the reservation of the mmapping with the DRM core code, but
Chris Wilson901782b2009-07-10 08:18:50 +01002035 * relinquish ownership of the pages back to the system.
2036 *
2037 * It is vital that we remove the page mapping if we have mapped a tiled
2038 * object through the GTT and then lose the fence register due to
2039 * resource pressure. Similarly if the object has been moved out of the
2040 * aperture, than pages mapped into userspace must be revoked. Removing the
2041 * mapping will then trigger a page fault on the next user access, allowing
2042 * fixup by i915_gem_fault().
2043 */
Eric Anholtd05ca302009-07-10 13:02:26 -07002044void
Chris Wilson05394f32010-11-08 19:18:58 +00002045i915_gem_release_mmap(struct drm_i915_gem_object *obj)
Chris Wilson901782b2009-07-10 08:18:50 +01002046{
Chris Wilson275f0392016-10-24 13:42:14 +01002047 struct drm_i915_private *i915 = to_i915(obj->base.dev);
Chris Wilson275f0392016-10-24 13:42:14 +01002048
Chris Wilson349f2cc2016-04-13 17:35:12 +01002049 /* Serialisation between user GTT access and our code depends upon
2050 * revoking the CPU's PTE whilst the mutex is held. The next user
2051 * pagefault then has to wait until we release the mutex.
Chris Wilson9c870d02016-10-24 13:42:15 +01002052 *
2053 * Note that RPM complicates somewhat by adding an additional
2054 * requirement that operations to the GGTT be made holding the RPM
2055 * wakeref.
Chris Wilson349f2cc2016-04-13 17:35:12 +01002056 */
Chris Wilson275f0392016-10-24 13:42:14 +01002057 lockdep_assert_held(&i915->drm.struct_mutex);
Chris Wilson9c870d02016-10-24 13:42:15 +01002058 intel_runtime_pm_get(i915);
Chris Wilson349f2cc2016-04-13 17:35:12 +01002059
Chris Wilsona65adaf2017-10-09 09:43:57 +01002060 if (!obj->userfault_count)
Chris Wilson9c870d02016-10-24 13:42:15 +01002061 goto out;
Chris Wilson901782b2009-07-10 08:18:50 +01002062
Chris Wilsona65adaf2017-10-09 09:43:57 +01002063 __i915_gem_object_release_mmap(obj);
Chris Wilson349f2cc2016-04-13 17:35:12 +01002064
2065 /* Ensure that the CPU's PTE are revoked and there are not outstanding
2066 * memory transactions from userspace before we return. The TLB
2067 * flushing implied above by changing the PTE above *should* be
2068 * sufficient, an extra barrier here just provides us with a bit
2069 * of paranoid documentation about our requirement to serialise
2070 * memory writes before touching registers / GSM.
2071 */
2072 wmb();
Chris Wilson9c870d02016-10-24 13:42:15 +01002073
2074out:
2075 intel_runtime_pm_put(i915);
Chris Wilson901782b2009-07-10 08:18:50 +01002076}
2077
Chris Wilson7c108fd2016-10-24 13:42:18 +01002078void i915_gem_runtime_suspend(struct drm_i915_private *dev_priv)
Chris Wilsoneedd10f2014-06-16 08:57:44 +01002079{
Chris Wilson3594a3e2016-10-24 13:42:16 +01002080 struct drm_i915_gem_object *obj, *on;
Chris Wilson7c108fd2016-10-24 13:42:18 +01002081 int i;
Chris Wilsoneedd10f2014-06-16 08:57:44 +01002082
Chris Wilson3594a3e2016-10-24 13:42:16 +01002083 /*
2084 * Only called during RPM suspend. All users of the userfault_list
2085 * must be holding an RPM wakeref to ensure that this can not
2086 * run concurrently with themselves (and use the struct_mutex for
2087 * protection between themselves).
2088 */
2089
2090 list_for_each_entry_safe(obj, on,
Chris Wilsona65adaf2017-10-09 09:43:57 +01002091 &dev_priv->mm.userfault_list, userfault_link)
2092 __i915_gem_object_release_mmap(obj);
Chris Wilson7c108fd2016-10-24 13:42:18 +01002093
2094 /* The fence will be lost when the device powers down. If any were
2095 * in use by hardware (i.e. they are pinned), we should not be powering
2096 * down! All other fences will be reacquired by the user upon waking.
2097 */
2098 for (i = 0; i < dev_priv->num_fence_regs; i++) {
2099 struct drm_i915_fence_reg *reg = &dev_priv->fence_regs[i];
2100
Chris Wilsone0ec3ec2017-02-03 12:57:17 +00002101 /* Ideally we want to assert that the fence register is not
2102 * live at this point (i.e. that no piece of code will be
2103 * trying to write through fence + GTT, as that both violates
2104 * our tracking of activity and associated locking/barriers,
2105 * but also is illegal given that the hw is powered down).
2106 *
2107 * Previously we used reg->pin_count as a "liveness" indicator.
2108 * That is not sufficient, and we need a more fine-grained
2109 * tool if we want to have a sanity check here.
2110 */
Chris Wilson7c108fd2016-10-24 13:42:18 +01002111
2112 if (!reg->vma)
2113 continue;
2114
Chris Wilsona65adaf2017-10-09 09:43:57 +01002115 GEM_BUG_ON(i915_vma_has_userfault(reg->vma));
Chris Wilson7c108fd2016-10-24 13:42:18 +01002116 reg->dirty = true;
2117 }
Chris Wilsoneedd10f2014-06-16 08:57:44 +01002118}
2119
Chris Wilsond8cb5082012-08-11 15:41:03 +01002120static int i915_gem_object_create_mmap_offset(struct drm_i915_gem_object *obj)
2121{
Chris Wilsonfac5e232016-07-04 11:34:36 +01002122 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
Chris Wilsonf3f61842016-08-05 10:14:14 +01002123 int err;
Chris Wilsond8cb5082012-08-11 15:41:03 +01002124
Chris Wilsonf3f61842016-08-05 10:14:14 +01002125 err = drm_gem_create_mmap_offset(&obj->base);
Chris Wilsonb42a13d2017-01-06 15:22:40 +00002126 if (likely(!err))
Chris Wilsonf3f61842016-08-05 10:14:14 +01002127 return 0;
Daniel Vetterda494d72012-12-20 15:11:16 +01002128
Chris Wilsonb42a13d2017-01-06 15:22:40 +00002129 /* Attempt to reap some mmap space from dead objects */
2130 do {
Chris Wilsonec625fb2018-07-09 13:20:42 +01002131 err = i915_gem_wait_for_idle(dev_priv,
2132 I915_WAIT_INTERRUPTIBLE,
2133 MAX_SCHEDULE_TIMEOUT);
Chris Wilsonb42a13d2017-01-06 15:22:40 +00002134 if (err)
2135 break;
Chris Wilsond8cb5082012-08-11 15:41:03 +01002136
Chris Wilsonb42a13d2017-01-06 15:22:40 +00002137 i915_gem_drain_freed_objects(dev_priv);
Chris Wilsonf3f61842016-08-05 10:14:14 +01002138 err = drm_gem_create_mmap_offset(&obj->base);
Chris Wilsonb42a13d2017-01-06 15:22:40 +00002139 if (!err)
2140 break;
2141
2142 } while (flush_delayed_work(&dev_priv->gt.retire_work));
Daniel Vetterda494d72012-12-20 15:11:16 +01002143
Chris Wilsonf3f61842016-08-05 10:14:14 +01002144 return err;
Chris Wilsond8cb5082012-08-11 15:41:03 +01002145}
2146
2147static void i915_gem_object_free_mmap_offset(struct drm_i915_gem_object *obj)
2148{
Chris Wilsond8cb5082012-08-11 15:41:03 +01002149 drm_gem_free_mmap_offset(&obj->base);
2150}
2151
Dave Airlieda6b51d2014-12-24 13:11:17 +10002152int
Dave Airlieff72145b2011-02-07 12:16:14 +10002153i915_gem_mmap_gtt(struct drm_file *file,
2154 struct drm_device *dev,
Dave Airlieda6b51d2014-12-24 13:11:17 +10002155 uint32_t handle,
Dave Airlieff72145b2011-02-07 12:16:14 +10002156 uint64_t *offset)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002157{
Chris Wilson05394f32010-11-08 19:18:58 +00002158 struct drm_i915_gem_object *obj;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002159 int ret;
2160
Chris Wilson03ac0642016-07-20 13:31:51 +01002161 obj = i915_gem_object_lookup(file, handle);
Chris Wilsonf3f61842016-08-05 10:14:14 +01002162 if (!obj)
2163 return -ENOENT;
Chris Wilsonab182822009-09-22 18:46:17 +01002164
Chris Wilsond8cb5082012-08-11 15:41:03 +01002165 ret = i915_gem_object_create_mmap_offset(obj);
Chris Wilsonf3f61842016-08-05 10:14:14 +01002166 if (ret == 0)
2167 *offset = drm_vma_node_offset_addr(&obj->base.vma_node);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002168
Chris Wilsonf0cd5182016-10-28 13:58:43 +01002169 i915_gem_object_put(obj);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01002170 return ret;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002171}
2172
Dave Airlieff72145b2011-02-07 12:16:14 +10002173/**
2174 * i915_gem_mmap_gtt_ioctl - prepare an object for GTT mmap'ing
2175 * @dev: DRM device
2176 * @data: GTT mapping ioctl data
2177 * @file: GEM object info
2178 *
2179 * Simply returns the fake offset to userspace so it can mmap it.
2180 * The mmap call will end up in drm_gem_mmap(), which will set things
2181 * up so we can get faults in the handler above.
2182 *
2183 * The fault handler will take care of binding the object into the GTT
2184 * (since it may have been evicted to make room for something), allocating
2185 * a fence register, and mapping the appropriate aperture address into
2186 * userspace.
2187 */
2188int
2189i915_gem_mmap_gtt_ioctl(struct drm_device *dev, void *data,
2190 struct drm_file *file)
2191{
2192 struct drm_i915_gem_mmap_gtt *args = data;
2193
Dave Airlieda6b51d2014-12-24 13:11:17 +10002194 return i915_gem_mmap_gtt(file, dev, args->handle, &args->offset);
Dave Airlieff72145b2011-02-07 12:16:14 +10002195}
2196
Daniel Vetter225067e2012-08-20 10:23:20 +02002197/* Immediately discard the backing storage */
2198static void
2199i915_gem_object_truncate(struct drm_i915_gem_object *obj)
Chris Wilsone5281cc2010-10-28 13:45:36 +01002200{
Chris Wilson4d6294bf2012-08-11 15:41:05 +01002201 i915_gem_object_free_mmap_offset(obj);
Daniel Vetter1286ff72012-05-10 15:25:09 +02002202
Chris Wilson4d6294bf2012-08-11 15:41:05 +01002203 if (obj->base.filp == NULL)
2204 return;
2205
Daniel Vetter225067e2012-08-20 10:23:20 +02002206 /* Our goal here is to return as much of the memory as
2207 * is possible back to the system as we are called from OOM.
2208 * To do this we must instruct the shmfs to drop all of its
2209 * backing pages, *now*.
Chris Wilsone5281cc2010-10-28 13:45:36 +01002210 */
Chris Wilson55372522014-03-25 13:23:06 +00002211 shmem_truncate_range(file_inode(obj->base.filp), 0, (loff_t)-1);
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002212 obj->mm.madv = __I915_MADV_PURGED;
Chris Wilson4e5462e2017-03-07 13:20:31 +00002213 obj->mm.pages = ERR_PTR(-EFAULT);
Chris Wilsone5281cc2010-10-28 13:45:36 +01002214}
Chris Wilsone5281cc2010-10-28 13:45:36 +01002215
Chris Wilson55372522014-03-25 13:23:06 +00002216/* Try to discard unwanted pages */
Chris Wilson03ac84f2016-10-28 13:58:36 +01002217void __i915_gem_object_invalidate(struct drm_i915_gem_object *obj)
Daniel Vetter225067e2012-08-20 10:23:20 +02002218{
Chris Wilson55372522014-03-25 13:23:06 +00002219 struct address_space *mapping;
2220
Chris Wilson1233e2d2016-10-28 13:58:37 +01002221 lockdep_assert_held(&obj->mm.lock);
Chris Wilsonf1fa4f42017-10-13 21:26:13 +01002222 GEM_BUG_ON(i915_gem_object_has_pages(obj));
Chris Wilson1233e2d2016-10-28 13:58:37 +01002223
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002224 switch (obj->mm.madv) {
Chris Wilson55372522014-03-25 13:23:06 +00002225 case I915_MADV_DONTNEED:
2226 i915_gem_object_truncate(obj);
2227 case __I915_MADV_PURGED:
2228 return;
2229 }
2230
2231 if (obj->base.filp == NULL)
2232 return;
2233
Al Viro93c76a32015-12-04 23:45:44 -05002234 mapping = obj->base.filp->f_mapping,
Chris Wilson55372522014-03-25 13:23:06 +00002235 invalidate_mapping_pages(mapping, 0, (loff_t)-1);
Chris Wilsone5281cc2010-10-28 13:45:36 +01002236}
2237
Kuo-Hsin Yang64e3d122018-11-06 13:23:24 +00002238/*
2239 * Move pages to appropriate lru and release the pagevec, decrementing the
2240 * ref count of those pages.
2241 */
2242static void check_release_pagevec(struct pagevec *pvec)
2243{
2244 check_move_unevictable_pages(pvec);
2245 __pagevec_release(pvec);
2246 cond_resched();
2247}
2248
Chris Wilson5cdf5882010-09-27 15:51:07 +01002249static void
Chris Wilson03ac84f2016-10-28 13:58:36 +01002250i915_gem_object_put_pages_gtt(struct drm_i915_gem_object *obj,
2251 struct sg_table *pages)
Eric Anholt673a3942008-07-30 12:06:12 -07002252{
Dave Gordon85d12252016-05-20 11:54:06 +01002253 struct sgt_iter sgt_iter;
Kuo-Hsin Yang64e3d122018-11-06 13:23:24 +00002254 struct pagevec pvec;
Dave Gordon85d12252016-05-20 11:54:06 +01002255 struct page *page;
Daniel Vetter1286ff72012-05-10 15:25:09 +02002256
Chris Wilsone5facdf2016-12-23 14:57:57 +00002257 __i915_gem_object_release_shmem(obj, pages, true);
Eric Anholt856fa192009-03-19 14:10:50 -07002258
Chris Wilson03ac84f2016-10-28 13:58:36 +01002259 i915_gem_gtt_finish_pages(obj, pages);
Imre Deake2273302015-07-09 12:59:05 +03002260
Daniel Vetter6dacfd22011-09-12 21:30:02 +02002261 if (i915_gem_object_needs_bit17_swizzle(obj))
Chris Wilson03ac84f2016-10-28 13:58:36 +01002262 i915_gem_object_save_bit_17_swizzle(obj, pages);
Eric Anholt280b7132009-03-12 16:56:27 -07002263
Kuo-Hsin Yang64e3d122018-11-06 13:23:24 +00002264 mapping_clear_unevictable(file_inode(obj->base.filp)->i_mapping);
2265
2266 pagevec_init(&pvec);
Chris Wilson03ac84f2016-10-28 13:58:36 +01002267 for_each_sgt_page(page, sgt_iter, pages) {
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002268 if (obj->mm.dirty)
Chris Wilson9da3da62012-06-01 15:20:22 +01002269 set_page_dirty(page);
Chris Wilson3ef94da2009-09-14 16:50:29 +01002270
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002271 if (obj->mm.madv == I915_MADV_WILLNEED)
Chris Wilson9da3da62012-06-01 15:20:22 +01002272 mark_page_accessed(page);
Chris Wilson3ef94da2009-09-14 16:50:29 +01002273
Kuo-Hsin Yang64e3d122018-11-06 13:23:24 +00002274 if (!pagevec_add(&pvec, page))
2275 check_release_pagevec(&pvec);
Chris Wilson3ef94da2009-09-14 16:50:29 +01002276 }
Kuo-Hsin Yang64e3d122018-11-06 13:23:24 +00002277 if (pagevec_count(&pvec))
2278 check_release_pagevec(&pvec);
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002279 obj->mm.dirty = false;
Eric Anholt673a3942008-07-30 12:06:12 -07002280
Chris Wilson03ac84f2016-10-28 13:58:36 +01002281 sg_free_table(pages);
2282 kfree(pages);
Chris Wilson37e680a2012-06-07 15:38:42 +01002283}
2284
Chris Wilson96d77632016-10-28 13:58:33 +01002285static void __i915_gem_object_reset_page_iter(struct drm_i915_gem_object *obj)
2286{
2287 struct radix_tree_iter iter;
Ville Syrjäläc23aa712017-09-01 20:12:51 +03002288 void __rcu **slot;
Chris Wilson96d77632016-10-28 13:58:33 +01002289
Chris Wilsonbea6e982017-10-26 14:00:31 +01002290 rcu_read_lock();
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002291 radix_tree_for_each_slot(slot, &obj->mm.get_page.radix, &iter, 0)
2292 radix_tree_delete(&obj->mm.get_page.radix, iter.index);
Chris Wilsonbea6e982017-10-26 14:00:31 +01002293 rcu_read_unlock();
Chris Wilson96d77632016-10-28 13:58:33 +01002294}
2295
Chris Wilsonacd1c1e2018-06-11 08:55:32 +01002296static struct sg_table *
2297__i915_gem_object_unset_pages(struct drm_i915_gem_object *obj)
Chris Wilson37e680a2012-06-07 15:38:42 +01002298{
Chris Wilsonf2123812017-10-16 12:40:37 +01002299 struct drm_i915_private *i915 = to_i915(obj->base.dev);
Chris Wilson03ac84f2016-10-28 13:58:36 +01002300 struct sg_table *pages;
Chris Wilson37e680a2012-06-07 15:38:42 +01002301
Chris Wilson03ac84f2016-10-28 13:58:36 +01002302 pages = fetch_and_zero(&obj->mm.pages);
Chris Wilsonacd1c1e2018-06-11 08:55:32 +01002303 if (!pages)
2304 return NULL;
Chris Wilsona2165e32012-12-03 11:49:00 +00002305
Chris Wilsonf2123812017-10-16 12:40:37 +01002306 spin_lock(&i915->mm.obj_lock);
2307 list_del(&obj->mm.link);
2308 spin_unlock(&i915->mm.obj_lock);
2309
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002310 if (obj->mm.mapping) {
Chris Wilson4b30cb22016-08-18 17:16:42 +01002311 void *ptr;
2312
Chris Wilson0ce81782017-05-17 13:09:59 +01002313 ptr = page_mask_bits(obj->mm.mapping);
Chris Wilson4b30cb22016-08-18 17:16:42 +01002314 if (is_vmalloc_addr(ptr))
2315 vunmap(ptr);
Chris Wilsonfb8621d2016-04-08 12:11:14 +01002316 else
Chris Wilson4b30cb22016-08-18 17:16:42 +01002317 kunmap(kmap_to_page(ptr));
2318
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002319 obj->mm.mapping = NULL;
Chris Wilson0a798eb2016-04-08 12:11:11 +01002320 }
2321
Chris Wilson96d77632016-10-28 13:58:33 +01002322 __i915_gem_object_reset_page_iter(obj);
Chris Wilsonacd1c1e2018-06-11 08:55:32 +01002323 obj->mm.page_sizes.phys = obj->mm.page_sizes.sg = 0;
Chris Wilson96d77632016-10-28 13:58:33 +01002324
Chris Wilsonacd1c1e2018-06-11 08:55:32 +01002325 return pages;
2326}
2327
2328void __i915_gem_object_put_pages(struct drm_i915_gem_object *obj,
2329 enum i915_mm_subclass subclass)
2330{
2331 struct sg_table *pages;
2332
2333 if (i915_gem_object_has_pinned_pages(obj))
2334 return;
2335
2336 GEM_BUG_ON(obj->bind_count);
2337 if (!i915_gem_object_has_pages(obj))
2338 return;
2339
2340 /* May be called by shrinker from within get_pages() (on another bo) */
2341 mutex_lock_nested(&obj->mm.lock, subclass);
2342 if (unlikely(atomic_read(&obj->mm.pages_pin_count)))
2343 goto unlock;
2344
2345 /*
2346 * ->put_pages might need to allocate memory for the bit17 swizzle
2347 * array, hence protect them from being reaped by removing them from gtt
2348 * lists early.
2349 */
2350 pages = __i915_gem_object_unset_pages(obj);
Chris Wilson4e5462e2017-03-07 13:20:31 +00002351 if (!IS_ERR(pages))
2352 obj->ops->put_pages(obj, pages);
2353
Chris Wilson1233e2d2016-10-28 13:58:37 +01002354unlock:
2355 mutex_unlock(&obj->mm.lock);
Chris Wilson6c085a72012-08-20 11:40:46 +02002356}
2357
Tvrtko Ursulinf8e57862018-09-26 09:03:53 +01002358bool i915_sg_trim(struct sg_table *orig_st)
Tvrtko Ursulin0c40ce12016-11-09 15:13:43 +00002359{
2360 struct sg_table new_st;
2361 struct scatterlist *sg, *new_sg;
2362 unsigned int i;
2363
2364 if (orig_st->nents == orig_st->orig_nents)
Chris Wilson935a2f72017-02-13 17:15:13 +00002365 return false;
Tvrtko Ursulin0c40ce12016-11-09 15:13:43 +00002366
Chris Wilson8bfc478f2016-12-23 14:57:58 +00002367 if (sg_alloc_table(&new_st, orig_st->nents, GFP_KERNEL | __GFP_NOWARN))
Chris Wilson935a2f72017-02-13 17:15:13 +00002368 return false;
Tvrtko Ursulin0c40ce12016-11-09 15:13:43 +00002369
2370 new_sg = new_st.sgl;
2371 for_each_sg(orig_st->sgl, sg, orig_st->nents, i) {
2372 sg_set_page(new_sg, sg_page(sg), sg->length, 0);
Matthew Auldc6d22ab2018-09-20 15:27:06 +01002373 sg_dma_address(new_sg) = sg_dma_address(sg);
2374 sg_dma_len(new_sg) = sg_dma_len(sg);
2375
Tvrtko Ursulin0c40ce12016-11-09 15:13:43 +00002376 new_sg = sg_next(new_sg);
2377 }
Chris Wilsonc2dc6cc2016-12-19 12:43:46 +00002378 GEM_BUG_ON(new_sg); /* Should walk exactly nents and hit the end */
Tvrtko Ursulin0c40ce12016-11-09 15:13:43 +00002379
2380 sg_free_table(orig_st);
2381
2382 *orig_st = new_st;
Chris Wilson935a2f72017-02-13 17:15:13 +00002383 return true;
Tvrtko Ursulin0c40ce12016-11-09 15:13:43 +00002384}
2385
Matthew Auldb91b09e2017-10-06 23:18:17 +01002386static int i915_gem_object_get_pages_gtt(struct drm_i915_gem_object *obj)
Eric Anholt673a3942008-07-30 12:06:12 -07002387{
Chris Wilsonfac5e232016-07-04 11:34:36 +01002388 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
Chris Wilsond766ef52016-12-19 12:43:45 +00002389 const unsigned long page_count = obj->base.size / PAGE_SIZE;
2390 unsigned long i;
Eric Anholt673a3942008-07-30 12:06:12 -07002391 struct address_space *mapping;
Chris Wilson9da3da62012-06-01 15:20:22 +01002392 struct sg_table *st;
2393 struct scatterlist *sg;
Dave Gordon85d12252016-05-20 11:54:06 +01002394 struct sgt_iter sgt_iter;
Eric Anholt673a3942008-07-30 12:06:12 -07002395 struct page *page;
Imre Deak90797e62013-02-18 19:28:03 +02002396 unsigned long last_pfn = 0; /* suppress gcc warning */
Tvrtko Ursulin56024522017-08-03 10:14:17 +01002397 unsigned int max_segment = i915_sg_segment_size();
Matthew Auld84e89782017-10-09 12:00:24 +01002398 unsigned int sg_page_sizes;
Kuo-Hsin Yang64e3d122018-11-06 13:23:24 +00002399 struct pagevec pvec;
Chris Wilson4846bf02017-06-09 12:03:46 +01002400 gfp_t noreclaim;
Imre Deake2273302015-07-09 12:59:05 +03002401 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07002402
Chris Wilsone0ff7a72018-09-03 09:33:36 +01002403 /*
2404 * Assert that the object is not currently in any GPU domain. As it
Chris Wilson6c085a72012-08-20 11:40:46 +02002405 * wasn't in the GTT, there shouldn't be any way it could have been in
2406 * a GPU cache
2407 */
Christian Königc0a51fd2018-02-16 13:43:38 +01002408 GEM_BUG_ON(obj->read_domains & I915_GEM_GPU_DOMAINS);
2409 GEM_BUG_ON(obj->write_domain & I915_GEM_GPU_DOMAINS);
Chris Wilson6c085a72012-08-20 11:40:46 +02002410
Chris Wilsone0ff7a72018-09-03 09:33:36 +01002411 /*
2412 * If there's no chance of allocating enough pages for the whole
2413 * object, bail early.
2414 */
2415 if (page_count > totalram_pages)
2416 return -ENOMEM;
2417
Chris Wilson9da3da62012-06-01 15:20:22 +01002418 st = kmalloc(sizeof(*st), GFP_KERNEL);
2419 if (st == NULL)
Matthew Auldb91b09e2017-10-06 23:18:17 +01002420 return -ENOMEM;
Eric Anholt673a3942008-07-30 12:06:12 -07002421
Chris Wilsond766ef52016-12-19 12:43:45 +00002422rebuild_st:
Chris Wilson9da3da62012-06-01 15:20:22 +01002423 if (sg_alloc_table(st, page_count, GFP_KERNEL)) {
Chris Wilson9da3da62012-06-01 15:20:22 +01002424 kfree(st);
Matthew Auldb91b09e2017-10-06 23:18:17 +01002425 return -ENOMEM;
Chris Wilson9da3da62012-06-01 15:20:22 +01002426 }
2427
Chris Wilsone0ff7a72018-09-03 09:33:36 +01002428 /*
2429 * Get the list of pages out of our struct file. They'll be pinned
Chris Wilson9da3da62012-06-01 15:20:22 +01002430 * at this point until we release them.
2431 *
2432 * Fail silently without starting the shrinker
2433 */
Al Viro93c76a32015-12-04 23:45:44 -05002434 mapping = obj->base.filp->f_mapping;
Kuo-Hsin Yang64e3d122018-11-06 13:23:24 +00002435 mapping_set_unevictable(mapping);
Chris Wilson0f6ab552017-06-09 12:03:48 +01002436 noreclaim = mapping_gfp_constraint(mapping, ~__GFP_RECLAIM);
Chris Wilson4846bf02017-06-09 12:03:46 +01002437 noreclaim |= __GFP_NORETRY | __GFP_NOWARN;
2438
Imre Deak90797e62013-02-18 19:28:03 +02002439 sg = st->sgl;
2440 st->nents = 0;
Matthew Auld84e89782017-10-09 12:00:24 +01002441 sg_page_sizes = 0;
Imre Deak90797e62013-02-18 19:28:03 +02002442 for (i = 0; i < page_count; i++) {
Chris Wilson4846bf02017-06-09 12:03:46 +01002443 const unsigned int shrink[] = {
2444 I915_SHRINK_BOUND | I915_SHRINK_UNBOUND | I915_SHRINK_PURGEABLE,
2445 0,
2446 }, *s = shrink;
2447 gfp_t gfp = noreclaim;
2448
2449 do {
Chris Wilsone6db7f42018-11-05 17:06:40 +00002450 cond_resched();
Chris Wilson6c085a72012-08-20 11:40:46 +02002451 page = shmem_read_mapping_page_gfp(mapping, i, gfp);
Chris Wilson4846bf02017-06-09 12:03:46 +01002452 if (likely(!IS_ERR(page)))
2453 break;
2454
2455 if (!*s) {
2456 ret = PTR_ERR(page);
2457 goto err_sg;
2458 }
2459
Chris Wilson912d5722017-09-06 16:19:30 -07002460 i915_gem_shrink(dev_priv, 2 * page_count, NULL, *s++);
Chris Wilson24f8e002017-03-22 11:05:21 +00002461
Chris Wilsone0ff7a72018-09-03 09:33:36 +01002462 /*
2463 * We've tried hard to allocate the memory by reaping
Chris Wilson6c085a72012-08-20 11:40:46 +02002464 * our own buffer, now let the real VM do its job and
2465 * go down in flames if truly OOM.
Chris Wilson24f8e002017-03-22 11:05:21 +00002466 *
2467 * However, since graphics tend to be disposable,
2468 * defer the oom here by reporting the ENOMEM back
2469 * to userspace.
Chris Wilson6c085a72012-08-20 11:40:46 +02002470 */
Chris Wilson4846bf02017-06-09 12:03:46 +01002471 if (!*s) {
2472 /* reclaim and warn, but no oom */
2473 gfp = mapping_gfp_mask(mapping);
Chris Wilsoneaf41802017-06-09 12:03:47 +01002474
Chris Wilsone0ff7a72018-09-03 09:33:36 +01002475 /*
2476 * Our bo are always dirty and so we require
Chris Wilsoneaf41802017-06-09 12:03:47 +01002477 * kswapd to reclaim our pages (direct reclaim
2478 * does not effectively begin pageout of our
2479 * buffers on its own). However, direct reclaim
2480 * only waits for kswapd when under allocation
2481 * congestion. So as a result __GFP_RECLAIM is
2482 * unreliable and fails to actually reclaim our
2483 * dirty pages -- unless you try over and over
2484 * again with !__GFP_NORETRY. However, we still
2485 * want to fail this allocation rather than
2486 * trigger the out-of-memory killer and for
Michal Hockodbb32952017-07-12 14:36:55 -07002487 * this we want __GFP_RETRY_MAYFAIL.
Chris Wilsoneaf41802017-06-09 12:03:47 +01002488 */
Michal Hockodbb32952017-07-12 14:36:55 -07002489 gfp |= __GFP_RETRY_MAYFAIL;
Imre Deake2273302015-07-09 12:59:05 +03002490 }
Chris Wilson4846bf02017-06-09 12:03:46 +01002491 } while (1);
2492
Chris Wilson871dfbd2016-10-11 09:20:21 +01002493 if (!i ||
2494 sg->length >= max_segment ||
2495 page_to_pfn(page) != last_pfn + 1) {
Matthew Aulda5c081662017-10-06 23:18:18 +01002496 if (i) {
Matthew Auld84e89782017-10-09 12:00:24 +01002497 sg_page_sizes |= sg->length;
Imre Deak90797e62013-02-18 19:28:03 +02002498 sg = sg_next(sg);
Matthew Aulda5c081662017-10-06 23:18:18 +01002499 }
Imre Deak90797e62013-02-18 19:28:03 +02002500 st->nents++;
2501 sg_set_page(sg, page, PAGE_SIZE, 0);
2502 } else {
2503 sg->length += PAGE_SIZE;
2504 }
2505 last_pfn = page_to_pfn(page);
Daniel Vetter3bbbe702013-10-07 17:15:45 -03002506
2507 /* Check that the i965g/gm workaround works. */
2508 WARN_ON((gfp & __GFP_DMA32) && (last_pfn >= 0x00100000UL));
Eric Anholt673a3942008-07-30 12:06:12 -07002509 }
Matthew Aulda5c081662017-10-06 23:18:18 +01002510 if (sg) { /* loop terminated early; short sg table */
Matthew Auld84e89782017-10-09 12:00:24 +01002511 sg_page_sizes |= sg->length;
Konrad Rzeszutek Wilk426729d2013-06-24 11:47:48 -04002512 sg_mark_end(sg);
Matthew Aulda5c081662017-10-06 23:18:18 +01002513 }
Chris Wilson74ce6b62012-10-19 15:51:06 +01002514
Tvrtko Ursulin0c40ce12016-11-09 15:13:43 +00002515 /* Trim unused sg entries to avoid wasting memory. */
2516 i915_sg_trim(st);
2517
Chris Wilson03ac84f2016-10-28 13:58:36 +01002518 ret = i915_gem_gtt_prepare_pages(obj, st);
Chris Wilsond766ef52016-12-19 12:43:45 +00002519 if (ret) {
Chris Wilsone0ff7a72018-09-03 09:33:36 +01002520 /*
2521 * DMA remapping failed? One possible cause is that
Chris Wilsond766ef52016-12-19 12:43:45 +00002522 * it could not reserve enough large entries, asking
2523 * for PAGE_SIZE chunks instead may be helpful.
2524 */
2525 if (max_segment > PAGE_SIZE) {
2526 for_each_sgt_page(page, sgt_iter, st)
2527 put_page(page);
2528 sg_free_table(st);
2529
2530 max_segment = PAGE_SIZE;
2531 goto rebuild_st;
2532 } else {
2533 dev_warn(&dev_priv->drm.pdev->dev,
2534 "Failed to DMA remap %lu pages\n",
2535 page_count);
2536 goto err_pages;
2537 }
2538 }
Imre Deake2273302015-07-09 12:59:05 +03002539
Eric Anholt673a3942008-07-30 12:06:12 -07002540 if (i915_gem_object_needs_bit17_swizzle(obj))
Chris Wilson03ac84f2016-10-28 13:58:36 +01002541 i915_gem_object_do_bit_17_swizzle(obj, st);
Eric Anholt673a3942008-07-30 12:06:12 -07002542
Matthew Auld84e89782017-10-09 12:00:24 +01002543 __i915_gem_object_set_pages(obj, st, sg_page_sizes);
Matthew Auldb91b09e2017-10-06 23:18:17 +01002544
2545 return 0;
Eric Anholt673a3942008-07-30 12:06:12 -07002546
Chris Wilsonb17993b2016-11-14 11:29:30 +00002547err_sg:
Imre Deak90797e62013-02-18 19:28:03 +02002548 sg_mark_end(sg);
Chris Wilsonb17993b2016-11-14 11:29:30 +00002549err_pages:
Kuo-Hsin Yang64e3d122018-11-06 13:23:24 +00002550 mapping_clear_unevictable(mapping);
2551 pagevec_init(&pvec);
2552 for_each_sgt_page(page, sgt_iter, st) {
2553 if (!pagevec_add(&pvec, page))
2554 check_release_pagevec(&pvec);
2555 }
2556 if (pagevec_count(&pvec))
2557 check_release_pagevec(&pvec);
Chris Wilson9da3da62012-06-01 15:20:22 +01002558 sg_free_table(st);
2559 kfree(st);
Chris Wilson0820baf2014-03-25 13:23:03 +00002560
Chris Wilsone0ff7a72018-09-03 09:33:36 +01002561 /*
2562 * shmemfs first checks if there is enough memory to allocate the page
Chris Wilson0820baf2014-03-25 13:23:03 +00002563 * and reports ENOSPC should there be insufficient, along with the usual
2564 * ENOMEM for a genuine allocation failure.
2565 *
2566 * We use ENOSPC in our driver to mean that we have run out of aperture
2567 * space and so want to translate the error from shmemfs back to our
2568 * usual understanding of ENOMEM.
2569 */
Imre Deake2273302015-07-09 12:59:05 +03002570 if (ret == -ENOSPC)
2571 ret = -ENOMEM;
2572
Matthew Auldb91b09e2017-10-06 23:18:17 +01002573 return ret;
Chris Wilson03ac84f2016-10-28 13:58:36 +01002574}
2575
2576void __i915_gem_object_set_pages(struct drm_i915_gem_object *obj,
Matthew Aulda5c081662017-10-06 23:18:18 +01002577 struct sg_table *pages,
Matthew Auld84e89782017-10-09 12:00:24 +01002578 unsigned int sg_page_sizes)
Chris Wilson03ac84f2016-10-28 13:58:36 +01002579{
Matthew Aulda5c081662017-10-06 23:18:18 +01002580 struct drm_i915_private *i915 = to_i915(obj->base.dev);
2581 unsigned long supported = INTEL_INFO(i915)->page_sizes;
2582 int i;
2583
Chris Wilson1233e2d2016-10-28 13:58:37 +01002584 lockdep_assert_held(&obj->mm.lock);
Chris Wilson03ac84f2016-10-28 13:58:36 +01002585
2586 obj->mm.get_page.sg_pos = pages->sgl;
2587 obj->mm.get_page.sg_idx = 0;
2588
2589 obj->mm.pages = pages;
Chris Wilson2c3a3f42016-11-04 10:30:01 +00002590
2591 if (i915_gem_object_is_tiled(obj) &&
Chris Wilsonf2123812017-10-16 12:40:37 +01002592 i915->quirks & QUIRK_PIN_SWIZZLED_PAGES) {
Chris Wilson2c3a3f42016-11-04 10:30:01 +00002593 GEM_BUG_ON(obj->mm.quirked);
2594 __i915_gem_object_pin_pages(obj);
2595 obj->mm.quirked = true;
2596 }
Matthew Aulda5c081662017-10-06 23:18:18 +01002597
Matthew Auld84e89782017-10-09 12:00:24 +01002598 GEM_BUG_ON(!sg_page_sizes);
2599 obj->mm.page_sizes.phys = sg_page_sizes;
Matthew Aulda5c081662017-10-06 23:18:18 +01002600
2601 /*
Matthew Auld84e89782017-10-09 12:00:24 +01002602 * Calculate the supported page-sizes which fit into the given
2603 * sg_page_sizes. This will give us the page-sizes which we may be able
2604 * to use opportunistically when later inserting into the GTT. For
2605 * example if phys=2G, then in theory we should be able to use 1G, 2M,
2606 * 64K or 4K pages, although in practice this will depend on a number of
2607 * other factors.
Matthew Aulda5c081662017-10-06 23:18:18 +01002608 */
2609 obj->mm.page_sizes.sg = 0;
2610 for_each_set_bit(i, &supported, ilog2(I915_GTT_MAX_PAGE_SIZE) + 1) {
2611 if (obj->mm.page_sizes.phys & ~0u << i)
2612 obj->mm.page_sizes.sg |= BIT(i);
2613 }
Matthew Aulda5c081662017-10-06 23:18:18 +01002614 GEM_BUG_ON(!HAS_PAGE_SIZES(i915, obj->mm.page_sizes.sg));
Chris Wilsonf2123812017-10-16 12:40:37 +01002615
2616 spin_lock(&i915->mm.obj_lock);
2617 list_add(&obj->mm.link, &i915->mm.unbound_list);
2618 spin_unlock(&i915->mm.obj_lock);
Chris Wilson03ac84f2016-10-28 13:58:36 +01002619}
2620
2621static int ____i915_gem_object_get_pages(struct drm_i915_gem_object *obj)
2622{
Matthew Auldb91b09e2017-10-06 23:18:17 +01002623 int err;
Chris Wilson03ac84f2016-10-28 13:58:36 +01002624
2625 if (unlikely(obj->mm.madv != I915_MADV_WILLNEED)) {
2626 DRM_DEBUG("Attempting to obtain a purgeable object\n");
2627 return -EFAULT;
2628 }
2629
Matthew Auldb91b09e2017-10-06 23:18:17 +01002630 err = obj->ops->get_pages(obj);
Matthew Auldb65a9b92017-12-18 10:38:55 +00002631 GEM_BUG_ON(!err && !i915_gem_object_has_pages(obj));
Chris Wilson03ac84f2016-10-28 13:58:36 +01002632
Matthew Auldb91b09e2017-10-06 23:18:17 +01002633 return err;
Eric Anholt673a3942008-07-30 12:06:12 -07002634}
2635
Chris Wilson37e680a2012-06-07 15:38:42 +01002636/* Ensure that the associated pages are gathered from the backing storage
Chris Wilson1233e2d2016-10-28 13:58:37 +01002637 * and pinned into our object. i915_gem_object_pin_pages() may be called
Chris Wilson37e680a2012-06-07 15:38:42 +01002638 * multiple times before they are released by a single call to
Chris Wilson1233e2d2016-10-28 13:58:37 +01002639 * i915_gem_object_unpin_pages() - once the pages are no longer referenced
Chris Wilson37e680a2012-06-07 15:38:42 +01002640 * either as a result of memory pressure (reaping pages under the shrinker)
2641 * or as the object is itself released.
2642 */
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002643int __i915_gem_object_get_pages(struct drm_i915_gem_object *obj)
Chris Wilson37e680a2012-06-07 15:38:42 +01002644{
Chris Wilson03ac84f2016-10-28 13:58:36 +01002645 int err;
Chris Wilson37e680a2012-06-07 15:38:42 +01002646
Chris Wilson1233e2d2016-10-28 13:58:37 +01002647 err = mutex_lock_interruptible(&obj->mm.lock);
2648 if (err)
2649 return err;
Chris Wilson4c7d62c2016-10-28 13:58:32 +01002650
Chris Wilsonf1fa4f42017-10-13 21:26:13 +01002651 if (unlikely(!i915_gem_object_has_pages(obj))) {
Chris Wilson88c880b2017-09-06 14:52:20 +01002652 GEM_BUG_ON(i915_gem_object_has_pinned_pages(obj));
2653
Chris Wilson2c3a3f42016-11-04 10:30:01 +00002654 err = ____i915_gem_object_get_pages(obj);
2655 if (err)
2656 goto unlock;
2657
2658 smp_mb__before_atomic();
Chris Wilson1233e2d2016-10-28 13:58:37 +01002659 }
Chris Wilson2c3a3f42016-11-04 10:30:01 +00002660 atomic_inc(&obj->mm.pages_pin_count);
Chris Wilson43e28f02013-01-08 10:53:09 +00002661
Chris Wilson1233e2d2016-10-28 13:58:37 +01002662unlock:
2663 mutex_unlock(&obj->mm.lock);
Chris Wilson03ac84f2016-10-28 13:58:36 +01002664 return err;
Eric Anholt673a3942008-07-30 12:06:12 -07002665}
2666
Dave Gordondd6034c2016-05-20 11:54:04 +01002667/* The 'mapping' part of i915_gem_object_pin_map() below */
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002668static void *i915_gem_object_map(const struct drm_i915_gem_object *obj,
2669 enum i915_map_type type)
Dave Gordondd6034c2016-05-20 11:54:04 +01002670{
2671 unsigned long n_pages = obj->base.size >> PAGE_SHIFT;
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002672 struct sg_table *sgt = obj->mm.pages;
Dave Gordon85d12252016-05-20 11:54:06 +01002673 struct sgt_iter sgt_iter;
2674 struct page *page;
Dave Gordonb338fa42016-05-20 11:54:05 +01002675 struct page *stack_pages[32];
2676 struct page **pages = stack_pages;
Dave Gordondd6034c2016-05-20 11:54:04 +01002677 unsigned long i = 0;
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002678 pgprot_t pgprot;
Dave Gordondd6034c2016-05-20 11:54:04 +01002679 void *addr;
2680
2681 /* A single page can always be kmapped */
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002682 if (n_pages == 1 && type == I915_MAP_WB)
Dave Gordondd6034c2016-05-20 11:54:04 +01002683 return kmap(sg_page(sgt->sgl));
2684
Dave Gordonb338fa42016-05-20 11:54:05 +01002685 if (n_pages > ARRAY_SIZE(stack_pages)) {
2686 /* Too big for stack -- allocate temporary array instead */
Michal Hocko0ee931c2017-09-13 16:28:29 -07002687 pages = kvmalloc_array(n_pages, sizeof(*pages), GFP_KERNEL);
Dave Gordonb338fa42016-05-20 11:54:05 +01002688 if (!pages)
2689 return NULL;
2690 }
Dave Gordondd6034c2016-05-20 11:54:04 +01002691
Dave Gordon85d12252016-05-20 11:54:06 +01002692 for_each_sgt_page(page, sgt_iter, sgt)
2693 pages[i++] = page;
Dave Gordondd6034c2016-05-20 11:54:04 +01002694
2695 /* Check that we have the expected number of pages */
2696 GEM_BUG_ON(i != n_pages);
2697
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002698 switch (type) {
Chris Wilsona575c672017-08-28 11:46:31 +01002699 default:
2700 MISSING_CASE(type);
2701 /* fallthrough to use PAGE_KERNEL anyway */
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002702 case I915_MAP_WB:
2703 pgprot = PAGE_KERNEL;
2704 break;
2705 case I915_MAP_WC:
2706 pgprot = pgprot_writecombine(PAGE_KERNEL_IO);
2707 break;
2708 }
2709 addr = vmap(pages, n_pages, 0, pgprot);
Dave Gordondd6034c2016-05-20 11:54:04 +01002710
Dave Gordonb338fa42016-05-20 11:54:05 +01002711 if (pages != stack_pages)
Michal Hocko20981052017-05-17 14:23:12 +02002712 kvfree(pages);
Dave Gordondd6034c2016-05-20 11:54:04 +01002713
2714 return addr;
2715}
2716
2717/* get, pin, and map the pages of the object into kernel space */
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002718void *i915_gem_object_pin_map(struct drm_i915_gem_object *obj,
2719 enum i915_map_type type)
Chris Wilson0a798eb2016-04-08 12:11:11 +01002720{
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002721 enum i915_map_type has_type;
2722 bool pinned;
2723 void *ptr;
Chris Wilson0a798eb2016-04-08 12:11:11 +01002724 int ret;
2725
Tina Zhanga03f3952017-11-14 10:25:13 +00002726 if (unlikely(!i915_gem_object_has_struct_page(obj)))
2727 return ERR_PTR(-ENXIO);
Chris Wilson0a798eb2016-04-08 12:11:11 +01002728
Chris Wilson1233e2d2016-10-28 13:58:37 +01002729 ret = mutex_lock_interruptible(&obj->mm.lock);
Chris Wilson0a798eb2016-04-08 12:11:11 +01002730 if (ret)
2731 return ERR_PTR(ret);
2732
Chris Wilsona575c672017-08-28 11:46:31 +01002733 pinned = !(type & I915_MAP_OVERRIDE);
2734 type &= ~I915_MAP_OVERRIDE;
2735
Chris Wilson1233e2d2016-10-28 13:58:37 +01002736 if (!atomic_inc_not_zero(&obj->mm.pages_pin_count)) {
Chris Wilsonf1fa4f42017-10-13 21:26:13 +01002737 if (unlikely(!i915_gem_object_has_pages(obj))) {
Chris Wilson88c880b2017-09-06 14:52:20 +01002738 GEM_BUG_ON(i915_gem_object_has_pinned_pages(obj));
2739
Chris Wilson2c3a3f42016-11-04 10:30:01 +00002740 ret = ____i915_gem_object_get_pages(obj);
2741 if (ret)
2742 goto err_unlock;
Chris Wilson1233e2d2016-10-28 13:58:37 +01002743
Chris Wilson2c3a3f42016-11-04 10:30:01 +00002744 smp_mb__before_atomic();
2745 }
2746 atomic_inc(&obj->mm.pages_pin_count);
Chris Wilson1233e2d2016-10-28 13:58:37 +01002747 pinned = false;
2748 }
Chris Wilsonf1fa4f42017-10-13 21:26:13 +01002749 GEM_BUG_ON(!i915_gem_object_has_pages(obj));
Chris Wilson0a798eb2016-04-08 12:11:11 +01002750
Chris Wilson0ce81782017-05-17 13:09:59 +01002751 ptr = page_unpack_bits(obj->mm.mapping, &has_type);
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002752 if (ptr && has_type != type) {
2753 if (pinned) {
2754 ret = -EBUSY;
Chris Wilson1233e2d2016-10-28 13:58:37 +01002755 goto err_unpin;
Chris Wilson0a798eb2016-04-08 12:11:11 +01002756 }
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002757
2758 if (is_vmalloc_addr(ptr))
2759 vunmap(ptr);
2760 else
2761 kunmap(kmap_to_page(ptr));
2762
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002763 ptr = obj->mm.mapping = NULL;
Chris Wilson0a798eb2016-04-08 12:11:11 +01002764 }
2765
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002766 if (!ptr) {
2767 ptr = i915_gem_object_map(obj, type);
2768 if (!ptr) {
2769 ret = -ENOMEM;
Chris Wilson1233e2d2016-10-28 13:58:37 +01002770 goto err_unpin;
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002771 }
2772
Chris Wilson0ce81782017-05-17 13:09:59 +01002773 obj->mm.mapping = page_pack_bits(ptr, type);
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002774 }
2775
Chris Wilson1233e2d2016-10-28 13:58:37 +01002776out_unlock:
2777 mutex_unlock(&obj->mm.lock);
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002778 return ptr;
2779
Chris Wilson1233e2d2016-10-28 13:58:37 +01002780err_unpin:
2781 atomic_dec(&obj->mm.pages_pin_count);
2782err_unlock:
2783 ptr = ERR_PTR(ret);
2784 goto out_unlock;
Chris Wilson0a798eb2016-04-08 12:11:11 +01002785}
2786
Chris Wilson7c55e2c2017-03-07 12:03:38 +00002787static int
2788i915_gem_object_pwrite_gtt(struct drm_i915_gem_object *obj,
2789 const struct drm_i915_gem_pwrite *arg)
2790{
2791 struct address_space *mapping = obj->base.filp->f_mapping;
2792 char __user *user_data = u64_to_user_ptr(arg->data_ptr);
2793 u64 remain, offset;
2794 unsigned int pg;
2795
2796 /* Before we instantiate/pin the backing store for our use, we
2797 * can prepopulate the shmemfs filp efficiently using a write into
2798 * the pagecache. We avoid the penalty of instantiating all the
2799 * pages, important if the user is just writing to a few and never
2800 * uses the object on the GPU, and using a direct write into shmemfs
2801 * allows it to avoid the cost of retrieving a page (either swapin
2802 * or clearing-before-use) before it is overwritten.
2803 */
Chris Wilsonf1fa4f42017-10-13 21:26:13 +01002804 if (i915_gem_object_has_pages(obj))
Chris Wilson7c55e2c2017-03-07 12:03:38 +00002805 return -ENODEV;
2806
Chris Wilsona6d65e42017-10-16 21:27:32 +01002807 if (obj->mm.madv != I915_MADV_WILLNEED)
2808 return -EFAULT;
2809
Chris Wilson7c55e2c2017-03-07 12:03:38 +00002810 /* Before the pages are instantiated the object is treated as being
2811 * in the CPU domain. The pages will be clflushed as required before
2812 * use, and we can freely write into the pages directly. If userspace
2813 * races pwrite with any other operation; corruption will ensue -
2814 * that is userspace's prerogative!
2815 */
2816
2817 remain = arg->size;
2818 offset = arg->offset;
2819 pg = offset_in_page(offset);
2820
2821 do {
2822 unsigned int len, unwritten;
2823 struct page *page;
2824 void *data, *vaddr;
2825 int err;
2826
2827 len = PAGE_SIZE - pg;
2828 if (len > remain)
2829 len = remain;
2830
2831 err = pagecache_write_begin(obj->base.filp, mapping,
2832 offset, len, 0,
2833 &page, &data);
2834 if (err < 0)
2835 return err;
2836
2837 vaddr = kmap(page);
2838 unwritten = copy_from_user(vaddr + pg, user_data, len);
2839 kunmap(page);
2840
2841 err = pagecache_write_end(obj->base.filp, mapping,
2842 offset, len, len - unwritten,
2843 page, data);
2844 if (err < 0)
2845 return err;
2846
2847 if (unwritten)
2848 return -EFAULT;
2849
2850 remain -= len;
2851 user_data += len;
2852 offset += len;
2853 pg = 0;
2854 } while (remain);
2855
2856 return 0;
2857}
2858
Mika Kuoppala14921f32018-06-15 13:44:29 +03002859static void i915_gem_client_mark_guilty(struct drm_i915_file_private *file_priv,
2860 const struct i915_gem_context *ctx)
2861{
2862 unsigned int score;
2863 unsigned long prev_hang;
2864
2865 if (i915_gem_context_is_banned(ctx))
2866 score = I915_CLIENT_SCORE_CONTEXT_BAN;
2867 else
2868 score = 0;
2869
2870 prev_hang = xchg(&file_priv->hang_timestamp, jiffies);
2871 if (time_before(jiffies, prev_hang + I915_CLIENT_FAST_HANG_JIFFIES))
2872 score += I915_CLIENT_SCORE_HANG_FAST;
2873
2874 if (score) {
2875 atomic_add(score, &file_priv->ban_score);
2876
2877 DRM_DEBUG_DRIVER("client %s: gained %u ban score, now %u\n",
2878 ctx->name, score,
2879 atomic_read(&file_priv->ban_score));
2880 }
2881}
2882
Mika Kuoppalae5e1fc42016-11-16 17:20:31 +02002883static void i915_gem_context_mark_guilty(struct i915_gem_context *ctx)
Mika Kuoppalaaa60c662013-06-12 15:13:20 +03002884{
Mika Kuoppala14921f32018-06-15 13:44:29 +03002885 unsigned int score;
2886 bool banned, bannable;
Mika Kuoppalab083a082016-11-18 15:10:47 +02002887
Chris Wilson77b25a92017-07-21 13:32:30 +01002888 atomic_inc(&ctx->guilty_count);
2889
Mika Kuoppala14921f32018-06-15 13:44:29 +03002890 bannable = i915_gem_context_is_bannable(ctx);
2891 score = atomic_add_return(CONTEXT_SCORE_GUILTY, &ctx->ban_score);
2892 banned = score >= CONTEXT_SCORE_BAN_THRESHOLD;
Chris Wilson24eae082018-02-05 09:22:01 +00002893
Mika Kuoppala14921f32018-06-15 13:44:29 +03002894 /* Cool contexts don't accumulate client ban score */
2895 if (!bannable)
Mika Kuoppalab083a082016-11-18 15:10:47 +02002896 return;
2897
Chris Wilsonbcc26612018-06-18 08:31:35 +01002898 if (banned) {
2899 DRM_DEBUG_DRIVER("context %s: guilty %d, score %u, banned\n",
2900 ctx->name, atomic_read(&ctx->guilty_count),
2901 score);
Mika Kuoppala14921f32018-06-15 13:44:29 +03002902 i915_gem_context_set_banned(ctx);
Chris Wilsonbcc26612018-06-18 08:31:35 +01002903 }
Mika Kuoppala14921f32018-06-15 13:44:29 +03002904
2905 if (!IS_ERR_OR_NULL(ctx->file_priv))
2906 i915_gem_client_mark_guilty(ctx->file_priv, ctx);
Mika Kuoppalae5e1fc42016-11-16 17:20:31 +02002907}
2908
2909static void i915_gem_context_mark_innocent(struct i915_gem_context *ctx)
2910{
Chris Wilson77b25a92017-07-21 13:32:30 +01002911 atomic_inc(&ctx->active_count);
Mika Kuoppalaaa60c662013-06-12 15:13:20 +03002912}
2913
Chris Wilsone61e0f52018-02-21 09:56:36 +00002914struct i915_request *
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00002915i915_gem_find_active_request(struct intel_engine_cs *engine)
Chris Wilson9375e442010-09-19 12:21:28 +01002916{
Chris Wilsone61e0f52018-02-21 09:56:36 +00002917 struct i915_request *request, *active = NULL;
Chris Wilson754c9fd2017-02-23 07:44:14 +00002918 unsigned long flags;
Mika Kuoppalaaa60c662013-06-12 15:13:20 +03002919
Chris Wilsoncc7cc532018-05-29 14:29:18 +01002920 /*
2921 * We are called by the error capture, reset and to dump engine
2922 * state at random points in time. In particular, note that neither is
2923 * crucially ordered with an interrupt. After a hang, the GPU is dead
2924 * and we assume that no more writes can happen (we waited long enough
2925 * for all writes that were in transaction to be flushed) - adding an
Chris Wilsonf69a02c2016-07-01 17:23:16 +01002926 * extra delay for a recent interrupt is pointless. Hence, we do
2927 * not need an engine->irq_seqno_barrier() before the seqno reads.
Chris Wilsoncc7cc532018-05-29 14:29:18 +01002928 * At all other times, we must assume the GPU is still running, but
2929 * we only care about the snapshot of this moment.
Chris Wilsonf69a02c2016-07-01 17:23:16 +01002930 */
Chris Wilsona89d1f92018-05-02 17:38:39 +01002931 spin_lock_irqsave(&engine->timeline.lock, flags);
2932 list_for_each_entry(request, &engine->timeline.requests, link) {
Chris Wilsone61e0f52018-02-21 09:56:36 +00002933 if (__i915_request_completed(request, request->global_seqno))
Chris Wilson4db080f2013-12-04 11:37:09 +00002934 continue;
Mika Kuoppalaaa60c662013-06-12 15:13:20 +03002935
Chris Wilson754c9fd2017-02-23 07:44:14 +00002936 active = request;
2937 break;
2938 }
Chris Wilsona89d1f92018-05-02 17:38:39 +01002939 spin_unlock_irqrestore(&engine->timeline.lock, flags);
Chris Wilson754c9fd2017-02-23 07:44:14 +00002940
2941 return active;
Mika Kuoppalab6b0fac2014-01-30 19:04:43 +02002942}
2943
Michel Thierrya1ef70e2017-06-20 10:57:47 +01002944/*
2945 * Ensure irq handler finishes, and not run again.
2946 * Also return the active request so that we only search for it once.
2947 */
Chris Wilsone61e0f52018-02-21 09:56:36 +00002948struct i915_request *
Michel Thierrya1ef70e2017-06-20 10:57:47 +01002949i915_gem_reset_prepare_engine(struct intel_engine_cs *engine)
2950{
Chris Wilson5adfb772018-05-16 19:33:51 +01002951 struct i915_request *request;
Michel Thierrya1ef70e2017-06-20 10:57:47 +01002952
Chris Wilson1749d902017-10-09 12:02:59 +01002953 /*
2954 * During the reset sequence, we must prevent the engine from
2955 * entering RC6. As the context state is undefined until we restart
2956 * the engine, if it does enter RC6 during the reset, the state
2957 * written to the powercontext is undefined and so we may lose
2958 * GPU state upon resume, i.e. fail to restart after a reset.
2959 */
2960 intel_uncore_forcewake_get(engine->i915, FORCEWAKE_ALL);
2961
Chris Wilson5adfb772018-05-16 19:33:51 +01002962 request = engine->reset.prepare(engine);
Chris Wilsond1d1ebf42017-07-21 13:32:33 +01002963 if (request && request->fence.error == -EIO)
2964 request = ERR_PTR(-EIO); /* Previous reset failed! */
Michel Thierrya1ef70e2017-06-20 10:57:47 +01002965
2966 return request;
2967}
2968
Chris Wilson0e178ae2017-01-17 17:59:06 +02002969int i915_gem_reset_prepare(struct drm_i915_private *dev_priv)
Chris Wilson4c965542017-01-17 17:59:01 +02002970{
2971 struct intel_engine_cs *engine;
Chris Wilsone61e0f52018-02-21 09:56:36 +00002972 struct i915_request *request;
Chris Wilson4c965542017-01-17 17:59:01 +02002973 enum intel_engine_id id;
Chris Wilson0e178ae2017-01-17 17:59:06 +02002974 int err = 0;
Chris Wilson4c965542017-01-17 17:59:01 +02002975
Chris Wilson0e178ae2017-01-17 17:59:06 +02002976 for_each_engine(engine, dev_priv, id) {
Michel Thierrya1ef70e2017-06-20 10:57:47 +01002977 request = i915_gem_reset_prepare_engine(engine);
2978 if (IS_ERR(request)) {
2979 err = PTR_ERR(request);
2980 continue;
Chris Wilson0e178ae2017-01-17 17:59:06 +02002981 }
Michel Thierryc64992e2017-06-20 10:57:44 +01002982
2983 engine->hangcheck.active_request = request;
Chris Wilson0e178ae2017-01-17 17:59:06 +02002984 }
2985
Chris Wilson4c965542017-01-17 17:59:01 +02002986 i915_gem_revoke_fences(dev_priv);
Michal Wajdeczkoc37d5722018-03-12 13:03:07 +00002987 intel_uc_sanitize(dev_priv);
Chris Wilson0e178ae2017-01-17 17:59:06 +02002988
2989 return err;
Chris Wilson4c965542017-01-17 17:59:01 +02002990}
2991
Chris Wilsone61e0f52018-02-21 09:56:36 +00002992static void engine_skip_context(struct i915_request *request)
Mika Kuoppala36193ac2017-01-17 17:59:02 +02002993{
2994 struct intel_engine_cs *engine = request->engine;
Chris Wilson4e0d64d2018-05-17 22:26:30 +01002995 struct i915_gem_context *hung_ctx = request->gem_context;
Chris Wilsona89d1f92018-05-02 17:38:39 +01002996 struct i915_timeline *timeline = request->timeline;
Mika Kuoppala36193ac2017-01-17 17:59:02 +02002997 unsigned long flags;
2998
Chris Wilsona89d1f92018-05-02 17:38:39 +01002999 GEM_BUG_ON(timeline == &engine->timeline);
Mika Kuoppala36193ac2017-01-17 17:59:02 +02003000
Chris Wilsona89d1f92018-05-02 17:38:39 +01003001 spin_lock_irqsave(&engine->timeline.lock, flags);
Chris Wilson890fd182018-07-06 22:07:10 +01003002 spin_lock(&timeline->lock);
Mika Kuoppala36193ac2017-01-17 17:59:02 +02003003
Chris Wilsona89d1f92018-05-02 17:38:39 +01003004 list_for_each_entry_continue(request, &engine->timeline.requests, link)
Chris Wilson4e0d64d2018-05-17 22:26:30 +01003005 if (request->gem_context == hung_ctx)
Chris Wilson6dd75262018-07-06 11:39:43 +01003006 i915_request_skip(request, -EIO);
Mika Kuoppala36193ac2017-01-17 17:59:02 +02003007
3008 list_for_each_entry(request, &timeline->requests, link)
Chris Wilson6dd75262018-07-06 11:39:43 +01003009 i915_request_skip(request, -EIO);
Mika Kuoppala36193ac2017-01-17 17:59:02 +02003010
3011 spin_unlock(&timeline->lock);
Chris Wilsona89d1f92018-05-02 17:38:39 +01003012 spin_unlock_irqrestore(&engine->timeline.lock, flags);
Mika Kuoppala36193ac2017-01-17 17:59:02 +02003013}
3014
Chris Wilsond1d1ebf42017-07-21 13:32:33 +01003015/* Returns the request if it was guilty of the hang */
Chris Wilsone61e0f52018-02-21 09:56:36 +00003016static struct i915_request *
Chris Wilsond1d1ebf42017-07-21 13:32:33 +01003017i915_gem_reset_request(struct intel_engine_cs *engine,
Chris Wilsonbba08692018-04-06 23:03:53 +01003018 struct i915_request *request,
3019 bool stalled)
Mika Kuoppala61da5362017-01-17 17:59:05 +02003020{
Mika Kuoppala71895a02017-01-17 17:59:07 +02003021 /* The guilty request will get skipped on a hung engine.
3022 *
3023 * Users of client default contexts do not rely on logical
3024 * state preserved between batches so it is safe to execute
3025 * queued requests following the hang. Non default contexts
3026 * rely on preserved state, so skipping a batch loses the
3027 * evolution of the state and it needs to be considered corrupted.
3028 * Executing more queued batches on top of corrupted state is
3029 * risky. But we take the risk by trying to advance through
3030 * the queued requests in order to make the client behaviour
3031 * more predictable around resets, by not throwing away random
3032 * amount of batches it has prepared for execution. Sophisticated
3033 * clients can use gem_reset_stats_ioctl and dma fence status
3034 * (exported via sync_file info ioctl on explicit fences) to observe
3035 * when it loses the context state and should rebuild accordingly.
3036 *
3037 * The context ban, and ultimately the client ban, mechanism are safety
3038 * valves if client submission ends up resulting in nothing more than
3039 * subsequent hangs.
3040 */
3041
Chris Wilsonbba08692018-04-06 23:03:53 +01003042 if (i915_request_completed(request)) {
3043 GEM_TRACE("%s pardoned global=%d (fence %llx:%d), current %d\n",
3044 engine->name, request->global_seqno,
3045 request->fence.context, request->fence.seqno,
3046 intel_engine_get_seqno(engine));
3047 stalled = false;
3048 }
3049
3050 if (stalled) {
Chris Wilson4e0d64d2018-05-17 22:26:30 +01003051 i915_gem_context_mark_guilty(request->gem_context);
Chris Wilson6dd75262018-07-06 11:39:43 +01003052 i915_request_skip(request, -EIO);
Chris Wilsond1d1ebf42017-07-21 13:32:33 +01003053
3054 /* If this context is now banned, skip all pending requests. */
Chris Wilson4e0d64d2018-05-17 22:26:30 +01003055 if (i915_gem_context_is_banned(request->gem_context))
Chris Wilsond1d1ebf42017-07-21 13:32:33 +01003056 engine_skip_context(request);
Mika Kuoppala61da5362017-01-17 17:59:05 +02003057 } else {
Chris Wilsond1d1ebf42017-07-21 13:32:33 +01003058 /*
3059 * Since this is not the hung engine, it may have advanced
3060 * since the hang declaration. Double check by refinding
3061 * the active request at the time of the reset.
3062 */
3063 request = i915_gem_find_active_request(engine);
3064 if (request) {
Chris Wilson042ed2d2018-06-15 10:31:36 +01003065 unsigned long flags;
3066
Chris Wilson4e0d64d2018-05-17 22:26:30 +01003067 i915_gem_context_mark_innocent(request->gem_context);
Chris Wilsond1d1ebf42017-07-21 13:32:33 +01003068 dma_fence_set_error(&request->fence, -EAGAIN);
3069
3070 /* Rewind the engine to replay the incomplete rq */
Chris Wilson042ed2d2018-06-15 10:31:36 +01003071 spin_lock_irqsave(&engine->timeline.lock, flags);
Chris Wilsond1d1ebf42017-07-21 13:32:33 +01003072 request = list_prev_entry(request, link);
Chris Wilsona89d1f92018-05-02 17:38:39 +01003073 if (&request->link == &engine->timeline.requests)
Chris Wilsond1d1ebf42017-07-21 13:32:33 +01003074 request = NULL;
Chris Wilson042ed2d2018-06-15 10:31:36 +01003075 spin_unlock_irqrestore(&engine->timeline.lock, flags);
Chris Wilsond1d1ebf42017-07-21 13:32:33 +01003076 }
Mika Kuoppala61da5362017-01-17 17:59:05 +02003077 }
3078
Chris Wilsond1d1ebf42017-07-21 13:32:33 +01003079 return request;
Mika Kuoppala61da5362017-01-17 17:59:05 +02003080}
3081
Michel Thierrya1ef70e2017-06-20 10:57:47 +01003082void i915_gem_reset_engine(struct intel_engine_cs *engine,
Chris Wilsonbba08692018-04-06 23:03:53 +01003083 struct i915_request *request,
3084 bool stalled)
Chris Wilson4db080f2013-12-04 11:37:09 +00003085{
Chris Wilsond1d1ebf42017-07-21 13:32:33 +01003086 if (request)
Chris Wilsonbba08692018-04-06 23:03:53 +01003087 request = i915_gem_reset_request(engine, request, stalled);
Chris Wilsond1d1ebf42017-07-21 13:32:33 +01003088
Chris Wilson821ed7d2016-09-09 14:11:53 +01003089 /* Setup the CS to resume from the breadcrumb of the hung request */
Chris Wilson5adfb772018-05-16 19:33:51 +01003090 engine->reset.reset(engine, request);
Chris Wilson821ed7d2016-09-09 14:11:53 +01003091}
3092
Chris Wilsond0667e92018-04-06 23:03:54 +01003093void i915_gem_reset(struct drm_i915_private *dev_priv,
3094 unsigned int stalled_mask)
Chris Wilson821ed7d2016-09-09 14:11:53 +01003095{
3096 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05303097 enum intel_engine_id id;
Chris Wilson821ed7d2016-09-09 14:11:53 +01003098
Chris Wilson4c7d62c2016-10-28 13:58:32 +01003099 lockdep_assert_held(&dev_priv->drm.struct_mutex);
3100
Chris Wilsone61e0f52018-02-21 09:56:36 +00003101 i915_retire_requests(dev_priv);
Chris Wilson821ed7d2016-09-09 14:11:53 +01003102
Chris Wilson2ae55732017-02-12 17:20:02 +00003103 for_each_engine(engine, dev_priv, id) {
Chris Wilson1fc44d92018-05-17 22:26:32 +01003104 struct intel_context *ce;
Chris Wilson2ae55732017-02-12 17:20:02 +00003105
Chris Wilsonbba08692018-04-06 23:03:53 +01003106 i915_gem_reset_engine(engine,
3107 engine->hangcheck.active_request,
Chris Wilsond0667e92018-04-06 23:03:54 +01003108 stalled_mask & ENGINE_MASK(id));
Chris Wilson1fc44d92018-05-17 22:26:32 +01003109 ce = fetch_and_zero(&engine->last_retired_context);
3110 if (ce)
3111 intel_context_unpin(ce);
Chris Wilson7b6da812017-12-16 00:03:34 +00003112
3113 /*
3114 * Ostensibily, we always want a context loaded for powersaving,
3115 * so if the engine is idle after the reset, send a request
3116 * to load our scratch kernel_context.
3117 *
3118 * More mysteriously, if we leave the engine idle after a reset,
3119 * the next userspace batch may hang, with what appears to be
3120 * an incoherent read by the CS (presumably stale TLB). An
3121 * empty request appears sufficient to paper over the glitch.
3122 */
Chris Wilson01b8fdc2018-02-05 15:24:31 +00003123 if (intel_engine_is_idle(engine)) {
Chris Wilsone61e0f52018-02-21 09:56:36 +00003124 struct i915_request *rq;
Chris Wilson7b6da812017-12-16 00:03:34 +00003125
Chris Wilsone61e0f52018-02-21 09:56:36 +00003126 rq = i915_request_alloc(engine,
3127 dev_priv->kernel_context);
Chris Wilson7b6da812017-12-16 00:03:34 +00003128 if (!IS_ERR(rq))
Chris Wilson697b9a82018-06-12 11:51:35 +01003129 i915_request_add(rq);
Chris Wilson7b6da812017-12-16 00:03:34 +00003130 }
Chris Wilson2ae55732017-02-12 17:20:02 +00003131 }
Chris Wilson821ed7d2016-09-09 14:11:53 +01003132
Tvrtko Ursulin4362f4f2016-11-16 08:55:33 +00003133 i915_gem_restore_fences(dev_priv);
Chris Wilson821ed7d2016-09-09 14:11:53 +01003134}
3135
Michel Thierrya1ef70e2017-06-20 10:57:47 +01003136void i915_gem_reset_finish_engine(struct intel_engine_cs *engine)
3137{
Chris Wilson5adfb772018-05-16 19:33:51 +01003138 engine->reset.finish(engine);
3139
Chris Wilson1749d902017-10-09 12:02:59 +01003140 intel_uncore_forcewake_put(engine->i915, FORCEWAKE_ALL);
Michel Thierrya1ef70e2017-06-20 10:57:47 +01003141}
3142
Chris Wilsond8027092017-02-08 14:30:32 +00003143void i915_gem_reset_finish(struct drm_i915_private *dev_priv)
3144{
Chris Wilson1f7b8472017-02-08 14:30:33 +00003145 struct intel_engine_cs *engine;
3146 enum intel_engine_id id;
3147
Chris Wilsond8027092017-02-08 14:30:32 +00003148 lockdep_assert_held(&dev_priv->drm.struct_mutex);
Chris Wilson1f7b8472017-02-08 14:30:33 +00003149
Chris Wilsonfe3288b2017-02-12 17:20:01 +00003150 for_each_engine(engine, dev_priv, id) {
Michel Thierryc64992e2017-06-20 10:57:44 +01003151 engine->hangcheck.active_request = NULL;
Michel Thierrya1ef70e2017-06-20 10:57:47 +01003152 i915_gem_reset_finish_engine(engine);
Chris Wilsonfe3288b2017-02-12 17:20:01 +00003153 }
Chris Wilsond8027092017-02-08 14:30:32 +00003154}
3155
Chris Wilsone61e0f52018-02-21 09:56:36 +00003156static void nop_submit_request(struct i915_request *request)
Chris Wilson821ed7d2016-09-09 14:11:53 +01003157{
Chris Wilson8d550822017-10-06 12:56:17 +01003158 unsigned long flags;
3159
Chris Wilsond9b13c42018-03-15 13:14:50 +00003160 GEM_TRACE("%s fence %llx:%d -> -EIO\n",
3161 request->engine->name,
3162 request->fence.context, request->fence.seqno);
Chris Wilson3cd94422017-01-10 17:22:45 +00003163 dma_fence_set_error(&request->fence, -EIO);
Chris Wilson8d550822017-10-06 12:56:17 +01003164
Chris Wilsona89d1f92018-05-02 17:38:39 +01003165 spin_lock_irqsave(&request->engine->timeline.lock, flags);
Chris Wilsone61e0f52018-02-21 09:56:36 +00003166 __i915_request_submit(request);
Chris Wilson6faf5912018-12-28 14:07:35 +00003167 intel_engine_write_global_seqno(request->engine, request->global_seqno);
Chris Wilsona89d1f92018-05-02 17:38:39 +01003168 spin_unlock_irqrestore(&request->engine->timeline.lock, flags);
Chris Wilson821ed7d2016-09-09 14:11:53 +01003169}
3170
Daniel Vetteraf7a8ff2017-10-11 11:10:19 +02003171void i915_gem_set_wedged(struct drm_i915_private *i915)
Chris Wilson821ed7d2016-09-09 14:11:53 +01003172{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00003173 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05303174 enum intel_engine_id id;
Eric Anholt673a3942008-07-30 12:06:12 -07003175
Chris Wilsond9b13c42018-03-15 13:14:50 +00003176 GEM_TRACE("start\n");
3177
Chris Wilson7f961d72018-04-26 11:32:19 +01003178 if (GEM_SHOW_DEBUG()) {
Chris Wilson559e0402018-02-05 09:21:59 +00003179 struct drm_printer p = drm_debug_printer(__func__);
3180
3181 for_each_engine(engine, i915, id)
3182 intel_engine_dump(engine, &p, "%s\n", engine->name);
3183 }
3184
Chris Wilson3970c652018-07-23 15:53:35 +01003185 if (test_and_set_bit(I915_WEDGED, &i915->gpu_error.flags))
3186 goto out;
Chris Wilson0d73e7a2018-02-07 15:13:50 +00003187
Daniel Vetteraf7a8ff2017-10-11 11:10:19 +02003188 /*
3189 * First, stop submission to hw, but do not yet complete requests by
3190 * rolling the global seqno forward (since this would complete requests
3191 * for which we haven't set the fence error to EIO yet).
3192 */
Chris Wilson38009602018-12-03 11:36:55 +00003193 for_each_engine(engine, i915, id)
Chris Wilson963ddd62018-03-02 11:33:24 +00003194 i915_gem_reset_prepare_engine(engine);
Chris Wilson47650db2018-03-07 13:42:25 +00003195
Chris Wilsonac697ae2018-03-15 15:10:15 +00003196 /* Even if the GPU reset fails, it should still stop the engines */
Chris Wilsonec5b65a2018-07-26 09:50:33 +01003197 if (INTEL_GEN(i915) >= 5)
3198 intel_gpu_reset(i915, ALL_ENGINES);
Chris Wilsonac697ae2018-03-15 15:10:15 +00003199
Daniel Vetteraf7a8ff2017-10-11 11:10:19 +02003200 for_each_engine(engine, i915, id) {
Chris Wilson38009602018-12-03 11:36:55 +00003201 engine->submit_request = nop_submit_request;
3202 engine->schedule = NULL;
Daniel Vetteraf7a8ff2017-10-11 11:10:19 +02003203 }
Chris Wilson38009602018-12-03 11:36:55 +00003204 i915->caps.scheduler = 0;
Daniel Vetteraf7a8ff2017-10-11 11:10:19 +02003205
3206 /*
3207 * Make sure no request can slip through without getting completed by
Chris Wilson6faf5912018-12-28 14:07:35 +00003208 * either this call here to intel_engine_write_global_seqno, or the one
Chris Wilson38009602018-12-03 11:36:55 +00003209 * in nop_submit_request.
Daniel Vetteraf7a8ff2017-10-11 11:10:19 +02003210 */
3211 synchronize_rcu();
3212
Chris Wilson38009602018-12-03 11:36:55 +00003213 /* Mark all executing requests as skipped */
3214 for_each_engine(engine, i915, id)
3215 engine->cancel_requests(engine);
3216
Daniel Vetteraf7a8ff2017-10-11 11:10:19 +02003217 for_each_engine(engine, i915, id) {
Chris Wilson963ddd62018-03-02 11:33:24 +00003218 i915_gem_reset_finish_engine(engine);
Chris Wilson38009602018-12-03 11:36:55 +00003219 intel_engine_wakeup(engine);
Daniel Vetteraf7a8ff2017-10-11 11:10:19 +02003220 }
Chris Wilson20e49332016-11-22 14:41:21 +00003221
Chris Wilson3970c652018-07-23 15:53:35 +01003222out:
Chris Wilsond9b13c42018-03-15 13:14:50 +00003223 GEM_TRACE("end\n");
3224
Chris Wilson3d7adbb2017-07-21 13:32:27 +01003225 wake_up_all(&i915->gpu_error.reset_queue);
Eric Anholt673a3942008-07-30 12:06:12 -07003226}
3227
Chris Wilson2e8f9d32017-03-16 17:13:04 +00003228bool i915_gem_unset_wedged(struct drm_i915_private *i915)
3229{
Chris Wilsona89d1f92018-05-02 17:38:39 +01003230 struct i915_timeline *tl;
Chris Wilson2e8f9d32017-03-16 17:13:04 +00003231
3232 lockdep_assert_held(&i915->drm.struct_mutex);
3233 if (!test_bit(I915_WEDGED, &i915->gpu_error.flags))
3234 return true;
3235
Chris Wilson55c15512019-01-03 21:33:40 +00003236 if (!i915->gt.scratch) /* Never full initialised, recovery impossible */
3237 return false;
3238
Chris Wilsond9b13c42018-03-15 13:14:50 +00003239 GEM_TRACE("start\n");
3240
Chris Wilson2d4ecac2018-03-07 13:42:21 +00003241 /*
3242 * Before unwedging, make sure that all pending operations
Chris Wilson2e8f9d32017-03-16 17:13:04 +00003243 * are flushed and errored out - we may have requests waiting upon
3244 * third party fences. We marked all inflight requests as EIO, and
3245 * every execbuf since returned EIO, for consistency we want all
3246 * the currently pending requests to also be marked as EIO, which
3247 * is done inside our nop_submit_request - and so we must wait.
3248 *
3249 * No more can be submitted until we reset the wedged bit.
3250 */
3251 list_for_each_entry(tl, &i915->gt.timelines, link) {
Chris Wilsona89d1f92018-05-02 17:38:39 +01003252 struct i915_request *rq;
Chris Wilson2e8f9d32017-03-16 17:13:04 +00003253
Chris Wilsona89d1f92018-05-02 17:38:39 +01003254 rq = i915_gem_active_peek(&tl->last_request,
3255 &i915->drm.struct_mutex);
3256 if (!rq)
3257 continue;
Chris Wilson2e8f9d32017-03-16 17:13:04 +00003258
Chris Wilsona89d1f92018-05-02 17:38:39 +01003259 /*
3260 * We can't use our normal waiter as we want to
3261 * avoid recursively trying to handle the current
3262 * reset. The basic dma_fence_default_wait() installs
3263 * a callback for dma_fence_signal(), which is
3264 * triggered by our nop handler (indirectly, the
3265 * callback enables the signaler thread which is
3266 * woken by the nop_submit_request() advancing the seqno
3267 * and when the seqno passes the fence, the signaler
3268 * then signals the fence waking us up).
3269 */
3270 if (dma_fence_default_wait(&rq->fence, true,
3271 MAX_SCHEDULE_TIMEOUT) < 0)
3272 return false;
Chris Wilson2e8f9d32017-03-16 17:13:04 +00003273 }
Chris Wilson2d4ecac2018-03-07 13:42:21 +00003274 i915_retire_requests(i915);
3275 GEM_BUG_ON(i915->gt.active_requests);
Chris Wilson2e8f9d32017-03-16 17:13:04 +00003276
Chris Wilson55277e12019-01-03 11:21:04 +00003277 intel_engines_sanitize(i915, false);
Chris Wilson8db601f2018-09-14 09:00:17 +01003278
Chris Wilson2d4ecac2018-03-07 13:42:21 +00003279 /*
3280 * Undo nop_submit_request. We prevent all new i915 requests from
Chris Wilson2e8f9d32017-03-16 17:13:04 +00003281 * being queued (by disallowing execbuf whilst wedged) so having
3282 * waited for all active requests above, we know the system is idle
3283 * and do not have to worry about a thread being inside
3284 * engine->submit_request() as we swap over. So unlike installing
3285 * the nop_submit_request on reset, we can do this from normal
3286 * context and do not require stop_machine().
3287 */
3288 intel_engines_reset_default_submission(i915);
Chris Wilson36703e72017-06-22 11:56:25 +01003289 i915_gem_contexts_lost(i915);
Chris Wilson2e8f9d32017-03-16 17:13:04 +00003290
Chris Wilsond9b13c42018-03-15 13:14:50 +00003291 GEM_TRACE("end\n");
3292
Chris Wilson2e8f9d32017-03-16 17:13:04 +00003293 smp_mb__before_atomic(); /* complete takeover before enabling execbuf */
3294 clear_bit(I915_WEDGED, &i915->gpu_error.flags);
3295
3296 return true;
3297}
3298
Daniel Vetter75ef9da2010-08-21 00:25:16 +02003299static void
Eric Anholt673a3942008-07-30 12:06:12 -07003300i915_gem_retire_work_handler(struct work_struct *work)
3301{
Chris Wilsonb29c19b2013-09-25 17:34:56 +01003302 struct drm_i915_private *dev_priv =
Chris Wilson67d97da2016-07-04 08:08:31 +01003303 container_of(work, typeof(*dev_priv), gt.retire_work.work);
Chris Wilson91c8a322016-07-05 10:40:23 +01003304 struct drm_device *dev = &dev_priv->drm;
Eric Anholt673a3942008-07-30 12:06:12 -07003305
Chris Wilson891b48c2010-09-29 12:26:37 +01003306 /* Come back later if the device is busy... */
Chris Wilsonb29c19b2013-09-25 17:34:56 +01003307 if (mutex_trylock(&dev->struct_mutex)) {
Chris Wilsone61e0f52018-02-21 09:56:36 +00003308 i915_retire_requests(dev_priv);
Chris Wilsonb29c19b2013-09-25 17:34:56 +01003309 mutex_unlock(&dev->struct_mutex);
3310 }
Chris Wilson67d97da2016-07-04 08:08:31 +01003311
Chris Wilson88923042018-01-29 14:41:04 +00003312 /*
3313 * Keep the retire handler running until we are finally idle.
Chris Wilson67d97da2016-07-04 08:08:31 +01003314 * We do not need to do this test under locking as in the worst-case
3315 * we queue the retire worker once too often.
3316 */
Chris Wilson88923042018-01-29 14:41:04 +00003317 if (READ_ONCE(dev_priv->gt.awake))
Chris Wilson67d97da2016-07-04 08:08:31 +01003318 queue_delayed_work(dev_priv->wq,
3319 &dev_priv->gt.retire_work,
Chris Wilsonbcb45082012-10-05 17:02:57 +01003320 round_jiffies_up_relative(HZ));
Chris Wilsonb29c19b2013-09-25 17:34:56 +01003321}
Chris Wilson891b48c2010-09-29 12:26:37 +01003322
Chris Wilson84a10742018-01-24 11:36:08 +00003323static void shrink_caches(struct drm_i915_private *i915)
3324{
3325 /*
3326 * kmem_cache_shrink() discards empty slabs and reorders partially
3327 * filled slabs to prioritise allocating from the mostly full slabs,
3328 * with the aim of reducing fragmentation.
3329 */
3330 kmem_cache_shrink(i915->priorities);
3331 kmem_cache_shrink(i915->dependencies);
3332 kmem_cache_shrink(i915->requests);
3333 kmem_cache_shrink(i915->luts);
3334 kmem_cache_shrink(i915->vmas);
3335 kmem_cache_shrink(i915->objects);
3336}
3337
3338struct sleep_rcu_work {
3339 union {
3340 struct rcu_head rcu;
3341 struct work_struct work;
3342 };
3343 struct drm_i915_private *i915;
3344 unsigned int epoch;
3345};
3346
3347static inline bool
3348same_epoch(struct drm_i915_private *i915, unsigned int epoch)
3349{
3350 /*
3351 * There is a small chance that the epoch wrapped since we started
3352 * sleeping. If we assume that epoch is at least a u32, then it will
3353 * take at least 2^32 * 100ms for it to wrap, or about 326 years.
3354 */
3355 return epoch == READ_ONCE(i915->gt.epoch);
3356}
3357
3358static void __sleep_work(struct work_struct *work)
3359{
3360 struct sleep_rcu_work *s = container_of(work, typeof(*s), work);
3361 struct drm_i915_private *i915 = s->i915;
3362 unsigned int epoch = s->epoch;
3363
3364 kfree(s);
3365 if (same_epoch(i915, epoch))
3366 shrink_caches(i915);
3367}
3368
3369static void __sleep_rcu(struct rcu_head *rcu)
3370{
3371 struct sleep_rcu_work *s = container_of(rcu, typeof(*s), rcu);
3372 struct drm_i915_private *i915 = s->i915;
3373
Chris Wilsona1db9c52018-11-08 09:21:01 +00003374 destroy_rcu_head(&s->rcu);
3375
Chris Wilson84a10742018-01-24 11:36:08 +00003376 if (same_epoch(i915, s->epoch)) {
3377 INIT_WORK(&s->work, __sleep_work);
3378 queue_work(i915->wq, &s->work);
3379 } else {
3380 kfree(s);
3381 }
3382}
3383
Chris Wilson5427f202017-10-23 22:32:34 +01003384static inline bool
3385new_requests_since_last_retire(const struct drm_i915_private *i915)
3386{
3387 return (READ_ONCE(i915->gt.active_requests) ||
3388 work_pending(&i915->gt.idle_work.work));
3389}
3390
Chris Wilson1934f5de2018-05-31 23:40:57 +01003391static void assert_kernel_context_is_current(struct drm_i915_private *i915)
3392{
3393 struct intel_engine_cs *engine;
3394 enum intel_engine_id id;
3395
3396 if (i915_terminally_wedged(&i915->gpu_error))
3397 return;
3398
3399 GEM_BUG_ON(i915->gt.active_requests);
3400 for_each_engine(engine, i915, id) {
3401 GEM_BUG_ON(__i915_gem_active_peek(&engine->timeline.last_request));
3402 GEM_BUG_ON(engine->last_retired_context !=
3403 to_intel_context(i915->kernel_context, engine));
3404 }
3405}
3406
Chris Wilsonb29c19b2013-09-25 17:34:56 +01003407static void
3408i915_gem_idle_work_handler(struct work_struct *work)
3409{
3410 struct drm_i915_private *dev_priv =
Chris Wilson67d97da2016-07-04 08:08:31 +01003411 container_of(work, typeof(*dev_priv), gt.idle_work.work);
Chris Wilson84a10742018-01-24 11:36:08 +00003412 unsigned int epoch = I915_EPOCH_INVALID;
Chris Wilson67d97da2016-07-04 08:08:31 +01003413 bool rearm_hangcheck;
3414
3415 if (!READ_ONCE(dev_priv->gt.awake))
3416 return;
3417
Chris Wilson4dfacb02018-05-31 09:22:43 +01003418 if (READ_ONCE(dev_priv->gt.active_requests))
3419 return;
3420
3421 /*
3422 * Flush out the last user context, leaving only the pinned
3423 * kernel context resident. When we are idling on the kernel_context,
3424 * no more new requests (with a context switch) are emitted and we
3425 * can finally rest. A consequence is that the idle work handler is
3426 * always called at least twice before idling (and if the system is
3427 * idle that implies a round trip through the retire worker).
3428 */
3429 mutex_lock(&dev_priv->drm.struct_mutex);
3430 i915_gem_switch_to_kernel_context(dev_priv);
3431 mutex_unlock(&dev_priv->drm.struct_mutex);
3432
3433 GEM_TRACE("active_requests=%d (after switch-to-kernel-context)\n",
3434 READ_ONCE(dev_priv->gt.active_requests));
3435
Imre Deak0cb56702016-11-07 11:20:04 +02003436 /*
3437 * Wait for last execlists context complete, but bail out in case a
Chris Wilsonffed7bd2018-03-01 10:33:38 +00003438 * new request is submitted. As we don't trust the hardware, we
3439 * continue on if the wait times out. This is necessary to allow
3440 * the machine to suspend even if the hardware dies, and we will
3441 * try to recover in resume (after depriving the hardware of power,
3442 * it may be in a better mmod).
Imre Deak0cb56702016-11-07 11:20:04 +02003443 */
Chris Wilsonffed7bd2018-03-01 10:33:38 +00003444 __wait_for(if (new_requests_since_last_retire(dev_priv)) return,
3445 intel_engines_are_idle(dev_priv),
3446 I915_IDLE_ENGINES_TIMEOUT * 1000,
3447 10, 500);
Chris Wilson67d97da2016-07-04 08:08:31 +01003448
3449 rearm_hangcheck =
3450 cancel_delayed_work_sync(&dev_priv->gpu_error.hangcheck_work);
3451
Chris Wilson5427f202017-10-23 22:32:34 +01003452 if (!mutex_trylock(&dev_priv->drm.struct_mutex)) {
Chris Wilson67d97da2016-07-04 08:08:31 +01003453 /* Currently busy, come back later */
3454 mod_delayed_work(dev_priv->wq,
3455 &dev_priv->gt.idle_work,
3456 msecs_to_jiffies(50));
3457 goto out_rearm;
3458 }
3459
Imre Deak93c97dc2016-11-07 11:20:03 +02003460 /*
3461 * New request retired after this work handler started, extend active
3462 * period until next instance of the work.
3463 */
Chris Wilson5427f202017-10-23 22:32:34 +01003464 if (new_requests_since_last_retire(dev_priv))
Imre Deak93c97dc2016-11-07 11:20:03 +02003465 goto out_unlock;
3466
Chris Wilsone4d20062018-04-06 16:51:44 +01003467 epoch = __i915_gem_park(dev_priv);
Chris Wilsonff320d62017-10-23 22:32:35 +01003468
Chris Wilson1934f5de2018-05-31 23:40:57 +01003469 assert_kernel_context_is_current(dev_priv);
3470
Chris Wilson67d97da2016-07-04 08:08:31 +01003471 rearm_hangcheck = false;
Chris Wilson67d97da2016-07-04 08:08:31 +01003472out_unlock:
Chris Wilson5427f202017-10-23 22:32:34 +01003473 mutex_unlock(&dev_priv->drm.struct_mutex);
Chris Wilson35c94182015-04-07 16:20:37 +01003474
Chris Wilson67d97da2016-07-04 08:08:31 +01003475out_rearm:
3476 if (rearm_hangcheck) {
3477 GEM_BUG_ON(!dev_priv->gt.awake);
3478 i915_queue_hangcheck(dev_priv);
Chris Wilson35c94182015-04-07 16:20:37 +01003479 }
Chris Wilson84a10742018-01-24 11:36:08 +00003480
3481 /*
3482 * When we are idle, it is an opportune time to reap our caches.
3483 * However, we have many objects that utilise RCU and the ordered
3484 * i915->wq that this work is executing on. To try and flush any
3485 * pending frees now we are idle, we first wait for an RCU grace
3486 * period, and then queue a task (that will run last on the wq) to
3487 * shrink and re-optimize the caches.
3488 */
3489 if (same_epoch(dev_priv, epoch)) {
3490 struct sleep_rcu_work *s = kmalloc(sizeof(*s), GFP_KERNEL);
3491 if (s) {
Chris Wilsona1db9c52018-11-08 09:21:01 +00003492 init_rcu_head(&s->rcu);
Chris Wilson84a10742018-01-24 11:36:08 +00003493 s->i915 = dev_priv;
3494 s->epoch = epoch;
3495 call_rcu(&s->rcu, __sleep_rcu);
3496 }
3497 }
Eric Anholt673a3942008-07-30 12:06:12 -07003498}
3499
Chris Wilsonb1f788c2016-08-04 07:52:45 +01003500void i915_gem_close_object(struct drm_gem_object *gem, struct drm_file *file)
3501{
Chris Wilsond1b48c12017-08-16 09:52:08 +01003502 struct drm_i915_private *i915 = to_i915(gem->dev);
Chris Wilsonb1f788c2016-08-04 07:52:45 +01003503 struct drm_i915_gem_object *obj = to_intel_bo(gem);
3504 struct drm_i915_file_private *fpriv = file->driver_priv;
Chris Wilsond1b48c12017-08-16 09:52:08 +01003505 struct i915_lut_handle *lut, *ln;
Chris Wilsonb1f788c2016-08-04 07:52:45 +01003506
Chris Wilsond1b48c12017-08-16 09:52:08 +01003507 mutex_lock(&i915->drm.struct_mutex);
3508
3509 list_for_each_entry_safe(lut, ln, &obj->lut_list, obj_link) {
3510 struct i915_gem_context *ctx = lut->ctx;
3511 struct i915_vma *vma;
3512
Chris Wilson432295d2017-08-22 12:05:15 +01003513 GEM_BUG_ON(ctx->file_priv == ERR_PTR(-EBADF));
Chris Wilsond1b48c12017-08-16 09:52:08 +01003514 if (ctx->file_priv != fpriv)
3515 continue;
3516
3517 vma = radix_tree_delete(&ctx->handles_vma, lut->handle);
Chris Wilson3ffff012017-08-22 12:05:17 +01003518 GEM_BUG_ON(vma->obj != obj);
3519
3520 /* We allow the process to have multiple handles to the same
3521 * vma, in the same fd namespace, by virtue of flink/open.
3522 */
3523 GEM_BUG_ON(!vma->open_count);
3524 if (!--vma->open_count && !i915_vma_is_ggtt(vma))
Chris Wilsonb1f788c2016-08-04 07:52:45 +01003525 i915_vma_close(vma);
Chris Wilsonf8a7fde2016-10-28 13:58:29 +01003526
Chris Wilsond1b48c12017-08-16 09:52:08 +01003527 list_del(&lut->obj_link);
3528 list_del(&lut->ctx_link);
Chris Wilson4ff4b442017-06-16 15:05:16 +01003529
Chris Wilsond1b48c12017-08-16 09:52:08 +01003530 kmem_cache_free(i915->luts, lut);
3531 __i915_gem_object_release_unless_active(obj);
Chris Wilsonf8a7fde2016-10-28 13:58:29 +01003532 }
Chris Wilsond1b48c12017-08-16 09:52:08 +01003533
3534 mutex_unlock(&i915->drm.struct_mutex);
Chris Wilsonb1f788c2016-08-04 07:52:45 +01003535}
3536
Chris Wilsone95433c2016-10-28 13:58:27 +01003537static unsigned long to_wait_timeout(s64 timeout_ns)
3538{
3539 if (timeout_ns < 0)
3540 return MAX_SCHEDULE_TIMEOUT;
3541
3542 if (timeout_ns == 0)
3543 return 0;
3544
3545 return nsecs_to_jiffies_timeout(timeout_ns);
3546}
3547
Ben Widawsky5816d642012-04-11 11:18:19 -07003548/**
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07003549 * i915_gem_wait_ioctl - implements DRM_IOCTL_I915_GEM_WAIT
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01003550 * @dev: drm device pointer
3551 * @data: ioctl data blob
3552 * @file: drm file pointer
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07003553 *
3554 * Returns 0 if successful, else an error is returned with the remaining time in
3555 * the timeout parameter.
3556 * -ETIME: object is still busy after timeout
3557 * -ERESTARTSYS: signal interrupted the wait
3558 * -ENONENT: object doesn't exist
3559 * Also possible, but rare:
Chris Wilsonb8050142017-08-11 11:57:31 +01003560 * -EAGAIN: incomplete, restart syscall
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07003561 * -ENOMEM: damn
3562 * -ENODEV: Internal IRQ fail
3563 * -E?: The add request failed
3564 *
3565 * The wait ioctl with a timeout of 0 reimplements the busy ioctl. With any
3566 * non-zero timeout parameter the wait ioctl will wait for the given number of
3567 * nanoseconds on an object becoming unbusy. Since the wait itself does so
3568 * without holding struct_mutex the object may become re-busied before this
3569 * function completes. A similar but shorter * race condition exists in the busy
3570 * ioctl
3571 */
3572int
3573i915_gem_wait_ioctl(struct drm_device *dev, void *data, struct drm_file *file)
3574{
3575 struct drm_i915_gem_wait *args = data;
3576 struct drm_i915_gem_object *obj;
Chris Wilsone95433c2016-10-28 13:58:27 +01003577 ktime_t start;
3578 long ret;
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07003579
Daniel Vetter11b5d512014-09-29 15:31:26 +02003580 if (args->flags != 0)
3581 return -EINVAL;
3582
Chris Wilson03ac0642016-07-20 13:31:51 +01003583 obj = i915_gem_object_lookup(file, args->bo_handle);
Chris Wilson033d5492016-08-05 10:14:17 +01003584 if (!obj)
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07003585 return -ENOENT;
Chris Wilson033d5492016-08-05 10:14:17 +01003586
Chris Wilsone95433c2016-10-28 13:58:27 +01003587 start = ktime_get();
3588
3589 ret = i915_gem_object_wait(obj,
Chris Wilsone9eaf822018-10-01 15:47:55 +01003590 I915_WAIT_INTERRUPTIBLE |
3591 I915_WAIT_PRIORITY |
3592 I915_WAIT_ALL,
Chris Wilsone95433c2016-10-28 13:58:27 +01003593 to_wait_timeout(args->timeout_ns),
3594 to_rps_client(file));
3595
3596 if (args->timeout_ns > 0) {
3597 args->timeout_ns -= ktime_to_ns(ktime_sub(ktime_get(), start));
3598 if (args->timeout_ns < 0)
3599 args->timeout_ns = 0;
Chris Wilsonc1d20612017-02-16 12:54:41 +00003600
3601 /*
3602 * Apparently ktime isn't accurate enough and occasionally has a
3603 * bit of mismatch in the jiffies<->nsecs<->ktime loop. So patch
3604 * things up to make the test happy. We allow up to 1 jiffy.
3605 *
3606 * This is a regression from the timespec->ktime conversion.
3607 */
3608 if (ret == -ETIME && !nsecs_to_jiffies(args->timeout_ns))
3609 args->timeout_ns = 0;
Chris Wilsonb8050142017-08-11 11:57:31 +01003610
3611 /* Asked to wait beyond the jiffie/scheduler precision? */
3612 if (ret == -ETIME && args->timeout_ns)
3613 ret = -EAGAIN;
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07003614 }
3615
Chris Wilsonf0cd5182016-10-28 13:58:43 +01003616 i915_gem_object_put(obj);
John Harrisonff865882014-11-24 18:49:28 +00003617 return ret;
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07003618}
3619
Chris Wilsonec625fb2018-07-09 13:20:42 +01003620static long wait_for_timeline(struct i915_timeline *tl,
3621 unsigned int flags, long timeout)
Daniel Vetter4df2faf2010-02-19 11:52:00 +01003622{
Chris Wilson06060352018-05-31 09:22:44 +01003623 struct i915_request *rq;
Chris Wilson06060352018-05-31 09:22:44 +01003624
3625 rq = i915_gem_active_get_unlocked(&tl->last_request);
3626 if (!rq)
Chris Wilsonec625fb2018-07-09 13:20:42 +01003627 return timeout;
Chris Wilson06060352018-05-31 09:22:44 +01003628
3629 /*
3630 * "Race-to-idle".
3631 *
3632 * Switching to the kernel context is often used a synchronous
3633 * step prior to idling, e.g. in suspend for flushing all
3634 * current operations to memory before sleeping. These we
3635 * want to complete as quickly as possible to avoid prolonged
3636 * stalls, so allow the gpu to boost to maximum clocks.
3637 */
3638 if (flags & I915_WAIT_FOR_IDLE_BOOST)
3639 gen6_rps_boost(rq, NULL);
3640
Chris Wilsonec625fb2018-07-09 13:20:42 +01003641 timeout = i915_request_wait(rq, flags, timeout);
Chris Wilson06060352018-05-31 09:22:44 +01003642 i915_request_put(rq);
3643
Chris Wilsonec625fb2018-07-09 13:20:42 +01003644 return timeout;
Chris Wilson73cb9702016-10-28 13:58:46 +01003645}
3646
Chris Wilson25112b62017-03-30 15:50:39 +01003647static int wait_for_engines(struct drm_i915_private *i915)
3648{
Chris Wilsonee42c002017-12-11 19:41:34 +00003649 if (wait_for(intel_engines_are_idle(i915), I915_IDLE_ENGINES_TIMEOUT)) {
Chris Wilson59e4b192017-12-11 19:41:35 +00003650 dev_err(i915->drm.dev,
3651 "Failed to idle engines, declaring wedged!\n");
Chris Wilson629820f2018-03-09 10:11:14 +00003652 GEM_TRACE_DUMP();
Chris Wilsoncad99462017-08-26 12:09:33 +01003653 i915_gem_set_wedged(i915);
3654 return -EIO;
Chris Wilson25112b62017-03-30 15:50:39 +01003655 }
3656
3657 return 0;
3658}
3659
Chris Wilsonec625fb2018-07-09 13:20:42 +01003660int i915_gem_wait_for_idle(struct drm_i915_private *i915,
3661 unsigned int flags, long timeout)
Chris Wilson73cb9702016-10-28 13:58:46 +01003662{
Chris Wilsonec625fb2018-07-09 13:20:42 +01003663 GEM_TRACE("flags=%x (%s), timeout=%ld%s\n",
3664 flags, flags & I915_WAIT_LOCKED ? "locked" : "unlocked",
3665 timeout, timeout == MAX_SCHEDULE_TIMEOUT ? " (forever)" : "");
Chris Wilson09a4c022018-05-24 09:11:35 +01003666
Chris Wilson863e9fd2017-05-30 13:13:32 +01003667 /* If the device is asleep, we have no requests outstanding */
3668 if (!READ_ONCE(i915->gt.awake))
3669 return 0;
3670
Chris Wilson9caa34a2016-11-11 14:58:08 +00003671 if (flags & I915_WAIT_LOCKED) {
Chris Wilsona89d1f92018-05-02 17:38:39 +01003672 struct i915_timeline *tl;
3673 int err;
Chris Wilson9caa34a2016-11-11 14:58:08 +00003674
3675 lockdep_assert_held(&i915->drm.struct_mutex);
3676
3677 list_for_each_entry(tl, &i915->gt.timelines, link) {
Chris Wilsonec625fb2018-07-09 13:20:42 +01003678 timeout = wait_for_timeline(tl, flags, timeout);
3679 if (timeout < 0)
3680 return timeout;
Chris Wilson9caa34a2016-11-11 14:58:08 +00003681 }
Chris Wilsonc1e63f62018-08-08 11:50:59 +01003682 if (GEM_SHOW_DEBUG() && !timeout) {
3683 /* Presume that timeout was non-zero to begin with! */
3684 dev_warn(&i915->drm.pdev->dev,
3685 "Missed idle-completion interrupt!\n");
3686 GEM_TRACE_DUMP();
3687 }
Chris Wilsona61b47f2018-06-27 12:53:34 +01003688
3689 err = wait_for_engines(i915);
3690 if (err)
3691 return err;
3692
Chris Wilsone61e0f52018-02-21 09:56:36 +00003693 i915_retire_requests(i915);
Chris Wilson09a4c022018-05-24 09:11:35 +01003694 GEM_BUG_ON(i915->gt.active_requests);
Chris Wilson9caa34a2016-11-11 14:58:08 +00003695 } else {
Chris Wilsona89d1f92018-05-02 17:38:39 +01003696 struct intel_engine_cs *engine;
3697 enum intel_engine_id id;
Zou Nan haid1b851f2010-05-21 09:08:57 +08003698
Chris Wilsona89d1f92018-05-02 17:38:39 +01003699 for_each_engine(engine, i915, id) {
Chris Wilsonec625fb2018-07-09 13:20:42 +01003700 struct i915_timeline *tl = &engine->timeline;
3701
3702 timeout = wait_for_timeline(tl, flags, timeout);
3703 if (timeout < 0)
3704 return timeout;
Chris Wilsona89d1f92018-05-02 17:38:39 +01003705 }
Chris Wilsona89d1f92018-05-02 17:38:39 +01003706 }
Chris Wilsona61b47f2018-06-27 12:53:34 +01003707
3708 return 0;
Daniel Vetter4df2faf2010-02-19 11:52:00 +01003709}
3710
Chris Wilson5a97bcc2017-02-22 11:40:46 +00003711static void __i915_gem_object_flush_for_display(struct drm_i915_gem_object *obj)
3712{
Chris Wilsone27ab732017-06-15 13:38:49 +01003713 /*
3714 * We manually flush the CPU domain so that we can override and
3715 * force the flush for the display, and perform it asyncrhonously.
3716 */
3717 flush_write_domain(obj, ~I915_GEM_DOMAIN_CPU);
3718 if (obj->cache_dirty)
3719 i915_gem_clflush_object(obj, I915_CLFLUSH_FORCE);
Christian Königc0a51fd2018-02-16 13:43:38 +01003720 obj->write_domain = 0;
Chris Wilson5a97bcc2017-02-22 11:40:46 +00003721}
3722
3723void i915_gem_object_flush_if_display(struct drm_i915_gem_object *obj)
3724{
Chris Wilsonbd3d2252017-10-13 21:26:14 +01003725 if (!READ_ONCE(obj->pin_global))
Chris Wilson5a97bcc2017-02-22 11:40:46 +00003726 return;
3727
3728 mutex_lock(&obj->base.dev->struct_mutex);
3729 __i915_gem_object_flush_for_display(obj);
3730 mutex_unlock(&obj->base.dev->struct_mutex);
3731}
3732
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003733/**
Chris Wilsone22d8e32017-04-12 12:01:11 +01003734 * Moves a single object to the WC read, and possibly write domain.
3735 * @obj: object to act on
3736 * @write: ask for write access or read only
3737 *
3738 * This function returns when the move is complete, including waiting on
3739 * flushes to occur.
3740 */
3741int
3742i915_gem_object_set_to_wc_domain(struct drm_i915_gem_object *obj, bool write)
3743{
3744 int ret;
3745
3746 lockdep_assert_held(&obj->base.dev->struct_mutex);
3747
3748 ret = i915_gem_object_wait(obj,
3749 I915_WAIT_INTERRUPTIBLE |
3750 I915_WAIT_LOCKED |
3751 (write ? I915_WAIT_ALL : 0),
3752 MAX_SCHEDULE_TIMEOUT,
3753 NULL);
3754 if (ret)
3755 return ret;
3756
Christian Königc0a51fd2018-02-16 13:43:38 +01003757 if (obj->write_domain == I915_GEM_DOMAIN_WC)
Chris Wilsone22d8e32017-04-12 12:01:11 +01003758 return 0;
3759
3760 /* Flush and acquire obj->pages so that we are coherent through
3761 * direct access in memory with previous cached writes through
3762 * shmemfs and that our cache domain tracking remains valid.
3763 * For example, if the obj->filp was moved to swap without us
3764 * being notified and releasing the pages, we would mistakenly
3765 * continue to assume that the obj remained out of the CPU cached
3766 * domain.
3767 */
3768 ret = i915_gem_object_pin_pages(obj);
3769 if (ret)
3770 return ret;
3771
3772 flush_write_domain(obj, ~I915_GEM_DOMAIN_WC);
3773
3774 /* Serialise direct access to this object with the barriers for
3775 * coherent writes from the GPU, by effectively invalidating the
3776 * WC domain upon first access.
3777 */
Christian Königc0a51fd2018-02-16 13:43:38 +01003778 if ((obj->read_domains & I915_GEM_DOMAIN_WC) == 0)
Chris Wilsone22d8e32017-04-12 12:01:11 +01003779 mb();
3780
3781 /* It should now be out of any other write domains, and we can update
3782 * the domain values for our changes.
3783 */
Christian Königc0a51fd2018-02-16 13:43:38 +01003784 GEM_BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_WC) != 0);
3785 obj->read_domains |= I915_GEM_DOMAIN_WC;
Chris Wilsone22d8e32017-04-12 12:01:11 +01003786 if (write) {
Christian Königc0a51fd2018-02-16 13:43:38 +01003787 obj->read_domains = I915_GEM_DOMAIN_WC;
3788 obj->write_domain = I915_GEM_DOMAIN_WC;
Chris Wilsone22d8e32017-04-12 12:01:11 +01003789 obj->mm.dirty = true;
3790 }
3791
3792 i915_gem_object_unpin_pages(obj);
3793 return 0;
3794}
3795
3796/**
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003797 * Moves a single object to the GTT read, and possibly write domain.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01003798 * @obj: object to act on
3799 * @write: ask for write access or read only
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003800 *
3801 * This function returns when the move is complete, including waiting on
3802 * flushes to occur.
3803 */
Jesse Barnes79e53942008-11-07 14:24:08 -08003804int
Chris Wilson20217462010-11-23 15:26:33 +00003805i915_gem_object_set_to_gtt_domain(struct drm_i915_gem_object *obj, bool write)
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003806{
Eric Anholte47c68e2008-11-14 13:35:19 -08003807 int ret;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003808
Chris Wilsone95433c2016-10-28 13:58:27 +01003809 lockdep_assert_held(&obj->base.dev->struct_mutex);
Chris Wilson4c7d62c2016-10-28 13:58:32 +01003810
Chris Wilsone95433c2016-10-28 13:58:27 +01003811 ret = i915_gem_object_wait(obj,
3812 I915_WAIT_INTERRUPTIBLE |
3813 I915_WAIT_LOCKED |
3814 (write ? I915_WAIT_ALL : 0),
3815 MAX_SCHEDULE_TIMEOUT,
3816 NULL);
Chris Wilson88241782011-01-07 17:09:48 +00003817 if (ret)
3818 return ret;
3819
Christian Königc0a51fd2018-02-16 13:43:38 +01003820 if (obj->write_domain == I915_GEM_DOMAIN_GTT)
Chris Wilsonc13d87e2016-07-20 09:21:15 +01003821 return 0;
3822
Chris Wilson43566de2015-01-02 16:29:29 +05303823 /* Flush and acquire obj->pages so that we are coherent through
3824 * direct access in memory with previous cached writes through
3825 * shmemfs and that our cache domain tracking remains valid.
3826 * For example, if the obj->filp was moved to swap without us
3827 * being notified and releasing the pages, we would mistakenly
3828 * continue to assume that the obj remained out of the CPU cached
3829 * domain.
3830 */
Chris Wilsona4f5ea62016-10-28 13:58:35 +01003831 ret = i915_gem_object_pin_pages(obj);
Chris Wilson43566de2015-01-02 16:29:29 +05303832 if (ret)
3833 return ret;
3834
Chris Wilsonef749212017-04-12 12:01:10 +01003835 flush_write_domain(obj, ~I915_GEM_DOMAIN_GTT);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003836
Chris Wilsond0a57782012-10-09 19:24:37 +01003837 /* Serialise direct access to this object with the barriers for
3838 * coherent writes from the GPU, by effectively invalidating the
3839 * GTT domain upon first access.
3840 */
Christian Königc0a51fd2018-02-16 13:43:38 +01003841 if ((obj->read_domains & I915_GEM_DOMAIN_GTT) == 0)
Chris Wilsond0a57782012-10-09 19:24:37 +01003842 mb();
3843
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003844 /* It should now be out of any other write domains, and we can update
3845 * the domain values for our changes.
3846 */
Christian Königc0a51fd2018-02-16 13:43:38 +01003847 GEM_BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_GTT) != 0);
3848 obj->read_domains |= I915_GEM_DOMAIN_GTT;
Eric Anholte47c68e2008-11-14 13:35:19 -08003849 if (write) {
Christian Königc0a51fd2018-02-16 13:43:38 +01003850 obj->read_domains = I915_GEM_DOMAIN_GTT;
3851 obj->write_domain = I915_GEM_DOMAIN_GTT;
Chris Wilsona4f5ea62016-10-28 13:58:35 +01003852 obj->mm.dirty = true;
Eric Anholte47c68e2008-11-14 13:35:19 -08003853 }
3854
Chris Wilsona4f5ea62016-10-28 13:58:35 +01003855 i915_gem_object_unpin_pages(obj);
Eric Anholte47c68e2008-11-14 13:35:19 -08003856 return 0;
3857}
3858
Chris Wilsonef55f922015-10-09 14:11:27 +01003859/**
3860 * Changes the cache-level of an object across all VMA.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01003861 * @obj: object to act on
3862 * @cache_level: new cache level to set for the object
Chris Wilsonef55f922015-10-09 14:11:27 +01003863 *
3864 * After this function returns, the object will be in the new cache-level
3865 * across all GTT and the contents of the backing storage will be coherent,
3866 * with respect to the new cache-level. In order to keep the backing storage
3867 * coherent for all users, we only allow a single cache level to be set
3868 * globally on the object and prevent it from being changed whilst the
3869 * hardware is reading from the object. That is if the object is currently
3870 * on the scanout it will be set to uncached (or equivalent display
3871 * cache coherency) and all non-MOCS GPU access will also be uncached so
3872 * that all direct access to the scanout remains coherent.
3873 */
Chris Wilsone4ffd172011-04-04 09:44:39 +01003874int i915_gem_object_set_cache_level(struct drm_i915_gem_object *obj,
3875 enum i915_cache_level cache_level)
3876{
Chris Wilsonaa653a62016-08-04 07:52:27 +01003877 struct i915_vma *vma;
Chris Wilsona6a7cc42016-11-18 21:17:46 +00003878 int ret;
Chris Wilsone4ffd172011-04-04 09:44:39 +01003879
Chris Wilson4c7d62c2016-10-28 13:58:32 +01003880 lockdep_assert_held(&obj->base.dev->struct_mutex);
3881
Chris Wilsone4ffd172011-04-04 09:44:39 +01003882 if (obj->cache_level == cache_level)
Chris Wilsona6a7cc42016-11-18 21:17:46 +00003883 return 0;
Chris Wilsone4ffd172011-04-04 09:44:39 +01003884
Chris Wilsonef55f922015-10-09 14:11:27 +01003885 /* Inspect the list of currently bound VMA and unbind any that would
3886 * be invalid given the new cache-level. This is principally to
3887 * catch the issue of the CS prefetch crossing page boundaries and
3888 * reading an invalid PTE on older architectures.
3889 */
Chris Wilsonaa653a62016-08-04 07:52:27 +01003890restart:
3891 list_for_each_entry(vma, &obj->vma_list, obj_link) {
Chris Wilsonef55f922015-10-09 14:11:27 +01003892 if (!drm_mm_node_allocated(&vma->node))
3893 continue;
3894
Chris Wilson20dfbde2016-08-04 16:32:30 +01003895 if (i915_vma_is_pinned(vma)) {
Chris Wilsonef55f922015-10-09 14:11:27 +01003896 DRM_DEBUG("can not change the cache level of pinned objects\n");
3897 return -EBUSY;
3898 }
3899
Chris Wilson010e3e62017-12-06 12:49:13 +00003900 if (!i915_vma_is_closed(vma) &&
3901 i915_gem_valid_gtt_space(vma, cache_level))
Chris Wilsonaa653a62016-08-04 07:52:27 +01003902 continue;
3903
3904 ret = i915_vma_unbind(vma);
3905 if (ret)
3906 return ret;
3907
3908 /* As unbinding may affect other elements in the
3909 * obj->vma_list (due to side-effects from retiring
3910 * an active vma), play safe and restart the iterator.
3911 */
3912 goto restart;
Chris Wilson42d6ab42012-07-26 11:49:32 +01003913 }
3914
Chris Wilsonef55f922015-10-09 14:11:27 +01003915 /* We can reuse the existing drm_mm nodes but need to change the
3916 * cache-level on the PTE. We could simply unbind them all and
3917 * rebind with the correct cache-level on next use. However since
3918 * we already have a valid slot, dma mapping, pages etc, we may as
3919 * rewrite the PTE in the belief that doing so tramples upon less
3920 * state and so involves less work.
3921 */
Chris Wilson15717de2016-08-04 07:52:26 +01003922 if (obj->bind_count) {
Chris Wilsonef55f922015-10-09 14:11:27 +01003923 /* Before we change the PTE, the GPU must not be accessing it.
3924 * If we wait upon the object, we know that all the bound
3925 * VMA are no longer active.
3926 */
Chris Wilsone95433c2016-10-28 13:58:27 +01003927 ret = i915_gem_object_wait(obj,
3928 I915_WAIT_INTERRUPTIBLE |
3929 I915_WAIT_LOCKED |
3930 I915_WAIT_ALL,
3931 MAX_SCHEDULE_TIMEOUT,
3932 NULL);
Chris Wilsone4ffd172011-04-04 09:44:39 +01003933 if (ret)
3934 return ret;
3935
Tvrtko Ursulin0031fb92016-11-04 14:42:44 +00003936 if (!HAS_LLC(to_i915(obj->base.dev)) &&
3937 cache_level != I915_CACHE_NONE) {
Chris Wilsonef55f922015-10-09 14:11:27 +01003938 /* Access to snoopable pages through the GTT is
3939 * incoherent and on some machines causes a hard
3940 * lockup. Relinquish the CPU mmaping to force
3941 * userspace to refault in the pages and we can
3942 * then double check if the GTT mapping is still
3943 * valid for that pointer access.
3944 */
3945 i915_gem_release_mmap(obj);
Chris Wilsone4ffd172011-04-04 09:44:39 +01003946
Chris Wilsonef55f922015-10-09 14:11:27 +01003947 /* As we no longer need a fence for GTT access,
3948 * we can relinquish it now (and so prevent having
3949 * to steal a fence from someone else on the next
3950 * fence request). Note GPU activity would have
3951 * dropped the fence as all snoopable access is
3952 * supposed to be linear.
3953 */
Chris Wilsone2189dd2017-12-07 21:14:07 +00003954 for_each_ggtt_vma(vma, obj) {
Chris Wilson49ef5292016-08-18 17:17:00 +01003955 ret = i915_vma_put_fence(vma);
3956 if (ret)
3957 return ret;
3958 }
Chris Wilsonef55f922015-10-09 14:11:27 +01003959 } else {
3960 /* We either have incoherent backing store and
3961 * so no GTT access or the architecture is fully
3962 * coherent. In such cases, existing GTT mmaps
3963 * ignore the cache bit in the PTE and we can
3964 * rewrite it without confusing the GPU or having
3965 * to force userspace to fault back in its mmaps.
3966 */
Chris Wilsone4ffd172011-04-04 09:44:39 +01003967 }
3968
Chris Wilson1c7f4bc2016-02-26 11:03:19 +00003969 list_for_each_entry(vma, &obj->vma_list, obj_link) {
Chris Wilsonef55f922015-10-09 14:11:27 +01003970 if (!drm_mm_node_allocated(&vma->node))
3971 continue;
3972
3973 ret = i915_vma_bind(vma, cache_level, PIN_UPDATE);
3974 if (ret)
3975 return ret;
3976 }
Chris Wilsone4ffd172011-04-04 09:44:39 +01003977 }
3978
Chris Wilson1c7f4bc2016-02-26 11:03:19 +00003979 list_for_each_entry(vma, &obj->vma_list, obj_link)
Chris Wilson2c225692013-08-09 12:26:45 +01003980 vma->node.color = cache_level;
Chris Wilsonb8f55be2017-08-11 12:11:16 +01003981 i915_gem_object_set_cache_coherency(obj, cache_level);
Chris Wilsone27ab732017-06-15 13:38:49 +01003982 obj->cache_dirty = true; /* Always invalidate stale cachelines */
Chris Wilson2c225692013-08-09 12:26:45 +01003983
Chris Wilsone4ffd172011-04-04 09:44:39 +01003984 return 0;
3985}
3986
Ben Widawsky199adf42012-09-21 17:01:20 -07003987int i915_gem_get_caching_ioctl(struct drm_device *dev, void *data,
3988 struct drm_file *file)
Chris Wilsone6994ae2012-07-10 10:27:08 +01003989{
Ben Widawsky199adf42012-09-21 17:01:20 -07003990 struct drm_i915_gem_caching *args = data;
Chris Wilsone6994ae2012-07-10 10:27:08 +01003991 struct drm_i915_gem_object *obj;
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01003992 int err = 0;
Chris Wilsone6994ae2012-07-10 10:27:08 +01003993
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01003994 rcu_read_lock();
3995 obj = i915_gem_object_lookup_rcu(file, args->handle);
3996 if (!obj) {
3997 err = -ENOENT;
3998 goto out;
3999 }
Chris Wilsone6994ae2012-07-10 10:27:08 +01004000
Chris Wilson651d7942013-08-08 14:41:10 +01004001 switch (obj->cache_level) {
4002 case I915_CACHE_LLC:
4003 case I915_CACHE_L3_LLC:
4004 args->caching = I915_CACHING_CACHED;
4005 break;
4006
Chris Wilson4257d3b2013-08-08 14:41:11 +01004007 case I915_CACHE_WT:
4008 args->caching = I915_CACHING_DISPLAY;
4009 break;
4010
Chris Wilson651d7942013-08-08 14:41:10 +01004011 default:
4012 args->caching = I915_CACHING_NONE;
4013 break;
4014 }
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004015out:
4016 rcu_read_unlock();
4017 return err;
Chris Wilsone6994ae2012-07-10 10:27:08 +01004018}
4019
Ben Widawsky199adf42012-09-21 17:01:20 -07004020int i915_gem_set_caching_ioctl(struct drm_device *dev, void *data,
4021 struct drm_file *file)
Chris Wilsone6994ae2012-07-10 10:27:08 +01004022{
Chris Wilson9c870d02016-10-24 13:42:15 +01004023 struct drm_i915_private *i915 = to_i915(dev);
Ben Widawsky199adf42012-09-21 17:01:20 -07004024 struct drm_i915_gem_caching *args = data;
Chris Wilsone6994ae2012-07-10 10:27:08 +01004025 struct drm_i915_gem_object *obj;
4026 enum i915_cache_level level;
Chris Wilsond65415d2017-01-19 08:22:10 +00004027 int ret = 0;
Chris Wilsone6994ae2012-07-10 10:27:08 +01004028
Ben Widawsky199adf42012-09-21 17:01:20 -07004029 switch (args->caching) {
4030 case I915_CACHING_NONE:
Chris Wilsone6994ae2012-07-10 10:27:08 +01004031 level = I915_CACHE_NONE;
4032 break;
Ben Widawsky199adf42012-09-21 17:01:20 -07004033 case I915_CACHING_CACHED:
Imre Deake5756c12015-08-14 18:43:30 +03004034 /*
4035 * Due to a HW issue on BXT A stepping, GPU stores via a
4036 * snooped mapping may leave stale data in a corresponding CPU
4037 * cacheline, whereas normally such cachelines would get
4038 * invalidated.
4039 */
Chris Wilson9c870d02016-10-24 13:42:15 +01004040 if (!HAS_LLC(i915) && !HAS_SNOOP(i915))
Imre Deake5756c12015-08-14 18:43:30 +03004041 return -ENODEV;
4042
Chris Wilsone6994ae2012-07-10 10:27:08 +01004043 level = I915_CACHE_LLC;
4044 break;
Chris Wilson4257d3b2013-08-08 14:41:11 +01004045 case I915_CACHING_DISPLAY:
Chris Wilson9c870d02016-10-24 13:42:15 +01004046 level = HAS_WT(i915) ? I915_CACHE_WT : I915_CACHE_NONE;
Chris Wilson4257d3b2013-08-08 14:41:11 +01004047 break;
Chris Wilsone6994ae2012-07-10 10:27:08 +01004048 default:
4049 return -EINVAL;
4050 }
4051
Chris Wilsond65415d2017-01-19 08:22:10 +00004052 obj = i915_gem_object_lookup(file, args->handle);
4053 if (!obj)
4054 return -ENOENT;
4055
Tina Zhanga03f3952017-11-14 10:25:13 +00004056 /*
4057 * The caching mode of proxy object is handled by its generator, and
4058 * not allowed to be changed by userspace.
4059 */
4060 if (i915_gem_object_is_proxy(obj)) {
4061 ret = -ENXIO;
4062 goto out;
4063 }
4064
Chris Wilsond65415d2017-01-19 08:22:10 +00004065 if (obj->cache_level == level)
4066 goto out;
4067
4068 ret = i915_gem_object_wait(obj,
4069 I915_WAIT_INTERRUPTIBLE,
4070 MAX_SCHEDULE_TIMEOUT,
4071 to_rps_client(file));
4072 if (ret)
4073 goto out;
4074
Ben Widawsky3bc29132012-09-26 16:15:20 -07004075 ret = i915_mutex_lock_interruptible(dev);
4076 if (ret)
Chris Wilsond65415d2017-01-19 08:22:10 +00004077 goto out;
Chris Wilsone6994ae2012-07-10 10:27:08 +01004078
4079 ret = i915_gem_object_set_cache_level(obj, level);
Chris Wilsone6994ae2012-07-10 10:27:08 +01004080 mutex_unlock(&dev->struct_mutex);
Chris Wilsond65415d2017-01-19 08:22:10 +00004081
4082out:
4083 i915_gem_object_put(obj);
Chris Wilsone6994ae2012-07-10 10:27:08 +01004084 return ret;
4085}
4086
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08004087/*
Dhinakaran Pandiyan07bcd992018-03-06 19:34:18 -08004088 * Prepare buffer for display plane (scanout, cursors, etc). Can be called from
4089 * an uninterruptible phase (modesetting) and allows any flushes to be pipelined
4090 * (for pageflips). We only flush the caches while preparing the buffer for
4091 * display, the callers are responsible for frontbuffer flush.
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08004092 */
Chris Wilson058d88c2016-08-15 10:49:06 +01004093struct i915_vma *
Chris Wilson2da3b9b2011-04-14 09:41:17 +01004094i915_gem_object_pin_to_display_plane(struct drm_i915_gem_object *obj,
4095 u32 alignment,
Chris Wilson59354852018-02-20 13:42:06 +00004096 const struct i915_ggtt_view *view,
4097 unsigned int flags)
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08004098{
Chris Wilson058d88c2016-08-15 10:49:06 +01004099 struct i915_vma *vma;
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08004100 int ret;
4101
Chris Wilson4c7d62c2016-10-28 13:58:32 +01004102 lockdep_assert_held(&obj->base.dev->struct_mutex);
4103
Chris Wilsonbd3d2252017-10-13 21:26:14 +01004104 /* Mark the global pin early so that we account for the
Chris Wilsoncc98b412013-08-09 12:25:09 +01004105 * display coherency whilst setting up the cache domains.
4106 */
Chris Wilsonbd3d2252017-10-13 21:26:14 +01004107 obj->pin_global++;
Chris Wilsoncc98b412013-08-09 12:25:09 +01004108
Eric Anholta7ef0642011-03-29 16:59:54 -07004109 /* The display engine is not coherent with the LLC cache on gen6. As
4110 * a result, we make sure that the pinning that is about to occur is
4111 * done with uncached PTEs. This is lowest common denominator for all
4112 * chipsets.
4113 *
4114 * However for gen6+, we could do better by using the GFDT bit instead
4115 * of uncaching, which would allow us to flush all the LLC-cached data
4116 * with that bit in the PTE to main memory with just one PIPE_CONTROL.
4117 */
Chris Wilson651d7942013-08-08 14:41:10 +01004118 ret = i915_gem_object_set_cache_level(obj,
Tvrtko Ursulin86527442016-10-13 11:03:00 +01004119 HAS_WT(to_i915(obj->base.dev)) ?
4120 I915_CACHE_WT : I915_CACHE_NONE);
Chris Wilson058d88c2016-08-15 10:49:06 +01004121 if (ret) {
4122 vma = ERR_PTR(ret);
Chris Wilsonbd3d2252017-10-13 21:26:14 +01004123 goto err_unpin_global;
Chris Wilson058d88c2016-08-15 10:49:06 +01004124 }
Eric Anholta7ef0642011-03-29 16:59:54 -07004125
Chris Wilson2da3b9b2011-04-14 09:41:17 +01004126 /* As the user may map the buffer once pinned in the display plane
4127 * (e.g. libkms for the bootup splash), we have to ensure that we
Chris Wilson2efb8132016-08-18 17:17:06 +01004128 * always use map_and_fenceable for all scanout buffers. However,
4129 * it may simply be too big to fit into mappable, in which case
4130 * put it anyway and hope that userspace can cope (but always first
4131 * try to preserve the existing ABI).
Chris Wilson2da3b9b2011-04-14 09:41:17 +01004132 */
Chris Wilson2efb8132016-08-18 17:17:06 +01004133 vma = ERR_PTR(-ENOSPC);
Chris Wilson59354852018-02-20 13:42:06 +00004134 if ((flags & PIN_MAPPABLE) == 0 &&
4135 (!view || view->type == I915_GGTT_VIEW_NORMAL))
Chris Wilson2efb8132016-08-18 17:17:06 +01004136 vma = i915_gem_object_ggtt_pin(obj, view, 0, alignment,
Chris Wilson59354852018-02-20 13:42:06 +00004137 flags |
4138 PIN_MAPPABLE |
4139 PIN_NONBLOCK);
4140 if (IS_ERR(vma))
Chris Wilson767a2222016-11-07 11:01:28 +00004141 vma = i915_gem_object_ggtt_pin(obj, view, 0, alignment, flags);
Chris Wilson058d88c2016-08-15 10:49:06 +01004142 if (IS_ERR(vma))
Chris Wilsonbd3d2252017-10-13 21:26:14 +01004143 goto err_unpin_global;
Chris Wilson2da3b9b2011-04-14 09:41:17 +01004144
Chris Wilsond8923dc2016-08-18 17:17:07 +01004145 vma->display_alignment = max_t(u64, vma->display_alignment, alignment);
4146
Chris Wilson5a97bcc2017-02-22 11:40:46 +00004147 __i915_gem_object_flush_for_display(obj);
Chris Wilsonb118c1e2010-05-27 13:18:14 +01004148
Chris Wilson2da3b9b2011-04-14 09:41:17 +01004149 /* It should now be out of any other write domains, and we can update
4150 * the domain values for our changes.
4151 */
Christian Königc0a51fd2018-02-16 13:43:38 +01004152 obj->read_domains |= I915_GEM_DOMAIN_GTT;
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08004153
Chris Wilson058d88c2016-08-15 10:49:06 +01004154 return vma;
Chris Wilsoncc98b412013-08-09 12:25:09 +01004155
Chris Wilsonbd3d2252017-10-13 21:26:14 +01004156err_unpin_global:
4157 obj->pin_global--;
Chris Wilson058d88c2016-08-15 10:49:06 +01004158 return vma;
Chris Wilsoncc98b412013-08-09 12:25:09 +01004159}
4160
4161void
Chris Wilson058d88c2016-08-15 10:49:06 +01004162i915_gem_object_unpin_from_display_plane(struct i915_vma *vma)
Chris Wilsoncc98b412013-08-09 12:25:09 +01004163{
Chris Wilson49d73912016-11-29 09:50:08 +00004164 lockdep_assert_held(&vma->vm->i915->drm.struct_mutex);
Chris Wilson4c7d62c2016-10-28 13:58:32 +01004165
Chris Wilsonbd3d2252017-10-13 21:26:14 +01004166 if (WARN_ON(vma->obj->pin_global == 0))
Tvrtko Ursulin8a0c39b2015-04-13 11:50:09 +01004167 return;
4168
Chris Wilsonbd3d2252017-10-13 21:26:14 +01004169 if (--vma->obj->pin_global == 0)
Chris Wilsonf51455d2017-01-10 14:47:34 +00004170 vma->display_alignment = I915_GTT_MIN_ALIGNMENT;
Tvrtko Ursuline6617332015-03-23 11:10:33 +00004171
Chris Wilson383d5822016-08-18 17:17:08 +01004172 /* Bump the LRU to try and avoid premature eviction whilst flipping */
Chris Wilsonbefedbb2017-01-19 19:26:55 +00004173 i915_gem_object_bump_inactive_ggtt(vma->obj);
Chris Wilson383d5822016-08-18 17:17:08 +01004174
Chris Wilson058d88c2016-08-15 10:49:06 +01004175 i915_vma_unpin(vma);
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08004176}
4177
Eric Anholte47c68e2008-11-14 13:35:19 -08004178/**
4179 * Moves a single object to the CPU read, and possibly write domain.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01004180 * @obj: object to act on
4181 * @write: requesting write or read-only access
Eric Anholte47c68e2008-11-14 13:35:19 -08004182 *
4183 * This function returns when the move is complete, including waiting on
4184 * flushes to occur.
4185 */
Chris Wilsondabdfe02012-03-26 10:10:27 +02004186int
Chris Wilson919926a2010-11-12 13:42:53 +00004187i915_gem_object_set_to_cpu_domain(struct drm_i915_gem_object *obj, bool write)
Eric Anholte47c68e2008-11-14 13:35:19 -08004188{
Eric Anholte47c68e2008-11-14 13:35:19 -08004189 int ret;
4190
Chris Wilsone95433c2016-10-28 13:58:27 +01004191 lockdep_assert_held(&obj->base.dev->struct_mutex);
Chris Wilson4c7d62c2016-10-28 13:58:32 +01004192
Chris Wilsone95433c2016-10-28 13:58:27 +01004193 ret = i915_gem_object_wait(obj,
4194 I915_WAIT_INTERRUPTIBLE |
4195 I915_WAIT_LOCKED |
4196 (write ? I915_WAIT_ALL : 0),
4197 MAX_SCHEDULE_TIMEOUT,
4198 NULL);
Chris Wilson88241782011-01-07 17:09:48 +00004199 if (ret)
4200 return ret;
4201
Chris Wilsonef749212017-04-12 12:01:10 +01004202 flush_write_domain(obj, ~I915_GEM_DOMAIN_CPU);
Eric Anholte47c68e2008-11-14 13:35:19 -08004203
Eric Anholte47c68e2008-11-14 13:35:19 -08004204 /* Flush the CPU cache if it's still invalid. */
Christian Königc0a51fd2018-02-16 13:43:38 +01004205 if ((obj->read_domains & I915_GEM_DOMAIN_CPU) == 0) {
Chris Wilson57822dc2017-02-22 11:40:48 +00004206 i915_gem_clflush_object(obj, I915_CLFLUSH_SYNC);
Christian Königc0a51fd2018-02-16 13:43:38 +01004207 obj->read_domains |= I915_GEM_DOMAIN_CPU;
Eric Anholte47c68e2008-11-14 13:35:19 -08004208 }
4209
4210 /* It should now be out of any other write domains, and we can update
4211 * the domain values for our changes.
4212 */
Christian Königc0a51fd2018-02-16 13:43:38 +01004213 GEM_BUG_ON(obj->write_domain & ~I915_GEM_DOMAIN_CPU);
Eric Anholte47c68e2008-11-14 13:35:19 -08004214
4215 /* If we're writing through the CPU, then the GPU read domains will
4216 * need to be invalidated at next use.
4217 */
Chris Wilsone27ab732017-06-15 13:38:49 +01004218 if (write)
4219 __start_cpu_write(obj);
Eric Anholt2ef7eea2008-11-10 10:53:25 -08004220
4221 return 0;
4222}
4223
Eric Anholt673a3942008-07-30 12:06:12 -07004224/* Throttle our rendering by waiting until the ring has completed our requests
4225 * emitted over 20 msec ago.
4226 *
Eric Anholtb9624422009-06-03 07:27:35 +00004227 * Note that if we were to use the current jiffies each time around the loop,
4228 * we wouldn't escape the function with any frames outstanding if the time to
4229 * render a frame was over 20ms.
4230 *
Eric Anholt673a3942008-07-30 12:06:12 -07004231 * This should get us reasonable parallelism between CPU and GPU but also
4232 * relatively low latency when blocking on a particular request to finish.
4233 */
4234static int
Chris Wilsonf787a5f2010-09-24 16:02:42 +01004235i915_gem_ring_throttle(struct drm_device *dev, struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07004236{
Chris Wilsonfac5e232016-07-04 11:34:36 +01004237 struct drm_i915_private *dev_priv = to_i915(dev);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01004238 struct drm_i915_file_private *file_priv = file->driver_priv;
Chris Wilsond0bc54f2015-05-21 21:01:48 +01004239 unsigned long recent_enough = jiffies - DRM_I915_THROTTLE_JIFFIES;
Chris Wilsone61e0f52018-02-21 09:56:36 +00004240 struct i915_request *request, *target = NULL;
Chris Wilsone95433c2016-10-28 13:58:27 +01004241 long ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004242
Chris Wilsonf4457ae2016-04-13 17:35:08 +01004243 /* ABI: return -EIO if already wedged */
4244 if (i915_terminally_wedged(&dev_priv->gpu_error))
4245 return -EIO;
Chris Wilsone110e8d2011-01-26 15:39:14 +00004246
Chris Wilson1c255952010-09-26 11:03:27 +01004247 spin_lock(&file_priv->mm.lock);
Chris Wilsonc8659ef2017-03-02 12:25:25 +00004248 list_for_each_entry(request, &file_priv->mm.request_list, client_link) {
Eric Anholtb9624422009-06-03 07:27:35 +00004249 if (time_after_eq(request->emitted_jiffies, recent_enough))
4250 break;
4251
Chris Wilsonc8659ef2017-03-02 12:25:25 +00004252 if (target) {
4253 list_del(&target->client_link);
4254 target->file_priv = NULL;
4255 }
John Harrisonfcfa423c2015-05-29 17:44:12 +01004256
John Harrison54fb2412014-11-24 18:49:27 +00004257 target = request;
Eric Anholtb9624422009-06-03 07:27:35 +00004258 }
John Harrisonff865882014-11-24 18:49:28 +00004259 if (target)
Chris Wilsone61e0f52018-02-21 09:56:36 +00004260 i915_request_get(target);
Chris Wilson1c255952010-09-26 11:03:27 +01004261 spin_unlock(&file_priv->mm.lock);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01004262
John Harrison54fb2412014-11-24 18:49:27 +00004263 if (target == NULL)
Chris Wilsonf787a5f2010-09-24 16:02:42 +01004264 return 0;
4265
Chris Wilsone61e0f52018-02-21 09:56:36 +00004266 ret = i915_request_wait(target,
Chris Wilsone95433c2016-10-28 13:58:27 +01004267 I915_WAIT_INTERRUPTIBLE,
4268 MAX_SCHEDULE_TIMEOUT);
Chris Wilsone61e0f52018-02-21 09:56:36 +00004269 i915_request_put(target);
John Harrisonff865882014-11-24 18:49:28 +00004270
Chris Wilsone95433c2016-10-28 13:58:27 +01004271 return ret < 0 ? ret : 0;
Eric Anholt673a3942008-07-30 12:06:12 -07004272}
4273
Chris Wilson058d88c2016-08-15 10:49:06 +01004274struct i915_vma *
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02004275i915_gem_object_ggtt_pin(struct drm_i915_gem_object *obj,
4276 const struct i915_ggtt_view *view,
Chris Wilson91b2db62016-08-04 16:32:23 +01004277 u64 size,
Chris Wilson2ffffd02016-08-04 16:32:22 +01004278 u64 alignment,
4279 u64 flags)
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02004280{
Chris Wilsonad16d2e2016-10-13 09:55:04 +01004281 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
Chris Wilson82ad6442018-06-05 16:37:58 +01004282 struct i915_address_space *vm = &dev_priv->ggtt.vm;
Chris Wilson59bfa122016-08-04 16:32:31 +01004283 struct i915_vma *vma;
4284 int ret;
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03004285
Chris Wilson4c7d62c2016-10-28 13:58:32 +01004286 lockdep_assert_held(&obj->base.dev->struct_mutex);
4287
Chris Wilsonac87a6fd2018-02-20 13:42:05 +00004288 if (flags & PIN_MAPPABLE &&
4289 (!view || view->type == I915_GGTT_VIEW_NORMAL)) {
Chris Wilson43ae70d92017-10-09 09:44:01 +01004290 /* If the required space is larger than the available
4291 * aperture, we will not able to find a slot for the
4292 * object and unbinding the object now will be in
4293 * vain. Worse, doing so may cause us to ping-pong
4294 * the object in and out of the Global GTT and
4295 * waste a lot of cycles under the mutex.
4296 */
4297 if (obj->base.size > dev_priv->ggtt.mappable_end)
4298 return ERR_PTR(-E2BIG);
4299
4300 /* If NONBLOCK is set the caller is optimistically
4301 * trying to cache the full object within the mappable
4302 * aperture, and *must* have a fallback in place for
4303 * situations where we cannot bind the object. We
4304 * can be a little more lax here and use the fallback
4305 * more often to avoid costly migrations of ourselves
4306 * and other objects within the aperture.
4307 *
4308 * Half-the-aperture is used as a simple heuristic.
4309 * More interesting would to do search for a free
4310 * block prior to making the commitment to unbind.
4311 * That caters for the self-harm case, and with a
4312 * little more heuristics (e.g. NOFAULT, NOEVICT)
4313 * we could try to minimise harm to others.
4314 */
4315 if (flags & PIN_NONBLOCK &&
4316 obj->base.size > dev_priv->ggtt.mappable_end / 2)
4317 return ERR_PTR(-ENOSPC);
4318 }
4319
Chris Wilson718659a2017-01-16 15:21:28 +00004320 vma = i915_vma_instance(obj, vm, view);
Chris Wilsone0216b72017-01-19 19:26:57 +00004321 if (unlikely(IS_ERR(vma)))
Chris Wilson058d88c2016-08-15 10:49:06 +01004322 return vma;
Chris Wilson59bfa122016-08-04 16:32:31 +01004323
4324 if (i915_vma_misplaced(vma, size, alignment, flags)) {
Chris Wilson43ae70d92017-10-09 09:44:01 +01004325 if (flags & PIN_NONBLOCK) {
4326 if (i915_vma_is_pinned(vma) || i915_vma_is_active(vma))
4327 return ERR_PTR(-ENOSPC);
Chris Wilson59bfa122016-08-04 16:32:31 +01004328
Chris Wilson43ae70d92017-10-09 09:44:01 +01004329 if (flags & PIN_MAPPABLE &&
Chris Wilson944397f2017-01-09 16:16:11 +00004330 vma->fence_size > dev_priv->ggtt.mappable_end / 2)
Chris Wilsonad16d2e2016-10-13 09:55:04 +01004331 return ERR_PTR(-ENOSPC);
4332 }
4333
Chris Wilson59bfa122016-08-04 16:32:31 +01004334 WARN(i915_vma_is_pinned(vma),
4335 "bo is already pinned in ggtt with incorrect alignment:"
Chris Wilson05a20d02016-08-18 17:16:55 +01004336 " offset=%08x, req.alignment=%llx,"
4337 " req.map_and_fenceable=%d, vma->map_and_fenceable=%d\n",
4338 i915_ggtt_offset(vma), alignment,
Chris Wilson59bfa122016-08-04 16:32:31 +01004339 !!(flags & PIN_MAPPABLE),
Chris Wilson05a20d02016-08-18 17:16:55 +01004340 i915_vma_is_map_and_fenceable(vma));
Chris Wilson59bfa122016-08-04 16:32:31 +01004341 ret = i915_vma_unbind(vma);
4342 if (ret)
Chris Wilson058d88c2016-08-15 10:49:06 +01004343 return ERR_PTR(ret);
Chris Wilson59bfa122016-08-04 16:32:31 +01004344 }
4345
Chris Wilson058d88c2016-08-15 10:49:06 +01004346 ret = i915_vma_pin(vma, size, alignment, flags | PIN_GLOBAL);
4347 if (ret)
4348 return ERR_PTR(ret);
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02004349
Chris Wilson058d88c2016-08-15 10:49:06 +01004350 return vma;
Eric Anholt673a3942008-07-30 12:06:12 -07004351}
4352
Chris Wilsonedf6b762016-08-09 09:23:33 +01004353static __always_inline unsigned int __busy_read_flag(unsigned int id)
Chris Wilson3fdc13c2016-08-05 10:14:18 +01004354{
4355 /* Note that we could alias engines in the execbuf API, but
4356 * that would be very unwise as it prevents userspace from
4357 * fine control over engine selection. Ahem.
4358 *
4359 * This should be something like EXEC_MAX_ENGINE instead of
4360 * I915_NUM_ENGINES.
4361 */
4362 BUILD_BUG_ON(I915_NUM_ENGINES > 16);
4363 return 0x10000 << id;
4364}
4365
4366static __always_inline unsigned int __busy_write_id(unsigned int id)
4367{
Chris Wilson70cb4722016-08-09 18:08:25 +01004368 /* The uABI guarantees an active writer is also amongst the read
4369 * engines. This would be true if we accessed the activity tracking
4370 * under the lock, but as we perform the lookup of the object and
4371 * its activity locklessly we can not guarantee that the last_write
4372 * being active implies that we have set the same engine flag from
4373 * last_read - hence we always set both read and write busy for
4374 * last_write.
4375 */
4376 return id | __busy_read_flag(id);
Chris Wilson3fdc13c2016-08-05 10:14:18 +01004377}
4378
Chris Wilsonedf6b762016-08-09 09:23:33 +01004379static __always_inline unsigned int
Chris Wilsond07f0e52016-10-28 13:58:44 +01004380__busy_set_if_active(const struct dma_fence *fence,
Chris Wilson3fdc13c2016-08-05 10:14:18 +01004381 unsigned int (*flag)(unsigned int id))
4382{
Chris Wilsone61e0f52018-02-21 09:56:36 +00004383 struct i915_request *rq;
Chris Wilson12555012016-08-16 09:50:40 +01004384
Chris Wilsond07f0e52016-10-28 13:58:44 +01004385 /* We have to check the current hw status of the fence as the uABI
4386 * guarantees forward progress. We could rely on the idle worker
4387 * to eventually flush us, but to minimise latency just ask the
4388 * hardware.
4389 *
4390 * Note we only report on the status of native fences.
4391 */
4392 if (!dma_fence_is_i915(fence))
Chris Wilson12555012016-08-16 09:50:40 +01004393 return 0;
4394
Chris Wilsond07f0e52016-10-28 13:58:44 +01004395 /* opencode to_request() in order to avoid const warnings */
Chris Wilsone61e0f52018-02-21 09:56:36 +00004396 rq = container_of(fence, struct i915_request, fence);
4397 if (i915_request_completed(rq))
Chris Wilsond07f0e52016-10-28 13:58:44 +01004398 return 0;
4399
Chris Wilson1d39f282017-04-11 13:43:06 +01004400 return flag(rq->engine->uabi_id);
Chris Wilson3fdc13c2016-08-05 10:14:18 +01004401}
4402
Chris Wilsonedf6b762016-08-09 09:23:33 +01004403static __always_inline unsigned int
Chris Wilsond07f0e52016-10-28 13:58:44 +01004404busy_check_reader(const struct dma_fence *fence)
Chris Wilson3fdc13c2016-08-05 10:14:18 +01004405{
Chris Wilsond07f0e52016-10-28 13:58:44 +01004406 return __busy_set_if_active(fence, __busy_read_flag);
Chris Wilson3fdc13c2016-08-05 10:14:18 +01004407}
4408
Chris Wilsonedf6b762016-08-09 09:23:33 +01004409static __always_inline unsigned int
Chris Wilsond07f0e52016-10-28 13:58:44 +01004410busy_check_writer(const struct dma_fence *fence)
Chris Wilson3fdc13c2016-08-05 10:14:18 +01004411{
Chris Wilsond07f0e52016-10-28 13:58:44 +01004412 if (!fence)
4413 return 0;
4414
4415 return __busy_set_if_active(fence, __busy_write_id);
Chris Wilson3fdc13c2016-08-05 10:14:18 +01004416}
4417
Eric Anholt673a3942008-07-30 12:06:12 -07004418int
Eric Anholt673a3942008-07-30 12:06:12 -07004419i915_gem_busy_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00004420 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07004421{
4422 struct drm_i915_gem_busy *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00004423 struct drm_i915_gem_object *obj;
Chris Wilsond07f0e52016-10-28 13:58:44 +01004424 struct reservation_object_list *list;
4425 unsigned int seq;
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004426 int err;
Eric Anholt673a3942008-07-30 12:06:12 -07004427
Chris Wilsond07f0e52016-10-28 13:58:44 +01004428 err = -ENOENT;
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004429 rcu_read_lock();
4430 obj = i915_gem_object_lookup_rcu(file, args->handle);
Chris Wilsond07f0e52016-10-28 13:58:44 +01004431 if (!obj)
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004432 goto out;
Chris Wilsond07f0e52016-10-28 13:58:44 +01004433
4434 /* A discrepancy here is that we do not report the status of
4435 * non-i915 fences, i.e. even though we may report the object as idle,
4436 * a call to set-domain may still stall waiting for foreign rendering.
4437 * This also means that wait-ioctl may report an object as busy,
4438 * where busy-ioctl considers it idle.
4439 *
4440 * We trade the ability to warn of foreign fences to report on which
4441 * i915 engines are active for the object.
4442 *
4443 * Alternatively, we can trade that extra information on read/write
4444 * activity with
4445 * args->busy =
4446 * !reservation_object_test_signaled_rcu(obj->resv, true);
4447 * to report the overall busyness. This is what the wait-ioctl does.
4448 *
4449 */
4450retry:
4451 seq = raw_read_seqcount(&obj->resv->seq);
4452
4453 /* Translate the exclusive fence to the READ *and* WRITE engine */
4454 args->busy = busy_check_writer(rcu_dereference(obj->resv->fence_excl));
4455
4456 /* Translate shared fences to READ set of engines */
4457 list = rcu_dereference(obj->resv->fence);
4458 if (list) {
4459 unsigned int shared_count = list->shared_count, i;
4460
4461 for (i = 0; i < shared_count; ++i) {
4462 struct dma_fence *fence =
4463 rcu_dereference(list->shared[i]);
4464
4465 args->busy |= busy_check_reader(fence);
4466 }
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004467 }
Zou Nan haid1b851f2010-05-21 09:08:57 +08004468
Chris Wilsond07f0e52016-10-28 13:58:44 +01004469 if (args->busy && read_seqcount_retry(&obj->resv->seq, seq))
4470 goto retry;
Chris Wilson426960b2016-01-15 16:51:46 +00004471
Chris Wilsond07f0e52016-10-28 13:58:44 +01004472 err = 0;
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004473out:
4474 rcu_read_unlock();
4475 return err;
Eric Anholt673a3942008-07-30 12:06:12 -07004476}
4477
4478int
4479i915_gem_throttle_ioctl(struct drm_device *dev, void *data,
4480 struct drm_file *file_priv)
4481{
Akshay Joshi0206e352011-08-16 15:34:10 -04004482 return i915_gem_ring_throttle(dev, file_priv);
Eric Anholt673a3942008-07-30 12:06:12 -07004483}
4484
Chris Wilson3ef94da2009-09-14 16:50:29 +01004485int
4486i915_gem_madvise_ioctl(struct drm_device *dev, void *data,
4487 struct drm_file *file_priv)
4488{
Chris Wilsonfac5e232016-07-04 11:34:36 +01004489 struct drm_i915_private *dev_priv = to_i915(dev);
Chris Wilson3ef94da2009-09-14 16:50:29 +01004490 struct drm_i915_gem_madvise *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00004491 struct drm_i915_gem_object *obj;
Chris Wilson1233e2d2016-10-28 13:58:37 +01004492 int err;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004493
4494 switch (args->madv) {
4495 case I915_MADV_DONTNEED:
4496 case I915_MADV_WILLNEED:
4497 break;
4498 default:
4499 return -EINVAL;
4500 }
4501
Chris Wilson03ac0642016-07-20 13:31:51 +01004502 obj = i915_gem_object_lookup(file_priv, args->handle);
Chris Wilson1233e2d2016-10-28 13:58:37 +01004503 if (!obj)
4504 return -ENOENT;
4505
4506 err = mutex_lock_interruptible(&obj->mm.lock);
4507 if (err)
4508 goto out;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004509
Chris Wilsonf1fa4f42017-10-13 21:26:13 +01004510 if (i915_gem_object_has_pages(obj) &&
Chris Wilson3e510a82016-08-05 10:14:23 +01004511 i915_gem_object_is_tiled(obj) &&
Daniel Vetter656bfa32014-11-20 09:26:30 +01004512 dev_priv->quirks & QUIRK_PIN_SWIZZLED_PAGES) {
Chris Wilsonbc0629a2016-11-01 10:03:17 +00004513 if (obj->mm.madv == I915_MADV_WILLNEED) {
4514 GEM_BUG_ON(!obj->mm.quirked);
Chris Wilsona4f5ea62016-10-28 13:58:35 +01004515 __i915_gem_object_unpin_pages(obj);
Chris Wilsonbc0629a2016-11-01 10:03:17 +00004516 obj->mm.quirked = false;
4517 }
4518 if (args->madv == I915_MADV_WILLNEED) {
Chris Wilson2c3a3f42016-11-04 10:30:01 +00004519 GEM_BUG_ON(obj->mm.quirked);
Chris Wilsona4f5ea62016-10-28 13:58:35 +01004520 __i915_gem_object_pin_pages(obj);
Chris Wilsonbc0629a2016-11-01 10:03:17 +00004521 obj->mm.quirked = true;
4522 }
Daniel Vetter656bfa32014-11-20 09:26:30 +01004523 }
4524
Chris Wilsona4f5ea62016-10-28 13:58:35 +01004525 if (obj->mm.madv != __I915_MADV_PURGED)
4526 obj->mm.madv = args->madv;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004527
Chris Wilson6c085a72012-08-20 11:40:46 +02004528 /* if the object is no longer attached, discard its backing storage */
Chris Wilsonf1fa4f42017-10-13 21:26:13 +01004529 if (obj->mm.madv == I915_MADV_DONTNEED &&
4530 !i915_gem_object_has_pages(obj))
Chris Wilson2d7ef392009-09-20 23:13:10 +01004531 i915_gem_object_truncate(obj);
4532
Chris Wilsona4f5ea62016-10-28 13:58:35 +01004533 args->retained = obj->mm.madv != __I915_MADV_PURGED;
Chris Wilson1233e2d2016-10-28 13:58:37 +01004534 mutex_unlock(&obj->mm.lock);
Chris Wilsonbb6baf72009-09-22 14:24:13 +01004535
Chris Wilson1233e2d2016-10-28 13:58:37 +01004536out:
Chris Wilsonf8c417c2016-07-20 13:31:53 +01004537 i915_gem_object_put(obj);
Chris Wilson1233e2d2016-10-28 13:58:37 +01004538 return err;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004539}
4540
Chris Wilson5b8c8ae2016-11-16 19:07:04 +00004541static void
Chris Wilsone61e0f52018-02-21 09:56:36 +00004542frontbuffer_retire(struct i915_gem_active *active, struct i915_request *request)
Chris Wilson5b8c8ae2016-11-16 19:07:04 +00004543{
4544 struct drm_i915_gem_object *obj =
4545 container_of(active, typeof(*obj), frontbuffer_write);
4546
Chris Wilsond59b21e2017-02-22 11:40:49 +00004547 intel_fb_obj_flush(obj, ORIGIN_CS);
Chris Wilson5b8c8ae2016-11-16 19:07:04 +00004548}
4549
Chris Wilson37e680a2012-06-07 15:38:42 +01004550void i915_gem_object_init(struct drm_i915_gem_object *obj,
4551 const struct drm_i915_gem_object_ops *ops)
Chris Wilson0327d6b2012-08-11 15:41:06 +01004552{
Chris Wilson1233e2d2016-10-28 13:58:37 +01004553 mutex_init(&obj->mm.lock);
4554
Ben Widawsky2f633152013-07-17 12:19:03 -07004555 INIT_LIST_HEAD(&obj->vma_list);
Chris Wilsond1b48c12017-08-16 09:52:08 +01004556 INIT_LIST_HEAD(&obj->lut_list);
Chris Wilson8d9d5742015-04-07 16:20:38 +01004557 INIT_LIST_HEAD(&obj->batch_pool_link);
Chris Wilson0327d6b2012-08-11 15:41:06 +01004558
Chris Wilson8811d612018-11-09 09:03:11 +00004559 init_rcu_head(&obj->rcu);
4560
Chris Wilson37e680a2012-06-07 15:38:42 +01004561 obj->ops = ops;
4562
Chris Wilsond07f0e52016-10-28 13:58:44 +01004563 reservation_object_init(&obj->__builtin_resv);
4564 obj->resv = &obj->__builtin_resv;
4565
Chris Wilson50349242016-08-18 17:17:04 +01004566 obj->frontbuffer_ggtt_origin = ORIGIN_GTT;
Chris Wilson5b8c8ae2016-11-16 19:07:04 +00004567 init_request_active(&obj->frontbuffer_write, frontbuffer_retire);
Chris Wilsona4f5ea62016-10-28 13:58:35 +01004568
4569 obj->mm.madv = I915_MADV_WILLNEED;
4570 INIT_RADIX_TREE(&obj->mm.get_page.radix, GFP_KERNEL | __GFP_NOWARN);
4571 mutex_init(&obj->mm.get_page.lock);
Chris Wilson0327d6b2012-08-11 15:41:06 +01004572
Dave Gordonf19ec8c2016-07-04 11:34:37 +01004573 i915_gem_info_add_obj(to_i915(obj->base.dev), obj->base.size);
Chris Wilson0327d6b2012-08-11 15:41:06 +01004574}
4575
Chris Wilson37e680a2012-06-07 15:38:42 +01004576static const struct drm_i915_gem_object_ops i915_gem_object_ops = {
Tvrtko Ursulin3599a912016-11-01 14:44:10 +00004577 .flags = I915_GEM_OBJECT_HAS_STRUCT_PAGE |
4578 I915_GEM_OBJECT_IS_SHRINKABLE,
Chris Wilson7c55e2c2017-03-07 12:03:38 +00004579
Chris Wilson37e680a2012-06-07 15:38:42 +01004580 .get_pages = i915_gem_object_get_pages_gtt,
4581 .put_pages = i915_gem_object_put_pages_gtt,
Chris Wilson7c55e2c2017-03-07 12:03:38 +00004582
4583 .pwrite = i915_gem_object_pwrite_gtt,
Chris Wilson37e680a2012-06-07 15:38:42 +01004584};
4585
Matthew Auld465c4032017-10-06 23:18:14 +01004586static int i915_gem_object_create_shmem(struct drm_device *dev,
4587 struct drm_gem_object *obj,
4588 size_t size)
4589{
4590 struct drm_i915_private *i915 = to_i915(dev);
4591 unsigned long flags = VM_NORESERVE;
4592 struct file *filp;
4593
4594 drm_gem_private_object_init(dev, obj, size);
4595
4596 if (i915->mm.gemfs)
4597 filp = shmem_file_setup_with_mnt(i915->mm.gemfs, "i915", size,
4598 flags);
4599 else
4600 filp = shmem_file_setup("i915", size, flags);
4601
4602 if (IS_ERR(filp))
4603 return PTR_ERR(filp);
4604
4605 obj->filp = filp;
4606
4607 return 0;
4608}
4609
Chris Wilsonb4bcbe22016-10-18 13:02:49 +01004610struct drm_i915_gem_object *
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +00004611i915_gem_object_create(struct drm_i915_private *dev_priv, u64 size)
Daniel Vetterac52bc52010-04-09 19:05:06 +00004612{
Daniel Vetterc397b902010-04-09 19:05:07 +00004613 struct drm_i915_gem_object *obj;
Hugh Dickins5949eac2011-06-27 16:18:18 -07004614 struct address_space *mapping;
Chris Wilsonb8f55be2017-08-11 12:11:16 +01004615 unsigned int cache_level;
Daniel Vetter1a240d42012-11-29 22:18:51 +01004616 gfp_t mask;
Chris Wilsonfe3db792016-04-25 13:32:13 +01004617 int ret;
Daniel Vetterc397b902010-04-09 19:05:07 +00004618
Chris Wilsonb4bcbe22016-10-18 13:02:49 +01004619 /* There is a prevalence of the assumption that we fit the object's
4620 * page count inside a 32bit _signed_ variable. Let's document this and
4621 * catch if we ever need to fix it. In the meantime, if you do spot
4622 * such a local variable, please consider fixing!
4623 */
Tvrtko Ursulin7a3ee5d2017-03-30 17:31:30 +01004624 if (size >> PAGE_SHIFT > INT_MAX)
Chris Wilsonb4bcbe22016-10-18 13:02:49 +01004625 return ERR_PTR(-E2BIG);
4626
4627 if (overflows_type(size, obj->base.size))
4628 return ERR_PTR(-E2BIG);
4629
Tvrtko Ursulin187685c2016-12-01 14:16:36 +00004630 obj = i915_gem_object_alloc(dev_priv);
Daniel Vetterc397b902010-04-09 19:05:07 +00004631 if (obj == NULL)
Chris Wilsonfe3db792016-04-25 13:32:13 +01004632 return ERR_PTR(-ENOMEM);
Daniel Vetterc397b902010-04-09 19:05:07 +00004633
Matthew Auld465c4032017-10-06 23:18:14 +01004634 ret = i915_gem_object_create_shmem(&dev_priv->drm, &obj->base, size);
Chris Wilsonfe3db792016-04-25 13:32:13 +01004635 if (ret)
4636 goto fail;
Daniel Vetterc397b902010-04-09 19:05:07 +00004637
Chris Wilsonbed1ea92012-05-24 20:48:12 +01004638 mask = GFP_HIGHUSER | __GFP_RECLAIMABLE;
Jani Nikulac0f86832016-12-07 12:13:04 +02004639 if (IS_I965GM(dev_priv) || IS_I965G(dev_priv)) {
Chris Wilsonbed1ea92012-05-24 20:48:12 +01004640 /* 965gm cannot relocate objects above 4GiB. */
4641 mask &= ~__GFP_HIGHMEM;
4642 mask |= __GFP_DMA32;
4643 }
4644
Al Viro93c76a32015-12-04 23:45:44 -05004645 mapping = obj->base.filp->f_mapping;
Chris Wilsonbed1ea92012-05-24 20:48:12 +01004646 mapping_set_gfp_mask(mapping, mask);
Chris Wilson4846bf02017-06-09 12:03:46 +01004647 GEM_BUG_ON(!(mapping_gfp_mask(mapping) & __GFP_RECLAIM));
Hugh Dickins5949eac2011-06-27 16:18:18 -07004648
Chris Wilson37e680a2012-06-07 15:38:42 +01004649 i915_gem_object_init(obj, &i915_gem_object_ops);
Chris Wilson73aa8082010-09-30 11:46:12 +01004650
Christian Königc0a51fd2018-02-16 13:43:38 +01004651 obj->write_domain = I915_GEM_DOMAIN_CPU;
4652 obj->read_domains = I915_GEM_DOMAIN_CPU;
Daniel Vetterc397b902010-04-09 19:05:07 +00004653
Chris Wilsonb8f55be2017-08-11 12:11:16 +01004654 if (HAS_LLC(dev_priv))
Eugeni Dodonov3d29b842012-01-17 14:43:53 -02004655 /* On some devices, we can have the GPU use the LLC (the CPU
Eric Anholta1871112011-03-29 16:59:55 -07004656 * cache) for about a 10% performance improvement
4657 * compared to uncached. Graphics requests other than
4658 * display scanout are coherent with the CPU in
4659 * accessing this cache. This means in this mode we
4660 * don't need to clflush on the CPU side, and on the
4661 * GPU side we only need to flush internal caches to
4662 * get data visible to the CPU.
4663 *
4664 * However, we maintain the display planes as UC, and so
4665 * need to rebind when first used as such.
4666 */
Chris Wilsonb8f55be2017-08-11 12:11:16 +01004667 cache_level = I915_CACHE_LLC;
4668 else
4669 cache_level = I915_CACHE_NONE;
Eric Anholta1871112011-03-29 16:59:55 -07004670
Chris Wilsonb8f55be2017-08-11 12:11:16 +01004671 i915_gem_object_set_cache_coherency(obj, cache_level);
Chris Wilsone27ab732017-06-15 13:38:49 +01004672
Daniel Vetterd861e332013-07-24 23:25:03 +02004673 trace_i915_gem_object_create(obj);
4674
Chris Wilson05394f32010-11-08 19:18:58 +00004675 return obj;
Chris Wilsonfe3db792016-04-25 13:32:13 +01004676
4677fail:
4678 i915_gem_object_free(obj);
Chris Wilsonfe3db792016-04-25 13:32:13 +01004679 return ERR_PTR(ret);
Daniel Vetterac52bc52010-04-09 19:05:06 +00004680}
4681
Chris Wilson340fbd82014-05-22 09:16:52 +01004682static bool discard_backing_storage(struct drm_i915_gem_object *obj)
4683{
4684 /* If we are the last user of the backing storage (be it shmemfs
4685 * pages or stolen etc), we know that the pages are going to be
4686 * immediately released. In this case, we can then skip copying
4687 * back the contents from the GPU.
4688 */
4689
Chris Wilsona4f5ea62016-10-28 13:58:35 +01004690 if (obj->mm.madv != I915_MADV_WILLNEED)
Chris Wilson340fbd82014-05-22 09:16:52 +01004691 return false;
4692
4693 if (obj->base.filp == NULL)
4694 return true;
4695
4696 /* At first glance, this looks racy, but then again so would be
4697 * userspace racing mmap against close. However, the first external
4698 * reference to the filp can only be obtained through the
4699 * i915_gem_mmap_ioctl() which safeguards us against the user
4700 * acquiring such a reference whilst we are in the middle of
4701 * freeing the object.
4702 */
4703 return atomic_long_read(&obj->base.filp->f_count) == 1;
4704}
4705
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004706static void __i915_gem_free_objects(struct drm_i915_private *i915,
4707 struct llist_node *freed)
Chris Wilsonbe726152010-07-23 23:18:50 +01004708{
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004709 struct drm_i915_gem_object *obj, *on;
Chris Wilsonbe726152010-07-23 23:18:50 +01004710
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004711 intel_runtime_pm_get(i915);
Chris Wilsoncc731f52017-10-13 21:26:21 +01004712 llist_for_each_entry_safe(obj, on, freed, freed) {
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004713 struct i915_vma *vma, *vn;
Paulo Zanonif65c9162013-11-27 18:20:34 -02004714
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004715 trace_i915_gem_object_destroy(obj);
4716
Chris Wilsoncc731f52017-10-13 21:26:21 +01004717 mutex_lock(&i915->drm.struct_mutex);
4718
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004719 GEM_BUG_ON(i915_gem_object_is_active(obj));
4720 list_for_each_entry_safe(vma, vn,
4721 &obj->vma_list, obj_link) {
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004722 GEM_BUG_ON(i915_vma_is_active(vma));
4723 vma->flags &= ~I915_VMA_PIN_MASK;
Chris Wilson3365e222018-05-03 20:51:14 +01004724 i915_vma_destroy(vma);
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004725 }
Chris Wilsondb6c2b42016-11-01 11:54:00 +00004726 GEM_BUG_ON(!list_empty(&obj->vma_list));
4727 GEM_BUG_ON(!RB_EMPTY_ROOT(&obj->vma_tree));
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004728
Chris Wilsonf2123812017-10-16 12:40:37 +01004729 /* This serializes freeing with the shrinker. Since the free
4730 * is delayed, first by RCU then by the workqueue, we want the
4731 * shrinker to be able to free pages of unreferenced objects,
4732 * or else we may oom whilst there are plenty of deferred
4733 * freed objects.
4734 */
4735 if (i915_gem_object_has_pages(obj)) {
4736 spin_lock(&i915->mm.obj_lock);
4737 list_del_init(&obj->mm.link);
4738 spin_unlock(&i915->mm.obj_lock);
4739 }
4740
Chris Wilsoncc731f52017-10-13 21:26:21 +01004741 mutex_unlock(&i915->drm.struct_mutex);
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004742
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004743 GEM_BUG_ON(obj->bind_count);
Chris Wilsona65adaf2017-10-09 09:43:57 +01004744 GEM_BUG_ON(obj->userfault_count);
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004745 GEM_BUG_ON(atomic_read(&obj->frontbuffer_bits));
Chris Wilson67b48042017-08-22 12:05:16 +01004746 GEM_BUG_ON(!list_empty(&obj->lut_list));
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004747
4748 if (obj->ops->release)
4749 obj->ops->release(obj);
4750
4751 if (WARN_ON(i915_gem_object_has_pinned_pages(obj)))
4752 atomic_set(&obj->mm.pages_pin_count, 0);
Chris Wilson548625e2016-11-01 12:11:34 +00004753 __i915_gem_object_put_pages(obj, I915_MM_NORMAL);
Chris Wilsonf1fa4f42017-10-13 21:26:13 +01004754 GEM_BUG_ON(i915_gem_object_has_pages(obj));
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004755
4756 if (obj->base.import_attach)
4757 drm_prime_gem_destroy(&obj->base, NULL);
4758
Chris Wilsond07f0e52016-10-28 13:58:44 +01004759 reservation_object_fini(&obj->__builtin_resv);
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004760 drm_gem_object_release(&obj->base);
4761 i915_gem_info_remove_obj(i915, obj->base.size);
4762
4763 kfree(obj->bit_17);
4764 i915_gem_object_free(obj);
Chris Wilsoncc731f52017-10-13 21:26:21 +01004765
Chris Wilsonc9c704712018-02-19 22:06:31 +00004766 GEM_BUG_ON(!atomic_read(&i915->mm.free_count));
4767 atomic_dec(&i915->mm.free_count);
4768
Chris Wilsoncc731f52017-10-13 21:26:21 +01004769 if (on)
4770 cond_resched();
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004771 }
Chris Wilsoncc731f52017-10-13 21:26:21 +01004772 intel_runtime_pm_put(i915);
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004773}
4774
4775static void i915_gem_flush_free_objects(struct drm_i915_private *i915)
4776{
4777 struct llist_node *freed;
4778
Chris Wilson87701b42017-10-13 21:26:20 +01004779 /* Free the oldest, most stale object to keep the free_list short */
4780 freed = NULL;
4781 if (!llist_empty(&i915->mm.free_list)) { /* quick test for hotpath */
4782 /* Only one consumer of llist_del_first() allowed */
4783 spin_lock(&i915->mm.free_lock);
4784 freed = llist_del_first(&i915->mm.free_list);
4785 spin_unlock(&i915->mm.free_lock);
4786 }
4787 if (unlikely(freed)) {
4788 freed->next = NULL;
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004789 __i915_gem_free_objects(i915, freed);
Chris Wilson87701b42017-10-13 21:26:20 +01004790 }
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004791}
4792
4793static void __i915_gem_free_work(struct work_struct *work)
4794{
4795 struct drm_i915_private *i915 =
4796 container_of(work, struct drm_i915_private, mm.free_work);
4797 struct llist_node *freed;
Chris Wilson26e12f82011-03-20 11:20:19 +00004798
Chris Wilson2ef1e722018-01-15 20:57:59 +00004799 /*
4800 * All file-owned VMA should have been released by this point through
Chris Wilsonb1f788c2016-08-04 07:52:45 +01004801 * i915_gem_close_object(), or earlier by i915_gem_context_close().
4802 * However, the object may also be bound into the global GTT (e.g.
4803 * older GPUs without per-process support, or for direct access through
4804 * the GTT either for the user or for scanout). Those VMA still need to
4805 * unbound now.
4806 */
Chris Wilson1488fc02012-04-24 15:47:31 +01004807
Chris Wilsonf991c492017-11-06 11:15:08 +00004808 spin_lock(&i915->mm.free_lock);
Chris Wilson5ad08be2017-04-07 11:25:51 +01004809 while ((freed = llist_del_all(&i915->mm.free_list))) {
Chris Wilsonf991c492017-11-06 11:15:08 +00004810 spin_unlock(&i915->mm.free_lock);
4811
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004812 __i915_gem_free_objects(i915, freed);
Chris Wilson5ad08be2017-04-07 11:25:51 +01004813 if (need_resched())
Chris Wilsonf991c492017-11-06 11:15:08 +00004814 return;
4815
4816 spin_lock(&i915->mm.free_lock);
Chris Wilson5ad08be2017-04-07 11:25:51 +01004817 }
Chris Wilsonf991c492017-11-06 11:15:08 +00004818 spin_unlock(&i915->mm.free_lock);
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004819}
4820
4821static void __i915_gem_free_object_rcu(struct rcu_head *head)
4822{
4823 struct drm_i915_gem_object *obj =
4824 container_of(head, typeof(*obj), rcu);
4825 struct drm_i915_private *i915 = to_i915(obj->base.dev);
4826
Chris Wilson2ef1e722018-01-15 20:57:59 +00004827 /*
Chris Wilson8811d612018-11-09 09:03:11 +00004828 * We reuse obj->rcu for the freed list, so we had better not treat
4829 * it like a rcu_head from this point forwards. And we expect all
4830 * objects to be freed via this path.
4831 */
4832 destroy_rcu_head(&obj->rcu);
4833
4834 /*
Chris Wilson2ef1e722018-01-15 20:57:59 +00004835 * Since we require blocking on struct_mutex to unbind the freed
4836 * object from the GPU before releasing resources back to the
4837 * system, we can not do that directly from the RCU callback (which may
4838 * be a softirq context), but must instead then defer that work onto a
4839 * kthread. We use the RCU callback rather than move the freed object
4840 * directly onto the work queue so that we can mix between using the
4841 * worker and performing frees directly from subsequent allocations for
4842 * crude but effective memory throttling.
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004843 */
4844 if (llist_add(&obj->freed, &i915->mm.free_list))
Chris Wilsonbeacbd12018-01-15 12:28:45 +00004845 queue_work(i915->wq, &i915->mm.free_work);
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004846}
4847
4848void i915_gem_free_object(struct drm_gem_object *gem_obj)
4849{
4850 struct drm_i915_gem_object *obj = to_intel_bo(gem_obj);
4851
Chris Wilsonbc0629a2016-11-01 10:03:17 +00004852 if (obj->mm.quirked)
4853 __i915_gem_object_unpin_pages(obj);
4854
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004855 if (discard_backing_storage(obj))
4856 obj->mm.madv = I915_MADV_DONTNEED;
Daniel Vettera071fa02014-06-18 23:28:09 +02004857
Chris Wilson2ef1e722018-01-15 20:57:59 +00004858 /*
4859 * Before we free the object, make sure any pure RCU-only
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004860 * read-side critical sections are complete, e.g.
4861 * i915_gem_busy_ioctl(). For the corresponding synchronized
4862 * lookup see i915_gem_object_lookup_rcu().
4863 */
Chris Wilsonc9c704712018-02-19 22:06:31 +00004864 atomic_inc(&to_i915(obj->base.dev)->mm.free_count);
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004865 call_rcu(&obj->rcu, __i915_gem_free_object_rcu);
Chris Wilsonbe726152010-07-23 23:18:50 +01004866}
4867
Chris Wilsonf8a7fde2016-10-28 13:58:29 +01004868void __i915_gem_object_release_unless_active(struct drm_i915_gem_object *obj)
4869{
4870 lockdep_assert_held(&obj->base.dev->struct_mutex);
4871
Chris Wilsond1b48c12017-08-16 09:52:08 +01004872 if (!i915_gem_object_has_active_reference(obj) &&
4873 i915_gem_object_is_active(obj))
Chris Wilsonf8a7fde2016-10-28 13:58:29 +01004874 i915_gem_object_set_active_reference(obj);
4875 else
4876 i915_gem_object_put(obj);
4877}
4878
Chris Wilson24145512017-01-24 11:01:35 +00004879void i915_gem_sanitize(struct drm_i915_private *i915)
4880{
Chris Wilsonc3160da2018-05-31 09:22:45 +01004881 GEM_TRACE("\n");
4882
Chris Wilson4dfacb02018-05-31 09:22:43 +01004883 mutex_lock(&i915->drm.struct_mutex);
Chris Wilsonc3160da2018-05-31 09:22:45 +01004884
4885 intel_runtime_pm_get(i915);
4886 intel_uncore_forcewake_get(i915, FORCEWAKE_ALL);
4887
4888 /*
4889 * As we have just resumed the machine and woken the device up from
4890 * deep PCI sleep (presumably D3_cold), assume the HW has been reset
4891 * back to defaults, recovering from whatever wedged state we left it
4892 * in and so worth trying to use the device once more.
4893 */
Chris Wilson4dfacb02018-05-31 09:22:43 +01004894 if (i915_terminally_wedged(&i915->gpu_error))
Chris Wilsonf36325f2017-08-26 12:09:34 +01004895 i915_gem_unset_wedged(i915);
Chris Wilsonf36325f2017-08-26 12:09:34 +01004896
Chris Wilson24145512017-01-24 11:01:35 +00004897 /*
4898 * If we inherit context state from the BIOS or earlier occupants
4899 * of the GPU, the GPU may be in an inconsistent state when we
4900 * try to take over. The only way to remove the earlier state
4901 * is by resetting. However, resetting on earlier gen is tricky as
4902 * it may impact the display and we are uncertain about the stability
Joonas Lahtinenea117b82017-04-28 10:53:38 +03004903 * of the reset, so this could be applied to even earlier gen.
Chris Wilson24145512017-01-24 11:01:35 +00004904 */
Chris Wilson55277e12019-01-03 11:21:04 +00004905 intel_engines_sanitize(i915, false);
Chris Wilsonc3160da2018-05-31 09:22:45 +01004906
4907 intel_uncore_forcewake_put(i915, FORCEWAKE_ALL);
4908 intel_runtime_pm_put(i915);
4909
Chris Wilson4dfacb02018-05-31 09:22:43 +01004910 i915_gem_contexts_lost(i915);
4911 mutex_unlock(&i915->drm.struct_mutex);
Chris Wilson24145512017-01-24 11:01:35 +00004912}
4913
Chris Wilsonbf061122018-07-09 14:02:04 +01004914int i915_gem_suspend(struct drm_i915_private *i915)
Eric Anholt673a3942008-07-30 12:06:12 -07004915{
Chris Wilsondcff85c2016-08-05 10:14:11 +01004916 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004917
Chris Wilson09a4c022018-05-24 09:11:35 +01004918 GEM_TRACE("\n");
4919
Chris Wilsonbf061122018-07-09 14:02:04 +01004920 intel_runtime_pm_get(i915);
4921 intel_suspend_gt_powersave(i915);
Chris Wilson54b4f682016-07-21 21:16:19 +01004922
Chris Wilsonbf061122018-07-09 14:02:04 +01004923 mutex_lock(&i915->drm.struct_mutex);
Chris Wilson5ab57c72016-07-15 14:56:20 +01004924
Chris Wilsonbf061122018-07-09 14:02:04 +01004925 /*
4926 * We have to flush all the executing contexts to main memory so
Chris Wilson5ab57c72016-07-15 14:56:20 +01004927 * that they can saved in the hibernation image. To ensure the last
4928 * context image is coherent, we have to switch away from it. That
Chris Wilsonbf061122018-07-09 14:02:04 +01004929 * leaves the i915->kernel_context still active when
Chris Wilson5ab57c72016-07-15 14:56:20 +01004930 * we actually suspend, and its image in memory may not match the GPU
4931 * state. Fortunately, the kernel_context is disposable and we do
4932 * not rely on its state.
4933 */
Chris Wilsonbf061122018-07-09 14:02:04 +01004934 if (!i915_terminally_wedged(&i915->gpu_error)) {
4935 ret = i915_gem_switch_to_kernel_context(i915);
Chris Wilsonecf73eb2017-11-30 10:29:51 +00004936 if (ret)
4937 goto err_unlock;
Chris Wilson5ab57c72016-07-15 14:56:20 +01004938
Chris Wilsonbf061122018-07-09 14:02:04 +01004939 ret = i915_gem_wait_for_idle(i915,
Chris Wilsonecf73eb2017-11-30 10:29:51 +00004940 I915_WAIT_INTERRUPTIBLE |
Chris Wilson06060352018-05-31 09:22:44 +01004941 I915_WAIT_LOCKED |
Chris Wilsonec625fb2018-07-09 13:20:42 +01004942 I915_WAIT_FOR_IDLE_BOOST,
4943 MAX_SCHEDULE_TIMEOUT);
Chris Wilsonecf73eb2017-11-30 10:29:51 +00004944 if (ret && ret != -EIO)
4945 goto err_unlock;
Chris Wilsonf7403342013-09-13 23:57:04 +01004946
Chris Wilsonbf061122018-07-09 14:02:04 +01004947 assert_kernel_context_is_current(i915);
Chris Wilsonecf73eb2017-11-30 10:29:51 +00004948 }
Chris Wilson01f8f332018-07-17 09:41:21 +01004949 i915_retire_requests(i915); /* ensure we flush after wedging */
4950
Chris Wilsonbf061122018-07-09 14:02:04 +01004951 mutex_unlock(&i915->drm.struct_mutex);
Chris Wilson45c5f202013-10-16 11:50:01 +01004952
Chris Wilsonbf061122018-07-09 14:02:04 +01004953 intel_uc_suspend(i915);
Sagar Arun Kamble63987bf2017-04-05 15:51:50 +05304954
Chris Wilsonbf061122018-07-09 14:02:04 +01004955 cancel_delayed_work_sync(&i915->gpu_error.hangcheck_work);
4956 cancel_delayed_work_sync(&i915->gt.retire_work);
Chris Wilsonbdeb9782016-12-23 14:57:56 +00004957
Chris Wilsonbf061122018-07-09 14:02:04 +01004958 /*
4959 * As the idle_work is rearming if it detects a race, play safe and
Chris Wilsonbdeb9782016-12-23 14:57:56 +00004960 * repeat the flush until it is definitely idle.
4961 */
Chris Wilsonbf061122018-07-09 14:02:04 +01004962 drain_delayed_work(&i915->gt.idle_work);
Chris Wilsonbdeb9782016-12-23 14:57:56 +00004963
Chris Wilsonbf061122018-07-09 14:02:04 +01004964 /*
4965 * Assert that we successfully flushed all the work and
Chris Wilsonbdcf1202014-11-25 11:56:33 +00004966 * reset the GPU back to its idle, low power state.
4967 */
Chris Wilsonbf061122018-07-09 14:02:04 +01004968 WARN_ON(i915->gt.awake);
4969 if (WARN_ON(!intel_engines_are_idle(i915)))
4970 i915_gem_set_wedged(i915); /* no hope, discard everything */
Chris Wilsonbdcf1202014-11-25 11:56:33 +00004971
Chris Wilsonbf061122018-07-09 14:02:04 +01004972 intel_runtime_pm_put(i915);
Chris Wilsonec92ad02018-05-31 09:22:46 +01004973 return 0;
4974
4975err_unlock:
Chris Wilsonbf061122018-07-09 14:02:04 +01004976 mutex_unlock(&i915->drm.struct_mutex);
4977 intel_runtime_pm_put(i915);
Chris Wilsonec92ad02018-05-31 09:22:46 +01004978 return ret;
4979}
4980
4981void i915_gem_suspend_late(struct drm_i915_private *i915)
4982{
Chris Wilson9776f472018-06-01 15:41:24 +01004983 struct drm_i915_gem_object *obj;
4984 struct list_head *phases[] = {
4985 &i915->mm.unbound_list,
4986 &i915->mm.bound_list,
4987 NULL
4988 }, **phase;
4989
Imre Deak1c777c52016-10-12 17:46:37 +03004990 /*
4991 * Neither the BIOS, ourselves or any other kernel
4992 * expects the system to be in execlists mode on startup,
4993 * so we need to reset the GPU back to legacy mode. And the only
4994 * known way to disable logical contexts is through a GPU reset.
4995 *
4996 * So in order to leave the system in a known default configuration,
4997 * always reset the GPU upon unload and suspend. Afterwards we then
4998 * clean up the GEM state tracking, flushing off the requests and
4999 * leaving the system in a known idle state.
5000 *
5001 * Note that is of the upmost importance that the GPU is idle and
5002 * all stray writes are flushed *before* we dismantle the backing
5003 * storage for the pinned objects.
5004 *
5005 * However, since we are uncertain that resetting the GPU on older
5006 * machines is a good idea, we don't - just in case it leaves the
5007 * machine in an unusable condition.
5008 */
Chris Wilsoncad99462017-08-26 12:09:33 +01005009
Chris Wilson9776f472018-06-01 15:41:24 +01005010 mutex_lock(&i915->drm.struct_mutex);
5011 for (phase = phases; *phase; phase++) {
5012 list_for_each_entry(obj, *phase, mm.link)
5013 WARN_ON(i915_gem_object_set_to_gtt_domain(obj, false));
5014 }
5015 mutex_unlock(&i915->drm.struct_mutex);
5016
Chris Wilsonec92ad02018-05-31 09:22:46 +01005017 intel_uc_sanitize(i915);
5018 i915_gem_sanitize(i915);
Eric Anholt673a3942008-07-30 12:06:12 -07005019}
5020
Chris Wilson37cd3302017-11-12 11:27:38 +00005021void i915_gem_resume(struct drm_i915_private *i915)
Chris Wilson5ab57c72016-07-15 14:56:20 +01005022{
Chris Wilson4dfacb02018-05-31 09:22:43 +01005023 GEM_TRACE("\n");
5024
Chris Wilson37cd3302017-11-12 11:27:38 +00005025 WARN_ON(i915->gt.awake);
Chris Wilson5ab57c72016-07-15 14:56:20 +01005026
Chris Wilson37cd3302017-11-12 11:27:38 +00005027 mutex_lock(&i915->drm.struct_mutex);
5028 intel_uncore_forcewake_get(i915, FORCEWAKE_ALL);
Imre Deak31ab49a2016-11-07 11:20:05 +02005029
Chris Wilson37cd3302017-11-12 11:27:38 +00005030 i915_gem_restore_gtt_mappings(i915);
5031 i915_gem_restore_fences(i915);
Chris Wilson5ab57c72016-07-15 14:56:20 +01005032
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00005033 /*
5034 * As we didn't flush the kernel context before suspend, we cannot
Chris Wilson5ab57c72016-07-15 14:56:20 +01005035 * guarantee that the context image is complete. So let's just reset
5036 * it and start again.
5037 */
Chris Wilson37cd3302017-11-12 11:27:38 +00005038 i915->gt.resume(i915);
Chris Wilson5ab57c72016-07-15 14:56:20 +01005039
Chris Wilson37cd3302017-11-12 11:27:38 +00005040 if (i915_gem_init_hw(i915))
5041 goto err_wedged;
5042
Michal Wajdeczko7cfca4a2018-03-02 11:15:49 +00005043 intel_uc_resume(i915);
Chris Wilson7469c622017-11-14 13:03:00 +00005044
Chris Wilson37cd3302017-11-12 11:27:38 +00005045 /* Always reload a context for powersaving. */
5046 if (i915_gem_switch_to_kernel_context(i915))
5047 goto err_wedged;
5048
5049out_unlock:
5050 intel_uncore_forcewake_put(i915, FORCEWAKE_ALL);
5051 mutex_unlock(&i915->drm.struct_mutex);
5052 return;
5053
5054err_wedged:
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00005055 if (!i915_terminally_wedged(&i915->gpu_error)) {
5056 DRM_ERROR("failed to re-initialize GPU, declaring wedged!\n");
5057 i915_gem_set_wedged(i915);
5058 }
Chris Wilson37cd3302017-11-12 11:27:38 +00005059 goto out_unlock;
Chris Wilson5ab57c72016-07-15 14:56:20 +01005060}
5061
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00005062void i915_gem_init_swizzling(struct drm_i915_private *dev_priv)
Daniel Vetterf691e2f2012-02-02 09:58:12 +01005063{
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00005064 if (INTEL_GEN(dev_priv) < 5 ||
Daniel Vetterf691e2f2012-02-02 09:58:12 +01005065 dev_priv->mm.bit_6_swizzle_x == I915_BIT_6_SWIZZLE_NONE)
5066 return;
5067
5068 I915_WRITE(DISP_ARB_CTL, I915_READ(DISP_ARB_CTL) |
5069 DISP_TILE_SURFACE_SWIZZLING);
5070
Lucas De Marchicf819ef2018-12-12 10:10:43 -08005071 if (IS_GEN(dev_priv, 5))
Daniel Vetter11782b02012-01-31 16:47:55 +01005072 return;
5073
Daniel Vetterf691e2f2012-02-02 09:58:12 +01005074 I915_WRITE(TILECTL, I915_READ(TILECTL) | TILECTL_SWZCTL);
Lucas De Marchicf819ef2018-12-12 10:10:43 -08005075 if (IS_GEN(dev_priv, 6))
Daniel Vetter6b26c862012-04-24 14:04:12 +02005076 I915_WRITE(ARB_MODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_SNB));
Lucas De Marchicf819ef2018-12-12 10:10:43 -08005077 else if (IS_GEN(dev_priv, 7))
Daniel Vetter6b26c862012-04-24 14:04:12 +02005078 I915_WRITE(ARB_MODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_IVB));
Lucas De Marchicf819ef2018-12-12 10:10:43 -08005079 else if (IS_GEN(dev_priv, 8))
Ben Widawsky31a53362013-11-02 21:07:04 -07005080 I915_WRITE(GAMTARBMODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_BDW));
Ben Widawsky8782e262012-12-18 10:31:23 -08005081 else
5082 BUG();
Daniel Vetterf691e2f2012-02-02 09:58:12 +01005083}
Daniel Vettere21af882012-02-09 20:53:27 +01005084
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01005085static void init_unused_ring(struct drm_i915_private *dev_priv, u32 base)
Ville Syrjälä81e7f202014-08-15 01:21:55 +03005086{
Ville Syrjälä81e7f202014-08-15 01:21:55 +03005087 I915_WRITE(RING_CTL(base), 0);
5088 I915_WRITE(RING_HEAD(base), 0);
5089 I915_WRITE(RING_TAIL(base), 0);
5090 I915_WRITE(RING_START(base), 0);
5091}
5092
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01005093static void init_unused_rings(struct drm_i915_private *dev_priv)
Ville Syrjälä81e7f202014-08-15 01:21:55 +03005094{
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01005095 if (IS_I830(dev_priv)) {
5096 init_unused_ring(dev_priv, PRB1_BASE);
5097 init_unused_ring(dev_priv, SRB0_BASE);
5098 init_unused_ring(dev_priv, SRB1_BASE);
5099 init_unused_ring(dev_priv, SRB2_BASE);
5100 init_unused_ring(dev_priv, SRB3_BASE);
Lucas De Marchicf819ef2018-12-12 10:10:43 -08005101 } else if (IS_GEN(dev_priv, 2)) {
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01005102 init_unused_ring(dev_priv, SRB0_BASE);
5103 init_unused_ring(dev_priv, SRB1_BASE);
Lucas De Marchicf819ef2018-12-12 10:10:43 -08005104 } else if (IS_GEN(dev_priv, 3)) {
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01005105 init_unused_ring(dev_priv, PRB1_BASE);
5106 init_unused_ring(dev_priv, PRB2_BASE);
Ville Syrjälä81e7f202014-08-15 01:21:55 +03005107 }
5108}
5109
Chris Wilson20a8a742017-02-08 14:30:31 +00005110static int __i915_gem_restart_engines(void *data)
Ben Widawsky4fc7c972013-02-08 11:49:24 -08005111{
Chris Wilson20a8a742017-02-08 14:30:31 +00005112 struct drm_i915_private *i915 = data;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00005113 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05305114 enum intel_engine_id id;
Chris Wilson20a8a742017-02-08 14:30:31 +00005115 int err;
5116
5117 for_each_engine(engine, i915, id) {
5118 err = engine->init_hw(engine);
Chris Wilson8177e112018-02-07 11:15:45 +00005119 if (err) {
5120 DRM_ERROR("Failed to restart %s (%d)\n",
5121 engine->name, err);
Chris Wilson20a8a742017-02-08 14:30:31 +00005122 return err;
Chris Wilson8177e112018-02-07 11:15:45 +00005123 }
Chris Wilson20a8a742017-02-08 14:30:31 +00005124 }
5125
5126 return 0;
5127}
5128
5129int i915_gem_init_hw(struct drm_i915_private *dev_priv)
5130{
Chris Wilsond200cda2016-04-28 09:56:44 +01005131 int ret;
Ben Widawsky4fc7c972013-02-08 11:49:24 -08005132
Chris Wilsonde867c22016-10-25 13:16:02 +01005133 dev_priv->gt.last_init_time = ktime_get();
5134
Chris Wilson5e4f5182015-02-13 14:35:59 +00005135 /* Double layer security blanket, see i915_gem_init() */
5136 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
5137
Tvrtko Ursulin0031fb92016-11-04 14:42:44 +00005138 if (HAS_EDRAM(dev_priv) && INTEL_GEN(dev_priv) < 9)
Ben Widawsky05e21cc2013-07-04 11:02:04 -07005139 I915_WRITE(HSW_IDICR, I915_READ(HSW_IDICR) | IDIHASHMSK(0xf));
Ben Widawsky4fc7c972013-02-08 11:49:24 -08005140
Tvrtko Ursulin772c2a52016-10-13 11:03:01 +01005141 if (IS_HASWELL(dev_priv))
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01005142 I915_WRITE(MI_PREDICATE_RESULT_2, IS_HSW_GT3(dev_priv) ?
Ville Syrjälä0bf21342013-11-29 14:56:12 +02005143 LOWER_SLICE_ENABLED : LOWER_SLICE_DISABLED);
Rodrigo Vivi94353732013-08-28 16:45:46 -03005144
Tvrtko Ursulin094304b2018-12-03 12:50:10 +00005145 /* Apply the GT workarounds... */
Tvrtko Ursulin25d140f2018-12-03 13:33:19 +00005146 intel_gt_apply_workarounds(dev_priv);
Tvrtko Ursulin094304b2018-12-03 12:50:10 +00005147 /* ...and determine whether they are sticking. */
5148 intel_gt_verify_workarounds(dev_priv, "init");
Oscar Mateo59b449d2018-04-10 09:12:47 -07005149
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00005150 i915_gem_init_swizzling(dev_priv);
Ben Widawsky4fc7c972013-02-08 11:49:24 -08005151
Daniel Vetterd5abdfd2014-11-20 09:45:19 +01005152 /*
5153 * At least 830 can leave some of the unused rings
5154 * "active" (ie. head != tail) after resume which
5155 * will prevent c3 entry. Makes sure all unused rings
5156 * are totally idle.
5157 */
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01005158 init_unused_rings(dev_priv);
Daniel Vetterd5abdfd2014-11-20 09:45:19 +01005159
Dave Gordoned54c1a2016-01-19 19:02:54 +00005160 BUG_ON(!dev_priv->kernel_context);
Chris Wilson6f74b362017-10-15 15:37:25 +01005161 if (i915_terminally_wedged(&dev_priv->gpu_error)) {
5162 ret = -EIO;
5163 goto out;
5164 }
John Harrison90638cc2015-05-29 17:43:37 +01005165
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00005166 ret = i915_ppgtt_init_hw(dev_priv);
John Harrison4ad2fd82015-06-18 13:11:20 +01005167 if (ret) {
Chris Wilson8177e112018-02-07 11:15:45 +00005168 DRM_ERROR("Enabling PPGTT failed (%d)\n", ret);
John Harrison4ad2fd82015-06-18 13:11:20 +01005169 goto out;
5170 }
5171
Jackie Lif08e2032018-03-13 17:32:53 -07005172 ret = intel_wopcm_init_hw(&dev_priv->wopcm);
5173 if (ret) {
5174 DRM_ERROR("Enabling WOPCM failed (%d)\n", ret);
5175 goto out;
5176 }
5177
Michał Winiarski9bdc3572017-10-25 18:25:19 +01005178 /* We can't enable contexts until all firmware is loaded */
5179 ret = intel_uc_init_hw(dev_priv);
Chris Wilson8177e112018-02-07 11:15:45 +00005180 if (ret) {
5181 DRM_ERROR("Enabling uc failed (%d)\n", ret);
Michał Winiarski9bdc3572017-10-25 18:25:19 +01005182 goto out;
Chris Wilson8177e112018-02-07 11:15:45 +00005183 }
Michał Winiarski9bdc3572017-10-25 18:25:19 +01005184
Tvrtko Ursulinbf9e8422016-12-01 14:16:38 +00005185 intel_mocs_init_l3cc_table(dev_priv);
Peter Antoine0ccdacf2016-04-13 15:03:25 +01005186
Chris Wilson136109c2017-11-02 13:14:30 +00005187 /* Only when the HW is re-initialised, can we replay the requests */
5188 ret = __i915_gem_restart_engines(dev_priv);
Michal Wajdeczkob96f6eb2018-06-05 12:24:43 +00005189 if (ret)
5190 goto cleanup_uc;
Michał Winiarski60c0a662018-07-12 14:48:10 +02005191
Chris Wilson5e4f5182015-02-13 14:35:59 +00005192 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Michał Winiarski60c0a662018-07-12 14:48:10 +02005193
5194 return 0;
Michal Wajdeczkob96f6eb2018-06-05 12:24:43 +00005195
5196cleanup_uc:
5197 intel_uc_fini_hw(dev_priv);
Michał Winiarski60c0a662018-07-12 14:48:10 +02005198out:
5199 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
5200
5201 return ret;
Zou Nan hai8187a2b2010-05-21 09:08:55 +08005202}
5203
Chris Wilsond2b4b972017-11-10 14:26:33 +00005204static int __intel_engines_record_defaults(struct drm_i915_private *i915)
5205{
5206 struct i915_gem_context *ctx;
5207 struct intel_engine_cs *engine;
5208 enum intel_engine_id id;
5209 int err;
5210
5211 /*
5212 * As we reset the gpu during very early sanitisation, the current
5213 * register state on the GPU should reflect its defaults values.
5214 * We load a context onto the hw (with restore-inhibit), then switch
5215 * over to a second context to save that default register state. We
5216 * can then prime every new context with that state so they all start
5217 * from the same default HW values.
5218 */
5219
5220 ctx = i915_gem_context_create_kernel(i915, 0);
5221 if (IS_ERR(ctx))
5222 return PTR_ERR(ctx);
5223
5224 for_each_engine(engine, i915, id) {
Chris Wilsone61e0f52018-02-21 09:56:36 +00005225 struct i915_request *rq;
Chris Wilsond2b4b972017-11-10 14:26:33 +00005226
Chris Wilsone61e0f52018-02-21 09:56:36 +00005227 rq = i915_request_alloc(engine, ctx);
Chris Wilsond2b4b972017-11-10 14:26:33 +00005228 if (IS_ERR(rq)) {
5229 err = PTR_ERR(rq);
5230 goto out_ctx;
5231 }
5232
Chris Wilson3fef5cd2017-11-20 10:20:02 +00005233 err = 0;
Chris Wilsond2b4b972017-11-10 14:26:33 +00005234 if (engine->init_context)
5235 err = engine->init_context(rq);
5236
Chris Wilson697b9a82018-06-12 11:51:35 +01005237 i915_request_add(rq);
Chris Wilsond2b4b972017-11-10 14:26:33 +00005238 if (err)
5239 goto err_active;
5240 }
5241
5242 err = i915_gem_switch_to_kernel_context(i915);
5243 if (err)
5244 goto err_active;
5245
Chris Wilson2621cef2018-07-09 13:20:43 +01005246 if (i915_gem_wait_for_idle(i915, I915_WAIT_LOCKED, HZ / 5)) {
5247 i915_gem_set_wedged(i915);
5248 err = -EIO; /* Caller will declare us wedged */
Chris Wilsond2b4b972017-11-10 14:26:33 +00005249 goto err_active;
Chris Wilson2621cef2018-07-09 13:20:43 +01005250 }
Chris Wilsond2b4b972017-11-10 14:26:33 +00005251
5252 assert_kernel_context_is_current(i915);
5253
Chris Wilson8e1cb322018-09-20 17:13:43 +01005254 /*
5255 * Immediately park the GPU so that we enable powersaving and
5256 * treat it as idle. The next time we issue a request, we will
5257 * unpark and start using the engine->pinned_default_state, otherwise
5258 * it is in limbo and an early reset may fail.
5259 */
5260 __i915_gem_park(i915);
5261
Chris Wilsond2b4b972017-11-10 14:26:33 +00005262 for_each_engine(engine, i915, id) {
5263 struct i915_vma *state;
Chris Wilson37d7c9c2018-09-14 13:35:03 +01005264 void *vaddr;
Chris Wilsond2b4b972017-11-10 14:26:33 +00005265
Chris Wilson666424a2018-09-14 13:35:04 +01005266 GEM_BUG_ON(to_intel_context(ctx, engine)->pin_count);
5267
Chris Wilsonab82a062018-04-30 14:15:01 +01005268 state = to_intel_context(ctx, engine)->state;
Chris Wilsond2b4b972017-11-10 14:26:33 +00005269 if (!state)
5270 continue;
5271
5272 /*
5273 * As we will hold a reference to the logical state, it will
5274 * not be torn down with the context, and importantly the
5275 * object will hold onto its vma (making it possible for a
5276 * stray GTT write to corrupt our defaults). Unmap the vma
5277 * from the GTT to prevent such accidents and reclaim the
5278 * space.
5279 */
5280 err = i915_vma_unbind(state);
5281 if (err)
5282 goto err_active;
5283
5284 err = i915_gem_object_set_to_cpu_domain(state->obj, false);
5285 if (err)
5286 goto err_active;
5287
5288 engine->default_state = i915_gem_object_get(state->obj);
Chris Wilson37d7c9c2018-09-14 13:35:03 +01005289
5290 /* Check we can acquire the image of the context state */
5291 vaddr = i915_gem_object_pin_map(engine->default_state,
Chris Wilson666424a2018-09-14 13:35:04 +01005292 I915_MAP_FORCE_WB);
Chris Wilson37d7c9c2018-09-14 13:35:03 +01005293 if (IS_ERR(vaddr)) {
5294 err = PTR_ERR(vaddr);
5295 goto err_active;
5296 }
5297
5298 i915_gem_object_unpin_map(engine->default_state);
Chris Wilsond2b4b972017-11-10 14:26:33 +00005299 }
5300
5301 if (IS_ENABLED(CONFIG_DRM_I915_DEBUG_GEM)) {
5302 unsigned int found = intel_engines_has_context_isolation(i915);
5303
5304 /*
5305 * Make sure that classes with multiple engine instances all
5306 * share the same basic configuration.
5307 */
5308 for_each_engine(engine, i915, id) {
5309 unsigned int bit = BIT(engine->uabi_class);
5310 unsigned int expected = engine->default_state ? bit : 0;
5311
5312 if ((found & bit) != expected) {
5313 DRM_ERROR("mismatching default context state for class %d on engine %s\n",
5314 engine->uabi_class, engine->name);
5315 }
5316 }
5317 }
5318
5319out_ctx:
5320 i915_gem_context_set_closed(ctx);
5321 i915_gem_context_put(ctx);
5322 return err;
5323
5324err_active:
5325 /*
5326 * If we have to abandon now, we expect the engines to be idle
5327 * and ready to be torn-down. First try to flush any remaining
5328 * request, ensure we are pointing at the kernel context and
5329 * then remove it.
5330 */
5331 if (WARN_ON(i915_gem_switch_to_kernel_context(i915)))
5332 goto out_ctx;
5333
Chris Wilsonec625fb2018-07-09 13:20:42 +01005334 if (WARN_ON(i915_gem_wait_for_idle(i915,
5335 I915_WAIT_LOCKED,
5336 MAX_SCHEDULE_TIMEOUT)))
Chris Wilsond2b4b972017-11-10 14:26:33 +00005337 goto out_ctx;
5338
5339 i915_gem_contexts_lost(i915);
5340 goto out_ctx;
5341}
5342
Chris Wilson51797492018-12-04 14:15:16 +00005343static int
5344i915_gem_init_scratch(struct drm_i915_private *i915, unsigned int size)
5345{
5346 struct drm_i915_gem_object *obj;
5347 struct i915_vma *vma;
5348 int ret;
5349
5350 obj = i915_gem_object_create_stolen(i915, size);
5351 if (!obj)
5352 obj = i915_gem_object_create_internal(i915, size);
5353 if (IS_ERR(obj)) {
5354 DRM_ERROR("Failed to allocate scratch page\n");
5355 return PTR_ERR(obj);
5356 }
5357
5358 vma = i915_vma_instance(obj, &i915->ggtt.vm, NULL);
5359 if (IS_ERR(vma)) {
5360 ret = PTR_ERR(vma);
5361 goto err_unref;
5362 }
5363
5364 ret = i915_vma_pin(vma, 0, 0, PIN_GLOBAL | PIN_HIGH);
5365 if (ret)
5366 goto err_unref;
5367
5368 i915->gt.scratch = vma;
5369 return 0;
5370
5371err_unref:
5372 i915_gem_object_put(obj);
5373 return ret;
5374}
5375
5376static void i915_gem_fini_scratch(struct drm_i915_private *i915)
5377{
5378 i915_vma_unpin_and_release(&i915->gt.scratch, 0);
5379}
5380
Tvrtko Ursulinbf9e8422016-12-01 14:16:38 +00005381int i915_gem_init(struct drm_i915_private *dev_priv)
Chris Wilson1070a422012-04-24 15:47:41 +01005382{
Chris Wilson1070a422012-04-24 15:47:41 +01005383 int ret;
5384
Changbin Du52b24162018-05-08 17:07:05 +08005385 /* We need to fallback to 4K pages if host doesn't support huge gtt. */
5386 if (intel_vgpu_active(dev_priv) && !intel_vgpu_has_huge_gtt(dev_priv))
Matthew Auldda9fe3f32017-10-06 23:18:31 +01005387 mkwrite_device_info(dev_priv)->page_sizes =
5388 I915_GTT_PAGE_SIZE_4K;
5389
Chris Wilson94312822017-05-03 10:39:18 +01005390 dev_priv->mm.unordered_timeline = dma_fence_context_alloc(1);
Chris Wilson57822dc2017-02-22 11:40:48 +00005391
Chris Wilsonfb5c5512017-11-20 20:55:00 +00005392 if (HAS_LOGICAL_RING_CONTEXTS(dev_priv)) {
Chris Wilson821ed7d2016-09-09 14:11:53 +01005393 dev_priv->gt.resume = intel_lr_context_resume;
Tvrtko Ursulin117897f2016-03-16 11:00:40 +00005394 dev_priv->gt.cleanup_engine = intel_logical_ring_cleanup;
Chris Wilsonfb5c5512017-11-20 20:55:00 +00005395 } else {
5396 dev_priv->gt.resume = intel_legacy_submission_resume;
5397 dev_priv->gt.cleanup_engine = intel_engine_cleanup;
Oscar Mateoa83014d2014-07-24 17:04:21 +01005398 }
5399
Chris Wilsonee487002017-11-22 17:26:21 +00005400 ret = i915_gem_init_userptr(dev_priv);
5401 if (ret)
5402 return ret;
5403
Sagar Arun Kamble70deead2018-01-24 21:16:58 +05305404 ret = intel_uc_init_misc(dev_priv);
Michał Winiarski3176ff42017-12-13 23:13:47 +01005405 if (ret)
5406 return ret;
5407
Michal Wajdeczkof7dc0152018-06-28 14:15:21 +00005408 ret = intel_wopcm_init(&dev_priv->wopcm);
5409 if (ret)
5410 goto err_uc_misc;
5411
Chris Wilson5e4f5182015-02-13 14:35:59 +00005412 /* This is just a security blanket to placate dragons.
5413 * On some systems, we very sporadically observe that the first TLBs
5414 * used by the CS may be stale, despite us poking the TLB reset. If
5415 * we hold the forcewake during initialisation these problems
5416 * just magically go away.
5417 */
Chris Wilsonee487002017-11-22 17:26:21 +00005418 mutex_lock(&dev_priv->drm.struct_mutex);
Chris Wilson5e4f5182015-02-13 14:35:59 +00005419 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
5420
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01005421 ret = i915_gem_init_ggtt(dev_priv);
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00005422 if (ret) {
5423 GEM_BUG_ON(ret == -EIO);
5424 goto err_unlock;
5425 }
Jesse Barnesd62b4892013-03-08 10:45:53 -08005426
Chris Wilson51797492018-12-04 14:15:16 +00005427 ret = i915_gem_init_scratch(dev_priv,
Lucas De Marchicf819ef2018-12-12 10:10:43 -08005428 IS_GEN(dev_priv, 2) ? SZ_256K : PAGE_SIZE);
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00005429 if (ret) {
5430 GEM_BUG_ON(ret == -EIO);
5431 goto err_ggtt;
5432 }
Ben Widawsky2fa48d82013-12-06 14:11:04 -08005433
Chris Wilson51797492018-12-04 14:15:16 +00005434 ret = i915_gem_contexts_init(dev_priv);
5435 if (ret) {
5436 GEM_BUG_ON(ret == -EIO);
5437 goto err_scratch;
5438 }
5439
Tvrtko Ursulinbf9e8422016-12-01 14:16:38 +00005440 ret = intel_engines_init(dev_priv);
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00005441 if (ret) {
5442 GEM_BUG_ON(ret == -EIO);
5443 goto err_context;
5444 }
Daniel Vetter53ca26c2012-04-26 23:28:03 +02005445
Chris Wilsonf58d13d2017-11-10 14:26:29 +00005446 intel_init_gt_powersave(dev_priv);
5447
Michał Winiarski61b5c152017-12-13 23:13:48 +01005448 ret = intel_uc_init(dev_priv);
Chris Wilsoncc6a8182017-11-10 14:26:30 +00005449 if (ret)
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00005450 goto err_pm;
Chris Wilsoncc6a8182017-11-10 14:26:30 +00005451
Michał Winiarski61b5c152017-12-13 23:13:48 +01005452 ret = i915_gem_init_hw(dev_priv);
5453 if (ret)
5454 goto err_uc_init;
5455
Chris Wilsoncc6a8182017-11-10 14:26:30 +00005456 /*
5457 * Despite its name intel_init_clock_gating applies both display
5458 * clock gating workarounds; GT mmio workarounds and the occasional
5459 * GT power context workaround. Worse, sometimes it includes a context
5460 * register workaround which we need to apply before we record the
5461 * default HW state for all contexts.
5462 *
5463 * FIXME: break up the workarounds and apply them at the right time!
5464 */
5465 intel_init_clock_gating(dev_priv);
5466
Chris Wilsond2b4b972017-11-10 14:26:33 +00005467 ret = __intel_engines_record_defaults(dev_priv);
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00005468 if (ret)
5469 goto err_init_hw;
5470
5471 if (i915_inject_load_failure()) {
5472 ret = -ENODEV;
5473 goto err_init_hw;
5474 }
5475
5476 if (i915_inject_load_failure()) {
5477 ret = -EIO;
5478 goto err_init_hw;
5479 }
5480
5481 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
5482 mutex_unlock(&dev_priv->drm.struct_mutex);
5483
5484 return 0;
5485
5486 /*
5487 * Unwinding is complicated by that we want to handle -EIO to mean
5488 * disable GPU submission but keep KMS alive. We want to mark the
5489 * HW as irrevisibly wedged, but keep enough state around that the
5490 * driver doesn't explode during runtime.
5491 */
5492err_init_hw:
Chris Wilson8571a052018-06-06 15:54:41 +01005493 mutex_unlock(&dev_priv->drm.struct_mutex);
5494
5495 WARN_ON(i915_gem_suspend(dev_priv));
5496 i915_gem_suspend_late(dev_priv);
5497
Chris Wilson8bcf9f72018-07-10 10:44:20 +01005498 i915_gem_drain_workqueue(dev_priv);
5499
Chris Wilson8571a052018-06-06 15:54:41 +01005500 mutex_lock(&dev_priv->drm.struct_mutex);
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00005501 intel_uc_fini_hw(dev_priv);
Michał Winiarski61b5c152017-12-13 23:13:48 +01005502err_uc_init:
5503 intel_uc_fini(dev_priv);
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00005504err_pm:
5505 if (ret != -EIO) {
5506 intel_cleanup_gt_powersave(dev_priv);
5507 i915_gem_cleanup_engines(dev_priv);
5508 }
5509err_context:
5510 if (ret != -EIO)
5511 i915_gem_contexts_fini(dev_priv);
Chris Wilson51797492018-12-04 14:15:16 +00005512err_scratch:
5513 i915_gem_fini_scratch(dev_priv);
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00005514err_ggtt:
5515err_unlock:
5516 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
5517 mutex_unlock(&dev_priv->drm.struct_mutex);
5518
Michal Wajdeczkof7dc0152018-06-28 14:15:21 +00005519err_uc_misc:
Sagar Arun Kamble70deead2018-01-24 21:16:58 +05305520 intel_uc_fini_misc(dev_priv);
Sagar Arun Kambleda943b52018-01-10 18:24:16 +05305521
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00005522 if (ret != -EIO)
5523 i915_gem_cleanup_userptr(dev_priv);
5524
Chris Wilson60990322014-04-09 09:19:42 +01005525 if (ret == -EIO) {
Chris Wilson7ed43df2018-07-26 09:50:32 +01005526 mutex_lock(&dev_priv->drm.struct_mutex);
5527
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00005528 /*
5529 * Allow engine initialisation to fail by marking the GPU as
Chris Wilson60990322014-04-09 09:19:42 +01005530 * wedged. But we only want to do this where the GPU is angry,
5531 * for all other failure, such as an allocation failure, bail.
5532 */
Chris Wilson6f74b362017-10-15 15:37:25 +01005533 if (!i915_terminally_wedged(&dev_priv->gpu_error)) {
Chris Wilson51c18bf2018-06-09 12:10:58 +01005534 i915_load_error(dev_priv,
5535 "Failed to initialize GPU, declaring it wedged!\n");
Chris Wilson6f74b362017-10-15 15:37:25 +01005536 i915_gem_set_wedged(dev_priv);
5537 }
Chris Wilson7ed43df2018-07-26 09:50:32 +01005538
5539 /* Minimal basic recovery for KMS */
5540 ret = i915_ggtt_enable_hw(dev_priv);
5541 i915_gem_restore_gtt_mappings(dev_priv);
5542 i915_gem_restore_fences(dev_priv);
5543 intel_init_clock_gating(dev_priv);
5544
5545 mutex_unlock(&dev_priv->drm.struct_mutex);
Chris Wilson1070a422012-04-24 15:47:41 +01005546 }
5547
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00005548 i915_gem_drain_freed_objects(dev_priv);
Chris Wilson60990322014-04-09 09:19:42 +01005549 return ret;
Chris Wilson1070a422012-04-24 15:47:41 +01005550}
5551
Michal Wajdeczko8979187a2018-06-04 09:00:32 +00005552void i915_gem_fini(struct drm_i915_private *dev_priv)
5553{
5554 i915_gem_suspend_late(dev_priv);
Chris Wilson30b710842018-08-12 23:36:29 +01005555 intel_disable_gt_powersave(dev_priv);
Michal Wajdeczko8979187a2018-06-04 09:00:32 +00005556
5557 /* Flush any outstanding unpin_work. */
5558 i915_gem_drain_workqueue(dev_priv);
5559
5560 mutex_lock(&dev_priv->drm.struct_mutex);
5561 intel_uc_fini_hw(dev_priv);
5562 intel_uc_fini(dev_priv);
5563 i915_gem_cleanup_engines(dev_priv);
5564 i915_gem_contexts_fini(dev_priv);
Chris Wilson51797492018-12-04 14:15:16 +00005565 i915_gem_fini_scratch(dev_priv);
Michal Wajdeczko8979187a2018-06-04 09:00:32 +00005566 mutex_unlock(&dev_priv->drm.struct_mutex);
5567
Tvrtko Ursulin25d140f2018-12-03 13:33:19 +00005568 intel_wa_list_free(&dev_priv->gt_wa_list);
5569
Chris Wilson30b710842018-08-12 23:36:29 +01005570 intel_cleanup_gt_powersave(dev_priv);
5571
Michal Wajdeczko8979187a2018-06-04 09:00:32 +00005572 intel_uc_fini_misc(dev_priv);
5573 i915_gem_cleanup_userptr(dev_priv);
5574
5575 i915_gem_drain_freed_objects(dev_priv);
5576
5577 WARN_ON(!list_empty(&dev_priv->contexts.list));
5578}
5579
Chris Wilson24145512017-01-24 11:01:35 +00005580void i915_gem_init_mmio(struct drm_i915_private *i915)
5581{
5582 i915_gem_sanitize(i915);
5583}
5584
Zou Nan hai8187a2b2010-05-21 09:08:55 +08005585void
Tvrtko Ursulincb15d9f2016-12-01 14:16:39 +00005586i915_gem_cleanup_engines(struct drm_i915_private *dev_priv)
Zou Nan hai8187a2b2010-05-21 09:08:55 +08005587{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00005588 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05305589 enum intel_engine_id id;
Zou Nan hai8187a2b2010-05-21 09:08:55 +08005590
Akash Goel3b3f1652016-10-13 22:44:48 +05305591 for_each_engine(engine, dev_priv, id)
Tvrtko Ursulin117897f2016-03-16 11:00:40 +00005592 dev_priv->gt.cleanup_engine(engine);
Zou Nan hai8187a2b2010-05-21 09:08:55 +08005593}
5594
Eric Anholt673a3942008-07-30 12:06:12 -07005595void
Imre Deak40ae4e12016-03-16 14:54:03 +02005596i915_gem_load_init_fences(struct drm_i915_private *dev_priv)
5597{
Chris Wilson49ef5292016-08-18 17:17:00 +01005598 int i;
Imre Deak40ae4e12016-03-16 14:54:03 +02005599
Tvrtko Ursulinc56b89f2018-02-09 21:58:46 +00005600 if (INTEL_GEN(dev_priv) >= 7 && !IS_VALLEYVIEW(dev_priv) &&
Imre Deak40ae4e12016-03-16 14:54:03 +02005601 !IS_CHERRYVIEW(dev_priv))
5602 dev_priv->num_fence_regs = 32;
Tvrtko Ursulinc56b89f2018-02-09 21:58:46 +00005603 else if (INTEL_GEN(dev_priv) >= 4 ||
Jani Nikula73f67aa2016-12-07 22:48:09 +02005604 IS_I945G(dev_priv) || IS_I945GM(dev_priv) ||
5605 IS_G33(dev_priv) || IS_PINEVIEW(dev_priv))
Imre Deak40ae4e12016-03-16 14:54:03 +02005606 dev_priv->num_fence_regs = 16;
5607 else
5608 dev_priv->num_fence_regs = 8;
5609
Chris Wilsonc0336662016-05-06 15:40:21 +01005610 if (intel_vgpu_active(dev_priv))
Imre Deak40ae4e12016-03-16 14:54:03 +02005611 dev_priv->num_fence_regs =
5612 I915_READ(vgtif_reg(avail_rs.fence_num));
5613
5614 /* Initialize fence registers to zero */
Chris Wilson49ef5292016-08-18 17:17:00 +01005615 for (i = 0; i < dev_priv->num_fence_regs; i++) {
5616 struct drm_i915_fence_reg *fence = &dev_priv->fence_regs[i];
5617
5618 fence->i915 = dev_priv;
5619 fence->id = i;
5620 list_add_tail(&fence->link, &dev_priv->mm.fence_list);
5621 }
Tvrtko Ursulin4362f4f2016-11-16 08:55:33 +00005622 i915_gem_restore_fences(dev_priv);
Imre Deak40ae4e12016-03-16 14:54:03 +02005623
Tvrtko Ursulin4362f4f2016-11-16 08:55:33 +00005624 i915_gem_detect_bit_6_swizzle(dev_priv);
Imre Deak40ae4e12016-03-16 14:54:03 +02005625}
5626
Chris Wilson9c52d1c2017-11-10 23:24:47 +00005627static void i915_gem_init__mm(struct drm_i915_private *i915)
5628{
5629 spin_lock_init(&i915->mm.object_stat_lock);
5630 spin_lock_init(&i915->mm.obj_lock);
5631 spin_lock_init(&i915->mm.free_lock);
5632
5633 init_llist_head(&i915->mm.free_list);
5634
5635 INIT_LIST_HEAD(&i915->mm.unbound_list);
5636 INIT_LIST_HEAD(&i915->mm.bound_list);
5637 INIT_LIST_HEAD(&i915->mm.fence_list);
5638 INIT_LIST_HEAD(&i915->mm.userfault_list);
5639
5640 INIT_WORK(&i915->mm.free_work, __i915_gem_free_work);
5641}
5642
Michal Wajdeczkoa0de9082018-03-23 12:34:49 +00005643int i915_gem_init_early(struct drm_i915_private *dev_priv)
Eric Anholt673a3942008-07-30 12:06:12 -07005644{
Tvrtko Ursulina9335682016-11-02 15:14:59 +00005645 int err = -ENOMEM;
Chris Wilson42dcedd2012-11-15 11:32:30 +00005646
Tvrtko Ursulina9335682016-11-02 15:14:59 +00005647 dev_priv->objects = KMEM_CACHE(drm_i915_gem_object, SLAB_HWCACHE_ALIGN);
5648 if (!dev_priv->objects)
Chris Wilson73cb9702016-10-28 13:58:46 +01005649 goto err_out;
Chris Wilson73cb9702016-10-28 13:58:46 +01005650
Tvrtko Ursulina9335682016-11-02 15:14:59 +00005651 dev_priv->vmas = KMEM_CACHE(i915_vma, SLAB_HWCACHE_ALIGN);
5652 if (!dev_priv->vmas)
Chris Wilson73cb9702016-10-28 13:58:46 +01005653 goto err_objects;
Chris Wilson73cb9702016-10-28 13:58:46 +01005654
Chris Wilsond1b48c12017-08-16 09:52:08 +01005655 dev_priv->luts = KMEM_CACHE(i915_lut_handle, 0);
5656 if (!dev_priv->luts)
5657 goto err_vmas;
5658
Chris Wilsone61e0f52018-02-21 09:56:36 +00005659 dev_priv->requests = KMEM_CACHE(i915_request,
Tvrtko Ursulina9335682016-11-02 15:14:59 +00005660 SLAB_HWCACHE_ALIGN |
5661 SLAB_RECLAIM_ACCOUNT |
Paul E. McKenney5f0d5a32017-01-18 02:53:44 -08005662 SLAB_TYPESAFE_BY_RCU);
Tvrtko Ursulina9335682016-11-02 15:14:59 +00005663 if (!dev_priv->requests)
Chris Wilsond1b48c12017-08-16 09:52:08 +01005664 goto err_luts;
Chris Wilson73cb9702016-10-28 13:58:46 +01005665
Chris Wilson52e54202016-11-14 20:41:02 +00005666 dev_priv->dependencies = KMEM_CACHE(i915_dependency,
5667 SLAB_HWCACHE_ALIGN |
5668 SLAB_RECLAIM_ACCOUNT);
5669 if (!dev_priv->dependencies)
5670 goto err_requests;
5671
Chris Wilsonc5cf9a92017-05-17 13:10:04 +01005672 dev_priv->priorities = KMEM_CACHE(i915_priolist, SLAB_HWCACHE_ALIGN);
5673 if (!dev_priv->priorities)
5674 goto err_dependencies;
5675
Chris Wilson73cb9702016-10-28 13:58:46 +01005676 INIT_LIST_HEAD(&dev_priv->gt.timelines);
Chris Wilson643b4502018-04-30 14:15:03 +01005677 INIT_LIST_HEAD(&dev_priv->gt.active_rings);
Chris Wilson3365e222018-05-03 20:51:14 +01005678 INIT_LIST_HEAD(&dev_priv->gt.closed_vma);
Chris Wilson643b4502018-04-30 14:15:03 +01005679
Chris Wilson9c52d1c2017-11-10 23:24:47 +00005680 i915_gem_init__mm(dev_priv);
Chris Wilsonf2123812017-10-16 12:40:37 +01005681
Chris Wilson67d97da2016-07-04 08:08:31 +01005682 INIT_DELAYED_WORK(&dev_priv->gt.retire_work,
Eric Anholt673a3942008-07-30 12:06:12 -07005683 i915_gem_retire_work_handler);
Chris Wilson67d97da2016-07-04 08:08:31 +01005684 INIT_DELAYED_WORK(&dev_priv->gt.idle_work,
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005685 i915_gem_idle_work_handler);
Chris Wilson1f15b762016-07-01 17:23:14 +01005686 init_waitqueue_head(&dev_priv->gpu_error.wait_queue);
Daniel Vetter1f83fee2012-11-15 17:17:22 +01005687 init_waitqueue_head(&dev_priv->gpu_error.reset_queue);
Chris Wilson31169712009-09-14 16:50:28 +01005688
Joonas Lahtinen6f633402016-09-01 14:58:21 +03005689 atomic_set(&dev_priv->mm.bsd_engine_dispatch_index, 0);
5690
Chris Wilsonb5add952016-08-04 16:32:36 +01005691 spin_lock_init(&dev_priv->fb_tracking.lock);
Chris Wilson73cb9702016-10-28 13:58:46 +01005692
Matthew Auld465c4032017-10-06 23:18:14 +01005693 err = i915_gemfs_init(dev_priv);
5694 if (err)
5695 DRM_NOTE("Unable to create a private tmpfs mount, hugepage support will be disabled(%d).\n", err);
5696
Chris Wilson73cb9702016-10-28 13:58:46 +01005697 return 0;
5698
Chris Wilson52e54202016-11-14 20:41:02 +00005699err_dependencies:
5700 kmem_cache_destroy(dev_priv->dependencies);
Chris Wilson73cb9702016-10-28 13:58:46 +01005701err_requests:
5702 kmem_cache_destroy(dev_priv->requests);
Chris Wilsond1b48c12017-08-16 09:52:08 +01005703err_luts:
5704 kmem_cache_destroy(dev_priv->luts);
Chris Wilson73cb9702016-10-28 13:58:46 +01005705err_vmas:
5706 kmem_cache_destroy(dev_priv->vmas);
5707err_objects:
5708 kmem_cache_destroy(dev_priv->objects);
5709err_out:
5710 return err;
Eric Anholt673a3942008-07-30 12:06:12 -07005711}
Dave Airlie71acb5e2008-12-30 20:31:46 +10005712
Michal Wajdeczkoa0de9082018-03-23 12:34:49 +00005713void i915_gem_cleanup_early(struct drm_i915_private *dev_priv)
Imre Deakd64aa092016-01-19 15:26:29 +02005714{
Chris Wilsonc4d4c1c2017-02-10 16:35:23 +00005715 i915_gem_drain_freed_objects(dev_priv);
Chris Wilsonc9c704712018-02-19 22:06:31 +00005716 GEM_BUG_ON(!llist_empty(&dev_priv->mm.free_list));
5717 GEM_BUG_ON(atomic_read(&dev_priv->mm.free_count));
Chris Wilsonc4d4c1c2017-02-10 16:35:23 +00005718 WARN_ON(dev_priv->mm.object_count);
Matthew Auldea84aa72016-11-17 21:04:11 +00005719 WARN_ON(!list_empty(&dev_priv->gt.timelines));
Matthew Auldea84aa72016-11-17 21:04:11 +00005720
Chris Wilsonc5cf9a92017-05-17 13:10:04 +01005721 kmem_cache_destroy(dev_priv->priorities);
Chris Wilson52e54202016-11-14 20:41:02 +00005722 kmem_cache_destroy(dev_priv->dependencies);
Imre Deakd64aa092016-01-19 15:26:29 +02005723 kmem_cache_destroy(dev_priv->requests);
Chris Wilsond1b48c12017-08-16 09:52:08 +01005724 kmem_cache_destroy(dev_priv->luts);
Imre Deakd64aa092016-01-19 15:26:29 +02005725 kmem_cache_destroy(dev_priv->vmas);
5726 kmem_cache_destroy(dev_priv->objects);
Chris Wilson0eafec62016-08-04 16:32:41 +01005727
5728 /* And ensure that our DESTROY_BY_RCU slabs are truly destroyed */
5729 rcu_barrier();
Matthew Auld465c4032017-10-06 23:18:14 +01005730
5731 i915_gemfs_fini(dev_priv);
Imre Deakd64aa092016-01-19 15:26:29 +02005732}
5733
Chris Wilson6a800ea2016-09-21 14:51:07 +01005734int i915_gem_freeze(struct drm_i915_private *dev_priv)
5735{
Chris Wilsond0aa3012017-04-07 11:25:49 +01005736 /* Discard all purgeable objects, let userspace recover those as
5737 * required after resuming.
5738 */
Chris Wilson6a800ea2016-09-21 14:51:07 +01005739 i915_gem_shrink_all(dev_priv);
Chris Wilson6a800ea2016-09-21 14:51:07 +01005740
Chris Wilson6a800ea2016-09-21 14:51:07 +01005741 return 0;
5742}
5743
Chris Wilson95c778d2018-06-01 15:41:25 +01005744int i915_gem_freeze_late(struct drm_i915_private *i915)
Chris Wilson461fb992016-05-14 07:26:33 +01005745{
5746 struct drm_i915_gem_object *obj;
Chris Wilson7aab2d52016-09-09 20:02:18 +01005747 struct list_head *phases[] = {
Chris Wilson95c778d2018-06-01 15:41:25 +01005748 &i915->mm.unbound_list,
5749 &i915->mm.bound_list,
Chris Wilson7aab2d52016-09-09 20:02:18 +01005750 NULL
Chris Wilson95c778d2018-06-01 15:41:25 +01005751 }, **phase;
Chris Wilson461fb992016-05-14 07:26:33 +01005752
Chris Wilson95c778d2018-06-01 15:41:25 +01005753 /*
5754 * Called just before we write the hibernation image.
Chris Wilson461fb992016-05-14 07:26:33 +01005755 *
5756 * We need to update the domain tracking to reflect that the CPU
5757 * will be accessing all the pages to create and restore from the
5758 * hibernation, and so upon restoration those pages will be in the
5759 * CPU domain.
5760 *
5761 * To make sure the hibernation image contains the latest state,
5762 * we update that state just before writing out the image.
Chris Wilson7aab2d52016-09-09 20:02:18 +01005763 *
5764 * To try and reduce the hibernation image, we manually shrink
Chris Wilsond0aa3012017-04-07 11:25:49 +01005765 * the objects as well, see i915_gem_freeze()
Chris Wilson461fb992016-05-14 07:26:33 +01005766 */
5767
Chris Wilson95c778d2018-06-01 15:41:25 +01005768 i915_gem_shrink(i915, -1UL, NULL, I915_SHRINK_UNBOUND);
5769 i915_gem_drain_freed_objects(i915);
Chris Wilson461fb992016-05-14 07:26:33 +01005770
Chris Wilson95c778d2018-06-01 15:41:25 +01005771 mutex_lock(&i915->drm.struct_mutex);
5772 for (phase = phases; *phase; phase++) {
5773 list_for_each_entry(obj, *phase, mm.link)
5774 WARN_ON(i915_gem_object_set_to_cpu_domain(obj, true));
Chris Wilson461fb992016-05-14 07:26:33 +01005775 }
Chris Wilson95c778d2018-06-01 15:41:25 +01005776 mutex_unlock(&i915->drm.struct_mutex);
Chris Wilson461fb992016-05-14 07:26:33 +01005777
5778 return 0;
5779}
5780
Chris Wilsonf787a5f2010-09-24 16:02:42 +01005781void i915_gem_release(struct drm_device *dev, struct drm_file *file)
Eric Anholtb9624422009-06-03 07:27:35 +00005782{
Chris Wilsonf787a5f2010-09-24 16:02:42 +01005783 struct drm_i915_file_private *file_priv = file->driver_priv;
Chris Wilsone61e0f52018-02-21 09:56:36 +00005784 struct i915_request *request;
Eric Anholtb9624422009-06-03 07:27:35 +00005785
5786 /* Clean up our request list when the client is going away, so that
5787 * later retire_requests won't dereference our soon-to-be-gone
5788 * file_priv.
5789 */
Chris Wilson1c255952010-09-26 11:03:27 +01005790 spin_lock(&file_priv->mm.lock);
Chris Wilsonc8659ef2017-03-02 12:25:25 +00005791 list_for_each_entry(request, &file_priv->mm.request_list, client_link)
Chris Wilsonf787a5f2010-09-24 16:02:42 +01005792 request->file_priv = NULL;
Chris Wilson1c255952010-09-26 11:03:27 +01005793 spin_unlock(&file_priv->mm.lock);
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005794}
5795
Chris Wilson829a0af2017-06-20 12:05:45 +01005796int i915_gem_open(struct drm_i915_private *i915, struct drm_file *file)
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005797{
5798 struct drm_i915_file_private *file_priv;
Ben Widawskye422b882013-12-06 14:10:58 -08005799 int ret;
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005800
Chris Wilsonc4c29d72016-11-09 10:45:07 +00005801 DRM_DEBUG("\n");
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005802
5803 file_priv = kzalloc(sizeof(*file_priv), GFP_KERNEL);
5804 if (!file_priv)
5805 return -ENOMEM;
5806
5807 file->driver_priv = file_priv;
Chris Wilson829a0af2017-06-20 12:05:45 +01005808 file_priv->dev_priv = i915;
Chris Wilsonab0e7ff2014-02-25 17:11:24 +02005809 file_priv->file = file;
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005810
5811 spin_lock_init(&file_priv->mm.lock);
5812 INIT_LIST_HEAD(&file_priv->mm.request_list);
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005813
Chris Wilsonc80ff162016-07-27 09:07:27 +01005814 file_priv->bsd_engine = -1;
Mika Kuoppala14921f32018-06-15 13:44:29 +03005815 file_priv->hang_timestamp = jiffies;
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00005816
Chris Wilson829a0af2017-06-20 12:05:45 +01005817 ret = i915_gem_context_open(i915, file);
Ben Widawskye422b882013-12-06 14:10:58 -08005818 if (ret)
5819 kfree(file_priv);
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005820
Ben Widawskye422b882013-12-06 14:10:58 -08005821 return ret;
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005822}
5823
Daniel Vetterb680c372014-09-19 18:27:27 +02005824/**
5825 * i915_gem_track_fb - update frontbuffer tracking
Geliang Tangd9072a32015-09-15 05:58:44 -07005826 * @old: current GEM buffer for the frontbuffer slots
5827 * @new: new GEM buffer for the frontbuffer slots
5828 * @frontbuffer_bits: bitmask of frontbuffer slots
Daniel Vetterb680c372014-09-19 18:27:27 +02005829 *
5830 * This updates the frontbuffer tracking bits @frontbuffer_bits by clearing them
5831 * from @old and setting them in @new. Both @old and @new can be NULL.
5832 */
Daniel Vettera071fa02014-06-18 23:28:09 +02005833void i915_gem_track_fb(struct drm_i915_gem_object *old,
5834 struct drm_i915_gem_object *new,
5835 unsigned frontbuffer_bits)
5836{
Chris Wilsonfaf5bf02016-08-04 16:32:37 +01005837 /* Control of individual bits within the mask are guarded by
5838 * the owning plane->mutex, i.e. we can never see concurrent
5839 * manipulation of individual bits. But since the bitfield as a whole
5840 * is updated using RMW, we need to use atomics in order to update
5841 * the bits.
5842 */
5843 BUILD_BUG_ON(INTEL_FRONTBUFFER_BITS_PER_PIPE * I915_MAX_PIPES >
Chris Wilson74f6e182018-09-26 11:47:07 +01005844 BITS_PER_TYPE(atomic_t));
Chris Wilsonfaf5bf02016-08-04 16:32:37 +01005845
Daniel Vettera071fa02014-06-18 23:28:09 +02005846 if (old) {
Chris Wilsonfaf5bf02016-08-04 16:32:37 +01005847 WARN_ON(!(atomic_read(&old->frontbuffer_bits) & frontbuffer_bits));
5848 atomic_andnot(frontbuffer_bits, &old->frontbuffer_bits);
Daniel Vettera071fa02014-06-18 23:28:09 +02005849 }
5850
5851 if (new) {
Chris Wilsonfaf5bf02016-08-04 16:32:37 +01005852 WARN_ON(atomic_read(&new->frontbuffer_bits) & frontbuffer_bits);
5853 atomic_or(frontbuffer_bits, &new->frontbuffer_bits);
Daniel Vettera071fa02014-06-18 23:28:09 +02005854 }
5855}
5856
Dave Gordonea702992015-07-09 19:29:02 +01005857/* Allocate a new GEM object and fill it with the supplied data */
5858struct drm_i915_gem_object *
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +00005859i915_gem_object_create_from_data(struct drm_i915_private *dev_priv,
Dave Gordonea702992015-07-09 19:29:02 +01005860 const void *data, size_t size)
5861{
5862 struct drm_i915_gem_object *obj;
Chris Wilsonbe062fa2017-03-17 19:46:48 +00005863 struct file *file;
5864 size_t offset;
5865 int err;
Dave Gordonea702992015-07-09 19:29:02 +01005866
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +00005867 obj = i915_gem_object_create(dev_priv, round_up(size, PAGE_SIZE));
Chris Wilsonfe3db792016-04-25 13:32:13 +01005868 if (IS_ERR(obj))
Dave Gordonea702992015-07-09 19:29:02 +01005869 return obj;
5870
Christian Königc0a51fd2018-02-16 13:43:38 +01005871 GEM_BUG_ON(obj->write_domain != I915_GEM_DOMAIN_CPU);
Dave Gordonea702992015-07-09 19:29:02 +01005872
Chris Wilsonbe062fa2017-03-17 19:46:48 +00005873 file = obj->base.filp;
5874 offset = 0;
5875 do {
5876 unsigned int len = min_t(typeof(size), size, PAGE_SIZE);
5877 struct page *page;
5878 void *pgdata, *vaddr;
Dave Gordonea702992015-07-09 19:29:02 +01005879
Chris Wilsonbe062fa2017-03-17 19:46:48 +00005880 err = pagecache_write_begin(file, file->f_mapping,
5881 offset, len, 0,
5882 &page, &pgdata);
5883 if (err < 0)
5884 goto fail;
Dave Gordonea702992015-07-09 19:29:02 +01005885
Chris Wilsonbe062fa2017-03-17 19:46:48 +00005886 vaddr = kmap(page);
5887 memcpy(vaddr, data, len);
5888 kunmap(page);
5889
5890 err = pagecache_write_end(file, file->f_mapping,
5891 offset, len, len,
5892 page, pgdata);
5893 if (err < 0)
5894 goto fail;
5895
5896 size -= len;
5897 data += len;
5898 offset += len;
5899 } while (size);
Dave Gordonea702992015-07-09 19:29:02 +01005900
5901 return obj;
5902
5903fail:
Chris Wilsonf8c417c2016-07-20 13:31:53 +01005904 i915_gem_object_put(obj);
Chris Wilsonbe062fa2017-03-17 19:46:48 +00005905 return ERR_PTR(err);
Dave Gordonea702992015-07-09 19:29:02 +01005906}
Chris Wilson96d77632016-10-28 13:58:33 +01005907
5908struct scatterlist *
5909i915_gem_object_get_sg(struct drm_i915_gem_object *obj,
5910 unsigned int n,
5911 unsigned int *offset)
5912{
Chris Wilsona4f5ea62016-10-28 13:58:35 +01005913 struct i915_gem_object_page_iter *iter = &obj->mm.get_page;
Chris Wilson96d77632016-10-28 13:58:33 +01005914 struct scatterlist *sg;
5915 unsigned int idx, count;
5916
5917 might_sleep();
5918 GEM_BUG_ON(n >= obj->base.size >> PAGE_SHIFT);
Chris Wilsona4f5ea62016-10-28 13:58:35 +01005919 GEM_BUG_ON(!i915_gem_object_has_pinned_pages(obj));
Chris Wilson96d77632016-10-28 13:58:33 +01005920
5921 /* As we iterate forward through the sg, we record each entry in a
5922 * radixtree for quick repeated (backwards) lookups. If we have seen
5923 * this index previously, we will have an entry for it.
5924 *
5925 * Initial lookup is O(N), but this is amortized to O(1) for
5926 * sequential page access (where each new request is consecutive
5927 * to the previous one). Repeated lookups are O(lg(obj->base.size)),
5928 * i.e. O(1) with a large constant!
5929 */
5930 if (n < READ_ONCE(iter->sg_idx))
5931 goto lookup;
5932
5933 mutex_lock(&iter->lock);
5934
5935 /* We prefer to reuse the last sg so that repeated lookup of this
5936 * (or the subsequent) sg are fast - comparing against the last
5937 * sg is faster than going through the radixtree.
5938 */
5939
5940 sg = iter->sg_pos;
5941 idx = iter->sg_idx;
5942 count = __sg_page_count(sg);
5943
5944 while (idx + count <= n) {
Matthew Wilcox3159f942017-11-03 13:30:42 -04005945 void *entry;
5946 unsigned long i;
Chris Wilson96d77632016-10-28 13:58:33 +01005947 int ret;
5948
5949 /* If we cannot allocate and insert this entry, or the
5950 * individual pages from this range, cancel updating the
5951 * sg_idx so that on this lookup we are forced to linearly
5952 * scan onwards, but on future lookups we will try the
5953 * insertion again (in which case we need to be careful of
5954 * the error return reporting that we have already inserted
5955 * this index).
5956 */
5957 ret = radix_tree_insert(&iter->radix, idx, sg);
5958 if (ret && ret != -EEXIST)
5959 goto scan;
5960
Matthew Wilcox3159f942017-11-03 13:30:42 -04005961 entry = xa_mk_value(idx);
Chris Wilson96d77632016-10-28 13:58:33 +01005962 for (i = 1; i < count; i++) {
Matthew Wilcox3159f942017-11-03 13:30:42 -04005963 ret = radix_tree_insert(&iter->radix, idx + i, entry);
Chris Wilson96d77632016-10-28 13:58:33 +01005964 if (ret && ret != -EEXIST)
5965 goto scan;
5966 }
5967
5968 idx += count;
5969 sg = ____sg_next(sg);
5970 count = __sg_page_count(sg);
5971 }
5972
5973scan:
5974 iter->sg_pos = sg;
5975 iter->sg_idx = idx;
5976
5977 mutex_unlock(&iter->lock);
5978
5979 if (unlikely(n < idx)) /* insertion completed by another thread */
5980 goto lookup;
5981
5982 /* In case we failed to insert the entry into the radixtree, we need
5983 * to look beyond the current sg.
5984 */
5985 while (idx + count <= n) {
5986 idx += count;
5987 sg = ____sg_next(sg);
5988 count = __sg_page_count(sg);
5989 }
5990
5991 *offset = n - idx;
5992 return sg;
5993
5994lookup:
5995 rcu_read_lock();
5996
5997 sg = radix_tree_lookup(&iter->radix, n);
5998 GEM_BUG_ON(!sg);
5999
6000 /* If this index is in the middle of multi-page sg entry,
Matthew Wilcox3159f942017-11-03 13:30:42 -04006001 * the radix tree will contain a value entry that points
Chris Wilson96d77632016-10-28 13:58:33 +01006002 * to the start of that range. We will return the pointer to
6003 * the base page and the offset of this page within the
6004 * sg entry's range.
6005 */
6006 *offset = 0;
Matthew Wilcox3159f942017-11-03 13:30:42 -04006007 if (unlikely(xa_is_value(sg))) {
6008 unsigned long base = xa_to_value(sg);
Chris Wilson96d77632016-10-28 13:58:33 +01006009
6010 sg = radix_tree_lookup(&iter->radix, base);
6011 GEM_BUG_ON(!sg);
6012
6013 *offset = n - base;
6014 }
6015
6016 rcu_read_unlock();
6017
6018 return sg;
6019}
6020
6021struct page *
6022i915_gem_object_get_page(struct drm_i915_gem_object *obj, unsigned int n)
6023{
6024 struct scatterlist *sg;
6025 unsigned int offset;
6026
6027 GEM_BUG_ON(!i915_gem_object_has_struct_page(obj));
6028
6029 sg = i915_gem_object_get_sg(obj, n, &offset);
6030 return nth_page(sg_page(sg), offset);
6031}
6032
6033/* Like i915_gem_object_get_page(), but mark the returned page dirty */
6034struct page *
6035i915_gem_object_get_dirty_page(struct drm_i915_gem_object *obj,
6036 unsigned int n)
6037{
6038 struct page *page;
6039
6040 page = i915_gem_object_get_page(obj, n);
Chris Wilsona4f5ea62016-10-28 13:58:35 +01006041 if (!obj->mm.dirty)
Chris Wilson96d77632016-10-28 13:58:33 +01006042 set_page_dirty(page);
6043
6044 return page;
6045}
6046
6047dma_addr_t
6048i915_gem_object_get_dma_address(struct drm_i915_gem_object *obj,
6049 unsigned long n)
6050{
6051 struct scatterlist *sg;
6052 unsigned int offset;
6053
6054 sg = i915_gem_object_get_sg(obj, n, &offset);
6055 return sg_dma_address(sg) + (offset << PAGE_SHIFT);
6056}
Chris Wilson935a2f72017-02-13 17:15:13 +00006057
Chris Wilson8eeb7902017-07-26 19:16:01 +01006058int i915_gem_object_attach_phys(struct drm_i915_gem_object *obj, int align)
6059{
6060 struct sg_table *pages;
6061 int err;
6062
6063 if (align > obj->base.size)
6064 return -EINVAL;
6065
6066 if (obj->ops == &i915_gem_phys_ops)
6067 return 0;
6068
6069 if (obj->ops != &i915_gem_object_ops)
6070 return -EINVAL;
6071
6072 err = i915_gem_object_unbind(obj);
6073 if (err)
6074 return err;
6075
6076 mutex_lock(&obj->mm.lock);
6077
6078 if (obj->mm.madv != I915_MADV_WILLNEED) {
6079 err = -EFAULT;
6080 goto err_unlock;
6081 }
6082
6083 if (obj->mm.quirked) {
6084 err = -EFAULT;
6085 goto err_unlock;
6086 }
6087
6088 if (obj->mm.mapping) {
6089 err = -EBUSY;
6090 goto err_unlock;
6091 }
6092
Chris Wilsonacd1c1e2018-06-11 08:55:32 +01006093 pages = __i915_gem_object_unset_pages(obj);
Chris Wilsonf2123812017-10-16 12:40:37 +01006094
Chris Wilson8eeb7902017-07-26 19:16:01 +01006095 obj->ops = &i915_gem_phys_ops;
6096
Chris Wilson8fb6a5d2017-07-26 19:16:02 +01006097 err = ____i915_gem_object_get_pages(obj);
Chris Wilson8eeb7902017-07-26 19:16:01 +01006098 if (err)
6099 goto err_xfer;
6100
6101 /* Perma-pin (until release) the physical set of pages */
6102 __i915_gem_object_pin_pages(obj);
6103
6104 if (!IS_ERR_OR_NULL(pages))
6105 i915_gem_object_ops.put_pages(obj, pages);
6106 mutex_unlock(&obj->mm.lock);
6107 return 0;
6108
6109err_xfer:
6110 obj->ops = &i915_gem_object_ops;
Chris Wilsonacd1c1e2018-06-11 08:55:32 +01006111 if (!IS_ERR_OR_NULL(pages)) {
6112 unsigned int sg_page_sizes = i915_sg_page_sizes(pages->sgl);
6113
6114 __i915_gem_object_set_pages(obj, pages, sg_page_sizes);
6115 }
Chris Wilson8eeb7902017-07-26 19:16:01 +01006116err_unlock:
6117 mutex_unlock(&obj->mm.lock);
6118 return err;
6119}
6120
Chris Wilson935a2f72017-02-13 17:15:13 +00006121#if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
6122#include "selftests/scatterlist.c"
Chris Wilson66d9cb52017-02-13 17:15:17 +00006123#include "selftests/mock_gem_device.c"
Chris Wilson44653982017-02-13 17:15:20 +00006124#include "selftests/huge_gem_object.c"
Matthew Auld40498662017-10-06 23:18:29 +01006125#include "selftests/huge_pages.c"
Chris Wilson8335fd62017-02-13 17:15:28 +00006126#include "selftests/i915_gem_object.c"
Chris Wilson17059452017-02-13 17:15:32 +00006127#include "selftests/i915_gem_coherency.c"
Chris Wilson3f51b7e12018-08-30 14:48:06 +01006128#include "selftests/i915_gem.c"
Chris Wilson935a2f72017-02-13 17:15:13 +00006129#endif