blob: f0644d1fbd75028d57d38991d6a3c6d7dde1d09c [file] [log] [blame]
Eric Anholt673a3942008-07-30 12:06:12 -07001/*
Daniel Vetterbe6a0372015-03-18 10:46:04 +01002 * Copyright © 2008-2015 Intel Corporation
Eric Anholt673a3942008-07-30 12:06:12 -07003 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 *
23 * Authors:
24 * Eric Anholt <eric@anholt.net>
25 *
26 */
27
David Howells760285e2012-10-02 18:01:07 +010028#include <drm/drmP.h>
David Herrmann0de23972013-07-24 21:07:52 +020029#include <drm/drm_vma_manager.h>
David Howells760285e2012-10-02 18:01:07 +010030#include <drm/i915_drm.h>
Eric Anholt673a3942008-07-30 12:06:12 -070031#include "i915_drv.h"
Chris Wilson57822dc2017-02-22 11:40:48 +000032#include "i915_gem_clflush.h"
Yu Zhangeb822892015-02-10 19:05:49 +080033#include "i915_vgpu.h"
Chris Wilson1c5d22f2009-08-25 11:15:50 +010034#include "i915_trace.h"
Jesse Barnes652c3932009-08-17 13:31:43 -070035#include "intel_drv.h"
Chris Wilson5d723d72016-08-04 16:32:35 +010036#include "intel_frontbuffer.h"
Peter Antoine0ccdacf2016-04-13 15:03:25 +010037#include "intel_mocs.h"
Oscar Mateo59b449d2018-04-10 09:12:47 -070038#include "intel_workarounds.h"
Matthew Auld465c4032017-10-06 23:18:14 +010039#include "i915_gemfs.h"
Chris Wilson6b5e90f2016-11-14 20:41:05 +000040#include <linux/dma-fence-array.h>
Chris Wilsonfe3288b2017-02-12 17:20:01 +000041#include <linux/kthread.h>
Chris Wilsonc13d87e2016-07-20 09:21:15 +010042#include <linux/reservation.h>
Hugh Dickins5949eac2011-06-27 16:18:18 -070043#include <linux/shmem_fs.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090044#include <linux/slab.h>
Chris Wilson20e49332016-11-22 14:41:21 +000045#include <linux/stop_machine.h>
Eric Anholt673a3942008-07-30 12:06:12 -070046#include <linux/swap.h>
Jesse Barnes79e53942008-11-07 14:24:08 -080047#include <linux/pci.h>
Daniel Vetter1286ff72012-05-10 15:25:09 +020048#include <linux/dma-buf.h>
Eric Anholt673a3942008-07-30 12:06:12 -070049
Chris Wilsonfbbd37b2016-10-28 13:58:42 +010050static void i915_gem_flush_free_objects(struct drm_i915_private *i915);
Chris Wilson61050802012-04-17 15:31:31 +010051
Chris Wilson2c225692013-08-09 12:26:45 +010052static bool cpu_write_needs_clflush(struct drm_i915_gem_object *obj)
53{
Chris Wilsone27ab732017-06-15 13:38:49 +010054 if (obj->cache_dirty)
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +053055 return false;
56
Chris Wilsonb8f55be2017-08-11 12:11:16 +010057 if (!(obj->cache_coherent & I915_BO_CACHE_COHERENT_FOR_WRITE))
Chris Wilson2c225692013-08-09 12:26:45 +010058 return true;
59
Chris Wilsonbd3d2252017-10-13 21:26:14 +010060 return obj->pin_global; /* currently in use by HW, keep flushed */
Chris Wilson2c225692013-08-09 12:26:45 +010061}
62
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +053063static int
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +010064insert_mappable_node(struct i915_ggtt *ggtt,
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +053065 struct drm_mm_node *node, u32 size)
66{
67 memset(node, 0, sizeof(*node));
Chris Wilson4e64e552017-02-02 21:04:38 +000068 return drm_mm_insert_node_in_range(&ggtt->base.mm, node,
69 size, 0, I915_COLOR_UNEVICTABLE,
70 0, ggtt->mappable_end,
71 DRM_MM_INSERT_LOW);
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +053072}
73
74static void
75remove_mappable_node(struct drm_mm_node *node)
76{
77 drm_mm_remove_node(node);
78}
79
Chris Wilson73aa8082010-09-30 11:46:12 +010080/* some bookkeeping */
81static void i915_gem_info_add_obj(struct drm_i915_private *dev_priv,
Chris Wilson3ef7f222016-10-18 13:02:48 +010082 u64 size)
Chris Wilson73aa8082010-09-30 11:46:12 +010083{
Daniel Vetterc20e8352013-07-24 22:40:23 +020084 spin_lock(&dev_priv->mm.object_stat_lock);
Chris Wilson73aa8082010-09-30 11:46:12 +010085 dev_priv->mm.object_count++;
86 dev_priv->mm.object_memory += size;
Daniel Vetterc20e8352013-07-24 22:40:23 +020087 spin_unlock(&dev_priv->mm.object_stat_lock);
Chris Wilson73aa8082010-09-30 11:46:12 +010088}
89
90static void i915_gem_info_remove_obj(struct drm_i915_private *dev_priv,
Chris Wilson3ef7f222016-10-18 13:02:48 +010091 u64 size)
Chris Wilson73aa8082010-09-30 11:46:12 +010092{
Daniel Vetterc20e8352013-07-24 22:40:23 +020093 spin_lock(&dev_priv->mm.object_stat_lock);
Chris Wilson73aa8082010-09-30 11:46:12 +010094 dev_priv->mm.object_count--;
95 dev_priv->mm.object_memory -= size;
Daniel Vetterc20e8352013-07-24 22:40:23 +020096 spin_unlock(&dev_priv->mm.object_stat_lock);
Chris Wilson73aa8082010-09-30 11:46:12 +010097}
98
Chris Wilson21dd3732011-01-26 15:55:56 +000099static int
Daniel Vetter33196de2012-11-14 17:14:05 +0100100i915_gem_wait_for_error(struct i915_gpu_error *error)
Chris Wilson30dbf0c2010-09-25 10:19:17 +0100101{
Chris Wilson30dbf0c2010-09-25 10:19:17 +0100102 int ret;
103
Chris Wilson4c7d62c2016-10-28 13:58:32 +0100104 might_sleep();
105
Daniel Vetter0a6759c2012-07-04 22:18:41 +0200106 /*
107 * Only wait 10 seconds for the gpu reset to complete to avoid hanging
108 * userspace. If it takes that long something really bad is going on and
109 * we should simply try to bail out and fail as gracefully as possible.
110 */
Daniel Vetter1f83fee2012-11-15 17:17:22 +0100111 ret = wait_event_interruptible_timeout(error->reset_queue,
Chris Wilson8c185ec2017-03-16 17:13:02 +0000112 !i915_reset_backoff(error),
Chris Wilsonb52992c2016-10-28 13:58:24 +0100113 I915_RESET_TIMEOUT);
Daniel Vetter0a6759c2012-07-04 22:18:41 +0200114 if (ret == 0) {
115 DRM_ERROR("Timed out waiting for the gpu reset to complete\n");
116 return -EIO;
117 } else if (ret < 0) {
Chris Wilson30dbf0c2010-09-25 10:19:17 +0100118 return ret;
Chris Wilsond98c52c2016-04-13 17:35:05 +0100119 } else {
120 return 0;
Daniel Vetter0a6759c2012-07-04 22:18:41 +0200121 }
Chris Wilson30dbf0c2010-09-25 10:19:17 +0100122}
123
Chris Wilson54cf91d2010-11-25 18:00:26 +0000124int i915_mutex_lock_interruptible(struct drm_device *dev)
Chris Wilson76c1dec2010-09-25 11:22:51 +0100125{
Chris Wilsonfac5e232016-07-04 11:34:36 +0100126 struct drm_i915_private *dev_priv = to_i915(dev);
Chris Wilson76c1dec2010-09-25 11:22:51 +0100127 int ret;
128
Daniel Vetter33196de2012-11-14 17:14:05 +0100129 ret = i915_gem_wait_for_error(&dev_priv->gpu_error);
Chris Wilson76c1dec2010-09-25 11:22:51 +0100130 if (ret)
131 return ret;
132
133 ret = mutex_lock_interruptible(&dev->struct_mutex);
134 if (ret)
135 return ret;
136
Chris Wilson76c1dec2010-09-25 11:22:51 +0100137 return 0;
138}
Chris Wilson30dbf0c2010-09-25 10:19:17 +0100139
Chris Wilsone4d20062018-04-06 16:51:44 +0100140static u32 __i915_gem_park(struct drm_i915_private *i915)
141{
142 lockdep_assert_held(&i915->drm.struct_mutex);
143 GEM_BUG_ON(i915->gt.active_requests);
144
145 if (!i915->gt.awake)
146 return I915_EPOCH_INVALID;
147
148 GEM_BUG_ON(i915->gt.epoch == I915_EPOCH_INVALID);
149
150 /*
151 * Be paranoid and flush a concurrent interrupt to make sure
152 * we don't reactivate any irq tasklets after parking.
153 *
154 * FIXME: Note that even though we have waited for execlists to be idle,
155 * there may still be an in-flight interrupt even though the CSB
156 * is now empty. synchronize_irq() makes sure that a residual interrupt
157 * is completed before we continue, but it doesn't prevent the HW from
158 * raising a spurious interrupt later. To complete the shield we should
159 * coordinate disabling the CS irq with flushing the interrupts.
160 */
161 synchronize_irq(i915->drm.irq);
162
163 intel_engines_park(i915);
164 i915_gem_timelines_park(i915);
165
166 i915_pmu_gt_parked(i915);
167
168 i915->gt.awake = false;
169
170 if (INTEL_GEN(i915) >= 6)
171 gen6_rps_idle(i915);
172
173 intel_display_power_put(i915, POWER_DOMAIN_GT_IRQ);
174
175 intel_runtime_pm_put(i915);
176
177 return i915->gt.epoch;
178}
179
180void i915_gem_park(struct drm_i915_private *i915)
181{
182 lockdep_assert_held(&i915->drm.struct_mutex);
183 GEM_BUG_ON(i915->gt.active_requests);
184
185 if (!i915->gt.awake)
186 return;
187
188 /* Defer the actual call to __i915_gem_park() to prevent ping-pongs */
189 mod_delayed_work(i915->wq, &i915->gt.idle_work, msecs_to_jiffies(100));
190}
191
192void i915_gem_unpark(struct drm_i915_private *i915)
193{
194 lockdep_assert_held(&i915->drm.struct_mutex);
195 GEM_BUG_ON(!i915->gt.active_requests);
196
197 if (i915->gt.awake)
198 return;
199
200 intel_runtime_pm_get_noresume(i915);
201
202 /*
203 * It seems that the DMC likes to transition between the DC states a lot
204 * when there are no connected displays (no active power domains) during
205 * command submission.
206 *
207 * This activity has negative impact on the performance of the chip with
208 * huge latencies observed in the interrupt handler and elsewhere.
209 *
210 * Work around it by grabbing a GT IRQ power domain whilst there is any
211 * GT activity, preventing any DC state transitions.
212 */
213 intel_display_power_get(i915, POWER_DOMAIN_GT_IRQ);
214
215 i915->gt.awake = true;
216 if (unlikely(++i915->gt.epoch == 0)) /* keep 0 as invalid */
217 i915->gt.epoch = 1;
218
219 intel_enable_gt_powersave(i915);
220 i915_update_gfx_val(i915);
221 if (INTEL_GEN(i915) >= 6)
222 gen6_rps_busy(i915);
223 i915_pmu_gt_unparked(i915);
224
225 intel_engines_unpark(i915);
226
227 i915_queue_hangcheck(i915);
228
229 queue_delayed_work(i915->wq,
230 &i915->gt.retire_work,
231 round_jiffies_up_relative(HZ));
232}
233
Eric Anholt673a3942008-07-30 12:06:12 -0700234int
Eric Anholt5a125c32008-10-22 21:40:13 -0700235i915_gem_get_aperture_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +0000236 struct drm_file *file)
Eric Anholt5a125c32008-10-22 21:40:13 -0700237{
Joonas Lahtinen72e96d62016-03-30 16:57:10 +0300238 struct drm_i915_private *dev_priv = to_i915(dev);
Joonas Lahtinen62106b42016-03-18 10:42:57 +0200239 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Joonas Lahtinen72e96d62016-03-30 16:57:10 +0300240 struct drm_i915_gem_get_aperture *args = data;
Tvrtko Ursulinca1543b2015-07-01 11:51:10 +0100241 struct i915_vma *vma;
Weinan Liff8f7972017-05-31 10:35:52 +0800242 u64 pinned;
Eric Anholt5a125c32008-10-22 21:40:13 -0700243
Weinan Liff8f7972017-05-31 10:35:52 +0800244 pinned = ggtt->base.reserved;
Chris Wilson73aa8082010-09-30 11:46:12 +0100245 mutex_lock(&dev->struct_mutex);
Chris Wilson1c7f4bc2016-02-26 11:03:19 +0000246 list_for_each_entry(vma, &ggtt->base.active_list, vm_link)
Chris Wilson20dfbde2016-08-04 16:32:30 +0100247 if (i915_vma_is_pinned(vma))
Tvrtko Ursulinca1543b2015-07-01 11:51:10 +0100248 pinned += vma->node.size;
Chris Wilson1c7f4bc2016-02-26 11:03:19 +0000249 list_for_each_entry(vma, &ggtt->base.inactive_list, vm_link)
Chris Wilson20dfbde2016-08-04 16:32:30 +0100250 if (i915_vma_is_pinned(vma))
Tvrtko Ursulinca1543b2015-07-01 11:51:10 +0100251 pinned += vma->node.size;
Chris Wilson73aa8082010-09-30 11:46:12 +0100252 mutex_unlock(&dev->struct_mutex);
Eric Anholt5a125c32008-10-22 21:40:13 -0700253
Joonas Lahtinen72e96d62016-03-30 16:57:10 +0300254 args->aper_size = ggtt->base.total;
Akshay Joshi0206e352011-08-16 15:34:10 -0400255 args->aper_available_size = args->aper_size - pinned;
Chris Wilson6299f992010-11-24 12:23:44 +0000256
Eric Anholt5a125c32008-10-22 21:40:13 -0700257 return 0;
258}
259
Matthew Auldb91b09e2017-10-06 23:18:17 +0100260static int i915_gem_object_get_pages_phys(struct drm_i915_gem_object *obj)
Chris Wilson00731152014-05-21 12:42:56 +0100261{
Al Viro93c76a32015-12-04 23:45:44 -0500262 struct address_space *mapping = obj->base.filp->f_mapping;
Chris Wilsondbb43512016-12-07 13:34:11 +0000263 drm_dma_handle_t *phys;
Chris Wilson6a2c4232014-11-04 04:51:40 -0800264 struct sg_table *st;
265 struct scatterlist *sg;
Chris Wilsondbb43512016-12-07 13:34:11 +0000266 char *vaddr;
Chris Wilson6a2c4232014-11-04 04:51:40 -0800267 int i;
Matthew Auldb91b09e2017-10-06 23:18:17 +0100268 int err;
Chris Wilson00731152014-05-21 12:42:56 +0100269
Chris Wilson6a2c4232014-11-04 04:51:40 -0800270 if (WARN_ON(i915_gem_object_needs_bit17_swizzle(obj)))
Matthew Auldb91b09e2017-10-06 23:18:17 +0100271 return -EINVAL;
Chris Wilson00731152014-05-21 12:42:56 +0100272
Chris Wilsondbb43512016-12-07 13:34:11 +0000273 /* Always aligning to the object size, allows a single allocation
274 * to handle all possible callers, and given typical object sizes,
275 * the alignment of the buddy allocation will naturally match.
276 */
277 phys = drm_pci_alloc(obj->base.dev,
Ville Syrjälä750fae22017-09-07 17:32:03 +0300278 roundup_pow_of_two(obj->base.size),
Chris Wilsondbb43512016-12-07 13:34:11 +0000279 roundup_pow_of_two(obj->base.size));
280 if (!phys)
Matthew Auldb91b09e2017-10-06 23:18:17 +0100281 return -ENOMEM;
Chris Wilsondbb43512016-12-07 13:34:11 +0000282
283 vaddr = phys->vaddr;
Chris Wilson6a2c4232014-11-04 04:51:40 -0800284 for (i = 0; i < obj->base.size / PAGE_SIZE; i++) {
285 struct page *page;
286 char *src;
287
288 page = shmem_read_mapping_page(mapping, i);
Chris Wilsondbb43512016-12-07 13:34:11 +0000289 if (IS_ERR(page)) {
Matthew Auldb91b09e2017-10-06 23:18:17 +0100290 err = PTR_ERR(page);
Chris Wilsondbb43512016-12-07 13:34:11 +0000291 goto err_phys;
292 }
Chris Wilson6a2c4232014-11-04 04:51:40 -0800293
294 src = kmap_atomic(page);
295 memcpy(vaddr, src, PAGE_SIZE);
296 drm_clflush_virt_range(vaddr, PAGE_SIZE);
297 kunmap_atomic(src);
298
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300299 put_page(page);
Chris Wilson6a2c4232014-11-04 04:51:40 -0800300 vaddr += PAGE_SIZE;
301 }
302
Chris Wilsonc0336662016-05-06 15:40:21 +0100303 i915_gem_chipset_flush(to_i915(obj->base.dev));
Chris Wilson6a2c4232014-11-04 04:51:40 -0800304
305 st = kmalloc(sizeof(*st), GFP_KERNEL);
Chris Wilsondbb43512016-12-07 13:34:11 +0000306 if (!st) {
Matthew Auldb91b09e2017-10-06 23:18:17 +0100307 err = -ENOMEM;
Chris Wilsondbb43512016-12-07 13:34:11 +0000308 goto err_phys;
309 }
Chris Wilson6a2c4232014-11-04 04:51:40 -0800310
311 if (sg_alloc_table(st, 1, GFP_KERNEL)) {
312 kfree(st);
Matthew Auldb91b09e2017-10-06 23:18:17 +0100313 err = -ENOMEM;
Chris Wilsondbb43512016-12-07 13:34:11 +0000314 goto err_phys;
Chris Wilson6a2c4232014-11-04 04:51:40 -0800315 }
316
317 sg = st->sgl;
318 sg->offset = 0;
319 sg->length = obj->base.size;
320
Chris Wilsondbb43512016-12-07 13:34:11 +0000321 sg_dma_address(sg) = phys->busaddr;
Chris Wilson6a2c4232014-11-04 04:51:40 -0800322 sg_dma_len(sg) = obj->base.size;
323
Chris Wilsondbb43512016-12-07 13:34:11 +0000324 obj->phys_handle = phys;
Matthew Auldb91b09e2017-10-06 23:18:17 +0100325
Matthew Aulda5c081662017-10-06 23:18:18 +0100326 __i915_gem_object_set_pages(obj, st, sg->length);
Matthew Auldb91b09e2017-10-06 23:18:17 +0100327
328 return 0;
Chris Wilsondbb43512016-12-07 13:34:11 +0000329
330err_phys:
331 drm_pci_free(obj->base.dev, phys);
Matthew Auldb91b09e2017-10-06 23:18:17 +0100332
333 return err;
Chris Wilson6a2c4232014-11-04 04:51:40 -0800334}
335
Chris Wilsone27ab732017-06-15 13:38:49 +0100336static void __start_cpu_write(struct drm_i915_gem_object *obj)
337{
Christian Königc0a51fd2018-02-16 13:43:38 +0100338 obj->read_domains = I915_GEM_DOMAIN_CPU;
339 obj->write_domain = I915_GEM_DOMAIN_CPU;
Chris Wilsone27ab732017-06-15 13:38:49 +0100340 if (cpu_write_needs_clflush(obj))
341 obj->cache_dirty = true;
342}
343
Chris Wilson6a2c4232014-11-04 04:51:40 -0800344static void
Chris Wilson2b3c8312016-11-11 14:58:09 +0000345__i915_gem_object_release_shmem(struct drm_i915_gem_object *obj,
Chris Wilsone5facdf2016-12-23 14:57:57 +0000346 struct sg_table *pages,
347 bool needs_clflush)
Chris Wilson6a2c4232014-11-04 04:51:40 -0800348{
Chris Wilsona4f5ea62016-10-28 13:58:35 +0100349 GEM_BUG_ON(obj->mm.madv == __I915_MADV_PURGED);
Chris Wilson6a2c4232014-11-04 04:51:40 -0800350
Chris Wilsona4f5ea62016-10-28 13:58:35 +0100351 if (obj->mm.madv == I915_MADV_DONTNEED)
352 obj->mm.dirty = false;
Chris Wilson6a2c4232014-11-04 04:51:40 -0800353
Chris Wilsone5facdf2016-12-23 14:57:57 +0000354 if (needs_clflush &&
Christian Königc0a51fd2018-02-16 13:43:38 +0100355 (obj->read_domains & I915_GEM_DOMAIN_CPU) == 0 &&
Chris Wilsonb8f55be2017-08-11 12:11:16 +0100356 !(obj->cache_coherent & I915_BO_CACHE_COHERENT_FOR_READ))
Chris Wilson2b3c8312016-11-11 14:58:09 +0000357 drm_clflush_sg(pages);
Chris Wilson03ac84f2016-10-28 13:58:36 +0100358
Chris Wilsone27ab732017-06-15 13:38:49 +0100359 __start_cpu_write(obj);
Chris Wilson03ac84f2016-10-28 13:58:36 +0100360}
361
362static void
363i915_gem_object_put_pages_phys(struct drm_i915_gem_object *obj,
364 struct sg_table *pages)
365{
Chris Wilsone5facdf2016-12-23 14:57:57 +0000366 __i915_gem_object_release_shmem(obj, pages, false);
Chris Wilson03ac84f2016-10-28 13:58:36 +0100367
Chris Wilsona4f5ea62016-10-28 13:58:35 +0100368 if (obj->mm.dirty) {
Al Viro93c76a32015-12-04 23:45:44 -0500369 struct address_space *mapping = obj->base.filp->f_mapping;
Chris Wilson6a2c4232014-11-04 04:51:40 -0800370 char *vaddr = obj->phys_handle->vaddr;
Chris Wilson00731152014-05-21 12:42:56 +0100371 int i;
372
373 for (i = 0; i < obj->base.size / PAGE_SIZE; i++) {
Chris Wilson6a2c4232014-11-04 04:51:40 -0800374 struct page *page;
375 char *dst;
Chris Wilson00731152014-05-21 12:42:56 +0100376
Chris Wilson6a2c4232014-11-04 04:51:40 -0800377 page = shmem_read_mapping_page(mapping, i);
378 if (IS_ERR(page))
379 continue;
380
381 dst = kmap_atomic(page);
382 drm_clflush_virt_range(vaddr, PAGE_SIZE);
383 memcpy(dst, vaddr, PAGE_SIZE);
384 kunmap_atomic(dst);
385
386 set_page_dirty(page);
Chris Wilsona4f5ea62016-10-28 13:58:35 +0100387 if (obj->mm.madv == I915_MADV_WILLNEED)
Chris Wilson00731152014-05-21 12:42:56 +0100388 mark_page_accessed(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300389 put_page(page);
Chris Wilson00731152014-05-21 12:42:56 +0100390 vaddr += PAGE_SIZE;
391 }
Chris Wilsona4f5ea62016-10-28 13:58:35 +0100392 obj->mm.dirty = false;
Chris Wilson00731152014-05-21 12:42:56 +0100393 }
394
Chris Wilson03ac84f2016-10-28 13:58:36 +0100395 sg_free_table(pages);
396 kfree(pages);
Chris Wilsondbb43512016-12-07 13:34:11 +0000397
398 drm_pci_free(obj->base.dev, obj->phys_handle);
Chris Wilson6a2c4232014-11-04 04:51:40 -0800399}
400
401static void
402i915_gem_object_release_phys(struct drm_i915_gem_object *obj)
403{
Chris Wilsona4f5ea62016-10-28 13:58:35 +0100404 i915_gem_object_unpin_pages(obj);
Chris Wilson6a2c4232014-11-04 04:51:40 -0800405}
406
407static const struct drm_i915_gem_object_ops i915_gem_phys_ops = {
408 .get_pages = i915_gem_object_get_pages_phys,
409 .put_pages = i915_gem_object_put_pages_phys,
410 .release = i915_gem_object_release_phys,
411};
412
Chris Wilson581ab1f2017-02-15 16:39:00 +0000413static const struct drm_i915_gem_object_ops i915_gem_object_ops;
414
Chris Wilson35a96112016-08-14 18:44:40 +0100415int i915_gem_object_unbind(struct drm_i915_gem_object *obj)
Chris Wilsonaa653a62016-08-04 07:52:27 +0100416{
417 struct i915_vma *vma;
418 LIST_HEAD(still_in_list);
Chris Wilson02bef8f2016-08-14 18:44:41 +0100419 int ret;
Chris Wilsonaa653a62016-08-04 07:52:27 +0100420
Chris Wilson02bef8f2016-08-14 18:44:41 +0100421 lockdep_assert_held(&obj->base.dev->struct_mutex);
422
423 /* Closed vma are removed from the obj->vma_list - but they may
424 * still have an active binding on the object. To remove those we
425 * must wait for all rendering to complete to the object (as unbinding
426 * must anyway), and retire the requests.
Chris Wilsonaa653a62016-08-04 07:52:27 +0100427 */
Chris Wilson5888fc92017-12-04 13:25:13 +0000428 ret = i915_gem_object_set_to_cpu_domain(obj, false);
Chris Wilson02bef8f2016-08-14 18:44:41 +0100429 if (ret)
430 return ret;
431
Chris Wilsonaa653a62016-08-04 07:52:27 +0100432 while ((vma = list_first_entry_or_null(&obj->vma_list,
433 struct i915_vma,
434 obj_link))) {
435 list_move_tail(&vma->obj_link, &still_in_list);
436 ret = i915_vma_unbind(vma);
437 if (ret)
438 break;
439 }
440 list_splice(&still_in_list, &obj->vma_list);
441
442 return ret;
443}
444
Chris Wilsone95433c2016-10-28 13:58:27 +0100445static long
446i915_gem_object_wait_fence(struct dma_fence *fence,
447 unsigned int flags,
448 long timeout,
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +0100449 struct intel_rps_client *rps_client)
Chris Wilsone95433c2016-10-28 13:58:27 +0100450{
Chris Wilsone61e0f52018-02-21 09:56:36 +0000451 struct i915_request *rq;
Chris Wilsone95433c2016-10-28 13:58:27 +0100452
453 BUILD_BUG_ON(I915_WAIT_INTERRUPTIBLE != 0x1);
454
455 if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags))
456 return timeout;
457
458 if (!dma_fence_is_i915(fence))
459 return dma_fence_wait_timeout(fence,
460 flags & I915_WAIT_INTERRUPTIBLE,
461 timeout);
462
463 rq = to_request(fence);
Chris Wilsone61e0f52018-02-21 09:56:36 +0000464 if (i915_request_completed(rq))
Chris Wilsone95433c2016-10-28 13:58:27 +0100465 goto out;
466
Chris Wilsone9af4ea2018-01-18 13:16:09 +0000467 /*
468 * This client is about to stall waiting for the GPU. In many cases
Chris Wilsone95433c2016-10-28 13:58:27 +0100469 * this is undesirable and limits the throughput of the system, as
470 * many clients cannot continue processing user input/output whilst
471 * blocked. RPS autotuning may take tens of milliseconds to respond
472 * to the GPU load and thus incurs additional latency for the client.
473 * We can circumvent that by promoting the GPU frequency to maximum
474 * before we wait. This makes the GPU throttle up much more quickly
475 * (good for benchmarks and user experience, e.g. window animations),
476 * but at a cost of spending more power processing the workload
477 * (bad for battery). Not all clients even want their results
478 * immediately and for them we should just let the GPU select its own
479 * frequency to maximise efficiency. To prevent a single client from
480 * forcing the clocks too high for the whole system, we only allow
481 * each client to waitboost once in a busy period.
482 */
Chris Wilsone61e0f52018-02-21 09:56:36 +0000483 if (rps_client && !i915_request_started(rq)) {
Chris Wilsone95433c2016-10-28 13:58:27 +0100484 if (INTEL_GEN(rq->i915) >= 6)
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +0100485 gen6_rps_boost(rq, rps_client);
Chris Wilsone95433c2016-10-28 13:58:27 +0100486 }
487
Chris Wilsone61e0f52018-02-21 09:56:36 +0000488 timeout = i915_request_wait(rq, flags, timeout);
Chris Wilsone95433c2016-10-28 13:58:27 +0100489
490out:
Chris Wilsone61e0f52018-02-21 09:56:36 +0000491 if (flags & I915_WAIT_LOCKED && i915_request_completed(rq))
492 i915_request_retire_upto(rq);
Chris Wilsone95433c2016-10-28 13:58:27 +0100493
Chris Wilsone95433c2016-10-28 13:58:27 +0100494 return timeout;
495}
496
497static long
498i915_gem_object_wait_reservation(struct reservation_object *resv,
499 unsigned int flags,
500 long timeout,
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +0100501 struct intel_rps_client *rps_client)
Chris Wilsone95433c2016-10-28 13:58:27 +0100502{
Chris Wilsone54ca972017-02-17 15:13:04 +0000503 unsigned int seq = __read_seqcount_begin(&resv->seq);
Chris Wilsone95433c2016-10-28 13:58:27 +0100504 struct dma_fence *excl;
Chris Wilsone54ca972017-02-17 15:13:04 +0000505 bool prune_fences = false;
Chris Wilsone95433c2016-10-28 13:58:27 +0100506
507 if (flags & I915_WAIT_ALL) {
508 struct dma_fence **shared;
509 unsigned int count, i;
510 int ret;
511
512 ret = reservation_object_get_fences_rcu(resv,
513 &excl, &count, &shared);
514 if (ret)
515 return ret;
516
517 for (i = 0; i < count; i++) {
518 timeout = i915_gem_object_wait_fence(shared[i],
519 flags, timeout,
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +0100520 rps_client);
Chris Wilsond892e932017-02-12 21:53:43 +0000521 if (timeout < 0)
Chris Wilsone95433c2016-10-28 13:58:27 +0100522 break;
523
524 dma_fence_put(shared[i]);
525 }
526
527 for (; i < count; i++)
528 dma_fence_put(shared[i]);
529 kfree(shared);
Chris Wilsone54ca972017-02-17 15:13:04 +0000530
Chris Wilsonfa730552018-03-07 17:13:03 +0000531 /*
532 * If both shared fences and an exclusive fence exist,
533 * then by construction the shared fences must be later
534 * than the exclusive fence. If we successfully wait for
535 * all the shared fences, we know that the exclusive fence
536 * must all be signaled. If all the shared fences are
537 * signaled, we can prune the array and recover the
538 * floating references on the fences/requests.
539 */
Chris Wilsone54ca972017-02-17 15:13:04 +0000540 prune_fences = count && timeout >= 0;
Chris Wilsone95433c2016-10-28 13:58:27 +0100541 } else {
542 excl = reservation_object_get_excl_rcu(resv);
543 }
544
Chris Wilsonfa730552018-03-07 17:13:03 +0000545 if (excl && timeout >= 0)
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +0100546 timeout = i915_gem_object_wait_fence(excl, flags, timeout,
547 rps_client);
Chris Wilsone95433c2016-10-28 13:58:27 +0100548
549 dma_fence_put(excl);
550
Chris Wilsonfa730552018-03-07 17:13:03 +0000551 /*
552 * Opportunistically prune the fences iff we know they have *all* been
Chris Wilson03d1cac2017-03-08 13:26:28 +0000553 * signaled and that the reservation object has not been changed (i.e.
554 * no new fences have been added).
555 */
Chris Wilsone54ca972017-02-17 15:13:04 +0000556 if (prune_fences && !__read_seqcount_retry(&resv->seq, seq)) {
Chris Wilson03d1cac2017-03-08 13:26:28 +0000557 if (reservation_object_trylock(resv)) {
558 if (!__read_seqcount_retry(&resv->seq, seq))
559 reservation_object_add_excl_fence(resv, NULL);
560 reservation_object_unlock(resv);
561 }
Chris Wilsone54ca972017-02-17 15:13:04 +0000562 }
563
Chris Wilsone95433c2016-10-28 13:58:27 +0100564 return timeout;
565}
566
Chris Wilsonb7268c52018-04-18 19:40:52 +0100567static void __fence_set_priority(struct dma_fence *fence,
568 const struct i915_sched_attr *attr)
Chris Wilson6b5e90f2016-11-14 20:41:05 +0000569{
Chris Wilsone61e0f52018-02-21 09:56:36 +0000570 struct i915_request *rq;
Chris Wilson6b5e90f2016-11-14 20:41:05 +0000571 struct intel_engine_cs *engine;
572
Chris Wilsonc218ee02018-01-06 10:56:18 +0000573 if (dma_fence_is_signaled(fence) || !dma_fence_is_i915(fence))
Chris Wilson6b5e90f2016-11-14 20:41:05 +0000574 return;
575
576 rq = to_request(fence);
577 engine = rq->engine;
Chris Wilson6b5e90f2016-11-14 20:41:05 +0000578
Chris Wilson47650db2018-03-07 13:42:25 +0000579 rcu_read_lock();
580 if (engine->schedule)
Chris Wilsonb7268c52018-04-18 19:40:52 +0100581 engine->schedule(rq, attr);
Chris Wilson47650db2018-03-07 13:42:25 +0000582 rcu_read_unlock();
Chris Wilson6b5e90f2016-11-14 20:41:05 +0000583}
584
Chris Wilsonb7268c52018-04-18 19:40:52 +0100585static void fence_set_priority(struct dma_fence *fence,
586 const struct i915_sched_attr *attr)
Chris Wilson6b5e90f2016-11-14 20:41:05 +0000587{
588 /* Recurse once into a fence-array */
589 if (dma_fence_is_array(fence)) {
590 struct dma_fence_array *array = to_dma_fence_array(fence);
591 int i;
592
593 for (i = 0; i < array->num_fences; i++)
Chris Wilsonb7268c52018-04-18 19:40:52 +0100594 __fence_set_priority(array->fences[i], attr);
Chris Wilson6b5e90f2016-11-14 20:41:05 +0000595 } else {
Chris Wilsonb7268c52018-04-18 19:40:52 +0100596 __fence_set_priority(fence, attr);
Chris Wilson6b5e90f2016-11-14 20:41:05 +0000597 }
598}
599
600int
601i915_gem_object_wait_priority(struct drm_i915_gem_object *obj,
602 unsigned int flags,
Chris Wilsonb7268c52018-04-18 19:40:52 +0100603 const struct i915_sched_attr *attr)
Chris Wilson6b5e90f2016-11-14 20:41:05 +0000604{
605 struct dma_fence *excl;
606
607 if (flags & I915_WAIT_ALL) {
608 struct dma_fence **shared;
609 unsigned int count, i;
610 int ret;
611
612 ret = reservation_object_get_fences_rcu(obj->resv,
613 &excl, &count, &shared);
614 if (ret)
615 return ret;
616
617 for (i = 0; i < count; i++) {
Chris Wilsonb7268c52018-04-18 19:40:52 +0100618 fence_set_priority(shared[i], attr);
Chris Wilson6b5e90f2016-11-14 20:41:05 +0000619 dma_fence_put(shared[i]);
620 }
621
622 kfree(shared);
623 } else {
624 excl = reservation_object_get_excl_rcu(obj->resv);
625 }
626
627 if (excl) {
Chris Wilsonb7268c52018-04-18 19:40:52 +0100628 fence_set_priority(excl, attr);
Chris Wilson6b5e90f2016-11-14 20:41:05 +0000629 dma_fence_put(excl);
630 }
631 return 0;
632}
633
Chris Wilson00e60f22016-08-04 16:32:40 +0100634/**
Chris Wilsone95433c2016-10-28 13:58:27 +0100635 * Waits for rendering to the object to be completed
Chris Wilson00e60f22016-08-04 16:32:40 +0100636 * @obj: i915 gem object
Chris Wilsone95433c2016-10-28 13:58:27 +0100637 * @flags: how to wait (under a lock, for all rendering or just for writes etc)
638 * @timeout: how long to wait
Chris Wilsona0a8b1c2017-11-09 14:06:44 +0000639 * @rps_client: client (user process) to charge for any waitboosting
Chris Wilson00e60f22016-08-04 16:32:40 +0100640 */
641int
Chris Wilsone95433c2016-10-28 13:58:27 +0100642i915_gem_object_wait(struct drm_i915_gem_object *obj,
643 unsigned int flags,
644 long timeout,
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +0100645 struct intel_rps_client *rps_client)
Chris Wilson00e60f22016-08-04 16:32:40 +0100646{
Chris Wilsone95433c2016-10-28 13:58:27 +0100647 might_sleep();
648#if IS_ENABLED(CONFIG_LOCKDEP)
649 GEM_BUG_ON(debug_locks &&
650 !!lockdep_is_held(&obj->base.dev->struct_mutex) !=
651 !!(flags & I915_WAIT_LOCKED));
652#endif
653 GEM_BUG_ON(timeout < 0);
Chris Wilson00e60f22016-08-04 16:32:40 +0100654
Chris Wilsond07f0e52016-10-28 13:58:44 +0100655 timeout = i915_gem_object_wait_reservation(obj->resv,
656 flags, timeout,
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +0100657 rps_client);
Chris Wilsone95433c2016-10-28 13:58:27 +0100658 return timeout < 0 ? timeout : 0;
Chris Wilson00e60f22016-08-04 16:32:40 +0100659}
660
661static struct intel_rps_client *to_rps_client(struct drm_file *file)
662{
663 struct drm_i915_file_private *fpriv = file->driver_priv;
664
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +0100665 return &fpriv->rps_client;
Chris Wilson00e60f22016-08-04 16:32:40 +0100666}
667
Chris Wilson00731152014-05-21 12:42:56 +0100668static int
669i915_gem_phys_pwrite(struct drm_i915_gem_object *obj,
670 struct drm_i915_gem_pwrite *args,
Chris Wilson03ac84f2016-10-28 13:58:36 +0100671 struct drm_file *file)
Chris Wilson00731152014-05-21 12:42:56 +0100672{
Chris Wilson00731152014-05-21 12:42:56 +0100673 void *vaddr = obj->phys_handle->vaddr + args->offset;
Gustavo Padovan3ed605b2016-04-26 12:32:27 -0300674 char __user *user_data = u64_to_user_ptr(args->data_ptr);
Chris Wilson6a2c4232014-11-04 04:51:40 -0800675
676 /* We manually control the domain here and pretend that it
677 * remains coherent i.e. in the GTT domain, like shmem_pwrite.
678 */
Rodrigo Vivi77a0d1c2015-06-18 11:43:24 -0700679 intel_fb_obj_invalidate(obj, ORIGIN_CPU);
Chris Wilson10466d22017-01-06 15:22:38 +0000680 if (copy_from_user(vaddr, user_data, args->size))
681 return -EFAULT;
Chris Wilson00731152014-05-21 12:42:56 +0100682
Chris Wilson6a2c4232014-11-04 04:51:40 -0800683 drm_clflush_virt_range(vaddr, args->size);
Chris Wilson10466d22017-01-06 15:22:38 +0000684 i915_gem_chipset_flush(to_i915(obj->base.dev));
Paulo Zanoni063e4e62015-02-13 17:23:45 -0200685
Chris Wilsond59b21e2017-02-22 11:40:49 +0000686 intel_fb_obj_flush(obj, ORIGIN_CPU);
Chris Wilson10466d22017-01-06 15:22:38 +0000687 return 0;
Chris Wilson00731152014-05-21 12:42:56 +0100688}
689
Tvrtko Ursulin187685c2016-12-01 14:16:36 +0000690void *i915_gem_object_alloc(struct drm_i915_private *dev_priv)
Chris Wilson42dcedd2012-11-15 11:32:30 +0000691{
Chris Wilsonefab6d82015-04-07 16:20:57 +0100692 return kmem_cache_zalloc(dev_priv->objects, GFP_KERNEL);
Chris Wilson42dcedd2012-11-15 11:32:30 +0000693}
694
695void i915_gem_object_free(struct drm_i915_gem_object *obj)
696{
Chris Wilsonfac5e232016-07-04 11:34:36 +0100697 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
Chris Wilsonefab6d82015-04-07 16:20:57 +0100698 kmem_cache_free(dev_priv->objects, obj);
Chris Wilson42dcedd2012-11-15 11:32:30 +0000699}
700
Dave Airlieff72145b2011-02-07 12:16:14 +1000701static int
702i915_gem_create(struct drm_file *file,
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +0000703 struct drm_i915_private *dev_priv,
Dave Airlieff72145b2011-02-07 12:16:14 +1000704 uint64_t size,
705 uint32_t *handle_p)
Eric Anholt673a3942008-07-30 12:06:12 -0700706{
Chris Wilson05394f32010-11-08 19:18:58 +0000707 struct drm_i915_gem_object *obj;
Pekka Paalanena1a2d1d2009-08-23 12:40:55 +0300708 int ret;
709 u32 handle;
Eric Anholt673a3942008-07-30 12:06:12 -0700710
Dave Airlieff72145b2011-02-07 12:16:14 +1000711 size = roundup(size, PAGE_SIZE);
Chris Wilson8ffc0242011-09-14 14:14:28 +0200712 if (size == 0)
713 return -EINVAL;
Eric Anholt673a3942008-07-30 12:06:12 -0700714
715 /* Allocate the new object */
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +0000716 obj = i915_gem_object_create(dev_priv, size);
Chris Wilsonfe3db792016-04-25 13:32:13 +0100717 if (IS_ERR(obj))
718 return PTR_ERR(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700719
Chris Wilson05394f32010-11-08 19:18:58 +0000720 ret = drm_gem_handle_create(file, &obj->base, &handle);
Chris Wilson202f2fe2010-10-14 13:20:40 +0100721 /* drop reference from allocate - handle holds it now */
Chris Wilsonf0cd5182016-10-28 13:58:43 +0100722 i915_gem_object_put(obj);
Daniel Vetterd861e332013-07-24 23:25:03 +0200723 if (ret)
724 return ret;
Chris Wilson202f2fe2010-10-14 13:20:40 +0100725
Dave Airlieff72145b2011-02-07 12:16:14 +1000726 *handle_p = handle;
Eric Anholt673a3942008-07-30 12:06:12 -0700727 return 0;
728}
729
Dave Airlieff72145b2011-02-07 12:16:14 +1000730int
731i915_gem_dumb_create(struct drm_file *file,
732 struct drm_device *dev,
733 struct drm_mode_create_dumb *args)
734{
735 /* have to work out size/pitch and return them */
Paulo Zanonide45eaf2013-10-18 18:48:24 -0300736 args->pitch = ALIGN(args->width * DIV_ROUND_UP(args->bpp, 8), 64);
Dave Airlieff72145b2011-02-07 12:16:14 +1000737 args->size = args->pitch * args->height;
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +0000738 return i915_gem_create(file, to_i915(dev),
Dave Airlieda6b51d2014-12-24 13:11:17 +1000739 args->size, &args->handle);
Dave Airlieff72145b2011-02-07 12:16:14 +1000740}
741
Chris Wilsone27ab732017-06-15 13:38:49 +0100742static bool gpu_write_needs_clflush(struct drm_i915_gem_object *obj)
743{
744 return !(obj->cache_level == I915_CACHE_NONE ||
745 obj->cache_level == I915_CACHE_WT);
746}
747
Dave Airlieff72145b2011-02-07 12:16:14 +1000748/**
749 * Creates a new mm object and returns a handle to it.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +0100750 * @dev: drm device pointer
751 * @data: ioctl data blob
752 * @file: drm file pointer
Dave Airlieff72145b2011-02-07 12:16:14 +1000753 */
754int
755i915_gem_create_ioctl(struct drm_device *dev, void *data,
756 struct drm_file *file)
757{
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +0000758 struct drm_i915_private *dev_priv = to_i915(dev);
Dave Airlieff72145b2011-02-07 12:16:14 +1000759 struct drm_i915_gem_create *args = data;
Daniel Vetter63ed2cb2012-04-23 16:50:50 +0200760
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +0000761 i915_gem_flush_free_objects(dev_priv);
Chris Wilsonfbbd37b2016-10-28 13:58:42 +0100762
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +0000763 return i915_gem_create(file, dev_priv,
Dave Airlieda6b51d2014-12-24 13:11:17 +1000764 args->size, &args->handle);
Dave Airlieff72145b2011-02-07 12:16:14 +1000765}
766
Chris Wilsonef749212017-04-12 12:01:10 +0100767static inline enum fb_op_origin
768fb_write_origin(struct drm_i915_gem_object *obj, unsigned int domain)
769{
770 return (domain == I915_GEM_DOMAIN_GTT ?
771 obj->frontbuffer_ggtt_origin : ORIGIN_CPU);
772}
773
Chris Wilson7125397b2017-12-06 12:49:14 +0000774void i915_gem_flush_ggtt_writes(struct drm_i915_private *dev_priv)
Chris Wilsonef749212017-04-12 12:01:10 +0100775{
Chris Wilson7125397b2017-12-06 12:49:14 +0000776 /*
777 * No actual flushing is required for the GTT write domain for reads
778 * from the GTT domain. Writes to it "immediately" go to main memory
779 * as far as we know, so there's no chipset flush. It also doesn't
780 * land in the GPU render cache.
Chris Wilsonef749212017-04-12 12:01:10 +0100781 *
782 * However, we do have to enforce the order so that all writes through
783 * the GTT land before any writes to the device, such as updates to
784 * the GATT itself.
785 *
786 * We also have to wait a bit for the writes to land from the GTT.
787 * An uncached read (i.e. mmio) seems to be ideal for the round-trip
788 * timing. This issue has only been observed when switching quickly
789 * between GTT writes and CPU reads from inside the kernel on recent hw,
790 * and it appears to only affect discrete GTT blocks (i.e. on LLC
Chris Wilson7125397b2017-12-06 12:49:14 +0000791 * system agents we cannot reproduce this behaviour, until Cannonlake
792 * that was!).
Chris Wilsonef749212017-04-12 12:01:10 +0100793 */
Chris Wilson7125397b2017-12-06 12:49:14 +0000794
Chris Wilsonef749212017-04-12 12:01:10 +0100795 wmb();
796
Chris Wilson7125397b2017-12-06 12:49:14 +0000797 intel_runtime_pm_get(dev_priv);
798 spin_lock_irq(&dev_priv->uncore.lock);
799
800 POSTING_READ_FW(RING_HEAD(RENDER_RING_BASE));
801
802 spin_unlock_irq(&dev_priv->uncore.lock);
803 intel_runtime_pm_put(dev_priv);
804}
805
806static void
807flush_write_domain(struct drm_i915_gem_object *obj, unsigned int flush_domains)
808{
809 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
810 struct i915_vma *vma;
811
Christian Königc0a51fd2018-02-16 13:43:38 +0100812 if (!(obj->write_domain & flush_domains))
Chris Wilson7125397b2017-12-06 12:49:14 +0000813 return;
814
Christian Königc0a51fd2018-02-16 13:43:38 +0100815 switch (obj->write_domain) {
Chris Wilsonef749212017-04-12 12:01:10 +0100816 case I915_GEM_DOMAIN_GTT:
Chris Wilson7125397b2017-12-06 12:49:14 +0000817 i915_gem_flush_ggtt_writes(dev_priv);
Chris Wilsonef749212017-04-12 12:01:10 +0100818
819 intel_fb_obj_flush(obj,
820 fb_write_origin(obj, I915_GEM_DOMAIN_GTT));
Chris Wilson7125397b2017-12-06 12:49:14 +0000821
Chris Wilsone2189dd2017-12-07 21:14:07 +0000822 for_each_ggtt_vma(vma, obj) {
Chris Wilson7125397b2017-12-06 12:49:14 +0000823 if (vma->iomap)
824 continue;
825
826 i915_vma_unset_ggtt_write(vma);
827 }
Chris Wilsonef749212017-04-12 12:01:10 +0100828 break;
829
830 case I915_GEM_DOMAIN_CPU:
831 i915_gem_clflush_object(obj, I915_CLFLUSH_SYNC);
832 break;
Chris Wilsone27ab732017-06-15 13:38:49 +0100833
834 case I915_GEM_DOMAIN_RENDER:
835 if (gpu_write_needs_clflush(obj))
836 obj->cache_dirty = true;
837 break;
Chris Wilsonef749212017-04-12 12:01:10 +0100838 }
839
Christian Königc0a51fd2018-02-16 13:43:38 +0100840 obj->write_domain = 0;
Chris Wilsonef749212017-04-12 12:01:10 +0100841}
842
Daniel Vetter8c599672011-12-14 13:57:31 +0100843static inline int
Daniel Vetter8461d222011-12-14 13:57:32 +0100844__copy_to_user_swizzled(char __user *cpu_vaddr,
845 const char *gpu_vaddr, int gpu_offset,
846 int length)
847{
848 int ret, cpu_offset = 0;
849
850 while (length > 0) {
851 int cacheline_end = ALIGN(gpu_offset + 1, 64);
852 int this_length = min(cacheline_end - gpu_offset, length);
853 int swizzled_gpu_offset = gpu_offset ^ 64;
854
855 ret = __copy_to_user(cpu_vaddr + cpu_offset,
856 gpu_vaddr + swizzled_gpu_offset,
857 this_length);
858 if (ret)
859 return ret + length;
860
861 cpu_offset += this_length;
862 gpu_offset += this_length;
863 length -= this_length;
864 }
865
866 return 0;
867}
868
869static inline int
Ben Widawsky4f0c7cf2012-04-16 14:07:47 -0700870__copy_from_user_swizzled(char *gpu_vaddr, int gpu_offset,
871 const char __user *cpu_vaddr,
Daniel Vetter8c599672011-12-14 13:57:31 +0100872 int length)
873{
874 int ret, cpu_offset = 0;
875
876 while (length > 0) {
877 int cacheline_end = ALIGN(gpu_offset + 1, 64);
878 int this_length = min(cacheline_end - gpu_offset, length);
879 int swizzled_gpu_offset = gpu_offset ^ 64;
880
881 ret = __copy_from_user(gpu_vaddr + swizzled_gpu_offset,
882 cpu_vaddr + cpu_offset,
883 this_length);
884 if (ret)
885 return ret + length;
886
887 cpu_offset += this_length;
888 gpu_offset += this_length;
889 length -= this_length;
890 }
891
892 return 0;
893}
894
Brad Volkin4c914c02014-02-18 10:15:45 -0800895/*
896 * Pins the specified object's pages and synchronizes the object with
897 * GPU accesses. Sets needs_clflush to non-zero if the caller should
898 * flush the object from the CPU cache.
899 */
900int i915_gem_obj_prepare_shmem_read(struct drm_i915_gem_object *obj,
Chris Wilson43394c72016-08-18 17:16:47 +0100901 unsigned int *needs_clflush)
Brad Volkin4c914c02014-02-18 10:15:45 -0800902{
903 int ret;
904
Chris Wilsone95433c2016-10-28 13:58:27 +0100905 lockdep_assert_held(&obj->base.dev->struct_mutex);
Brad Volkin4c914c02014-02-18 10:15:45 -0800906
Chris Wilsone95433c2016-10-28 13:58:27 +0100907 *needs_clflush = 0;
Chris Wilson43394c72016-08-18 17:16:47 +0100908 if (!i915_gem_object_has_struct_page(obj))
909 return -ENODEV;
Brad Volkin4c914c02014-02-18 10:15:45 -0800910
Chris Wilsone95433c2016-10-28 13:58:27 +0100911 ret = i915_gem_object_wait(obj,
912 I915_WAIT_INTERRUPTIBLE |
913 I915_WAIT_LOCKED,
914 MAX_SCHEDULE_TIMEOUT,
915 NULL);
Chris Wilsonc13d87e2016-07-20 09:21:15 +0100916 if (ret)
917 return ret;
918
Chris Wilsona4f5ea62016-10-28 13:58:35 +0100919 ret = i915_gem_object_pin_pages(obj);
Chris Wilson97649512016-08-18 17:16:50 +0100920 if (ret)
921 return ret;
922
Chris Wilsonb8f55be2017-08-11 12:11:16 +0100923 if (obj->cache_coherent & I915_BO_CACHE_COHERENT_FOR_READ ||
924 !static_cpu_has(X86_FEATURE_CLFLUSH)) {
Chris Wilson7f5f95d2017-03-10 00:09:42 +0000925 ret = i915_gem_object_set_to_cpu_domain(obj, false);
926 if (ret)
927 goto err_unpin;
928 else
929 goto out;
930 }
931
Chris Wilsonef749212017-04-12 12:01:10 +0100932 flush_write_domain(obj, ~I915_GEM_DOMAIN_CPU);
Chris Wilsona314d5c2016-08-18 17:16:48 +0100933
Chris Wilson43394c72016-08-18 17:16:47 +0100934 /* If we're not in the cpu read domain, set ourself into the gtt
935 * read domain and manually flush cachelines (if required). This
936 * optimizes for the case when the gpu will dirty the data
937 * anyway again before the next pread happens.
938 */
Chris Wilsone27ab732017-06-15 13:38:49 +0100939 if (!obj->cache_dirty &&
Christian Königc0a51fd2018-02-16 13:43:38 +0100940 !(obj->read_domains & I915_GEM_DOMAIN_CPU))
Chris Wilson7f5f95d2017-03-10 00:09:42 +0000941 *needs_clflush = CLFLUSH_BEFORE;
Brad Volkin4c914c02014-02-18 10:15:45 -0800942
Chris Wilson7f5f95d2017-03-10 00:09:42 +0000943out:
Chris Wilson97649512016-08-18 17:16:50 +0100944 /* return with the pages pinned */
Chris Wilson43394c72016-08-18 17:16:47 +0100945 return 0;
Chris Wilson97649512016-08-18 17:16:50 +0100946
947err_unpin:
948 i915_gem_object_unpin_pages(obj);
949 return ret;
Chris Wilson43394c72016-08-18 17:16:47 +0100950}
951
952int i915_gem_obj_prepare_shmem_write(struct drm_i915_gem_object *obj,
953 unsigned int *needs_clflush)
954{
955 int ret;
956
Chris Wilsone95433c2016-10-28 13:58:27 +0100957 lockdep_assert_held(&obj->base.dev->struct_mutex);
958
Chris Wilson43394c72016-08-18 17:16:47 +0100959 *needs_clflush = 0;
960 if (!i915_gem_object_has_struct_page(obj))
961 return -ENODEV;
962
Chris Wilsone95433c2016-10-28 13:58:27 +0100963 ret = i915_gem_object_wait(obj,
964 I915_WAIT_INTERRUPTIBLE |
965 I915_WAIT_LOCKED |
966 I915_WAIT_ALL,
967 MAX_SCHEDULE_TIMEOUT,
968 NULL);
Chris Wilson43394c72016-08-18 17:16:47 +0100969 if (ret)
970 return ret;
971
Chris Wilsona4f5ea62016-10-28 13:58:35 +0100972 ret = i915_gem_object_pin_pages(obj);
Chris Wilson97649512016-08-18 17:16:50 +0100973 if (ret)
974 return ret;
975
Chris Wilsonb8f55be2017-08-11 12:11:16 +0100976 if (obj->cache_coherent & I915_BO_CACHE_COHERENT_FOR_WRITE ||
977 !static_cpu_has(X86_FEATURE_CLFLUSH)) {
Chris Wilson7f5f95d2017-03-10 00:09:42 +0000978 ret = i915_gem_object_set_to_cpu_domain(obj, true);
979 if (ret)
980 goto err_unpin;
981 else
982 goto out;
983 }
984
Chris Wilsonef749212017-04-12 12:01:10 +0100985 flush_write_domain(obj, ~I915_GEM_DOMAIN_CPU);
Chris Wilsona314d5c2016-08-18 17:16:48 +0100986
Chris Wilson43394c72016-08-18 17:16:47 +0100987 /* If we're not in the cpu write domain, set ourself into the
988 * gtt write domain and manually flush cachelines (as required).
989 * This optimizes for the case when the gpu will use the data
990 * right away and we therefore have to clflush anyway.
991 */
Chris Wilsone27ab732017-06-15 13:38:49 +0100992 if (!obj->cache_dirty) {
Chris Wilson7f5f95d2017-03-10 00:09:42 +0000993 *needs_clflush |= CLFLUSH_AFTER;
Chris Wilson43394c72016-08-18 17:16:47 +0100994
Chris Wilsone27ab732017-06-15 13:38:49 +0100995 /*
996 * Same trick applies to invalidate partially written
997 * cachelines read before writing.
998 */
Christian Königc0a51fd2018-02-16 13:43:38 +0100999 if (!(obj->read_domains & I915_GEM_DOMAIN_CPU))
Chris Wilsone27ab732017-06-15 13:38:49 +01001000 *needs_clflush |= CLFLUSH_BEFORE;
1001 }
Chris Wilson43394c72016-08-18 17:16:47 +01001002
Chris Wilson7f5f95d2017-03-10 00:09:42 +00001003out:
Chris Wilson43394c72016-08-18 17:16:47 +01001004 intel_fb_obj_invalidate(obj, ORIGIN_CPU);
Chris Wilsona4f5ea62016-10-28 13:58:35 +01001005 obj->mm.dirty = true;
Chris Wilson97649512016-08-18 17:16:50 +01001006 /* return with the pages pinned */
Chris Wilson43394c72016-08-18 17:16:47 +01001007 return 0;
Chris Wilson97649512016-08-18 17:16:50 +01001008
1009err_unpin:
1010 i915_gem_object_unpin_pages(obj);
1011 return ret;
Brad Volkin4c914c02014-02-18 10:15:45 -08001012}
1013
Daniel Vetter23c18c72012-03-25 19:47:42 +02001014static void
1015shmem_clflush_swizzled_range(char *addr, unsigned long length,
1016 bool swizzled)
1017{
Daniel Vettere7e58eb2012-03-25 19:47:43 +02001018 if (unlikely(swizzled)) {
Daniel Vetter23c18c72012-03-25 19:47:42 +02001019 unsigned long start = (unsigned long) addr;
1020 unsigned long end = (unsigned long) addr + length;
1021
1022 /* For swizzling simply ensure that we always flush both
1023 * channels. Lame, but simple and it works. Swizzled
1024 * pwrite/pread is far from a hotpath - current userspace
1025 * doesn't use it at all. */
1026 start = round_down(start, 128);
1027 end = round_up(end, 128);
1028
1029 drm_clflush_virt_range((void *)start, end - start);
1030 } else {
1031 drm_clflush_virt_range(addr, length);
1032 }
1033
1034}
1035
Daniel Vetterd174bd62012-03-25 19:47:40 +02001036/* Only difference to the fast-path function is that this can handle bit17
1037 * and uses non-atomic copy and kmap functions. */
1038static int
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001039shmem_pread_slow(struct page *page, int offset, int length,
Daniel Vetterd174bd62012-03-25 19:47:40 +02001040 char __user *user_data,
1041 bool page_do_bit17_swizzling, bool needs_clflush)
1042{
1043 char *vaddr;
1044 int ret;
1045
1046 vaddr = kmap(page);
1047 if (needs_clflush)
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001048 shmem_clflush_swizzled_range(vaddr + offset, length,
Daniel Vetter23c18c72012-03-25 19:47:42 +02001049 page_do_bit17_swizzling);
Daniel Vetterd174bd62012-03-25 19:47:40 +02001050
1051 if (page_do_bit17_swizzling)
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001052 ret = __copy_to_user_swizzled(user_data, vaddr, offset, length);
Daniel Vetterd174bd62012-03-25 19:47:40 +02001053 else
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001054 ret = __copy_to_user(user_data, vaddr + offset, length);
Daniel Vetterd174bd62012-03-25 19:47:40 +02001055 kunmap(page);
1056
Chris Wilsonf60d7f02012-09-04 21:02:56 +01001057 return ret ? - EFAULT : 0;
Daniel Vetterd174bd62012-03-25 19:47:40 +02001058}
1059
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001060static int
1061shmem_pread(struct page *page, int offset, int length, char __user *user_data,
1062 bool page_do_bit17_swizzling, bool needs_clflush)
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301063{
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001064 int ret;
1065
1066 ret = -ENODEV;
1067 if (!page_do_bit17_swizzling) {
1068 char *vaddr = kmap_atomic(page);
1069
1070 if (needs_clflush)
1071 drm_clflush_virt_range(vaddr + offset, length);
1072 ret = __copy_to_user_inatomic(user_data, vaddr + offset, length);
1073 kunmap_atomic(vaddr);
1074 }
1075 if (ret == 0)
1076 return 0;
1077
1078 return shmem_pread_slow(page, offset, length, user_data,
1079 page_do_bit17_swizzling, needs_clflush);
1080}
1081
1082static int
1083i915_gem_shmem_pread(struct drm_i915_gem_object *obj,
1084 struct drm_i915_gem_pread *args)
1085{
1086 char __user *user_data;
1087 u64 remain;
1088 unsigned int obj_do_bit17_swizzling;
1089 unsigned int needs_clflush;
1090 unsigned int idx, offset;
1091 int ret;
1092
1093 obj_do_bit17_swizzling = 0;
1094 if (i915_gem_object_needs_bit17_swizzle(obj))
1095 obj_do_bit17_swizzling = BIT(17);
1096
1097 ret = mutex_lock_interruptible(&obj->base.dev->struct_mutex);
1098 if (ret)
1099 return ret;
1100
1101 ret = i915_gem_obj_prepare_shmem_read(obj, &needs_clflush);
1102 mutex_unlock(&obj->base.dev->struct_mutex);
1103 if (ret)
1104 return ret;
1105
1106 remain = args->size;
1107 user_data = u64_to_user_ptr(args->data_ptr);
1108 offset = offset_in_page(args->offset);
1109 for (idx = args->offset >> PAGE_SHIFT; remain; idx++) {
1110 struct page *page = i915_gem_object_get_page(obj, idx);
1111 int length;
1112
1113 length = remain;
1114 if (offset + length > PAGE_SIZE)
1115 length = PAGE_SIZE - offset;
1116
1117 ret = shmem_pread(page, offset, length, user_data,
1118 page_to_phys(page) & obj_do_bit17_swizzling,
1119 needs_clflush);
1120 if (ret)
1121 break;
1122
1123 remain -= length;
1124 user_data += length;
1125 offset = 0;
1126 }
1127
1128 i915_gem_obj_finish_shmem_access(obj);
1129 return ret;
1130}
1131
1132static inline bool
1133gtt_user_read(struct io_mapping *mapping,
1134 loff_t base, int offset,
1135 char __user *user_data, int length)
1136{
Ville Syrjäläafe722b2017-09-01 20:12:52 +03001137 void __iomem *vaddr;
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001138 unsigned long unwritten;
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301139
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301140 /* We can use the cpu mem copy function because this is X86. */
Ville Syrjäläafe722b2017-09-01 20:12:52 +03001141 vaddr = io_mapping_map_atomic_wc(mapping, base);
1142 unwritten = __copy_to_user_inatomic(user_data,
1143 (void __force *)vaddr + offset,
1144 length);
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001145 io_mapping_unmap_atomic(vaddr);
1146 if (unwritten) {
Ville Syrjäläafe722b2017-09-01 20:12:52 +03001147 vaddr = io_mapping_map_wc(mapping, base, PAGE_SIZE);
1148 unwritten = copy_to_user(user_data,
1149 (void __force *)vaddr + offset,
1150 length);
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001151 io_mapping_unmap(vaddr);
1152 }
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301153 return unwritten;
1154}
1155
1156static int
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001157i915_gem_gtt_pread(struct drm_i915_gem_object *obj,
1158 const struct drm_i915_gem_pread *args)
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301159{
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001160 struct drm_i915_private *i915 = to_i915(obj->base.dev);
1161 struct i915_ggtt *ggtt = &i915->ggtt;
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301162 struct drm_mm_node node;
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001163 struct i915_vma *vma;
1164 void __user *user_data;
1165 u64 remain, offset;
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301166 int ret;
1167
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001168 ret = mutex_lock_interruptible(&i915->drm.struct_mutex);
1169 if (ret)
1170 return ret;
1171
1172 intel_runtime_pm_get(i915);
1173 vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0,
Chris Wilsona3259ca2017-10-09 09:44:00 +01001174 PIN_MAPPABLE |
1175 PIN_NONFAULT |
1176 PIN_NONBLOCK);
Chris Wilson18034582016-08-18 17:16:45 +01001177 if (!IS_ERR(vma)) {
1178 node.start = i915_ggtt_offset(vma);
1179 node.allocated = false;
Chris Wilson49ef5292016-08-18 17:17:00 +01001180 ret = i915_vma_put_fence(vma);
Chris Wilson18034582016-08-18 17:16:45 +01001181 if (ret) {
1182 i915_vma_unpin(vma);
1183 vma = ERR_PTR(ret);
1184 }
1185 }
Chris Wilson058d88c2016-08-15 10:49:06 +01001186 if (IS_ERR(vma)) {
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001187 ret = insert_mappable_node(ggtt, &node, PAGE_SIZE);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301188 if (ret)
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001189 goto out_unlock;
1190 GEM_BUG_ON(!node.allocated);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301191 }
1192
1193 ret = i915_gem_object_set_to_gtt_domain(obj, false);
1194 if (ret)
1195 goto out_unpin;
1196
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001197 mutex_unlock(&i915->drm.struct_mutex);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301198
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001199 user_data = u64_to_user_ptr(args->data_ptr);
1200 remain = args->size;
1201 offset = args->offset;
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301202
1203 while (remain > 0) {
1204 /* Operation in this page
1205 *
1206 * page_base = page offset within aperture
1207 * page_offset = offset within page
1208 * page_length = bytes to copy for this page
1209 */
1210 u32 page_base = node.start;
1211 unsigned page_offset = offset_in_page(offset);
1212 unsigned page_length = PAGE_SIZE - page_offset;
1213 page_length = remain < page_length ? remain : page_length;
1214 if (node.allocated) {
1215 wmb();
1216 ggtt->base.insert_page(&ggtt->base,
1217 i915_gem_object_get_dma_address(obj, offset >> PAGE_SHIFT),
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001218 node.start, I915_CACHE_NONE, 0);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301219 wmb();
1220 } else {
1221 page_base += offset & PAGE_MASK;
1222 }
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001223
Matthew Auld73ebd502017-12-11 15:18:20 +00001224 if (gtt_user_read(&ggtt->iomap, page_base, page_offset,
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001225 user_data, page_length)) {
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301226 ret = -EFAULT;
1227 break;
1228 }
1229
1230 remain -= page_length;
1231 user_data += page_length;
1232 offset += page_length;
1233 }
1234
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001235 mutex_lock(&i915->drm.struct_mutex);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301236out_unpin:
1237 if (node.allocated) {
1238 wmb();
1239 ggtt->base.clear_range(&ggtt->base,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02001240 node.start, node.size);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301241 remove_mappable_node(&node);
1242 } else {
Chris Wilson058d88c2016-08-15 10:49:06 +01001243 i915_vma_unpin(vma);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301244 }
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001245out_unlock:
1246 intel_runtime_pm_put(i915);
1247 mutex_unlock(&i915->drm.struct_mutex);
Chris Wilsonf60d7f02012-09-04 21:02:56 +01001248
Eric Anholteb014592009-03-10 11:44:52 -07001249 return ret;
1250}
1251
Eric Anholt673a3942008-07-30 12:06:12 -07001252/**
1253 * Reads data from the object referenced by handle.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01001254 * @dev: drm device pointer
1255 * @data: ioctl data blob
1256 * @file: drm file pointer
Eric Anholt673a3942008-07-30 12:06:12 -07001257 *
1258 * On error, the contents of *data are undefined.
1259 */
1260int
1261i915_gem_pread_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00001262 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001263{
1264 struct drm_i915_gem_pread *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00001265 struct drm_i915_gem_object *obj;
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001266 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07001267
Chris Wilson51311d02010-11-17 09:10:42 +00001268 if (args->size == 0)
1269 return 0;
1270
1271 if (!access_ok(VERIFY_WRITE,
Gustavo Padovan3ed605b2016-04-26 12:32:27 -03001272 u64_to_user_ptr(args->data_ptr),
Chris Wilson51311d02010-11-17 09:10:42 +00001273 args->size))
1274 return -EFAULT;
1275
Chris Wilson03ac0642016-07-20 13:31:51 +01001276 obj = i915_gem_object_lookup(file, args->handle);
Chris Wilson258a5ed2016-08-05 10:14:16 +01001277 if (!obj)
1278 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07001279
Chris Wilson7dcd2492010-09-26 20:21:44 +01001280 /* Bounds check source. */
Matthew Auld966d5bf2016-12-13 20:32:22 +00001281 if (range_overflows_t(u64, args->offset, args->size, obj->base.size)) {
Chris Wilsonce9d4192010-09-26 20:50:05 +01001282 ret = -EINVAL;
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001283 goto out;
Chris Wilsonce9d4192010-09-26 20:50:05 +01001284 }
1285
Chris Wilsondb53a302011-02-03 11:57:46 +00001286 trace_i915_gem_object_pread(obj, args->offset, args->size);
1287
Chris Wilsone95433c2016-10-28 13:58:27 +01001288 ret = i915_gem_object_wait(obj,
1289 I915_WAIT_INTERRUPTIBLE,
1290 MAX_SCHEDULE_TIMEOUT,
1291 to_rps_client(file));
Chris Wilson258a5ed2016-08-05 10:14:16 +01001292 if (ret)
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001293 goto out;
Chris Wilson258a5ed2016-08-05 10:14:16 +01001294
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001295 ret = i915_gem_object_pin_pages(obj);
Chris Wilson258a5ed2016-08-05 10:14:16 +01001296 if (ret)
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001297 goto out;
Chris Wilson258a5ed2016-08-05 10:14:16 +01001298
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001299 ret = i915_gem_shmem_pread(obj, args);
Chris Wilson9c870d02016-10-24 13:42:15 +01001300 if (ret == -EFAULT || ret == -ENODEV)
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001301 ret = i915_gem_gtt_pread(obj, args);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301302
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001303 i915_gem_object_unpin_pages(obj);
1304out:
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001305 i915_gem_object_put(obj);
Eric Anholteb014592009-03-10 11:44:52 -07001306 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07001307}
1308
Keith Packard0839ccb2008-10-30 19:38:48 -07001309/* This is the fast write path which cannot handle
1310 * page faults in the source data
Linus Torvalds9b7530cc2008-10-20 14:16:43 -07001311 */
Linus Torvalds9b7530cc2008-10-20 14:16:43 -07001312
Chris Wilsonfe115622016-10-28 13:58:40 +01001313static inline bool
1314ggtt_write(struct io_mapping *mapping,
1315 loff_t base, int offset,
1316 char __user *user_data, int length)
Keith Packard0839ccb2008-10-30 19:38:48 -07001317{
Ville Syrjäläafe722b2017-09-01 20:12:52 +03001318 void __iomem *vaddr;
Keith Packard0839ccb2008-10-30 19:38:48 -07001319 unsigned long unwritten;
1320
Ben Widawsky4f0c7cf2012-04-16 14:07:47 -07001321 /* We can use the cpu mem copy function because this is X86. */
Ville Syrjäläafe722b2017-09-01 20:12:52 +03001322 vaddr = io_mapping_map_atomic_wc(mapping, base);
1323 unwritten = __copy_from_user_inatomic_nocache((void __force *)vaddr + offset,
Keith Packard0839ccb2008-10-30 19:38:48 -07001324 user_data, length);
Chris Wilsonfe115622016-10-28 13:58:40 +01001325 io_mapping_unmap_atomic(vaddr);
1326 if (unwritten) {
Ville Syrjäläafe722b2017-09-01 20:12:52 +03001327 vaddr = io_mapping_map_wc(mapping, base, PAGE_SIZE);
1328 unwritten = copy_from_user((void __force *)vaddr + offset,
1329 user_data, length);
Chris Wilsonfe115622016-10-28 13:58:40 +01001330 io_mapping_unmap(vaddr);
1331 }
Keith Packard0839ccb2008-10-30 19:38:48 -07001332
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001333 return unwritten;
1334}
1335
Eric Anholt3de09aa2009-03-09 09:42:23 -07001336/**
1337 * This is the fast pwrite path, where we copy the data directly from the
1338 * user into the GTT, uncached.
Chris Wilsonfe115622016-10-28 13:58:40 +01001339 * @obj: i915 GEM object
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01001340 * @args: pwrite arguments structure
Eric Anholt3de09aa2009-03-09 09:42:23 -07001341 */
Eric Anholt673a3942008-07-30 12:06:12 -07001342static int
Chris Wilsonfe115622016-10-28 13:58:40 +01001343i915_gem_gtt_pwrite_fast(struct drm_i915_gem_object *obj,
1344 const struct drm_i915_gem_pwrite *args)
Eric Anholt673a3942008-07-30 12:06:12 -07001345{
Chris Wilsonfe115622016-10-28 13:58:40 +01001346 struct drm_i915_private *i915 = to_i915(obj->base.dev);
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301347 struct i915_ggtt *ggtt = &i915->ggtt;
1348 struct drm_mm_node node;
Chris Wilsonfe115622016-10-28 13:58:40 +01001349 struct i915_vma *vma;
1350 u64 remain, offset;
1351 void __user *user_data;
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301352 int ret;
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301353
Chris Wilsonfe115622016-10-28 13:58:40 +01001354 ret = mutex_lock_interruptible(&i915->drm.struct_mutex);
1355 if (ret)
1356 return ret;
Daniel Vetter935aaa62012-03-25 19:47:35 +02001357
Chris Wilson8bd818152017-10-19 07:37:33 +01001358 if (i915_gem_object_has_struct_page(obj)) {
1359 /*
1360 * Avoid waking the device up if we can fallback, as
1361 * waking/resuming is very slow (worst-case 10-100 ms
1362 * depending on PCI sleeps and our own resume time).
1363 * This easily dwarfs any performance advantage from
1364 * using the cache bypass of indirect GGTT access.
1365 */
1366 if (!intel_runtime_pm_get_if_in_use(i915)) {
1367 ret = -EFAULT;
1368 goto out_unlock;
1369 }
1370 } else {
1371 /* No backing pages, no fallback, we must force GGTT access */
1372 intel_runtime_pm_get(i915);
1373 }
1374
Chris Wilson058d88c2016-08-15 10:49:06 +01001375 vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0,
Chris Wilsona3259ca2017-10-09 09:44:00 +01001376 PIN_MAPPABLE |
1377 PIN_NONFAULT |
1378 PIN_NONBLOCK);
Chris Wilson18034582016-08-18 17:16:45 +01001379 if (!IS_ERR(vma)) {
1380 node.start = i915_ggtt_offset(vma);
1381 node.allocated = false;
Chris Wilson49ef5292016-08-18 17:17:00 +01001382 ret = i915_vma_put_fence(vma);
Chris Wilson18034582016-08-18 17:16:45 +01001383 if (ret) {
1384 i915_vma_unpin(vma);
1385 vma = ERR_PTR(ret);
1386 }
1387 }
Chris Wilson058d88c2016-08-15 10:49:06 +01001388 if (IS_ERR(vma)) {
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001389 ret = insert_mappable_node(ggtt, &node, PAGE_SIZE);
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301390 if (ret)
Chris Wilson8bd818152017-10-19 07:37:33 +01001391 goto out_rpm;
Chris Wilsonfe115622016-10-28 13:58:40 +01001392 GEM_BUG_ON(!node.allocated);
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301393 }
Daniel Vetter935aaa62012-03-25 19:47:35 +02001394
1395 ret = i915_gem_object_set_to_gtt_domain(obj, true);
1396 if (ret)
1397 goto out_unpin;
1398
Chris Wilsonfe115622016-10-28 13:58:40 +01001399 mutex_unlock(&i915->drm.struct_mutex);
1400
Chris Wilsonb19482d2016-08-18 17:16:43 +01001401 intel_fb_obj_invalidate(obj, ORIGIN_CPU);
Paulo Zanoni063e4e62015-02-13 17:23:45 -02001402
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301403 user_data = u64_to_user_ptr(args->data_ptr);
1404 offset = args->offset;
1405 remain = args->size;
1406 while (remain) {
Eric Anholt673a3942008-07-30 12:06:12 -07001407 /* Operation in this page
1408 *
Keith Packard0839ccb2008-10-30 19:38:48 -07001409 * page_base = page offset within aperture
1410 * page_offset = offset within page
1411 * page_length = bytes to copy for this page
Eric Anholt673a3942008-07-30 12:06:12 -07001412 */
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301413 u32 page_base = node.start;
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001414 unsigned int page_offset = offset_in_page(offset);
1415 unsigned int page_length = PAGE_SIZE - page_offset;
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301416 page_length = remain < page_length ? remain : page_length;
1417 if (node.allocated) {
1418 wmb(); /* flush the write before we modify the GGTT */
1419 ggtt->base.insert_page(&ggtt->base,
1420 i915_gem_object_get_dma_address(obj, offset >> PAGE_SHIFT),
1421 node.start, I915_CACHE_NONE, 0);
1422 wmb(); /* flush modifications to the GGTT (insert_page) */
1423 } else {
1424 page_base += offset & PAGE_MASK;
1425 }
Keith Packard0839ccb2008-10-30 19:38:48 -07001426 /* If we get a fault while copying data, then (presumably) our
Eric Anholt3de09aa2009-03-09 09:42:23 -07001427 * source page isn't available. Return the error and we'll
1428 * retry in the slow path.
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301429 * If the object is non-shmem backed, we retry again with the
1430 * path that handles page fault.
Keith Packard0839ccb2008-10-30 19:38:48 -07001431 */
Matthew Auld73ebd502017-12-11 15:18:20 +00001432 if (ggtt_write(&ggtt->iomap, page_base, page_offset,
Chris Wilsonfe115622016-10-28 13:58:40 +01001433 user_data, page_length)) {
1434 ret = -EFAULT;
1435 break;
Daniel Vetter935aaa62012-03-25 19:47:35 +02001436 }
Eric Anholt673a3942008-07-30 12:06:12 -07001437
Keith Packard0839ccb2008-10-30 19:38:48 -07001438 remain -= page_length;
1439 user_data += page_length;
1440 offset += page_length;
Eric Anholt673a3942008-07-30 12:06:12 -07001441 }
Chris Wilsond59b21e2017-02-22 11:40:49 +00001442 intel_fb_obj_flush(obj, ORIGIN_CPU);
Chris Wilsonfe115622016-10-28 13:58:40 +01001443
1444 mutex_lock(&i915->drm.struct_mutex);
Daniel Vetter935aaa62012-03-25 19:47:35 +02001445out_unpin:
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301446 if (node.allocated) {
1447 wmb();
1448 ggtt->base.clear_range(&ggtt->base,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02001449 node.start, node.size);
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301450 remove_mappable_node(&node);
1451 } else {
Chris Wilson058d88c2016-08-15 10:49:06 +01001452 i915_vma_unpin(vma);
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301453 }
Chris Wilson8bd818152017-10-19 07:37:33 +01001454out_rpm:
Chris Wilson9c870d02016-10-24 13:42:15 +01001455 intel_runtime_pm_put(i915);
Chris Wilson8bd818152017-10-19 07:37:33 +01001456out_unlock:
Chris Wilsonfe115622016-10-28 13:58:40 +01001457 mutex_unlock(&i915->drm.struct_mutex);
Eric Anholt3de09aa2009-03-09 09:42:23 -07001458 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07001459}
1460
Eric Anholt673a3942008-07-30 12:06:12 -07001461static int
Chris Wilsonfe115622016-10-28 13:58:40 +01001462shmem_pwrite_slow(struct page *page, int offset, int length,
Daniel Vetterd174bd62012-03-25 19:47:40 +02001463 char __user *user_data,
1464 bool page_do_bit17_swizzling,
1465 bool needs_clflush_before,
1466 bool needs_clflush_after)
Eric Anholt673a3942008-07-30 12:06:12 -07001467{
Daniel Vetterd174bd62012-03-25 19:47:40 +02001468 char *vaddr;
1469 int ret;
Eric Anholt40123c12009-03-09 13:42:30 -07001470
Daniel Vetterd174bd62012-03-25 19:47:40 +02001471 vaddr = kmap(page);
Daniel Vettere7e58eb2012-03-25 19:47:43 +02001472 if (unlikely(needs_clflush_before || page_do_bit17_swizzling))
Chris Wilsonfe115622016-10-28 13:58:40 +01001473 shmem_clflush_swizzled_range(vaddr + offset, length,
Daniel Vetter23c18c72012-03-25 19:47:42 +02001474 page_do_bit17_swizzling);
Daniel Vetterd174bd62012-03-25 19:47:40 +02001475 if (page_do_bit17_swizzling)
Chris Wilsonfe115622016-10-28 13:58:40 +01001476 ret = __copy_from_user_swizzled(vaddr, offset, user_data,
1477 length);
Daniel Vetterd174bd62012-03-25 19:47:40 +02001478 else
Chris Wilsonfe115622016-10-28 13:58:40 +01001479 ret = __copy_from_user(vaddr + offset, user_data, length);
Daniel Vetterd174bd62012-03-25 19:47:40 +02001480 if (needs_clflush_after)
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 kunmap(page);
Chris Wilsone5281cc2010-10-28 13:45:36 +01001484
Chris Wilson755d2212012-09-04 21:02:55 +01001485 return ret ? -EFAULT : 0;
Eric Anholt40123c12009-03-09 13:42:30 -07001486}
1487
Chris Wilsonfe115622016-10-28 13:58:40 +01001488/* Per-page copy function for the shmem pwrite fastpath.
1489 * Flushes invalid cachelines before writing to the target if
1490 * needs_clflush_before is set and flushes out any written cachelines after
1491 * writing if needs_clflush is set.
1492 */
Eric Anholt40123c12009-03-09 13:42:30 -07001493static int
Chris Wilsonfe115622016-10-28 13:58:40 +01001494shmem_pwrite(struct page *page, int offset, int len, char __user *user_data,
1495 bool page_do_bit17_swizzling,
1496 bool needs_clflush_before,
1497 bool needs_clflush_after)
Eric Anholt40123c12009-03-09 13:42:30 -07001498{
Chris Wilsonfe115622016-10-28 13:58:40 +01001499 int ret;
Eric Anholt40123c12009-03-09 13:42:30 -07001500
Chris Wilsonfe115622016-10-28 13:58:40 +01001501 ret = -ENODEV;
1502 if (!page_do_bit17_swizzling) {
1503 char *vaddr = kmap_atomic(page);
1504
1505 if (needs_clflush_before)
1506 drm_clflush_virt_range(vaddr + offset, len);
1507 ret = __copy_from_user_inatomic(vaddr + offset, user_data, len);
1508 if (needs_clflush_after)
1509 drm_clflush_virt_range(vaddr + offset, len);
1510
1511 kunmap_atomic(vaddr);
1512 }
1513 if (ret == 0)
1514 return ret;
1515
1516 return shmem_pwrite_slow(page, offset, len, user_data,
1517 page_do_bit17_swizzling,
1518 needs_clflush_before,
1519 needs_clflush_after);
1520}
1521
1522static int
1523i915_gem_shmem_pwrite(struct drm_i915_gem_object *obj,
1524 const struct drm_i915_gem_pwrite *args)
1525{
1526 struct drm_i915_private *i915 = to_i915(obj->base.dev);
1527 void __user *user_data;
1528 u64 remain;
1529 unsigned int obj_do_bit17_swizzling;
1530 unsigned int partial_cacheline_write;
1531 unsigned int needs_clflush;
1532 unsigned int offset, idx;
1533 int ret;
1534
1535 ret = mutex_lock_interruptible(&i915->drm.struct_mutex);
Chris Wilson43394c72016-08-18 17:16:47 +01001536 if (ret)
1537 return ret;
Eric Anholt40123c12009-03-09 13:42:30 -07001538
Chris Wilsonfe115622016-10-28 13:58:40 +01001539 ret = i915_gem_obj_prepare_shmem_write(obj, &needs_clflush);
1540 mutex_unlock(&i915->drm.struct_mutex);
1541 if (ret)
1542 return ret;
1543
1544 obj_do_bit17_swizzling = 0;
1545 if (i915_gem_object_needs_bit17_swizzle(obj))
1546 obj_do_bit17_swizzling = BIT(17);
1547
1548 /* If we don't overwrite a cacheline completely we need to be
1549 * careful to have up-to-date data by first clflushing. Don't
1550 * overcomplicate things and flush the entire patch.
1551 */
1552 partial_cacheline_write = 0;
1553 if (needs_clflush & CLFLUSH_BEFORE)
1554 partial_cacheline_write = boot_cpu_data.x86_clflush_size - 1;
1555
Chris Wilson43394c72016-08-18 17:16:47 +01001556 user_data = u64_to_user_ptr(args->data_ptr);
Chris Wilson43394c72016-08-18 17:16:47 +01001557 remain = args->size;
Chris Wilsonfe115622016-10-28 13:58:40 +01001558 offset = offset_in_page(args->offset);
1559 for (idx = args->offset >> PAGE_SHIFT; remain; idx++) {
1560 struct page *page = i915_gem_object_get_page(obj, idx);
1561 int length;
Eric Anholt40123c12009-03-09 13:42:30 -07001562
Chris Wilsonfe115622016-10-28 13:58:40 +01001563 length = remain;
1564 if (offset + length > PAGE_SIZE)
1565 length = PAGE_SIZE - offset;
Chris Wilsone5281cc2010-10-28 13:45:36 +01001566
Chris Wilsonfe115622016-10-28 13:58:40 +01001567 ret = shmem_pwrite(page, offset, length, user_data,
1568 page_to_phys(page) & obj_do_bit17_swizzling,
1569 (offset | length) & partial_cacheline_write,
1570 needs_clflush & CLFLUSH_AFTER);
1571 if (ret)
Chris Wilson9da3da62012-06-01 15:20:22 +01001572 break;
1573
Chris Wilsonfe115622016-10-28 13:58:40 +01001574 remain -= length;
1575 user_data += length;
1576 offset = 0;
Eric Anholt40123c12009-03-09 13:42:30 -07001577 }
1578
Chris Wilsond59b21e2017-02-22 11:40:49 +00001579 intel_fb_obj_flush(obj, ORIGIN_CPU);
Chris Wilsonfe115622016-10-28 13:58:40 +01001580 i915_gem_obj_finish_shmem_access(obj);
Eric Anholt40123c12009-03-09 13:42:30 -07001581 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07001582}
1583
1584/**
1585 * Writes data to the object referenced by handle.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01001586 * @dev: drm device
1587 * @data: ioctl data blob
1588 * @file: drm file
Eric Anholt673a3942008-07-30 12:06:12 -07001589 *
1590 * On error, the contents of the buffer that were to be modified are undefined.
1591 */
1592int
1593i915_gem_pwrite_ioctl(struct drm_device *dev, void *data,
Chris Wilsonfbd5a262010-10-14 15:03:58 +01001594 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001595{
1596 struct drm_i915_gem_pwrite *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00001597 struct drm_i915_gem_object *obj;
Chris Wilson51311d02010-11-17 09:10:42 +00001598 int ret;
1599
1600 if (args->size == 0)
1601 return 0;
1602
1603 if (!access_ok(VERIFY_READ,
Gustavo Padovan3ed605b2016-04-26 12:32:27 -03001604 u64_to_user_ptr(args->data_ptr),
Chris Wilson51311d02010-11-17 09:10:42 +00001605 args->size))
1606 return -EFAULT;
1607
Chris Wilson03ac0642016-07-20 13:31:51 +01001608 obj = i915_gem_object_lookup(file, args->handle);
Chris Wilson258a5ed2016-08-05 10:14:16 +01001609 if (!obj)
1610 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07001611
Chris Wilson7dcd2492010-09-26 20:21:44 +01001612 /* Bounds check destination. */
Matthew Auld966d5bf2016-12-13 20:32:22 +00001613 if (range_overflows_t(u64, args->offset, args->size, obj->base.size)) {
Chris Wilsonce9d4192010-09-26 20:50:05 +01001614 ret = -EINVAL;
Chris Wilson258a5ed2016-08-05 10:14:16 +01001615 goto err;
Chris Wilsonce9d4192010-09-26 20:50:05 +01001616 }
1617
Chris Wilsondb53a302011-02-03 11:57:46 +00001618 trace_i915_gem_object_pwrite(obj, args->offset, args->size);
1619
Chris Wilson7c55e2c2017-03-07 12:03:38 +00001620 ret = -ENODEV;
1621 if (obj->ops->pwrite)
1622 ret = obj->ops->pwrite(obj, args);
1623 if (ret != -ENODEV)
1624 goto err;
1625
Chris Wilsone95433c2016-10-28 13:58:27 +01001626 ret = i915_gem_object_wait(obj,
1627 I915_WAIT_INTERRUPTIBLE |
1628 I915_WAIT_ALL,
1629 MAX_SCHEDULE_TIMEOUT,
1630 to_rps_client(file));
Chris Wilson258a5ed2016-08-05 10:14:16 +01001631 if (ret)
1632 goto err;
1633
Chris Wilsonfe115622016-10-28 13:58:40 +01001634 ret = i915_gem_object_pin_pages(obj);
Chris Wilson258a5ed2016-08-05 10:14:16 +01001635 if (ret)
Chris Wilsonfe115622016-10-28 13:58:40 +01001636 goto err;
Chris Wilson258a5ed2016-08-05 10:14:16 +01001637
Daniel Vetter935aaa62012-03-25 19:47:35 +02001638 ret = -EFAULT;
Eric Anholt673a3942008-07-30 12:06:12 -07001639 /* We can only do the GTT pwrite on untiled buffers, as otherwise
1640 * it would end up going through the fenced access, and we'll get
1641 * different detiling behavior between reading and writing.
1642 * pread/pwrite currently are reading and writing from the CPU
1643 * perspective, requiring manual detiling by the client.
1644 */
Chris Wilson6eae0052016-06-20 15:05:52 +01001645 if (!i915_gem_object_has_struct_page(obj) ||
Chris Wilson9c870d02016-10-24 13:42:15 +01001646 cpu_write_needs_clflush(obj))
Daniel Vetter935aaa62012-03-25 19:47:35 +02001647 /* Note that the gtt paths might fail with non-page-backed user
1648 * pointers (e.g. gtt mappings when moving data between
Chris Wilson9c870d02016-10-24 13:42:15 +01001649 * textures). Fallback to the shmem path in that case.
1650 */
Chris Wilsonfe115622016-10-28 13:58:40 +01001651 ret = i915_gem_gtt_pwrite_fast(obj, args);
Eric Anholt673a3942008-07-30 12:06:12 -07001652
Chris Wilsond1054ee2016-07-16 18:42:36 +01001653 if (ret == -EFAULT || ret == -ENOSPC) {
Chris Wilson6a2c4232014-11-04 04:51:40 -08001654 if (obj->phys_handle)
1655 ret = i915_gem_phys_pwrite(obj, args, file);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301656 else
Chris Wilsonfe115622016-10-28 13:58:40 +01001657 ret = i915_gem_shmem_pwrite(obj, args);
Chris Wilson6a2c4232014-11-04 04:51:40 -08001658 }
Daniel Vetter5c0480f2011-12-14 13:57:30 +01001659
Chris Wilsonfe115622016-10-28 13:58:40 +01001660 i915_gem_object_unpin_pages(obj);
Chris Wilson258a5ed2016-08-05 10:14:16 +01001661err:
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001662 i915_gem_object_put(obj);
Chris Wilson258a5ed2016-08-05 10:14:16 +01001663 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07001664}
1665
Chris Wilson40e62d52016-10-28 13:58:41 +01001666static void i915_gem_object_bump_inactive_ggtt(struct drm_i915_gem_object *obj)
1667{
1668 struct drm_i915_private *i915;
1669 struct list_head *list;
1670 struct i915_vma *vma;
1671
Chris Wilsonf2123812017-10-16 12:40:37 +01001672 GEM_BUG_ON(!i915_gem_object_has_pinned_pages(obj));
1673
Chris Wilsone2189dd2017-12-07 21:14:07 +00001674 for_each_ggtt_vma(vma, obj) {
Chris Wilson40e62d52016-10-28 13:58:41 +01001675 if (i915_vma_is_active(vma))
1676 continue;
1677
1678 if (!drm_mm_node_allocated(&vma->node))
1679 continue;
1680
1681 list_move_tail(&vma->vm_link, &vma->vm->inactive_list);
1682 }
1683
1684 i915 = to_i915(obj->base.dev);
Chris Wilsonf2123812017-10-16 12:40:37 +01001685 spin_lock(&i915->mm.obj_lock);
Chris Wilson40e62d52016-10-28 13:58:41 +01001686 list = obj->bind_count ? &i915->mm.bound_list : &i915->mm.unbound_list;
Chris Wilsonf2123812017-10-16 12:40:37 +01001687 list_move_tail(&obj->mm.link, list);
1688 spin_unlock(&i915->mm.obj_lock);
Chris Wilson40e62d52016-10-28 13:58:41 +01001689}
1690
Eric Anholt673a3942008-07-30 12:06:12 -07001691/**
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001692 * Called when user space prepares to use an object with the CPU, either
1693 * through the mmap ioctl's mapping or a GTT mapping.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01001694 * @dev: drm device
1695 * @data: ioctl data blob
1696 * @file: drm file
Eric Anholt673a3942008-07-30 12:06:12 -07001697 */
1698int
1699i915_gem_set_domain_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00001700 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001701{
1702 struct drm_i915_gem_set_domain *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00001703 struct drm_i915_gem_object *obj;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001704 uint32_t read_domains = args->read_domains;
1705 uint32_t write_domain = args->write_domain;
Chris Wilson40e62d52016-10-28 13:58:41 +01001706 int err;
Eric Anholt673a3942008-07-30 12:06:12 -07001707
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001708 /* Only handle setting domains to types used by the CPU. */
Chris Wilsonb8f90962016-08-05 10:14:07 +01001709 if ((write_domain | read_domains) & I915_GEM_GPU_DOMAINS)
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001710 return -EINVAL;
1711
1712 /* Having something in the write domain implies it's in the read
1713 * domain, and only that read domain. Enforce that in the request.
1714 */
1715 if (write_domain != 0 && read_domains != write_domain)
1716 return -EINVAL;
1717
Chris Wilson03ac0642016-07-20 13:31:51 +01001718 obj = i915_gem_object_lookup(file, args->handle);
Chris Wilsonb8f90962016-08-05 10:14:07 +01001719 if (!obj)
1720 return -ENOENT;
Jesse Barnes652c3932009-08-17 13:31:43 -07001721
Chris Wilson3236f572012-08-24 09:35:09 +01001722 /* Try to flush the object off the GPU without holding the lock.
1723 * We will repeat the flush holding the lock in the normal manner
1724 * to catch cases where we are gazumped.
1725 */
Chris Wilson40e62d52016-10-28 13:58:41 +01001726 err = i915_gem_object_wait(obj,
Chris Wilsone95433c2016-10-28 13:58:27 +01001727 I915_WAIT_INTERRUPTIBLE |
1728 (write_domain ? I915_WAIT_ALL : 0),
1729 MAX_SCHEDULE_TIMEOUT,
1730 to_rps_client(file));
Chris Wilson40e62d52016-10-28 13:58:41 +01001731 if (err)
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001732 goto out;
Chris Wilsonb8f90962016-08-05 10:14:07 +01001733
Tina Zhanga03f3952017-11-14 10:25:13 +00001734 /*
1735 * Proxy objects do not control access to the backing storage, ergo
1736 * they cannot be used as a means to manipulate the cache domain
1737 * tracking for that backing storage. The proxy object is always
1738 * considered to be outside of any cache domain.
1739 */
1740 if (i915_gem_object_is_proxy(obj)) {
1741 err = -ENXIO;
1742 goto out;
1743 }
1744
1745 /*
1746 * Flush and acquire obj->pages so that we are coherent through
Chris Wilson40e62d52016-10-28 13:58:41 +01001747 * direct access in memory with previous cached writes through
1748 * shmemfs and that our cache domain tracking remains valid.
1749 * For example, if the obj->filp was moved to swap without us
1750 * being notified and releasing the pages, we would mistakenly
1751 * continue to assume that the obj remained out of the CPU cached
1752 * domain.
1753 */
1754 err = i915_gem_object_pin_pages(obj);
1755 if (err)
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001756 goto out;
Chris Wilson40e62d52016-10-28 13:58:41 +01001757
1758 err = i915_mutex_lock_interruptible(dev);
1759 if (err)
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001760 goto out_unpin;
Chris Wilson3236f572012-08-24 09:35:09 +01001761
Chris Wilsone22d8e32017-04-12 12:01:11 +01001762 if (read_domains & I915_GEM_DOMAIN_WC)
1763 err = i915_gem_object_set_to_wc_domain(obj, write_domain);
1764 else if (read_domains & I915_GEM_DOMAIN_GTT)
1765 err = i915_gem_object_set_to_gtt_domain(obj, write_domain);
Chris Wilson43566de2015-01-02 16:29:29 +05301766 else
Chris Wilsone22d8e32017-04-12 12:01:11 +01001767 err = i915_gem_object_set_to_cpu_domain(obj, write_domain);
Chris Wilson40e62d52016-10-28 13:58:41 +01001768
1769 /* And bump the LRU for this access */
1770 i915_gem_object_bump_inactive_ggtt(obj);
1771
1772 mutex_unlock(&dev->struct_mutex);
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001773
Daniel Vetter031b6982015-06-26 19:35:16 +02001774 if (write_domain != 0)
Chris Wilsonef749212017-04-12 12:01:10 +01001775 intel_fb_obj_invalidate(obj,
1776 fb_write_origin(obj, write_domain));
Daniel Vetter031b6982015-06-26 19:35:16 +02001777
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001778out_unpin:
Chris Wilson40e62d52016-10-28 13:58:41 +01001779 i915_gem_object_unpin_pages(obj);
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001780out:
1781 i915_gem_object_put(obj);
Chris Wilson40e62d52016-10-28 13:58:41 +01001782 return err;
Eric Anholt673a3942008-07-30 12:06:12 -07001783}
1784
1785/**
1786 * Called when user space has done writes to this buffer
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01001787 * @dev: drm device
1788 * @data: ioctl data blob
1789 * @file: drm file
Eric Anholt673a3942008-07-30 12:06:12 -07001790 */
1791int
1792i915_gem_sw_finish_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00001793 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001794{
1795 struct drm_i915_gem_sw_finish *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00001796 struct drm_i915_gem_object *obj;
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001797
Chris Wilson03ac0642016-07-20 13:31:51 +01001798 obj = i915_gem_object_lookup(file, args->handle);
Chris Wilsonc21724c2016-08-05 10:14:19 +01001799 if (!obj)
1800 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07001801
Tina Zhanga03f3952017-11-14 10:25:13 +00001802 /*
1803 * Proxy objects are barred from CPU access, so there is no
1804 * need to ban sw_finish as it is a nop.
1805 */
1806
Eric Anholt673a3942008-07-30 12:06:12 -07001807 /* Pinned buffers may be scanout, so flush the cache */
Chris Wilson5a97bcc2017-02-22 11:40:46 +00001808 i915_gem_object_flush_if_display(obj);
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001809 i915_gem_object_put(obj);
Chris Wilson5a97bcc2017-02-22 11:40:46 +00001810
1811 return 0;
Eric Anholt673a3942008-07-30 12:06:12 -07001812}
1813
1814/**
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01001815 * i915_gem_mmap_ioctl - Maps the contents of an object, returning the address
1816 * it is mapped to.
1817 * @dev: drm device
1818 * @data: ioctl data blob
1819 * @file: drm file
Eric Anholt673a3942008-07-30 12:06:12 -07001820 *
1821 * While the mapping holds a reference on the contents of the object, it doesn't
1822 * imply a ref on the object itself.
Daniel Vetter34367382014-10-16 12:28:18 +02001823 *
1824 * IMPORTANT:
1825 *
1826 * DRM driver writers who look a this function as an example for how to do GEM
1827 * mmap support, please don't implement mmap support like here. The modern way
1828 * to implement DRM mmap support is with an mmap offset ioctl (like
1829 * i915_gem_mmap_gtt) and then using the mmap syscall on the DRM fd directly.
1830 * That way debug tooling like valgrind will understand what's going on, hiding
1831 * the mmap call in a driver private ioctl will break that. The i915 driver only
1832 * does cpu mmaps this way because we didn't know better.
Eric Anholt673a3942008-07-30 12:06:12 -07001833 */
1834int
1835i915_gem_mmap_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00001836 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001837{
1838 struct drm_i915_gem_mmap *args = data;
Chris Wilson03ac0642016-07-20 13:31:51 +01001839 struct drm_i915_gem_object *obj;
Eric Anholt673a3942008-07-30 12:06:12 -07001840 unsigned long addr;
1841
Akash Goel1816f922015-01-02 16:29:30 +05301842 if (args->flags & ~(I915_MMAP_WC))
1843 return -EINVAL;
1844
Borislav Petkov568a58e2016-03-29 17:42:01 +02001845 if (args->flags & I915_MMAP_WC && !boot_cpu_has(X86_FEATURE_PAT))
Akash Goel1816f922015-01-02 16:29:30 +05301846 return -ENODEV;
1847
Chris Wilson03ac0642016-07-20 13:31:51 +01001848 obj = i915_gem_object_lookup(file, args->handle);
1849 if (!obj)
Chris Wilsonbf79cb92010-08-04 14:19:46 +01001850 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07001851
Daniel Vetter1286ff72012-05-10 15:25:09 +02001852 /* prime objects have no backing filp to GEM mmap
1853 * pages from.
1854 */
Chris Wilson03ac0642016-07-20 13:31:51 +01001855 if (!obj->base.filp) {
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001856 i915_gem_object_put(obj);
Tina Zhang274b2462017-11-14 10:25:12 +00001857 return -ENXIO;
Daniel Vetter1286ff72012-05-10 15:25:09 +02001858 }
1859
Chris Wilson03ac0642016-07-20 13:31:51 +01001860 addr = vm_mmap(obj->base.filp, 0, args->size,
Eric Anholt673a3942008-07-30 12:06:12 -07001861 PROT_READ | PROT_WRITE, MAP_SHARED,
1862 args->offset);
Akash Goel1816f922015-01-02 16:29:30 +05301863 if (args->flags & I915_MMAP_WC) {
1864 struct mm_struct *mm = current->mm;
1865 struct vm_area_struct *vma;
1866
Michal Hocko80a89a52016-05-23 16:26:11 -07001867 if (down_write_killable(&mm->mmap_sem)) {
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001868 i915_gem_object_put(obj);
Michal Hocko80a89a52016-05-23 16:26:11 -07001869 return -EINTR;
1870 }
Akash Goel1816f922015-01-02 16:29:30 +05301871 vma = find_vma(mm, addr);
1872 if (vma)
1873 vma->vm_page_prot =
1874 pgprot_writecombine(vm_get_page_prot(vma->vm_flags));
1875 else
1876 addr = -ENOMEM;
1877 up_write(&mm->mmap_sem);
Chris Wilsonaeecc962016-06-17 14:46:39 -03001878
1879 /* This may race, but that's ok, it only gets set */
Chris Wilson50349242016-08-18 17:17:04 +01001880 WRITE_ONCE(obj->frontbuffer_ggtt_origin, ORIGIN_CPU);
Akash Goel1816f922015-01-02 16:29:30 +05301881 }
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001882 i915_gem_object_put(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001883 if (IS_ERR((void *)addr))
1884 return addr;
1885
1886 args->addr_ptr = (uint64_t) addr;
1887
1888 return 0;
1889}
1890
Chris Wilson03af84f2016-08-18 17:17:01 +01001891static unsigned int tile_row_pages(struct drm_i915_gem_object *obj)
1892{
Chris Wilson6649a0b2017-01-09 16:16:08 +00001893 return i915_gem_object_get_tile_row_size(obj) >> PAGE_SHIFT;
Chris Wilson03af84f2016-08-18 17:17:01 +01001894}
1895
Jesse Barnesde151cf2008-11-12 10:03:55 -08001896/**
Chris Wilson4cc69072016-08-25 19:05:19 +01001897 * i915_gem_mmap_gtt_version - report the current feature set for GTT mmaps
1898 *
1899 * A history of the GTT mmap interface:
1900 *
1901 * 0 - Everything had to fit into the GTT. Both parties of a memcpy had to
1902 * aligned and suitable for fencing, and still fit into the available
1903 * mappable space left by the pinned display objects. A classic problem
1904 * we called the page-fault-of-doom where we would ping-pong between
1905 * two objects that could not fit inside the GTT and so the memcpy
1906 * would page one object in at the expense of the other between every
1907 * single byte.
1908 *
1909 * 1 - Objects can be any size, and have any compatible fencing (X Y, or none
1910 * as set via i915_gem_set_tiling() [DRM_I915_GEM_SET_TILING]). If the
1911 * object is too large for the available space (or simply too large
1912 * for the mappable aperture!), a view is created instead and faulted
1913 * into userspace. (This view is aligned and sized appropriately for
1914 * fenced access.)
1915 *
Chris Wilsone22d8e32017-04-12 12:01:11 +01001916 * 2 - Recognise WC as a separate cache domain so that we can flush the
1917 * delayed writes via GTT before performing direct access via WC.
1918 *
Chris Wilson4cc69072016-08-25 19:05:19 +01001919 * Restrictions:
1920 *
1921 * * snoopable objects cannot be accessed via the GTT. It can cause machine
1922 * hangs on some architectures, corruption on others. An attempt to service
1923 * a GTT page fault from a snoopable object will generate a SIGBUS.
1924 *
1925 * * the object must be able to fit into RAM (physical memory, though no
1926 * limited to the mappable aperture).
1927 *
1928 *
1929 * Caveats:
1930 *
1931 * * a new GTT page fault will synchronize rendering from the GPU and flush
1932 * all data to system memory. Subsequent access will not be synchronized.
1933 *
1934 * * all mappings are revoked on runtime device suspend.
1935 *
1936 * * there are only 8, 16 or 32 fence registers to share between all users
1937 * (older machines require fence register for display and blitter access
1938 * as well). Contention of the fence registers will cause the previous users
1939 * to be unmapped and any new access will generate new page faults.
1940 *
1941 * * running out of memory while servicing a fault may generate a SIGBUS,
1942 * rather than the expected SIGSEGV.
1943 */
1944int i915_gem_mmap_gtt_version(void)
1945{
Chris Wilsone22d8e32017-04-12 12:01:11 +01001946 return 2;
Chris Wilson4cc69072016-08-25 19:05:19 +01001947}
1948
Chris Wilson2d4281b2017-01-10 09:56:32 +00001949static inline struct i915_ggtt_view
1950compute_partial_view(struct drm_i915_gem_object *obj,
Chris Wilson2d4281b2017-01-10 09:56:32 +00001951 pgoff_t page_offset,
1952 unsigned int chunk)
1953{
1954 struct i915_ggtt_view view;
1955
1956 if (i915_gem_object_is_tiled(obj))
1957 chunk = roundup(chunk, tile_row_pages(obj));
1958
Chris Wilson2d4281b2017-01-10 09:56:32 +00001959 view.type = I915_GGTT_VIEW_PARTIAL;
Chris Wilson8bab11932017-01-14 00:28:25 +00001960 view.partial.offset = rounddown(page_offset, chunk);
1961 view.partial.size =
Chris Wilson2d4281b2017-01-10 09:56:32 +00001962 min_t(unsigned int, chunk,
Chris Wilson8bab11932017-01-14 00:28:25 +00001963 (obj->base.size >> PAGE_SHIFT) - view.partial.offset);
Chris Wilson2d4281b2017-01-10 09:56:32 +00001964
1965 /* If the partial covers the entire object, just create a normal VMA. */
1966 if (chunk >= obj->base.size >> PAGE_SHIFT)
1967 view.type = I915_GGTT_VIEW_NORMAL;
1968
1969 return view;
1970}
1971
Chris Wilson4cc69072016-08-25 19:05:19 +01001972/**
Jesse Barnesde151cf2008-11-12 10:03:55 -08001973 * i915_gem_fault - fault a page into the GTT
Geliang Tangd9072a32015-09-15 05:58:44 -07001974 * @vmf: fault info
Jesse Barnesde151cf2008-11-12 10:03:55 -08001975 *
1976 * The fault handler is set up by drm_gem_mmap() when a object is GTT mapped
1977 * from userspace. The fault handler takes care of binding the object to
1978 * the GTT (if needed), allocating and programming a fence register (again,
1979 * only if needed based on whether the old reg is still valid or the object
1980 * is tiled) and inserting a new PTE into the faulting process.
1981 *
1982 * Note that the faulting process may involve evicting existing objects
1983 * from the GTT and/or fence registers to make room. So performance may
1984 * suffer if the GTT working set is large or there are few fence registers
1985 * left.
Chris Wilson4cc69072016-08-25 19:05:19 +01001986 *
1987 * The current feature set supported by i915_gem_fault() and thus GTT mmaps
1988 * is exposed via I915_PARAM_MMAP_GTT_VERSION (see i915_gem_mmap_gtt_version).
Jesse Barnesde151cf2008-11-12 10:03:55 -08001989 */
Dave Jiang11bac802017-02-24 14:56:41 -08001990int i915_gem_fault(struct vm_fault *vmf)
Jesse Barnesde151cf2008-11-12 10:03:55 -08001991{
Chris Wilson03af84f2016-08-18 17:17:01 +01001992#define MIN_CHUNK_PAGES ((1 << 20) >> PAGE_SHIFT) /* 1 MiB */
Dave Jiang11bac802017-02-24 14:56:41 -08001993 struct vm_area_struct *area = vmf->vma;
Chris Wilson058d88c2016-08-15 10:49:06 +01001994 struct drm_i915_gem_object *obj = to_intel_bo(area->vm_private_data);
Chris Wilson05394f32010-11-08 19:18:58 +00001995 struct drm_device *dev = obj->base.dev;
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03001996 struct drm_i915_private *dev_priv = to_i915(dev);
1997 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Chris Wilsonb8f90962016-08-05 10:14:07 +01001998 bool write = !!(vmf->flags & FAULT_FLAG_WRITE);
Chris Wilson058d88c2016-08-15 10:49:06 +01001999 struct i915_vma *vma;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002000 pgoff_t page_offset;
Chris Wilson82118872016-08-18 17:17:05 +01002001 unsigned int flags;
Chris Wilsonb8f90962016-08-05 10:14:07 +01002002 int ret;
Paulo Zanonif65c9162013-11-27 18:20:34 -02002003
Jesse Barnesde151cf2008-11-12 10:03:55 -08002004 /* We don't use vmf->pgoff since that has the fake offset */
Jan Kara1a29d852016-12-14 15:07:01 -08002005 page_offset = (vmf->address - area->vm_start) >> PAGE_SHIFT;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002006
Chris Wilsondb53a302011-02-03 11:57:46 +00002007 trace_i915_gem_object_fault(obj, page_offset, true, write);
2008
Chris Wilson6e4930f2014-02-07 18:37:06 -02002009 /* Try to flush the object off the GPU first without holding the lock.
Chris Wilsonb8f90962016-08-05 10:14:07 +01002010 * Upon acquiring the lock, we will perform our sanity checks and then
Chris Wilson6e4930f2014-02-07 18:37:06 -02002011 * repeat the flush holding the lock in the normal manner to catch cases
2012 * where we are gazumped.
2013 */
Chris Wilsone95433c2016-10-28 13:58:27 +01002014 ret = i915_gem_object_wait(obj,
2015 I915_WAIT_INTERRUPTIBLE,
2016 MAX_SCHEDULE_TIMEOUT,
2017 NULL);
Chris Wilson6e4930f2014-02-07 18:37:06 -02002018 if (ret)
Chris Wilsonb8f90962016-08-05 10:14:07 +01002019 goto err;
2020
Chris Wilson40e62d52016-10-28 13:58:41 +01002021 ret = i915_gem_object_pin_pages(obj);
2022 if (ret)
2023 goto err;
2024
Chris Wilsonb8f90962016-08-05 10:14:07 +01002025 intel_runtime_pm_get(dev_priv);
2026
2027 ret = i915_mutex_lock_interruptible(dev);
2028 if (ret)
2029 goto err_rpm;
Chris Wilson6e4930f2014-02-07 18:37:06 -02002030
Chris Wilsoneb119bd2012-12-16 12:43:36 +00002031 /* Access to snoopable pages through the GTT is incoherent. */
Tvrtko Ursulin0031fb92016-11-04 14:42:44 +00002032 if (obj->cache_level != I915_CACHE_NONE && !HAS_LLC(dev_priv)) {
Chris Wilsonddeff6e2014-05-28 16:16:41 +01002033 ret = -EFAULT;
Chris Wilsonb8f90962016-08-05 10:14:07 +01002034 goto err_unlock;
Chris Wilsoneb119bd2012-12-16 12:43:36 +00002035 }
2036
Chris Wilson82118872016-08-18 17:17:05 +01002037 /* If the object is smaller than a couple of partial vma, it is
2038 * not worth only creating a single partial vma - we may as well
2039 * clear enough space for the full object.
2040 */
2041 flags = PIN_MAPPABLE;
2042 if (obj->base.size > 2 * MIN_CHUNK_PAGES << PAGE_SHIFT)
2043 flags |= PIN_NONBLOCK | PIN_NONFAULT;
2044
Chris Wilsona61007a2016-08-18 17:17:02 +01002045 /* Now pin it into the GTT as needed */
Chris Wilson82118872016-08-18 17:17:05 +01002046 vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0, flags);
Chris Wilsona61007a2016-08-18 17:17:02 +01002047 if (IS_ERR(vma)) {
Chris Wilsona61007a2016-08-18 17:17:02 +01002048 /* Use a partial view if it is bigger than available space */
Chris Wilson2d4281b2017-01-10 09:56:32 +00002049 struct i915_ggtt_view view =
Chris Wilson8201c1f2017-01-10 09:56:33 +00002050 compute_partial_view(obj, page_offset, MIN_CHUNK_PAGES);
Chris Wilsonaa136d92016-08-18 17:17:03 +01002051
Chris Wilson50349242016-08-18 17:17:04 +01002052 /* Userspace is now writing through an untracked VMA, abandon
2053 * all hope that the hardware is able to track future writes.
2054 */
2055 obj->frontbuffer_ggtt_origin = ORIGIN_CPU;
2056
Chris Wilsona61007a2016-08-18 17:17:02 +01002057 vma = i915_gem_object_ggtt_pin(obj, &view, 0, 0, PIN_MAPPABLE);
2058 }
Chris Wilson058d88c2016-08-15 10:49:06 +01002059 if (IS_ERR(vma)) {
2060 ret = PTR_ERR(vma);
Chris Wilsonb8f90962016-08-05 10:14:07 +01002061 goto err_unlock;
Chris Wilson058d88c2016-08-15 10:49:06 +01002062 }
Jesse Barnesde151cf2008-11-12 10:03:55 -08002063
Chris Wilsonc9839302012-11-20 10:45:17 +00002064 ret = i915_gem_object_set_to_gtt_domain(obj, write);
2065 if (ret)
Chris Wilsonb8f90962016-08-05 10:14:07 +01002066 goto err_unpin;
Chris Wilsonc9839302012-11-20 10:45:17 +00002067
Chris Wilson3bd40732017-10-09 09:43:56 +01002068 ret = i915_vma_pin_fence(vma);
Chris Wilsonc9839302012-11-20 10:45:17 +00002069 if (ret)
Chris Wilsonb8f90962016-08-05 10:14:07 +01002070 goto err_unpin;
Chris Wilson7d1c4802010-08-07 21:45:03 +01002071
Chris Wilsonb90b91d2014-06-10 12:14:40 +01002072 /* Finally, remap it using the new GTT offset */
Chris Wilsonc58305a2016-08-19 16:54:28 +01002073 ret = remap_io_mapping(area,
Chris Wilson8bab11932017-01-14 00:28:25 +00002074 area->vm_start + (vma->ggtt_view.partial.offset << PAGE_SHIFT),
Matthew Auld73ebd502017-12-11 15:18:20 +00002075 (ggtt->gmadr.start + vma->node.start) >> PAGE_SHIFT,
Chris Wilsonc58305a2016-08-19 16:54:28 +01002076 min_t(u64, vma->size, area->vm_end - area->vm_start),
Matthew Auld73ebd502017-12-11 15:18:20 +00002077 &ggtt->iomap);
Chris Wilsona65adaf2017-10-09 09:43:57 +01002078 if (ret)
2079 goto err_fence;
Chris Wilsona61007a2016-08-18 17:17:02 +01002080
Chris Wilsona65adaf2017-10-09 09:43:57 +01002081 /* Mark as being mmapped into userspace for later revocation */
2082 assert_rpm_wakelock_held(dev_priv);
2083 if (!i915_vma_set_userfault(vma) && !obj->userfault_count++)
2084 list_add(&obj->userfault_link, &dev_priv->mm.userfault_list);
2085 GEM_BUG_ON(!obj->userfault_count);
2086
Chris Wilson7125397b2017-12-06 12:49:14 +00002087 i915_vma_set_ggtt_write(vma);
2088
Chris Wilsona65adaf2017-10-09 09:43:57 +01002089err_fence:
Chris Wilson3bd40732017-10-09 09:43:56 +01002090 i915_vma_unpin_fence(vma);
Chris Wilsonb8f90962016-08-05 10:14:07 +01002091err_unpin:
Chris Wilson058d88c2016-08-15 10:49:06 +01002092 __i915_vma_unpin(vma);
Chris Wilsonb8f90962016-08-05 10:14:07 +01002093err_unlock:
Jesse Barnesde151cf2008-11-12 10:03:55 -08002094 mutex_unlock(&dev->struct_mutex);
Chris Wilsonb8f90962016-08-05 10:14:07 +01002095err_rpm:
2096 intel_runtime_pm_put(dev_priv);
Chris Wilson40e62d52016-10-28 13:58:41 +01002097 i915_gem_object_unpin_pages(obj);
Chris Wilsonb8f90962016-08-05 10:14:07 +01002098err:
Jesse Barnesde151cf2008-11-12 10:03:55 -08002099 switch (ret) {
Chris Wilsond9bc7e92011-02-07 13:09:31 +00002100 case -EIO:
Daniel Vetter2232f032014-09-04 09:36:18 +02002101 /*
2102 * We eat errors when the gpu is terminally wedged to avoid
2103 * userspace unduly crashing (gl has no provisions for mmaps to
2104 * fail). But any other -EIO isn't ours (e.g. swap in failure)
2105 * and so needs to be reported.
2106 */
2107 if (!i915_terminally_wedged(&dev_priv->gpu_error)) {
Paulo Zanonif65c9162013-11-27 18:20:34 -02002108 ret = VM_FAULT_SIGBUS;
2109 break;
2110 }
Chris Wilson045e7692010-11-07 09:18:22 +00002111 case -EAGAIN:
Daniel Vetter571c6082013-09-12 17:57:28 +02002112 /*
2113 * EAGAIN means the gpu is hung and we'll wait for the error
2114 * handler to reset everything when re-faulting in
2115 * i915_mutex_lock_interruptible.
Chris Wilsond9bc7e92011-02-07 13:09:31 +00002116 */
Chris Wilsonc7150892009-09-23 00:43:56 +01002117 case 0:
2118 case -ERESTARTSYS:
Chris Wilsonbed636a2011-02-11 20:31:19 +00002119 case -EINTR:
Dmitry Rogozhkine79e0fe2012-10-03 17:15:26 +03002120 case -EBUSY:
2121 /*
2122 * EBUSY is ok: this just means that another thread
2123 * already did the job.
2124 */
Paulo Zanonif65c9162013-11-27 18:20:34 -02002125 ret = VM_FAULT_NOPAGE;
2126 break;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002127 case -ENOMEM:
Paulo Zanonif65c9162013-11-27 18:20:34 -02002128 ret = VM_FAULT_OOM;
2129 break;
Daniel Vettera7c2e1a2012-10-17 11:17:16 +02002130 case -ENOSPC:
Chris Wilson45d67812014-01-31 11:34:57 +00002131 case -EFAULT:
Paulo Zanonif65c9162013-11-27 18:20:34 -02002132 ret = VM_FAULT_SIGBUS;
2133 break;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002134 default:
Daniel Vettera7c2e1a2012-10-17 11:17:16 +02002135 WARN_ONCE(ret, "unhandled error in i915_gem_fault: %i\n", ret);
Paulo Zanonif65c9162013-11-27 18:20:34 -02002136 ret = VM_FAULT_SIGBUS;
2137 break;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002138 }
Paulo Zanonif65c9162013-11-27 18:20:34 -02002139 return ret;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002140}
2141
Chris Wilsona65adaf2017-10-09 09:43:57 +01002142static void __i915_gem_object_release_mmap(struct drm_i915_gem_object *obj)
2143{
2144 struct i915_vma *vma;
2145
2146 GEM_BUG_ON(!obj->userfault_count);
2147
2148 obj->userfault_count = 0;
2149 list_del(&obj->userfault_link);
2150 drm_vma_node_unmap(&obj->base.vma_node,
2151 obj->base.dev->anon_inode->i_mapping);
2152
Chris Wilsone2189dd2017-12-07 21:14:07 +00002153 for_each_ggtt_vma(vma, obj)
Chris Wilsona65adaf2017-10-09 09:43:57 +01002154 i915_vma_unset_userfault(vma);
Chris Wilsona65adaf2017-10-09 09:43:57 +01002155}
2156
Jesse Barnesde151cf2008-11-12 10:03:55 -08002157/**
Chris Wilson901782b2009-07-10 08:18:50 +01002158 * i915_gem_release_mmap - remove physical page mappings
2159 * @obj: obj in question
2160 *
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02002161 * Preserve the reservation of the mmapping with the DRM core code, but
Chris Wilson901782b2009-07-10 08:18:50 +01002162 * relinquish ownership of the pages back to the system.
2163 *
2164 * It is vital that we remove the page mapping if we have mapped a tiled
2165 * object through the GTT and then lose the fence register due to
2166 * resource pressure. Similarly if the object has been moved out of the
2167 * aperture, than pages mapped into userspace must be revoked. Removing the
2168 * mapping will then trigger a page fault on the next user access, allowing
2169 * fixup by i915_gem_fault().
2170 */
Eric Anholtd05ca302009-07-10 13:02:26 -07002171void
Chris Wilson05394f32010-11-08 19:18:58 +00002172i915_gem_release_mmap(struct drm_i915_gem_object *obj)
Chris Wilson901782b2009-07-10 08:18:50 +01002173{
Chris Wilson275f0392016-10-24 13:42:14 +01002174 struct drm_i915_private *i915 = to_i915(obj->base.dev);
Chris Wilson275f0392016-10-24 13:42:14 +01002175
Chris Wilson349f2cc2016-04-13 17:35:12 +01002176 /* Serialisation between user GTT access and our code depends upon
2177 * revoking the CPU's PTE whilst the mutex is held. The next user
2178 * pagefault then has to wait until we release the mutex.
Chris Wilson9c870d02016-10-24 13:42:15 +01002179 *
2180 * Note that RPM complicates somewhat by adding an additional
2181 * requirement that operations to the GGTT be made holding the RPM
2182 * wakeref.
Chris Wilson349f2cc2016-04-13 17:35:12 +01002183 */
Chris Wilson275f0392016-10-24 13:42:14 +01002184 lockdep_assert_held(&i915->drm.struct_mutex);
Chris Wilson9c870d02016-10-24 13:42:15 +01002185 intel_runtime_pm_get(i915);
Chris Wilson349f2cc2016-04-13 17:35:12 +01002186
Chris Wilsona65adaf2017-10-09 09:43:57 +01002187 if (!obj->userfault_count)
Chris Wilson9c870d02016-10-24 13:42:15 +01002188 goto out;
Chris Wilson901782b2009-07-10 08:18:50 +01002189
Chris Wilsona65adaf2017-10-09 09:43:57 +01002190 __i915_gem_object_release_mmap(obj);
Chris Wilson349f2cc2016-04-13 17:35:12 +01002191
2192 /* Ensure that the CPU's PTE are revoked and there are not outstanding
2193 * memory transactions from userspace before we return. The TLB
2194 * flushing implied above by changing the PTE above *should* be
2195 * sufficient, an extra barrier here just provides us with a bit
2196 * of paranoid documentation about our requirement to serialise
2197 * memory writes before touching registers / GSM.
2198 */
2199 wmb();
Chris Wilson9c870d02016-10-24 13:42:15 +01002200
2201out:
2202 intel_runtime_pm_put(i915);
Chris Wilson901782b2009-07-10 08:18:50 +01002203}
2204
Chris Wilson7c108fd2016-10-24 13:42:18 +01002205void i915_gem_runtime_suspend(struct drm_i915_private *dev_priv)
Chris Wilsoneedd10f2014-06-16 08:57:44 +01002206{
Chris Wilson3594a3e2016-10-24 13:42:16 +01002207 struct drm_i915_gem_object *obj, *on;
Chris Wilson7c108fd2016-10-24 13:42:18 +01002208 int i;
Chris Wilsoneedd10f2014-06-16 08:57:44 +01002209
Chris Wilson3594a3e2016-10-24 13:42:16 +01002210 /*
2211 * Only called during RPM suspend. All users of the userfault_list
2212 * must be holding an RPM wakeref to ensure that this can not
2213 * run concurrently with themselves (and use the struct_mutex for
2214 * protection between themselves).
2215 */
2216
2217 list_for_each_entry_safe(obj, on,
Chris Wilsona65adaf2017-10-09 09:43:57 +01002218 &dev_priv->mm.userfault_list, userfault_link)
2219 __i915_gem_object_release_mmap(obj);
Chris Wilson7c108fd2016-10-24 13:42:18 +01002220
2221 /* The fence will be lost when the device powers down. If any were
2222 * in use by hardware (i.e. they are pinned), we should not be powering
2223 * down! All other fences will be reacquired by the user upon waking.
2224 */
2225 for (i = 0; i < dev_priv->num_fence_regs; i++) {
2226 struct drm_i915_fence_reg *reg = &dev_priv->fence_regs[i];
2227
Chris Wilsone0ec3ec2017-02-03 12:57:17 +00002228 /* Ideally we want to assert that the fence register is not
2229 * live at this point (i.e. that no piece of code will be
2230 * trying to write through fence + GTT, as that both violates
2231 * our tracking of activity and associated locking/barriers,
2232 * but also is illegal given that the hw is powered down).
2233 *
2234 * Previously we used reg->pin_count as a "liveness" indicator.
2235 * That is not sufficient, and we need a more fine-grained
2236 * tool if we want to have a sanity check here.
2237 */
Chris Wilson7c108fd2016-10-24 13:42:18 +01002238
2239 if (!reg->vma)
2240 continue;
2241
Chris Wilsona65adaf2017-10-09 09:43:57 +01002242 GEM_BUG_ON(i915_vma_has_userfault(reg->vma));
Chris Wilson7c108fd2016-10-24 13:42:18 +01002243 reg->dirty = true;
2244 }
Chris Wilsoneedd10f2014-06-16 08:57:44 +01002245}
2246
Chris Wilsond8cb5082012-08-11 15:41:03 +01002247static int i915_gem_object_create_mmap_offset(struct drm_i915_gem_object *obj)
2248{
Chris Wilsonfac5e232016-07-04 11:34:36 +01002249 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
Chris Wilsonf3f61842016-08-05 10:14:14 +01002250 int err;
Chris Wilsond8cb5082012-08-11 15:41:03 +01002251
Chris Wilsonf3f61842016-08-05 10:14:14 +01002252 err = drm_gem_create_mmap_offset(&obj->base);
Chris Wilsonb42a13d2017-01-06 15:22:40 +00002253 if (likely(!err))
Chris Wilsonf3f61842016-08-05 10:14:14 +01002254 return 0;
Daniel Vetterda494d72012-12-20 15:11:16 +01002255
Chris Wilsonb42a13d2017-01-06 15:22:40 +00002256 /* Attempt to reap some mmap space from dead objects */
2257 do {
2258 err = i915_gem_wait_for_idle(dev_priv, I915_WAIT_INTERRUPTIBLE);
2259 if (err)
2260 break;
Chris Wilsond8cb5082012-08-11 15:41:03 +01002261
Chris Wilsonb42a13d2017-01-06 15:22:40 +00002262 i915_gem_drain_freed_objects(dev_priv);
Chris Wilsonf3f61842016-08-05 10:14:14 +01002263 err = drm_gem_create_mmap_offset(&obj->base);
Chris Wilsonb42a13d2017-01-06 15:22:40 +00002264 if (!err)
2265 break;
2266
2267 } while (flush_delayed_work(&dev_priv->gt.retire_work));
Daniel Vetterda494d72012-12-20 15:11:16 +01002268
Chris Wilsonf3f61842016-08-05 10:14:14 +01002269 return err;
Chris Wilsond8cb5082012-08-11 15:41:03 +01002270}
2271
2272static void i915_gem_object_free_mmap_offset(struct drm_i915_gem_object *obj)
2273{
Chris Wilsond8cb5082012-08-11 15:41:03 +01002274 drm_gem_free_mmap_offset(&obj->base);
2275}
2276
Dave Airlieda6b51d2014-12-24 13:11:17 +10002277int
Dave Airlieff72145b2011-02-07 12:16:14 +10002278i915_gem_mmap_gtt(struct drm_file *file,
2279 struct drm_device *dev,
Dave Airlieda6b51d2014-12-24 13:11:17 +10002280 uint32_t handle,
Dave Airlieff72145b2011-02-07 12:16:14 +10002281 uint64_t *offset)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002282{
Chris Wilson05394f32010-11-08 19:18:58 +00002283 struct drm_i915_gem_object *obj;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002284 int ret;
2285
Chris Wilson03ac0642016-07-20 13:31:51 +01002286 obj = i915_gem_object_lookup(file, handle);
Chris Wilsonf3f61842016-08-05 10:14:14 +01002287 if (!obj)
2288 return -ENOENT;
Chris Wilsonab182822009-09-22 18:46:17 +01002289
Chris Wilsond8cb5082012-08-11 15:41:03 +01002290 ret = i915_gem_object_create_mmap_offset(obj);
Chris Wilsonf3f61842016-08-05 10:14:14 +01002291 if (ret == 0)
2292 *offset = drm_vma_node_offset_addr(&obj->base.vma_node);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002293
Chris Wilsonf0cd5182016-10-28 13:58:43 +01002294 i915_gem_object_put(obj);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01002295 return ret;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002296}
2297
Dave Airlieff72145b2011-02-07 12:16:14 +10002298/**
2299 * i915_gem_mmap_gtt_ioctl - prepare an object for GTT mmap'ing
2300 * @dev: DRM device
2301 * @data: GTT mapping ioctl data
2302 * @file: GEM object info
2303 *
2304 * Simply returns the fake offset to userspace so it can mmap it.
2305 * The mmap call will end up in drm_gem_mmap(), which will set things
2306 * up so we can get faults in the handler above.
2307 *
2308 * The fault handler will take care of binding the object into the GTT
2309 * (since it may have been evicted to make room for something), allocating
2310 * a fence register, and mapping the appropriate aperture address into
2311 * userspace.
2312 */
2313int
2314i915_gem_mmap_gtt_ioctl(struct drm_device *dev, void *data,
2315 struct drm_file *file)
2316{
2317 struct drm_i915_gem_mmap_gtt *args = data;
2318
Dave Airlieda6b51d2014-12-24 13:11:17 +10002319 return i915_gem_mmap_gtt(file, dev, args->handle, &args->offset);
Dave Airlieff72145b2011-02-07 12:16:14 +10002320}
2321
Daniel Vetter225067e2012-08-20 10:23:20 +02002322/* Immediately discard the backing storage */
2323static void
2324i915_gem_object_truncate(struct drm_i915_gem_object *obj)
Chris Wilsone5281cc2010-10-28 13:45:36 +01002325{
Chris Wilson4d6294bf2012-08-11 15:41:05 +01002326 i915_gem_object_free_mmap_offset(obj);
Daniel Vetter1286ff72012-05-10 15:25:09 +02002327
Chris Wilson4d6294bf2012-08-11 15:41:05 +01002328 if (obj->base.filp == NULL)
2329 return;
2330
Daniel Vetter225067e2012-08-20 10:23:20 +02002331 /* Our goal here is to return as much of the memory as
2332 * is possible back to the system as we are called from OOM.
2333 * To do this we must instruct the shmfs to drop all of its
2334 * backing pages, *now*.
Chris Wilsone5281cc2010-10-28 13:45:36 +01002335 */
Chris Wilson55372522014-03-25 13:23:06 +00002336 shmem_truncate_range(file_inode(obj->base.filp), 0, (loff_t)-1);
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002337 obj->mm.madv = __I915_MADV_PURGED;
Chris Wilson4e5462e2017-03-07 13:20:31 +00002338 obj->mm.pages = ERR_PTR(-EFAULT);
Chris Wilsone5281cc2010-10-28 13:45:36 +01002339}
Chris Wilsone5281cc2010-10-28 13:45:36 +01002340
Chris Wilson55372522014-03-25 13:23:06 +00002341/* Try to discard unwanted pages */
Chris Wilson03ac84f2016-10-28 13:58:36 +01002342void __i915_gem_object_invalidate(struct drm_i915_gem_object *obj)
Daniel Vetter225067e2012-08-20 10:23:20 +02002343{
Chris Wilson55372522014-03-25 13:23:06 +00002344 struct address_space *mapping;
2345
Chris Wilson1233e2d2016-10-28 13:58:37 +01002346 lockdep_assert_held(&obj->mm.lock);
Chris Wilsonf1fa4f42017-10-13 21:26:13 +01002347 GEM_BUG_ON(i915_gem_object_has_pages(obj));
Chris Wilson1233e2d2016-10-28 13:58:37 +01002348
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002349 switch (obj->mm.madv) {
Chris Wilson55372522014-03-25 13:23:06 +00002350 case I915_MADV_DONTNEED:
2351 i915_gem_object_truncate(obj);
2352 case __I915_MADV_PURGED:
2353 return;
2354 }
2355
2356 if (obj->base.filp == NULL)
2357 return;
2358
Al Viro93c76a32015-12-04 23:45:44 -05002359 mapping = obj->base.filp->f_mapping,
Chris Wilson55372522014-03-25 13:23:06 +00002360 invalidate_mapping_pages(mapping, 0, (loff_t)-1);
Chris Wilsone5281cc2010-10-28 13:45:36 +01002361}
2362
Chris Wilson5cdf5882010-09-27 15:51:07 +01002363static void
Chris Wilson03ac84f2016-10-28 13:58:36 +01002364i915_gem_object_put_pages_gtt(struct drm_i915_gem_object *obj,
2365 struct sg_table *pages)
Eric Anholt673a3942008-07-30 12:06:12 -07002366{
Dave Gordon85d12252016-05-20 11:54:06 +01002367 struct sgt_iter sgt_iter;
2368 struct page *page;
Daniel Vetter1286ff72012-05-10 15:25:09 +02002369
Chris Wilsone5facdf2016-12-23 14:57:57 +00002370 __i915_gem_object_release_shmem(obj, pages, true);
Eric Anholt856fa192009-03-19 14:10:50 -07002371
Chris Wilson03ac84f2016-10-28 13:58:36 +01002372 i915_gem_gtt_finish_pages(obj, pages);
Imre Deake2273302015-07-09 12:59:05 +03002373
Daniel Vetter6dacfd22011-09-12 21:30:02 +02002374 if (i915_gem_object_needs_bit17_swizzle(obj))
Chris Wilson03ac84f2016-10-28 13:58:36 +01002375 i915_gem_object_save_bit_17_swizzle(obj, pages);
Eric Anholt280b7132009-03-12 16:56:27 -07002376
Chris Wilson03ac84f2016-10-28 13:58:36 +01002377 for_each_sgt_page(page, sgt_iter, pages) {
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002378 if (obj->mm.dirty)
Chris Wilson9da3da62012-06-01 15:20:22 +01002379 set_page_dirty(page);
Chris Wilson3ef94da2009-09-14 16:50:29 +01002380
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002381 if (obj->mm.madv == I915_MADV_WILLNEED)
Chris Wilson9da3da62012-06-01 15:20:22 +01002382 mark_page_accessed(page);
Chris Wilson3ef94da2009-09-14 16:50:29 +01002383
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002384 put_page(page);
Chris Wilson3ef94da2009-09-14 16:50:29 +01002385 }
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002386 obj->mm.dirty = false;
Eric Anholt673a3942008-07-30 12:06:12 -07002387
Chris Wilson03ac84f2016-10-28 13:58:36 +01002388 sg_free_table(pages);
2389 kfree(pages);
Chris Wilson37e680a2012-06-07 15:38:42 +01002390}
2391
Chris Wilson96d77632016-10-28 13:58:33 +01002392static void __i915_gem_object_reset_page_iter(struct drm_i915_gem_object *obj)
2393{
2394 struct radix_tree_iter iter;
Ville Syrjäläc23aa712017-09-01 20:12:51 +03002395 void __rcu **slot;
Chris Wilson96d77632016-10-28 13:58:33 +01002396
Chris Wilsonbea6e982017-10-26 14:00:31 +01002397 rcu_read_lock();
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002398 radix_tree_for_each_slot(slot, &obj->mm.get_page.radix, &iter, 0)
2399 radix_tree_delete(&obj->mm.get_page.radix, iter.index);
Chris Wilsonbea6e982017-10-26 14:00:31 +01002400 rcu_read_unlock();
Chris Wilson96d77632016-10-28 13:58:33 +01002401}
2402
Chris Wilson548625e2016-11-01 12:11:34 +00002403void __i915_gem_object_put_pages(struct drm_i915_gem_object *obj,
2404 enum i915_mm_subclass subclass)
Chris Wilson37e680a2012-06-07 15:38:42 +01002405{
Chris Wilsonf2123812017-10-16 12:40:37 +01002406 struct drm_i915_private *i915 = to_i915(obj->base.dev);
Chris Wilson03ac84f2016-10-28 13:58:36 +01002407 struct sg_table *pages;
Chris Wilson37e680a2012-06-07 15:38:42 +01002408
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002409 if (i915_gem_object_has_pinned_pages(obj))
Chris Wilson03ac84f2016-10-28 13:58:36 +01002410 return;
Chris Wilsona5570172012-09-04 21:02:54 +01002411
Chris Wilson15717de2016-08-04 07:52:26 +01002412 GEM_BUG_ON(obj->bind_count);
Chris Wilsonf1fa4f42017-10-13 21:26:13 +01002413 if (!i915_gem_object_has_pages(obj))
Chris Wilson1233e2d2016-10-28 13:58:37 +01002414 return;
2415
2416 /* May be called by shrinker from within get_pages() (on another bo) */
Chris Wilson548625e2016-11-01 12:11:34 +00002417 mutex_lock_nested(&obj->mm.lock, subclass);
Chris Wilson1233e2d2016-10-28 13:58:37 +01002418 if (unlikely(atomic_read(&obj->mm.pages_pin_count)))
2419 goto unlock;
Ben Widawsky3e123022013-07-31 17:00:04 -07002420
Chris Wilsona2165e32012-12-03 11:49:00 +00002421 /* ->put_pages might need to allocate memory for the bit17 swizzle
2422 * array, hence protect them from being reaped by removing them from gtt
2423 * lists early. */
Chris Wilson03ac84f2016-10-28 13:58:36 +01002424 pages = fetch_and_zero(&obj->mm.pages);
2425 GEM_BUG_ON(!pages);
Chris Wilsona2165e32012-12-03 11:49:00 +00002426
Chris Wilsonf2123812017-10-16 12:40:37 +01002427 spin_lock(&i915->mm.obj_lock);
2428 list_del(&obj->mm.link);
2429 spin_unlock(&i915->mm.obj_lock);
2430
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002431 if (obj->mm.mapping) {
Chris Wilson4b30cb22016-08-18 17:16:42 +01002432 void *ptr;
2433
Chris Wilson0ce81782017-05-17 13:09:59 +01002434 ptr = page_mask_bits(obj->mm.mapping);
Chris Wilson4b30cb22016-08-18 17:16:42 +01002435 if (is_vmalloc_addr(ptr))
2436 vunmap(ptr);
Chris Wilsonfb8621d2016-04-08 12:11:14 +01002437 else
Chris Wilson4b30cb22016-08-18 17:16:42 +01002438 kunmap(kmap_to_page(ptr));
2439
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002440 obj->mm.mapping = NULL;
Chris Wilson0a798eb2016-04-08 12:11:11 +01002441 }
2442
Chris Wilson96d77632016-10-28 13:58:33 +01002443 __i915_gem_object_reset_page_iter(obj);
2444
Chris Wilson4e5462e2017-03-07 13:20:31 +00002445 if (!IS_ERR(pages))
2446 obj->ops->put_pages(obj, pages);
2447
Matthew Aulda5c081662017-10-06 23:18:18 +01002448 obj->mm.page_sizes.phys = obj->mm.page_sizes.sg = 0;
2449
Chris Wilson1233e2d2016-10-28 13:58:37 +01002450unlock:
2451 mutex_unlock(&obj->mm.lock);
Chris Wilson6c085a72012-08-20 11:40:46 +02002452}
2453
Chris Wilson935a2f72017-02-13 17:15:13 +00002454static bool i915_sg_trim(struct sg_table *orig_st)
Tvrtko Ursulin0c40ce12016-11-09 15:13:43 +00002455{
2456 struct sg_table new_st;
2457 struct scatterlist *sg, *new_sg;
2458 unsigned int i;
2459
2460 if (orig_st->nents == orig_st->orig_nents)
Chris Wilson935a2f72017-02-13 17:15:13 +00002461 return false;
Tvrtko Ursulin0c40ce12016-11-09 15:13:43 +00002462
Chris Wilson8bfc478f2016-12-23 14:57:58 +00002463 if (sg_alloc_table(&new_st, orig_st->nents, GFP_KERNEL | __GFP_NOWARN))
Chris Wilson935a2f72017-02-13 17:15:13 +00002464 return false;
Tvrtko Ursulin0c40ce12016-11-09 15:13:43 +00002465
2466 new_sg = new_st.sgl;
2467 for_each_sg(orig_st->sgl, sg, orig_st->nents, i) {
2468 sg_set_page(new_sg, sg_page(sg), sg->length, 0);
2469 /* called before being DMA mapped, no need to copy sg->dma_* */
2470 new_sg = sg_next(new_sg);
2471 }
Chris Wilsonc2dc6cc2016-12-19 12:43:46 +00002472 GEM_BUG_ON(new_sg); /* Should walk exactly nents and hit the end */
Tvrtko Ursulin0c40ce12016-11-09 15:13:43 +00002473
2474 sg_free_table(orig_st);
2475
2476 *orig_st = new_st;
Chris Wilson935a2f72017-02-13 17:15:13 +00002477 return true;
Tvrtko Ursulin0c40ce12016-11-09 15:13:43 +00002478}
2479
Matthew Auldb91b09e2017-10-06 23:18:17 +01002480static int i915_gem_object_get_pages_gtt(struct drm_i915_gem_object *obj)
Eric Anholt673a3942008-07-30 12:06:12 -07002481{
Chris Wilsonfac5e232016-07-04 11:34:36 +01002482 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
Chris Wilsond766ef52016-12-19 12:43:45 +00002483 const unsigned long page_count = obj->base.size / PAGE_SIZE;
2484 unsigned long i;
Eric Anholt673a3942008-07-30 12:06:12 -07002485 struct address_space *mapping;
Chris Wilson9da3da62012-06-01 15:20:22 +01002486 struct sg_table *st;
2487 struct scatterlist *sg;
Dave Gordon85d12252016-05-20 11:54:06 +01002488 struct sgt_iter sgt_iter;
Eric Anholt673a3942008-07-30 12:06:12 -07002489 struct page *page;
Imre Deak90797e62013-02-18 19:28:03 +02002490 unsigned long last_pfn = 0; /* suppress gcc warning */
Tvrtko Ursulin56024522017-08-03 10:14:17 +01002491 unsigned int max_segment = i915_sg_segment_size();
Matthew Auld84e89782017-10-09 12:00:24 +01002492 unsigned int sg_page_sizes;
Chris Wilson4846bf02017-06-09 12:03:46 +01002493 gfp_t noreclaim;
Imre Deake2273302015-07-09 12:59:05 +03002494 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07002495
Chris Wilson6c085a72012-08-20 11:40:46 +02002496 /* Assert that the object is not currently in any GPU domain. As it
2497 * wasn't in the GTT, there shouldn't be any way it could have been in
2498 * a GPU cache
2499 */
Christian Königc0a51fd2018-02-16 13:43:38 +01002500 GEM_BUG_ON(obj->read_domains & I915_GEM_GPU_DOMAINS);
2501 GEM_BUG_ON(obj->write_domain & I915_GEM_GPU_DOMAINS);
Chris Wilson6c085a72012-08-20 11:40:46 +02002502
Chris Wilson9da3da62012-06-01 15:20:22 +01002503 st = kmalloc(sizeof(*st), GFP_KERNEL);
2504 if (st == NULL)
Matthew Auldb91b09e2017-10-06 23:18:17 +01002505 return -ENOMEM;
Eric Anholt673a3942008-07-30 12:06:12 -07002506
Chris Wilsond766ef52016-12-19 12:43:45 +00002507rebuild_st:
Chris Wilson9da3da62012-06-01 15:20:22 +01002508 if (sg_alloc_table(st, page_count, GFP_KERNEL)) {
Chris Wilson9da3da62012-06-01 15:20:22 +01002509 kfree(st);
Matthew Auldb91b09e2017-10-06 23:18:17 +01002510 return -ENOMEM;
Chris Wilson9da3da62012-06-01 15:20:22 +01002511 }
2512
2513 /* Get the list of pages out of our struct file. They'll be pinned
2514 * at this point until we release them.
2515 *
2516 * Fail silently without starting the shrinker
2517 */
Al Viro93c76a32015-12-04 23:45:44 -05002518 mapping = obj->base.filp->f_mapping;
Chris Wilson0f6ab552017-06-09 12:03:48 +01002519 noreclaim = mapping_gfp_constraint(mapping, ~__GFP_RECLAIM);
Chris Wilson4846bf02017-06-09 12:03:46 +01002520 noreclaim |= __GFP_NORETRY | __GFP_NOWARN;
2521
Imre Deak90797e62013-02-18 19:28:03 +02002522 sg = st->sgl;
2523 st->nents = 0;
Matthew Auld84e89782017-10-09 12:00:24 +01002524 sg_page_sizes = 0;
Imre Deak90797e62013-02-18 19:28:03 +02002525 for (i = 0; i < page_count; i++) {
Chris Wilson4846bf02017-06-09 12:03:46 +01002526 const unsigned int shrink[] = {
2527 I915_SHRINK_BOUND | I915_SHRINK_UNBOUND | I915_SHRINK_PURGEABLE,
2528 0,
2529 }, *s = shrink;
2530 gfp_t gfp = noreclaim;
2531
2532 do {
Chris Wilson6c085a72012-08-20 11:40:46 +02002533 page = shmem_read_mapping_page_gfp(mapping, i, gfp);
Chris Wilson4846bf02017-06-09 12:03:46 +01002534 if (likely(!IS_ERR(page)))
2535 break;
2536
2537 if (!*s) {
2538 ret = PTR_ERR(page);
2539 goto err_sg;
2540 }
2541
Chris Wilson912d5722017-09-06 16:19:30 -07002542 i915_gem_shrink(dev_priv, 2 * page_count, NULL, *s++);
Chris Wilson4846bf02017-06-09 12:03:46 +01002543 cond_resched();
Chris Wilson24f8e002017-03-22 11:05:21 +00002544
Chris Wilson6c085a72012-08-20 11:40:46 +02002545 /* We've tried hard to allocate the memory by reaping
2546 * our own buffer, now let the real VM do its job and
2547 * go down in flames if truly OOM.
Chris Wilson24f8e002017-03-22 11:05:21 +00002548 *
2549 * However, since graphics tend to be disposable,
2550 * defer the oom here by reporting the ENOMEM back
2551 * to userspace.
Chris Wilson6c085a72012-08-20 11:40:46 +02002552 */
Chris Wilson4846bf02017-06-09 12:03:46 +01002553 if (!*s) {
2554 /* reclaim and warn, but no oom */
2555 gfp = mapping_gfp_mask(mapping);
Chris Wilsoneaf41802017-06-09 12:03:47 +01002556
2557 /* Our bo are always dirty and so we require
2558 * kswapd to reclaim our pages (direct reclaim
2559 * does not effectively begin pageout of our
2560 * buffers on its own). However, direct reclaim
2561 * only waits for kswapd when under allocation
2562 * congestion. So as a result __GFP_RECLAIM is
2563 * unreliable and fails to actually reclaim our
2564 * dirty pages -- unless you try over and over
2565 * again with !__GFP_NORETRY. However, we still
2566 * want to fail this allocation rather than
2567 * trigger the out-of-memory killer and for
Michal Hockodbb32952017-07-12 14:36:55 -07002568 * this we want __GFP_RETRY_MAYFAIL.
Chris Wilsoneaf41802017-06-09 12:03:47 +01002569 */
Michal Hockodbb32952017-07-12 14:36:55 -07002570 gfp |= __GFP_RETRY_MAYFAIL;
Imre Deake2273302015-07-09 12:59:05 +03002571 }
Chris Wilson4846bf02017-06-09 12:03:46 +01002572 } while (1);
2573
Chris Wilson871dfbd2016-10-11 09:20:21 +01002574 if (!i ||
2575 sg->length >= max_segment ||
2576 page_to_pfn(page) != last_pfn + 1) {
Matthew Aulda5c081662017-10-06 23:18:18 +01002577 if (i) {
Matthew Auld84e89782017-10-09 12:00:24 +01002578 sg_page_sizes |= sg->length;
Imre Deak90797e62013-02-18 19:28:03 +02002579 sg = sg_next(sg);
Matthew Aulda5c081662017-10-06 23:18:18 +01002580 }
Imre Deak90797e62013-02-18 19:28:03 +02002581 st->nents++;
2582 sg_set_page(sg, page, PAGE_SIZE, 0);
2583 } else {
2584 sg->length += PAGE_SIZE;
2585 }
2586 last_pfn = page_to_pfn(page);
Daniel Vetter3bbbe702013-10-07 17:15:45 -03002587
2588 /* Check that the i965g/gm workaround works. */
2589 WARN_ON((gfp & __GFP_DMA32) && (last_pfn >= 0x00100000UL));
Eric Anholt673a3942008-07-30 12:06:12 -07002590 }
Matthew Aulda5c081662017-10-06 23:18:18 +01002591 if (sg) { /* loop terminated early; short sg table */
Matthew Auld84e89782017-10-09 12:00:24 +01002592 sg_page_sizes |= sg->length;
Konrad Rzeszutek Wilk426729d2013-06-24 11:47:48 -04002593 sg_mark_end(sg);
Matthew Aulda5c081662017-10-06 23:18:18 +01002594 }
Chris Wilson74ce6b62012-10-19 15:51:06 +01002595
Tvrtko Ursulin0c40ce12016-11-09 15:13:43 +00002596 /* Trim unused sg entries to avoid wasting memory. */
2597 i915_sg_trim(st);
2598
Chris Wilson03ac84f2016-10-28 13:58:36 +01002599 ret = i915_gem_gtt_prepare_pages(obj, st);
Chris Wilsond766ef52016-12-19 12:43:45 +00002600 if (ret) {
2601 /* DMA remapping failed? One possible cause is that
2602 * it could not reserve enough large entries, asking
2603 * for PAGE_SIZE chunks instead may be helpful.
2604 */
2605 if (max_segment > PAGE_SIZE) {
2606 for_each_sgt_page(page, sgt_iter, st)
2607 put_page(page);
2608 sg_free_table(st);
2609
2610 max_segment = PAGE_SIZE;
2611 goto rebuild_st;
2612 } else {
2613 dev_warn(&dev_priv->drm.pdev->dev,
2614 "Failed to DMA remap %lu pages\n",
2615 page_count);
2616 goto err_pages;
2617 }
2618 }
Imre Deake2273302015-07-09 12:59:05 +03002619
Eric Anholt673a3942008-07-30 12:06:12 -07002620 if (i915_gem_object_needs_bit17_swizzle(obj))
Chris Wilson03ac84f2016-10-28 13:58:36 +01002621 i915_gem_object_do_bit_17_swizzle(obj, st);
Eric Anholt673a3942008-07-30 12:06:12 -07002622
Matthew Auld84e89782017-10-09 12:00:24 +01002623 __i915_gem_object_set_pages(obj, st, sg_page_sizes);
Matthew Auldb91b09e2017-10-06 23:18:17 +01002624
2625 return 0;
Eric Anholt673a3942008-07-30 12:06:12 -07002626
Chris Wilsonb17993b2016-11-14 11:29:30 +00002627err_sg:
Imre Deak90797e62013-02-18 19:28:03 +02002628 sg_mark_end(sg);
Chris Wilsonb17993b2016-11-14 11:29:30 +00002629err_pages:
Dave Gordon85d12252016-05-20 11:54:06 +01002630 for_each_sgt_page(page, sgt_iter, st)
2631 put_page(page);
Chris Wilson9da3da62012-06-01 15:20:22 +01002632 sg_free_table(st);
2633 kfree(st);
Chris Wilson0820baf2014-03-25 13:23:03 +00002634
2635 /* shmemfs first checks if there is enough memory to allocate the page
2636 * and reports ENOSPC should there be insufficient, along with the usual
2637 * ENOMEM for a genuine allocation failure.
2638 *
2639 * We use ENOSPC in our driver to mean that we have run out of aperture
2640 * space and so want to translate the error from shmemfs back to our
2641 * usual understanding of ENOMEM.
2642 */
Imre Deake2273302015-07-09 12:59:05 +03002643 if (ret == -ENOSPC)
2644 ret = -ENOMEM;
2645
Matthew Auldb91b09e2017-10-06 23:18:17 +01002646 return ret;
Chris Wilson03ac84f2016-10-28 13:58:36 +01002647}
2648
2649void __i915_gem_object_set_pages(struct drm_i915_gem_object *obj,
Matthew Aulda5c081662017-10-06 23:18:18 +01002650 struct sg_table *pages,
Matthew Auld84e89782017-10-09 12:00:24 +01002651 unsigned int sg_page_sizes)
Chris Wilson03ac84f2016-10-28 13:58:36 +01002652{
Matthew Aulda5c081662017-10-06 23:18:18 +01002653 struct drm_i915_private *i915 = to_i915(obj->base.dev);
2654 unsigned long supported = INTEL_INFO(i915)->page_sizes;
2655 int i;
2656
Chris Wilson1233e2d2016-10-28 13:58:37 +01002657 lockdep_assert_held(&obj->mm.lock);
Chris Wilson03ac84f2016-10-28 13:58:36 +01002658
2659 obj->mm.get_page.sg_pos = pages->sgl;
2660 obj->mm.get_page.sg_idx = 0;
2661
2662 obj->mm.pages = pages;
Chris Wilson2c3a3f42016-11-04 10:30:01 +00002663
2664 if (i915_gem_object_is_tiled(obj) &&
Chris Wilsonf2123812017-10-16 12:40:37 +01002665 i915->quirks & QUIRK_PIN_SWIZZLED_PAGES) {
Chris Wilson2c3a3f42016-11-04 10:30:01 +00002666 GEM_BUG_ON(obj->mm.quirked);
2667 __i915_gem_object_pin_pages(obj);
2668 obj->mm.quirked = true;
2669 }
Matthew Aulda5c081662017-10-06 23:18:18 +01002670
Matthew Auld84e89782017-10-09 12:00:24 +01002671 GEM_BUG_ON(!sg_page_sizes);
2672 obj->mm.page_sizes.phys = sg_page_sizes;
Matthew Aulda5c081662017-10-06 23:18:18 +01002673
2674 /*
Matthew Auld84e89782017-10-09 12:00:24 +01002675 * Calculate the supported page-sizes which fit into the given
2676 * sg_page_sizes. This will give us the page-sizes which we may be able
2677 * to use opportunistically when later inserting into the GTT. For
2678 * example if phys=2G, then in theory we should be able to use 1G, 2M,
2679 * 64K or 4K pages, although in practice this will depend on a number of
2680 * other factors.
Matthew Aulda5c081662017-10-06 23:18:18 +01002681 */
2682 obj->mm.page_sizes.sg = 0;
2683 for_each_set_bit(i, &supported, ilog2(I915_GTT_MAX_PAGE_SIZE) + 1) {
2684 if (obj->mm.page_sizes.phys & ~0u << i)
2685 obj->mm.page_sizes.sg |= BIT(i);
2686 }
Matthew Aulda5c081662017-10-06 23:18:18 +01002687 GEM_BUG_ON(!HAS_PAGE_SIZES(i915, obj->mm.page_sizes.sg));
Chris Wilsonf2123812017-10-16 12:40:37 +01002688
2689 spin_lock(&i915->mm.obj_lock);
2690 list_add(&obj->mm.link, &i915->mm.unbound_list);
2691 spin_unlock(&i915->mm.obj_lock);
Chris Wilson03ac84f2016-10-28 13:58:36 +01002692}
2693
2694static int ____i915_gem_object_get_pages(struct drm_i915_gem_object *obj)
2695{
Matthew Auldb91b09e2017-10-06 23:18:17 +01002696 int err;
Chris Wilson03ac84f2016-10-28 13:58:36 +01002697
2698 if (unlikely(obj->mm.madv != I915_MADV_WILLNEED)) {
2699 DRM_DEBUG("Attempting to obtain a purgeable object\n");
2700 return -EFAULT;
2701 }
2702
Matthew Auldb91b09e2017-10-06 23:18:17 +01002703 err = obj->ops->get_pages(obj);
Matthew Auldb65a9b92017-12-18 10:38:55 +00002704 GEM_BUG_ON(!err && !i915_gem_object_has_pages(obj));
Chris Wilson03ac84f2016-10-28 13:58:36 +01002705
Matthew Auldb91b09e2017-10-06 23:18:17 +01002706 return err;
Eric Anholt673a3942008-07-30 12:06:12 -07002707}
2708
Chris Wilson37e680a2012-06-07 15:38:42 +01002709/* Ensure that the associated pages are gathered from the backing storage
Chris Wilson1233e2d2016-10-28 13:58:37 +01002710 * and pinned into our object. i915_gem_object_pin_pages() may be called
Chris Wilson37e680a2012-06-07 15:38:42 +01002711 * multiple times before they are released by a single call to
Chris Wilson1233e2d2016-10-28 13:58:37 +01002712 * i915_gem_object_unpin_pages() - once the pages are no longer referenced
Chris Wilson37e680a2012-06-07 15:38:42 +01002713 * either as a result of memory pressure (reaping pages under the shrinker)
2714 * or as the object is itself released.
2715 */
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002716int __i915_gem_object_get_pages(struct drm_i915_gem_object *obj)
Chris Wilson37e680a2012-06-07 15:38:42 +01002717{
Chris Wilson03ac84f2016-10-28 13:58:36 +01002718 int err;
Chris Wilson37e680a2012-06-07 15:38:42 +01002719
Chris Wilson1233e2d2016-10-28 13:58:37 +01002720 err = mutex_lock_interruptible(&obj->mm.lock);
2721 if (err)
2722 return err;
Chris Wilson4c7d62c2016-10-28 13:58:32 +01002723
Chris Wilsonf1fa4f42017-10-13 21:26:13 +01002724 if (unlikely(!i915_gem_object_has_pages(obj))) {
Chris Wilson88c880b2017-09-06 14:52:20 +01002725 GEM_BUG_ON(i915_gem_object_has_pinned_pages(obj));
2726
Chris Wilson2c3a3f42016-11-04 10:30:01 +00002727 err = ____i915_gem_object_get_pages(obj);
2728 if (err)
2729 goto unlock;
2730
2731 smp_mb__before_atomic();
Chris Wilson1233e2d2016-10-28 13:58:37 +01002732 }
Chris Wilson2c3a3f42016-11-04 10:30:01 +00002733 atomic_inc(&obj->mm.pages_pin_count);
Chris Wilson43e28f02013-01-08 10:53:09 +00002734
Chris Wilson1233e2d2016-10-28 13:58:37 +01002735unlock:
2736 mutex_unlock(&obj->mm.lock);
Chris Wilson03ac84f2016-10-28 13:58:36 +01002737 return err;
Eric Anholt673a3942008-07-30 12:06:12 -07002738}
2739
Dave Gordondd6034c2016-05-20 11:54:04 +01002740/* The 'mapping' part of i915_gem_object_pin_map() below */
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002741static void *i915_gem_object_map(const struct drm_i915_gem_object *obj,
2742 enum i915_map_type type)
Dave Gordondd6034c2016-05-20 11:54:04 +01002743{
2744 unsigned long n_pages = obj->base.size >> PAGE_SHIFT;
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002745 struct sg_table *sgt = obj->mm.pages;
Dave Gordon85d12252016-05-20 11:54:06 +01002746 struct sgt_iter sgt_iter;
2747 struct page *page;
Dave Gordonb338fa42016-05-20 11:54:05 +01002748 struct page *stack_pages[32];
2749 struct page **pages = stack_pages;
Dave Gordondd6034c2016-05-20 11:54:04 +01002750 unsigned long i = 0;
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002751 pgprot_t pgprot;
Dave Gordondd6034c2016-05-20 11:54:04 +01002752 void *addr;
2753
2754 /* A single page can always be kmapped */
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002755 if (n_pages == 1 && type == I915_MAP_WB)
Dave Gordondd6034c2016-05-20 11:54:04 +01002756 return kmap(sg_page(sgt->sgl));
2757
Dave Gordonb338fa42016-05-20 11:54:05 +01002758 if (n_pages > ARRAY_SIZE(stack_pages)) {
2759 /* Too big for stack -- allocate temporary array instead */
Michal Hocko0ee931c2017-09-13 16:28:29 -07002760 pages = kvmalloc_array(n_pages, sizeof(*pages), GFP_KERNEL);
Dave Gordonb338fa42016-05-20 11:54:05 +01002761 if (!pages)
2762 return NULL;
2763 }
Dave Gordondd6034c2016-05-20 11:54:04 +01002764
Dave Gordon85d12252016-05-20 11:54:06 +01002765 for_each_sgt_page(page, sgt_iter, sgt)
2766 pages[i++] = page;
Dave Gordondd6034c2016-05-20 11:54:04 +01002767
2768 /* Check that we have the expected number of pages */
2769 GEM_BUG_ON(i != n_pages);
2770
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002771 switch (type) {
Chris Wilsona575c672017-08-28 11:46:31 +01002772 default:
2773 MISSING_CASE(type);
2774 /* fallthrough to use PAGE_KERNEL anyway */
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002775 case I915_MAP_WB:
2776 pgprot = PAGE_KERNEL;
2777 break;
2778 case I915_MAP_WC:
2779 pgprot = pgprot_writecombine(PAGE_KERNEL_IO);
2780 break;
2781 }
2782 addr = vmap(pages, n_pages, 0, pgprot);
Dave Gordondd6034c2016-05-20 11:54:04 +01002783
Dave Gordonb338fa42016-05-20 11:54:05 +01002784 if (pages != stack_pages)
Michal Hocko20981052017-05-17 14:23:12 +02002785 kvfree(pages);
Dave Gordondd6034c2016-05-20 11:54:04 +01002786
2787 return addr;
2788}
2789
2790/* get, pin, and map the pages of the object into kernel space */
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002791void *i915_gem_object_pin_map(struct drm_i915_gem_object *obj,
2792 enum i915_map_type type)
Chris Wilson0a798eb2016-04-08 12:11:11 +01002793{
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002794 enum i915_map_type has_type;
2795 bool pinned;
2796 void *ptr;
Chris Wilson0a798eb2016-04-08 12:11:11 +01002797 int ret;
2798
Tina Zhanga03f3952017-11-14 10:25:13 +00002799 if (unlikely(!i915_gem_object_has_struct_page(obj)))
2800 return ERR_PTR(-ENXIO);
Chris Wilson0a798eb2016-04-08 12:11:11 +01002801
Chris Wilson1233e2d2016-10-28 13:58:37 +01002802 ret = mutex_lock_interruptible(&obj->mm.lock);
Chris Wilson0a798eb2016-04-08 12:11:11 +01002803 if (ret)
2804 return ERR_PTR(ret);
2805
Chris Wilsona575c672017-08-28 11:46:31 +01002806 pinned = !(type & I915_MAP_OVERRIDE);
2807 type &= ~I915_MAP_OVERRIDE;
2808
Chris Wilson1233e2d2016-10-28 13:58:37 +01002809 if (!atomic_inc_not_zero(&obj->mm.pages_pin_count)) {
Chris Wilsonf1fa4f42017-10-13 21:26:13 +01002810 if (unlikely(!i915_gem_object_has_pages(obj))) {
Chris Wilson88c880b2017-09-06 14:52:20 +01002811 GEM_BUG_ON(i915_gem_object_has_pinned_pages(obj));
2812
Chris Wilson2c3a3f42016-11-04 10:30:01 +00002813 ret = ____i915_gem_object_get_pages(obj);
2814 if (ret)
2815 goto err_unlock;
Chris Wilson1233e2d2016-10-28 13:58:37 +01002816
Chris Wilson2c3a3f42016-11-04 10:30:01 +00002817 smp_mb__before_atomic();
2818 }
2819 atomic_inc(&obj->mm.pages_pin_count);
Chris Wilson1233e2d2016-10-28 13:58:37 +01002820 pinned = false;
2821 }
Chris Wilsonf1fa4f42017-10-13 21:26:13 +01002822 GEM_BUG_ON(!i915_gem_object_has_pages(obj));
Chris Wilson0a798eb2016-04-08 12:11:11 +01002823
Chris Wilson0ce81782017-05-17 13:09:59 +01002824 ptr = page_unpack_bits(obj->mm.mapping, &has_type);
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002825 if (ptr && has_type != type) {
2826 if (pinned) {
2827 ret = -EBUSY;
Chris Wilson1233e2d2016-10-28 13:58:37 +01002828 goto err_unpin;
Chris Wilson0a798eb2016-04-08 12:11:11 +01002829 }
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002830
2831 if (is_vmalloc_addr(ptr))
2832 vunmap(ptr);
2833 else
2834 kunmap(kmap_to_page(ptr));
2835
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002836 ptr = obj->mm.mapping = NULL;
Chris Wilson0a798eb2016-04-08 12:11:11 +01002837 }
2838
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002839 if (!ptr) {
2840 ptr = i915_gem_object_map(obj, type);
2841 if (!ptr) {
2842 ret = -ENOMEM;
Chris Wilson1233e2d2016-10-28 13:58:37 +01002843 goto err_unpin;
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002844 }
2845
Chris Wilson0ce81782017-05-17 13:09:59 +01002846 obj->mm.mapping = page_pack_bits(ptr, type);
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002847 }
2848
Chris Wilson1233e2d2016-10-28 13:58:37 +01002849out_unlock:
2850 mutex_unlock(&obj->mm.lock);
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002851 return ptr;
2852
Chris Wilson1233e2d2016-10-28 13:58:37 +01002853err_unpin:
2854 atomic_dec(&obj->mm.pages_pin_count);
2855err_unlock:
2856 ptr = ERR_PTR(ret);
2857 goto out_unlock;
Chris Wilson0a798eb2016-04-08 12:11:11 +01002858}
2859
Chris Wilson7c55e2c2017-03-07 12:03:38 +00002860static int
2861i915_gem_object_pwrite_gtt(struct drm_i915_gem_object *obj,
2862 const struct drm_i915_gem_pwrite *arg)
2863{
2864 struct address_space *mapping = obj->base.filp->f_mapping;
2865 char __user *user_data = u64_to_user_ptr(arg->data_ptr);
2866 u64 remain, offset;
2867 unsigned int pg;
2868
2869 /* Before we instantiate/pin the backing store for our use, we
2870 * can prepopulate the shmemfs filp efficiently using a write into
2871 * the pagecache. We avoid the penalty of instantiating all the
2872 * pages, important if the user is just writing to a few and never
2873 * uses the object on the GPU, and using a direct write into shmemfs
2874 * allows it to avoid the cost of retrieving a page (either swapin
2875 * or clearing-before-use) before it is overwritten.
2876 */
Chris Wilsonf1fa4f42017-10-13 21:26:13 +01002877 if (i915_gem_object_has_pages(obj))
Chris Wilson7c55e2c2017-03-07 12:03:38 +00002878 return -ENODEV;
2879
Chris Wilsona6d65e42017-10-16 21:27:32 +01002880 if (obj->mm.madv != I915_MADV_WILLNEED)
2881 return -EFAULT;
2882
Chris Wilson7c55e2c2017-03-07 12:03:38 +00002883 /* Before the pages are instantiated the object is treated as being
2884 * in the CPU domain. The pages will be clflushed as required before
2885 * use, and we can freely write into the pages directly. If userspace
2886 * races pwrite with any other operation; corruption will ensue -
2887 * that is userspace's prerogative!
2888 */
2889
2890 remain = arg->size;
2891 offset = arg->offset;
2892 pg = offset_in_page(offset);
2893
2894 do {
2895 unsigned int len, unwritten;
2896 struct page *page;
2897 void *data, *vaddr;
2898 int err;
2899
2900 len = PAGE_SIZE - pg;
2901 if (len > remain)
2902 len = remain;
2903
2904 err = pagecache_write_begin(obj->base.filp, mapping,
2905 offset, len, 0,
2906 &page, &data);
2907 if (err < 0)
2908 return err;
2909
2910 vaddr = kmap(page);
2911 unwritten = copy_from_user(vaddr + pg, user_data, len);
2912 kunmap(page);
2913
2914 err = pagecache_write_end(obj->base.filp, mapping,
2915 offset, len, len - unwritten,
2916 page, data);
2917 if (err < 0)
2918 return err;
2919
2920 if (unwritten)
2921 return -EFAULT;
2922
2923 remain -= len;
2924 user_data += len;
2925 offset += len;
2926 pg = 0;
2927 } while (remain);
2928
2929 return 0;
2930}
2931
Mika Kuoppalae5e1fc42016-11-16 17:20:31 +02002932static void i915_gem_context_mark_guilty(struct i915_gem_context *ctx)
Mika Kuoppalaaa60c662013-06-12 15:13:20 +03002933{
Chris Wilson77b25a92017-07-21 13:32:30 +01002934 bool banned;
Mika Kuoppalab083a082016-11-18 15:10:47 +02002935
Chris Wilson77b25a92017-07-21 13:32:30 +01002936 atomic_inc(&ctx->guilty_count);
2937
Chris Wilson24eae082018-02-05 09:22:01 +00002938 banned = false;
2939 if (i915_gem_context_is_bannable(ctx)) {
2940 unsigned int score;
2941
2942 score = atomic_add_return(CONTEXT_SCORE_GUILTY,
2943 &ctx->ban_score);
2944 banned = score >= CONTEXT_SCORE_BAN_THRESHOLD;
2945
2946 DRM_DEBUG_DRIVER("context %s marked guilty (score %d) banned? %s\n",
2947 ctx->name, score, yesno(banned));
2948 }
Chris Wilson77b25a92017-07-21 13:32:30 +01002949 if (!banned)
Mika Kuoppalab083a082016-11-18 15:10:47 +02002950 return;
2951
Chris Wilson77b25a92017-07-21 13:32:30 +01002952 i915_gem_context_set_banned(ctx);
2953 if (!IS_ERR_OR_NULL(ctx->file_priv)) {
2954 atomic_inc(&ctx->file_priv->context_bans);
2955 DRM_DEBUG_DRIVER("client %s has had %d context banned\n",
2956 ctx->name, atomic_read(&ctx->file_priv->context_bans));
2957 }
Mika Kuoppalae5e1fc42016-11-16 17:20:31 +02002958}
2959
2960static void i915_gem_context_mark_innocent(struct i915_gem_context *ctx)
2961{
Chris Wilson77b25a92017-07-21 13:32:30 +01002962 atomic_inc(&ctx->active_count);
Mika Kuoppalaaa60c662013-06-12 15:13:20 +03002963}
2964
Chris Wilsone61e0f52018-02-21 09:56:36 +00002965struct i915_request *
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00002966i915_gem_find_active_request(struct intel_engine_cs *engine)
Chris Wilson9375e442010-09-19 12:21:28 +01002967{
Chris Wilsone61e0f52018-02-21 09:56:36 +00002968 struct i915_request *request, *active = NULL;
Chris Wilson754c9fd2017-02-23 07:44:14 +00002969 unsigned long flags;
Mika Kuoppalaaa60c662013-06-12 15:13:20 +03002970
Chris Wilsonf69a02c2016-07-01 17:23:16 +01002971 /* We are called by the error capture and reset at a random
2972 * point in time. In particular, note that neither is crucially
2973 * ordered with an interrupt. After a hang, the GPU is dead and we
2974 * assume that no more writes can happen (we waited long enough for
2975 * all writes that were in transaction to be flushed) - adding an
2976 * extra delay for a recent interrupt is pointless. Hence, we do
2977 * not need an engine->irq_seqno_barrier() before the seqno reads.
2978 */
Chris Wilson754c9fd2017-02-23 07:44:14 +00002979 spin_lock_irqsave(&engine->timeline->lock, flags);
Chris Wilson73cb9702016-10-28 13:58:46 +01002980 list_for_each_entry(request, &engine->timeline->requests, link) {
Chris Wilsone61e0f52018-02-21 09:56:36 +00002981 if (__i915_request_completed(request, request->global_seqno))
Chris Wilson4db080f2013-12-04 11:37:09 +00002982 continue;
Mika Kuoppalaaa60c662013-06-12 15:13:20 +03002983
Mika Kuoppala36193ac2017-01-17 17:59:02 +02002984 GEM_BUG_ON(request->engine != engine);
Chris Wilsonc00122f32017-02-12 17:19:58 +00002985 GEM_BUG_ON(test_bit(DMA_FENCE_FLAG_SIGNALED_BIT,
2986 &request->fence.flags));
Mika Kuoppalab6b0fac2014-01-30 19:04:43 +02002987
Chris Wilson754c9fd2017-02-23 07:44:14 +00002988 active = request;
2989 break;
2990 }
2991 spin_unlock_irqrestore(&engine->timeline->lock, flags);
2992
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 Wilsone61e0f52018-02-21 09:56:36 +00003003 struct i915_request *request = NULL;
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
3014 /*
3015 * Prevent the signaler thread from updating the request
Michel Thierrya1ef70e2017-06-20 10:57:47 +01003016 * state (by calling dma_fence_signal) as we are processing
3017 * the reset. The write from the GPU of the seqno is
3018 * asynchronous and the signaler thread may see a different
3019 * value to us and declare the request complete, even though
3020 * the reset routine have picked that request as the active
3021 * (incomplete) request. This conflict is not handled
3022 * gracefully!
3023 */
3024 kthread_park(engine->breadcrumbs.signaler);
3025
Chris Wilson1749d902017-10-09 12:02:59 +01003026 /*
3027 * Prevent request submission to the hardware until we have
Michel Thierrya1ef70e2017-06-20 10:57:47 +01003028 * completed the reset in i915_gem_reset_finish(). If a request
3029 * is completed by one engine, it may then queue a request
Sagar Arun Kamblec6dce8f2017-11-16 19:02:37 +05303030 * to a second via its execlists->tasklet *just* as we are
Michel Thierrya1ef70e2017-06-20 10:57:47 +01003031 * calling engine->init_hw() and also writing the ELSP.
Sagar Arun Kamblec6dce8f2017-11-16 19:02:37 +05303032 * Turning off the execlists->tasklet until the reset is over
Michel Thierrya1ef70e2017-06-20 10:57:47 +01003033 * prevents the race.
Chris Wilson68ad3612018-03-07 13:42:26 +00003034 *
3035 * Note that this needs to be a single atomic operation on the
3036 * tasklet (flush existing tasks, prevent new tasks) to prevent
3037 * a race between reset and set-wedged. It is not, so we do the best
3038 * we can atm and make sure we don't lock the machine up in the more
3039 * common case of recursively being called from set-wedged from inside
3040 * i915_reset.
Michel Thierrya1ef70e2017-06-20 10:57:47 +01003041 */
Chris Wilson68ad3612018-03-07 13:42:26 +00003042 if (!atomic_read(&engine->execlists.tasklet.count))
3043 tasklet_kill(&engine->execlists.tasklet);
Sagar Arun Kamblec6dce8f2017-11-16 19:02:37 +05303044 tasklet_disable(&engine->execlists.tasklet);
Michel Thierrya1ef70e2017-06-20 10:57:47 +01003045
Michał Winiarskic41937f2017-10-26 15:35:58 +02003046 /*
3047 * We're using worker to queue preemption requests from the tasklet in
3048 * GuC submission mode.
3049 * Even though tasklet was disabled, we may still have a worker queued.
3050 * Let's make sure that all workers scheduled before disabling the
3051 * tasklet are completed before continuing with the reset.
3052 */
3053 if (engine->i915->guc.preempt_wq)
3054 flush_workqueue(engine->i915->guc.preempt_wq);
3055
Michel Thierrya1ef70e2017-06-20 10:57:47 +01003056 if (engine->irq_seqno_barrier)
3057 engine->irq_seqno_barrier(engine);
3058
Chris Wilsond1d1ebf42017-07-21 13:32:33 +01003059 request = i915_gem_find_active_request(engine);
3060 if (request && request->fence.error == -EIO)
3061 request = ERR_PTR(-EIO); /* Previous reset failed! */
Michel Thierrya1ef70e2017-06-20 10:57:47 +01003062
3063 return request;
3064}
3065
Chris Wilson0e178ae2017-01-17 17:59:06 +02003066int i915_gem_reset_prepare(struct drm_i915_private *dev_priv)
Chris Wilson4c965542017-01-17 17:59:01 +02003067{
3068 struct intel_engine_cs *engine;
Chris Wilsone61e0f52018-02-21 09:56:36 +00003069 struct i915_request *request;
Chris Wilson4c965542017-01-17 17:59:01 +02003070 enum intel_engine_id id;
Chris Wilson0e178ae2017-01-17 17:59:06 +02003071 int err = 0;
Chris Wilson4c965542017-01-17 17:59:01 +02003072
Chris Wilson0e178ae2017-01-17 17:59:06 +02003073 for_each_engine(engine, dev_priv, id) {
Michel Thierrya1ef70e2017-06-20 10:57:47 +01003074 request = i915_gem_reset_prepare_engine(engine);
3075 if (IS_ERR(request)) {
3076 err = PTR_ERR(request);
3077 continue;
Chris Wilson0e178ae2017-01-17 17:59:06 +02003078 }
Michel Thierryc64992e2017-06-20 10:57:44 +01003079
3080 engine->hangcheck.active_request = request;
Chris Wilson0e178ae2017-01-17 17:59:06 +02003081 }
3082
Chris Wilson4c965542017-01-17 17:59:01 +02003083 i915_gem_revoke_fences(dev_priv);
Michal Wajdeczkoc37d5722018-03-12 13:03:07 +00003084 intel_uc_sanitize(dev_priv);
Chris Wilson0e178ae2017-01-17 17:59:06 +02003085
3086 return err;
Chris Wilson4c965542017-01-17 17:59:01 +02003087}
3088
Chris Wilsone61e0f52018-02-21 09:56:36 +00003089static void skip_request(struct i915_request *request)
Mika Kuoppalab6b0fac2014-01-30 19:04:43 +02003090{
Chris Wilson821ed7d2016-09-09 14:11:53 +01003091 void *vaddr = request->ring->vaddr;
3092 u32 head;
Mika Kuoppalab6b0fac2014-01-30 19:04:43 +02003093
Chris Wilson821ed7d2016-09-09 14:11:53 +01003094 /* As this request likely depends on state from the lost
3095 * context, clear out all the user operations leaving the
3096 * breadcrumb at the end (so we get the fence notifications).
3097 */
3098 head = request->head;
3099 if (request->postfix < head) {
3100 memset(vaddr + head, 0, request->ring->size - head);
3101 head = 0;
3102 }
3103 memset(vaddr + head, 0, request->postfix - head);
Chris Wilsonc0d5f322017-01-10 17:22:43 +00003104
3105 dma_fence_set_error(&request->fence, -EIO);
Chris Wilson4db080f2013-12-04 11:37:09 +00003106}
3107
Chris Wilsone61e0f52018-02-21 09:56:36 +00003108static void engine_skip_context(struct i915_request *request)
Mika Kuoppala36193ac2017-01-17 17:59:02 +02003109{
3110 struct intel_engine_cs *engine = request->engine;
3111 struct i915_gem_context *hung_ctx = request->ctx;
3112 struct intel_timeline *timeline;
3113 unsigned long flags;
3114
3115 timeline = i915_gem_context_lookup_timeline(hung_ctx, engine);
3116
3117 spin_lock_irqsave(&engine->timeline->lock, flags);
3118 spin_lock(&timeline->lock);
3119
3120 list_for_each_entry_continue(request, &engine->timeline->requests, link)
3121 if (request->ctx == hung_ctx)
3122 skip_request(request);
3123
3124 list_for_each_entry(request, &timeline->requests, link)
3125 skip_request(request);
3126
3127 spin_unlock(&timeline->lock);
3128 spin_unlock_irqrestore(&engine->timeline->lock, flags);
3129}
3130
Chris Wilsond1d1ebf42017-07-21 13:32:33 +01003131/* Returns the request if it was guilty of the hang */
Chris Wilsone61e0f52018-02-21 09:56:36 +00003132static struct i915_request *
Chris Wilsond1d1ebf42017-07-21 13:32:33 +01003133i915_gem_reset_request(struct intel_engine_cs *engine,
Chris Wilsonbba08692018-04-06 23:03:53 +01003134 struct i915_request *request,
3135 bool stalled)
Mika Kuoppala61da5362017-01-17 17:59:05 +02003136{
Mika Kuoppala71895a02017-01-17 17:59:07 +02003137 /* The guilty request will get skipped on a hung engine.
3138 *
3139 * Users of client default contexts do not rely on logical
3140 * state preserved between batches so it is safe to execute
3141 * queued requests following the hang. Non default contexts
3142 * rely on preserved state, so skipping a batch loses the
3143 * evolution of the state and it needs to be considered corrupted.
3144 * Executing more queued batches on top of corrupted state is
3145 * risky. But we take the risk by trying to advance through
3146 * the queued requests in order to make the client behaviour
3147 * more predictable around resets, by not throwing away random
3148 * amount of batches it has prepared for execution. Sophisticated
3149 * clients can use gem_reset_stats_ioctl and dma fence status
3150 * (exported via sync_file info ioctl on explicit fences) to observe
3151 * when it loses the context state and should rebuild accordingly.
3152 *
3153 * The context ban, and ultimately the client ban, mechanism are safety
3154 * valves if client submission ends up resulting in nothing more than
3155 * subsequent hangs.
3156 */
3157
Chris Wilsonbba08692018-04-06 23:03:53 +01003158 if (i915_request_completed(request)) {
3159 GEM_TRACE("%s pardoned global=%d (fence %llx:%d), current %d\n",
3160 engine->name, request->global_seqno,
3161 request->fence.context, request->fence.seqno,
3162 intel_engine_get_seqno(engine));
3163 stalled = false;
3164 }
3165
3166 if (stalled) {
Mika Kuoppala61da5362017-01-17 17:59:05 +02003167 i915_gem_context_mark_guilty(request->ctx);
3168 skip_request(request);
Chris Wilsond1d1ebf42017-07-21 13:32:33 +01003169
3170 /* If this context is now banned, skip all pending requests. */
3171 if (i915_gem_context_is_banned(request->ctx))
3172 engine_skip_context(request);
Mika Kuoppala61da5362017-01-17 17:59:05 +02003173 } else {
Chris Wilsond1d1ebf42017-07-21 13:32:33 +01003174 /*
3175 * Since this is not the hung engine, it may have advanced
3176 * since the hang declaration. Double check by refinding
3177 * the active request at the time of the reset.
3178 */
3179 request = i915_gem_find_active_request(engine);
3180 if (request) {
3181 i915_gem_context_mark_innocent(request->ctx);
3182 dma_fence_set_error(&request->fence, -EAGAIN);
3183
3184 /* Rewind the engine to replay the incomplete rq */
3185 spin_lock_irq(&engine->timeline->lock);
3186 request = list_prev_entry(request, link);
3187 if (&request->link == &engine->timeline->requests)
3188 request = NULL;
3189 spin_unlock_irq(&engine->timeline->lock);
3190 }
Mika Kuoppala61da5362017-01-17 17:59:05 +02003191 }
3192
Chris Wilsond1d1ebf42017-07-21 13:32:33 +01003193 return request;
Mika Kuoppala61da5362017-01-17 17:59:05 +02003194}
3195
Michel Thierrya1ef70e2017-06-20 10:57:47 +01003196void i915_gem_reset_engine(struct intel_engine_cs *engine,
Chris Wilsonbba08692018-04-06 23:03:53 +01003197 struct i915_request *request,
3198 bool stalled)
Chris Wilson4db080f2013-12-04 11:37:09 +00003199{
Chris Wilsonfcb1de52017-12-19 09:01:10 +00003200 /*
3201 * Make sure this write is visible before we re-enable the interrupt
3202 * handlers on another CPU, as tasklet_enable() resolves to just
3203 * a compiler barrier which is insufficient for our purpose here.
3204 */
3205 smp_store_mb(engine->irq_posted, 0);
Chris Wilsoned454f22017-07-21 13:32:29 +01003206
Chris Wilsond1d1ebf42017-07-21 13:32:33 +01003207 if (request)
Chris Wilsonbba08692018-04-06 23:03:53 +01003208 request = i915_gem_reset_request(engine, request, stalled);
Chris Wilsond1d1ebf42017-07-21 13:32:33 +01003209
3210 if (request) {
Chris Wilsonc0dcb202017-02-07 15:24:37 +00003211 DRM_DEBUG_DRIVER("resetting %s to restart from tail of request 0x%x\n",
3212 engine->name, request->global_seqno);
Chris Wilsonc0dcb202017-02-07 15:24:37 +00003213 }
Chris Wilson821ed7d2016-09-09 14:11:53 +01003214
3215 /* Setup the CS to resume from the breadcrumb of the hung request */
3216 engine->reset_hw(engine, request);
Chris Wilson821ed7d2016-09-09 14:11:53 +01003217}
3218
Chris Wilsond0667e92018-04-06 23:03:54 +01003219void i915_gem_reset(struct drm_i915_private *dev_priv,
3220 unsigned int stalled_mask)
Chris Wilson821ed7d2016-09-09 14:11:53 +01003221{
3222 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05303223 enum intel_engine_id id;
Chris Wilson821ed7d2016-09-09 14:11:53 +01003224
Chris Wilson4c7d62c2016-10-28 13:58:32 +01003225 lockdep_assert_held(&dev_priv->drm.struct_mutex);
3226
Chris Wilsone61e0f52018-02-21 09:56:36 +00003227 i915_retire_requests(dev_priv);
Chris Wilson821ed7d2016-09-09 14:11:53 +01003228
Chris Wilson2ae55732017-02-12 17:20:02 +00003229 for_each_engine(engine, dev_priv, id) {
3230 struct i915_gem_context *ctx;
3231
Chris Wilsonbba08692018-04-06 23:03:53 +01003232 i915_gem_reset_engine(engine,
3233 engine->hangcheck.active_request,
Chris Wilsond0667e92018-04-06 23:03:54 +01003234 stalled_mask & ENGINE_MASK(id));
Chris Wilson2ae55732017-02-12 17:20:02 +00003235 ctx = fetch_and_zero(&engine->last_retired_context);
3236 if (ctx)
Chris Wilsonab82a062018-04-30 14:15:01 +01003237 intel_context_unpin(ctx, engine);
Chris Wilson7b6da812017-12-16 00:03:34 +00003238
3239 /*
3240 * Ostensibily, we always want a context loaded for powersaving,
3241 * so if the engine is idle after the reset, send a request
3242 * to load our scratch kernel_context.
3243 *
3244 * More mysteriously, if we leave the engine idle after a reset,
3245 * the next userspace batch may hang, with what appears to be
3246 * an incoherent read by the CS (presumably stale TLB). An
3247 * empty request appears sufficient to paper over the glitch.
3248 */
Chris Wilson01b8fdc2018-02-05 15:24:31 +00003249 if (intel_engine_is_idle(engine)) {
Chris Wilsone61e0f52018-02-21 09:56:36 +00003250 struct i915_request *rq;
Chris Wilson7b6da812017-12-16 00:03:34 +00003251
Chris Wilsone61e0f52018-02-21 09:56:36 +00003252 rq = i915_request_alloc(engine,
3253 dev_priv->kernel_context);
Chris Wilson7b6da812017-12-16 00:03:34 +00003254 if (!IS_ERR(rq))
Chris Wilsone61e0f52018-02-21 09:56:36 +00003255 __i915_request_add(rq, false);
Chris Wilson7b6da812017-12-16 00:03:34 +00003256 }
Chris Wilson2ae55732017-02-12 17:20:02 +00003257 }
Chris Wilson821ed7d2016-09-09 14:11:53 +01003258
Tvrtko Ursulin4362f4f2016-11-16 08:55:33 +00003259 i915_gem_restore_fences(dev_priv);
Chris Wilson821ed7d2016-09-09 14:11:53 +01003260}
3261
Michel Thierrya1ef70e2017-06-20 10:57:47 +01003262void i915_gem_reset_finish_engine(struct intel_engine_cs *engine)
3263{
Sagar Arun Kamblec6dce8f2017-11-16 19:02:37 +05303264 tasklet_enable(&engine->execlists.tasklet);
Michel Thierrya1ef70e2017-06-20 10:57:47 +01003265 kthread_unpark(engine->breadcrumbs.signaler);
Chris Wilson1749d902017-10-09 12:02:59 +01003266
3267 intel_uncore_forcewake_put(engine->i915, FORCEWAKE_ALL);
Michel Thierrya1ef70e2017-06-20 10:57:47 +01003268}
3269
Chris Wilsond8027092017-02-08 14:30:32 +00003270void i915_gem_reset_finish(struct drm_i915_private *dev_priv)
3271{
Chris Wilson1f7b8472017-02-08 14:30:33 +00003272 struct intel_engine_cs *engine;
3273 enum intel_engine_id id;
3274
Chris Wilsond8027092017-02-08 14:30:32 +00003275 lockdep_assert_held(&dev_priv->drm.struct_mutex);
Chris Wilson1f7b8472017-02-08 14:30:33 +00003276
Chris Wilsonfe3288b2017-02-12 17:20:01 +00003277 for_each_engine(engine, dev_priv, id) {
Michel Thierryc64992e2017-06-20 10:57:44 +01003278 engine->hangcheck.active_request = NULL;
Michel Thierrya1ef70e2017-06-20 10:57:47 +01003279 i915_gem_reset_finish_engine(engine);
Chris Wilsonfe3288b2017-02-12 17:20:01 +00003280 }
Chris Wilsond8027092017-02-08 14:30:32 +00003281}
3282
Chris Wilsone61e0f52018-02-21 09:56:36 +00003283static void nop_submit_request(struct i915_request *request)
Chris Wilson821ed7d2016-09-09 14:11:53 +01003284{
Chris Wilsond9b13c42018-03-15 13:14:50 +00003285 GEM_TRACE("%s fence %llx:%d -> -EIO\n",
3286 request->engine->name,
3287 request->fence.context, request->fence.seqno);
Daniel Vetteraf7a8ff2017-10-11 11:10:19 +02003288 dma_fence_set_error(&request->fence, -EIO);
3289
Chris Wilsone61e0f52018-02-21 09:56:36 +00003290 i915_request_submit(request);
Daniel Vetteraf7a8ff2017-10-11 11:10:19 +02003291}
3292
Chris Wilsone61e0f52018-02-21 09:56:36 +00003293static void nop_complete_submit_request(struct i915_request *request)
Daniel Vetteraf7a8ff2017-10-11 11:10:19 +02003294{
Chris Wilson8d550822017-10-06 12:56:17 +01003295 unsigned long flags;
3296
Chris Wilsond9b13c42018-03-15 13:14:50 +00003297 GEM_TRACE("%s fence %llx:%d -> -EIO\n",
3298 request->engine->name,
3299 request->fence.context, request->fence.seqno);
Chris Wilson3cd94422017-01-10 17:22:45 +00003300 dma_fence_set_error(&request->fence, -EIO);
Chris Wilson8d550822017-10-06 12:56:17 +01003301
3302 spin_lock_irqsave(&request->engine->timeline->lock, flags);
Chris Wilsone61e0f52018-02-21 09:56:36 +00003303 __i915_request_submit(request);
Chris Wilson3dcf93f72016-11-22 14:41:20 +00003304 intel_engine_init_global_seqno(request->engine, request->global_seqno);
Chris Wilson8d550822017-10-06 12:56:17 +01003305 spin_unlock_irqrestore(&request->engine->timeline->lock, flags);
Chris Wilson821ed7d2016-09-09 14:11:53 +01003306}
3307
Daniel Vetteraf7a8ff2017-10-11 11:10:19 +02003308void i915_gem_set_wedged(struct drm_i915_private *i915)
Chris Wilson821ed7d2016-09-09 14:11:53 +01003309{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00003310 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05303311 enum intel_engine_id id;
Eric Anholt673a3942008-07-30 12:06:12 -07003312
Chris Wilsond9b13c42018-03-15 13:14:50 +00003313 GEM_TRACE("start\n");
3314
Chris Wilson7f961d72018-04-26 11:32:19 +01003315 if (GEM_SHOW_DEBUG()) {
Chris Wilson559e0402018-02-05 09:21:59 +00003316 struct drm_printer p = drm_debug_printer(__func__);
3317
3318 for_each_engine(engine, i915, id)
3319 intel_engine_dump(engine, &p, "%s\n", engine->name);
3320 }
3321
Chris Wilson0d73e7a2018-02-07 15:13:50 +00003322 set_bit(I915_WEDGED, &i915->gpu_error.flags);
3323 smp_mb__after_atomic();
3324
Daniel Vetteraf7a8ff2017-10-11 11:10:19 +02003325 /*
3326 * First, stop submission to hw, but do not yet complete requests by
3327 * rolling the global seqno forward (since this would complete requests
3328 * for which we haven't set the fence error to EIO yet).
3329 */
Chris Wilson963ddd62018-03-02 11:33:24 +00003330 for_each_engine(engine, i915, id) {
3331 i915_gem_reset_prepare_engine(engine);
Chris Wilson47650db2018-03-07 13:42:25 +00003332
Daniel Vetteraf7a8ff2017-10-11 11:10:19 +02003333 engine->submit_request = nop_submit_request;
Chris Wilson47650db2018-03-07 13:42:25 +00003334 engine->schedule = NULL;
Chris Wilson963ddd62018-03-02 11:33:24 +00003335 }
Chris Wilson47650db2018-03-07 13:42:25 +00003336 i915->caps.scheduler = 0;
Daniel Vetteraf7a8ff2017-10-11 11:10:19 +02003337
Chris Wilsonac697ae2018-03-15 15:10:15 +00003338 /* Even if the GPU reset fails, it should still stop the engines */
3339 intel_gpu_reset(i915, ALL_ENGINES);
3340
Daniel Vetteraf7a8ff2017-10-11 11:10:19 +02003341 /*
3342 * Make sure no one is running the old callback before we proceed with
3343 * cancelling requests and resetting the completion tracking. Otherwise
3344 * we might submit a request to the hardware which never completes.
3345 */
3346 synchronize_rcu();
3347
3348 for_each_engine(engine, i915, id) {
3349 /* Mark all executing requests as skipped */
3350 engine->cancel_requests(engine);
3351
3352 /*
3353 * Only once we've force-cancelled all in-flight requests can we
3354 * start to complete all requests.
3355 */
3356 engine->submit_request = nop_complete_submit_request;
3357 }
3358
3359 /*
3360 * Make sure no request can slip through without getting completed by
3361 * either this call here to intel_engine_init_global_seqno, or the one
3362 * in nop_complete_submit_request.
3363 */
3364 synchronize_rcu();
3365
3366 for_each_engine(engine, i915, id) {
3367 unsigned long flags;
3368
Chris Wilson0d73e7a2018-02-07 15:13:50 +00003369 /*
3370 * Mark all pending requests as complete so that any concurrent
Daniel Vetteraf7a8ff2017-10-11 11:10:19 +02003371 * (lockless) lookup doesn't try and wait upon the request as we
3372 * reset it.
3373 */
3374 spin_lock_irqsave(&engine->timeline->lock, flags);
3375 intel_engine_init_global_seqno(engine,
3376 intel_engine_last_submit(engine));
3377 spin_unlock_irqrestore(&engine->timeline->lock, flags);
Chris Wilson963ddd62018-03-02 11:33:24 +00003378
3379 i915_gem_reset_finish_engine(engine);
Daniel Vetteraf7a8ff2017-10-11 11:10:19 +02003380 }
Chris Wilson20e49332016-11-22 14:41:21 +00003381
Chris Wilsond9b13c42018-03-15 13:14:50 +00003382 GEM_TRACE("end\n");
3383
Chris Wilson3d7adbb2017-07-21 13:32:27 +01003384 wake_up_all(&i915->gpu_error.reset_queue);
Eric Anholt673a3942008-07-30 12:06:12 -07003385}
3386
Chris Wilson2e8f9d32017-03-16 17:13:04 +00003387bool i915_gem_unset_wedged(struct drm_i915_private *i915)
3388{
3389 struct i915_gem_timeline *tl;
3390 int i;
3391
3392 lockdep_assert_held(&i915->drm.struct_mutex);
3393 if (!test_bit(I915_WEDGED, &i915->gpu_error.flags))
3394 return true;
3395
Chris Wilsond9b13c42018-03-15 13:14:50 +00003396 GEM_TRACE("start\n");
3397
Chris Wilson2d4ecac2018-03-07 13:42:21 +00003398 /*
3399 * Before unwedging, make sure that all pending operations
Chris Wilson2e8f9d32017-03-16 17:13:04 +00003400 * are flushed and errored out - we may have requests waiting upon
3401 * third party fences. We marked all inflight requests as EIO, and
3402 * every execbuf since returned EIO, for consistency we want all
3403 * the currently pending requests to also be marked as EIO, which
3404 * is done inside our nop_submit_request - and so we must wait.
3405 *
3406 * No more can be submitted until we reset the wedged bit.
3407 */
3408 list_for_each_entry(tl, &i915->gt.timelines, link) {
3409 for (i = 0; i < ARRAY_SIZE(tl->engine); i++) {
Chris Wilsone61e0f52018-02-21 09:56:36 +00003410 struct i915_request *rq;
Chris Wilson2e8f9d32017-03-16 17:13:04 +00003411
3412 rq = i915_gem_active_peek(&tl->engine[i].last_request,
3413 &i915->drm.struct_mutex);
3414 if (!rq)
3415 continue;
3416
Chris Wilson2d4ecac2018-03-07 13:42:21 +00003417 /*
3418 * We can't use our normal waiter as we want to
Chris Wilson2e8f9d32017-03-16 17:13:04 +00003419 * avoid recursively trying to handle the current
3420 * reset. The basic dma_fence_default_wait() installs
3421 * a callback for dma_fence_signal(), which is
3422 * triggered by our nop handler (indirectly, the
3423 * callback enables the signaler thread which is
3424 * woken by the nop_submit_request() advancing the seqno
3425 * and when the seqno passes the fence, the signaler
3426 * then signals the fence waking us up).
3427 */
3428 if (dma_fence_default_wait(&rq->fence, true,
3429 MAX_SCHEDULE_TIMEOUT) < 0)
3430 return false;
3431 }
3432 }
Chris Wilson2d4ecac2018-03-07 13:42:21 +00003433 i915_retire_requests(i915);
3434 GEM_BUG_ON(i915->gt.active_requests);
Chris Wilson2e8f9d32017-03-16 17:13:04 +00003435
Chris Wilson2d4ecac2018-03-07 13:42:21 +00003436 /*
3437 * Undo nop_submit_request. We prevent all new i915 requests from
Chris Wilson2e8f9d32017-03-16 17:13:04 +00003438 * being queued (by disallowing execbuf whilst wedged) so having
3439 * waited for all active requests above, we know the system is idle
3440 * and do not have to worry about a thread being inside
3441 * engine->submit_request() as we swap over. So unlike installing
3442 * the nop_submit_request on reset, we can do this from normal
3443 * context and do not require stop_machine().
3444 */
3445 intel_engines_reset_default_submission(i915);
Chris Wilson36703e72017-06-22 11:56:25 +01003446 i915_gem_contexts_lost(i915);
Chris Wilson2e8f9d32017-03-16 17:13:04 +00003447
Chris Wilsond9b13c42018-03-15 13:14:50 +00003448 GEM_TRACE("end\n");
3449
Chris Wilson2e8f9d32017-03-16 17:13:04 +00003450 smp_mb__before_atomic(); /* complete takeover before enabling execbuf */
3451 clear_bit(I915_WEDGED, &i915->gpu_error.flags);
3452
3453 return true;
3454}
3455
Daniel Vetter75ef9da2010-08-21 00:25:16 +02003456static void
Eric Anholt673a3942008-07-30 12:06:12 -07003457i915_gem_retire_work_handler(struct work_struct *work)
3458{
Chris Wilsonb29c19b2013-09-25 17:34:56 +01003459 struct drm_i915_private *dev_priv =
Chris Wilson67d97da2016-07-04 08:08:31 +01003460 container_of(work, typeof(*dev_priv), gt.retire_work.work);
Chris Wilson91c8a322016-07-05 10:40:23 +01003461 struct drm_device *dev = &dev_priv->drm;
Eric Anholt673a3942008-07-30 12:06:12 -07003462
Chris Wilson891b48c2010-09-29 12:26:37 +01003463 /* Come back later if the device is busy... */
Chris Wilsonb29c19b2013-09-25 17:34:56 +01003464 if (mutex_trylock(&dev->struct_mutex)) {
Chris Wilsone61e0f52018-02-21 09:56:36 +00003465 i915_retire_requests(dev_priv);
Chris Wilsonb29c19b2013-09-25 17:34:56 +01003466 mutex_unlock(&dev->struct_mutex);
3467 }
Chris Wilson67d97da2016-07-04 08:08:31 +01003468
Chris Wilson88923042018-01-29 14:41:04 +00003469 /*
3470 * Keep the retire handler running until we are finally idle.
Chris Wilson67d97da2016-07-04 08:08:31 +01003471 * We do not need to do this test under locking as in the worst-case
3472 * we queue the retire worker once too often.
3473 */
Chris Wilson88923042018-01-29 14:41:04 +00003474 if (READ_ONCE(dev_priv->gt.awake))
Chris Wilson67d97da2016-07-04 08:08:31 +01003475 queue_delayed_work(dev_priv->wq,
3476 &dev_priv->gt.retire_work,
Chris Wilsonbcb45082012-10-05 17:02:57 +01003477 round_jiffies_up_relative(HZ));
Chris Wilsonb29c19b2013-09-25 17:34:56 +01003478}
Chris Wilson891b48c2010-09-29 12:26:37 +01003479
Chris Wilson84a10742018-01-24 11:36:08 +00003480static void shrink_caches(struct drm_i915_private *i915)
3481{
3482 /*
3483 * kmem_cache_shrink() discards empty slabs and reorders partially
3484 * filled slabs to prioritise allocating from the mostly full slabs,
3485 * with the aim of reducing fragmentation.
3486 */
3487 kmem_cache_shrink(i915->priorities);
3488 kmem_cache_shrink(i915->dependencies);
3489 kmem_cache_shrink(i915->requests);
3490 kmem_cache_shrink(i915->luts);
3491 kmem_cache_shrink(i915->vmas);
3492 kmem_cache_shrink(i915->objects);
3493}
3494
3495struct sleep_rcu_work {
3496 union {
3497 struct rcu_head rcu;
3498 struct work_struct work;
3499 };
3500 struct drm_i915_private *i915;
3501 unsigned int epoch;
3502};
3503
3504static inline bool
3505same_epoch(struct drm_i915_private *i915, unsigned int epoch)
3506{
3507 /*
3508 * There is a small chance that the epoch wrapped since we started
3509 * sleeping. If we assume that epoch is at least a u32, then it will
3510 * take at least 2^32 * 100ms for it to wrap, or about 326 years.
3511 */
3512 return epoch == READ_ONCE(i915->gt.epoch);
3513}
3514
3515static void __sleep_work(struct work_struct *work)
3516{
3517 struct sleep_rcu_work *s = container_of(work, typeof(*s), work);
3518 struct drm_i915_private *i915 = s->i915;
3519 unsigned int epoch = s->epoch;
3520
3521 kfree(s);
3522 if (same_epoch(i915, epoch))
3523 shrink_caches(i915);
3524}
3525
3526static void __sleep_rcu(struct rcu_head *rcu)
3527{
3528 struct sleep_rcu_work *s = container_of(rcu, typeof(*s), rcu);
3529 struct drm_i915_private *i915 = s->i915;
3530
3531 if (same_epoch(i915, s->epoch)) {
3532 INIT_WORK(&s->work, __sleep_work);
3533 queue_work(i915->wq, &s->work);
3534 } else {
3535 kfree(s);
3536 }
3537}
3538
Chris Wilson5427f202017-10-23 22:32:34 +01003539static inline bool
3540new_requests_since_last_retire(const struct drm_i915_private *i915)
3541{
3542 return (READ_ONCE(i915->gt.active_requests) ||
3543 work_pending(&i915->gt.idle_work.work));
3544}
3545
Chris Wilsonb29c19b2013-09-25 17:34:56 +01003546static void
3547i915_gem_idle_work_handler(struct work_struct *work)
3548{
3549 struct drm_i915_private *dev_priv =
Chris Wilson67d97da2016-07-04 08:08:31 +01003550 container_of(work, typeof(*dev_priv), gt.idle_work.work);
Chris Wilson84a10742018-01-24 11:36:08 +00003551 unsigned int epoch = I915_EPOCH_INVALID;
Chris Wilson67d97da2016-07-04 08:08:31 +01003552 bool rearm_hangcheck;
3553
3554 if (!READ_ONCE(dev_priv->gt.awake))
3555 return;
3556
Imre Deak0cb56702016-11-07 11:20:04 +02003557 /*
3558 * Wait for last execlists context complete, but bail out in case a
Chris Wilsonffed7bd2018-03-01 10:33:38 +00003559 * new request is submitted. As we don't trust the hardware, we
3560 * continue on if the wait times out. This is necessary to allow
3561 * the machine to suspend even if the hardware dies, and we will
3562 * try to recover in resume (after depriving the hardware of power,
3563 * it may be in a better mmod).
Imre Deak0cb56702016-11-07 11:20:04 +02003564 */
Chris Wilsonffed7bd2018-03-01 10:33:38 +00003565 __wait_for(if (new_requests_since_last_retire(dev_priv)) return,
3566 intel_engines_are_idle(dev_priv),
3567 I915_IDLE_ENGINES_TIMEOUT * 1000,
3568 10, 500);
Chris Wilson67d97da2016-07-04 08:08:31 +01003569
3570 rearm_hangcheck =
3571 cancel_delayed_work_sync(&dev_priv->gpu_error.hangcheck_work);
3572
Chris Wilson5427f202017-10-23 22:32:34 +01003573 if (!mutex_trylock(&dev_priv->drm.struct_mutex)) {
Chris Wilson67d97da2016-07-04 08:08:31 +01003574 /* Currently busy, come back later */
3575 mod_delayed_work(dev_priv->wq,
3576 &dev_priv->gt.idle_work,
3577 msecs_to_jiffies(50));
3578 goto out_rearm;
3579 }
3580
Imre Deak93c97dc2016-11-07 11:20:03 +02003581 /*
3582 * New request retired after this work handler started, extend active
3583 * period until next instance of the work.
3584 */
Chris Wilson5427f202017-10-23 22:32:34 +01003585 if (new_requests_since_last_retire(dev_priv))
Imre Deak93c97dc2016-11-07 11:20:03 +02003586 goto out_unlock;
3587
Chris Wilsone4d20062018-04-06 16:51:44 +01003588 epoch = __i915_gem_park(dev_priv);
Chris Wilsonff320d62017-10-23 22:32:35 +01003589
Chris Wilson67d97da2016-07-04 08:08:31 +01003590 rearm_hangcheck = false;
Chris Wilson67d97da2016-07-04 08:08:31 +01003591out_unlock:
Chris Wilson5427f202017-10-23 22:32:34 +01003592 mutex_unlock(&dev_priv->drm.struct_mutex);
Chris Wilson35c94182015-04-07 16:20:37 +01003593
Chris Wilson67d97da2016-07-04 08:08:31 +01003594out_rearm:
3595 if (rearm_hangcheck) {
3596 GEM_BUG_ON(!dev_priv->gt.awake);
3597 i915_queue_hangcheck(dev_priv);
Chris Wilson35c94182015-04-07 16:20:37 +01003598 }
Chris Wilson84a10742018-01-24 11:36:08 +00003599
3600 /*
3601 * When we are idle, it is an opportune time to reap our caches.
3602 * However, we have many objects that utilise RCU and the ordered
3603 * i915->wq that this work is executing on. To try and flush any
3604 * pending frees now we are idle, we first wait for an RCU grace
3605 * period, and then queue a task (that will run last on the wq) to
3606 * shrink and re-optimize the caches.
3607 */
3608 if (same_epoch(dev_priv, epoch)) {
3609 struct sleep_rcu_work *s = kmalloc(sizeof(*s), GFP_KERNEL);
3610 if (s) {
3611 s->i915 = dev_priv;
3612 s->epoch = epoch;
3613 call_rcu(&s->rcu, __sleep_rcu);
3614 }
3615 }
Eric Anholt673a3942008-07-30 12:06:12 -07003616}
3617
Chris Wilsonb1f788c2016-08-04 07:52:45 +01003618void i915_gem_close_object(struct drm_gem_object *gem, struct drm_file *file)
3619{
Chris Wilsond1b48c12017-08-16 09:52:08 +01003620 struct drm_i915_private *i915 = to_i915(gem->dev);
Chris Wilsonb1f788c2016-08-04 07:52:45 +01003621 struct drm_i915_gem_object *obj = to_intel_bo(gem);
3622 struct drm_i915_file_private *fpriv = file->driver_priv;
Chris Wilsond1b48c12017-08-16 09:52:08 +01003623 struct i915_lut_handle *lut, *ln;
Chris Wilsonb1f788c2016-08-04 07:52:45 +01003624
Chris Wilsond1b48c12017-08-16 09:52:08 +01003625 mutex_lock(&i915->drm.struct_mutex);
3626
3627 list_for_each_entry_safe(lut, ln, &obj->lut_list, obj_link) {
3628 struct i915_gem_context *ctx = lut->ctx;
3629 struct i915_vma *vma;
3630
Chris Wilson432295d2017-08-22 12:05:15 +01003631 GEM_BUG_ON(ctx->file_priv == ERR_PTR(-EBADF));
Chris Wilsond1b48c12017-08-16 09:52:08 +01003632 if (ctx->file_priv != fpriv)
3633 continue;
3634
3635 vma = radix_tree_delete(&ctx->handles_vma, lut->handle);
Chris Wilson3ffff012017-08-22 12:05:17 +01003636 GEM_BUG_ON(vma->obj != obj);
3637
3638 /* We allow the process to have multiple handles to the same
3639 * vma, in the same fd namespace, by virtue of flink/open.
3640 */
3641 GEM_BUG_ON(!vma->open_count);
3642 if (!--vma->open_count && !i915_vma_is_ggtt(vma))
Chris Wilsonb1f788c2016-08-04 07:52:45 +01003643 i915_vma_close(vma);
Chris Wilsonf8a7fde2016-10-28 13:58:29 +01003644
Chris Wilsond1b48c12017-08-16 09:52:08 +01003645 list_del(&lut->obj_link);
3646 list_del(&lut->ctx_link);
Chris Wilson4ff4b442017-06-16 15:05:16 +01003647
Chris Wilsond1b48c12017-08-16 09:52:08 +01003648 kmem_cache_free(i915->luts, lut);
3649 __i915_gem_object_release_unless_active(obj);
Chris Wilsonf8a7fde2016-10-28 13:58:29 +01003650 }
Chris Wilsond1b48c12017-08-16 09:52:08 +01003651
3652 mutex_unlock(&i915->drm.struct_mutex);
Chris Wilsonb1f788c2016-08-04 07:52:45 +01003653}
3654
Chris Wilsone95433c2016-10-28 13:58:27 +01003655static unsigned long to_wait_timeout(s64 timeout_ns)
3656{
3657 if (timeout_ns < 0)
3658 return MAX_SCHEDULE_TIMEOUT;
3659
3660 if (timeout_ns == 0)
3661 return 0;
3662
3663 return nsecs_to_jiffies_timeout(timeout_ns);
3664}
3665
Ben Widawsky5816d642012-04-11 11:18:19 -07003666/**
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07003667 * i915_gem_wait_ioctl - implements DRM_IOCTL_I915_GEM_WAIT
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01003668 * @dev: drm device pointer
3669 * @data: ioctl data blob
3670 * @file: drm file pointer
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07003671 *
3672 * Returns 0 if successful, else an error is returned with the remaining time in
3673 * the timeout parameter.
3674 * -ETIME: object is still busy after timeout
3675 * -ERESTARTSYS: signal interrupted the wait
3676 * -ENONENT: object doesn't exist
3677 * Also possible, but rare:
Chris Wilsonb8050142017-08-11 11:57:31 +01003678 * -EAGAIN: incomplete, restart syscall
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07003679 * -ENOMEM: damn
3680 * -ENODEV: Internal IRQ fail
3681 * -E?: The add request failed
3682 *
3683 * The wait ioctl with a timeout of 0 reimplements the busy ioctl. With any
3684 * non-zero timeout parameter the wait ioctl will wait for the given number of
3685 * nanoseconds on an object becoming unbusy. Since the wait itself does so
3686 * without holding struct_mutex the object may become re-busied before this
3687 * function completes. A similar but shorter * race condition exists in the busy
3688 * ioctl
3689 */
3690int
3691i915_gem_wait_ioctl(struct drm_device *dev, void *data, struct drm_file *file)
3692{
3693 struct drm_i915_gem_wait *args = data;
3694 struct drm_i915_gem_object *obj;
Chris Wilsone95433c2016-10-28 13:58:27 +01003695 ktime_t start;
3696 long ret;
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07003697
Daniel Vetter11b5d512014-09-29 15:31:26 +02003698 if (args->flags != 0)
3699 return -EINVAL;
3700
Chris Wilson03ac0642016-07-20 13:31:51 +01003701 obj = i915_gem_object_lookup(file, args->bo_handle);
Chris Wilson033d5492016-08-05 10:14:17 +01003702 if (!obj)
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07003703 return -ENOENT;
Chris Wilson033d5492016-08-05 10:14:17 +01003704
Chris Wilsone95433c2016-10-28 13:58:27 +01003705 start = ktime_get();
3706
3707 ret = i915_gem_object_wait(obj,
3708 I915_WAIT_INTERRUPTIBLE | I915_WAIT_ALL,
3709 to_wait_timeout(args->timeout_ns),
3710 to_rps_client(file));
3711
3712 if (args->timeout_ns > 0) {
3713 args->timeout_ns -= ktime_to_ns(ktime_sub(ktime_get(), start));
3714 if (args->timeout_ns < 0)
3715 args->timeout_ns = 0;
Chris Wilsonc1d20612017-02-16 12:54:41 +00003716
3717 /*
3718 * Apparently ktime isn't accurate enough and occasionally has a
3719 * bit of mismatch in the jiffies<->nsecs<->ktime loop. So patch
3720 * things up to make the test happy. We allow up to 1 jiffy.
3721 *
3722 * This is a regression from the timespec->ktime conversion.
3723 */
3724 if (ret == -ETIME && !nsecs_to_jiffies(args->timeout_ns))
3725 args->timeout_ns = 0;
Chris Wilsonb8050142017-08-11 11:57:31 +01003726
3727 /* Asked to wait beyond the jiffie/scheduler precision? */
3728 if (ret == -ETIME && args->timeout_ns)
3729 ret = -EAGAIN;
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07003730 }
3731
Chris Wilsonf0cd5182016-10-28 13:58:43 +01003732 i915_gem_object_put(obj);
John Harrisonff865882014-11-24 18:49:28 +00003733 return ret;
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07003734}
3735
Chris Wilson73cb9702016-10-28 13:58:46 +01003736static int wait_for_timeline(struct i915_gem_timeline *tl, unsigned int flags)
Daniel Vetter4df2faf2010-02-19 11:52:00 +01003737{
Chris Wilson73cb9702016-10-28 13:58:46 +01003738 int ret, i;
3739
3740 for (i = 0; i < ARRAY_SIZE(tl->engine); i++) {
3741 ret = i915_gem_active_wait(&tl->engine[i].last_request, flags);
3742 if (ret)
3743 return ret;
3744 }
3745
3746 return 0;
3747}
3748
Chris Wilson25112b62017-03-30 15:50:39 +01003749static int wait_for_engines(struct drm_i915_private *i915)
3750{
Chris Wilsonee42c002017-12-11 19:41:34 +00003751 if (wait_for(intel_engines_are_idle(i915), I915_IDLE_ENGINES_TIMEOUT)) {
Chris Wilson59e4b192017-12-11 19:41:35 +00003752 dev_err(i915->drm.dev,
3753 "Failed to idle engines, declaring wedged!\n");
Chris Wilson629820f2018-03-09 10:11:14 +00003754 GEM_TRACE_DUMP();
Chris Wilsoncad99462017-08-26 12:09:33 +01003755 i915_gem_set_wedged(i915);
3756 return -EIO;
Chris Wilson25112b62017-03-30 15:50:39 +01003757 }
3758
3759 return 0;
3760}
3761
Chris Wilson73cb9702016-10-28 13:58:46 +01003762int i915_gem_wait_for_idle(struct drm_i915_private *i915, unsigned int flags)
3763{
Dave Gordonb4ac5af2016-03-24 11:20:38 +00003764 int ret;
Daniel Vetter4df2faf2010-02-19 11:52:00 +01003765
Chris Wilson863e9fd2017-05-30 13:13:32 +01003766 /* If the device is asleep, we have no requests outstanding */
3767 if (!READ_ONCE(i915->gt.awake))
3768 return 0;
3769
Chris Wilson9caa34a2016-11-11 14:58:08 +00003770 if (flags & I915_WAIT_LOCKED) {
3771 struct i915_gem_timeline *tl;
3772
3773 lockdep_assert_held(&i915->drm.struct_mutex);
3774
3775 list_for_each_entry(tl, &i915->gt.timelines, link) {
3776 ret = wait_for_timeline(tl, flags);
3777 if (ret)
3778 return ret;
3779 }
Chris Wilsone61e0f52018-02-21 09:56:36 +00003780 i915_retire_requests(i915);
Chris Wilson25112b62017-03-30 15:50:39 +01003781
3782 ret = wait_for_engines(i915);
Chris Wilson9caa34a2016-11-11 14:58:08 +00003783 } else {
3784 ret = wait_for_timeline(&i915->gt.global_timeline, flags);
Chris Wilson1ec14ad2010-12-04 11:30:53 +00003785 }
Zou Nan haid1b851f2010-05-21 09:08:57 +08003786
Chris Wilson25112b62017-03-30 15:50:39 +01003787 return ret;
Daniel Vetter4df2faf2010-02-19 11:52:00 +01003788}
3789
Chris Wilson5a97bcc2017-02-22 11:40:46 +00003790static void __i915_gem_object_flush_for_display(struct drm_i915_gem_object *obj)
3791{
Chris Wilsone27ab732017-06-15 13:38:49 +01003792 /*
3793 * We manually flush the CPU domain so that we can override and
3794 * force the flush for the display, and perform it asyncrhonously.
3795 */
3796 flush_write_domain(obj, ~I915_GEM_DOMAIN_CPU);
3797 if (obj->cache_dirty)
3798 i915_gem_clflush_object(obj, I915_CLFLUSH_FORCE);
Christian Königc0a51fd2018-02-16 13:43:38 +01003799 obj->write_domain = 0;
Chris Wilson5a97bcc2017-02-22 11:40:46 +00003800}
3801
3802void i915_gem_object_flush_if_display(struct drm_i915_gem_object *obj)
3803{
Chris Wilsonbd3d2252017-10-13 21:26:14 +01003804 if (!READ_ONCE(obj->pin_global))
Chris Wilson5a97bcc2017-02-22 11:40:46 +00003805 return;
3806
3807 mutex_lock(&obj->base.dev->struct_mutex);
3808 __i915_gem_object_flush_for_display(obj);
3809 mutex_unlock(&obj->base.dev->struct_mutex);
3810}
3811
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003812/**
Chris Wilsone22d8e32017-04-12 12:01:11 +01003813 * Moves a single object to the WC read, and possibly write domain.
3814 * @obj: object to act on
3815 * @write: ask for write access or read only
3816 *
3817 * This function returns when the move is complete, including waiting on
3818 * flushes to occur.
3819 */
3820int
3821i915_gem_object_set_to_wc_domain(struct drm_i915_gem_object *obj, bool write)
3822{
3823 int ret;
3824
3825 lockdep_assert_held(&obj->base.dev->struct_mutex);
3826
3827 ret = i915_gem_object_wait(obj,
3828 I915_WAIT_INTERRUPTIBLE |
3829 I915_WAIT_LOCKED |
3830 (write ? I915_WAIT_ALL : 0),
3831 MAX_SCHEDULE_TIMEOUT,
3832 NULL);
3833 if (ret)
3834 return ret;
3835
Christian Königc0a51fd2018-02-16 13:43:38 +01003836 if (obj->write_domain == I915_GEM_DOMAIN_WC)
Chris Wilsone22d8e32017-04-12 12:01:11 +01003837 return 0;
3838
3839 /* Flush and acquire obj->pages so that we are coherent through
3840 * direct access in memory with previous cached writes through
3841 * shmemfs and that our cache domain tracking remains valid.
3842 * For example, if the obj->filp was moved to swap without us
3843 * being notified and releasing the pages, we would mistakenly
3844 * continue to assume that the obj remained out of the CPU cached
3845 * domain.
3846 */
3847 ret = i915_gem_object_pin_pages(obj);
3848 if (ret)
3849 return ret;
3850
3851 flush_write_domain(obj, ~I915_GEM_DOMAIN_WC);
3852
3853 /* Serialise direct access to this object with the barriers for
3854 * coherent writes from the GPU, by effectively invalidating the
3855 * WC domain upon first access.
3856 */
Christian Königc0a51fd2018-02-16 13:43:38 +01003857 if ((obj->read_domains & I915_GEM_DOMAIN_WC) == 0)
Chris Wilsone22d8e32017-04-12 12:01:11 +01003858 mb();
3859
3860 /* It should now be out of any other write domains, and we can update
3861 * the domain values for our changes.
3862 */
Christian Königc0a51fd2018-02-16 13:43:38 +01003863 GEM_BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_WC) != 0);
3864 obj->read_domains |= I915_GEM_DOMAIN_WC;
Chris Wilsone22d8e32017-04-12 12:01:11 +01003865 if (write) {
Christian Königc0a51fd2018-02-16 13:43:38 +01003866 obj->read_domains = I915_GEM_DOMAIN_WC;
3867 obj->write_domain = I915_GEM_DOMAIN_WC;
Chris Wilsone22d8e32017-04-12 12:01:11 +01003868 obj->mm.dirty = true;
3869 }
3870
3871 i915_gem_object_unpin_pages(obj);
3872 return 0;
3873}
3874
3875/**
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003876 * Moves a single object to the GTT read, and possibly write domain.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01003877 * @obj: object to act on
3878 * @write: ask for write access or read only
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003879 *
3880 * This function returns when the move is complete, including waiting on
3881 * flushes to occur.
3882 */
Jesse Barnes79e53942008-11-07 14:24:08 -08003883int
Chris Wilson20217462010-11-23 15:26:33 +00003884i915_gem_object_set_to_gtt_domain(struct drm_i915_gem_object *obj, bool write)
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003885{
Eric Anholte47c68e2008-11-14 13:35:19 -08003886 int ret;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003887
Chris Wilsone95433c2016-10-28 13:58:27 +01003888 lockdep_assert_held(&obj->base.dev->struct_mutex);
Chris Wilson4c7d62c2016-10-28 13:58:32 +01003889
Chris Wilsone95433c2016-10-28 13:58:27 +01003890 ret = i915_gem_object_wait(obj,
3891 I915_WAIT_INTERRUPTIBLE |
3892 I915_WAIT_LOCKED |
3893 (write ? I915_WAIT_ALL : 0),
3894 MAX_SCHEDULE_TIMEOUT,
3895 NULL);
Chris Wilson88241782011-01-07 17:09:48 +00003896 if (ret)
3897 return ret;
3898
Christian Königc0a51fd2018-02-16 13:43:38 +01003899 if (obj->write_domain == I915_GEM_DOMAIN_GTT)
Chris Wilsonc13d87e2016-07-20 09:21:15 +01003900 return 0;
3901
Chris Wilson43566de2015-01-02 16:29:29 +05303902 /* Flush and acquire obj->pages so that we are coherent through
3903 * direct access in memory with previous cached writes through
3904 * shmemfs and that our cache domain tracking remains valid.
3905 * For example, if the obj->filp was moved to swap without us
3906 * being notified and releasing the pages, we would mistakenly
3907 * continue to assume that the obj remained out of the CPU cached
3908 * domain.
3909 */
Chris Wilsona4f5ea62016-10-28 13:58:35 +01003910 ret = i915_gem_object_pin_pages(obj);
Chris Wilson43566de2015-01-02 16:29:29 +05303911 if (ret)
3912 return ret;
3913
Chris Wilsonef749212017-04-12 12:01:10 +01003914 flush_write_domain(obj, ~I915_GEM_DOMAIN_GTT);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003915
Chris Wilsond0a57782012-10-09 19:24:37 +01003916 /* Serialise direct access to this object with the barriers for
3917 * coherent writes from the GPU, by effectively invalidating the
3918 * GTT domain upon first access.
3919 */
Christian Königc0a51fd2018-02-16 13:43:38 +01003920 if ((obj->read_domains & I915_GEM_DOMAIN_GTT) == 0)
Chris Wilsond0a57782012-10-09 19:24:37 +01003921 mb();
3922
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003923 /* It should now be out of any other write domains, and we can update
3924 * the domain values for our changes.
3925 */
Christian Königc0a51fd2018-02-16 13:43:38 +01003926 GEM_BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_GTT) != 0);
3927 obj->read_domains |= I915_GEM_DOMAIN_GTT;
Eric Anholte47c68e2008-11-14 13:35:19 -08003928 if (write) {
Christian Königc0a51fd2018-02-16 13:43:38 +01003929 obj->read_domains = I915_GEM_DOMAIN_GTT;
3930 obj->write_domain = I915_GEM_DOMAIN_GTT;
Chris Wilsona4f5ea62016-10-28 13:58:35 +01003931 obj->mm.dirty = true;
Eric Anholte47c68e2008-11-14 13:35:19 -08003932 }
3933
Chris Wilsona4f5ea62016-10-28 13:58:35 +01003934 i915_gem_object_unpin_pages(obj);
Eric Anholte47c68e2008-11-14 13:35:19 -08003935 return 0;
3936}
3937
Chris Wilsonef55f922015-10-09 14:11:27 +01003938/**
3939 * Changes the cache-level of an object across all VMA.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01003940 * @obj: object to act on
3941 * @cache_level: new cache level to set for the object
Chris Wilsonef55f922015-10-09 14:11:27 +01003942 *
3943 * After this function returns, the object will be in the new cache-level
3944 * across all GTT and the contents of the backing storage will be coherent,
3945 * with respect to the new cache-level. In order to keep the backing storage
3946 * coherent for all users, we only allow a single cache level to be set
3947 * globally on the object and prevent it from being changed whilst the
3948 * hardware is reading from the object. That is if the object is currently
3949 * on the scanout it will be set to uncached (or equivalent display
3950 * cache coherency) and all non-MOCS GPU access will also be uncached so
3951 * that all direct access to the scanout remains coherent.
3952 */
Chris Wilsone4ffd172011-04-04 09:44:39 +01003953int i915_gem_object_set_cache_level(struct drm_i915_gem_object *obj,
3954 enum i915_cache_level cache_level)
3955{
Chris Wilsonaa653a62016-08-04 07:52:27 +01003956 struct i915_vma *vma;
Chris Wilsona6a7cc42016-11-18 21:17:46 +00003957 int ret;
Chris Wilsone4ffd172011-04-04 09:44:39 +01003958
Chris Wilson4c7d62c2016-10-28 13:58:32 +01003959 lockdep_assert_held(&obj->base.dev->struct_mutex);
3960
Chris Wilsone4ffd172011-04-04 09:44:39 +01003961 if (obj->cache_level == cache_level)
Chris Wilsona6a7cc42016-11-18 21:17:46 +00003962 return 0;
Chris Wilsone4ffd172011-04-04 09:44:39 +01003963
Chris Wilsonef55f922015-10-09 14:11:27 +01003964 /* Inspect the list of currently bound VMA and unbind any that would
3965 * be invalid given the new cache-level. This is principally to
3966 * catch the issue of the CS prefetch crossing page boundaries and
3967 * reading an invalid PTE on older architectures.
3968 */
Chris Wilsonaa653a62016-08-04 07:52:27 +01003969restart:
3970 list_for_each_entry(vma, &obj->vma_list, obj_link) {
Chris Wilsonef55f922015-10-09 14:11:27 +01003971 if (!drm_mm_node_allocated(&vma->node))
3972 continue;
3973
Chris Wilson20dfbde2016-08-04 16:32:30 +01003974 if (i915_vma_is_pinned(vma)) {
Chris Wilsonef55f922015-10-09 14:11:27 +01003975 DRM_DEBUG("can not change the cache level of pinned objects\n");
3976 return -EBUSY;
3977 }
3978
Chris Wilson010e3e62017-12-06 12:49:13 +00003979 if (!i915_vma_is_closed(vma) &&
3980 i915_gem_valid_gtt_space(vma, cache_level))
Chris Wilsonaa653a62016-08-04 07:52:27 +01003981 continue;
3982
3983 ret = i915_vma_unbind(vma);
3984 if (ret)
3985 return ret;
3986
3987 /* As unbinding may affect other elements in the
3988 * obj->vma_list (due to side-effects from retiring
3989 * an active vma), play safe and restart the iterator.
3990 */
3991 goto restart;
Chris Wilson42d6ab42012-07-26 11:49:32 +01003992 }
3993
Chris Wilsonef55f922015-10-09 14:11:27 +01003994 /* We can reuse the existing drm_mm nodes but need to change the
3995 * cache-level on the PTE. We could simply unbind them all and
3996 * rebind with the correct cache-level on next use. However since
3997 * we already have a valid slot, dma mapping, pages etc, we may as
3998 * rewrite the PTE in the belief that doing so tramples upon less
3999 * state and so involves less work.
4000 */
Chris Wilson15717de2016-08-04 07:52:26 +01004001 if (obj->bind_count) {
Chris Wilsonef55f922015-10-09 14:11:27 +01004002 /* Before we change the PTE, the GPU must not be accessing it.
4003 * If we wait upon the object, we know that all the bound
4004 * VMA are no longer active.
4005 */
Chris Wilsone95433c2016-10-28 13:58:27 +01004006 ret = i915_gem_object_wait(obj,
4007 I915_WAIT_INTERRUPTIBLE |
4008 I915_WAIT_LOCKED |
4009 I915_WAIT_ALL,
4010 MAX_SCHEDULE_TIMEOUT,
4011 NULL);
Chris Wilsone4ffd172011-04-04 09:44:39 +01004012 if (ret)
4013 return ret;
4014
Tvrtko Ursulin0031fb92016-11-04 14:42:44 +00004015 if (!HAS_LLC(to_i915(obj->base.dev)) &&
4016 cache_level != I915_CACHE_NONE) {
Chris Wilsonef55f922015-10-09 14:11:27 +01004017 /* Access to snoopable pages through the GTT is
4018 * incoherent and on some machines causes a hard
4019 * lockup. Relinquish the CPU mmaping to force
4020 * userspace to refault in the pages and we can
4021 * then double check if the GTT mapping is still
4022 * valid for that pointer access.
4023 */
4024 i915_gem_release_mmap(obj);
Chris Wilsone4ffd172011-04-04 09:44:39 +01004025
Chris Wilsonef55f922015-10-09 14:11:27 +01004026 /* As we no longer need a fence for GTT access,
4027 * we can relinquish it now (and so prevent having
4028 * to steal a fence from someone else on the next
4029 * fence request). Note GPU activity would have
4030 * dropped the fence as all snoopable access is
4031 * supposed to be linear.
4032 */
Chris Wilsone2189dd2017-12-07 21:14:07 +00004033 for_each_ggtt_vma(vma, obj) {
Chris Wilson49ef5292016-08-18 17:17:00 +01004034 ret = i915_vma_put_fence(vma);
4035 if (ret)
4036 return ret;
4037 }
Chris Wilsonef55f922015-10-09 14:11:27 +01004038 } else {
4039 /* We either have incoherent backing store and
4040 * so no GTT access or the architecture is fully
4041 * coherent. In such cases, existing GTT mmaps
4042 * ignore the cache bit in the PTE and we can
4043 * rewrite it without confusing the GPU or having
4044 * to force userspace to fault back in its mmaps.
4045 */
Chris Wilsone4ffd172011-04-04 09:44:39 +01004046 }
4047
Chris Wilson1c7f4bc2016-02-26 11:03:19 +00004048 list_for_each_entry(vma, &obj->vma_list, obj_link) {
Chris Wilsonef55f922015-10-09 14:11:27 +01004049 if (!drm_mm_node_allocated(&vma->node))
4050 continue;
4051
4052 ret = i915_vma_bind(vma, cache_level, PIN_UPDATE);
4053 if (ret)
4054 return ret;
4055 }
Chris Wilsone4ffd172011-04-04 09:44:39 +01004056 }
4057
Chris Wilson1c7f4bc2016-02-26 11:03:19 +00004058 list_for_each_entry(vma, &obj->vma_list, obj_link)
Chris Wilson2c225692013-08-09 12:26:45 +01004059 vma->node.color = cache_level;
Chris Wilsonb8f55be2017-08-11 12:11:16 +01004060 i915_gem_object_set_cache_coherency(obj, cache_level);
Chris Wilsone27ab732017-06-15 13:38:49 +01004061 obj->cache_dirty = true; /* Always invalidate stale cachelines */
Chris Wilson2c225692013-08-09 12:26:45 +01004062
Chris Wilsone4ffd172011-04-04 09:44:39 +01004063 return 0;
4064}
4065
Ben Widawsky199adf42012-09-21 17:01:20 -07004066int i915_gem_get_caching_ioctl(struct drm_device *dev, void *data,
4067 struct drm_file *file)
Chris Wilsone6994ae2012-07-10 10:27:08 +01004068{
Ben Widawsky199adf42012-09-21 17:01:20 -07004069 struct drm_i915_gem_caching *args = data;
Chris Wilsone6994ae2012-07-10 10:27:08 +01004070 struct drm_i915_gem_object *obj;
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004071 int err = 0;
Chris Wilsone6994ae2012-07-10 10:27:08 +01004072
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004073 rcu_read_lock();
4074 obj = i915_gem_object_lookup_rcu(file, args->handle);
4075 if (!obj) {
4076 err = -ENOENT;
4077 goto out;
4078 }
Chris Wilsone6994ae2012-07-10 10:27:08 +01004079
Chris Wilson651d7942013-08-08 14:41:10 +01004080 switch (obj->cache_level) {
4081 case I915_CACHE_LLC:
4082 case I915_CACHE_L3_LLC:
4083 args->caching = I915_CACHING_CACHED;
4084 break;
4085
Chris Wilson4257d3b2013-08-08 14:41:11 +01004086 case I915_CACHE_WT:
4087 args->caching = I915_CACHING_DISPLAY;
4088 break;
4089
Chris Wilson651d7942013-08-08 14:41:10 +01004090 default:
4091 args->caching = I915_CACHING_NONE;
4092 break;
4093 }
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004094out:
4095 rcu_read_unlock();
4096 return err;
Chris Wilsone6994ae2012-07-10 10:27:08 +01004097}
4098
Ben Widawsky199adf42012-09-21 17:01:20 -07004099int i915_gem_set_caching_ioctl(struct drm_device *dev, void *data,
4100 struct drm_file *file)
Chris Wilsone6994ae2012-07-10 10:27:08 +01004101{
Chris Wilson9c870d02016-10-24 13:42:15 +01004102 struct drm_i915_private *i915 = to_i915(dev);
Ben Widawsky199adf42012-09-21 17:01:20 -07004103 struct drm_i915_gem_caching *args = data;
Chris Wilsone6994ae2012-07-10 10:27:08 +01004104 struct drm_i915_gem_object *obj;
4105 enum i915_cache_level level;
Chris Wilsond65415d2017-01-19 08:22:10 +00004106 int ret = 0;
Chris Wilsone6994ae2012-07-10 10:27:08 +01004107
Ben Widawsky199adf42012-09-21 17:01:20 -07004108 switch (args->caching) {
4109 case I915_CACHING_NONE:
Chris Wilsone6994ae2012-07-10 10:27:08 +01004110 level = I915_CACHE_NONE;
4111 break;
Ben Widawsky199adf42012-09-21 17:01:20 -07004112 case I915_CACHING_CACHED:
Imre Deake5756c12015-08-14 18:43:30 +03004113 /*
4114 * Due to a HW issue on BXT A stepping, GPU stores via a
4115 * snooped mapping may leave stale data in a corresponding CPU
4116 * cacheline, whereas normally such cachelines would get
4117 * invalidated.
4118 */
Chris Wilson9c870d02016-10-24 13:42:15 +01004119 if (!HAS_LLC(i915) && !HAS_SNOOP(i915))
Imre Deake5756c12015-08-14 18:43:30 +03004120 return -ENODEV;
4121
Chris Wilsone6994ae2012-07-10 10:27:08 +01004122 level = I915_CACHE_LLC;
4123 break;
Chris Wilson4257d3b2013-08-08 14:41:11 +01004124 case I915_CACHING_DISPLAY:
Chris Wilson9c870d02016-10-24 13:42:15 +01004125 level = HAS_WT(i915) ? I915_CACHE_WT : I915_CACHE_NONE;
Chris Wilson4257d3b2013-08-08 14:41:11 +01004126 break;
Chris Wilsone6994ae2012-07-10 10:27:08 +01004127 default:
4128 return -EINVAL;
4129 }
4130
Chris Wilsond65415d2017-01-19 08:22:10 +00004131 obj = i915_gem_object_lookup(file, args->handle);
4132 if (!obj)
4133 return -ENOENT;
4134
Tina Zhanga03f3952017-11-14 10:25:13 +00004135 /*
4136 * The caching mode of proxy object is handled by its generator, and
4137 * not allowed to be changed by userspace.
4138 */
4139 if (i915_gem_object_is_proxy(obj)) {
4140 ret = -ENXIO;
4141 goto out;
4142 }
4143
Chris Wilsond65415d2017-01-19 08:22:10 +00004144 if (obj->cache_level == level)
4145 goto out;
4146
4147 ret = i915_gem_object_wait(obj,
4148 I915_WAIT_INTERRUPTIBLE,
4149 MAX_SCHEDULE_TIMEOUT,
4150 to_rps_client(file));
4151 if (ret)
4152 goto out;
4153
Ben Widawsky3bc29132012-09-26 16:15:20 -07004154 ret = i915_mutex_lock_interruptible(dev);
4155 if (ret)
Chris Wilsond65415d2017-01-19 08:22:10 +00004156 goto out;
Chris Wilsone6994ae2012-07-10 10:27:08 +01004157
4158 ret = i915_gem_object_set_cache_level(obj, level);
Chris Wilsone6994ae2012-07-10 10:27:08 +01004159 mutex_unlock(&dev->struct_mutex);
Chris Wilsond65415d2017-01-19 08:22:10 +00004160
4161out:
4162 i915_gem_object_put(obj);
Chris Wilsone6994ae2012-07-10 10:27:08 +01004163 return ret;
4164}
4165
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08004166/*
Dhinakaran Pandiyan07bcd992018-03-06 19:34:18 -08004167 * Prepare buffer for display plane (scanout, cursors, etc). Can be called from
4168 * an uninterruptible phase (modesetting) and allows any flushes to be pipelined
4169 * (for pageflips). We only flush the caches while preparing the buffer for
4170 * display, the callers are responsible for frontbuffer flush.
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08004171 */
Chris Wilson058d88c2016-08-15 10:49:06 +01004172struct i915_vma *
Chris Wilson2da3b9b2011-04-14 09:41:17 +01004173i915_gem_object_pin_to_display_plane(struct drm_i915_gem_object *obj,
4174 u32 alignment,
Chris Wilson59354852018-02-20 13:42:06 +00004175 const struct i915_ggtt_view *view,
4176 unsigned int flags)
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08004177{
Chris Wilson058d88c2016-08-15 10:49:06 +01004178 struct i915_vma *vma;
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08004179 int ret;
4180
Chris Wilson4c7d62c2016-10-28 13:58:32 +01004181 lockdep_assert_held(&obj->base.dev->struct_mutex);
4182
Chris Wilsonbd3d2252017-10-13 21:26:14 +01004183 /* Mark the global pin early so that we account for the
Chris Wilsoncc98b412013-08-09 12:25:09 +01004184 * display coherency whilst setting up the cache domains.
4185 */
Chris Wilsonbd3d2252017-10-13 21:26:14 +01004186 obj->pin_global++;
Chris Wilsoncc98b412013-08-09 12:25:09 +01004187
Eric Anholta7ef0642011-03-29 16:59:54 -07004188 /* The display engine is not coherent with the LLC cache on gen6. As
4189 * a result, we make sure that the pinning that is about to occur is
4190 * done with uncached PTEs. This is lowest common denominator for all
4191 * chipsets.
4192 *
4193 * However for gen6+, we could do better by using the GFDT bit instead
4194 * of uncaching, which would allow us to flush all the LLC-cached data
4195 * with that bit in the PTE to main memory with just one PIPE_CONTROL.
4196 */
Chris Wilson651d7942013-08-08 14:41:10 +01004197 ret = i915_gem_object_set_cache_level(obj,
Tvrtko Ursulin86527442016-10-13 11:03:00 +01004198 HAS_WT(to_i915(obj->base.dev)) ?
4199 I915_CACHE_WT : I915_CACHE_NONE);
Chris Wilson058d88c2016-08-15 10:49:06 +01004200 if (ret) {
4201 vma = ERR_PTR(ret);
Chris Wilsonbd3d2252017-10-13 21:26:14 +01004202 goto err_unpin_global;
Chris Wilson058d88c2016-08-15 10:49:06 +01004203 }
Eric Anholta7ef0642011-03-29 16:59:54 -07004204
Chris Wilson2da3b9b2011-04-14 09:41:17 +01004205 /* As the user may map the buffer once pinned in the display plane
4206 * (e.g. libkms for the bootup splash), we have to ensure that we
Chris Wilson2efb8132016-08-18 17:17:06 +01004207 * always use map_and_fenceable for all scanout buffers. However,
4208 * it may simply be too big to fit into mappable, in which case
4209 * put it anyway and hope that userspace can cope (but always first
4210 * try to preserve the existing ABI).
Chris Wilson2da3b9b2011-04-14 09:41:17 +01004211 */
Chris Wilson2efb8132016-08-18 17:17:06 +01004212 vma = ERR_PTR(-ENOSPC);
Chris Wilson59354852018-02-20 13:42:06 +00004213 if ((flags & PIN_MAPPABLE) == 0 &&
4214 (!view || view->type == I915_GGTT_VIEW_NORMAL))
Chris Wilson2efb8132016-08-18 17:17:06 +01004215 vma = i915_gem_object_ggtt_pin(obj, view, 0, alignment,
Chris Wilson59354852018-02-20 13:42:06 +00004216 flags |
4217 PIN_MAPPABLE |
4218 PIN_NONBLOCK);
4219 if (IS_ERR(vma))
Chris Wilson767a2222016-11-07 11:01:28 +00004220 vma = i915_gem_object_ggtt_pin(obj, view, 0, alignment, flags);
Chris Wilson058d88c2016-08-15 10:49:06 +01004221 if (IS_ERR(vma))
Chris Wilsonbd3d2252017-10-13 21:26:14 +01004222 goto err_unpin_global;
Chris Wilson2da3b9b2011-04-14 09:41:17 +01004223
Chris Wilsond8923dc2016-08-18 17:17:07 +01004224 vma->display_alignment = max_t(u64, vma->display_alignment, alignment);
4225
Chris Wilson5a97bcc2017-02-22 11:40:46 +00004226 __i915_gem_object_flush_for_display(obj);
Chris Wilsonb118c1e2010-05-27 13:18:14 +01004227
Chris Wilson2da3b9b2011-04-14 09:41:17 +01004228 /* It should now be out of any other write domains, and we can update
4229 * the domain values for our changes.
4230 */
Christian Königc0a51fd2018-02-16 13:43:38 +01004231 obj->read_domains |= I915_GEM_DOMAIN_GTT;
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08004232
Chris Wilson058d88c2016-08-15 10:49:06 +01004233 return vma;
Chris Wilsoncc98b412013-08-09 12:25:09 +01004234
Chris Wilsonbd3d2252017-10-13 21:26:14 +01004235err_unpin_global:
4236 obj->pin_global--;
Chris Wilson058d88c2016-08-15 10:49:06 +01004237 return vma;
Chris Wilsoncc98b412013-08-09 12:25:09 +01004238}
4239
4240void
Chris Wilson058d88c2016-08-15 10:49:06 +01004241i915_gem_object_unpin_from_display_plane(struct i915_vma *vma)
Chris Wilsoncc98b412013-08-09 12:25:09 +01004242{
Chris Wilson49d73912016-11-29 09:50:08 +00004243 lockdep_assert_held(&vma->vm->i915->drm.struct_mutex);
Chris Wilson4c7d62c2016-10-28 13:58:32 +01004244
Chris Wilsonbd3d2252017-10-13 21:26:14 +01004245 if (WARN_ON(vma->obj->pin_global == 0))
Tvrtko Ursulin8a0c39b2015-04-13 11:50:09 +01004246 return;
4247
Chris Wilsonbd3d2252017-10-13 21:26:14 +01004248 if (--vma->obj->pin_global == 0)
Chris Wilsonf51455d2017-01-10 14:47:34 +00004249 vma->display_alignment = I915_GTT_MIN_ALIGNMENT;
Tvrtko Ursuline6617332015-03-23 11:10:33 +00004250
Chris Wilson383d5822016-08-18 17:17:08 +01004251 /* Bump the LRU to try and avoid premature eviction whilst flipping */
Chris Wilsonbefedbb2017-01-19 19:26:55 +00004252 i915_gem_object_bump_inactive_ggtt(vma->obj);
Chris Wilson383d5822016-08-18 17:17:08 +01004253
Chris Wilson058d88c2016-08-15 10:49:06 +01004254 i915_vma_unpin(vma);
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08004255}
4256
Eric Anholte47c68e2008-11-14 13:35:19 -08004257/**
4258 * Moves a single object to the CPU read, and possibly write domain.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01004259 * @obj: object to act on
4260 * @write: requesting write or read-only access
Eric Anholte47c68e2008-11-14 13:35:19 -08004261 *
4262 * This function returns when the move is complete, including waiting on
4263 * flushes to occur.
4264 */
Chris Wilsondabdfe02012-03-26 10:10:27 +02004265int
Chris Wilson919926a2010-11-12 13:42:53 +00004266i915_gem_object_set_to_cpu_domain(struct drm_i915_gem_object *obj, bool write)
Eric Anholte47c68e2008-11-14 13:35:19 -08004267{
Eric Anholte47c68e2008-11-14 13:35:19 -08004268 int ret;
4269
Chris Wilsone95433c2016-10-28 13:58:27 +01004270 lockdep_assert_held(&obj->base.dev->struct_mutex);
Chris Wilson4c7d62c2016-10-28 13:58:32 +01004271
Chris Wilsone95433c2016-10-28 13:58:27 +01004272 ret = i915_gem_object_wait(obj,
4273 I915_WAIT_INTERRUPTIBLE |
4274 I915_WAIT_LOCKED |
4275 (write ? I915_WAIT_ALL : 0),
4276 MAX_SCHEDULE_TIMEOUT,
4277 NULL);
Chris Wilson88241782011-01-07 17:09:48 +00004278 if (ret)
4279 return ret;
4280
Chris Wilsonef749212017-04-12 12:01:10 +01004281 flush_write_domain(obj, ~I915_GEM_DOMAIN_CPU);
Eric Anholte47c68e2008-11-14 13:35:19 -08004282
Eric Anholte47c68e2008-11-14 13:35:19 -08004283 /* Flush the CPU cache if it's still invalid. */
Christian Königc0a51fd2018-02-16 13:43:38 +01004284 if ((obj->read_domains & I915_GEM_DOMAIN_CPU) == 0) {
Chris Wilson57822dc2017-02-22 11:40:48 +00004285 i915_gem_clflush_object(obj, I915_CLFLUSH_SYNC);
Christian Königc0a51fd2018-02-16 13:43:38 +01004286 obj->read_domains |= I915_GEM_DOMAIN_CPU;
Eric Anholte47c68e2008-11-14 13:35:19 -08004287 }
4288
4289 /* It should now be out of any other write domains, and we can update
4290 * the domain values for our changes.
4291 */
Christian Königc0a51fd2018-02-16 13:43:38 +01004292 GEM_BUG_ON(obj->write_domain & ~I915_GEM_DOMAIN_CPU);
Eric Anholte47c68e2008-11-14 13:35:19 -08004293
4294 /* If we're writing through the CPU, then the GPU read domains will
4295 * need to be invalidated at next use.
4296 */
Chris Wilsone27ab732017-06-15 13:38:49 +01004297 if (write)
4298 __start_cpu_write(obj);
Eric Anholt2ef7eea2008-11-10 10:53:25 -08004299
4300 return 0;
4301}
4302
Eric Anholt673a3942008-07-30 12:06:12 -07004303/* Throttle our rendering by waiting until the ring has completed our requests
4304 * emitted over 20 msec ago.
4305 *
Eric Anholtb9624422009-06-03 07:27:35 +00004306 * Note that if we were to use the current jiffies each time around the loop,
4307 * we wouldn't escape the function with any frames outstanding if the time to
4308 * render a frame was over 20ms.
4309 *
Eric Anholt673a3942008-07-30 12:06:12 -07004310 * This should get us reasonable parallelism between CPU and GPU but also
4311 * relatively low latency when blocking on a particular request to finish.
4312 */
4313static int
Chris Wilsonf787a5f2010-09-24 16:02:42 +01004314i915_gem_ring_throttle(struct drm_device *dev, struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07004315{
Chris Wilsonfac5e232016-07-04 11:34:36 +01004316 struct drm_i915_private *dev_priv = to_i915(dev);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01004317 struct drm_i915_file_private *file_priv = file->driver_priv;
Chris Wilsond0bc54f2015-05-21 21:01:48 +01004318 unsigned long recent_enough = jiffies - DRM_I915_THROTTLE_JIFFIES;
Chris Wilsone61e0f52018-02-21 09:56:36 +00004319 struct i915_request *request, *target = NULL;
Chris Wilsone95433c2016-10-28 13:58:27 +01004320 long ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004321
Chris Wilsonf4457ae2016-04-13 17:35:08 +01004322 /* ABI: return -EIO if already wedged */
4323 if (i915_terminally_wedged(&dev_priv->gpu_error))
4324 return -EIO;
Chris Wilsone110e8d2011-01-26 15:39:14 +00004325
Chris Wilson1c255952010-09-26 11:03:27 +01004326 spin_lock(&file_priv->mm.lock);
Chris Wilsonc8659ef2017-03-02 12:25:25 +00004327 list_for_each_entry(request, &file_priv->mm.request_list, client_link) {
Eric Anholtb9624422009-06-03 07:27:35 +00004328 if (time_after_eq(request->emitted_jiffies, recent_enough))
4329 break;
4330
Chris Wilsonc8659ef2017-03-02 12:25:25 +00004331 if (target) {
4332 list_del(&target->client_link);
4333 target->file_priv = NULL;
4334 }
John Harrisonfcfa423c2015-05-29 17:44:12 +01004335
John Harrison54fb2412014-11-24 18:49:27 +00004336 target = request;
Eric Anholtb9624422009-06-03 07:27:35 +00004337 }
John Harrisonff865882014-11-24 18:49:28 +00004338 if (target)
Chris Wilsone61e0f52018-02-21 09:56:36 +00004339 i915_request_get(target);
Chris Wilson1c255952010-09-26 11:03:27 +01004340 spin_unlock(&file_priv->mm.lock);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01004341
John Harrison54fb2412014-11-24 18:49:27 +00004342 if (target == NULL)
Chris Wilsonf787a5f2010-09-24 16:02:42 +01004343 return 0;
4344
Chris Wilsone61e0f52018-02-21 09:56:36 +00004345 ret = i915_request_wait(target,
Chris Wilsone95433c2016-10-28 13:58:27 +01004346 I915_WAIT_INTERRUPTIBLE,
4347 MAX_SCHEDULE_TIMEOUT);
Chris Wilsone61e0f52018-02-21 09:56:36 +00004348 i915_request_put(target);
John Harrisonff865882014-11-24 18:49:28 +00004349
Chris Wilsone95433c2016-10-28 13:58:27 +01004350 return ret < 0 ? ret : 0;
Eric Anholt673a3942008-07-30 12:06:12 -07004351}
4352
Chris Wilson058d88c2016-08-15 10:49:06 +01004353struct i915_vma *
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02004354i915_gem_object_ggtt_pin(struct drm_i915_gem_object *obj,
4355 const struct i915_ggtt_view *view,
Chris Wilson91b2db62016-08-04 16:32:23 +01004356 u64 size,
Chris Wilson2ffffd02016-08-04 16:32:22 +01004357 u64 alignment,
4358 u64 flags)
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02004359{
Chris Wilsonad16d2e2016-10-13 09:55:04 +01004360 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
4361 struct i915_address_space *vm = &dev_priv->ggtt.base;
Chris Wilson59bfa122016-08-04 16:32:31 +01004362 struct i915_vma *vma;
4363 int ret;
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03004364
Chris Wilson4c7d62c2016-10-28 13:58:32 +01004365 lockdep_assert_held(&obj->base.dev->struct_mutex);
4366
Chris Wilsonac87a6fd2018-02-20 13:42:05 +00004367 if (flags & PIN_MAPPABLE &&
4368 (!view || view->type == I915_GGTT_VIEW_NORMAL)) {
Chris Wilson43ae70d92017-10-09 09:44:01 +01004369 /* If the required space is larger than the available
4370 * aperture, we will not able to find a slot for the
4371 * object and unbinding the object now will be in
4372 * vain. Worse, doing so may cause us to ping-pong
4373 * the object in and out of the Global GTT and
4374 * waste a lot of cycles under the mutex.
4375 */
4376 if (obj->base.size > dev_priv->ggtt.mappable_end)
4377 return ERR_PTR(-E2BIG);
4378
4379 /* If NONBLOCK is set the caller is optimistically
4380 * trying to cache the full object within the mappable
4381 * aperture, and *must* have a fallback in place for
4382 * situations where we cannot bind the object. We
4383 * can be a little more lax here and use the fallback
4384 * more often to avoid costly migrations of ourselves
4385 * and other objects within the aperture.
4386 *
4387 * Half-the-aperture is used as a simple heuristic.
4388 * More interesting would to do search for a free
4389 * block prior to making the commitment to unbind.
4390 * That caters for the self-harm case, and with a
4391 * little more heuristics (e.g. NOFAULT, NOEVICT)
4392 * we could try to minimise harm to others.
4393 */
4394 if (flags & PIN_NONBLOCK &&
4395 obj->base.size > dev_priv->ggtt.mappable_end / 2)
4396 return ERR_PTR(-ENOSPC);
4397 }
4398
Chris Wilson718659a2017-01-16 15:21:28 +00004399 vma = i915_vma_instance(obj, vm, view);
Chris Wilsone0216b72017-01-19 19:26:57 +00004400 if (unlikely(IS_ERR(vma)))
Chris Wilson058d88c2016-08-15 10:49:06 +01004401 return vma;
Chris Wilson59bfa122016-08-04 16:32:31 +01004402
4403 if (i915_vma_misplaced(vma, size, alignment, flags)) {
Chris Wilson43ae70d92017-10-09 09:44:01 +01004404 if (flags & PIN_NONBLOCK) {
4405 if (i915_vma_is_pinned(vma) || i915_vma_is_active(vma))
4406 return ERR_PTR(-ENOSPC);
Chris Wilson59bfa122016-08-04 16:32:31 +01004407
Chris Wilson43ae70d92017-10-09 09:44:01 +01004408 if (flags & PIN_MAPPABLE &&
Chris Wilson944397f2017-01-09 16:16:11 +00004409 vma->fence_size > dev_priv->ggtt.mappable_end / 2)
Chris Wilsonad16d2e2016-10-13 09:55:04 +01004410 return ERR_PTR(-ENOSPC);
4411 }
4412
Chris Wilson59bfa122016-08-04 16:32:31 +01004413 WARN(i915_vma_is_pinned(vma),
4414 "bo is already pinned in ggtt with incorrect alignment:"
Chris Wilson05a20d02016-08-18 17:16:55 +01004415 " offset=%08x, req.alignment=%llx,"
4416 " req.map_and_fenceable=%d, vma->map_and_fenceable=%d\n",
4417 i915_ggtt_offset(vma), alignment,
Chris Wilson59bfa122016-08-04 16:32:31 +01004418 !!(flags & PIN_MAPPABLE),
Chris Wilson05a20d02016-08-18 17:16:55 +01004419 i915_vma_is_map_and_fenceable(vma));
Chris Wilson59bfa122016-08-04 16:32:31 +01004420 ret = i915_vma_unbind(vma);
4421 if (ret)
Chris Wilson058d88c2016-08-15 10:49:06 +01004422 return ERR_PTR(ret);
Chris Wilson59bfa122016-08-04 16:32:31 +01004423 }
4424
Chris Wilson058d88c2016-08-15 10:49:06 +01004425 ret = i915_vma_pin(vma, size, alignment, flags | PIN_GLOBAL);
4426 if (ret)
4427 return ERR_PTR(ret);
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02004428
Chris Wilson058d88c2016-08-15 10:49:06 +01004429 return vma;
Eric Anholt673a3942008-07-30 12:06:12 -07004430}
4431
Chris Wilsonedf6b762016-08-09 09:23:33 +01004432static __always_inline unsigned int __busy_read_flag(unsigned int id)
Chris Wilson3fdc13c2016-08-05 10:14:18 +01004433{
4434 /* Note that we could alias engines in the execbuf API, but
4435 * that would be very unwise as it prevents userspace from
4436 * fine control over engine selection. Ahem.
4437 *
4438 * This should be something like EXEC_MAX_ENGINE instead of
4439 * I915_NUM_ENGINES.
4440 */
4441 BUILD_BUG_ON(I915_NUM_ENGINES > 16);
4442 return 0x10000 << id;
4443}
4444
4445static __always_inline unsigned int __busy_write_id(unsigned int id)
4446{
Chris Wilson70cb4722016-08-09 18:08:25 +01004447 /* The uABI guarantees an active writer is also amongst the read
4448 * engines. This would be true if we accessed the activity tracking
4449 * under the lock, but as we perform the lookup of the object and
4450 * its activity locklessly we can not guarantee that the last_write
4451 * being active implies that we have set the same engine flag from
4452 * last_read - hence we always set both read and write busy for
4453 * last_write.
4454 */
4455 return id | __busy_read_flag(id);
Chris Wilson3fdc13c2016-08-05 10:14:18 +01004456}
4457
Chris Wilsonedf6b762016-08-09 09:23:33 +01004458static __always_inline unsigned int
Chris Wilsond07f0e52016-10-28 13:58:44 +01004459__busy_set_if_active(const struct dma_fence *fence,
Chris Wilson3fdc13c2016-08-05 10:14:18 +01004460 unsigned int (*flag)(unsigned int id))
4461{
Chris Wilsone61e0f52018-02-21 09:56:36 +00004462 struct i915_request *rq;
Chris Wilson12555012016-08-16 09:50:40 +01004463
Chris Wilsond07f0e52016-10-28 13:58:44 +01004464 /* We have to check the current hw status of the fence as the uABI
4465 * guarantees forward progress. We could rely on the idle worker
4466 * to eventually flush us, but to minimise latency just ask the
4467 * hardware.
4468 *
4469 * Note we only report on the status of native fences.
4470 */
4471 if (!dma_fence_is_i915(fence))
Chris Wilson12555012016-08-16 09:50:40 +01004472 return 0;
4473
Chris Wilsond07f0e52016-10-28 13:58:44 +01004474 /* opencode to_request() in order to avoid const warnings */
Chris Wilsone61e0f52018-02-21 09:56:36 +00004475 rq = container_of(fence, struct i915_request, fence);
4476 if (i915_request_completed(rq))
Chris Wilsond07f0e52016-10-28 13:58:44 +01004477 return 0;
4478
Chris Wilson1d39f282017-04-11 13:43:06 +01004479 return flag(rq->engine->uabi_id);
Chris Wilson3fdc13c2016-08-05 10:14:18 +01004480}
4481
Chris Wilsonedf6b762016-08-09 09:23:33 +01004482static __always_inline unsigned int
Chris Wilsond07f0e52016-10-28 13:58:44 +01004483busy_check_reader(const struct dma_fence *fence)
Chris Wilson3fdc13c2016-08-05 10:14:18 +01004484{
Chris Wilsond07f0e52016-10-28 13:58:44 +01004485 return __busy_set_if_active(fence, __busy_read_flag);
Chris Wilson3fdc13c2016-08-05 10:14:18 +01004486}
4487
Chris Wilsonedf6b762016-08-09 09:23:33 +01004488static __always_inline unsigned int
Chris Wilsond07f0e52016-10-28 13:58:44 +01004489busy_check_writer(const struct dma_fence *fence)
Chris Wilson3fdc13c2016-08-05 10:14:18 +01004490{
Chris Wilsond07f0e52016-10-28 13:58:44 +01004491 if (!fence)
4492 return 0;
4493
4494 return __busy_set_if_active(fence, __busy_write_id);
Chris Wilson3fdc13c2016-08-05 10:14:18 +01004495}
4496
Eric Anholt673a3942008-07-30 12:06:12 -07004497int
Eric Anholt673a3942008-07-30 12:06:12 -07004498i915_gem_busy_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00004499 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07004500{
4501 struct drm_i915_gem_busy *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00004502 struct drm_i915_gem_object *obj;
Chris Wilsond07f0e52016-10-28 13:58:44 +01004503 struct reservation_object_list *list;
4504 unsigned int seq;
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004505 int err;
Eric Anholt673a3942008-07-30 12:06:12 -07004506
Chris Wilsond07f0e52016-10-28 13:58:44 +01004507 err = -ENOENT;
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004508 rcu_read_lock();
4509 obj = i915_gem_object_lookup_rcu(file, args->handle);
Chris Wilsond07f0e52016-10-28 13:58:44 +01004510 if (!obj)
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004511 goto out;
Chris Wilsond07f0e52016-10-28 13:58:44 +01004512
4513 /* A discrepancy here is that we do not report the status of
4514 * non-i915 fences, i.e. even though we may report the object as idle,
4515 * a call to set-domain may still stall waiting for foreign rendering.
4516 * This also means that wait-ioctl may report an object as busy,
4517 * where busy-ioctl considers it idle.
4518 *
4519 * We trade the ability to warn of foreign fences to report on which
4520 * i915 engines are active for the object.
4521 *
4522 * Alternatively, we can trade that extra information on read/write
4523 * activity with
4524 * args->busy =
4525 * !reservation_object_test_signaled_rcu(obj->resv, true);
4526 * to report the overall busyness. This is what the wait-ioctl does.
4527 *
4528 */
4529retry:
4530 seq = raw_read_seqcount(&obj->resv->seq);
4531
4532 /* Translate the exclusive fence to the READ *and* WRITE engine */
4533 args->busy = busy_check_writer(rcu_dereference(obj->resv->fence_excl));
4534
4535 /* Translate shared fences to READ set of engines */
4536 list = rcu_dereference(obj->resv->fence);
4537 if (list) {
4538 unsigned int shared_count = list->shared_count, i;
4539
4540 for (i = 0; i < shared_count; ++i) {
4541 struct dma_fence *fence =
4542 rcu_dereference(list->shared[i]);
4543
4544 args->busy |= busy_check_reader(fence);
4545 }
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004546 }
Zou Nan haid1b851f2010-05-21 09:08:57 +08004547
Chris Wilsond07f0e52016-10-28 13:58:44 +01004548 if (args->busy && read_seqcount_retry(&obj->resv->seq, seq))
4549 goto retry;
Chris Wilson426960b2016-01-15 16:51:46 +00004550
Chris Wilsond07f0e52016-10-28 13:58:44 +01004551 err = 0;
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004552out:
4553 rcu_read_unlock();
4554 return err;
Eric Anholt673a3942008-07-30 12:06:12 -07004555}
4556
4557int
4558i915_gem_throttle_ioctl(struct drm_device *dev, void *data,
4559 struct drm_file *file_priv)
4560{
Akshay Joshi0206e352011-08-16 15:34:10 -04004561 return i915_gem_ring_throttle(dev, file_priv);
Eric Anholt673a3942008-07-30 12:06:12 -07004562}
4563
Chris Wilson3ef94da2009-09-14 16:50:29 +01004564int
4565i915_gem_madvise_ioctl(struct drm_device *dev, void *data,
4566 struct drm_file *file_priv)
4567{
Chris Wilsonfac5e232016-07-04 11:34:36 +01004568 struct drm_i915_private *dev_priv = to_i915(dev);
Chris Wilson3ef94da2009-09-14 16:50:29 +01004569 struct drm_i915_gem_madvise *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00004570 struct drm_i915_gem_object *obj;
Chris Wilson1233e2d2016-10-28 13:58:37 +01004571 int err;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004572
4573 switch (args->madv) {
4574 case I915_MADV_DONTNEED:
4575 case I915_MADV_WILLNEED:
4576 break;
4577 default:
4578 return -EINVAL;
4579 }
4580
Chris Wilson03ac0642016-07-20 13:31:51 +01004581 obj = i915_gem_object_lookup(file_priv, args->handle);
Chris Wilson1233e2d2016-10-28 13:58:37 +01004582 if (!obj)
4583 return -ENOENT;
4584
4585 err = mutex_lock_interruptible(&obj->mm.lock);
4586 if (err)
4587 goto out;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004588
Chris Wilsonf1fa4f42017-10-13 21:26:13 +01004589 if (i915_gem_object_has_pages(obj) &&
Chris Wilson3e510a82016-08-05 10:14:23 +01004590 i915_gem_object_is_tiled(obj) &&
Daniel Vetter656bfa32014-11-20 09:26:30 +01004591 dev_priv->quirks & QUIRK_PIN_SWIZZLED_PAGES) {
Chris Wilsonbc0629a2016-11-01 10:03:17 +00004592 if (obj->mm.madv == I915_MADV_WILLNEED) {
4593 GEM_BUG_ON(!obj->mm.quirked);
Chris Wilsona4f5ea62016-10-28 13:58:35 +01004594 __i915_gem_object_unpin_pages(obj);
Chris Wilsonbc0629a2016-11-01 10:03:17 +00004595 obj->mm.quirked = false;
4596 }
4597 if (args->madv == I915_MADV_WILLNEED) {
Chris Wilson2c3a3f42016-11-04 10:30:01 +00004598 GEM_BUG_ON(obj->mm.quirked);
Chris Wilsona4f5ea62016-10-28 13:58:35 +01004599 __i915_gem_object_pin_pages(obj);
Chris Wilsonbc0629a2016-11-01 10:03:17 +00004600 obj->mm.quirked = true;
4601 }
Daniel Vetter656bfa32014-11-20 09:26:30 +01004602 }
4603
Chris Wilsona4f5ea62016-10-28 13:58:35 +01004604 if (obj->mm.madv != __I915_MADV_PURGED)
4605 obj->mm.madv = args->madv;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004606
Chris Wilson6c085a72012-08-20 11:40:46 +02004607 /* if the object is no longer attached, discard its backing storage */
Chris Wilsonf1fa4f42017-10-13 21:26:13 +01004608 if (obj->mm.madv == I915_MADV_DONTNEED &&
4609 !i915_gem_object_has_pages(obj))
Chris Wilson2d7ef392009-09-20 23:13:10 +01004610 i915_gem_object_truncate(obj);
4611
Chris Wilsona4f5ea62016-10-28 13:58:35 +01004612 args->retained = obj->mm.madv != __I915_MADV_PURGED;
Chris Wilson1233e2d2016-10-28 13:58:37 +01004613 mutex_unlock(&obj->mm.lock);
Chris Wilsonbb6baf72009-09-22 14:24:13 +01004614
Chris Wilson1233e2d2016-10-28 13:58:37 +01004615out:
Chris Wilsonf8c417c2016-07-20 13:31:53 +01004616 i915_gem_object_put(obj);
Chris Wilson1233e2d2016-10-28 13:58:37 +01004617 return err;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004618}
4619
Chris Wilson5b8c8ae2016-11-16 19:07:04 +00004620static void
Chris Wilsone61e0f52018-02-21 09:56:36 +00004621frontbuffer_retire(struct i915_gem_active *active, struct i915_request *request)
Chris Wilson5b8c8ae2016-11-16 19:07:04 +00004622{
4623 struct drm_i915_gem_object *obj =
4624 container_of(active, typeof(*obj), frontbuffer_write);
4625
Chris Wilsond59b21e2017-02-22 11:40:49 +00004626 intel_fb_obj_flush(obj, ORIGIN_CS);
Chris Wilson5b8c8ae2016-11-16 19:07:04 +00004627}
4628
Chris Wilson37e680a2012-06-07 15:38:42 +01004629void i915_gem_object_init(struct drm_i915_gem_object *obj,
4630 const struct drm_i915_gem_object_ops *ops)
Chris Wilson0327d6b2012-08-11 15:41:06 +01004631{
Chris Wilson1233e2d2016-10-28 13:58:37 +01004632 mutex_init(&obj->mm.lock);
4633
Ben Widawsky2f633152013-07-17 12:19:03 -07004634 INIT_LIST_HEAD(&obj->vma_list);
Chris Wilsond1b48c12017-08-16 09:52:08 +01004635 INIT_LIST_HEAD(&obj->lut_list);
Chris Wilson8d9d5742015-04-07 16:20:38 +01004636 INIT_LIST_HEAD(&obj->batch_pool_link);
Chris Wilson0327d6b2012-08-11 15:41:06 +01004637
Chris Wilson37e680a2012-06-07 15:38:42 +01004638 obj->ops = ops;
4639
Chris Wilsond07f0e52016-10-28 13:58:44 +01004640 reservation_object_init(&obj->__builtin_resv);
4641 obj->resv = &obj->__builtin_resv;
4642
Chris Wilson50349242016-08-18 17:17:04 +01004643 obj->frontbuffer_ggtt_origin = ORIGIN_GTT;
Chris Wilson5b8c8ae2016-11-16 19:07:04 +00004644 init_request_active(&obj->frontbuffer_write, frontbuffer_retire);
Chris Wilsona4f5ea62016-10-28 13:58:35 +01004645
4646 obj->mm.madv = I915_MADV_WILLNEED;
4647 INIT_RADIX_TREE(&obj->mm.get_page.radix, GFP_KERNEL | __GFP_NOWARN);
4648 mutex_init(&obj->mm.get_page.lock);
Chris Wilson0327d6b2012-08-11 15:41:06 +01004649
Dave Gordonf19ec8c2016-07-04 11:34:37 +01004650 i915_gem_info_add_obj(to_i915(obj->base.dev), obj->base.size);
Chris Wilson0327d6b2012-08-11 15:41:06 +01004651}
4652
Chris Wilson37e680a2012-06-07 15:38:42 +01004653static const struct drm_i915_gem_object_ops i915_gem_object_ops = {
Tvrtko Ursulin3599a912016-11-01 14:44:10 +00004654 .flags = I915_GEM_OBJECT_HAS_STRUCT_PAGE |
4655 I915_GEM_OBJECT_IS_SHRINKABLE,
Chris Wilson7c55e2c2017-03-07 12:03:38 +00004656
Chris Wilson37e680a2012-06-07 15:38:42 +01004657 .get_pages = i915_gem_object_get_pages_gtt,
4658 .put_pages = i915_gem_object_put_pages_gtt,
Chris Wilson7c55e2c2017-03-07 12:03:38 +00004659
4660 .pwrite = i915_gem_object_pwrite_gtt,
Chris Wilson37e680a2012-06-07 15:38:42 +01004661};
4662
Matthew Auld465c4032017-10-06 23:18:14 +01004663static int i915_gem_object_create_shmem(struct drm_device *dev,
4664 struct drm_gem_object *obj,
4665 size_t size)
4666{
4667 struct drm_i915_private *i915 = to_i915(dev);
4668 unsigned long flags = VM_NORESERVE;
4669 struct file *filp;
4670
4671 drm_gem_private_object_init(dev, obj, size);
4672
4673 if (i915->mm.gemfs)
4674 filp = shmem_file_setup_with_mnt(i915->mm.gemfs, "i915", size,
4675 flags);
4676 else
4677 filp = shmem_file_setup("i915", size, flags);
4678
4679 if (IS_ERR(filp))
4680 return PTR_ERR(filp);
4681
4682 obj->filp = filp;
4683
4684 return 0;
4685}
4686
Chris Wilsonb4bcbe22016-10-18 13:02:49 +01004687struct drm_i915_gem_object *
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +00004688i915_gem_object_create(struct drm_i915_private *dev_priv, u64 size)
Daniel Vetterac52bc52010-04-09 19:05:06 +00004689{
Daniel Vetterc397b902010-04-09 19:05:07 +00004690 struct drm_i915_gem_object *obj;
Hugh Dickins5949eac2011-06-27 16:18:18 -07004691 struct address_space *mapping;
Chris Wilsonb8f55be2017-08-11 12:11:16 +01004692 unsigned int cache_level;
Daniel Vetter1a240d42012-11-29 22:18:51 +01004693 gfp_t mask;
Chris Wilsonfe3db792016-04-25 13:32:13 +01004694 int ret;
Daniel Vetterc397b902010-04-09 19:05:07 +00004695
Chris Wilsonb4bcbe22016-10-18 13:02:49 +01004696 /* There is a prevalence of the assumption that we fit the object's
4697 * page count inside a 32bit _signed_ variable. Let's document this and
4698 * catch if we ever need to fix it. In the meantime, if you do spot
4699 * such a local variable, please consider fixing!
4700 */
Tvrtko Ursulin7a3ee5d2017-03-30 17:31:30 +01004701 if (size >> PAGE_SHIFT > INT_MAX)
Chris Wilsonb4bcbe22016-10-18 13:02:49 +01004702 return ERR_PTR(-E2BIG);
4703
4704 if (overflows_type(size, obj->base.size))
4705 return ERR_PTR(-E2BIG);
4706
Tvrtko Ursulin187685c2016-12-01 14:16:36 +00004707 obj = i915_gem_object_alloc(dev_priv);
Daniel Vetterc397b902010-04-09 19:05:07 +00004708 if (obj == NULL)
Chris Wilsonfe3db792016-04-25 13:32:13 +01004709 return ERR_PTR(-ENOMEM);
Daniel Vetterc397b902010-04-09 19:05:07 +00004710
Matthew Auld465c4032017-10-06 23:18:14 +01004711 ret = i915_gem_object_create_shmem(&dev_priv->drm, &obj->base, size);
Chris Wilsonfe3db792016-04-25 13:32:13 +01004712 if (ret)
4713 goto fail;
Daniel Vetterc397b902010-04-09 19:05:07 +00004714
Chris Wilsonbed1ea92012-05-24 20:48:12 +01004715 mask = GFP_HIGHUSER | __GFP_RECLAIMABLE;
Jani Nikulac0f86832016-12-07 12:13:04 +02004716 if (IS_I965GM(dev_priv) || IS_I965G(dev_priv)) {
Chris Wilsonbed1ea92012-05-24 20:48:12 +01004717 /* 965gm cannot relocate objects above 4GiB. */
4718 mask &= ~__GFP_HIGHMEM;
4719 mask |= __GFP_DMA32;
4720 }
4721
Al Viro93c76a32015-12-04 23:45:44 -05004722 mapping = obj->base.filp->f_mapping;
Chris Wilsonbed1ea92012-05-24 20:48:12 +01004723 mapping_set_gfp_mask(mapping, mask);
Chris Wilson4846bf02017-06-09 12:03:46 +01004724 GEM_BUG_ON(!(mapping_gfp_mask(mapping) & __GFP_RECLAIM));
Hugh Dickins5949eac2011-06-27 16:18:18 -07004725
Chris Wilson37e680a2012-06-07 15:38:42 +01004726 i915_gem_object_init(obj, &i915_gem_object_ops);
Chris Wilson73aa8082010-09-30 11:46:12 +01004727
Christian Königc0a51fd2018-02-16 13:43:38 +01004728 obj->write_domain = I915_GEM_DOMAIN_CPU;
4729 obj->read_domains = I915_GEM_DOMAIN_CPU;
Daniel Vetterc397b902010-04-09 19:05:07 +00004730
Chris Wilsonb8f55be2017-08-11 12:11:16 +01004731 if (HAS_LLC(dev_priv))
Eugeni Dodonov3d29b842012-01-17 14:43:53 -02004732 /* On some devices, we can have the GPU use the LLC (the CPU
Eric Anholta1871112011-03-29 16:59:55 -07004733 * cache) for about a 10% performance improvement
4734 * compared to uncached. Graphics requests other than
4735 * display scanout are coherent with the CPU in
4736 * accessing this cache. This means in this mode we
4737 * don't need to clflush on the CPU side, and on the
4738 * GPU side we only need to flush internal caches to
4739 * get data visible to the CPU.
4740 *
4741 * However, we maintain the display planes as UC, and so
4742 * need to rebind when first used as such.
4743 */
Chris Wilsonb8f55be2017-08-11 12:11:16 +01004744 cache_level = I915_CACHE_LLC;
4745 else
4746 cache_level = I915_CACHE_NONE;
Eric Anholta1871112011-03-29 16:59:55 -07004747
Chris Wilsonb8f55be2017-08-11 12:11:16 +01004748 i915_gem_object_set_cache_coherency(obj, cache_level);
Chris Wilsone27ab732017-06-15 13:38:49 +01004749
Daniel Vetterd861e332013-07-24 23:25:03 +02004750 trace_i915_gem_object_create(obj);
4751
Chris Wilson05394f32010-11-08 19:18:58 +00004752 return obj;
Chris Wilsonfe3db792016-04-25 13:32:13 +01004753
4754fail:
4755 i915_gem_object_free(obj);
Chris Wilsonfe3db792016-04-25 13:32:13 +01004756 return ERR_PTR(ret);
Daniel Vetterac52bc52010-04-09 19:05:06 +00004757}
4758
Chris Wilson340fbd82014-05-22 09:16:52 +01004759static bool discard_backing_storage(struct drm_i915_gem_object *obj)
4760{
4761 /* If we are the last user of the backing storage (be it shmemfs
4762 * pages or stolen etc), we know that the pages are going to be
4763 * immediately released. In this case, we can then skip copying
4764 * back the contents from the GPU.
4765 */
4766
Chris Wilsona4f5ea62016-10-28 13:58:35 +01004767 if (obj->mm.madv != I915_MADV_WILLNEED)
Chris Wilson340fbd82014-05-22 09:16:52 +01004768 return false;
4769
4770 if (obj->base.filp == NULL)
4771 return true;
4772
4773 /* At first glance, this looks racy, but then again so would be
4774 * userspace racing mmap against close. However, the first external
4775 * reference to the filp can only be obtained through the
4776 * i915_gem_mmap_ioctl() which safeguards us against the user
4777 * acquiring such a reference whilst we are in the middle of
4778 * freeing the object.
4779 */
4780 return atomic_long_read(&obj->base.filp->f_count) == 1;
4781}
4782
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004783static void __i915_gem_free_objects(struct drm_i915_private *i915,
4784 struct llist_node *freed)
Chris Wilsonbe726152010-07-23 23:18:50 +01004785{
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004786 struct drm_i915_gem_object *obj, *on;
Chris Wilsonbe726152010-07-23 23:18:50 +01004787
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004788 intel_runtime_pm_get(i915);
Chris Wilsoncc731f52017-10-13 21:26:21 +01004789 llist_for_each_entry_safe(obj, on, freed, freed) {
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004790 struct i915_vma *vma, *vn;
Paulo Zanonif65c9162013-11-27 18:20:34 -02004791
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004792 trace_i915_gem_object_destroy(obj);
4793
Chris Wilsoncc731f52017-10-13 21:26:21 +01004794 mutex_lock(&i915->drm.struct_mutex);
4795
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004796 GEM_BUG_ON(i915_gem_object_is_active(obj));
4797 list_for_each_entry_safe(vma, vn,
4798 &obj->vma_list, obj_link) {
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004799 GEM_BUG_ON(i915_vma_is_active(vma));
4800 vma->flags &= ~I915_VMA_PIN_MASK;
4801 i915_vma_close(vma);
4802 }
Chris Wilsondb6c2b42016-11-01 11:54:00 +00004803 GEM_BUG_ON(!list_empty(&obj->vma_list));
4804 GEM_BUG_ON(!RB_EMPTY_ROOT(&obj->vma_tree));
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004805
Chris Wilsonf2123812017-10-16 12:40:37 +01004806 /* This serializes freeing with the shrinker. Since the free
4807 * is delayed, first by RCU then by the workqueue, we want the
4808 * shrinker to be able to free pages of unreferenced objects,
4809 * or else we may oom whilst there are plenty of deferred
4810 * freed objects.
4811 */
4812 if (i915_gem_object_has_pages(obj)) {
4813 spin_lock(&i915->mm.obj_lock);
4814 list_del_init(&obj->mm.link);
4815 spin_unlock(&i915->mm.obj_lock);
4816 }
4817
Chris Wilsoncc731f52017-10-13 21:26:21 +01004818 mutex_unlock(&i915->drm.struct_mutex);
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004819
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004820 GEM_BUG_ON(obj->bind_count);
Chris Wilsona65adaf2017-10-09 09:43:57 +01004821 GEM_BUG_ON(obj->userfault_count);
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004822 GEM_BUG_ON(atomic_read(&obj->frontbuffer_bits));
Chris Wilson67b48042017-08-22 12:05:16 +01004823 GEM_BUG_ON(!list_empty(&obj->lut_list));
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004824
4825 if (obj->ops->release)
4826 obj->ops->release(obj);
4827
4828 if (WARN_ON(i915_gem_object_has_pinned_pages(obj)))
4829 atomic_set(&obj->mm.pages_pin_count, 0);
Chris Wilson548625e2016-11-01 12:11:34 +00004830 __i915_gem_object_put_pages(obj, I915_MM_NORMAL);
Chris Wilsonf1fa4f42017-10-13 21:26:13 +01004831 GEM_BUG_ON(i915_gem_object_has_pages(obj));
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004832
4833 if (obj->base.import_attach)
4834 drm_prime_gem_destroy(&obj->base, NULL);
4835
Chris Wilsond07f0e52016-10-28 13:58:44 +01004836 reservation_object_fini(&obj->__builtin_resv);
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004837 drm_gem_object_release(&obj->base);
4838 i915_gem_info_remove_obj(i915, obj->base.size);
4839
4840 kfree(obj->bit_17);
4841 i915_gem_object_free(obj);
Chris Wilsoncc731f52017-10-13 21:26:21 +01004842
Chris Wilsonc9c704712018-02-19 22:06:31 +00004843 GEM_BUG_ON(!atomic_read(&i915->mm.free_count));
4844 atomic_dec(&i915->mm.free_count);
4845
Chris Wilsoncc731f52017-10-13 21:26:21 +01004846 if (on)
4847 cond_resched();
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004848 }
Chris Wilsoncc731f52017-10-13 21:26:21 +01004849 intel_runtime_pm_put(i915);
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004850}
4851
4852static void i915_gem_flush_free_objects(struct drm_i915_private *i915)
4853{
4854 struct llist_node *freed;
4855
Chris Wilson87701b42017-10-13 21:26:20 +01004856 /* Free the oldest, most stale object to keep the free_list short */
4857 freed = NULL;
4858 if (!llist_empty(&i915->mm.free_list)) { /* quick test for hotpath */
4859 /* Only one consumer of llist_del_first() allowed */
4860 spin_lock(&i915->mm.free_lock);
4861 freed = llist_del_first(&i915->mm.free_list);
4862 spin_unlock(&i915->mm.free_lock);
4863 }
4864 if (unlikely(freed)) {
4865 freed->next = NULL;
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004866 __i915_gem_free_objects(i915, freed);
Chris Wilson87701b42017-10-13 21:26:20 +01004867 }
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004868}
4869
4870static void __i915_gem_free_work(struct work_struct *work)
4871{
4872 struct drm_i915_private *i915 =
4873 container_of(work, struct drm_i915_private, mm.free_work);
4874 struct llist_node *freed;
Chris Wilson26e12f82011-03-20 11:20:19 +00004875
Chris Wilson2ef1e722018-01-15 20:57:59 +00004876 /*
4877 * All file-owned VMA should have been released by this point through
Chris Wilsonb1f788c2016-08-04 07:52:45 +01004878 * i915_gem_close_object(), or earlier by i915_gem_context_close().
4879 * However, the object may also be bound into the global GTT (e.g.
4880 * older GPUs without per-process support, or for direct access through
4881 * the GTT either for the user or for scanout). Those VMA still need to
4882 * unbound now.
4883 */
Chris Wilson1488fc02012-04-24 15:47:31 +01004884
Chris Wilsonf991c492017-11-06 11:15:08 +00004885 spin_lock(&i915->mm.free_lock);
Chris Wilson5ad08be2017-04-07 11:25:51 +01004886 while ((freed = llist_del_all(&i915->mm.free_list))) {
Chris Wilsonf991c492017-11-06 11:15:08 +00004887 spin_unlock(&i915->mm.free_lock);
4888
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004889 __i915_gem_free_objects(i915, freed);
Chris Wilson5ad08be2017-04-07 11:25:51 +01004890 if (need_resched())
Chris Wilsonf991c492017-11-06 11:15:08 +00004891 return;
4892
4893 spin_lock(&i915->mm.free_lock);
Chris Wilson5ad08be2017-04-07 11:25:51 +01004894 }
Chris Wilsonf991c492017-11-06 11:15:08 +00004895 spin_unlock(&i915->mm.free_lock);
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004896}
4897
4898static void __i915_gem_free_object_rcu(struct rcu_head *head)
4899{
4900 struct drm_i915_gem_object *obj =
4901 container_of(head, typeof(*obj), rcu);
4902 struct drm_i915_private *i915 = to_i915(obj->base.dev);
4903
Chris Wilson2ef1e722018-01-15 20:57:59 +00004904 /*
4905 * Since we require blocking on struct_mutex to unbind the freed
4906 * object from the GPU before releasing resources back to the
4907 * system, we can not do that directly from the RCU callback (which may
4908 * be a softirq context), but must instead then defer that work onto a
4909 * kthread. We use the RCU callback rather than move the freed object
4910 * directly onto the work queue so that we can mix between using the
4911 * worker and performing frees directly from subsequent allocations for
4912 * crude but effective memory throttling.
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004913 */
4914 if (llist_add(&obj->freed, &i915->mm.free_list))
Chris Wilsonbeacbd12018-01-15 12:28:45 +00004915 queue_work(i915->wq, &i915->mm.free_work);
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004916}
4917
4918void i915_gem_free_object(struct drm_gem_object *gem_obj)
4919{
4920 struct drm_i915_gem_object *obj = to_intel_bo(gem_obj);
4921
Chris Wilsonbc0629a2016-11-01 10:03:17 +00004922 if (obj->mm.quirked)
4923 __i915_gem_object_unpin_pages(obj);
4924
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004925 if (discard_backing_storage(obj))
4926 obj->mm.madv = I915_MADV_DONTNEED;
Daniel Vettera071fa02014-06-18 23:28:09 +02004927
Chris Wilson2ef1e722018-01-15 20:57:59 +00004928 /*
4929 * Before we free the object, make sure any pure RCU-only
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004930 * read-side critical sections are complete, e.g.
4931 * i915_gem_busy_ioctl(). For the corresponding synchronized
4932 * lookup see i915_gem_object_lookup_rcu().
4933 */
Chris Wilsonc9c704712018-02-19 22:06:31 +00004934 atomic_inc(&to_i915(obj->base.dev)->mm.free_count);
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004935 call_rcu(&obj->rcu, __i915_gem_free_object_rcu);
Chris Wilsonbe726152010-07-23 23:18:50 +01004936}
4937
Chris Wilsonf8a7fde2016-10-28 13:58:29 +01004938void __i915_gem_object_release_unless_active(struct drm_i915_gem_object *obj)
4939{
4940 lockdep_assert_held(&obj->base.dev->struct_mutex);
4941
Chris Wilsond1b48c12017-08-16 09:52:08 +01004942 if (!i915_gem_object_has_active_reference(obj) &&
4943 i915_gem_object_is_active(obj))
Chris Wilsonf8a7fde2016-10-28 13:58:29 +01004944 i915_gem_object_set_active_reference(obj);
4945 else
4946 i915_gem_object_put(obj);
4947}
4948
Chris Wilsonae6c4572017-11-10 14:26:28 +00004949static void assert_kernel_context_is_current(struct drm_i915_private *i915)
Chris Wilson3033aca2016-10-28 13:58:47 +01004950{
Chris Wilsonae6c4572017-11-10 14:26:28 +00004951 struct i915_gem_context *kernel_context = i915->kernel_context;
Chris Wilson3033aca2016-10-28 13:58:47 +01004952 struct intel_engine_cs *engine;
4953 enum intel_engine_id id;
4954
Chris Wilsonae6c4572017-11-10 14:26:28 +00004955 for_each_engine(engine, i915, id) {
4956 GEM_BUG_ON(__i915_gem_active_peek(&engine->timeline->last_request));
4957 GEM_BUG_ON(engine->last_retired_context != kernel_context);
4958 }
Chris Wilson3033aca2016-10-28 13:58:47 +01004959}
4960
Chris Wilson24145512017-01-24 11:01:35 +00004961void i915_gem_sanitize(struct drm_i915_private *i915)
4962{
Chris Wilsonf36325f2017-08-26 12:09:34 +01004963 if (i915_terminally_wedged(&i915->gpu_error)) {
4964 mutex_lock(&i915->drm.struct_mutex);
4965 i915_gem_unset_wedged(i915);
4966 mutex_unlock(&i915->drm.struct_mutex);
4967 }
4968
Chris Wilson24145512017-01-24 11:01:35 +00004969 /*
4970 * If we inherit context state from the BIOS or earlier occupants
4971 * of the GPU, the GPU may be in an inconsistent state when we
4972 * try to take over. The only way to remove the earlier state
4973 * is by resetting. However, resetting on earlier gen is tricky as
4974 * it may impact the display and we are uncertain about the stability
Joonas Lahtinenea117b82017-04-28 10:53:38 +03004975 * of the reset, so this could be applied to even earlier gen.
Chris Wilson24145512017-01-24 11:01:35 +00004976 */
Daniele Ceraolo Spurioce1599a2018-02-07 13:24:40 -08004977 if (INTEL_GEN(i915) >= 5 && intel_has_gpu_reset(i915))
4978 WARN_ON(intel_gpu_reset(i915, ALL_ENGINES));
Chris Wilson24145512017-01-24 11:01:35 +00004979}
4980
Tvrtko Ursulinbf9e8422016-12-01 14:16:38 +00004981int i915_gem_suspend(struct drm_i915_private *dev_priv)
Eric Anholt673a3942008-07-30 12:06:12 -07004982{
Tvrtko Ursulinbf9e8422016-12-01 14:16:38 +00004983 struct drm_device *dev = &dev_priv->drm;
Chris Wilsondcff85c2016-08-05 10:14:11 +01004984 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004985
Chris Wilsonc998e8a2017-03-02 08:30:29 +00004986 intel_runtime_pm_get(dev_priv);
Chris Wilson54b4f682016-07-21 21:16:19 +01004987 intel_suspend_gt_powersave(dev_priv);
4988
Chris Wilson45c5f202013-10-16 11:50:01 +01004989 mutex_lock(&dev->struct_mutex);
Chris Wilson5ab57c72016-07-15 14:56:20 +01004990
4991 /* We have to flush all the executing contexts to main memory so
4992 * that they can saved in the hibernation image. To ensure the last
4993 * context image is coherent, we have to switch away from it. That
4994 * leaves the dev_priv->kernel_context still active when
4995 * we actually suspend, and its image in memory may not match the GPU
4996 * state. Fortunately, the kernel_context is disposable and we do
4997 * not rely on its state.
4998 */
Chris Wilsonecf73eb2017-11-30 10:29:51 +00004999 if (!i915_terminally_wedged(&dev_priv->gpu_error)) {
5000 ret = i915_gem_switch_to_kernel_context(dev_priv);
5001 if (ret)
5002 goto err_unlock;
Chris Wilson5ab57c72016-07-15 14:56:20 +01005003
Chris Wilsonecf73eb2017-11-30 10:29:51 +00005004 ret = i915_gem_wait_for_idle(dev_priv,
5005 I915_WAIT_INTERRUPTIBLE |
5006 I915_WAIT_LOCKED);
5007 if (ret && ret != -EIO)
5008 goto err_unlock;
Chris Wilsonf7403342013-09-13 23:57:04 +01005009
Chris Wilsonecf73eb2017-11-30 10:29:51 +00005010 assert_kernel_context_is_current(dev_priv);
5011 }
Chris Wilson829a0af2017-06-20 12:05:45 +01005012 i915_gem_contexts_lost(dev_priv);
Chris Wilson45c5f202013-10-16 11:50:01 +01005013 mutex_unlock(&dev->struct_mutex);
5014
Michal Wajdeczko7cfca4a2018-03-02 11:15:49 +00005015 intel_uc_suspend(dev_priv);
Sagar Arun Kamble63987bf2017-04-05 15:51:50 +05305016
Chris Wilson737b1502015-01-26 18:03:03 +02005017 cancel_delayed_work_sync(&dev_priv->gpu_error.hangcheck_work);
Chris Wilson67d97da2016-07-04 08:08:31 +01005018 cancel_delayed_work_sync(&dev_priv->gt.retire_work);
Chris Wilsonbdeb9782016-12-23 14:57:56 +00005019
5020 /* As the idle_work is rearming if it detects a race, play safe and
5021 * repeat the flush until it is definitely idle.
5022 */
Chris Wilson7c262402017-10-06 11:40:38 +01005023 drain_delayed_work(&dev_priv->gt.idle_work);
Chris Wilsonbdeb9782016-12-23 14:57:56 +00005024
Chris Wilsonbdcf1202014-11-25 11:56:33 +00005025 /* Assert that we sucessfully flushed all the work and
5026 * reset the GPU back to its idle, low power state.
5027 */
Chris Wilson67d97da2016-07-04 08:08:31 +01005028 WARN_ON(dev_priv->gt.awake);
Chris Wilsonfc692bd2017-08-26 12:09:35 +01005029 if (WARN_ON(!intel_engines_are_idle(dev_priv)))
5030 i915_gem_set_wedged(dev_priv); /* no hope, discard everything */
Chris Wilsonbdcf1202014-11-25 11:56:33 +00005031
Imre Deak1c777c52016-10-12 17:46:37 +03005032 /*
5033 * Neither the BIOS, ourselves or any other kernel
5034 * expects the system to be in execlists mode on startup,
5035 * so we need to reset the GPU back to legacy mode. And the only
5036 * known way to disable logical contexts is through a GPU reset.
5037 *
5038 * So in order to leave the system in a known default configuration,
5039 * always reset the GPU upon unload and suspend. Afterwards we then
5040 * clean up the GEM state tracking, flushing off the requests and
5041 * leaving the system in a known idle state.
5042 *
5043 * Note that is of the upmost importance that the GPU is idle and
5044 * all stray writes are flushed *before* we dismantle the backing
5045 * storage for the pinned objects.
5046 *
5047 * However, since we are uncertain that resetting the GPU on older
5048 * machines is a good idea, we don't - just in case it leaves the
5049 * machine in an unusable condition.
5050 */
Michal Wajdeczkoc37d5722018-03-12 13:03:07 +00005051 intel_uc_sanitize(dev_priv);
Chris Wilson24145512017-01-24 11:01:35 +00005052 i915_gem_sanitize(dev_priv);
Chris Wilsoncad99462017-08-26 12:09:33 +01005053
5054 intel_runtime_pm_put(dev_priv);
5055 return 0;
Imre Deak1c777c52016-10-12 17:46:37 +03005056
Chris Wilsonc998e8a2017-03-02 08:30:29 +00005057err_unlock:
Chris Wilson45c5f202013-10-16 11:50:01 +01005058 mutex_unlock(&dev->struct_mutex);
Chris Wilsonc998e8a2017-03-02 08:30:29 +00005059 intel_runtime_pm_put(dev_priv);
Chris Wilson45c5f202013-10-16 11:50:01 +01005060 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07005061}
5062
Chris Wilson37cd3302017-11-12 11:27:38 +00005063void i915_gem_resume(struct drm_i915_private *i915)
Chris Wilson5ab57c72016-07-15 14:56:20 +01005064{
Chris Wilson37cd3302017-11-12 11:27:38 +00005065 WARN_ON(i915->gt.awake);
Chris Wilson5ab57c72016-07-15 14:56:20 +01005066
Chris Wilson37cd3302017-11-12 11:27:38 +00005067 mutex_lock(&i915->drm.struct_mutex);
5068 intel_uncore_forcewake_get(i915, FORCEWAKE_ALL);
Imre Deak31ab49a2016-11-07 11:20:05 +02005069
Chris Wilson37cd3302017-11-12 11:27:38 +00005070 i915_gem_restore_gtt_mappings(i915);
5071 i915_gem_restore_fences(i915);
Chris Wilson5ab57c72016-07-15 14:56:20 +01005072
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00005073 /*
5074 * As we didn't flush the kernel context before suspend, we cannot
Chris Wilson5ab57c72016-07-15 14:56:20 +01005075 * guarantee that the context image is complete. So let's just reset
5076 * it and start again.
5077 */
Chris Wilson37cd3302017-11-12 11:27:38 +00005078 i915->gt.resume(i915);
Chris Wilson5ab57c72016-07-15 14:56:20 +01005079
Chris Wilson37cd3302017-11-12 11:27:38 +00005080 if (i915_gem_init_hw(i915))
5081 goto err_wedged;
5082
Michal Wajdeczko7cfca4a2018-03-02 11:15:49 +00005083 intel_uc_resume(i915);
Chris Wilson7469c622017-11-14 13:03:00 +00005084
Chris Wilson37cd3302017-11-12 11:27:38 +00005085 /* Always reload a context for powersaving. */
5086 if (i915_gem_switch_to_kernel_context(i915))
5087 goto err_wedged;
5088
5089out_unlock:
5090 intel_uncore_forcewake_put(i915, FORCEWAKE_ALL);
5091 mutex_unlock(&i915->drm.struct_mutex);
5092 return;
5093
5094err_wedged:
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00005095 if (!i915_terminally_wedged(&i915->gpu_error)) {
5096 DRM_ERROR("failed to re-initialize GPU, declaring wedged!\n");
5097 i915_gem_set_wedged(i915);
5098 }
Chris Wilson37cd3302017-11-12 11:27:38 +00005099 goto out_unlock;
Chris Wilson5ab57c72016-07-15 14:56:20 +01005100}
5101
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00005102void i915_gem_init_swizzling(struct drm_i915_private *dev_priv)
Daniel Vetterf691e2f2012-02-02 09:58:12 +01005103{
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00005104 if (INTEL_GEN(dev_priv) < 5 ||
Daniel Vetterf691e2f2012-02-02 09:58:12 +01005105 dev_priv->mm.bit_6_swizzle_x == I915_BIT_6_SWIZZLE_NONE)
5106 return;
5107
5108 I915_WRITE(DISP_ARB_CTL, I915_READ(DISP_ARB_CTL) |
5109 DISP_TILE_SURFACE_SWIZZLING);
5110
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01005111 if (IS_GEN5(dev_priv))
Daniel Vetter11782b02012-01-31 16:47:55 +01005112 return;
5113
Daniel Vetterf691e2f2012-02-02 09:58:12 +01005114 I915_WRITE(TILECTL, I915_READ(TILECTL) | TILECTL_SWZCTL);
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01005115 if (IS_GEN6(dev_priv))
Daniel Vetter6b26c862012-04-24 14:04:12 +02005116 I915_WRITE(ARB_MODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_SNB));
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01005117 else if (IS_GEN7(dev_priv))
Daniel Vetter6b26c862012-04-24 14:04:12 +02005118 I915_WRITE(ARB_MODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_IVB));
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01005119 else if (IS_GEN8(dev_priv))
Ben Widawsky31a53362013-11-02 21:07:04 -07005120 I915_WRITE(GAMTARBMODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_BDW));
Ben Widawsky8782e262012-12-18 10:31:23 -08005121 else
5122 BUG();
Daniel Vetterf691e2f2012-02-02 09:58:12 +01005123}
Daniel Vettere21af882012-02-09 20:53:27 +01005124
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01005125static void init_unused_ring(struct drm_i915_private *dev_priv, u32 base)
Ville Syrjälä81e7f202014-08-15 01:21:55 +03005126{
Ville Syrjälä81e7f202014-08-15 01:21:55 +03005127 I915_WRITE(RING_CTL(base), 0);
5128 I915_WRITE(RING_HEAD(base), 0);
5129 I915_WRITE(RING_TAIL(base), 0);
5130 I915_WRITE(RING_START(base), 0);
5131}
5132
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01005133static void init_unused_rings(struct drm_i915_private *dev_priv)
Ville Syrjälä81e7f202014-08-15 01:21:55 +03005134{
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01005135 if (IS_I830(dev_priv)) {
5136 init_unused_ring(dev_priv, PRB1_BASE);
5137 init_unused_ring(dev_priv, SRB0_BASE);
5138 init_unused_ring(dev_priv, SRB1_BASE);
5139 init_unused_ring(dev_priv, SRB2_BASE);
5140 init_unused_ring(dev_priv, SRB3_BASE);
5141 } else if (IS_GEN2(dev_priv)) {
5142 init_unused_ring(dev_priv, SRB0_BASE);
5143 init_unused_ring(dev_priv, SRB1_BASE);
5144 } else if (IS_GEN3(dev_priv)) {
5145 init_unused_ring(dev_priv, PRB1_BASE);
5146 init_unused_ring(dev_priv, PRB2_BASE);
Ville Syrjälä81e7f202014-08-15 01:21:55 +03005147 }
5148}
5149
Chris Wilson20a8a742017-02-08 14:30:31 +00005150static int __i915_gem_restart_engines(void *data)
Ben Widawsky4fc7c972013-02-08 11:49:24 -08005151{
Chris Wilson20a8a742017-02-08 14:30:31 +00005152 struct drm_i915_private *i915 = data;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00005153 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05305154 enum intel_engine_id id;
Chris Wilson20a8a742017-02-08 14:30:31 +00005155 int err;
5156
5157 for_each_engine(engine, i915, id) {
5158 err = engine->init_hw(engine);
Chris Wilson8177e112018-02-07 11:15:45 +00005159 if (err) {
5160 DRM_ERROR("Failed to restart %s (%d)\n",
5161 engine->name, err);
Chris Wilson20a8a742017-02-08 14:30:31 +00005162 return err;
Chris Wilson8177e112018-02-07 11:15:45 +00005163 }
Chris Wilson20a8a742017-02-08 14:30:31 +00005164 }
5165
5166 return 0;
5167}
5168
5169int i915_gem_init_hw(struct drm_i915_private *dev_priv)
5170{
Chris Wilsond200cda2016-04-28 09:56:44 +01005171 int ret;
Ben Widawsky4fc7c972013-02-08 11:49:24 -08005172
Chris Wilsonde867c22016-10-25 13:16:02 +01005173 dev_priv->gt.last_init_time = ktime_get();
5174
Chris Wilson5e4f5182015-02-13 14:35:59 +00005175 /* Double layer security blanket, see i915_gem_init() */
5176 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
5177
Tvrtko Ursulin0031fb92016-11-04 14:42:44 +00005178 if (HAS_EDRAM(dev_priv) && INTEL_GEN(dev_priv) < 9)
Ben Widawsky05e21cc2013-07-04 11:02:04 -07005179 I915_WRITE(HSW_IDICR, I915_READ(HSW_IDICR) | IDIHASHMSK(0xf));
Ben Widawsky4fc7c972013-02-08 11:49:24 -08005180
Tvrtko Ursulin772c2a52016-10-13 11:03:01 +01005181 if (IS_HASWELL(dev_priv))
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01005182 I915_WRITE(MI_PREDICATE_RESULT_2, IS_HSW_GT3(dev_priv) ?
Ville Syrjälä0bf21342013-11-29 14:56:12 +02005183 LOWER_SLICE_ENABLED : LOWER_SLICE_DISABLED);
Rodrigo Vivi94353732013-08-28 16:45:46 -03005184
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01005185 if (HAS_PCH_NOP(dev_priv)) {
Tvrtko Ursulinfd6b8f42016-10-14 10:13:06 +01005186 if (IS_IVYBRIDGE(dev_priv)) {
Daniel Vetter6ba844b2014-01-22 23:39:30 +01005187 u32 temp = I915_READ(GEN7_MSG_CTL);
5188 temp &= ~(WAIT_FOR_PCH_FLR_ACK | WAIT_FOR_PCH_RESET_ACK);
5189 I915_WRITE(GEN7_MSG_CTL, temp);
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00005190 } else if (INTEL_GEN(dev_priv) >= 7) {
Daniel Vetter6ba844b2014-01-22 23:39:30 +01005191 u32 temp = I915_READ(HSW_NDE_RSTWRN_OPT);
5192 temp &= ~RESET_PCH_HANDSHAKE_ENABLE;
5193 I915_WRITE(HSW_NDE_RSTWRN_OPT, temp);
5194 }
Ben Widawsky88a2b2a2013-04-05 13:12:43 -07005195 }
5196
Oscar Mateo59b449d2018-04-10 09:12:47 -07005197 intel_gt_workarounds_apply(dev_priv);
5198
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00005199 i915_gem_init_swizzling(dev_priv);
Ben Widawsky4fc7c972013-02-08 11:49:24 -08005200
Daniel Vetterd5abdfd2014-11-20 09:45:19 +01005201 /*
5202 * At least 830 can leave some of the unused rings
5203 * "active" (ie. head != tail) after resume which
5204 * will prevent c3 entry. Makes sure all unused rings
5205 * are totally idle.
5206 */
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01005207 init_unused_rings(dev_priv);
Daniel Vetterd5abdfd2014-11-20 09:45:19 +01005208
Dave Gordoned54c1a2016-01-19 19:02:54 +00005209 BUG_ON(!dev_priv->kernel_context);
Chris Wilson6f74b362017-10-15 15:37:25 +01005210 if (i915_terminally_wedged(&dev_priv->gpu_error)) {
5211 ret = -EIO;
5212 goto out;
5213 }
John Harrison90638cc2015-05-29 17:43:37 +01005214
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00005215 ret = i915_ppgtt_init_hw(dev_priv);
John Harrison4ad2fd82015-06-18 13:11:20 +01005216 if (ret) {
Chris Wilson8177e112018-02-07 11:15:45 +00005217 DRM_ERROR("Enabling PPGTT failed (%d)\n", ret);
John Harrison4ad2fd82015-06-18 13:11:20 +01005218 goto out;
5219 }
5220
Jackie Lif08e2032018-03-13 17:32:53 -07005221 ret = intel_wopcm_init_hw(&dev_priv->wopcm);
5222 if (ret) {
5223 DRM_ERROR("Enabling WOPCM failed (%d)\n", ret);
5224 goto out;
5225 }
5226
Michał Winiarski9bdc3572017-10-25 18:25:19 +01005227 /* We can't enable contexts until all firmware is loaded */
5228 ret = intel_uc_init_hw(dev_priv);
Chris Wilson8177e112018-02-07 11:15:45 +00005229 if (ret) {
5230 DRM_ERROR("Enabling uc failed (%d)\n", ret);
Michał Winiarski9bdc3572017-10-25 18:25:19 +01005231 goto out;
Chris Wilson8177e112018-02-07 11:15:45 +00005232 }
Michał Winiarski9bdc3572017-10-25 18:25:19 +01005233
Tvrtko Ursulinbf9e8422016-12-01 14:16:38 +00005234 intel_mocs_init_l3cc_table(dev_priv);
Peter Antoine0ccdacf2016-04-13 15:03:25 +01005235
Chris Wilson136109c2017-11-02 13:14:30 +00005236 /* Only when the HW is re-initialised, can we replay the requests */
5237 ret = __i915_gem_restart_engines(dev_priv);
Chris Wilson5e4f5182015-02-13 14:35:59 +00005238out:
5239 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Ben Widawsky2fa48d82013-12-06 14:11:04 -08005240 return ret;
Zou Nan hai8187a2b2010-05-21 09:08:55 +08005241}
5242
Chris Wilsond2b4b972017-11-10 14:26:33 +00005243static int __intel_engines_record_defaults(struct drm_i915_private *i915)
5244{
5245 struct i915_gem_context *ctx;
5246 struct intel_engine_cs *engine;
5247 enum intel_engine_id id;
5248 int err;
5249
5250 /*
5251 * As we reset the gpu during very early sanitisation, the current
5252 * register state on the GPU should reflect its defaults values.
5253 * We load a context onto the hw (with restore-inhibit), then switch
5254 * over to a second context to save that default register state. We
5255 * can then prime every new context with that state so they all start
5256 * from the same default HW values.
5257 */
5258
5259 ctx = i915_gem_context_create_kernel(i915, 0);
5260 if (IS_ERR(ctx))
5261 return PTR_ERR(ctx);
5262
5263 for_each_engine(engine, i915, id) {
Chris Wilsone61e0f52018-02-21 09:56:36 +00005264 struct i915_request *rq;
Chris Wilsond2b4b972017-11-10 14:26:33 +00005265
Chris Wilsone61e0f52018-02-21 09:56:36 +00005266 rq = i915_request_alloc(engine, ctx);
Chris Wilsond2b4b972017-11-10 14:26:33 +00005267 if (IS_ERR(rq)) {
5268 err = PTR_ERR(rq);
5269 goto out_ctx;
5270 }
5271
Chris Wilson3fef5cd2017-11-20 10:20:02 +00005272 err = 0;
Chris Wilsond2b4b972017-11-10 14:26:33 +00005273 if (engine->init_context)
5274 err = engine->init_context(rq);
5275
Chris Wilsone61e0f52018-02-21 09:56:36 +00005276 __i915_request_add(rq, true);
Chris Wilsond2b4b972017-11-10 14:26:33 +00005277 if (err)
5278 goto err_active;
5279 }
5280
5281 err = i915_gem_switch_to_kernel_context(i915);
5282 if (err)
5283 goto err_active;
5284
5285 err = i915_gem_wait_for_idle(i915, I915_WAIT_LOCKED);
5286 if (err)
5287 goto err_active;
5288
5289 assert_kernel_context_is_current(i915);
5290
5291 for_each_engine(engine, i915, id) {
5292 struct i915_vma *state;
5293
Chris Wilsonab82a062018-04-30 14:15:01 +01005294 state = to_intel_context(ctx, engine)->state;
Chris Wilsond2b4b972017-11-10 14:26:33 +00005295 if (!state)
5296 continue;
5297
5298 /*
5299 * As we will hold a reference to the logical state, it will
5300 * not be torn down with the context, and importantly the
5301 * object will hold onto its vma (making it possible for a
5302 * stray GTT write to corrupt our defaults). Unmap the vma
5303 * from the GTT to prevent such accidents and reclaim the
5304 * space.
5305 */
5306 err = i915_vma_unbind(state);
5307 if (err)
5308 goto err_active;
5309
5310 err = i915_gem_object_set_to_cpu_domain(state->obj, false);
5311 if (err)
5312 goto err_active;
5313
5314 engine->default_state = i915_gem_object_get(state->obj);
5315 }
5316
5317 if (IS_ENABLED(CONFIG_DRM_I915_DEBUG_GEM)) {
5318 unsigned int found = intel_engines_has_context_isolation(i915);
5319
5320 /*
5321 * Make sure that classes with multiple engine instances all
5322 * share the same basic configuration.
5323 */
5324 for_each_engine(engine, i915, id) {
5325 unsigned int bit = BIT(engine->uabi_class);
5326 unsigned int expected = engine->default_state ? bit : 0;
5327
5328 if ((found & bit) != expected) {
5329 DRM_ERROR("mismatching default context state for class %d on engine %s\n",
5330 engine->uabi_class, engine->name);
5331 }
5332 }
5333 }
5334
5335out_ctx:
5336 i915_gem_context_set_closed(ctx);
5337 i915_gem_context_put(ctx);
5338 return err;
5339
5340err_active:
5341 /*
5342 * If we have to abandon now, we expect the engines to be idle
5343 * and ready to be torn-down. First try to flush any remaining
5344 * request, ensure we are pointing at the kernel context and
5345 * then remove it.
5346 */
5347 if (WARN_ON(i915_gem_switch_to_kernel_context(i915)))
5348 goto out_ctx;
5349
5350 if (WARN_ON(i915_gem_wait_for_idle(i915, I915_WAIT_LOCKED)))
5351 goto out_ctx;
5352
5353 i915_gem_contexts_lost(i915);
5354 goto out_ctx;
5355}
5356
Tvrtko Ursulinbf9e8422016-12-01 14:16:38 +00005357int i915_gem_init(struct drm_i915_private *dev_priv)
Chris Wilson1070a422012-04-24 15:47:41 +01005358{
Chris Wilson1070a422012-04-24 15:47:41 +01005359 int ret;
5360
Matthew Auldda9fe3f32017-10-06 23:18:31 +01005361 /*
5362 * We need to fallback to 4K pages since gvt gtt handling doesn't
5363 * support huge page entries - we will need to check either hypervisor
5364 * mm can support huge guest page or just do emulation in gvt.
5365 */
5366 if (intel_vgpu_active(dev_priv))
5367 mkwrite_device_info(dev_priv)->page_sizes =
5368 I915_GTT_PAGE_SIZE_4K;
5369
Chris Wilson94312822017-05-03 10:39:18 +01005370 dev_priv->mm.unordered_timeline = dma_fence_context_alloc(1);
Chris Wilson57822dc2017-02-22 11:40:48 +00005371
Chris Wilsonfb5c5512017-11-20 20:55:00 +00005372 if (HAS_LOGICAL_RING_CONTEXTS(dev_priv)) {
Chris Wilson821ed7d2016-09-09 14:11:53 +01005373 dev_priv->gt.resume = intel_lr_context_resume;
Tvrtko Ursulin117897f2016-03-16 11:00:40 +00005374 dev_priv->gt.cleanup_engine = intel_logical_ring_cleanup;
Chris Wilsonfb5c5512017-11-20 20:55:00 +00005375 } else {
5376 dev_priv->gt.resume = intel_legacy_submission_resume;
5377 dev_priv->gt.cleanup_engine = intel_engine_cleanup;
Oscar Mateoa83014d2014-07-24 17:04:21 +01005378 }
5379
Chris Wilsonee487002017-11-22 17:26:21 +00005380 ret = i915_gem_init_userptr(dev_priv);
5381 if (ret)
5382 return ret;
5383
Jackie Li6b0478f2018-03-13 17:32:50 -07005384 ret = intel_wopcm_init(&dev_priv->wopcm);
5385 if (ret)
5386 return ret;
5387
Sagar Arun Kamble70deead2018-01-24 21:16:58 +05305388 ret = intel_uc_init_misc(dev_priv);
Michał Winiarski3176ff42017-12-13 23:13:47 +01005389 if (ret)
5390 return ret;
5391
Chris Wilson5e4f5182015-02-13 14:35:59 +00005392 /* This is just a security blanket to placate dragons.
5393 * On some systems, we very sporadically observe that the first TLBs
5394 * used by the CS may be stale, despite us poking the TLB reset. If
5395 * we hold the forcewake during initialisation these problems
5396 * just magically go away.
5397 */
Chris Wilsonee487002017-11-22 17:26:21 +00005398 mutex_lock(&dev_priv->drm.struct_mutex);
Chris Wilson5e4f5182015-02-13 14:35:59 +00005399 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
5400
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01005401 ret = i915_gem_init_ggtt(dev_priv);
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00005402 if (ret) {
5403 GEM_BUG_ON(ret == -EIO);
5404 goto err_unlock;
5405 }
Jesse Barnesd62b4892013-03-08 10:45:53 -08005406
Chris Wilson829a0af2017-06-20 12:05:45 +01005407 ret = i915_gem_contexts_init(dev_priv);
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00005408 if (ret) {
5409 GEM_BUG_ON(ret == -EIO);
5410 goto err_ggtt;
5411 }
Ben Widawsky2fa48d82013-12-06 14:11:04 -08005412
Tvrtko Ursulinbf9e8422016-12-01 14:16:38 +00005413 ret = intel_engines_init(dev_priv);
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00005414 if (ret) {
5415 GEM_BUG_ON(ret == -EIO);
5416 goto err_context;
5417 }
Daniel Vetter53ca26c2012-04-26 23:28:03 +02005418
Chris Wilsonf58d13d2017-11-10 14:26:29 +00005419 intel_init_gt_powersave(dev_priv);
5420
Michał Winiarski61b5c152017-12-13 23:13:48 +01005421 ret = intel_uc_init(dev_priv);
Chris Wilsoncc6a8182017-11-10 14:26:30 +00005422 if (ret)
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00005423 goto err_pm;
Chris Wilsoncc6a8182017-11-10 14:26:30 +00005424
Michał Winiarski61b5c152017-12-13 23:13:48 +01005425 ret = i915_gem_init_hw(dev_priv);
5426 if (ret)
5427 goto err_uc_init;
5428
Chris Wilsoncc6a8182017-11-10 14:26:30 +00005429 /*
5430 * Despite its name intel_init_clock_gating applies both display
5431 * clock gating workarounds; GT mmio workarounds and the occasional
5432 * GT power context workaround. Worse, sometimes it includes a context
5433 * register workaround which we need to apply before we record the
5434 * default HW state for all contexts.
5435 *
5436 * FIXME: break up the workarounds and apply them at the right time!
5437 */
5438 intel_init_clock_gating(dev_priv);
5439
Chris Wilsond2b4b972017-11-10 14:26:33 +00005440 ret = __intel_engines_record_defaults(dev_priv);
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00005441 if (ret)
5442 goto err_init_hw;
5443
5444 if (i915_inject_load_failure()) {
5445 ret = -ENODEV;
5446 goto err_init_hw;
5447 }
5448
5449 if (i915_inject_load_failure()) {
5450 ret = -EIO;
5451 goto err_init_hw;
5452 }
5453
5454 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
5455 mutex_unlock(&dev_priv->drm.struct_mutex);
5456
5457 return 0;
5458
5459 /*
5460 * Unwinding is complicated by that we want to handle -EIO to mean
5461 * disable GPU submission but keep KMS alive. We want to mark the
5462 * HW as irrevisibly wedged, but keep enough state around that the
5463 * driver doesn't explode during runtime.
5464 */
5465err_init_hw:
5466 i915_gem_wait_for_idle(dev_priv, I915_WAIT_LOCKED);
5467 i915_gem_contexts_lost(dev_priv);
5468 intel_uc_fini_hw(dev_priv);
Michał Winiarski61b5c152017-12-13 23:13:48 +01005469err_uc_init:
5470 intel_uc_fini(dev_priv);
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00005471err_pm:
5472 if (ret != -EIO) {
5473 intel_cleanup_gt_powersave(dev_priv);
5474 i915_gem_cleanup_engines(dev_priv);
5475 }
5476err_context:
5477 if (ret != -EIO)
5478 i915_gem_contexts_fini(dev_priv);
5479err_ggtt:
5480err_unlock:
5481 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
5482 mutex_unlock(&dev_priv->drm.struct_mutex);
5483
Sagar Arun Kamble70deead2018-01-24 21:16:58 +05305484 intel_uc_fini_misc(dev_priv);
Sagar Arun Kambleda943b52018-01-10 18:24:16 +05305485
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00005486 if (ret != -EIO)
5487 i915_gem_cleanup_userptr(dev_priv);
5488
Chris Wilson60990322014-04-09 09:19:42 +01005489 if (ret == -EIO) {
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00005490 /*
5491 * Allow engine initialisation to fail by marking the GPU as
Chris Wilson60990322014-04-09 09:19:42 +01005492 * wedged. But we only want to do this where the GPU is angry,
5493 * for all other failure, such as an allocation failure, bail.
5494 */
Chris Wilson6f74b362017-10-15 15:37:25 +01005495 if (!i915_terminally_wedged(&dev_priv->gpu_error)) {
5496 DRM_ERROR("Failed to initialize GPU, declaring it wedged\n");
5497 i915_gem_set_wedged(dev_priv);
5498 }
Chris Wilson60990322014-04-09 09:19:42 +01005499 ret = 0;
Chris Wilson1070a422012-04-24 15:47:41 +01005500 }
5501
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00005502 i915_gem_drain_freed_objects(dev_priv);
Chris Wilson60990322014-04-09 09:19:42 +01005503 return ret;
Chris Wilson1070a422012-04-24 15:47:41 +01005504}
5505
Chris Wilson24145512017-01-24 11:01:35 +00005506void i915_gem_init_mmio(struct drm_i915_private *i915)
5507{
5508 i915_gem_sanitize(i915);
5509}
5510
Zou Nan hai8187a2b2010-05-21 09:08:55 +08005511void
Tvrtko Ursulincb15d9f2016-12-01 14:16:39 +00005512i915_gem_cleanup_engines(struct drm_i915_private *dev_priv)
Zou Nan hai8187a2b2010-05-21 09:08:55 +08005513{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00005514 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05305515 enum intel_engine_id id;
Zou Nan hai8187a2b2010-05-21 09:08:55 +08005516
Akash Goel3b3f1652016-10-13 22:44:48 +05305517 for_each_engine(engine, dev_priv, id)
Tvrtko Ursulin117897f2016-03-16 11:00:40 +00005518 dev_priv->gt.cleanup_engine(engine);
Zou Nan hai8187a2b2010-05-21 09:08:55 +08005519}
5520
Eric Anholt673a3942008-07-30 12:06:12 -07005521void
Imre Deak40ae4e12016-03-16 14:54:03 +02005522i915_gem_load_init_fences(struct drm_i915_private *dev_priv)
5523{
Chris Wilson49ef5292016-08-18 17:17:00 +01005524 int i;
Imre Deak40ae4e12016-03-16 14:54:03 +02005525
Tvrtko Ursulinc56b89f2018-02-09 21:58:46 +00005526 if (INTEL_GEN(dev_priv) >= 7 && !IS_VALLEYVIEW(dev_priv) &&
Imre Deak40ae4e12016-03-16 14:54:03 +02005527 !IS_CHERRYVIEW(dev_priv))
5528 dev_priv->num_fence_regs = 32;
Tvrtko Ursulinc56b89f2018-02-09 21:58:46 +00005529 else if (INTEL_GEN(dev_priv) >= 4 ||
Jani Nikula73f67aa2016-12-07 22:48:09 +02005530 IS_I945G(dev_priv) || IS_I945GM(dev_priv) ||
5531 IS_G33(dev_priv) || IS_PINEVIEW(dev_priv))
Imre Deak40ae4e12016-03-16 14:54:03 +02005532 dev_priv->num_fence_regs = 16;
5533 else
5534 dev_priv->num_fence_regs = 8;
5535
Chris Wilsonc0336662016-05-06 15:40:21 +01005536 if (intel_vgpu_active(dev_priv))
Imre Deak40ae4e12016-03-16 14:54:03 +02005537 dev_priv->num_fence_regs =
5538 I915_READ(vgtif_reg(avail_rs.fence_num));
5539
5540 /* Initialize fence registers to zero */
Chris Wilson49ef5292016-08-18 17:17:00 +01005541 for (i = 0; i < dev_priv->num_fence_regs; i++) {
5542 struct drm_i915_fence_reg *fence = &dev_priv->fence_regs[i];
5543
5544 fence->i915 = dev_priv;
5545 fence->id = i;
5546 list_add_tail(&fence->link, &dev_priv->mm.fence_list);
5547 }
Tvrtko Ursulin4362f4f2016-11-16 08:55:33 +00005548 i915_gem_restore_fences(dev_priv);
Imre Deak40ae4e12016-03-16 14:54:03 +02005549
Tvrtko Ursulin4362f4f2016-11-16 08:55:33 +00005550 i915_gem_detect_bit_6_swizzle(dev_priv);
Imre Deak40ae4e12016-03-16 14:54:03 +02005551}
5552
Chris Wilson9c52d1c2017-11-10 23:24:47 +00005553static void i915_gem_init__mm(struct drm_i915_private *i915)
5554{
5555 spin_lock_init(&i915->mm.object_stat_lock);
5556 spin_lock_init(&i915->mm.obj_lock);
5557 spin_lock_init(&i915->mm.free_lock);
5558
5559 init_llist_head(&i915->mm.free_list);
5560
5561 INIT_LIST_HEAD(&i915->mm.unbound_list);
5562 INIT_LIST_HEAD(&i915->mm.bound_list);
5563 INIT_LIST_HEAD(&i915->mm.fence_list);
5564 INIT_LIST_HEAD(&i915->mm.userfault_list);
5565
5566 INIT_WORK(&i915->mm.free_work, __i915_gem_free_work);
5567}
5568
Michal Wajdeczkoa0de9082018-03-23 12:34:49 +00005569int i915_gem_init_early(struct drm_i915_private *dev_priv)
Eric Anholt673a3942008-07-30 12:06:12 -07005570{
Tvrtko Ursulina9335682016-11-02 15:14:59 +00005571 int err = -ENOMEM;
Chris Wilson42dcedd2012-11-15 11:32:30 +00005572
Tvrtko Ursulina9335682016-11-02 15:14:59 +00005573 dev_priv->objects = KMEM_CACHE(drm_i915_gem_object, SLAB_HWCACHE_ALIGN);
5574 if (!dev_priv->objects)
Chris Wilson73cb9702016-10-28 13:58:46 +01005575 goto err_out;
Chris Wilson73cb9702016-10-28 13:58:46 +01005576
Tvrtko Ursulina9335682016-11-02 15:14:59 +00005577 dev_priv->vmas = KMEM_CACHE(i915_vma, SLAB_HWCACHE_ALIGN);
5578 if (!dev_priv->vmas)
Chris Wilson73cb9702016-10-28 13:58:46 +01005579 goto err_objects;
Chris Wilson73cb9702016-10-28 13:58:46 +01005580
Chris Wilsond1b48c12017-08-16 09:52:08 +01005581 dev_priv->luts = KMEM_CACHE(i915_lut_handle, 0);
5582 if (!dev_priv->luts)
5583 goto err_vmas;
5584
Chris Wilsone61e0f52018-02-21 09:56:36 +00005585 dev_priv->requests = KMEM_CACHE(i915_request,
Tvrtko Ursulina9335682016-11-02 15:14:59 +00005586 SLAB_HWCACHE_ALIGN |
5587 SLAB_RECLAIM_ACCOUNT |
Paul E. McKenney5f0d5a32017-01-18 02:53:44 -08005588 SLAB_TYPESAFE_BY_RCU);
Tvrtko Ursulina9335682016-11-02 15:14:59 +00005589 if (!dev_priv->requests)
Chris Wilsond1b48c12017-08-16 09:52:08 +01005590 goto err_luts;
Chris Wilson73cb9702016-10-28 13:58:46 +01005591
Chris Wilson52e54202016-11-14 20:41:02 +00005592 dev_priv->dependencies = KMEM_CACHE(i915_dependency,
5593 SLAB_HWCACHE_ALIGN |
5594 SLAB_RECLAIM_ACCOUNT);
5595 if (!dev_priv->dependencies)
5596 goto err_requests;
5597
Chris Wilsonc5cf9a92017-05-17 13:10:04 +01005598 dev_priv->priorities = KMEM_CACHE(i915_priolist, SLAB_HWCACHE_ALIGN);
5599 if (!dev_priv->priorities)
5600 goto err_dependencies;
5601
Chris Wilson73cb9702016-10-28 13:58:46 +01005602 mutex_lock(&dev_priv->drm.struct_mutex);
Chris Wilsonb887d612018-04-30 14:15:02 +01005603 INIT_LIST_HEAD(&dev_priv->gt.rings);
Chris Wilson73cb9702016-10-28 13:58:46 +01005604 INIT_LIST_HEAD(&dev_priv->gt.timelines);
Chris Wilsonbb894852016-11-14 20:40:57 +00005605 err = i915_gem_timeline_init__global(dev_priv);
Chris Wilson73cb9702016-10-28 13:58:46 +01005606 mutex_unlock(&dev_priv->drm.struct_mutex);
5607 if (err)
Chris Wilsonc5cf9a92017-05-17 13:10:04 +01005608 goto err_priorities;
Eric Anholt673a3942008-07-30 12:06:12 -07005609
Chris Wilson9c52d1c2017-11-10 23:24:47 +00005610 i915_gem_init__mm(dev_priv);
Chris Wilsonf2123812017-10-16 12:40:37 +01005611
Chris Wilson67d97da2016-07-04 08:08:31 +01005612 INIT_DELAYED_WORK(&dev_priv->gt.retire_work,
Eric Anholt673a3942008-07-30 12:06:12 -07005613 i915_gem_retire_work_handler);
Chris Wilson67d97da2016-07-04 08:08:31 +01005614 INIT_DELAYED_WORK(&dev_priv->gt.idle_work,
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005615 i915_gem_idle_work_handler);
Chris Wilson1f15b762016-07-01 17:23:14 +01005616 init_waitqueue_head(&dev_priv->gpu_error.wait_queue);
Daniel Vetter1f83fee2012-11-15 17:17:22 +01005617 init_waitqueue_head(&dev_priv->gpu_error.reset_queue);
Chris Wilson31169712009-09-14 16:50:28 +01005618
Joonas Lahtinen6f633402016-09-01 14:58:21 +03005619 atomic_set(&dev_priv->mm.bsd_engine_dispatch_index, 0);
5620
Chris Wilsonb5add952016-08-04 16:32:36 +01005621 spin_lock_init(&dev_priv->fb_tracking.lock);
Chris Wilson73cb9702016-10-28 13:58:46 +01005622
Matthew Auld465c4032017-10-06 23:18:14 +01005623 err = i915_gemfs_init(dev_priv);
5624 if (err)
5625 DRM_NOTE("Unable to create a private tmpfs mount, hugepage support will be disabled(%d).\n", err);
5626
Chris Wilson73cb9702016-10-28 13:58:46 +01005627 return 0;
5628
Chris Wilsonc5cf9a92017-05-17 13:10:04 +01005629err_priorities:
5630 kmem_cache_destroy(dev_priv->priorities);
Chris Wilson52e54202016-11-14 20:41:02 +00005631err_dependencies:
5632 kmem_cache_destroy(dev_priv->dependencies);
Chris Wilson73cb9702016-10-28 13:58:46 +01005633err_requests:
5634 kmem_cache_destroy(dev_priv->requests);
Chris Wilsond1b48c12017-08-16 09:52:08 +01005635err_luts:
5636 kmem_cache_destroy(dev_priv->luts);
Chris Wilson73cb9702016-10-28 13:58:46 +01005637err_vmas:
5638 kmem_cache_destroy(dev_priv->vmas);
5639err_objects:
5640 kmem_cache_destroy(dev_priv->objects);
5641err_out:
5642 return err;
Eric Anholt673a3942008-07-30 12:06:12 -07005643}
Dave Airlie71acb5e2008-12-30 20:31:46 +10005644
Michal Wajdeczkoa0de9082018-03-23 12:34:49 +00005645void i915_gem_cleanup_early(struct drm_i915_private *dev_priv)
Imre Deakd64aa092016-01-19 15:26:29 +02005646{
Chris Wilsonc4d4c1c2017-02-10 16:35:23 +00005647 i915_gem_drain_freed_objects(dev_priv);
Chris Wilsonc9c704712018-02-19 22:06:31 +00005648 GEM_BUG_ON(!llist_empty(&dev_priv->mm.free_list));
5649 GEM_BUG_ON(atomic_read(&dev_priv->mm.free_count));
Chris Wilsonc4d4c1c2017-02-10 16:35:23 +00005650 WARN_ON(dev_priv->mm.object_count);
Chris Wilson7d5d59e2016-11-01 08:48:41 +00005651
Matthew Auldea84aa72016-11-17 21:04:11 +00005652 mutex_lock(&dev_priv->drm.struct_mutex);
5653 i915_gem_timeline_fini(&dev_priv->gt.global_timeline);
5654 WARN_ON(!list_empty(&dev_priv->gt.timelines));
5655 mutex_unlock(&dev_priv->drm.struct_mutex);
5656
Chris Wilsonc5cf9a92017-05-17 13:10:04 +01005657 kmem_cache_destroy(dev_priv->priorities);
Chris Wilson52e54202016-11-14 20:41:02 +00005658 kmem_cache_destroy(dev_priv->dependencies);
Imre Deakd64aa092016-01-19 15:26:29 +02005659 kmem_cache_destroy(dev_priv->requests);
Chris Wilsond1b48c12017-08-16 09:52:08 +01005660 kmem_cache_destroy(dev_priv->luts);
Imre Deakd64aa092016-01-19 15:26:29 +02005661 kmem_cache_destroy(dev_priv->vmas);
5662 kmem_cache_destroy(dev_priv->objects);
Chris Wilson0eafec62016-08-04 16:32:41 +01005663
5664 /* And ensure that our DESTROY_BY_RCU slabs are truly destroyed */
5665 rcu_barrier();
Matthew Auld465c4032017-10-06 23:18:14 +01005666
5667 i915_gemfs_fini(dev_priv);
Imre Deakd64aa092016-01-19 15:26:29 +02005668}
5669
Chris Wilson6a800ea2016-09-21 14:51:07 +01005670int i915_gem_freeze(struct drm_i915_private *dev_priv)
5671{
Chris Wilsond0aa3012017-04-07 11:25:49 +01005672 /* Discard all purgeable objects, let userspace recover those as
5673 * required after resuming.
5674 */
Chris Wilson6a800ea2016-09-21 14:51:07 +01005675 i915_gem_shrink_all(dev_priv);
Chris Wilson6a800ea2016-09-21 14:51:07 +01005676
Chris Wilson6a800ea2016-09-21 14:51:07 +01005677 return 0;
5678}
5679
Chris Wilson461fb992016-05-14 07:26:33 +01005680int i915_gem_freeze_late(struct drm_i915_private *dev_priv)
5681{
5682 struct drm_i915_gem_object *obj;
Chris Wilson7aab2d52016-09-09 20:02:18 +01005683 struct list_head *phases[] = {
5684 &dev_priv->mm.unbound_list,
5685 &dev_priv->mm.bound_list,
5686 NULL
5687 }, **p;
Chris Wilson461fb992016-05-14 07:26:33 +01005688
5689 /* Called just before we write the hibernation image.
5690 *
5691 * We need to update the domain tracking to reflect that the CPU
5692 * will be accessing all the pages to create and restore from the
5693 * hibernation, and so upon restoration those pages will be in the
5694 * CPU domain.
5695 *
5696 * To make sure the hibernation image contains the latest state,
5697 * we update that state just before writing out the image.
Chris Wilson7aab2d52016-09-09 20:02:18 +01005698 *
5699 * To try and reduce the hibernation image, we manually shrink
Chris Wilsond0aa3012017-04-07 11:25:49 +01005700 * the objects as well, see i915_gem_freeze()
Chris Wilson461fb992016-05-14 07:26:33 +01005701 */
5702
Chris Wilson912d5722017-09-06 16:19:30 -07005703 i915_gem_shrink(dev_priv, -1UL, NULL, I915_SHRINK_UNBOUND);
Chris Wilson17b93c42017-04-07 11:25:50 +01005704 i915_gem_drain_freed_objects(dev_priv);
Chris Wilson461fb992016-05-14 07:26:33 +01005705
Chris Wilsonf2123812017-10-16 12:40:37 +01005706 spin_lock(&dev_priv->mm.obj_lock);
Chris Wilson7aab2d52016-09-09 20:02:18 +01005707 for (p = phases; *p; p++) {
Chris Wilsonf2123812017-10-16 12:40:37 +01005708 list_for_each_entry(obj, *p, mm.link)
Chris Wilsone27ab732017-06-15 13:38:49 +01005709 __start_cpu_write(obj);
Chris Wilson461fb992016-05-14 07:26:33 +01005710 }
Chris Wilsonf2123812017-10-16 12:40:37 +01005711 spin_unlock(&dev_priv->mm.obj_lock);
Chris Wilson461fb992016-05-14 07:26:33 +01005712
5713 return 0;
5714}
5715
Chris Wilsonf787a5f2010-09-24 16:02:42 +01005716void i915_gem_release(struct drm_device *dev, struct drm_file *file)
Eric Anholtb9624422009-06-03 07:27:35 +00005717{
Chris Wilsonf787a5f2010-09-24 16:02:42 +01005718 struct drm_i915_file_private *file_priv = file->driver_priv;
Chris Wilsone61e0f52018-02-21 09:56:36 +00005719 struct i915_request *request;
Eric Anholtb9624422009-06-03 07:27:35 +00005720
5721 /* Clean up our request list when the client is going away, so that
5722 * later retire_requests won't dereference our soon-to-be-gone
5723 * file_priv.
5724 */
Chris Wilson1c255952010-09-26 11:03:27 +01005725 spin_lock(&file_priv->mm.lock);
Chris Wilsonc8659ef2017-03-02 12:25:25 +00005726 list_for_each_entry(request, &file_priv->mm.request_list, client_link)
Chris Wilsonf787a5f2010-09-24 16:02:42 +01005727 request->file_priv = NULL;
Chris Wilson1c255952010-09-26 11:03:27 +01005728 spin_unlock(&file_priv->mm.lock);
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005729}
5730
Chris Wilson829a0af2017-06-20 12:05:45 +01005731int i915_gem_open(struct drm_i915_private *i915, struct drm_file *file)
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005732{
5733 struct drm_i915_file_private *file_priv;
Ben Widawskye422b882013-12-06 14:10:58 -08005734 int ret;
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005735
Chris Wilsonc4c29d72016-11-09 10:45:07 +00005736 DRM_DEBUG("\n");
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005737
5738 file_priv = kzalloc(sizeof(*file_priv), GFP_KERNEL);
5739 if (!file_priv)
5740 return -ENOMEM;
5741
5742 file->driver_priv = file_priv;
Chris Wilson829a0af2017-06-20 12:05:45 +01005743 file_priv->dev_priv = i915;
Chris Wilsonab0e7ff2014-02-25 17:11:24 +02005744 file_priv->file = file;
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005745
5746 spin_lock_init(&file_priv->mm.lock);
5747 INIT_LIST_HEAD(&file_priv->mm.request_list);
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005748
Chris Wilsonc80ff162016-07-27 09:07:27 +01005749 file_priv->bsd_engine = -1;
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00005750
Chris Wilson829a0af2017-06-20 12:05:45 +01005751 ret = i915_gem_context_open(i915, file);
Ben Widawskye422b882013-12-06 14:10:58 -08005752 if (ret)
5753 kfree(file_priv);
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005754
Ben Widawskye422b882013-12-06 14:10:58 -08005755 return ret;
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005756}
5757
Daniel Vetterb680c372014-09-19 18:27:27 +02005758/**
5759 * i915_gem_track_fb - update frontbuffer tracking
Geliang Tangd9072a32015-09-15 05:58:44 -07005760 * @old: current GEM buffer for the frontbuffer slots
5761 * @new: new GEM buffer for the frontbuffer slots
5762 * @frontbuffer_bits: bitmask of frontbuffer slots
Daniel Vetterb680c372014-09-19 18:27:27 +02005763 *
5764 * This updates the frontbuffer tracking bits @frontbuffer_bits by clearing them
5765 * from @old and setting them in @new. Both @old and @new can be NULL.
5766 */
Daniel Vettera071fa02014-06-18 23:28:09 +02005767void i915_gem_track_fb(struct drm_i915_gem_object *old,
5768 struct drm_i915_gem_object *new,
5769 unsigned frontbuffer_bits)
5770{
Chris Wilsonfaf5bf02016-08-04 16:32:37 +01005771 /* Control of individual bits within the mask are guarded by
5772 * the owning plane->mutex, i.e. we can never see concurrent
5773 * manipulation of individual bits. But since the bitfield as a whole
5774 * is updated using RMW, we need to use atomics in order to update
5775 * the bits.
5776 */
5777 BUILD_BUG_ON(INTEL_FRONTBUFFER_BITS_PER_PIPE * I915_MAX_PIPES >
5778 sizeof(atomic_t) * BITS_PER_BYTE);
5779
Daniel Vettera071fa02014-06-18 23:28:09 +02005780 if (old) {
Chris Wilsonfaf5bf02016-08-04 16:32:37 +01005781 WARN_ON(!(atomic_read(&old->frontbuffer_bits) & frontbuffer_bits));
5782 atomic_andnot(frontbuffer_bits, &old->frontbuffer_bits);
Daniel Vettera071fa02014-06-18 23:28:09 +02005783 }
5784
5785 if (new) {
Chris Wilsonfaf5bf02016-08-04 16:32:37 +01005786 WARN_ON(atomic_read(&new->frontbuffer_bits) & frontbuffer_bits);
5787 atomic_or(frontbuffer_bits, &new->frontbuffer_bits);
Daniel Vettera071fa02014-06-18 23:28:09 +02005788 }
5789}
5790
Dave Gordonea702992015-07-09 19:29:02 +01005791/* Allocate a new GEM object and fill it with the supplied data */
5792struct drm_i915_gem_object *
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +00005793i915_gem_object_create_from_data(struct drm_i915_private *dev_priv,
Dave Gordonea702992015-07-09 19:29:02 +01005794 const void *data, size_t size)
5795{
5796 struct drm_i915_gem_object *obj;
Chris Wilsonbe062fa2017-03-17 19:46:48 +00005797 struct file *file;
5798 size_t offset;
5799 int err;
Dave Gordonea702992015-07-09 19:29:02 +01005800
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +00005801 obj = i915_gem_object_create(dev_priv, round_up(size, PAGE_SIZE));
Chris Wilsonfe3db792016-04-25 13:32:13 +01005802 if (IS_ERR(obj))
Dave Gordonea702992015-07-09 19:29:02 +01005803 return obj;
5804
Christian Königc0a51fd2018-02-16 13:43:38 +01005805 GEM_BUG_ON(obj->write_domain != I915_GEM_DOMAIN_CPU);
Dave Gordonea702992015-07-09 19:29:02 +01005806
Chris Wilsonbe062fa2017-03-17 19:46:48 +00005807 file = obj->base.filp;
5808 offset = 0;
5809 do {
5810 unsigned int len = min_t(typeof(size), size, PAGE_SIZE);
5811 struct page *page;
5812 void *pgdata, *vaddr;
Dave Gordonea702992015-07-09 19:29:02 +01005813
Chris Wilsonbe062fa2017-03-17 19:46:48 +00005814 err = pagecache_write_begin(file, file->f_mapping,
5815 offset, len, 0,
5816 &page, &pgdata);
5817 if (err < 0)
5818 goto fail;
Dave Gordonea702992015-07-09 19:29:02 +01005819
Chris Wilsonbe062fa2017-03-17 19:46:48 +00005820 vaddr = kmap(page);
5821 memcpy(vaddr, data, len);
5822 kunmap(page);
5823
5824 err = pagecache_write_end(file, file->f_mapping,
5825 offset, len, len,
5826 page, pgdata);
5827 if (err < 0)
5828 goto fail;
5829
5830 size -= len;
5831 data += len;
5832 offset += len;
5833 } while (size);
Dave Gordonea702992015-07-09 19:29:02 +01005834
5835 return obj;
5836
5837fail:
Chris Wilsonf8c417c2016-07-20 13:31:53 +01005838 i915_gem_object_put(obj);
Chris Wilsonbe062fa2017-03-17 19:46:48 +00005839 return ERR_PTR(err);
Dave Gordonea702992015-07-09 19:29:02 +01005840}
Chris Wilson96d77632016-10-28 13:58:33 +01005841
5842struct scatterlist *
5843i915_gem_object_get_sg(struct drm_i915_gem_object *obj,
5844 unsigned int n,
5845 unsigned int *offset)
5846{
Chris Wilsona4f5ea62016-10-28 13:58:35 +01005847 struct i915_gem_object_page_iter *iter = &obj->mm.get_page;
Chris Wilson96d77632016-10-28 13:58:33 +01005848 struct scatterlist *sg;
5849 unsigned int idx, count;
5850
5851 might_sleep();
5852 GEM_BUG_ON(n >= obj->base.size >> PAGE_SHIFT);
Chris Wilsona4f5ea62016-10-28 13:58:35 +01005853 GEM_BUG_ON(!i915_gem_object_has_pinned_pages(obj));
Chris Wilson96d77632016-10-28 13:58:33 +01005854
5855 /* As we iterate forward through the sg, we record each entry in a
5856 * radixtree for quick repeated (backwards) lookups. If we have seen
5857 * this index previously, we will have an entry for it.
5858 *
5859 * Initial lookup is O(N), but this is amortized to O(1) for
5860 * sequential page access (where each new request is consecutive
5861 * to the previous one). Repeated lookups are O(lg(obj->base.size)),
5862 * i.e. O(1) with a large constant!
5863 */
5864 if (n < READ_ONCE(iter->sg_idx))
5865 goto lookup;
5866
5867 mutex_lock(&iter->lock);
5868
5869 /* We prefer to reuse the last sg so that repeated lookup of this
5870 * (or the subsequent) sg are fast - comparing against the last
5871 * sg is faster than going through the radixtree.
5872 */
5873
5874 sg = iter->sg_pos;
5875 idx = iter->sg_idx;
5876 count = __sg_page_count(sg);
5877
5878 while (idx + count <= n) {
5879 unsigned long exception, i;
5880 int ret;
5881
5882 /* If we cannot allocate and insert this entry, or the
5883 * individual pages from this range, cancel updating the
5884 * sg_idx so that on this lookup we are forced to linearly
5885 * scan onwards, but on future lookups we will try the
5886 * insertion again (in which case we need to be careful of
5887 * the error return reporting that we have already inserted
5888 * this index).
5889 */
5890 ret = radix_tree_insert(&iter->radix, idx, sg);
5891 if (ret && ret != -EEXIST)
5892 goto scan;
5893
5894 exception =
5895 RADIX_TREE_EXCEPTIONAL_ENTRY |
5896 idx << RADIX_TREE_EXCEPTIONAL_SHIFT;
5897 for (i = 1; i < count; i++) {
5898 ret = radix_tree_insert(&iter->radix, idx + i,
5899 (void *)exception);
5900 if (ret && ret != -EEXIST)
5901 goto scan;
5902 }
5903
5904 idx += count;
5905 sg = ____sg_next(sg);
5906 count = __sg_page_count(sg);
5907 }
5908
5909scan:
5910 iter->sg_pos = sg;
5911 iter->sg_idx = idx;
5912
5913 mutex_unlock(&iter->lock);
5914
5915 if (unlikely(n < idx)) /* insertion completed by another thread */
5916 goto lookup;
5917
5918 /* In case we failed to insert the entry into the radixtree, we need
5919 * to look beyond the current sg.
5920 */
5921 while (idx + count <= n) {
5922 idx += count;
5923 sg = ____sg_next(sg);
5924 count = __sg_page_count(sg);
5925 }
5926
5927 *offset = n - idx;
5928 return sg;
5929
5930lookup:
5931 rcu_read_lock();
5932
5933 sg = radix_tree_lookup(&iter->radix, n);
5934 GEM_BUG_ON(!sg);
5935
5936 /* If this index is in the middle of multi-page sg entry,
5937 * the radixtree will contain an exceptional entry that points
5938 * to the start of that range. We will return the pointer to
5939 * the base page and the offset of this page within the
5940 * sg entry's range.
5941 */
5942 *offset = 0;
5943 if (unlikely(radix_tree_exception(sg))) {
5944 unsigned long base =
5945 (unsigned long)sg >> RADIX_TREE_EXCEPTIONAL_SHIFT;
5946
5947 sg = radix_tree_lookup(&iter->radix, base);
5948 GEM_BUG_ON(!sg);
5949
5950 *offset = n - base;
5951 }
5952
5953 rcu_read_unlock();
5954
5955 return sg;
5956}
5957
5958struct page *
5959i915_gem_object_get_page(struct drm_i915_gem_object *obj, unsigned int n)
5960{
5961 struct scatterlist *sg;
5962 unsigned int offset;
5963
5964 GEM_BUG_ON(!i915_gem_object_has_struct_page(obj));
5965
5966 sg = i915_gem_object_get_sg(obj, n, &offset);
5967 return nth_page(sg_page(sg), offset);
5968}
5969
5970/* Like i915_gem_object_get_page(), but mark the returned page dirty */
5971struct page *
5972i915_gem_object_get_dirty_page(struct drm_i915_gem_object *obj,
5973 unsigned int n)
5974{
5975 struct page *page;
5976
5977 page = i915_gem_object_get_page(obj, n);
Chris Wilsona4f5ea62016-10-28 13:58:35 +01005978 if (!obj->mm.dirty)
Chris Wilson96d77632016-10-28 13:58:33 +01005979 set_page_dirty(page);
5980
5981 return page;
5982}
5983
5984dma_addr_t
5985i915_gem_object_get_dma_address(struct drm_i915_gem_object *obj,
5986 unsigned long n)
5987{
5988 struct scatterlist *sg;
5989 unsigned int offset;
5990
5991 sg = i915_gem_object_get_sg(obj, n, &offset);
5992 return sg_dma_address(sg) + (offset << PAGE_SHIFT);
5993}
Chris Wilson935a2f72017-02-13 17:15:13 +00005994
Chris Wilson8eeb7902017-07-26 19:16:01 +01005995int i915_gem_object_attach_phys(struct drm_i915_gem_object *obj, int align)
5996{
5997 struct sg_table *pages;
5998 int err;
5999
6000 if (align > obj->base.size)
6001 return -EINVAL;
6002
6003 if (obj->ops == &i915_gem_phys_ops)
6004 return 0;
6005
6006 if (obj->ops != &i915_gem_object_ops)
6007 return -EINVAL;
6008
6009 err = i915_gem_object_unbind(obj);
6010 if (err)
6011 return err;
6012
6013 mutex_lock(&obj->mm.lock);
6014
6015 if (obj->mm.madv != I915_MADV_WILLNEED) {
6016 err = -EFAULT;
6017 goto err_unlock;
6018 }
6019
6020 if (obj->mm.quirked) {
6021 err = -EFAULT;
6022 goto err_unlock;
6023 }
6024
6025 if (obj->mm.mapping) {
6026 err = -EBUSY;
6027 goto err_unlock;
6028 }
6029
Chris Wilsonf2123812017-10-16 12:40:37 +01006030 pages = fetch_and_zero(&obj->mm.pages);
6031 if (pages) {
6032 struct drm_i915_private *i915 = to_i915(obj->base.dev);
6033
6034 __i915_gem_object_reset_page_iter(obj);
6035
6036 spin_lock(&i915->mm.obj_lock);
6037 list_del(&obj->mm.link);
6038 spin_unlock(&i915->mm.obj_lock);
6039 }
6040
Chris Wilson8eeb7902017-07-26 19:16:01 +01006041 obj->ops = &i915_gem_phys_ops;
6042
Chris Wilson8fb6a5d2017-07-26 19:16:02 +01006043 err = ____i915_gem_object_get_pages(obj);
Chris Wilson8eeb7902017-07-26 19:16:01 +01006044 if (err)
6045 goto err_xfer;
6046
6047 /* Perma-pin (until release) the physical set of pages */
6048 __i915_gem_object_pin_pages(obj);
6049
6050 if (!IS_ERR_OR_NULL(pages))
6051 i915_gem_object_ops.put_pages(obj, pages);
6052 mutex_unlock(&obj->mm.lock);
6053 return 0;
6054
6055err_xfer:
6056 obj->ops = &i915_gem_object_ops;
6057 obj->mm.pages = pages;
6058err_unlock:
6059 mutex_unlock(&obj->mm.lock);
6060 return err;
6061}
6062
Chris Wilson935a2f72017-02-13 17:15:13 +00006063#if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
6064#include "selftests/scatterlist.c"
Chris Wilson66d9cb52017-02-13 17:15:17 +00006065#include "selftests/mock_gem_device.c"
Chris Wilson44653982017-02-13 17:15:20 +00006066#include "selftests/huge_gem_object.c"
Matthew Auld40498662017-10-06 23:18:29 +01006067#include "selftests/huge_pages.c"
Chris Wilson8335fd62017-02-13 17:15:28 +00006068#include "selftests/i915_gem_object.c"
Chris Wilson17059452017-02-13 17:15:32 +00006069#include "selftests/i915_gem_coherency.c"
Chris Wilson935a2f72017-02-13 17:15:13 +00006070#endif