blob: f5c4ef052001711ba578b324ae18260f3d3630a2 [file] [log] [blame]
Eric Anholt673a3942008-07-30 12:06:12 -07001/*
Daniel Vetterbe6a0372015-03-18 10:46:04 +01002 * Copyright © 2008-2015 Intel Corporation
Eric Anholt673a3942008-07-30 12:06:12 -07003 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 *
23 * Authors:
24 * Eric Anholt <eric@anholt.net>
25 *
26 */
27
David Howells760285e2012-10-02 18:01:07 +010028#include <drm/drmP.h>
David Herrmann0de23972013-07-24 21:07:52 +020029#include <drm/drm_vma_manager.h>
David Howells760285e2012-10-02 18:01:07 +010030#include <drm/i915_drm.h>
Eric Anholt673a3942008-07-30 12:06:12 -070031#include "i915_drv.h"
Chris Wilson57822dc2017-02-22 11:40:48 +000032#include "i915_gem_clflush.h"
Yu Zhangeb822892015-02-10 19:05:49 +080033#include "i915_vgpu.h"
Chris Wilson1c5d22f2009-08-25 11:15:50 +010034#include "i915_trace.h"
Jesse Barnes652c3932009-08-17 13:31:43 -070035#include "intel_drv.h"
Chris Wilson5d723d72016-08-04 16:32:35 +010036#include "intel_frontbuffer.h"
Peter Antoine0ccdacf2016-04-13 15:03:25 +010037#include "intel_mocs.h"
Oscar Mateo59b449d2018-04-10 09:12:47 -070038#include "intel_workarounds.h"
Matthew Auld465c4032017-10-06 23:18:14 +010039#include "i915_gemfs.h"
Chris Wilson6b5e90f2016-11-14 20:41:05 +000040#include <linux/dma-fence-array.h>
Chris Wilsonfe3288b2017-02-12 17:20:01 +000041#include <linux/kthread.h>
Chris Wilsonc13d87e2016-07-20 09:21:15 +010042#include <linux/reservation.h>
Hugh Dickins5949eac2011-06-27 16:18:18 -070043#include <linux/shmem_fs.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090044#include <linux/slab.h>
Chris Wilson20e49332016-11-22 14:41:21 +000045#include <linux/stop_machine.h>
Eric Anholt673a3942008-07-30 12:06:12 -070046#include <linux/swap.h>
Jesse Barnes79e53942008-11-07 14:24:08 -080047#include <linux/pci.h>
Daniel Vetter1286ff72012-05-10 15:25:09 +020048#include <linux/dma-buf.h>
Eric Anholt673a3942008-07-30 12:06:12 -070049
Chris Wilsonfbbd37b2016-10-28 13:58:42 +010050static void i915_gem_flush_free_objects(struct drm_i915_private *i915);
Chris Wilson61050802012-04-17 15:31:31 +010051
Chris Wilson2c225692013-08-09 12:26:45 +010052static bool cpu_write_needs_clflush(struct drm_i915_gem_object *obj)
53{
Chris Wilsone27ab732017-06-15 13:38:49 +010054 if (obj->cache_dirty)
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +053055 return false;
56
Chris Wilsonb8f55be2017-08-11 12:11:16 +010057 if (!(obj->cache_coherent & I915_BO_CACHE_COHERENT_FOR_WRITE))
Chris Wilson2c225692013-08-09 12:26:45 +010058 return true;
59
Chris Wilsonbd3d2252017-10-13 21:26:14 +010060 return obj->pin_global; /* currently in use by HW, keep flushed */
Chris Wilson2c225692013-08-09 12:26:45 +010061}
62
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +053063static int
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +010064insert_mappable_node(struct i915_ggtt *ggtt,
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +053065 struct drm_mm_node *node, u32 size)
66{
67 memset(node, 0, sizeof(*node));
Chris Wilson4e64e552017-02-02 21:04:38 +000068 return drm_mm_insert_node_in_range(&ggtt->base.mm, node,
69 size, 0, I915_COLOR_UNEVICTABLE,
70 0, ggtt->mappable_end,
71 DRM_MM_INSERT_LOW);
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +053072}
73
74static void
75remove_mappable_node(struct drm_mm_node *node)
76{
77 drm_mm_remove_node(node);
78}
79
Chris Wilson73aa8082010-09-30 11:46:12 +010080/* some bookkeeping */
81static void i915_gem_info_add_obj(struct drm_i915_private *dev_priv,
Chris Wilson3ef7f222016-10-18 13:02:48 +010082 u64 size)
Chris Wilson73aa8082010-09-30 11:46:12 +010083{
Daniel Vetterc20e8352013-07-24 22:40:23 +020084 spin_lock(&dev_priv->mm.object_stat_lock);
Chris Wilson73aa8082010-09-30 11:46:12 +010085 dev_priv->mm.object_count++;
86 dev_priv->mm.object_memory += size;
Daniel Vetterc20e8352013-07-24 22:40:23 +020087 spin_unlock(&dev_priv->mm.object_stat_lock);
Chris Wilson73aa8082010-09-30 11:46:12 +010088}
89
90static void i915_gem_info_remove_obj(struct drm_i915_private *dev_priv,
Chris Wilson3ef7f222016-10-18 13:02:48 +010091 u64 size)
Chris Wilson73aa8082010-09-30 11:46:12 +010092{
Daniel Vetterc20e8352013-07-24 22:40:23 +020093 spin_lock(&dev_priv->mm.object_stat_lock);
Chris Wilson73aa8082010-09-30 11:46:12 +010094 dev_priv->mm.object_count--;
95 dev_priv->mm.object_memory -= size;
Daniel Vetterc20e8352013-07-24 22:40:23 +020096 spin_unlock(&dev_priv->mm.object_stat_lock);
Chris Wilson73aa8082010-09-30 11:46:12 +010097}
98
Chris Wilson21dd3732011-01-26 15:55:56 +000099static int
Daniel Vetter33196de2012-11-14 17:14:05 +0100100i915_gem_wait_for_error(struct i915_gpu_error *error)
Chris Wilson30dbf0c2010-09-25 10:19:17 +0100101{
Chris Wilson30dbf0c2010-09-25 10:19:17 +0100102 int ret;
103
Chris Wilson4c7d62c2016-10-28 13:58:32 +0100104 might_sleep();
105
Daniel Vetter0a6759c2012-07-04 22:18:41 +0200106 /*
107 * Only wait 10 seconds for the gpu reset to complete to avoid hanging
108 * userspace. If it takes that long something really bad is going on and
109 * we should simply try to bail out and fail as gracefully as possible.
110 */
Daniel Vetter1f83fee2012-11-15 17:17:22 +0100111 ret = wait_event_interruptible_timeout(error->reset_queue,
Chris Wilson8c185ec2017-03-16 17:13:02 +0000112 !i915_reset_backoff(error),
Chris Wilsonb52992c2016-10-28 13:58:24 +0100113 I915_RESET_TIMEOUT);
Daniel Vetter0a6759c2012-07-04 22:18:41 +0200114 if (ret == 0) {
115 DRM_ERROR("Timed out waiting for the gpu reset to complete\n");
116 return -EIO;
117 } else if (ret < 0) {
Chris Wilson30dbf0c2010-09-25 10:19:17 +0100118 return ret;
Chris Wilsond98c52c2016-04-13 17:35:05 +0100119 } else {
120 return 0;
Daniel Vetter0a6759c2012-07-04 22:18:41 +0200121 }
Chris Wilson30dbf0c2010-09-25 10:19:17 +0100122}
123
Chris Wilson54cf91d2010-11-25 18:00:26 +0000124int i915_mutex_lock_interruptible(struct drm_device *dev)
Chris Wilson76c1dec2010-09-25 11:22:51 +0100125{
Chris Wilsonfac5e232016-07-04 11:34:36 +0100126 struct drm_i915_private *dev_priv = to_i915(dev);
Chris Wilson76c1dec2010-09-25 11:22:51 +0100127 int ret;
128
Daniel Vetter33196de2012-11-14 17:14:05 +0100129 ret = i915_gem_wait_for_error(&dev_priv->gpu_error);
Chris Wilson76c1dec2010-09-25 11:22:51 +0100130 if (ret)
131 return ret;
132
133 ret = mutex_lock_interruptible(&dev->struct_mutex);
134 if (ret)
135 return ret;
136
Chris Wilson76c1dec2010-09-25 11:22:51 +0100137 return 0;
138}
Chris Wilson30dbf0c2010-09-25 10:19:17 +0100139
Chris Wilsone4d20062018-04-06 16:51:44 +0100140static u32 __i915_gem_park(struct drm_i915_private *i915)
141{
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
Weinan Liff8f7972017-05-31 10:35:52 +0800252 pinned = ggtt->base.reserved;
Chris Wilson73aa8082010-09-30 11:46:12 +0100253 mutex_lock(&dev->struct_mutex);
Chris Wilson1c7f4bc2016-02-26 11:03:19 +0000254 list_for_each_entry(vma, &ggtt->base.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 Wilson1c7f4bc2016-02-26 11:03:19 +0000257 list_for_each_entry(vma, &ggtt->base.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
Joonas Lahtinen72e96d62016-03-30 16:57:10 +0300262 args->aper_size = ggtt->base.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 Wilsonef749212017-04-12 12:01:10 +0100805 wmb();
806
Chris Wilson7125397b2017-12-06 12:49:14 +0000807 intel_runtime_pm_get(dev_priv);
808 spin_lock_irq(&dev_priv->uncore.lock);
809
810 POSTING_READ_FW(RING_HEAD(RENDER_RING_BASE));
811
812 spin_unlock_irq(&dev_priv->uncore.lock);
813 intel_runtime_pm_put(dev_priv);
814}
815
816static void
817flush_write_domain(struct drm_i915_gem_object *obj, unsigned int flush_domains)
818{
819 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
820 struct i915_vma *vma;
821
Christian Königc0a51fd2018-02-16 13:43:38 +0100822 if (!(obj->write_domain & flush_domains))
Chris Wilson7125397b2017-12-06 12:49:14 +0000823 return;
824
Christian Königc0a51fd2018-02-16 13:43:38 +0100825 switch (obj->write_domain) {
Chris Wilsonef749212017-04-12 12:01:10 +0100826 case I915_GEM_DOMAIN_GTT:
Chris Wilson7125397b2017-12-06 12:49:14 +0000827 i915_gem_flush_ggtt_writes(dev_priv);
Chris Wilsonef749212017-04-12 12:01:10 +0100828
829 intel_fb_obj_flush(obj,
830 fb_write_origin(obj, I915_GEM_DOMAIN_GTT));
Chris Wilson7125397b2017-12-06 12:49:14 +0000831
Chris Wilsone2189dd2017-12-07 21:14:07 +0000832 for_each_ggtt_vma(vma, obj) {
Chris Wilson7125397b2017-12-06 12:49:14 +0000833 if (vma->iomap)
834 continue;
835
836 i915_vma_unset_ggtt_write(vma);
837 }
Chris Wilsonef749212017-04-12 12:01:10 +0100838 break;
839
840 case I915_GEM_DOMAIN_CPU:
841 i915_gem_clflush_object(obj, I915_CLFLUSH_SYNC);
842 break;
Chris Wilsone27ab732017-06-15 13:38:49 +0100843
844 case I915_GEM_DOMAIN_RENDER:
845 if (gpu_write_needs_clflush(obj))
846 obj->cache_dirty = true;
847 break;
Chris Wilsonef749212017-04-12 12:01:10 +0100848 }
849
Christian Königc0a51fd2018-02-16 13:43:38 +0100850 obj->write_domain = 0;
Chris Wilsonef749212017-04-12 12:01:10 +0100851}
852
Daniel Vetter8c599672011-12-14 13:57:31 +0100853static inline int
Daniel Vetter8461d222011-12-14 13:57:32 +0100854__copy_to_user_swizzled(char __user *cpu_vaddr,
855 const char *gpu_vaddr, int gpu_offset,
856 int length)
857{
858 int ret, cpu_offset = 0;
859
860 while (length > 0) {
861 int cacheline_end = ALIGN(gpu_offset + 1, 64);
862 int this_length = min(cacheline_end - gpu_offset, length);
863 int swizzled_gpu_offset = gpu_offset ^ 64;
864
865 ret = __copy_to_user(cpu_vaddr + cpu_offset,
866 gpu_vaddr + swizzled_gpu_offset,
867 this_length);
868 if (ret)
869 return ret + length;
870
871 cpu_offset += this_length;
872 gpu_offset += this_length;
873 length -= this_length;
874 }
875
876 return 0;
877}
878
879static inline int
Ben Widawsky4f0c7cf2012-04-16 14:07:47 -0700880__copy_from_user_swizzled(char *gpu_vaddr, int gpu_offset,
881 const char __user *cpu_vaddr,
Daniel Vetter8c599672011-12-14 13:57:31 +0100882 int length)
883{
884 int ret, cpu_offset = 0;
885
886 while (length > 0) {
887 int cacheline_end = ALIGN(gpu_offset + 1, 64);
888 int this_length = min(cacheline_end - gpu_offset, length);
889 int swizzled_gpu_offset = gpu_offset ^ 64;
890
891 ret = __copy_from_user(gpu_vaddr + swizzled_gpu_offset,
892 cpu_vaddr + cpu_offset,
893 this_length);
894 if (ret)
895 return ret + length;
896
897 cpu_offset += this_length;
898 gpu_offset += this_length;
899 length -= this_length;
900 }
901
902 return 0;
903}
904
Brad Volkin4c914c02014-02-18 10:15:45 -0800905/*
906 * Pins the specified object's pages and synchronizes the object with
907 * GPU accesses. Sets needs_clflush to non-zero if the caller should
908 * flush the object from the CPU cache.
909 */
910int i915_gem_obj_prepare_shmem_read(struct drm_i915_gem_object *obj,
Chris Wilson43394c72016-08-18 17:16:47 +0100911 unsigned int *needs_clflush)
Brad Volkin4c914c02014-02-18 10:15:45 -0800912{
913 int ret;
914
Chris Wilsone95433c2016-10-28 13:58:27 +0100915 lockdep_assert_held(&obj->base.dev->struct_mutex);
Brad Volkin4c914c02014-02-18 10:15:45 -0800916
Chris Wilsone95433c2016-10-28 13:58:27 +0100917 *needs_clflush = 0;
Chris Wilson43394c72016-08-18 17:16:47 +0100918 if (!i915_gem_object_has_struct_page(obj))
919 return -ENODEV;
Brad Volkin4c914c02014-02-18 10:15:45 -0800920
Chris Wilsone95433c2016-10-28 13:58:27 +0100921 ret = i915_gem_object_wait(obj,
922 I915_WAIT_INTERRUPTIBLE |
923 I915_WAIT_LOCKED,
924 MAX_SCHEDULE_TIMEOUT,
925 NULL);
Chris Wilsonc13d87e2016-07-20 09:21:15 +0100926 if (ret)
927 return ret;
928
Chris Wilsona4f5ea62016-10-28 13:58:35 +0100929 ret = i915_gem_object_pin_pages(obj);
Chris Wilson97649512016-08-18 17:16:50 +0100930 if (ret)
931 return ret;
932
Chris Wilsonb8f55be2017-08-11 12:11:16 +0100933 if (obj->cache_coherent & I915_BO_CACHE_COHERENT_FOR_READ ||
934 !static_cpu_has(X86_FEATURE_CLFLUSH)) {
Chris Wilson7f5f95d2017-03-10 00:09:42 +0000935 ret = i915_gem_object_set_to_cpu_domain(obj, false);
936 if (ret)
937 goto err_unpin;
938 else
939 goto out;
940 }
941
Chris Wilsonef749212017-04-12 12:01:10 +0100942 flush_write_domain(obj, ~I915_GEM_DOMAIN_CPU);
Chris Wilsona314d5c2016-08-18 17:16:48 +0100943
Chris Wilson43394c72016-08-18 17:16:47 +0100944 /* If we're not in the cpu read domain, set ourself into the gtt
945 * read domain and manually flush cachelines (if required). This
946 * optimizes for the case when the gpu will dirty the data
947 * anyway again before the next pread happens.
948 */
Chris Wilsone27ab732017-06-15 13:38:49 +0100949 if (!obj->cache_dirty &&
Christian Königc0a51fd2018-02-16 13:43:38 +0100950 !(obj->read_domains & I915_GEM_DOMAIN_CPU))
Chris Wilson7f5f95d2017-03-10 00:09:42 +0000951 *needs_clflush = CLFLUSH_BEFORE;
Brad Volkin4c914c02014-02-18 10:15:45 -0800952
Chris Wilson7f5f95d2017-03-10 00:09:42 +0000953out:
Chris Wilson97649512016-08-18 17:16:50 +0100954 /* return with the pages pinned */
Chris Wilson43394c72016-08-18 17:16:47 +0100955 return 0;
Chris Wilson97649512016-08-18 17:16:50 +0100956
957err_unpin:
958 i915_gem_object_unpin_pages(obj);
959 return ret;
Chris Wilson43394c72016-08-18 17:16:47 +0100960}
961
962int i915_gem_obj_prepare_shmem_write(struct drm_i915_gem_object *obj,
963 unsigned int *needs_clflush)
964{
965 int ret;
966
Chris Wilsone95433c2016-10-28 13:58:27 +0100967 lockdep_assert_held(&obj->base.dev->struct_mutex);
968
Chris Wilson43394c72016-08-18 17:16:47 +0100969 *needs_clflush = 0;
970 if (!i915_gem_object_has_struct_page(obj))
971 return -ENODEV;
972
Chris Wilsone95433c2016-10-28 13:58:27 +0100973 ret = i915_gem_object_wait(obj,
974 I915_WAIT_INTERRUPTIBLE |
975 I915_WAIT_LOCKED |
976 I915_WAIT_ALL,
977 MAX_SCHEDULE_TIMEOUT,
978 NULL);
Chris Wilson43394c72016-08-18 17:16:47 +0100979 if (ret)
980 return ret;
981
Chris Wilsona4f5ea62016-10-28 13:58:35 +0100982 ret = i915_gem_object_pin_pages(obj);
Chris Wilson97649512016-08-18 17:16:50 +0100983 if (ret)
984 return ret;
985
Chris Wilsonb8f55be2017-08-11 12:11:16 +0100986 if (obj->cache_coherent & I915_BO_CACHE_COHERENT_FOR_WRITE ||
987 !static_cpu_has(X86_FEATURE_CLFLUSH)) {
Chris Wilson7f5f95d2017-03-10 00:09:42 +0000988 ret = i915_gem_object_set_to_cpu_domain(obj, true);
989 if (ret)
990 goto err_unpin;
991 else
992 goto out;
993 }
994
Chris Wilsonef749212017-04-12 12:01:10 +0100995 flush_write_domain(obj, ~I915_GEM_DOMAIN_CPU);
Chris Wilsona314d5c2016-08-18 17:16:48 +0100996
Chris Wilson43394c72016-08-18 17:16:47 +0100997 /* If we're not in the cpu write domain, set ourself into the
998 * gtt write domain and manually flush cachelines (as required).
999 * This optimizes for the case when the gpu will use the data
1000 * right away and we therefore have to clflush anyway.
1001 */
Chris Wilsone27ab732017-06-15 13:38:49 +01001002 if (!obj->cache_dirty) {
Chris Wilson7f5f95d2017-03-10 00:09:42 +00001003 *needs_clflush |= CLFLUSH_AFTER;
Chris Wilson43394c72016-08-18 17:16:47 +01001004
Chris Wilsone27ab732017-06-15 13:38:49 +01001005 /*
1006 * Same trick applies to invalidate partially written
1007 * cachelines read before writing.
1008 */
Christian Königc0a51fd2018-02-16 13:43:38 +01001009 if (!(obj->read_domains & I915_GEM_DOMAIN_CPU))
Chris Wilsone27ab732017-06-15 13:38:49 +01001010 *needs_clflush |= CLFLUSH_BEFORE;
1011 }
Chris Wilson43394c72016-08-18 17:16:47 +01001012
Chris Wilson7f5f95d2017-03-10 00:09:42 +00001013out:
Chris Wilson43394c72016-08-18 17:16:47 +01001014 intel_fb_obj_invalidate(obj, ORIGIN_CPU);
Chris Wilsona4f5ea62016-10-28 13:58:35 +01001015 obj->mm.dirty = true;
Chris Wilson97649512016-08-18 17:16:50 +01001016 /* return with the pages pinned */
Chris Wilson43394c72016-08-18 17:16:47 +01001017 return 0;
Chris Wilson97649512016-08-18 17:16:50 +01001018
1019err_unpin:
1020 i915_gem_object_unpin_pages(obj);
1021 return ret;
Brad Volkin4c914c02014-02-18 10:15:45 -08001022}
1023
Daniel Vetter23c18c72012-03-25 19:47:42 +02001024static void
1025shmem_clflush_swizzled_range(char *addr, unsigned long length,
1026 bool swizzled)
1027{
Daniel Vettere7e58eb2012-03-25 19:47:43 +02001028 if (unlikely(swizzled)) {
Daniel Vetter23c18c72012-03-25 19:47:42 +02001029 unsigned long start = (unsigned long) addr;
1030 unsigned long end = (unsigned long) addr + length;
1031
1032 /* For swizzling simply ensure that we always flush both
1033 * channels. Lame, but simple and it works. Swizzled
1034 * pwrite/pread is far from a hotpath - current userspace
1035 * doesn't use it at all. */
1036 start = round_down(start, 128);
1037 end = round_up(end, 128);
1038
1039 drm_clflush_virt_range((void *)start, end - start);
1040 } else {
1041 drm_clflush_virt_range(addr, length);
1042 }
1043
1044}
1045
Daniel Vetterd174bd62012-03-25 19:47:40 +02001046/* Only difference to the fast-path function is that this can handle bit17
1047 * and uses non-atomic copy and kmap functions. */
1048static int
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001049shmem_pread_slow(struct page *page, int offset, int length,
Daniel Vetterd174bd62012-03-25 19:47:40 +02001050 char __user *user_data,
1051 bool page_do_bit17_swizzling, bool needs_clflush)
1052{
1053 char *vaddr;
1054 int ret;
1055
1056 vaddr = kmap(page);
1057 if (needs_clflush)
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001058 shmem_clflush_swizzled_range(vaddr + offset, length,
Daniel Vetter23c18c72012-03-25 19:47:42 +02001059 page_do_bit17_swizzling);
Daniel Vetterd174bd62012-03-25 19:47:40 +02001060
1061 if (page_do_bit17_swizzling)
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001062 ret = __copy_to_user_swizzled(user_data, vaddr, offset, length);
Daniel Vetterd174bd62012-03-25 19:47:40 +02001063 else
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001064 ret = __copy_to_user(user_data, vaddr + offset, length);
Daniel Vetterd174bd62012-03-25 19:47:40 +02001065 kunmap(page);
1066
Chris Wilsonf60d7f02012-09-04 21:02:56 +01001067 return ret ? - EFAULT : 0;
Daniel Vetterd174bd62012-03-25 19:47:40 +02001068}
1069
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001070static int
1071shmem_pread(struct page *page, int offset, int length, char __user *user_data,
1072 bool page_do_bit17_swizzling, bool needs_clflush)
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301073{
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001074 int ret;
1075
1076 ret = -ENODEV;
1077 if (!page_do_bit17_swizzling) {
1078 char *vaddr = kmap_atomic(page);
1079
1080 if (needs_clflush)
1081 drm_clflush_virt_range(vaddr + offset, length);
1082 ret = __copy_to_user_inatomic(user_data, vaddr + offset, length);
1083 kunmap_atomic(vaddr);
1084 }
1085 if (ret == 0)
1086 return 0;
1087
1088 return shmem_pread_slow(page, offset, length, user_data,
1089 page_do_bit17_swizzling, needs_clflush);
1090}
1091
1092static int
1093i915_gem_shmem_pread(struct drm_i915_gem_object *obj,
1094 struct drm_i915_gem_pread *args)
1095{
1096 char __user *user_data;
1097 u64 remain;
1098 unsigned int obj_do_bit17_swizzling;
1099 unsigned int needs_clflush;
1100 unsigned int idx, offset;
1101 int ret;
1102
1103 obj_do_bit17_swizzling = 0;
1104 if (i915_gem_object_needs_bit17_swizzle(obj))
1105 obj_do_bit17_swizzling = BIT(17);
1106
1107 ret = mutex_lock_interruptible(&obj->base.dev->struct_mutex);
1108 if (ret)
1109 return ret;
1110
1111 ret = i915_gem_obj_prepare_shmem_read(obj, &needs_clflush);
1112 mutex_unlock(&obj->base.dev->struct_mutex);
1113 if (ret)
1114 return ret;
1115
1116 remain = args->size;
1117 user_data = u64_to_user_ptr(args->data_ptr);
1118 offset = offset_in_page(args->offset);
1119 for (idx = args->offset >> PAGE_SHIFT; remain; idx++) {
1120 struct page *page = i915_gem_object_get_page(obj, idx);
1121 int length;
1122
1123 length = remain;
1124 if (offset + length > PAGE_SIZE)
1125 length = PAGE_SIZE - offset;
1126
1127 ret = shmem_pread(page, offset, length, user_data,
1128 page_to_phys(page) & obj_do_bit17_swizzling,
1129 needs_clflush);
1130 if (ret)
1131 break;
1132
1133 remain -= length;
1134 user_data += length;
1135 offset = 0;
1136 }
1137
1138 i915_gem_obj_finish_shmem_access(obj);
1139 return ret;
1140}
1141
1142static inline bool
1143gtt_user_read(struct io_mapping *mapping,
1144 loff_t base, int offset,
1145 char __user *user_data, int length)
1146{
Ville Syrjäläafe722b2017-09-01 20:12:52 +03001147 void __iomem *vaddr;
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001148 unsigned long unwritten;
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301149
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301150 /* We can use the cpu mem copy function because this is X86. */
Ville Syrjäläafe722b2017-09-01 20:12:52 +03001151 vaddr = io_mapping_map_atomic_wc(mapping, base);
1152 unwritten = __copy_to_user_inatomic(user_data,
1153 (void __force *)vaddr + offset,
1154 length);
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001155 io_mapping_unmap_atomic(vaddr);
1156 if (unwritten) {
Ville Syrjäläafe722b2017-09-01 20:12:52 +03001157 vaddr = io_mapping_map_wc(mapping, base, PAGE_SIZE);
1158 unwritten = copy_to_user(user_data,
1159 (void __force *)vaddr + offset,
1160 length);
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001161 io_mapping_unmap(vaddr);
1162 }
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301163 return unwritten;
1164}
1165
1166static int
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001167i915_gem_gtt_pread(struct drm_i915_gem_object *obj,
1168 const struct drm_i915_gem_pread *args)
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301169{
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001170 struct drm_i915_private *i915 = to_i915(obj->base.dev);
1171 struct i915_ggtt *ggtt = &i915->ggtt;
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301172 struct drm_mm_node node;
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001173 struct i915_vma *vma;
1174 void __user *user_data;
1175 u64 remain, offset;
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301176 int ret;
1177
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001178 ret = mutex_lock_interruptible(&i915->drm.struct_mutex);
1179 if (ret)
1180 return ret;
1181
1182 intel_runtime_pm_get(i915);
1183 vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0,
Chris Wilsona3259ca2017-10-09 09:44:00 +01001184 PIN_MAPPABLE |
1185 PIN_NONFAULT |
1186 PIN_NONBLOCK);
Chris Wilson18034582016-08-18 17:16:45 +01001187 if (!IS_ERR(vma)) {
1188 node.start = i915_ggtt_offset(vma);
1189 node.allocated = false;
Chris Wilson49ef5292016-08-18 17:17:00 +01001190 ret = i915_vma_put_fence(vma);
Chris Wilson18034582016-08-18 17:16:45 +01001191 if (ret) {
1192 i915_vma_unpin(vma);
1193 vma = ERR_PTR(ret);
1194 }
1195 }
Chris Wilson058d88c2016-08-15 10:49:06 +01001196 if (IS_ERR(vma)) {
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001197 ret = insert_mappable_node(ggtt, &node, PAGE_SIZE);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301198 if (ret)
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001199 goto out_unlock;
1200 GEM_BUG_ON(!node.allocated);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301201 }
1202
1203 ret = i915_gem_object_set_to_gtt_domain(obj, false);
1204 if (ret)
1205 goto out_unpin;
1206
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001207 mutex_unlock(&i915->drm.struct_mutex);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301208
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001209 user_data = u64_to_user_ptr(args->data_ptr);
1210 remain = args->size;
1211 offset = args->offset;
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301212
1213 while (remain > 0) {
1214 /* Operation in this page
1215 *
1216 * page_base = page offset within aperture
1217 * page_offset = offset within page
1218 * page_length = bytes to copy for this page
1219 */
1220 u32 page_base = node.start;
1221 unsigned page_offset = offset_in_page(offset);
1222 unsigned page_length = PAGE_SIZE - page_offset;
1223 page_length = remain < page_length ? remain : page_length;
1224 if (node.allocated) {
1225 wmb();
1226 ggtt->base.insert_page(&ggtt->base,
1227 i915_gem_object_get_dma_address(obj, offset >> PAGE_SHIFT),
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001228 node.start, I915_CACHE_NONE, 0);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301229 wmb();
1230 } else {
1231 page_base += offset & PAGE_MASK;
1232 }
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001233
Matthew Auld73ebd502017-12-11 15:18:20 +00001234 if (gtt_user_read(&ggtt->iomap, page_base, page_offset,
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001235 user_data, page_length)) {
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301236 ret = -EFAULT;
1237 break;
1238 }
1239
1240 remain -= page_length;
1241 user_data += page_length;
1242 offset += page_length;
1243 }
1244
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001245 mutex_lock(&i915->drm.struct_mutex);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301246out_unpin:
1247 if (node.allocated) {
1248 wmb();
1249 ggtt->base.clear_range(&ggtt->base,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02001250 node.start, node.size);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301251 remove_mappable_node(&node);
1252 } else {
Chris Wilson058d88c2016-08-15 10:49:06 +01001253 i915_vma_unpin(vma);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301254 }
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001255out_unlock:
1256 intel_runtime_pm_put(i915);
1257 mutex_unlock(&i915->drm.struct_mutex);
Chris Wilsonf60d7f02012-09-04 21:02:56 +01001258
Eric Anholteb014592009-03-10 11:44:52 -07001259 return ret;
1260}
1261
Eric Anholt673a3942008-07-30 12:06:12 -07001262/**
1263 * Reads data from the object referenced by handle.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01001264 * @dev: drm device pointer
1265 * @data: ioctl data blob
1266 * @file: drm file pointer
Eric Anholt673a3942008-07-30 12:06:12 -07001267 *
1268 * On error, the contents of *data are undefined.
1269 */
1270int
1271i915_gem_pread_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00001272 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001273{
1274 struct drm_i915_gem_pread *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00001275 struct drm_i915_gem_object *obj;
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001276 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07001277
Chris Wilson51311d02010-11-17 09:10:42 +00001278 if (args->size == 0)
1279 return 0;
1280
1281 if (!access_ok(VERIFY_WRITE,
Gustavo Padovan3ed605b2016-04-26 12:32:27 -03001282 u64_to_user_ptr(args->data_ptr),
Chris Wilson51311d02010-11-17 09:10:42 +00001283 args->size))
1284 return -EFAULT;
1285
Chris Wilson03ac0642016-07-20 13:31:51 +01001286 obj = i915_gem_object_lookup(file, args->handle);
Chris Wilson258a5ed2016-08-05 10:14:16 +01001287 if (!obj)
1288 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07001289
Chris Wilson7dcd2492010-09-26 20:21:44 +01001290 /* Bounds check source. */
Matthew Auld966d5bf2016-12-13 20:32:22 +00001291 if (range_overflows_t(u64, args->offset, args->size, obj->base.size)) {
Chris Wilsonce9d4192010-09-26 20:50:05 +01001292 ret = -EINVAL;
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001293 goto out;
Chris Wilsonce9d4192010-09-26 20:50:05 +01001294 }
1295
Chris Wilsondb53a302011-02-03 11:57:46 +00001296 trace_i915_gem_object_pread(obj, args->offset, args->size);
1297
Chris Wilsone95433c2016-10-28 13:58:27 +01001298 ret = i915_gem_object_wait(obj,
1299 I915_WAIT_INTERRUPTIBLE,
1300 MAX_SCHEDULE_TIMEOUT,
1301 to_rps_client(file));
Chris Wilson258a5ed2016-08-05 10:14:16 +01001302 if (ret)
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001303 goto out;
Chris Wilson258a5ed2016-08-05 10:14:16 +01001304
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001305 ret = i915_gem_object_pin_pages(obj);
Chris Wilson258a5ed2016-08-05 10:14:16 +01001306 if (ret)
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001307 goto out;
Chris Wilson258a5ed2016-08-05 10:14:16 +01001308
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001309 ret = i915_gem_shmem_pread(obj, args);
Chris Wilson9c870d02016-10-24 13:42:15 +01001310 if (ret == -EFAULT || ret == -ENODEV)
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001311 ret = i915_gem_gtt_pread(obj, args);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301312
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001313 i915_gem_object_unpin_pages(obj);
1314out:
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001315 i915_gem_object_put(obj);
Eric Anholteb014592009-03-10 11:44:52 -07001316 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07001317}
1318
Keith Packard0839ccb2008-10-30 19:38:48 -07001319/* This is the fast write path which cannot handle
1320 * page faults in the source data
Linus Torvalds9b7530cc2008-10-20 14:16:43 -07001321 */
Linus Torvalds9b7530cc2008-10-20 14:16:43 -07001322
Chris Wilsonfe115622016-10-28 13:58:40 +01001323static inline bool
1324ggtt_write(struct io_mapping *mapping,
1325 loff_t base, int offset,
1326 char __user *user_data, int length)
Keith Packard0839ccb2008-10-30 19:38:48 -07001327{
Ville Syrjäläafe722b2017-09-01 20:12:52 +03001328 void __iomem *vaddr;
Keith Packard0839ccb2008-10-30 19:38:48 -07001329 unsigned long unwritten;
1330
Ben Widawsky4f0c7cf2012-04-16 14:07:47 -07001331 /* We can use the cpu mem copy function because this is X86. */
Ville Syrjäläafe722b2017-09-01 20:12:52 +03001332 vaddr = io_mapping_map_atomic_wc(mapping, base);
1333 unwritten = __copy_from_user_inatomic_nocache((void __force *)vaddr + offset,
Keith Packard0839ccb2008-10-30 19:38:48 -07001334 user_data, length);
Chris Wilsonfe115622016-10-28 13:58:40 +01001335 io_mapping_unmap_atomic(vaddr);
1336 if (unwritten) {
Ville Syrjäläafe722b2017-09-01 20:12:52 +03001337 vaddr = io_mapping_map_wc(mapping, base, PAGE_SIZE);
1338 unwritten = copy_from_user((void __force *)vaddr + offset,
1339 user_data, length);
Chris Wilsonfe115622016-10-28 13:58:40 +01001340 io_mapping_unmap(vaddr);
1341 }
Keith Packard0839ccb2008-10-30 19:38:48 -07001342
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001343 return unwritten;
1344}
1345
Eric Anholt3de09aa2009-03-09 09:42:23 -07001346/**
1347 * This is the fast pwrite path, where we copy the data directly from the
1348 * user into the GTT, uncached.
Chris Wilsonfe115622016-10-28 13:58:40 +01001349 * @obj: i915 GEM object
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01001350 * @args: pwrite arguments structure
Eric Anholt3de09aa2009-03-09 09:42:23 -07001351 */
Eric Anholt673a3942008-07-30 12:06:12 -07001352static int
Chris Wilsonfe115622016-10-28 13:58:40 +01001353i915_gem_gtt_pwrite_fast(struct drm_i915_gem_object *obj,
1354 const struct drm_i915_gem_pwrite *args)
Eric Anholt673a3942008-07-30 12:06:12 -07001355{
Chris Wilsonfe115622016-10-28 13:58:40 +01001356 struct drm_i915_private *i915 = to_i915(obj->base.dev);
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301357 struct i915_ggtt *ggtt = &i915->ggtt;
1358 struct drm_mm_node node;
Chris Wilsonfe115622016-10-28 13:58:40 +01001359 struct i915_vma *vma;
1360 u64 remain, offset;
1361 void __user *user_data;
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301362 int ret;
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301363
Chris Wilsonfe115622016-10-28 13:58:40 +01001364 ret = mutex_lock_interruptible(&i915->drm.struct_mutex);
1365 if (ret)
1366 return ret;
Daniel Vetter935aaa62012-03-25 19:47:35 +02001367
Chris Wilson8bd818152017-10-19 07:37:33 +01001368 if (i915_gem_object_has_struct_page(obj)) {
1369 /*
1370 * Avoid waking the device up if we can fallback, as
1371 * waking/resuming is very slow (worst-case 10-100 ms
1372 * depending on PCI sleeps and our own resume time).
1373 * This easily dwarfs any performance advantage from
1374 * using the cache bypass of indirect GGTT access.
1375 */
1376 if (!intel_runtime_pm_get_if_in_use(i915)) {
1377 ret = -EFAULT;
1378 goto out_unlock;
1379 }
1380 } else {
1381 /* No backing pages, no fallback, we must force GGTT access */
1382 intel_runtime_pm_get(i915);
1383 }
1384
Chris Wilson058d88c2016-08-15 10:49:06 +01001385 vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0,
Chris Wilsona3259ca2017-10-09 09:44:00 +01001386 PIN_MAPPABLE |
1387 PIN_NONFAULT |
1388 PIN_NONBLOCK);
Chris Wilson18034582016-08-18 17:16:45 +01001389 if (!IS_ERR(vma)) {
1390 node.start = i915_ggtt_offset(vma);
1391 node.allocated = false;
Chris Wilson49ef5292016-08-18 17:17:00 +01001392 ret = i915_vma_put_fence(vma);
Chris Wilson18034582016-08-18 17:16:45 +01001393 if (ret) {
1394 i915_vma_unpin(vma);
1395 vma = ERR_PTR(ret);
1396 }
1397 }
Chris Wilson058d88c2016-08-15 10:49:06 +01001398 if (IS_ERR(vma)) {
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001399 ret = insert_mappable_node(ggtt, &node, PAGE_SIZE);
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301400 if (ret)
Chris Wilson8bd818152017-10-19 07:37:33 +01001401 goto out_rpm;
Chris Wilsonfe115622016-10-28 13:58:40 +01001402 GEM_BUG_ON(!node.allocated);
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301403 }
Daniel Vetter935aaa62012-03-25 19:47:35 +02001404
1405 ret = i915_gem_object_set_to_gtt_domain(obj, true);
1406 if (ret)
1407 goto out_unpin;
1408
Chris Wilsonfe115622016-10-28 13:58:40 +01001409 mutex_unlock(&i915->drm.struct_mutex);
1410
Chris Wilsonb19482d2016-08-18 17:16:43 +01001411 intel_fb_obj_invalidate(obj, ORIGIN_CPU);
Paulo Zanoni063e4e62015-02-13 17:23:45 -02001412
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301413 user_data = u64_to_user_ptr(args->data_ptr);
1414 offset = args->offset;
1415 remain = args->size;
1416 while (remain) {
Eric Anholt673a3942008-07-30 12:06:12 -07001417 /* Operation in this page
1418 *
Keith Packard0839ccb2008-10-30 19:38:48 -07001419 * page_base = page offset within aperture
1420 * page_offset = offset within page
1421 * page_length = bytes to copy for this page
Eric Anholt673a3942008-07-30 12:06:12 -07001422 */
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301423 u32 page_base = node.start;
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001424 unsigned int page_offset = offset_in_page(offset);
1425 unsigned int page_length = PAGE_SIZE - page_offset;
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301426 page_length = remain < page_length ? remain : page_length;
1427 if (node.allocated) {
1428 wmb(); /* flush the write before we modify the GGTT */
1429 ggtt->base.insert_page(&ggtt->base,
1430 i915_gem_object_get_dma_address(obj, offset >> PAGE_SHIFT),
1431 node.start, I915_CACHE_NONE, 0);
1432 wmb(); /* flush modifications to the GGTT (insert_page) */
1433 } else {
1434 page_base += offset & PAGE_MASK;
1435 }
Keith Packard0839ccb2008-10-30 19:38:48 -07001436 /* If we get a fault while copying data, then (presumably) our
Eric Anholt3de09aa2009-03-09 09:42:23 -07001437 * source page isn't available. Return the error and we'll
1438 * retry in the slow path.
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301439 * If the object is non-shmem backed, we retry again with the
1440 * path that handles page fault.
Keith Packard0839ccb2008-10-30 19:38:48 -07001441 */
Matthew Auld73ebd502017-12-11 15:18:20 +00001442 if (ggtt_write(&ggtt->iomap, page_base, page_offset,
Chris Wilsonfe115622016-10-28 13:58:40 +01001443 user_data, page_length)) {
1444 ret = -EFAULT;
1445 break;
Daniel Vetter935aaa62012-03-25 19:47:35 +02001446 }
Eric Anholt673a3942008-07-30 12:06:12 -07001447
Keith Packard0839ccb2008-10-30 19:38:48 -07001448 remain -= page_length;
1449 user_data += page_length;
1450 offset += page_length;
Eric Anholt673a3942008-07-30 12:06:12 -07001451 }
Chris Wilsond59b21e2017-02-22 11:40:49 +00001452 intel_fb_obj_flush(obj, ORIGIN_CPU);
Chris Wilsonfe115622016-10-28 13:58:40 +01001453
1454 mutex_lock(&i915->drm.struct_mutex);
Daniel Vetter935aaa62012-03-25 19:47:35 +02001455out_unpin:
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301456 if (node.allocated) {
1457 wmb();
1458 ggtt->base.clear_range(&ggtt->base,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02001459 node.start, node.size);
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301460 remove_mappable_node(&node);
1461 } else {
Chris Wilson058d88c2016-08-15 10:49:06 +01001462 i915_vma_unpin(vma);
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301463 }
Chris Wilson8bd818152017-10-19 07:37:33 +01001464out_rpm:
Chris Wilson9c870d02016-10-24 13:42:15 +01001465 intel_runtime_pm_put(i915);
Chris Wilson8bd818152017-10-19 07:37:33 +01001466out_unlock:
Chris Wilsonfe115622016-10-28 13:58:40 +01001467 mutex_unlock(&i915->drm.struct_mutex);
Eric Anholt3de09aa2009-03-09 09:42:23 -07001468 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07001469}
1470
Eric Anholt673a3942008-07-30 12:06:12 -07001471static int
Chris Wilsonfe115622016-10-28 13:58:40 +01001472shmem_pwrite_slow(struct page *page, int offset, int length,
Daniel Vetterd174bd62012-03-25 19:47:40 +02001473 char __user *user_data,
1474 bool page_do_bit17_swizzling,
1475 bool needs_clflush_before,
1476 bool needs_clflush_after)
Eric Anholt673a3942008-07-30 12:06:12 -07001477{
Daniel Vetterd174bd62012-03-25 19:47:40 +02001478 char *vaddr;
1479 int ret;
Eric Anholt40123c12009-03-09 13:42:30 -07001480
Daniel Vetterd174bd62012-03-25 19:47:40 +02001481 vaddr = kmap(page);
Daniel Vettere7e58eb2012-03-25 19:47:43 +02001482 if (unlikely(needs_clflush_before || page_do_bit17_swizzling))
Chris Wilsonfe115622016-10-28 13:58:40 +01001483 shmem_clflush_swizzled_range(vaddr + offset, length,
Daniel Vetter23c18c72012-03-25 19:47:42 +02001484 page_do_bit17_swizzling);
Daniel Vetterd174bd62012-03-25 19:47:40 +02001485 if (page_do_bit17_swizzling)
Chris Wilsonfe115622016-10-28 13:58:40 +01001486 ret = __copy_from_user_swizzled(vaddr, offset, user_data,
1487 length);
Daniel Vetterd174bd62012-03-25 19:47:40 +02001488 else
Chris Wilsonfe115622016-10-28 13:58:40 +01001489 ret = __copy_from_user(vaddr + offset, user_data, length);
Daniel Vetterd174bd62012-03-25 19:47:40 +02001490 if (needs_clflush_after)
Chris Wilsonfe115622016-10-28 13:58:40 +01001491 shmem_clflush_swizzled_range(vaddr + offset, length,
Daniel Vetter23c18c72012-03-25 19:47:42 +02001492 page_do_bit17_swizzling);
Daniel Vetterd174bd62012-03-25 19:47:40 +02001493 kunmap(page);
Chris Wilsone5281cc2010-10-28 13:45:36 +01001494
Chris Wilson755d2212012-09-04 21:02:55 +01001495 return ret ? -EFAULT : 0;
Eric Anholt40123c12009-03-09 13:42:30 -07001496}
1497
Chris Wilsonfe115622016-10-28 13:58:40 +01001498/* Per-page copy function for the shmem pwrite fastpath.
1499 * Flushes invalid cachelines before writing to the target if
1500 * needs_clflush_before is set and flushes out any written cachelines after
1501 * writing if needs_clflush is set.
1502 */
Eric Anholt40123c12009-03-09 13:42:30 -07001503static int
Chris Wilsonfe115622016-10-28 13:58:40 +01001504shmem_pwrite(struct page *page, int offset, int len, char __user *user_data,
1505 bool page_do_bit17_swizzling,
1506 bool needs_clflush_before,
1507 bool needs_clflush_after)
Eric Anholt40123c12009-03-09 13:42:30 -07001508{
Chris Wilsonfe115622016-10-28 13:58:40 +01001509 int ret;
Eric Anholt40123c12009-03-09 13:42:30 -07001510
Chris Wilsonfe115622016-10-28 13:58:40 +01001511 ret = -ENODEV;
1512 if (!page_do_bit17_swizzling) {
1513 char *vaddr = kmap_atomic(page);
1514
1515 if (needs_clflush_before)
1516 drm_clflush_virt_range(vaddr + offset, len);
1517 ret = __copy_from_user_inatomic(vaddr + offset, user_data, len);
1518 if (needs_clflush_after)
1519 drm_clflush_virt_range(vaddr + offset, len);
1520
1521 kunmap_atomic(vaddr);
1522 }
1523 if (ret == 0)
1524 return ret;
1525
1526 return shmem_pwrite_slow(page, offset, len, user_data,
1527 page_do_bit17_swizzling,
1528 needs_clflush_before,
1529 needs_clflush_after);
1530}
1531
1532static int
1533i915_gem_shmem_pwrite(struct drm_i915_gem_object *obj,
1534 const struct drm_i915_gem_pwrite *args)
1535{
1536 struct drm_i915_private *i915 = to_i915(obj->base.dev);
1537 void __user *user_data;
1538 u64 remain;
1539 unsigned int obj_do_bit17_swizzling;
1540 unsigned int partial_cacheline_write;
1541 unsigned int needs_clflush;
1542 unsigned int offset, idx;
1543 int ret;
1544
1545 ret = mutex_lock_interruptible(&i915->drm.struct_mutex);
Chris Wilson43394c72016-08-18 17:16:47 +01001546 if (ret)
1547 return ret;
Eric Anholt40123c12009-03-09 13:42:30 -07001548
Chris Wilsonfe115622016-10-28 13:58:40 +01001549 ret = i915_gem_obj_prepare_shmem_write(obj, &needs_clflush);
1550 mutex_unlock(&i915->drm.struct_mutex);
1551 if (ret)
1552 return ret;
1553
1554 obj_do_bit17_swizzling = 0;
1555 if (i915_gem_object_needs_bit17_swizzle(obj))
1556 obj_do_bit17_swizzling = BIT(17);
1557
1558 /* If we don't overwrite a cacheline completely we need to be
1559 * careful to have up-to-date data by first clflushing. Don't
1560 * overcomplicate things and flush the entire patch.
1561 */
1562 partial_cacheline_write = 0;
1563 if (needs_clflush & CLFLUSH_BEFORE)
1564 partial_cacheline_write = boot_cpu_data.x86_clflush_size - 1;
1565
Chris Wilson43394c72016-08-18 17:16:47 +01001566 user_data = u64_to_user_ptr(args->data_ptr);
Chris Wilson43394c72016-08-18 17:16:47 +01001567 remain = args->size;
Chris Wilsonfe115622016-10-28 13:58:40 +01001568 offset = offset_in_page(args->offset);
1569 for (idx = args->offset >> PAGE_SHIFT; remain; idx++) {
1570 struct page *page = i915_gem_object_get_page(obj, idx);
1571 int length;
Eric Anholt40123c12009-03-09 13:42:30 -07001572
Chris Wilsonfe115622016-10-28 13:58:40 +01001573 length = remain;
1574 if (offset + length > PAGE_SIZE)
1575 length = PAGE_SIZE - offset;
Chris Wilsone5281cc2010-10-28 13:45:36 +01001576
Chris Wilsonfe115622016-10-28 13:58:40 +01001577 ret = shmem_pwrite(page, offset, length, user_data,
1578 page_to_phys(page) & obj_do_bit17_swizzling,
1579 (offset | length) & partial_cacheline_write,
1580 needs_clflush & CLFLUSH_AFTER);
1581 if (ret)
Chris Wilson9da3da62012-06-01 15:20:22 +01001582 break;
1583
Chris Wilsonfe115622016-10-28 13:58:40 +01001584 remain -= length;
1585 user_data += length;
1586 offset = 0;
Eric Anholt40123c12009-03-09 13:42:30 -07001587 }
1588
Chris Wilsond59b21e2017-02-22 11:40:49 +00001589 intel_fb_obj_flush(obj, ORIGIN_CPU);
Chris Wilsonfe115622016-10-28 13:58:40 +01001590 i915_gem_obj_finish_shmem_access(obj);
Eric Anholt40123c12009-03-09 13:42:30 -07001591 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07001592}
1593
1594/**
1595 * Writes data to the object referenced by handle.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01001596 * @dev: drm device
1597 * @data: ioctl data blob
1598 * @file: drm file
Eric Anholt673a3942008-07-30 12:06:12 -07001599 *
1600 * On error, the contents of the buffer that were to be modified are undefined.
1601 */
1602int
1603i915_gem_pwrite_ioctl(struct drm_device *dev, void *data,
Chris Wilsonfbd5a262010-10-14 15:03:58 +01001604 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001605{
1606 struct drm_i915_gem_pwrite *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00001607 struct drm_i915_gem_object *obj;
Chris Wilson51311d02010-11-17 09:10:42 +00001608 int ret;
1609
1610 if (args->size == 0)
1611 return 0;
1612
1613 if (!access_ok(VERIFY_READ,
Gustavo Padovan3ed605b2016-04-26 12:32:27 -03001614 u64_to_user_ptr(args->data_ptr),
Chris Wilson51311d02010-11-17 09:10:42 +00001615 args->size))
1616 return -EFAULT;
1617
Chris Wilson03ac0642016-07-20 13:31:51 +01001618 obj = i915_gem_object_lookup(file, args->handle);
Chris Wilson258a5ed2016-08-05 10:14:16 +01001619 if (!obj)
1620 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07001621
Chris Wilson7dcd2492010-09-26 20:21:44 +01001622 /* Bounds check destination. */
Matthew Auld966d5bf2016-12-13 20:32:22 +00001623 if (range_overflows_t(u64, args->offset, args->size, obj->base.size)) {
Chris Wilsonce9d4192010-09-26 20:50:05 +01001624 ret = -EINVAL;
Chris Wilson258a5ed2016-08-05 10:14:16 +01001625 goto err;
Chris Wilsonce9d4192010-09-26 20:50:05 +01001626 }
1627
Chris Wilsondb53a302011-02-03 11:57:46 +00001628 trace_i915_gem_object_pwrite(obj, args->offset, args->size);
1629
Chris Wilson7c55e2c2017-03-07 12:03:38 +00001630 ret = -ENODEV;
1631 if (obj->ops->pwrite)
1632 ret = obj->ops->pwrite(obj, args);
1633 if (ret != -ENODEV)
1634 goto err;
1635
Chris Wilsone95433c2016-10-28 13:58:27 +01001636 ret = i915_gem_object_wait(obj,
1637 I915_WAIT_INTERRUPTIBLE |
1638 I915_WAIT_ALL,
1639 MAX_SCHEDULE_TIMEOUT,
1640 to_rps_client(file));
Chris Wilson258a5ed2016-08-05 10:14:16 +01001641 if (ret)
1642 goto err;
1643
Chris Wilsonfe115622016-10-28 13:58:40 +01001644 ret = i915_gem_object_pin_pages(obj);
Chris Wilson258a5ed2016-08-05 10:14:16 +01001645 if (ret)
Chris Wilsonfe115622016-10-28 13:58:40 +01001646 goto err;
Chris Wilson258a5ed2016-08-05 10:14:16 +01001647
Daniel Vetter935aaa62012-03-25 19:47:35 +02001648 ret = -EFAULT;
Eric Anholt673a3942008-07-30 12:06:12 -07001649 /* We can only do the GTT pwrite on untiled buffers, as otherwise
1650 * it would end up going through the fenced access, and we'll get
1651 * different detiling behavior between reading and writing.
1652 * pread/pwrite currently are reading and writing from the CPU
1653 * perspective, requiring manual detiling by the client.
1654 */
Chris Wilson6eae0052016-06-20 15:05:52 +01001655 if (!i915_gem_object_has_struct_page(obj) ||
Chris Wilson9c870d02016-10-24 13:42:15 +01001656 cpu_write_needs_clflush(obj))
Daniel Vetter935aaa62012-03-25 19:47:35 +02001657 /* Note that the gtt paths might fail with non-page-backed user
1658 * pointers (e.g. gtt mappings when moving data between
Chris Wilson9c870d02016-10-24 13:42:15 +01001659 * textures). Fallback to the shmem path in that case.
1660 */
Chris Wilsonfe115622016-10-28 13:58:40 +01001661 ret = i915_gem_gtt_pwrite_fast(obj, args);
Eric Anholt673a3942008-07-30 12:06:12 -07001662
Chris Wilsond1054ee2016-07-16 18:42:36 +01001663 if (ret == -EFAULT || ret == -ENOSPC) {
Chris Wilson6a2c4232014-11-04 04:51:40 -08001664 if (obj->phys_handle)
1665 ret = i915_gem_phys_pwrite(obj, args, file);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301666 else
Chris Wilsonfe115622016-10-28 13:58:40 +01001667 ret = i915_gem_shmem_pwrite(obj, args);
Chris Wilson6a2c4232014-11-04 04:51:40 -08001668 }
Daniel Vetter5c0480f2011-12-14 13:57:30 +01001669
Chris Wilsonfe115622016-10-28 13:58:40 +01001670 i915_gem_object_unpin_pages(obj);
Chris Wilson258a5ed2016-08-05 10:14:16 +01001671err:
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001672 i915_gem_object_put(obj);
Chris Wilson258a5ed2016-08-05 10:14:16 +01001673 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07001674}
1675
Chris Wilson40e62d52016-10-28 13:58:41 +01001676static void i915_gem_object_bump_inactive_ggtt(struct drm_i915_gem_object *obj)
1677{
1678 struct drm_i915_private *i915;
1679 struct list_head *list;
1680 struct i915_vma *vma;
1681
Chris Wilsonf2123812017-10-16 12:40:37 +01001682 GEM_BUG_ON(!i915_gem_object_has_pinned_pages(obj));
1683
Chris Wilsone2189dd2017-12-07 21:14:07 +00001684 for_each_ggtt_vma(vma, obj) {
Chris Wilson40e62d52016-10-28 13:58:41 +01001685 if (i915_vma_is_active(vma))
1686 continue;
1687
1688 if (!drm_mm_node_allocated(&vma->node))
1689 continue;
1690
1691 list_move_tail(&vma->vm_link, &vma->vm->inactive_list);
1692 }
1693
1694 i915 = to_i915(obj->base.dev);
Chris Wilsonf2123812017-10-16 12:40:37 +01001695 spin_lock(&i915->mm.obj_lock);
Chris Wilson40e62d52016-10-28 13:58:41 +01001696 list = obj->bind_count ? &i915->mm.bound_list : &i915->mm.unbound_list;
Chris Wilsonf2123812017-10-16 12:40:37 +01001697 list_move_tail(&obj->mm.link, list);
1698 spin_unlock(&i915->mm.obj_lock);
Chris Wilson40e62d52016-10-28 13:58:41 +01001699}
1700
Eric Anholt673a3942008-07-30 12:06:12 -07001701/**
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001702 * Called when user space prepares to use an object with the CPU, either
1703 * through the mmap ioctl's mapping or a GTT mapping.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01001704 * @dev: drm device
1705 * @data: ioctl data blob
1706 * @file: drm file
Eric Anholt673a3942008-07-30 12:06:12 -07001707 */
1708int
1709i915_gem_set_domain_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00001710 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001711{
1712 struct drm_i915_gem_set_domain *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00001713 struct drm_i915_gem_object *obj;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001714 uint32_t read_domains = args->read_domains;
1715 uint32_t write_domain = args->write_domain;
Chris Wilson40e62d52016-10-28 13:58:41 +01001716 int err;
Eric Anholt673a3942008-07-30 12:06:12 -07001717
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001718 /* Only handle setting domains to types used by the CPU. */
Chris Wilsonb8f90962016-08-05 10:14:07 +01001719 if ((write_domain | read_domains) & I915_GEM_GPU_DOMAINS)
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001720 return -EINVAL;
1721
1722 /* Having something in the write domain implies it's in the read
1723 * domain, and only that read domain. Enforce that in the request.
1724 */
1725 if (write_domain != 0 && read_domains != write_domain)
1726 return -EINVAL;
1727
Chris Wilson03ac0642016-07-20 13:31:51 +01001728 obj = i915_gem_object_lookup(file, args->handle);
Chris Wilsonb8f90962016-08-05 10:14:07 +01001729 if (!obj)
1730 return -ENOENT;
Jesse Barnes652c3932009-08-17 13:31:43 -07001731
Chris Wilson3236f572012-08-24 09:35:09 +01001732 /* Try to flush the object off the GPU without holding the lock.
1733 * We will repeat the flush holding the lock in the normal manner
1734 * to catch cases where we are gazumped.
1735 */
Chris Wilson40e62d52016-10-28 13:58:41 +01001736 err = i915_gem_object_wait(obj,
Chris Wilsone95433c2016-10-28 13:58:27 +01001737 I915_WAIT_INTERRUPTIBLE |
1738 (write_domain ? I915_WAIT_ALL : 0),
1739 MAX_SCHEDULE_TIMEOUT,
1740 to_rps_client(file));
Chris Wilson40e62d52016-10-28 13:58:41 +01001741 if (err)
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001742 goto out;
Chris Wilsonb8f90962016-08-05 10:14:07 +01001743
Tina Zhanga03f3952017-11-14 10:25:13 +00001744 /*
1745 * Proxy objects do not control access to the backing storage, ergo
1746 * they cannot be used as a means to manipulate the cache domain
1747 * tracking for that backing storage. The proxy object is always
1748 * considered to be outside of any cache domain.
1749 */
1750 if (i915_gem_object_is_proxy(obj)) {
1751 err = -ENXIO;
1752 goto out;
1753 }
1754
1755 /*
1756 * Flush and acquire obj->pages so that we are coherent through
Chris Wilson40e62d52016-10-28 13:58:41 +01001757 * direct access in memory with previous cached writes through
1758 * shmemfs and that our cache domain tracking remains valid.
1759 * For example, if the obj->filp was moved to swap without us
1760 * being notified and releasing the pages, we would mistakenly
1761 * continue to assume that the obj remained out of the CPU cached
1762 * domain.
1763 */
1764 err = i915_gem_object_pin_pages(obj);
1765 if (err)
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001766 goto out;
Chris Wilson40e62d52016-10-28 13:58:41 +01001767
1768 err = i915_mutex_lock_interruptible(dev);
1769 if (err)
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001770 goto out_unpin;
Chris Wilson3236f572012-08-24 09:35:09 +01001771
Chris Wilsone22d8e32017-04-12 12:01:11 +01001772 if (read_domains & I915_GEM_DOMAIN_WC)
1773 err = i915_gem_object_set_to_wc_domain(obj, write_domain);
1774 else if (read_domains & I915_GEM_DOMAIN_GTT)
1775 err = i915_gem_object_set_to_gtt_domain(obj, write_domain);
Chris Wilson43566de2015-01-02 16:29:29 +05301776 else
Chris Wilsone22d8e32017-04-12 12:01:11 +01001777 err = i915_gem_object_set_to_cpu_domain(obj, write_domain);
Chris Wilson40e62d52016-10-28 13:58:41 +01001778
1779 /* And bump the LRU for this access */
1780 i915_gem_object_bump_inactive_ggtt(obj);
1781
1782 mutex_unlock(&dev->struct_mutex);
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001783
Daniel Vetter031b6982015-06-26 19:35:16 +02001784 if (write_domain != 0)
Chris Wilsonef749212017-04-12 12:01:10 +01001785 intel_fb_obj_invalidate(obj,
1786 fb_write_origin(obj, write_domain));
Daniel Vetter031b6982015-06-26 19:35:16 +02001787
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001788out_unpin:
Chris Wilson40e62d52016-10-28 13:58:41 +01001789 i915_gem_object_unpin_pages(obj);
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001790out:
1791 i915_gem_object_put(obj);
Chris Wilson40e62d52016-10-28 13:58:41 +01001792 return err;
Eric Anholt673a3942008-07-30 12:06:12 -07001793}
1794
1795/**
1796 * Called when user space has done writes to this buffer
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01001797 * @dev: drm device
1798 * @data: ioctl data blob
1799 * @file: drm file
Eric Anholt673a3942008-07-30 12:06:12 -07001800 */
1801int
1802i915_gem_sw_finish_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00001803 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001804{
1805 struct drm_i915_gem_sw_finish *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00001806 struct drm_i915_gem_object *obj;
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001807
Chris Wilson03ac0642016-07-20 13:31:51 +01001808 obj = i915_gem_object_lookup(file, args->handle);
Chris Wilsonc21724c2016-08-05 10:14:19 +01001809 if (!obj)
1810 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07001811
Tina Zhanga03f3952017-11-14 10:25:13 +00001812 /*
1813 * Proxy objects are barred from CPU access, so there is no
1814 * need to ban sw_finish as it is a nop.
1815 */
1816
Eric Anholt673a3942008-07-30 12:06:12 -07001817 /* Pinned buffers may be scanout, so flush the cache */
Chris Wilson5a97bcc2017-02-22 11:40:46 +00001818 i915_gem_object_flush_if_display(obj);
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001819 i915_gem_object_put(obj);
Chris Wilson5a97bcc2017-02-22 11:40:46 +00001820
1821 return 0;
Eric Anholt673a3942008-07-30 12:06:12 -07001822}
1823
1824/**
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01001825 * i915_gem_mmap_ioctl - Maps the contents of an object, returning the address
1826 * it is mapped to.
1827 * @dev: drm device
1828 * @data: ioctl data blob
1829 * @file: drm file
Eric Anholt673a3942008-07-30 12:06:12 -07001830 *
1831 * While the mapping holds a reference on the contents of the object, it doesn't
1832 * imply a ref on the object itself.
Daniel Vetter34367382014-10-16 12:28:18 +02001833 *
1834 * IMPORTANT:
1835 *
1836 * DRM driver writers who look a this function as an example for how to do GEM
1837 * mmap support, please don't implement mmap support like here. The modern way
1838 * to implement DRM mmap support is with an mmap offset ioctl (like
1839 * i915_gem_mmap_gtt) and then using the mmap syscall on the DRM fd directly.
1840 * That way debug tooling like valgrind will understand what's going on, hiding
1841 * the mmap call in a driver private ioctl will break that. The i915 driver only
1842 * does cpu mmaps this way because we didn't know better.
Eric Anholt673a3942008-07-30 12:06:12 -07001843 */
1844int
1845i915_gem_mmap_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00001846 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001847{
1848 struct drm_i915_gem_mmap *args = data;
Chris Wilson03ac0642016-07-20 13:31:51 +01001849 struct drm_i915_gem_object *obj;
Eric Anholt673a3942008-07-30 12:06:12 -07001850 unsigned long addr;
1851
Akash Goel1816f922015-01-02 16:29:30 +05301852 if (args->flags & ~(I915_MMAP_WC))
1853 return -EINVAL;
1854
Borislav Petkov568a58e2016-03-29 17:42:01 +02001855 if (args->flags & I915_MMAP_WC && !boot_cpu_has(X86_FEATURE_PAT))
Akash Goel1816f922015-01-02 16:29:30 +05301856 return -ENODEV;
1857
Chris Wilson03ac0642016-07-20 13:31:51 +01001858 obj = i915_gem_object_lookup(file, args->handle);
1859 if (!obj)
Chris Wilsonbf79cb92010-08-04 14:19:46 +01001860 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07001861
Daniel Vetter1286ff72012-05-10 15:25:09 +02001862 /* prime objects have no backing filp to GEM mmap
1863 * pages from.
1864 */
Chris Wilson03ac0642016-07-20 13:31:51 +01001865 if (!obj->base.filp) {
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001866 i915_gem_object_put(obj);
Tina Zhang274b2462017-11-14 10:25:12 +00001867 return -ENXIO;
Daniel Vetter1286ff72012-05-10 15:25:09 +02001868 }
1869
Chris Wilson03ac0642016-07-20 13:31:51 +01001870 addr = vm_mmap(obj->base.filp, 0, args->size,
Eric Anholt673a3942008-07-30 12:06:12 -07001871 PROT_READ | PROT_WRITE, MAP_SHARED,
1872 args->offset);
Akash Goel1816f922015-01-02 16:29:30 +05301873 if (args->flags & I915_MMAP_WC) {
1874 struct mm_struct *mm = current->mm;
1875 struct vm_area_struct *vma;
1876
Michal Hocko80a89a52016-05-23 16:26:11 -07001877 if (down_write_killable(&mm->mmap_sem)) {
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001878 i915_gem_object_put(obj);
Michal Hocko80a89a52016-05-23 16:26:11 -07001879 return -EINTR;
1880 }
Akash Goel1816f922015-01-02 16:29:30 +05301881 vma = find_vma(mm, addr);
1882 if (vma)
1883 vma->vm_page_prot =
1884 pgprot_writecombine(vm_get_page_prot(vma->vm_flags));
1885 else
1886 addr = -ENOMEM;
1887 up_write(&mm->mmap_sem);
Chris Wilsonaeecc962016-06-17 14:46:39 -03001888
1889 /* This may race, but that's ok, it only gets set */
Chris Wilson50349242016-08-18 17:17:04 +01001890 WRITE_ONCE(obj->frontbuffer_ggtt_origin, ORIGIN_CPU);
Akash Goel1816f922015-01-02 16:29:30 +05301891 }
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001892 i915_gem_object_put(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001893 if (IS_ERR((void *)addr))
1894 return addr;
1895
1896 args->addr_ptr = (uint64_t) addr;
1897
1898 return 0;
1899}
1900
Chris Wilson03af84f2016-08-18 17:17:01 +01001901static unsigned int tile_row_pages(struct drm_i915_gem_object *obj)
1902{
Chris Wilson6649a0b2017-01-09 16:16:08 +00001903 return i915_gem_object_get_tile_row_size(obj) >> PAGE_SHIFT;
Chris Wilson03af84f2016-08-18 17:17:01 +01001904}
1905
Jesse Barnesde151cf2008-11-12 10:03:55 -08001906/**
Chris Wilson4cc69072016-08-25 19:05:19 +01001907 * i915_gem_mmap_gtt_version - report the current feature set for GTT mmaps
1908 *
1909 * A history of the GTT mmap interface:
1910 *
1911 * 0 - Everything had to fit into the GTT. Both parties of a memcpy had to
1912 * aligned and suitable for fencing, and still fit into the available
1913 * mappable space left by the pinned display objects. A classic problem
1914 * we called the page-fault-of-doom where we would ping-pong between
1915 * two objects that could not fit inside the GTT and so the memcpy
1916 * would page one object in at the expense of the other between every
1917 * single byte.
1918 *
1919 * 1 - Objects can be any size, and have any compatible fencing (X Y, or none
1920 * as set via i915_gem_set_tiling() [DRM_I915_GEM_SET_TILING]). If the
1921 * object is too large for the available space (or simply too large
1922 * for the mappable aperture!), a view is created instead and faulted
1923 * into userspace. (This view is aligned and sized appropriately for
1924 * fenced access.)
1925 *
Chris Wilsone22d8e32017-04-12 12:01:11 +01001926 * 2 - Recognise WC as a separate cache domain so that we can flush the
1927 * delayed writes via GTT before performing direct access via WC.
1928 *
Chris Wilson4cc69072016-08-25 19:05:19 +01001929 * Restrictions:
1930 *
1931 * * snoopable objects cannot be accessed via the GTT. It can cause machine
1932 * hangs on some architectures, corruption on others. An attempt to service
1933 * a GTT page fault from a snoopable object will generate a SIGBUS.
1934 *
1935 * * the object must be able to fit into RAM (physical memory, though no
1936 * limited to the mappable aperture).
1937 *
1938 *
1939 * Caveats:
1940 *
1941 * * a new GTT page fault will synchronize rendering from the GPU and flush
1942 * all data to system memory. Subsequent access will not be synchronized.
1943 *
1944 * * all mappings are revoked on runtime device suspend.
1945 *
1946 * * there are only 8, 16 or 32 fence registers to share between all users
1947 * (older machines require fence register for display and blitter access
1948 * as well). Contention of the fence registers will cause the previous users
1949 * to be unmapped and any new access will generate new page faults.
1950 *
1951 * * running out of memory while servicing a fault may generate a SIGBUS,
1952 * rather than the expected SIGSEGV.
1953 */
1954int i915_gem_mmap_gtt_version(void)
1955{
Chris Wilsone22d8e32017-04-12 12:01:11 +01001956 return 2;
Chris Wilson4cc69072016-08-25 19:05:19 +01001957}
1958
Chris Wilson2d4281b2017-01-10 09:56:32 +00001959static inline struct i915_ggtt_view
1960compute_partial_view(struct drm_i915_gem_object *obj,
Chris Wilson2d4281b2017-01-10 09:56:32 +00001961 pgoff_t page_offset,
1962 unsigned int chunk)
1963{
1964 struct i915_ggtt_view view;
1965
1966 if (i915_gem_object_is_tiled(obj))
1967 chunk = roundup(chunk, tile_row_pages(obj));
1968
Chris Wilson2d4281b2017-01-10 09:56:32 +00001969 view.type = I915_GGTT_VIEW_PARTIAL;
Chris Wilson8bab11932017-01-14 00:28:25 +00001970 view.partial.offset = rounddown(page_offset, chunk);
1971 view.partial.size =
Chris Wilson2d4281b2017-01-10 09:56:32 +00001972 min_t(unsigned int, chunk,
Chris Wilson8bab11932017-01-14 00:28:25 +00001973 (obj->base.size >> PAGE_SHIFT) - view.partial.offset);
Chris Wilson2d4281b2017-01-10 09:56:32 +00001974
1975 /* If the partial covers the entire object, just create a normal VMA. */
1976 if (chunk >= obj->base.size >> PAGE_SHIFT)
1977 view.type = I915_GGTT_VIEW_NORMAL;
1978
1979 return view;
1980}
1981
Chris Wilson4cc69072016-08-25 19:05:19 +01001982/**
Jesse Barnesde151cf2008-11-12 10:03:55 -08001983 * i915_gem_fault - fault a page into the GTT
Geliang Tangd9072a32015-09-15 05:58:44 -07001984 * @vmf: fault info
Jesse Barnesde151cf2008-11-12 10:03:55 -08001985 *
1986 * The fault handler is set up by drm_gem_mmap() when a object is GTT mapped
1987 * from userspace. The fault handler takes care of binding the object to
1988 * the GTT (if needed), allocating and programming a fence register (again,
1989 * only if needed based on whether the old reg is still valid or the object
1990 * is tiled) and inserting a new PTE into the faulting process.
1991 *
1992 * Note that the faulting process may involve evicting existing objects
1993 * from the GTT and/or fence registers to make room. So performance may
1994 * suffer if the GTT working set is large or there are few fence registers
1995 * left.
Chris Wilson4cc69072016-08-25 19:05:19 +01001996 *
1997 * The current feature set supported by i915_gem_fault() and thus GTT mmaps
1998 * is exposed via I915_PARAM_MMAP_GTT_VERSION (see i915_gem_mmap_gtt_version).
Jesse Barnesde151cf2008-11-12 10:03:55 -08001999 */
Dave Jiang11bac802017-02-24 14:56:41 -08002000int i915_gem_fault(struct vm_fault *vmf)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002001{
Chris Wilson03af84f2016-08-18 17:17:01 +01002002#define MIN_CHUNK_PAGES ((1 << 20) >> PAGE_SHIFT) /* 1 MiB */
Dave Jiang11bac802017-02-24 14:56:41 -08002003 struct vm_area_struct *area = vmf->vma;
Chris Wilson058d88c2016-08-15 10:49:06 +01002004 struct drm_i915_gem_object *obj = to_intel_bo(area->vm_private_data);
Chris Wilson05394f32010-11-08 19:18:58 +00002005 struct drm_device *dev = obj->base.dev;
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002006 struct drm_i915_private *dev_priv = to_i915(dev);
2007 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Chris Wilsonb8f90962016-08-05 10:14:07 +01002008 bool write = !!(vmf->flags & FAULT_FLAG_WRITE);
Chris Wilson058d88c2016-08-15 10:49:06 +01002009 struct i915_vma *vma;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002010 pgoff_t page_offset;
Chris Wilson82118872016-08-18 17:17:05 +01002011 unsigned int flags;
Chris Wilsonb8f90962016-08-05 10:14:07 +01002012 int ret;
Paulo Zanonif65c9162013-11-27 18:20:34 -02002013
Jesse Barnesde151cf2008-11-12 10:03:55 -08002014 /* We don't use vmf->pgoff since that has the fake offset */
Jan Kara1a29d852016-12-14 15:07:01 -08002015 page_offset = (vmf->address - area->vm_start) >> PAGE_SHIFT;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002016
Chris Wilsondb53a302011-02-03 11:57:46 +00002017 trace_i915_gem_object_fault(obj, page_offset, true, write);
2018
Chris Wilson6e4930f2014-02-07 18:37:06 -02002019 /* Try to flush the object off the GPU first without holding the lock.
Chris Wilsonb8f90962016-08-05 10:14:07 +01002020 * Upon acquiring the lock, we will perform our sanity checks and then
Chris Wilson6e4930f2014-02-07 18:37:06 -02002021 * repeat the flush holding the lock in the normal manner to catch cases
2022 * where we are gazumped.
2023 */
Chris Wilsone95433c2016-10-28 13:58:27 +01002024 ret = i915_gem_object_wait(obj,
2025 I915_WAIT_INTERRUPTIBLE,
2026 MAX_SCHEDULE_TIMEOUT,
2027 NULL);
Chris Wilson6e4930f2014-02-07 18:37:06 -02002028 if (ret)
Chris Wilsonb8f90962016-08-05 10:14:07 +01002029 goto err;
2030
Chris Wilson40e62d52016-10-28 13:58:41 +01002031 ret = i915_gem_object_pin_pages(obj);
2032 if (ret)
2033 goto err;
2034
Chris Wilsonb8f90962016-08-05 10:14:07 +01002035 intel_runtime_pm_get(dev_priv);
2036
2037 ret = i915_mutex_lock_interruptible(dev);
2038 if (ret)
2039 goto err_rpm;
Chris Wilson6e4930f2014-02-07 18:37:06 -02002040
Chris Wilsoneb119bd2012-12-16 12:43:36 +00002041 /* Access to snoopable pages through the GTT is incoherent. */
Tvrtko Ursulin0031fb92016-11-04 14:42:44 +00002042 if (obj->cache_level != I915_CACHE_NONE && !HAS_LLC(dev_priv)) {
Chris Wilsonddeff6e2014-05-28 16:16:41 +01002043 ret = -EFAULT;
Chris Wilsonb8f90962016-08-05 10:14:07 +01002044 goto err_unlock;
Chris Wilsoneb119bd2012-12-16 12:43:36 +00002045 }
2046
Chris Wilson82118872016-08-18 17:17:05 +01002047 /* If the object is smaller than a couple of partial vma, it is
2048 * not worth only creating a single partial vma - we may as well
2049 * clear enough space for the full object.
2050 */
2051 flags = PIN_MAPPABLE;
2052 if (obj->base.size > 2 * MIN_CHUNK_PAGES << PAGE_SHIFT)
2053 flags |= PIN_NONBLOCK | PIN_NONFAULT;
2054
Chris Wilsona61007a2016-08-18 17:17:02 +01002055 /* Now pin it into the GTT as needed */
Chris Wilson82118872016-08-18 17:17:05 +01002056 vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0, flags);
Chris Wilsona61007a2016-08-18 17:17:02 +01002057 if (IS_ERR(vma)) {
Chris Wilsona61007a2016-08-18 17:17:02 +01002058 /* Use a partial view if it is bigger than available space */
Chris Wilson2d4281b2017-01-10 09:56:32 +00002059 struct i915_ggtt_view view =
Chris Wilson8201c1f2017-01-10 09:56:33 +00002060 compute_partial_view(obj, page_offset, MIN_CHUNK_PAGES);
Chris Wilsonaa136d92016-08-18 17:17:03 +01002061
Chris Wilson50349242016-08-18 17:17:04 +01002062 /* Userspace is now writing through an untracked VMA, abandon
2063 * all hope that the hardware is able to track future writes.
2064 */
2065 obj->frontbuffer_ggtt_origin = ORIGIN_CPU;
2066
Chris Wilsona61007a2016-08-18 17:17:02 +01002067 vma = i915_gem_object_ggtt_pin(obj, &view, 0, 0, PIN_MAPPABLE);
2068 }
Chris Wilson058d88c2016-08-15 10:49:06 +01002069 if (IS_ERR(vma)) {
2070 ret = PTR_ERR(vma);
Chris Wilsonb8f90962016-08-05 10:14:07 +01002071 goto err_unlock;
Chris Wilson058d88c2016-08-15 10:49:06 +01002072 }
Jesse Barnesde151cf2008-11-12 10:03:55 -08002073
Chris Wilsonc9839302012-11-20 10:45:17 +00002074 ret = i915_gem_object_set_to_gtt_domain(obj, write);
2075 if (ret)
Chris Wilsonb8f90962016-08-05 10:14:07 +01002076 goto err_unpin;
Chris Wilsonc9839302012-11-20 10:45:17 +00002077
Chris Wilson3bd40732017-10-09 09:43:56 +01002078 ret = i915_vma_pin_fence(vma);
Chris Wilsonc9839302012-11-20 10:45:17 +00002079 if (ret)
Chris Wilsonb8f90962016-08-05 10:14:07 +01002080 goto err_unpin;
Chris Wilson7d1c4802010-08-07 21:45:03 +01002081
Chris Wilsonb90b91d2014-06-10 12:14:40 +01002082 /* Finally, remap it using the new GTT offset */
Chris Wilsonc58305a2016-08-19 16:54:28 +01002083 ret = remap_io_mapping(area,
Chris Wilson8bab11932017-01-14 00:28:25 +00002084 area->vm_start + (vma->ggtt_view.partial.offset << PAGE_SHIFT),
Matthew Auld73ebd502017-12-11 15:18:20 +00002085 (ggtt->gmadr.start + vma->node.start) >> PAGE_SHIFT,
Chris Wilsonc58305a2016-08-19 16:54:28 +01002086 min_t(u64, vma->size, area->vm_end - area->vm_start),
Matthew Auld73ebd502017-12-11 15:18:20 +00002087 &ggtt->iomap);
Chris Wilsona65adaf2017-10-09 09:43:57 +01002088 if (ret)
2089 goto err_fence;
Chris Wilsona61007a2016-08-18 17:17:02 +01002090
Chris Wilsona65adaf2017-10-09 09:43:57 +01002091 /* Mark as being mmapped into userspace for later revocation */
2092 assert_rpm_wakelock_held(dev_priv);
2093 if (!i915_vma_set_userfault(vma) && !obj->userfault_count++)
2094 list_add(&obj->userfault_link, &dev_priv->mm.userfault_list);
2095 GEM_BUG_ON(!obj->userfault_count);
2096
Chris Wilson7125397b2017-12-06 12:49:14 +00002097 i915_vma_set_ggtt_write(vma);
2098
Chris Wilsona65adaf2017-10-09 09:43:57 +01002099err_fence:
Chris Wilson3bd40732017-10-09 09:43:56 +01002100 i915_vma_unpin_fence(vma);
Chris Wilsonb8f90962016-08-05 10:14:07 +01002101err_unpin:
Chris Wilson058d88c2016-08-15 10:49:06 +01002102 __i915_vma_unpin(vma);
Chris Wilsonb8f90962016-08-05 10:14:07 +01002103err_unlock:
Jesse Barnesde151cf2008-11-12 10:03:55 -08002104 mutex_unlock(&dev->struct_mutex);
Chris Wilsonb8f90962016-08-05 10:14:07 +01002105err_rpm:
2106 intel_runtime_pm_put(dev_priv);
Chris Wilson40e62d52016-10-28 13:58:41 +01002107 i915_gem_object_unpin_pages(obj);
Chris Wilsonb8f90962016-08-05 10:14:07 +01002108err:
Jesse Barnesde151cf2008-11-12 10:03:55 -08002109 switch (ret) {
Chris Wilsond9bc7e92011-02-07 13:09:31 +00002110 case -EIO:
Daniel Vetter2232f032014-09-04 09:36:18 +02002111 /*
2112 * We eat errors when the gpu is terminally wedged to avoid
2113 * userspace unduly crashing (gl has no provisions for mmaps to
2114 * fail). But any other -EIO isn't ours (e.g. swap in failure)
2115 * and so needs to be reported.
2116 */
2117 if (!i915_terminally_wedged(&dev_priv->gpu_error)) {
Paulo Zanonif65c9162013-11-27 18:20:34 -02002118 ret = VM_FAULT_SIGBUS;
2119 break;
2120 }
Chris Wilson045e7692010-11-07 09:18:22 +00002121 case -EAGAIN:
Daniel Vetter571c6082013-09-12 17:57:28 +02002122 /*
2123 * EAGAIN means the gpu is hung and we'll wait for the error
2124 * handler to reset everything when re-faulting in
2125 * i915_mutex_lock_interruptible.
Chris Wilsond9bc7e92011-02-07 13:09:31 +00002126 */
Chris Wilsonc7150892009-09-23 00:43:56 +01002127 case 0:
2128 case -ERESTARTSYS:
Chris Wilsonbed636a2011-02-11 20:31:19 +00002129 case -EINTR:
Dmitry Rogozhkine79e0fe2012-10-03 17:15:26 +03002130 case -EBUSY:
2131 /*
2132 * EBUSY is ok: this just means that another thread
2133 * already did the job.
2134 */
Paulo Zanonif65c9162013-11-27 18:20:34 -02002135 ret = VM_FAULT_NOPAGE;
2136 break;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002137 case -ENOMEM:
Paulo Zanonif65c9162013-11-27 18:20:34 -02002138 ret = VM_FAULT_OOM;
2139 break;
Daniel Vettera7c2e1a2012-10-17 11:17:16 +02002140 case -ENOSPC:
Chris Wilson45d67812014-01-31 11:34:57 +00002141 case -EFAULT:
Paulo Zanonif65c9162013-11-27 18:20:34 -02002142 ret = VM_FAULT_SIGBUS;
2143 break;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002144 default:
Daniel Vettera7c2e1a2012-10-17 11:17:16 +02002145 WARN_ONCE(ret, "unhandled error in i915_gem_fault: %i\n", ret);
Paulo Zanonif65c9162013-11-27 18:20:34 -02002146 ret = VM_FAULT_SIGBUS;
2147 break;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002148 }
Paulo Zanonif65c9162013-11-27 18:20:34 -02002149 return ret;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002150}
2151
Chris Wilsona65adaf2017-10-09 09:43:57 +01002152static void __i915_gem_object_release_mmap(struct drm_i915_gem_object *obj)
2153{
2154 struct i915_vma *vma;
2155
2156 GEM_BUG_ON(!obj->userfault_count);
2157
2158 obj->userfault_count = 0;
2159 list_del(&obj->userfault_link);
2160 drm_vma_node_unmap(&obj->base.vma_node,
2161 obj->base.dev->anon_inode->i_mapping);
2162
Chris Wilsone2189dd2017-12-07 21:14:07 +00002163 for_each_ggtt_vma(vma, obj)
Chris Wilsona65adaf2017-10-09 09:43:57 +01002164 i915_vma_unset_userfault(vma);
Chris Wilsona65adaf2017-10-09 09:43:57 +01002165}
2166
Jesse Barnesde151cf2008-11-12 10:03:55 -08002167/**
Chris Wilson901782b2009-07-10 08:18:50 +01002168 * i915_gem_release_mmap - remove physical page mappings
2169 * @obj: obj in question
2170 *
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02002171 * Preserve the reservation of the mmapping with the DRM core code, but
Chris Wilson901782b2009-07-10 08:18:50 +01002172 * relinquish ownership of the pages back to the system.
2173 *
2174 * It is vital that we remove the page mapping if we have mapped a tiled
2175 * object through the GTT and then lose the fence register due to
2176 * resource pressure. Similarly if the object has been moved out of the
2177 * aperture, than pages mapped into userspace must be revoked. Removing the
2178 * mapping will then trigger a page fault on the next user access, allowing
2179 * fixup by i915_gem_fault().
2180 */
Eric Anholtd05ca302009-07-10 13:02:26 -07002181void
Chris Wilson05394f32010-11-08 19:18:58 +00002182i915_gem_release_mmap(struct drm_i915_gem_object *obj)
Chris Wilson901782b2009-07-10 08:18:50 +01002183{
Chris Wilson275f0392016-10-24 13:42:14 +01002184 struct drm_i915_private *i915 = to_i915(obj->base.dev);
Chris Wilson275f0392016-10-24 13:42:14 +01002185
Chris Wilson349f2cc2016-04-13 17:35:12 +01002186 /* Serialisation between user GTT access and our code depends upon
2187 * revoking the CPU's PTE whilst the mutex is held. The next user
2188 * pagefault then has to wait until we release the mutex.
Chris Wilson9c870d02016-10-24 13:42:15 +01002189 *
2190 * Note that RPM complicates somewhat by adding an additional
2191 * requirement that operations to the GGTT be made holding the RPM
2192 * wakeref.
Chris Wilson349f2cc2016-04-13 17:35:12 +01002193 */
Chris Wilson275f0392016-10-24 13:42:14 +01002194 lockdep_assert_held(&i915->drm.struct_mutex);
Chris Wilson9c870d02016-10-24 13:42:15 +01002195 intel_runtime_pm_get(i915);
Chris Wilson349f2cc2016-04-13 17:35:12 +01002196
Chris Wilsona65adaf2017-10-09 09:43:57 +01002197 if (!obj->userfault_count)
Chris Wilson9c870d02016-10-24 13:42:15 +01002198 goto out;
Chris Wilson901782b2009-07-10 08:18:50 +01002199
Chris Wilsona65adaf2017-10-09 09:43:57 +01002200 __i915_gem_object_release_mmap(obj);
Chris Wilson349f2cc2016-04-13 17:35:12 +01002201
2202 /* Ensure that the CPU's PTE are revoked and there are not outstanding
2203 * memory transactions from userspace before we return. The TLB
2204 * flushing implied above by changing the PTE above *should* be
2205 * sufficient, an extra barrier here just provides us with a bit
2206 * of paranoid documentation about our requirement to serialise
2207 * memory writes before touching registers / GSM.
2208 */
2209 wmb();
Chris Wilson9c870d02016-10-24 13:42:15 +01002210
2211out:
2212 intel_runtime_pm_put(i915);
Chris Wilson901782b2009-07-10 08:18:50 +01002213}
2214
Chris Wilson7c108fd2016-10-24 13:42:18 +01002215void i915_gem_runtime_suspend(struct drm_i915_private *dev_priv)
Chris Wilsoneedd10f2014-06-16 08:57:44 +01002216{
Chris Wilson3594a3e2016-10-24 13:42:16 +01002217 struct drm_i915_gem_object *obj, *on;
Chris Wilson7c108fd2016-10-24 13:42:18 +01002218 int i;
Chris Wilsoneedd10f2014-06-16 08:57:44 +01002219
Chris Wilson3594a3e2016-10-24 13:42:16 +01002220 /*
2221 * Only called during RPM suspend. All users of the userfault_list
2222 * must be holding an RPM wakeref to ensure that this can not
2223 * run concurrently with themselves (and use the struct_mutex for
2224 * protection between themselves).
2225 */
2226
2227 list_for_each_entry_safe(obj, on,
Chris Wilsona65adaf2017-10-09 09:43:57 +01002228 &dev_priv->mm.userfault_list, userfault_link)
2229 __i915_gem_object_release_mmap(obj);
Chris Wilson7c108fd2016-10-24 13:42:18 +01002230
2231 /* The fence will be lost when the device powers down. If any were
2232 * in use by hardware (i.e. they are pinned), we should not be powering
2233 * down! All other fences will be reacquired by the user upon waking.
2234 */
2235 for (i = 0; i < dev_priv->num_fence_regs; i++) {
2236 struct drm_i915_fence_reg *reg = &dev_priv->fence_regs[i];
2237
Chris Wilsone0ec3ec2017-02-03 12:57:17 +00002238 /* Ideally we want to assert that the fence register is not
2239 * live at this point (i.e. that no piece of code will be
2240 * trying to write through fence + GTT, as that both violates
2241 * our tracking of activity and associated locking/barriers,
2242 * but also is illegal given that the hw is powered down).
2243 *
2244 * Previously we used reg->pin_count as a "liveness" indicator.
2245 * That is not sufficient, and we need a more fine-grained
2246 * tool if we want to have a sanity check here.
2247 */
Chris Wilson7c108fd2016-10-24 13:42:18 +01002248
2249 if (!reg->vma)
2250 continue;
2251
Chris Wilsona65adaf2017-10-09 09:43:57 +01002252 GEM_BUG_ON(i915_vma_has_userfault(reg->vma));
Chris Wilson7c108fd2016-10-24 13:42:18 +01002253 reg->dirty = true;
2254 }
Chris Wilsoneedd10f2014-06-16 08:57:44 +01002255}
2256
Chris Wilsond8cb5082012-08-11 15:41:03 +01002257static int i915_gem_object_create_mmap_offset(struct drm_i915_gem_object *obj)
2258{
Chris Wilsonfac5e232016-07-04 11:34:36 +01002259 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
Chris Wilsonf3f61842016-08-05 10:14:14 +01002260 int err;
Chris Wilsond8cb5082012-08-11 15:41:03 +01002261
Chris Wilsonf3f61842016-08-05 10:14:14 +01002262 err = drm_gem_create_mmap_offset(&obj->base);
Chris Wilsonb42a13d2017-01-06 15:22:40 +00002263 if (likely(!err))
Chris Wilsonf3f61842016-08-05 10:14:14 +01002264 return 0;
Daniel Vetterda494d72012-12-20 15:11:16 +01002265
Chris Wilsonb42a13d2017-01-06 15:22:40 +00002266 /* Attempt to reap some mmap space from dead objects */
2267 do {
2268 err = i915_gem_wait_for_idle(dev_priv, I915_WAIT_INTERRUPTIBLE);
2269 if (err)
2270 break;
Chris Wilsond8cb5082012-08-11 15:41:03 +01002271
Chris Wilsonb42a13d2017-01-06 15:22:40 +00002272 i915_gem_drain_freed_objects(dev_priv);
Chris Wilsonf3f61842016-08-05 10:14:14 +01002273 err = drm_gem_create_mmap_offset(&obj->base);
Chris Wilsonb42a13d2017-01-06 15:22:40 +00002274 if (!err)
2275 break;
2276
2277 } while (flush_delayed_work(&dev_priv->gt.retire_work));
Daniel Vetterda494d72012-12-20 15:11:16 +01002278
Chris Wilsonf3f61842016-08-05 10:14:14 +01002279 return err;
Chris Wilsond8cb5082012-08-11 15:41:03 +01002280}
2281
2282static void i915_gem_object_free_mmap_offset(struct drm_i915_gem_object *obj)
2283{
Chris Wilsond8cb5082012-08-11 15:41:03 +01002284 drm_gem_free_mmap_offset(&obj->base);
2285}
2286
Dave Airlieda6b51d2014-12-24 13:11:17 +10002287int
Dave Airlieff72145b2011-02-07 12:16:14 +10002288i915_gem_mmap_gtt(struct drm_file *file,
2289 struct drm_device *dev,
Dave Airlieda6b51d2014-12-24 13:11:17 +10002290 uint32_t handle,
Dave Airlieff72145b2011-02-07 12:16:14 +10002291 uint64_t *offset)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002292{
Chris Wilson05394f32010-11-08 19:18:58 +00002293 struct drm_i915_gem_object *obj;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002294 int ret;
2295
Chris Wilson03ac0642016-07-20 13:31:51 +01002296 obj = i915_gem_object_lookup(file, handle);
Chris Wilsonf3f61842016-08-05 10:14:14 +01002297 if (!obj)
2298 return -ENOENT;
Chris Wilsonab182822009-09-22 18:46:17 +01002299
Chris Wilsond8cb5082012-08-11 15:41:03 +01002300 ret = i915_gem_object_create_mmap_offset(obj);
Chris Wilsonf3f61842016-08-05 10:14:14 +01002301 if (ret == 0)
2302 *offset = drm_vma_node_offset_addr(&obj->base.vma_node);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002303
Chris Wilsonf0cd5182016-10-28 13:58:43 +01002304 i915_gem_object_put(obj);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01002305 return ret;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002306}
2307
Dave Airlieff72145b2011-02-07 12:16:14 +10002308/**
2309 * i915_gem_mmap_gtt_ioctl - prepare an object for GTT mmap'ing
2310 * @dev: DRM device
2311 * @data: GTT mapping ioctl data
2312 * @file: GEM object info
2313 *
2314 * Simply returns the fake offset to userspace so it can mmap it.
2315 * The mmap call will end up in drm_gem_mmap(), which will set things
2316 * up so we can get faults in the handler above.
2317 *
2318 * The fault handler will take care of binding the object into the GTT
2319 * (since it may have been evicted to make room for something), allocating
2320 * a fence register, and mapping the appropriate aperture address into
2321 * userspace.
2322 */
2323int
2324i915_gem_mmap_gtt_ioctl(struct drm_device *dev, void *data,
2325 struct drm_file *file)
2326{
2327 struct drm_i915_gem_mmap_gtt *args = data;
2328
Dave Airlieda6b51d2014-12-24 13:11:17 +10002329 return i915_gem_mmap_gtt(file, dev, args->handle, &args->offset);
Dave Airlieff72145b2011-02-07 12:16:14 +10002330}
2331
Daniel Vetter225067e2012-08-20 10:23:20 +02002332/* Immediately discard the backing storage */
2333static void
2334i915_gem_object_truncate(struct drm_i915_gem_object *obj)
Chris Wilsone5281cc2010-10-28 13:45:36 +01002335{
Chris Wilson4d6294bf2012-08-11 15:41:05 +01002336 i915_gem_object_free_mmap_offset(obj);
Daniel Vetter1286ff72012-05-10 15:25:09 +02002337
Chris Wilson4d6294bf2012-08-11 15:41:05 +01002338 if (obj->base.filp == NULL)
2339 return;
2340
Daniel Vetter225067e2012-08-20 10:23:20 +02002341 /* Our goal here is to return as much of the memory as
2342 * is possible back to the system as we are called from OOM.
2343 * To do this we must instruct the shmfs to drop all of its
2344 * backing pages, *now*.
Chris Wilsone5281cc2010-10-28 13:45:36 +01002345 */
Chris Wilson55372522014-03-25 13:23:06 +00002346 shmem_truncate_range(file_inode(obj->base.filp), 0, (loff_t)-1);
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002347 obj->mm.madv = __I915_MADV_PURGED;
Chris Wilson4e5462e2017-03-07 13:20:31 +00002348 obj->mm.pages = ERR_PTR(-EFAULT);
Chris Wilsone5281cc2010-10-28 13:45:36 +01002349}
Chris Wilsone5281cc2010-10-28 13:45:36 +01002350
Chris Wilson55372522014-03-25 13:23:06 +00002351/* Try to discard unwanted pages */
Chris Wilson03ac84f2016-10-28 13:58:36 +01002352void __i915_gem_object_invalidate(struct drm_i915_gem_object *obj)
Daniel Vetter225067e2012-08-20 10:23:20 +02002353{
Chris Wilson55372522014-03-25 13:23:06 +00002354 struct address_space *mapping;
2355
Chris Wilson1233e2d2016-10-28 13:58:37 +01002356 lockdep_assert_held(&obj->mm.lock);
Chris Wilsonf1fa4f42017-10-13 21:26:13 +01002357 GEM_BUG_ON(i915_gem_object_has_pages(obj));
Chris Wilson1233e2d2016-10-28 13:58:37 +01002358
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002359 switch (obj->mm.madv) {
Chris Wilson55372522014-03-25 13:23:06 +00002360 case I915_MADV_DONTNEED:
2361 i915_gem_object_truncate(obj);
2362 case __I915_MADV_PURGED:
2363 return;
2364 }
2365
2366 if (obj->base.filp == NULL)
2367 return;
2368
Al Viro93c76a32015-12-04 23:45:44 -05002369 mapping = obj->base.filp->f_mapping,
Chris Wilson55372522014-03-25 13:23:06 +00002370 invalidate_mapping_pages(mapping, 0, (loff_t)-1);
Chris Wilsone5281cc2010-10-28 13:45:36 +01002371}
2372
Chris Wilson5cdf5882010-09-27 15:51:07 +01002373static void
Chris Wilson03ac84f2016-10-28 13:58:36 +01002374i915_gem_object_put_pages_gtt(struct drm_i915_gem_object *obj,
2375 struct sg_table *pages)
Eric Anholt673a3942008-07-30 12:06:12 -07002376{
Dave Gordon85d12252016-05-20 11:54:06 +01002377 struct sgt_iter sgt_iter;
2378 struct page *page;
Daniel Vetter1286ff72012-05-10 15:25:09 +02002379
Chris Wilsone5facdf2016-12-23 14:57:57 +00002380 __i915_gem_object_release_shmem(obj, pages, true);
Eric Anholt856fa192009-03-19 14:10:50 -07002381
Chris Wilson03ac84f2016-10-28 13:58:36 +01002382 i915_gem_gtt_finish_pages(obj, pages);
Imre Deake2273302015-07-09 12:59:05 +03002383
Daniel Vetter6dacfd22011-09-12 21:30:02 +02002384 if (i915_gem_object_needs_bit17_swizzle(obj))
Chris Wilson03ac84f2016-10-28 13:58:36 +01002385 i915_gem_object_save_bit_17_swizzle(obj, pages);
Eric Anholt280b7132009-03-12 16:56:27 -07002386
Chris Wilson03ac84f2016-10-28 13:58:36 +01002387 for_each_sgt_page(page, sgt_iter, pages) {
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002388 if (obj->mm.dirty)
Chris Wilson9da3da62012-06-01 15:20:22 +01002389 set_page_dirty(page);
Chris Wilson3ef94da2009-09-14 16:50:29 +01002390
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002391 if (obj->mm.madv == I915_MADV_WILLNEED)
Chris Wilson9da3da62012-06-01 15:20:22 +01002392 mark_page_accessed(page);
Chris Wilson3ef94da2009-09-14 16:50:29 +01002393
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002394 put_page(page);
Chris Wilson3ef94da2009-09-14 16:50:29 +01002395 }
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002396 obj->mm.dirty = false;
Eric Anholt673a3942008-07-30 12:06:12 -07002397
Chris Wilson03ac84f2016-10-28 13:58:36 +01002398 sg_free_table(pages);
2399 kfree(pages);
Chris Wilson37e680a2012-06-07 15:38:42 +01002400}
2401
Chris Wilson96d77632016-10-28 13:58:33 +01002402static void __i915_gem_object_reset_page_iter(struct drm_i915_gem_object *obj)
2403{
2404 struct radix_tree_iter iter;
Ville Syrjäläc23aa712017-09-01 20:12:51 +03002405 void __rcu **slot;
Chris Wilson96d77632016-10-28 13:58:33 +01002406
Chris Wilsonbea6e982017-10-26 14:00:31 +01002407 rcu_read_lock();
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002408 radix_tree_for_each_slot(slot, &obj->mm.get_page.radix, &iter, 0)
2409 radix_tree_delete(&obj->mm.get_page.radix, iter.index);
Chris Wilsonbea6e982017-10-26 14:00:31 +01002410 rcu_read_unlock();
Chris Wilson96d77632016-10-28 13:58:33 +01002411}
2412
Chris Wilson548625e2016-11-01 12:11:34 +00002413void __i915_gem_object_put_pages(struct drm_i915_gem_object *obj,
2414 enum i915_mm_subclass subclass)
Chris Wilson37e680a2012-06-07 15:38:42 +01002415{
Chris Wilsonf2123812017-10-16 12:40:37 +01002416 struct drm_i915_private *i915 = to_i915(obj->base.dev);
Chris Wilson03ac84f2016-10-28 13:58:36 +01002417 struct sg_table *pages;
Chris Wilson37e680a2012-06-07 15:38:42 +01002418
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002419 if (i915_gem_object_has_pinned_pages(obj))
Chris Wilson03ac84f2016-10-28 13:58:36 +01002420 return;
Chris Wilsona5570172012-09-04 21:02:54 +01002421
Chris Wilson15717de2016-08-04 07:52:26 +01002422 GEM_BUG_ON(obj->bind_count);
Chris Wilsonf1fa4f42017-10-13 21:26:13 +01002423 if (!i915_gem_object_has_pages(obj))
Chris Wilson1233e2d2016-10-28 13:58:37 +01002424 return;
2425
2426 /* May be called by shrinker from within get_pages() (on another bo) */
Chris Wilson548625e2016-11-01 12:11:34 +00002427 mutex_lock_nested(&obj->mm.lock, subclass);
Chris Wilson1233e2d2016-10-28 13:58:37 +01002428 if (unlikely(atomic_read(&obj->mm.pages_pin_count)))
2429 goto unlock;
Ben Widawsky3e123022013-07-31 17:00:04 -07002430
Chris Wilsona2165e32012-12-03 11:49:00 +00002431 /* ->put_pages might need to allocate memory for the bit17 swizzle
2432 * array, hence protect them from being reaped by removing them from gtt
2433 * lists early. */
Chris Wilson03ac84f2016-10-28 13:58:36 +01002434 pages = fetch_and_zero(&obj->mm.pages);
2435 GEM_BUG_ON(!pages);
Chris Wilsona2165e32012-12-03 11:49:00 +00002436
Chris Wilsonf2123812017-10-16 12:40:37 +01002437 spin_lock(&i915->mm.obj_lock);
2438 list_del(&obj->mm.link);
2439 spin_unlock(&i915->mm.obj_lock);
2440
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002441 if (obj->mm.mapping) {
Chris Wilson4b30cb22016-08-18 17:16:42 +01002442 void *ptr;
2443
Chris Wilson0ce81782017-05-17 13:09:59 +01002444 ptr = page_mask_bits(obj->mm.mapping);
Chris Wilson4b30cb22016-08-18 17:16:42 +01002445 if (is_vmalloc_addr(ptr))
2446 vunmap(ptr);
Chris Wilsonfb8621d2016-04-08 12:11:14 +01002447 else
Chris Wilson4b30cb22016-08-18 17:16:42 +01002448 kunmap(kmap_to_page(ptr));
2449
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002450 obj->mm.mapping = NULL;
Chris Wilson0a798eb2016-04-08 12:11:11 +01002451 }
2452
Chris Wilson96d77632016-10-28 13:58:33 +01002453 __i915_gem_object_reset_page_iter(obj);
2454
Chris Wilson4e5462e2017-03-07 13:20:31 +00002455 if (!IS_ERR(pages))
2456 obj->ops->put_pages(obj, pages);
2457
Matthew Aulda5c081662017-10-06 23:18:18 +01002458 obj->mm.page_sizes.phys = obj->mm.page_sizes.sg = 0;
2459
Chris Wilson1233e2d2016-10-28 13:58:37 +01002460unlock:
2461 mutex_unlock(&obj->mm.lock);
Chris Wilson6c085a72012-08-20 11:40:46 +02002462}
2463
Chris Wilson935a2f72017-02-13 17:15:13 +00002464static bool i915_sg_trim(struct sg_table *orig_st)
Tvrtko Ursulin0c40ce12016-11-09 15:13:43 +00002465{
2466 struct sg_table new_st;
2467 struct scatterlist *sg, *new_sg;
2468 unsigned int i;
2469
2470 if (orig_st->nents == orig_st->orig_nents)
Chris Wilson935a2f72017-02-13 17:15:13 +00002471 return false;
Tvrtko Ursulin0c40ce12016-11-09 15:13:43 +00002472
Chris Wilson8bfc478f2016-12-23 14:57:58 +00002473 if (sg_alloc_table(&new_st, orig_st->nents, GFP_KERNEL | __GFP_NOWARN))
Chris Wilson935a2f72017-02-13 17:15:13 +00002474 return false;
Tvrtko Ursulin0c40ce12016-11-09 15:13:43 +00002475
2476 new_sg = new_st.sgl;
2477 for_each_sg(orig_st->sgl, sg, orig_st->nents, i) {
2478 sg_set_page(new_sg, sg_page(sg), sg->length, 0);
2479 /* called before being DMA mapped, no need to copy sg->dma_* */
2480 new_sg = sg_next(new_sg);
2481 }
Chris Wilsonc2dc6cc2016-12-19 12:43:46 +00002482 GEM_BUG_ON(new_sg); /* Should walk exactly nents and hit the end */
Tvrtko Ursulin0c40ce12016-11-09 15:13:43 +00002483
2484 sg_free_table(orig_st);
2485
2486 *orig_st = new_st;
Chris Wilson935a2f72017-02-13 17:15:13 +00002487 return true;
Tvrtko Ursulin0c40ce12016-11-09 15:13:43 +00002488}
2489
Matthew Auldb91b09e2017-10-06 23:18:17 +01002490static int i915_gem_object_get_pages_gtt(struct drm_i915_gem_object *obj)
Eric Anholt673a3942008-07-30 12:06:12 -07002491{
Chris Wilsonfac5e232016-07-04 11:34:36 +01002492 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
Chris Wilsond766ef52016-12-19 12:43:45 +00002493 const unsigned long page_count = obj->base.size / PAGE_SIZE;
2494 unsigned long i;
Eric Anholt673a3942008-07-30 12:06:12 -07002495 struct address_space *mapping;
Chris Wilson9da3da62012-06-01 15:20:22 +01002496 struct sg_table *st;
2497 struct scatterlist *sg;
Dave Gordon85d12252016-05-20 11:54:06 +01002498 struct sgt_iter sgt_iter;
Eric Anholt673a3942008-07-30 12:06:12 -07002499 struct page *page;
Imre Deak90797e62013-02-18 19:28:03 +02002500 unsigned long last_pfn = 0; /* suppress gcc warning */
Tvrtko Ursulin56024522017-08-03 10:14:17 +01002501 unsigned int max_segment = i915_sg_segment_size();
Matthew Auld84e89782017-10-09 12:00:24 +01002502 unsigned int sg_page_sizes;
Chris Wilson4846bf02017-06-09 12:03:46 +01002503 gfp_t noreclaim;
Imre Deake2273302015-07-09 12:59:05 +03002504 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07002505
Chris Wilson6c085a72012-08-20 11:40:46 +02002506 /* Assert that the object is not currently in any GPU domain. As it
2507 * wasn't in the GTT, there shouldn't be any way it could have been in
2508 * a GPU cache
2509 */
Christian Königc0a51fd2018-02-16 13:43:38 +01002510 GEM_BUG_ON(obj->read_domains & I915_GEM_GPU_DOMAINS);
2511 GEM_BUG_ON(obj->write_domain & I915_GEM_GPU_DOMAINS);
Chris Wilson6c085a72012-08-20 11:40:46 +02002512
Chris Wilson9da3da62012-06-01 15:20:22 +01002513 st = kmalloc(sizeof(*st), GFP_KERNEL);
2514 if (st == NULL)
Matthew Auldb91b09e2017-10-06 23:18:17 +01002515 return -ENOMEM;
Eric Anholt673a3942008-07-30 12:06:12 -07002516
Chris Wilsond766ef52016-12-19 12:43:45 +00002517rebuild_st:
Chris Wilson9da3da62012-06-01 15:20:22 +01002518 if (sg_alloc_table(st, page_count, GFP_KERNEL)) {
Chris Wilson9da3da62012-06-01 15:20:22 +01002519 kfree(st);
Matthew Auldb91b09e2017-10-06 23:18:17 +01002520 return -ENOMEM;
Chris Wilson9da3da62012-06-01 15:20:22 +01002521 }
2522
2523 /* Get the list of pages out of our struct file. They'll be pinned
2524 * at this point until we release them.
2525 *
2526 * Fail silently without starting the shrinker
2527 */
Al Viro93c76a32015-12-04 23:45:44 -05002528 mapping = obj->base.filp->f_mapping;
Chris Wilson0f6ab552017-06-09 12:03:48 +01002529 noreclaim = mapping_gfp_constraint(mapping, ~__GFP_RECLAIM);
Chris Wilson4846bf02017-06-09 12:03:46 +01002530 noreclaim |= __GFP_NORETRY | __GFP_NOWARN;
2531
Imre Deak90797e62013-02-18 19:28:03 +02002532 sg = st->sgl;
2533 st->nents = 0;
Matthew Auld84e89782017-10-09 12:00:24 +01002534 sg_page_sizes = 0;
Imre Deak90797e62013-02-18 19:28:03 +02002535 for (i = 0; i < page_count; i++) {
Chris Wilson4846bf02017-06-09 12:03:46 +01002536 const unsigned int shrink[] = {
2537 I915_SHRINK_BOUND | I915_SHRINK_UNBOUND | I915_SHRINK_PURGEABLE,
2538 0,
2539 }, *s = shrink;
2540 gfp_t gfp = noreclaim;
2541
2542 do {
Chris Wilson6c085a72012-08-20 11:40:46 +02002543 page = shmem_read_mapping_page_gfp(mapping, i, gfp);
Chris Wilson4846bf02017-06-09 12:03:46 +01002544 if (likely(!IS_ERR(page)))
2545 break;
2546
2547 if (!*s) {
2548 ret = PTR_ERR(page);
2549 goto err_sg;
2550 }
2551
Chris Wilson912d5722017-09-06 16:19:30 -07002552 i915_gem_shrink(dev_priv, 2 * page_count, NULL, *s++);
Chris Wilson4846bf02017-06-09 12:03:46 +01002553 cond_resched();
Chris Wilson24f8e002017-03-22 11:05:21 +00002554
Chris Wilson6c085a72012-08-20 11:40:46 +02002555 /* We've tried hard to allocate the memory by reaping
2556 * our own buffer, now let the real VM do its job and
2557 * go down in flames if truly OOM.
Chris Wilson24f8e002017-03-22 11:05:21 +00002558 *
2559 * However, since graphics tend to be disposable,
2560 * defer the oom here by reporting the ENOMEM back
2561 * to userspace.
Chris Wilson6c085a72012-08-20 11:40:46 +02002562 */
Chris Wilson4846bf02017-06-09 12:03:46 +01002563 if (!*s) {
2564 /* reclaim and warn, but no oom */
2565 gfp = mapping_gfp_mask(mapping);
Chris Wilsoneaf41802017-06-09 12:03:47 +01002566
2567 /* Our bo are always dirty and so we require
2568 * kswapd to reclaim our pages (direct reclaim
2569 * does not effectively begin pageout of our
2570 * buffers on its own). However, direct reclaim
2571 * only waits for kswapd when under allocation
2572 * congestion. So as a result __GFP_RECLAIM is
2573 * unreliable and fails to actually reclaim our
2574 * dirty pages -- unless you try over and over
2575 * again with !__GFP_NORETRY. However, we still
2576 * want to fail this allocation rather than
2577 * trigger the out-of-memory killer and for
Michal Hockodbb32952017-07-12 14:36:55 -07002578 * this we want __GFP_RETRY_MAYFAIL.
Chris Wilsoneaf41802017-06-09 12:03:47 +01002579 */
Michal Hockodbb32952017-07-12 14:36:55 -07002580 gfp |= __GFP_RETRY_MAYFAIL;
Imre Deake2273302015-07-09 12:59:05 +03002581 }
Chris Wilson4846bf02017-06-09 12:03:46 +01002582 } while (1);
2583
Chris Wilson871dfbd2016-10-11 09:20:21 +01002584 if (!i ||
2585 sg->length >= max_segment ||
2586 page_to_pfn(page) != last_pfn + 1) {
Matthew Aulda5c081662017-10-06 23:18:18 +01002587 if (i) {
Matthew Auld84e89782017-10-09 12:00:24 +01002588 sg_page_sizes |= sg->length;
Imre Deak90797e62013-02-18 19:28:03 +02002589 sg = sg_next(sg);
Matthew Aulda5c081662017-10-06 23:18:18 +01002590 }
Imre Deak90797e62013-02-18 19:28:03 +02002591 st->nents++;
2592 sg_set_page(sg, page, PAGE_SIZE, 0);
2593 } else {
2594 sg->length += PAGE_SIZE;
2595 }
2596 last_pfn = page_to_pfn(page);
Daniel Vetter3bbbe702013-10-07 17:15:45 -03002597
2598 /* Check that the i965g/gm workaround works. */
2599 WARN_ON((gfp & __GFP_DMA32) && (last_pfn >= 0x00100000UL));
Eric Anholt673a3942008-07-30 12:06:12 -07002600 }
Matthew Aulda5c081662017-10-06 23:18:18 +01002601 if (sg) { /* loop terminated early; short sg table */
Matthew Auld84e89782017-10-09 12:00:24 +01002602 sg_page_sizes |= sg->length;
Konrad Rzeszutek Wilk426729d2013-06-24 11:47:48 -04002603 sg_mark_end(sg);
Matthew Aulda5c081662017-10-06 23:18:18 +01002604 }
Chris Wilson74ce6b62012-10-19 15:51:06 +01002605
Tvrtko Ursulin0c40ce12016-11-09 15:13:43 +00002606 /* Trim unused sg entries to avoid wasting memory. */
2607 i915_sg_trim(st);
2608
Chris Wilson03ac84f2016-10-28 13:58:36 +01002609 ret = i915_gem_gtt_prepare_pages(obj, st);
Chris Wilsond766ef52016-12-19 12:43:45 +00002610 if (ret) {
2611 /* DMA remapping failed? One possible cause is that
2612 * it could not reserve enough large entries, asking
2613 * for PAGE_SIZE chunks instead may be helpful.
2614 */
2615 if (max_segment > PAGE_SIZE) {
2616 for_each_sgt_page(page, sgt_iter, st)
2617 put_page(page);
2618 sg_free_table(st);
2619
2620 max_segment = PAGE_SIZE;
2621 goto rebuild_st;
2622 } else {
2623 dev_warn(&dev_priv->drm.pdev->dev,
2624 "Failed to DMA remap %lu pages\n",
2625 page_count);
2626 goto err_pages;
2627 }
2628 }
Imre Deake2273302015-07-09 12:59:05 +03002629
Eric Anholt673a3942008-07-30 12:06:12 -07002630 if (i915_gem_object_needs_bit17_swizzle(obj))
Chris Wilson03ac84f2016-10-28 13:58:36 +01002631 i915_gem_object_do_bit_17_swizzle(obj, st);
Eric Anholt673a3942008-07-30 12:06:12 -07002632
Matthew Auld84e89782017-10-09 12:00:24 +01002633 __i915_gem_object_set_pages(obj, st, sg_page_sizes);
Matthew Auldb91b09e2017-10-06 23:18:17 +01002634
2635 return 0;
Eric Anholt673a3942008-07-30 12:06:12 -07002636
Chris Wilsonb17993b2016-11-14 11:29:30 +00002637err_sg:
Imre Deak90797e62013-02-18 19:28:03 +02002638 sg_mark_end(sg);
Chris Wilsonb17993b2016-11-14 11:29:30 +00002639err_pages:
Dave Gordon85d12252016-05-20 11:54:06 +01002640 for_each_sgt_page(page, sgt_iter, st)
2641 put_page(page);
Chris Wilson9da3da62012-06-01 15:20:22 +01002642 sg_free_table(st);
2643 kfree(st);
Chris Wilson0820baf2014-03-25 13:23:03 +00002644
2645 /* shmemfs first checks if there is enough memory to allocate the page
2646 * and reports ENOSPC should there be insufficient, along with the usual
2647 * ENOMEM for a genuine allocation failure.
2648 *
2649 * We use ENOSPC in our driver to mean that we have run out of aperture
2650 * space and so want to translate the error from shmemfs back to our
2651 * usual understanding of ENOMEM.
2652 */
Imre Deake2273302015-07-09 12:59:05 +03002653 if (ret == -ENOSPC)
2654 ret = -ENOMEM;
2655
Matthew Auldb91b09e2017-10-06 23:18:17 +01002656 return ret;
Chris Wilson03ac84f2016-10-28 13:58:36 +01002657}
2658
2659void __i915_gem_object_set_pages(struct drm_i915_gem_object *obj,
Matthew Aulda5c081662017-10-06 23:18:18 +01002660 struct sg_table *pages,
Matthew Auld84e89782017-10-09 12:00:24 +01002661 unsigned int sg_page_sizes)
Chris Wilson03ac84f2016-10-28 13:58:36 +01002662{
Matthew Aulda5c081662017-10-06 23:18:18 +01002663 struct drm_i915_private *i915 = to_i915(obj->base.dev);
2664 unsigned long supported = INTEL_INFO(i915)->page_sizes;
2665 int i;
2666
Chris Wilson1233e2d2016-10-28 13:58:37 +01002667 lockdep_assert_held(&obj->mm.lock);
Chris Wilson03ac84f2016-10-28 13:58:36 +01002668
2669 obj->mm.get_page.sg_pos = pages->sgl;
2670 obj->mm.get_page.sg_idx = 0;
2671
2672 obj->mm.pages = pages;
Chris Wilson2c3a3f42016-11-04 10:30:01 +00002673
2674 if (i915_gem_object_is_tiled(obj) &&
Chris Wilsonf2123812017-10-16 12:40:37 +01002675 i915->quirks & QUIRK_PIN_SWIZZLED_PAGES) {
Chris Wilson2c3a3f42016-11-04 10:30:01 +00002676 GEM_BUG_ON(obj->mm.quirked);
2677 __i915_gem_object_pin_pages(obj);
2678 obj->mm.quirked = true;
2679 }
Matthew Aulda5c081662017-10-06 23:18:18 +01002680
Matthew Auld84e89782017-10-09 12:00:24 +01002681 GEM_BUG_ON(!sg_page_sizes);
2682 obj->mm.page_sizes.phys = sg_page_sizes;
Matthew Aulda5c081662017-10-06 23:18:18 +01002683
2684 /*
Matthew Auld84e89782017-10-09 12:00:24 +01002685 * Calculate the supported page-sizes which fit into the given
2686 * sg_page_sizes. This will give us the page-sizes which we may be able
2687 * to use opportunistically when later inserting into the GTT. For
2688 * example if phys=2G, then in theory we should be able to use 1G, 2M,
2689 * 64K or 4K pages, although in practice this will depend on a number of
2690 * other factors.
Matthew Aulda5c081662017-10-06 23:18:18 +01002691 */
2692 obj->mm.page_sizes.sg = 0;
2693 for_each_set_bit(i, &supported, ilog2(I915_GTT_MAX_PAGE_SIZE) + 1) {
2694 if (obj->mm.page_sizes.phys & ~0u << i)
2695 obj->mm.page_sizes.sg |= BIT(i);
2696 }
Matthew Aulda5c081662017-10-06 23:18:18 +01002697 GEM_BUG_ON(!HAS_PAGE_SIZES(i915, obj->mm.page_sizes.sg));
Chris Wilsonf2123812017-10-16 12:40:37 +01002698
2699 spin_lock(&i915->mm.obj_lock);
2700 list_add(&obj->mm.link, &i915->mm.unbound_list);
2701 spin_unlock(&i915->mm.obj_lock);
Chris Wilson03ac84f2016-10-28 13:58:36 +01002702}
2703
2704static int ____i915_gem_object_get_pages(struct drm_i915_gem_object *obj)
2705{
Matthew Auldb91b09e2017-10-06 23:18:17 +01002706 int err;
Chris Wilson03ac84f2016-10-28 13:58:36 +01002707
2708 if (unlikely(obj->mm.madv != I915_MADV_WILLNEED)) {
2709 DRM_DEBUG("Attempting to obtain a purgeable object\n");
2710 return -EFAULT;
2711 }
2712
Matthew Auldb91b09e2017-10-06 23:18:17 +01002713 err = obj->ops->get_pages(obj);
Matthew Auldb65a9b92017-12-18 10:38:55 +00002714 GEM_BUG_ON(!err && !i915_gem_object_has_pages(obj));
Chris Wilson03ac84f2016-10-28 13:58:36 +01002715
Matthew Auldb91b09e2017-10-06 23:18:17 +01002716 return err;
Eric Anholt673a3942008-07-30 12:06:12 -07002717}
2718
Chris Wilson37e680a2012-06-07 15:38:42 +01002719/* Ensure that the associated pages are gathered from the backing storage
Chris Wilson1233e2d2016-10-28 13:58:37 +01002720 * and pinned into our object. i915_gem_object_pin_pages() may be called
Chris Wilson37e680a2012-06-07 15:38:42 +01002721 * multiple times before they are released by a single call to
Chris Wilson1233e2d2016-10-28 13:58:37 +01002722 * i915_gem_object_unpin_pages() - once the pages are no longer referenced
Chris Wilson37e680a2012-06-07 15:38:42 +01002723 * either as a result of memory pressure (reaping pages under the shrinker)
2724 * or as the object is itself released.
2725 */
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002726int __i915_gem_object_get_pages(struct drm_i915_gem_object *obj)
Chris Wilson37e680a2012-06-07 15:38:42 +01002727{
Chris Wilson03ac84f2016-10-28 13:58:36 +01002728 int err;
Chris Wilson37e680a2012-06-07 15:38:42 +01002729
Chris Wilson1233e2d2016-10-28 13:58:37 +01002730 err = mutex_lock_interruptible(&obj->mm.lock);
2731 if (err)
2732 return err;
Chris Wilson4c7d62c2016-10-28 13:58:32 +01002733
Chris Wilsonf1fa4f42017-10-13 21:26:13 +01002734 if (unlikely(!i915_gem_object_has_pages(obj))) {
Chris Wilson88c880b2017-09-06 14:52:20 +01002735 GEM_BUG_ON(i915_gem_object_has_pinned_pages(obj));
2736
Chris Wilson2c3a3f42016-11-04 10:30:01 +00002737 err = ____i915_gem_object_get_pages(obj);
2738 if (err)
2739 goto unlock;
2740
2741 smp_mb__before_atomic();
Chris Wilson1233e2d2016-10-28 13:58:37 +01002742 }
Chris Wilson2c3a3f42016-11-04 10:30:01 +00002743 atomic_inc(&obj->mm.pages_pin_count);
Chris Wilson43e28f02013-01-08 10:53:09 +00002744
Chris Wilson1233e2d2016-10-28 13:58:37 +01002745unlock:
2746 mutex_unlock(&obj->mm.lock);
Chris Wilson03ac84f2016-10-28 13:58:36 +01002747 return err;
Eric Anholt673a3942008-07-30 12:06:12 -07002748}
2749
Dave Gordondd6034c2016-05-20 11:54:04 +01002750/* The 'mapping' part of i915_gem_object_pin_map() below */
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002751static void *i915_gem_object_map(const struct drm_i915_gem_object *obj,
2752 enum i915_map_type type)
Dave Gordondd6034c2016-05-20 11:54:04 +01002753{
2754 unsigned long n_pages = obj->base.size >> PAGE_SHIFT;
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002755 struct sg_table *sgt = obj->mm.pages;
Dave Gordon85d12252016-05-20 11:54:06 +01002756 struct sgt_iter sgt_iter;
2757 struct page *page;
Dave Gordonb338fa42016-05-20 11:54:05 +01002758 struct page *stack_pages[32];
2759 struct page **pages = stack_pages;
Dave Gordondd6034c2016-05-20 11:54:04 +01002760 unsigned long i = 0;
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002761 pgprot_t pgprot;
Dave Gordondd6034c2016-05-20 11:54:04 +01002762 void *addr;
2763
2764 /* A single page can always be kmapped */
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002765 if (n_pages == 1 && type == I915_MAP_WB)
Dave Gordondd6034c2016-05-20 11:54:04 +01002766 return kmap(sg_page(sgt->sgl));
2767
Dave Gordonb338fa42016-05-20 11:54:05 +01002768 if (n_pages > ARRAY_SIZE(stack_pages)) {
2769 /* Too big for stack -- allocate temporary array instead */
Michal Hocko0ee931c2017-09-13 16:28:29 -07002770 pages = kvmalloc_array(n_pages, sizeof(*pages), GFP_KERNEL);
Dave Gordonb338fa42016-05-20 11:54:05 +01002771 if (!pages)
2772 return NULL;
2773 }
Dave Gordondd6034c2016-05-20 11:54:04 +01002774
Dave Gordon85d12252016-05-20 11:54:06 +01002775 for_each_sgt_page(page, sgt_iter, sgt)
2776 pages[i++] = page;
Dave Gordondd6034c2016-05-20 11:54:04 +01002777
2778 /* Check that we have the expected number of pages */
2779 GEM_BUG_ON(i != n_pages);
2780
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002781 switch (type) {
Chris Wilsona575c672017-08-28 11:46:31 +01002782 default:
2783 MISSING_CASE(type);
2784 /* fallthrough to use PAGE_KERNEL anyway */
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002785 case I915_MAP_WB:
2786 pgprot = PAGE_KERNEL;
2787 break;
2788 case I915_MAP_WC:
2789 pgprot = pgprot_writecombine(PAGE_KERNEL_IO);
2790 break;
2791 }
2792 addr = vmap(pages, n_pages, 0, pgprot);
Dave Gordondd6034c2016-05-20 11:54:04 +01002793
Dave Gordonb338fa42016-05-20 11:54:05 +01002794 if (pages != stack_pages)
Michal Hocko20981052017-05-17 14:23:12 +02002795 kvfree(pages);
Dave Gordondd6034c2016-05-20 11:54:04 +01002796
2797 return addr;
2798}
2799
2800/* get, pin, and map the pages of the object into kernel space */
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002801void *i915_gem_object_pin_map(struct drm_i915_gem_object *obj,
2802 enum i915_map_type type)
Chris Wilson0a798eb2016-04-08 12:11:11 +01002803{
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002804 enum i915_map_type has_type;
2805 bool pinned;
2806 void *ptr;
Chris Wilson0a798eb2016-04-08 12:11:11 +01002807 int ret;
2808
Tina Zhanga03f3952017-11-14 10:25:13 +00002809 if (unlikely(!i915_gem_object_has_struct_page(obj)))
2810 return ERR_PTR(-ENXIO);
Chris Wilson0a798eb2016-04-08 12:11:11 +01002811
Chris Wilson1233e2d2016-10-28 13:58:37 +01002812 ret = mutex_lock_interruptible(&obj->mm.lock);
Chris Wilson0a798eb2016-04-08 12:11:11 +01002813 if (ret)
2814 return ERR_PTR(ret);
2815
Chris Wilsona575c672017-08-28 11:46:31 +01002816 pinned = !(type & I915_MAP_OVERRIDE);
2817 type &= ~I915_MAP_OVERRIDE;
2818
Chris Wilson1233e2d2016-10-28 13:58:37 +01002819 if (!atomic_inc_not_zero(&obj->mm.pages_pin_count)) {
Chris Wilsonf1fa4f42017-10-13 21:26:13 +01002820 if (unlikely(!i915_gem_object_has_pages(obj))) {
Chris Wilson88c880b2017-09-06 14:52:20 +01002821 GEM_BUG_ON(i915_gem_object_has_pinned_pages(obj));
2822
Chris Wilson2c3a3f42016-11-04 10:30:01 +00002823 ret = ____i915_gem_object_get_pages(obj);
2824 if (ret)
2825 goto err_unlock;
Chris Wilson1233e2d2016-10-28 13:58:37 +01002826
Chris Wilson2c3a3f42016-11-04 10:30:01 +00002827 smp_mb__before_atomic();
2828 }
2829 atomic_inc(&obj->mm.pages_pin_count);
Chris Wilson1233e2d2016-10-28 13:58:37 +01002830 pinned = false;
2831 }
Chris Wilsonf1fa4f42017-10-13 21:26:13 +01002832 GEM_BUG_ON(!i915_gem_object_has_pages(obj));
Chris Wilson0a798eb2016-04-08 12:11:11 +01002833
Chris Wilson0ce81782017-05-17 13:09:59 +01002834 ptr = page_unpack_bits(obj->mm.mapping, &has_type);
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002835 if (ptr && has_type != type) {
2836 if (pinned) {
2837 ret = -EBUSY;
Chris Wilson1233e2d2016-10-28 13:58:37 +01002838 goto err_unpin;
Chris Wilson0a798eb2016-04-08 12:11:11 +01002839 }
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002840
2841 if (is_vmalloc_addr(ptr))
2842 vunmap(ptr);
2843 else
2844 kunmap(kmap_to_page(ptr));
2845
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002846 ptr = obj->mm.mapping = NULL;
Chris Wilson0a798eb2016-04-08 12:11:11 +01002847 }
2848
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002849 if (!ptr) {
2850 ptr = i915_gem_object_map(obj, type);
2851 if (!ptr) {
2852 ret = -ENOMEM;
Chris Wilson1233e2d2016-10-28 13:58:37 +01002853 goto err_unpin;
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002854 }
2855
Chris Wilson0ce81782017-05-17 13:09:59 +01002856 obj->mm.mapping = page_pack_bits(ptr, type);
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002857 }
2858
Chris Wilson1233e2d2016-10-28 13:58:37 +01002859out_unlock:
2860 mutex_unlock(&obj->mm.lock);
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002861 return ptr;
2862
Chris Wilson1233e2d2016-10-28 13:58:37 +01002863err_unpin:
2864 atomic_dec(&obj->mm.pages_pin_count);
2865err_unlock:
2866 ptr = ERR_PTR(ret);
2867 goto out_unlock;
Chris Wilson0a798eb2016-04-08 12:11:11 +01002868}
2869
Chris Wilson7c55e2c2017-03-07 12:03:38 +00002870static int
2871i915_gem_object_pwrite_gtt(struct drm_i915_gem_object *obj,
2872 const struct drm_i915_gem_pwrite *arg)
2873{
2874 struct address_space *mapping = obj->base.filp->f_mapping;
2875 char __user *user_data = u64_to_user_ptr(arg->data_ptr);
2876 u64 remain, offset;
2877 unsigned int pg;
2878
2879 /* Before we instantiate/pin the backing store for our use, we
2880 * can prepopulate the shmemfs filp efficiently using a write into
2881 * the pagecache. We avoid the penalty of instantiating all the
2882 * pages, important if the user is just writing to a few and never
2883 * uses the object on the GPU, and using a direct write into shmemfs
2884 * allows it to avoid the cost of retrieving a page (either swapin
2885 * or clearing-before-use) before it is overwritten.
2886 */
Chris Wilsonf1fa4f42017-10-13 21:26:13 +01002887 if (i915_gem_object_has_pages(obj))
Chris Wilson7c55e2c2017-03-07 12:03:38 +00002888 return -ENODEV;
2889
Chris Wilsona6d65e42017-10-16 21:27:32 +01002890 if (obj->mm.madv != I915_MADV_WILLNEED)
2891 return -EFAULT;
2892
Chris Wilson7c55e2c2017-03-07 12:03:38 +00002893 /* Before the pages are instantiated the object is treated as being
2894 * in the CPU domain. The pages will be clflushed as required before
2895 * use, and we can freely write into the pages directly. If userspace
2896 * races pwrite with any other operation; corruption will ensue -
2897 * that is userspace's prerogative!
2898 */
2899
2900 remain = arg->size;
2901 offset = arg->offset;
2902 pg = offset_in_page(offset);
2903
2904 do {
2905 unsigned int len, unwritten;
2906 struct page *page;
2907 void *data, *vaddr;
2908 int err;
2909
2910 len = PAGE_SIZE - pg;
2911 if (len > remain)
2912 len = remain;
2913
2914 err = pagecache_write_begin(obj->base.filp, mapping,
2915 offset, len, 0,
2916 &page, &data);
2917 if (err < 0)
2918 return err;
2919
2920 vaddr = kmap(page);
2921 unwritten = copy_from_user(vaddr + pg, user_data, len);
2922 kunmap(page);
2923
2924 err = pagecache_write_end(obj->base.filp, mapping,
2925 offset, len, len - unwritten,
2926 page, data);
2927 if (err < 0)
2928 return err;
2929
2930 if (unwritten)
2931 return -EFAULT;
2932
2933 remain -= len;
2934 user_data += len;
2935 offset += len;
2936 pg = 0;
2937 } while (remain);
2938
2939 return 0;
2940}
2941
Mika Kuoppalae5e1fc42016-11-16 17:20:31 +02002942static void i915_gem_context_mark_guilty(struct i915_gem_context *ctx)
Mika Kuoppalaaa60c662013-06-12 15:13:20 +03002943{
Chris Wilson77b25a92017-07-21 13:32:30 +01002944 bool banned;
Mika Kuoppalab083a082016-11-18 15:10:47 +02002945
Chris Wilson77b25a92017-07-21 13:32:30 +01002946 atomic_inc(&ctx->guilty_count);
2947
Chris Wilson24eae082018-02-05 09:22:01 +00002948 banned = false;
2949 if (i915_gem_context_is_bannable(ctx)) {
2950 unsigned int score;
2951
2952 score = atomic_add_return(CONTEXT_SCORE_GUILTY,
2953 &ctx->ban_score);
2954 banned = score >= CONTEXT_SCORE_BAN_THRESHOLD;
2955
2956 DRM_DEBUG_DRIVER("context %s marked guilty (score %d) banned? %s\n",
2957 ctx->name, score, yesno(banned));
2958 }
Chris Wilson77b25a92017-07-21 13:32:30 +01002959 if (!banned)
Mika Kuoppalab083a082016-11-18 15:10:47 +02002960 return;
2961
Chris Wilson77b25a92017-07-21 13:32:30 +01002962 i915_gem_context_set_banned(ctx);
2963 if (!IS_ERR_OR_NULL(ctx->file_priv)) {
2964 atomic_inc(&ctx->file_priv->context_bans);
2965 DRM_DEBUG_DRIVER("client %s has had %d context banned\n",
2966 ctx->name, atomic_read(&ctx->file_priv->context_bans));
2967 }
Mika Kuoppalae5e1fc42016-11-16 17:20:31 +02002968}
2969
2970static void i915_gem_context_mark_innocent(struct i915_gem_context *ctx)
2971{
Chris Wilson77b25a92017-07-21 13:32:30 +01002972 atomic_inc(&ctx->active_count);
Mika Kuoppalaaa60c662013-06-12 15:13:20 +03002973}
2974
Chris Wilsone61e0f52018-02-21 09:56:36 +00002975struct i915_request *
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00002976i915_gem_find_active_request(struct intel_engine_cs *engine)
Chris Wilson9375e442010-09-19 12:21:28 +01002977{
Chris Wilsone61e0f52018-02-21 09:56:36 +00002978 struct i915_request *request, *active = NULL;
Chris Wilson754c9fd2017-02-23 07:44:14 +00002979 unsigned long flags;
Mika Kuoppalaaa60c662013-06-12 15:13:20 +03002980
Chris Wilsoncc7cc532018-05-29 14:29:18 +01002981 /*
2982 * We are called by the error capture, reset and to dump engine
2983 * state at random points in time. In particular, note that neither is
2984 * crucially ordered with an interrupt. After a hang, the GPU is dead
2985 * and we assume that no more writes can happen (we waited long enough
2986 * for all writes that were in transaction to be flushed) - adding an
Chris Wilsonf69a02c2016-07-01 17:23:16 +01002987 * extra delay for a recent interrupt is pointless. Hence, we do
2988 * not need an engine->irq_seqno_barrier() before the seqno reads.
Chris Wilsoncc7cc532018-05-29 14:29:18 +01002989 * At all other times, we must assume the GPU is still running, but
2990 * we only care about the snapshot of this moment.
Chris Wilsonf69a02c2016-07-01 17:23:16 +01002991 */
Chris Wilsona89d1f92018-05-02 17:38:39 +01002992 spin_lock_irqsave(&engine->timeline.lock, flags);
2993 list_for_each_entry(request, &engine->timeline.requests, link) {
Chris Wilsone61e0f52018-02-21 09:56:36 +00002994 if (__i915_request_completed(request, request->global_seqno))
Chris Wilson4db080f2013-12-04 11:37:09 +00002995 continue;
Mika Kuoppalaaa60c662013-06-12 15:13:20 +03002996
Chris Wilson754c9fd2017-02-23 07:44:14 +00002997 active = request;
2998 break;
2999 }
Chris Wilsona89d1f92018-05-02 17:38:39 +01003000 spin_unlock_irqrestore(&engine->timeline.lock, flags);
Chris Wilson754c9fd2017-02-23 07:44:14 +00003001
3002 return active;
Mika Kuoppalab6b0fac2014-01-30 19:04:43 +02003003}
3004
Michel Thierrya1ef70e2017-06-20 10:57:47 +01003005/*
3006 * Ensure irq handler finishes, and not run again.
3007 * Also return the active request so that we only search for it once.
3008 */
Chris Wilsone61e0f52018-02-21 09:56:36 +00003009struct i915_request *
Michel Thierrya1ef70e2017-06-20 10:57:47 +01003010i915_gem_reset_prepare_engine(struct intel_engine_cs *engine)
3011{
Chris Wilson5adfb772018-05-16 19:33:51 +01003012 struct i915_request *request;
Michel Thierrya1ef70e2017-06-20 10:57:47 +01003013
Chris Wilson1749d902017-10-09 12:02:59 +01003014 /*
3015 * During the reset sequence, we must prevent the engine from
3016 * entering RC6. As the context state is undefined until we restart
3017 * the engine, if it does enter RC6 during the reset, the state
3018 * written to the powercontext is undefined and so we may lose
3019 * GPU state upon resume, i.e. fail to restart after a reset.
3020 */
3021 intel_uncore_forcewake_get(engine->i915, FORCEWAKE_ALL);
3022
Chris Wilson5adfb772018-05-16 19:33:51 +01003023 request = engine->reset.prepare(engine);
Chris Wilsond1d1ebf42017-07-21 13:32:33 +01003024 if (request && request->fence.error == -EIO)
3025 request = ERR_PTR(-EIO); /* Previous reset failed! */
Michel Thierrya1ef70e2017-06-20 10:57:47 +01003026
3027 return request;
3028}
3029
Chris Wilson0e178ae2017-01-17 17:59:06 +02003030int i915_gem_reset_prepare(struct drm_i915_private *dev_priv)
Chris Wilson4c965542017-01-17 17:59:01 +02003031{
3032 struct intel_engine_cs *engine;
Chris Wilsone61e0f52018-02-21 09:56:36 +00003033 struct i915_request *request;
Chris Wilson4c965542017-01-17 17:59:01 +02003034 enum intel_engine_id id;
Chris Wilson0e178ae2017-01-17 17:59:06 +02003035 int err = 0;
Chris Wilson4c965542017-01-17 17:59:01 +02003036
Chris Wilson0e178ae2017-01-17 17:59:06 +02003037 for_each_engine(engine, dev_priv, id) {
Michel Thierrya1ef70e2017-06-20 10:57:47 +01003038 request = i915_gem_reset_prepare_engine(engine);
3039 if (IS_ERR(request)) {
3040 err = PTR_ERR(request);
3041 continue;
Chris Wilson0e178ae2017-01-17 17:59:06 +02003042 }
Michel Thierryc64992e2017-06-20 10:57:44 +01003043
3044 engine->hangcheck.active_request = request;
Chris Wilson0e178ae2017-01-17 17:59:06 +02003045 }
3046
Chris Wilson4c965542017-01-17 17:59:01 +02003047 i915_gem_revoke_fences(dev_priv);
Michal Wajdeczkoc37d5722018-03-12 13:03:07 +00003048 intel_uc_sanitize(dev_priv);
Chris Wilson0e178ae2017-01-17 17:59:06 +02003049
3050 return err;
Chris Wilson4c965542017-01-17 17:59:01 +02003051}
3052
Chris Wilsone61e0f52018-02-21 09:56:36 +00003053static void skip_request(struct i915_request *request)
Mika Kuoppalab6b0fac2014-01-30 19:04:43 +02003054{
Chris Wilson821ed7d2016-09-09 14:11:53 +01003055 void *vaddr = request->ring->vaddr;
3056 u32 head;
Mika Kuoppalab6b0fac2014-01-30 19:04:43 +02003057
Chris Wilson821ed7d2016-09-09 14:11:53 +01003058 /* As this request likely depends on state from the lost
3059 * context, clear out all the user operations leaving the
3060 * breadcrumb at the end (so we get the fence notifications).
3061 */
3062 head = request->head;
3063 if (request->postfix < head) {
3064 memset(vaddr + head, 0, request->ring->size - head);
3065 head = 0;
3066 }
3067 memset(vaddr + head, 0, request->postfix - head);
Chris Wilsonc0d5f322017-01-10 17:22:43 +00003068
3069 dma_fence_set_error(&request->fence, -EIO);
Chris Wilson4db080f2013-12-04 11:37:09 +00003070}
3071
Chris Wilsone61e0f52018-02-21 09:56:36 +00003072static void engine_skip_context(struct i915_request *request)
Mika Kuoppala36193ac2017-01-17 17:59:02 +02003073{
3074 struct intel_engine_cs *engine = request->engine;
Chris Wilson4e0d64d2018-05-17 22:26:30 +01003075 struct i915_gem_context *hung_ctx = request->gem_context;
Chris Wilsona89d1f92018-05-02 17:38:39 +01003076 struct i915_timeline *timeline = request->timeline;
Mika Kuoppala36193ac2017-01-17 17:59:02 +02003077 unsigned long flags;
3078
Chris Wilsona89d1f92018-05-02 17:38:39 +01003079 GEM_BUG_ON(timeline == &engine->timeline);
Mika Kuoppala36193ac2017-01-17 17:59:02 +02003080
Chris Wilsona89d1f92018-05-02 17:38:39 +01003081 spin_lock_irqsave(&engine->timeline.lock, flags);
Chris Wilson0c591a42018-05-12 09:49:57 +01003082 spin_lock_nested(&timeline->lock, SINGLE_DEPTH_NESTING);
Mika Kuoppala36193ac2017-01-17 17:59:02 +02003083
Chris Wilsona89d1f92018-05-02 17:38:39 +01003084 list_for_each_entry_continue(request, &engine->timeline.requests, link)
Chris Wilson4e0d64d2018-05-17 22:26:30 +01003085 if (request->gem_context == hung_ctx)
Mika Kuoppala36193ac2017-01-17 17:59:02 +02003086 skip_request(request);
3087
3088 list_for_each_entry(request, &timeline->requests, link)
3089 skip_request(request);
3090
3091 spin_unlock(&timeline->lock);
Chris Wilsona89d1f92018-05-02 17:38:39 +01003092 spin_unlock_irqrestore(&engine->timeline.lock, flags);
Mika Kuoppala36193ac2017-01-17 17:59:02 +02003093}
3094
Chris Wilsond1d1ebf42017-07-21 13:32:33 +01003095/* Returns the request if it was guilty of the hang */
Chris Wilsone61e0f52018-02-21 09:56:36 +00003096static struct i915_request *
Chris Wilsond1d1ebf42017-07-21 13:32:33 +01003097i915_gem_reset_request(struct intel_engine_cs *engine,
Chris Wilsonbba08692018-04-06 23:03:53 +01003098 struct i915_request *request,
3099 bool stalled)
Mika Kuoppala61da5362017-01-17 17:59:05 +02003100{
Mika Kuoppala71895a02017-01-17 17:59:07 +02003101 /* The guilty request will get skipped on a hung engine.
3102 *
3103 * Users of client default contexts do not rely on logical
3104 * state preserved between batches so it is safe to execute
3105 * queued requests following the hang. Non default contexts
3106 * rely on preserved state, so skipping a batch loses the
3107 * evolution of the state and it needs to be considered corrupted.
3108 * Executing more queued batches on top of corrupted state is
3109 * risky. But we take the risk by trying to advance through
3110 * the queued requests in order to make the client behaviour
3111 * more predictable around resets, by not throwing away random
3112 * amount of batches it has prepared for execution. Sophisticated
3113 * clients can use gem_reset_stats_ioctl and dma fence status
3114 * (exported via sync_file info ioctl on explicit fences) to observe
3115 * when it loses the context state and should rebuild accordingly.
3116 *
3117 * The context ban, and ultimately the client ban, mechanism are safety
3118 * valves if client submission ends up resulting in nothing more than
3119 * subsequent hangs.
3120 */
3121
Chris Wilsonbba08692018-04-06 23:03:53 +01003122 if (i915_request_completed(request)) {
3123 GEM_TRACE("%s pardoned global=%d (fence %llx:%d), current %d\n",
3124 engine->name, request->global_seqno,
3125 request->fence.context, request->fence.seqno,
3126 intel_engine_get_seqno(engine));
3127 stalled = false;
3128 }
3129
3130 if (stalled) {
Chris Wilson4e0d64d2018-05-17 22:26:30 +01003131 i915_gem_context_mark_guilty(request->gem_context);
Mika Kuoppala61da5362017-01-17 17:59:05 +02003132 skip_request(request);
Chris Wilsond1d1ebf42017-07-21 13:32:33 +01003133
3134 /* If this context is now banned, skip all pending requests. */
Chris Wilson4e0d64d2018-05-17 22:26:30 +01003135 if (i915_gem_context_is_banned(request->gem_context))
Chris Wilsond1d1ebf42017-07-21 13:32:33 +01003136 engine_skip_context(request);
Mika Kuoppala61da5362017-01-17 17:59:05 +02003137 } else {
Chris Wilsond1d1ebf42017-07-21 13:32:33 +01003138 /*
3139 * Since this is not the hung engine, it may have advanced
3140 * since the hang declaration. Double check by refinding
3141 * the active request at the time of the reset.
3142 */
3143 request = i915_gem_find_active_request(engine);
3144 if (request) {
Chris Wilson4e0d64d2018-05-17 22:26:30 +01003145 i915_gem_context_mark_innocent(request->gem_context);
Chris Wilsond1d1ebf42017-07-21 13:32:33 +01003146 dma_fence_set_error(&request->fence, -EAGAIN);
3147
3148 /* Rewind the engine to replay the incomplete rq */
Chris Wilsona89d1f92018-05-02 17:38:39 +01003149 spin_lock_irq(&engine->timeline.lock);
Chris Wilsond1d1ebf42017-07-21 13:32:33 +01003150 request = list_prev_entry(request, link);
Chris Wilsona89d1f92018-05-02 17:38:39 +01003151 if (&request->link == &engine->timeline.requests)
Chris Wilsond1d1ebf42017-07-21 13:32:33 +01003152 request = NULL;
Chris Wilsona89d1f92018-05-02 17:38:39 +01003153 spin_unlock_irq(&engine->timeline.lock);
Chris Wilsond1d1ebf42017-07-21 13:32:33 +01003154 }
Mika Kuoppala61da5362017-01-17 17:59:05 +02003155 }
3156
Chris Wilsond1d1ebf42017-07-21 13:32:33 +01003157 return request;
Mika Kuoppala61da5362017-01-17 17:59:05 +02003158}
3159
Michel Thierrya1ef70e2017-06-20 10:57:47 +01003160void i915_gem_reset_engine(struct intel_engine_cs *engine,
Chris Wilsonbba08692018-04-06 23:03:53 +01003161 struct i915_request *request,
3162 bool stalled)
Chris Wilson4db080f2013-12-04 11:37:09 +00003163{
Chris Wilsonfcb1de52017-12-19 09:01:10 +00003164 /*
3165 * Make sure this write is visible before we re-enable the interrupt
3166 * handlers on another CPU, as tasklet_enable() resolves to just
3167 * a compiler barrier which is insufficient for our purpose here.
3168 */
3169 smp_store_mb(engine->irq_posted, 0);
Chris Wilsoned454f22017-07-21 13:32:29 +01003170
Chris Wilsond1d1ebf42017-07-21 13:32:33 +01003171 if (request)
Chris Wilsonbba08692018-04-06 23:03:53 +01003172 request = i915_gem_reset_request(engine, request, stalled);
Chris Wilsond1d1ebf42017-07-21 13:32:33 +01003173
Chris Wilson821ed7d2016-09-09 14:11:53 +01003174 /* Setup the CS to resume from the breadcrumb of the hung request */
Chris Wilson5adfb772018-05-16 19:33:51 +01003175 engine->reset.reset(engine, request);
Chris Wilson821ed7d2016-09-09 14:11:53 +01003176}
3177
Chris Wilsond0667e92018-04-06 23:03:54 +01003178void i915_gem_reset(struct drm_i915_private *dev_priv,
3179 unsigned int stalled_mask)
Chris Wilson821ed7d2016-09-09 14:11:53 +01003180{
3181 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05303182 enum intel_engine_id id;
Chris Wilson821ed7d2016-09-09 14:11:53 +01003183
Chris Wilson4c7d62c2016-10-28 13:58:32 +01003184 lockdep_assert_held(&dev_priv->drm.struct_mutex);
3185
Chris Wilsone61e0f52018-02-21 09:56:36 +00003186 i915_retire_requests(dev_priv);
Chris Wilson821ed7d2016-09-09 14:11:53 +01003187
Chris Wilson2ae55732017-02-12 17:20:02 +00003188 for_each_engine(engine, dev_priv, id) {
Chris Wilson1fc44d92018-05-17 22:26:32 +01003189 struct intel_context *ce;
Chris Wilson2ae55732017-02-12 17:20:02 +00003190
Chris Wilsonbba08692018-04-06 23:03:53 +01003191 i915_gem_reset_engine(engine,
3192 engine->hangcheck.active_request,
Chris Wilsond0667e92018-04-06 23:03:54 +01003193 stalled_mask & ENGINE_MASK(id));
Chris Wilson1fc44d92018-05-17 22:26:32 +01003194 ce = fetch_and_zero(&engine->last_retired_context);
3195 if (ce)
3196 intel_context_unpin(ce);
Chris Wilson7b6da812017-12-16 00:03:34 +00003197
3198 /*
3199 * Ostensibily, we always want a context loaded for powersaving,
3200 * so if the engine is idle after the reset, send a request
3201 * to load our scratch kernel_context.
3202 *
3203 * More mysteriously, if we leave the engine idle after a reset,
3204 * the next userspace batch may hang, with what appears to be
3205 * an incoherent read by the CS (presumably stale TLB). An
3206 * empty request appears sufficient to paper over the glitch.
3207 */
Chris Wilson01b8fdc2018-02-05 15:24:31 +00003208 if (intel_engine_is_idle(engine)) {
Chris Wilsone61e0f52018-02-21 09:56:36 +00003209 struct i915_request *rq;
Chris Wilson7b6da812017-12-16 00:03:34 +00003210
Chris Wilsone61e0f52018-02-21 09:56:36 +00003211 rq = i915_request_alloc(engine,
3212 dev_priv->kernel_context);
Chris Wilson7b6da812017-12-16 00:03:34 +00003213 if (!IS_ERR(rq))
Chris Wilsone61e0f52018-02-21 09:56:36 +00003214 __i915_request_add(rq, false);
Chris Wilson7b6da812017-12-16 00:03:34 +00003215 }
Chris Wilson2ae55732017-02-12 17:20:02 +00003216 }
Chris Wilson821ed7d2016-09-09 14:11:53 +01003217
Tvrtko Ursulin4362f4f2016-11-16 08:55:33 +00003218 i915_gem_restore_fences(dev_priv);
Chris Wilson821ed7d2016-09-09 14:11:53 +01003219}
3220
Michel Thierrya1ef70e2017-06-20 10:57:47 +01003221void i915_gem_reset_finish_engine(struct intel_engine_cs *engine)
3222{
Chris Wilson5adfb772018-05-16 19:33:51 +01003223 engine->reset.finish(engine);
3224
Chris Wilson1749d902017-10-09 12:02:59 +01003225 intel_uncore_forcewake_put(engine->i915, FORCEWAKE_ALL);
Michel Thierrya1ef70e2017-06-20 10:57:47 +01003226}
3227
Chris Wilsond8027092017-02-08 14:30:32 +00003228void i915_gem_reset_finish(struct drm_i915_private *dev_priv)
3229{
Chris Wilson1f7b8472017-02-08 14:30:33 +00003230 struct intel_engine_cs *engine;
3231 enum intel_engine_id id;
3232
Chris Wilsond8027092017-02-08 14:30:32 +00003233 lockdep_assert_held(&dev_priv->drm.struct_mutex);
Chris Wilson1f7b8472017-02-08 14:30:33 +00003234
Chris Wilsonfe3288b2017-02-12 17:20:01 +00003235 for_each_engine(engine, dev_priv, id) {
Michel Thierryc64992e2017-06-20 10:57:44 +01003236 engine->hangcheck.active_request = NULL;
Michel Thierrya1ef70e2017-06-20 10:57:47 +01003237 i915_gem_reset_finish_engine(engine);
Chris Wilsonfe3288b2017-02-12 17:20:01 +00003238 }
Chris Wilsond8027092017-02-08 14:30:32 +00003239}
3240
Chris Wilsone61e0f52018-02-21 09:56:36 +00003241static void nop_submit_request(struct i915_request *request)
Chris Wilson821ed7d2016-09-09 14:11:53 +01003242{
Chris Wilsond9b13c42018-03-15 13:14:50 +00003243 GEM_TRACE("%s fence %llx:%d -> -EIO\n",
3244 request->engine->name,
3245 request->fence.context, request->fence.seqno);
Daniel Vetteraf7a8ff2017-10-11 11:10:19 +02003246 dma_fence_set_error(&request->fence, -EIO);
3247
Chris Wilsone61e0f52018-02-21 09:56:36 +00003248 i915_request_submit(request);
Daniel Vetteraf7a8ff2017-10-11 11:10:19 +02003249}
3250
Chris Wilsone61e0f52018-02-21 09:56:36 +00003251static void nop_complete_submit_request(struct i915_request *request)
Daniel Vetteraf7a8ff2017-10-11 11:10:19 +02003252{
Chris Wilson8d550822017-10-06 12:56:17 +01003253 unsigned long flags;
3254
Chris Wilsond9b13c42018-03-15 13:14:50 +00003255 GEM_TRACE("%s fence %llx:%d -> -EIO\n",
3256 request->engine->name,
3257 request->fence.context, request->fence.seqno);
Chris Wilson3cd94422017-01-10 17:22:45 +00003258 dma_fence_set_error(&request->fence, -EIO);
Chris Wilson8d550822017-10-06 12:56:17 +01003259
Chris Wilsona89d1f92018-05-02 17:38:39 +01003260 spin_lock_irqsave(&request->engine->timeline.lock, flags);
Chris Wilsone61e0f52018-02-21 09:56:36 +00003261 __i915_request_submit(request);
Chris Wilson3dcf93f72016-11-22 14:41:20 +00003262 intel_engine_init_global_seqno(request->engine, request->global_seqno);
Chris Wilsona89d1f92018-05-02 17:38:39 +01003263 spin_unlock_irqrestore(&request->engine->timeline.lock, flags);
Chris Wilson821ed7d2016-09-09 14:11:53 +01003264}
3265
Daniel Vetteraf7a8ff2017-10-11 11:10:19 +02003266void i915_gem_set_wedged(struct drm_i915_private *i915)
Chris Wilson821ed7d2016-09-09 14:11:53 +01003267{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00003268 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05303269 enum intel_engine_id id;
Eric Anholt673a3942008-07-30 12:06:12 -07003270
Chris Wilsond9b13c42018-03-15 13:14:50 +00003271 GEM_TRACE("start\n");
3272
Chris Wilson7f961d72018-04-26 11:32:19 +01003273 if (GEM_SHOW_DEBUG()) {
Chris Wilson559e0402018-02-05 09:21:59 +00003274 struct drm_printer p = drm_debug_printer(__func__);
3275
3276 for_each_engine(engine, i915, id)
3277 intel_engine_dump(engine, &p, "%s\n", engine->name);
3278 }
3279
Chris Wilson0d73e7a2018-02-07 15:13:50 +00003280 set_bit(I915_WEDGED, &i915->gpu_error.flags);
3281 smp_mb__after_atomic();
3282
Daniel Vetteraf7a8ff2017-10-11 11:10:19 +02003283 /*
3284 * First, stop submission to hw, but do not yet complete requests by
3285 * rolling the global seqno forward (since this would complete requests
3286 * for which we haven't set the fence error to EIO yet).
3287 */
Chris Wilson963ddd62018-03-02 11:33:24 +00003288 for_each_engine(engine, i915, id) {
3289 i915_gem_reset_prepare_engine(engine);
Chris Wilson47650db2018-03-07 13:42:25 +00003290
Daniel Vetteraf7a8ff2017-10-11 11:10:19 +02003291 engine->submit_request = nop_submit_request;
Chris Wilson47650db2018-03-07 13:42:25 +00003292 engine->schedule = NULL;
Chris Wilson963ddd62018-03-02 11:33:24 +00003293 }
Chris Wilson47650db2018-03-07 13:42:25 +00003294 i915->caps.scheduler = 0;
Daniel Vetteraf7a8ff2017-10-11 11:10:19 +02003295
Chris Wilsonac697ae2018-03-15 15:10:15 +00003296 /* Even if the GPU reset fails, it should still stop the engines */
3297 intel_gpu_reset(i915, ALL_ENGINES);
3298
Daniel Vetteraf7a8ff2017-10-11 11:10:19 +02003299 /*
3300 * Make sure no one is running the old callback before we proceed with
3301 * cancelling requests and resetting the completion tracking. Otherwise
3302 * we might submit a request to the hardware which never completes.
3303 */
3304 synchronize_rcu();
3305
3306 for_each_engine(engine, i915, id) {
3307 /* Mark all executing requests as skipped */
3308 engine->cancel_requests(engine);
3309
3310 /*
3311 * Only once we've force-cancelled all in-flight requests can we
3312 * start to complete all requests.
3313 */
3314 engine->submit_request = nop_complete_submit_request;
3315 }
3316
3317 /*
3318 * Make sure no request can slip through without getting completed by
3319 * either this call here to intel_engine_init_global_seqno, or the one
3320 * in nop_complete_submit_request.
3321 */
3322 synchronize_rcu();
3323
3324 for_each_engine(engine, i915, id) {
3325 unsigned long flags;
3326
Chris Wilson0d73e7a2018-02-07 15:13:50 +00003327 /*
3328 * Mark all pending requests as complete so that any concurrent
Daniel Vetteraf7a8ff2017-10-11 11:10:19 +02003329 * (lockless) lookup doesn't try and wait upon the request as we
3330 * reset it.
3331 */
Chris Wilsona89d1f92018-05-02 17:38:39 +01003332 spin_lock_irqsave(&engine->timeline.lock, flags);
Daniel Vetteraf7a8ff2017-10-11 11:10:19 +02003333 intel_engine_init_global_seqno(engine,
3334 intel_engine_last_submit(engine));
Chris Wilsona89d1f92018-05-02 17:38:39 +01003335 spin_unlock_irqrestore(&engine->timeline.lock, flags);
Chris Wilson963ddd62018-03-02 11:33:24 +00003336
3337 i915_gem_reset_finish_engine(engine);
Daniel Vetteraf7a8ff2017-10-11 11:10:19 +02003338 }
Chris Wilson20e49332016-11-22 14:41:21 +00003339
Chris Wilsond9b13c42018-03-15 13:14:50 +00003340 GEM_TRACE("end\n");
3341
Chris Wilson3d7adbb2017-07-21 13:32:27 +01003342 wake_up_all(&i915->gpu_error.reset_queue);
Eric Anholt673a3942008-07-30 12:06:12 -07003343}
3344
Chris Wilson2e8f9d32017-03-16 17:13:04 +00003345bool i915_gem_unset_wedged(struct drm_i915_private *i915)
3346{
Chris Wilsona89d1f92018-05-02 17:38:39 +01003347 struct i915_timeline *tl;
Chris Wilson2e8f9d32017-03-16 17:13:04 +00003348
3349 lockdep_assert_held(&i915->drm.struct_mutex);
3350 if (!test_bit(I915_WEDGED, &i915->gpu_error.flags))
3351 return true;
3352
Chris Wilsond9b13c42018-03-15 13:14:50 +00003353 GEM_TRACE("start\n");
3354
Chris Wilson2d4ecac2018-03-07 13:42:21 +00003355 /*
3356 * Before unwedging, make sure that all pending operations
Chris Wilson2e8f9d32017-03-16 17:13:04 +00003357 * are flushed and errored out - we may have requests waiting upon
3358 * third party fences. We marked all inflight requests as EIO, and
3359 * every execbuf since returned EIO, for consistency we want all
3360 * the currently pending requests to also be marked as EIO, which
3361 * is done inside our nop_submit_request - and so we must wait.
3362 *
3363 * No more can be submitted until we reset the wedged bit.
3364 */
3365 list_for_each_entry(tl, &i915->gt.timelines, link) {
Chris Wilsona89d1f92018-05-02 17:38:39 +01003366 struct i915_request *rq;
Chris Wilson2e8f9d32017-03-16 17:13:04 +00003367
Chris Wilsona89d1f92018-05-02 17:38:39 +01003368 rq = i915_gem_active_peek(&tl->last_request,
3369 &i915->drm.struct_mutex);
3370 if (!rq)
3371 continue;
Chris Wilson2e8f9d32017-03-16 17:13:04 +00003372
Chris Wilsona89d1f92018-05-02 17:38:39 +01003373 /*
3374 * We can't use our normal waiter as we want to
3375 * avoid recursively trying to handle the current
3376 * reset. The basic dma_fence_default_wait() installs
3377 * a callback for dma_fence_signal(), which is
3378 * triggered by our nop handler (indirectly, the
3379 * callback enables the signaler thread which is
3380 * woken by the nop_submit_request() advancing the seqno
3381 * and when the seqno passes the fence, the signaler
3382 * then signals the fence waking us up).
3383 */
3384 if (dma_fence_default_wait(&rq->fence, true,
3385 MAX_SCHEDULE_TIMEOUT) < 0)
3386 return false;
Chris Wilson2e8f9d32017-03-16 17:13:04 +00003387 }
Chris Wilson2d4ecac2018-03-07 13:42:21 +00003388 i915_retire_requests(i915);
3389 GEM_BUG_ON(i915->gt.active_requests);
Chris Wilson2e8f9d32017-03-16 17:13:04 +00003390
Chris Wilson2d4ecac2018-03-07 13:42:21 +00003391 /*
3392 * Undo nop_submit_request. We prevent all new i915 requests from
Chris Wilson2e8f9d32017-03-16 17:13:04 +00003393 * being queued (by disallowing execbuf whilst wedged) so having
3394 * waited for all active requests above, we know the system is idle
3395 * and do not have to worry about a thread being inside
3396 * engine->submit_request() as we swap over. So unlike installing
3397 * the nop_submit_request on reset, we can do this from normal
3398 * context and do not require stop_machine().
3399 */
3400 intel_engines_reset_default_submission(i915);
Chris Wilson36703e72017-06-22 11:56:25 +01003401 i915_gem_contexts_lost(i915);
Chris Wilson2e8f9d32017-03-16 17:13:04 +00003402
Chris Wilsond9b13c42018-03-15 13:14:50 +00003403 GEM_TRACE("end\n");
3404
Chris Wilson2e8f9d32017-03-16 17:13:04 +00003405 smp_mb__before_atomic(); /* complete takeover before enabling execbuf */
3406 clear_bit(I915_WEDGED, &i915->gpu_error.flags);
3407
3408 return true;
3409}
3410
Daniel Vetter75ef9da2010-08-21 00:25:16 +02003411static void
Eric Anholt673a3942008-07-30 12:06:12 -07003412i915_gem_retire_work_handler(struct work_struct *work)
3413{
Chris Wilsonb29c19b2013-09-25 17:34:56 +01003414 struct drm_i915_private *dev_priv =
Chris Wilson67d97da2016-07-04 08:08:31 +01003415 container_of(work, typeof(*dev_priv), gt.retire_work.work);
Chris Wilson91c8a322016-07-05 10:40:23 +01003416 struct drm_device *dev = &dev_priv->drm;
Eric Anholt673a3942008-07-30 12:06:12 -07003417
Chris Wilson891b48c2010-09-29 12:26:37 +01003418 /* Come back later if the device is busy... */
Chris Wilsonb29c19b2013-09-25 17:34:56 +01003419 if (mutex_trylock(&dev->struct_mutex)) {
Chris Wilsone61e0f52018-02-21 09:56:36 +00003420 i915_retire_requests(dev_priv);
Chris Wilsonb29c19b2013-09-25 17:34:56 +01003421 mutex_unlock(&dev->struct_mutex);
3422 }
Chris Wilson67d97da2016-07-04 08:08:31 +01003423
Chris Wilson88923042018-01-29 14:41:04 +00003424 /*
3425 * Keep the retire handler running until we are finally idle.
Chris Wilson67d97da2016-07-04 08:08:31 +01003426 * We do not need to do this test under locking as in the worst-case
3427 * we queue the retire worker once too often.
3428 */
Chris Wilson88923042018-01-29 14:41:04 +00003429 if (READ_ONCE(dev_priv->gt.awake))
Chris Wilson67d97da2016-07-04 08:08:31 +01003430 queue_delayed_work(dev_priv->wq,
3431 &dev_priv->gt.retire_work,
Chris Wilsonbcb45082012-10-05 17:02:57 +01003432 round_jiffies_up_relative(HZ));
Chris Wilsonb29c19b2013-09-25 17:34:56 +01003433}
Chris Wilson891b48c2010-09-29 12:26:37 +01003434
Chris Wilson84a10742018-01-24 11:36:08 +00003435static void shrink_caches(struct drm_i915_private *i915)
3436{
3437 /*
3438 * kmem_cache_shrink() discards empty slabs and reorders partially
3439 * filled slabs to prioritise allocating from the mostly full slabs,
3440 * with the aim of reducing fragmentation.
3441 */
3442 kmem_cache_shrink(i915->priorities);
3443 kmem_cache_shrink(i915->dependencies);
3444 kmem_cache_shrink(i915->requests);
3445 kmem_cache_shrink(i915->luts);
3446 kmem_cache_shrink(i915->vmas);
3447 kmem_cache_shrink(i915->objects);
3448}
3449
3450struct sleep_rcu_work {
3451 union {
3452 struct rcu_head rcu;
3453 struct work_struct work;
3454 };
3455 struct drm_i915_private *i915;
3456 unsigned int epoch;
3457};
3458
3459static inline bool
3460same_epoch(struct drm_i915_private *i915, unsigned int epoch)
3461{
3462 /*
3463 * There is a small chance that the epoch wrapped since we started
3464 * sleeping. If we assume that epoch is at least a u32, then it will
3465 * take at least 2^32 * 100ms for it to wrap, or about 326 years.
3466 */
3467 return epoch == READ_ONCE(i915->gt.epoch);
3468}
3469
3470static void __sleep_work(struct work_struct *work)
3471{
3472 struct sleep_rcu_work *s = container_of(work, typeof(*s), work);
3473 struct drm_i915_private *i915 = s->i915;
3474 unsigned int epoch = s->epoch;
3475
3476 kfree(s);
3477 if (same_epoch(i915, epoch))
3478 shrink_caches(i915);
3479}
3480
3481static void __sleep_rcu(struct rcu_head *rcu)
3482{
3483 struct sleep_rcu_work *s = container_of(rcu, typeof(*s), rcu);
3484 struct drm_i915_private *i915 = s->i915;
3485
3486 if (same_epoch(i915, s->epoch)) {
3487 INIT_WORK(&s->work, __sleep_work);
3488 queue_work(i915->wq, &s->work);
3489 } else {
3490 kfree(s);
3491 }
3492}
3493
Chris Wilson5427f202017-10-23 22:32:34 +01003494static inline bool
3495new_requests_since_last_retire(const struct drm_i915_private *i915)
3496{
3497 return (READ_ONCE(i915->gt.active_requests) ||
3498 work_pending(&i915->gt.idle_work.work));
3499}
3500
Chris Wilsonb29c19b2013-09-25 17:34:56 +01003501static void
3502i915_gem_idle_work_handler(struct work_struct *work)
3503{
3504 struct drm_i915_private *dev_priv =
Chris Wilson67d97da2016-07-04 08:08:31 +01003505 container_of(work, typeof(*dev_priv), gt.idle_work.work);
Chris Wilson84a10742018-01-24 11:36:08 +00003506 unsigned int epoch = I915_EPOCH_INVALID;
Chris Wilson67d97da2016-07-04 08:08:31 +01003507 bool rearm_hangcheck;
3508
3509 if (!READ_ONCE(dev_priv->gt.awake))
3510 return;
3511
Chris Wilson4dfacb02018-05-31 09:22:43 +01003512 if (READ_ONCE(dev_priv->gt.active_requests))
3513 return;
3514
3515 /*
3516 * Flush out the last user context, leaving only the pinned
3517 * kernel context resident. When we are idling on the kernel_context,
3518 * no more new requests (with a context switch) are emitted and we
3519 * can finally rest. A consequence is that the idle work handler is
3520 * always called at least twice before idling (and if the system is
3521 * idle that implies a round trip through the retire worker).
3522 */
3523 mutex_lock(&dev_priv->drm.struct_mutex);
3524 i915_gem_switch_to_kernel_context(dev_priv);
3525 mutex_unlock(&dev_priv->drm.struct_mutex);
3526
3527 GEM_TRACE("active_requests=%d (after switch-to-kernel-context)\n",
3528 READ_ONCE(dev_priv->gt.active_requests));
3529
Imre Deak0cb56702016-11-07 11:20:04 +02003530 /*
3531 * Wait for last execlists context complete, but bail out in case a
Chris Wilsonffed7bd2018-03-01 10:33:38 +00003532 * new request is submitted. As we don't trust the hardware, we
3533 * continue on if the wait times out. This is necessary to allow
3534 * the machine to suspend even if the hardware dies, and we will
3535 * try to recover in resume (after depriving the hardware of power,
3536 * it may be in a better mmod).
Imre Deak0cb56702016-11-07 11:20:04 +02003537 */
Chris Wilsonffed7bd2018-03-01 10:33:38 +00003538 __wait_for(if (new_requests_since_last_retire(dev_priv)) return,
3539 intel_engines_are_idle(dev_priv),
3540 I915_IDLE_ENGINES_TIMEOUT * 1000,
3541 10, 500);
Chris Wilson67d97da2016-07-04 08:08:31 +01003542
3543 rearm_hangcheck =
3544 cancel_delayed_work_sync(&dev_priv->gpu_error.hangcheck_work);
3545
Chris Wilson5427f202017-10-23 22:32:34 +01003546 if (!mutex_trylock(&dev_priv->drm.struct_mutex)) {
Chris Wilson67d97da2016-07-04 08:08:31 +01003547 /* Currently busy, come back later */
3548 mod_delayed_work(dev_priv->wq,
3549 &dev_priv->gt.idle_work,
3550 msecs_to_jiffies(50));
3551 goto out_rearm;
3552 }
3553
Imre Deak93c97dc2016-11-07 11:20:03 +02003554 /*
3555 * New request retired after this work handler started, extend active
3556 * period until next instance of the work.
3557 */
Chris Wilson5427f202017-10-23 22:32:34 +01003558 if (new_requests_since_last_retire(dev_priv))
Imre Deak93c97dc2016-11-07 11:20:03 +02003559 goto out_unlock;
3560
Chris Wilsone4d20062018-04-06 16:51:44 +01003561 epoch = __i915_gem_park(dev_priv);
Chris Wilsonff320d62017-10-23 22:32:35 +01003562
Chris Wilson67d97da2016-07-04 08:08:31 +01003563 rearm_hangcheck = false;
Chris Wilson67d97da2016-07-04 08:08:31 +01003564out_unlock:
Chris Wilson5427f202017-10-23 22:32:34 +01003565 mutex_unlock(&dev_priv->drm.struct_mutex);
Chris Wilson35c94182015-04-07 16:20:37 +01003566
Chris Wilson67d97da2016-07-04 08:08:31 +01003567out_rearm:
3568 if (rearm_hangcheck) {
3569 GEM_BUG_ON(!dev_priv->gt.awake);
3570 i915_queue_hangcheck(dev_priv);
Chris Wilson35c94182015-04-07 16:20:37 +01003571 }
Chris Wilson84a10742018-01-24 11:36:08 +00003572
3573 /*
3574 * When we are idle, it is an opportune time to reap our caches.
3575 * However, we have many objects that utilise RCU and the ordered
3576 * i915->wq that this work is executing on. To try and flush any
3577 * pending frees now we are idle, we first wait for an RCU grace
3578 * period, and then queue a task (that will run last on the wq) to
3579 * shrink and re-optimize the caches.
3580 */
3581 if (same_epoch(dev_priv, epoch)) {
3582 struct sleep_rcu_work *s = kmalloc(sizeof(*s), GFP_KERNEL);
3583 if (s) {
3584 s->i915 = dev_priv;
3585 s->epoch = epoch;
3586 call_rcu(&s->rcu, __sleep_rcu);
3587 }
3588 }
Eric Anholt673a3942008-07-30 12:06:12 -07003589}
3590
Chris Wilsonb1f788c2016-08-04 07:52:45 +01003591void i915_gem_close_object(struct drm_gem_object *gem, struct drm_file *file)
3592{
Chris Wilsond1b48c12017-08-16 09:52:08 +01003593 struct drm_i915_private *i915 = to_i915(gem->dev);
Chris Wilsonb1f788c2016-08-04 07:52:45 +01003594 struct drm_i915_gem_object *obj = to_intel_bo(gem);
3595 struct drm_i915_file_private *fpriv = file->driver_priv;
Chris Wilsond1b48c12017-08-16 09:52:08 +01003596 struct i915_lut_handle *lut, *ln;
Chris Wilsonb1f788c2016-08-04 07:52:45 +01003597
Chris Wilsond1b48c12017-08-16 09:52:08 +01003598 mutex_lock(&i915->drm.struct_mutex);
3599
3600 list_for_each_entry_safe(lut, ln, &obj->lut_list, obj_link) {
3601 struct i915_gem_context *ctx = lut->ctx;
3602 struct i915_vma *vma;
3603
Chris Wilson432295d2017-08-22 12:05:15 +01003604 GEM_BUG_ON(ctx->file_priv == ERR_PTR(-EBADF));
Chris Wilsond1b48c12017-08-16 09:52:08 +01003605 if (ctx->file_priv != fpriv)
3606 continue;
3607
3608 vma = radix_tree_delete(&ctx->handles_vma, lut->handle);
Chris Wilson3ffff012017-08-22 12:05:17 +01003609 GEM_BUG_ON(vma->obj != obj);
3610
3611 /* We allow the process to have multiple handles to the same
3612 * vma, in the same fd namespace, by virtue of flink/open.
3613 */
3614 GEM_BUG_ON(!vma->open_count);
3615 if (!--vma->open_count && !i915_vma_is_ggtt(vma))
Chris Wilsonb1f788c2016-08-04 07:52:45 +01003616 i915_vma_close(vma);
Chris Wilsonf8a7fde2016-10-28 13:58:29 +01003617
Chris Wilsond1b48c12017-08-16 09:52:08 +01003618 list_del(&lut->obj_link);
3619 list_del(&lut->ctx_link);
Chris Wilson4ff4b442017-06-16 15:05:16 +01003620
Chris Wilsond1b48c12017-08-16 09:52:08 +01003621 kmem_cache_free(i915->luts, lut);
3622 __i915_gem_object_release_unless_active(obj);
Chris Wilsonf8a7fde2016-10-28 13:58:29 +01003623 }
Chris Wilsond1b48c12017-08-16 09:52:08 +01003624
3625 mutex_unlock(&i915->drm.struct_mutex);
Chris Wilsonb1f788c2016-08-04 07:52:45 +01003626}
3627
Chris Wilsone95433c2016-10-28 13:58:27 +01003628static unsigned long to_wait_timeout(s64 timeout_ns)
3629{
3630 if (timeout_ns < 0)
3631 return MAX_SCHEDULE_TIMEOUT;
3632
3633 if (timeout_ns == 0)
3634 return 0;
3635
3636 return nsecs_to_jiffies_timeout(timeout_ns);
3637}
3638
Ben Widawsky5816d642012-04-11 11:18:19 -07003639/**
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07003640 * i915_gem_wait_ioctl - implements DRM_IOCTL_I915_GEM_WAIT
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01003641 * @dev: drm device pointer
3642 * @data: ioctl data blob
3643 * @file: drm file pointer
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07003644 *
3645 * Returns 0 if successful, else an error is returned with the remaining time in
3646 * the timeout parameter.
3647 * -ETIME: object is still busy after timeout
3648 * -ERESTARTSYS: signal interrupted the wait
3649 * -ENONENT: object doesn't exist
3650 * Also possible, but rare:
Chris Wilsonb8050142017-08-11 11:57:31 +01003651 * -EAGAIN: incomplete, restart syscall
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07003652 * -ENOMEM: damn
3653 * -ENODEV: Internal IRQ fail
3654 * -E?: The add request failed
3655 *
3656 * The wait ioctl with a timeout of 0 reimplements the busy ioctl. With any
3657 * non-zero timeout parameter the wait ioctl will wait for the given number of
3658 * nanoseconds on an object becoming unbusy. Since the wait itself does so
3659 * without holding struct_mutex the object may become re-busied before this
3660 * function completes. A similar but shorter * race condition exists in the busy
3661 * ioctl
3662 */
3663int
3664i915_gem_wait_ioctl(struct drm_device *dev, void *data, struct drm_file *file)
3665{
3666 struct drm_i915_gem_wait *args = data;
3667 struct drm_i915_gem_object *obj;
Chris Wilsone95433c2016-10-28 13:58:27 +01003668 ktime_t start;
3669 long ret;
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07003670
Daniel Vetter11b5d512014-09-29 15:31:26 +02003671 if (args->flags != 0)
3672 return -EINVAL;
3673
Chris Wilson03ac0642016-07-20 13:31:51 +01003674 obj = i915_gem_object_lookup(file, args->bo_handle);
Chris Wilson033d5492016-08-05 10:14:17 +01003675 if (!obj)
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07003676 return -ENOENT;
Chris Wilson033d5492016-08-05 10:14:17 +01003677
Chris Wilsone95433c2016-10-28 13:58:27 +01003678 start = ktime_get();
3679
3680 ret = i915_gem_object_wait(obj,
3681 I915_WAIT_INTERRUPTIBLE | I915_WAIT_ALL,
3682 to_wait_timeout(args->timeout_ns),
3683 to_rps_client(file));
3684
3685 if (args->timeout_ns > 0) {
3686 args->timeout_ns -= ktime_to_ns(ktime_sub(ktime_get(), start));
3687 if (args->timeout_ns < 0)
3688 args->timeout_ns = 0;
Chris Wilsonc1d20612017-02-16 12:54:41 +00003689
3690 /*
3691 * Apparently ktime isn't accurate enough and occasionally has a
3692 * bit of mismatch in the jiffies<->nsecs<->ktime loop. So patch
3693 * things up to make the test happy. We allow up to 1 jiffy.
3694 *
3695 * This is a regression from the timespec->ktime conversion.
3696 */
3697 if (ret == -ETIME && !nsecs_to_jiffies(args->timeout_ns))
3698 args->timeout_ns = 0;
Chris Wilsonb8050142017-08-11 11:57:31 +01003699
3700 /* Asked to wait beyond the jiffie/scheduler precision? */
3701 if (ret == -ETIME && args->timeout_ns)
3702 ret = -EAGAIN;
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07003703 }
3704
Chris Wilsonf0cd5182016-10-28 13:58:43 +01003705 i915_gem_object_put(obj);
John Harrisonff865882014-11-24 18:49:28 +00003706 return ret;
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07003707}
3708
Chris Wilsona89d1f92018-05-02 17:38:39 +01003709static int wait_for_timeline(struct i915_timeline *tl, unsigned int flags)
Daniel Vetter4df2faf2010-02-19 11:52:00 +01003710{
Chris Wilson06060352018-05-31 09:22:44 +01003711 struct i915_request *rq;
3712 long ret;
3713
3714 rq = i915_gem_active_get_unlocked(&tl->last_request);
3715 if (!rq)
3716 return 0;
3717
3718 /*
3719 * "Race-to-idle".
3720 *
3721 * Switching to the kernel context is often used a synchronous
3722 * step prior to idling, e.g. in suspend for flushing all
3723 * current operations to memory before sleeping. These we
3724 * want to complete as quickly as possible to avoid prolonged
3725 * stalls, so allow the gpu to boost to maximum clocks.
3726 */
3727 if (flags & I915_WAIT_FOR_IDLE_BOOST)
3728 gen6_rps_boost(rq, NULL);
3729
3730 ret = i915_request_wait(rq, flags, MAX_SCHEDULE_TIMEOUT);
3731 i915_request_put(rq);
3732
3733 return ret < 0 ? ret : 0;
Chris Wilson73cb9702016-10-28 13:58:46 +01003734}
3735
Chris Wilson25112b62017-03-30 15:50:39 +01003736static int wait_for_engines(struct drm_i915_private *i915)
3737{
Chris Wilsonee42c002017-12-11 19:41:34 +00003738 if (wait_for(intel_engines_are_idle(i915), I915_IDLE_ENGINES_TIMEOUT)) {
Chris Wilson59e4b192017-12-11 19:41:35 +00003739 dev_err(i915->drm.dev,
3740 "Failed to idle engines, declaring wedged!\n");
Chris Wilson629820f2018-03-09 10:11:14 +00003741 GEM_TRACE_DUMP();
Chris Wilsoncad99462017-08-26 12:09:33 +01003742 i915_gem_set_wedged(i915);
3743 return -EIO;
Chris Wilson25112b62017-03-30 15:50:39 +01003744 }
3745
3746 return 0;
3747}
3748
Chris Wilson73cb9702016-10-28 13:58:46 +01003749int i915_gem_wait_for_idle(struct drm_i915_private *i915, unsigned int flags)
3750{
Chris Wilson09a4c022018-05-24 09:11:35 +01003751 GEM_TRACE("flags=%x (%s)\n",
3752 flags, flags & I915_WAIT_LOCKED ? "locked" : "unlocked");
3753
Chris Wilson863e9fd2017-05-30 13:13:32 +01003754 /* If the device is asleep, we have no requests outstanding */
3755 if (!READ_ONCE(i915->gt.awake))
3756 return 0;
3757
Chris Wilson9caa34a2016-11-11 14:58:08 +00003758 if (flags & I915_WAIT_LOCKED) {
Chris Wilsona89d1f92018-05-02 17:38:39 +01003759 struct i915_timeline *tl;
3760 int err;
Chris Wilson9caa34a2016-11-11 14:58:08 +00003761
3762 lockdep_assert_held(&i915->drm.struct_mutex);
3763
3764 list_for_each_entry(tl, &i915->gt.timelines, link) {
Chris Wilsona89d1f92018-05-02 17:38:39 +01003765 err = wait_for_timeline(tl, flags);
3766 if (err)
3767 return err;
Chris Wilson9caa34a2016-11-11 14:58:08 +00003768 }
Chris Wilsone61e0f52018-02-21 09:56:36 +00003769 i915_retire_requests(i915);
Chris Wilson09a4c022018-05-24 09:11:35 +01003770 GEM_BUG_ON(i915->gt.active_requests);
Chris Wilson25112b62017-03-30 15:50:39 +01003771
Chris Wilsona89d1f92018-05-02 17:38:39 +01003772 return wait_for_engines(i915);
Chris Wilson9caa34a2016-11-11 14:58:08 +00003773 } else {
Chris Wilsona89d1f92018-05-02 17:38:39 +01003774 struct intel_engine_cs *engine;
3775 enum intel_engine_id id;
3776 int err;
Zou Nan haid1b851f2010-05-21 09:08:57 +08003777
Chris Wilsona89d1f92018-05-02 17:38:39 +01003778 for_each_engine(engine, i915, id) {
3779 err = wait_for_timeline(&engine->timeline, flags);
3780 if (err)
3781 return err;
3782 }
3783
3784 return 0;
3785 }
Daniel Vetter4df2faf2010-02-19 11:52:00 +01003786}
3787
Chris Wilson5a97bcc2017-02-22 11:40:46 +00003788static void __i915_gem_object_flush_for_display(struct drm_i915_gem_object *obj)
3789{
Chris Wilsone27ab732017-06-15 13:38:49 +01003790 /*
3791 * We manually flush the CPU domain so that we can override and
3792 * force the flush for the display, and perform it asyncrhonously.
3793 */
3794 flush_write_domain(obj, ~I915_GEM_DOMAIN_CPU);
3795 if (obj->cache_dirty)
3796 i915_gem_clflush_object(obj, I915_CLFLUSH_FORCE);
Christian Königc0a51fd2018-02-16 13:43:38 +01003797 obj->write_domain = 0;
Chris Wilson5a97bcc2017-02-22 11:40:46 +00003798}
3799
3800void i915_gem_object_flush_if_display(struct drm_i915_gem_object *obj)
3801{
Chris Wilsonbd3d2252017-10-13 21:26:14 +01003802 if (!READ_ONCE(obj->pin_global))
Chris Wilson5a97bcc2017-02-22 11:40:46 +00003803 return;
3804
3805 mutex_lock(&obj->base.dev->struct_mutex);
3806 __i915_gem_object_flush_for_display(obj);
3807 mutex_unlock(&obj->base.dev->struct_mutex);
3808}
3809
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003810/**
Chris Wilsone22d8e32017-04-12 12:01:11 +01003811 * Moves a single object to the WC read, and possibly write domain.
3812 * @obj: object to act on
3813 * @write: ask for write access or read only
3814 *
3815 * This function returns when the move is complete, including waiting on
3816 * flushes to occur.
3817 */
3818int
3819i915_gem_object_set_to_wc_domain(struct drm_i915_gem_object *obj, bool write)
3820{
3821 int ret;
3822
3823 lockdep_assert_held(&obj->base.dev->struct_mutex);
3824
3825 ret = i915_gem_object_wait(obj,
3826 I915_WAIT_INTERRUPTIBLE |
3827 I915_WAIT_LOCKED |
3828 (write ? I915_WAIT_ALL : 0),
3829 MAX_SCHEDULE_TIMEOUT,
3830 NULL);
3831 if (ret)
3832 return ret;
3833
Christian Königc0a51fd2018-02-16 13:43:38 +01003834 if (obj->write_domain == I915_GEM_DOMAIN_WC)
Chris Wilsone22d8e32017-04-12 12:01:11 +01003835 return 0;
3836
3837 /* Flush and acquire obj->pages so that we are coherent through
3838 * direct access in memory with previous cached writes through
3839 * shmemfs and that our cache domain tracking remains valid.
3840 * For example, if the obj->filp was moved to swap without us
3841 * being notified and releasing the pages, we would mistakenly
3842 * continue to assume that the obj remained out of the CPU cached
3843 * domain.
3844 */
3845 ret = i915_gem_object_pin_pages(obj);
3846 if (ret)
3847 return ret;
3848
3849 flush_write_domain(obj, ~I915_GEM_DOMAIN_WC);
3850
3851 /* Serialise direct access to this object with the barriers for
3852 * coherent writes from the GPU, by effectively invalidating the
3853 * WC domain upon first access.
3854 */
Christian Königc0a51fd2018-02-16 13:43:38 +01003855 if ((obj->read_domains & I915_GEM_DOMAIN_WC) == 0)
Chris Wilsone22d8e32017-04-12 12:01:11 +01003856 mb();
3857
3858 /* It should now be out of any other write domains, and we can update
3859 * the domain values for our changes.
3860 */
Christian Königc0a51fd2018-02-16 13:43:38 +01003861 GEM_BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_WC) != 0);
3862 obj->read_domains |= I915_GEM_DOMAIN_WC;
Chris Wilsone22d8e32017-04-12 12:01:11 +01003863 if (write) {
Christian Königc0a51fd2018-02-16 13:43:38 +01003864 obj->read_domains = I915_GEM_DOMAIN_WC;
3865 obj->write_domain = I915_GEM_DOMAIN_WC;
Chris Wilsone22d8e32017-04-12 12:01:11 +01003866 obj->mm.dirty = true;
3867 }
3868
3869 i915_gem_object_unpin_pages(obj);
3870 return 0;
3871}
3872
3873/**
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003874 * Moves a single object to the GTT read, and possibly write domain.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01003875 * @obj: object to act on
3876 * @write: ask for write access or read only
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003877 *
3878 * This function returns when the move is complete, including waiting on
3879 * flushes to occur.
3880 */
Jesse Barnes79e53942008-11-07 14:24:08 -08003881int
Chris Wilson20217462010-11-23 15:26:33 +00003882i915_gem_object_set_to_gtt_domain(struct drm_i915_gem_object *obj, bool write)
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003883{
Eric Anholte47c68e2008-11-14 13:35:19 -08003884 int ret;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003885
Chris Wilsone95433c2016-10-28 13:58:27 +01003886 lockdep_assert_held(&obj->base.dev->struct_mutex);
Chris Wilson4c7d62c2016-10-28 13:58:32 +01003887
Chris Wilsone95433c2016-10-28 13:58:27 +01003888 ret = i915_gem_object_wait(obj,
3889 I915_WAIT_INTERRUPTIBLE |
3890 I915_WAIT_LOCKED |
3891 (write ? I915_WAIT_ALL : 0),
3892 MAX_SCHEDULE_TIMEOUT,
3893 NULL);
Chris Wilson88241782011-01-07 17:09:48 +00003894 if (ret)
3895 return ret;
3896
Christian Königc0a51fd2018-02-16 13:43:38 +01003897 if (obj->write_domain == I915_GEM_DOMAIN_GTT)
Chris Wilsonc13d87e2016-07-20 09:21:15 +01003898 return 0;
3899
Chris Wilson43566de2015-01-02 16:29:29 +05303900 /* Flush and acquire obj->pages so that we are coherent through
3901 * direct access in memory with previous cached writes through
3902 * shmemfs and that our cache domain tracking remains valid.
3903 * For example, if the obj->filp was moved to swap without us
3904 * being notified and releasing the pages, we would mistakenly
3905 * continue to assume that the obj remained out of the CPU cached
3906 * domain.
3907 */
Chris Wilsona4f5ea62016-10-28 13:58:35 +01003908 ret = i915_gem_object_pin_pages(obj);
Chris Wilson43566de2015-01-02 16:29:29 +05303909 if (ret)
3910 return ret;
3911
Chris Wilsonef749212017-04-12 12:01:10 +01003912 flush_write_domain(obj, ~I915_GEM_DOMAIN_GTT);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003913
Chris Wilsond0a57782012-10-09 19:24:37 +01003914 /* Serialise direct access to this object with the barriers for
3915 * coherent writes from the GPU, by effectively invalidating the
3916 * GTT domain upon first access.
3917 */
Christian Königc0a51fd2018-02-16 13:43:38 +01003918 if ((obj->read_domains & I915_GEM_DOMAIN_GTT) == 0)
Chris Wilsond0a57782012-10-09 19:24:37 +01003919 mb();
3920
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003921 /* It should now be out of any other write domains, and we can update
3922 * the domain values for our changes.
3923 */
Christian Königc0a51fd2018-02-16 13:43:38 +01003924 GEM_BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_GTT) != 0);
3925 obj->read_domains |= I915_GEM_DOMAIN_GTT;
Eric Anholte47c68e2008-11-14 13:35:19 -08003926 if (write) {
Christian Königc0a51fd2018-02-16 13:43:38 +01003927 obj->read_domains = I915_GEM_DOMAIN_GTT;
3928 obj->write_domain = I915_GEM_DOMAIN_GTT;
Chris Wilsona4f5ea62016-10-28 13:58:35 +01003929 obj->mm.dirty = true;
Eric Anholte47c68e2008-11-14 13:35:19 -08003930 }
3931
Chris Wilsona4f5ea62016-10-28 13:58:35 +01003932 i915_gem_object_unpin_pages(obj);
Eric Anholte47c68e2008-11-14 13:35:19 -08003933 return 0;
3934}
3935
Chris Wilsonef55f922015-10-09 14:11:27 +01003936/**
3937 * Changes the cache-level of an object across all VMA.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01003938 * @obj: object to act on
3939 * @cache_level: new cache level to set for the object
Chris Wilsonef55f922015-10-09 14:11:27 +01003940 *
3941 * After this function returns, the object will be in the new cache-level
3942 * across all GTT and the contents of the backing storage will be coherent,
3943 * with respect to the new cache-level. In order to keep the backing storage
3944 * coherent for all users, we only allow a single cache level to be set
3945 * globally on the object and prevent it from being changed whilst the
3946 * hardware is reading from the object. That is if the object is currently
3947 * on the scanout it will be set to uncached (or equivalent display
3948 * cache coherency) and all non-MOCS GPU access will also be uncached so
3949 * that all direct access to the scanout remains coherent.
3950 */
Chris Wilsone4ffd172011-04-04 09:44:39 +01003951int i915_gem_object_set_cache_level(struct drm_i915_gem_object *obj,
3952 enum i915_cache_level cache_level)
3953{
Chris Wilsonaa653a62016-08-04 07:52:27 +01003954 struct i915_vma *vma;
Chris Wilsona6a7cc42016-11-18 21:17:46 +00003955 int ret;
Chris Wilsone4ffd172011-04-04 09:44:39 +01003956
Chris Wilson4c7d62c2016-10-28 13:58:32 +01003957 lockdep_assert_held(&obj->base.dev->struct_mutex);
3958
Chris Wilsone4ffd172011-04-04 09:44:39 +01003959 if (obj->cache_level == cache_level)
Chris Wilsona6a7cc42016-11-18 21:17:46 +00003960 return 0;
Chris Wilsone4ffd172011-04-04 09:44:39 +01003961
Chris Wilsonef55f922015-10-09 14:11:27 +01003962 /* Inspect the list of currently bound VMA and unbind any that would
3963 * be invalid given the new cache-level. This is principally to
3964 * catch the issue of the CS prefetch crossing page boundaries and
3965 * reading an invalid PTE on older architectures.
3966 */
Chris Wilsonaa653a62016-08-04 07:52:27 +01003967restart:
3968 list_for_each_entry(vma, &obj->vma_list, obj_link) {
Chris Wilsonef55f922015-10-09 14:11:27 +01003969 if (!drm_mm_node_allocated(&vma->node))
3970 continue;
3971
Chris Wilson20dfbde2016-08-04 16:32:30 +01003972 if (i915_vma_is_pinned(vma)) {
Chris Wilsonef55f922015-10-09 14:11:27 +01003973 DRM_DEBUG("can not change the cache level of pinned objects\n");
3974 return -EBUSY;
3975 }
3976
Chris Wilson010e3e62017-12-06 12:49:13 +00003977 if (!i915_vma_is_closed(vma) &&
3978 i915_gem_valid_gtt_space(vma, cache_level))
Chris Wilsonaa653a62016-08-04 07:52:27 +01003979 continue;
3980
3981 ret = i915_vma_unbind(vma);
3982 if (ret)
3983 return ret;
3984
3985 /* As unbinding may affect other elements in the
3986 * obj->vma_list (due to side-effects from retiring
3987 * an active vma), play safe and restart the iterator.
3988 */
3989 goto restart;
Chris Wilson42d6ab42012-07-26 11:49:32 +01003990 }
3991
Chris Wilsonef55f922015-10-09 14:11:27 +01003992 /* We can reuse the existing drm_mm nodes but need to change the
3993 * cache-level on the PTE. We could simply unbind them all and
3994 * rebind with the correct cache-level on next use. However since
3995 * we already have a valid slot, dma mapping, pages etc, we may as
3996 * rewrite the PTE in the belief that doing so tramples upon less
3997 * state and so involves less work.
3998 */
Chris Wilson15717de2016-08-04 07:52:26 +01003999 if (obj->bind_count) {
Chris Wilsonef55f922015-10-09 14:11:27 +01004000 /* Before we change the PTE, the GPU must not be accessing it.
4001 * If we wait upon the object, we know that all the bound
4002 * VMA are no longer active.
4003 */
Chris Wilsone95433c2016-10-28 13:58:27 +01004004 ret = i915_gem_object_wait(obj,
4005 I915_WAIT_INTERRUPTIBLE |
4006 I915_WAIT_LOCKED |
4007 I915_WAIT_ALL,
4008 MAX_SCHEDULE_TIMEOUT,
4009 NULL);
Chris Wilsone4ffd172011-04-04 09:44:39 +01004010 if (ret)
4011 return ret;
4012
Tvrtko Ursulin0031fb92016-11-04 14:42:44 +00004013 if (!HAS_LLC(to_i915(obj->base.dev)) &&
4014 cache_level != I915_CACHE_NONE) {
Chris Wilsonef55f922015-10-09 14:11:27 +01004015 /* Access to snoopable pages through the GTT is
4016 * incoherent and on some machines causes a hard
4017 * lockup. Relinquish the CPU mmaping to force
4018 * userspace to refault in the pages and we can
4019 * then double check if the GTT mapping is still
4020 * valid for that pointer access.
4021 */
4022 i915_gem_release_mmap(obj);
Chris Wilsone4ffd172011-04-04 09:44:39 +01004023
Chris Wilsonef55f922015-10-09 14:11:27 +01004024 /* As we no longer need a fence for GTT access,
4025 * we can relinquish it now (and so prevent having
4026 * to steal a fence from someone else on the next
4027 * fence request). Note GPU activity would have
4028 * dropped the fence as all snoopable access is
4029 * supposed to be linear.
4030 */
Chris Wilsone2189dd2017-12-07 21:14:07 +00004031 for_each_ggtt_vma(vma, obj) {
Chris Wilson49ef5292016-08-18 17:17:00 +01004032 ret = i915_vma_put_fence(vma);
4033 if (ret)
4034 return ret;
4035 }
Chris Wilsonef55f922015-10-09 14:11:27 +01004036 } else {
4037 /* We either have incoherent backing store and
4038 * so no GTT access or the architecture is fully
4039 * coherent. In such cases, existing GTT mmaps
4040 * ignore the cache bit in the PTE and we can
4041 * rewrite it without confusing the GPU or having
4042 * to force userspace to fault back in its mmaps.
4043 */
Chris Wilsone4ffd172011-04-04 09:44:39 +01004044 }
4045
Chris Wilson1c7f4bc2016-02-26 11:03:19 +00004046 list_for_each_entry(vma, &obj->vma_list, obj_link) {
Chris Wilsonef55f922015-10-09 14:11:27 +01004047 if (!drm_mm_node_allocated(&vma->node))
4048 continue;
4049
4050 ret = i915_vma_bind(vma, cache_level, PIN_UPDATE);
4051 if (ret)
4052 return ret;
4053 }
Chris Wilsone4ffd172011-04-04 09:44:39 +01004054 }
4055
Chris Wilson1c7f4bc2016-02-26 11:03:19 +00004056 list_for_each_entry(vma, &obj->vma_list, obj_link)
Chris Wilson2c225692013-08-09 12:26:45 +01004057 vma->node.color = cache_level;
Chris Wilsonb8f55be2017-08-11 12:11:16 +01004058 i915_gem_object_set_cache_coherency(obj, cache_level);
Chris Wilsone27ab732017-06-15 13:38:49 +01004059 obj->cache_dirty = true; /* Always invalidate stale cachelines */
Chris Wilson2c225692013-08-09 12:26:45 +01004060
Chris Wilsone4ffd172011-04-04 09:44:39 +01004061 return 0;
4062}
4063
Ben Widawsky199adf42012-09-21 17:01:20 -07004064int i915_gem_get_caching_ioctl(struct drm_device *dev, void *data,
4065 struct drm_file *file)
Chris Wilsone6994ae2012-07-10 10:27:08 +01004066{
Ben Widawsky199adf42012-09-21 17:01:20 -07004067 struct drm_i915_gem_caching *args = data;
Chris Wilsone6994ae2012-07-10 10:27:08 +01004068 struct drm_i915_gem_object *obj;
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004069 int err = 0;
Chris Wilsone6994ae2012-07-10 10:27:08 +01004070
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004071 rcu_read_lock();
4072 obj = i915_gem_object_lookup_rcu(file, args->handle);
4073 if (!obj) {
4074 err = -ENOENT;
4075 goto out;
4076 }
Chris Wilsone6994ae2012-07-10 10:27:08 +01004077
Chris Wilson651d7942013-08-08 14:41:10 +01004078 switch (obj->cache_level) {
4079 case I915_CACHE_LLC:
4080 case I915_CACHE_L3_LLC:
4081 args->caching = I915_CACHING_CACHED;
4082 break;
4083
Chris Wilson4257d3b2013-08-08 14:41:11 +01004084 case I915_CACHE_WT:
4085 args->caching = I915_CACHING_DISPLAY;
4086 break;
4087
Chris Wilson651d7942013-08-08 14:41:10 +01004088 default:
4089 args->caching = I915_CACHING_NONE;
4090 break;
4091 }
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004092out:
4093 rcu_read_unlock();
4094 return err;
Chris Wilsone6994ae2012-07-10 10:27:08 +01004095}
4096
Ben Widawsky199adf42012-09-21 17:01:20 -07004097int i915_gem_set_caching_ioctl(struct drm_device *dev, void *data,
4098 struct drm_file *file)
Chris Wilsone6994ae2012-07-10 10:27:08 +01004099{
Chris Wilson9c870d02016-10-24 13:42:15 +01004100 struct drm_i915_private *i915 = to_i915(dev);
Ben Widawsky199adf42012-09-21 17:01:20 -07004101 struct drm_i915_gem_caching *args = data;
Chris Wilsone6994ae2012-07-10 10:27:08 +01004102 struct drm_i915_gem_object *obj;
4103 enum i915_cache_level level;
Chris Wilsond65415d2017-01-19 08:22:10 +00004104 int ret = 0;
Chris Wilsone6994ae2012-07-10 10:27:08 +01004105
Ben Widawsky199adf42012-09-21 17:01:20 -07004106 switch (args->caching) {
4107 case I915_CACHING_NONE:
Chris Wilsone6994ae2012-07-10 10:27:08 +01004108 level = I915_CACHE_NONE;
4109 break;
Ben Widawsky199adf42012-09-21 17:01:20 -07004110 case I915_CACHING_CACHED:
Imre Deake5756c12015-08-14 18:43:30 +03004111 /*
4112 * Due to a HW issue on BXT A stepping, GPU stores via a
4113 * snooped mapping may leave stale data in a corresponding CPU
4114 * cacheline, whereas normally such cachelines would get
4115 * invalidated.
4116 */
Chris Wilson9c870d02016-10-24 13:42:15 +01004117 if (!HAS_LLC(i915) && !HAS_SNOOP(i915))
Imre Deake5756c12015-08-14 18:43:30 +03004118 return -ENODEV;
4119
Chris Wilsone6994ae2012-07-10 10:27:08 +01004120 level = I915_CACHE_LLC;
4121 break;
Chris Wilson4257d3b2013-08-08 14:41:11 +01004122 case I915_CACHING_DISPLAY:
Chris Wilson9c870d02016-10-24 13:42:15 +01004123 level = HAS_WT(i915) ? I915_CACHE_WT : I915_CACHE_NONE;
Chris Wilson4257d3b2013-08-08 14:41:11 +01004124 break;
Chris Wilsone6994ae2012-07-10 10:27:08 +01004125 default:
4126 return -EINVAL;
4127 }
4128
Chris Wilsond65415d2017-01-19 08:22:10 +00004129 obj = i915_gem_object_lookup(file, args->handle);
4130 if (!obj)
4131 return -ENOENT;
4132
Tina Zhanga03f3952017-11-14 10:25:13 +00004133 /*
4134 * The caching mode of proxy object is handled by its generator, and
4135 * not allowed to be changed by userspace.
4136 */
4137 if (i915_gem_object_is_proxy(obj)) {
4138 ret = -ENXIO;
4139 goto out;
4140 }
4141
Chris Wilsond65415d2017-01-19 08:22:10 +00004142 if (obj->cache_level == level)
4143 goto out;
4144
4145 ret = i915_gem_object_wait(obj,
4146 I915_WAIT_INTERRUPTIBLE,
4147 MAX_SCHEDULE_TIMEOUT,
4148 to_rps_client(file));
4149 if (ret)
4150 goto out;
4151
Ben Widawsky3bc29132012-09-26 16:15:20 -07004152 ret = i915_mutex_lock_interruptible(dev);
4153 if (ret)
Chris Wilsond65415d2017-01-19 08:22:10 +00004154 goto out;
Chris Wilsone6994ae2012-07-10 10:27:08 +01004155
4156 ret = i915_gem_object_set_cache_level(obj, level);
Chris Wilsone6994ae2012-07-10 10:27:08 +01004157 mutex_unlock(&dev->struct_mutex);
Chris Wilsond65415d2017-01-19 08:22:10 +00004158
4159out:
4160 i915_gem_object_put(obj);
Chris Wilsone6994ae2012-07-10 10:27:08 +01004161 return ret;
4162}
4163
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08004164/*
Dhinakaran Pandiyan07bcd992018-03-06 19:34:18 -08004165 * Prepare buffer for display plane (scanout, cursors, etc). Can be called from
4166 * an uninterruptible phase (modesetting) and allows any flushes to be pipelined
4167 * (for pageflips). We only flush the caches while preparing the buffer for
4168 * display, the callers are responsible for frontbuffer flush.
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08004169 */
Chris Wilson058d88c2016-08-15 10:49:06 +01004170struct i915_vma *
Chris Wilson2da3b9b2011-04-14 09:41:17 +01004171i915_gem_object_pin_to_display_plane(struct drm_i915_gem_object *obj,
4172 u32 alignment,
Chris Wilson59354852018-02-20 13:42:06 +00004173 const struct i915_ggtt_view *view,
4174 unsigned int flags)
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08004175{
Chris Wilson058d88c2016-08-15 10:49:06 +01004176 struct i915_vma *vma;
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08004177 int ret;
4178
Chris Wilson4c7d62c2016-10-28 13:58:32 +01004179 lockdep_assert_held(&obj->base.dev->struct_mutex);
4180
Chris Wilsonbd3d2252017-10-13 21:26:14 +01004181 /* Mark the global pin early so that we account for the
Chris Wilsoncc98b412013-08-09 12:25:09 +01004182 * display coherency whilst setting up the cache domains.
4183 */
Chris Wilsonbd3d2252017-10-13 21:26:14 +01004184 obj->pin_global++;
Chris Wilsoncc98b412013-08-09 12:25:09 +01004185
Eric Anholta7ef0642011-03-29 16:59:54 -07004186 /* The display engine is not coherent with the LLC cache on gen6. As
4187 * a result, we make sure that the pinning that is about to occur is
4188 * done with uncached PTEs. This is lowest common denominator for all
4189 * chipsets.
4190 *
4191 * However for gen6+, we could do better by using the GFDT bit instead
4192 * of uncaching, which would allow us to flush all the LLC-cached data
4193 * with that bit in the PTE to main memory with just one PIPE_CONTROL.
4194 */
Chris Wilson651d7942013-08-08 14:41:10 +01004195 ret = i915_gem_object_set_cache_level(obj,
Tvrtko Ursulin86527442016-10-13 11:03:00 +01004196 HAS_WT(to_i915(obj->base.dev)) ?
4197 I915_CACHE_WT : I915_CACHE_NONE);
Chris Wilson058d88c2016-08-15 10:49:06 +01004198 if (ret) {
4199 vma = ERR_PTR(ret);
Chris Wilsonbd3d2252017-10-13 21:26:14 +01004200 goto err_unpin_global;
Chris Wilson058d88c2016-08-15 10:49:06 +01004201 }
Eric Anholta7ef0642011-03-29 16:59:54 -07004202
Chris Wilson2da3b9b2011-04-14 09:41:17 +01004203 /* As the user may map the buffer once pinned in the display plane
4204 * (e.g. libkms for the bootup splash), we have to ensure that we
Chris Wilson2efb8132016-08-18 17:17:06 +01004205 * always use map_and_fenceable for all scanout buffers. However,
4206 * it may simply be too big to fit into mappable, in which case
4207 * put it anyway and hope that userspace can cope (but always first
4208 * try to preserve the existing ABI).
Chris Wilson2da3b9b2011-04-14 09:41:17 +01004209 */
Chris Wilson2efb8132016-08-18 17:17:06 +01004210 vma = ERR_PTR(-ENOSPC);
Chris Wilson59354852018-02-20 13:42:06 +00004211 if ((flags & PIN_MAPPABLE) == 0 &&
4212 (!view || view->type == I915_GGTT_VIEW_NORMAL))
Chris Wilson2efb8132016-08-18 17:17:06 +01004213 vma = i915_gem_object_ggtt_pin(obj, view, 0, alignment,
Chris Wilson59354852018-02-20 13:42:06 +00004214 flags |
4215 PIN_MAPPABLE |
4216 PIN_NONBLOCK);
4217 if (IS_ERR(vma))
Chris Wilson767a2222016-11-07 11:01:28 +00004218 vma = i915_gem_object_ggtt_pin(obj, view, 0, alignment, flags);
Chris Wilson058d88c2016-08-15 10:49:06 +01004219 if (IS_ERR(vma))
Chris Wilsonbd3d2252017-10-13 21:26:14 +01004220 goto err_unpin_global;
Chris Wilson2da3b9b2011-04-14 09:41:17 +01004221
Chris Wilsond8923dc2016-08-18 17:17:07 +01004222 vma->display_alignment = max_t(u64, vma->display_alignment, alignment);
4223
Chris Wilson5a97bcc2017-02-22 11:40:46 +00004224 __i915_gem_object_flush_for_display(obj);
Chris Wilsonb118c1e2010-05-27 13:18:14 +01004225
Chris Wilson2da3b9b2011-04-14 09:41:17 +01004226 /* It should now be out of any other write domains, and we can update
4227 * the domain values for our changes.
4228 */
Christian Königc0a51fd2018-02-16 13:43:38 +01004229 obj->read_domains |= I915_GEM_DOMAIN_GTT;
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08004230
Chris Wilson058d88c2016-08-15 10:49:06 +01004231 return vma;
Chris Wilsoncc98b412013-08-09 12:25:09 +01004232
Chris Wilsonbd3d2252017-10-13 21:26:14 +01004233err_unpin_global:
4234 obj->pin_global--;
Chris Wilson058d88c2016-08-15 10:49:06 +01004235 return vma;
Chris Wilsoncc98b412013-08-09 12:25:09 +01004236}
4237
4238void
Chris Wilson058d88c2016-08-15 10:49:06 +01004239i915_gem_object_unpin_from_display_plane(struct i915_vma *vma)
Chris Wilsoncc98b412013-08-09 12:25:09 +01004240{
Chris Wilson49d73912016-11-29 09:50:08 +00004241 lockdep_assert_held(&vma->vm->i915->drm.struct_mutex);
Chris Wilson4c7d62c2016-10-28 13:58:32 +01004242
Chris Wilsonbd3d2252017-10-13 21:26:14 +01004243 if (WARN_ON(vma->obj->pin_global == 0))
Tvrtko Ursulin8a0c39b2015-04-13 11:50:09 +01004244 return;
4245
Chris Wilsonbd3d2252017-10-13 21:26:14 +01004246 if (--vma->obj->pin_global == 0)
Chris Wilsonf51455d2017-01-10 14:47:34 +00004247 vma->display_alignment = I915_GTT_MIN_ALIGNMENT;
Tvrtko Ursuline6617332015-03-23 11:10:33 +00004248
Chris Wilson383d5822016-08-18 17:17:08 +01004249 /* Bump the LRU to try and avoid premature eviction whilst flipping */
Chris Wilsonbefedbb2017-01-19 19:26:55 +00004250 i915_gem_object_bump_inactive_ggtt(vma->obj);
Chris Wilson383d5822016-08-18 17:17:08 +01004251
Chris Wilson058d88c2016-08-15 10:49:06 +01004252 i915_vma_unpin(vma);
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08004253}
4254
Eric Anholte47c68e2008-11-14 13:35:19 -08004255/**
4256 * Moves a single object to the CPU read, and possibly write domain.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01004257 * @obj: object to act on
4258 * @write: requesting write or read-only access
Eric Anholte47c68e2008-11-14 13:35:19 -08004259 *
4260 * This function returns when the move is complete, including waiting on
4261 * flushes to occur.
4262 */
Chris Wilsondabdfe02012-03-26 10:10:27 +02004263int
Chris Wilson919926a2010-11-12 13:42:53 +00004264i915_gem_object_set_to_cpu_domain(struct drm_i915_gem_object *obj, bool write)
Eric Anholte47c68e2008-11-14 13:35:19 -08004265{
Eric Anholte47c68e2008-11-14 13:35:19 -08004266 int ret;
4267
Chris Wilsone95433c2016-10-28 13:58:27 +01004268 lockdep_assert_held(&obj->base.dev->struct_mutex);
Chris Wilson4c7d62c2016-10-28 13:58:32 +01004269
Chris Wilsone95433c2016-10-28 13:58:27 +01004270 ret = i915_gem_object_wait(obj,
4271 I915_WAIT_INTERRUPTIBLE |
4272 I915_WAIT_LOCKED |
4273 (write ? I915_WAIT_ALL : 0),
4274 MAX_SCHEDULE_TIMEOUT,
4275 NULL);
Chris Wilson88241782011-01-07 17:09:48 +00004276 if (ret)
4277 return ret;
4278
Chris Wilsonef749212017-04-12 12:01:10 +01004279 flush_write_domain(obj, ~I915_GEM_DOMAIN_CPU);
Eric Anholte47c68e2008-11-14 13:35:19 -08004280
Eric Anholte47c68e2008-11-14 13:35:19 -08004281 /* Flush the CPU cache if it's still invalid. */
Christian Königc0a51fd2018-02-16 13:43:38 +01004282 if ((obj->read_domains & I915_GEM_DOMAIN_CPU) == 0) {
Chris Wilson57822dc2017-02-22 11:40:48 +00004283 i915_gem_clflush_object(obj, I915_CLFLUSH_SYNC);
Christian Königc0a51fd2018-02-16 13:43:38 +01004284 obj->read_domains |= I915_GEM_DOMAIN_CPU;
Eric Anholte47c68e2008-11-14 13:35:19 -08004285 }
4286
4287 /* It should now be out of any other write domains, and we can update
4288 * the domain values for our changes.
4289 */
Christian Königc0a51fd2018-02-16 13:43:38 +01004290 GEM_BUG_ON(obj->write_domain & ~I915_GEM_DOMAIN_CPU);
Eric Anholte47c68e2008-11-14 13:35:19 -08004291
4292 /* If we're writing through the CPU, then the GPU read domains will
4293 * need to be invalidated at next use.
4294 */
Chris Wilsone27ab732017-06-15 13:38:49 +01004295 if (write)
4296 __start_cpu_write(obj);
Eric Anholt2ef7eea2008-11-10 10:53:25 -08004297
4298 return 0;
4299}
4300
Eric Anholt673a3942008-07-30 12:06:12 -07004301/* Throttle our rendering by waiting until the ring has completed our requests
4302 * emitted over 20 msec ago.
4303 *
Eric Anholtb9624422009-06-03 07:27:35 +00004304 * Note that if we were to use the current jiffies each time around the loop,
4305 * we wouldn't escape the function with any frames outstanding if the time to
4306 * render a frame was over 20ms.
4307 *
Eric Anholt673a3942008-07-30 12:06:12 -07004308 * This should get us reasonable parallelism between CPU and GPU but also
4309 * relatively low latency when blocking on a particular request to finish.
4310 */
4311static int
Chris Wilsonf787a5f2010-09-24 16:02:42 +01004312i915_gem_ring_throttle(struct drm_device *dev, struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07004313{
Chris Wilsonfac5e232016-07-04 11:34:36 +01004314 struct drm_i915_private *dev_priv = to_i915(dev);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01004315 struct drm_i915_file_private *file_priv = file->driver_priv;
Chris Wilsond0bc54f2015-05-21 21:01:48 +01004316 unsigned long recent_enough = jiffies - DRM_I915_THROTTLE_JIFFIES;
Chris Wilsone61e0f52018-02-21 09:56:36 +00004317 struct i915_request *request, *target = NULL;
Chris Wilsone95433c2016-10-28 13:58:27 +01004318 long ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004319
Chris Wilsonf4457ae2016-04-13 17:35:08 +01004320 /* ABI: return -EIO if already wedged */
4321 if (i915_terminally_wedged(&dev_priv->gpu_error))
4322 return -EIO;
Chris Wilsone110e8d2011-01-26 15:39:14 +00004323
Chris Wilson1c255952010-09-26 11:03:27 +01004324 spin_lock(&file_priv->mm.lock);
Chris Wilsonc8659ef2017-03-02 12:25:25 +00004325 list_for_each_entry(request, &file_priv->mm.request_list, client_link) {
Eric Anholtb9624422009-06-03 07:27:35 +00004326 if (time_after_eq(request->emitted_jiffies, recent_enough))
4327 break;
4328
Chris Wilsonc8659ef2017-03-02 12:25:25 +00004329 if (target) {
4330 list_del(&target->client_link);
4331 target->file_priv = NULL;
4332 }
John Harrisonfcfa423c2015-05-29 17:44:12 +01004333
John Harrison54fb2412014-11-24 18:49:27 +00004334 target = request;
Eric Anholtb9624422009-06-03 07:27:35 +00004335 }
John Harrisonff865882014-11-24 18:49:28 +00004336 if (target)
Chris Wilsone61e0f52018-02-21 09:56:36 +00004337 i915_request_get(target);
Chris Wilson1c255952010-09-26 11:03:27 +01004338 spin_unlock(&file_priv->mm.lock);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01004339
John Harrison54fb2412014-11-24 18:49:27 +00004340 if (target == NULL)
Chris Wilsonf787a5f2010-09-24 16:02:42 +01004341 return 0;
4342
Chris Wilsone61e0f52018-02-21 09:56:36 +00004343 ret = i915_request_wait(target,
Chris Wilsone95433c2016-10-28 13:58:27 +01004344 I915_WAIT_INTERRUPTIBLE,
4345 MAX_SCHEDULE_TIMEOUT);
Chris Wilsone61e0f52018-02-21 09:56:36 +00004346 i915_request_put(target);
John Harrisonff865882014-11-24 18:49:28 +00004347
Chris Wilsone95433c2016-10-28 13:58:27 +01004348 return ret < 0 ? ret : 0;
Eric Anholt673a3942008-07-30 12:06:12 -07004349}
4350
Chris Wilson058d88c2016-08-15 10:49:06 +01004351struct i915_vma *
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02004352i915_gem_object_ggtt_pin(struct drm_i915_gem_object *obj,
4353 const struct i915_ggtt_view *view,
Chris Wilson91b2db62016-08-04 16:32:23 +01004354 u64 size,
Chris Wilson2ffffd02016-08-04 16:32:22 +01004355 u64 alignment,
4356 u64 flags)
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02004357{
Chris Wilsonad16d2e2016-10-13 09:55:04 +01004358 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
4359 struct i915_address_space *vm = &dev_priv->ggtt.base;
Chris Wilson59bfa122016-08-04 16:32:31 +01004360 struct i915_vma *vma;
4361 int ret;
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03004362
Chris Wilson4c7d62c2016-10-28 13:58:32 +01004363 lockdep_assert_held(&obj->base.dev->struct_mutex);
4364
Chris Wilsonac87a6fd2018-02-20 13:42:05 +00004365 if (flags & PIN_MAPPABLE &&
4366 (!view || view->type == I915_GGTT_VIEW_NORMAL)) {
Chris Wilson43ae70d92017-10-09 09:44:01 +01004367 /* If the required space is larger than the available
4368 * aperture, we will not able to find a slot for the
4369 * object and unbinding the object now will be in
4370 * vain. Worse, doing so may cause us to ping-pong
4371 * the object in and out of the Global GTT and
4372 * waste a lot of cycles under the mutex.
4373 */
4374 if (obj->base.size > dev_priv->ggtt.mappable_end)
4375 return ERR_PTR(-E2BIG);
4376
4377 /* If NONBLOCK is set the caller is optimistically
4378 * trying to cache the full object within the mappable
4379 * aperture, and *must* have a fallback in place for
4380 * situations where we cannot bind the object. We
4381 * can be a little more lax here and use the fallback
4382 * more often to avoid costly migrations of ourselves
4383 * and other objects within the aperture.
4384 *
4385 * Half-the-aperture is used as a simple heuristic.
4386 * More interesting would to do search for a free
4387 * block prior to making the commitment to unbind.
4388 * That caters for the self-harm case, and with a
4389 * little more heuristics (e.g. NOFAULT, NOEVICT)
4390 * we could try to minimise harm to others.
4391 */
4392 if (flags & PIN_NONBLOCK &&
4393 obj->base.size > dev_priv->ggtt.mappable_end / 2)
4394 return ERR_PTR(-ENOSPC);
4395 }
4396
Chris Wilson718659a2017-01-16 15:21:28 +00004397 vma = i915_vma_instance(obj, vm, view);
Chris Wilsone0216b72017-01-19 19:26:57 +00004398 if (unlikely(IS_ERR(vma)))
Chris Wilson058d88c2016-08-15 10:49:06 +01004399 return vma;
Chris Wilson59bfa122016-08-04 16:32:31 +01004400
4401 if (i915_vma_misplaced(vma, size, alignment, flags)) {
Chris Wilson43ae70d92017-10-09 09:44:01 +01004402 if (flags & PIN_NONBLOCK) {
4403 if (i915_vma_is_pinned(vma) || i915_vma_is_active(vma))
4404 return ERR_PTR(-ENOSPC);
Chris Wilson59bfa122016-08-04 16:32:31 +01004405
Chris Wilson43ae70d92017-10-09 09:44:01 +01004406 if (flags & PIN_MAPPABLE &&
Chris Wilson944397f2017-01-09 16:16:11 +00004407 vma->fence_size > dev_priv->ggtt.mappable_end / 2)
Chris Wilsonad16d2e2016-10-13 09:55:04 +01004408 return ERR_PTR(-ENOSPC);
4409 }
4410
Chris Wilson59bfa122016-08-04 16:32:31 +01004411 WARN(i915_vma_is_pinned(vma),
4412 "bo is already pinned in ggtt with incorrect alignment:"
Chris Wilson05a20d02016-08-18 17:16:55 +01004413 " offset=%08x, req.alignment=%llx,"
4414 " req.map_and_fenceable=%d, vma->map_and_fenceable=%d\n",
4415 i915_ggtt_offset(vma), alignment,
Chris Wilson59bfa122016-08-04 16:32:31 +01004416 !!(flags & PIN_MAPPABLE),
Chris Wilson05a20d02016-08-18 17:16:55 +01004417 i915_vma_is_map_and_fenceable(vma));
Chris Wilson59bfa122016-08-04 16:32:31 +01004418 ret = i915_vma_unbind(vma);
4419 if (ret)
Chris Wilson058d88c2016-08-15 10:49:06 +01004420 return ERR_PTR(ret);
Chris Wilson59bfa122016-08-04 16:32:31 +01004421 }
4422
Chris Wilson058d88c2016-08-15 10:49:06 +01004423 ret = i915_vma_pin(vma, size, alignment, flags | PIN_GLOBAL);
4424 if (ret)
4425 return ERR_PTR(ret);
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02004426
Chris Wilson058d88c2016-08-15 10:49:06 +01004427 return vma;
Eric Anholt673a3942008-07-30 12:06:12 -07004428}
4429
Chris Wilsonedf6b762016-08-09 09:23:33 +01004430static __always_inline unsigned int __busy_read_flag(unsigned int id)
Chris Wilson3fdc13c2016-08-05 10:14:18 +01004431{
4432 /* Note that we could alias engines in the execbuf API, but
4433 * that would be very unwise as it prevents userspace from
4434 * fine control over engine selection. Ahem.
4435 *
4436 * This should be something like EXEC_MAX_ENGINE instead of
4437 * I915_NUM_ENGINES.
4438 */
4439 BUILD_BUG_ON(I915_NUM_ENGINES > 16);
4440 return 0x10000 << id;
4441}
4442
4443static __always_inline unsigned int __busy_write_id(unsigned int id)
4444{
Chris Wilson70cb4722016-08-09 18:08:25 +01004445 /* The uABI guarantees an active writer is also amongst the read
4446 * engines. This would be true if we accessed the activity tracking
4447 * under the lock, but as we perform the lookup of the object and
4448 * its activity locklessly we can not guarantee that the last_write
4449 * being active implies that we have set the same engine flag from
4450 * last_read - hence we always set both read and write busy for
4451 * last_write.
4452 */
4453 return id | __busy_read_flag(id);
Chris Wilson3fdc13c2016-08-05 10:14:18 +01004454}
4455
Chris Wilsonedf6b762016-08-09 09:23:33 +01004456static __always_inline unsigned int
Chris Wilsond07f0e52016-10-28 13:58:44 +01004457__busy_set_if_active(const struct dma_fence *fence,
Chris Wilson3fdc13c2016-08-05 10:14:18 +01004458 unsigned int (*flag)(unsigned int id))
4459{
Chris Wilsone61e0f52018-02-21 09:56:36 +00004460 struct i915_request *rq;
Chris Wilson12555012016-08-16 09:50:40 +01004461
Chris Wilsond07f0e52016-10-28 13:58:44 +01004462 /* We have to check the current hw status of the fence as the uABI
4463 * guarantees forward progress. We could rely on the idle worker
4464 * to eventually flush us, but to minimise latency just ask the
4465 * hardware.
4466 *
4467 * Note we only report on the status of native fences.
4468 */
4469 if (!dma_fence_is_i915(fence))
Chris Wilson12555012016-08-16 09:50:40 +01004470 return 0;
4471
Chris Wilsond07f0e52016-10-28 13:58:44 +01004472 /* opencode to_request() in order to avoid const warnings */
Chris Wilsone61e0f52018-02-21 09:56:36 +00004473 rq = container_of(fence, struct i915_request, fence);
4474 if (i915_request_completed(rq))
Chris Wilsond07f0e52016-10-28 13:58:44 +01004475 return 0;
4476
Chris Wilson1d39f282017-04-11 13:43:06 +01004477 return flag(rq->engine->uabi_id);
Chris Wilson3fdc13c2016-08-05 10:14:18 +01004478}
4479
Chris Wilsonedf6b762016-08-09 09:23:33 +01004480static __always_inline unsigned int
Chris Wilsond07f0e52016-10-28 13:58:44 +01004481busy_check_reader(const struct dma_fence *fence)
Chris Wilson3fdc13c2016-08-05 10:14:18 +01004482{
Chris Wilsond07f0e52016-10-28 13:58:44 +01004483 return __busy_set_if_active(fence, __busy_read_flag);
Chris Wilson3fdc13c2016-08-05 10:14:18 +01004484}
4485
Chris Wilsonedf6b762016-08-09 09:23:33 +01004486static __always_inline unsigned int
Chris Wilsond07f0e52016-10-28 13:58:44 +01004487busy_check_writer(const struct dma_fence *fence)
Chris Wilson3fdc13c2016-08-05 10:14:18 +01004488{
Chris Wilsond07f0e52016-10-28 13:58:44 +01004489 if (!fence)
4490 return 0;
4491
4492 return __busy_set_if_active(fence, __busy_write_id);
Chris Wilson3fdc13c2016-08-05 10:14:18 +01004493}
4494
Eric Anholt673a3942008-07-30 12:06:12 -07004495int
Eric Anholt673a3942008-07-30 12:06:12 -07004496i915_gem_busy_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00004497 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07004498{
4499 struct drm_i915_gem_busy *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00004500 struct drm_i915_gem_object *obj;
Chris Wilsond07f0e52016-10-28 13:58:44 +01004501 struct reservation_object_list *list;
4502 unsigned int seq;
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004503 int err;
Eric Anholt673a3942008-07-30 12:06:12 -07004504
Chris Wilsond07f0e52016-10-28 13:58:44 +01004505 err = -ENOENT;
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004506 rcu_read_lock();
4507 obj = i915_gem_object_lookup_rcu(file, args->handle);
Chris Wilsond07f0e52016-10-28 13:58:44 +01004508 if (!obj)
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004509 goto out;
Chris Wilsond07f0e52016-10-28 13:58:44 +01004510
4511 /* A discrepancy here is that we do not report the status of
4512 * non-i915 fences, i.e. even though we may report the object as idle,
4513 * a call to set-domain may still stall waiting for foreign rendering.
4514 * This also means that wait-ioctl may report an object as busy,
4515 * where busy-ioctl considers it idle.
4516 *
4517 * We trade the ability to warn of foreign fences to report on which
4518 * i915 engines are active for the object.
4519 *
4520 * Alternatively, we can trade that extra information on read/write
4521 * activity with
4522 * args->busy =
4523 * !reservation_object_test_signaled_rcu(obj->resv, true);
4524 * to report the overall busyness. This is what the wait-ioctl does.
4525 *
4526 */
4527retry:
4528 seq = raw_read_seqcount(&obj->resv->seq);
4529
4530 /* Translate the exclusive fence to the READ *and* WRITE engine */
4531 args->busy = busy_check_writer(rcu_dereference(obj->resv->fence_excl));
4532
4533 /* Translate shared fences to READ set of engines */
4534 list = rcu_dereference(obj->resv->fence);
4535 if (list) {
4536 unsigned int shared_count = list->shared_count, i;
4537
4538 for (i = 0; i < shared_count; ++i) {
4539 struct dma_fence *fence =
4540 rcu_dereference(list->shared[i]);
4541
4542 args->busy |= busy_check_reader(fence);
4543 }
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004544 }
Zou Nan haid1b851f2010-05-21 09:08:57 +08004545
Chris Wilsond07f0e52016-10-28 13:58:44 +01004546 if (args->busy && read_seqcount_retry(&obj->resv->seq, seq))
4547 goto retry;
Chris Wilson426960b2016-01-15 16:51:46 +00004548
Chris Wilsond07f0e52016-10-28 13:58:44 +01004549 err = 0;
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004550out:
4551 rcu_read_unlock();
4552 return err;
Eric Anholt673a3942008-07-30 12:06:12 -07004553}
4554
4555int
4556i915_gem_throttle_ioctl(struct drm_device *dev, void *data,
4557 struct drm_file *file_priv)
4558{
Akshay Joshi0206e352011-08-16 15:34:10 -04004559 return i915_gem_ring_throttle(dev, file_priv);
Eric Anholt673a3942008-07-30 12:06:12 -07004560}
4561
Chris Wilson3ef94da2009-09-14 16:50:29 +01004562int
4563i915_gem_madvise_ioctl(struct drm_device *dev, void *data,
4564 struct drm_file *file_priv)
4565{
Chris Wilsonfac5e232016-07-04 11:34:36 +01004566 struct drm_i915_private *dev_priv = to_i915(dev);
Chris Wilson3ef94da2009-09-14 16:50:29 +01004567 struct drm_i915_gem_madvise *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00004568 struct drm_i915_gem_object *obj;
Chris Wilson1233e2d2016-10-28 13:58:37 +01004569 int err;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004570
4571 switch (args->madv) {
4572 case I915_MADV_DONTNEED:
4573 case I915_MADV_WILLNEED:
4574 break;
4575 default:
4576 return -EINVAL;
4577 }
4578
Chris Wilson03ac0642016-07-20 13:31:51 +01004579 obj = i915_gem_object_lookup(file_priv, args->handle);
Chris Wilson1233e2d2016-10-28 13:58:37 +01004580 if (!obj)
4581 return -ENOENT;
4582
4583 err = mutex_lock_interruptible(&obj->mm.lock);
4584 if (err)
4585 goto out;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004586
Chris Wilsonf1fa4f42017-10-13 21:26:13 +01004587 if (i915_gem_object_has_pages(obj) &&
Chris Wilson3e510a82016-08-05 10:14:23 +01004588 i915_gem_object_is_tiled(obj) &&
Daniel Vetter656bfa32014-11-20 09:26:30 +01004589 dev_priv->quirks & QUIRK_PIN_SWIZZLED_PAGES) {
Chris Wilsonbc0629a2016-11-01 10:03:17 +00004590 if (obj->mm.madv == I915_MADV_WILLNEED) {
4591 GEM_BUG_ON(!obj->mm.quirked);
Chris Wilsona4f5ea62016-10-28 13:58:35 +01004592 __i915_gem_object_unpin_pages(obj);
Chris Wilsonbc0629a2016-11-01 10:03:17 +00004593 obj->mm.quirked = false;
4594 }
4595 if (args->madv == I915_MADV_WILLNEED) {
Chris Wilson2c3a3f42016-11-04 10:30:01 +00004596 GEM_BUG_ON(obj->mm.quirked);
Chris Wilsona4f5ea62016-10-28 13:58:35 +01004597 __i915_gem_object_pin_pages(obj);
Chris Wilsonbc0629a2016-11-01 10:03:17 +00004598 obj->mm.quirked = true;
4599 }
Daniel Vetter656bfa32014-11-20 09:26:30 +01004600 }
4601
Chris Wilsona4f5ea62016-10-28 13:58:35 +01004602 if (obj->mm.madv != __I915_MADV_PURGED)
4603 obj->mm.madv = args->madv;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004604
Chris Wilson6c085a72012-08-20 11:40:46 +02004605 /* if the object is no longer attached, discard its backing storage */
Chris Wilsonf1fa4f42017-10-13 21:26:13 +01004606 if (obj->mm.madv == I915_MADV_DONTNEED &&
4607 !i915_gem_object_has_pages(obj))
Chris Wilson2d7ef392009-09-20 23:13:10 +01004608 i915_gem_object_truncate(obj);
4609
Chris Wilsona4f5ea62016-10-28 13:58:35 +01004610 args->retained = obj->mm.madv != __I915_MADV_PURGED;
Chris Wilson1233e2d2016-10-28 13:58:37 +01004611 mutex_unlock(&obj->mm.lock);
Chris Wilsonbb6baf72009-09-22 14:24:13 +01004612
Chris Wilson1233e2d2016-10-28 13:58:37 +01004613out:
Chris Wilsonf8c417c2016-07-20 13:31:53 +01004614 i915_gem_object_put(obj);
Chris Wilson1233e2d2016-10-28 13:58:37 +01004615 return err;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004616}
4617
Chris Wilson5b8c8ae2016-11-16 19:07:04 +00004618static void
Chris Wilsone61e0f52018-02-21 09:56:36 +00004619frontbuffer_retire(struct i915_gem_active *active, struct i915_request *request)
Chris Wilson5b8c8ae2016-11-16 19:07:04 +00004620{
4621 struct drm_i915_gem_object *obj =
4622 container_of(active, typeof(*obj), frontbuffer_write);
4623
Chris Wilsond59b21e2017-02-22 11:40:49 +00004624 intel_fb_obj_flush(obj, ORIGIN_CS);
Chris Wilson5b8c8ae2016-11-16 19:07:04 +00004625}
4626
Chris Wilson37e680a2012-06-07 15:38:42 +01004627void i915_gem_object_init(struct drm_i915_gem_object *obj,
4628 const struct drm_i915_gem_object_ops *ops)
Chris Wilson0327d6b2012-08-11 15:41:06 +01004629{
Chris Wilson1233e2d2016-10-28 13:58:37 +01004630 mutex_init(&obj->mm.lock);
4631
Ben Widawsky2f633152013-07-17 12:19:03 -07004632 INIT_LIST_HEAD(&obj->vma_list);
Chris Wilsond1b48c12017-08-16 09:52:08 +01004633 INIT_LIST_HEAD(&obj->lut_list);
Chris Wilson8d9d5742015-04-07 16:20:38 +01004634 INIT_LIST_HEAD(&obj->batch_pool_link);
Chris Wilson0327d6b2012-08-11 15:41:06 +01004635
Chris Wilson37e680a2012-06-07 15:38:42 +01004636 obj->ops = ops;
4637
Chris Wilsond07f0e52016-10-28 13:58:44 +01004638 reservation_object_init(&obj->__builtin_resv);
4639 obj->resv = &obj->__builtin_resv;
4640
Chris Wilson50349242016-08-18 17:17:04 +01004641 obj->frontbuffer_ggtt_origin = ORIGIN_GTT;
Chris Wilson5b8c8ae2016-11-16 19:07:04 +00004642 init_request_active(&obj->frontbuffer_write, frontbuffer_retire);
Chris Wilsona4f5ea62016-10-28 13:58:35 +01004643
4644 obj->mm.madv = I915_MADV_WILLNEED;
4645 INIT_RADIX_TREE(&obj->mm.get_page.radix, GFP_KERNEL | __GFP_NOWARN);
4646 mutex_init(&obj->mm.get_page.lock);
Chris Wilson0327d6b2012-08-11 15:41:06 +01004647
Dave Gordonf19ec8c2016-07-04 11:34:37 +01004648 i915_gem_info_add_obj(to_i915(obj->base.dev), obj->base.size);
Chris Wilson0327d6b2012-08-11 15:41:06 +01004649}
4650
Chris Wilson37e680a2012-06-07 15:38:42 +01004651static const struct drm_i915_gem_object_ops i915_gem_object_ops = {
Tvrtko Ursulin3599a912016-11-01 14:44:10 +00004652 .flags = I915_GEM_OBJECT_HAS_STRUCT_PAGE |
4653 I915_GEM_OBJECT_IS_SHRINKABLE,
Chris Wilson7c55e2c2017-03-07 12:03:38 +00004654
Chris Wilson37e680a2012-06-07 15:38:42 +01004655 .get_pages = i915_gem_object_get_pages_gtt,
4656 .put_pages = i915_gem_object_put_pages_gtt,
Chris Wilson7c55e2c2017-03-07 12:03:38 +00004657
4658 .pwrite = i915_gem_object_pwrite_gtt,
Chris Wilson37e680a2012-06-07 15:38:42 +01004659};
4660
Matthew Auld465c4032017-10-06 23:18:14 +01004661static int i915_gem_object_create_shmem(struct drm_device *dev,
4662 struct drm_gem_object *obj,
4663 size_t size)
4664{
4665 struct drm_i915_private *i915 = to_i915(dev);
4666 unsigned long flags = VM_NORESERVE;
4667 struct file *filp;
4668
4669 drm_gem_private_object_init(dev, obj, size);
4670
4671 if (i915->mm.gemfs)
4672 filp = shmem_file_setup_with_mnt(i915->mm.gemfs, "i915", size,
4673 flags);
4674 else
4675 filp = shmem_file_setup("i915", size, flags);
4676
4677 if (IS_ERR(filp))
4678 return PTR_ERR(filp);
4679
4680 obj->filp = filp;
4681
4682 return 0;
4683}
4684
Chris Wilsonb4bcbe22016-10-18 13:02:49 +01004685struct drm_i915_gem_object *
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +00004686i915_gem_object_create(struct drm_i915_private *dev_priv, u64 size)
Daniel Vetterac52bc52010-04-09 19:05:06 +00004687{
Daniel Vetterc397b902010-04-09 19:05:07 +00004688 struct drm_i915_gem_object *obj;
Hugh Dickins5949eac2011-06-27 16:18:18 -07004689 struct address_space *mapping;
Chris Wilsonb8f55be2017-08-11 12:11:16 +01004690 unsigned int cache_level;
Daniel Vetter1a240d42012-11-29 22:18:51 +01004691 gfp_t mask;
Chris Wilsonfe3db792016-04-25 13:32:13 +01004692 int ret;
Daniel Vetterc397b902010-04-09 19:05:07 +00004693
Chris Wilsonb4bcbe22016-10-18 13:02:49 +01004694 /* There is a prevalence of the assumption that we fit the object's
4695 * page count inside a 32bit _signed_ variable. Let's document this and
4696 * catch if we ever need to fix it. In the meantime, if you do spot
4697 * such a local variable, please consider fixing!
4698 */
Tvrtko Ursulin7a3ee5d2017-03-30 17:31:30 +01004699 if (size >> PAGE_SHIFT > INT_MAX)
Chris Wilsonb4bcbe22016-10-18 13:02:49 +01004700 return ERR_PTR(-E2BIG);
4701
4702 if (overflows_type(size, obj->base.size))
4703 return ERR_PTR(-E2BIG);
4704
Tvrtko Ursulin187685c2016-12-01 14:16:36 +00004705 obj = i915_gem_object_alloc(dev_priv);
Daniel Vetterc397b902010-04-09 19:05:07 +00004706 if (obj == NULL)
Chris Wilsonfe3db792016-04-25 13:32:13 +01004707 return ERR_PTR(-ENOMEM);
Daniel Vetterc397b902010-04-09 19:05:07 +00004708
Matthew Auld465c4032017-10-06 23:18:14 +01004709 ret = i915_gem_object_create_shmem(&dev_priv->drm, &obj->base, size);
Chris Wilsonfe3db792016-04-25 13:32:13 +01004710 if (ret)
4711 goto fail;
Daniel Vetterc397b902010-04-09 19:05:07 +00004712
Chris Wilsonbed1ea92012-05-24 20:48:12 +01004713 mask = GFP_HIGHUSER | __GFP_RECLAIMABLE;
Jani Nikulac0f86832016-12-07 12:13:04 +02004714 if (IS_I965GM(dev_priv) || IS_I965G(dev_priv)) {
Chris Wilsonbed1ea92012-05-24 20:48:12 +01004715 /* 965gm cannot relocate objects above 4GiB. */
4716 mask &= ~__GFP_HIGHMEM;
4717 mask |= __GFP_DMA32;
4718 }
4719
Al Viro93c76a32015-12-04 23:45:44 -05004720 mapping = obj->base.filp->f_mapping;
Chris Wilsonbed1ea92012-05-24 20:48:12 +01004721 mapping_set_gfp_mask(mapping, mask);
Chris Wilson4846bf02017-06-09 12:03:46 +01004722 GEM_BUG_ON(!(mapping_gfp_mask(mapping) & __GFP_RECLAIM));
Hugh Dickins5949eac2011-06-27 16:18:18 -07004723
Chris Wilson37e680a2012-06-07 15:38:42 +01004724 i915_gem_object_init(obj, &i915_gem_object_ops);
Chris Wilson73aa8082010-09-30 11:46:12 +01004725
Christian Königc0a51fd2018-02-16 13:43:38 +01004726 obj->write_domain = I915_GEM_DOMAIN_CPU;
4727 obj->read_domains = I915_GEM_DOMAIN_CPU;
Daniel Vetterc397b902010-04-09 19:05:07 +00004728
Chris Wilsonb8f55be2017-08-11 12:11:16 +01004729 if (HAS_LLC(dev_priv))
Eugeni Dodonov3d29b842012-01-17 14:43:53 -02004730 /* On some devices, we can have the GPU use the LLC (the CPU
Eric Anholta1871112011-03-29 16:59:55 -07004731 * cache) for about a 10% performance improvement
4732 * compared to uncached. Graphics requests other than
4733 * display scanout are coherent with the CPU in
4734 * accessing this cache. This means in this mode we
4735 * don't need to clflush on the CPU side, and on the
4736 * GPU side we only need to flush internal caches to
4737 * get data visible to the CPU.
4738 *
4739 * However, we maintain the display planes as UC, and so
4740 * need to rebind when first used as such.
4741 */
Chris Wilsonb8f55be2017-08-11 12:11:16 +01004742 cache_level = I915_CACHE_LLC;
4743 else
4744 cache_level = I915_CACHE_NONE;
Eric Anholta1871112011-03-29 16:59:55 -07004745
Chris Wilsonb8f55be2017-08-11 12:11:16 +01004746 i915_gem_object_set_cache_coherency(obj, cache_level);
Chris Wilsone27ab732017-06-15 13:38:49 +01004747
Daniel Vetterd861e332013-07-24 23:25:03 +02004748 trace_i915_gem_object_create(obj);
4749
Chris Wilson05394f32010-11-08 19:18:58 +00004750 return obj;
Chris Wilsonfe3db792016-04-25 13:32:13 +01004751
4752fail:
4753 i915_gem_object_free(obj);
Chris Wilsonfe3db792016-04-25 13:32:13 +01004754 return ERR_PTR(ret);
Daniel Vetterac52bc52010-04-09 19:05:06 +00004755}
4756
Chris Wilson340fbd82014-05-22 09:16:52 +01004757static bool discard_backing_storage(struct drm_i915_gem_object *obj)
4758{
4759 /* If we are the last user of the backing storage (be it shmemfs
4760 * pages or stolen etc), we know that the pages are going to be
4761 * immediately released. In this case, we can then skip copying
4762 * back the contents from the GPU.
4763 */
4764
Chris Wilsona4f5ea62016-10-28 13:58:35 +01004765 if (obj->mm.madv != I915_MADV_WILLNEED)
Chris Wilson340fbd82014-05-22 09:16:52 +01004766 return false;
4767
4768 if (obj->base.filp == NULL)
4769 return true;
4770
4771 /* At first glance, this looks racy, but then again so would be
4772 * userspace racing mmap against close. However, the first external
4773 * reference to the filp can only be obtained through the
4774 * i915_gem_mmap_ioctl() which safeguards us against the user
4775 * acquiring such a reference whilst we are in the middle of
4776 * freeing the object.
4777 */
4778 return atomic_long_read(&obj->base.filp->f_count) == 1;
4779}
4780
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004781static void __i915_gem_free_objects(struct drm_i915_private *i915,
4782 struct llist_node *freed)
Chris Wilsonbe726152010-07-23 23:18:50 +01004783{
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004784 struct drm_i915_gem_object *obj, *on;
Chris Wilsonbe726152010-07-23 23:18:50 +01004785
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004786 intel_runtime_pm_get(i915);
Chris Wilsoncc731f52017-10-13 21:26:21 +01004787 llist_for_each_entry_safe(obj, on, freed, freed) {
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004788 struct i915_vma *vma, *vn;
Paulo Zanonif65c9162013-11-27 18:20:34 -02004789
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004790 trace_i915_gem_object_destroy(obj);
4791
Chris Wilsoncc731f52017-10-13 21:26:21 +01004792 mutex_lock(&i915->drm.struct_mutex);
4793
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004794 GEM_BUG_ON(i915_gem_object_is_active(obj));
4795 list_for_each_entry_safe(vma, vn,
4796 &obj->vma_list, obj_link) {
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004797 GEM_BUG_ON(i915_vma_is_active(vma));
4798 vma->flags &= ~I915_VMA_PIN_MASK;
Chris Wilson3365e222018-05-03 20:51:14 +01004799 i915_vma_destroy(vma);
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004800 }
Chris Wilsondb6c2b42016-11-01 11:54:00 +00004801 GEM_BUG_ON(!list_empty(&obj->vma_list));
4802 GEM_BUG_ON(!RB_EMPTY_ROOT(&obj->vma_tree));
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004803
Chris Wilsonf2123812017-10-16 12:40:37 +01004804 /* This serializes freeing with the shrinker. Since the free
4805 * is delayed, first by RCU then by the workqueue, we want the
4806 * shrinker to be able to free pages of unreferenced objects,
4807 * or else we may oom whilst there are plenty of deferred
4808 * freed objects.
4809 */
4810 if (i915_gem_object_has_pages(obj)) {
4811 spin_lock(&i915->mm.obj_lock);
4812 list_del_init(&obj->mm.link);
4813 spin_unlock(&i915->mm.obj_lock);
4814 }
4815
Chris Wilsoncc731f52017-10-13 21:26:21 +01004816 mutex_unlock(&i915->drm.struct_mutex);
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004817
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004818 GEM_BUG_ON(obj->bind_count);
Chris Wilsona65adaf2017-10-09 09:43:57 +01004819 GEM_BUG_ON(obj->userfault_count);
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004820 GEM_BUG_ON(atomic_read(&obj->frontbuffer_bits));
Chris Wilson67b48042017-08-22 12:05:16 +01004821 GEM_BUG_ON(!list_empty(&obj->lut_list));
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004822
4823 if (obj->ops->release)
4824 obj->ops->release(obj);
4825
4826 if (WARN_ON(i915_gem_object_has_pinned_pages(obj)))
4827 atomic_set(&obj->mm.pages_pin_count, 0);
Chris Wilson548625e2016-11-01 12:11:34 +00004828 __i915_gem_object_put_pages(obj, I915_MM_NORMAL);
Chris Wilsonf1fa4f42017-10-13 21:26:13 +01004829 GEM_BUG_ON(i915_gem_object_has_pages(obj));
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004830
4831 if (obj->base.import_attach)
4832 drm_prime_gem_destroy(&obj->base, NULL);
4833
Chris Wilsond07f0e52016-10-28 13:58:44 +01004834 reservation_object_fini(&obj->__builtin_resv);
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004835 drm_gem_object_release(&obj->base);
4836 i915_gem_info_remove_obj(i915, obj->base.size);
4837
4838 kfree(obj->bit_17);
4839 i915_gem_object_free(obj);
Chris Wilsoncc731f52017-10-13 21:26:21 +01004840
Chris Wilsonc9c704712018-02-19 22:06:31 +00004841 GEM_BUG_ON(!atomic_read(&i915->mm.free_count));
4842 atomic_dec(&i915->mm.free_count);
4843
Chris Wilsoncc731f52017-10-13 21:26:21 +01004844 if (on)
4845 cond_resched();
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004846 }
Chris Wilsoncc731f52017-10-13 21:26:21 +01004847 intel_runtime_pm_put(i915);
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004848}
4849
4850static void i915_gem_flush_free_objects(struct drm_i915_private *i915)
4851{
4852 struct llist_node *freed;
4853
Chris Wilson87701b42017-10-13 21:26:20 +01004854 /* Free the oldest, most stale object to keep the free_list short */
4855 freed = NULL;
4856 if (!llist_empty(&i915->mm.free_list)) { /* quick test for hotpath */
4857 /* Only one consumer of llist_del_first() allowed */
4858 spin_lock(&i915->mm.free_lock);
4859 freed = llist_del_first(&i915->mm.free_list);
4860 spin_unlock(&i915->mm.free_lock);
4861 }
4862 if (unlikely(freed)) {
4863 freed->next = NULL;
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004864 __i915_gem_free_objects(i915, freed);
Chris Wilson87701b42017-10-13 21:26:20 +01004865 }
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004866}
4867
4868static void __i915_gem_free_work(struct work_struct *work)
4869{
4870 struct drm_i915_private *i915 =
4871 container_of(work, struct drm_i915_private, mm.free_work);
4872 struct llist_node *freed;
Chris Wilson26e12f82011-03-20 11:20:19 +00004873
Chris Wilson2ef1e722018-01-15 20:57:59 +00004874 /*
4875 * All file-owned VMA should have been released by this point through
Chris Wilsonb1f788c2016-08-04 07:52:45 +01004876 * i915_gem_close_object(), or earlier by i915_gem_context_close().
4877 * However, the object may also be bound into the global GTT (e.g.
4878 * older GPUs without per-process support, or for direct access through
4879 * the GTT either for the user or for scanout). Those VMA still need to
4880 * unbound now.
4881 */
Chris Wilson1488fc02012-04-24 15:47:31 +01004882
Chris Wilsonf991c492017-11-06 11:15:08 +00004883 spin_lock(&i915->mm.free_lock);
Chris Wilson5ad08be2017-04-07 11:25:51 +01004884 while ((freed = llist_del_all(&i915->mm.free_list))) {
Chris Wilsonf991c492017-11-06 11:15:08 +00004885 spin_unlock(&i915->mm.free_lock);
4886
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004887 __i915_gem_free_objects(i915, freed);
Chris Wilson5ad08be2017-04-07 11:25:51 +01004888 if (need_resched())
Chris Wilsonf991c492017-11-06 11:15:08 +00004889 return;
4890
4891 spin_lock(&i915->mm.free_lock);
Chris Wilson5ad08be2017-04-07 11:25:51 +01004892 }
Chris Wilsonf991c492017-11-06 11:15:08 +00004893 spin_unlock(&i915->mm.free_lock);
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004894}
4895
4896static void __i915_gem_free_object_rcu(struct rcu_head *head)
4897{
4898 struct drm_i915_gem_object *obj =
4899 container_of(head, typeof(*obj), rcu);
4900 struct drm_i915_private *i915 = to_i915(obj->base.dev);
4901
Chris Wilson2ef1e722018-01-15 20:57:59 +00004902 /*
4903 * Since we require blocking on struct_mutex to unbind the freed
4904 * object from the GPU before releasing resources back to the
4905 * system, we can not do that directly from the RCU callback (which may
4906 * be a softirq context), but must instead then defer that work onto a
4907 * kthread. We use the RCU callback rather than move the freed object
4908 * directly onto the work queue so that we can mix between using the
4909 * worker and performing frees directly from subsequent allocations for
4910 * crude but effective memory throttling.
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004911 */
4912 if (llist_add(&obj->freed, &i915->mm.free_list))
Chris Wilsonbeacbd12018-01-15 12:28:45 +00004913 queue_work(i915->wq, &i915->mm.free_work);
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004914}
4915
4916void i915_gem_free_object(struct drm_gem_object *gem_obj)
4917{
4918 struct drm_i915_gem_object *obj = to_intel_bo(gem_obj);
4919
Chris Wilsonbc0629a2016-11-01 10:03:17 +00004920 if (obj->mm.quirked)
4921 __i915_gem_object_unpin_pages(obj);
4922
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004923 if (discard_backing_storage(obj))
4924 obj->mm.madv = I915_MADV_DONTNEED;
Daniel Vettera071fa02014-06-18 23:28:09 +02004925
Chris Wilson2ef1e722018-01-15 20:57:59 +00004926 /*
4927 * Before we free the object, make sure any pure RCU-only
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004928 * read-side critical sections are complete, e.g.
4929 * i915_gem_busy_ioctl(). For the corresponding synchronized
4930 * lookup see i915_gem_object_lookup_rcu().
4931 */
Chris Wilsonc9c704712018-02-19 22:06:31 +00004932 atomic_inc(&to_i915(obj->base.dev)->mm.free_count);
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004933 call_rcu(&obj->rcu, __i915_gem_free_object_rcu);
Chris Wilsonbe726152010-07-23 23:18:50 +01004934}
4935
Chris Wilsonf8a7fde2016-10-28 13:58:29 +01004936void __i915_gem_object_release_unless_active(struct drm_i915_gem_object *obj)
4937{
4938 lockdep_assert_held(&obj->base.dev->struct_mutex);
4939
Chris Wilsond1b48c12017-08-16 09:52:08 +01004940 if (!i915_gem_object_has_active_reference(obj) &&
4941 i915_gem_object_is_active(obj))
Chris Wilsonf8a7fde2016-10-28 13:58:29 +01004942 i915_gem_object_set_active_reference(obj);
4943 else
4944 i915_gem_object_put(obj);
4945}
4946
Chris Wilsonae6c4572017-11-10 14:26:28 +00004947static void assert_kernel_context_is_current(struct drm_i915_private *i915)
Chris Wilson3033aca2016-10-28 13:58:47 +01004948{
Chris Wilson1fc44d92018-05-17 22:26:32 +01004949 struct i915_gem_context *kctx = i915->kernel_context;
Chris Wilson3033aca2016-10-28 13:58:47 +01004950 struct intel_engine_cs *engine;
4951 enum intel_engine_id id;
4952
Chris Wilson09a4c022018-05-24 09:11:35 +01004953 GEM_BUG_ON(i915->gt.active_requests);
Chris Wilsonae6c4572017-11-10 14:26:28 +00004954 for_each_engine(engine, i915, id) {
Chris Wilsona89d1f92018-05-02 17:38:39 +01004955 GEM_BUG_ON(__i915_gem_active_peek(&engine->timeline.last_request));
Chris Wilson1fc44d92018-05-17 22:26:32 +01004956 GEM_BUG_ON(engine->last_retired_context->gem_context != kctx);
Chris Wilsonae6c4572017-11-10 14:26:28 +00004957 }
Chris Wilson3033aca2016-10-28 13:58:47 +01004958}
4959
Chris Wilson24145512017-01-24 11:01:35 +00004960void i915_gem_sanitize(struct drm_i915_private *i915)
4961{
Chris Wilsonc3160da2018-05-31 09:22:45 +01004962 struct intel_engine_cs *engine;
4963 enum intel_engine_id id;
4964
4965 GEM_TRACE("\n");
4966
Chris Wilson4dfacb02018-05-31 09:22:43 +01004967 mutex_lock(&i915->drm.struct_mutex);
Chris Wilsonc3160da2018-05-31 09:22:45 +01004968
4969 intel_runtime_pm_get(i915);
4970 intel_uncore_forcewake_get(i915, FORCEWAKE_ALL);
4971
4972 /*
4973 * As we have just resumed the machine and woken the device up from
4974 * deep PCI sleep (presumably D3_cold), assume the HW has been reset
4975 * back to defaults, recovering from whatever wedged state we left it
4976 * in and so worth trying to use the device once more.
4977 */
Chris Wilson4dfacb02018-05-31 09:22:43 +01004978 if (i915_terminally_wedged(&i915->gpu_error))
Chris Wilsonf36325f2017-08-26 12:09:34 +01004979 i915_gem_unset_wedged(i915);
Chris Wilsonf36325f2017-08-26 12:09:34 +01004980
Chris Wilson24145512017-01-24 11:01:35 +00004981 /*
4982 * If we inherit context state from the BIOS or earlier occupants
4983 * of the GPU, the GPU may be in an inconsistent state when we
4984 * try to take over. The only way to remove the earlier state
4985 * is by resetting. However, resetting on earlier gen is tricky as
4986 * it may impact the display and we are uncertain about the stability
Joonas Lahtinenea117b82017-04-28 10:53:38 +03004987 * of the reset, so this could be applied to even earlier gen.
Chris Wilson24145512017-01-24 11:01:35 +00004988 */
Daniele Ceraolo Spurioce1599a2018-02-07 13:24:40 -08004989 if (INTEL_GEN(i915) >= 5 && intel_has_gpu_reset(i915))
4990 WARN_ON(intel_gpu_reset(i915, ALL_ENGINES));
Chris Wilson4dfacb02018-05-31 09:22:43 +01004991
Chris Wilsonc3160da2018-05-31 09:22:45 +01004992 /* Reset the submission backend after resume as well as the GPU reset */
4993 for_each_engine(engine, i915, id) {
4994 if (engine->reset.reset)
4995 engine->reset.reset(engine, NULL);
4996 }
4997
4998 intel_uncore_forcewake_put(i915, FORCEWAKE_ALL);
4999 intel_runtime_pm_put(i915);
5000
Chris Wilson4dfacb02018-05-31 09:22:43 +01005001 i915_gem_contexts_lost(i915);
5002 mutex_unlock(&i915->drm.struct_mutex);
Chris Wilson24145512017-01-24 11:01:35 +00005003}
5004
Tvrtko Ursulinbf9e8422016-12-01 14:16:38 +00005005int i915_gem_suspend(struct drm_i915_private *dev_priv)
Eric Anholt673a3942008-07-30 12:06:12 -07005006{
Tvrtko Ursulinbf9e8422016-12-01 14:16:38 +00005007 struct drm_device *dev = &dev_priv->drm;
Chris Wilsondcff85c2016-08-05 10:14:11 +01005008 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07005009
Chris Wilson09a4c022018-05-24 09:11:35 +01005010 GEM_TRACE("\n");
5011
Chris Wilsonc998e8a2017-03-02 08:30:29 +00005012 intel_runtime_pm_get(dev_priv);
Chris Wilson54b4f682016-07-21 21:16:19 +01005013 intel_suspend_gt_powersave(dev_priv);
5014
Chris Wilson45c5f202013-10-16 11:50:01 +01005015 mutex_lock(&dev->struct_mutex);
Chris Wilson5ab57c72016-07-15 14:56:20 +01005016
5017 /* We have to flush all the executing contexts to main memory so
5018 * that they can saved in the hibernation image. To ensure the last
5019 * context image is coherent, we have to switch away from it. That
5020 * leaves the dev_priv->kernel_context still active when
5021 * we actually suspend, and its image in memory may not match the GPU
5022 * state. Fortunately, the kernel_context is disposable and we do
5023 * not rely on its state.
5024 */
Chris Wilsonecf73eb2017-11-30 10:29:51 +00005025 if (!i915_terminally_wedged(&dev_priv->gpu_error)) {
5026 ret = i915_gem_switch_to_kernel_context(dev_priv);
5027 if (ret)
5028 goto err_unlock;
Chris Wilson5ab57c72016-07-15 14:56:20 +01005029
Chris Wilsonecf73eb2017-11-30 10:29:51 +00005030 ret = i915_gem_wait_for_idle(dev_priv,
5031 I915_WAIT_INTERRUPTIBLE |
Chris Wilson06060352018-05-31 09:22:44 +01005032 I915_WAIT_LOCKED |
5033 I915_WAIT_FOR_IDLE_BOOST);
Chris Wilsonecf73eb2017-11-30 10:29:51 +00005034 if (ret && ret != -EIO)
5035 goto err_unlock;
Chris Wilsonf7403342013-09-13 23:57:04 +01005036
Chris Wilsonecf73eb2017-11-30 10:29:51 +00005037 assert_kernel_context_is_current(dev_priv);
5038 }
Chris Wilson45c5f202013-10-16 11:50:01 +01005039 mutex_unlock(&dev->struct_mutex);
5040
Michal Wajdeczko7cfca4a2018-03-02 11:15:49 +00005041 intel_uc_suspend(dev_priv);
Sagar Arun Kamble63987bf2017-04-05 15:51:50 +05305042
Chris Wilson737b1502015-01-26 18:03:03 +02005043 cancel_delayed_work_sync(&dev_priv->gpu_error.hangcheck_work);
Chris Wilson67d97da2016-07-04 08:08:31 +01005044 cancel_delayed_work_sync(&dev_priv->gt.retire_work);
Chris Wilsonbdeb9782016-12-23 14:57:56 +00005045
5046 /* As the idle_work is rearming if it detects a race, play safe and
5047 * repeat the flush until it is definitely idle.
5048 */
Chris Wilson7c262402017-10-06 11:40:38 +01005049 drain_delayed_work(&dev_priv->gt.idle_work);
Chris Wilsonbdeb9782016-12-23 14:57:56 +00005050
Chris Wilsonbdcf1202014-11-25 11:56:33 +00005051 /* Assert that we sucessfully flushed all the work and
5052 * reset the GPU back to its idle, low power state.
5053 */
Chris Wilson67d97da2016-07-04 08:08:31 +01005054 WARN_ON(dev_priv->gt.awake);
Chris Wilsonfc692bd2017-08-26 12:09:35 +01005055 if (WARN_ON(!intel_engines_are_idle(dev_priv)))
5056 i915_gem_set_wedged(dev_priv); /* no hope, discard everything */
Chris Wilsonbdcf1202014-11-25 11:56:33 +00005057
Chris Wilsonec92ad02018-05-31 09:22:46 +01005058 intel_runtime_pm_put(dev_priv);
5059 return 0;
5060
5061err_unlock:
5062 mutex_unlock(&dev->struct_mutex);
5063 intel_runtime_pm_put(dev_priv);
5064 return ret;
5065}
5066
5067void i915_gem_suspend_late(struct drm_i915_private *i915)
5068{
Imre Deak1c777c52016-10-12 17:46:37 +03005069 /*
5070 * Neither the BIOS, ourselves or any other kernel
5071 * expects the system to be in execlists mode on startup,
5072 * so we need to reset the GPU back to legacy mode. And the only
5073 * known way to disable logical contexts is through a GPU reset.
5074 *
5075 * So in order to leave the system in a known default configuration,
5076 * always reset the GPU upon unload and suspend. Afterwards we then
5077 * clean up the GEM state tracking, flushing off the requests and
5078 * leaving the system in a known idle state.
5079 *
5080 * Note that is of the upmost importance that the GPU is idle and
5081 * all stray writes are flushed *before* we dismantle the backing
5082 * storage for the pinned objects.
5083 *
5084 * However, since we are uncertain that resetting the GPU on older
5085 * machines is a good idea, we don't - just in case it leaves the
5086 * machine in an unusable condition.
5087 */
Chris Wilsoncad99462017-08-26 12:09:33 +01005088
Chris Wilsonec92ad02018-05-31 09:22:46 +01005089 intel_uc_sanitize(i915);
5090 i915_gem_sanitize(i915);
Eric Anholt673a3942008-07-30 12:06:12 -07005091}
5092
Chris Wilson37cd3302017-11-12 11:27:38 +00005093void i915_gem_resume(struct drm_i915_private *i915)
Chris Wilson5ab57c72016-07-15 14:56:20 +01005094{
Chris Wilson4dfacb02018-05-31 09:22:43 +01005095 GEM_TRACE("\n");
5096
Chris Wilson37cd3302017-11-12 11:27:38 +00005097 WARN_ON(i915->gt.awake);
Chris Wilson5ab57c72016-07-15 14:56:20 +01005098
Chris Wilson37cd3302017-11-12 11:27:38 +00005099 mutex_lock(&i915->drm.struct_mutex);
5100 intel_uncore_forcewake_get(i915, FORCEWAKE_ALL);
Imre Deak31ab49a2016-11-07 11:20:05 +02005101
Chris Wilson37cd3302017-11-12 11:27:38 +00005102 i915_gem_restore_gtt_mappings(i915);
5103 i915_gem_restore_fences(i915);
Chris Wilson5ab57c72016-07-15 14:56:20 +01005104
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00005105 /*
5106 * As we didn't flush the kernel context before suspend, we cannot
Chris Wilson5ab57c72016-07-15 14:56:20 +01005107 * guarantee that the context image is complete. So let's just reset
5108 * it and start again.
5109 */
Chris Wilson37cd3302017-11-12 11:27:38 +00005110 i915->gt.resume(i915);
Chris Wilson5ab57c72016-07-15 14:56:20 +01005111
Chris Wilson37cd3302017-11-12 11:27:38 +00005112 if (i915_gem_init_hw(i915))
5113 goto err_wedged;
5114
Michal Wajdeczko7cfca4a2018-03-02 11:15:49 +00005115 intel_uc_resume(i915);
Chris Wilson7469c622017-11-14 13:03:00 +00005116
Chris Wilson37cd3302017-11-12 11:27:38 +00005117 /* Always reload a context for powersaving. */
5118 if (i915_gem_switch_to_kernel_context(i915))
5119 goto err_wedged;
5120
5121out_unlock:
5122 intel_uncore_forcewake_put(i915, FORCEWAKE_ALL);
5123 mutex_unlock(&i915->drm.struct_mutex);
5124 return;
5125
5126err_wedged:
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00005127 if (!i915_terminally_wedged(&i915->gpu_error)) {
5128 DRM_ERROR("failed to re-initialize GPU, declaring wedged!\n");
5129 i915_gem_set_wedged(i915);
5130 }
Chris Wilson37cd3302017-11-12 11:27:38 +00005131 goto out_unlock;
Chris Wilson5ab57c72016-07-15 14:56:20 +01005132}
5133
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00005134void i915_gem_init_swizzling(struct drm_i915_private *dev_priv)
Daniel Vetterf691e2f2012-02-02 09:58:12 +01005135{
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00005136 if (INTEL_GEN(dev_priv) < 5 ||
Daniel Vetterf691e2f2012-02-02 09:58:12 +01005137 dev_priv->mm.bit_6_swizzle_x == I915_BIT_6_SWIZZLE_NONE)
5138 return;
5139
5140 I915_WRITE(DISP_ARB_CTL, I915_READ(DISP_ARB_CTL) |
5141 DISP_TILE_SURFACE_SWIZZLING);
5142
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01005143 if (IS_GEN5(dev_priv))
Daniel Vetter11782b02012-01-31 16:47:55 +01005144 return;
5145
Daniel Vetterf691e2f2012-02-02 09:58:12 +01005146 I915_WRITE(TILECTL, I915_READ(TILECTL) | TILECTL_SWZCTL);
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01005147 if (IS_GEN6(dev_priv))
Daniel Vetter6b26c862012-04-24 14:04:12 +02005148 I915_WRITE(ARB_MODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_SNB));
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01005149 else if (IS_GEN7(dev_priv))
Daniel Vetter6b26c862012-04-24 14:04:12 +02005150 I915_WRITE(ARB_MODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_IVB));
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01005151 else if (IS_GEN8(dev_priv))
Ben Widawsky31a53362013-11-02 21:07:04 -07005152 I915_WRITE(GAMTARBMODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_BDW));
Ben Widawsky8782e262012-12-18 10:31:23 -08005153 else
5154 BUG();
Daniel Vetterf691e2f2012-02-02 09:58:12 +01005155}
Daniel Vettere21af882012-02-09 20:53:27 +01005156
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01005157static void init_unused_ring(struct drm_i915_private *dev_priv, u32 base)
Ville Syrjälä81e7f202014-08-15 01:21:55 +03005158{
Ville Syrjälä81e7f202014-08-15 01:21:55 +03005159 I915_WRITE(RING_CTL(base), 0);
5160 I915_WRITE(RING_HEAD(base), 0);
5161 I915_WRITE(RING_TAIL(base), 0);
5162 I915_WRITE(RING_START(base), 0);
5163}
5164
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01005165static void init_unused_rings(struct drm_i915_private *dev_priv)
Ville Syrjälä81e7f202014-08-15 01:21:55 +03005166{
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01005167 if (IS_I830(dev_priv)) {
5168 init_unused_ring(dev_priv, PRB1_BASE);
5169 init_unused_ring(dev_priv, SRB0_BASE);
5170 init_unused_ring(dev_priv, SRB1_BASE);
5171 init_unused_ring(dev_priv, SRB2_BASE);
5172 init_unused_ring(dev_priv, SRB3_BASE);
5173 } else if (IS_GEN2(dev_priv)) {
5174 init_unused_ring(dev_priv, SRB0_BASE);
5175 init_unused_ring(dev_priv, SRB1_BASE);
5176 } else if (IS_GEN3(dev_priv)) {
5177 init_unused_ring(dev_priv, PRB1_BASE);
5178 init_unused_ring(dev_priv, PRB2_BASE);
Ville Syrjälä81e7f202014-08-15 01:21:55 +03005179 }
5180}
5181
Chris Wilson20a8a742017-02-08 14:30:31 +00005182static int __i915_gem_restart_engines(void *data)
Ben Widawsky4fc7c972013-02-08 11:49:24 -08005183{
Chris Wilson20a8a742017-02-08 14:30:31 +00005184 struct drm_i915_private *i915 = data;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00005185 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05305186 enum intel_engine_id id;
Chris Wilson20a8a742017-02-08 14:30:31 +00005187 int err;
5188
5189 for_each_engine(engine, i915, id) {
5190 err = engine->init_hw(engine);
Chris Wilson8177e112018-02-07 11:15:45 +00005191 if (err) {
5192 DRM_ERROR("Failed to restart %s (%d)\n",
5193 engine->name, err);
Chris Wilson20a8a742017-02-08 14:30:31 +00005194 return err;
Chris Wilson8177e112018-02-07 11:15:45 +00005195 }
Chris Wilson20a8a742017-02-08 14:30:31 +00005196 }
5197
5198 return 0;
5199}
5200
5201int i915_gem_init_hw(struct drm_i915_private *dev_priv)
5202{
Chris Wilsond200cda2016-04-28 09:56:44 +01005203 int ret;
Ben Widawsky4fc7c972013-02-08 11:49:24 -08005204
Chris Wilsonde867c22016-10-25 13:16:02 +01005205 dev_priv->gt.last_init_time = ktime_get();
5206
Chris Wilson5e4f5182015-02-13 14:35:59 +00005207 /* Double layer security blanket, see i915_gem_init() */
5208 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
5209
Tvrtko Ursulin0031fb92016-11-04 14:42:44 +00005210 if (HAS_EDRAM(dev_priv) && INTEL_GEN(dev_priv) < 9)
Ben Widawsky05e21cc2013-07-04 11:02:04 -07005211 I915_WRITE(HSW_IDICR, I915_READ(HSW_IDICR) | IDIHASHMSK(0xf));
Ben Widawsky4fc7c972013-02-08 11:49:24 -08005212
Tvrtko Ursulin772c2a52016-10-13 11:03:01 +01005213 if (IS_HASWELL(dev_priv))
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01005214 I915_WRITE(MI_PREDICATE_RESULT_2, IS_HSW_GT3(dev_priv) ?
Ville Syrjälä0bf21342013-11-29 14:56:12 +02005215 LOWER_SLICE_ENABLED : LOWER_SLICE_DISABLED);
Rodrigo Vivi94353732013-08-28 16:45:46 -03005216
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01005217 if (HAS_PCH_NOP(dev_priv)) {
Tvrtko Ursulinfd6b8f42016-10-14 10:13:06 +01005218 if (IS_IVYBRIDGE(dev_priv)) {
Daniel Vetter6ba844b2014-01-22 23:39:30 +01005219 u32 temp = I915_READ(GEN7_MSG_CTL);
5220 temp &= ~(WAIT_FOR_PCH_FLR_ACK | WAIT_FOR_PCH_RESET_ACK);
5221 I915_WRITE(GEN7_MSG_CTL, temp);
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00005222 } else if (INTEL_GEN(dev_priv) >= 7) {
Daniel Vetter6ba844b2014-01-22 23:39:30 +01005223 u32 temp = I915_READ(HSW_NDE_RSTWRN_OPT);
5224 temp &= ~RESET_PCH_HANDSHAKE_ENABLE;
5225 I915_WRITE(HSW_NDE_RSTWRN_OPT, temp);
5226 }
Ben Widawsky88a2b2a2013-04-05 13:12:43 -07005227 }
5228
Oscar Mateo59b449d2018-04-10 09:12:47 -07005229 intel_gt_workarounds_apply(dev_priv);
5230
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00005231 i915_gem_init_swizzling(dev_priv);
Ben Widawsky4fc7c972013-02-08 11:49:24 -08005232
Daniel Vetterd5abdfd2014-11-20 09:45:19 +01005233 /*
5234 * At least 830 can leave some of the unused rings
5235 * "active" (ie. head != tail) after resume which
5236 * will prevent c3 entry. Makes sure all unused rings
5237 * are totally idle.
5238 */
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01005239 init_unused_rings(dev_priv);
Daniel Vetterd5abdfd2014-11-20 09:45:19 +01005240
Dave Gordoned54c1a2016-01-19 19:02:54 +00005241 BUG_ON(!dev_priv->kernel_context);
Chris Wilson6f74b362017-10-15 15:37:25 +01005242 if (i915_terminally_wedged(&dev_priv->gpu_error)) {
5243 ret = -EIO;
5244 goto out;
5245 }
John Harrison90638cc2015-05-29 17:43:37 +01005246
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00005247 ret = i915_ppgtt_init_hw(dev_priv);
John Harrison4ad2fd82015-06-18 13:11:20 +01005248 if (ret) {
Chris Wilson8177e112018-02-07 11:15:45 +00005249 DRM_ERROR("Enabling PPGTT failed (%d)\n", ret);
John Harrison4ad2fd82015-06-18 13:11:20 +01005250 goto out;
5251 }
5252
Jackie Lif08e2032018-03-13 17:32:53 -07005253 ret = intel_wopcm_init_hw(&dev_priv->wopcm);
5254 if (ret) {
5255 DRM_ERROR("Enabling WOPCM failed (%d)\n", ret);
5256 goto out;
5257 }
5258
Michał Winiarski9bdc3572017-10-25 18:25:19 +01005259 /* We can't enable contexts until all firmware is loaded */
5260 ret = intel_uc_init_hw(dev_priv);
Chris Wilson8177e112018-02-07 11:15:45 +00005261 if (ret) {
5262 DRM_ERROR("Enabling uc failed (%d)\n", ret);
Michał Winiarski9bdc3572017-10-25 18:25:19 +01005263 goto out;
Chris Wilson8177e112018-02-07 11:15:45 +00005264 }
Michał Winiarski9bdc3572017-10-25 18:25:19 +01005265
Tvrtko Ursulinbf9e8422016-12-01 14:16:38 +00005266 intel_mocs_init_l3cc_table(dev_priv);
Peter Antoine0ccdacf2016-04-13 15:03:25 +01005267
Chris Wilson136109c2017-11-02 13:14:30 +00005268 /* Only when the HW is re-initialised, can we replay the requests */
5269 ret = __i915_gem_restart_engines(dev_priv);
Chris Wilson5e4f5182015-02-13 14:35:59 +00005270out:
5271 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Ben Widawsky2fa48d82013-12-06 14:11:04 -08005272 return ret;
Zou Nan hai8187a2b2010-05-21 09:08:55 +08005273}
5274
Chris Wilsond2b4b972017-11-10 14:26:33 +00005275static int __intel_engines_record_defaults(struct drm_i915_private *i915)
5276{
5277 struct i915_gem_context *ctx;
5278 struct intel_engine_cs *engine;
5279 enum intel_engine_id id;
5280 int err;
5281
5282 /*
5283 * As we reset the gpu during very early sanitisation, the current
5284 * register state on the GPU should reflect its defaults values.
5285 * We load a context onto the hw (with restore-inhibit), then switch
5286 * over to a second context to save that default register state. We
5287 * can then prime every new context with that state so they all start
5288 * from the same default HW values.
5289 */
5290
5291 ctx = i915_gem_context_create_kernel(i915, 0);
5292 if (IS_ERR(ctx))
5293 return PTR_ERR(ctx);
5294
5295 for_each_engine(engine, i915, id) {
Chris Wilsone61e0f52018-02-21 09:56:36 +00005296 struct i915_request *rq;
Chris Wilsond2b4b972017-11-10 14:26:33 +00005297
Chris Wilsone61e0f52018-02-21 09:56:36 +00005298 rq = i915_request_alloc(engine, ctx);
Chris Wilsond2b4b972017-11-10 14:26:33 +00005299 if (IS_ERR(rq)) {
5300 err = PTR_ERR(rq);
5301 goto out_ctx;
5302 }
5303
Chris Wilson3fef5cd2017-11-20 10:20:02 +00005304 err = 0;
Chris Wilsond2b4b972017-11-10 14:26:33 +00005305 if (engine->init_context)
5306 err = engine->init_context(rq);
5307
Chris Wilsone61e0f52018-02-21 09:56:36 +00005308 __i915_request_add(rq, true);
Chris Wilsond2b4b972017-11-10 14:26:33 +00005309 if (err)
5310 goto err_active;
5311 }
5312
5313 err = i915_gem_switch_to_kernel_context(i915);
5314 if (err)
5315 goto err_active;
5316
5317 err = i915_gem_wait_for_idle(i915, I915_WAIT_LOCKED);
5318 if (err)
5319 goto err_active;
5320
5321 assert_kernel_context_is_current(i915);
5322
5323 for_each_engine(engine, i915, id) {
5324 struct i915_vma *state;
5325
Chris Wilsonab82a062018-04-30 14:15:01 +01005326 state = to_intel_context(ctx, engine)->state;
Chris Wilsond2b4b972017-11-10 14:26:33 +00005327 if (!state)
5328 continue;
5329
5330 /*
5331 * As we will hold a reference to the logical state, it will
5332 * not be torn down with the context, and importantly the
5333 * object will hold onto its vma (making it possible for a
5334 * stray GTT write to corrupt our defaults). Unmap the vma
5335 * from the GTT to prevent such accidents and reclaim the
5336 * space.
5337 */
5338 err = i915_vma_unbind(state);
5339 if (err)
5340 goto err_active;
5341
5342 err = i915_gem_object_set_to_cpu_domain(state->obj, false);
5343 if (err)
5344 goto err_active;
5345
5346 engine->default_state = i915_gem_object_get(state->obj);
5347 }
5348
5349 if (IS_ENABLED(CONFIG_DRM_I915_DEBUG_GEM)) {
5350 unsigned int found = intel_engines_has_context_isolation(i915);
5351
5352 /*
5353 * Make sure that classes with multiple engine instances all
5354 * share the same basic configuration.
5355 */
5356 for_each_engine(engine, i915, id) {
5357 unsigned int bit = BIT(engine->uabi_class);
5358 unsigned int expected = engine->default_state ? bit : 0;
5359
5360 if ((found & bit) != expected) {
5361 DRM_ERROR("mismatching default context state for class %d on engine %s\n",
5362 engine->uabi_class, engine->name);
5363 }
5364 }
5365 }
5366
5367out_ctx:
5368 i915_gem_context_set_closed(ctx);
5369 i915_gem_context_put(ctx);
5370 return err;
5371
5372err_active:
5373 /*
5374 * If we have to abandon now, we expect the engines to be idle
5375 * and ready to be torn-down. First try to flush any remaining
5376 * request, ensure we are pointing at the kernel context and
5377 * then remove it.
5378 */
5379 if (WARN_ON(i915_gem_switch_to_kernel_context(i915)))
5380 goto out_ctx;
5381
5382 if (WARN_ON(i915_gem_wait_for_idle(i915, I915_WAIT_LOCKED)))
5383 goto out_ctx;
5384
5385 i915_gem_contexts_lost(i915);
5386 goto out_ctx;
5387}
5388
Tvrtko Ursulinbf9e8422016-12-01 14:16:38 +00005389int i915_gem_init(struct drm_i915_private *dev_priv)
Chris Wilson1070a422012-04-24 15:47:41 +01005390{
Chris Wilson1070a422012-04-24 15:47:41 +01005391 int ret;
5392
Matthew Auldda9fe3f32017-10-06 23:18:31 +01005393 /*
5394 * We need to fallback to 4K pages since gvt gtt handling doesn't
5395 * support huge page entries - we will need to check either hypervisor
5396 * mm can support huge guest page or just do emulation in gvt.
5397 */
5398 if (intel_vgpu_active(dev_priv))
5399 mkwrite_device_info(dev_priv)->page_sizes =
5400 I915_GTT_PAGE_SIZE_4K;
5401
Chris Wilson94312822017-05-03 10:39:18 +01005402 dev_priv->mm.unordered_timeline = dma_fence_context_alloc(1);
Chris Wilson57822dc2017-02-22 11:40:48 +00005403
Chris Wilsonfb5c5512017-11-20 20:55:00 +00005404 if (HAS_LOGICAL_RING_CONTEXTS(dev_priv)) {
Chris Wilson821ed7d2016-09-09 14:11:53 +01005405 dev_priv->gt.resume = intel_lr_context_resume;
Tvrtko Ursulin117897f2016-03-16 11:00:40 +00005406 dev_priv->gt.cleanup_engine = intel_logical_ring_cleanup;
Chris Wilsonfb5c5512017-11-20 20:55:00 +00005407 } else {
5408 dev_priv->gt.resume = intel_legacy_submission_resume;
5409 dev_priv->gt.cleanup_engine = intel_engine_cleanup;
Oscar Mateoa83014d2014-07-24 17:04:21 +01005410 }
5411
Chris Wilsonee487002017-11-22 17:26:21 +00005412 ret = i915_gem_init_userptr(dev_priv);
5413 if (ret)
5414 return ret;
5415
Jackie Li6b0478f2018-03-13 17:32:50 -07005416 ret = intel_wopcm_init(&dev_priv->wopcm);
5417 if (ret)
5418 return ret;
5419
Sagar Arun Kamble70deead2018-01-24 21:16:58 +05305420 ret = intel_uc_init_misc(dev_priv);
Michał Winiarski3176ff42017-12-13 23:13:47 +01005421 if (ret)
5422 return ret;
5423
Chris Wilson5e4f5182015-02-13 14:35:59 +00005424 /* This is just a security blanket to placate dragons.
5425 * On some systems, we very sporadically observe that the first TLBs
5426 * used by the CS may be stale, despite us poking the TLB reset. If
5427 * we hold the forcewake during initialisation these problems
5428 * just magically go away.
5429 */
Chris Wilsonee487002017-11-22 17:26:21 +00005430 mutex_lock(&dev_priv->drm.struct_mutex);
Chris Wilson5e4f5182015-02-13 14:35:59 +00005431 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
5432
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01005433 ret = i915_gem_init_ggtt(dev_priv);
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00005434 if (ret) {
5435 GEM_BUG_ON(ret == -EIO);
5436 goto err_unlock;
5437 }
Jesse Barnesd62b4892013-03-08 10:45:53 -08005438
Chris Wilson829a0af2017-06-20 12:05:45 +01005439 ret = i915_gem_contexts_init(dev_priv);
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00005440 if (ret) {
5441 GEM_BUG_ON(ret == -EIO);
5442 goto err_ggtt;
5443 }
Ben Widawsky2fa48d82013-12-06 14:11:04 -08005444
Tvrtko Ursulinbf9e8422016-12-01 14:16:38 +00005445 ret = intel_engines_init(dev_priv);
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00005446 if (ret) {
5447 GEM_BUG_ON(ret == -EIO);
5448 goto err_context;
5449 }
Daniel Vetter53ca26c2012-04-26 23:28:03 +02005450
Chris Wilsonf58d13d2017-11-10 14:26:29 +00005451 intel_init_gt_powersave(dev_priv);
5452
Michał Winiarski61b5c152017-12-13 23:13:48 +01005453 ret = intel_uc_init(dev_priv);
Chris Wilsoncc6a8182017-11-10 14:26:30 +00005454 if (ret)
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00005455 goto err_pm;
Chris Wilsoncc6a8182017-11-10 14:26:30 +00005456
Michał Winiarski61b5c152017-12-13 23:13:48 +01005457 ret = i915_gem_init_hw(dev_priv);
5458 if (ret)
5459 goto err_uc_init;
5460
Chris Wilsoncc6a8182017-11-10 14:26:30 +00005461 /*
5462 * Despite its name intel_init_clock_gating applies both display
5463 * clock gating workarounds; GT mmio workarounds and the occasional
5464 * GT power context workaround. Worse, sometimes it includes a context
5465 * register workaround which we need to apply before we record the
5466 * default HW state for all contexts.
5467 *
5468 * FIXME: break up the workarounds and apply them at the right time!
5469 */
5470 intel_init_clock_gating(dev_priv);
5471
Chris Wilsond2b4b972017-11-10 14:26:33 +00005472 ret = __intel_engines_record_defaults(dev_priv);
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00005473 if (ret)
5474 goto err_init_hw;
5475
5476 if (i915_inject_load_failure()) {
5477 ret = -ENODEV;
5478 goto err_init_hw;
5479 }
5480
5481 if (i915_inject_load_failure()) {
5482 ret = -EIO;
5483 goto err_init_hw;
5484 }
5485
5486 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
5487 mutex_unlock(&dev_priv->drm.struct_mutex);
5488
5489 return 0;
5490
5491 /*
5492 * Unwinding is complicated by that we want to handle -EIO to mean
5493 * disable GPU submission but keep KMS alive. We want to mark the
5494 * HW as irrevisibly wedged, but keep enough state around that the
5495 * driver doesn't explode during runtime.
5496 */
5497err_init_hw:
5498 i915_gem_wait_for_idle(dev_priv, I915_WAIT_LOCKED);
5499 i915_gem_contexts_lost(dev_priv);
5500 intel_uc_fini_hw(dev_priv);
Michał Winiarski61b5c152017-12-13 23:13:48 +01005501err_uc_init:
5502 intel_uc_fini(dev_priv);
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00005503err_pm:
5504 if (ret != -EIO) {
5505 intel_cleanup_gt_powersave(dev_priv);
5506 i915_gem_cleanup_engines(dev_priv);
5507 }
5508err_context:
5509 if (ret != -EIO)
5510 i915_gem_contexts_fini(dev_priv);
5511err_ggtt:
5512err_unlock:
5513 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
5514 mutex_unlock(&dev_priv->drm.struct_mutex);
5515
Sagar Arun Kamble70deead2018-01-24 21:16:58 +05305516 intel_uc_fini_misc(dev_priv);
Sagar Arun Kambleda943b52018-01-10 18:24:16 +05305517
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00005518 if (ret != -EIO)
5519 i915_gem_cleanup_userptr(dev_priv);
5520
Chris Wilson60990322014-04-09 09:19:42 +01005521 if (ret == -EIO) {
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00005522 /*
5523 * Allow engine initialisation to fail by marking the GPU as
Chris Wilson60990322014-04-09 09:19:42 +01005524 * wedged. But we only want to do this where the GPU is angry,
5525 * for all other failure, such as an allocation failure, bail.
5526 */
Chris Wilson6f74b362017-10-15 15:37:25 +01005527 if (!i915_terminally_wedged(&dev_priv->gpu_error)) {
5528 DRM_ERROR("Failed to initialize GPU, declaring it wedged\n");
5529 i915_gem_set_wedged(dev_priv);
5530 }
Chris Wilson60990322014-04-09 09:19:42 +01005531 ret = 0;
Chris Wilson1070a422012-04-24 15:47:41 +01005532 }
5533
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00005534 i915_gem_drain_freed_objects(dev_priv);
Chris Wilson60990322014-04-09 09:19:42 +01005535 return ret;
Chris Wilson1070a422012-04-24 15:47:41 +01005536}
5537
Chris Wilson24145512017-01-24 11:01:35 +00005538void i915_gem_init_mmio(struct drm_i915_private *i915)
5539{
5540 i915_gem_sanitize(i915);
5541}
5542
Zou Nan hai8187a2b2010-05-21 09:08:55 +08005543void
Tvrtko Ursulincb15d9f2016-12-01 14:16:39 +00005544i915_gem_cleanup_engines(struct drm_i915_private *dev_priv)
Zou Nan hai8187a2b2010-05-21 09:08:55 +08005545{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00005546 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05305547 enum intel_engine_id id;
Zou Nan hai8187a2b2010-05-21 09:08:55 +08005548
Akash Goel3b3f1652016-10-13 22:44:48 +05305549 for_each_engine(engine, dev_priv, id)
Tvrtko Ursulin117897f2016-03-16 11:00:40 +00005550 dev_priv->gt.cleanup_engine(engine);
Zou Nan hai8187a2b2010-05-21 09:08:55 +08005551}
5552
Eric Anholt673a3942008-07-30 12:06:12 -07005553void
Imre Deak40ae4e12016-03-16 14:54:03 +02005554i915_gem_load_init_fences(struct drm_i915_private *dev_priv)
5555{
Chris Wilson49ef5292016-08-18 17:17:00 +01005556 int i;
Imre Deak40ae4e12016-03-16 14:54:03 +02005557
Tvrtko Ursulinc56b89f2018-02-09 21:58:46 +00005558 if (INTEL_GEN(dev_priv) >= 7 && !IS_VALLEYVIEW(dev_priv) &&
Imre Deak40ae4e12016-03-16 14:54:03 +02005559 !IS_CHERRYVIEW(dev_priv))
5560 dev_priv->num_fence_regs = 32;
Tvrtko Ursulinc56b89f2018-02-09 21:58:46 +00005561 else if (INTEL_GEN(dev_priv) >= 4 ||
Jani Nikula73f67aa2016-12-07 22:48:09 +02005562 IS_I945G(dev_priv) || IS_I945GM(dev_priv) ||
5563 IS_G33(dev_priv) || IS_PINEVIEW(dev_priv))
Imre Deak40ae4e12016-03-16 14:54:03 +02005564 dev_priv->num_fence_regs = 16;
5565 else
5566 dev_priv->num_fence_regs = 8;
5567
Chris Wilsonc0336662016-05-06 15:40:21 +01005568 if (intel_vgpu_active(dev_priv))
Imre Deak40ae4e12016-03-16 14:54:03 +02005569 dev_priv->num_fence_regs =
5570 I915_READ(vgtif_reg(avail_rs.fence_num));
5571
5572 /* Initialize fence registers to zero */
Chris Wilson49ef5292016-08-18 17:17:00 +01005573 for (i = 0; i < dev_priv->num_fence_regs; i++) {
5574 struct drm_i915_fence_reg *fence = &dev_priv->fence_regs[i];
5575
5576 fence->i915 = dev_priv;
5577 fence->id = i;
5578 list_add_tail(&fence->link, &dev_priv->mm.fence_list);
5579 }
Tvrtko Ursulin4362f4f2016-11-16 08:55:33 +00005580 i915_gem_restore_fences(dev_priv);
Imre Deak40ae4e12016-03-16 14:54:03 +02005581
Tvrtko Ursulin4362f4f2016-11-16 08:55:33 +00005582 i915_gem_detect_bit_6_swizzle(dev_priv);
Imre Deak40ae4e12016-03-16 14:54:03 +02005583}
5584
Chris Wilson9c52d1c2017-11-10 23:24:47 +00005585static void i915_gem_init__mm(struct drm_i915_private *i915)
5586{
5587 spin_lock_init(&i915->mm.object_stat_lock);
5588 spin_lock_init(&i915->mm.obj_lock);
5589 spin_lock_init(&i915->mm.free_lock);
5590
5591 init_llist_head(&i915->mm.free_list);
5592
5593 INIT_LIST_HEAD(&i915->mm.unbound_list);
5594 INIT_LIST_HEAD(&i915->mm.bound_list);
5595 INIT_LIST_HEAD(&i915->mm.fence_list);
5596 INIT_LIST_HEAD(&i915->mm.userfault_list);
5597
5598 INIT_WORK(&i915->mm.free_work, __i915_gem_free_work);
5599}
5600
Michal Wajdeczkoa0de9082018-03-23 12:34:49 +00005601int i915_gem_init_early(struct drm_i915_private *dev_priv)
Eric Anholt673a3942008-07-30 12:06:12 -07005602{
Tvrtko Ursulina9335682016-11-02 15:14:59 +00005603 int err = -ENOMEM;
Chris Wilson42dcedd2012-11-15 11:32:30 +00005604
Tvrtko Ursulina9335682016-11-02 15:14:59 +00005605 dev_priv->objects = KMEM_CACHE(drm_i915_gem_object, SLAB_HWCACHE_ALIGN);
5606 if (!dev_priv->objects)
Chris Wilson73cb9702016-10-28 13:58:46 +01005607 goto err_out;
Chris Wilson73cb9702016-10-28 13:58:46 +01005608
Tvrtko Ursulina9335682016-11-02 15:14:59 +00005609 dev_priv->vmas = KMEM_CACHE(i915_vma, SLAB_HWCACHE_ALIGN);
5610 if (!dev_priv->vmas)
Chris Wilson73cb9702016-10-28 13:58:46 +01005611 goto err_objects;
Chris Wilson73cb9702016-10-28 13:58:46 +01005612
Chris Wilsond1b48c12017-08-16 09:52:08 +01005613 dev_priv->luts = KMEM_CACHE(i915_lut_handle, 0);
5614 if (!dev_priv->luts)
5615 goto err_vmas;
5616
Chris Wilsone61e0f52018-02-21 09:56:36 +00005617 dev_priv->requests = KMEM_CACHE(i915_request,
Tvrtko Ursulina9335682016-11-02 15:14:59 +00005618 SLAB_HWCACHE_ALIGN |
5619 SLAB_RECLAIM_ACCOUNT |
Paul E. McKenney5f0d5a32017-01-18 02:53:44 -08005620 SLAB_TYPESAFE_BY_RCU);
Tvrtko Ursulina9335682016-11-02 15:14:59 +00005621 if (!dev_priv->requests)
Chris Wilsond1b48c12017-08-16 09:52:08 +01005622 goto err_luts;
Chris Wilson73cb9702016-10-28 13:58:46 +01005623
Chris Wilson52e54202016-11-14 20:41:02 +00005624 dev_priv->dependencies = KMEM_CACHE(i915_dependency,
5625 SLAB_HWCACHE_ALIGN |
5626 SLAB_RECLAIM_ACCOUNT);
5627 if (!dev_priv->dependencies)
5628 goto err_requests;
5629
Chris Wilsonc5cf9a92017-05-17 13:10:04 +01005630 dev_priv->priorities = KMEM_CACHE(i915_priolist, SLAB_HWCACHE_ALIGN);
5631 if (!dev_priv->priorities)
5632 goto err_dependencies;
5633
Chris Wilson73cb9702016-10-28 13:58:46 +01005634 INIT_LIST_HEAD(&dev_priv->gt.timelines);
Chris Wilson643b4502018-04-30 14:15:03 +01005635 INIT_LIST_HEAD(&dev_priv->gt.active_rings);
Chris Wilson3365e222018-05-03 20:51:14 +01005636 INIT_LIST_HEAD(&dev_priv->gt.closed_vma);
Chris Wilson643b4502018-04-30 14:15:03 +01005637
Chris Wilson9c52d1c2017-11-10 23:24:47 +00005638 i915_gem_init__mm(dev_priv);
Chris Wilsonf2123812017-10-16 12:40:37 +01005639
Chris Wilson67d97da2016-07-04 08:08:31 +01005640 INIT_DELAYED_WORK(&dev_priv->gt.retire_work,
Eric Anholt673a3942008-07-30 12:06:12 -07005641 i915_gem_retire_work_handler);
Chris Wilson67d97da2016-07-04 08:08:31 +01005642 INIT_DELAYED_WORK(&dev_priv->gt.idle_work,
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005643 i915_gem_idle_work_handler);
Chris Wilson1f15b762016-07-01 17:23:14 +01005644 init_waitqueue_head(&dev_priv->gpu_error.wait_queue);
Daniel Vetter1f83fee2012-11-15 17:17:22 +01005645 init_waitqueue_head(&dev_priv->gpu_error.reset_queue);
Chris Wilson31169712009-09-14 16:50:28 +01005646
Joonas Lahtinen6f633402016-09-01 14:58:21 +03005647 atomic_set(&dev_priv->mm.bsd_engine_dispatch_index, 0);
5648
Chris Wilsonb5add952016-08-04 16:32:36 +01005649 spin_lock_init(&dev_priv->fb_tracking.lock);
Chris Wilson73cb9702016-10-28 13:58:46 +01005650
Matthew Auld465c4032017-10-06 23:18:14 +01005651 err = i915_gemfs_init(dev_priv);
5652 if (err)
5653 DRM_NOTE("Unable to create a private tmpfs mount, hugepage support will be disabled(%d).\n", err);
5654
Chris Wilson73cb9702016-10-28 13:58:46 +01005655 return 0;
5656
Chris Wilson52e54202016-11-14 20:41:02 +00005657err_dependencies:
5658 kmem_cache_destroy(dev_priv->dependencies);
Chris Wilson73cb9702016-10-28 13:58:46 +01005659err_requests:
5660 kmem_cache_destroy(dev_priv->requests);
Chris Wilsond1b48c12017-08-16 09:52:08 +01005661err_luts:
5662 kmem_cache_destroy(dev_priv->luts);
Chris Wilson73cb9702016-10-28 13:58:46 +01005663err_vmas:
5664 kmem_cache_destroy(dev_priv->vmas);
5665err_objects:
5666 kmem_cache_destroy(dev_priv->objects);
5667err_out:
5668 return err;
Eric Anholt673a3942008-07-30 12:06:12 -07005669}
Dave Airlie71acb5e2008-12-30 20:31:46 +10005670
Michal Wajdeczkoa0de9082018-03-23 12:34:49 +00005671void i915_gem_cleanup_early(struct drm_i915_private *dev_priv)
Imre Deakd64aa092016-01-19 15:26:29 +02005672{
Chris Wilsonc4d4c1c2017-02-10 16:35:23 +00005673 i915_gem_drain_freed_objects(dev_priv);
Chris Wilsonc9c704712018-02-19 22:06:31 +00005674 GEM_BUG_ON(!llist_empty(&dev_priv->mm.free_list));
5675 GEM_BUG_ON(atomic_read(&dev_priv->mm.free_count));
Chris Wilsonc4d4c1c2017-02-10 16:35:23 +00005676 WARN_ON(dev_priv->mm.object_count);
Matthew Auldea84aa72016-11-17 21:04:11 +00005677 WARN_ON(!list_empty(&dev_priv->gt.timelines));
Matthew Auldea84aa72016-11-17 21:04:11 +00005678
Chris Wilsonc5cf9a92017-05-17 13:10:04 +01005679 kmem_cache_destroy(dev_priv->priorities);
Chris Wilson52e54202016-11-14 20:41:02 +00005680 kmem_cache_destroy(dev_priv->dependencies);
Imre Deakd64aa092016-01-19 15:26:29 +02005681 kmem_cache_destroy(dev_priv->requests);
Chris Wilsond1b48c12017-08-16 09:52:08 +01005682 kmem_cache_destroy(dev_priv->luts);
Imre Deakd64aa092016-01-19 15:26:29 +02005683 kmem_cache_destroy(dev_priv->vmas);
5684 kmem_cache_destroy(dev_priv->objects);
Chris Wilson0eafec62016-08-04 16:32:41 +01005685
5686 /* And ensure that our DESTROY_BY_RCU slabs are truly destroyed */
5687 rcu_barrier();
Matthew Auld465c4032017-10-06 23:18:14 +01005688
5689 i915_gemfs_fini(dev_priv);
Imre Deakd64aa092016-01-19 15:26:29 +02005690}
5691
Chris Wilson6a800ea2016-09-21 14:51:07 +01005692int i915_gem_freeze(struct drm_i915_private *dev_priv)
5693{
Chris Wilsond0aa3012017-04-07 11:25:49 +01005694 /* Discard all purgeable objects, let userspace recover those as
5695 * required after resuming.
5696 */
Chris Wilson6a800ea2016-09-21 14:51:07 +01005697 i915_gem_shrink_all(dev_priv);
Chris Wilson6a800ea2016-09-21 14:51:07 +01005698
Chris Wilson6a800ea2016-09-21 14:51:07 +01005699 return 0;
5700}
5701
Chris Wilson461fb992016-05-14 07:26:33 +01005702int i915_gem_freeze_late(struct drm_i915_private *dev_priv)
5703{
5704 struct drm_i915_gem_object *obj;
Chris Wilson7aab2d52016-09-09 20:02:18 +01005705 struct list_head *phases[] = {
5706 &dev_priv->mm.unbound_list,
5707 &dev_priv->mm.bound_list,
5708 NULL
5709 }, **p;
Chris Wilson461fb992016-05-14 07:26:33 +01005710
5711 /* Called just before we write the hibernation image.
5712 *
5713 * We need to update the domain tracking to reflect that the CPU
5714 * will be accessing all the pages to create and restore from the
5715 * hibernation, and so upon restoration those pages will be in the
5716 * CPU domain.
5717 *
5718 * To make sure the hibernation image contains the latest state,
5719 * we update that state just before writing out the image.
Chris Wilson7aab2d52016-09-09 20:02:18 +01005720 *
5721 * To try and reduce the hibernation image, we manually shrink
Chris Wilsond0aa3012017-04-07 11:25:49 +01005722 * the objects as well, see i915_gem_freeze()
Chris Wilson461fb992016-05-14 07:26:33 +01005723 */
5724
Chris Wilson912d5722017-09-06 16:19:30 -07005725 i915_gem_shrink(dev_priv, -1UL, NULL, I915_SHRINK_UNBOUND);
Chris Wilson17b93c42017-04-07 11:25:50 +01005726 i915_gem_drain_freed_objects(dev_priv);
Chris Wilson461fb992016-05-14 07:26:33 +01005727
Chris Wilsonf2123812017-10-16 12:40:37 +01005728 spin_lock(&dev_priv->mm.obj_lock);
Chris Wilson7aab2d52016-09-09 20:02:18 +01005729 for (p = phases; *p; p++) {
Chris Wilsonf2123812017-10-16 12:40:37 +01005730 list_for_each_entry(obj, *p, mm.link)
Chris Wilsone27ab732017-06-15 13:38:49 +01005731 __start_cpu_write(obj);
Chris Wilson461fb992016-05-14 07:26:33 +01005732 }
Chris Wilsonf2123812017-10-16 12:40:37 +01005733 spin_unlock(&dev_priv->mm.obj_lock);
Chris Wilson461fb992016-05-14 07:26:33 +01005734
5735 return 0;
5736}
5737
Chris Wilsonf787a5f2010-09-24 16:02:42 +01005738void i915_gem_release(struct drm_device *dev, struct drm_file *file)
Eric Anholtb9624422009-06-03 07:27:35 +00005739{
Chris Wilsonf787a5f2010-09-24 16:02:42 +01005740 struct drm_i915_file_private *file_priv = file->driver_priv;
Chris Wilsone61e0f52018-02-21 09:56:36 +00005741 struct i915_request *request;
Eric Anholtb9624422009-06-03 07:27:35 +00005742
5743 /* Clean up our request list when the client is going away, so that
5744 * later retire_requests won't dereference our soon-to-be-gone
5745 * file_priv.
5746 */
Chris Wilson1c255952010-09-26 11:03:27 +01005747 spin_lock(&file_priv->mm.lock);
Chris Wilsonc8659ef2017-03-02 12:25:25 +00005748 list_for_each_entry(request, &file_priv->mm.request_list, client_link)
Chris Wilsonf787a5f2010-09-24 16:02:42 +01005749 request->file_priv = NULL;
Chris Wilson1c255952010-09-26 11:03:27 +01005750 spin_unlock(&file_priv->mm.lock);
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005751}
5752
Chris Wilson829a0af2017-06-20 12:05:45 +01005753int i915_gem_open(struct drm_i915_private *i915, struct drm_file *file)
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005754{
5755 struct drm_i915_file_private *file_priv;
Ben Widawskye422b882013-12-06 14:10:58 -08005756 int ret;
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005757
Chris Wilsonc4c29d72016-11-09 10:45:07 +00005758 DRM_DEBUG("\n");
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005759
5760 file_priv = kzalloc(sizeof(*file_priv), GFP_KERNEL);
5761 if (!file_priv)
5762 return -ENOMEM;
5763
5764 file->driver_priv = file_priv;
Chris Wilson829a0af2017-06-20 12:05:45 +01005765 file_priv->dev_priv = i915;
Chris Wilsonab0e7ff2014-02-25 17:11:24 +02005766 file_priv->file = file;
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005767
5768 spin_lock_init(&file_priv->mm.lock);
5769 INIT_LIST_HEAD(&file_priv->mm.request_list);
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005770
Chris Wilsonc80ff162016-07-27 09:07:27 +01005771 file_priv->bsd_engine = -1;
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00005772
Chris Wilson829a0af2017-06-20 12:05:45 +01005773 ret = i915_gem_context_open(i915, file);
Ben Widawskye422b882013-12-06 14:10:58 -08005774 if (ret)
5775 kfree(file_priv);
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005776
Ben Widawskye422b882013-12-06 14:10:58 -08005777 return ret;
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005778}
5779
Daniel Vetterb680c372014-09-19 18:27:27 +02005780/**
5781 * i915_gem_track_fb - update frontbuffer tracking
Geliang Tangd9072a32015-09-15 05:58:44 -07005782 * @old: current GEM buffer for the frontbuffer slots
5783 * @new: new GEM buffer for the frontbuffer slots
5784 * @frontbuffer_bits: bitmask of frontbuffer slots
Daniel Vetterb680c372014-09-19 18:27:27 +02005785 *
5786 * This updates the frontbuffer tracking bits @frontbuffer_bits by clearing them
5787 * from @old and setting them in @new. Both @old and @new can be NULL.
5788 */
Daniel Vettera071fa02014-06-18 23:28:09 +02005789void i915_gem_track_fb(struct drm_i915_gem_object *old,
5790 struct drm_i915_gem_object *new,
5791 unsigned frontbuffer_bits)
5792{
Chris Wilsonfaf5bf02016-08-04 16:32:37 +01005793 /* Control of individual bits within the mask are guarded by
5794 * the owning plane->mutex, i.e. we can never see concurrent
5795 * manipulation of individual bits. But since the bitfield as a whole
5796 * is updated using RMW, we need to use atomics in order to update
5797 * the bits.
5798 */
5799 BUILD_BUG_ON(INTEL_FRONTBUFFER_BITS_PER_PIPE * I915_MAX_PIPES >
5800 sizeof(atomic_t) * BITS_PER_BYTE);
5801
Daniel Vettera071fa02014-06-18 23:28:09 +02005802 if (old) {
Chris Wilsonfaf5bf02016-08-04 16:32:37 +01005803 WARN_ON(!(atomic_read(&old->frontbuffer_bits) & frontbuffer_bits));
5804 atomic_andnot(frontbuffer_bits, &old->frontbuffer_bits);
Daniel Vettera071fa02014-06-18 23:28:09 +02005805 }
5806
5807 if (new) {
Chris Wilsonfaf5bf02016-08-04 16:32:37 +01005808 WARN_ON(atomic_read(&new->frontbuffer_bits) & frontbuffer_bits);
5809 atomic_or(frontbuffer_bits, &new->frontbuffer_bits);
Daniel Vettera071fa02014-06-18 23:28:09 +02005810 }
5811}
5812
Dave Gordonea702992015-07-09 19:29:02 +01005813/* Allocate a new GEM object and fill it with the supplied data */
5814struct drm_i915_gem_object *
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +00005815i915_gem_object_create_from_data(struct drm_i915_private *dev_priv,
Dave Gordonea702992015-07-09 19:29:02 +01005816 const void *data, size_t size)
5817{
5818 struct drm_i915_gem_object *obj;
Chris Wilsonbe062fa2017-03-17 19:46:48 +00005819 struct file *file;
5820 size_t offset;
5821 int err;
Dave Gordonea702992015-07-09 19:29:02 +01005822
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +00005823 obj = i915_gem_object_create(dev_priv, round_up(size, PAGE_SIZE));
Chris Wilsonfe3db792016-04-25 13:32:13 +01005824 if (IS_ERR(obj))
Dave Gordonea702992015-07-09 19:29:02 +01005825 return obj;
5826
Christian Königc0a51fd2018-02-16 13:43:38 +01005827 GEM_BUG_ON(obj->write_domain != I915_GEM_DOMAIN_CPU);
Dave Gordonea702992015-07-09 19:29:02 +01005828
Chris Wilsonbe062fa2017-03-17 19:46:48 +00005829 file = obj->base.filp;
5830 offset = 0;
5831 do {
5832 unsigned int len = min_t(typeof(size), size, PAGE_SIZE);
5833 struct page *page;
5834 void *pgdata, *vaddr;
Dave Gordonea702992015-07-09 19:29:02 +01005835
Chris Wilsonbe062fa2017-03-17 19:46:48 +00005836 err = pagecache_write_begin(file, file->f_mapping,
5837 offset, len, 0,
5838 &page, &pgdata);
5839 if (err < 0)
5840 goto fail;
Dave Gordonea702992015-07-09 19:29:02 +01005841
Chris Wilsonbe062fa2017-03-17 19:46:48 +00005842 vaddr = kmap(page);
5843 memcpy(vaddr, data, len);
5844 kunmap(page);
5845
5846 err = pagecache_write_end(file, file->f_mapping,
5847 offset, len, len,
5848 page, pgdata);
5849 if (err < 0)
5850 goto fail;
5851
5852 size -= len;
5853 data += len;
5854 offset += len;
5855 } while (size);
Dave Gordonea702992015-07-09 19:29:02 +01005856
5857 return obj;
5858
5859fail:
Chris Wilsonf8c417c2016-07-20 13:31:53 +01005860 i915_gem_object_put(obj);
Chris Wilsonbe062fa2017-03-17 19:46:48 +00005861 return ERR_PTR(err);
Dave Gordonea702992015-07-09 19:29:02 +01005862}
Chris Wilson96d77632016-10-28 13:58:33 +01005863
5864struct scatterlist *
5865i915_gem_object_get_sg(struct drm_i915_gem_object *obj,
5866 unsigned int n,
5867 unsigned int *offset)
5868{
Chris Wilsona4f5ea62016-10-28 13:58:35 +01005869 struct i915_gem_object_page_iter *iter = &obj->mm.get_page;
Chris Wilson96d77632016-10-28 13:58:33 +01005870 struct scatterlist *sg;
5871 unsigned int idx, count;
5872
5873 might_sleep();
5874 GEM_BUG_ON(n >= obj->base.size >> PAGE_SHIFT);
Chris Wilsona4f5ea62016-10-28 13:58:35 +01005875 GEM_BUG_ON(!i915_gem_object_has_pinned_pages(obj));
Chris Wilson96d77632016-10-28 13:58:33 +01005876
5877 /* As we iterate forward through the sg, we record each entry in a
5878 * radixtree for quick repeated (backwards) lookups. If we have seen
5879 * this index previously, we will have an entry for it.
5880 *
5881 * Initial lookup is O(N), but this is amortized to O(1) for
5882 * sequential page access (where each new request is consecutive
5883 * to the previous one). Repeated lookups are O(lg(obj->base.size)),
5884 * i.e. O(1) with a large constant!
5885 */
5886 if (n < READ_ONCE(iter->sg_idx))
5887 goto lookup;
5888
5889 mutex_lock(&iter->lock);
5890
5891 /* We prefer to reuse the last sg so that repeated lookup of this
5892 * (or the subsequent) sg are fast - comparing against the last
5893 * sg is faster than going through the radixtree.
5894 */
5895
5896 sg = iter->sg_pos;
5897 idx = iter->sg_idx;
5898 count = __sg_page_count(sg);
5899
5900 while (idx + count <= n) {
5901 unsigned long exception, i;
5902 int ret;
5903
5904 /* If we cannot allocate and insert this entry, or the
5905 * individual pages from this range, cancel updating the
5906 * sg_idx so that on this lookup we are forced to linearly
5907 * scan onwards, but on future lookups we will try the
5908 * insertion again (in which case we need to be careful of
5909 * the error return reporting that we have already inserted
5910 * this index).
5911 */
5912 ret = radix_tree_insert(&iter->radix, idx, sg);
5913 if (ret && ret != -EEXIST)
5914 goto scan;
5915
5916 exception =
5917 RADIX_TREE_EXCEPTIONAL_ENTRY |
5918 idx << RADIX_TREE_EXCEPTIONAL_SHIFT;
5919 for (i = 1; i < count; i++) {
5920 ret = radix_tree_insert(&iter->radix, idx + i,
5921 (void *)exception);
5922 if (ret && ret != -EEXIST)
5923 goto scan;
5924 }
5925
5926 idx += count;
5927 sg = ____sg_next(sg);
5928 count = __sg_page_count(sg);
5929 }
5930
5931scan:
5932 iter->sg_pos = sg;
5933 iter->sg_idx = idx;
5934
5935 mutex_unlock(&iter->lock);
5936
5937 if (unlikely(n < idx)) /* insertion completed by another thread */
5938 goto lookup;
5939
5940 /* In case we failed to insert the entry into the radixtree, we need
5941 * to look beyond the current sg.
5942 */
5943 while (idx + count <= n) {
5944 idx += count;
5945 sg = ____sg_next(sg);
5946 count = __sg_page_count(sg);
5947 }
5948
5949 *offset = n - idx;
5950 return sg;
5951
5952lookup:
5953 rcu_read_lock();
5954
5955 sg = radix_tree_lookup(&iter->radix, n);
5956 GEM_BUG_ON(!sg);
5957
5958 /* If this index is in the middle of multi-page sg entry,
5959 * the radixtree will contain an exceptional entry that points
5960 * to the start of that range. We will return the pointer to
5961 * the base page and the offset of this page within the
5962 * sg entry's range.
5963 */
5964 *offset = 0;
5965 if (unlikely(radix_tree_exception(sg))) {
5966 unsigned long base =
5967 (unsigned long)sg >> RADIX_TREE_EXCEPTIONAL_SHIFT;
5968
5969 sg = radix_tree_lookup(&iter->radix, base);
5970 GEM_BUG_ON(!sg);
5971
5972 *offset = n - base;
5973 }
5974
5975 rcu_read_unlock();
5976
5977 return sg;
5978}
5979
5980struct page *
5981i915_gem_object_get_page(struct drm_i915_gem_object *obj, unsigned int n)
5982{
5983 struct scatterlist *sg;
5984 unsigned int offset;
5985
5986 GEM_BUG_ON(!i915_gem_object_has_struct_page(obj));
5987
5988 sg = i915_gem_object_get_sg(obj, n, &offset);
5989 return nth_page(sg_page(sg), offset);
5990}
5991
5992/* Like i915_gem_object_get_page(), but mark the returned page dirty */
5993struct page *
5994i915_gem_object_get_dirty_page(struct drm_i915_gem_object *obj,
5995 unsigned int n)
5996{
5997 struct page *page;
5998
5999 page = i915_gem_object_get_page(obj, n);
Chris Wilsona4f5ea62016-10-28 13:58:35 +01006000 if (!obj->mm.dirty)
Chris Wilson96d77632016-10-28 13:58:33 +01006001 set_page_dirty(page);
6002
6003 return page;
6004}
6005
6006dma_addr_t
6007i915_gem_object_get_dma_address(struct drm_i915_gem_object *obj,
6008 unsigned long n)
6009{
6010 struct scatterlist *sg;
6011 unsigned int offset;
6012
6013 sg = i915_gem_object_get_sg(obj, n, &offset);
6014 return sg_dma_address(sg) + (offset << PAGE_SHIFT);
6015}
Chris Wilson935a2f72017-02-13 17:15:13 +00006016
Chris Wilson8eeb7902017-07-26 19:16:01 +01006017int i915_gem_object_attach_phys(struct drm_i915_gem_object *obj, int align)
6018{
6019 struct sg_table *pages;
6020 int err;
6021
6022 if (align > obj->base.size)
6023 return -EINVAL;
6024
6025 if (obj->ops == &i915_gem_phys_ops)
6026 return 0;
6027
6028 if (obj->ops != &i915_gem_object_ops)
6029 return -EINVAL;
6030
6031 err = i915_gem_object_unbind(obj);
6032 if (err)
6033 return err;
6034
6035 mutex_lock(&obj->mm.lock);
6036
6037 if (obj->mm.madv != I915_MADV_WILLNEED) {
6038 err = -EFAULT;
6039 goto err_unlock;
6040 }
6041
6042 if (obj->mm.quirked) {
6043 err = -EFAULT;
6044 goto err_unlock;
6045 }
6046
6047 if (obj->mm.mapping) {
6048 err = -EBUSY;
6049 goto err_unlock;
6050 }
6051
Chris Wilsonf2123812017-10-16 12:40:37 +01006052 pages = fetch_and_zero(&obj->mm.pages);
6053 if (pages) {
6054 struct drm_i915_private *i915 = to_i915(obj->base.dev);
6055
6056 __i915_gem_object_reset_page_iter(obj);
6057
6058 spin_lock(&i915->mm.obj_lock);
6059 list_del(&obj->mm.link);
6060 spin_unlock(&i915->mm.obj_lock);
6061 }
6062
Chris Wilson8eeb7902017-07-26 19:16:01 +01006063 obj->ops = &i915_gem_phys_ops;
6064
Chris Wilson8fb6a5d2017-07-26 19:16:02 +01006065 err = ____i915_gem_object_get_pages(obj);
Chris Wilson8eeb7902017-07-26 19:16:01 +01006066 if (err)
6067 goto err_xfer;
6068
6069 /* Perma-pin (until release) the physical set of pages */
6070 __i915_gem_object_pin_pages(obj);
6071
6072 if (!IS_ERR_OR_NULL(pages))
6073 i915_gem_object_ops.put_pages(obj, pages);
6074 mutex_unlock(&obj->mm.lock);
6075 return 0;
6076
6077err_xfer:
6078 obj->ops = &i915_gem_object_ops;
6079 obj->mm.pages = pages;
6080err_unlock:
6081 mutex_unlock(&obj->mm.lock);
6082 return err;
6083}
6084
Chris Wilson935a2f72017-02-13 17:15:13 +00006085#if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
6086#include "selftests/scatterlist.c"
Chris Wilson66d9cb52017-02-13 17:15:17 +00006087#include "selftests/mock_gem_device.c"
Chris Wilson44653982017-02-13 17:15:20 +00006088#include "selftests/huge_gem_object.c"
Matthew Auld40498662017-10-06 23:18:29 +01006089#include "selftests/huge_pages.c"
Chris Wilson8335fd62017-02-13 17:15:28 +00006090#include "selftests/i915_gem_object.c"
Chris Wilson17059452017-02-13 17:15:32 +00006091#include "selftests/i915_gem_coherency.c"
Chris Wilson935a2f72017-02-13 17:15:13 +00006092#endif