blob: 0f28b5ddfd156059c51acf38aa1f1021377c21b0 [file] [log] [blame]
Eric Anholt673a3942008-07-30 12:06:12 -07001/*
Daniel Vetterbe6a0372015-03-18 10:46:04 +01002 * Copyright © 2008-2015 Intel Corporation
Eric Anholt673a3942008-07-30 12:06:12 -07003 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 *
23 * Authors:
24 * Eric Anholt <eric@anholt.net>
25 *
26 */
27
David Howells760285e2012-10-02 18:01:07 +010028#include <drm/drmP.h>
David Herrmann0de23972013-07-24 21:07:52 +020029#include <drm/drm_vma_manager.h>
David Howells760285e2012-10-02 18:01:07 +010030#include <drm/i915_drm.h>
Eric Anholt673a3942008-07-30 12:06:12 -070031#include "i915_drv.h"
Chris Wilson57822dc2017-02-22 11:40:48 +000032#include "i915_gem_clflush.h"
Yu Zhangeb822892015-02-10 19:05:49 +080033#include "i915_vgpu.h"
Chris Wilson1c5d22f2009-08-25 11:15:50 +010034#include "i915_trace.h"
Jesse Barnes652c3932009-08-17 13:31:43 -070035#include "intel_drv.h"
Chris Wilson5d723d72016-08-04 16:32:35 +010036#include "intel_frontbuffer.h"
Peter Antoine0ccdacf2016-04-13 15:03:25 +010037#include "intel_mocs.h"
Oscar Mateo59b449d2018-04-10 09:12:47 -070038#include "intel_workarounds.h"
Matthew Auld465c4032017-10-06 23:18:14 +010039#include "i915_gemfs.h"
Chris Wilson6b5e90f2016-11-14 20:41:05 +000040#include <linux/dma-fence-array.h>
Chris Wilsonfe3288b2017-02-12 17:20:01 +000041#include <linux/kthread.h>
Chris Wilsonc13d87e2016-07-20 09:21:15 +010042#include <linux/reservation.h>
Hugh Dickins5949eac2011-06-27 16:18:18 -070043#include <linux/shmem_fs.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090044#include <linux/slab.h>
Chris Wilson20e49332016-11-22 14:41:21 +000045#include <linux/stop_machine.h>
Eric Anholt673a3942008-07-30 12:06:12 -070046#include <linux/swap.h>
Jesse Barnes79e53942008-11-07 14:24:08 -080047#include <linux/pci.h>
Daniel Vetter1286ff72012-05-10 15:25:09 +020048#include <linux/dma-buf.h>
Eric Anholt673a3942008-07-30 12:06:12 -070049
Chris Wilsonfbbd37b2016-10-28 13:58:42 +010050static void i915_gem_flush_free_objects(struct drm_i915_private *i915);
Chris Wilson61050802012-04-17 15:31:31 +010051
Chris Wilson2c225692013-08-09 12:26:45 +010052static bool cpu_write_needs_clflush(struct drm_i915_gem_object *obj)
53{
Chris Wilsone27ab732017-06-15 13:38:49 +010054 if (obj->cache_dirty)
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +053055 return false;
56
Chris Wilsonb8f55be2017-08-11 12:11:16 +010057 if (!(obj->cache_coherent & I915_BO_CACHE_COHERENT_FOR_WRITE))
Chris Wilson2c225692013-08-09 12:26:45 +010058 return true;
59
Chris Wilsonbd3d2252017-10-13 21:26:14 +010060 return obj->pin_global; /* currently in use by HW, keep flushed */
Chris Wilson2c225692013-08-09 12:26:45 +010061}
62
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +053063static int
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +010064insert_mappable_node(struct i915_ggtt *ggtt,
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +053065 struct drm_mm_node *node, u32 size)
66{
67 memset(node, 0, sizeof(*node));
Chris Wilson82ad6442018-06-05 16:37:58 +010068 return drm_mm_insert_node_in_range(&ggtt->vm.mm, node,
Chris Wilson4e64e552017-02-02 21:04:38 +000069 size, 0, I915_COLOR_UNEVICTABLE,
70 0, ggtt->mappable_end,
71 DRM_MM_INSERT_LOW);
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +053072}
73
74static void
75remove_mappable_node(struct drm_mm_node *node)
76{
77 drm_mm_remove_node(node);
78}
79
Chris Wilson73aa8082010-09-30 11:46:12 +010080/* some bookkeeping */
81static void i915_gem_info_add_obj(struct drm_i915_private *dev_priv,
Chris Wilson3ef7f222016-10-18 13:02:48 +010082 u64 size)
Chris Wilson73aa8082010-09-30 11:46:12 +010083{
Daniel Vetterc20e8352013-07-24 22:40:23 +020084 spin_lock(&dev_priv->mm.object_stat_lock);
Chris Wilson73aa8082010-09-30 11:46:12 +010085 dev_priv->mm.object_count++;
86 dev_priv->mm.object_memory += size;
Daniel Vetterc20e8352013-07-24 22:40:23 +020087 spin_unlock(&dev_priv->mm.object_stat_lock);
Chris Wilson73aa8082010-09-30 11:46:12 +010088}
89
90static void i915_gem_info_remove_obj(struct drm_i915_private *dev_priv,
Chris Wilson3ef7f222016-10-18 13:02:48 +010091 u64 size)
Chris Wilson73aa8082010-09-30 11:46:12 +010092{
Daniel Vetterc20e8352013-07-24 22:40:23 +020093 spin_lock(&dev_priv->mm.object_stat_lock);
Chris Wilson73aa8082010-09-30 11:46:12 +010094 dev_priv->mm.object_count--;
95 dev_priv->mm.object_memory -= size;
Daniel Vetterc20e8352013-07-24 22:40:23 +020096 spin_unlock(&dev_priv->mm.object_stat_lock);
Chris Wilson73aa8082010-09-30 11:46:12 +010097}
98
Chris Wilson21dd3732011-01-26 15:55:56 +000099static int
Daniel Vetter33196de2012-11-14 17:14:05 +0100100i915_gem_wait_for_error(struct i915_gpu_error *error)
Chris Wilson30dbf0c2010-09-25 10:19:17 +0100101{
Chris Wilson30dbf0c2010-09-25 10:19:17 +0100102 int ret;
103
Chris Wilson4c7d62c2016-10-28 13:58:32 +0100104 might_sleep();
105
Daniel Vetter0a6759c2012-07-04 22:18:41 +0200106 /*
107 * Only wait 10 seconds for the gpu reset to complete to avoid hanging
108 * userspace. If it takes that long something really bad is going on and
109 * we should simply try to bail out and fail as gracefully as possible.
110 */
Daniel Vetter1f83fee2012-11-15 17:17:22 +0100111 ret = wait_event_interruptible_timeout(error->reset_queue,
Chris Wilson8c185ec2017-03-16 17:13:02 +0000112 !i915_reset_backoff(error),
Chris Wilsonb52992c2016-10-28 13:58:24 +0100113 I915_RESET_TIMEOUT);
Daniel Vetter0a6759c2012-07-04 22:18:41 +0200114 if (ret == 0) {
115 DRM_ERROR("Timed out waiting for the gpu reset to complete\n");
116 return -EIO;
117 } else if (ret < 0) {
Chris Wilson30dbf0c2010-09-25 10:19:17 +0100118 return ret;
Chris Wilsond98c52c2016-04-13 17:35:05 +0100119 } else {
120 return 0;
Daniel Vetter0a6759c2012-07-04 22:18:41 +0200121 }
Chris Wilson30dbf0c2010-09-25 10:19:17 +0100122}
123
Chris Wilson54cf91d2010-11-25 18:00:26 +0000124int i915_mutex_lock_interruptible(struct drm_device *dev)
Chris Wilson76c1dec2010-09-25 11:22:51 +0100125{
Chris Wilsonfac5e232016-07-04 11:34:36 +0100126 struct drm_i915_private *dev_priv = to_i915(dev);
Chris Wilson76c1dec2010-09-25 11:22:51 +0100127 int ret;
128
Daniel Vetter33196de2012-11-14 17:14:05 +0100129 ret = i915_gem_wait_for_error(&dev_priv->gpu_error);
Chris Wilson76c1dec2010-09-25 11:22:51 +0100130 if (ret)
131 return ret;
132
133 ret = mutex_lock_interruptible(&dev->struct_mutex);
134 if (ret)
135 return ret;
136
Chris Wilson76c1dec2010-09-25 11:22:51 +0100137 return 0;
138}
Chris Wilson30dbf0c2010-09-25 10:19:17 +0100139
Chris Wilsone4d20062018-04-06 16:51:44 +0100140static u32 __i915_gem_park(struct drm_i915_private *i915)
141{
Chris Wilson4dfacb02018-05-31 09:22:43 +0100142 GEM_TRACE("\n");
143
Chris Wilsone4d20062018-04-06 16:51:44 +0100144 lockdep_assert_held(&i915->drm.struct_mutex);
145 GEM_BUG_ON(i915->gt.active_requests);
Chris Wilson643b4502018-04-30 14:15:03 +0100146 GEM_BUG_ON(!list_empty(&i915->gt.active_rings));
Chris Wilsone4d20062018-04-06 16:51:44 +0100147
148 if (!i915->gt.awake)
149 return I915_EPOCH_INVALID;
150
151 GEM_BUG_ON(i915->gt.epoch == I915_EPOCH_INVALID);
152
153 /*
154 * Be paranoid and flush a concurrent interrupt to make sure
155 * we don't reactivate any irq tasklets after parking.
156 *
157 * FIXME: Note that even though we have waited for execlists to be idle,
158 * there may still be an in-flight interrupt even though the CSB
159 * is now empty. synchronize_irq() makes sure that a residual interrupt
160 * is completed before we continue, but it doesn't prevent the HW from
161 * raising a spurious interrupt later. To complete the shield we should
162 * coordinate disabling the CS irq with flushing the interrupts.
163 */
164 synchronize_irq(i915->drm.irq);
165
166 intel_engines_park(i915);
Chris Wilsona89d1f92018-05-02 17:38:39 +0100167 i915_timelines_park(i915);
Chris Wilsone4d20062018-04-06 16:51:44 +0100168
169 i915_pmu_gt_parked(i915);
Chris Wilson3365e222018-05-03 20:51:14 +0100170 i915_vma_parked(i915);
Chris Wilsone4d20062018-04-06 16:51:44 +0100171
172 i915->gt.awake = false;
173
174 if (INTEL_GEN(i915) >= 6)
175 gen6_rps_idle(i915);
176
177 intel_display_power_put(i915, POWER_DOMAIN_GT_IRQ);
178
179 intel_runtime_pm_put(i915);
180
181 return i915->gt.epoch;
182}
183
184void i915_gem_park(struct drm_i915_private *i915)
185{
Chris Wilson4dfacb02018-05-31 09:22:43 +0100186 GEM_TRACE("\n");
187
Chris Wilsone4d20062018-04-06 16:51:44 +0100188 lockdep_assert_held(&i915->drm.struct_mutex);
189 GEM_BUG_ON(i915->gt.active_requests);
190
191 if (!i915->gt.awake)
192 return;
193
194 /* Defer the actual call to __i915_gem_park() to prevent ping-pongs */
195 mod_delayed_work(i915->wq, &i915->gt.idle_work, msecs_to_jiffies(100));
196}
197
198void i915_gem_unpark(struct drm_i915_private *i915)
199{
Chris Wilson4dfacb02018-05-31 09:22:43 +0100200 GEM_TRACE("\n");
201
Chris Wilsone4d20062018-04-06 16:51:44 +0100202 lockdep_assert_held(&i915->drm.struct_mutex);
203 GEM_BUG_ON(!i915->gt.active_requests);
204
205 if (i915->gt.awake)
206 return;
207
208 intel_runtime_pm_get_noresume(i915);
209
210 /*
211 * It seems that the DMC likes to transition between the DC states a lot
212 * when there are no connected displays (no active power domains) during
213 * command submission.
214 *
215 * This activity has negative impact on the performance of the chip with
216 * huge latencies observed in the interrupt handler and elsewhere.
217 *
218 * Work around it by grabbing a GT IRQ power domain whilst there is any
219 * GT activity, preventing any DC state transitions.
220 */
221 intel_display_power_get(i915, POWER_DOMAIN_GT_IRQ);
222
223 i915->gt.awake = true;
224 if (unlikely(++i915->gt.epoch == 0)) /* keep 0 as invalid */
225 i915->gt.epoch = 1;
226
227 intel_enable_gt_powersave(i915);
228 i915_update_gfx_val(i915);
229 if (INTEL_GEN(i915) >= 6)
230 gen6_rps_busy(i915);
231 i915_pmu_gt_unparked(i915);
232
233 intel_engines_unpark(i915);
234
235 i915_queue_hangcheck(i915);
236
237 queue_delayed_work(i915->wq,
238 &i915->gt.retire_work,
239 round_jiffies_up_relative(HZ));
240}
241
Eric Anholt673a3942008-07-30 12:06:12 -0700242int
Eric Anholt5a125c32008-10-22 21:40:13 -0700243i915_gem_get_aperture_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +0000244 struct drm_file *file)
Eric Anholt5a125c32008-10-22 21:40:13 -0700245{
Joonas Lahtinen72e96d62016-03-30 16:57:10 +0300246 struct drm_i915_private *dev_priv = to_i915(dev);
Joonas Lahtinen62106b42016-03-18 10:42:57 +0200247 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Joonas Lahtinen72e96d62016-03-30 16:57:10 +0300248 struct drm_i915_gem_get_aperture *args = data;
Tvrtko Ursulinca1543b2015-07-01 11:51:10 +0100249 struct i915_vma *vma;
Weinan Liff8f7972017-05-31 10:35:52 +0800250 u64 pinned;
Eric Anholt5a125c32008-10-22 21:40:13 -0700251
Chris Wilson82ad6442018-06-05 16:37:58 +0100252 pinned = ggtt->vm.reserved;
Chris Wilson73aa8082010-09-30 11:46:12 +0100253 mutex_lock(&dev->struct_mutex);
Chris Wilson82ad6442018-06-05 16:37:58 +0100254 list_for_each_entry(vma, &ggtt->vm.active_list, vm_link)
Chris Wilson20dfbde2016-08-04 16:32:30 +0100255 if (i915_vma_is_pinned(vma))
Tvrtko Ursulinca1543b2015-07-01 11:51:10 +0100256 pinned += vma->node.size;
Chris Wilson82ad6442018-06-05 16:37:58 +0100257 list_for_each_entry(vma, &ggtt->vm.inactive_list, vm_link)
Chris Wilson20dfbde2016-08-04 16:32:30 +0100258 if (i915_vma_is_pinned(vma))
Tvrtko Ursulinca1543b2015-07-01 11:51:10 +0100259 pinned += vma->node.size;
Chris Wilson73aa8082010-09-30 11:46:12 +0100260 mutex_unlock(&dev->struct_mutex);
Eric Anholt5a125c32008-10-22 21:40:13 -0700261
Chris Wilson82ad6442018-06-05 16:37:58 +0100262 args->aper_size = ggtt->vm.total;
Akshay Joshi0206e352011-08-16 15:34:10 -0400263 args->aper_available_size = args->aper_size - pinned;
Chris Wilson6299f992010-11-24 12:23:44 +0000264
Eric Anholt5a125c32008-10-22 21:40:13 -0700265 return 0;
266}
267
Matthew Auldb91b09e2017-10-06 23:18:17 +0100268static int i915_gem_object_get_pages_phys(struct drm_i915_gem_object *obj)
Chris Wilson00731152014-05-21 12:42:56 +0100269{
Al Viro93c76a32015-12-04 23:45:44 -0500270 struct address_space *mapping = obj->base.filp->f_mapping;
Chris Wilsondbb43512016-12-07 13:34:11 +0000271 drm_dma_handle_t *phys;
Chris Wilson6a2c4232014-11-04 04:51:40 -0800272 struct sg_table *st;
273 struct scatterlist *sg;
Chris Wilsondbb43512016-12-07 13:34:11 +0000274 char *vaddr;
Chris Wilson6a2c4232014-11-04 04:51:40 -0800275 int i;
Matthew Auldb91b09e2017-10-06 23:18:17 +0100276 int err;
Chris Wilson00731152014-05-21 12:42:56 +0100277
Chris Wilson6a2c4232014-11-04 04:51:40 -0800278 if (WARN_ON(i915_gem_object_needs_bit17_swizzle(obj)))
Matthew Auldb91b09e2017-10-06 23:18:17 +0100279 return -EINVAL;
Chris Wilson00731152014-05-21 12:42:56 +0100280
Chris Wilsondbb43512016-12-07 13:34:11 +0000281 /* Always aligning to the object size, allows a single allocation
282 * to handle all possible callers, and given typical object sizes,
283 * the alignment of the buddy allocation will naturally match.
284 */
285 phys = drm_pci_alloc(obj->base.dev,
Ville Syrjälä750fae22017-09-07 17:32:03 +0300286 roundup_pow_of_two(obj->base.size),
Chris Wilsondbb43512016-12-07 13:34:11 +0000287 roundup_pow_of_two(obj->base.size));
288 if (!phys)
Matthew Auldb91b09e2017-10-06 23:18:17 +0100289 return -ENOMEM;
Chris Wilsondbb43512016-12-07 13:34:11 +0000290
291 vaddr = phys->vaddr;
Chris Wilson6a2c4232014-11-04 04:51:40 -0800292 for (i = 0; i < obj->base.size / PAGE_SIZE; i++) {
293 struct page *page;
294 char *src;
295
296 page = shmem_read_mapping_page(mapping, i);
Chris Wilsondbb43512016-12-07 13:34:11 +0000297 if (IS_ERR(page)) {
Matthew Auldb91b09e2017-10-06 23:18:17 +0100298 err = PTR_ERR(page);
Chris Wilsondbb43512016-12-07 13:34:11 +0000299 goto err_phys;
300 }
Chris Wilson6a2c4232014-11-04 04:51:40 -0800301
302 src = kmap_atomic(page);
303 memcpy(vaddr, src, PAGE_SIZE);
304 drm_clflush_virt_range(vaddr, PAGE_SIZE);
305 kunmap_atomic(src);
306
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300307 put_page(page);
Chris Wilson6a2c4232014-11-04 04:51:40 -0800308 vaddr += PAGE_SIZE;
309 }
310
Chris Wilsonc0336662016-05-06 15:40:21 +0100311 i915_gem_chipset_flush(to_i915(obj->base.dev));
Chris Wilson6a2c4232014-11-04 04:51:40 -0800312
313 st = kmalloc(sizeof(*st), GFP_KERNEL);
Chris Wilsondbb43512016-12-07 13:34:11 +0000314 if (!st) {
Matthew Auldb91b09e2017-10-06 23:18:17 +0100315 err = -ENOMEM;
Chris Wilsondbb43512016-12-07 13:34:11 +0000316 goto err_phys;
317 }
Chris Wilson6a2c4232014-11-04 04:51:40 -0800318
319 if (sg_alloc_table(st, 1, GFP_KERNEL)) {
320 kfree(st);
Matthew Auldb91b09e2017-10-06 23:18:17 +0100321 err = -ENOMEM;
Chris Wilsondbb43512016-12-07 13:34:11 +0000322 goto err_phys;
Chris Wilson6a2c4232014-11-04 04:51:40 -0800323 }
324
325 sg = st->sgl;
326 sg->offset = 0;
327 sg->length = obj->base.size;
328
Chris Wilsondbb43512016-12-07 13:34:11 +0000329 sg_dma_address(sg) = phys->busaddr;
Chris Wilson6a2c4232014-11-04 04:51:40 -0800330 sg_dma_len(sg) = obj->base.size;
331
Chris Wilsondbb43512016-12-07 13:34:11 +0000332 obj->phys_handle = phys;
Matthew Auldb91b09e2017-10-06 23:18:17 +0100333
Matthew Aulda5c081662017-10-06 23:18:18 +0100334 __i915_gem_object_set_pages(obj, st, sg->length);
Matthew Auldb91b09e2017-10-06 23:18:17 +0100335
336 return 0;
Chris Wilsondbb43512016-12-07 13:34:11 +0000337
338err_phys:
339 drm_pci_free(obj->base.dev, phys);
Matthew Auldb91b09e2017-10-06 23:18:17 +0100340
341 return err;
Chris Wilson6a2c4232014-11-04 04:51:40 -0800342}
343
Chris Wilsone27ab732017-06-15 13:38:49 +0100344static void __start_cpu_write(struct drm_i915_gem_object *obj)
345{
Christian Königc0a51fd2018-02-16 13:43:38 +0100346 obj->read_domains = I915_GEM_DOMAIN_CPU;
347 obj->write_domain = I915_GEM_DOMAIN_CPU;
Chris Wilsone27ab732017-06-15 13:38:49 +0100348 if (cpu_write_needs_clflush(obj))
349 obj->cache_dirty = true;
350}
351
Chris Wilson6a2c4232014-11-04 04:51:40 -0800352static void
Chris Wilson2b3c8312016-11-11 14:58:09 +0000353__i915_gem_object_release_shmem(struct drm_i915_gem_object *obj,
Chris Wilsone5facdf2016-12-23 14:57:57 +0000354 struct sg_table *pages,
355 bool needs_clflush)
Chris Wilson6a2c4232014-11-04 04:51:40 -0800356{
Chris Wilsona4f5ea62016-10-28 13:58:35 +0100357 GEM_BUG_ON(obj->mm.madv == __I915_MADV_PURGED);
Chris Wilson6a2c4232014-11-04 04:51:40 -0800358
Chris Wilsona4f5ea62016-10-28 13:58:35 +0100359 if (obj->mm.madv == I915_MADV_DONTNEED)
360 obj->mm.dirty = false;
Chris Wilson6a2c4232014-11-04 04:51:40 -0800361
Chris Wilsone5facdf2016-12-23 14:57:57 +0000362 if (needs_clflush &&
Christian Königc0a51fd2018-02-16 13:43:38 +0100363 (obj->read_domains & I915_GEM_DOMAIN_CPU) == 0 &&
Chris Wilsonb8f55be2017-08-11 12:11:16 +0100364 !(obj->cache_coherent & I915_BO_CACHE_COHERENT_FOR_READ))
Chris Wilson2b3c8312016-11-11 14:58:09 +0000365 drm_clflush_sg(pages);
Chris Wilson03ac84f2016-10-28 13:58:36 +0100366
Chris Wilsone27ab732017-06-15 13:38:49 +0100367 __start_cpu_write(obj);
Chris Wilson03ac84f2016-10-28 13:58:36 +0100368}
369
370static void
371i915_gem_object_put_pages_phys(struct drm_i915_gem_object *obj,
372 struct sg_table *pages)
373{
Chris Wilsone5facdf2016-12-23 14:57:57 +0000374 __i915_gem_object_release_shmem(obj, pages, false);
Chris Wilson03ac84f2016-10-28 13:58:36 +0100375
Chris Wilsona4f5ea62016-10-28 13:58:35 +0100376 if (obj->mm.dirty) {
Al Viro93c76a32015-12-04 23:45:44 -0500377 struct address_space *mapping = obj->base.filp->f_mapping;
Chris Wilson6a2c4232014-11-04 04:51:40 -0800378 char *vaddr = obj->phys_handle->vaddr;
Chris Wilson00731152014-05-21 12:42:56 +0100379 int i;
380
381 for (i = 0; i < obj->base.size / PAGE_SIZE; i++) {
Chris Wilson6a2c4232014-11-04 04:51:40 -0800382 struct page *page;
383 char *dst;
Chris Wilson00731152014-05-21 12:42:56 +0100384
Chris Wilson6a2c4232014-11-04 04:51:40 -0800385 page = shmem_read_mapping_page(mapping, i);
386 if (IS_ERR(page))
387 continue;
388
389 dst = kmap_atomic(page);
390 drm_clflush_virt_range(vaddr, PAGE_SIZE);
391 memcpy(dst, vaddr, PAGE_SIZE);
392 kunmap_atomic(dst);
393
394 set_page_dirty(page);
Chris Wilsona4f5ea62016-10-28 13:58:35 +0100395 if (obj->mm.madv == I915_MADV_WILLNEED)
Chris Wilson00731152014-05-21 12:42:56 +0100396 mark_page_accessed(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300397 put_page(page);
Chris Wilson00731152014-05-21 12:42:56 +0100398 vaddr += PAGE_SIZE;
399 }
Chris Wilsona4f5ea62016-10-28 13:58:35 +0100400 obj->mm.dirty = false;
Chris Wilson00731152014-05-21 12:42:56 +0100401 }
402
Chris Wilson03ac84f2016-10-28 13:58:36 +0100403 sg_free_table(pages);
404 kfree(pages);
Chris Wilsondbb43512016-12-07 13:34:11 +0000405
406 drm_pci_free(obj->base.dev, obj->phys_handle);
Chris Wilson6a2c4232014-11-04 04:51:40 -0800407}
408
409static void
410i915_gem_object_release_phys(struct drm_i915_gem_object *obj)
411{
Chris Wilsona4f5ea62016-10-28 13:58:35 +0100412 i915_gem_object_unpin_pages(obj);
Chris Wilson6a2c4232014-11-04 04:51:40 -0800413}
414
415static const struct drm_i915_gem_object_ops i915_gem_phys_ops = {
416 .get_pages = i915_gem_object_get_pages_phys,
417 .put_pages = i915_gem_object_put_pages_phys,
418 .release = i915_gem_object_release_phys,
419};
420
Chris Wilson581ab1f2017-02-15 16:39:00 +0000421static const struct drm_i915_gem_object_ops i915_gem_object_ops;
422
Chris Wilson35a96112016-08-14 18:44:40 +0100423int i915_gem_object_unbind(struct drm_i915_gem_object *obj)
Chris Wilsonaa653a62016-08-04 07:52:27 +0100424{
425 struct i915_vma *vma;
426 LIST_HEAD(still_in_list);
Chris Wilson02bef8f2016-08-14 18:44:41 +0100427 int ret;
Chris Wilsonaa653a62016-08-04 07:52:27 +0100428
Chris Wilson02bef8f2016-08-14 18:44:41 +0100429 lockdep_assert_held(&obj->base.dev->struct_mutex);
430
431 /* Closed vma are removed from the obj->vma_list - but they may
432 * still have an active binding on the object. To remove those we
433 * must wait for all rendering to complete to the object (as unbinding
434 * must anyway), and retire the requests.
Chris Wilsonaa653a62016-08-04 07:52:27 +0100435 */
Chris Wilson5888fc92017-12-04 13:25:13 +0000436 ret = i915_gem_object_set_to_cpu_domain(obj, false);
Chris Wilson02bef8f2016-08-14 18:44:41 +0100437 if (ret)
438 return ret;
439
Chris Wilsonaa653a62016-08-04 07:52:27 +0100440 while ((vma = list_first_entry_or_null(&obj->vma_list,
441 struct i915_vma,
442 obj_link))) {
443 list_move_tail(&vma->obj_link, &still_in_list);
444 ret = i915_vma_unbind(vma);
445 if (ret)
446 break;
447 }
448 list_splice(&still_in_list, &obj->vma_list);
449
450 return ret;
451}
452
Chris Wilsone95433c2016-10-28 13:58:27 +0100453static long
454i915_gem_object_wait_fence(struct dma_fence *fence,
455 unsigned int flags,
456 long timeout,
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +0100457 struct intel_rps_client *rps_client)
Chris Wilsone95433c2016-10-28 13:58:27 +0100458{
Chris Wilsone61e0f52018-02-21 09:56:36 +0000459 struct i915_request *rq;
Chris Wilsone95433c2016-10-28 13:58:27 +0100460
461 BUILD_BUG_ON(I915_WAIT_INTERRUPTIBLE != 0x1);
462
463 if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags))
464 return timeout;
465
466 if (!dma_fence_is_i915(fence))
467 return dma_fence_wait_timeout(fence,
468 flags & I915_WAIT_INTERRUPTIBLE,
469 timeout);
470
471 rq = to_request(fence);
Chris Wilsone61e0f52018-02-21 09:56:36 +0000472 if (i915_request_completed(rq))
Chris Wilsone95433c2016-10-28 13:58:27 +0100473 goto out;
474
Chris Wilsone9af4ea2018-01-18 13:16:09 +0000475 /*
476 * This client is about to stall waiting for the GPU. In many cases
Chris Wilsone95433c2016-10-28 13:58:27 +0100477 * this is undesirable and limits the throughput of the system, as
478 * many clients cannot continue processing user input/output whilst
479 * blocked. RPS autotuning may take tens of milliseconds to respond
480 * to the GPU load and thus incurs additional latency for the client.
481 * We can circumvent that by promoting the GPU frequency to maximum
482 * before we wait. This makes the GPU throttle up much more quickly
483 * (good for benchmarks and user experience, e.g. window animations),
484 * but at a cost of spending more power processing the workload
485 * (bad for battery). Not all clients even want their results
486 * immediately and for them we should just let the GPU select its own
487 * frequency to maximise efficiency. To prevent a single client from
488 * forcing the clocks too high for the whole system, we only allow
489 * each client to waitboost once in a busy period.
490 */
Chris Wilsone61e0f52018-02-21 09:56:36 +0000491 if (rps_client && !i915_request_started(rq)) {
Chris Wilsone95433c2016-10-28 13:58:27 +0100492 if (INTEL_GEN(rq->i915) >= 6)
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +0100493 gen6_rps_boost(rq, rps_client);
Chris Wilsone95433c2016-10-28 13:58:27 +0100494 }
495
Chris Wilsone61e0f52018-02-21 09:56:36 +0000496 timeout = i915_request_wait(rq, flags, timeout);
Chris Wilsone95433c2016-10-28 13:58:27 +0100497
498out:
Chris Wilsone61e0f52018-02-21 09:56:36 +0000499 if (flags & I915_WAIT_LOCKED && i915_request_completed(rq))
500 i915_request_retire_upto(rq);
Chris Wilsone95433c2016-10-28 13:58:27 +0100501
Chris Wilsone95433c2016-10-28 13:58:27 +0100502 return timeout;
503}
504
505static long
506i915_gem_object_wait_reservation(struct reservation_object *resv,
507 unsigned int flags,
508 long timeout,
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +0100509 struct intel_rps_client *rps_client)
Chris Wilsone95433c2016-10-28 13:58:27 +0100510{
Chris Wilsone54ca972017-02-17 15:13:04 +0000511 unsigned int seq = __read_seqcount_begin(&resv->seq);
Chris Wilsone95433c2016-10-28 13:58:27 +0100512 struct dma_fence *excl;
Chris Wilsone54ca972017-02-17 15:13:04 +0000513 bool prune_fences = false;
Chris Wilsone95433c2016-10-28 13:58:27 +0100514
515 if (flags & I915_WAIT_ALL) {
516 struct dma_fence **shared;
517 unsigned int count, i;
518 int ret;
519
520 ret = reservation_object_get_fences_rcu(resv,
521 &excl, &count, &shared);
522 if (ret)
523 return ret;
524
525 for (i = 0; i < count; i++) {
526 timeout = i915_gem_object_wait_fence(shared[i],
527 flags, timeout,
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +0100528 rps_client);
Chris Wilsond892e932017-02-12 21:53:43 +0000529 if (timeout < 0)
Chris Wilsone95433c2016-10-28 13:58:27 +0100530 break;
531
532 dma_fence_put(shared[i]);
533 }
534
535 for (; i < count; i++)
536 dma_fence_put(shared[i]);
537 kfree(shared);
Chris Wilsone54ca972017-02-17 15:13:04 +0000538
Chris Wilsonfa730552018-03-07 17:13:03 +0000539 /*
540 * If both shared fences and an exclusive fence exist,
541 * then by construction the shared fences must be later
542 * than the exclusive fence. If we successfully wait for
543 * all the shared fences, we know that the exclusive fence
544 * must all be signaled. If all the shared fences are
545 * signaled, we can prune the array and recover the
546 * floating references on the fences/requests.
547 */
Chris Wilsone54ca972017-02-17 15:13:04 +0000548 prune_fences = count && timeout >= 0;
Chris Wilsone95433c2016-10-28 13:58:27 +0100549 } else {
550 excl = reservation_object_get_excl_rcu(resv);
551 }
552
Chris Wilsonfa730552018-03-07 17:13:03 +0000553 if (excl && timeout >= 0)
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +0100554 timeout = i915_gem_object_wait_fence(excl, flags, timeout,
555 rps_client);
Chris Wilsone95433c2016-10-28 13:58:27 +0100556
557 dma_fence_put(excl);
558
Chris Wilsonfa730552018-03-07 17:13:03 +0000559 /*
560 * Opportunistically prune the fences iff we know they have *all* been
Chris Wilson03d1cac2017-03-08 13:26:28 +0000561 * signaled and that the reservation object has not been changed (i.e.
562 * no new fences have been added).
563 */
Chris Wilsone54ca972017-02-17 15:13:04 +0000564 if (prune_fences && !__read_seqcount_retry(&resv->seq, seq)) {
Chris Wilson03d1cac2017-03-08 13:26:28 +0000565 if (reservation_object_trylock(resv)) {
566 if (!__read_seqcount_retry(&resv->seq, seq))
567 reservation_object_add_excl_fence(resv, NULL);
568 reservation_object_unlock(resv);
569 }
Chris Wilsone54ca972017-02-17 15:13:04 +0000570 }
571
Chris Wilsone95433c2016-10-28 13:58:27 +0100572 return timeout;
573}
574
Chris Wilsonb7268c52018-04-18 19:40:52 +0100575static void __fence_set_priority(struct dma_fence *fence,
576 const struct i915_sched_attr *attr)
Chris Wilson6b5e90f2016-11-14 20:41:05 +0000577{
Chris Wilsone61e0f52018-02-21 09:56:36 +0000578 struct i915_request *rq;
Chris Wilson6b5e90f2016-11-14 20:41:05 +0000579 struct intel_engine_cs *engine;
580
Chris Wilsonc218ee02018-01-06 10:56:18 +0000581 if (dma_fence_is_signaled(fence) || !dma_fence_is_i915(fence))
Chris Wilson6b5e90f2016-11-14 20:41:05 +0000582 return;
583
584 rq = to_request(fence);
585 engine = rq->engine;
Chris Wilson6b5e90f2016-11-14 20:41:05 +0000586
Chris Wilson4f6d8fc2018-05-07 14:57:25 +0100587 local_bh_disable();
588 rcu_read_lock(); /* RCU serialisation for set-wedged protection */
Chris Wilson47650db2018-03-07 13:42:25 +0000589 if (engine->schedule)
Chris Wilsonb7268c52018-04-18 19:40:52 +0100590 engine->schedule(rq, attr);
Chris Wilson47650db2018-03-07 13:42:25 +0000591 rcu_read_unlock();
Chris Wilson4f6d8fc2018-05-07 14:57:25 +0100592 local_bh_enable(); /* kick the tasklets if queues were reprioritised */
Chris Wilson6b5e90f2016-11-14 20:41:05 +0000593}
594
Chris Wilsonb7268c52018-04-18 19:40:52 +0100595static void fence_set_priority(struct dma_fence *fence,
596 const struct i915_sched_attr *attr)
Chris Wilson6b5e90f2016-11-14 20:41:05 +0000597{
598 /* Recurse once into a fence-array */
599 if (dma_fence_is_array(fence)) {
600 struct dma_fence_array *array = to_dma_fence_array(fence);
601 int i;
602
603 for (i = 0; i < array->num_fences; i++)
Chris Wilsonb7268c52018-04-18 19:40:52 +0100604 __fence_set_priority(array->fences[i], attr);
Chris Wilson6b5e90f2016-11-14 20:41:05 +0000605 } else {
Chris Wilsonb7268c52018-04-18 19:40:52 +0100606 __fence_set_priority(fence, attr);
Chris Wilson6b5e90f2016-11-14 20:41:05 +0000607 }
608}
609
610int
611i915_gem_object_wait_priority(struct drm_i915_gem_object *obj,
612 unsigned int flags,
Chris Wilsonb7268c52018-04-18 19:40:52 +0100613 const struct i915_sched_attr *attr)
Chris Wilson6b5e90f2016-11-14 20:41:05 +0000614{
615 struct dma_fence *excl;
616
617 if (flags & I915_WAIT_ALL) {
618 struct dma_fence **shared;
619 unsigned int count, i;
620 int ret;
621
622 ret = reservation_object_get_fences_rcu(obj->resv,
623 &excl, &count, &shared);
624 if (ret)
625 return ret;
626
627 for (i = 0; i < count; i++) {
Chris Wilsonb7268c52018-04-18 19:40:52 +0100628 fence_set_priority(shared[i], attr);
Chris Wilson6b5e90f2016-11-14 20:41:05 +0000629 dma_fence_put(shared[i]);
630 }
631
632 kfree(shared);
633 } else {
634 excl = reservation_object_get_excl_rcu(obj->resv);
635 }
636
637 if (excl) {
Chris Wilsonb7268c52018-04-18 19:40:52 +0100638 fence_set_priority(excl, attr);
Chris Wilson6b5e90f2016-11-14 20:41:05 +0000639 dma_fence_put(excl);
640 }
641 return 0;
642}
643
Chris Wilson00e60f22016-08-04 16:32:40 +0100644/**
Chris Wilsone95433c2016-10-28 13:58:27 +0100645 * Waits for rendering to the object to be completed
Chris Wilson00e60f22016-08-04 16:32:40 +0100646 * @obj: i915 gem object
Chris Wilsone95433c2016-10-28 13:58:27 +0100647 * @flags: how to wait (under a lock, for all rendering or just for writes etc)
648 * @timeout: how long to wait
Chris Wilsona0a8b1c2017-11-09 14:06:44 +0000649 * @rps_client: client (user process) to charge for any waitboosting
Chris Wilson00e60f22016-08-04 16:32:40 +0100650 */
651int
Chris Wilsone95433c2016-10-28 13:58:27 +0100652i915_gem_object_wait(struct drm_i915_gem_object *obj,
653 unsigned int flags,
654 long timeout,
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +0100655 struct intel_rps_client *rps_client)
Chris Wilson00e60f22016-08-04 16:32:40 +0100656{
Chris Wilsone95433c2016-10-28 13:58:27 +0100657 might_sleep();
658#if IS_ENABLED(CONFIG_LOCKDEP)
659 GEM_BUG_ON(debug_locks &&
660 !!lockdep_is_held(&obj->base.dev->struct_mutex) !=
661 !!(flags & I915_WAIT_LOCKED));
662#endif
663 GEM_BUG_ON(timeout < 0);
Chris Wilson00e60f22016-08-04 16:32:40 +0100664
Chris Wilsond07f0e52016-10-28 13:58:44 +0100665 timeout = i915_gem_object_wait_reservation(obj->resv,
666 flags, timeout,
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +0100667 rps_client);
Chris Wilsone95433c2016-10-28 13:58:27 +0100668 return timeout < 0 ? timeout : 0;
Chris Wilson00e60f22016-08-04 16:32:40 +0100669}
670
671static struct intel_rps_client *to_rps_client(struct drm_file *file)
672{
673 struct drm_i915_file_private *fpriv = file->driver_priv;
674
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +0100675 return &fpriv->rps_client;
Chris Wilson00e60f22016-08-04 16:32:40 +0100676}
677
Chris Wilson00731152014-05-21 12:42:56 +0100678static int
679i915_gem_phys_pwrite(struct drm_i915_gem_object *obj,
680 struct drm_i915_gem_pwrite *args,
Chris Wilson03ac84f2016-10-28 13:58:36 +0100681 struct drm_file *file)
Chris Wilson00731152014-05-21 12:42:56 +0100682{
Chris Wilson00731152014-05-21 12:42:56 +0100683 void *vaddr = obj->phys_handle->vaddr + args->offset;
Gustavo Padovan3ed605b2016-04-26 12:32:27 -0300684 char __user *user_data = u64_to_user_ptr(args->data_ptr);
Chris Wilson6a2c4232014-11-04 04:51:40 -0800685
686 /* We manually control the domain here and pretend that it
687 * remains coherent i.e. in the GTT domain, like shmem_pwrite.
688 */
Rodrigo Vivi77a0d1c2015-06-18 11:43:24 -0700689 intel_fb_obj_invalidate(obj, ORIGIN_CPU);
Chris Wilson10466d22017-01-06 15:22:38 +0000690 if (copy_from_user(vaddr, user_data, args->size))
691 return -EFAULT;
Chris Wilson00731152014-05-21 12:42:56 +0100692
Chris Wilson6a2c4232014-11-04 04:51:40 -0800693 drm_clflush_virt_range(vaddr, args->size);
Chris Wilson10466d22017-01-06 15:22:38 +0000694 i915_gem_chipset_flush(to_i915(obj->base.dev));
Paulo Zanoni063e4e62015-02-13 17:23:45 -0200695
Chris Wilsond59b21e2017-02-22 11:40:49 +0000696 intel_fb_obj_flush(obj, ORIGIN_CPU);
Chris Wilson10466d22017-01-06 15:22:38 +0000697 return 0;
Chris Wilson00731152014-05-21 12:42:56 +0100698}
699
Tvrtko Ursulin187685c2016-12-01 14:16:36 +0000700void *i915_gem_object_alloc(struct drm_i915_private *dev_priv)
Chris Wilson42dcedd2012-11-15 11:32:30 +0000701{
Chris Wilsonefab6d82015-04-07 16:20:57 +0100702 return kmem_cache_zalloc(dev_priv->objects, GFP_KERNEL);
Chris Wilson42dcedd2012-11-15 11:32:30 +0000703}
704
705void i915_gem_object_free(struct drm_i915_gem_object *obj)
706{
Chris Wilsonfac5e232016-07-04 11:34:36 +0100707 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
Chris Wilsonefab6d82015-04-07 16:20:57 +0100708 kmem_cache_free(dev_priv->objects, obj);
Chris Wilson42dcedd2012-11-15 11:32:30 +0000709}
710
Dave Airlieff72145b2011-02-07 12:16:14 +1000711static int
712i915_gem_create(struct drm_file *file,
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +0000713 struct drm_i915_private *dev_priv,
Dave Airlieff72145b2011-02-07 12:16:14 +1000714 uint64_t size,
715 uint32_t *handle_p)
Eric Anholt673a3942008-07-30 12:06:12 -0700716{
Chris Wilson05394f32010-11-08 19:18:58 +0000717 struct drm_i915_gem_object *obj;
Pekka Paalanena1a2d1d2009-08-23 12:40:55 +0300718 int ret;
719 u32 handle;
Eric Anholt673a3942008-07-30 12:06:12 -0700720
Dave Airlieff72145b2011-02-07 12:16:14 +1000721 size = roundup(size, PAGE_SIZE);
Chris Wilson8ffc0242011-09-14 14:14:28 +0200722 if (size == 0)
723 return -EINVAL;
Eric Anholt673a3942008-07-30 12:06:12 -0700724
725 /* Allocate the new object */
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +0000726 obj = i915_gem_object_create(dev_priv, size);
Chris Wilsonfe3db792016-04-25 13:32:13 +0100727 if (IS_ERR(obj))
728 return PTR_ERR(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700729
Chris Wilson05394f32010-11-08 19:18:58 +0000730 ret = drm_gem_handle_create(file, &obj->base, &handle);
Chris Wilson202f2fe2010-10-14 13:20:40 +0100731 /* drop reference from allocate - handle holds it now */
Chris Wilsonf0cd5182016-10-28 13:58:43 +0100732 i915_gem_object_put(obj);
Daniel Vetterd861e332013-07-24 23:25:03 +0200733 if (ret)
734 return ret;
Chris Wilson202f2fe2010-10-14 13:20:40 +0100735
Dave Airlieff72145b2011-02-07 12:16:14 +1000736 *handle_p = handle;
Eric Anholt673a3942008-07-30 12:06:12 -0700737 return 0;
738}
739
Dave Airlieff72145b2011-02-07 12:16:14 +1000740int
741i915_gem_dumb_create(struct drm_file *file,
742 struct drm_device *dev,
743 struct drm_mode_create_dumb *args)
744{
745 /* have to work out size/pitch and return them */
Paulo Zanonide45eaf2013-10-18 18:48:24 -0300746 args->pitch = ALIGN(args->width * DIV_ROUND_UP(args->bpp, 8), 64);
Dave Airlieff72145b2011-02-07 12:16:14 +1000747 args->size = args->pitch * args->height;
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +0000748 return i915_gem_create(file, to_i915(dev),
Dave Airlieda6b51d2014-12-24 13:11:17 +1000749 args->size, &args->handle);
Dave Airlieff72145b2011-02-07 12:16:14 +1000750}
751
Chris Wilsone27ab732017-06-15 13:38:49 +0100752static bool gpu_write_needs_clflush(struct drm_i915_gem_object *obj)
753{
754 return !(obj->cache_level == I915_CACHE_NONE ||
755 obj->cache_level == I915_CACHE_WT);
756}
757
Dave Airlieff72145b2011-02-07 12:16:14 +1000758/**
759 * Creates a new mm object and returns a handle to it.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +0100760 * @dev: drm device pointer
761 * @data: ioctl data blob
762 * @file: drm file pointer
Dave Airlieff72145b2011-02-07 12:16:14 +1000763 */
764int
765i915_gem_create_ioctl(struct drm_device *dev, void *data,
766 struct drm_file *file)
767{
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +0000768 struct drm_i915_private *dev_priv = to_i915(dev);
Dave Airlieff72145b2011-02-07 12:16:14 +1000769 struct drm_i915_gem_create *args = data;
Daniel Vetter63ed2cb2012-04-23 16:50:50 +0200770
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +0000771 i915_gem_flush_free_objects(dev_priv);
Chris Wilsonfbbd37b2016-10-28 13:58:42 +0100772
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +0000773 return i915_gem_create(file, dev_priv,
Dave Airlieda6b51d2014-12-24 13:11:17 +1000774 args->size, &args->handle);
Dave Airlieff72145b2011-02-07 12:16:14 +1000775}
776
Chris Wilsonef749212017-04-12 12:01:10 +0100777static inline enum fb_op_origin
778fb_write_origin(struct drm_i915_gem_object *obj, unsigned int domain)
779{
780 return (domain == I915_GEM_DOMAIN_GTT ?
781 obj->frontbuffer_ggtt_origin : ORIGIN_CPU);
782}
783
Chris Wilson7125397b2017-12-06 12:49:14 +0000784void i915_gem_flush_ggtt_writes(struct drm_i915_private *dev_priv)
Chris Wilsonef749212017-04-12 12:01:10 +0100785{
Chris Wilson7125397b2017-12-06 12:49:14 +0000786 /*
787 * No actual flushing is required for the GTT write domain for reads
788 * from the GTT domain. Writes to it "immediately" go to main memory
789 * as far as we know, so there's no chipset flush. It also doesn't
790 * land in the GPU render cache.
Chris Wilsonef749212017-04-12 12:01:10 +0100791 *
792 * However, we do have to enforce the order so that all writes through
793 * the GTT land before any writes to the device, such as updates to
794 * the GATT itself.
795 *
796 * We also have to wait a bit for the writes to land from the GTT.
797 * An uncached read (i.e. mmio) seems to be ideal for the round-trip
798 * timing. This issue has only been observed when switching quickly
799 * between GTT writes and CPU reads from inside the kernel on recent hw,
800 * and it appears to only affect discrete GTT blocks (i.e. on LLC
Chris Wilson7125397b2017-12-06 12:49:14 +0000801 * system agents we cannot reproduce this behaviour, until Cannonlake
802 * that was!).
Chris Wilsonef749212017-04-12 12:01:10 +0100803 */
Chris Wilson7125397b2017-12-06 12:49:14 +0000804
Chris 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();
Chris Wilson82ad6442018-06-05 16:37:58 +01001226 ggtt->vm.insert_page(&ggtt->vm,
1227 i915_gem_object_get_dma_address(obj, offset >> PAGE_SHIFT),
1228 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();
Chris Wilson82ad6442018-06-05 16:37:58 +01001249 ggtt->vm.clear_range(&ggtt->vm, node.start, node.size);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301250 remove_mappable_node(&node);
1251 } else {
Chris Wilson058d88c2016-08-15 10:49:06 +01001252 i915_vma_unpin(vma);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301253 }
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001254out_unlock:
1255 intel_runtime_pm_put(i915);
1256 mutex_unlock(&i915->drm.struct_mutex);
Chris Wilsonf60d7f02012-09-04 21:02:56 +01001257
Eric Anholteb014592009-03-10 11:44:52 -07001258 return ret;
1259}
1260
Eric Anholt673a3942008-07-30 12:06:12 -07001261/**
1262 * Reads data from the object referenced by handle.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01001263 * @dev: drm device pointer
1264 * @data: ioctl data blob
1265 * @file: drm file pointer
Eric Anholt673a3942008-07-30 12:06:12 -07001266 *
1267 * On error, the contents of *data are undefined.
1268 */
1269int
1270i915_gem_pread_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00001271 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001272{
1273 struct drm_i915_gem_pread *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00001274 struct drm_i915_gem_object *obj;
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001275 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07001276
Chris Wilson51311d02010-11-17 09:10:42 +00001277 if (args->size == 0)
1278 return 0;
1279
1280 if (!access_ok(VERIFY_WRITE,
Gustavo Padovan3ed605b2016-04-26 12:32:27 -03001281 u64_to_user_ptr(args->data_ptr),
Chris Wilson51311d02010-11-17 09:10:42 +00001282 args->size))
1283 return -EFAULT;
1284
Chris Wilson03ac0642016-07-20 13:31:51 +01001285 obj = i915_gem_object_lookup(file, args->handle);
Chris Wilson258a5ed2016-08-05 10:14:16 +01001286 if (!obj)
1287 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07001288
Chris Wilson7dcd2492010-09-26 20:21:44 +01001289 /* Bounds check source. */
Matthew Auld966d5bf2016-12-13 20:32:22 +00001290 if (range_overflows_t(u64, args->offset, args->size, obj->base.size)) {
Chris Wilsonce9d4192010-09-26 20:50:05 +01001291 ret = -EINVAL;
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001292 goto out;
Chris Wilsonce9d4192010-09-26 20:50:05 +01001293 }
1294
Chris Wilsondb53a302011-02-03 11:57:46 +00001295 trace_i915_gem_object_pread(obj, args->offset, args->size);
1296
Chris Wilsone95433c2016-10-28 13:58:27 +01001297 ret = i915_gem_object_wait(obj,
1298 I915_WAIT_INTERRUPTIBLE,
1299 MAX_SCHEDULE_TIMEOUT,
1300 to_rps_client(file));
Chris Wilson258a5ed2016-08-05 10:14:16 +01001301 if (ret)
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001302 goto out;
Chris Wilson258a5ed2016-08-05 10:14:16 +01001303
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001304 ret = i915_gem_object_pin_pages(obj);
Chris Wilson258a5ed2016-08-05 10:14:16 +01001305 if (ret)
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001306 goto out;
Chris Wilson258a5ed2016-08-05 10:14:16 +01001307
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001308 ret = i915_gem_shmem_pread(obj, args);
Chris Wilson9c870d02016-10-24 13:42:15 +01001309 if (ret == -EFAULT || ret == -ENODEV)
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001310 ret = i915_gem_gtt_pread(obj, args);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301311
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001312 i915_gem_object_unpin_pages(obj);
1313out:
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001314 i915_gem_object_put(obj);
Eric Anholteb014592009-03-10 11:44:52 -07001315 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07001316}
1317
Keith Packard0839ccb2008-10-30 19:38:48 -07001318/* This is the fast write path which cannot handle
1319 * page faults in the source data
Linus Torvalds9b7530cc2008-10-20 14:16:43 -07001320 */
Linus Torvalds9b7530cc2008-10-20 14:16:43 -07001321
Chris Wilsonfe115622016-10-28 13:58:40 +01001322static inline bool
1323ggtt_write(struct io_mapping *mapping,
1324 loff_t base, int offset,
1325 char __user *user_data, int length)
Keith Packard0839ccb2008-10-30 19:38:48 -07001326{
Ville Syrjäläafe722b2017-09-01 20:12:52 +03001327 void __iomem *vaddr;
Keith Packard0839ccb2008-10-30 19:38:48 -07001328 unsigned long unwritten;
1329
Ben Widawsky4f0c7cf2012-04-16 14:07:47 -07001330 /* We can use the cpu mem copy function because this is X86. */
Ville Syrjäläafe722b2017-09-01 20:12:52 +03001331 vaddr = io_mapping_map_atomic_wc(mapping, base);
1332 unwritten = __copy_from_user_inatomic_nocache((void __force *)vaddr + offset,
Keith Packard0839ccb2008-10-30 19:38:48 -07001333 user_data, length);
Chris Wilsonfe115622016-10-28 13:58:40 +01001334 io_mapping_unmap_atomic(vaddr);
1335 if (unwritten) {
Ville Syrjäläafe722b2017-09-01 20:12:52 +03001336 vaddr = io_mapping_map_wc(mapping, base, PAGE_SIZE);
1337 unwritten = copy_from_user((void __force *)vaddr + offset,
1338 user_data, length);
Chris Wilsonfe115622016-10-28 13:58:40 +01001339 io_mapping_unmap(vaddr);
1340 }
Keith Packard0839ccb2008-10-30 19:38:48 -07001341
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001342 return unwritten;
1343}
1344
Eric Anholt3de09aa2009-03-09 09:42:23 -07001345/**
1346 * This is the fast pwrite path, where we copy the data directly from the
1347 * user into the GTT, uncached.
Chris Wilsonfe115622016-10-28 13:58:40 +01001348 * @obj: i915 GEM object
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01001349 * @args: pwrite arguments structure
Eric Anholt3de09aa2009-03-09 09:42:23 -07001350 */
Eric Anholt673a3942008-07-30 12:06:12 -07001351static int
Chris Wilsonfe115622016-10-28 13:58:40 +01001352i915_gem_gtt_pwrite_fast(struct drm_i915_gem_object *obj,
1353 const struct drm_i915_gem_pwrite *args)
Eric Anholt673a3942008-07-30 12:06:12 -07001354{
Chris Wilsonfe115622016-10-28 13:58:40 +01001355 struct drm_i915_private *i915 = to_i915(obj->base.dev);
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301356 struct i915_ggtt *ggtt = &i915->ggtt;
1357 struct drm_mm_node node;
Chris Wilsonfe115622016-10-28 13:58:40 +01001358 struct i915_vma *vma;
1359 u64 remain, offset;
1360 void __user *user_data;
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301361 int ret;
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301362
Chris Wilsonfe115622016-10-28 13:58:40 +01001363 ret = mutex_lock_interruptible(&i915->drm.struct_mutex);
1364 if (ret)
1365 return ret;
Daniel Vetter935aaa62012-03-25 19:47:35 +02001366
Chris Wilson8bd818152017-10-19 07:37:33 +01001367 if (i915_gem_object_has_struct_page(obj)) {
1368 /*
1369 * Avoid waking the device up if we can fallback, as
1370 * waking/resuming is very slow (worst-case 10-100 ms
1371 * depending on PCI sleeps and our own resume time).
1372 * This easily dwarfs any performance advantage from
1373 * using the cache bypass of indirect GGTT access.
1374 */
1375 if (!intel_runtime_pm_get_if_in_use(i915)) {
1376 ret = -EFAULT;
1377 goto out_unlock;
1378 }
1379 } else {
1380 /* No backing pages, no fallback, we must force GGTT access */
1381 intel_runtime_pm_get(i915);
1382 }
1383
Chris Wilson058d88c2016-08-15 10:49:06 +01001384 vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0,
Chris Wilsona3259ca2017-10-09 09:44:00 +01001385 PIN_MAPPABLE |
1386 PIN_NONFAULT |
1387 PIN_NONBLOCK);
Chris Wilson18034582016-08-18 17:16:45 +01001388 if (!IS_ERR(vma)) {
1389 node.start = i915_ggtt_offset(vma);
1390 node.allocated = false;
Chris Wilson49ef5292016-08-18 17:17:00 +01001391 ret = i915_vma_put_fence(vma);
Chris Wilson18034582016-08-18 17:16:45 +01001392 if (ret) {
1393 i915_vma_unpin(vma);
1394 vma = ERR_PTR(ret);
1395 }
1396 }
Chris Wilson058d88c2016-08-15 10:49:06 +01001397 if (IS_ERR(vma)) {
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001398 ret = insert_mappable_node(ggtt, &node, PAGE_SIZE);
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301399 if (ret)
Chris Wilson8bd818152017-10-19 07:37:33 +01001400 goto out_rpm;
Chris Wilsonfe115622016-10-28 13:58:40 +01001401 GEM_BUG_ON(!node.allocated);
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301402 }
Daniel Vetter935aaa62012-03-25 19:47:35 +02001403
1404 ret = i915_gem_object_set_to_gtt_domain(obj, true);
1405 if (ret)
1406 goto out_unpin;
1407
Chris Wilsonfe115622016-10-28 13:58:40 +01001408 mutex_unlock(&i915->drm.struct_mutex);
1409
Chris Wilsonb19482d2016-08-18 17:16:43 +01001410 intel_fb_obj_invalidate(obj, ORIGIN_CPU);
Paulo Zanoni063e4e62015-02-13 17:23:45 -02001411
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301412 user_data = u64_to_user_ptr(args->data_ptr);
1413 offset = args->offset;
1414 remain = args->size;
1415 while (remain) {
Eric Anholt673a3942008-07-30 12:06:12 -07001416 /* Operation in this page
1417 *
Keith Packard0839ccb2008-10-30 19:38:48 -07001418 * page_base = page offset within aperture
1419 * page_offset = offset within page
1420 * page_length = bytes to copy for this page
Eric Anholt673a3942008-07-30 12:06:12 -07001421 */
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301422 u32 page_base = node.start;
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001423 unsigned int page_offset = offset_in_page(offset);
1424 unsigned int page_length = PAGE_SIZE - page_offset;
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301425 page_length = remain < page_length ? remain : page_length;
1426 if (node.allocated) {
1427 wmb(); /* flush the write before we modify the GGTT */
Chris Wilson82ad6442018-06-05 16:37:58 +01001428 ggtt->vm.insert_page(&ggtt->vm,
1429 i915_gem_object_get_dma_address(obj, offset >> PAGE_SHIFT),
1430 node.start, I915_CACHE_NONE, 0);
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301431 wmb(); /* flush modifications to the GGTT (insert_page) */
1432 } else {
1433 page_base += offset & PAGE_MASK;
1434 }
Keith Packard0839ccb2008-10-30 19:38:48 -07001435 /* If we get a fault while copying data, then (presumably) our
Eric Anholt3de09aa2009-03-09 09:42:23 -07001436 * source page isn't available. Return the error and we'll
1437 * retry in the slow path.
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301438 * If the object is non-shmem backed, we retry again with the
1439 * path that handles page fault.
Keith Packard0839ccb2008-10-30 19:38:48 -07001440 */
Matthew Auld73ebd502017-12-11 15:18:20 +00001441 if (ggtt_write(&ggtt->iomap, page_base, page_offset,
Chris Wilsonfe115622016-10-28 13:58:40 +01001442 user_data, page_length)) {
1443 ret = -EFAULT;
1444 break;
Daniel Vetter935aaa62012-03-25 19:47:35 +02001445 }
Eric Anholt673a3942008-07-30 12:06:12 -07001446
Keith Packard0839ccb2008-10-30 19:38:48 -07001447 remain -= page_length;
1448 user_data += page_length;
1449 offset += page_length;
Eric Anholt673a3942008-07-30 12:06:12 -07001450 }
Chris Wilsond59b21e2017-02-22 11:40:49 +00001451 intel_fb_obj_flush(obj, ORIGIN_CPU);
Chris Wilsonfe115622016-10-28 13:58:40 +01001452
1453 mutex_lock(&i915->drm.struct_mutex);
Daniel Vetter935aaa62012-03-25 19:47:35 +02001454out_unpin:
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301455 if (node.allocated) {
1456 wmb();
Chris Wilson82ad6442018-06-05 16:37:58 +01001457 ggtt->vm.clear_range(&ggtt->vm, node.start, node.size);
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301458 remove_mappable_node(&node);
1459 } else {
Chris Wilson058d88c2016-08-15 10:49:06 +01001460 i915_vma_unpin(vma);
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301461 }
Chris Wilson8bd818152017-10-19 07:37:33 +01001462out_rpm:
Chris Wilson9c870d02016-10-24 13:42:15 +01001463 intel_runtime_pm_put(i915);
Chris Wilson8bd818152017-10-19 07:37:33 +01001464out_unlock:
Chris Wilsonfe115622016-10-28 13:58:40 +01001465 mutex_unlock(&i915->drm.struct_mutex);
Eric Anholt3de09aa2009-03-09 09:42:23 -07001466 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07001467}
1468
Eric Anholt673a3942008-07-30 12:06:12 -07001469static int
Chris Wilsonfe115622016-10-28 13:58:40 +01001470shmem_pwrite_slow(struct page *page, int offset, int length,
Daniel Vetterd174bd62012-03-25 19:47:40 +02001471 char __user *user_data,
1472 bool page_do_bit17_swizzling,
1473 bool needs_clflush_before,
1474 bool needs_clflush_after)
Eric Anholt673a3942008-07-30 12:06:12 -07001475{
Daniel Vetterd174bd62012-03-25 19:47:40 +02001476 char *vaddr;
1477 int ret;
Eric Anholt40123c12009-03-09 13:42:30 -07001478
Daniel Vetterd174bd62012-03-25 19:47:40 +02001479 vaddr = kmap(page);
Daniel Vettere7e58eb2012-03-25 19:47:43 +02001480 if (unlikely(needs_clflush_before || page_do_bit17_swizzling))
Chris Wilsonfe115622016-10-28 13:58:40 +01001481 shmem_clflush_swizzled_range(vaddr + offset, length,
Daniel Vetter23c18c72012-03-25 19:47:42 +02001482 page_do_bit17_swizzling);
Daniel Vetterd174bd62012-03-25 19:47:40 +02001483 if (page_do_bit17_swizzling)
Chris Wilsonfe115622016-10-28 13:58:40 +01001484 ret = __copy_from_user_swizzled(vaddr, offset, user_data,
1485 length);
Daniel Vetterd174bd62012-03-25 19:47:40 +02001486 else
Chris Wilsonfe115622016-10-28 13:58:40 +01001487 ret = __copy_from_user(vaddr + offset, user_data, length);
Daniel Vetterd174bd62012-03-25 19:47:40 +02001488 if (needs_clflush_after)
Chris Wilsonfe115622016-10-28 13:58:40 +01001489 shmem_clflush_swizzled_range(vaddr + offset, length,
Daniel Vetter23c18c72012-03-25 19:47:42 +02001490 page_do_bit17_swizzling);
Daniel Vetterd174bd62012-03-25 19:47:40 +02001491 kunmap(page);
Chris Wilsone5281cc2010-10-28 13:45:36 +01001492
Chris Wilson755d2212012-09-04 21:02:55 +01001493 return ret ? -EFAULT : 0;
Eric Anholt40123c12009-03-09 13:42:30 -07001494}
1495
Chris Wilsonfe115622016-10-28 13:58:40 +01001496/* Per-page copy function for the shmem pwrite fastpath.
1497 * Flushes invalid cachelines before writing to the target if
1498 * needs_clflush_before is set and flushes out any written cachelines after
1499 * writing if needs_clflush is set.
1500 */
Eric Anholt40123c12009-03-09 13:42:30 -07001501static int
Chris Wilsonfe115622016-10-28 13:58:40 +01001502shmem_pwrite(struct page *page, int offset, int len, char __user *user_data,
1503 bool page_do_bit17_swizzling,
1504 bool needs_clflush_before,
1505 bool needs_clflush_after)
Eric Anholt40123c12009-03-09 13:42:30 -07001506{
Chris Wilsonfe115622016-10-28 13:58:40 +01001507 int ret;
Eric Anholt40123c12009-03-09 13:42:30 -07001508
Chris Wilsonfe115622016-10-28 13:58:40 +01001509 ret = -ENODEV;
1510 if (!page_do_bit17_swizzling) {
1511 char *vaddr = kmap_atomic(page);
1512
1513 if (needs_clflush_before)
1514 drm_clflush_virt_range(vaddr + offset, len);
1515 ret = __copy_from_user_inatomic(vaddr + offset, user_data, len);
1516 if (needs_clflush_after)
1517 drm_clflush_virt_range(vaddr + offset, len);
1518
1519 kunmap_atomic(vaddr);
1520 }
1521 if (ret == 0)
1522 return ret;
1523
1524 return shmem_pwrite_slow(page, offset, len, user_data,
1525 page_do_bit17_swizzling,
1526 needs_clflush_before,
1527 needs_clflush_after);
1528}
1529
1530static int
1531i915_gem_shmem_pwrite(struct drm_i915_gem_object *obj,
1532 const struct drm_i915_gem_pwrite *args)
1533{
1534 struct drm_i915_private *i915 = to_i915(obj->base.dev);
1535 void __user *user_data;
1536 u64 remain;
1537 unsigned int obj_do_bit17_swizzling;
1538 unsigned int partial_cacheline_write;
1539 unsigned int needs_clflush;
1540 unsigned int offset, idx;
1541 int ret;
1542
1543 ret = mutex_lock_interruptible(&i915->drm.struct_mutex);
Chris Wilson43394c72016-08-18 17:16:47 +01001544 if (ret)
1545 return ret;
Eric Anholt40123c12009-03-09 13:42:30 -07001546
Chris Wilsonfe115622016-10-28 13:58:40 +01001547 ret = i915_gem_obj_prepare_shmem_write(obj, &needs_clflush);
1548 mutex_unlock(&i915->drm.struct_mutex);
1549 if (ret)
1550 return ret;
1551
1552 obj_do_bit17_swizzling = 0;
1553 if (i915_gem_object_needs_bit17_swizzle(obj))
1554 obj_do_bit17_swizzling = BIT(17);
1555
1556 /* If we don't overwrite a cacheline completely we need to be
1557 * careful to have up-to-date data by first clflushing. Don't
1558 * overcomplicate things and flush the entire patch.
1559 */
1560 partial_cacheline_write = 0;
1561 if (needs_clflush & CLFLUSH_BEFORE)
1562 partial_cacheline_write = boot_cpu_data.x86_clflush_size - 1;
1563
Chris Wilson43394c72016-08-18 17:16:47 +01001564 user_data = u64_to_user_ptr(args->data_ptr);
Chris Wilson43394c72016-08-18 17:16:47 +01001565 remain = args->size;
Chris Wilsonfe115622016-10-28 13:58:40 +01001566 offset = offset_in_page(args->offset);
1567 for (idx = args->offset >> PAGE_SHIFT; remain; idx++) {
1568 struct page *page = i915_gem_object_get_page(obj, idx);
1569 int length;
Eric Anholt40123c12009-03-09 13:42:30 -07001570
Chris Wilsonfe115622016-10-28 13:58:40 +01001571 length = remain;
1572 if (offset + length > PAGE_SIZE)
1573 length = PAGE_SIZE - offset;
Chris Wilsone5281cc2010-10-28 13:45:36 +01001574
Chris Wilsonfe115622016-10-28 13:58:40 +01001575 ret = shmem_pwrite(page, offset, length, user_data,
1576 page_to_phys(page) & obj_do_bit17_swizzling,
1577 (offset | length) & partial_cacheline_write,
1578 needs_clflush & CLFLUSH_AFTER);
1579 if (ret)
Chris Wilson9da3da62012-06-01 15:20:22 +01001580 break;
1581
Chris Wilsonfe115622016-10-28 13:58:40 +01001582 remain -= length;
1583 user_data += length;
1584 offset = 0;
Eric Anholt40123c12009-03-09 13:42:30 -07001585 }
1586
Chris Wilsond59b21e2017-02-22 11:40:49 +00001587 intel_fb_obj_flush(obj, ORIGIN_CPU);
Chris Wilsonfe115622016-10-28 13:58:40 +01001588 i915_gem_obj_finish_shmem_access(obj);
Eric Anholt40123c12009-03-09 13:42:30 -07001589 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07001590}
1591
1592/**
1593 * Writes data to the object referenced by handle.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01001594 * @dev: drm device
1595 * @data: ioctl data blob
1596 * @file: drm file
Eric Anholt673a3942008-07-30 12:06:12 -07001597 *
1598 * On error, the contents of the buffer that were to be modified are undefined.
1599 */
1600int
1601i915_gem_pwrite_ioctl(struct drm_device *dev, void *data,
Chris Wilsonfbd5a262010-10-14 15:03:58 +01001602 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001603{
1604 struct drm_i915_gem_pwrite *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00001605 struct drm_i915_gem_object *obj;
Chris Wilson51311d02010-11-17 09:10:42 +00001606 int ret;
1607
1608 if (args->size == 0)
1609 return 0;
1610
1611 if (!access_ok(VERIFY_READ,
Gustavo Padovan3ed605b2016-04-26 12:32:27 -03001612 u64_to_user_ptr(args->data_ptr),
Chris Wilson51311d02010-11-17 09:10:42 +00001613 args->size))
1614 return -EFAULT;
1615
Chris Wilson03ac0642016-07-20 13:31:51 +01001616 obj = i915_gem_object_lookup(file, args->handle);
Chris Wilson258a5ed2016-08-05 10:14:16 +01001617 if (!obj)
1618 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07001619
Chris Wilson7dcd2492010-09-26 20:21:44 +01001620 /* Bounds check destination. */
Matthew Auld966d5bf2016-12-13 20:32:22 +00001621 if (range_overflows_t(u64, args->offset, args->size, obj->base.size)) {
Chris Wilsonce9d4192010-09-26 20:50:05 +01001622 ret = -EINVAL;
Chris Wilson258a5ed2016-08-05 10:14:16 +01001623 goto err;
Chris Wilsonce9d4192010-09-26 20:50:05 +01001624 }
1625
Chris Wilsondb53a302011-02-03 11:57:46 +00001626 trace_i915_gem_object_pwrite(obj, args->offset, args->size);
1627
Chris Wilson7c55e2c2017-03-07 12:03:38 +00001628 ret = -ENODEV;
1629 if (obj->ops->pwrite)
1630 ret = obj->ops->pwrite(obj, args);
1631 if (ret != -ENODEV)
1632 goto err;
1633
Chris Wilsone95433c2016-10-28 13:58:27 +01001634 ret = i915_gem_object_wait(obj,
1635 I915_WAIT_INTERRUPTIBLE |
1636 I915_WAIT_ALL,
1637 MAX_SCHEDULE_TIMEOUT,
1638 to_rps_client(file));
Chris Wilson258a5ed2016-08-05 10:14:16 +01001639 if (ret)
1640 goto err;
1641
Chris Wilsonfe115622016-10-28 13:58:40 +01001642 ret = i915_gem_object_pin_pages(obj);
Chris Wilson258a5ed2016-08-05 10:14:16 +01001643 if (ret)
Chris Wilsonfe115622016-10-28 13:58:40 +01001644 goto err;
Chris Wilson258a5ed2016-08-05 10:14:16 +01001645
Daniel Vetter935aaa62012-03-25 19:47:35 +02001646 ret = -EFAULT;
Eric Anholt673a3942008-07-30 12:06:12 -07001647 /* We can only do the GTT pwrite on untiled buffers, as otherwise
1648 * it would end up going through the fenced access, and we'll get
1649 * different detiling behavior between reading and writing.
1650 * pread/pwrite currently are reading and writing from the CPU
1651 * perspective, requiring manual detiling by the client.
1652 */
Chris Wilson6eae0052016-06-20 15:05:52 +01001653 if (!i915_gem_object_has_struct_page(obj) ||
Chris Wilson9c870d02016-10-24 13:42:15 +01001654 cpu_write_needs_clflush(obj))
Daniel Vetter935aaa62012-03-25 19:47:35 +02001655 /* Note that the gtt paths might fail with non-page-backed user
1656 * pointers (e.g. gtt mappings when moving data between
Chris Wilson9c870d02016-10-24 13:42:15 +01001657 * textures). Fallback to the shmem path in that case.
1658 */
Chris Wilsonfe115622016-10-28 13:58:40 +01001659 ret = i915_gem_gtt_pwrite_fast(obj, args);
Eric Anholt673a3942008-07-30 12:06:12 -07001660
Chris Wilsond1054ee2016-07-16 18:42:36 +01001661 if (ret == -EFAULT || ret == -ENOSPC) {
Chris Wilson6a2c4232014-11-04 04:51:40 -08001662 if (obj->phys_handle)
1663 ret = i915_gem_phys_pwrite(obj, args, file);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301664 else
Chris Wilsonfe115622016-10-28 13:58:40 +01001665 ret = i915_gem_shmem_pwrite(obj, args);
Chris Wilson6a2c4232014-11-04 04:51:40 -08001666 }
Daniel Vetter5c0480f2011-12-14 13:57:30 +01001667
Chris Wilsonfe115622016-10-28 13:58:40 +01001668 i915_gem_object_unpin_pages(obj);
Chris Wilson258a5ed2016-08-05 10:14:16 +01001669err:
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001670 i915_gem_object_put(obj);
Chris Wilson258a5ed2016-08-05 10:14:16 +01001671 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07001672}
1673
Chris Wilson40e62d52016-10-28 13:58:41 +01001674static void i915_gem_object_bump_inactive_ggtt(struct drm_i915_gem_object *obj)
1675{
1676 struct drm_i915_private *i915;
1677 struct list_head *list;
1678 struct i915_vma *vma;
1679
Chris Wilsonf2123812017-10-16 12:40:37 +01001680 GEM_BUG_ON(!i915_gem_object_has_pinned_pages(obj));
1681
Chris Wilsone2189dd2017-12-07 21:14:07 +00001682 for_each_ggtt_vma(vma, obj) {
Chris Wilson40e62d52016-10-28 13:58:41 +01001683 if (i915_vma_is_active(vma))
1684 continue;
1685
1686 if (!drm_mm_node_allocated(&vma->node))
1687 continue;
1688
1689 list_move_tail(&vma->vm_link, &vma->vm->inactive_list);
1690 }
1691
1692 i915 = to_i915(obj->base.dev);
Chris Wilsonf2123812017-10-16 12:40:37 +01001693 spin_lock(&i915->mm.obj_lock);
Chris Wilson40e62d52016-10-28 13:58:41 +01001694 list = obj->bind_count ? &i915->mm.bound_list : &i915->mm.unbound_list;
Chris Wilsonf2123812017-10-16 12:40:37 +01001695 list_move_tail(&obj->mm.link, list);
1696 spin_unlock(&i915->mm.obj_lock);
Chris Wilson40e62d52016-10-28 13:58:41 +01001697}
1698
Eric Anholt673a3942008-07-30 12:06:12 -07001699/**
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001700 * Called when user space prepares to use an object with the CPU, either
1701 * through the mmap ioctl's mapping or a GTT mapping.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01001702 * @dev: drm device
1703 * @data: ioctl data blob
1704 * @file: drm file
Eric Anholt673a3942008-07-30 12:06:12 -07001705 */
1706int
1707i915_gem_set_domain_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00001708 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001709{
1710 struct drm_i915_gem_set_domain *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00001711 struct drm_i915_gem_object *obj;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001712 uint32_t read_domains = args->read_domains;
1713 uint32_t write_domain = args->write_domain;
Chris Wilson40e62d52016-10-28 13:58:41 +01001714 int err;
Eric Anholt673a3942008-07-30 12:06:12 -07001715
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001716 /* Only handle setting domains to types used by the CPU. */
Chris Wilsonb8f90962016-08-05 10:14:07 +01001717 if ((write_domain | read_domains) & I915_GEM_GPU_DOMAINS)
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001718 return -EINVAL;
1719
1720 /* Having something in the write domain implies it's in the read
1721 * domain, and only that read domain. Enforce that in the request.
1722 */
1723 if (write_domain != 0 && read_domains != write_domain)
1724 return -EINVAL;
1725
Chris Wilson03ac0642016-07-20 13:31:51 +01001726 obj = i915_gem_object_lookup(file, args->handle);
Chris Wilsonb8f90962016-08-05 10:14:07 +01001727 if (!obj)
1728 return -ENOENT;
Jesse Barnes652c3932009-08-17 13:31:43 -07001729
Chris Wilson3236f572012-08-24 09:35:09 +01001730 /* Try to flush the object off the GPU without holding the lock.
1731 * We will repeat the flush holding the lock in the normal manner
1732 * to catch cases where we are gazumped.
1733 */
Chris Wilson40e62d52016-10-28 13:58:41 +01001734 err = i915_gem_object_wait(obj,
Chris Wilsone95433c2016-10-28 13:58:27 +01001735 I915_WAIT_INTERRUPTIBLE |
1736 (write_domain ? I915_WAIT_ALL : 0),
1737 MAX_SCHEDULE_TIMEOUT,
1738 to_rps_client(file));
Chris Wilson40e62d52016-10-28 13:58:41 +01001739 if (err)
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001740 goto out;
Chris Wilsonb8f90962016-08-05 10:14:07 +01001741
Tina Zhanga03f3952017-11-14 10:25:13 +00001742 /*
1743 * Proxy objects do not control access to the backing storage, ergo
1744 * they cannot be used as a means to manipulate the cache domain
1745 * tracking for that backing storage. The proxy object is always
1746 * considered to be outside of any cache domain.
1747 */
1748 if (i915_gem_object_is_proxy(obj)) {
1749 err = -ENXIO;
1750 goto out;
1751 }
1752
1753 /*
1754 * Flush and acquire obj->pages so that we are coherent through
Chris Wilson40e62d52016-10-28 13:58:41 +01001755 * direct access in memory with previous cached writes through
1756 * shmemfs and that our cache domain tracking remains valid.
1757 * For example, if the obj->filp was moved to swap without us
1758 * being notified and releasing the pages, we would mistakenly
1759 * continue to assume that the obj remained out of the CPU cached
1760 * domain.
1761 */
1762 err = i915_gem_object_pin_pages(obj);
1763 if (err)
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001764 goto out;
Chris Wilson40e62d52016-10-28 13:58:41 +01001765
1766 err = i915_mutex_lock_interruptible(dev);
1767 if (err)
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001768 goto out_unpin;
Chris Wilson3236f572012-08-24 09:35:09 +01001769
Chris Wilsone22d8e32017-04-12 12:01:11 +01001770 if (read_domains & I915_GEM_DOMAIN_WC)
1771 err = i915_gem_object_set_to_wc_domain(obj, write_domain);
1772 else if (read_domains & I915_GEM_DOMAIN_GTT)
1773 err = i915_gem_object_set_to_gtt_domain(obj, write_domain);
Chris Wilson43566de2015-01-02 16:29:29 +05301774 else
Chris Wilsone22d8e32017-04-12 12:01:11 +01001775 err = i915_gem_object_set_to_cpu_domain(obj, write_domain);
Chris Wilson40e62d52016-10-28 13:58:41 +01001776
1777 /* And bump the LRU for this access */
1778 i915_gem_object_bump_inactive_ggtt(obj);
1779
1780 mutex_unlock(&dev->struct_mutex);
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001781
Daniel Vetter031b6982015-06-26 19:35:16 +02001782 if (write_domain != 0)
Chris Wilsonef749212017-04-12 12:01:10 +01001783 intel_fb_obj_invalidate(obj,
1784 fb_write_origin(obj, write_domain));
Daniel Vetter031b6982015-06-26 19:35:16 +02001785
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001786out_unpin:
Chris Wilson40e62d52016-10-28 13:58:41 +01001787 i915_gem_object_unpin_pages(obj);
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001788out:
1789 i915_gem_object_put(obj);
Chris Wilson40e62d52016-10-28 13:58:41 +01001790 return err;
Eric Anholt673a3942008-07-30 12:06:12 -07001791}
1792
1793/**
1794 * Called when user space has done writes to this buffer
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01001795 * @dev: drm device
1796 * @data: ioctl data blob
1797 * @file: drm file
Eric Anholt673a3942008-07-30 12:06:12 -07001798 */
1799int
1800i915_gem_sw_finish_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00001801 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001802{
1803 struct drm_i915_gem_sw_finish *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00001804 struct drm_i915_gem_object *obj;
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001805
Chris Wilson03ac0642016-07-20 13:31:51 +01001806 obj = i915_gem_object_lookup(file, args->handle);
Chris Wilsonc21724c2016-08-05 10:14:19 +01001807 if (!obj)
1808 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07001809
Tina Zhanga03f3952017-11-14 10:25:13 +00001810 /*
1811 * Proxy objects are barred from CPU access, so there is no
1812 * need to ban sw_finish as it is a nop.
1813 */
1814
Eric Anholt673a3942008-07-30 12:06:12 -07001815 /* Pinned buffers may be scanout, so flush the cache */
Chris Wilson5a97bcc2017-02-22 11:40:46 +00001816 i915_gem_object_flush_if_display(obj);
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001817 i915_gem_object_put(obj);
Chris Wilson5a97bcc2017-02-22 11:40:46 +00001818
1819 return 0;
Eric Anholt673a3942008-07-30 12:06:12 -07001820}
1821
1822/**
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01001823 * i915_gem_mmap_ioctl - Maps the contents of an object, returning the address
1824 * it is mapped to.
1825 * @dev: drm device
1826 * @data: ioctl data blob
1827 * @file: drm file
Eric Anholt673a3942008-07-30 12:06:12 -07001828 *
1829 * While the mapping holds a reference on the contents of the object, it doesn't
1830 * imply a ref on the object itself.
Daniel Vetter34367382014-10-16 12:28:18 +02001831 *
1832 * IMPORTANT:
1833 *
1834 * DRM driver writers who look a this function as an example for how to do GEM
1835 * mmap support, please don't implement mmap support like here. The modern way
1836 * to implement DRM mmap support is with an mmap offset ioctl (like
1837 * i915_gem_mmap_gtt) and then using the mmap syscall on the DRM fd directly.
1838 * That way debug tooling like valgrind will understand what's going on, hiding
1839 * the mmap call in a driver private ioctl will break that. The i915 driver only
1840 * does cpu mmaps this way because we didn't know better.
Eric Anholt673a3942008-07-30 12:06:12 -07001841 */
1842int
1843i915_gem_mmap_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00001844 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001845{
1846 struct drm_i915_gem_mmap *args = data;
Chris Wilson03ac0642016-07-20 13:31:51 +01001847 struct drm_i915_gem_object *obj;
Eric Anholt673a3942008-07-30 12:06:12 -07001848 unsigned long addr;
1849
Akash Goel1816f922015-01-02 16:29:30 +05301850 if (args->flags & ~(I915_MMAP_WC))
1851 return -EINVAL;
1852
Borislav Petkov568a58e2016-03-29 17:42:01 +02001853 if (args->flags & I915_MMAP_WC && !boot_cpu_has(X86_FEATURE_PAT))
Akash Goel1816f922015-01-02 16:29:30 +05301854 return -ENODEV;
1855
Chris Wilson03ac0642016-07-20 13:31:51 +01001856 obj = i915_gem_object_lookup(file, args->handle);
1857 if (!obj)
Chris Wilsonbf79cb92010-08-04 14:19:46 +01001858 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07001859
Daniel Vetter1286ff72012-05-10 15:25:09 +02001860 /* prime objects have no backing filp to GEM mmap
1861 * pages from.
1862 */
Chris Wilson03ac0642016-07-20 13:31:51 +01001863 if (!obj->base.filp) {
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001864 i915_gem_object_put(obj);
Tina Zhang274b2462017-11-14 10:25:12 +00001865 return -ENXIO;
Daniel Vetter1286ff72012-05-10 15:25:09 +02001866 }
1867
Chris Wilson03ac0642016-07-20 13:31:51 +01001868 addr = vm_mmap(obj->base.filp, 0, args->size,
Eric Anholt673a3942008-07-30 12:06:12 -07001869 PROT_READ | PROT_WRITE, MAP_SHARED,
1870 args->offset);
Akash Goel1816f922015-01-02 16:29:30 +05301871 if (args->flags & I915_MMAP_WC) {
1872 struct mm_struct *mm = current->mm;
1873 struct vm_area_struct *vma;
1874
Michal Hocko80a89a52016-05-23 16:26:11 -07001875 if (down_write_killable(&mm->mmap_sem)) {
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001876 i915_gem_object_put(obj);
Michal Hocko80a89a52016-05-23 16:26:11 -07001877 return -EINTR;
1878 }
Akash Goel1816f922015-01-02 16:29:30 +05301879 vma = find_vma(mm, addr);
1880 if (vma)
1881 vma->vm_page_prot =
1882 pgprot_writecombine(vm_get_page_prot(vma->vm_flags));
1883 else
1884 addr = -ENOMEM;
1885 up_write(&mm->mmap_sem);
Chris Wilsonaeecc962016-06-17 14:46:39 -03001886
1887 /* This may race, but that's ok, it only gets set */
Chris Wilson50349242016-08-18 17:17:04 +01001888 WRITE_ONCE(obj->frontbuffer_ggtt_origin, ORIGIN_CPU);
Akash Goel1816f922015-01-02 16:29:30 +05301889 }
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001890 i915_gem_object_put(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001891 if (IS_ERR((void *)addr))
1892 return addr;
1893
1894 args->addr_ptr = (uint64_t) addr;
1895
1896 return 0;
1897}
1898
Chris Wilson03af84f2016-08-18 17:17:01 +01001899static unsigned int tile_row_pages(struct drm_i915_gem_object *obj)
1900{
Chris Wilson6649a0b2017-01-09 16:16:08 +00001901 return i915_gem_object_get_tile_row_size(obj) >> PAGE_SHIFT;
Chris Wilson03af84f2016-08-18 17:17:01 +01001902}
1903
Jesse Barnesde151cf2008-11-12 10:03:55 -08001904/**
Chris Wilson4cc69072016-08-25 19:05:19 +01001905 * i915_gem_mmap_gtt_version - report the current feature set for GTT mmaps
1906 *
1907 * A history of the GTT mmap interface:
1908 *
1909 * 0 - Everything had to fit into the GTT. Both parties of a memcpy had to
1910 * aligned and suitable for fencing, and still fit into the available
1911 * mappable space left by the pinned display objects. A classic problem
1912 * we called the page-fault-of-doom where we would ping-pong between
1913 * two objects that could not fit inside the GTT and so the memcpy
1914 * would page one object in at the expense of the other between every
1915 * single byte.
1916 *
1917 * 1 - Objects can be any size, and have any compatible fencing (X Y, or none
1918 * as set via i915_gem_set_tiling() [DRM_I915_GEM_SET_TILING]). If the
1919 * object is too large for the available space (or simply too large
1920 * for the mappable aperture!), a view is created instead and faulted
1921 * into userspace. (This view is aligned and sized appropriately for
1922 * fenced access.)
1923 *
Chris Wilsone22d8e32017-04-12 12:01:11 +01001924 * 2 - Recognise WC as a separate cache domain so that we can flush the
1925 * delayed writes via GTT before performing direct access via WC.
1926 *
Chris Wilson4cc69072016-08-25 19:05:19 +01001927 * Restrictions:
1928 *
1929 * * snoopable objects cannot be accessed via the GTT. It can cause machine
1930 * hangs on some architectures, corruption on others. An attempt to service
1931 * a GTT page fault from a snoopable object will generate a SIGBUS.
1932 *
1933 * * the object must be able to fit into RAM (physical memory, though no
1934 * limited to the mappable aperture).
1935 *
1936 *
1937 * Caveats:
1938 *
1939 * * a new GTT page fault will synchronize rendering from the GPU and flush
1940 * all data to system memory. Subsequent access will not be synchronized.
1941 *
1942 * * all mappings are revoked on runtime device suspend.
1943 *
1944 * * there are only 8, 16 or 32 fence registers to share between all users
1945 * (older machines require fence register for display and blitter access
1946 * as well). Contention of the fence registers will cause the previous users
1947 * to be unmapped and any new access will generate new page faults.
1948 *
1949 * * running out of memory while servicing a fault may generate a SIGBUS,
1950 * rather than the expected SIGSEGV.
1951 */
1952int i915_gem_mmap_gtt_version(void)
1953{
Chris Wilsone22d8e32017-04-12 12:01:11 +01001954 return 2;
Chris Wilson4cc69072016-08-25 19:05:19 +01001955}
1956
Chris Wilson2d4281b2017-01-10 09:56:32 +00001957static inline struct i915_ggtt_view
1958compute_partial_view(struct drm_i915_gem_object *obj,
Chris Wilson2d4281b2017-01-10 09:56:32 +00001959 pgoff_t page_offset,
1960 unsigned int chunk)
1961{
1962 struct i915_ggtt_view view;
1963
1964 if (i915_gem_object_is_tiled(obj))
1965 chunk = roundup(chunk, tile_row_pages(obj));
1966
Chris Wilson2d4281b2017-01-10 09:56:32 +00001967 view.type = I915_GGTT_VIEW_PARTIAL;
Chris Wilson8bab11932017-01-14 00:28:25 +00001968 view.partial.offset = rounddown(page_offset, chunk);
1969 view.partial.size =
Chris Wilson2d4281b2017-01-10 09:56:32 +00001970 min_t(unsigned int, chunk,
Chris Wilson8bab11932017-01-14 00:28:25 +00001971 (obj->base.size >> PAGE_SHIFT) - view.partial.offset);
Chris Wilson2d4281b2017-01-10 09:56:32 +00001972
1973 /* If the partial covers the entire object, just create a normal VMA. */
1974 if (chunk >= obj->base.size >> PAGE_SHIFT)
1975 view.type = I915_GGTT_VIEW_NORMAL;
1976
1977 return view;
1978}
1979
Chris Wilson4cc69072016-08-25 19:05:19 +01001980/**
Jesse Barnesde151cf2008-11-12 10:03:55 -08001981 * i915_gem_fault - fault a page into the GTT
Geliang Tangd9072a32015-09-15 05:58:44 -07001982 * @vmf: fault info
Jesse Barnesde151cf2008-11-12 10:03:55 -08001983 *
1984 * The fault handler is set up by drm_gem_mmap() when a object is GTT mapped
1985 * from userspace. The fault handler takes care of binding the object to
1986 * the GTT (if needed), allocating and programming a fence register (again,
1987 * only if needed based on whether the old reg is still valid or the object
1988 * is tiled) and inserting a new PTE into the faulting process.
1989 *
1990 * Note that the faulting process may involve evicting existing objects
1991 * from the GTT and/or fence registers to make room. So performance may
1992 * suffer if the GTT working set is large or there are few fence registers
1993 * left.
Chris Wilson4cc69072016-08-25 19:05:19 +01001994 *
1995 * The current feature set supported by i915_gem_fault() and thus GTT mmaps
1996 * is exposed via I915_PARAM_MMAP_GTT_VERSION (see i915_gem_mmap_gtt_version).
Jesse Barnesde151cf2008-11-12 10:03:55 -08001997 */
Chris Wilson52137012018-06-06 22:45:20 +01001998vm_fault_t i915_gem_fault(struct vm_fault *vmf)
Jesse Barnesde151cf2008-11-12 10:03:55 -08001999{
Chris Wilson420980c2018-06-05 14:57:46 +01002000#define MIN_CHUNK_PAGES (SZ_1M >> PAGE_SHIFT)
Dave Jiang11bac802017-02-24 14:56:41 -08002001 struct vm_area_struct *area = vmf->vma;
Chris Wilson058d88c2016-08-15 10:49:06 +01002002 struct drm_i915_gem_object *obj = to_intel_bo(area->vm_private_data);
Chris Wilson05394f32010-11-08 19:18:58 +00002003 struct drm_device *dev = obj->base.dev;
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002004 struct drm_i915_private *dev_priv = to_i915(dev);
2005 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Chris Wilsonb8f90962016-08-05 10:14:07 +01002006 bool write = !!(vmf->flags & FAULT_FLAG_WRITE);
Chris Wilson058d88c2016-08-15 10:49:06 +01002007 struct i915_vma *vma;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002008 pgoff_t page_offset;
Chris Wilson82118872016-08-18 17:17:05 +01002009 unsigned int flags;
Chris Wilsonb8f90962016-08-05 10:14:07 +01002010 int ret;
Paulo Zanonif65c9162013-11-27 18:20:34 -02002011
Jesse Barnesde151cf2008-11-12 10:03:55 -08002012 /* We don't use vmf->pgoff since that has the fake offset */
Jan Kara1a29d852016-12-14 15:07:01 -08002013 page_offset = (vmf->address - area->vm_start) >> PAGE_SHIFT;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002014
Chris Wilsondb53a302011-02-03 11:57:46 +00002015 trace_i915_gem_object_fault(obj, page_offset, true, write);
2016
Chris Wilson6e4930f2014-02-07 18:37:06 -02002017 /* Try to flush the object off the GPU first without holding the lock.
Chris Wilsonb8f90962016-08-05 10:14:07 +01002018 * Upon acquiring the lock, we will perform our sanity checks and then
Chris Wilson6e4930f2014-02-07 18:37:06 -02002019 * repeat the flush holding the lock in the normal manner to catch cases
2020 * where we are gazumped.
2021 */
Chris Wilsone95433c2016-10-28 13:58:27 +01002022 ret = i915_gem_object_wait(obj,
2023 I915_WAIT_INTERRUPTIBLE,
2024 MAX_SCHEDULE_TIMEOUT,
2025 NULL);
Chris Wilson6e4930f2014-02-07 18:37:06 -02002026 if (ret)
Chris Wilsonb8f90962016-08-05 10:14:07 +01002027 goto err;
2028
Chris Wilson40e62d52016-10-28 13:58:41 +01002029 ret = i915_gem_object_pin_pages(obj);
2030 if (ret)
2031 goto err;
2032
Chris Wilsonb8f90962016-08-05 10:14:07 +01002033 intel_runtime_pm_get(dev_priv);
2034
2035 ret = i915_mutex_lock_interruptible(dev);
2036 if (ret)
2037 goto err_rpm;
Chris Wilson6e4930f2014-02-07 18:37:06 -02002038
Chris Wilsoneb119bd2012-12-16 12:43:36 +00002039 /* Access to snoopable pages through the GTT is incoherent. */
Tvrtko Ursulin0031fb92016-11-04 14:42:44 +00002040 if (obj->cache_level != I915_CACHE_NONE && !HAS_LLC(dev_priv)) {
Chris Wilsonddeff6e2014-05-28 16:16:41 +01002041 ret = -EFAULT;
Chris Wilsonb8f90962016-08-05 10:14:07 +01002042 goto err_unlock;
Chris Wilsoneb119bd2012-12-16 12:43:36 +00002043 }
2044
Chris Wilson82118872016-08-18 17:17:05 +01002045 /* If the object is smaller than a couple of partial vma, it is
2046 * not worth only creating a single partial vma - we may as well
2047 * clear enough space for the full object.
2048 */
2049 flags = PIN_MAPPABLE;
2050 if (obj->base.size > 2 * MIN_CHUNK_PAGES << PAGE_SHIFT)
2051 flags |= PIN_NONBLOCK | PIN_NONFAULT;
2052
Chris Wilsona61007a2016-08-18 17:17:02 +01002053 /* Now pin it into the GTT as needed */
Chris Wilson82118872016-08-18 17:17:05 +01002054 vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0, flags);
Chris Wilsona61007a2016-08-18 17:17:02 +01002055 if (IS_ERR(vma)) {
Chris Wilsona61007a2016-08-18 17:17:02 +01002056 /* Use a partial view if it is bigger than available space */
Chris Wilson2d4281b2017-01-10 09:56:32 +00002057 struct i915_ggtt_view view =
Chris Wilson8201c1f2017-01-10 09:56:33 +00002058 compute_partial_view(obj, page_offset, MIN_CHUNK_PAGES);
Chris Wilsonaa136d92016-08-18 17:17:03 +01002059
Chris Wilson50349242016-08-18 17:17:04 +01002060 /* Userspace is now writing through an untracked VMA, abandon
2061 * all hope that the hardware is able to track future writes.
2062 */
2063 obj->frontbuffer_ggtt_origin = ORIGIN_CPU;
2064
Chris Wilsona61007a2016-08-18 17:17:02 +01002065 vma = i915_gem_object_ggtt_pin(obj, &view, 0, 0, PIN_MAPPABLE);
2066 }
Chris Wilson058d88c2016-08-15 10:49:06 +01002067 if (IS_ERR(vma)) {
2068 ret = PTR_ERR(vma);
Chris Wilsonb8f90962016-08-05 10:14:07 +01002069 goto err_unlock;
Chris Wilson058d88c2016-08-15 10:49:06 +01002070 }
Jesse Barnesde151cf2008-11-12 10:03:55 -08002071
Chris Wilsonc9839302012-11-20 10:45:17 +00002072 ret = i915_gem_object_set_to_gtt_domain(obj, write);
2073 if (ret)
Chris Wilsonb8f90962016-08-05 10:14:07 +01002074 goto err_unpin;
Chris Wilsonc9839302012-11-20 10:45:17 +00002075
Chris Wilson3bd40732017-10-09 09:43:56 +01002076 ret = i915_vma_pin_fence(vma);
Chris Wilsonc9839302012-11-20 10:45:17 +00002077 if (ret)
Chris Wilsonb8f90962016-08-05 10:14:07 +01002078 goto err_unpin;
Chris Wilson7d1c4802010-08-07 21:45:03 +01002079
Chris Wilsonb90b91d2014-06-10 12:14:40 +01002080 /* Finally, remap it using the new GTT offset */
Chris Wilsonc58305a2016-08-19 16:54:28 +01002081 ret = remap_io_mapping(area,
Chris Wilson8bab11932017-01-14 00:28:25 +00002082 area->vm_start + (vma->ggtt_view.partial.offset << PAGE_SHIFT),
Matthew Auld73ebd502017-12-11 15:18:20 +00002083 (ggtt->gmadr.start + vma->node.start) >> PAGE_SHIFT,
Chris Wilsonc58305a2016-08-19 16:54:28 +01002084 min_t(u64, vma->size, area->vm_end - area->vm_start),
Matthew Auld73ebd502017-12-11 15:18:20 +00002085 &ggtt->iomap);
Chris Wilsona65adaf2017-10-09 09:43:57 +01002086 if (ret)
2087 goto err_fence;
Chris Wilsona61007a2016-08-18 17:17:02 +01002088
Chris Wilsona65adaf2017-10-09 09:43:57 +01002089 /* Mark as being mmapped into userspace for later revocation */
2090 assert_rpm_wakelock_held(dev_priv);
2091 if (!i915_vma_set_userfault(vma) && !obj->userfault_count++)
2092 list_add(&obj->userfault_link, &dev_priv->mm.userfault_list);
2093 GEM_BUG_ON(!obj->userfault_count);
2094
Chris Wilson7125397b2017-12-06 12:49:14 +00002095 i915_vma_set_ggtt_write(vma);
2096
Chris Wilsona65adaf2017-10-09 09:43:57 +01002097err_fence:
Chris Wilson3bd40732017-10-09 09:43:56 +01002098 i915_vma_unpin_fence(vma);
Chris Wilsonb8f90962016-08-05 10:14:07 +01002099err_unpin:
Chris Wilson058d88c2016-08-15 10:49:06 +01002100 __i915_vma_unpin(vma);
Chris Wilsonb8f90962016-08-05 10:14:07 +01002101err_unlock:
Jesse Barnesde151cf2008-11-12 10:03:55 -08002102 mutex_unlock(&dev->struct_mutex);
Chris Wilsonb8f90962016-08-05 10:14:07 +01002103err_rpm:
2104 intel_runtime_pm_put(dev_priv);
Chris Wilson40e62d52016-10-28 13:58:41 +01002105 i915_gem_object_unpin_pages(obj);
Chris Wilsonb8f90962016-08-05 10:14:07 +01002106err:
Jesse Barnesde151cf2008-11-12 10:03:55 -08002107 switch (ret) {
Chris Wilsond9bc7e92011-02-07 13:09:31 +00002108 case -EIO:
Daniel Vetter2232f032014-09-04 09:36:18 +02002109 /*
2110 * We eat errors when the gpu is terminally wedged to avoid
2111 * userspace unduly crashing (gl has no provisions for mmaps to
2112 * fail). But any other -EIO isn't ours (e.g. swap in failure)
2113 * and so needs to be reported.
2114 */
Chris Wilson52137012018-06-06 22:45:20 +01002115 if (!i915_terminally_wedged(&dev_priv->gpu_error))
2116 return VM_FAULT_SIGBUS;
Chris Wilson045e7692010-11-07 09:18:22 +00002117 case -EAGAIN:
Daniel Vetter571c6082013-09-12 17:57:28 +02002118 /*
2119 * EAGAIN means the gpu is hung and we'll wait for the error
2120 * handler to reset everything when re-faulting in
2121 * i915_mutex_lock_interruptible.
Chris Wilsond9bc7e92011-02-07 13:09:31 +00002122 */
Chris Wilsonc7150892009-09-23 00:43:56 +01002123 case 0:
2124 case -ERESTARTSYS:
Chris Wilsonbed636a2011-02-11 20:31:19 +00002125 case -EINTR:
Dmitry Rogozhkine79e0fe2012-10-03 17:15:26 +03002126 case -EBUSY:
2127 /*
2128 * EBUSY is ok: this just means that another thread
2129 * already did the job.
2130 */
Chris Wilson52137012018-06-06 22:45:20 +01002131 return VM_FAULT_NOPAGE;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002132 case -ENOMEM:
Chris Wilson52137012018-06-06 22:45:20 +01002133 return VM_FAULT_OOM;
Daniel Vettera7c2e1a2012-10-17 11:17:16 +02002134 case -ENOSPC:
Chris Wilson45d67812014-01-31 11:34:57 +00002135 case -EFAULT:
Chris Wilson52137012018-06-06 22:45:20 +01002136 return VM_FAULT_SIGBUS;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002137 default:
Daniel Vettera7c2e1a2012-10-17 11:17:16 +02002138 WARN_ONCE(ret, "unhandled error in i915_gem_fault: %i\n", ret);
Chris Wilson52137012018-06-06 22:45:20 +01002139 return VM_FAULT_SIGBUS;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002140 }
2141}
2142
Chris Wilsona65adaf2017-10-09 09:43:57 +01002143static void __i915_gem_object_release_mmap(struct drm_i915_gem_object *obj)
2144{
2145 struct i915_vma *vma;
2146
2147 GEM_BUG_ON(!obj->userfault_count);
2148
2149 obj->userfault_count = 0;
2150 list_del(&obj->userfault_link);
2151 drm_vma_node_unmap(&obj->base.vma_node,
2152 obj->base.dev->anon_inode->i_mapping);
2153
Chris Wilsone2189dd2017-12-07 21:14:07 +00002154 for_each_ggtt_vma(vma, obj)
Chris Wilsona65adaf2017-10-09 09:43:57 +01002155 i915_vma_unset_userfault(vma);
Chris Wilsona65adaf2017-10-09 09:43:57 +01002156}
2157
Jesse Barnesde151cf2008-11-12 10:03:55 -08002158/**
Chris Wilson901782b2009-07-10 08:18:50 +01002159 * i915_gem_release_mmap - remove physical page mappings
2160 * @obj: obj in question
2161 *
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02002162 * Preserve the reservation of the mmapping with the DRM core code, but
Chris Wilson901782b2009-07-10 08:18:50 +01002163 * relinquish ownership of the pages back to the system.
2164 *
2165 * It is vital that we remove the page mapping if we have mapped a tiled
2166 * object through the GTT and then lose the fence register due to
2167 * resource pressure. Similarly if the object has been moved out of the
2168 * aperture, than pages mapped into userspace must be revoked. Removing the
2169 * mapping will then trigger a page fault on the next user access, allowing
2170 * fixup by i915_gem_fault().
2171 */
Eric Anholtd05ca302009-07-10 13:02:26 -07002172void
Chris Wilson05394f32010-11-08 19:18:58 +00002173i915_gem_release_mmap(struct drm_i915_gem_object *obj)
Chris Wilson901782b2009-07-10 08:18:50 +01002174{
Chris Wilson275f0392016-10-24 13:42:14 +01002175 struct drm_i915_private *i915 = to_i915(obj->base.dev);
Chris Wilson275f0392016-10-24 13:42:14 +01002176
Chris Wilson349f2cc2016-04-13 17:35:12 +01002177 /* Serialisation between user GTT access and our code depends upon
2178 * revoking the CPU's PTE whilst the mutex is held. The next user
2179 * pagefault then has to wait until we release the mutex.
Chris Wilson9c870d02016-10-24 13:42:15 +01002180 *
2181 * Note that RPM complicates somewhat by adding an additional
2182 * requirement that operations to the GGTT be made holding the RPM
2183 * wakeref.
Chris Wilson349f2cc2016-04-13 17:35:12 +01002184 */
Chris Wilson275f0392016-10-24 13:42:14 +01002185 lockdep_assert_held(&i915->drm.struct_mutex);
Chris Wilson9c870d02016-10-24 13:42:15 +01002186 intel_runtime_pm_get(i915);
Chris Wilson349f2cc2016-04-13 17:35:12 +01002187
Chris Wilsona65adaf2017-10-09 09:43:57 +01002188 if (!obj->userfault_count)
Chris Wilson9c870d02016-10-24 13:42:15 +01002189 goto out;
Chris Wilson901782b2009-07-10 08:18:50 +01002190
Chris Wilsona65adaf2017-10-09 09:43:57 +01002191 __i915_gem_object_release_mmap(obj);
Chris Wilson349f2cc2016-04-13 17:35:12 +01002192
2193 /* Ensure that the CPU's PTE are revoked and there are not outstanding
2194 * memory transactions from userspace before we return. The TLB
2195 * flushing implied above by changing the PTE above *should* be
2196 * sufficient, an extra barrier here just provides us with a bit
2197 * of paranoid documentation about our requirement to serialise
2198 * memory writes before touching registers / GSM.
2199 */
2200 wmb();
Chris Wilson9c870d02016-10-24 13:42:15 +01002201
2202out:
2203 intel_runtime_pm_put(i915);
Chris Wilson901782b2009-07-10 08:18:50 +01002204}
2205
Chris Wilson7c108fd2016-10-24 13:42:18 +01002206void i915_gem_runtime_suspend(struct drm_i915_private *dev_priv)
Chris Wilsoneedd10f2014-06-16 08:57:44 +01002207{
Chris Wilson3594a3e2016-10-24 13:42:16 +01002208 struct drm_i915_gem_object *obj, *on;
Chris Wilson7c108fd2016-10-24 13:42:18 +01002209 int i;
Chris Wilsoneedd10f2014-06-16 08:57:44 +01002210
Chris Wilson3594a3e2016-10-24 13:42:16 +01002211 /*
2212 * Only called during RPM suspend. All users of the userfault_list
2213 * must be holding an RPM wakeref to ensure that this can not
2214 * run concurrently with themselves (and use the struct_mutex for
2215 * protection between themselves).
2216 */
2217
2218 list_for_each_entry_safe(obj, on,
Chris Wilsona65adaf2017-10-09 09:43:57 +01002219 &dev_priv->mm.userfault_list, userfault_link)
2220 __i915_gem_object_release_mmap(obj);
Chris Wilson7c108fd2016-10-24 13:42:18 +01002221
2222 /* The fence will be lost when the device powers down. If any were
2223 * in use by hardware (i.e. they are pinned), we should not be powering
2224 * down! All other fences will be reacquired by the user upon waking.
2225 */
2226 for (i = 0; i < dev_priv->num_fence_regs; i++) {
2227 struct drm_i915_fence_reg *reg = &dev_priv->fence_regs[i];
2228
Chris Wilsone0ec3ec2017-02-03 12:57:17 +00002229 /* Ideally we want to assert that the fence register is not
2230 * live at this point (i.e. that no piece of code will be
2231 * trying to write through fence + GTT, as that both violates
2232 * our tracking of activity and associated locking/barriers,
2233 * but also is illegal given that the hw is powered down).
2234 *
2235 * Previously we used reg->pin_count as a "liveness" indicator.
2236 * That is not sufficient, and we need a more fine-grained
2237 * tool if we want to have a sanity check here.
2238 */
Chris Wilson7c108fd2016-10-24 13:42:18 +01002239
2240 if (!reg->vma)
2241 continue;
2242
Chris Wilsona65adaf2017-10-09 09:43:57 +01002243 GEM_BUG_ON(i915_vma_has_userfault(reg->vma));
Chris Wilson7c108fd2016-10-24 13:42:18 +01002244 reg->dirty = true;
2245 }
Chris Wilsoneedd10f2014-06-16 08:57:44 +01002246}
2247
Chris Wilsond8cb5082012-08-11 15:41:03 +01002248static int i915_gem_object_create_mmap_offset(struct drm_i915_gem_object *obj)
2249{
Chris Wilsonfac5e232016-07-04 11:34:36 +01002250 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
Chris Wilsonf3f61842016-08-05 10:14:14 +01002251 int err;
Chris Wilsond8cb5082012-08-11 15:41:03 +01002252
Chris Wilsonf3f61842016-08-05 10:14:14 +01002253 err = drm_gem_create_mmap_offset(&obj->base);
Chris Wilsonb42a13d2017-01-06 15:22:40 +00002254 if (likely(!err))
Chris Wilsonf3f61842016-08-05 10:14:14 +01002255 return 0;
Daniel Vetterda494d72012-12-20 15:11:16 +01002256
Chris Wilsonb42a13d2017-01-06 15:22:40 +00002257 /* Attempt to reap some mmap space from dead objects */
2258 do {
2259 err = i915_gem_wait_for_idle(dev_priv, I915_WAIT_INTERRUPTIBLE);
2260 if (err)
2261 break;
Chris Wilsond8cb5082012-08-11 15:41:03 +01002262
Chris Wilsonb42a13d2017-01-06 15:22:40 +00002263 i915_gem_drain_freed_objects(dev_priv);
Chris Wilsonf3f61842016-08-05 10:14:14 +01002264 err = drm_gem_create_mmap_offset(&obj->base);
Chris Wilsonb42a13d2017-01-06 15:22:40 +00002265 if (!err)
2266 break;
2267
2268 } while (flush_delayed_work(&dev_priv->gt.retire_work));
Daniel Vetterda494d72012-12-20 15:11:16 +01002269
Chris Wilsonf3f61842016-08-05 10:14:14 +01002270 return err;
Chris Wilsond8cb5082012-08-11 15:41:03 +01002271}
2272
2273static void i915_gem_object_free_mmap_offset(struct drm_i915_gem_object *obj)
2274{
Chris Wilsond8cb5082012-08-11 15:41:03 +01002275 drm_gem_free_mmap_offset(&obj->base);
2276}
2277
Dave Airlieda6b51d2014-12-24 13:11:17 +10002278int
Dave Airlieff72145b2011-02-07 12:16:14 +10002279i915_gem_mmap_gtt(struct drm_file *file,
2280 struct drm_device *dev,
Dave Airlieda6b51d2014-12-24 13:11:17 +10002281 uint32_t handle,
Dave Airlieff72145b2011-02-07 12:16:14 +10002282 uint64_t *offset)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002283{
Chris Wilson05394f32010-11-08 19:18:58 +00002284 struct drm_i915_gem_object *obj;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002285 int ret;
2286
Chris Wilson03ac0642016-07-20 13:31:51 +01002287 obj = i915_gem_object_lookup(file, handle);
Chris Wilsonf3f61842016-08-05 10:14:14 +01002288 if (!obj)
2289 return -ENOENT;
Chris Wilsonab182822009-09-22 18:46:17 +01002290
Chris Wilsond8cb5082012-08-11 15:41:03 +01002291 ret = i915_gem_object_create_mmap_offset(obj);
Chris Wilsonf3f61842016-08-05 10:14:14 +01002292 if (ret == 0)
2293 *offset = drm_vma_node_offset_addr(&obj->base.vma_node);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002294
Chris Wilsonf0cd5182016-10-28 13:58:43 +01002295 i915_gem_object_put(obj);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01002296 return ret;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002297}
2298
Dave Airlieff72145b2011-02-07 12:16:14 +10002299/**
2300 * i915_gem_mmap_gtt_ioctl - prepare an object for GTT mmap'ing
2301 * @dev: DRM device
2302 * @data: GTT mapping ioctl data
2303 * @file: GEM object info
2304 *
2305 * Simply returns the fake offset to userspace so it can mmap it.
2306 * The mmap call will end up in drm_gem_mmap(), which will set things
2307 * up so we can get faults in the handler above.
2308 *
2309 * The fault handler will take care of binding the object into the GTT
2310 * (since it may have been evicted to make room for something), allocating
2311 * a fence register, and mapping the appropriate aperture address into
2312 * userspace.
2313 */
2314int
2315i915_gem_mmap_gtt_ioctl(struct drm_device *dev, void *data,
2316 struct drm_file *file)
2317{
2318 struct drm_i915_gem_mmap_gtt *args = data;
2319
Dave Airlieda6b51d2014-12-24 13:11:17 +10002320 return i915_gem_mmap_gtt(file, dev, args->handle, &args->offset);
Dave Airlieff72145b2011-02-07 12:16:14 +10002321}
2322
Daniel Vetter225067e2012-08-20 10:23:20 +02002323/* Immediately discard the backing storage */
2324static void
2325i915_gem_object_truncate(struct drm_i915_gem_object *obj)
Chris Wilsone5281cc2010-10-28 13:45:36 +01002326{
Chris Wilson4d6294bf2012-08-11 15:41:05 +01002327 i915_gem_object_free_mmap_offset(obj);
Daniel Vetter1286ff72012-05-10 15:25:09 +02002328
Chris Wilson4d6294bf2012-08-11 15:41:05 +01002329 if (obj->base.filp == NULL)
2330 return;
2331
Daniel Vetter225067e2012-08-20 10:23:20 +02002332 /* Our goal here is to return as much of the memory as
2333 * is possible back to the system as we are called from OOM.
2334 * To do this we must instruct the shmfs to drop all of its
2335 * backing pages, *now*.
Chris Wilsone5281cc2010-10-28 13:45:36 +01002336 */
Chris Wilson55372522014-03-25 13:23:06 +00002337 shmem_truncate_range(file_inode(obj->base.filp), 0, (loff_t)-1);
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002338 obj->mm.madv = __I915_MADV_PURGED;
Chris Wilson4e5462e2017-03-07 13:20:31 +00002339 obj->mm.pages = ERR_PTR(-EFAULT);
Chris Wilsone5281cc2010-10-28 13:45:36 +01002340}
Chris Wilsone5281cc2010-10-28 13:45:36 +01002341
Chris Wilson55372522014-03-25 13:23:06 +00002342/* Try to discard unwanted pages */
Chris Wilson03ac84f2016-10-28 13:58:36 +01002343void __i915_gem_object_invalidate(struct drm_i915_gem_object *obj)
Daniel Vetter225067e2012-08-20 10:23:20 +02002344{
Chris Wilson55372522014-03-25 13:23:06 +00002345 struct address_space *mapping;
2346
Chris Wilson1233e2d2016-10-28 13:58:37 +01002347 lockdep_assert_held(&obj->mm.lock);
Chris Wilsonf1fa4f42017-10-13 21:26:13 +01002348 GEM_BUG_ON(i915_gem_object_has_pages(obj));
Chris Wilson1233e2d2016-10-28 13:58:37 +01002349
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002350 switch (obj->mm.madv) {
Chris Wilson55372522014-03-25 13:23:06 +00002351 case I915_MADV_DONTNEED:
2352 i915_gem_object_truncate(obj);
2353 case __I915_MADV_PURGED:
2354 return;
2355 }
2356
2357 if (obj->base.filp == NULL)
2358 return;
2359
Al Viro93c76a32015-12-04 23:45:44 -05002360 mapping = obj->base.filp->f_mapping,
Chris Wilson55372522014-03-25 13:23:06 +00002361 invalidate_mapping_pages(mapping, 0, (loff_t)-1);
Chris Wilsone5281cc2010-10-28 13:45:36 +01002362}
2363
Chris Wilson5cdf5882010-09-27 15:51:07 +01002364static void
Chris Wilson03ac84f2016-10-28 13:58:36 +01002365i915_gem_object_put_pages_gtt(struct drm_i915_gem_object *obj,
2366 struct sg_table *pages)
Eric Anholt673a3942008-07-30 12:06:12 -07002367{
Dave Gordon85d12252016-05-20 11:54:06 +01002368 struct sgt_iter sgt_iter;
2369 struct page *page;
Daniel Vetter1286ff72012-05-10 15:25:09 +02002370
Chris Wilsone5facdf2016-12-23 14:57:57 +00002371 __i915_gem_object_release_shmem(obj, pages, true);
Eric Anholt856fa192009-03-19 14:10:50 -07002372
Chris Wilson03ac84f2016-10-28 13:58:36 +01002373 i915_gem_gtt_finish_pages(obj, pages);
Imre Deake2273302015-07-09 12:59:05 +03002374
Daniel Vetter6dacfd22011-09-12 21:30:02 +02002375 if (i915_gem_object_needs_bit17_swizzle(obj))
Chris Wilson03ac84f2016-10-28 13:58:36 +01002376 i915_gem_object_save_bit_17_swizzle(obj, pages);
Eric Anholt280b7132009-03-12 16:56:27 -07002377
Chris Wilson03ac84f2016-10-28 13:58:36 +01002378 for_each_sgt_page(page, sgt_iter, pages) {
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002379 if (obj->mm.dirty)
Chris Wilson9da3da62012-06-01 15:20:22 +01002380 set_page_dirty(page);
Chris Wilson3ef94da2009-09-14 16:50:29 +01002381
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002382 if (obj->mm.madv == I915_MADV_WILLNEED)
Chris Wilson9da3da62012-06-01 15:20:22 +01002383 mark_page_accessed(page);
Chris Wilson3ef94da2009-09-14 16:50:29 +01002384
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002385 put_page(page);
Chris Wilson3ef94da2009-09-14 16:50:29 +01002386 }
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002387 obj->mm.dirty = false;
Eric Anholt673a3942008-07-30 12:06:12 -07002388
Chris Wilson03ac84f2016-10-28 13:58:36 +01002389 sg_free_table(pages);
2390 kfree(pages);
Chris Wilson37e680a2012-06-07 15:38:42 +01002391}
2392
Chris Wilson96d77632016-10-28 13:58:33 +01002393static void __i915_gem_object_reset_page_iter(struct drm_i915_gem_object *obj)
2394{
2395 struct radix_tree_iter iter;
Ville Syrjäläc23aa712017-09-01 20:12:51 +03002396 void __rcu **slot;
Chris Wilson96d77632016-10-28 13:58:33 +01002397
Chris Wilsonbea6e982017-10-26 14:00:31 +01002398 rcu_read_lock();
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002399 radix_tree_for_each_slot(slot, &obj->mm.get_page.radix, &iter, 0)
2400 radix_tree_delete(&obj->mm.get_page.radix, iter.index);
Chris Wilsonbea6e982017-10-26 14:00:31 +01002401 rcu_read_unlock();
Chris Wilson96d77632016-10-28 13:58:33 +01002402}
2403
Chris Wilson548625e2016-11-01 12:11:34 +00002404void __i915_gem_object_put_pages(struct drm_i915_gem_object *obj,
2405 enum i915_mm_subclass subclass)
Chris Wilson37e680a2012-06-07 15:38:42 +01002406{
Chris Wilsonf2123812017-10-16 12:40:37 +01002407 struct drm_i915_private *i915 = to_i915(obj->base.dev);
Chris Wilson03ac84f2016-10-28 13:58:36 +01002408 struct sg_table *pages;
Chris Wilson37e680a2012-06-07 15:38:42 +01002409
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002410 if (i915_gem_object_has_pinned_pages(obj))
Chris Wilson03ac84f2016-10-28 13:58:36 +01002411 return;
Chris Wilsona5570172012-09-04 21:02:54 +01002412
Chris Wilson15717de2016-08-04 07:52:26 +01002413 GEM_BUG_ON(obj->bind_count);
Chris Wilsonf1fa4f42017-10-13 21:26:13 +01002414 if (!i915_gem_object_has_pages(obj))
Chris Wilson1233e2d2016-10-28 13:58:37 +01002415 return;
2416
2417 /* May be called by shrinker from within get_pages() (on another bo) */
Chris Wilson548625e2016-11-01 12:11:34 +00002418 mutex_lock_nested(&obj->mm.lock, subclass);
Chris Wilson1233e2d2016-10-28 13:58:37 +01002419 if (unlikely(atomic_read(&obj->mm.pages_pin_count)))
2420 goto unlock;
Ben Widawsky3e123022013-07-31 17:00:04 -07002421
Chris Wilsona2165e32012-12-03 11:49:00 +00002422 /* ->put_pages might need to allocate memory for the bit17 swizzle
2423 * array, hence protect them from being reaped by removing them from gtt
2424 * lists early. */
Chris Wilson03ac84f2016-10-28 13:58:36 +01002425 pages = fetch_and_zero(&obj->mm.pages);
2426 GEM_BUG_ON(!pages);
Chris Wilsona2165e32012-12-03 11:49:00 +00002427
Chris Wilsonf2123812017-10-16 12:40:37 +01002428 spin_lock(&i915->mm.obj_lock);
2429 list_del(&obj->mm.link);
2430 spin_unlock(&i915->mm.obj_lock);
2431
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002432 if (obj->mm.mapping) {
Chris Wilson4b30cb22016-08-18 17:16:42 +01002433 void *ptr;
2434
Chris Wilson0ce81782017-05-17 13:09:59 +01002435 ptr = page_mask_bits(obj->mm.mapping);
Chris Wilson4b30cb22016-08-18 17:16:42 +01002436 if (is_vmalloc_addr(ptr))
2437 vunmap(ptr);
Chris Wilsonfb8621d2016-04-08 12:11:14 +01002438 else
Chris Wilson4b30cb22016-08-18 17:16:42 +01002439 kunmap(kmap_to_page(ptr));
2440
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002441 obj->mm.mapping = NULL;
Chris Wilson0a798eb2016-04-08 12:11:11 +01002442 }
2443
Chris Wilson96d77632016-10-28 13:58:33 +01002444 __i915_gem_object_reset_page_iter(obj);
2445
Chris Wilson4e5462e2017-03-07 13:20:31 +00002446 if (!IS_ERR(pages))
2447 obj->ops->put_pages(obj, pages);
2448
Matthew Aulda5c081662017-10-06 23:18:18 +01002449 obj->mm.page_sizes.phys = obj->mm.page_sizes.sg = 0;
2450
Chris Wilson1233e2d2016-10-28 13:58:37 +01002451unlock:
2452 mutex_unlock(&obj->mm.lock);
Chris Wilson6c085a72012-08-20 11:40:46 +02002453}
2454
Chris Wilson935a2f72017-02-13 17:15:13 +00002455static bool i915_sg_trim(struct sg_table *orig_st)
Tvrtko Ursulin0c40ce12016-11-09 15:13:43 +00002456{
2457 struct sg_table new_st;
2458 struct scatterlist *sg, *new_sg;
2459 unsigned int i;
2460
2461 if (orig_st->nents == orig_st->orig_nents)
Chris Wilson935a2f72017-02-13 17:15:13 +00002462 return false;
Tvrtko Ursulin0c40ce12016-11-09 15:13:43 +00002463
Chris Wilson8bfc478f2016-12-23 14:57:58 +00002464 if (sg_alloc_table(&new_st, orig_st->nents, GFP_KERNEL | __GFP_NOWARN))
Chris Wilson935a2f72017-02-13 17:15:13 +00002465 return false;
Tvrtko Ursulin0c40ce12016-11-09 15:13:43 +00002466
2467 new_sg = new_st.sgl;
2468 for_each_sg(orig_st->sgl, sg, orig_st->nents, i) {
2469 sg_set_page(new_sg, sg_page(sg), sg->length, 0);
2470 /* called before being DMA mapped, no need to copy sg->dma_* */
2471 new_sg = sg_next(new_sg);
2472 }
Chris Wilsonc2dc6cc2016-12-19 12:43:46 +00002473 GEM_BUG_ON(new_sg); /* Should walk exactly nents and hit the end */
Tvrtko Ursulin0c40ce12016-11-09 15:13:43 +00002474
2475 sg_free_table(orig_st);
2476
2477 *orig_st = new_st;
Chris Wilson935a2f72017-02-13 17:15:13 +00002478 return true;
Tvrtko Ursulin0c40ce12016-11-09 15:13:43 +00002479}
2480
Matthew Auldb91b09e2017-10-06 23:18:17 +01002481static int i915_gem_object_get_pages_gtt(struct drm_i915_gem_object *obj)
Eric Anholt673a3942008-07-30 12:06:12 -07002482{
Chris Wilsonfac5e232016-07-04 11:34:36 +01002483 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
Chris Wilsond766ef52016-12-19 12:43:45 +00002484 const unsigned long page_count = obj->base.size / PAGE_SIZE;
2485 unsigned long i;
Eric Anholt673a3942008-07-30 12:06:12 -07002486 struct address_space *mapping;
Chris Wilson9da3da62012-06-01 15:20:22 +01002487 struct sg_table *st;
2488 struct scatterlist *sg;
Dave Gordon85d12252016-05-20 11:54:06 +01002489 struct sgt_iter sgt_iter;
Eric Anholt673a3942008-07-30 12:06:12 -07002490 struct page *page;
Imre Deak90797e62013-02-18 19:28:03 +02002491 unsigned long last_pfn = 0; /* suppress gcc warning */
Tvrtko Ursulin56024522017-08-03 10:14:17 +01002492 unsigned int max_segment = i915_sg_segment_size();
Matthew Auld84e89782017-10-09 12:00:24 +01002493 unsigned int sg_page_sizes;
Chris Wilson4846bf02017-06-09 12:03:46 +01002494 gfp_t noreclaim;
Imre Deake2273302015-07-09 12:59:05 +03002495 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07002496
Chris Wilson6c085a72012-08-20 11:40:46 +02002497 /* Assert that the object is not currently in any GPU domain. As it
2498 * wasn't in the GTT, there shouldn't be any way it could have been in
2499 * a GPU cache
2500 */
Christian Königc0a51fd2018-02-16 13:43:38 +01002501 GEM_BUG_ON(obj->read_domains & I915_GEM_GPU_DOMAINS);
2502 GEM_BUG_ON(obj->write_domain & I915_GEM_GPU_DOMAINS);
Chris Wilson6c085a72012-08-20 11:40:46 +02002503
Chris Wilson9da3da62012-06-01 15:20:22 +01002504 st = kmalloc(sizeof(*st), GFP_KERNEL);
2505 if (st == NULL)
Matthew Auldb91b09e2017-10-06 23:18:17 +01002506 return -ENOMEM;
Eric Anholt673a3942008-07-30 12:06:12 -07002507
Chris Wilsond766ef52016-12-19 12:43:45 +00002508rebuild_st:
Chris Wilson9da3da62012-06-01 15:20:22 +01002509 if (sg_alloc_table(st, page_count, GFP_KERNEL)) {
Chris Wilson9da3da62012-06-01 15:20:22 +01002510 kfree(st);
Matthew Auldb91b09e2017-10-06 23:18:17 +01002511 return -ENOMEM;
Chris Wilson9da3da62012-06-01 15:20:22 +01002512 }
2513
2514 /* Get the list of pages out of our struct file. They'll be pinned
2515 * at this point until we release them.
2516 *
2517 * Fail silently without starting the shrinker
2518 */
Al Viro93c76a32015-12-04 23:45:44 -05002519 mapping = obj->base.filp->f_mapping;
Chris Wilson0f6ab552017-06-09 12:03:48 +01002520 noreclaim = mapping_gfp_constraint(mapping, ~__GFP_RECLAIM);
Chris Wilson4846bf02017-06-09 12:03:46 +01002521 noreclaim |= __GFP_NORETRY | __GFP_NOWARN;
2522
Imre Deak90797e62013-02-18 19:28:03 +02002523 sg = st->sgl;
2524 st->nents = 0;
Matthew Auld84e89782017-10-09 12:00:24 +01002525 sg_page_sizes = 0;
Imre Deak90797e62013-02-18 19:28:03 +02002526 for (i = 0; i < page_count; i++) {
Chris Wilson4846bf02017-06-09 12:03:46 +01002527 const unsigned int shrink[] = {
2528 I915_SHRINK_BOUND | I915_SHRINK_UNBOUND | I915_SHRINK_PURGEABLE,
2529 0,
2530 }, *s = shrink;
2531 gfp_t gfp = noreclaim;
2532
2533 do {
Chris Wilson6c085a72012-08-20 11:40:46 +02002534 page = shmem_read_mapping_page_gfp(mapping, i, gfp);
Chris Wilson4846bf02017-06-09 12:03:46 +01002535 if (likely(!IS_ERR(page)))
2536 break;
2537
2538 if (!*s) {
2539 ret = PTR_ERR(page);
2540 goto err_sg;
2541 }
2542
Chris Wilson912d5722017-09-06 16:19:30 -07002543 i915_gem_shrink(dev_priv, 2 * page_count, NULL, *s++);
Chris Wilson4846bf02017-06-09 12:03:46 +01002544 cond_resched();
Chris Wilson24f8e002017-03-22 11:05:21 +00002545
Chris Wilson6c085a72012-08-20 11:40:46 +02002546 /* We've tried hard to allocate the memory by reaping
2547 * our own buffer, now let the real VM do its job and
2548 * go down in flames if truly OOM.
Chris Wilson24f8e002017-03-22 11:05:21 +00002549 *
2550 * However, since graphics tend to be disposable,
2551 * defer the oom here by reporting the ENOMEM back
2552 * to userspace.
Chris Wilson6c085a72012-08-20 11:40:46 +02002553 */
Chris Wilson4846bf02017-06-09 12:03:46 +01002554 if (!*s) {
2555 /* reclaim and warn, but no oom */
2556 gfp = mapping_gfp_mask(mapping);
Chris Wilsoneaf41802017-06-09 12:03:47 +01002557
2558 /* Our bo are always dirty and so we require
2559 * kswapd to reclaim our pages (direct reclaim
2560 * does not effectively begin pageout of our
2561 * buffers on its own). However, direct reclaim
2562 * only waits for kswapd when under allocation
2563 * congestion. So as a result __GFP_RECLAIM is
2564 * unreliable and fails to actually reclaim our
2565 * dirty pages -- unless you try over and over
2566 * again with !__GFP_NORETRY. However, we still
2567 * want to fail this allocation rather than
2568 * trigger the out-of-memory killer and for
Michal Hockodbb32952017-07-12 14:36:55 -07002569 * this we want __GFP_RETRY_MAYFAIL.
Chris Wilsoneaf41802017-06-09 12:03:47 +01002570 */
Michal Hockodbb32952017-07-12 14:36:55 -07002571 gfp |= __GFP_RETRY_MAYFAIL;
Imre Deake2273302015-07-09 12:59:05 +03002572 }
Chris Wilson4846bf02017-06-09 12:03:46 +01002573 } while (1);
2574
Chris Wilson871dfbd2016-10-11 09:20:21 +01002575 if (!i ||
2576 sg->length >= max_segment ||
2577 page_to_pfn(page) != last_pfn + 1) {
Matthew Aulda5c081662017-10-06 23:18:18 +01002578 if (i) {
Matthew Auld84e89782017-10-09 12:00:24 +01002579 sg_page_sizes |= sg->length;
Imre Deak90797e62013-02-18 19:28:03 +02002580 sg = sg_next(sg);
Matthew Aulda5c081662017-10-06 23:18:18 +01002581 }
Imre Deak90797e62013-02-18 19:28:03 +02002582 st->nents++;
2583 sg_set_page(sg, page, PAGE_SIZE, 0);
2584 } else {
2585 sg->length += PAGE_SIZE;
2586 }
2587 last_pfn = page_to_pfn(page);
Daniel Vetter3bbbe702013-10-07 17:15:45 -03002588
2589 /* Check that the i965g/gm workaround works. */
2590 WARN_ON((gfp & __GFP_DMA32) && (last_pfn >= 0x00100000UL));
Eric Anholt673a3942008-07-30 12:06:12 -07002591 }
Matthew Aulda5c081662017-10-06 23:18:18 +01002592 if (sg) { /* loop terminated early; short sg table */
Matthew Auld84e89782017-10-09 12:00:24 +01002593 sg_page_sizes |= sg->length;
Konrad Rzeszutek Wilk426729d2013-06-24 11:47:48 -04002594 sg_mark_end(sg);
Matthew Aulda5c081662017-10-06 23:18:18 +01002595 }
Chris Wilson74ce6b62012-10-19 15:51:06 +01002596
Tvrtko Ursulin0c40ce12016-11-09 15:13:43 +00002597 /* Trim unused sg entries to avoid wasting memory. */
2598 i915_sg_trim(st);
2599
Chris Wilson03ac84f2016-10-28 13:58:36 +01002600 ret = i915_gem_gtt_prepare_pages(obj, st);
Chris Wilsond766ef52016-12-19 12:43:45 +00002601 if (ret) {
2602 /* DMA remapping failed? One possible cause is that
2603 * it could not reserve enough large entries, asking
2604 * for PAGE_SIZE chunks instead may be helpful.
2605 */
2606 if (max_segment > PAGE_SIZE) {
2607 for_each_sgt_page(page, sgt_iter, st)
2608 put_page(page);
2609 sg_free_table(st);
2610
2611 max_segment = PAGE_SIZE;
2612 goto rebuild_st;
2613 } else {
2614 dev_warn(&dev_priv->drm.pdev->dev,
2615 "Failed to DMA remap %lu pages\n",
2616 page_count);
2617 goto err_pages;
2618 }
2619 }
Imre Deake2273302015-07-09 12:59:05 +03002620
Eric Anholt673a3942008-07-30 12:06:12 -07002621 if (i915_gem_object_needs_bit17_swizzle(obj))
Chris Wilson03ac84f2016-10-28 13:58:36 +01002622 i915_gem_object_do_bit_17_swizzle(obj, st);
Eric Anholt673a3942008-07-30 12:06:12 -07002623
Matthew Auld84e89782017-10-09 12:00:24 +01002624 __i915_gem_object_set_pages(obj, st, sg_page_sizes);
Matthew Auldb91b09e2017-10-06 23:18:17 +01002625
2626 return 0;
Eric Anholt673a3942008-07-30 12:06:12 -07002627
Chris Wilsonb17993b2016-11-14 11:29:30 +00002628err_sg:
Imre Deak90797e62013-02-18 19:28:03 +02002629 sg_mark_end(sg);
Chris Wilsonb17993b2016-11-14 11:29:30 +00002630err_pages:
Dave Gordon85d12252016-05-20 11:54:06 +01002631 for_each_sgt_page(page, sgt_iter, st)
2632 put_page(page);
Chris Wilson9da3da62012-06-01 15:20:22 +01002633 sg_free_table(st);
2634 kfree(st);
Chris Wilson0820baf2014-03-25 13:23:03 +00002635
2636 /* shmemfs first checks if there is enough memory to allocate the page
2637 * and reports ENOSPC should there be insufficient, along with the usual
2638 * ENOMEM for a genuine allocation failure.
2639 *
2640 * We use ENOSPC in our driver to mean that we have run out of aperture
2641 * space and so want to translate the error from shmemfs back to our
2642 * usual understanding of ENOMEM.
2643 */
Imre Deake2273302015-07-09 12:59:05 +03002644 if (ret == -ENOSPC)
2645 ret = -ENOMEM;
2646
Matthew Auldb91b09e2017-10-06 23:18:17 +01002647 return ret;
Chris Wilson03ac84f2016-10-28 13:58:36 +01002648}
2649
2650void __i915_gem_object_set_pages(struct drm_i915_gem_object *obj,
Matthew Aulda5c081662017-10-06 23:18:18 +01002651 struct sg_table *pages,
Matthew Auld84e89782017-10-09 12:00:24 +01002652 unsigned int sg_page_sizes)
Chris Wilson03ac84f2016-10-28 13:58:36 +01002653{
Matthew Aulda5c081662017-10-06 23:18:18 +01002654 struct drm_i915_private *i915 = to_i915(obj->base.dev);
2655 unsigned long supported = INTEL_INFO(i915)->page_sizes;
2656 int i;
2657
Chris Wilson1233e2d2016-10-28 13:58:37 +01002658 lockdep_assert_held(&obj->mm.lock);
Chris Wilson03ac84f2016-10-28 13:58:36 +01002659
2660 obj->mm.get_page.sg_pos = pages->sgl;
2661 obj->mm.get_page.sg_idx = 0;
2662
2663 obj->mm.pages = pages;
Chris Wilson2c3a3f42016-11-04 10:30:01 +00002664
2665 if (i915_gem_object_is_tiled(obj) &&
Chris Wilsonf2123812017-10-16 12:40:37 +01002666 i915->quirks & QUIRK_PIN_SWIZZLED_PAGES) {
Chris Wilson2c3a3f42016-11-04 10:30:01 +00002667 GEM_BUG_ON(obj->mm.quirked);
2668 __i915_gem_object_pin_pages(obj);
2669 obj->mm.quirked = true;
2670 }
Matthew Aulda5c081662017-10-06 23:18:18 +01002671
Matthew Auld84e89782017-10-09 12:00:24 +01002672 GEM_BUG_ON(!sg_page_sizes);
2673 obj->mm.page_sizes.phys = sg_page_sizes;
Matthew Aulda5c081662017-10-06 23:18:18 +01002674
2675 /*
Matthew Auld84e89782017-10-09 12:00:24 +01002676 * Calculate the supported page-sizes which fit into the given
2677 * sg_page_sizes. This will give us the page-sizes which we may be able
2678 * to use opportunistically when later inserting into the GTT. For
2679 * example if phys=2G, then in theory we should be able to use 1G, 2M,
2680 * 64K or 4K pages, although in practice this will depend on a number of
2681 * other factors.
Matthew Aulda5c081662017-10-06 23:18:18 +01002682 */
2683 obj->mm.page_sizes.sg = 0;
2684 for_each_set_bit(i, &supported, ilog2(I915_GTT_MAX_PAGE_SIZE) + 1) {
2685 if (obj->mm.page_sizes.phys & ~0u << i)
2686 obj->mm.page_sizes.sg |= BIT(i);
2687 }
Matthew Aulda5c081662017-10-06 23:18:18 +01002688 GEM_BUG_ON(!HAS_PAGE_SIZES(i915, obj->mm.page_sizes.sg));
Chris Wilsonf2123812017-10-16 12:40:37 +01002689
2690 spin_lock(&i915->mm.obj_lock);
2691 list_add(&obj->mm.link, &i915->mm.unbound_list);
2692 spin_unlock(&i915->mm.obj_lock);
Chris Wilson03ac84f2016-10-28 13:58:36 +01002693}
2694
2695static int ____i915_gem_object_get_pages(struct drm_i915_gem_object *obj)
2696{
Matthew Auldb91b09e2017-10-06 23:18:17 +01002697 int err;
Chris Wilson03ac84f2016-10-28 13:58:36 +01002698
2699 if (unlikely(obj->mm.madv != I915_MADV_WILLNEED)) {
2700 DRM_DEBUG("Attempting to obtain a purgeable object\n");
2701 return -EFAULT;
2702 }
2703
Matthew Auldb91b09e2017-10-06 23:18:17 +01002704 err = obj->ops->get_pages(obj);
Matthew Auldb65a9b92017-12-18 10:38:55 +00002705 GEM_BUG_ON(!err && !i915_gem_object_has_pages(obj));
Chris Wilson03ac84f2016-10-28 13:58:36 +01002706
Matthew Auldb91b09e2017-10-06 23:18:17 +01002707 return err;
Eric Anholt673a3942008-07-30 12:06:12 -07002708}
2709
Chris Wilson37e680a2012-06-07 15:38:42 +01002710/* Ensure that the associated pages are gathered from the backing storage
Chris Wilson1233e2d2016-10-28 13:58:37 +01002711 * and pinned into our object. i915_gem_object_pin_pages() may be called
Chris Wilson37e680a2012-06-07 15:38:42 +01002712 * multiple times before they are released by a single call to
Chris Wilson1233e2d2016-10-28 13:58:37 +01002713 * i915_gem_object_unpin_pages() - once the pages are no longer referenced
Chris Wilson37e680a2012-06-07 15:38:42 +01002714 * either as a result of memory pressure (reaping pages under the shrinker)
2715 * or as the object is itself released.
2716 */
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002717int __i915_gem_object_get_pages(struct drm_i915_gem_object *obj)
Chris Wilson37e680a2012-06-07 15:38:42 +01002718{
Chris Wilson03ac84f2016-10-28 13:58:36 +01002719 int err;
Chris Wilson37e680a2012-06-07 15:38:42 +01002720
Chris Wilson1233e2d2016-10-28 13:58:37 +01002721 err = mutex_lock_interruptible(&obj->mm.lock);
2722 if (err)
2723 return err;
Chris Wilson4c7d62c2016-10-28 13:58:32 +01002724
Chris Wilsonf1fa4f42017-10-13 21:26:13 +01002725 if (unlikely(!i915_gem_object_has_pages(obj))) {
Chris Wilson88c880b2017-09-06 14:52:20 +01002726 GEM_BUG_ON(i915_gem_object_has_pinned_pages(obj));
2727
Chris Wilson2c3a3f42016-11-04 10:30:01 +00002728 err = ____i915_gem_object_get_pages(obj);
2729 if (err)
2730 goto unlock;
2731
2732 smp_mb__before_atomic();
Chris Wilson1233e2d2016-10-28 13:58:37 +01002733 }
Chris Wilson2c3a3f42016-11-04 10:30:01 +00002734 atomic_inc(&obj->mm.pages_pin_count);
Chris Wilson43e28f02013-01-08 10:53:09 +00002735
Chris Wilson1233e2d2016-10-28 13:58:37 +01002736unlock:
2737 mutex_unlock(&obj->mm.lock);
Chris Wilson03ac84f2016-10-28 13:58:36 +01002738 return err;
Eric Anholt673a3942008-07-30 12:06:12 -07002739}
2740
Dave Gordondd6034c2016-05-20 11:54:04 +01002741/* The 'mapping' part of i915_gem_object_pin_map() below */
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002742static void *i915_gem_object_map(const struct drm_i915_gem_object *obj,
2743 enum i915_map_type type)
Dave Gordondd6034c2016-05-20 11:54:04 +01002744{
2745 unsigned long n_pages = obj->base.size >> PAGE_SHIFT;
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002746 struct sg_table *sgt = obj->mm.pages;
Dave Gordon85d12252016-05-20 11:54:06 +01002747 struct sgt_iter sgt_iter;
2748 struct page *page;
Dave Gordonb338fa42016-05-20 11:54:05 +01002749 struct page *stack_pages[32];
2750 struct page **pages = stack_pages;
Dave Gordondd6034c2016-05-20 11:54:04 +01002751 unsigned long i = 0;
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002752 pgprot_t pgprot;
Dave Gordondd6034c2016-05-20 11:54:04 +01002753 void *addr;
2754
2755 /* A single page can always be kmapped */
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002756 if (n_pages == 1 && type == I915_MAP_WB)
Dave Gordondd6034c2016-05-20 11:54:04 +01002757 return kmap(sg_page(sgt->sgl));
2758
Dave Gordonb338fa42016-05-20 11:54:05 +01002759 if (n_pages > ARRAY_SIZE(stack_pages)) {
2760 /* Too big for stack -- allocate temporary array instead */
Michal Hocko0ee931c2017-09-13 16:28:29 -07002761 pages = kvmalloc_array(n_pages, sizeof(*pages), GFP_KERNEL);
Dave Gordonb338fa42016-05-20 11:54:05 +01002762 if (!pages)
2763 return NULL;
2764 }
Dave Gordondd6034c2016-05-20 11:54:04 +01002765
Dave Gordon85d12252016-05-20 11:54:06 +01002766 for_each_sgt_page(page, sgt_iter, sgt)
2767 pages[i++] = page;
Dave Gordondd6034c2016-05-20 11:54:04 +01002768
2769 /* Check that we have the expected number of pages */
2770 GEM_BUG_ON(i != n_pages);
2771
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002772 switch (type) {
Chris Wilsona575c672017-08-28 11:46:31 +01002773 default:
2774 MISSING_CASE(type);
2775 /* fallthrough to use PAGE_KERNEL anyway */
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002776 case I915_MAP_WB:
2777 pgprot = PAGE_KERNEL;
2778 break;
2779 case I915_MAP_WC:
2780 pgprot = pgprot_writecombine(PAGE_KERNEL_IO);
2781 break;
2782 }
2783 addr = vmap(pages, n_pages, 0, pgprot);
Dave Gordondd6034c2016-05-20 11:54:04 +01002784
Dave Gordonb338fa42016-05-20 11:54:05 +01002785 if (pages != stack_pages)
Michal Hocko20981052017-05-17 14:23:12 +02002786 kvfree(pages);
Dave Gordondd6034c2016-05-20 11:54:04 +01002787
2788 return addr;
2789}
2790
2791/* get, pin, and map the pages of the object into kernel space */
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002792void *i915_gem_object_pin_map(struct drm_i915_gem_object *obj,
2793 enum i915_map_type type)
Chris Wilson0a798eb2016-04-08 12:11:11 +01002794{
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002795 enum i915_map_type has_type;
2796 bool pinned;
2797 void *ptr;
Chris Wilson0a798eb2016-04-08 12:11:11 +01002798 int ret;
2799
Tina Zhanga03f3952017-11-14 10:25:13 +00002800 if (unlikely(!i915_gem_object_has_struct_page(obj)))
2801 return ERR_PTR(-ENXIO);
Chris Wilson0a798eb2016-04-08 12:11:11 +01002802
Chris Wilson1233e2d2016-10-28 13:58:37 +01002803 ret = mutex_lock_interruptible(&obj->mm.lock);
Chris Wilson0a798eb2016-04-08 12:11:11 +01002804 if (ret)
2805 return ERR_PTR(ret);
2806
Chris Wilsona575c672017-08-28 11:46:31 +01002807 pinned = !(type & I915_MAP_OVERRIDE);
2808 type &= ~I915_MAP_OVERRIDE;
2809
Chris Wilson1233e2d2016-10-28 13:58:37 +01002810 if (!atomic_inc_not_zero(&obj->mm.pages_pin_count)) {
Chris Wilsonf1fa4f42017-10-13 21:26:13 +01002811 if (unlikely(!i915_gem_object_has_pages(obj))) {
Chris Wilson88c880b2017-09-06 14:52:20 +01002812 GEM_BUG_ON(i915_gem_object_has_pinned_pages(obj));
2813
Chris Wilson2c3a3f42016-11-04 10:30:01 +00002814 ret = ____i915_gem_object_get_pages(obj);
2815 if (ret)
2816 goto err_unlock;
Chris Wilson1233e2d2016-10-28 13:58:37 +01002817
Chris Wilson2c3a3f42016-11-04 10:30:01 +00002818 smp_mb__before_atomic();
2819 }
2820 atomic_inc(&obj->mm.pages_pin_count);
Chris Wilson1233e2d2016-10-28 13:58:37 +01002821 pinned = false;
2822 }
Chris Wilsonf1fa4f42017-10-13 21:26:13 +01002823 GEM_BUG_ON(!i915_gem_object_has_pages(obj));
Chris Wilson0a798eb2016-04-08 12:11:11 +01002824
Chris Wilson0ce81782017-05-17 13:09:59 +01002825 ptr = page_unpack_bits(obj->mm.mapping, &has_type);
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002826 if (ptr && has_type != type) {
2827 if (pinned) {
2828 ret = -EBUSY;
Chris Wilson1233e2d2016-10-28 13:58:37 +01002829 goto err_unpin;
Chris Wilson0a798eb2016-04-08 12:11:11 +01002830 }
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002831
2832 if (is_vmalloc_addr(ptr))
2833 vunmap(ptr);
2834 else
2835 kunmap(kmap_to_page(ptr));
2836
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002837 ptr = obj->mm.mapping = NULL;
Chris Wilson0a798eb2016-04-08 12:11:11 +01002838 }
2839
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002840 if (!ptr) {
2841 ptr = i915_gem_object_map(obj, type);
2842 if (!ptr) {
2843 ret = -ENOMEM;
Chris Wilson1233e2d2016-10-28 13:58:37 +01002844 goto err_unpin;
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002845 }
2846
Chris Wilson0ce81782017-05-17 13:09:59 +01002847 obj->mm.mapping = page_pack_bits(ptr, type);
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002848 }
2849
Chris Wilson1233e2d2016-10-28 13:58:37 +01002850out_unlock:
2851 mutex_unlock(&obj->mm.lock);
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002852 return ptr;
2853
Chris Wilson1233e2d2016-10-28 13:58:37 +01002854err_unpin:
2855 atomic_dec(&obj->mm.pages_pin_count);
2856err_unlock:
2857 ptr = ERR_PTR(ret);
2858 goto out_unlock;
Chris Wilson0a798eb2016-04-08 12:11:11 +01002859}
2860
Chris Wilson7c55e2c2017-03-07 12:03:38 +00002861static int
2862i915_gem_object_pwrite_gtt(struct drm_i915_gem_object *obj,
2863 const struct drm_i915_gem_pwrite *arg)
2864{
2865 struct address_space *mapping = obj->base.filp->f_mapping;
2866 char __user *user_data = u64_to_user_ptr(arg->data_ptr);
2867 u64 remain, offset;
2868 unsigned int pg;
2869
2870 /* Before we instantiate/pin the backing store for our use, we
2871 * can prepopulate the shmemfs filp efficiently using a write into
2872 * the pagecache. We avoid the penalty of instantiating all the
2873 * pages, important if the user is just writing to a few and never
2874 * uses the object on the GPU, and using a direct write into shmemfs
2875 * allows it to avoid the cost of retrieving a page (either swapin
2876 * or clearing-before-use) before it is overwritten.
2877 */
Chris Wilsonf1fa4f42017-10-13 21:26:13 +01002878 if (i915_gem_object_has_pages(obj))
Chris Wilson7c55e2c2017-03-07 12:03:38 +00002879 return -ENODEV;
2880
Chris Wilsona6d65e42017-10-16 21:27:32 +01002881 if (obj->mm.madv != I915_MADV_WILLNEED)
2882 return -EFAULT;
2883
Chris Wilson7c55e2c2017-03-07 12:03:38 +00002884 /* Before the pages are instantiated the object is treated as being
2885 * in the CPU domain. The pages will be clflushed as required before
2886 * use, and we can freely write into the pages directly. If userspace
2887 * races pwrite with any other operation; corruption will ensue -
2888 * that is userspace's prerogative!
2889 */
2890
2891 remain = arg->size;
2892 offset = arg->offset;
2893 pg = offset_in_page(offset);
2894
2895 do {
2896 unsigned int len, unwritten;
2897 struct page *page;
2898 void *data, *vaddr;
2899 int err;
2900
2901 len = PAGE_SIZE - pg;
2902 if (len > remain)
2903 len = remain;
2904
2905 err = pagecache_write_begin(obj->base.filp, mapping,
2906 offset, len, 0,
2907 &page, &data);
2908 if (err < 0)
2909 return err;
2910
2911 vaddr = kmap(page);
2912 unwritten = copy_from_user(vaddr + pg, user_data, len);
2913 kunmap(page);
2914
2915 err = pagecache_write_end(obj->base.filp, mapping,
2916 offset, len, len - unwritten,
2917 page, data);
2918 if (err < 0)
2919 return err;
2920
2921 if (unwritten)
2922 return -EFAULT;
2923
2924 remain -= len;
2925 user_data += len;
2926 offset += len;
2927 pg = 0;
2928 } while (remain);
2929
2930 return 0;
2931}
2932
Mika Kuoppalae5e1fc42016-11-16 17:20:31 +02002933static void i915_gem_context_mark_guilty(struct i915_gem_context *ctx)
Mika Kuoppalaaa60c662013-06-12 15:13:20 +03002934{
Chris Wilson77b25a92017-07-21 13:32:30 +01002935 bool banned;
Mika Kuoppalab083a082016-11-18 15:10:47 +02002936
Chris Wilson77b25a92017-07-21 13:32:30 +01002937 atomic_inc(&ctx->guilty_count);
2938
Chris Wilson24eae082018-02-05 09:22:01 +00002939 banned = false;
2940 if (i915_gem_context_is_bannable(ctx)) {
2941 unsigned int score;
2942
2943 score = atomic_add_return(CONTEXT_SCORE_GUILTY,
2944 &ctx->ban_score);
2945 banned = score >= CONTEXT_SCORE_BAN_THRESHOLD;
2946
2947 DRM_DEBUG_DRIVER("context %s marked guilty (score %d) banned? %s\n",
2948 ctx->name, score, yesno(banned));
2949 }
Chris Wilson77b25a92017-07-21 13:32:30 +01002950 if (!banned)
Mika Kuoppalab083a082016-11-18 15:10:47 +02002951 return;
2952
Chris Wilson77b25a92017-07-21 13:32:30 +01002953 i915_gem_context_set_banned(ctx);
2954 if (!IS_ERR_OR_NULL(ctx->file_priv)) {
2955 atomic_inc(&ctx->file_priv->context_bans);
2956 DRM_DEBUG_DRIVER("client %s has had %d context banned\n",
2957 ctx->name, atomic_read(&ctx->file_priv->context_bans));
2958 }
Mika Kuoppalae5e1fc42016-11-16 17:20:31 +02002959}
2960
2961static void i915_gem_context_mark_innocent(struct i915_gem_context *ctx)
2962{
Chris Wilson77b25a92017-07-21 13:32:30 +01002963 atomic_inc(&ctx->active_count);
Mika Kuoppalaaa60c662013-06-12 15:13:20 +03002964}
2965
Chris Wilsone61e0f52018-02-21 09:56:36 +00002966struct i915_request *
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00002967i915_gem_find_active_request(struct intel_engine_cs *engine)
Chris Wilson9375e442010-09-19 12:21:28 +01002968{
Chris Wilsone61e0f52018-02-21 09:56:36 +00002969 struct i915_request *request, *active = NULL;
Chris Wilson754c9fd2017-02-23 07:44:14 +00002970 unsigned long flags;
Mika Kuoppalaaa60c662013-06-12 15:13:20 +03002971
Chris Wilsoncc7cc532018-05-29 14:29:18 +01002972 /*
2973 * We are called by the error capture, reset and to dump engine
2974 * state at random points in time. In particular, note that neither is
2975 * crucially ordered with an interrupt. After a hang, the GPU is dead
2976 * and we assume that no more writes can happen (we waited long enough
2977 * for all writes that were in transaction to be flushed) - adding an
Chris Wilsonf69a02c2016-07-01 17:23:16 +01002978 * extra delay for a recent interrupt is pointless. Hence, we do
2979 * not need an engine->irq_seqno_barrier() before the seqno reads.
Chris Wilsoncc7cc532018-05-29 14:29:18 +01002980 * At all other times, we must assume the GPU is still running, but
2981 * we only care about the snapshot of this moment.
Chris Wilsonf69a02c2016-07-01 17:23:16 +01002982 */
Chris Wilsona89d1f92018-05-02 17:38:39 +01002983 spin_lock_irqsave(&engine->timeline.lock, flags);
2984 list_for_each_entry(request, &engine->timeline.requests, link) {
Chris Wilsone61e0f52018-02-21 09:56:36 +00002985 if (__i915_request_completed(request, request->global_seqno))
Chris Wilson4db080f2013-12-04 11:37:09 +00002986 continue;
Mika Kuoppalaaa60c662013-06-12 15:13:20 +03002987
Chris Wilson754c9fd2017-02-23 07:44:14 +00002988 active = request;
2989 break;
2990 }
Chris Wilsona89d1f92018-05-02 17:38:39 +01002991 spin_unlock_irqrestore(&engine->timeline.lock, flags);
Chris Wilson754c9fd2017-02-23 07:44:14 +00002992
2993 return active;
Mika Kuoppalab6b0fac2014-01-30 19:04:43 +02002994}
2995
Michel Thierrya1ef70e2017-06-20 10:57:47 +01002996/*
2997 * Ensure irq handler finishes, and not run again.
2998 * Also return the active request so that we only search for it once.
2999 */
Chris Wilsone61e0f52018-02-21 09:56:36 +00003000struct i915_request *
Michel Thierrya1ef70e2017-06-20 10:57:47 +01003001i915_gem_reset_prepare_engine(struct intel_engine_cs *engine)
3002{
Chris Wilson5adfb772018-05-16 19:33:51 +01003003 struct i915_request *request;
Michel Thierrya1ef70e2017-06-20 10:57:47 +01003004
Chris Wilson1749d902017-10-09 12:02:59 +01003005 /*
3006 * During the reset sequence, we must prevent the engine from
3007 * entering RC6. As the context state is undefined until we restart
3008 * the engine, if it does enter RC6 during the reset, the state
3009 * written to the powercontext is undefined and so we may lose
3010 * GPU state upon resume, i.e. fail to restart after a reset.
3011 */
3012 intel_uncore_forcewake_get(engine->i915, FORCEWAKE_ALL);
3013
Chris Wilson5adfb772018-05-16 19:33:51 +01003014 request = engine->reset.prepare(engine);
Chris Wilsond1d1ebf42017-07-21 13:32:33 +01003015 if (request && request->fence.error == -EIO)
3016 request = ERR_PTR(-EIO); /* Previous reset failed! */
Michel Thierrya1ef70e2017-06-20 10:57:47 +01003017
3018 return request;
3019}
3020
Chris Wilson0e178ae2017-01-17 17:59:06 +02003021int i915_gem_reset_prepare(struct drm_i915_private *dev_priv)
Chris Wilson4c965542017-01-17 17:59:01 +02003022{
3023 struct intel_engine_cs *engine;
Chris Wilsone61e0f52018-02-21 09:56:36 +00003024 struct i915_request *request;
Chris Wilson4c965542017-01-17 17:59:01 +02003025 enum intel_engine_id id;
Chris Wilson0e178ae2017-01-17 17:59:06 +02003026 int err = 0;
Chris Wilson4c965542017-01-17 17:59:01 +02003027
Chris Wilson0e178ae2017-01-17 17:59:06 +02003028 for_each_engine(engine, dev_priv, id) {
Michel Thierrya1ef70e2017-06-20 10:57:47 +01003029 request = i915_gem_reset_prepare_engine(engine);
3030 if (IS_ERR(request)) {
3031 err = PTR_ERR(request);
3032 continue;
Chris Wilson0e178ae2017-01-17 17:59:06 +02003033 }
Michel Thierryc64992e2017-06-20 10:57:44 +01003034
3035 engine->hangcheck.active_request = request;
Chris Wilson0e178ae2017-01-17 17:59:06 +02003036 }
3037
Chris Wilson4c965542017-01-17 17:59:01 +02003038 i915_gem_revoke_fences(dev_priv);
Michal Wajdeczkoc37d5722018-03-12 13:03:07 +00003039 intel_uc_sanitize(dev_priv);
Chris Wilson0e178ae2017-01-17 17:59:06 +02003040
3041 return err;
Chris Wilson4c965542017-01-17 17:59:01 +02003042}
3043
Chris Wilsone61e0f52018-02-21 09:56:36 +00003044static void skip_request(struct i915_request *request)
Mika Kuoppalab6b0fac2014-01-30 19:04:43 +02003045{
Chris Wilson821ed7d2016-09-09 14:11:53 +01003046 void *vaddr = request->ring->vaddr;
3047 u32 head;
Mika Kuoppalab6b0fac2014-01-30 19:04:43 +02003048
Chris Wilson821ed7d2016-09-09 14:11:53 +01003049 /* As this request likely depends on state from the lost
3050 * context, clear out all the user operations leaving the
3051 * breadcrumb at the end (so we get the fence notifications).
3052 */
3053 head = request->head;
3054 if (request->postfix < head) {
3055 memset(vaddr + head, 0, request->ring->size - head);
3056 head = 0;
3057 }
3058 memset(vaddr + head, 0, request->postfix - head);
Chris Wilsonc0d5f322017-01-10 17:22:43 +00003059
3060 dma_fence_set_error(&request->fence, -EIO);
Chris Wilson4db080f2013-12-04 11:37:09 +00003061}
3062
Chris Wilsone61e0f52018-02-21 09:56:36 +00003063static void engine_skip_context(struct i915_request *request)
Mika Kuoppala36193ac2017-01-17 17:59:02 +02003064{
3065 struct intel_engine_cs *engine = request->engine;
Chris Wilson4e0d64d2018-05-17 22:26:30 +01003066 struct i915_gem_context *hung_ctx = request->gem_context;
Chris Wilsona89d1f92018-05-02 17:38:39 +01003067 struct i915_timeline *timeline = request->timeline;
Mika Kuoppala36193ac2017-01-17 17:59:02 +02003068 unsigned long flags;
3069
Chris Wilsona89d1f92018-05-02 17:38:39 +01003070 GEM_BUG_ON(timeline == &engine->timeline);
Mika Kuoppala36193ac2017-01-17 17:59:02 +02003071
Chris Wilsona89d1f92018-05-02 17:38:39 +01003072 spin_lock_irqsave(&engine->timeline.lock, flags);
Chris Wilson0c591a42018-05-12 09:49:57 +01003073 spin_lock_nested(&timeline->lock, SINGLE_DEPTH_NESTING);
Mika Kuoppala36193ac2017-01-17 17:59:02 +02003074
Chris Wilsona89d1f92018-05-02 17:38:39 +01003075 list_for_each_entry_continue(request, &engine->timeline.requests, link)
Chris Wilson4e0d64d2018-05-17 22:26:30 +01003076 if (request->gem_context == hung_ctx)
Mika Kuoppala36193ac2017-01-17 17:59:02 +02003077 skip_request(request);
3078
3079 list_for_each_entry(request, &timeline->requests, link)
3080 skip_request(request);
3081
3082 spin_unlock(&timeline->lock);
Chris Wilsona89d1f92018-05-02 17:38:39 +01003083 spin_unlock_irqrestore(&engine->timeline.lock, flags);
Mika Kuoppala36193ac2017-01-17 17:59:02 +02003084}
3085
Chris Wilsond1d1ebf42017-07-21 13:32:33 +01003086/* Returns the request if it was guilty of the hang */
Chris Wilsone61e0f52018-02-21 09:56:36 +00003087static struct i915_request *
Chris Wilsond1d1ebf42017-07-21 13:32:33 +01003088i915_gem_reset_request(struct intel_engine_cs *engine,
Chris Wilsonbba08692018-04-06 23:03:53 +01003089 struct i915_request *request,
3090 bool stalled)
Mika Kuoppala61da5362017-01-17 17:59:05 +02003091{
Mika Kuoppala71895a02017-01-17 17:59:07 +02003092 /* The guilty request will get skipped on a hung engine.
3093 *
3094 * Users of client default contexts do not rely on logical
3095 * state preserved between batches so it is safe to execute
3096 * queued requests following the hang. Non default contexts
3097 * rely on preserved state, so skipping a batch loses the
3098 * evolution of the state and it needs to be considered corrupted.
3099 * Executing more queued batches on top of corrupted state is
3100 * risky. But we take the risk by trying to advance through
3101 * the queued requests in order to make the client behaviour
3102 * more predictable around resets, by not throwing away random
3103 * amount of batches it has prepared for execution. Sophisticated
3104 * clients can use gem_reset_stats_ioctl and dma fence status
3105 * (exported via sync_file info ioctl on explicit fences) to observe
3106 * when it loses the context state and should rebuild accordingly.
3107 *
3108 * The context ban, and ultimately the client ban, mechanism are safety
3109 * valves if client submission ends up resulting in nothing more than
3110 * subsequent hangs.
3111 */
3112
Chris Wilsonbba08692018-04-06 23:03:53 +01003113 if (i915_request_completed(request)) {
3114 GEM_TRACE("%s pardoned global=%d (fence %llx:%d), current %d\n",
3115 engine->name, request->global_seqno,
3116 request->fence.context, request->fence.seqno,
3117 intel_engine_get_seqno(engine));
3118 stalled = false;
3119 }
3120
3121 if (stalled) {
Chris Wilson4e0d64d2018-05-17 22:26:30 +01003122 i915_gem_context_mark_guilty(request->gem_context);
Mika Kuoppala61da5362017-01-17 17:59:05 +02003123 skip_request(request);
Chris Wilsond1d1ebf42017-07-21 13:32:33 +01003124
3125 /* If this context is now banned, skip all pending requests. */
Chris Wilson4e0d64d2018-05-17 22:26:30 +01003126 if (i915_gem_context_is_banned(request->gem_context))
Chris Wilsond1d1ebf42017-07-21 13:32:33 +01003127 engine_skip_context(request);
Mika Kuoppala61da5362017-01-17 17:59:05 +02003128 } else {
Chris Wilsond1d1ebf42017-07-21 13:32:33 +01003129 /*
3130 * Since this is not the hung engine, it may have advanced
3131 * since the hang declaration. Double check by refinding
3132 * the active request at the time of the reset.
3133 */
3134 request = i915_gem_find_active_request(engine);
3135 if (request) {
Chris Wilson4e0d64d2018-05-17 22:26:30 +01003136 i915_gem_context_mark_innocent(request->gem_context);
Chris Wilsond1d1ebf42017-07-21 13:32:33 +01003137 dma_fence_set_error(&request->fence, -EAGAIN);
3138
3139 /* Rewind the engine to replay the incomplete rq */
Chris Wilsona89d1f92018-05-02 17:38:39 +01003140 spin_lock_irq(&engine->timeline.lock);
Chris Wilsond1d1ebf42017-07-21 13:32:33 +01003141 request = list_prev_entry(request, link);
Chris Wilsona89d1f92018-05-02 17:38:39 +01003142 if (&request->link == &engine->timeline.requests)
Chris Wilsond1d1ebf42017-07-21 13:32:33 +01003143 request = NULL;
Chris Wilsona89d1f92018-05-02 17:38:39 +01003144 spin_unlock_irq(&engine->timeline.lock);
Chris Wilsond1d1ebf42017-07-21 13:32:33 +01003145 }
Mika Kuoppala61da5362017-01-17 17:59:05 +02003146 }
3147
Chris Wilsond1d1ebf42017-07-21 13:32:33 +01003148 return request;
Mika Kuoppala61da5362017-01-17 17:59:05 +02003149}
3150
Michel Thierrya1ef70e2017-06-20 10:57:47 +01003151void i915_gem_reset_engine(struct intel_engine_cs *engine,
Chris Wilsonbba08692018-04-06 23:03:53 +01003152 struct i915_request *request,
3153 bool stalled)
Chris Wilson4db080f2013-12-04 11:37:09 +00003154{
Chris Wilsonfcb1de52017-12-19 09:01:10 +00003155 /*
3156 * Make sure this write is visible before we re-enable the interrupt
3157 * handlers on another CPU, as tasklet_enable() resolves to just
3158 * a compiler barrier which is insufficient for our purpose here.
3159 */
3160 smp_store_mb(engine->irq_posted, 0);
Chris Wilsoned454f22017-07-21 13:32:29 +01003161
Chris Wilsond1d1ebf42017-07-21 13:32:33 +01003162 if (request)
Chris Wilsonbba08692018-04-06 23:03:53 +01003163 request = i915_gem_reset_request(engine, request, stalled);
Chris Wilsond1d1ebf42017-07-21 13:32:33 +01003164
Chris Wilson821ed7d2016-09-09 14:11:53 +01003165 /* Setup the CS to resume from the breadcrumb of the hung request */
Chris Wilson5adfb772018-05-16 19:33:51 +01003166 engine->reset.reset(engine, request);
Chris Wilson821ed7d2016-09-09 14:11:53 +01003167}
3168
Chris Wilsond0667e92018-04-06 23:03:54 +01003169void i915_gem_reset(struct drm_i915_private *dev_priv,
3170 unsigned int stalled_mask)
Chris Wilson821ed7d2016-09-09 14:11:53 +01003171{
3172 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05303173 enum intel_engine_id id;
Chris Wilson821ed7d2016-09-09 14:11:53 +01003174
Chris Wilson4c7d62c2016-10-28 13:58:32 +01003175 lockdep_assert_held(&dev_priv->drm.struct_mutex);
3176
Chris Wilsone61e0f52018-02-21 09:56:36 +00003177 i915_retire_requests(dev_priv);
Chris Wilson821ed7d2016-09-09 14:11:53 +01003178
Chris Wilson2ae55732017-02-12 17:20:02 +00003179 for_each_engine(engine, dev_priv, id) {
Chris Wilson1fc44d92018-05-17 22:26:32 +01003180 struct intel_context *ce;
Chris Wilson2ae55732017-02-12 17:20:02 +00003181
Chris Wilsonbba08692018-04-06 23:03:53 +01003182 i915_gem_reset_engine(engine,
3183 engine->hangcheck.active_request,
Chris Wilsond0667e92018-04-06 23:03:54 +01003184 stalled_mask & ENGINE_MASK(id));
Chris Wilson1fc44d92018-05-17 22:26:32 +01003185 ce = fetch_and_zero(&engine->last_retired_context);
3186 if (ce)
3187 intel_context_unpin(ce);
Chris Wilson7b6da812017-12-16 00:03:34 +00003188
3189 /*
3190 * Ostensibily, we always want a context loaded for powersaving,
3191 * so if the engine is idle after the reset, send a request
3192 * to load our scratch kernel_context.
3193 *
3194 * More mysteriously, if we leave the engine idle after a reset,
3195 * the next userspace batch may hang, with what appears to be
3196 * an incoherent read by the CS (presumably stale TLB). An
3197 * empty request appears sufficient to paper over the glitch.
3198 */
Chris Wilson01b8fdc2018-02-05 15:24:31 +00003199 if (intel_engine_is_idle(engine)) {
Chris Wilsone61e0f52018-02-21 09:56:36 +00003200 struct i915_request *rq;
Chris Wilson7b6da812017-12-16 00:03:34 +00003201
Chris Wilsone61e0f52018-02-21 09:56:36 +00003202 rq = i915_request_alloc(engine,
3203 dev_priv->kernel_context);
Chris Wilson7b6da812017-12-16 00:03:34 +00003204 if (!IS_ERR(rq))
Chris Wilsone61e0f52018-02-21 09:56:36 +00003205 __i915_request_add(rq, false);
Chris Wilson7b6da812017-12-16 00:03:34 +00003206 }
Chris Wilson2ae55732017-02-12 17:20:02 +00003207 }
Chris Wilson821ed7d2016-09-09 14:11:53 +01003208
Tvrtko Ursulin4362f4f2016-11-16 08:55:33 +00003209 i915_gem_restore_fences(dev_priv);
Chris Wilson821ed7d2016-09-09 14:11:53 +01003210}
3211
Michel Thierrya1ef70e2017-06-20 10:57:47 +01003212void i915_gem_reset_finish_engine(struct intel_engine_cs *engine)
3213{
Chris Wilson5adfb772018-05-16 19:33:51 +01003214 engine->reset.finish(engine);
3215
Chris Wilson1749d902017-10-09 12:02:59 +01003216 intel_uncore_forcewake_put(engine->i915, FORCEWAKE_ALL);
Michel Thierrya1ef70e2017-06-20 10:57:47 +01003217}
3218
Chris Wilsond8027092017-02-08 14:30:32 +00003219void i915_gem_reset_finish(struct drm_i915_private *dev_priv)
3220{
Chris Wilson1f7b8472017-02-08 14:30:33 +00003221 struct intel_engine_cs *engine;
3222 enum intel_engine_id id;
3223
Chris Wilsond8027092017-02-08 14:30:32 +00003224 lockdep_assert_held(&dev_priv->drm.struct_mutex);
Chris Wilson1f7b8472017-02-08 14:30:33 +00003225
Chris Wilsonfe3288b2017-02-12 17:20:01 +00003226 for_each_engine(engine, dev_priv, id) {
Michel Thierryc64992e2017-06-20 10:57:44 +01003227 engine->hangcheck.active_request = NULL;
Michel Thierrya1ef70e2017-06-20 10:57:47 +01003228 i915_gem_reset_finish_engine(engine);
Chris Wilsonfe3288b2017-02-12 17:20:01 +00003229 }
Chris Wilsond8027092017-02-08 14:30:32 +00003230}
3231
Chris Wilsone61e0f52018-02-21 09:56:36 +00003232static void nop_submit_request(struct i915_request *request)
Chris Wilson821ed7d2016-09-09 14:11:53 +01003233{
Chris Wilsond9b13c42018-03-15 13:14:50 +00003234 GEM_TRACE("%s fence %llx:%d -> -EIO\n",
3235 request->engine->name,
3236 request->fence.context, request->fence.seqno);
Daniel Vetteraf7a8ff2017-10-11 11:10:19 +02003237 dma_fence_set_error(&request->fence, -EIO);
3238
Chris Wilsone61e0f52018-02-21 09:56:36 +00003239 i915_request_submit(request);
Daniel Vetteraf7a8ff2017-10-11 11:10:19 +02003240}
3241
Chris Wilsone61e0f52018-02-21 09:56:36 +00003242static void nop_complete_submit_request(struct i915_request *request)
Daniel Vetteraf7a8ff2017-10-11 11:10:19 +02003243{
Chris Wilson8d550822017-10-06 12:56:17 +01003244 unsigned long flags;
3245
Chris Wilsond9b13c42018-03-15 13:14:50 +00003246 GEM_TRACE("%s fence %llx:%d -> -EIO\n",
3247 request->engine->name,
3248 request->fence.context, request->fence.seqno);
Chris Wilson3cd94422017-01-10 17:22:45 +00003249 dma_fence_set_error(&request->fence, -EIO);
Chris Wilson8d550822017-10-06 12:56:17 +01003250
Chris Wilsona89d1f92018-05-02 17:38:39 +01003251 spin_lock_irqsave(&request->engine->timeline.lock, flags);
Chris Wilsone61e0f52018-02-21 09:56:36 +00003252 __i915_request_submit(request);
Chris Wilson3dcf93f72016-11-22 14:41:20 +00003253 intel_engine_init_global_seqno(request->engine, request->global_seqno);
Chris Wilsona89d1f92018-05-02 17:38:39 +01003254 spin_unlock_irqrestore(&request->engine->timeline.lock, flags);
Chris Wilson821ed7d2016-09-09 14:11:53 +01003255}
3256
Daniel Vetteraf7a8ff2017-10-11 11:10:19 +02003257void i915_gem_set_wedged(struct drm_i915_private *i915)
Chris Wilson821ed7d2016-09-09 14:11:53 +01003258{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00003259 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05303260 enum intel_engine_id id;
Eric Anholt673a3942008-07-30 12:06:12 -07003261
Chris Wilsond9b13c42018-03-15 13:14:50 +00003262 GEM_TRACE("start\n");
3263
Chris Wilson7f961d72018-04-26 11:32:19 +01003264 if (GEM_SHOW_DEBUG()) {
Chris Wilson559e0402018-02-05 09:21:59 +00003265 struct drm_printer p = drm_debug_printer(__func__);
3266
3267 for_each_engine(engine, i915, id)
3268 intel_engine_dump(engine, &p, "%s\n", engine->name);
3269 }
3270
Chris Wilson0d73e7a2018-02-07 15:13:50 +00003271 set_bit(I915_WEDGED, &i915->gpu_error.flags);
3272 smp_mb__after_atomic();
3273
Daniel Vetteraf7a8ff2017-10-11 11:10:19 +02003274 /*
3275 * First, stop submission to hw, but do not yet complete requests by
3276 * rolling the global seqno forward (since this would complete requests
3277 * for which we haven't set the fence error to EIO yet).
3278 */
Chris Wilson963ddd62018-03-02 11:33:24 +00003279 for_each_engine(engine, i915, id) {
3280 i915_gem_reset_prepare_engine(engine);
Chris Wilson47650db2018-03-07 13:42:25 +00003281
Daniel Vetteraf7a8ff2017-10-11 11:10:19 +02003282 engine->submit_request = nop_submit_request;
Chris Wilson47650db2018-03-07 13:42:25 +00003283 engine->schedule = NULL;
Chris Wilson963ddd62018-03-02 11:33:24 +00003284 }
Chris Wilson47650db2018-03-07 13:42:25 +00003285 i915->caps.scheduler = 0;
Daniel Vetteraf7a8ff2017-10-11 11:10:19 +02003286
Chris Wilsonac697ae2018-03-15 15:10:15 +00003287 /* Even if the GPU reset fails, it should still stop the engines */
3288 intel_gpu_reset(i915, ALL_ENGINES);
3289
Daniel Vetteraf7a8ff2017-10-11 11:10:19 +02003290 /*
3291 * Make sure no one is running the old callback before we proceed with
3292 * cancelling requests and resetting the completion tracking. Otherwise
3293 * we might submit a request to the hardware which never completes.
3294 */
3295 synchronize_rcu();
3296
3297 for_each_engine(engine, i915, id) {
3298 /* Mark all executing requests as skipped */
3299 engine->cancel_requests(engine);
3300
3301 /*
3302 * Only once we've force-cancelled all in-flight requests can we
3303 * start to complete all requests.
3304 */
3305 engine->submit_request = nop_complete_submit_request;
3306 }
3307
3308 /*
3309 * Make sure no request can slip through without getting completed by
3310 * either this call here to intel_engine_init_global_seqno, or the one
3311 * in nop_complete_submit_request.
3312 */
3313 synchronize_rcu();
3314
3315 for_each_engine(engine, i915, id) {
3316 unsigned long flags;
3317
Chris Wilson0d73e7a2018-02-07 15:13:50 +00003318 /*
3319 * Mark all pending requests as complete so that any concurrent
Daniel Vetteraf7a8ff2017-10-11 11:10:19 +02003320 * (lockless) lookup doesn't try and wait upon the request as we
3321 * reset it.
3322 */
Chris Wilsona89d1f92018-05-02 17:38:39 +01003323 spin_lock_irqsave(&engine->timeline.lock, flags);
Daniel Vetteraf7a8ff2017-10-11 11:10:19 +02003324 intel_engine_init_global_seqno(engine,
3325 intel_engine_last_submit(engine));
Chris Wilsona89d1f92018-05-02 17:38:39 +01003326 spin_unlock_irqrestore(&engine->timeline.lock, flags);
Chris Wilson963ddd62018-03-02 11:33:24 +00003327
3328 i915_gem_reset_finish_engine(engine);
Daniel Vetteraf7a8ff2017-10-11 11:10:19 +02003329 }
Chris Wilson20e49332016-11-22 14:41:21 +00003330
Chris Wilsond9b13c42018-03-15 13:14:50 +00003331 GEM_TRACE("end\n");
3332
Chris Wilson3d7adbb2017-07-21 13:32:27 +01003333 wake_up_all(&i915->gpu_error.reset_queue);
Eric Anholt673a3942008-07-30 12:06:12 -07003334}
3335
Chris Wilson2e8f9d32017-03-16 17:13:04 +00003336bool i915_gem_unset_wedged(struct drm_i915_private *i915)
3337{
Chris Wilsona89d1f92018-05-02 17:38:39 +01003338 struct i915_timeline *tl;
Chris Wilson2e8f9d32017-03-16 17:13:04 +00003339
3340 lockdep_assert_held(&i915->drm.struct_mutex);
3341 if (!test_bit(I915_WEDGED, &i915->gpu_error.flags))
3342 return true;
3343
Chris Wilsond9b13c42018-03-15 13:14:50 +00003344 GEM_TRACE("start\n");
3345
Chris Wilson2d4ecac2018-03-07 13:42:21 +00003346 /*
3347 * Before unwedging, make sure that all pending operations
Chris Wilson2e8f9d32017-03-16 17:13:04 +00003348 * are flushed and errored out - we may have requests waiting upon
3349 * third party fences. We marked all inflight requests as EIO, and
3350 * every execbuf since returned EIO, for consistency we want all
3351 * the currently pending requests to also be marked as EIO, which
3352 * is done inside our nop_submit_request - and so we must wait.
3353 *
3354 * No more can be submitted until we reset the wedged bit.
3355 */
3356 list_for_each_entry(tl, &i915->gt.timelines, link) {
Chris Wilsona89d1f92018-05-02 17:38:39 +01003357 struct i915_request *rq;
Chris Wilson2e8f9d32017-03-16 17:13:04 +00003358
Chris Wilsona89d1f92018-05-02 17:38:39 +01003359 rq = i915_gem_active_peek(&tl->last_request,
3360 &i915->drm.struct_mutex);
3361 if (!rq)
3362 continue;
Chris Wilson2e8f9d32017-03-16 17:13:04 +00003363
Chris Wilsona89d1f92018-05-02 17:38:39 +01003364 /*
3365 * We can't use our normal waiter as we want to
3366 * avoid recursively trying to handle the current
3367 * reset. The basic dma_fence_default_wait() installs
3368 * a callback for dma_fence_signal(), which is
3369 * triggered by our nop handler (indirectly, the
3370 * callback enables the signaler thread which is
3371 * woken by the nop_submit_request() advancing the seqno
3372 * and when the seqno passes the fence, the signaler
3373 * then signals the fence waking us up).
3374 */
3375 if (dma_fence_default_wait(&rq->fence, true,
3376 MAX_SCHEDULE_TIMEOUT) < 0)
3377 return false;
Chris Wilson2e8f9d32017-03-16 17:13:04 +00003378 }
Chris Wilson2d4ecac2018-03-07 13:42:21 +00003379 i915_retire_requests(i915);
3380 GEM_BUG_ON(i915->gt.active_requests);
Chris Wilson2e8f9d32017-03-16 17:13:04 +00003381
Chris Wilson2d4ecac2018-03-07 13:42:21 +00003382 /*
3383 * Undo nop_submit_request. We prevent all new i915 requests from
Chris Wilson2e8f9d32017-03-16 17:13:04 +00003384 * being queued (by disallowing execbuf whilst wedged) so having
3385 * waited for all active requests above, we know the system is idle
3386 * and do not have to worry about a thread being inside
3387 * engine->submit_request() as we swap over. So unlike installing
3388 * the nop_submit_request on reset, we can do this from normal
3389 * context and do not require stop_machine().
3390 */
3391 intel_engines_reset_default_submission(i915);
Chris Wilson36703e72017-06-22 11:56:25 +01003392 i915_gem_contexts_lost(i915);
Chris Wilson2e8f9d32017-03-16 17:13:04 +00003393
Chris Wilsond9b13c42018-03-15 13:14:50 +00003394 GEM_TRACE("end\n");
3395
Chris Wilson2e8f9d32017-03-16 17:13:04 +00003396 smp_mb__before_atomic(); /* complete takeover before enabling execbuf */
3397 clear_bit(I915_WEDGED, &i915->gpu_error.flags);
3398
3399 return true;
3400}
3401
Daniel Vetter75ef9da2010-08-21 00:25:16 +02003402static void
Eric Anholt673a3942008-07-30 12:06:12 -07003403i915_gem_retire_work_handler(struct work_struct *work)
3404{
Chris Wilsonb29c19b2013-09-25 17:34:56 +01003405 struct drm_i915_private *dev_priv =
Chris Wilson67d97da2016-07-04 08:08:31 +01003406 container_of(work, typeof(*dev_priv), gt.retire_work.work);
Chris Wilson91c8a322016-07-05 10:40:23 +01003407 struct drm_device *dev = &dev_priv->drm;
Eric Anholt673a3942008-07-30 12:06:12 -07003408
Chris Wilson891b48c2010-09-29 12:26:37 +01003409 /* Come back later if the device is busy... */
Chris Wilsonb29c19b2013-09-25 17:34:56 +01003410 if (mutex_trylock(&dev->struct_mutex)) {
Chris Wilsone61e0f52018-02-21 09:56:36 +00003411 i915_retire_requests(dev_priv);
Chris Wilsonb29c19b2013-09-25 17:34:56 +01003412 mutex_unlock(&dev->struct_mutex);
3413 }
Chris Wilson67d97da2016-07-04 08:08:31 +01003414
Chris Wilson88923042018-01-29 14:41:04 +00003415 /*
3416 * Keep the retire handler running until we are finally idle.
Chris Wilson67d97da2016-07-04 08:08:31 +01003417 * We do not need to do this test under locking as in the worst-case
3418 * we queue the retire worker once too often.
3419 */
Chris Wilson88923042018-01-29 14:41:04 +00003420 if (READ_ONCE(dev_priv->gt.awake))
Chris Wilson67d97da2016-07-04 08:08:31 +01003421 queue_delayed_work(dev_priv->wq,
3422 &dev_priv->gt.retire_work,
Chris Wilsonbcb45082012-10-05 17:02:57 +01003423 round_jiffies_up_relative(HZ));
Chris Wilsonb29c19b2013-09-25 17:34:56 +01003424}
Chris Wilson891b48c2010-09-29 12:26:37 +01003425
Chris Wilson84a10742018-01-24 11:36:08 +00003426static void shrink_caches(struct drm_i915_private *i915)
3427{
3428 /*
3429 * kmem_cache_shrink() discards empty slabs and reorders partially
3430 * filled slabs to prioritise allocating from the mostly full slabs,
3431 * with the aim of reducing fragmentation.
3432 */
3433 kmem_cache_shrink(i915->priorities);
3434 kmem_cache_shrink(i915->dependencies);
3435 kmem_cache_shrink(i915->requests);
3436 kmem_cache_shrink(i915->luts);
3437 kmem_cache_shrink(i915->vmas);
3438 kmem_cache_shrink(i915->objects);
3439}
3440
3441struct sleep_rcu_work {
3442 union {
3443 struct rcu_head rcu;
3444 struct work_struct work;
3445 };
3446 struct drm_i915_private *i915;
3447 unsigned int epoch;
3448};
3449
3450static inline bool
3451same_epoch(struct drm_i915_private *i915, unsigned int epoch)
3452{
3453 /*
3454 * There is a small chance that the epoch wrapped since we started
3455 * sleeping. If we assume that epoch is at least a u32, then it will
3456 * take at least 2^32 * 100ms for it to wrap, or about 326 years.
3457 */
3458 return epoch == READ_ONCE(i915->gt.epoch);
3459}
3460
3461static void __sleep_work(struct work_struct *work)
3462{
3463 struct sleep_rcu_work *s = container_of(work, typeof(*s), work);
3464 struct drm_i915_private *i915 = s->i915;
3465 unsigned int epoch = s->epoch;
3466
3467 kfree(s);
3468 if (same_epoch(i915, epoch))
3469 shrink_caches(i915);
3470}
3471
3472static void __sleep_rcu(struct rcu_head *rcu)
3473{
3474 struct sleep_rcu_work *s = container_of(rcu, typeof(*s), rcu);
3475 struct drm_i915_private *i915 = s->i915;
3476
3477 if (same_epoch(i915, s->epoch)) {
3478 INIT_WORK(&s->work, __sleep_work);
3479 queue_work(i915->wq, &s->work);
3480 } else {
3481 kfree(s);
3482 }
3483}
3484
Chris Wilson5427f202017-10-23 22:32:34 +01003485static inline bool
3486new_requests_since_last_retire(const struct drm_i915_private *i915)
3487{
3488 return (READ_ONCE(i915->gt.active_requests) ||
3489 work_pending(&i915->gt.idle_work.work));
3490}
3491
Chris Wilson1934f5de2018-05-31 23:40:57 +01003492static void assert_kernel_context_is_current(struct drm_i915_private *i915)
3493{
3494 struct intel_engine_cs *engine;
3495 enum intel_engine_id id;
3496
3497 if (i915_terminally_wedged(&i915->gpu_error))
3498 return;
3499
3500 GEM_BUG_ON(i915->gt.active_requests);
3501 for_each_engine(engine, i915, id) {
3502 GEM_BUG_ON(__i915_gem_active_peek(&engine->timeline.last_request));
3503 GEM_BUG_ON(engine->last_retired_context !=
3504 to_intel_context(i915->kernel_context, engine));
3505 }
3506}
3507
Chris Wilsonb29c19b2013-09-25 17:34:56 +01003508static void
3509i915_gem_idle_work_handler(struct work_struct *work)
3510{
3511 struct drm_i915_private *dev_priv =
Chris Wilson67d97da2016-07-04 08:08:31 +01003512 container_of(work, typeof(*dev_priv), gt.idle_work.work);
Chris Wilson84a10742018-01-24 11:36:08 +00003513 unsigned int epoch = I915_EPOCH_INVALID;
Chris Wilson67d97da2016-07-04 08:08:31 +01003514 bool rearm_hangcheck;
3515
3516 if (!READ_ONCE(dev_priv->gt.awake))
3517 return;
3518
Chris Wilson4dfacb02018-05-31 09:22:43 +01003519 if (READ_ONCE(dev_priv->gt.active_requests))
3520 return;
3521
3522 /*
3523 * Flush out the last user context, leaving only the pinned
3524 * kernel context resident. When we are idling on the kernel_context,
3525 * no more new requests (with a context switch) are emitted and we
3526 * can finally rest. A consequence is that the idle work handler is
3527 * always called at least twice before idling (and if the system is
3528 * idle that implies a round trip through the retire worker).
3529 */
3530 mutex_lock(&dev_priv->drm.struct_mutex);
3531 i915_gem_switch_to_kernel_context(dev_priv);
3532 mutex_unlock(&dev_priv->drm.struct_mutex);
3533
3534 GEM_TRACE("active_requests=%d (after switch-to-kernel-context)\n",
3535 READ_ONCE(dev_priv->gt.active_requests));
3536
Imre Deak0cb56702016-11-07 11:20:04 +02003537 /*
3538 * Wait for last execlists context complete, but bail out in case a
Chris Wilsonffed7bd2018-03-01 10:33:38 +00003539 * new request is submitted. As we don't trust the hardware, we
3540 * continue on if the wait times out. This is necessary to allow
3541 * the machine to suspend even if the hardware dies, and we will
3542 * try to recover in resume (after depriving the hardware of power,
3543 * it may be in a better mmod).
Imre Deak0cb56702016-11-07 11:20:04 +02003544 */
Chris Wilsonffed7bd2018-03-01 10:33:38 +00003545 __wait_for(if (new_requests_since_last_retire(dev_priv)) return,
3546 intel_engines_are_idle(dev_priv),
3547 I915_IDLE_ENGINES_TIMEOUT * 1000,
3548 10, 500);
Chris Wilson67d97da2016-07-04 08:08:31 +01003549
3550 rearm_hangcheck =
3551 cancel_delayed_work_sync(&dev_priv->gpu_error.hangcheck_work);
3552
Chris Wilson5427f202017-10-23 22:32:34 +01003553 if (!mutex_trylock(&dev_priv->drm.struct_mutex)) {
Chris Wilson67d97da2016-07-04 08:08:31 +01003554 /* Currently busy, come back later */
3555 mod_delayed_work(dev_priv->wq,
3556 &dev_priv->gt.idle_work,
3557 msecs_to_jiffies(50));
3558 goto out_rearm;
3559 }
3560
Imre Deak93c97dc2016-11-07 11:20:03 +02003561 /*
3562 * New request retired after this work handler started, extend active
3563 * period until next instance of the work.
3564 */
Chris Wilson5427f202017-10-23 22:32:34 +01003565 if (new_requests_since_last_retire(dev_priv))
Imre Deak93c97dc2016-11-07 11:20:03 +02003566 goto out_unlock;
3567
Chris Wilsone4d20062018-04-06 16:51:44 +01003568 epoch = __i915_gem_park(dev_priv);
Chris Wilsonff320d62017-10-23 22:32:35 +01003569
Chris Wilson1934f5de2018-05-31 23:40:57 +01003570 assert_kernel_context_is_current(dev_priv);
3571
Chris Wilson67d97da2016-07-04 08:08:31 +01003572 rearm_hangcheck = false;
Chris Wilson67d97da2016-07-04 08:08:31 +01003573out_unlock:
Chris Wilson5427f202017-10-23 22:32:34 +01003574 mutex_unlock(&dev_priv->drm.struct_mutex);
Chris Wilson35c94182015-04-07 16:20:37 +01003575
Chris Wilson67d97da2016-07-04 08:08:31 +01003576out_rearm:
3577 if (rearm_hangcheck) {
3578 GEM_BUG_ON(!dev_priv->gt.awake);
3579 i915_queue_hangcheck(dev_priv);
Chris Wilson35c94182015-04-07 16:20:37 +01003580 }
Chris Wilson84a10742018-01-24 11:36:08 +00003581
3582 /*
3583 * When we are idle, it is an opportune time to reap our caches.
3584 * However, we have many objects that utilise RCU and the ordered
3585 * i915->wq that this work is executing on. To try and flush any
3586 * pending frees now we are idle, we first wait for an RCU grace
3587 * period, and then queue a task (that will run last on the wq) to
3588 * shrink and re-optimize the caches.
3589 */
3590 if (same_epoch(dev_priv, epoch)) {
3591 struct sleep_rcu_work *s = kmalloc(sizeof(*s), GFP_KERNEL);
3592 if (s) {
3593 s->i915 = dev_priv;
3594 s->epoch = epoch;
3595 call_rcu(&s->rcu, __sleep_rcu);
3596 }
3597 }
Eric Anholt673a3942008-07-30 12:06:12 -07003598}
3599
Chris Wilsonb1f788c2016-08-04 07:52:45 +01003600void i915_gem_close_object(struct drm_gem_object *gem, struct drm_file *file)
3601{
Chris Wilsond1b48c12017-08-16 09:52:08 +01003602 struct drm_i915_private *i915 = to_i915(gem->dev);
Chris Wilsonb1f788c2016-08-04 07:52:45 +01003603 struct drm_i915_gem_object *obj = to_intel_bo(gem);
3604 struct drm_i915_file_private *fpriv = file->driver_priv;
Chris Wilsond1b48c12017-08-16 09:52:08 +01003605 struct i915_lut_handle *lut, *ln;
Chris Wilsonb1f788c2016-08-04 07:52:45 +01003606
Chris Wilsond1b48c12017-08-16 09:52:08 +01003607 mutex_lock(&i915->drm.struct_mutex);
3608
3609 list_for_each_entry_safe(lut, ln, &obj->lut_list, obj_link) {
3610 struct i915_gem_context *ctx = lut->ctx;
3611 struct i915_vma *vma;
3612
Chris Wilson432295d2017-08-22 12:05:15 +01003613 GEM_BUG_ON(ctx->file_priv == ERR_PTR(-EBADF));
Chris Wilsond1b48c12017-08-16 09:52:08 +01003614 if (ctx->file_priv != fpriv)
3615 continue;
3616
3617 vma = radix_tree_delete(&ctx->handles_vma, lut->handle);
Chris Wilson3ffff012017-08-22 12:05:17 +01003618 GEM_BUG_ON(vma->obj != obj);
3619
3620 /* We allow the process to have multiple handles to the same
3621 * vma, in the same fd namespace, by virtue of flink/open.
3622 */
3623 GEM_BUG_ON(!vma->open_count);
3624 if (!--vma->open_count && !i915_vma_is_ggtt(vma))
Chris Wilsonb1f788c2016-08-04 07:52:45 +01003625 i915_vma_close(vma);
Chris Wilsonf8a7fde2016-10-28 13:58:29 +01003626
Chris Wilsond1b48c12017-08-16 09:52:08 +01003627 list_del(&lut->obj_link);
3628 list_del(&lut->ctx_link);
Chris Wilson4ff4b442017-06-16 15:05:16 +01003629
Chris Wilsond1b48c12017-08-16 09:52:08 +01003630 kmem_cache_free(i915->luts, lut);
3631 __i915_gem_object_release_unless_active(obj);
Chris Wilsonf8a7fde2016-10-28 13:58:29 +01003632 }
Chris Wilsond1b48c12017-08-16 09:52:08 +01003633
3634 mutex_unlock(&i915->drm.struct_mutex);
Chris Wilsonb1f788c2016-08-04 07:52:45 +01003635}
3636
Chris Wilsone95433c2016-10-28 13:58:27 +01003637static unsigned long to_wait_timeout(s64 timeout_ns)
3638{
3639 if (timeout_ns < 0)
3640 return MAX_SCHEDULE_TIMEOUT;
3641
3642 if (timeout_ns == 0)
3643 return 0;
3644
3645 return nsecs_to_jiffies_timeout(timeout_ns);
3646}
3647
Ben Widawsky5816d642012-04-11 11:18:19 -07003648/**
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07003649 * i915_gem_wait_ioctl - implements DRM_IOCTL_I915_GEM_WAIT
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01003650 * @dev: drm device pointer
3651 * @data: ioctl data blob
3652 * @file: drm file pointer
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07003653 *
3654 * Returns 0 if successful, else an error is returned with the remaining time in
3655 * the timeout parameter.
3656 * -ETIME: object is still busy after timeout
3657 * -ERESTARTSYS: signal interrupted the wait
3658 * -ENONENT: object doesn't exist
3659 * Also possible, but rare:
Chris Wilsonb8050142017-08-11 11:57:31 +01003660 * -EAGAIN: incomplete, restart syscall
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07003661 * -ENOMEM: damn
3662 * -ENODEV: Internal IRQ fail
3663 * -E?: The add request failed
3664 *
3665 * The wait ioctl with a timeout of 0 reimplements the busy ioctl. With any
3666 * non-zero timeout parameter the wait ioctl will wait for the given number of
3667 * nanoseconds on an object becoming unbusy. Since the wait itself does so
3668 * without holding struct_mutex the object may become re-busied before this
3669 * function completes. A similar but shorter * race condition exists in the busy
3670 * ioctl
3671 */
3672int
3673i915_gem_wait_ioctl(struct drm_device *dev, void *data, struct drm_file *file)
3674{
3675 struct drm_i915_gem_wait *args = data;
3676 struct drm_i915_gem_object *obj;
Chris Wilsone95433c2016-10-28 13:58:27 +01003677 ktime_t start;
3678 long ret;
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07003679
Daniel Vetter11b5d512014-09-29 15:31:26 +02003680 if (args->flags != 0)
3681 return -EINVAL;
3682
Chris Wilson03ac0642016-07-20 13:31:51 +01003683 obj = i915_gem_object_lookup(file, args->bo_handle);
Chris Wilson033d5492016-08-05 10:14:17 +01003684 if (!obj)
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07003685 return -ENOENT;
Chris Wilson033d5492016-08-05 10:14:17 +01003686
Chris Wilsone95433c2016-10-28 13:58:27 +01003687 start = ktime_get();
3688
3689 ret = i915_gem_object_wait(obj,
3690 I915_WAIT_INTERRUPTIBLE | I915_WAIT_ALL,
3691 to_wait_timeout(args->timeout_ns),
3692 to_rps_client(file));
3693
3694 if (args->timeout_ns > 0) {
3695 args->timeout_ns -= ktime_to_ns(ktime_sub(ktime_get(), start));
3696 if (args->timeout_ns < 0)
3697 args->timeout_ns = 0;
Chris Wilsonc1d20612017-02-16 12:54:41 +00003698
3699 /*
3700 * Apparently ktime isn't accurate enough and occasionally has a
3701 * bit of mismatch in the jiffies<->nsecs<->ktime loop. So patch
3702 * things up to make the test happy. We allow up to 1 jiffy.
3703 *
3704 * This is a regression from the timespec->ktime conversion.
3705 */
3706 if (ret == -ETIME && !nsecs_to_jiffies(args->timeout_ns))
3707 args->timeout_ns = 0;
Chris Wilsonb8050142017-08-11 11:57:31 +01003708
3709 /* Asked to wait beyond the jiffie/scheduler precision? */
3710 if (ret == -ETIME && args->timeout_ns)
3711 ret = -EAGAIN;
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07003712 }
3713
Chris Wilsonf0cd5182016-10-28 13:58:43 +01003714 i915_gem_object_put(obj);
John Harrisonff865882014-11-24 18:49:28 +00003715 return ret;
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07003716}
3717
Chris Wilsona89d1f92018-05-02 17:38:39 +01003718static int wait_for_timeline(struct i915_timeline *tl, unsigned int flags)
Daniel Vetter4df2faf2010-02-19 11:52:00 +01003719{
Chris Wilson06060352018-05-31 09:22:44 +01003720 struct i915_request *rq;
3721 long ret;
3722
3723 rq = i915_gem_active_get_unlocked(&tl->last_request);
3724 if (!rq)
3725 return 0;
3726
3727 /*
3728 * "Race-to-idle".
3729 *
3730 * Switching to the kernel context is often used a synchronous
3731 * step prior to idling, e.g. in suspend for flushing all
3732 * current operations to memory before sleeping. These we
3733 * want to complete as quickly as possible to avoid prolonged
3734 * stalls, so allow the gpu to boost to maximum clocks.
3735 */
3736 if (flags & I915_WAIT_FOR_IDLE_BOOST)
3737 gen6_rps_boost(rq, NULL);
3738
3739 ret = i915_request_wait(rq, flags, MAX_SCHEDULE_TIMEOUT);
3740 i915_request_put(rq);
3741
3742 return ret < 0 ? ret : 0;
Chris Wilson73cb9702016-10-28 13:58:46 +01003743}
3744
Chris Wilson25112b62017-03-30 15:50:39 +01003745static int wait_for_engines(struct drm_i915_private *i915)
3746{
Chris Wilsonee42c002017-12-11 19:41:34 +00003747 if (wait_for(intel_engines_are_idle(i915), I915_IDLE_ENGINES_TIMEOUT)) {
Chris Wilson59e4b192017-12-11 19:41:35 +00003748 dev_err(i915->drm.dev,
3749 "Failed to idle engines, declaring wedged!\n");
Chris Wilson629820f2018-03-09 10:11:14 +00003750 GEM_TRACE_DUMP();
Chris Wilsoncad99462017-08-26 12:09:33 +01003751 i915_gem_set_wedged(i915);
3752 return -EIO;
Chris Wilson25112b62017-03-30 15:50:39 +01003753 }
3754
3755 return 0;
3756}
3757
Chris Wilson73cb9702016-10-28 13:58:46 +01003758int i915_gem_wait_for_idle(struct drm_i915_private *i915, unsigned int flags)
3759{
Chris Wilson09a4c022018-05-24 09:11:35 +01003760 GEM_TRACE("flags=%x (%s)\n",
3761 flags, flags & I915_WAIT_LOCKED ? "locked" : "unlocked");
3762
Chris Wilson863e9fd2017-05-30 13:13:32 +01003763 /* If the device is asleep, we have no requests outstanding */
3764 if (!READ_ONCE(i915->gt.awake))
3765 return 0;
3766
Chris Wilson9caa34a2016-11-11 14:58:08 +00003767 if (flags & I915_WAIT_LOCKED) {
Chris Wilsona89d1f92018-05-02 17:38:39 +01003768 struct i915_timeline *tl;
3769 int err;
Chris Wilson9caa34a2016-11-11 14:58:08 +00003770
3771 lockdep_assert_held(&i915->drm.struct_mutex);
3772
3773 list_for_each_entry(tl, &i915->gt.timelines, link) {
Chris Wilsona89d1f92018-05-02 17:38:39 +01003774 err = wait_for_timeline(tl, flags);
3775 if (err)
3776 return err;
Chris Wilson9caa34a2016-11-11 14:58:08 +00003777 }
Chris Wilsone61e0f52018-02-21 09:56:36 +00003778 i915_retire_requests(i915);
Chris Wilson09a4c022018-05-24 09:11:35 +01003779 GEM_BUG_ON(i915->gt.active_requests);
Chris Wilson25112b62017-03-30 15:50:39 +01003780
Chris Wilsona89d1f92018-05-02 17:38:39 +01003781 return wait_for_engines(i915);
Chris Wilson9caa34a2016-11-11 14:58:08 +00003782 } else {
Chris Wilsona89d1f92018-05-02 17:38:39 +01003783 struct intel_engine_cs *engine;
3784 enum intel_engine_id id;
3785 int err;
Zou Nan haid1b851f2010-05-21 09:08:57 +08003786
Chris Wilsona89d1f92018-05-02 17:38:39 +01003787 for_each_engine(engine, i915, id) {
3788 err = wait_for_timeline(&engine->timeline, flags);
3789 if (err)
3790 return err;
3791 }
3792
3793 return 0;
3794 }
Daniel Vetter4df2faf2010-02-19 11:52:00 +01003795}
3796
Chris Wilson5a97bcc2017-02-22 11:40:46 +00003797static void __i915_gem_object_flush_for_display(struct drm_i915_gem_object *obj)
3798{
Chris Wilsone27ab732017-06-15 13:38:49 +01003799 /*
3800 * We manually flush the CPU domain so that we can override and
3801 * force the flush for the display, and perform it asyncrhonously.
3802 */
3803 flush_write_domain(obj, ~I915_GEM_DOMAIN_CPU);
3804 if (obj->cache_dirty)
3805 i915_gem_clflush_object(obj, I915_CLFLUSH_FORCE);
Christian Königc0a51fd2018-02-16 13:43:38 +01003806 obj->write_domain = 0;
Chris Wilson5a97bcc2017-02-22 11:40:46 +00003807}
3808
3809void i915_gem_object_flush_if_display(struct drm_i915_gem_object *obj)
3810{
Chris Wilsonbd3d2252017-10-13 21:26:14 +01003811 if (!READ_ONCE(obj->pin_global))
Chris Wilson5a97bcc2017-02-22 11:40:46 +00003812 return;
3813
3814 mutex_lock(&obj->base.dev->struct_mutex);
3815 __i915_gem_object_flush_for_display(obj);
3816 mutex_unlock(&obj->base.dev->struct_mutex);
3817}
3818
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003819/**
Chris Wilsone22d8e32017-04-12 12:01:11 +01003820 * Moves a single object to the WC read, and possibly write domain.
3821 * @obj: object to act on
3822 * @write: ask for write access or read only
3823 *
3824 * This function returns when the move is complete, including waiting on
3825 * flushes to occur.
3826 */
3827int
3828i915_gem_object_set_to_wc_domain(struct drm_i915_gem_object *obj, bool write)
3829{
3830 int ret;
3831
3832 lockdep_assert_held(&obj->base.dev->struct_mutex);
3833
3834 ret = i915_gem_object_wait(obj,
3835 I915_WAIT_INTERRUPTIBLE |
3836 I915_WAIT_LOCKED |
3837 (write ? I915_WAIT_ALL : 0),
3838 MAX_SCHEDULE_TIMEOUT,
3839 NULL);
3840 if (ret)
3841 return ret;
3842
Christian Königc0a51fd2018-02-16 13:43:38 +01003843 if (obj->write_domain == I915_GEM_DOMAIN_WC)
Chris Wilsone22d8e32017-04-12 12:01:11 +01003844 return 0;
3845
3846 /* Flush and acquire obj->pages so that we are coherent through
3847 * direct access in memory with previous cached writes through
3848 * shmemfs and that our cache domain tracking remains valid.
3849 * For example, if the obj->filp was moved to swap without us
3850 * being notified and releasing the pages, we would mistakenly
3851 * continue to assume that the obj remained out of the CPU cached
3852 * domain.
3853 */
3854 ret = i915_gem_object_pin_pages(obj);
3855 if (ret)
3856 return ret;
3857
3858 flush_write_domain(obj, ~I915_GEM_DOMAIN_WC);
3859
3860 /* Serialise direct access to this object with the barriers for
3861 * coherent writes from the GPU, by effectively invalidating the
3862 * WC domain upon first access.
3863 */
Christian Königc0a51fd2018-02-16 13:43:38 +01003864 if ((obj->read_domains & I915_GEM_DOMAIN_WC) == 0)
Chris Wilsone22d8e32017-04-12 12:01:11 +01003865 mb();
3866
3867 /* It should now be out of any other write domains, and we can update
3868 * the domain values for our changes.
3869 */
Christian Königc0a51fd2018-02-16 13:43:38 +01003870 GEM_BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_WC) != 0);
3871 obj->read_domains |= I915_GEM_DOMAIN_WC;
Chris Wilsone22d8e32017-04-12 12:01:11 +01003872 if (write) {
Christian Königc0a51fd2018-02-16 13:43:38 +01003873 obj->read_domains = I915_GEM_DOMAIN_WC;
3874 obj->write_domain = I915_GEM_DOMAIN_WC;
Chris Wilsone22d8e32017-04-12 12:01:11 +01003875 obj->mm.dirty = true;
3876 }
3877
3878 i915_gem_object_unpin_pages(obj);
3879 return 0;
3880}
3881
3882/**
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003883 * Moves a single object to the GTT read, and possibly write domain.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01003884 * @obj: object to act on
3885 * @write: ask for write access or read only
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003886 *
3887 * This function returns when the move is complete, including waiting on
3888 * flushes to occur.
3889 */
Jesse Barnes79e53942008-11-07 14:24:08 -08003890int
Chris Wilson20217462010-11-23 15:26:33 +00003891i915_gem_object_set_to_gtt_domain(struct drm_i915_gem_object *obj, bool write)
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003892{
Eric Anholte47c68e2008-11-14 13:35:19 -08003893 int ret;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003894
Chris Wilsone95433c2016-10-28 13:58:27 +01003895 lockdep_assert_held(&obj->base.dev->struct_mutex);
Chris Wilson4c7d62c2016-10-28 13:58:32 +01003896
Chris Wilsone95433c2016-10-28 13:58:27 +01003897 ret = i915_gem_object_wait(obj,
3898 I915_WAIT_INTERRUPTIBLE |
3899 I915_WAIT_LOCKED |
3900 (write ? I915_WAIT_ALL : 0),
3901 MAX_SCHEDULE_TIMEOUT,
3902 NULL);
Chris Wilson88241782011-01-07 17:09:48 +00003903 if (ret)
3904 return ret;
3905
Christian Königc0a51fd2018-02-16 13:43:38 +01003906 if (obj->write_domain == I915_GEM_DOMAIN_GTT)
Chris Wilsonc13d87e2016-07-20 09:21:15 +01003907 return 0;
3908
Chris Wilson43566de2015-01-02 16:29:29 +05303909 /* Flush and acquire obj->pages so that we are coherent through
3910 * direct access in memory with previous cached writes through
3911 * shmemfs and that our cache domain tracking remains valid.
3912 * For example, if the obj->filp was moved to swap without us
3913 * being notified and releasing the pages, we would mistakenly
3914 * continue to assume that the obj remained out of the CPU cached
3915 * domain.
3916 */
Chris Wilsona4f5ea62016-10-28 13:58:35 +01003917 ret = i915_gem_object_pin_pages(obj);
Chris Wilson43566de2015-01-02 16:29:29 +05303918 if (ret)
3919 return ret;
3920
Chris Wilsonef749212017-04-12 12:01:10 +01003921 flush_write_domain(obj, ~I915_GEM_DOMAIN_GTT);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003922
Chris Wilsond0a57782012-10-09 19:24:37 +01003923 /* Serialise direct access to this object with the barriers for
3924 * coherent writes from the GPU, by effectively invalidating the
3925 * GTT domain upon first access.
3926 */
Christian Königc0a51fd2018-02-16 13:43:38 +01003927 if ((obj->read_domains & I915_GEM_DOMAIN_GTT) == 0)
Chris Wilsond0a57782012-10-09 19:24:37 +01003928 mb();
3929
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003930 /* It should now be out of any other write domains, and we can update
3931 * the domain values for our changes.
3932 */
Christian Königc0a51fd2018-02-16 13:43:38 +01003933 GEM_BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_GTT) != 0);
3934 obj->read_domains |= I915_GEM_DOMAIN_GTT;
Eric Anholte47c68e2008-11-14 13:35:19 -08003935 if (write) {
Christian Königc0a51fd2018-02-16 13:43:38 +01003936 obj->read_domains = I915_GEM_DOMAIN_GTT;
3937 obj->write_domain = I915_GEM_DOMAIN_GTT;
Chris Wilsona4f5ea62016-10-28 13:58:35 +01003938 obj->mm.dirty = true;
Eric Anholte47c68e2008-11-14 13:35:19 -08003939 }
3940
Chris Wilsona4f5ea62016-10-28 13:58:35 +01003941 i915_gem_object_unpin_pages(obj);
Eric Anholte47c68e2008-11-14 13:35:19 -08003942 return 0;
3943}
3944
Chris Wilsonef55f922015-10-09 14:11:27 +01003945/**
3946 * Changes the cache-level of an object across all VMA.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01003947 * @obj: object to act on
3948 * @cache_level: new cache level to set for the object
Chris Wilsonef55f922015-10-09 14:11:27 +01003949 *
3950 * After this function returns, the object will be in the new cache-level
3951 * across all GTT and the contents of the backing storage will be coherent,
3952 * with respect to the new cache-level. In order to keep the backing storage
3953 * coherent for all users, we only allow a single cache level to be set
3954 * globally on the object and prevent it from being changed whilst the
3955 * hardware is reading from the object. That is if the object is currently
3956 * on the scanout it will be set to uncached (or equivalent display
3957 * cache coherency) and all non-MOCS GPU access will also be uncached so
3958 * that all direct access to the scanout remains coherent.
3959 */
Chris Wilsone4ffd172011-04-04 09:44:39 +01003960int i915_gem_object_set_cache_level(struct drm_i915_gem_object *obj,
3961 enum i915_cache_level cache_level)
3962{
Chris Wilsonaa653a62016-08-04 07:52:27 +01003963 struct i915_vma *vma;
Chris Wilsona6a7cc42016-11-18 21:17:46 +00003964 int ret;
Chris Wilsone4ffd172011-04-04 09:44:39 +01003965
Chris Wilson4c7d62c2016-10-28 13:58:32 +01003966 lockdep_assert_held(&obj->base.dev->struct_mutex);
3967
Chris Wilsone4ffd172011-04-04 09:44:39 +01003968 if (obj->cache_level == cache_level)
Chris Wilsona6a7cc42016-11-18 21:17:46 +00003969 return 0;
Chris Wilsone4ffd172011-04-04 09:44:39 +01003970
Chris Wilsonef55f922015-10-09 14:11:27 +01003971 /* Inspect the list of currently bound VMA and unbind any that would
3972 * be invalid given the new cache-level. This is principally to
3973 * catch the issue of the CS prefetch crossing page boundaries and
3974 * reading an invalid PTE on older architectures.
3975 */
Chris Wilsonaa653a62016-08-04 07:52:27 +01003976restart:
3977 list_for_each_entry(vma, &obj->vma_list, obj_link) {
Chris Wilsonef55f922015-10-09 14:11:27 +01003978 if (!drm_mm_node_allocated(&vma->node))
3979 continue;
3980
Chris Wilson20dfbde2016-08-04 16:32:30 +01003981 if (i915_vma_is_pinned(vma)) {
Chris Wilsonef55f922015-10-09 14:11:27 +01003982 DRM_DEBUG("can not change the cache level of pinned objects\n");
3983 return -EBUSY;
3984 }
3985
Chris Wilson010e3e62017-12-06 12:49:13 +00003986 if (!i915_vma_is_closed(vma) &&
3987 i915_gem_valid_gtt_space(vma, cache_level))
Chris Wilsonaa653a62016-08-04 07:52:27 +01003988 continue;
3989
3990 ret = i915_vma_unbind(vma);
3991 if (ret)
3992 return ret;
3993
3994 /* As unbinding may affect other elements in the
3995 * obj->vma_list (due to side-effects from retiring
3996 * an active vma), play safe and restart the iterator.
3997 */
3998 goto restart;
Chris Wilson42d6ab42012-07-26 11:49:32 +01003999 }
4000
Chris Wilsonef55f922015-10-09 14:11:27 +01004001 /* We can reuse the existing drm_mm nodes but need to change the
4002 * cache-level on the PTE. We could simply unbind them all and
4003 * rebind with the correct cache-level on next use. However since
4004 * we already have a valid slot, dma mapping, pages etc, we may as
4005 * rewrite the PTE in the belief that doing so tramples upon less
4006 * state and so involves less work.
4007 */
Chris Wilson15717de2016-08-04 07:52:26 +01004008 if (obj->bind_count) {
Chris Wilsonef55f922015-10-09 14:11:27 +01004009 /* Before we change the PTE, the GPU must not be accessing it.
4010 * If we wait upon the object, we know that all the bound
4011 * VMA are no longer active.
4012 */
Chris Wilsone95433c2016-10-28 13:58:27 +01004013 ret = i915_gem_object_wait(obj,
4014 I915_WAIT_INTERRUPTIBLE |
4015 I915_WAIT_LOCKED |
4016 I915_WAIT_ALL,
4017 MAX_SCHEDULE_TIMEOUT,
4018 NULL);
Chris Wilsone4ffd172011-04-04 09:44:39 +01004019 if (ret)
4020 return ret;
4021
Tvrtko Ursulin0031fb92016-11-04 14:42:44 +00004022 if (!HAS_LLC(to_i915(obj->base.dev)) &&
4023 cache_level != I915_CACHE_NONE) {
Chris Wilsonef55f922015-10-09 14:11:27 +01004024 /* Access to snoopable pages through the GTT is
4025 * incoherent and on some machines causes a hard
4026 * lockup. Relinquish the CPU mmaping to force
4027 * userspace to refault in the pages and we can
4028 * then double check if the GTT mapping is still
4029 * valid for that pointer access.
4030 */
4031 i915_gem_release_mmap(obj);
Chris Wilsone4ffd172011-04-04 09:44:39 +01004032
Chris Wilsonef55f922015-10-09 14:11:27 +01004033 /* As we no longer need a fence for GTT access,
4034 * we can relinquish it now (and so prevent having
4035 * to steal a fence from someone else on the next
4036 * fence request). Note GPU activity would have
4037 * dropped the fence as all snoopable access is
4038 * supposed to be linear.
4039 */
Chris Wilsone2189dd2017-12-07 21:14:07 +00004040 for_each_ggtt_vma(vma, obj) {
Chris Wilson49ef5292016-08-18 17:17:00 +01004041 ret = i915_vma_put_fence(vma);
4042 if (ret)
4043 return ret;
4044 }
Chris Wilsonef55f922015-10-09 14:11:27 +01004045 } else {
4046 /* We either have incoherent backing store and
4047 * so no GTT access or the architecture is fully
4048 * coherent. In such cases, existing GTT mmaps
4049 * ignore the cache bit in the PTE and we can
4050 * rewrite it without confusing the GPU or having
4051 * to force userspace to fault back in its mmaps.
4052 */
Chris Wilsone4ffd172011-04-04 09:44:39 +01004053 }
4054
Chris Wilson1c7f4bc2016-02-26 11:03:19 +00004055 list_for_each_entry(vma, &obj->vma_list, obj_link) {
Chris Wilsonef55f922015-10-09 14:11:27 +01004056 if (!drm_mm_node_allocated(&vma->node))
4057 continue;
4058
4059 ret = i915_vma_bind(vma, cache_level, PIN_UPDATE);
4060 if (ret)
4061 return ret;
4062 }
Chris Wilsone4ffd172011-04-04 09:44:39 +01004063 }
4064
Chris Wilson1c7f4bc2016-02-26 11:03:19 +00004065 list_for_each_entry(vma, &obj->vma_list, obj_link)
Chris Wilson2c225692013-08-09 12:26:45 +01004066 vma->node.color = cache_level;
Chris Wilsonb8f55be2017-08-11 12:11:16 +01004067 i915_gem_object_set_cache_coherency(obj, cache_level);
Chris Wilsone27ab732017-06-15 13:38:49 +01004068 obj->cache_dirty = true; /* Always invalidate stale cachelines */
Chris Wilson2c225692013-08-09 12:26:45 +01004069
Chris Wilsone4ffd172011-04-04 09:44:39 +01004070 return 0;
4071}
4072
Ben Widawsky199adf42012-09-21 17:01:20 -07004073int i915_gem_get_caching_ioctl(struct drm_device *dev, void *data,
4074 struct drm_file *file)
Chris Wilsone6994ae2012-07-10 10:27:08 +01004075{
Ben Widawsky199adf42012-09-21 17:01:20 -07004076 struct drm_i915_gem_caching *args = data;
Chris Wilsone6994ae2012-07-10 10:27:08 +01004077 struct drm_i915_gem_object *obj;
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004078 int err = 0;
Chris Wilsone6994ae2012-07-10 10:27:08 +01004079
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004080 rcu_read_lock();
4081 obj = i915_gem_object_lookup_rcu(file, args->handle);
4082 if (!obj) {
4083 err = -ENOENT;
4084 goto out;
4085 }
Chris Wilsone6994ae2012-07-10 10:27:08 +01004086
Chris Wilson651d7942013-08-08 14:41:10 +01004087 switch (obj->cache_level) {
4088 case I915_CACHE_LLC:
4089 case I915_CACHE_L3_LLC:
4090 args->caching = I915_CACHING_CACHED;
4091 break;
4092
Chris Wilson4257d3b2013-08-08 14:41:11 +01004093 case I915_CACHE_WT:
4094 args->caching = I915_CACHING_DISPLAY;
4095 break;
4096
Chris Wilson651d7942013-08-08 14:41:10 +01004097 default:
4098 args->caching = I915_CACHING_NONE;
4099 break;
4100 }
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004101out:
4102 rcu_read_unlock();
4103 return err;
Chris Wilsone6994ae2012-07-10 10:27:08 +01004104}
4105
Ben Widawsky199adf42012-09-21 17:01:20 -07004106int i915_gem_set_caching_ioctl(struct drm_device *dev, void *data,
4107 struct drm_file *file)
Chris Wilsone6994ae2012-07-10 10:27:08 +01004108{
Chris Wilson9c870d02016-10-24 13:42:15 +01004109 struct drm_i915_private *i915 = to_i915(dev);
Ben Widawsky199adf42012-09-21 17:01:20 -07004110 struct drm_i915_gem_caching *args = data;
Chris Wilsone6994ae2012-07-10 10:27:08 +01004111 struct drm_i915_gem_object *obj;
4112 enum i915_cache_level level;
Chris Wilsond65415d2017-01-19 08:22:10 +00004113 int ret = 0;
Chris Wilsone6994ae2012-07-10 10:27:08 +01004114
Ben Widawsky199adf42012-09-21 17:01:20 -07004115 switch (args->caching) {
4116 case I915_CACHING_NONE:
Chris Wilsone6994ae2012-07-10 10:27:08 +01004117 level = I915_CACHE_NONE;
4118 break;
Ben Widawsky199adf42012-09-21 17:01:20 -07004119 case I915_CACHING_CACHED:
Imre Deake5756c12015-08-14 18:43:30 +03004120 /*
4121 * Due to a HW issue on BXT A stepping, GPU stores via a
4122 * snooped mapping may leave stale data in a corresponding CPU
4123 * cacheline, whereas normally such cachelines would get
4124 * invalidated.
4125 */
Chris Wilson9c870d02016-10-24 13:42:15 +01004126 if (!HAS_LLC(i915) && !HAS_SNOOP(i915))
Imre Deake5756c12015-08-14 18:43:30 +03004127 return -ENODEV;
4128
Chris Wilsone6994ae2012-07-10 10:27:08 +01004129 level = I915_CACHE_LLC;
4130 break;
Chris Wilson4257d3b2013-08-08 14:41:11 +01004131 case I915_CACHING_DISPLAY:
Chris Wilson9c870d02016-10-24 13:42:15 +01004132 level = HAS_WT(i915) ? I915_CACHE_WT : I915_CACHE_NONE;
Chris Wilson4257d3b2013-08-08 14:41:11 +01004133 break;
Chris Wilsone6994ae2012-07-10 10:27:08 +01004134 default:
4135 return -EINVAL;
4136 }
4137
Chris Wilsond65415d2017-01-19 08:22:10 +00004138 obj = i915_gem_object_lookup(file, args->handle);
4139 if (!obj)
4140 return -ENOENT;
4141
Tina Zhanga03f3952017-11-14 10:25:13 +00004142 /*
4143 * The caching mode of proxy object is handled by its generator, and
4144 * not allowed to be changed by userspace.
4145 */
4146 if (i915_gem_object_is_proxy(obj)) {
4147 ret = -ENXIO;
4148 goto out;
4149 }
4150
Chris Wilsond65415d2017-01-19 08:22:10 +00004151 if (obj->cache_level == level)
4152 goto out;
4153
4154 ret = i915_gem_object_wait(obj,
4155 I915_WAIT_INTERRUPTIBLE,
4156 MAX_SCHEDULE_TIMEOUT,
4157 to_rps_client(file));
4158 if (ret)
4159 goto out;
4160
Ben Widawsky3bc29132012-09-26 16:15:20 -07004161 ret = i915_mutex_lock_interruptible(dev);
4162 if (ret)
Chris Wilsond65415d2017-01-19 08:22:10 +00004163 goto out;
Chris Wilsone6994ae2012-07-10 10:27:08 +01004164
4165 ret = i915_gem_object_set_cache_level(obj, level);
Chris Wilsone6994ae2012-07-10 10:27:08 +01004166 mutex_unlock(&dev->struct_mutex);
Chris Wilsond65415d2017-01-19 08:22:10 +00004167
4168out:
4169 i915_gem_object_put(obj);
Chris Wilsone6994ae2012-07-10 10:27:08 +01004170 return ret;
4171}
4172
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08004173/*
Dhinakaran Pandiyan07bcd992018-03-06 19:34:18 -08004174 * Prepare buffer for display plane (scanout, cursors, etc). Can be called from
4175 * an uninterruptible phase (modesetting) and allows any flushes to be pipelined
4176 * (for pageflips). We only flush the caches while preparing the buffer for
4177 * display, the callers are responsible for frontbuffer flush.
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08004178 */
Chris Wilson058d88c2016-08-15 10:49:06 +01004179struct i915_vma *
Chris Wilson2da3b9b2011-04-14 09:41:17 +01004180i915_gem_object_pin_to_display_plane(struct drm_i915_gem_object *obj,
4181 u32 alignment,
Chris Wilson59354852018-02-20 13:42:06 +00004182 const struct i915_ggtt_view *view,
4183 unsigned int flags)
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08004184{
Chris Wilson058d88c2016-08-15 10:49:06 +01004185 struct i915_vma *vma;
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08004186 int ret;
4187
Chris Wilson4c7d62c2016-10-28 13:58:32 +01004188 lockdep_assert_held(&obj->base.dev->struct_mutex);
4189
Chris Wilsonbd3d2252017-10-13 21:26:14 +01004190 /* Mark the global pin early so that we account for the
Chris Wilsoncc98b412013-08-09 12:25:09 +01004191 * display coherency whilst setting up the cache domains.
4192 */
Chris Wilsonbd3d2252017-10-13 21:26:14 +01004193 obj->pin_global++;
Chris Wilsoncc98b412013-08-09 12:25:09 +01004194
Eric Anholta7ef0642011-03-29 16:59:54 -07004195 /* The display engine is not coherent with the LLC cache on gen6. As
4196 * a result, we make sure that the pinning that is about to occur is
4197 * done with uncached PTEs. This is lowest common denominator for all
4198 * chipsets.
4199 *
4200 * However for gen6+, we could do better by using the GFDT bit instead
4201 * of uncaching, which would allow us to flush all the LLC-cached data
4202 * with that bit in the PTE to main memory with just one PIPE_CONTROL.
4203 */
Chris Wilson651d7942013-08-08 14:41:10 +01004204 ret = i915_gem_object_set_cache_level(obj,
Tvrtko Ursulin86527442016-10-13 11:03:00 +01004205 HAS_WT(to_i915(obj->base.dev)) ?
4206 I915_CACHE_WT : I915_CACHE_NONE);
Chris Wilson058d88c2016-08-15 10:49:06 +01004207 if (ret) {
4208 vma = ERR_PTR(ret);
Chris Wilsonbd3d2252017-10-13 21:26:14 +01004209 goto err_unpin_global;
Chris Wilson058d88c2016-08-15 10:49:06 +01004210 }
Eric Anholta7ef0642011-03-29 16:59:54 -07004211
Chris Wilson2da3b9b2011-04-14 09:41:17 +01004212 /* As the user may map the buffer once pinned in the display plane
4213 * (e.g. libkms for the bootup splash), we have to ensure that we
Chris Wilson2efb8132016-08-18 17:17:06 +01004214 * always use map_and_fenceable for all scanout buffers. However,
4215 * it may simply be too big to fit into mappable, in which case
4216 * put it anyway and hope that userspace can cope (but always first
4217 * try to preserve the existing ABI).
Chris Wilson2da3b9b2011-04-14 09:41:17 +01004218 */
Chris Wilson2efb8132016-08-18 17:17:06 +01004219 vma = ERR_PTR(-ENOSPC);
Chris Wilson59354852018-02-20 13:42:06 +00004220 if ((flags & PIN_MAPPABLE) == 0 &&
4221 (!view || view->type == I915_GGTT_VIEW_NORMAL))
Chris Wilson2efb8132016-08-18 17:17:06 +01004222 vma = i915_gem_object_ggtt_pin(obj, view, 0, alignment,
Chris Wilson59354852018-02-20 13:42:06 +00004223 flags |
4224 PIN_MAPPABLE |
4225 PIN_NONBLOCK);
4226 if (IS_ERR(vma))
Chris Wilson767a2222016-11-07 11:01:28 +00004227 vma = i915_gem_object_ggtt_pin(obj, view, 0, alignment, flags);
Chris Wilson058d88c2016-08-15 10:49:06 +01004228 if (IS_ERR(vma))
Chris Wilsonbd3d2252017-10-13 21:26:14 +01004229 goto err_unpin_global;
Chris Wilson2da3b9b2011-04-14 09:41:17 +01004230
Chris Wilsond8923dc2016-08-18 17:17:07 +01004231 vma->display_alignment = max_t(u64, vma->display_alignment, alignment);
4232
Chris Wilson5a97bcc2017-02-22 11:40:46 +00004233 __i915_gem_object_flush_for_display(obj);
Chris Wilsonb118c1e2010-05-27 13:18:14 +01004234
Chris Wilson2da3b9b2011-04-14 09:41:17 +01004235 /* It should now be out of any other write domains, and we can update
4236 * the domain values for our changes.
4237 */
Christian Königc0a51fd2018-02-16 13:43:38 +01004238 obj->read_domains |= I915_GEM_DOMAIN_GTT;
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08004239
Chris Wilson058d88c2016-08-15 10:49:06 +01004240 return vma;
Chris Wilsoncc98b412013-08-09 12:25:09 +01004241
Chris Wilsonbd3d2252017-10-13 21:26:14 +01004242err_unpin_global:
4243 obj->pin_global--;
Chris Wilson058d88c2016-08-15 10:49:06 +01004244 return vma;
Chris Wilsoncc98b412013-08-09 12:25:09 +01004245}
4246
4247void
Chris Wilson058d88c2016-08-15 10:49:06 +01004248i915_gem_object_unpin_from_display_plane(struct i915_vma *vma)
Chris Wilsoncc98b412013-08-09 12:25:09 +01004249{
Chris Wilson49d73912016-11-29 09:50:08 +00004250 lockdep_assert_held(&vma->vm->i915->drm.struct_mutex);
Chris Wilson4c7d62c2016-10-28 13:58:32 +01004251
Chris Wilsonbd3d2252017-10-13 21:26:14 +01004252 if (WARN_ON(vma->obj->pin_global == 0))
Tvrtko Ursulin8a0c39b2015-04-13 11:50:09 +01004253 return;
4254
Chris Wilsonbd3d2252017-10-13 21:26:14 +01004255 if (--vma->obj->pin_global == 0)
Chris Wilsonf51455d2017-01-10 14:47:34 +00004256 vma->display_alignment = I915_GTT_MIN_ALIGNMENT;
Tvrtko Ursuline6617332015-03-23 11:10:33 +00004257
Chris Wilson383d5822016-08-18 17:17:08 +01004258 /* Bump the LRU to try and avoid premature eviction whilst flipping */
Chris Wilsonbefedbb2017-01-19 19:26:55 +00004259 i915_gem_object_bump_inactive_ggtt(vma->obj);
Chris Wilson383d5822016-08-18 17:17:08 +01004260
Chris Wilson058d88c2016-08-15 10:49:06 +01004261 i915_vma_unpin(vma);
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08004262}
4263
Eric Anholte47c68e2008-11-14 13:35:19 -08004264/**
4265 * Moves a single object to the CPU read, and possibly write domain.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01004266 * @obj: object to act on
4267 * @write: requesting write or read-only access
Eric Anholte47c68e2008-11-14 13:35:19 -08004268 *
4269 * This function returns when the move is complete, including waiting on
4270 * flushes to occur.
4271 */
Chris Wilsondabdfe02012-03-26 10:10:27 +02004272int
Chris Wilson919926a2010-11-12 13:42:53 +00004273i915_gem_object_set_to_cpu_domain(struct drm_i915_gem_object *obj, bool write)
Eric Anholte47c68e2008-11-14 13:35:19 -08004274{
Eric Anholte47c68e2008-11-14 13:35:19 -08004275 int ret;
4276
Chris Wilsone95433c2016-10-28 13:58:27 +01004277 lockdep_assert_held(&obj->base.dev->struct_mutex);
Chris Wilson4c7d62c2016-10-28 13:58:32 +01004278
Chris Wilsone95433c2016-10-28 13:58:27 +01004279 ret = i915_gem_object_wait(obj,
4280 I915_WAIT_INTERRUPTIBLE |
4281 I915_WAIT_LOCKED |
4282 (write ? I915_WAIT_ALL : 0),
4283 MAX_SCHEDULE_TIMEOUT,
4284 NULL);
Chris Wilson88241782011-01-07 17:09:48 +00004285 if (ret)
4286 return ret;
4287
Chris Wilsonef749212017-04-12 12:01:10 +01004288 flush_write_domain(obj, ~I915_GEM_DOMAIN_CPU);
Eric Anholte47c68e2008-11-14 13:35:19 -08004289
Eric Anholte47c68e2008-11-14 13:35:19 -08004290 /* Flush the CPU cache if it's still invalid. */
Christian Königc0a51fd2018-02-16 13:43:38 +01004291 if ((obj->read_domains & I915_GEM_DOMAIN_CPU) == 0) {
Chris Wilson57822dc2017-02-22 11:40:48 +00004292 i915_gem_clflush_object(obj, I915_CLFLUSH_SYNC);
Christian Königc0a51fd2018-02-16 13:43:38 +01004293 obj->read_domains |= I915_GEM_DOMAIN_CPU;
Eric Anholte47c68e2008-11-14 13:35:19 -08004294 }
4295
4296 /* It should now be out of any other write domains, and we can update
4297 * the domain values for our changes.
4298 */
Christian Königc0a51fd2018-02-16 13:43:38 +01004299 GEM_BUG_ON(obj->write_domain & ~I915_GEM_DOMAIN_CPU);
Eric Anholte47c68e2008-11-14 13:35:19 -08004300
4301 /* If we're writing through the CPU, then the GPU read domains will
4302 * need to be invalidated at next use.
4303 */
Chris Wilsone27ab732017-06-15 13:38:49 +01004304 if (write)
4305 __start_cpu_write(obj);
Eric Anholt2ef7eea2008-11-10 10:53:25 -08004306
4307 return 0;
4308}
4309
Eric Anholt673a3942008-07-30 12:06:12 -07004310/* Throttle our rendering by waiting until the ring has completed our requests
4311 * emitted over 20 msec ago.
4312 *
Eric Anholtb9624422009-06-03 07:27:35 +00004313 * Note that if we were to use the current jiffies each time around the loop,
4314 * we wouldn't escape the function with any frames outstanding if the time to
4315 * render a frame was over 20ms.
4316 *
Eric Anholt673a3942008-07-30 12:06:12 -07004317 * This should get us reasonable parallelism between CPU and GPU but also
4318 * relatively low latency when blocking on a particular request to finish.
4319 */
4320static int
Chris Wilsonf787a5f2010-09-24 16:02:42 +01004321i915_gem_ring_throttle(struct drm_device *dev, struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07004322{
Chris Wilsonfac5e232016-07-04 11:34:36 +01004323 struct drm_i915_private *dev_priv = to_i915(dev);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01004324 struct drm_i915_file_private *file_priv = file->driver_priv;
Chris Wilsond0bc54f2015-05-21 21:01:48 +01004325 unsigned long recent_enough = jiffies - DRM_I915_THROTTLE_JIFFIES;
Chris Wilsone61e0f52018-02-21 09:56:36 +00004326 struct i915_request *request, *target = NULL;
Chris Wilsone95433c2016-10-28 13:58:27 +01004327 long ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004328
Chris Wilsonf4457ae2016-04-13 17:35:08 +01004329 /* ABI: return -EIO if already wedged */
4330 if (i915_terminally_wedged(&dev_priv->gpu_error))
4331 return -EIO;
Chris Wilsone110e8d2011-01-26 15:39:14 +00004332
Chris Wilson1c255952010-09-26 11:03:27 +01004333 spin_lock(&file_priv->mm.lock);
Chris Wilsonc8659ef2017-03-02 12:25:25 +00004334 list_for_each_entry(request, &file_priv->mm.request_list, client_link) {
Eric Anholtb9624422009-06-03 07:27:35 +00004335 if (time_after_eq(request->emitted_jiffies, recent_enough))
4336 break;
4337
Chris Wilsonc8659ef2017-03-02 12:25:25 +00004338 if (target) {
4339 list_del(&target->client_link);
4340 target->file_priv = NULL;
4341 }
John Harrisonfcfa423c2015-05-29 17:44:12 +01004342
John Harrison54fb2412014-11-24 18:49:27 +00004343 target = request;
Eric Anholtb9624422009-06-03 07:27:35 +00004344 }
John Harrisonff865882014-11-24 18:49:28 +00004345 if (target)
Chris Wilsone61e0f52018-02-21 09:56:36 +00004346 i915_request_get(target);
Chris Wilson1c255952010-09-26 11:03:27 +01004347 spin_unlock(&file_priv->mm.lock);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01004348
John Harrison54fb2412014-11-24 18:49:27 +00004349 if (target == NULL)
Chris Wilsonf787a5f2010-09-24 16:02:42 +01004350 return 0;
4351
Chris Wilsone61e0f52018-02-21 09:56:36 +00004352 ret = i915_request_wait(target,
Chris Wilsone95433c2016-10-28 13:58:27 +01004353 I915_WAIT_INTERRUPTIBLE,
4354 MAX_SCHEDULE_TIMEOUT);
Chris Wilsone61e0f52018-02-21 09:56:36 +00004355 i915_request_put(target);
John Harrisonff865882014-11-24 18:49:28 +00004356
Chris Wilsone95433c2016-10-28 13:58:27 +01004357 return ret < 0 ? ret : 0;
Eric Anholt673a3942008-07-30 12:06:12 -07004358}
4359
Chris Wilson058d88c2016-08-15 10:49:06 +01004360struct i915_vma *
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02004361i915_gem_object_ggtt_pin(struct drm_i915_gem_object *obj,
4362 const struct i915_ggtt_view *view,
Chris Wilson91b2db62016-08-04 16:32:23 +01004363 u64 size,
Chris Wilson2ffffd02016-08-04 16:32:22 +01004364 u64 alignment,
4365 u64 flags)
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02004366{
Chris Wilsonad16d2e2016-10-13 09:55:04 +01004367 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
Chris Wilson82ad6442018-06-05 16:37:58 +01004368 struct i915_address_space *vm = &dev_priv->ggtt.vm;
Chris Wilson59bfa122016-08-04 16:32:31 +01004369 struct i915_vma *vma;
4370 int ret;
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03004371
Chris Wilson4c7d62c2016-10-28 13:58:32 +01004372 lockdep_assert_held(&obj->base.dev->struct_mutex);
4373
Chris Wilsonac87a6fd2018-02-20 13:42:05 +00004374 if (flags & PIN_MAPPABLE &&
4375 (!view || view->type == I915_GGTT_VIEW_NORMAL)) {
Chris Wilson43ae70d92017-10-09 09:44:01 +01004376 /* If the required space is larger than the available
4377 * aperture, we will not able to find a slot for the
4378 * object and unbinding the object now will be in
4379 * vain. Worse, doing so may cause us to ping-pong
4380 * the object in and out of the Global GTT and
4381 * waste a lot of cycles under the mutex.
4382 */
4383 if (obj->base.size > dev_priv->ggtt.mappable_end)
4384 return ERR_PTR(-E2BIG);
4385
4386 /* If NONBLOCK is set the caller is optimistically
4387 * trying to cache the full object within the mappable
4388 * aperture, and *must* have a fallback in place for
4389 * situations where we cannot bind the object. We
4390 * can be a little more lax here and use the fallback
4391 * more often to avoid costly migrations of ourselves
4392 * and other objects within the aperture.
4393 *
4394 * Half-the-aperture is used as a simple heuristic.
4395 * More interesting would to do search for a free
4396 * block prior to making the commitment to unbind.
4397 * That caters for the self-harm case, and with a
4398 * little more heuristics (e.g. NOFAULT, NOEVICT)
4399 * we could try to minimise harm to others.
4400 */
4401 if (flags & PIN_NONBLOCK &&
4402 obj->base.size > dev_priv->ggtt.mappable_end / 2)
4403 return ERR_PTR(-ENOSPC);
4404 }
4405
Chris Wilson718659a2017-01-16 15:21:28 +00004406 vma = i915_vma_instance(obj, vm, view);
Chris Wilsone0216b72017-01-19 19:26:57 +00004407 if (unlikely(IS_ERR(vma)))
Chris Wilson058d88c2016-08-15 10:49:06 +01004408 return vma;
Chris Wilson59bfa122016-08-04 16:32:31 +01004409
4410 if (i915_vma_misplaced(vma, size, alignment, flags)) {
Chris Wilson43ae70d92017-10-09 09:44:01 +01004411 if (flags & PIN_NONBLOCK) {
4412 if (i915_vma_is_pinned(vma) || i915_vma_is_active(vma))
4413 return ERR_PTR(-ENOSPC);
Chris Wilson59bfa122016-08-04 16:32:31 +01004414
Chris Wilson43ae70d92017-10-09 09:44:01 +01004415 if (flags & PIN_MAPPABLE &&
Chris Wilson944397f2017-01-09 16:16:11 +00004416 vma->fence_size > dev_priv->ggtt.mappable_end / 2)
Chris Wilsonad16d2e2016-10-13 09:55:04 +01004417 return ERR_PTR(-ENOSPC);
4418 }
4419
Chris Wilson59bfa122016-08-04 16:32:31 +01004420 WARN(i915_vma_is_pinned(vma),
4421 "bo is already pinned in ggtt with incorrect alignment:"
Chris Wilson05a20d02016-08-18 17:16:55 +01004422 " offset=%08x, req.alignment=%llx,"
4423 " req.map_and_fenceable=%d, vma->map_and_fenceable=%d\n",
4424 i915_ggtt_offset(vma), alignment,
Chris Wilson59bfa122016-08-04 16:32:31 +01004425 !!(flags & PIN_MAPPABLE),
Chris Wilson05a20d02016-08-18 17:16:55 +01004426 i915_vma_is_map_and_fenceable(vma));
Chris Wilson59bfa122016-08-04 16:32:31 +01004427 ret = i915_vma_unbind(vma);
4428 if (ret)
Chris Wilson058d88c2016-08-15 10:49:06 +01004429 return ERR_PTR(ret);
Chris Wilson59bfa122016-08-04 16:32:31 +01004430 }
4431
Chris Wilson058d88c2016-08-15 10:49:06 +01004432 ret = i915_vma_pin(vma, size, alignment, flags | PIN_GLOBAL);
4433 if (ret)
4434 return ERR_PTR(ret);
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02004435
Chris Wilson058d88c2016-08-15 10:49:06 +01004436 return vma;
Eric Anholt673a3942008-07-30 12:06:12 -07004437}
4438
Chris Wilsonedf6b762016-08-09 09:23:33 +01004439static __always_inline unsigned int __busy_read_flag(unsigned int id)
Chris Wilson3fdc13c2016-08-05 10:14:18 +01004440{
4441 /* Note that we could alias engines in the execbuf API, but
4442 * that would be very unwise as it prevents userspace from
4443 * fine control over engine selection. Ahem.
4444 *
4445 * This should be something like EXEC_MAX_ENGINE instead of
4446 * I915_NUM_ENGINES.
4447 */
4448 BUILD_BUG_ON(I915_NUM_ENGINES > 16);
4449 return 0x10000 << id;
4450}
4451
4452static __always_inline unsigned int __busy_write_id(unsigned int id)
4453{
Chris Wilson70cb4722016-08-09 18:08:25 +01004454 /* The uABI guarantees an active writer is also amongst the read
4455 * engines. This would be true if we accessed the activity tracking
4456 * under the lock, but as we perform the lookup of the object and
4457 * its activity locklessly we can not guarantee that the last_write
4458 * being active implies that we have set the same engine flag from
4459 * last_read - hence we always set both read and write busy for
4460 * last_write.
4461 */
4462 return id | __busy_read_flag(id);
Chris Wilson3fdc13c2016-08-05 10:14:18 +01004463}
4464
Chris Wilsonedf6b762016-08-09 09:23:33 +01004465static __always_inline unsigned int
Chris Wilsond07f0e52016-10-28 13:58:44 +01004466__busy_set_if_active(const struct dma_fence *fence,
Chris Wilson3fdc13c2016-08-05 10:14:18 +01004467 unsigned int (*flag)(unsigned int id))
4468{
Chris Wilsone61e0f52018-02-21 09:56:36 +00004469 struct i915_request *rq;
Chris Wilson12555012016-08-16 09:50:40 +01004470
Chris Wilsond07f0e52016-10-28 13:58:44 +01004471 /* We have to check the current hw status of the fence as the uABI
4472 * guarantees forward progress. We could rely on the idle worker
4473 * to eventually flush us, but to minimise latency just ask the
4474 * hardware.
4475 *
4476 * Note we only report on the status of native fences.
4477 */
4478 if (!dma_fence_is_i915(fence))
Chris Wilson12555012016-08-16 09:50:40 +01004479 return 0;
4480
Chris Wilsond07f0e52016-10-28 13:58:44 +01004481 /* opencode to_request() in order to avoid const warnings */
Chris Wilsone61e0f52018-02-21 09:56:36 +00004482 rq = container_of(fence, struct i915_request, fence);
4483 if (i915_request_completed(rq))
Chris Wilsond07f0e52016-10-28 13:58:44 +01004484 return 0;
4485
Chris Wilson1d39f282017-04-11 13:43:06 +01004486 return flag(rq->engine->uabi_id);
Chris Wilson3fdc13c2016-08-05 10:14:18 +01004487}
4488
Chris Wilsonedf6b762016-08-09 09:23:33 +01004489static __always_inline unsigned int
Chris Wilsond07f0e52016-10-28 13:58:44 +01004490busy_check_reader(const struct dma_fence *fence)
Chris Wilson3fdc13c2016-08-05 10:14:18 +01004491{
Chris Wilsond07f0e52016-10-28 13:58:44 +01004492 return __busy_set_if_active(fence, __busy_read_flag);
Chris Wilson3fdc13c2016-08-05 10:14:18 +01004493}
4494
Chris Wilsonedf6b762016-08-09 09:23:33 +01004495static __always_inline unsigned int
Chris Wilsond07f0e52016-10-28 13:58:44 +01004496busy_check_writer(const struct dma_fence *fence)
Chris Wilson3fdc13c2016-08-05 10:14:18 +01004497{
Chris Wilsond07f0e52016-10-28 13:58:44 +01004498 if (!fence)
4499 return 0;
4500
4501 return __busy_set_if_active(fence, __busy_write_id);
Chris Wilson3fdc13c2016-08-05 10:14:18 +01004502}
4503
Eric Anholt673a3942008-07-30 12:06:12 -07004504int
Eric Anholt673a3942008-07-30 12:06:12 -07004505i915_gem_busy_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00004506 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07004507{
4508 struct drm_i915_gem_busy *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00004509 struct drm_i915_gem_object *obj;
Chris Wilsond07f0e52016-10-28 13:58:44 +01004510 struct reservation_object_list *list;
4511 unsigned int seq;
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004512 int err;
Eric Anholt673a3942008-07-30 12:06:12 -07004513
Chris Wilsond07f0e52016-10-28 13:58:44 +01004514 err = -ENOENT;
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004515 rcu_read_lock();
4516 obj = i915_gem_object_lookup_rcu(file, args->handle);
Chris Wilsond07f0e52016-10-28 13:58:44 +01004517 if (!obj)
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004518 goto out;
Chris Wilsond07f0e52016-10-28 13:58:44 +01004519
4520 /* A discrepancy here is that we do not report the status of
4521 * non-i915 fences, i.e. even though we may report the object as idle,
4522 * a call to set-domain may still stall waiting for foreign rendering.
4523 * This also means that wait-ioctl may report an object as busy,
4524 * where busy-ioctl considers it idle.
4525 *
4526 * We trade the ability to warn of foreign fences to report on which
4527 * i915 engines are active for the object.
4528 *
4529 * Alternatively, we can trade that extra information on read/write
4530 * activity with
4531 * args->busy =
4532 * !reservation_object_test_signaled_rcu(obj->resv, true);
4533 * to report the overall busyness. This is what the wait-ioctl does.
4534 *
4535 */
4536retry:
4537 seq = raw_read_seqcount(&obj->resv->seq);
4538
4539 /* Translate the exclusive fence to the READ *and* WRITE engine */
4540 args->busy = busy_check_writer(rcu_dereference(obj->resv->fence_excl));
4541
4542 /* Translate shared fences to READ set of engines */
4543 list = rcu_dereference(obj->resv->fence);
4544 if (list) {
4545 unsigned int shared_count = list->shared_count, i;
4546
4547 for (i = 0; i < shared_count; ++i) {
4548 struct dma_fence *fence =
4549 rcu_dereference(list->shared[i]);
4550
4551 args->busy |= busy_check_reader(fence);
4552 }
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004553 }
Zou Nan haid1b851f2010-05-21 09:08:57 +08004554
Chris Wilsond07f0e52016-10-28 13:58:44 +01004555 if (args->busy && read_seqcount_retry(&obj->resv->seq, seq))
4556 goto retry;
Chris Wilson426960b2016-01-15 16:51:46 +00004557
Chris Wilsond07f0e52016-10-28 13:58:44 +01004558 err = 0;
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004559out:
4560 rcu_read_unlock();
4561 return err;
Eric Anholt673a3942008-07-30 12:06:12 -07004562}
4563
4564int
4565i915_gem_throttle_ioctl(struct drm_device *dev, void *data,
4566 struct drm_file *file_priv)
4567{
Akshay Joshi0206e352011-08-16 15:34:10 -04004568 return i915_gem_ring_throttle(dev, file_priv);
Eric Anholt673a3942008-07-30 12:06:12 -07004569}
4570
Chris Wilson3ef94da2009-09-14 16:50:29 +01004571int
4572i915_gem_madvise_ioctl(struct drm_device *dev, void *data,
4573 struct drm_file *file_priv)
4574{
Chris Wilsonfac5e232016-07-04 11:34:36 +01004575 struct drm_i915_private *dev_priv = to_i915(dev);
Chris Wilson3ef94da2009-09-14 16:50:29 +01004576 struct drm_i915_gem_madvise *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00004577 struct drm_i915_gem_object *obj;
Chris Wilson1233e2d2016-10-28 13:58:37 +01004578 int err;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004579
4580 switch (args->madv) {
4581 case I915_MADV_DONTNEED:
4582 case I915_MADV_WILLNEED:
4583 break;
4584 default:
4585 return -EINVAL;
4586 }
4587
Chris Wilson03ac0642016-07-20 13:31:51 +01004588 obj = i915_gem_object_lookup(file_priv, args->handle);
Chris Wilson1233e2d2016-10-28 13:58:37 +01004589 if (!obj)
4590 return -ENOENT;
4591
4592 err = mutex_lock_interruptible(&obj->mm.lock);
4593 if (err)
4594 goto out;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004595
Chris Wilsonf1fa4f42017-10-13 21:26:13 +01004596 if (i915_gem_object_has_pages(obj) &&
Chris Wilson3e510a82016-08-05 10:14:23 +01004597 i915_gem_object_is_tiled(obj) &&
Daniel Vetter656bfa32014-11-20 09:26:30 +01004598 dev_priv->quirks & QUIRK_PIN_SWIZZLED_PAGES) {
Chris Wilsonbc0629a2016-11-01 10:03:17 +00004599 if (obj->mm.madv == I915_MADV_WILLNEED) {
4600 GEM_BUG_ON(!obj->mm.quirked);
Chris Wilsona4f5ea62016-10-28 13:58:35 +01004601 __i915_gem_object_unpin_pages(obj);
Chris Wilsonbc0629a2016-11-01 10:03:17 +00004602 obj->mm.quirked = false;
4603 }
4604 if (args->madv == I915_MADV_WILLNEED) {
Chris Wilson2c3a3f42016-11-04 10:30:01 +00004605 GEM_BUG_ON(obj->mm.quirked);
Chris Wilsona4f5ea62016-10-28 13:58:35 +01004606 __i915_gem_object_pin_pages(obj);
Chris Wilsonbc0629a2016-11-01 10:03:17 +00004607 obj->mm.quirked = true;
4608 }
Daniel Vetter656bfa32014-11-20 09:26:30 +01004609 }
4610
Chris Wilsona4f5ea62016-10-28 13:58:35 +01004611 if (obj->mm.madv != __I915_MADV_PURGED)
4612 obj->mm.madv = args->madv;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004613
Chris Wilson6c085a72012-08-20 11:40:46 +02004614 /* if the object is no longer attached, discard its backing storage */
Chris Wilsonf1fa4f42017-10-13 21:26:13 +01004615 if (obj->mm.madv == I915_MADV_DONTNEED &&
4616 !i915_gem_object_has_pages(obj))
Chris Wilson2d7ef392009-09-20 23:13:10 +01004617 i915_gem_object_truncate(obj);
4618
Chris Wilsona4f5ea62016-10-28 13:58:35 +01004619 args->retained = obj->mm.madv != __I915_MADV_PURGED;
Chris Wilson1233e2d2016-10-28 13:58:37 +01004620 mutex_unlock(&obj->mm.lock);
Chris Wilsonbb6baf72009-09-22 14:24:13 +01004621
Chris Wilson1233e2d2016-10-28 13:58:37 +01004622out:
Chris Wilsonf8c417c2016-07-20 13:31:53 +01004623 i915_gem_object_put(obj);
Chris Wilson1233e2d2016-10-28 13:58:37 +01004624 return err;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004625}
4626
Chris Wilson5b8c8ae2016-11-16 19:07:04 +00004627static void
Chris Wilsone61e0f52018-02-21 09:56:36 +00004628frontbuffer_retire(struct i915_gem_active *active, struct i915_request *request)
Chris Wilson5b8c8ae2016-11-16 19:07:04 +00004629{
4630 struct drm_i915_gem_object *obj =
4631 container_of(active, typeof(*obj), frontbuffer_write);
4632
Chris Wilsond59b21e2017-02-22 11:40:49 +00004633 intel_fb_obj_flush(obj, ORIGIN_CS);
Chris Wilson5b8c8ae2016-11-16 19:07:04 +00004634}
4635
Chris Wilson37e680a2012-06-07 15:38:42 +01004636void i915_gem_object_init(struct drm_i915_gem_object *obj,
4637 const struct drm_i915_gem_object_ops *ops)
Chris Wilson0327d6b2012-08-11 15:41:06 +01004638{
Chris Wilson1233e2d2016-10-28 13:58:37 +01004639 mutex_init(&obj->mm.lock);
4640
Ben Widawsky2f633152013-07-17 12:19:03 -07004641 INIT_LIST_HEAD(&obj->vma_list);
Chris Wilsond1b48c12017-08-16 09:52:08 +01004642 INIT_LIST_HEAD(&obj->lut_list);
Chris Wilson8d9d5742015-04-07 16:20:38 +01004643 INIT_LIST_HEAD(&obj->batch_pool_link);
Chris Wilson0327d6b2012-08-11 15:41:06 +01004644
Chris Wilson37e680a2012-06-07 15:38:42 +01004645 obj->ops = ops;
4646
Chris Wilsond07f0e52016-10-28 13:58:44 +01004647 reservation_object_init(&obj->__builtin_resv);
4648 obj->resv = &obj->__builtin_resv;
4649
Chris Wilson50349242016-08-18 17:17:04 +01004650 obj->frontbuffer_ggtt_origin = ORIGIN_GTT;
Chris Wilson5b8c8ae2016-11-16 19:07:04 +00004651 init_request_active(&obj->frontbuffer_write, frontbuffer_retire);
Chris Wilsona4f5ea62016-10-28 13:58:35 +01004652
4653 obj->mm.madv = I915_MADV_WILLNEED;
4654 INIT_RADIX_TREE(&obj->mm.get_page.radix, GFP_KERNEL | __GFP_NOWARN);
4655 mutex_init(&obj->mm.get_page.lock);
Chris Wilson0327d6b2012-08-11 15:41:06 +01004656
Dave Gordonf19ec8c2016-07-04 11:34:37 +01004657 i915_gem_info_add_obj(to_i915(obj->base.dev), obj->base.size);
Chris Wilson0327d6b2012-08-11 15:41:06 +01004658}
4659
Chris Wilson37e680a2012-06-07 15:38:42 +01004660static const struct drm_i915_gem_object_ops i915_gem_object_ops = {
Tvrtko Ursulin3599a912016-11-01 14:44:10 +00004661 .flags = I915_GEM_OBJECT_HAS_STRUCT_PAGE |
4662 I915_GEM_OBJECT_IS_SHRINKABLE,
Chris Wilson7c55e2c2017-03-07 12:03:38 +00004663
Chris Wilson37e680a2012-06-07 15:38:42 +01004664 .get_pages = i915_gem_object_get_pages_gtt,
4665 .put_pages = i915_gem_object_put_pages_gtt,
Chris Wilson7c55e2c2017-03-07 12:03:38 +00004666
4667 .pwrite = i915_gem_object_pwrite_gtt,
Chris Wilson37e680a2012-06-07 15:38:42 +01004668};
4669
Matthew Auld465c4032017-10-06 23:18:14 +01004670static int i915_gem_object_create_shmem(struct drm_device *dev,
4671 struct drm_gem_object *obj,
4672 size_t size)
4673{
4674 struct drm_i915_private *i915 = to_i915(dev);
4675 unsigned long flags = VM_NORESERVE;
4676 struct file *filp;
4677
4678 drm_gem_private_object_init(dev, obj, size);
4679
4680 if (i915->mm.gemfs)
4681 filp = shmem_file_setup_with_mnt(i915->mm.gemfs, "i915", size,
4682 flags);
4683 else
4684 filp = shmem_file_setup("i915", size, flags);
4685
4686 if (IS_ERR(filp))
4687 return PTR_ERR(filp);
4688
4689 obj->filp = filp;
4690
4691 return 0;
4692}
4693
Chris Wilsonb4bcbe22016-10-18 13:02:49 +01004694struct drm_i915_gem_object *
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +00004695i915_gem_object_create(struct drm_i915_private *dev_priv, u64 size)
Daniel Vetterac52bc52010-04-09 19:05:06 +00004696{
Daniel Vetterc397b902010-04-09 19:05:07 +00004697 struct drm_i915_gem_object *obj;
Hugh Dickins5949eac2011-06-27 16:18:18 -07004698 struct address_space *mapping;
Chris Wilsonb8f55be2017-08-11 12:11:16 +01004699 unsigned int cache_level;
Daniel Vetter1a240d42012-11-29 22:18:51 +01004700 gfp_t mask;
Chris Wilsonfe3db792016-04-25 13:32:13 +01004701 int ret;
Daniel Vetterc397b902010-04-09 19:05:07 +00004702
Chris Wilsonb4bcbe22016-10-18 13:02:49 +01004703 /* There is a prevalence of the assumption that we fit the object's
4704 * page count inside a 32bit _signed_ variable. Let's document this and
4705 * catch if we ever need to fix it. In the meantime, if you do spot
4706 * such a local variable, please consider fixing!
4707 */
Tvrtko Ursulin7a3ee5d2017-03-30 17:31:30 +01004708 if (size >> PAGE_SHIFT > INT_MAX)
Chris Wilsonb4bcbe22016-10-18 13:02:49 +01004709 return ERR_PTR(-E2BIG);
4710
4711 if (overflows_type(size, obj->base.size))
4712 return ERR_PTR(-E2BIG);
4713
Tvrtko Ursulin187685c2016-12-01 14:16:36 +00004714 obj = i915_gem_object_alloc(dev_priv);
Daniel Vetterc397b902010-04-09 19:05:07 +00004715 if (obj == NULL)
Chris Wilsonfe3db792016-04-25 13:32:13 +01004716 return ERR_PTR(-ENOMEM);
Daniel Vetterc397b902010-04-09 19:05:07 +00004717
Matthew Auld465c4032017-10-06 23:18:14 +01004718 ret = i915_gem_object_create_shmem(&dev_priv->drm, &obj->base, size);
Chris Wilsonfe3db792016-04-25 13:32:13 +01004719 if (ret)
4720 goto fail;
Daniel Vetterc397b902010-04-09 19:05:07 +00004721
Chris Wilsonbed1ea92012-05-24 20:48:12 +01004722 mask = GFP_HIGHUSER | __GFP_RECLAIMABLE;
Jani Nikulac0f86832016-12-07 12:13:04 +02004723 if (IS_I965GM(dev_priv) || IS_I965G(dev_priv)) {
Chris Wilsonbed1ea92012-05-24 20:48:12 +01004724 /* 965gm cannot relocate objects above 4GiB. */
4725 mask &= ~__GFP_HIGHMEM;
4726 mask |= __GFP_DMA32;
4727 }
4728
Al Viro93c76a32015-12-04 23:45:44 -05004729 mapping = obj->base.filp->f_mapping;
Chris Wilsonbed1ea92012-05-24 20:48:12 +01004730 mapping_set_gfp_mask(mapping, mask);
Chris Wilson4846bf02017-06-09 12:03:46 +01004731 GEM_BUG_ON(!(mapping_gfp_mask(mapping) & __GFP_RECLAIM));
Hugh Dickins5949eac2011-06-27 16:18:18 -07004732
Chris Wilson37e680a2012-06-07 15:38:42 +01004733 i915_gem_object_init(obj, &i915_gem_object_ops);
Chris Wilson73aa8082010-09-30 11:46:12 +01004734
Christian Königc0a51fd2018-02-16 13:43:38 +01004735 obj->write_domain = I915_GEM_DOMAIN_CPU;
4736 obj->read_domains = I915_GEM_DOMAIN_CPU;
Daniel Vetterc397b902010-04-09 19:05:07 +00004737
Chris Wilsonb8f55be2017-08-11 12:11:16 +01004738 if (HAS_LLC(dev_priv))
Eugeni Dodonov3d29b842012-01-17 14:43:53 -02004739 /* On some devices, we can have the GPU use the LLC (the CPU
Eric Anholta1871112011-03-29 16:59:55 -07004740 * cache) for about a 10% performance improvement
4741 * compared to uncached. Graphics requests other than
4742 * display scanout are coherent with the CPU in
4743 * accessing this cache. This means in this mode we
4744 * don't need to clflush on the CPU side, and on the
4745 * GPU side we only need to flush internal caches to
4746 * get data visible to the CPU.
4747 *
4748 * However, we maintain the display planes as UC, and so
4749 * need to rebind when first used as such.
4750 */
Chris Wilsonb8f55be2017-08-11 12:11:16 +01004751 cache_level = I915_CACHE_LLC;
4752 else
4753 cache_level = I915_CACHE_NONE;
Eric Anholta1871112011-03-29 16:59:55 -07004754
Chris Wilsonb8f55be2017-08-11 12:11:16 +01004755 i915_gem_object_set_cache_coherency(obj, cache_level);
Chris Wilsone27ab732017-06-15 13:38:49 +01004756
Daniel Vetterd861e332013-07-24 23:25:03 +02004757 trace_i915_gem_object_create(obj);
4758
Chris Wilson05394f32010-11-08 19:18:58 +00004759 return obj;
Chris Wilsonfe3db792016-04-25 13:32:13 +01004760
4761fail:
4762 i915_gem_object_free(obj);
Chris Wilsonfe3db792016-04-25 13:32:13 +01004763 return ERR_PTR(ret);
Daniel Vetterac52bc52010-04-09 19:05:06 +00004764}
4765
Chris Wilson340fbd82014-05-22 09:16:52 +01004766static bool discard_backing_storage(struct drm_i915_gem_object *obj)
4767{
4768 /* If we are the last user of the backing storage (be it shmemfs
4769 * pages or stolen etc), we know that the pages are going to be
4770 * immediately released. In this case, we can then skip copying
4771 * back the contents from the GPU.
4772 */
4773
Chris Wilsona4f5ea62016-10-28 13:58:35 +01004774 if (obj->mm.madv != I915_MADV_WILLNEED)
Chris Wilson340fbd82014-05-22 09:16:52 +01004775 return false;
4776
4777 if (obj->base.filp == NULL)
4778 return true;
4779
4780 /* At first glance, this looks racy, but then again so would be
4781 * userspace racing mmap against close. However, the first external
4782 * reference to the filp can only be obtained through the
4783 * i915_gem_mmap_ioctl() which safeguards us against the user
4784 * acquiring such a reference whilst we are in the middle of
4785 * freeing the object.
4786 */
4787 return atomic_long_read(&obj->base.filp->f_count) == 1;
4788}
4789
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004790static void __i915_gem_free_objects(struct drm_i915_private *i915,
4791 struct llist_node *freed)
Chris Wilsonbe726152010-07-23 23:18:50 +01004792{
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004793 struct drm_i915_gem_object *obj, *on;
Chris Wilsonbe726152010-07-23 23:18:50 +01004794
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004795 intel_runtime_pm_get(i915);
Chris Wilsoncc731f52017-10-13 21:26:21 +01004796 llist_for_each_entry_safe(obj, on, freed, freed) {
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004797 struct i915_vma *vma, *vn;
Paulo Zanonif65c9162013-11-27 18:20:34 -02004798
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004799 trace_i915_gem_object_destroy(obj);
4800
Chris Wilsoncc731f52017-10-13 21:26:21 +01004801 mutex_lock(&i915->drm.struct_mutex);
4802
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004803 GEM_BUG_ON(i915_gem_object_is_active(obj));
4804 list_for_each_entry_safe(vma, vn,
4805 &obj->vma_list, obj_link) {
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004806 GEM_BUG_ON(i915_vma_is_active(vma));
4807 vma->flags &= ~I915_VMA_PIN_MASK;
Chris Wilson3365e222018-05-03 20:51:14 +01004808 i915_vma_destroy(vma);
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004809 }
Chris Wilsondb6c2b42016-11-01 11:54:00 +00004810 GEM_BUG_ON(!list_empty(&obj->vma_list));
4811 GEM_BUG_ON(!RB_EMPTY_ROOT(&obj->vma_tree));
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004812
Chris Wilsonf2123812017-10-16 12:40:37 +01004813 /* This serializes freeing with the shrinker. Since the free
4814 * is delayed, first by RCU then by the workqueue, we want the
4815 * shrinker to be able to free pages of unreferenced objects,
4816 * or else we may oom whilst there are plenty of deferred
4817 * freed objects.
4818 */
4819 if (i915_gem_object_has_pages(obj)) {
4820 spin_lock(&i915->mm.obj_lock);
4821 list_del_init(&obj->mm.link);
4822 spin_unlock(&i915->mm.obj_lock);
4823 }
4824
Chris Wilsoncc731f52017-10-13 21:26:21 +01004825 mutex_unlock(&i915->drm.struct_mutex);
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004826
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004827 GEM_BUG_ON(obj->bind_count);
Chris Wilsona65adaf2017-10-09 09:43:57 +01004828 GEM_BUG_ON(obj->userfault_count);
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004829 GEM_BUG_ON(atomic_read(&obj->frontbuffer_bits));
Chris Wilson67b48042017-08-22 12:05:16 +01004830 GEM_BUG_ON(!list_empty(&obj->lut_list));
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004831
4832 if (obj->ops->release)
4833 obj->ops->release(obj);
4834
4835 if (WARN_ON(i915_gem_object_has_pinned_pages(obj)))
4836 atomic_set(&obj->mm.pages_pin_count, 0);
Chris Wilson548625e2016-11-01 12:11:34 +00004837 __i915_gem_object_put_pages(obj, I915_MM_NORMAL);
Chris Wilsonf1fa4f42017-10-13 21:26:13 +01004838 GEM_BUG_ON(i915_gem_object_has_pages(obj));
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004839
4840 if (obj->base.import_attach)
4841 drm_prime_gem_destroy(&obj->base, NULL);
4842
Chris Wilsond07f0e52016-10-28 13:58:44 +01004843 reservation_object_fini(&obj->__builtin_resv);
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004844 drm_gem_object_release(&obj->base);
4845 i915_gem_info_remove_obj(i915, obj->base.size);
4846
4847 kfree(obj->bit_17);
4848 i915_gem_object_free(obj);
Chris Wilsoncc731f52017-10-13 21:26:21 +01004849
Chris Wilsonc9c704712018-02-19 22:06:31 +00004850 GEM_BUG_ON(!atomic_read(&i915->mm.free_count));
4851 atomic_dec(&i915->mm.free_count);
4852
Chris Wilsoncc731f52017-10-13 21:26:21 +01004853 if (on)
4854 cond_resched();
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004855 }
Chris Wilsoncc731f52017-10-13 21:26:21 +01004856 intel_runtime_pm_put(i915);
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004857}
4858
4859static void i915_gem_flush_free_objects(struct drm_i915_private *i915)
4860{
4861 struct llist_node *freed;
4862
Chris Wilson87701b42017-10-13 21:26:20 +01004863 /* Free the oldest, most stale object to keep the free_list short */
4864 freed = NULL;
4865 if (!llist_empty(&i915->mm.free_list)) { /* quick test for hotpath */
4866 /* Only one consumer of llist_del_first() allowed */
4867 spin_lock(&i915->mm.free_lock);
4868 freed = llist_del_first(&i915->mm.free_list);
4869 spin_unlock(&i915->mm.free_lock);
4870 }
4871 if (unlikely(freed)) {
4872 freed->next = NULL;
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004873 __i915_gem_free_objects(i915, freed);
Chris Wilson87701b42017-10-13 21:26:20 +01004874 }
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004875}
4876
4877static void __i915_gem_free_work(struct work_struct *work)
4878{
4879 struct drm_i915_private *i915 =
4880 container_of(work, struct drm_i915_private, mm.free_work);
4881 struct llist_node *freed;
Chris Wilson26e12f82011-03-20 11:20:19 +00004882
Chris Wilson2ef1e722018-01-15 20:57:59 +00004883 /*
4884 * All file-owned VMA should have been released by this point through
Chris Wilsonb1f788c2016-08-04 07:52:45 +01004885 * i915_gem_close_object(), or earlier by i915_gem_context_close().
4886 * However, the object may also be bound into the global GTT (e.g.
4887 * older GPUs without per-process support, or for direct access through
4888 * the GTT either for the user or for scanout). Those VMA still need to
4889 * unbound now.
4890 */
Chris Wilson1488fc02012-04-24 15:47:31 +01004891
Chris Wilsonf991c492017-11-06 11:15:08 +00004892 spin_lock(&i915->mm.free_lock);
Chris Wilson5ad08be2017-04-07 11:25:51 +01004893 while ((freed = llist_del_all(&i915->mm.free_list))) {
Chris Wilsonf991c492017-11-06 11:15:08 +00004894 spin_unlock(&i915->mm.free_lock);
4895
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004896 __i915_gem_free_objects(i915, freed);
Chris Wilson5ad08be2017-04-07 11:25:51 +01004897 if (need_resched())
Chris Wilsonf991c492017-11-06 11:15:08 +00004898 return;
4899
4900 spin_lock(&i915->mm.free_lock);
Chris Wilson5ad08be2017-04-07 11:25:51 +01004901 }
Chris Wilsonf991c492017-11-06 11:15:08 +00004902 spin_unlock(&i915->mm.free_lock);
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004903}
4904
4905static void __i915_gem_free_object_rcu(struct rcu_head *head)
4906{
4907 struct drm_i915_gem_object *obj =
4908 container_of(head, typeof(*obj), rcu);
4909 struct drm_i915_private *i915 = to_i915(obj->base.dev);
4910
Chris Wilson2ef1e722018-01-15 20:57:59 +00004911 /*
4912 * Since we require blocking on struct_mutex to unbind the freed
4913 * object from the GPU before releasing resources back to the
4914 * system, we can not do that directly from the RCU callback (which may
4915 * be a softirq context), but must instead then defer that work onto a
4916 * kthread. We use the RCU callback rather than move the freed object
4917 * directly onto the work queue so that we can mix between using the
4918 * worker and performing frees directly from subsequent allocations for
4919 * crude but effective memory throttling.
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004920 */
4921 if (llist_add(&obj->freed, &i915->mm.free_list))
Chris Wilsonbeacbd12018-01-15 12:28:45 +00004922 queue_work(i915->wq, &i915->mm.free_work);
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004923}
4924
4925void i915_gem_free_object(struct drm_gem_object *gem_obj)
4926{
4927 struct drm_i915_gem_object *obj = to_intel_bo(gem_obj);
4928
Chris Wilsonbc0629a2016-11-01 10:03:17 +00004929 if (obj->mm.quirked)
4930 __i915_gem_object_unpin_pages(obj);
4931
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004932 if (discard_backing_storage(obj))
4933 obj->mm.madv = I915_MADV_DONTNEED;
Daniel Vettera071fa02014-06-18 23:28:09 +02004934
Chris Wilson2ef1e722018-01-15 20:57:59 +00004935 /*
4936 * Before we free the object, make sure any pure RCU-only
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004937 * read-side critical sections are complete, e.g.
4938 * i915_gem_busy_ioctl(). For the corresponding synchronized
4939 * lookup see i915_gem_object_lookup_rcu().
4940 */
Chris Wilsonc9c704712018-02-19 22:06:31 +00004941 atomic_inc(&to_i915(obj->base.dev)->mm.free_count);
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004942 call_rcu(&obj->rcu, __i915_gem_free_object_rcu);
Chris Wilsonbe726152010-07-23 23:18:50 +01004943}
4944
Chris Wilsonf8a7fde2016-10-28 13:58:29 +01004945void __i915_gem_object_release_unless_active(struct drm_i915_gem_object *obj)
4946{
4947 lockdep_assert_held(&obj->base.dev->struct_mutex);
4948
Chris Wilsond1b48c12017-08-16 09:52:08 +01004949 if (!i915_gem_object_has_active_reference(obj) &&
4950 i915_gem_object_is_active(obj))
Chris Wilsonf8a7fde2016-10-28 13:58:29 +01004951 i915_gem_object_set_active_reference(obj);
4952 else
4953 i915_gem_object_put(obj);
4954}
4955
Chris Wilson24145512017-01-24 11:01:35 +00004956void i915_gem_sanitize(struct drm_i915_private *i915)
4957{
Chris Wilsonc3160da2018-05-31 09:22:45 +01004958 struct intel_engine_cs *engine;
4959 enum intel_engine_id id;
4960
4961 GEM_TRACE("\n");
4962
Chris Wilson4dfacb02018-05-31 09:22:43 +01004963 mutex_lock(&i915->drm.struct_mutex);
Chris Wilsonc3160da2018-05-31 09:22:45 +01004964
4965 intel_runtime_pm_get(i915);
4966 intel_uncore_forcewake_get(i915, FORCEWAKE_ALL);
4967
4968 /*
4969 * As we have just resumed the machine and woken the device up from
4970 * deep PCI sleep (presumably D3_cold), assume the HW has been reset
4971 * back to defaults, recovering from whatever wedged state we left it
4972 * in and so worth trying to use the device once more.
4973 */
Chris Wilson4dfacb02018-05-31 09:22:43 +01004974 if (i915_terminally_wedged(&i915->gpu_error))
Chris Wilsonf36325f2017-08-26 12:09:34 +01004975 i915_gem_unset_wedged(i915);
Chris Wilsonf36325f2017-08-26 12:09:34 +01004976
Chris Wilson24145512017-01-24 11:01:35 +00004977 /*
4978 * If we inherit context state from the BIOS or earlier occupants
4979 * of the GPU, the GPU may be in an inconsistent state when we
4980 * try to take over. The only way to remove the earlier state
4981 * is by resetting. However, resetting on earlier gen is tricky as
4982 * it may impact the display and we are uncertain about the stability
Joonas Lahtinenea117b82017-04-28 10:53:38 +03004983 * of the reset, so this could be applied to even earlier gen.
Chris Wilson24145512017-01-24 11:01:35 +00004984 */
Daniele Ceraolo Spurioce1599a2018-02-07 13:24:40 -08004985 if (INTEL_GEN(i915) >= 5 && intel_has_gpu_reset(i915))
4986 WARN_ON(intel_gpu_reset(i915, ALL_ENGINES));
Chris Wilson4dfacb02018-05-31 09:22:43 +01004987
Chris Wilsonc3160da2018-05-31 09:22:45 +01004988 /* Reset the submission backend after resume as well as the GPU reset */
4989 for_each_engine(engine, i915, id) {
4990 if (engine->reset.reset)
4991 engine->reset.reset(engine, NULL);
4992 }
4993
4994 intel_uncore_forcewake_put(i915, FORCEWAKE_ALL);
4995 intel_runtime_pm_put(i915);
4996
Chris Wilson4dfacb02018-05-31 09:22:43 +01004997 i915_gem_contexts_lost(i915);
4998 mutex_unlock(&i915->drm.struct_mutex);
Chris Wilson24145512017-01-24 11:01:35 +00004999}
5000
Tvrtko Ursulinbf9e8422016-12-01 14:16:38 +00005001int i915_gem_suspend(struct drm_i915_private *dev_priv)
Eric Anholt673a3942008-07-30 12:06:12 -07005002{
Tvrtko Ursulinbf9e8422016-12-01 14:16:38 +00005003 struct drm_device *dev = &dev_priv->drm;
Chris Wilsondcff85c2016-08-05 10:14:11 +01005004 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07005005
Chris Wilson09a4c022018-05-24 09:11:35 +01005006 GEM_TRACE("\n");
5007
Chris Wilsonc998e8a2017-03-02 08:30:29 +00005008 intel_runtime_pm_get(dev_priv);
Chris Wilson54b4f682016-07-21 21:16:19 +01005009 intel_suspend_gt_powersave(dev_priv);
5010
Chris Wilson45c5f202013-10-16 11:50:01 +01005011 mutex_lock(&dev->struct_mutex);
Chris Wilson5ab57c72016-07-15 14:56:20 +01005012
5013 /* We have to flush all the executing contexts to main memory so
5014 * that they can saved in the hibernation image. To ensure the last
5015 * context image is coherent, we have to switch away from it. That
5016 * leaves the dev_priv->kernel_context still active when
5017 * we actually suspend, and its image in memory may not match the GPU
5018 * state. Fortunately, the kernel_context is disposable and we do
5019 * not rely on its state.
5020 */
Chris Wilsonecf73eb2017-11-30 10:29:51 +00005021 if (!i915_terminally_wedged(&dev_priv->gpu_error)) {
5022 ret = i915_gem_switch_to_kernel_context(dev_priv);
5023 if (ret)
5024 goto err_unlock;
Chris Wilson5ab57c72016-07-15 14:56:20 +01005025
Chris Wilsonecf73eb2017-11-30 10:29:51 +00005026 ret = i915_gem_wait_for_idle(dev_priv,
5027 I915_WAIT_INTERRUPTIBLE |
Chris Wilson06060352018-05-31 09:22:44 +01005028 I915_WAIT_LOCKED |
5029 I915_WAIT_FOR_IDLE_BOOST);
Chris Wilsonecf73eb2017-11-30 10:29:51 +00005030 if (ret && ret != -EIO)
5031 goto err_unlock;
Chris Wilsonf7403342013-09-13 23:57:04 +01005032
Chris Wilsonecf73eb2017-11-30 10:29:51 +00005033 assert_kernel_context_is_current(dev_priv);
5034 }
Chris Wilson45c5f202013-10-16 11:50:01 +01005035 mutex_unlock(&dev->struct_mutex);
5036
Michal Wajdeczko7cfca4a2018-03-02 11:15:49 +00005037 intel_uc_suspend(dev_priv);
Sagar Arun Kamble63987bf2017-04-05 15:51:50 +05305038
Chris Wilson737b1502015-01-26 18:03:03 +02005039 cancel_delayed_work_sync(&dev_priv->gpu_error.hangcheck_work);
Chris Wilson67d97da2016-07-04 08:08:31 +01005040 cancel_delayed_work_sync(&dev_priv->gt.retire_work);
Chris Wilsonbdeb9782016-12-23 14:57:56 +00005041
5042 /* As the idle_work is rearming if it detects a race, play safe and
5043 * repeat the flush until it is definitely idle.
5044 */
Chris Wilson7c262402017-10-06 11:40:38 +01005045 drain_delayed_work(&dev_priv->gt.idle_work);
Chris Wilsonbdeb9782016-12-23 14:57:56 +00005046
Chris Wilsonbdcf1202014-11-25 11:56:33 +00005047 /* Assert that we sucessfully flushed all the work and
5048 * reset the GPU back to its idle, low power state.
5049 */
Chris Wilson67d97da2016-07-04 08:08:31 +01005050 WARN_ON(dev_priv->gt.awake);
Chris Wilsonfc692bd2017-08-26 12:09:35 +01005051 if (WARN_ON(!intel_engines_are_idle(dev_priv)))
5052 i915_gem_set_wedged(dev_priv); /* no hope, discard everything */
Chris Wilsonbdcf1202014-11-25 11:56:33 +00005053
Chris Wilsonec92ad02018-05-31 09:22:46 +01005054 intel_runtime_pm_put(dev_priv);
5055 return 0;
5056
5057err_unlock:
5058 mutex_unlock(&dev->struct_mutex);
5059 intel_runtime_pm_put(dev_priv);
5060 return ret;
5061}
5062
5063void i915_gem_suspend_late(struct drm_i915_private *i915)
5064{
Chris Wilson9776f472018-06-01 15:41:24 +01005065 struct drm_i915_gem_object *obj;
5066 struct list_head *phases[] = {
5067 &i915->mm.unbound_list,
5068 &i915->mm.bound_list,
5069 NULL
5070 }, **phase;
5071
Imre Deak1c777c52016-10-12 17:46:37 +03005072 /*
5073 * Neither the BIOS, ourselves or any other kernel
5074 * expects the system to be in execlists mode on startup,
5075 * so we need to reset the GPU back to legacy mode. And the only
5076 * known way to disable logical contexts is through a GPU reset.
5077 *
5078 * So in order to leave the system in a known default configuration,
5079 * always reset the GPU upon unload and suspend. Afterwards we then
5080 * clean up the GEM state tracking, flushing off the requests and
5081 * leaving the system in a known idle state.
5082 *
5083 * Note that is of the upmost importance that the GPU is idle and
5084 * all stray writes are flushed *before* we dismantle the backing
5085 * storage for the pinned objects.
5086 *
5087 * However, since we are uncertain that resetting the GPU on older
5088 * machines is a good idea, we don't - just in case it leaves the
5089 * machine in an unusable condition.
5090 */
Chris Wilsoncad99462017-08-26 12:09:33 +01005091
Chris Wilson9776f472018-06-01 15:41:24 +01005092 mutex_lock(&i915->drm.struct_mutex);
5093 for (phase = phases; *phase; phase++) {
5094 list_for_each_entry(obj, *phase, mm.link)
5095 WARN_ON(i915_gem_object_set_to_gtt_domain(obj, false));
5096 }
5097 mutex_unlock(&i915->drm.struct_mutex);
5098
Chris Wilsonec92ad02018-05-31 09:22:46 +01005099 intel_uc_sanitize(i915);
5100 i915_gem_sanitize(i915);
Eric Anholt673a3942008-07-30 12:06:12 -07005101}
5102
Chris Wilson37cd3302017-11-12 11:27:38 +00005103void i915_gem_resume(struct drm_i915_private *i915)
Chris Wilson5ab57c72016-07-15 14:56:20 +01005104{
Chris Wilson4dfacb02018-05-31 09:22:43 +01005105 GEM_TRACE("\n");
5106
Chris Wilson37cd3302017-11-12 11:27:38 +00005107 WARN_ON(i915->gt.awake);
Chris Wilson5ab57c72016-07-15 14:56:20 +01005108
Chris Wilson37cd3302017-11-12 11:27:38 +00005109 mutex_lock(&i915->drm.struct_mutex);
5110 intel_uncore_forcewake_get(i915, FORCEWAKE_ALL);
Imre Deak31ab49a2016-11-07 11:20:05 +02005111
Chris Wilson37cd3302017-11-12 11:27:38 +00005112 i915_gem_restore_gtt_mappings(i915);
5113 i915_gem_restore_fences(i915);
Chris Wilson5ab57c72016-07-15 14:56:20 +01005114
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00005115 /*
5116 * As we didn't flush the kernel context before suspend, we cannot
Chris Wilson5ab57c72016-07-15 14:56:20 +01005117 * guarantee that the context image is complete. So let's just reset
5118 * it and start again.
5119 */
Chris Wilson37cd3302017-11-12 11:27:38 +00005120 i915->gt.resume(i915);
Chris Wilson5ab57c72016-07-15 14:56:20 +01005121
Chris Wilson37cd3302017-11-12 11:27:38 +00005122 if (i915_gem_init_hw(i915))
5123 goto err_wedged;
5124
Michal Wajdeczko7cfca4a2018-03-02 11:15:49 +00005125 intel_uc_resume(i915);
Chris Wilson7469c622017-11-14 13:03:00 +00005126
Chris Wilson37cd3302017-11-12 11:27:38 +00005127 /* Always reload a context for powersaving. */
5128 if (i915_gem_switch_to_kernel_context(i915))
5129 goto err_wedged;
5130
5131out_unlock:
5132 intel_uncore_forcewake_put(i915, FORCEWAKE_ALL);
5133 mutex_unlock(&i915->drm.struct_mutex);
5134 return;
5135
5136err_wedged:
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00005137 if (!i915_terminally_wedged(&i915->gpu_error)) {
5138 DRM_ERROR("failed to re-initialize GPU, declaring wedged!\n");
5139 i915_gem_set_wedged(i915);
5140 }
Chris Wilson37cd3302017-11-12 11:27:38 +00005141 goto out_unlock;
Chris Wilson5ab57c72016-07-15 14:56:20 +01005142}
5143
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00005144void i915_gem_init_swizzling(struct drm_i915_private *dev_priv)
Daniel Vetterf691e2f2012-02-02 09:58:12 +01005145{
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00005146 if (INTEL_GEN(dev_priv) < 5 ||
Daniel Vetterf691e2f2012-02-02 09:58:12 +01005147 dev_priv->mm.bit_6_swizzle_x == I915_BIT_6_SWIZZLE_NONE)
5148 return;
5149
5150 I915_WRITE(DISP_ARB_CTL, I915_READ(DISP_ARB_CTL) |
5151 DISP_TILE_SURFACE_SWIZZLING);
5152
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01005153 if (IS_GEN5(dev_priv))
Daniel Vetter11782b02012-01-31 16:47:55 +01005154 return;
5155
Daniel Vetterf691e2f2012-02-02 09:58:12 +01005156 I915_WRITE(TILECTL, I915_READ(TILECTL) | TILECTL_SWZCTL);
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01005157 if (IS_GEN6(dev_priv))
Daniel Vetter6b26c862012-04-24 14:04:12 +02005158 I915_WRITE(ARB_MODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_SNB));
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01005159 else if (IS_GEN7(dev_priv))
Daniel Vetter6b26c862012-04-24 14:04:12 +02005160 I915_WRITE(ARB_MODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_IVB));
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01005161 else if (IS_GEN8(dev_priv))
Ben Widawsky31a53362013-11-02 21:07:04 -07005162 I915_WRITE(GAMTARBMODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_BDW));
Ben Widawsky8782e262012-12-18 10:31:23 -08005163 else
5164 BUG();
Daniel Vetterf691e2f2012-02-02 09:58:12 +01005165}
Daniel Vettere21af882012-02-09 20:53:27 +01005166
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01005167static void init_unused_ring(struct drm_i915_private *dev_priv, u32 base)
Ville Syrjälä81e7f202014-08-15 01:21:55 +03005168{
Ville Syrjälä81e7f202014-08-15 01:21:55 +03005169 I915_WRITE(RING_CTL(base), 0);
5170 I915_WRITE(RING_HEAD(base), 0);
5171 I915_WRITE(RING_TAIL(base), 0);
5172 I915_WRITE(RING_START(base), 0);
5173}
5174
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01005175static void init_unused_rings(struct drm_i915_private *dev_priv)
Ville Syrjälä81e7f202014-08-15 01:21:55 +03005176{
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01005177 if (IS_I830(dev_priv)) {
5178 init_unused_ring(dev_priv, PRB1_BASE);
5179 init_unused_ring(dev_priv, SRB0_BASE);
5180 init_unused_ring(dev_priv, SRB1_BASE);
5181 init_unused_ring(dev_priv, SRB2_BASE);
5182 init_unused_ring(dev_priv, SRB3_BASE);
5183 } else if (IS_GEN2(dev_priv)) {
5184 init_unused_ring(dev_priv, SRB0_BASE);
5185 init_unused_ring(dev_priv, SRB1_BASE);
5186 } else if (IS_GEN3(dev_priv)) {
5187 init_unused_ring(dev_priv, PRB1_BASE);
5188 init_unused_ring(dev_priv, PRB2_BASE);
Ville Syrjälä81e7f202014-08-15 01:21:55 +03005189 }
5190}
5191
Chris Wilson20a8a742017-02-08 14:30:31 +00005192static int __i915_gem_restart_engines(void *data)
Ben Widawsky4fc7c972013-02-08 11:49:24 -08005193{
Chris Wilson20a8a742017-02-08 14:30:31 +00005194 struct drm_i915_private *i915 = data;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00005195 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05305196 enum intel_engine_id id;
Chris Wilson20a8a742017-02-08 14:30:31 +00005197 int err;
5198
5199 for_each_engine(engine, i915, id) {
5200 err = engine->init_hw(engine);
Chris Wilson8177e112018-02-07 11:15:45 +00005201 if (err) {
5202 DRM_ERROR("Failed to restart %s (%d)\n",
5203 engine->name, err);
Chris Wilson20a8a742017-02-08 14:30:31 +00005204 return err;
Chris Wilson8177e112018-02-07 11:15:45 +00005205 }
Chris Wilson20a8a742017-02-08 14:30:31 +00005206 }
5207
5208 return 0;
5209}
5210
5211int i915_gem_init_hw(struct drm_i915_private *dev_priv)
5212{
Chris Wilsond200cda2016-04-28 09:56:44 +01005213 int ret;
Ben Widawsky4fc7c972013-02-08 11:49:24 -08005214
Chris Wilsonde867c22016-10-25 13:16:02 +01005215 dev_priv->gt.last_init_time = ktime_get();
5216
Chris Wilson5e4f5182015-02-13 14:35:59 +00005217 /* Double layer security blanket, see i915_gem_init() */
5218 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
5219
Tvrtko Ursulin0031fb92016-11-04 14:42:44 +00005220 if (HAS_EDRAM(dev_priv) && INTEL_GEN(dev_priv) < 9)
Ben Widawsky05e21cc2013-07-04 11:02:04 -07005221 I915_WRITE(HSW_IDICR, I915_READ(HSW_IDICR) | IDIHASHMSK(0xf));
Ben Widawsky4fc7c972013-02-08 11:49:24 -08005222
Tvrtko Ursulin772c2a52016-10-13 11:03:01 +01005223 if (IS_HASWELL(dev_priv))
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01005224 I915_WRITE(MI_PREDICATE_RESULT_2, IS_HSW_GT3(dev_priv) ?
Ville Syrjälä0bf21342013-11-29 14:56:12 +02005225 LOWER_SLICE_ENABLED : LOWER_SLICE_DISABLED);
Rodrigo Vivi94353732013-08-28 16:45:46 -03005226
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01005227 if (HAS_PCH_NOP(dev_priv)) {
Tvrtko Ursulinfd6b8f42016-10-14 10:13:06 +01005228 if (IS_IVYBRIDGE(dev_priv)) {
Daniel Vetter6ba844b2014-01-22 23:39:30 +01005229 u32 temp = I915_READ(GEN7_MSG_CTL);
5230 temp &= ~(WAIT_FOR_PCH_FLR_ACK | WAIT_FOR_PCH_RESET_ACK);
5231 I915_WRITE(GEN7_MSG_CTL, temp);
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00005232 } else if (INTEL_GEN(dev_priv) >= 7) {
Daniel Vetter6ba844b2014-01-22 23:39:30 +01005233 u32 temp = I915_READ(HSW_NDE_RSTWRN_OPT);
5234 temp &= ~RESET_PCH_HANDSHAKE_ENABLE;
5235 I915_WRITE(HSW_NDE_RSTWRN_OPT, temp);
5236 }
Ben Widawsky88a2b2a2013-04-05 13:12:43 -07005237 }
5238
Oscar Mateo59b449d2018-04-10 09:12:47 -07005239 intel_gt_workarounds_apply(dev_priv);
5240
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00005241 i915_gem_init_swizzling(dev_priv);
Ben Widawsky4fc7c972013-02-08 11:49:24 -08005242
Daniel Vetterd5abdfd2014-11-20 09:45:19 +01005243 /*
5244 * At least 830 can leave some of the unused rings
5245 * "active" (ie. head != tail) after resume which
5246 * will prevent c3 entry. Makes sure all unused rings
5247 * are totally idle.
5248 */
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01005249 init_unused_rings(dev_priv);
Daniel Vetterd5abdfd2014-11-20 09:45:19 +01005250
Dave Gordoned54c1a2016-01-19 19:02:54 +00005251 BUG_ON(!dev_priv->kernel_context);
Chris Wilson6f74b362017-10-15 15:37:25 +01005252 if (i915_terminally_wedged(&dev_priv->gpu_error)) {
5253 ret = -EIO;
5254 goto out;
5255 }
John Harrison90638cc2015-05-29 17:43:37 +01005256
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00005257 ret = i915_ppgtt_init_hw(dev_priv);
John Harrison4ad2fd82015-06-18 13:11:20 +01005258 if (ret) {
Chris Wilson8177e112018-02-07 11:15:45 +00005259 DRM_ERROR("Enabling PPGTT failed (%d)\n", ret);
John Harrison4ad2fd82015-06-18 13:11:20 +01005260 goto out;
5261 }
5262
Jackie Lif08e2032018-03-13 17:32:53 -07005263 ret = intel_wopcm_init_hw(&dev_priv->wopcm);
5264 if (ret) {
5265 DRM_ERROR("Enabling WOPCM failed (%d)\n", ret);
5266 goto out;
5267 }
5268
Michał Winiarski9bdc3572017-10-25 18:25:19 +01005269 /* We can't enable contexts until all firmware is loaded */
5270 ret = intel_uc_init_hw(dev_priv);
Chris Wilson8177e112018-02-07 11:15:45 +00005271 if (ret) {
5272 DRM_ERROR("Enabling uc failed (%d)\n", ret);
Michał Winiarski9bdc3572017-10-25 18:25:19 +01005273 goto out;
Chris Wilson8177e112018-02-07 11:15:45 +00005274 }
Michał Winiarski9bdc3572017-10-25 18:25:19 +01005275
Tvrtko Ursulinbf9e8422016-12-01 14:16:38 +00005276 intel_mocs_init_l3cc_table(dev_priv);
Peter Antoine0ccdacf2016-04-13 15:03:25 +01005277
Chris Wilson136109c2017-11-02 13:14:30 +00005278 /* Only when the HW is re-initialised, can we replay the requests */
5279 ret = __i915_gem_restart_engines(dev_priv);
Michal Wajdeczkob96f6eb2018-06-05 12:24:43 +00005280 if (ret)
5281 goto cleanup_uc;
Chris Wilson5e4f5182015-02-13 14:35:59 +00005282out:
5283 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Ben Widawsky2fa48d82013-12-06 14:11:04 -08005284 return ret;
Michal Wajdeczkob96f6eb2018-06-05 12:24:43 +00005285
5286cleanup_uc:
5287 intel_uc_fini_hw(dev_priv);
5288 goto out;
Zou Nan hai8187a2b2010-05-21 09:08:55 +08005289}
5290
Chris Wilsond2b4b972017-11-10 14:26:33 +00005291static int __intel_engines_record_defaults(struct drm_i915_private *i915)
5292{
5293 struct i915_gem_context *ctx;
5294 struct intel_engine_cs *engine;
5295 enum intel_engine_id id;
5296 int err;
5297
5298 /*
5299 * As we reset the gpu during very early sanitisation, the current
5300 * register state on the GPU should reflect its defaults values.
5301 * We load a context onto the hw (with restore-inhibit), then switch
5302 * over to a second context to save that default register state. We
5303 * can then prime every new context with that state so they all start
5304 * from the same default HW values.
5305 */
5306
5307 ctx = i915_gem_context_create_kernel(i915, 0);
5308 if (IS_ERR(ctx))
5309 return PTR_ERR(ctx);
5310
5311 for_each_engine(engine, i915, id) {
Chris Wilsone61e0f52018-02-21 09:56:36 +00005312 struct i915_request *rq;
Chris Wilsond2b4b972017-11-10 14:26:33 +00005313
Chris Wilsone61e0f52018-02-21 09:56:36 +00005314 rq = i915_request_alloc(engine, ctx);
Chris Wilsond2b4b972017-11-10 14:26:33 +00005315 if (IS_ERR(rq)) {
5316 err = PTR_ERR(rq);
5317 goto out_ctx;
5318 }
5319
Chris Wilson3fef5cd2017-11-20 10:20:02 +00005320 err = 0;
Chris Wilsond2b4b972017-11-10 14:26:33 +00005321 if (engine->init_context)
5322 err = engine->init_context(rq);
5323
Chris Wilsone61e0f52018-02-21 09:56:36 +00005324 __i915_request_add(rq, true);
Chris Wilsond2b4b972017-11-10 14:26:33 +00005325 if (err)
5326 goto err_active;
5327 }
5328
5329 err = i915_gem_switch_to_kernel_context(i915);
5330 if (err)
5331 goto err_active;
5332
5333 err = i915_gem_wait_for_idle(i915, I915_WAIT_LOCKED);
5334 if (err)
5335 goto err_active;
5336
5337 assert_kernel_context_is_current(i915);
5338
5339 for_each_engine(engine, i915, id) {
5340 struct i915_vma *state;
5341
Chris Wilsonab82a062018-04-30 14:15:01 +01005342 state = to_intel_context(ctx, engine)->state;
Chris Wilsond2b4b972017-11-10 14:26:33 +00005343 if (!state)
5344 continue;
5345
5346 /*
5347 * As we will hold a reference to the logical state, it will
5348 * not be torn down with the context, and importantly the
5349 * object will hold onto its vma (making it possible for a
5350 * stray GTT write to corrupt our defaults). Unmap the vma
5351 * from the GTT to prevent such accidents and reclaim the
5352 * space.
5353 */
5354 err = i915_vma_unbind(state);
5355 if (err)
5356 goto err_active;
5357
5358 err = i915_gem_object_set_to_cpu_domain(state->obj, false);
5359 if (err)
5360 goto err_active;
5361
5362 engine->default_state = i915_gem_object_get(state->obj);
5363 }
5364
5365 if (IS_ENABLED(CONFIG_DRM_I915_DEBUG_GEM)) {
5366 unsigned int found = intel_engines_has_context_isolation(i915);
5367
5368 /*
5369 * Make sure that classes with multiple engine instances all
5370 * share the same basic configuration.
5371 */
5372 for_each_engine(engine, i915, id) {
5373 unsigned int bit = BIT(engine->uabi_class);
5374 unsigned int expected = engine->default_state ? bit : 0;
5375
5376 if ((found & bit) != expected) {
5377 DRM_ERROR("mismatching default context state for class %d on engine %s\n",
5378 engine->uabi_class, engine->name);
5379 }
5380 }
5381 }
5382
5383out_ctx:
5384 i915_gem_context_set_closed(ctx);
5385 i915_gem_context_put(ctx);
5386 return err;
5387
5388err_active:
5389 /*
5390 * If we have to abandon now, we expect the engines to be idle
5391 * and ready to be torn-down. First try to flush any remaining
5392 * request, ensure we are pointing at the kernel context and
5393 * then remove it.
5394 */
5395 if (WARN_ON(i915_gem_switch_to_kernel_context(i915)))
5396 goto out_ctx;
5397
5398 if (WARN_ON(i915_gem_wait_for_idle(i915, I915_WAIT_LOCKED)))
5399 goto out_ctx;
5400
5401 i915_gem_contexts_lost(i915);
5402 goto out_ctx;
5403}
5404
Tvrtko Ursulinbf9e8422016-12-01 14:16:38 +00005405int i915_gem_init(struct drm_i915_private *dev_priv)
Chris Wilson1070a422012-04-24 15:47:41 +01005406{
Chris Wilson1070a422012-04-24 15:47:41 +01005407 int ret;
5408
Changbin Du52b24162018-05-08 17:07:05 +08005409 /* We need to fallback to 4K pages if host doesn't support huge gtt. */
5410 if (intel_vgpu_active(dev_priv) && !intel_vgpu_has_huge_gtt(dev_priv))
Matthew Auldda9fe3f32017-10-06 23:18:31 +01005411 mkwrite_device_info(dev_priv)->page_sizes =
5412 I915_GTT_PAGE_SIZE_4K;
5413
Chris Wilson94312822017-05-03 10:39:18 +01005414 dev_priv->mm.unordered_timeline = dma_fence_context_alloc(1);
Chris Wilson57822dc2017-02-22 11:40:48 +00005415
Chris Wilsonfb5c5512017-11-20 20:55:00 +00005416 if (HAS_LOGICAL_RING_CONTEXTS(dev_priv)) {
Chris Wilson821ed7d2016-09-09 14:11:53 +01005417 dev_priv->gt.resume = intel_lr_context_resume;
Tvrtko Ursulin117897f2016-03-16 11:00:40 +00005418 dev_priv->gt.cleanup_engine = intel_logical_ring_cleanup;
Chris Wilsonfb5c5512017-11-20 20:55:00 +00005419 } else {
5420 dev_priv->gt.resume = intel_legacy_submission_resume;
5421 dev_priv->gt.cleanup_engine = intel_engine_cleanup;
Oscar Mateoa83014d2014-07-24 17:04:21 +01005422 }
5423
Chris Wilsonee487002017-11-22 17:26:21 +00005424 ret = i915_gem_init_userptr(dev_priv);
5425 if (ret)
5426 return ret;
5427
Jackie Li6b0478f2018-03-13 17:32:50 -07005428 ret = intel_wopcm_init(&dev_priv->wopcm);
5429 if (ret)
5430 return ret;
5431
Sagar Arun Kamble70deead2018-01-24 21:16:58 +05305432 ret = intel_uc_init_misc(dev_priv);
Michał Winiarski3176ff42017-12-13 23:13:47 +01005433 if (ret)
5434 return ret;
5435
Chris Wilson5e4f5182015-02-13 14:35:59 +00005436 /* This is just a security blanket to placate dragons.
5437 * On some systems, we very sporadically observe that the first TLBs
5438 * used by the CS may be stale, despite us poking the TLB reset. If
5439 * we hold the forcewake during initialisation these problems
5440 * just magically go away.
5441 */
Chris Wilsonee487002017-11-22 17:26:21 +00005442 mutex_lock(&dev_priv->drm.struct_mutex);
Chris Wilson5e4f5182015-02-13 14:35:59 +00005443 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
5444
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01005445 ret = i915_gem_init_ggtt(dev_priv);
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00005446 if (ret) {
5447 GEM_BUG_ON(ret == -EIO);
5448 goto err_unlock;
5449 }
Jesse Barnesd62b4892013-03-08 10:45:53 -08005450
Chris Wilson829a0af2017-06-20 12:05:45 +01005451 ret = i915_gem_contexts_init(dev_priv);
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00005452 if (ret) {
5453 GEM_BUG_ON(ret == -EIO);
5454 goto err_ggtt;
5455 }
Ben Widawsky2fa48d82013-12-06 14:11:04 -08005456
Tvrtko Ursulinbf9e8422016-12-01 14:16:38 +00005457 ret = intel_engines_init(dev_priv);
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00005458 if (ret) {
5459 GEM_BUG_ON(ret == -EIO);
5460 goto err_context;
5461 }
Daniel Vetter53ca26c2012-04-26 23:28:03 +02005462
Chris Wilsonf58d13d2017-11-10 14:26:29 +00005463 intel_init_gt_powersave(dev_priv);
5464
Michał Winiarski61b5c152017-12-13 23:13:48 +01005465 ret = intel_uc_init(dev_priv);
Chris Wilsoncc6a8182017-11-10 14:26:30 +00005466 if (ret)
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00005467 goto err_pm;
Chris Wilsoncc6a8182017-11-10 14:26:30 +00005468
Michał Winiarski61b5c152017-12-13 23:13:48 +01005469 ret = i915_gem_init_hw(dev_priv);
5470 if (ret)
5471 goto err_uc_init;
5472
Chris Wilsoncc6a8182017-11-10 14:26:30 +00005473 /*
5474 * Despite its name intel_init_clock_gating applies both display
5475 * clock gating workarounds; GT mmio workarounds and the occasional
5476 * GT power context workaround. Worse, sometimes it includes a context
5477 * register workaround which we need to apply before we record the
5478 * default HW state for all contexts.
5479 *
5480 * FIXME: break up the workarounds and apply them at the right time!
5481 */
5482 intel_init_clock_gating(dev_priv);
5483
Chris Wilsond2b4b972017-11-10 14:26:33 +00005484 ret = __intel_engines_record_defaults(dev_priv);
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00005485 if (ret)
5486 goto err_init_hw;
5487
5488 if (i915_inject_load_failure()) {
5489 ret = -ENODEV;
5490 goto err_init_hw;
5491 }
5492
5493 if (i915_inject_load_failure()) {
5494 ret = -EIO;
5495 goto err_init_hw;
5496 }
5497
5498 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
5499 mutex_unlock(&dev_priv->drm.struct_mutex);
5500
5501 return 0;
5502
5503 /*
5504 * Unwinding is complicated by that we want to handle -EIO to mean
5505 * disable GPU submission but keep KMS alive. We want to mark the
5506 * HW as irrevisibly wedged, but keep enough state around that the
5507 * driver doesn't explode during runtime.
5508 */
5509err_init_hw:
Chris Wilson8571a052018-06-06 15:54:41 +01005510 mutex_unlock(&dev_priv->drm.struct_mutex);
5511
5512 WARN_ON(i915_gem_suspend(dev_priv));
5513 i915_gem_suspend_late(dev_priv);
5514
5515 mutex_lock(&dev_priv->drm.struct_mutex);
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00005516 intel_uc_fini_hw(dev_priv);
Michał Winiarski61b5c152017-12-13 23:13:48 +01005517err_uc_init:
5518 intel_uc_fini(dev_priv);
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00005519err_pm:
5520 if (ret != -EIO) {
5521 intel_cleanup_gt_powersave(dev_priv);
5522 i915_gem_cleanup_engines(dev_priv);
5523 }
5524err_context:
5525 if (ret != -EIO)
5526 i915_gem_contexts_fini(dev_priv);
5527err_ggtt:
5528err_unlock:
5529 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
5530 mutex_unlock(&dev_priv->drm.struct_mutex);
5531
Sagar Arun Kamble70deead2018-01-24 21:16:58 +05305532 intel_uc_fini_misc(dev_priv);
Sagar Arun Kambleda943b52018-01-10 18:24:16 +05305533
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00005534 if (ret != -EIO)
5535 i915_gem_cleanup_userptr(dev_priv);
5536
Chris Wilson60990322014-04-09 09:19:42 +01005537 if (ret == -EIO) {
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00005538 /*
5539 * Allow engine initialisation to fail by marking the GPU as
Chris Wilson60990322014-04-09 09:19:42 +01005540 * wedged. But we only want to do this where the GPU is angry,
5541 * for all other failure, such as an allocation failure, bail.
5542 */
Chris Wilson6f74b362017-10-15 15:37:25 +01005543 if (!i915_terminally_wedged(&dev_priv->gpu_error)) {
Chris Wilson51c18bf2018-06-09 12:10:58 +01005544 i915_load_error(dev_priv,
5545 "Failed to initialize GPU, declaring it wedged!\n");
Chris Wilson6f74b362017-10-15 15:37:25 +01005546 i915_gem_set_wedged(dev_priv);
5547 }
Chris Wilson60990322014-04-09 09:19:42 +01005548 ret = 0;
Chris Wilson1070a422012-04-24 15:47:41 +01005549 }
5550
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00005551 i915_gem_drain_freed_objects(dev_priv);
Chris Wilson60990322014-04-09 09:19:42 +01005552 return ret;
Chris Wilson1070a422012-04-24 15:47:41 +01005553}
5554
Michal Wajdeczko8979187a2018-06-04 09:00:32 +00005555void i915_gem_fini(struct drm_i915_private *dev_priv)
5556{
5557 i915_gem_suspend_late(dev_priv);
5558
5559 /* Flush any outstanding unpin_work. */
5560 i915_gem_drain_workqueue(dev_priv);
5561
5562 mutex_lock(&dev_priv->drm.struct_mutex);
5563 intel_uc_fini_hw(dev_priv);
5564 intel_uc_fini(dev_priv);
5565 i915_gem_cleanup_engines(dev_priv);
5566 i915_gem_contexts_fini(dev_priv);
5567 mutex_unlock(&dev_priv->drm.struct_mutex);
5568
5569 intel_uc_fini_misc(dev_priv);
5570 i915_gem_cleanup_userptr(dev_priv);
5571
5572 i915_gem_drain_freed_objects(dev_priv);
5573
5574 WARN_ON(!list_empty(&dev_priv->contexts.list));
5575}
5576
Chris Wilson24145512017-01-24 11:01:35 +00005577void i915_gem_init_mmio(struct drm_i915_private *i915)
5578{
5579 i915_gem_sanitize(i915);
5580}
5581
Zou Nan hai8187a2b2010-05-21 09:08:55 +08005582void
Tvrtko Ursulincb15d9f2016-12-01 14:16:39 +00005583i915_gem_cleanup_engines(struct drm_i915_private *dev_priv)
Zou Nan hai8187a2b2010-05-21 09:08:55 +08005584{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00005585 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05305586 enum intel_engine_id id;
Zou Nan hai8187a2b2010-05-21 09:08:55 +08005587
Akash Goel3b3f1652016-10-13 22:44:48 +05305588 for_each_engine(engine, dev_priv, id)
Tvrtko Ursulin117897f2016-03-16 11:00:40 +00005589 dev_priv->gt.cleanup_engine(engine);
Zou Nan hai8187a2b2010-05-21 09:08:55 +08005590}
5591
Eric Anholt673a3942008-07-30 12:06:12 -07005592void
Imre Deak40ae4e12016-03-16 14:54:03 +02005593i915_gem_load_init_fences(struct drm_i915_private *dev_priv)
5594{
Chris Wilson49ef5292016-08-18 17:17:00 +01005595 int i;
Imre Deak40ae4e12016-03-16 14:54:03 +02005596
Tvrtko Ursulinc56b89f2018-02-09 21:58:46 +00005597 if (INTEL_GEN(dev_priv) >= 7 && !IS_VALLEYVIEW(dev_priv) &&
Imre Deak40ae4e12016-03-16 14:54:03 +02005598 !IS_CHERRYVIEW(dev_priv))
5599 dev_priv->num_fence_regs = 32;
Tvrtko Ursulinc56b89f2018-02-09 21:58:46 +00005600 else if (INTEL_GEN(dev_priv) >= 4 ||
Jani Nikula73f67aa2016-12-07 22:48:09 +02005601 IS_I945G(dev_priv) || IS_I945GM(dev_priv) ||
5602 IS_G33(dev_priv) || IS_PINEVIEW(dev_priv))
Imre Deak40ae4e12016-03-16 14:54:03 +02005603 dev_priv->num_fence_regs = 16;
5604 else
5605 dev_priv->num_fence_regs = 8;
5606
Chris Wilsonc0336662016-05-06 15:40:21 +01005607 if (intel_vgpu_active(dev_priv))
Imre Deak40ae4e12016-03-16 14:54:03 +02005608 dev_priv->num_fence_regs =
5609 I915_READ(vgtif_reg(avail_rs.fence_num));
5610
5611 /* Initialize fence registers to zero */
Chris Wilson49ef5292016-08-18 17:17:00 +01005612 for (i = 0; i < dev_priv->num_fence_regs; i++) {
5613 struct drm_i915_fence_reg *fence = &dev_priv->fence_regs[i];
5614
5615 fence->i915 = dev_priv;
5616 fence->id = i;
5617 list_add_tail(&fence->link, &dev_priv->mm.fence_list);
5618 }
Tvrtko Ursulin4362f4f2016-11-16 08:55:33 +00005619 i915_gem_restore_fences(dev_priv);
Imre Deak40ae4e12016-03-16 14:54:03 +02005620
Tvrtko Ursulin4362f4f2016-11-16 08:55:33 +00005621 i915_gem_detect_bit_6_swizzle(dev_priv);
Imre Deak40ae4e12016-03-16 14:54:03 +02005622}
5623
Chris Wilson9c52d1c2017-11-10 23:24:47 +00005624static void i915_gem_init__mm(struct drm_i915_private *i915)
5625{
5626 spin_lock_init(&i915->mm.object_stat_lock);
5627 spin_lock_init(&i915->mm.obj_lock);
5628 spin_lock_init(&i915->mm.free_lock);
5629
5630 init_llist_head(&i915->mm.free_list);
5631
5632 INIT_LIST_HEAD(&i915->mm.unbound_list);
5633 INIT_LIST_HEAD(&i915->mm.bound_list);
5634 INIT_LIST_HEAD(&i915->mm.fence_list);
5635 INIT_LIST_HEAD(&i915->mm.userfault_list);
5636
5637 INIT_WORK(&i915->mm.free_work, __i915_gem_free_work);
5638}
5639
Michal Wajdeczkoa0de9082018-03-23 12:34:49 +00005640int i915_gem_init_early(struct drm_i915_private *dev_priv)
Eric Anholt673a3942008-07-30 12:06:12 -07005641{
Tvrtko Ursulina9335682016-11-02 15:14:59 +00005642 int err = -ENOMEM;
Chris Wilson42dcedd2012-11-15 11:32:30 +00005643
Tvrtko Ursulina9335682016-11-02 15:14:59 +00005644 dev_priv->objects = KMEM_CACHE(drm_i915_gem_object, SLAB_HWCACHE_ALIGN);
5645 if (!dev_priv->objects)
Chris Wilson73cb9702016-10-28 13:58:46 +01005646 goto err_out;
Chris Wilson73cb9702016-10-28 13:58:46 +01005647
Tvrtko Ursulina9335682016-11-02 15:14:59 +00005648 dev_priv->vmas = KMEM_CACHE(i915_vma, SLAB_HWCACHE_ALIGN);
5649 if (!dev_priv->vmas)
Chris Wilson73cb9702016-10-28 13:58:46 +01005650 goto err_objects;
Chris Wilson73cb9702016-10-28 13:58:46 +01005651
Chris Wilsond1b48c12017-08-16 09:52:08 +01005652 dev_priv->luts = KMEM_CACHE(i915_lut_handle, 0);
5653 if (!dev_priv->luts)
5654 goto err_vmas;
5655
Chris Wilsone61e0f52018-02-21 09:56:36 +00005656 dev_priv->requests = KMEM_CACHE(i915_request,
Tvrtko Ursulina9335682016-11-02 15:14:59 +00005657 SLAB_HWCACHE_ALIGN |
5658 SLAB_RECLAIM_ACCOUNT |
Paul E. McKenney5f0d5a32017-01-18 02:53:44 -08005659 SLAB_TYPESAFE_BY_RCU);
Tvrtko Ursulina9335682016-11-02 15:14:59 +00005660 if (!dev_priv->requests)
Chris Wilsond1b48c12017-08-16 09:52:08 +01005661 goto err_luts;
Chris Wilson73cb9702016-10-28 13:58:46 +01005662
Chris Wilson52e54202016-11-14 20:41:02 +00005663 dev_priv->dependencies = KMEM_CACHE(i915_dependency,
5664 SLAB_HWCACHE_ALIGN |
5665 SLAB_RECLAIM_ACCOUNT);
5666 if (!dev_priv->dependencies)
5667 goto err_requests;
5668
Chris Wilsonc5cf9a92017-05-17 13:10:04 +01005669 dev_priv->priorities = KMEM_CACHE(i915_priolist, SLAB_HWCACHE_ALIGN);
5670 if (!dev_priv->priorities)
5671 goto err_dependencies;
5672
Chris Wilson73cb9702016-10-28 13:58:46 +01005673 INIT_LIST_HEAD(&dev_priv->gt.timelines);
Chris Wilson643b4502018-04-30 14:15:03 +01005674 INIT_LIST_HEAD(&dev_priv->gt.active_rings);
Chris Wilson3365e222018-05-03 20:51:14 +01005675 INIT_LIST_HEAD(&dev_priv->gt.closed_vma);
Chris Wilson643b4502018-04-30 14:15:03 +01005676
Chris Wilson9c52d1c2017-11-10 23:24:47 +00005677 i915_gem_init__mm(dev_priv);
Chris Wilsonf2123812017-10-16 12:40:37 +01005678
Chris Wilson67d97da2016-07-04 08:08:31 +01005679 INIT_DELAYED_WORK(&dev_priv->gt.retire_work,
Eric Anholt673a3942008-07-30 12:06:12 -07005680 i915_gem_retire_work_handler);
Chris Wilson67d97da2016-07-04 08:08:31 +01005681 INIT_DELAYED_WORK(&dev_priv->gt.idle_work,
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005682 i915_gem_idle_work_handler);
Chris Wilson1f15b762016-07-01 17:23:14 +01005683 init_waitqueue_head(&dev_priv->gpu_error.wait_queue);
Daniel Vetter1f83fee2012-11-15 17:17:22 +01005684 init_waitqueue_head(&dev_priv->gpu_error.reset_queue);
Chris Wilson31169712009-09-14 16:50:28 +01005685
Joonas Lahtinen6f633402016-09-01 14:58:21 +03005686 atomic_set(&dev_priv->mm.bsd_engine_dispatch_index, 0);
5687
Chris Wilsonb5add952016-08-04 16:32:36 +01005688 spin_lock_init(&dev_priv->fb_tracking.lock);
Chris Wilson73cb9702016-10-28 13:58:46 +01005689
Matthew Auld465c4032017-10-06 23:18:14 +01005690 err = i915_gemfs_init(dev_priv);
5691 if (err)
5692 DRM_NOTE("Unable to create a private tmpfs mount, hugepage support will be disabled(%d).\n", err);
5693
Chris Wilson73cb9702016-10-28 13:58:46 +01005694 return 0;
5695
Chris Wilson52e54202016-11-14 20:41:02 +00005696err_dependencies:
5697 kmem_cache_destroy(dev_priv->dependencies);
Chris Wilson73cb9702016-10-28 13:58:46 +01005698err_requests:
5699 kmem_cache_destroy(dev_priv->requests);
Chris Wilsond1b48c12017-08-16 09:52:08 +01005700err_luts:
5701 kmem_cache_destroy(dev_priv->luts);
Chris Wilson73cb9702016-10-28 13:58:46 +01005702err_vmas:
5703 kmem_cache_destroy(dev_priv->vmas);
5704err_objects:
5705 kmem_cache_destroy(dev_priv->objects);
5706err_out:
5707 return err;
Eric Anholt673a3942008-07-30 12:06:12 -07005708}
Dave Airlie71acb5e2008-12-30 20:31:46 +10005709
Michal Wajdeczkoa0de9082018-03-23 12:34:49 +00005710void i915_gem_cleanup_early(struct drm_i915_private *dev_priv)
Imre Deakd64aa092016-01-19 15:26:29 +02005711{
Chris Wilsonc4d4c1c2017-02-10 16:35:23 +00005712 i915_gem_drain_freed_objects(dev_priv);
Chris Wilsonc9c704712018-02-19 22:06:31 +00005713 GEM_BUG_ON(!llist_empty(&dev_priv->mm.free_list));
5714 GEM_BUG_ON(atomic_read(&dev_priv->mm.free_count));
Chris Wilsonc4d4c1c2017-02-10 16:35:23 +00005715 WARN_ON(dev_priv->mm.object_count);
Matthew Auldea84aa72016-11-17 21:04:11 +00005716 WARN_ON(!list_empty(&dev_priv->gt.timelines));
Matthew Auldea84aa72016-11-17 21:04:11 +00005717
Chris Wilsonc5cf9a92017-05-17 13:10:04 +01005718 kmem_cache_destroy(dev_priv->priorities);
Chris Wilson52e54202016-11-14 20:41:02 +00005719 kmem_cache_destroy(dev_priv->dependencies);
Imre Deakd64aa092016-01-19 15:26:29 +02005720 kmem_cache_destroy(dev_priv->requests);
Chris Wilsond1b48c12017-08-16 09:52:08 +01005721 kmem_cache_destroy(dev_priv->luts);
Imre Deakd64aa092016-01-19 15:26:29 +02005722 kmem_cache_destroy(dev_priv->vmas);
5723 kmem_cache_destroy(dev_priv->objects);
Chris Wilson0eafec62016-08-04 16:32:41 +01005724
5725 /* And ensure that our DESTROY_BY_RCU slabs are truly destroyed */
5726 rcu_barrier();
Matthew Auld465c4032017-10-06 23:18:14 +01005727
5728 i915_gemfs_fini(dev_priv);
Imre Deakd64aa092016-01-19 15:26:29 +02005729}
5730
Chris Wilson6a800ea2016-09-21 14:51:07 +01005731int i915_gem_freeze(struct drm_i915_private *dev_priv)
5732{
Chris Wilsond0aa3012017-04-07 11:25:49 +01005733 /* Discard all purgeable objects, let userspace recover those as
5734 * required after resuming.
5735 */
Chris Wilson6a800ea2016-09-21 14:51:07 +01005736 i915_gem_shrink_all(dev_priv);
Chris Wilson6a800ea2016-09-21 14:51:07 +01005737
Chris Wilson6a800ea2016-09-21 14:51:07 +01005738 return 0;
5739}
5740
Chris Wilson95c778d2018-06-01 15:41:25 +01005741int i915_gem_freeze_late(struct drm_i915_private *i915)
Chris Wilson461fb992016-05-14 07:26:33 +01005742{
5743 struct drm_i915_gem_object *obj;
Chris Wilson7aab2d52016-09-09 20:02:18 +01005744 struct list_head *phases[] = {
Chris Wilson95c778d2018-06-01 15:41:25 +01005745 &i915->mm.unbound_list,
5746 &i915->mm.bound_list,
Chris Wilson7aab2d52016-09-09 20:02:18 +01005747 NULL
Chris Wilson95c778d2018-06-01 15:41:25 +01005748 }, **phase;
Chris Wilson461fb992016-05-14 07:26:33 +01005749
Chris Wilson95c778d2018-06-01 15:41:25 +01005750 /*
5751 * Called just before we write the hibernation image.
Chris Wilson461fb992016-05-14 07:26:33 +01005752 *
5753 * We need to update the domain tracking to reflect that the CPU
5754 * will be accessing all the pages to create and restore from the
5755 * hibernation, and so upon restoration those pages will be in the
5756 * CPU domain.
5757 *
5758 * To make sure the hibernation image contains the latest state,
5759 * we update that state just before writing out the image.
Chris Wilson7aab2d52016-09-09 20:02:18 +01005760 *
5761 * To try and reduce the hibernation image, we manually shrink
Chris Wilsond0aa3012017-04-07 11:25:49 +01005762 * the objects as well, see i915_gem_freeze()
Chris Wilson461fb992016-05-14 07:26:33 +01005763 */
5764
Chris Wilson95c778d2018-06-01 15:41:25 +01005765 i915_gem_shrink(i915, -1UL, NULL, I915_SHRINK_UNBOUND);
5766 i915_gem_drain_freed_objects(i915);
Chris Wilson461fb992016-05-14 07:26:33 +01005767
Chris Wilson95c778d2018-06-01 15:41:25 +01005768 mutex_lock(&i915->drm.struct_mutex);
5769 for (phase = phases; *phase; phase++) {
5770 list_for_each_entry(obj, *phase, mm.link)
5771 WARN_ON(i915_gem_object_set_to_cpu_domain(obj, true));
Chris Wilson461fb992016-05-14 07:26:33 +01005772 }
Chris Wilson95c778d2018-06-01 15:41:25 +01005773 mutex_unlock(&i915->drm.struct_mutex);
Chris Wilson461fb992016-05-14 07:26:33 +01005774
5775 return 0;
5776}
5777
Chris Wilsonf787a5f2010-09-24 16:02:42 +01005778void i915_gem_release(struct drm_device *dev, struct drm_file *file)
Eric Anholtb9624422009-06-03 07:27:35 +00005779{
Chris Wilsonf787a5f2010-09-24 16:02:42 +01005780 struct drm_i915_file_private *file_priv = file->driver_priv;
Chris Wilsone61e0f52018-02-21 09:56:36 +00005781 struct i915_request *request;
Eric Anholtb9624422009-06-03 07:27:35 +00005782
5783 /* Clean up our request list when the client is going away, so that
5784 * later retire_requests won't dereference our soon-to-be-gone
5785 * file_priv.
5786 */
Chris Wilson1c255952010-09-26 11:03:27 +01005787 spin_lock(&file_priv->mm.lock);
Chris Wilsonc8659ef2017-03-02 12:25:25 +00005788 list_for_each_entry(request, &file_priv->mm.request_list, client_link)
Chris Wilsonf787a5f2010-09-24 16:02:42 +01005789 request->file_priv = NULL;
Chris Wilson1c255952010-09-26 11:03:27 +01005790 spin_unlock(&file_priv->mm.lock);
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005791}
5792
Chris Wilson829a0af2017-06-20 12:05:45 +01005793int i915_gem_open(struct drm_i915_private *i915, struct drm_file *file)
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005794{
5795 struct drm_i915_file_private *file_priv;
Ben Widawskye422b882013-12-06 14:10:58 -08005796 int ret;
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005797
Chris Wilsonc4c29d72016-11-09 10:45:07 +00005798 DRM_DEBUG("\n");
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005799
5800 file_priv = kzalloc(sizeof(*file_priv), GFP_KERNEL);
5801 if (!file_priv)
5802 return -ENOMEM;
5803
5804 file->driver_priv = file_priv;
Chris Wilson829a0af2017-06-20 12:05:45 +01005805 file_priv->dev_priv = i915;
Chris Wilsonab0e7ff2014-02-25 17:11:24 +02005806 file_priv->file = file;
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005807
5808 spin_lock_init(&file_priv->mm.lock);
5809 INIT_LIST_HEAD(&file_priv->mm.request_list);
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005810
Chris Wilsonc80ff162016-07-27 09:07:27 +01005811 file_priv->bsd_engine = -1;
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00005812
Chris Wilson829a0af2017-06-20 12:05:45 +01005813 ret = i915_gem_context_open(i915, file);
Ben Widawskye422b882013-12-06 14:10:58 -08005814 if (ret)
5815 kfree(file_priv);
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005816
Ben Widawskye422b882013-12-06 14:10:58 -08005817 return ret;
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005818}
5819
Daniel Vetterb680c372014-09-19 18:27:27 +02005820/**
5821 * i915_gem_track_fb - update frontbuffer tracking
Geliang Tangd9072a32015-09-15 05:58:44 -07005822 * @old: current GEM buffer for the frontbuffer slots
5823 * @new: new GEM buffer for the frontbuffer slots
5824 * @frontbuffer_bits: bitmask of frontbuffer slots
Daniel Vetterb680c372014-09-19 18:27:27 +02005825 *
5826 * This updates the frontbuffer tracking bits @frontbuffer_bits by clearing them
5827 * from @old and setting them in @new. Both @old and @new can be NULL.
5828 */
Daniel Vettera071fa02014-06-18 23:28:09 +02005829void i915_gem_track_fb(struct drm_i915_gem_object *old,
5830 struct drm_i915_gem_object *new,
5831 unsigned frontbuffer_bits)
5832{
Chris Wilsonfaf5bf02016-08-04 16:32:37 +01005833 /* Control of individual bits within the mask are guarded by
5834 * the owning plane->mutex, i.e. we can never see concurrent
5835 * manipulation of individual bits. But since the bitfield as a whole
5836 * is updated using RMW, we need to use atomics in order to update
5837 * the bits.
5838 */
5839 BUILD_BUG_ON(INTEL_FRONTBUFFER_BITS_PER_PIPE * I915_MAX_PIPES >
5840 sizeof(atomic_t) * BITS_PER_BYTE);
5841
Daniel Vettera071fa02014-06-18 23:28:09 +02005842 if (old) {
Chris Wilsonfaf5bf02016-08-04 16:32:37 +01005843 WARN_ON(!(atomic_read(&old->frontbuffer_bits) & frontbuffer_bits));
5844 atomic_andnot(frontbuffer_bits, &old->frontbuffer_bits);
Daniel Vettera071fa02014-06-18 23:28:09 +02005845 }
5846
5847 if (new) {
Chris Wilsonfaf5bf02016-08-04 16:32:37 +01005848 WARN_ON(atomic_read(&new->frontbuffer_bits) & frontbuffer_bits);
5849 atomic_or(frontbuffer_bits, &new->frontbuffer_bits);
Daniel Vettera071fa02014-06-18 23:28:09 +02005850 }
5851}
5852
Dave Gordonea702992015-07-09 19:29:02 +01005853/* Allocate a new GEM object and fill it with the supplied data */
5854struct drm_i915_gem_object *
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +00005855i915_gem_object_create_from_data(struct drm_i915_private *dev_priv,
Dave Gordonea702992015-07-09 19:29:02 +01005856 const void *data, size_t size)
5857{
5858 struct drm_i915_gem_object *obj;
Chris Wilsonbe062fa2017-03-17 19:46:48 +00005859 struct file *file;
5860 size_t offset;
5861 int err;
Dave Gordonea702992015-07-09 19:29:02 +01005862
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +00005863 obj = i915_gem_object_create(dev_priv, round_up(size, PAGE_SIZE));
Chris Wilsonfe3db792016-04-25 13:32:13 +01005864 if (IS_ERR(obj))
Dave Gordonea702992015-07-09 19:29:02 +01005865 return obj;
5866
Christian Königc0a51fd2018-02-16 13:43:38 +01005867 GEM_BUG_ON(obj->write_domain != I915_GEM_DOMAIN_CPU);
Dave Gordonea702992015-07-09 19:29:02 +01005868
Chris Wilsonbe062fa2017-03-17 19:46:48 +00005869 file = obj->base.filp;
5870 offset = 0;
5871 do {
5872 unsigned int len = min_t(typeof(size), size, PAGE_SIZE);
5873 struct page *page;
5874 void *pgdata, *vaddr;
Dave Gordonea702992015-07-09 19:29:02 +01005875
Chris Wilsonbe062fa2017-03-17 19:46:48 +00005876 err = pagecache_write_begin(file, file->f_mapping,
5877 offset, len, 0,
5878 &page, &pgdata);
5879 if (err < 0)
5880 goto fail;
Dave Gordonea702992015-07-09 19:29:02 +01005881
Chris Wilsonbe062fa2017-03-17 19:46:48 +00005882 vaddr = kmap(page);
5883 memcpy(vaddr, data, len);
5884 kunmap(page);
5885
5886 err = pagecache_write_end(file, file->f_mapping,
5887 offset, len, len,
5888 page, pgdata);
5889 if (err < 0)
5890 goto fail;
5891
5892 size -= len;
5893 data += len;
5894 offset += len;
5895 } while (size);
Dave Gordonea702992015-07-09 19:29:02 +01005896
5897 return obj;
5898
5899fail:
Chris Wilsonf8c417c2016-07-20 13:31:53 +01005900 i915_gem_object_put(obj);
Chris Wilsonbe062fa2017-03-17 19:46:48 +00005901 return ERR_PTR(err);
Dave Gordonea702992015-07-09 19:29:02 +01005902}
Chris Wilson96d77632016-10-28 13:58:33 +01005903
5904struct scatterlist *
5905i915_gem_object_get_sg(struct drm_i915_gem_object *obj,
5906 unsigned int n,
5907 unsigned int *offset)
5908{
Chris Wilsona4f5ea62016-10-28 13:58:35 +01005909 struct i915_gem_object_page_iter *iter = &obj->mm.get_page;
Chris Wilson96d77632016-10-28 13:58:33 +01005910 struct scatterlist *sg;
5911 unsigned int idx, count;
5912
5913 might_sleep();
5914 GEM_BUG_ON(n >= obj->base.size >> PAGE_SHIFT);
Chris Wilsona4f5ea62016-10-28 13:58:35 +01005915 GEM_BUG_ON(!i915_gem_object_has_pinned_pages(obj));
Chris Wilson96d77632016-10-28 13:58:33 +01005916
5917 /* As we iterate forward through the sg, we record each entry in a
5918 * radixtree for quick repeated (backwards) lookups. If we have seen
5919 * this index previously, we will have an entry for it.
5920 *
5921 * Initial lookup is O(N), but this is amortized to O(1) for
5922 * sequential page access (where each new request is consecutive
5923 * to the previous one). Repeated lookups are O(lg(obj->base.size)),
5924 * i.e. O(1) with a large constant!
5925 */
5926 if (n < READ_ONCE(iter->sg_idx))
5927 goto lookup;
5928
5929 mutex_lock(&iter->lock);
5930
5931 /* We prefer to reuse the last sg so that repeated lookup of this
5932 * (or the subsequent) sg are fast - comparing against the last
5933 * sg is faster than going through the radixtree.
5934 */
5935
5936 sg = iter->sg_pos;
5937 idx = iter->sg_idx;
5938 count = __sg_page_count(sg);
5939
5940 while (idx + count <= n) {
5941 unsigned long exception, i;
5942 int ret;
5943
5944 /* If we cannot allocate and insert this entry, or the
5945 * individual pages from this range, cancel updating the
5946 * sg_idx so that on this lookup we are forced to linearly
5947 * scan onwards, but on future lookups we will try the
5948 * insertion again (in which case we need to be careful of
5949 * the error return reporting that we have already inserted
5950 * this index).
5951 */
5952 ret = radix_tree_insert(&iter->radix, idx, sg);
5953 if (ret && ret != -EEXIST)
5954 goto scan;
5955
5956 exception =
5957 RADIX_TREE_EXCEPTIONAL_ENTRY |
5958 idx << RADIX_TREE_EXCEPTIONAL_SHIFT;
5959 for (i = 1; i < count; i++) {
5960 ret = radix_tree_insert(&iter->radix, idx + i,
5961 (void *)exception);
5962 if (ret && ret != -EEXIST)
5963 goto scan;
5964 }
5965
5966 idx += count;
5967 sg = ____sg_next(sg);
5968 count = __sg_page_count(sg);
5969 }
5970
5971scan:
5972 iter->sg_pos = sg;
5973 iter->sg_idx = idx;
5974
5975 mutex_unlock(&iter->lock);
5976
5977 if (unlikely(n < idx)) /* insertion completed by another thread */
5978 goto lookup;
5979
5980 /* In case we failed to insert the entry into the radixtree, we need
5981 * to look beyond the current sg.
5982 */
5983 while (idx + count <= n) {
5984 idx += count;
5985 sg = ____sg_next(sg);
5986 count = __sg_page_count(sg);
5987 }
5988
5989 *offset = n - idx;
5990 return sg;
5991
5992lookup:
5993 rcu_read_lock();
5994
5995 sg = radix_tree_lookup(&iter->radix, n);
5996 GEM_BUG_ON(!sg);
5997
5998 /* If this index is in the middle of multi-page sg entry,
5999 * the radixtree will contain an exceptional entry that points
6000 * to the start of that range. We will return the pointer to
6001 * the base page and the offset of this page within the
6002 * sg entry's range.
6003 */
6004 *offset = 0;
6005 if (unlikely(radix_tree_exception(sg))) {
6006 unsigned long base =
6007 (unsigned long)sg >> RADIX_TREE_EXCEPTIONAL_SHIFT;
6008
6009 sg = radix_tree_lookup(&iter->radix, base);
6010 GEM_BUG_ON(!sg);
6011
6012 *offset = n - base;
6013 }
6014
6015 rcu_read_unlock();
6016
6017 return sg;
6018}
6019
6020struct page *
6021i915_gem_object_get_page(struct drm_i915_gem_object *obj, unsigned int n)
6022{
6023 struct scatterlist *sg;
6024 unsigned int offset;
6025
6026 GEM_BUG_ON(!i915_gem_object_has_struct_page(obj));
6027
6028 sg = i915_gem_object_get_sg(obj, n, &offset);
6029 return nth_page(sg_page(sg), offset);
6030}
6031
6032/* Like i915_gem_object_get_page(), but mark the returned page dirty */
6033struct page *
6034i915_gem_object_get_dirty_page(struct drm_i915_gem_object *obj,
6035 unsigned int n)
6036{
6037 struct page *page;
6038
6039 page = i915_gem_object_get_page(obj, n);
Chris Wilsona4f5ea62016-10-28 13:58:35 +01006040 if (!obj->mm.dirty)
Chris Wilson96d77632016-10-28 13:58:33 +01006041 set_page_dirty(page);
6042
6043 return page;
6044}
6045
6046dma_addr_t
6047i915_gem_object_get_dma_address(struct drm_i915_gem_object *obj,
6048 unsigned long n)
6049{
6050 struct scatterlist *sg;
6051 unsigned int offset;
6052
6053 sg = i915_gem_object_get_sg(obj, n, &offset);
6054 return sg_dma_address(sg) + (offset << PAGE_SHIFT);
6055}
Chris Wilson935a2f72017-02-13 17:15:13 +00006056
Chris Wilson8eeb7902017-07-26 19:16:01 +01006057int i915_gem_object_attach_phys(struct drm_i915_gem_object *obj, int align)
6058{
6059 struct sg_table *pages;
6060 int err;
6061
6062 if (align > obj->base.size)
6063 return -EINVAL;
6064
6065 if (obj->ops == &i915_gem_phys_ops)
6066 return 0;
6067
6068 if (obj->ops != &i915_gem_object_ops)
6069 return -EINVAL;
6070
6071 err = i915_gem_object_unbind(obj);
6072 if (err)
6073 return err;
6074
6075 mutex_lock(&obj->mm.lock);
6076
6077 if (obj->mm.madv != I915_MADV_WILLNEED) {
6078 err = -EFAULT;
6079 goto err_unlock;
6080 }
6081
6082 if (obj->mm.quirked) {
6083 err = -EFAULT;
6084 goto err_unlock;
6085 }
6086
6087 if (obj->mm.mapping) {
6088 err = -EBUSY;
6089 goto err_unlock;
6090 }
6091
Chris Wilsonf2123812017-10-16 12:40:37 +01006092 pages = fetch_and_zero(&obj->mm.pages);
6093 if (pages) {
6094 struct drm_i915_private *i915 = to_i915(obj->base.dev);
6095
6096 __i915_gem_object_reset_page_iter(obj);
6097
6098 spin_lock(&i915->mm.obj_lock);
6099 list_del(&obj->mm.link);
6100 spin_unlock(&i915->mm.obj_lock);
6101 }
6102
Chris Wilson8eeb7902017-07-26 19:16:01 +01006103 obj->ops = &i915_gem_phys_ops;
6104
Chris Wilson8fb6a5d2017-07-26 19:16:02 +01006105 err = ____i915_gem_object_get_pages(obj);
Chris Wilson8eeb7902017-07-26 19:16:01 +01006106 if (err)
6107 goto err_xfer;
6108
6109 /* Perma-pin (until release) the physical set of pages */
6110 __i915_gem_object_pin_pages(obj);
6111
6112 if (!IS_ERR_OR_NULL(pages))
6113 i915_gem_object_ops.put_pages(obj, pages);
6114 mutex_unlock(&obj->mm.lock);
6115 return 0;
6116
6117err_xfer:
6118 obj->ops = &i915_gem_object_ops;
6119 obj->mm.pages = pages;
6120err_unlock:
6121 mutex_unlock(&obj->mm.lock);
6122 return err;
6123}
6124
Chris Wilson935a2f72017-02-13 17:15:13 +00006125#if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
6126#include "selftests/scatterlist.c"
Chris Wilson66d9cb52017-02-13 17:15:17 +00006127#include "selftests/mock_gem_device.c"
Chris Wilson44653982017-02-13 17:15:20 +00006128#include "selftests/huge_gem_object.c"
Matthew Auld40498662017-10-06 23:18:29 +01006129#include "selftests/huge_pages.c"
Chris Wilson8335fd62017-02-13 17:15:28 +00006130#include "selftests/i915_gem_object.c"
Chris Wilson17059452017-02-13 17:15:32 +00006131#include "selftests/i915_gem_coherency.c"
Chris Wilson935a2f72017-02-13 17:15:13 +00006132#endif