blob: 4c9d2a6f7d28faf300eedfc497c424b9c793c76f [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 Wilson6b5e90f2016-11-14 20:41:05 +0000567static void __fence_set_priority(struct dma_fence *fence, int prio)
568{
Chris Wilsone61e0f52018-02-21 09:56:36 +0000569 struct i915_request *rq;
Chris Wilson6b5e90f2016-11-14 20:41:05 +0000570 struct intel_engine_cs *engine;
571
Chris Wilsonc218ee02018-01-06 10:56:18 +0000572 if (dma_fence_is_signaled(fence) || !dma_fence_is_i915(fence))
Chris Wilson6b5e90f2016-11-14 20:41:05 +0000573 return;
574
575 rq = to_request(fence);
576 engine = rq->engine;
Chris Wilson6b5e90f2016-11-14 20:41:05 +0000577
Chris Wilson47650db2018-03-07 13:42:25 +0000578 rcu_read_lock();
579 if (engine->schedule)
580 engine->schedule(rq, prio);
581 rcu_read_unlock();
Chris Wilson6b5e90f2016-11-14 20:41:05 +0000582}
583
584static void fence_set_priority(struct dma_fence *fence, int prio)
585{
586 /* Recurse once into a fence-array */
587 if (dma_fence_is_array(fence)) {
588 struct dma_fence_array *array = to_dma_fence_array(fence);
589 int i;
590
591 for (i = 0; i < array->num_fences; i++)
592 __fence_set_priority(array->fences[i], prio);
593 } else {
594 __fence_set_priority(fence, prio);
595 }
596}
597
598int
599i915_gem_object_wait_priority(struct drm_i915_gem_object *obj,
600 unsigned int flags,
601 int prio)
602{
603 struct dma_fence *excl;
604
605 if (flags & I915_WAIT_ALL) {
606 struct dma_fence **shared;
607 unsigned int count, i;
608 int ret;
609
610 ret = reservation_object_get_fences_rcu(obj->resv,
611 &excl, &count, &shared);
612 if (ret)
613 return ret;
614
615 for (i = 0; i < count; i++) {
616 fence_set_priority(shared[i], prio);
617 dma_fence_put(shared[i]);
618 }
619
620 kfree(shared);
621 } else {
622 excl = reservation_object_get_excl_rcu(obj->resv);
623 }
624
625 if (excl) {
626 fence_set_priority(excl, prio);
627 dma_fence_put(excl);
628 }
629 return 0;
630}
631
Chris Wilson00e60f22016-08-04 16:32:40 +0100632/**
Chris Wilsone95433c2016-10-28 13:58:27 +0100633 * Waits for rendering to the object to be completed
Chris Wilson00e60f22016-08-04 16:32:40 +0100634 * @obj: i915 gem object
Chris Wilsone95433c2016-10-28 13:58:27 +0100635 * @flags: how to wait (under a lock, for all rendering or just for writes etc)
636 * @timeout: how long to wait
Chris Wilsona0a8b1c2017-11-09 14:06:44 +0000637 * @rps_client: client (user process) to charge for any waitboosting
Chris Wilson00e60f22016-08-04 16:32:40 +0100638 */
639int
Chris Wilsone95433c2016-10-28 13:58:27 +0100640i915_gem_object_wait(struct drm_i915_gem_object *obj,
641 unsigned int flags,
642 long timeout,
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +0100643 struct intel_rps_client *rps_client)
Chris Wilson00e60f22016-08-04 16:32:40 +0100644{
Chris Wilsone95433c2016-10-28 13:58:27 +0100645 might_sleep();
646#if IS_ENABLED(CONFIG_LOCKDEP)
647 GEM_BUG_ON(debug_locks &&
648 !!lockdep_is_held(&obj->base.dev->struct_mutex) !=
649 !!(flags & I915_WAIT_LOCKED));
650#endif
651 GEM_BUG_ON(timeout < 0);
Chris Wilson00e60f22016-08-04 16:32:40 +0100652
Chris Wilsond07f0e52016-10-28 13:58:44 +0100653 timeout = i915_gem_object_wait_reservation(obj->resv,
654 flags, timeout,
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +0100655 rps_client);
Chris Wilsone95433c2016-10-28 13:58:27 +0100656 return timeout < 0 ? timeout : 0;
Chris Wilson00e60f22016-08-04 16:32:40 +0100657}
658
659static struct intel_rps_client *to_rps_client(struct drm_file *file)
660{
661 struct drm_i915_file_private *fpriv = file->driver_priv;
662
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +0100663 return &fpriv->rps_client;
Chris Wilson00e60f22016-08-04 16:32:40 +0100664}
665
Chris Wilson00731152014-05-21 12:42:56 +0100666static int
667i915_gem_phys_pwrite(struct drm_i915_gem_object *obj,
668 struct drm_i915_gem_pwrite *args,
Chris Wilson03ac84f2016-10-28 13:58:36 +0100669 struct drm_file *file)
Chris Wilson00731152014-05-21 12:42:56 +0100670{
Chris Wilson00731152014-05-21 12:42:56 +0100671 void *vaddr = obj->phys_handle->vaddr + args->offset;
Gustavo Padovan3ed605b2016-04-26 12:32:27 -0300672 char __user *user_data = u64_to_user_ptr(args->data_ptr);
Chris Wilson6a2c4232014-11-04 04:51:40 -0800673
674 /* We manually control the domain here and pretend that it
675 * remains coherent i.e. in the GTT domain, like shmem_pwrite.
676 */
Rodrigo Vivi77a0d1c2015-06-18 11:43:24 -0700677 intel_fb_obj_invalidate(obj, ORIGIN_CPU);
Chris Wilson10466d22017-01-06 15:22:38 +0000678 if (copy_from_user(vaddr, user_data, args->size))
679 return -EFAULT;
Chris Wilson00731152014-05-21 12:42:56 +0100680
Chris Wilson6a2c4232014-11-04 04:51:40 -0800681 drm_clflush_virt_range(vaddr, args->size);
Chris Wilson10466d22017-01-06 15:22:38 +0000682 i915_gem_chipset_flush(to_i915(obj->base.dev));
Paulo Zanoni063e4e62015-02-13 17:23:45 -0200683
Chris Wilsond59b21e2017-02-22 11:40:49 +0000684 intel_fb_obj_flush(obj, ORIGIN_CPU);
Chris Wilson10466d22017-01-06 15:22:38 +0000685 return 0;
Chris Wilson00731152014-05-21 12:42:56 +0100686}
687
Tvrtko Ursulin187685c2016-12-01 14:16:36 +0000688void *i915_gem_object_alloc(struct drm_i915_private *dev_priv)
Chris Wilson42dcedd2012-11-15 11:32:30 +0000689{
Chris Wilsonefab6d82015-04-07 16:20:57 +0100690 return kmem_cache_zalloc(dev_priv->objects, GFP_KERNEL);
Chris Wilson42dcedd2012-11-15 11:32:30 +0000691}
692
693void i915_gem_object_free(struct drm_i915_gem_object *obj)
694{
Chris Wilsonfac5e232016-07-04 11:34:36 +0100695 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
Chris Wilsonefab6d82015-04-07 16:20:57 +0100696 kmem_cache_free(dev_priv->objects, obj);
Chris Wilson42dcedd2012-11-15 11:32:30 +0000697}
698
Dave Airlieff72145b2011-02-07 12:16:14 +1000699static int
700i915_gem_create(struct drm_file *file,
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +0000701 struct drm_i915_private *dev_priv,
Dave Airlieff72145b2011-02-07 12:16:14 +1000702 uint64_t size,
703 uint32_t *handle_p)
Eric Anholt673a3942008-07-30 12:06:12 -0700704{
Chris Wilson05394f32010-11-08 19:18:58 +0000705 struct drm_i915_gem_object *obj;
Pekka Paalanena1a2d1d2009-08-23 12:40:55 +0300706 int ret;
707 u32 handle;
Eric Anholt673a3942008-07-30 12:06:12 -0700708
Dave Airlieff72145b2011-02-07 12:16:14 +1000709 size = roundup(size, PAGE_SIZE);
Chris Wilson8ffc0242011-09-14 14:14:28 +0200710 if (size == 0)
711 return -EINVAL;
Eric Anholt673a3942008-07-30 12:06:12 -0700712
713 /* Allocate the new object */
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +0000714 obj = i915_gem_object_create(dev_priv, size);
Chris Wilsonfe3db792016-04-25 13:32:13 +0100715 if (IS_ERR(obj))
716 return PTR_ERR(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700717
Chris Wilson05394f32010-11-08 19:18:58 +0000718 ret = drm_gem_handle_create(file, &obj->base, &handle);
Chris Wilson202f2fe2010-10-14 13:20:40 +0100719 /* drop reference from allocate - handle holds it now */
Chris Wilsonf0cd5182016-10-28 13:58:43 +0100720 i915_gem_object_put(obj);
Daniel Vetterd861e332013-07-24 23:25:03 +0200721 if (ret)
722 return ret;
Chris Wilson202f2fe2010-10-14 13:20:40 +0100723
Dave Airlieff72145b2011-02-07 12:16:14 +1000724 *handle_p = handle;
Eric Anholt673a3942008-07-30 12:06:12 -0700725 return 0;
726}
727
Dave Airlieff72145b2011-02-07 12:16:14 +1000728int
729i915_gem_dumb_create(struct drm_file *file,
730 struct drm_device *dev,
731 struct drm_mode_create_dumb *args)
732{
733 /* have to work out size/pitch and return them */
Paulo Zanonide45eaf2013-10-18 18:48:24 -0300734 args->pitch = ALIGN(args->width * DIV_ROUND_UP(args->bpp, 8), 64);
Dave Airlieff72145b2011-02-07 12:16:14 +1000735 args->size = args->pitch * args->height;
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +0000736 return i915_gem_create(file, to_i915(dev),
Dave Airlieda6b51d2014-12-24 13:11:17 +1000737 args->size, &args->handle);
Dave Airlieff72145b2011-02-07 12:16:14 +1000738}
739
Chris Wilsone27ab732017-06-15 13:38:49 +0100740static bool gpu_write_needs_clflush(struct drm_i915_gem_object *obj)
741{
742 return !(obj->cache_level == I915_CACHE_NONE ||
743 obj->cache_level == I915_CACHE_WT);
744}
745
Dave Airlieff72145b2011-02-07 12:16:14 +1000746/**
747 * Creates a new mm object and returns a handle to it.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +0100748 * @dev: drm device pointer
749 * @data: ioctl data blob
750 * @file: drm file pointer
Dave Airlieff72145b2011-02-07 12:16:14 +1000751 */
752int
753i915_gem_create_ioctl(struct drm_device *dev, void *data,
754 struct drm_file *file)
755{
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +0000756 struct drm_i915_private *dev_priv = to_i915(dev);
Dave Airlieff72145b2011-02-07 12:16:14 +1000757 struct drm_i915_gem_create *args = data;
Daniel Vetter63ed2cb2012-04-23 16:50:50 +0200758
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +0000759 i915_gem_flush_free_objects(dev_priv);
Chris Wilsonfbbd37b2016-10-28 13:58:42 +0100760
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +0000761 return i915_gem_create(file, dev_priv,
Dave Airlieda6b51d2014-12-24 13:11:17 +1000762 args->size, &args->handle);
Dave Airlieff72145b2011-02-07 12:16:14 +1000763}
764
Chris Wilsonef749212017-04-12 12:01:10 +0100765static inline enum fb_op_origin
766fb_write_origin(struct drm_i915_gem_object *obj, unsigned int domain)
767{
768 return (domain == I915_GEM_DOMAIN_GTT ?
769 obj->frontbuffer_ggtt_origin : ORIGIN_CPU);
770}
771
Chris Wilson71253972017-12-06 12:49:14 +0000772void i915_gem_flush_ggtt_writes(struct drm_i915_private *dev_priv)
Chris Wilsonef749212017-04-12 12:01:10 +0100773{
Chris Wilson71253972017-12-06 12:49:14 +0000774 /*
775 * No actual flushing is required for the GTT write domain for reads
776 * from the GTT domain. Writes to it "immediately" go to main memory
777 * as far as we know, so there's no chipset flush. It also doesn't
778 * land in the GPU render cache.
Chris Wilsonef749212017-04-12 12:01:10 +0100779 *
780 * However, we do have to enforce the order so that all writes through
781 * the GTT land before any writes to the device, such as updates to
782 * the GATT itself.
783 *
784 * We also have to wait a bit for the writes to land from the GTT.
785 * An uncached read (i.e. mmio) seems to be ideal for the round-trip
786 * timing. This issue has only been observed when switching quickly
787 * between GTT writes and CPU reads from inside the kernel on recent hw,
788 * and it appears to only affect discrete GTT blocks (i.e. on LLC
Chris Wilson71253972017-12-06 12:49:14 +0000789 * system agents we cannot reproduce this behaviour, until Cannonlake
790 * that was!).
Chris Wilsonef749212017-04-12 12:01:10 +0100791 */
Chris Wilson71253972017-12-06 12:49:14 +0000792
Chris Wilsonef749212017-04-12 12:01:10 +0100793 wmb();
794
Chris Wilson71253972017-12-06 12:49:14 +0000795 intel_runtime_pm_get(dev_priv);
796 spin_lock_irq(&dev_priv->uncore.lock);
797
798 POSTING_READ_FW(RING_HEAD(RENDER_RING_BASE));
799
800 spin_unlock_irq(&dev_priv->uncore.lock);
801 intel_runtime_pm_put(dev_priv);
802}
803
804static void
805flush_write_domain(struct drm_i915_gem_object *obj, unsigned int flush_domains)
806{
807 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
808 struct i915_vma *vma;
809
Christian Königc0a51fd2018-02-16 13:43:38 +0100810 if (!(obj->write_domain & flush_domains))
Chris Wilson71253972017-12-06 12:49:14 +0000811 return;
812
Christian Königc0a51fd2018-02-16 13:43:38 +0100813 switch (obj->write_domain) {
Chris Wilsonef749212017-04-12 12:01:10 +0100814 case I915_GEM_DOMAIN_GTT:
Chris Wilson71253972017-12-06 12:49:14 +0000815 i915_gem_flush_ggtt_writes(dev_priv);
Chris Wilsonef749212017-04-12 12:01:10 +0100816
817 intel_fb_obj_flush(obj,
818 fb_write_origin(obj, I915_GEM_DOMAIN_GTT));
Chris Wilson71253972017-12-06 12:49:14 +0000819
Chris Wilsone2189dd2017-12-07 21:14:07 +0000820 for_each_ggtt_vma(vma, obj) {
Chris Wilson71253972017-12-06 12:49:14 +0000821 if (vma->iomap)
822 continue;
823
824 i915_vma_unset_ggtt_write(vma);
825 }
Chris Wilsonef749212017-04-12 12:01:10 +0100826 break;
827
828 case I915_GEM_DOMAIN_CPU:
829 i915_gem_clflush_object(obj, I915_CLFLUSH_SYNC);
830 break;
Chris Wilsone27ab732017-06-15 13:38:49 +0100831
832 case I915_GEM_DOMAIN_RENDER:
833 if (gpu_write_needs_clflush(obj))
834 obj->cache_dirty = true;
835 break;
Chris Wilsonef749212017-04-12 12:01:10 +0100836 }
837
Christian Königc0a51fd2018-02-16 13:43:38 +0100838 obj->write_domain = 0;
Chris Wilsonef749212017-04-12 12:01:10 +0100839}
840
Daniel Vetter8c599672011-12-14 13:57:31 +0100841static inline int
Daniel Vetter8461d222011-12-14 13:57:32 +0100842__copy_to_user_swizzled(char __user *cpu_vaddr,
843 const char *gpu_vaddr, int gpu_offset,
844 int length)
845{
846 int ret, cpu_offset = 0;
847
848 while (length > 0) {
849 int cacheline_end = ALIGN(gpu_offset + 1, 64);
850 int this_length = min(cacheline_end - gpu_offset, length);
851 int swizzled_gpu_offset = gpu_offset ^ 64;
852
853 ret = __copy_to_user(cpu_vaddr + cpu_offset,
854 gpu_vaddr + swizzled_gpu_offset,
855 this_length);
856 if (ret)
857 return ret + length;
858
859 cpu_offset += this_length;
860 gpu_offset += this_length;
861 length -= this_length;
862 }
863
864 return 0;
865}
866
867static inline int
Ben Widawsky4f0c7cf2012-04-16 14:07:47 -0700868__copy_from_user_swizzled(char *gpu_vaddr, int gpu_offset,
869 const char __user *cpu_vaddr,
Daniel Vetter8c599672011-12-14 13:57:31 +0100870 int length)
871{
872 int ret, cpu_offset = 0;
873
874 while (length > 0) {
875 int cacheline_end = ALIGN(gpu_offset + 1, 64);
876 int this_length = min(cacheline_end - gpu_offset, length);
877 int swizzled_gpu_offset = gpu_offset ^ 64;
878
879 ret = __copy_from_user(gpu_vaddr + swizzled_gpu_offset,
880 cpu_vaddr + cpu_offset,
881 this_length);
882 if (ret)
883 return ret + length;
884
885 cpu_offset += this_length;
886 gpu_offset += this_length;
887 length -= this_length;
888 }
889
890 return 0;
891}
892
Brad Volkin4c914c02014-02-18 10:15:45 -0800893/*
894 * Pins the specified object's pages and synchronizes the object with
895 * GPU accesses. Sets needs_clflush to non-zero if the caller should
896 * flush the object from the CPU cache.
897 */
898int i915_gem_obj_prepare_shmem_read(struct drm_i915_gem_object *obj,
Chris Wilson43394c72016-08-18 17:16:47 +0100899 unsigned int *needs_clflush)
Brad Volkin4c914c02014-02-18 10:15:45 -0800900{
901 int ret;
902
Chris Wilsone95433c2016-10-28 13:58:27 +0100903 lockdep_assert_held(&obj->base.dev->struct_mutex);
Brad Volkin4c914c02014-02-18 10:15:45 -0800904
Chris Wilsone95433c2016-10-28 13:58:27 +0100905 *needs_clflush = 0;
Chris Wilson43394c72016-08-18 17:16:47 +0100906 if (!i915_gem_object_has_struct_page(obj))
907 return -ENODEV;
Brad Volkin4c914c02014-02-18 10:15:45 -0800908
Chris Wilsone95433c2016-10-28 13:58:27 +0100909 ret = i915_gem_object_wait(obj,
910 I915_WAIT_INTERRUPTIBLE |
911 I915_WAIT_LOCKED,
912 MAX_SCHEDULE_TIMEOUT,
913 NULL);
Chris Wilsonc13d87e2016-07-20 09:21:15 +0100914 if (ret)
915 return ret;
916
Chris Wilsona4f5ea62016-10-28 13:58:35 +0100917 ret = i915_gem_object_pin_pages(obj);
Chris Wilson97649512016-08-18 17:16:50 +0100918 if (ret)
919 return ret;
920
Chris Wilsonb8f55be2017-08-11 12:11:16 +0100921 if (obj->cache_coherent & I915_BO_CACHE_COHERENT_FOR_READ ||
922 !static_cpu_has(X86_FEATURE_CLFLUSH)) {
Chris Wilson7f5f95d2017-03-10 00:09:42 +0000923 ret = i915_gem_object_set_to_cpu_domain(obj, false);
924 if (ret)
925 goto err_unpin;
926 else
927 goto out;
928 }
929
Chris Wilsonef749212017-04-12 12:01:10 +0100930 flush_write_domain(obj, ~I915_GEM_DOMAIN_CPU);
Chris Wilsona314d5c2016-08-18 17:16:48 +0100931
Chris Wilson43394c72016-08-18 17:16:47 +0100932 /* If we're not in the cpu read domain, set ourself into the gtt
933 * read domain and manually flush cachelines (if required). This
934 * optimizes for the case when the gpu will dirty the data
935 * anyway again before the next pread happens.
936 */
Chris Wilsone27ab732017-06-15 13:38:49 +0100937 if (!obj->cache_dirty &&
Christian Königc0a51fd2018-02-16 13:43:38 +0100938 !(obj->read_domains & I915_GEM_DOMAIN_CPU))
Chris Wilson7f5f95d2017-03-10 00:09:42 +0000939 *needs_clflush = CLFLUSH_BEFORE;
Brad Volkin4c914c02014-02-18 10:15:45 -0800940
Chris Wilson7f5f95d2017-03-10 00:09:42 +0000941out:
Chris Wilson97649512016-08-18 17:16:50 +0100942 /* return with the pages pinned */
Chris Wilson43394c72016-08-18 17:16:47 +0100943 return 0;
Chris Wilson97649512016-08-18 17:16:50 +0100944
945err_unpin:
946 i915_gem_object_unpin_pages(obj);
947 return ret;
Chris Wilson43394c72016-08-18 17:16:47 +0100948}
949
950int i915_gem_obj_prepare_shmem_write(struct drm_i915_gem_object *obj,
951 unsigned int *needs_clflush)
952{
953 int ret;
954
Chris Wilsone95433c2016-10-28 13:58:27 +0100955 lockdep_assert_held(&obj->base.dev->struct_mutex);
956
Chris Wilson43394c72016-08-18 17:16:47 +0100957 *needs_clflush = 0;
958 if (!i915_gem_object_has_struct_page(obj))
959 return -ENODEV;
960
Chris Wilsone95433c2016-10-28 13:58:27 +0100961 ret = i915_gem_object_wait(obj,
962 I915_WAIT_INTERRUPTIBLE |
963 I915_WAIT_LOCKED |
964 I915_WAIT_ALL,
965 MAX_SCHEDULE_TIMEOUT,
966 NULL);
Chris Wilson43394c72016-08-18 17:16:47 +0100967 if (ret)
968 return ret;
969
Chris Wilsona4f5ea62016-10-28 13:58:35 +0100970 ret = i915_gem_object_pin_pages(obj);
Chris Wilson97649512016-08-18 17:16:50 +0100971 if (ret)
972 return ret;
973
Chris Wilsonb8f55be2017-08-11 12:11:16 +0100974 if (obj->cache_coherent & I915_BO_CACHE_COHERENT_FOR_WRITE ||
975 !static_cpu_has(X86_FEATURE_CLFLUSH)) {
Chris Wilson7f5f95d2017-03-10 00:09:42 +0000976 ret = i915_gem_object_set_to_cpu_domain(obj, true);
977 if (ret)
978 goto err_unpin;
979 else
980 goto out;
981 }
982
Chris Wilsonef749212017-04-12 12:01:10 +0100983 flush_write_domain(obj, ~I915_GEM_DOMAIN_CPU);
Chris Wilsona314d5c2016-08-18 17:16:48 +0100984
Chris Wilson43394c72016-08-18 17:16:47 +0100985 /* If we're not in the cpu write domain, set ourself into the
986 * gtt write domain and manually flush cachelines (as required).
987 * This optimizes for the case when the gpu will use the data
988 * right away and we therefore have to clflush anyway.
989 */
Chris Wilsone27ab732017-06-15 13:38:49 +0100990 if (!obj->cache_dirty) {
Chris Wilson7f5f95d2017-03-10 00:09:42 +0000991 *needs_clflush |= CLFLUSH_AFTER;
Chris Wilson43394c72016-08-18 17:16:47 +0100992
Chris Wilsone27ab732017-06-15 13:38:49 +0100993 /*
994 * Same trick applies to invalidate partially written
995 * cachelines read before writing.
996 */
Christian Königc0a51fd2018-02-16 13:43:38 +0100997 if (!(obj->read_domains & I915_GEM_DOMAIN_CPU))
Chris Wilsone27ab732017-06-15 13:38:49 +0100998 *needs_clflush |= CLFLUSH_BEFORE;
999 }
Chris Wilson43394c72016-08-18 17:16:47 +01001000
Chris Wilson7f5f95d2017-03-10 00:09:42 +00001001out:
Chris Wilson43394c72016-08-18 17:16:47 +01001002 intel_fb_obj_invalidate(obj, ORIGIN_CPU);
Chris Wilsona4f5ea62016-10-28 13:58:35 +01001003 obj->mm.dirty = true;
Chris Wilson97649512016-08-18 17:16:50 +01001004 /* return with the pages pinned */
Chris Wilson43394c72016-08-18 17:16:47 +01001005 return 0;
Chris Wilson97649512016-08-18 17:16:50 +01001006
1007err_unpin:
1008 i915_gem_object_unpin_pages(obj);
1009 return ret;
Brad Volkin4c914c02014-02-18 10:15:45 -08001010}
1011
Daniel Vetter23c18c72012-03-25 19:47:42 +02001012static void
1013shmem_clflush_swizzled_range(char *addr, unsigned long length,
1014 bool swizzled)
1015{
Daniel Vettere7e58eb2012-03-25 19:47:43 +02001016 if (unlikely(swizzled)) {
Daniel Vetter23c18c72012-03-25 19:47:42 +02001017 unsigned long start = (unsigned long) addr;
1018 unsigned long end = (unsigned long) addr + length;
1019
1020 /* For swizzling simply ensure that we always flush both
1021 * channels. Lame, but simple and it works. Swizzled
1022 * pwrite/pread is far from a hotpath - current userspace
1023 * doesn't use it at all. */
1024 start = round_down(start, 128);
1025 end = round_up(end, 128);
1026
1027 drm_clflush_virt_range((void *)start, end - start);
1028 } else {
1029 drm_clflush_virt_range(addr, length);
1030 }
1031
1032}
1033
Daniel Vetterd174bd62012-03-25 19:47:40 +02001034/* Only difference to the fast-path function is that this can handle bit17
1035 * and uses non-atomic copy and kmap functions. */
1036static int
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001037shmem_pread_slow(struct page *page, int offset, int length,
Daniel Vetterd174bd62012-03-25 19:47:40 +02001038 char __user *user_data,
1039 bool page_do_bit17_swizzling, bool needs_clflush)
1040{
1041 char *vaddr;
1042 int ret;
1043
1044 vaddr = kmap(page);
1045 if (needs_clflush)
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001046 shmem_clflush_swizzled_range(vaddr + offset, length,
Daniel Vetter23c18c72012-03-25 19:47:42 +02001047 page_do_bit17_swizzling);
Daniel Vetterd174bd62012-03-25 19:47:40 +02001048
1049 if (page_do_bit17_swizzling)
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001050 ret = __copy_to_user_swizzled(user_data, vaddr, offset, length);
Daniel Vetterd174bd62012-03-25 19:47:40 +02001051 else
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001052 ret = __copy_to_user(user_data, vaddr + offset, length);
Daniel Vetterd174bd62012-03-25 19:47:40 +02001053 kunmap(page);
1054
Chris Wilsonf60d7f02012-09-04 21:02:56 +01001055 return ret ? - EFAULT : 0;
Daniel Vetterd174bd62012-03-25 19:47:40 +02001056}
1057
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001058static int
1059shmem_pread(struct page *page, int offset, int length, char __user *user_data,
1060 bool page_do_bit17_swizzling, bool needs_clflush)
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301061{
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001062 int ret;
1063
1064 ret = -ENODEV;
1065 if (!page_do_bit17_swizzling) {
1066 char *vaddr = kmap_atomic(page);
1067
1068 if (needs_clflush)
1069 drm_clflush_virt_range(vaddr + offset, length);
1070 ret = __copy_to_user_inatomic(user_data, vaddr + offset, length);
1071 kunmap_atomic(vaddr);
1072 }
1073 if (ret == 0)
1074 return 0;
1075
1076 return shmem_pread_slow(page, offset, length, user_data,
1077 page_do_bit17_swizzling, needs_clflush);
1078}
1079
1080static int
1081i915_gem_shmem_pread(struct drm_i915_gem_object *obj,
1082 struct drm_i915_gem_pread *args)
1083{
1084 char __user *user_data;
1085 u64 remain;
1086 unsigned int obj_do_bit17_swizzling;
1087 unsigned int needs_clflush;
1088 unsigned int idx, offset;
1089 int ret;
1090
1091 obj_do_bit17_swizzling = 0;
1092 if (i915_gem_object_needs_bit17_swizzle(obj))
1093 obj_do_bit17_swizzling = BIT(17);
1094
1095 ret = mutex_lock_interruptible(&obj->base.dev->struct_mutex);
1096 if (ret)
1097 return ret;
1098
1099 ret = i915_gem_obj_prepare_shmem_read(obj, &needs_clflush);
1100 mutex_unlock(&obj->base.dev->struct_mutex);
1101 if (ret)
1102 return ret;
1103
1104 remain = args->size;
1105 user_data = u64_to_user_ptr(args->data_ptr);
1106 offset = offset_in_page(args->offset);
1107 for (idx = args->offset >> PAGE_SHIFT; remain; idx++) {
1108 struct page *page = i915_gem_object_get_page(obj, idx);
1109 int length;
1110
1111 length = remain;
1112 if (offset + length > PAGE_SIZE)
1113 length = PAGE_SIZE - offset;
1114
1115 ret = shmem_pread(page, offset, length, user_data,
1116 page_to_phys(page) & obj_do_bit17_swizzling,
1117 needs_clflush);
1118 if (ret)
1119 break;
1120
1121 remain -= length;
1122 user_data += length;
1123 offset = 0;
1124 }
1125
1126 i915_gem_obj_finish_shmem_access(obj);
1127 return ret;
1128}
1129
1130static inline bool
1131gtt_user_read(struct io_mapping *mapping,
1132 loff_t base, int offset,
1133 char __user *user_data, int length)
1134{
Ville Syrjäläafe722b2017-09-01 20:12:52 +03001135 void __iomem *vaddr;
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001136 unsigned long unwritten;
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301137
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301138 /* We can use the cpu mem copy function because this is X86. */
Ville Syrjäläafe722b2017-09-01 20:12:52 +03001139 vaddr = io_mapping_map_atomic_wc(mapping, base);
1140 unwritten = __copy_to_user_inatomic(user_data,
1141 (void __force *)vaddr + offset,
1142 length);
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001143 io_mapping_unmap_atomic(vaddr);
1144 if (unwritten) {
Ville Syrjäläafe722b2017-09-01 20:12:52 +03001145 vaddr = io_mapping_map_wc(mapping, base, PAGE_SIZE);
1146 unwritten = copy_to_user(user_data,
1147 (void __force *)vaddr + offset,
1148 length);
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001149 io_mapping_unmap(vaddr);
1150 }
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301151 return unwritten;
1152}
1153
1154static int
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001155i915_gem_gtt_pread(struct drm_i915_gem_object *obj,
1156 const struct drm_i915_gem_pread *args)
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301157{
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001158 struct drm_i915_private *i915 = to_i915(obj->base.dev);
1159 struct i915_ggtt *ggtt = &i915->ggtt;
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301160 struct drm_mm_node node;
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001161 struct i915_vma *vma;
1162 void __user *user_data;
1163 u64 remain, offset;
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301164 int ret;
1165
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001166 ret = mutex_lock_interruptible(&i915->drm.struct_mutex);
1167 if (ret)
1168 return ret;
1169
1170 intel_runtime_pm_get(i915);
1171 vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0,
Chris Wilsona3259ca2017-10-09 09:44:00 +01001172 PIN_MAPPABLE |
1173 PIN_NONFAULT |
1174 PIN_NONBLOCK);
Chris Wilson18034582016-08-18 17:16:45 +01001175 if (!IS_ERR(vma)) {
1176 node.start = i915_ggtt_offset(vma);
1177 node.allocated = false;
Chris Wilson49ef5292016-08-18 17:17:00 +01001178 ret = i915_vma_put_fence(vma);
Chris Wilson18034582016-08-18 17:16:45 +01001179 if (ret) {
1180 i915_vma_unpin(vma);
1181 vma = ERR_PTR(ret);
1182 }
1183 }
Chris Wilson058d88c2016-08-15 10:49:06 +01001184 if (IS_ERR(vma)) {
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001185 ret = insert_mappable_node(ggtt, &node, PAGE_SIZE);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301186 if (ret)
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001187 goto out_unlock;
1188 GEM_BUG_ON(!node.allocated);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301189 }
1190
1191 ret = i915_gem_object_set_to_gtt_domain(obj, false);
1192 if (ret)
1193 goto out_unpin;
1194
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001195 mutex_unlock(&i915->drm.struct_mutex);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301196
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001197 user_data = u64_to_user_ptr(args->data_ptr);
1198 remain = args->size;
1199 offset = args->offset;
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301200
1201 while (remain > 0) {
1202 /* Operation in this page
1203 *
1204 * page_base = page offset within aperture
1205 * page_offset = offset within page
1206 * page_length = bytes to copy for this page
1207 */
1208 u32 page_base = node.start;
1209 unsigned page_offset = offset_in_page(offset);
1210 unsigned page_length = PAGE_SIZE - page_offset;
1211 page_length = remain < page_length ? remain : page_length;
1212 if (node.allocated) {
1213 wmb();
1214 ggtt->base.insert_page(&ggtt->base,
1215 i915_gem_object_get_dma_address(obj, offset >> PAGE_SHIFT),
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001216 node.start, I915_CACHE_NONE, 0);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301217 wmb();
1218 } else {
1219 page_base += offset & PAGE_MASK;
1220 }
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001221
Matthew Auld73ebd502017-12-11 15:18:20 +00001222 if (gtt_user_read(&ggtt->iomap, page_base, page_offset,
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001223 user_data, page_length)) {
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301224 ret = -EFAULT;
1225 break;
1226 }
1227
1228 remain -= page_length;
1229 user_data += page_length;
1230 offset += page_length;
1231 }
1232
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001233 mutex_lock(&i915->drm.struct_mutex);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301234out_unpin:
1235 if (node.allocated) {
1236 wmb();
1237 ggtt->base.clear_range(&ggtt->base,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02001238 node.start, node.size);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301239 remove_mappable_node(&node);
1240 } else {
Chris Wilson058d88c2016-08-15 10:49:06 +01001241 i915_vma_unpin(vma);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301242 }
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001243out_unlock:
1244 intel_runtime_pm_put(i915);
1245 mutex_unlock(&i915->drm.struct_mutex);
Chris Wilsonf60d7f02012-09-04 21:02:56 +01001246
Eric Anholteb014592009-03-10 11:44:52 -07001247 return ret;
1248}
1249
Eric Anholt673a3942008-07-30 12:06:12 -07001250/**
1251 * Reads data from the object referenced by handle.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01001252 * @dev: drm device pointer
1253 * @data: ioctl data blob
1254 * @file: drm file pointer
Eric Anholt673a3942008-07-30 12:06:12 -07001255 *
1256 * On error, the contents of *data are undefined.
1257 */
1258int
1259i915_gem_pread_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00001260 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001261{
1262 struct drm_i915_gem_pread *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00001263 struct drm_i915_gem_object *obj;
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001264 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07001265
Chris Wilson51311d02010-11-17 09:10:42 +00001266 if (args->size == 0)
1267 return 0;
1268
1269 if (!access_ok(VERIFY_WRITE,
Gustavo Padovan3ed605b2016-04-26 12:32:27 -03001270 u64_to_user_ptr(args->data_ptr),
Chris Wilson51311d02010-11-17 09:10:42 +00001271 args->size))
1272 return -EFAULT;
1273
Chris Wilson03ac0642016-07-20 13:31:51 +01001274 obj = i915_gem_object_lookup(file, args->handle);
Chris Wilson258a5ed2016-08-05 10:14:16 +01001275 if (!obj)
1276 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07001277
Chris Wilson7dcd2492010-09-26 20:21:44 +01001278 /* Bounds check source. */
Matthew Auld966d5bf2016-12-13 20:32:22 +00001279 if (range_overflows_t(u64, args->offset, args->size, obj->base.size)) {
Chris Wilsonce9d4192010-09-26 20:50:05 +01001280 ret = -EINVAL;
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001281 goto out;
Chris Wilsonce9d4192010-09-26 20:50:05 +01001282 }
1283
Chris Wilsondb53a302011-02-03 11:57:46 +00001284 trace_i915_gem_object_pread(obj, args->offset, args->size);
1285
Chris Wilsone95433c2016-10-28 13:58:27 +01001286 ret = i915_gem_object_wait(obj,
1287 I915_WAIT_INTERRUPTIBLE,
1288 MAX_SCHEDULE_TIMEOUT,
1289 to_rps_client(file));
Chris Wilson258a5ed2016-08-05 10:14:16 +01001290 if (ret)
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001291 goto out;
Chris Wilson258a5ed2016-08-05 10:14:16 +01001292
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001293 ret = i915_gem_object_pin_pages(obj);
Chris Wilson258a5ed2016-08-05 10:14:16 +01001294 if (ret)
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001295 goto out;
Chris Wilson258a5ed2016-08-05 10:14:16 +01001296
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001297 ret = i915_gem_shmem_pread(obj, args);
Chris Wilson9c870d02016-10-24 13:42:15 +01001298 if (ret == -EFAULT || ret == -ENODEV)
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001299 ret = i915_gem_gtt_pread(obj, args);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301300
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001301 i915_gem_object_unpin_pages(obj);
1302out:
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001303 i915_gem_object_put(obj);
Eric Anholteb014592009-03-10 11:44:52 -07001304 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07001305}
1306
Keith Packard0839ccb2008-10-30 19:38:48 -07001307/* This is the fast write path which cannot handle
1308 * page faults in the source data
Linus Torvalds9b7530cc2008-10-20 14:16:43 -07001309 */
Linus Torvalds9b7530cc2008-10-20 14:16:43 -07001310
Chris Wilsonfe115622016-10-28 13:58:40 +01001311static inline bool
1312ggtt_write(struct io_mapping *mapping,
1313 loff_t base, int offset,
1314 char __user *user_data, int length)
Keith Packard0839ccb2008-10-30 19:38:48 -07001315{
Ville Syrjäläafe722b2017-09-01 20:12:52 +03001316 void __iomem *vaddr;
Keith Packard0839ccb2008-10-30 19:38:48 -07001317 unsigned long unwritten;
1318
Ben Widawsky4f0c7cf2012-04-16 14:07:47 -07001319 /* We can use the cpu mem copy function because this is X86. */
Ville Syrjäläafe722b2017-09-01 20:12:52 +03001320 vaddr = io_mapping_map_atomic_wc(mapping, base);
1321 unwritten = __copy_from_user_inatomic_nocache((void __force *)vaddr + offset,
Keith Packard0839ccb2008-10-30 19:38:48 -07001322 user_data, length);
Chris Wilsonfe115622016-10-28 13:58:40 +01001323 io_mapping_unmap_atomic(vaddr);
1324 if (unwritten) {
Ville Syrjäläafe722b2017-09-01 20:12:52 +03001325 vaddr = io_mapping_map_wc(mapping, base, PAGE_SIZE);
1326 unwritten = copy_from_user((void __force *)vaddr + offset,
1327 user_data, length);
Chris Wilsonfe115622016-10-28 13:58:40 +01001328 io_mapping_unmap(vaddr);
1329 }
Keith Packard0839ccb2008-10-30 19:38:48 -07001330
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001331 return unwritten;
1332}
1333
Eric Anholt3de09aa2009-03-09 09:42:23 -07001334/**
1335 * This is the fast pwrite path, where we copy the data directly from the
1336 * user into the GTT, uncached.
Chris Wilsonfe115622016-10-28 13:58:40 +01001337 * @obj: i915 GEM object
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01001338 * @args: pwrite arguments structure
Eric Anholt3de09aa2009-03-09 09:42:23 -07001339 */
Eric Anholt673a3942008-07-30 12:06:12 -07001340static int
Chris Wilsonfe115622016-10-28 13:58:40 +01001341i915_gem_gtt_pwrite_fast(struct drm_i915_gem_object *obj,
1342 const struct drm_i915_gem_pwrite *args)
Eric Anholt673a3942008-07-30 12:06:12 -07001343{
Chris Wilsonfe115622016-10-28 13:58:40 +01001344 struct drm_i915_private *i915 = to_i915(obj->base.dev);
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301345 struct i915_ggtt *ggtt = &i915->ggtt;
1346 struct drm_mm_node node;
Chris Wilsonfe115622016-10-28 13:58:40 +01001347 struct i915_vma *vma;
1348 u64 remain, offset;
1349 void __user *user_data;
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301350 int ret;
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301351
Chris Wilsonfe115622016-10-28 13:58:40 +01001352 ret = mutex_lock_interruptible(&i915->drm.struct_mutex);
1353 if (ret)
1354 return ret;
Daniel Vetter935aaa62012-03-25 19:47:35 +02001355
Chris Wilson8bd81812017-10-19 07:37:33 +01001356 if (i915_gem_object_has_struct_page(obj)) {
1357 /*
1358 * Avoid waking the device up if we can fallback, as
1359 * waking/resuming is very slow (worst-case 10-100 ms
1360 * depending on PCI sleeps and our own resume time).
1361 * This easily dwarfs any performance advantage from
1362 * using the cache bypass of indirect GGTT access.
1363 */
1364 if (!intel_runtime_pm_get_if_in_use(i915)) {
1365 ret = -EFAULT;
1366 goto out_unlock;
1367 }
1368 } else {
1369 /* No backing pages, no fallback, we must force GGTT access */
1370 intel_runtime_pm_get(i915);
1371 }
1372
Chris Wilson058d88c2016-08-15 10:49:06 +01001373 vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0,
Chris Wilsona3259ca2017-10-09 09:44:00 +01001374 PIN_MAPPABLE |
1375 PIN_NONFAULT |
1376 PIN_NONBLOCK);
Chris Wilson18034582016-08-18 17:16:45 +01001377 if (!IS_ERR(vma)) {
1378 node.start = i915_ggtt_offset(vma);
1379 node.allocated = false;
Chris Wilson49ef5292016-08-18 17:17:00 +01001380 ret = i915_vma_put_fence(vma);
Chris Wilson18034582016-08-18 17:16:45 +01001381 if (ret) {
1382 i915_vma_unpin(vma);
1383 vma = ERR_PTR(ret);
1384 }
1385 }
Chris Wilson058d88c2016-08-15 10:49:06 +01001386 if (IS_ERR(vma)) {
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001387 ret = insert_mappable_node(ggtt, &node, PAGE_SIZE);
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301388 if (ret)
Chris Wilson8bd81812017-10-19 07:37:33 +01001389 goto out_rpm;
Chris Wilsonfe115622016-10-28 13:58:40 +01001390 GEM_BUG_ON(!node.allocated);
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301391 }
Daniel Vetter935aaa62012-03-25 19:47:35 +02001392
1393 ret = i915_gem_object_set_to_gtt_domain(obj, true);
1394 if (ret)
1395 goto out_unpin;
1396
Chris Wilsonfe115622016-10-28 13:58:40 +01001397 mutex_unlock(&i915->drm.struct_mutex);
1398
Chris Wilsonb19482d2016-08-18 17:16:43 +01001399 intel_fb_obj_invalidate(obj, ORIGIN_CPU);
Paulo Zanoni063e4e62015-02-13 17:23:45 -02001400
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301401 user_data = u64_to_user_ptr(args->data_ptr);
1402 offset = args->offset;
1403 remain = args->size;
1404 while (remain) {
Eric Anholt673a3942008-07-30 12:06:12 -07001405 /* Operation in this page
1406 *
Keith Packard0839ccb2008-10-30 19:38:48 -07001407 * page_base = page offset within aperture
1408 * page_offset = offset within page
1409 * page_length = bytes to copy for this page
Eric Anholt673a3942008-07-30 12:06:12 -07001410 */
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301411 u32 page_base = node.start;
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001412 unsigned int page_offset = offset_in_page(offset);
1413 unsigned int page_length = PAGE_SIZE - page_offset;
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301414 page_length = remain < page_length ? remain : page_length;
1415 if (node.allocated) {
1416 wmb(); /* flush the write before we modify the GGTT */
1417 ggtt->base.insert_page(&ggtt->base,
1418 i915_gem_object_get_dma_address(obj, offset >> PAGE_SHIFT),
1419 node.start, I915_CACHE_NONE, 0);
1420 wmb(); /* flush modifications to the GGTT (insert_page) */
1421 } else {
1422 page_base += offset & PAGE_MASK;
1423 }
Keith Packard0839ccb2008-10-30 19:38:48 -07001424 /* If we get a fault while copying data, then (presumably) our
Eric Anholt3de09aa2009-03-09 09:42:23 -07001425 * source page isn't available. Return the error and we'll
1426 * retry in the slow path.
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301427 * If the object is non-shmem backed, we retry again with the
1428 * path that handles page fault.
Keith Packard0839ccb2008-10-30 19:38:48 -07001429 */
Matthew Auld73ebd502017-12-11 15:18:20 +00001430 if (ggtt_write(&ggtt->iomap, page_base, page_offset,
Chris Wilsonfe115622016-10-28 13:58:40 +01001431 user_data, page_length)) {
1432 ret = -EFAULT;
1433 break;
Daniel Vetter935aaa62012-03-25 19:47:35 +02001434 }
Eric Anholt673a3942008-07-30 12:06:12 -07001435
Keith Packard0839ccb2008-10-30 19:38:48 -07001436 remain -= page_length;
1437 user_data += page_length;
1438 offset += page_length;
Eric Anholt673a3942008-07-30 12:06:12 -07001439 }
Chris Wilsond59b21e2017-02-22 11:40:49 +00001440 intel_fb_obj_flush(obj, ORIGIN_CPU);
Chris Wilsonfe115622016-10-28 13:58:40 +01001441
1442 mutex_lock(&i915->drm.struct_mutex);
Daniel Vetter935aaa62012-03-25 19:47:35 +02001443out_unpin:
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301444 if (node.allocated) {
1445 wmb();
1446 ggtt->base.clear_range(&ggtt->base,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02001447 node.start, node.size);
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301448 remove_mappable_node(&node);
1449 } else {
Chris Wilson058d88c2016-08-15 10:49:06 +01001450 i915_vma_unpin(vma);
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301451 }
Chris Wilson8bd81812017-10-19 07:37:33 +01001452out_rpm:
Chris Wilson9c870d02016-10-24 13:42:15 +01001453 intel_runtime_pm_put(i915);
Chris Wilson8bd81812017-10-19 07:37:33 +01001454out_unlock:
Chris Wilsonfe115622016-10-28 13:58:40 +01001455 mutex_unlock(&i915->drm.struct_mutex);
Eric Anholt3de09aa2009-03-09 09:42:23 -07001456 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07001457}
1458
Eric Anholt673a3942008-07-30 12:06:12 -07001459static int
Chris Wilsonfe115622016-10-28 13:58:40 +01001460shmem_pwrite_slow(struct page *page, int offset, int length,
Daniel Vetterd174bd62012-03-25 19:47:40 +02001461 char __user *user_data,
1462 bool page_do_bit17_swizzling,
1463 bool needs_clflush_before,
1464 bool needs_clflush_after)
Eric Anholt673a3942008-07-30 12:06:12 -07001465{
Daniel Vetterd174bd62012-03-25 19:47:40 +02001466 char *vaddr;
1467 int ret;
Eric Anholt40123c12009-03-09 13:42:30 -07001468
Daniel Vetterd174bd62012-03-25 19:47:40 +02001469 vaddr = kmap(page);
Daniel Vettere7e58eb2012-03-25 19:47:43 +02001470 if (unlikely(needs_clflush_before || page_do_bit17_swizzling))
Chris Wilsonfe115622016-10-28 13:58:40 +01001471 shmem_clflush_swizzled_range(vaddr + offset, length,
Daniel Vetter23c18c72012-03-25 19:47:42 +02001472 page_do_bit17_swizzling);
Daniel Vetterd174bd62012-03-25 19:47:40 +02001473 if (page_do_bit17_swizzling)
Chris Wilsonfe115622016-10-28 13:58:40 +01001474 ret = __copy_from_user_swizzled(vaddr, offset, user_data,
1475 length);
Daniel Vetterd174bd62012-03-25 19:47:40 +02001476 else
Chris Wilsonfe115622016-10-28 13:58:40 +01001477 ret = __copy_from_user(vaddr + offset, user_data, length);
Daniel Vetterd174bd62012-03-25 19:47:40 +02001478 if (needs_clflush_after)
Chris Wilsonfe115622016-10-28 13:58:40 +01001479 shmem_clflush_swizzled_range(vaddr + offset, length,
Daniel Vetter23c18c72012-03-25 19:47:42 +02001480 page_do_bit17_swizzling);
Daniel Vetterd174bd62012-03-25 19:47:40 +02001481 kunmap(page);
Chris Wilsone5281cc2010-10-28 13:45:36 +01001482
Chris Wilson755d2212012-09-04 21:02:55 +01001483 return ret ? -EFAULT : 0;
Eric Anholt40123c12009-03-09 13:42:30 -07001484}
1485
Chris Wilsonfe115622016-10-28 13:58:40 +01001486/* Per-page copy function for the shmem pwrite fastpath.
1487 * Flushes invalid cachelines before writing to the target if
1488 * needs_clflush_before is set and flushes out any written cachelines after
1489 * writing if needs_clflush is set.
1490 */
Eric Anholt40123c12009-03-09 13:42:30 -07001491static int
Chris Wilsonfe115622016-10-28 13:58:40 +01001492shmem_pwrite(struct page *page, int offset, int len, char __user *user_data,
1493 bool page_do_bit17_swizzling,
1494 bool needs_clflush_before,
1495 bool needs_clflush_after)
Eric Anholt40123c12009-03-09 13:42:30 -07001496{
Chris Wilsonfe115622016-10-28 13:58:40 +01001497 int ret;
Eric Anholt40123c12009-03-09 13:42:30 -07001498
Chris Wilsonfe115622016-10-28 13:58:40 +01001499 ret = -ENODEV;
1500 if (!page_do_bit17_swizzling) {
1501 char *vaddr = kmap_atomic(page);
1502
1503 if (needs_clflush_before)
1504 drm_clflush_virt_range(vaddr + offset, len);
1505 ret = __copy_from_user_inatomic(vaddr + offset, user_data, len);
1506 if (needs_clflush_after)
1507 drm_clflush_virt_range(vaddr + offset, len);
1508
1509 kunmap_atomic(vaddr);
1510 }
1511 if (ret == 0)
1512 return ret;
1513
1514 return shmem_pwrite_slow(page, offset, len, user_data,
1515 page_do_bit17_swizzling,
1516 needs_clflush_before,
1517 needs_clflush_after);
1518}
1519
1520static int
1521i915_gem_shmem_pwrite(struct drm_i915_gem_object *obj,
1522 const struct drm_i915_gem_pwrite *args)
1523{
1524 struct drm_i915_private *i915 = to_i915(obj->base.dev);
1525 void __user *user_data;
1526 u64 remain;
1527 unsigned int obj_do_bit17_swizzling;
1528 unsigned int partial_cacheline_write;
1529 unsigned int needs_clflush;
1530 unsigned int offset, idx;
1531 int ret;
1532
1533 ret = mutex_lock_interruptible(&i915->drm.struct_mutex);
Chris Wilson43394c72016-08-18 17:16:47 +01001534 if (ret)
1535 return ret;
Eric Anholt40123c12009-03-09 13:42:30 -07001536
Chris Wilsonfe115622016-10-28 13:58:40 +01001537 ret = i915_gem_obj_prepare_shmem_write(obj, &needs_clflush);
1538 mutex_unlock(&i915->drm.struct_mutex);
1539 if (ret)
1540 return ret;
1541
1542 obj_do_bit17_swizzling = 0;
1543 if (i915_gem_object_needs_bit17_swizzle(obj))
1544 obj_do_bit17_swizzling = BIT(17);
1545
1546 /* If we don't overwrite a cacheline completely we need to be
1547 * careful to have up-to-date data by first clflushing. Don't
1548 * overcomplicate things and flush the entire patch.
1549 */
1550 partial_cacheline_write = 0;
1551 if (needs_clflush & CLFLUSH_BEFORE)
1552 partial_cacheline_write = boot_cpu_data.x86_clflush_size - 1;
1553
Chris Wilson43394c72016-08-18 17:16:47 +01001554 user_data = u64_to_user_ptr(args->data_ptr);
Chris Wilson43394c72016-08-18 17:16:47 +01001555 remain = args->size;
Chris Wilsonfe115622016-10-28 13:58:40 +01001556 offset = offset_in_page(args->offset);
1557 for (idx = args->offset >> PAGE_SHIFT; remain; idx++) {
1558 struct page *page = i915_gem_object_get_page(obj, idx);
1559 int length;
Eric Anholt40123c12009-03-09 13:42:30 -07001560
Chris Wilsonfe115622016-10-28 13:58:40 +01001561 length = remain;
1562 if (offset + length > PAGE_SIZE)
1563 length = PAGE_SIZE - offset;
Chris Wilsone5281cc2010-10-28 13:45:36 +01001564
Chris Wilsonfe115622016-10-28 13:58:40 +01001565 ret = shmem_pwrite(page, offset, length, user_data,
1566 page_to_phys(page) & obj_do_bit17_swizzling,
1567 (offset | length) & partial_cacheline_write,
1568 needs_clflush & CLFLUSH_AFTER);
1569 if (ret)
Chris Wilson9da3da62012-06-01 15:20:22 +01001570 break;
1571
Chris Wilsonfe115622016-10-28 13:58:40 +01001572 remain -= length;
1573 user_data += length;
1574 offset = 0;
Eric Anholt40123c12009-03-09 13:42:30 -07001575 }
1576
Chris Wilsond59b21e2017-02-22 11:40:49 +00001577 intel_fb_obj_flush(obj, ORIGIN_CPU);
Chris Wilsonfe115622016-10-28 13:58:40 +01001578 i915_gem_obj_finish_shmem_access(obj);
Eric Anholt40123c12009-03-09 13:42:30 -07001579 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07001580}
1581
1582/**
1583 * Writes data to the object referenced by handle.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01001584 * @dev: drm device
1585 * @data: ioctl data blob
1586 * @file: drm file
Eric Anholt673a3942008-07-30 12:06:12 -07001587 *
1588 * On error, the contents of the buffer that were to be modified are undefined.
1589 */
1590int
1591i915_gem_pwrite_ioctl(struct drm_device *dev, void *data,
Chris Wilsonfbd5a262010-10-14 15:03:58 +01001592 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001593{
1594 struct drm_i915_gem_pwrite *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00001595 struct drm_i915_gem_object *obj;
Chris Wilson51311d02010-11-17 09:10:42 +00001596 int ret;
1597
1598 if (args->size == 0)
1599 return 0;
1600
1601 if (!access_ok(VERIFY_READ,
Gustavo Padovan3ed605b2016-04-26 12:32:27 -03001602 u64_to_user_ptr(args->data_ptr),
Chris Wilson51311d02010-11-17 09:10:42 +00001603 args->size))
1604 return -EFAULT;
1605
Chris Wilson03ac0642016-07-20 13:31:51 +01001606 obj = i915_gem_object_lookup(file, args->handle);
Chris Wilson258a5ed2016-08-05 10:14:16 +01001607 if (!obj)
1608 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07001609
Chris Wilson7dcd2492010-09-26 20:21:44 +01001610 /* Bounds check destination. */
Matthew Auld966d5bf2016-12-13 20:32:22 +00001611 if (range_overflows_t(u64, args->offset, args->size, obj->base.size)) {
Chris Wilsonce9d4192010-09-26 20:50:05 +01001612 ret = -EINVAL;
Chris Wilson258a5ed2016-08-05 10:14:16 +01001613 goto err;
Chris Wilsonce9d4192010-09-26 20:50:05 +01001614 }
1615
Chris Wilsondb53a302011-02-03 11:57:46 +00001616 trace_i915_gem_object_pwrite(obj, args->offset, args->size);
1617
Chris Wilson7c55e2c2017-03-07 12:03:38 +00001618 ret = -ENODEV;
1619 if (obj->ops->pwrite)
1620 ret = obj->ops->pwrite(obj, args);
1621 if (ret != -ENODEV)
1622 goto err;
1623
Chris Wilsone95433c2016-10-28 13:58:27 +01001624 ret = i915_gem_object_wait(obj,
1625 I915_WAIT_INTERRUPTIBLE |
1626 I915_WAIT_ALL,
1627 MAX_SCHEDULE_TIMEOUT,
1628 to_rps_client(file));
Chris Wilson258a5ed2016-08-05 10:14:16 +01001629 if (ret)
1630 goto err;
1631
Chris Wilsonfe115622016-10-28 13:58:40 +01001632 ret = i915_gem_object_pin_pages(obj);
Chris Wilson258a5ed2016-08-05 10:14:16 +01001633 if (ret)
Chris Wilsonfe115622016-10-28 13:58:40 +01001634 goto err;
Chris Wilson258a5ed2016-08-05 10:14:16 +01001635
Daniel Vetter935aaa62012-03-25 19:47:35 +02001636 ret = -EFAULT;
Eric Anholt673a3942008-07-30 12:06:12 -07001637 /* We can only do the GTT pwrite on untiled buffers, as otherwise
1638 * it would end up going through the fenced access, and we'll get
1639 * different detiling behavior between reading and writing.
1640 * pread/pwrite currently are reading and writing from the CPU
1641 * perspective, requiring manual detiling by the client.
1642 */
Chris Wilson6eae0052016-06-20 15:05:52 +01001643 if (!i915_gem_object_has_struct_page(obj) ||
Chris Wilson9c870d02016-10-24 13:42:15 +01001644 cpu_write_needs_clflush(obj))
Daniel Vetter935aaa62012-03-25 19:47:35 +02001645 /* Note that the gtt paths might fail with non-page-backed user
1646 * pointers (e.g. gtt mappings when moving data between
Chris Wilson9c870d02016-10-24 13:42:15 +01001647 * textures). Fallback to the shmem path in that case.
1648 */
Chris Wilsonfe115622016-10-28 13:58:40 +01001649 ret = i915_gem_gtt_pwrite_fast(obj, args);
Eric Anholt673a3942008-07-30 12:06:12 -07001650
Chris Wilsond1054ee2016-07-16 18:42:36 +01001651 if (ret == -EFAULT || ret == -ENOSPC) {
Chris Wilson6a2c4232014-11-04 04:51:40 -08001652 if (obj->phys_handle)
1653 ret = i915_gem_phys_pwrite(obj, args, file);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301654 else
Chris Wilsonfe115622016-10-28 13:58:40 +01001655 ret = i915_gem_shmem_pwrite(obj, args);
Chris Wilson6a2c4232014-11-04 04:51:40 -08001656 }
Daniel Vetter5c0480f2011-12-14 13:57:30 +01001657
Chris Wilsonfe115622016-10-28 13:58:40 +01001658 i915_gem_object_unpin_pages(obj);
Chris Wilson258a5ed2016-08-05 10:14:16 +01001659err:
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001660 i915_gem_object_put(obj);
Chris Wilson258a5ed2016-08-05 10:14:16 +01001661 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07001662}
1663
Chris Wilson40e62d52016-10-28 13:58:41 +01001664static void i915_gem_object_bump_inactive_ggtt(struct drm_i915_gem_object *obj)
1665{
1666 struct drm_i915_private *i915;
1667 struct list_head *list;
1668 struct i915_vma *vma;
1669
Chris Wilsonf2123812017-10-16 12:40:37 +01001670 GEM_BUG_ON(!i915_gem_object_has_pinned_pages(obj));
1671
Chris Wilsone2189dd2017-12-07 21:14:07 +00001672 for_each_ggtt_vma(vma, obj) {
Chris Wilson40e62d52016-10-28 13:58:41 +01001673 if (i915_vma_is_active(vma))
1674 continue;
1675
1676 if (!drm_mm_node_allocated(&vma->node))
1677 continue;
1678
1679 list_move_tail(&vma->vm_link, &vma->vm->inactive_list);
1680 }
1681
1682 i915 = to_i915(obj->base.dev);
Chris Wilsonf2123812017-10-16 12:40:37 +01001683 spin_lock(&i915->mm.obj_lock);
Chris Wilson40e62d52016-10-28 13:58:41 +01001684 list = obj->bind_count ? &i915->mm.bound_list : &i915->mm.unbound_list;
Chris Wilsonf2123812017-10-16 12:40:37 +01001685 list_move_tail(&obj->mm.link, list);
1686 spin_unlock(&i915->mm.obj_lock);
Chris Wilson40e62d52016-10-28 13:58:41 +01001687}
1688
Eric Anholt673a3942008-07-30 12:06:12 -07001689/**
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001690 * Called when user space prepares to use an object with the CPU, either
1691 * through the mmap ioctl's mapping or a GTT mapping.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01001692 * @dev: drm device
1693 * @data: ioctl data blob
1694 * @file: drm file
Eric Anholt673a3942008-07-30 12:06:12 -07001695 */
1696int
1697i915_gem_set_domain_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00001698 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001699{
1700 struct drm_i915_gem_set_domain *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00001701 struct drm_i915_gem_object *obj;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001702 uint32_t read_domains = args->read_domains;
1703 uint32_t write_domain = args->write_domain;
Chris Wilson40e62d52016-10-28 13:58:41 +01001704 int err;
Eric Anholt673a3942008-07-30 12:06:12 -07001705
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001706 /* Only handle setting domains to types used by the CPU. */
Chris Wilsonb8f90962016-08-05 10:14:07 +01001707 if ((write_domain | read_domains) & I915_GEM_GPU_DOMAINS)
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001708 return -EINVAL;
1709
1710 /* Having something in the write domain implies it's in the read
1711 * domain, and only that read domain. Enforce that in the request.
1712 */
1713 if (write_domain != 0 && read_domains != write_domain)
1714 return -EINVAL;
1715
Chris Wilson03ac0642016-07-20 13:31:51 +01001716 obj = i915_gem_object_lookup(file, args->handle);
Chris Wilsonb8f90962016-08-05 10:14:07 +01001717 if (!obj)
1718 return -ENOENT;
Jesse Barnes652c3932009-08-17 13:31:43 -07001719
Chris Wilson3236f572012-08-24 09:35:09 +01001720 /* Try to flush the object off the GPU without holding the lock.
1721 * We will repeat the flush holding the lock in the normal manner
1722 * to catch cases where we are gazumped.
1723 */
Chris Wilson40e62d52016-10-28 13:58:41 +01001724 err = i915_gem_object_wait(obj,
Chris Wilsone95433c2016-10-28 13:58:27 +01001725 I915_WAIT_INTERRUPTIBLE |
1726 (write_domain ? I915_WAIT_ALL : 0),
1727 MAX_SCHEDULE_TIMEOUT,
1728 to_rps_client(file));
Chris Wilson40e62d52016-10-28 13:58:41 +01001729 if (err)
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001730 goto out;
Chris Wilsonb8f90962016-08-05 10:14:07 +01001731
Tina Zhanga03f3952017-11-14 10:25:13 +00001732 /*
1733 * Proxy objects do not control access to the backing storage, ergo
1734 * they cannot be used as a means to manipulate the cache domain
1735 * tracking for that backing storage. The proxy object is always
1736 * considered to be outside of any cache domain.
1737 */
1738 if (i915_gem_object_is_proxy(obj)) {
1739 err = -ENXIO;
1740 goto out;
1741 }
1742
1743 /*
1744 * Flush and acquire obj->pages so that we are coherent through
Chris Wilson40e62d52016-10-28 13:58:41 +01001745 * direct access in memory with previous cached writes through
1746 * shmemfs and that our cache domain tracking remains valid.
1747 * For example, if the obj->filp was moved to swap without us
1748 * being notified and releasing the pages, we would mistakenly
1749 * continue to assume that the obj remained out of the CPU cached
1750 * domain.
1751 */
1752 err = i915_gem_object_pin_pages(obj);
1753 if (err)
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001754 goto out;
Chris Wilson40e62d52016-10-28 13:58:41 +01001755
1756 err = i915_mutex_lock_interruptible(dev);
1757 if (err)
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001758 goto out_unpin;
Chris Wilson3236f572012-08-24 09:35:09 +01001759
Chris Wilsone22d8e32017-04-12 12:01:11 +01001760 if (read_domains & I915_GEM_DOMAIN_WC)
1761 err = i915_gem_object_set_to_wc_domain(obj, write_domain);
1762 else if (read_domains & I915_GEM_DOMAIN_GTT)
1763 err = i915_gem_object_set_to_gtt_domain(obj, write_domain);
Chris Wilson43566de2015-01-02 16:29:29 +05301764 else
Chris Wilsone22d8e32017-04-12 12:01:11 +01001765 err = i915_gem_object_set_to_cpu_domain(obj, write_domain);
Chris Wilson40e62d52016-10-28 13:58:41 +01001766
1767 /* And bump the LRU for this access */
1768 i915_gem_object_bump_inactive_ggtt(obj);
1769
1770 mutex_unlock(&dev->struct_mutex);
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001771
Daniel Vetter031b6982015-06-26 19:35:16 +02001772 if (write_domain != 0)
Chris Wilsonef749212017-04-12 12:01:10 +01001773 intel_fb_obj_invalidate(obj,
1774 fb_write_origin(obj, write_domain));
Daniel Vetter031b6982015-06-26 19:35:16 +02001775
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001776out_unpin:
Chris Wilson40e62d52016-10-28 13:58:41 +01001777 i915_gem_object_unpin_pages(obj);
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001778out:
1779 i915_gem_object_put(obj);
Chris Wilson40e62d52016-10-28 13:58:41 +01001780 return err;
Eric Anholt673a3942008-07-30 12:06:12 -07001781}
1782
1783/**
1784 * Called when user space has done writes to this buffer
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01001785 * @dev: drm device
1786 * @data: ioctl data blob
1787 * @file: drm file
Eric Anholt673a3942008-07-30 12:06:12 -07001788 */
1789int
1790i915_gem_sw_finish_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00001791 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001792{
1793 struct drm_i915_gem_sw_finish *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00001794 struct drm_i915_gem_object *obj;
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001795
Chris Wilson03ac0642016-07-20 13:31:51 +01001796 obj = i915_gem_object_lookup(file, args->handle);
Chris Wilsonc21724c2016-08-05 10:14:19 +01001797 if (!obj)
1798 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07001799
Tina Zhanga03f3952017-11-14 10:25:13 +00001800 /*
1801 * Proxy objects are barred from CPU access, so there is no
1802 * need to ban sw_finish as it is a nop.
1803 */
1804
Eric Anholt673a3942008-07-30 12:06:12 -07001805 /* Pinned buffers may be scanout, so flush the cache */
Chris Wilson5a97bcc2017-02-22 11:40:46 +00001806 i915_gem_object_flush_if_display(obj);
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001807 i915_gem_object_put(obj);
Chris Wilson5a97bcc2017-02-22 11:40:46 +00001808
1809 return 0;
Eric Anholt673a3942008-07-30 12:06:12 -07001810}
1811
1812/**
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01001813 * i915_gem_mmap_ioctl - Maps the contents of an object, returning the address
1814 * it is mapped to.
1815 * @dev: drm device
1816 * @data: ioctl data blob
1817 * @file: drm file
Eric Anholt673a3942008-07-30 12:06:12 -07001818 *
1819 * While the mapping holds a reference on the contents of the object, it doesn't
1820 * imply a ref on the object itself.
Daniel Vetter34367382014-10-16 12:28:18 +02001821 *
1822 * IMPORTANT:
1823 *
1824 * DRM driver writers who look a this function as an example for how to do GEM
1825 * mmap support, please don't implement mmap support like here. The modern way
1826 * to implement DRM mmap support is with an mmap offset ioctl (like
1827 * i915_gem_mmap_gtt) and then using the mmap syscall on the DRM fd directly.
1828 * That way debug tooling like valgrind will understand what's going on, hiding
1829 * the mmap call in a driver private ioctl will break that. The i915 driver only
1830 * does cpu mmaps this way because we didn't know better.
Eric Anholt673a3942008-07-30 12:06:12 -07001831 */
1832int
1833i915_gem_mmap_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00001834 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001835{
1836 struct drm_i915_gem_mmap *args = data;
Chris Wilson03ac0642016-07-20 13:31:51 +01001837 struct drm_i915_gem_object *obj;
Eric Anholt673a3942008-07-30 12:06:12 -07001838 unsigned long addr;
1839
Akash Goel1816f922015-01-02 16:29:30 +05301840 if (args->flags & ~(I915_MMAP_WC))
1841 return -EINVAL;
1842
Borislav Petkov568a58e2016-03-29 17:42:01 +02001843 if (args->flags & I915_MMAP_WC && !boot_cpu_has(X86_FEATURE_PAT))
Akash Goel1816f922015-01-02 16:29:30 +05301844 return -ENODEV;
1845
Chris Wilson03ac0642016-07-20 13:31:51 +01001846 obj = i915_gem_object_lookup(file, args->handle);
1847 if (!obj)
Chris Wilsonbf79cb92010-08-04 14:19:46 +01001848 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07001849
Daniel Vetter1286ff72012-05-10 15:25:09 +02001850 /* prime objects have no backing filp to GEM mmap
1851 * pages from.
1852 */
Chris Wilson03ac0642016-07-20 13:31:51 +01001853 if (!obj->base.filp) {
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001854 i915_gem_object_put(obj);
Tina Zhang274b2462017-11-14 10:25:12 +00001855 return -ENXIO;
Daniel Vetter1286ff72012-05-10 15:25:09 +02001856 }
1857
Chris Wilson03ac0642016-07-20 13:31:51 +01001858 addr = vm_mmap(obj->base.filp, 0, args->size,
Eric Anholt673a3942008-07-30 12:06:12 -07001859 PROT_READ | PROT_WRITE, MAP_SHARED,
1860 args->offset);
Akash Goel1816f922015-01-02 16:29:30 +05301861 if (args->flags & I915_MMAP_WC) {
1862 struct mm_struct *mm = current->mm;
1863 struct vm_area_struct *vma;
1864
Michal Hocko80a89a52016-05-23 16:26:11 -07001865 if (down_write_killable(&mm->mmap_sem)) {
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001866 i915_gem_object_put(obj);
Michal Hocko80a89a52016-05-23 16:26:11 -07001867 return -EINTR;
1868 }
Akash Goel1816f922015-01-02 16:29:30 +05301869 vma = find_vma(mm, addr);
1870 if (vma)
1871 vma->vm_page_prot =
1872 pgprot_writecombine(vm_get_page_prot(vma->vm_flags));
1873 else
1874 addr = -ENOMEM;
1875 up_write(&mm->mmap_sem);
Chris Wilsonaeecc962016-06-17 14:46:39 -03001876
1877 /* This may race, but that's ok, it only gets set */
Chris Wilson50349242016-08-18 17:17:04 +01001878 WRITE_ONCE(obj->frontbuffer_ggtt_origin, ORIGIN_CPU);
Akash Goel1816f922015-01-02 16:29:30 +05301879 }
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001880 i915_gem_object_put(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001881 if (IS_ERR((void *)addr))
1882 return addr;
1883
1884 args->addr_ptr = (uint64_t) addr;
1885
1886 return 0;
1887}
1888
Chris Wilson03af84f2016-08-18 17:17:01 +01001889static unsigned int tile_row_pages(struct drm_i915_gem_object *obj)
1890{
Chris Wilson6649a0b2017-01-09 16:16:08 +00001891 return i915_gem_object_get_tile_row_size(obj) >> PAGE_SHIFT;
Chris Wilson03af84f2016-08-18 17:17:01 +01001892}
1893
Jesse Barnesde151cf2008-11-12 10:03:55 -08001894/**
Chris Wilson4cc69072016-08-25 19:05:19 +01001895 * i915_gem_mmap_gtt_version - report the current feature set for GTT mmaps
1896 *
1897 * A history of the GTT mmap interface:
1898 *
1899 * 0 - Everything had to fit into the GTT. Both parties of a memcpy had to
1900 * aligned and suitable for fencing, and still fit into the available
1901 * mappable space left by the pinned display objects. A classic problem
1902 * we called the page-fault-of-doom where we would ping-pong between
1903 * two objects that could not fit inside the GTT and so the memcpy
1904 * would page one object in at the expense of the other between every
1905 * single byte.
1906 *
1907 * 1 - Objects can be any size, and have any compatible fencing (X Y, or none
1908 * as set via i915_gem_set_tiling() [DRM_I915_GEM_SET_TILING]). If the
1909 * object is too large for the available space (or simply too large
1910 * for the mappable aperture!), a view is created instead and faulted
1911 * into userspace. (This view is aligned and sized appropriately for
1912 * fenced access.)
1913 *
Chris Wilsone22d8e32017-04-12 12:01:11 +01001914 * 2 - Recognise WC as a separate cache domain so that we can flush the
1915 * delayed writes via GTT before performing direct access via WC.
1916 *
Chris Wilson4cc69072016-08-25 19:05:19 +01001917 * Restrictions:
1918 *
1919 * * snoopable objects cannot be accessed via the GTT. It can cause machine
1920 * hangs on some architectures, corruption on others. An attempt to service
1921 * a GTT page fault from a snoopable object will generate a SIGBUS.
1922 *
1923 * * the object must be able to fit into RAM (physical memory, though no
1924 * limited to the mappable aperture).
1925 *
1926 *
1927 * Caveats:
1928 *
1929 * * a new GTT page fault will synchronize rendering from the GPU and flush
1930 * all data to system memory. Subsequent access will not be synchronized.
1931 *
1932 * * all mappings are revoked on runtime device suspend.
1933 *
1934 * * there are only 8, 16 or 32 fence registers to share between all users
1935 * (older machines require fence register for display and blitter access
1936 * as well). Contention of the fence registers will cause the previous users
1937 * to be unmapped and any new access will generate new page faults.
1938 *
1939 * * running out of memory while servicing a fault may generate a SIGBUS,
1940 * rather than the expected SIGSEGV.
1941 */
1942int i915_gem_mmap_gtt_version(void)
1943{
Chris Wilsone22d8e32017-04-12 12:01:11 +01001944 return 2;
Chris Wilson4cc69072016-08-25 19:05:19 +01001945}
1946
Chris Wilson2d4281b2017-01-10 09:56:32 +00001947static inline struct i915_ggtt_view
1948compute_partial_view(struct drm_i915_gem_object *obj,
Chris Wilson2d4281b2017-01-10 09:56:32 +00001949 pgoff_t page_offset,
1950 unsigned int chunk)
1951{
1952 struct i915_ggtt_view view;
1953
1954 if (i915_gem_object_is_tiled(obj))
1955 chunk = roundup(chunk, tile_row_pages(obj));
1956
Chris Wilson2d4281b2017-01-10 09:56:32 +00001957 view.type = I915_GGTT_VIEW_PARTIAL;
Chris Wilson8bab11932017-01-14 00:28:25 +00001958 view.partial.offset = rounddown(page_offset, chunk);
1959 view.partial.size =
Chris Wilson2d4281b2017-01-10 09:56:32 +00001960 min_t(unsigned int, chunk,
Chris Wilson8bab11932017-01-14 00:28:25 +00001961 (obj->base.size >> PAGE_SHIFT) - view.partial.offset);
Chris Wilson2d4281b2017-01-10 09:56:32 +00001962
1963 /* If the partial covers the entire object, just create a normal VMA. */
1964 if (chunk >= obj->base.size >> PAGE_SHIFT)
1965 view.type = I915_GGTT_VIEW_NORMAL;
1966
1967 return view;
1968}
1969
Chris Wilson4cc69072016-08-25 19:05:19 +01001970/**
Jesse Barnesde151cf2008-11-12 10:03:55 -08001971 * i915_gem_fault - fault a page into the GTT
Geliang Tangd9072a32015-09-15 05:58:44 -07001972 * @vmf: fault info
Jesse Barnesde151cf2008-11-12 10:03:55 -08001973 *
1974 * The fault handler is set up by drm_gem_mmap() when a object is GTT mapped
1975 * from userspace. The fault handler takes care of binding the object to
1976 * the GTT (if needed), allocating and programming a fence register (again,
1977 * only if needed based on whether the old reg is still valid or the object
1978 * is tiled) and inserting a new PTE into the faulting process.
1979 *
1980 * Note that the faulting process may involve evicting existing objects
1981 * from the GTT and/or fence registers to make room. So performance may
1982 * suffer if the GTT working set is large or there are few fence registers
1983 * left.
Chris Wilson4cc69072016-08-25 19:05:19 +01001984 *
1985 * The current feature set supported by i915_gem_fault() and thus GTT mmaps
1986 * is exposed via I915_PARAM_MMAP_GTT_VERSION (see i915_gem_mmap_gtt_version).
Jesse Barnesde151cf2008-11-12 10:03:55 -08001987 */
Dave Jiang11bac802017-02-24 14:56:41 -08001988int i915_gem_fault(struct vm_fault *vmf)
Jesse Barnesde151cf2008-11-12 10:03:55 -08001989{
Chris Wilson03af84f2016-08-18 17:17:01 +01001990#define MIN_CHUNK_PAGES ((1 << 20) >> PAGE_SHIFT) /* 1 MiB */
Dave Jiang11bac802017-02-24 14:56:41 -08001991 struct vm_area_struct *area = vmf->vma;
Chris Wilson058d88c2016-08-15 10:49:06 +01001992 struct drm_i915_gem_object *obj = to_intel_bo(area->vm_private_data);
Chris Wilson05394f32010-11-08 19:18:58 +00001993 struct drm_device *dev = obj->base.dev;
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03001994 struct drm_i915_private *dev_priv = to_i915(dev);
1995 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Chris Wilsonb8f90962016-08-05 10:14:07 +01001996 bool write = !!(vmf->flags & FAULT_FLAG_WRITE);
Chris Wilson058d88c2016-08-15 10:49:06 +01001997 struct i915_vma *vma;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001998 pgoff_t page_offset;
Chris Wilson82118872016-08-18 17:17:05 +01001999 unsigned int flags;
Chris Wilsonb8f90962016-08-05 10:14:07 +01002000 int ret;
Paulo Zanonif65c9162013-11-27 18:20:34 -02002001
Jesse Barnesde151cf2008-11-12 10:03:55 -08002002 /* We don't use vmf->pgoff since that has the fake offset */
Jan Kara1a29d852016-12-14 15:07:01 -08002003 page_offset = (vmf->address - area->vm_start) >> PAGE_SHIFT;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002004
Chris Wilsondb53a302011-02-03 11:57:46 +00002005 trace_i915_gem_object_fault(obj, page_offset, true, write);
2006
Chris Wilson6e4930f2014-02-07 18:37:06 -02002007 /* Try to flush the object off the GPU first without holding the lock.
Chris Wilsonb8f90962016-08-05 10:14:07 +01002008 * Upon acquiring the lock, we will perform our sanity checks and then
Chris Wilson6e4930f2014-02-07 18:37:06 -02002009 * repeat the flush holding the lock in the normal manner to catch cases
2010 * where we are gazumped.
2011 */
Chris Wilsone95433c2016-10-28 13:58:27 +01002012 ret = i915_gem_object_wait(obj,
2013 I915_WAIT_INTERRUPTIBLE,
2014 MAX_SCHEDULE_TIMEOUT,
2015 NULL);
Chris Wilson6e4930f2014-02-07 18:37:06 -02002016 if (ret)
Chris Wilsonb8f90962016-08-05 10:14:07 +01002017 goto err;
2018
Chris Wilson40e62d52016-10-28 13:58:41 +01002019 ret = i915_gem_object_pin_pages(obj);
2020 if (ret)
2021 goto err;
2022
Chris Wilsonb8f90962016-08-05 10:14:07 +01002023 intel_runtime_pm_get(dev_priv);
2024
2025 ret = i915_mutex_lock_interruptible(dev);
2026 if (ret)
2027 goto err_rpm;
Chris Wilson6e4930f2014-02-07 18:37:06 -02002028
Chris Wilsoneb119bd2012-12-16 12:43:36 +00002029 /* Access to snoopable pages through the GTT is incoherent. */
Tvrtko Ursulin0031fb92016-11-04 14:42:44 +00002030 if (obj->cache_level != I915_CACHE_NONE && !HAS_LLC(dev_priv)) {
Chris Wilsonddeff6e2014-05-28 16:16:41 +01002031 ret = -EFAULT;
Chris Wilsonb8f90962016-08-05 10:14:07 +01002032 goto err_unlock;
Chris Wilsoneb119bd2012-12-16 12:43:36 +00002033 }
2034
Chris Wilson82118872016-08-18 17:17:05 +01002035 /* If the object is smaller than a couple of partial vma, it is
2036 * not worth only creating a single partial vma - we may as well
2037 * clear enough space for the full object.
2038 */
2039 flags = PIN_MAPPABLE;
2040 if (obj->base.size > 2 * MIN_CHUNK_PAGES << PAGE_SHIFT)
2041 flags |= PIN_NONBLOCK | PIN_NONFAULT;
2042
Chris Wilsona61007a2016-08-18 17:17:02 +01002043 /* Now pin it into the GTT as needed */
Chris Wilson82118872016-08-18 17:17:05 +01002044 vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0, flags);
Chris Wilsona61007a2016-08-18 17:17:02 +01002045 if (IS_ERR(vma)) {
Chris Wilsona61007a2016-08-18 17:17:02 +01002046 /* Use a partial view if it is bigger than available space */
Chris Wilson2d4281b2017-01-10 09:56:32 +00002047 struct i915_ggtt_view view =
Chris Wilson8201c1f2017-01-10 09:56:33 +00002048 compute_partial_view(obj, page_offset, MIN_CHUNK_PAGES);
Chris Wilsonaa136d92016-08-18 17:17:03 +01002049
Chris Wilson50349242016-08-18 17:17:04 +01002050 /* Userspace is now writing through an untracked VMA, abandon
2051 * all hope that the hardware is able to track future writes.
2052 */
2053 obj->frontbuffer_ggtt_origin = ORIGIN_CPU;
2054
Chris Wilsona61007a2016-08-18 17:17:02 +01002055 vma = i915_gem_object_ggtt_pin(obj, &view, 0, 0, PIN_MAPPABLE);
2056 }
Chris Wilson058d88c2016-08-15 10:49:06 +01002057 if (IS_ERR(vma)) {
2058 ret = PTR_ERR(vma);
Chris Wilsonb8f90962016-08-05 10:14:07 +01002059 goto err_unlock;
Chris Wilson058d88c2016-08-15 10:49:06 +01002060 }
Jesse Barnesde151cf2008-11-12 10:03:55 -08002061
Chris Wilsonc9839302012-11-20 10:45:17 +00002062 ret = i915_gem_object_set_to_gtt_domain(obj, write);
2063 if (ret)
Chris Wilsonb8f90962016-08-05 10:14:07 +01002064 goto err_unpin;
Chris Wilsonc9839302012-11-20 10:45:17 +00002065
Chris Wilson3bd40732017-10-09 09:43:56 +01002066 ret = i915_vma_pin_fence(vma);
Chris Wilsonc9839302012-11-20 10:45:17 +00002067 if (ret)
Chris Wilsonb8f90962016-08-05 10:14:07 +01002068 goto err_unpin;
Chris Wilson7d1c4802010-08-07 21:45:03 +01002069
Chris Wilsonb90b91d2014-06-10 12:14:40 +01002070 /* Finally, remap it using the new GTT offset */
Chris Wilsonc58305a2016-08-19 16:54:28 +01002071 ret = remap_io_mapping(area,
Chris Wilson8bab11932017-01-14 00:28:25 +00002072 area->vm_start + (vma->ggtt_view.partial.offset << PAGE_SHIFT),
Matthew Auld73ebd502017-12-11 15:18:20 +00002073 (ggtt->gmadr.start + vma->node.start) >> PAGE_SHIFT,
Chris Wilsonc58305a2016-08-19 16:54:28 +01002074 min_t(u64, vma->size, area->vm_end - area->vm_start),
Matthew Auld73ebd502017-12-11 15:18:20 +00002075 &ggtt->iomap);
Chris Wilsona65adaf2017-10-09 09:43:57 +01002076 if (ret)
2077 goto err_fence;
Chris Wilsona61007a2016-08-18 17:17:02 +01002078
Chris Wilsona65adaf2017-10-09 09:43:57 +01002079 /* Mark as being mmapped into userspace for later revocation */
2080 assert_rpm_wakelock_held(dev_priv);
2081 if (!i915_vma_set_userfault(vma) && !obj->userfault_count++)
2082 list_add(&obj->userfault_link, &dev_priv->mm.userfault_list);
2083 GEM_BUG_ON(!obj->userfault_count);
2084
Chris Wilson71253972017-12-06 12:49:14 +00002085 i915_vma_set_ggtt_write(vma);
2086
Chris Wilsona65adaf2017-10-09 09:43:57 +01002087err_fence:
Chris Wilson3bd40732017-10-09 09:43:56 +01002088 i915_vma_unpin_fence(vma);
Chris Wilsonb8f90962016-08-05 10:14:07 +01002089err_unpin:
Chris Wilson058d88c2016-08-15 10:49:06 +01002090 __i915_vma_unpin(vma);
Chris Wilsonb8f90962016-08-05 10:14:07 +01002091err_unlock:
Jesse Barnesde151cf2008-11-12 10:03:55 -08002092 mutex_unlock(&dev->struct_mutex);
Chris Wilsonb8f90962016-08-05 10:14:07 +01002093err_rpm:
2094 intel_runtime_pm_put(dev_priv);
Chris Wilson40e62d52016-10-28 13:58:41 +01002095 i915_gem_object_unpin_pages(obj);
Chris Wilsonb8f90962016-08-05 10:14:07 +01002096err:
Jesse Barnesde151cf2008-11-12 10:03:55 -08002097 switch (ret) {
Chris Wilsond9bc7e92011-02-07 13:09:31 +00002098 case -EIO:
Daniel Vetter2232f032014-09-04 09:36:18 +02002099 /*
2100 * We eat errors when the gpu is terminally wedged to avoid
2101 * userspace unduly crashing (gl has no provisions for mmaps to
2102 * fail). But any other -EIO isn't ours (e.g. swap in failure)
2103 * and so needs to be reported.
2104 */
2105 if (!i915_terminally_wedged(&dev_priv->gpu_error)) {
Paulo Zanonif65c9162013-11-27 18:20:34 -02002106 ret = VM_FAULT_SIGBUS;
2107 break;
2108 }
Chris Wilson045e7692010-11-07 09:18:22 +00002109 case -EAGAIN:
Daniel Vetter571c6082013-09-12 17:57:28 +02002110 /*
2111 * EAGAIN means the gpu is hung and we'll wait for the error
2112 * handler to reset everything when re-faulting in
2113 * i915_mutex_lock_interruptible.
Chris Wilsond9bc7e92011-02-07 13:09:31 +00002114 */
Chris Wilsonc7150892009-09-23 00:43:56 +01002115 case 0:
2116 case -ERESTARTSYS:
Chris Wilsonbed636a2011-02-11 20:31:19 +00002117 case -EINTR:
Dmitry Rogozhkine79e0fe2012-10-03 17:15:26 +03002118 case -EBUSY:
2119 /*
2120 * EBUSY is ok: this just means that another thread
2121 * already did the job.
2122 */
Paulo Zanonif65c9162013-11-27 18:20:34 -02002123 ret = VM_FAULT_NOPAGE;
2124 break;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002125 case -ENOMEM:
Paulo Zanonif65c9162013-11-27 18:20:34 -02002126 ret = VM_FAULT_OOM;
2127 break;
Daniel Vettera7c2e1a2012-10-17 11:17:16 +02002128 case -ENOSPC:
Chris Wilson45d67812014-01-31 11:34:57 +00002129 case -EFAULT:
Paulo Zanonif65c9162013-11-27 18:20:34 -02002130 ret = VM_FAULT_SIGBUS;
2131 break;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002132 default:
Daniel Vettera7c2e1a2012-10-17 11:17:16 +02002133 WARN_ONCE(ret, "unhandled error in i915_gem_fault: %i\n", ret);
Paulo Zanonif65c9162013-11-27 18:20:34 -02002134 ret = VM_FAULT_SIGBUS;
2135 break;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002136 }
Paulo Zanonif65c9162013-11-27 18:20:34 -02002137 return ret;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002138}
2139
Chris Wilsona65adaf2017-10-09 09:43:57 +01002140static void __i915_gem_object_release_mmap(struct drm_i915_gem_object *obj)
2141{
2142 struct i915_vma *vma;
2143
2144 GEM_BUG_ON(!obj->userfault_count);
2145
2146 obj->userfault_count = 0;
2147 list_del(&obj->userfault_link);
2148 drm_vma_node_unmap(&obj->base.vma_node,
2149 obj->base.dev->anon_inode->i_mapping);
2150
Chris Wilsone2189dd2017-12-07 21:14:07 +00002151 for_each_ggtt_vma(vma, obj)
Chris Wilsona65adaf2017-10-09 09:43:57 +01002152 i915_vma_unset_userfault(vma);
Chris Wilsona65adaf2017-10-09 09:43:57 +01002153}
2154
Jesse Barnesde151cf2008-11-12 10:03:55 -08002155/**
Chris Wilson901782b2009-07-10 08:18:50 +01002156 * i915_gem_release_mmap - remove physical page mappings
2157 * @obj: obj in question
2158 *
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02002159 * Preserve the reservation of the mmapping with the DRM core code, but
Chris Wilson901782b2009-07-10 08:18:50 +01002160 * relinquish ownership of the pages back to the system.
2161 *
2162 * It is vital that we remove the page mapping if we have mapped a tiled
2163 * object through the GTT and then lose the fence register due to
2164 * resource pressure. Similarly if the object has been moved out of the
2165 * aperture, than pages mapped into userspace must be revoked. Removing the
2166 * mapping will then trigger a page fault on the next user access, allowing
2167 * fixup by i915_gem_fault().
2168 */
Eric Anholtd05ca302009-07-10 13:02:26 -07002169void
Chris Wilson05394f32010-11-08 19:18:58 +00002170i915_gem_release_mmap(struct drm_i915_gem_object *obj)
Chris Wilson901782b2009-07-10 08:18:50 +01002171{
Chris Wilson275f0392016-10-24 13:42:14 +01002172 struct drm_i915_private *i915 = to_i915(obj->base.dev);
Chris Wilson275f0392016-10-24 13:42:14 +01002173
Chris Wilson349f2cc2016-04-13 17:35:12 +01002174 /* Serialisation between user GTT access and our code depends upon
2175 * revoking the CPU's PTE whilst the mutex is held. The next user
2176 * pagefault then has to wait until we release the mutex.
Chris Wilson9c870d02016-10-24 13:42:15 +01002177 *
2178 * Note that RPM complicates somewhat by adding an additional
2179 * requirement that operations to the GGTT be made holding the RPM
2180 * wakeref.
Chris Wilson349f2cc2016-04-13 17:35:12 +01002181 */
Chris Wilson275f0392016-10-24 13:42:14 +01002182 lockdep_assert_held(&i915->drm.struct_mutex);
Chris Wilson9c870d02016-10-24 13:42:15 +01002183 intel_runtime_pm_get(i915);
Chris Wilson349f2cc2016-04-13 17:35:12 +01002184
Chris Wilsona65adaf2017-10-09 09:43:57 +01002185 if (!obj->userfault_count)
Chris Wilson9c870d02016-10-24 13:42:15 +01002186 goto out;
Chris Wilson901782b2009-07-10 08:18:50 +01002187
Chris Wilsona65adaf2017-10-09 09:43:57 +01002188 __i915_gem_object_release_mmap(obj);
Chris Wilson349f2cc2016-04-13 17:35:12 +01002189
2190 /* Ensure that the CPU's PTE are revoked and there are not outstanding
2191 * memory transactions from userspace before we return. The TLB
2192 * flushing implied above by changing the PTE above *should* be
2193 * sufficient, an extra barrier here just provides us with a bit
2194 * of paranoid documentation about our requirement to serialise
2195 * memory writes before touching registers / GSM.
2196 */
2197 wmb();
Chris Wilson9c870d02016-10-24 13:42:15 +01002198
2199out:
2200 intel_runtime_pm_put(i915);
Chris Wilson901782b2009-07-10 08:18:50 +01002201}
2202
Chris Wilson7c108fd2016-10-24 13:42:18 +01002203void i915_gem_runtime_suspend(struct drm_i915_private *dev_priv)
Chris Wilsoneedd10f2014-06-16 08:57:44 +01002204{
Chris Wilson3594a3e2016-10-24 13:42:16 +01002205 struct drm_i915_gem_object *obj, *on;
Chris Wilson7c108fd2016-10-24 13:42:18 +01002206 int i;
Chris Wilsoneedd10f2014-06-16 08:57:44 +01002207
Chris Wilson3594a3e2016-10-24 13:42:16 +01002208 /*
2209 * Only called during RPM suspend. All users of the userfault_list
2210 * must be holding an RPM wakeref to ensure that this can not
2211 * run concurrently with themselves (and use the struct_mutex for
2212 * protection between themselves).
2213 */
2214
2215 list_for_each_entry_safe(obj, on,
Chris Wilsona65adaf2017-10-09 09:43:57 +01002216 &dev_priv->mm.userfault_list, userfault_link)
2217 __i915_gem_object_release_mmap(obj);
Chris Wilson7c108fd2016-10-24 13:42:18 +01002218
2219 /* The fence will be lost when the device powers down. If any were
2220 * in use by hardware (i.e. they are pinned), we should not be powering
2221 * down! All other fences will be reacquired by the user upon waking.
2222 */
2223 for (i = 0; i < dev_priv->num_fence_regs; i++) {
2224 struct drm_i915_fence_reg *reg = &dev_priv->fence_regs[i];
2225
Chris Wilsone0ec3ec2017-02-03 12:57:17 +00002226 /* Ideally we want to assert that the fence register is not
2227 * live at this point (i.e. that no piece of code will be
2228 * trying to write through fence + GTT, as that both violates
2229 * our tracking of activity and associated locking/barriers,
2230 * but also is illegal given that the hw is powered down).
2231 *
2232 * Previously we used reg->pin_count as a "liveness" indicator.
2233 * That is not sufficient, and we need a more fine-grained
2234 * tool if we want to have a sanity check here.
2235 */
Chris Wilson7c108fd2016-10-24 13:42:18 +01002236
2237 if (!reg->vma)
2238 continue;
2239
Chris Wilsona65adaf2017-10-09 09:43:57 +01002240 GEM_BUG_ON(i915_vma_has_userfault(reg->vma));
Chris Wilson7c108fd2016-10-24 13:42:18 +01002241 reg->dirty = true;
2242 }
Chris Wilsoneedd10f2014-06-16 08:57:44 +01002243}
2244
Chris Wilsond8cb5082012-08-11 15:41:03 +01002245static int i915_gem_object_create_mmap_offset(struct drm_i915_gem_object *obj)
2246{
Chris Wilsonfac5e232016-07-04 11:34:36 +01002247 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
Chris Wilsonf3f61842016-08-05 10:14:14 +01002248 int err;
Chris Wilsond8cb5082012-08-11 15:41:03 +01002249
Chris Wilsonf3f61842016-08-05 10:14:14 +01002250 err = drm_gem_create_mmap_offset(&obj->base);
Chris Wilsonb42a13d2017-01-06 15:22:40 +00002251 if (likely(!err))
Chris Wilsonf3f61842016-08-05 10:14:14 +01002252 return 0;
Daniel Vetterda494d72012-12-20 15:11:16 +01002253
Chris Wilsonb42a13d2017-01-06 15:22:40 +00002254 /* Attempt to reap some mmap space from dead objects */
2255 do {
2256 err = i915_gem_wait_for_idle(dev_priv, I915_WAIT_INTERRUPTIBLE);
2257 if (err)
2258 break;
Chris Wilsond8cb5082012-08-11 15:41:03 +01002259
Chris Wilsonb42a13d2017-01-06 15:22:40 +00002260 i915_gem_drain_freed_objects(dev_priv);
Chris Wilsonf3f61842016-08-05 10:14:14 +01002261 err = drm_gem_create_mmap_offset(&obj->base);
Chris Wilsonb42a13d2017-01-06 15:22:40 +00002262 if (!err)
2263 break;
2264
2265 } while (flush_delayed_work(&dev_priv->gt.retire_work));
Daniel Vetterda494d72012-12-20 15:11:16 +01002266
Chris Wilsonf3f61842016-08-05 10:14:14 +01002267 return err;
Chris Wilsond8cb5082012-08-11 15:41:03 +01002268}
2269
2270static void i915_gem_object_free_mmap_offset(struct drm_i915_gem_object *obj)
2271{
Chris Wilsond8cb5082012-08-11 15:41:03 +01002272 drm_gem_free_mmap_offset(&obj->base);
2273}
2274
Dave Airlieda6b51d2014-12-24 13:11:17 +10002275int
Dave Airlieff72145b2011-02-07 12:16:14 +10002276i915_gem_mmap_gtt(struct drm_file *file,
2277 struct drm_device *dev,
Dave Airlieda6b51d2014-12-24 13:11:17 +10002278 uint32_t handle,
Dave Airlieff72145b2011-02-07 12:16:14 +10002279 uint64_t *offset)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002280{
Chris Wilson05394f32010-11-08 19:18:58 +00002281 struct drm_i915_gem_object *obj;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002282 int ret;
2283
Chris Wilson03ac0642016-07-20 13:31:51 +01002284 obj = i915_gem_object_lookup(file, handle);
Chris Wilsonf3f61842016-08-05 10:14:14 +01002285 if (!obj)
2286 return -ENOENT;
Chris Wilsonab182822009-09-22 18:46:17 +01002287
Chris Wilsond8cb5082012-08-11 15:41:03 +01002288 ret = i915_gem_object_create_mmap_offset(obj);
Chris Wilsonf3f61842016-08-05 10:14:14 +01002289 if (ret == 0)
2290 *offset = drm_vma_node_offset_addr(&obj->base.vma_node);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002291
Chris Wilsonf0cd5182016-10-28 13:58:43 +01002292 i915_gem_object_put(obj);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01002293 return ret;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002294}
2295
Dave Airlieff72145b2011-02-07 12:16:14 +10002296/**
2297 * i915_gem_mmap_gtt_ioctl - prepare an object for GTT mmap'ing
2298 * @dev: DRM device
2299 * @data: GTT mapping ioctl data
2300 * @file: GEM object info
2301 *
2302 * Simply returns the fake offset to userspace so it can mmap it.
2303 * The mmap call will end up in drm_gem_mmap(), which will set things
2304 * up so we can get faults in the handler above.
2305 *
2306 * The fault handler will take care of binding the object into the GTT
2307 * (since it may have been evicted to make room for something), allocating
2308 * a fence register, and mapping the appropriate aperture address into
2309 * userspace.
2310 */
2311int
2312i915_gem_mmap_gtt_ioctl(struct drm_device *dev, void *data,
2313 struct drm_file *file)
2314{
2315 struct drm_i915_gem_mmap_gtt *args = data;
2316
Dave Airlieda6b51d2014-12-24 13:11:17 +10002317 return i915_gem_mmap_gtt(file, dev, args->handle, &args->offset);
Dave Airlieff72145b2011-02-07 12:16:14 +10002318}
2319
Daniel Vetter225067e2012-08-20 10:23:20 +02002320/* Immediately discard the backing storage */
2321static void
2322i915_gem_object_truncate(struct drm_i915_gem_object *obj)
Chris Wilsone5281cc2010-10-28 13:45:36 +01002323{
Chris Wilson4d6294bf2012-08-11 15:41:05 +01002324 i915_gem_object_free_mmap_offset(obj);
Daniel Vetter1286ff72012-05-10 15:25:09 +02002325
Chris Wilson4d6294bf2012-08-11 15:41:05 +01002326 if (obj->base.filp == NULL)
2327 return;
2328
Daniel Vetter225067e2012-08-20 10:23:20 +02002329 /* Our goal here is to return as much of the memory as
2330 * is possible back to the system as we are called from OOM.
2331 * To do this we must instruct the shmfs to drop all of its
2332 * backing pages, *now*.
Chris Wilsone5281cc2010-10-28 13:45:36 +01002333 */
Chris Wilson55372522014-03-25 13:23:06 +00002334 shmem_truncate_range(file_inode(obj->base.filp), 0, (loff_t)-1);
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002335 obj->mm.madv = __I915_MADV_PURGED;
Chris Wilson4e5462e2017-03-07 13:20:31 +00002336 obj->mm.pages = ERR_PTR(-EFAULT);
Chris Wilsone5281cc2010-10-28 13:45:36 +01002337}
Chris Wilsone5281cc2010-10-28 13:45:36 +01002338
Chris Wilson55372522014-03-25 13:23:06 +00002339/* Try to discard unwanted pages */
Chris Wilson03ac84f2016-10-28 13:58:36 +01002340void __i915_gem_object_invalidate(struct drm_i915_gem_object *obj)
Daniel Vetter225067e2012-08-20 10:23:20 +02002341{
Chris Wilson55372522014-03-25 13:23:06 +00002342 struct address_space *mapping;
2343
Chris Wilson1233e2d2016-10-28 13:58:37 +01002344 lockdep_assert_held(&obj->mm.lock);
Chris Wilsonf1fa4f42017-10-13 21:26:13 +01002345 GEM_BUG_ON(i915_gem_object_has_pages(obj));
Chris Wilson1233e2d2016-10-28 13:58:37 +01002346
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002347 switch (obj->mm.madv) {
Chris Wilson55372522014-03-25 13:23:06 +00002348 case I915_MADV_DONTNEED:
2349 i915_gem_object_truncate(obj);
2350 case __I915_MADV_PURGED:
2351 return;
2352 }
2353
2354 if (obj->base.filp == NULL)
2355 return;
2356
Al Viro93c76a32015-12-04 23:45:44 -05002357 mapping = obj->base.filp->f_mapping,
Chris Wilson55372522014-03-25 13:23:06 +00002358 invalidate_mapping_pages(mapping, 0, (loff_t)-1);
Chris Wilsone5281cc2010-10-28 13:45:36 +01002359}
2360
Chris Wilson5cdf5882010-09-27 15:51:07 +01002361static void
Chris Wilson03ac84f2016-10-28 13:58:36 +01002362i915_gem_object_put_pages_gtt(struct drm_i915_gem_object *obj,
2363 struct sg_table *pages)
Eric Anholt673a3942008-07-30 12:06:12 -07002364{
Dave Gordon85d12252016-05-20 11:54:06 +01002365 struct sgt_iter sgt_iter;
2366 struct page *page;
Daniel Vetter1286ff72012-05-10 15:25:09 +02002367
Chris Wilsone5facdf2016-12-23 14:57:57 +00002368 __i915_gem_object_release_shmem(obj, pages, true);
Eric Anholt856fa192009-03-19 14:10:50 -07002369
Chris Wilson03ac84f2016-10-28 13:58:36 +01002370 i915_gem_gtt_finish_pages(obj, pages);
Imre Deake2273302015-07-09 12:59:05 +03002371
Daniel Vetter6dacfd22011-09-12 21:30:02 +02002372 if (i915_gem_object_needs_bit17_swizzle(obj))
Chris Wilson03ac84f2016-10-28 13:58:36 +01002373 i915_gem_object_save_bit_17_swizzle(obj, pages);
Eric Anholt280b7132009-03-12 16:56:27 -07002374
Chris Wilson03ac84f2016-10-28 13:58:36 +01002375 for_each_sgt_page(page, sgt_iter, pages) {
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002376 if (obj->mm.dirty)
Chris Wilson9da3da62012-06-01 15:20:22 +01002377 set_page_dirty(page);
Chris Wilson3ef94da2009-09-14 16:50:29 +01002378
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002379 if (obj->mm.madv == I915_MADV_WILLNEED)
Chris Wilson9da3da62012-06-01 15:20:22 +01002380 mark_page_accessed(page);
Chris Wilson3ef94da2009-09-14 16:50:29 +01002381
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002382 put_page(page);
Chris Wilson3ef94da2009-09-14 16:50:29 +01002383 }
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002384 obj->mm.dirty = false;
Eric Anholt673a3942008-07-30 12:06:12 -07002385
Chris Wilson03ac84f2016-10-28 13:58:36 +01002386 sg_free_table(pages);
2387 kfree(pages);
Chris Wilson37e680a2012-06-07 15:38:42 +01002388}
2389
Chris Wilson96d77632016-10-28 13:58:33 +01002390static void __i915_gem_object_reset_page_iter(struct drm_i915_gem_object *obj)
2391{
2392 struct radix_tree_iter iter;
Ville Syrjäläc23aa712017-09-01 20:12:51 +03002393 void __rcu **slot;
Chris Wilson96d77632016-10-28 13:58:33 +01002394
Chris Wilsonbea6e982017-10-26 14:00:31 +01002395 rcu_read_lock();
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002396 radix_tree_for_each_slot(slot, &obj->mm.get_page.radix, &iter, 0)
2397 radix_tree_delete(&obj->mm.get_page.radix, iter.index);
Chris Wilsonbea6e982017-10-26 14:00:31 +01002398 rcu_read_unlock();
Chris Wilson96d77632016-10-28 13:58:33 +01002399}
2400
Chris Wilson548625e2016-11-01 12:11:34 +00002401void __i915_gem_object_put_pages(struct drm_i915_gem_object *obj,
2402 enum i915_mm_subclass subclass)
Chris Wilson37e680a2012-06-07 15:38:42 +01002403{
Chris Wilsonf2123812017-10-16 12:40:37 +01002404 struct drm_i915_private *i915 = to_i915(obj->base.dev);
Chris Wilson03ac84f2016-10-28 13:58:36 +01002405 struct sg_table *pages;
Chris Wilson37e680a2012-06-07 15:38:42 +01002406
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002407 if (i915_gem_object_has_pinned_pages(obj))
Chris Wilson03ac84f2016-10-28 13:58:36 +01002408 return;
Chris Wilsona5570172012-09-04 21:02:54 +01002409
Chris Wilson15717de2016-08-04 07:52:26 +01002410 GEM_BUG_ON(obj->bind_count);
Chris Wilsonf1fa4f42017-10-13 21:26:13 +01002411 if (!i915_gem_object_has_pages(obj))
Chris Wilson1233e2d2016-10-28 13:58:37 +01002412 return;
2413
2414 /* May be called by shrinker from within get_pages() (on another bo) */
Chris Wilson548625e2016-11-01 12:11:34 +00002415 mutex_lock_nested(&obj->mm.lock, subclass);
Chris Wilson1233e2d2016-10-28 13:58:37 +01002416 if (unlikely(atomic_read(&obj->mm.pages_pin_count)))
2417 goto unlock;
Ben Widawsky3e123022013-07-31 17:00:04 -07002418
Chris Wilsona2165e32012-12-03 11:49:00 +00002419 /* ->put_pages might need to allocate memory for the bit17 swizzle
2420 * array, hence protect them from being reaped by removing them from gtt
2421 * lists early. */
Chris Wilson03ac84f2016-10-28 13:58:36 +01002422 pages = fetch_and_zero(&obj->mm.pages);
2423 GEM_BUG_ON(!pages);
Chris Wilsona2165e32012-12-03 11:49:00 +00002424
Chris Wilsonf2123812017-10-16 12:40:37 +01002425 spin_lock(&i915->mm.obj_lock);
2426 list_del(&obj->mm.link);
2427 spin_unlock(&i915->mm.obj_lock);
2428
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002429 if (obj->mm.mapping) {
Chris Wilson4b30cb22016-08-18 17:16:42 +01002430 void *ptr;
2431
Chris Wilson0ce81782017-05-17 13:09:59 +01002432 ptr = page_mask_bits(obj->mm.mapping);
Chris Wilson4b30cb22016-08-18 17:16:42 +01002433 if (is_vmalloc_addr(ptr))
2434 vunmap(ptr);
Chris Wilsonfb8621d2016-04-08 12:11:14 +01002435 else
Chris Wilson4b30cb22016-08-18 17:16:42 +01002436 kunmap(kmap_to_page(ptr));
2437
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002438 obj->mm.mapping = NULL;
Chris Wilson0a798eb2016-04-08 12:11:11 +01002439 }
2440
Chris Wilson96d77632016-10-28 13:58:33 +01002441 __i915_gem_object_reset_page_iter(obj);
2442
Chris Wilson4e5462e2017-03-07 13:20:31 +00002443 if (!IS_ERR(pages))
2444 obj->ops->put_pages(obj, pages);
2445
Matthew Aulda5c081662017-10-06 23:18:18 +01002446 obj->mm.page_sizes.phys = obj->mm.page_sizes.sg = 0;
2447
Chris Wilson1233e2d2016-10-28 13:58:37 +01002448unlock:
2449 mutex_unlock(&obj->mm.lock);
Chris Wilson6c085a72012-08-20 11:40:46 +02002450}
2451
Chris Wilson935a2f72017-02-13 17:15:13 +00002452static bool i915_sg_trim(struct sg_table *orig_st)
Tvrtko Ursulin0c40ce12016-11-09 15:13:43 +00002453{
2454 struct sg_table new_st;
2455 struct scatterlist *sg, *new_sg;
2456 unsigned int i;
2457
2458 if (orig_st->nents == orig_st->orig_nents)
Chris Wilson935a2f72017-02-13 17:15:13 +00002459 return false;
Tvrtko Ursulin0c40ce12016-11-09 15:13:43 +00002460
Chris Wilson8bfc478f2016-12-23 14:57:58 +00002461 if (sg_alloc_table(&new_st, orig_st->nents, GFP_KERNEL | __GFP_NOWARN))
Chris Wilson935a2f72017-02-13 17:15:13 +00002462 return false;
Tvrtko Ursulin0c40ce12016-11-09 15:13:43 +00002463
2464 new_sg = new_st.sgl;
2465 for_each_sg(orig_st->sgl, sg, orig_st->nents, i) {
2466 sg_set_page(new_sg, sg_page(sg), sg->length, 0);
2467 /* called before being DMA mapped, no need to copy sg->dma_* */
2468 new_sg = sg_next(new_sg);
2469 }
Chris Wilsonc2dc6cc2016-12-19 12:43:46 +00002470 GEM_BUG_ON(new_sg); /* Should walk exactly nents and hit the end */
Tvrtko Ursulin0c40ce12016-11-09 15:13:43 +00002471
2472 sg_free_table(orig_st);
2473
2474 *orig_st = new_st;
Chris Wilson935a2f72017-02-13 17:15:13 +00002475 return true;
Tvrtko Ursulin0c40ce12016-11-09 15:13:43 +00002476}
2477
Matthew Auldb91b09e2017-10-06 23:18:17 +01002478static int i915_gem_object_get_pages_gtt(struct drm_i915_gem_object *obj)
Eric Anholt673a3942008-07-30 12:06:12 -07002479{
Chris Wilsonfac5e232016-07-04 11:34:36 +01002480 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
Chris Wilsond766ef52016-12-19 12:43:45 +00002481 const unsigned long page_count = obj->base.size / PAGE_SIZE;
2482 unsigned long i;
Eric Anholt673a3942008-07-30 12:06:12 -07002483 struct address_space *mapping;
Chris Wilson9da3da62012-06-01 15:20:22 +01002484 struct sg_table *st;
2485 struct scatterlist *sg;
Dave Gordon85d12252016-05-20 11:54:06 +01002486 struct sgt_iter sgt_iter;
Eric Anholt673a3942008-07-30 12:06:12 -07002487 struct page *page;
Imre Deak90797e62013-02-18 19:28:03 +02002488 unsigned long last_pfn = 0; /* suppress gcc warning */
Tvrtko Ursulin56024522017-08-03 10:14:17 +01002489 unsigned int max_segment = i915_sg_segment_size();
Matthew Auld84e89782017-10-09 12:00:24 +01002490 unsigned int sg_page_sizes;
Chris Wilson4846bf02017-06-09 12:03:46 +01002491 gfp_t noreclaim;
Imre Deake2273302015-07-09 12:59:05 +03002492 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07002493
Chris Wilson6c085a72012-08-20 11:40:46 +02002494 /* Assert that the object is not currently in any GPU domain. As it
2495 * wasn't in the GTT, there shouldn't be any way it could have been in
2496 * a GPU cache
2497 */
Christian Königc0a51fd2018-02-16 13:43:38 +01002498 GEM_BUG_ON(obj->read_domains & I915_GEM_GPU_DOMAINS);
2499 GEM_BUG_ON(obj->write_domain & I915_GEM_GPU_DOMAINS);
Chris Wilson6c085a72012-08-20 11:40:46 +02002500
Chris Wilson9da3da62012-06-01 15:20:22 +01002501 st = kmalloc(sizeof(*st), GFP_KERNEL);
2502 if (st == NULL)
Matthew Auldb91b09e2017-10-06 23:18:17 +01002503 return -ENOMEM;
Eric Anholt673a3942008-07-30 12:06:12 -07002504
Chris Wilsond766ef52016-12-19 12:43:45 +00002505rebuild_st:
Chris Wilson9da3da62012-06-01 15:20:22 +01002506 if (sg_alloc_table(st, page_count, GFP_KERNEL)) {
Chris Wilson9da3da62012-06-01 15:20:22 +01002507 kfree(st);
Matthew Auldb91b09e2017-10-06 23:18:17 +01002508 return -ENOMEM;
Chris Wilson9da3da62012-06-01 15:20:22 +01002509 }
2510
2511 /* Get the list of pages out of our struct file. They'll be pinned
2512 * at this point until we release them.
2513 *
2514 * Fail silently without starting the shrinker
2515 */
Al Viro93c76a32015-12-04 23:45:44 -05002516 mapping = obj->base.filp->f_mapping;
Chris Wilson0f6ab552017-06-09 12:03:48 +01002517 noreclaim = mapping_gfp_constraint(mapping, ~__GFP_RECLAIM);
Chris Wilson4846bf02017-06-09 12:03:46 +01002518 noreclaim |= __GFP_NORETRY | __GFP_NOWARN;
2519
Imre Deak90797e62013-02-18 19:28:03 +02002520 sg = st->sgl;
2521 st->nents = 0;
Matthew Auld84e89782017-10-09 12:00:24 +01002522 sg_page_sizes = 0;
Imre Deak90797e62013-02-18 19:28:03 +02002523 for (i = 0; i < page_count; i++) {
Chris Wilson4846bf02017-06-09 12:03:46 +01002524 const unsigned int shrink[] = {
2525 I915_SHRINK_BOUND | I915_SHRINK_UNBOUND | I915_SHRINK_PURGEABLE,
2526 0,
2527 }, *s = shrink;
2528 gfp_t gfp = noreclaim;
2529
2530 do {
Chris Wilson6c085a72012-08-20 11:40:46 +02002531 page = shmem_read_mapping_page_gfp(mapping, i, gfp);
Chris Wilson4846bf02017-06-09 12:03:46 +01002532 if (likely(!IS_ERR(page)))
2533 break;
2534
2535 if (!*s) {
2536 ret = PTR_ERR(page);
2537 goto err_sg;
2538 }
2539
Chris Wilson912d5722017-09-06 16:19:30 -07002540 i915_gem_shrink(dev_priv, 2 * page_count, NULL, *s++);
Chris Wilson4846bf02017-06-09 12:03:46 +01002541 cond_resched();
Chris Wilson24f8e002017-03-22 11:05:21 +00002542
Chris Wilson6c085a72012-08-20 11:40:46 +02002543 /* We've tried hard to allocate the memory by reaping
2544 * our own buffer, now let the real VM do its job and
2545 * go down in flames if truly OOM.
Chris Wilson24f8e002017-03-22 11:05:21 +00002546 *
2547 * However, since graphics tend to be disposable,
2548 * defer the oom here by reporting the ENOMEM back
2549 * to userspace.
Chris Wilson6c085a72012-08-20 11:40:46 +02002550 */
Chris Wilson4846bf02017-06-09 12:03:46 +01002551 if (!*s) {
2552 /* reclaim and warn, but no oom */
2553 gfp = mapping_gfp_mask(mapping);
Chris Wilsoneaf41802017-06-09 12:03:47 +01002554
2555 /* Our bo are always dirty and so we require
2556 * kswapd to reclaim our pages (direct reclaim
2557 * does not effectively begin pageout of our
2558 * buffers on its own). However, direct reclaim
2559 * only waits for kswapd when under allocation
2560 * congestion. So as a result __GFP_RECLAIM is
2561 * unreliable and fails to actually reclaim our
2562 * dirty pages -- unless you try over and over
2563 * again with !__GFP_NORETRY. However, we still
2564 * want to fail this allocation rather than
2565 * trigger the out-of-memory killer and for
Michal Hockodbb32952017-07-12 14:36:55 -07002566 * this we want __GFP_RETRY_MAYFAIL.
Chris Wilsoneaf41802017-06-09 12:03:47 +01002567 */
Michal Hockodbb32952017-07-12 14:36:55 -07002568 gfp |= __GFP_RETRY_MAYFAIL;
Imre Deake2273302015-07-09 12:59:05 +03002569 }
Chris Wilson4846bf02017-06-09 12:03:46 +01002570 } while (1);
2571
Chris Wilson871dfbd2016-10-11 09:20:21 +01002572 if (!i ||
2573 sg->length >= max_segment ||
2574 page_to_pfn(page) != last_pfn + 1) {
Matthew Aulda5c081662017-10-06 23:18:18 +01002575 if (i) {
Matthew Auld84e89782017-10-09 12:00:24 +01002576 sg_page_sizes |= sg->length;
Imre Deak90797e62013-02-18 19:28:03 +02002577 sg = sg_next(sg);
Matthew Aulda5c081662017-10-06 23:18:18 +01002578 }
Imre Deak90797e62013-02-18 19:28:03 +02002579 st->nents++;
2580 sg_set_page(sg, page, PAGE_SIZE, 0);
2581 } else {
2582 sg->length += PAGE_SIZE;
2583 }
2584 last_pfn = page_to_pfn(page);
Daniel Vetter3bbbe702013-10-07 17:15:45 -03002585
2586 /* Check that the i965g/gm workaround works. */
2587 WARN_ON((gfp & __GFP_DMA32) && (last_pfn >= 0x00100000UL));
Eric Anholt673a3942008-07-30 12:06:12 -07002588 }
Matthew Aulda5c081662017-10-06 23:18:18 +01002589 if (sg) { /* loop terminated early; short sg table */
Matthew Auld84e89782017-10-09 12:00:24 +01002590 sg_page_sizes |= sg->length;
Konrad Rzeszutek Wilk426729d2013-06-24 11:47:48 -04002591 sg_mark_end(sg);
Matthew Aulda5c081662017-10-06 23:18:18 +01002592 }
Chris Wilson74ce6b62012-10-19 15:51:06 +01002593
Tvrtko Ursulin0c40ce12016-11-09 15:13:43 +00002594 /* Trim unused sg entries to avoid wasting memory. */
2595 i915_sg_trim(st);
2596
Chris Wilson03ac84f2016-10-28 13:58:36 +01002597 ret = i915_gem_gtt_prepare_pages(obj, st);
Chris Wilsond766ef52016-12-19 12:43:45 +00002598 if (ret) {
2599 /* DMA remapping failed? One possible cause is that
2600 * it could not reserve enough large entries, asking
2601 * for PAGE_SIZE chunks instead may be helpful.
2602 */
2603 if (max_segment > PAGE_SIZE) {
2604 for_each_sgt_page(page, sgt_iter, st)
2605 put_page(page);
2606 sg_free_table(st);
2607
2608 max_segment = PAGE_SIZE;
2609 goto rebuild_st;
2610 } else {
2611 dev_warn(&dev_priv->drm.pdev->dev,
2612 "Failed to DMA remap %lu pages\n",
2613 page_count);
2614 goto err_pages;
2615 }
2616 }
Imre Deake2273302015-07-09 12:59:05 +03002617
Eric Anholt673a3942008-07-30 12:06:12 -07002618 if (i915_gem_object_needs_bit17_swizzle(obj))
Chris Wilson03ac84f2016-10-28 13:58:36 +01002619 i915_gem_object_do_bit_17_swizzle(obj, st);
Eric Anholt673a3942008-07-30 12:06:12 -07002620
Matthew Auld84e89782017-10-09 12:00:24 +01002621 __i915_gem_object_set_pages(obj, st, sg_page_sizes);
Matthew Auldb91b09e2017-10-06 23:18:17 +01002622
2623 return 0;
Eric Anholt673a3942008-07-30 12:06:12 -07002624
Chris Wilsonb17993b2016-11-14 11:29:30 +00002625err_sg:
Imre Deak90797e62013-02-18 19:28:03 +02002626 sg_mark_end(sg);
Chris Wilsonb17993b2016-11-14 11:29:30 +00002627err_pages:
Dave Gordon85d12252016-05-20 11:54:06 +01002628 for_each_sgt_page(page, sgt_iter, st)
2629 put_page(page);
Chris Wilson9da3da62012-06-01 15:20:22 +01002630 sg_free_table(st);
2631 kfree(st);
Chris Wilson0820baf2014-03-25 13:23:03 +00002632
2633 /* shmemfs first checks if there is enough memory to allocate the page
2634 * and reports ENOSPC should there be insufficient, along with the usual
2635 * ENOMEM for a genuine allocation failure.
2636 *
2637 * We use ENOSPC in our driver to mean that we have run out of aperture
2638 * space and so want to translate the error from shmemfs back to our
2639 * usual understanding of ENOMEM.
2640 */
Imre Deake2273302015-07-09 12:59:05 +03002641 if (ret == -ENOSPC)
2642 ret = -ENOMEM;
2643
Matthew Auldb91b09e2017-10-06 23:18:17 +01002644 return ret;
Chris Wilson03ac84f2016-10-28 13:58:36 +01002645}
2646
2647void __i915_gem_object_set_pages(struct drm_i915_gem_object *obj,
Matthew Aulda5c081662017-10-06 23:18:18 +01002648 struct sg_table *pages,
Matthew Auld84e89782017-10-09 12:00:24 +01002649 unsigned int sg_page_sizes)
Chris Wilson03ac84f2016-10-28 13:58:36 +01002650{
Matthew Aulda5c081662017-10-06 23:18:18 +01002651 struct drm_i915_private *i915 = to_i915(obj->base.dev);
2652 unsigned long supported = INTEL_INFO(i915)->page_sizes;
2653 int i;
2654
Chris Wilson1233e2d2016-10-28 13:58:37 +01002655 lockdep_assert_held(&obj->mm.lock);
Chris Wilson03ac84f2016-10-28 13:58:36 +01002656
2657 obj->mm.get_page.sg_pos = pages->sgl;
2658 obj->mm.get_page.sg_idx = 0;
2659
2660 obj->mm.pages = pages;
Chris Wilson2c3a3f42016-11-04 10:30:01 +00002661
2662 if (i915_gem_object_is_tiled(obj) &&
Chris Wilsonf2123812017-10-16 12:40:37 +01002663 i915->quirks & QUIRK_PIN_SWIZZLED_PAGES) {
Chris Wilson2c3a3f42016-11-04 10:30:01 +00002664 GEM_BUG_ON(obj->mm.quirked);
2665 __i915_gem_object_pin_pages(obj);
2666 obj->mm.quirked = true;
2667 }
Matthew Aulda5c081662017-10-06 23:18:18 +01002668
Matthew Auld84e89782017-10-09 12:00:24 +01002669 GEM_BUG_ON(!sg_page_sizes);
2670 obj->mm.page_sizes.phys = sg_page_sizes;
Matthew Aulda5c081662017-10-06 23:18:18 +01002671
2672 /*
Matthew Auld84e89782017-10-09 12:00:24 +01002673 * Calculate the supported page-sizes which fit into the given
2674 * sg_page_sizes. This will give us the page-sizes which we may be able
2675 * to use opportunistically when later inserting into the GTT. For
2676 * example if phys=2G, then in theory we should be able to use 1G, 2M,
2677 * 64K or 4K pages, although in practice this will depend on a number of
2678 * other factors.
Matthew Aulda5c081662017-10-06 23:18:18 +01002679 */
2680 obj->mm.page_sizes.sg = 0;
2681 for_each_set_bit(i, &supported, ilog2(I915_GTT_MAX_PAGE_SIZE) + 1) {
2682 if (obj->mm.page_sizes.phys & ~0u << i)
2683 obj->mm.page_sizes.sg |= BIT(i);
2684 }
Matthew Aulda5c081662017-10-06 23:18:18 +01002685 GEM_BUG_ON(!HAS_PAGE_SIZES(i915, obj->mm.page_sizes.sg));
Chris Wilsonf2123812017-10-16 12:40:37 +01002686
2687 spin_lock(&i915->mm.obj_lock);
2688 list_add(&obj->mm.link, &i915->mm.unbound_list);
2689 spin_unlock(&i915->mm.obj_lock);
Chris Wilson03ac84f2016-10-28 13:58:36 +01002690}
2691
2692static int ____i915_gem_object_get_pages(struct drm_i915_gem_object *obj)
2693{
Matthew Auldb91b09e2017-10-06 23:18:17 +01002694 int err;
Chris Wilson03ac84f2016-10-28 13:58:36 +01002695
2696 if (unlikely(obj->mm.madv != I915_MADV_WILLNEED)) {
2697 DRM_DEBUG("Attempting to obtain a purgeable object\n");
2698 return -EFAULT;
2699 }
2700
Matthew Auldb91b09e2017-10-06 23:18:17 +01002701 err = obj->ops->get_pages(obj);
Matthew Auldb65a9b92017-12-18 10:38:55 +00002702 GEM_BUG_ON(!err && !i915_gem_object_has_pages(obj));
Chris Wilson03ac84f2016-10-28 13:58:36 +01002703
Matthew Auldb91b09e2017-10-06 23:18:17 +01002704 return err;
Eric Anholt673a3942008-07-30 12:06:12 -07002705}
2706
Chris Wilson37e680a2012-06-07 15:38:42 +01002707/* Ensure that the associated pages are gathered from the backing storage
Chris Wilson1233e2d2016-10-28 13:58:37 +01002708 * and pinned into our object. i915_gem_object_pin_pages() may be called
Chris Wilson37e680a2012-06-07 15:38:42 +01002709 * multiple times before they are released by a single call to
Chris Wilson1233e2d2016-10-28 13:58:37 +01002710 * i915_gem_object_unpin_pages() - once the pages are no longer referenced
Chris Wilson37e680a2012-06-07 15:38:42 +01002711 * either as a result of memory pressure (reaping pages under the shrinker)
2712 * or as the object is itself released.
2713 */
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002714int __i915_gem_object_get_pages(struct drm_i915_gem_object *obj)
Chris Wilson37e680a2012-06-07 15:38:42 +01002715{
Chris Wilson03ac84f2016-10-28 13:58:36 +01002716 int err;
Chris Wilson37e680a2012-06-07 15:38:42 +01002717
Chris Wilson1233e2d2016-10-28 13:58:37 +01002718 err = mutex_lock_interruptible(&obj->mm.lock);
2719 if (err)
2720 return err;
Chris Wilson4c7d62c2016-10-28 13:58:32 +01002721
Chris Wilsonf1fa4f42017-10-13 21:26:13 +01002722 if (unlikely(!i915_gem_object_has_pages(obj))) {
Chris Wilson88c880b2017-09-06 14:52:20 +01002723 GEM_BUG_ON(i915_gem_object_has_pinned_pages(obj));
2724
Chris Wilson2c3a3f42016-11-04 10:30:01 +00002725 err = ____i915_gem_object_get_pages(obj);
2726 if (err)
2727 goto unlock;
2728
2729 smp_mb__before_atomic();
Chris Wilson1233e2d2016-10-28 13:58:37 +01002730 }
Chris Wilson2c3a3f42016-11-04 10:30:01 +00002731 atomic_inc(&obj->mm.pages_pin_count);
Chris Wilson43e28f02013-01-08 10:53:09 +00002732
Chris Wilson1233e2d2016-10-28 13:58:37 +01002733unlock:
2734 mutex_unlock(&obj->mm.lock);
Chris Wilson03ac84f2016-10-28 13:58:36 +01002735 return err;
Eric Anholt673a3942008-07-30 12:06:12 -07002736}
2737
Dave Gordondd6034c2016-05-20 11:54:04 +01002738/* The 'mapping' part of i915_gem_object_pin_map() below */
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002739static void *i915_gem_object_map(const struct drm_i915_gem_object *obj,
2740 enum i915_map_type type)
Dave Gordondd6034c2016-05-20 11:54:04 +01002741{
2742 unsigned long n_pages = obj->base.size >> PAGE_SHIFT;
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002743 struct sg_table *sgt = obj->mm.pages;
Dave Gordon85d12252016-05-20 11:54:06 +01002744 struct sgt_iter sgt_iter;
2745 struct page *page;
Dave Gordonb338fa42016-05-20 11:54:05 +01002746 struct page *stack_pages[32];
2747 struct page **pages = stack_pages;
Dave Gordondd6034c2016-05-20 11:54:04 +01002748 unsigned long i = 0;
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002749 pgprot_t pgprot;
Dave Gordondd6034c2016-05-20 11:54:04 +01002750 void *addr;
2751
2752 /* A single page can always be kmapped */
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002753 if (n_pages == 1 && type == I915_MAP_WB)
Dave Gordondd6034c2016-05-20 11:54:04 +01002754 return kmap(sg_page(sgt->sgl));
2755
Dave Gordonb338fa42016-05-20 11:54:05 +01002756 if (n_pages > ARRAY_SIZE(stack_pages)) {
2757 /* Too big for stack -- allocate temporary array instead */
Michal Hocko0ee931c2017-09-13 16:28:29 -07002758 pages = kvmalloc_array(n_pages, sizeof(*pages), GFP_KERNEL);
Dave Gordonb338fa42016-05-20 11:54:05 +01002759 if (!pages)
2760 return NULL;
2761 }
Dave Gordondd6034c2016-05-20 11:54:04 +01002762
Dave Gordon85d12252016-05-20 11:54:06 +01002763 for_each_sgt_page(page, sgt_iter, sgt)
2764 pages[i++] = page;
Dave Gordondd6034c2016-05-20 11:54:04 +01002765
2766 /* Check that we have the expected number of pages */
2767 GEM_BUG_ON(i != n_pages);
2768
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002769 switch (type) {
Chris Wilsona575c672017-08-28 11:46:31 +01002770 default:
2771 MISSING_CASE(type);
2772 /* fallthrough to use PAGE_KERNEL anyway */
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002773 case I915_MAP_WB:
2774 pgprot = PAGE_KERNEL;
2775 break;
2776 case I915_MAP_WC:
2777 pgprot = pgprot_writecombine(PAGE_KERNEL_IO);
2778 break;
2779 }
2780 addr = vmap(pages, n_pages, 0, pgprot);
Dave Gordondd6034c2016-05-20 11:54:04 +01002781
Dave Gordonb338fa42016-05-20 11:54:05 +01002782 if (pages != stack_pages)
Michal Hocko20981052017-05-17 14:23:12 +02002783 kvfree(pages);
Dave Gordondd6034c2016-05-20 11:54:04 +01002784
2785 return addr;
2786}
2787
2788/* get, pin, and map the pages of the object into kernel space */
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002789void *i915_gem_object_pin_map(struct drm_i915_gem_object *obj,
2790 enum i915_map_type type)
Chris Wilson0a798eb2016-04-08 12:11:11 +01002791{
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002792 enum i915_map_type has_type;
2793 bool pinned;
2794 void *ptr;
Chris Wilson0a798eb2016-04-08 12:11:11 +01002795 int ret;
2796
Tina Zhanga03f3952017-11-14 10:25:13 +00002797 if (unlikely(!i915_gem_object_has_struct_page(obj)))
2798 return ERR_PTR(-ENXIO);
Chris Wilson0a798eb2016-04-08 12:11:11 +01002799
Chris Wilson1233e2d2016-10-28 13:58:37 +01002800 ret = mutex_lock_interruptible(&obj->mm.lock);
Chris Wilson0a798eb2016-04-08 12:11:11 +01002801 if (ret)
2802 return ERR_PTR(ret);
2803
Chris Wilsona575c672017-08-28 11:46:31 +01002804 pinned = !(type & I915_MAP_OVERRIDE);
2805 type &= ~I915_MAP_OVERRIDE;
2806
Chris Wilson1233e2d2016-10-28 13:58:37 +01002807 if (!atomic_inc_not_zero(&obj->mm.pages_pin_count)) {
Chris Wilsonf1fa4f42017-10-13 21:26:13 +01002808 if (unlikely(!i915_gem_object_has_pages(obj))) {
Chris Wilson88c880b2017-09-06 14:52:20 +01002809 GEM_BUG_ON(i915_gem_object_has_pinned_pages(obj));
2810
Chris Wilson2c3a3f42016-11-04 10:30:01 +00002811 ret = ____i915_gem_object_get_pages(obj);
2812 if (ret)
2813 goto err_unlock;
Chris Wilson1233e2d2016-10-28 13:58:37 +01002814
Chris Wilson2c3a3f42016-11-04 10:30:01 +00002815 smp_mb__before_atomic();
2816 }
2817 atomic_inc(&obj->mm.pages_pin_count);
Chris Wilson1233e2d2016-10-28 13:58:37 +01002818 pinned = false;
2819 }
Chris Wilsonf1fa4f42017-10-13 21:26:13 +01002820 GEM_BUG_ON(!i915_gem_object_has_pages(obj));
Chris Wilson0a798eb2016-04-08 12:11:11 +01002821
Chris Wilson0ce81782017-05-17 13:09:59 +01002822 ptr = page_unpack_bits(obj->mm.mapping, &has_type);
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002823 if (ptr && has_type != type) {
2824 if (pinned) {
2825 ret = -EBUSY;
Chris Wilson1233e2d2016-10-28 13:58:37 +01002826 goto err_unpin;
Chris Wilson0a798eb2016-04-08 12:11:11 +01002827 }
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002828
2829 if (is_vmalloc_addr(ptr))
2830 vunmap(ptr);
2831 else
2832 kunmap(kmap_to_page(ptr));
2833
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002834 ptr = obj->mm.mapping = NULL;
Chris Wilson0a798eb2016-04-08 12:11:11 +01002835 }
2836
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002837 if (!ptr) {
2838 ptr = i915_gem_object_map(obj, type);
2839 if (!ptr) {
2840 ret = -ENOMEM;
Chris Wilson1233e2d2016-10-28 13:58:37 +01002841 goto err_unpin;
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002842 }
2843
Chris Wilson0ce81782017-05-17 13:09:59 +01002844 obj->mm.mapping = page_pack_bits(ptr, type);
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002845 }
2846
Chris Wilson1233e2d2016-10-28 13:58:37 +01002847out_unlock:
2848 mutex_unlock(&obj->mm.lock);
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002849 return ptr;
2850
Chris Wilson1233e2d2016-10-28 13:58:37 +01002851err_unpin:
2852 atomic_dec(&obj->mm.pages_pin_count);
2853err_unlock:
2854 ptr = ERR_PTR(ret);
2855 goto out_unlock;
Chris Wilson0a798eb2016-04-08 12:11:11 +01002856}
2857
Chris Wilson7c55e2c2017-03-07 12:03:38 +00002858static int
2859i915_gem_object_pwrite_gtt(struct drm_i915_gem_object *obj,
2860 const struct drm_i915_gem_pwrite *arg)
2861{
2862 struct address_space *mapping = obj->base.filp->f_mapping;
2863 char __user *user_data = u64_to_user_ptr(arg->data_ptr);
2864 u64 remain, offset;
2865 unsigned int pg;
2866
2867 /* Before we instantiate/pin the backing store for our use, we
2868 * can prepopulate the shmemfs filp efficiently using a write into
2869 * the pagecache. We avoid the penalty of instantiating all the
2870 * pages, important if the user is just writing to a few and never
2871 * uses the object on the GPU, and using a direct write into shmemfs
2872 * allows it to avoid the cost of retrieving a page (either swapin
2873 * or clearing-before-use) before it is overwritten.
2874 */
Chris Wilsonf1fa4f42017-10-13 21:26:13 +01002875 if (i915_gem_object_has_pages(obj))
Chris Wilson7c55e2c2017-03-07 12:03:38 +00002876 return -ENODEV;
2877
Chris Wilsona6d65e42017-10-16 21:27:32 +01002878 if (obj->mm.madv != I915_MADV_WILLNEED)
2879 return -EFAULT;
2880
Chris Wilson7c55e2c2017-03-07 12:03:38 +00002881 /* Before the pages are instantiated the object is treated as being
2882 * in the CPU domain. The pages will be clflushed as required before
2883 * use, and we can freely write into the pages directly. If userspace
2884 * races pwrite with any other operation; corruption will ensue -
2885 * that is userspace's prerogative!
2886 */
2887
2888 remain = arg->size;
2889 offset = arg->offset;
2890 pg = offset_in_page(offset);
2891
2892 do {
2893 unsigned int len, unwritten;
2894 struct page *page;
2895 void *data, *vaddr;
2896 int err;
2897
2898 len = PAGE_SIZE - pg;
2899 if (len > remain)
2900 len = remain;
2901
2902 err = pagecache_write_begin(obj->base.filp, mapping,
2903 offset, len, 0,
2904 &page, &data);
2905 if (err < 0)
2906 return err;
2907
2908 vaddr = kmap(page);
2909 unwritten = copy_from_user(vaddr + pg, user_data, len);
2910 kunmap(page);
2911
2912 err = pagecache_write_end(obj->base.filp, mapping,
2913 offset, len, len - unwritten,
2914 page, data);
2915 if (err < 0)
2916 return err;
2917
2918 if (unwritten)
2919 return -EFAULT;
2920
2921 remain -= len;
2922 user_data += len;
2923 offset += len;
2924 pg = 0;
2925 } while (remain);
2926
2927 return 0;
2928}
2929
Mika Kuoppalae5e1fc42016-11-16 17:20:31 +02002930static void i915_gem_context_mark_guilty(struct i915_gem_context *ctx)
Mika Kuoppalaaa60c662013-06-12 15:13:20 +03002931{
Chris Wilson77b25a92017-07-21 13:32:30 +01002932 bool banned;
Mika Kuoppalab083a082016-11-18 15:10:47 +02002933
Chris Wilson77b25a92017-07-21 13:32:30 +01002934 atomic_inc(&ctx->guilty_count);
2935
Chris Wilson24eae082018-02-05 09:22:01 +00002936 banned = false;
2937 if (i915_gem_context_is_bannable(ctx)) {
2938 unsigned int score;
2939
2940 score = atomic_add_return(CONTEXT_SCORE_GUILTY,
2941 &ctx->ban_score);
2942 banned = score >= CONTEXT_SCORE_BAN_THRESHOLD;
2943
2944 DRM_DEBUG_DRIVER("context %s marked guilty (score %d) banned? %s\n",
2945 ctx->name, score, yesno(banned));
2946 }
Chris Wilson77b25a92017-07-21 13:32:30 +01002947 if (!banned)
Mika Kuoppalab083a082016-11-18 15:10:47 +02002948 return;
2949
Chris Wilson77b25a92017-07-21 13:32:30 +01002950 i915_gem_context_set_banned(ctx);
2951 if (!IS_ERR_OR_NULL(ctx->file_priv)) {
2952 atomic_inc(&ctx->file_priv->context_bans);
2953 DRM_DEBUG_DRIVER("client %s has had %d context banned\n",
2954 ctx->name, atomic_read(&ctx->file_priv->context_bans));
2955 }
Mika Kuoppalae5e1fc42016-11-16 17:20:31 +02002956}
2957
2958static void i915_gem_context_mark_innocent(struct i915_gem_context *ctx)
2959{
Chris Wilson77b25a92017-07-21 13:32:30 +01002960 atomic_inc(&ctx->active_count);
Mika Kuoppalaaa60c662013-06-12 15:13:20 +03002961}
2962
Chris Wilsone61e0f52018-02-21 09:56:36 +00002963struct i915_request *
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00002964i915_gem_find_active_request(struct intel_engine_cs *engine)
Chris Wilson9375e442010-09-19 12:21:28 +01002965{
Chris Wilsone61e0f52018-02-21 09:56:36 +00002966 struct i915_request *request, *active = NULL;
Chris Wilson754c9fd2017-02-23 07:44:14 +00002967 unsigned long flags;
Mika Kuoppalaaa60c662013-06-12 15:13:20 +03002968
Chris Wilsonf69a02c2016-07-01 17:23:16 +01002969 /* We are called by the error capture and reset at a random
2970 * point in time. In particular, note that neither is crucially
2971 * ordered with an interrupt. After a hang, the GPU is dead and we
2972 * assume that no more writes can happen (we waited long enough for
2973 * all writes that were in transaction to be flushed) - adding an
2974 * extra delay for a recent interrupt is pointless. Hence, we do
2975 * not need an engine->irq_seqno_barrier() before the seqno reads.
2976 */
Chris Wilson754c9fd2017-02-23 07:44:14 +00002977 spin_lock_irqsave(&engine->timeline->lock, flags);
Chris Wilson73cb9702016-10-28 13:58:46 +01002978 list_for_each_entry(request, &engine->timeline->requests, link) {
Chris Wilsone61e0f52018-02-21 09:56:36 +00002979 if (__i915_request_completed(request, request->global_seqno))
Chris Wilson4db080f2013-12-04 11:37:09 +00002980 continue;
Mika Kuoppalaaa60c662013-06-12 15:13:20 +03002981
Mika Kuoppala36193ac2017-01-17 17:59:02 +02002982 GEM_BUG_ON(request->engine != engine);
Chris Wilsonc00122f32017-02-12 17:19:58 +00002983 GEM_BUG_ON(test_bit(DMA_FENCE_FLAG_SIGNALED_BIT,
2984 &request->fence.flags));
Mika Kuoppalab6b0fac2014-01-30 19:04:43 +02002985
Chris Wilson754c9fd2017-02-23 07:44:14 +00002986 active = request;
2987 break;
2988 }
2989 spin_unlock_irqrestore(&engine->timeline->lock, flags);
2990
2991 return active;
Mika Kuoppalab6b0fac2014-01-30 19:04:43 +02002992}
2993
Michel Thierrya1ef70e2017-06-20 10:57:47 +01002994/*
2995 * Ensure irq handler finishes, and not run again.
2996 * Also return the active request so that we only search for it once.
2997 */
Chris Wilsone61e0f52018-02-21 09:56:36 +00002998struct i915_request *
Michel Thierrya1ef70e2017-06-20 10:57:47 +01002999i915_gem_reset_prepare_engine(struct intel_engine_cs *engine)
3000{
Chris Wilsone61e0f52018-02-21 09:56:36 +00003001 struct i915_request *request = NULL;
Michel Thierrya1ef70e2017-06-20 10:57:47 +01003002
Chris Wilson1749d902017-10-09 12:02:59 +01003003 /*
3004 * During the reset sequence, we must prevent the engine from
3005 * entering RC6. As the context state is undefined until we restart
3006 * the engine, if it does enter RC6 during the reset, the state
3007 * written to the powercontext is undefined and so we may lose
3008 * GPU state upon resume, i.e. fail to restart after a reset.
3009 */
3010 intel_uncore_forcewake_get(engine->i915, FORCEWAKE_ALL);
3011
3012 /*
3013 * Prevent the signaler thread from updating the request
Michel Thierrya1ef70e2017-06-20 10:57:47 +01003014 * state (by calling dma_fence_signal) as we are processing
3015 * the reset. The write from the GPU of the seqno is
3016 * asynchronous and the signaler thread may see a different
3017 * value to us and declare the request complete, even though
3018 * the reset routine have picked that request as the active
3019 * (incomplete) request. This conflict is not handled
3020 * gracefully!
3021 */
3022 kthread_park(engine->breadcrumbs.signaler);
3023
Chris Wilson1749d902017-10-09 12:02:59 +01003024 /*
3025 * Prevent request submission to the hardware until we have
Michel Thierrya1ef70e2017-06-20 10:57:47 +01003026 * completed the reset in i915_gem_reset_finish(). If a request
3027 * is completed by one engine, it may then queue a request
Sagar Arun Kamblec6dce8f2017-11-16 19:02:37 +05303028 * to a second via its execlists->tasklet *just* as we are
Michel Thierrya1ef70e2017-06-20 10:57:47 +01003029 * calling engine->init_hw() and also writing the ELSP.
Sagar Arun Kamblec6dce8f2017-11-16 19:02:37 +05303030 * Turning off the execlists->tasklet until the reset is over
Michel Thierrya1ef70e2017-06-20 10:57:47 +01003031 * prevents the race.
Chris Wilson68ad3612018-03-07 13:42:26 +00003032 *
3033 * Note that this needs to be a single atomic operation on the
3034 * tasklet (flush existing tasks, prevent new tasks) to prevent
3035 * a race between reset and set-wedged. It is not, so we do the best
3036 * we can atm and make sure we don't lock the machine up in the more
3037 * common case of recursively being called from set-wedged from inside
3038 * i915_reset.
Michel Thierrya1ef70e2017-06-20 10:57:47 +01003039 */
Chris Wilson68ad3612018-03-07 13:42:26 +00003040 if (!atomic_read(&engine->execlists.tasklet.count))
3041 tasklet_kill(&engine->execlists.tasklet);
Sagar Arun Kamblec6dce8f2017-11-16 19:02:37 +05303042 tasklet_disable(&engine->execlists.tasklet);
Michel Thierrya1ef70e2017-06-20 10:57:47 +01003043
Michał Winiarskic41937f2017-10-26 15:35:58 +02003044 /*
3045 * We're using worker to queue preemption requests from the tasklet in
3046 * GuC submission mode.
3047 * Even though tasklet was disabled, we may still have a worker queued.
3048 * Let's make sure that all workers scheduled before disabling the
3049 * tasklet are completed before continuing with the reset.
3050 */
3051 if (engine->i915->guc.preempt_wq)
3052 flush_workqueue(engine->i915->guc.preempt_wq);
3053
Michel Thierrya1ef70e2017-06-20 10:57:47 +01003054 if (engine->irq_seqno_barrier)
3055 engine->irq_seqno_barrier(engine);
3056
Chris Wilsond1d1ebf42017-07-21 13:32:33 +01003057 request = i915_gem_find_active_request(engine);
3058 if (request && request->fence.error == -EIO)
3059 request = ERR_PTR(-EIO); /* Previous reset failed! */
Michel Thierrya1ef70e2017-06-20 10:57:47 +01003060
3061 return request;
3062}
3063
Chris Wilson0e178ae2017-01-17 17:59:06 +02003064int i915_gem_reset_prepare(struct drm_i915_private *dev_priv)
Chris Wilson4c965542017-01-17 17:59:01 +02003065{
3066 struct intel_engine_cs *engine;
Chris Wilsone61e0f52018-02-21 09:56:36 +00003067 struct i915_request *request;
Chris Wilson4c965542017-01-17 17:59:01 +02003068 enum intel_engine_id id;
Chris Wilson0e178ae2017-01-17 17:59:06 +02003069 int err = 0;
Chris Wilson4c965542017-01-17 17:59:01 +02003070
Chris Wilson0e178ae2017-01-17 17:59:06 +02003071 for_each_engine(engine, dev_priv, id) {
Michel Thierrya1ef70e2017-06-20 10:57:47 +01003072 request = i915_gem_reset_prepare_engine(engine);
3073 if (IS_ERR(request)) {
3074 err = PTR_ERR(request);
3075 continue;
Chris Wilson0e178ae2017-01-17 17:59:06 +02003076 }
Michel Thierryc64992e2017-06-20 10:57:44 +01003077
3078 engine->hangcheck.active_request = request;
Chris Wilson0e178ae2017-01-17 17:59:06 +02003079 }
3080
Chris Wilson4c965542017-01-17 17:59:01 +02003081 i915_gem_revoke_fences(dev_priv);
Michal Wajdeczkoc37d5722018-03-12 13:03:07 +00003082 intel_uc_sanitize(dev_priv);
Chris Wilson0e178ae2017-01-17 17:59:06 +02003083
3084 return err;
Chris Wilson4c965542017-01-17 17:59:01 +02003085}
3086
Chris Wilsone61e0f52018-02-21 09:56:36 +00003087static void skip_request(struct i915_request *request)
Mika Kuoppalab6b0fac2014-01-30 19:04:43 +02003088{
Chris Wilson821ed7d2016-09-09 14:11:53 +01003089 void *vaddr = request->ring->vaddr;
3090 u32 head;
Mika Kuoppalab6b0fac2014-01-30 19:04:43 +02003091
Chris Wilson821ed7d2016-09-09 14:11:53 +01003092 /* As this request likely depends on state from the lost
3093 * context, clear out all the user operations leaving the
3094 * breadcrumb at the end (so we get the fence notifications).
3095 */
3096 head = request->head;
3097 if (request->postfix < head) {
3098 memset(vaddr + head, 0, request->ring->size - head);
3099 head = 0;
3100 }
3101 memset(vaddr + head, 0, request->postfix - head);
Chris Wilsonc0d5f322017-01-10 17:22:43 +00003102
3103 dma_fence_set_error(&request->fence, -EIO);
Chris Wilson4db080f2013-12-04 11:37:09 +00003104}
3105
Chris Wilsone61e0f52018-02-21 09:56:36 +00003106static void engine_skip_context(struct i915_request *request)
Mika Kuoppala36193ac2017-01-17 17:59:02 +02003107{
3108 struct intel_engine_cs *engine = request->engine;
3109 struct i915_gem_context *hung_ctx = request->ctx;
3110 struct intel_timeline *timeline;
3111 unsigned long flags;
3112
3113 timeline = i915_gem_context_lookup_timeline(hung_ctx, engine);
3114
3115 spin_lock_irqsave(&engine->timeline->lock, flags);
3116 spin_lock(&timeline->lock);
3117
3118 list_for_each_entry_continue(request, &engine->timeline->requests, link)
3119 if (request->ctx == hung_ctx)
3120 skip_request(request);
3121
3122 list_for_each_entry(request, &timeline->requests, link)
3123 skip_request(request);
3124
3125 spin_unlock(&timeline->lock);
3126 spin_unlock_irqrestore(&engine->timeline->lock, flags);
3127}
3128
Chris Wilsond1d1ebf42017-07-21 13:32:33 +01003129/* Returns the request if it was guilty of the hang */
Chris Wilsone61e0f52018-02-21 09:56:36 +00003130static struct i915_request *
Chris Wilsond1d1ebf42017-07-21 13:32:33 +01003131i915_gem_reset_request(struct intel_engine_cs *engine,
Chris Wilsonbba08692018-04-06 23:03:53 +01003132 struct i915_request *request,
3133 bool stalled)
Mika Kuoppala61da5362017-01-17 17:59:05 +02003134{
Mika Kuoppala71895a02017-01-17 17:59:07 +02003135 /* The guilty request will get skipped on a hung engine.
3136 *
3137 * Users of client default contexts do not rely on logical
3138 * state preserved between batches so it is safe to execute
3139 * queued requests following the hang. Non default contexts
3140 * rely on preserved state, so skipping a batch loses the
3141 * evolution of the state and it needs to be considered corrupted.
3142 * Executing more queued batches on top of corrupted state is
3143 * risky. But we take the risk by trying to advance through
3144 * the queued requests in order to make the client behaviour
3145 * more predictable around resets, by not throwing away random
3146 * amount of batches it has prepared for execution. Sophisticated
3147 * clients can use gem_reset_stats_ioctl and dma fence status
3148 * (exported via sync_file info ioctl on explicit fences) to observe
3149 * when it loses the context state and should rebuild accordingly.
3150 *
3151 * The context ban, and ultimately the client ban, mechanism are safety
3152 * valves if client submission ends up resulting in nothing more than
3153 * subsequent hangs.
3154 */
3155
Chris Wilsonbba08692018-04-06 23:03:53 +01003156 if (i915_request_completed(request)) {
3157 GEM_TRACE("%s pardoned global=%d (fence %llx:%d), current %d\n",
3158 engine->name, request->global_seqno,
3159 request->fence.context, request->fence.seqno,
3160 intel_engine_get_seqno(engine));
3161 stalled = false;
3162 }
3163
3164 if (stalled) {
Mika Kuoppala61da5362017-01-17 17:59:05 +02003165 i915_gem_context_mark_guilty(request->ctx);
3166 skip_request(request);
Chris Wilsond1d1ebf42017-07-21 13:32:33 +01003167
3168 /* If this context is now banned, skip all pending requests. */
3169 if (i915_gem_context_is_banned(request->ctx))
3170 engine_skip_context(request);
Mika Kuoppala61da5362017-01-17 17:59:05 +02003171 } else {
Chris Wilsond1d1ebf42017-07-21 13:32:33 +01003172 /*
3173 * Since this is not the hung engine, it may have advanced
3174 * since the hang declaration. Double check by refinding
3175 * the active request at the time of the reset.
3176 */
3177 request = i915_gem_find_active_request(engine);
3178 if (request) {
3179 i915_gem_context_mark_innocent(request->ctx);
3180 dma_fence_set_error(&request->fence, -EAGAIN);
3181
3182 /* Rewind the engine to replay the incomplete rq */
3183 spin_lock_irq(&engine->timeline->lock);
3184 request = list_prev_entry(request, link);
3185 if (&request->link == &engine->timeline->requests)
3186 request = NULL;
3187 spin_unlock_irq(&engine->timeline->lock);
3188 }
Mika Kuoppala61da5362017-01-17 17:59:05 +02003189 }
3190
Chris Wilsond1d1ebf42017-07-21 13:32:33 +01003191 return request;
Mika Kuoppala61da5362017-01-17 17:59:05 +02003192}
3193
Michel Thierrya1ef70e2017-06-20 10:57:47 +01003194void i915_gem_reset_engine(struct intel_engine_cs *engine,
Chris Wilsonbba08692018-04-06 23:03:53 +01003195 struct i915_request *request,
3196 bool stalled)
Chris Wilson4db080f2013-12-04 11:37:09 +00003197{
Chris Wilsonfcb1de52017-12-19 09:01:10 +00003198 /*
3199 * Make sure this write is visible before we re-enable the interrupt
3200 * handlers on another CPU, as tasklet_enable() resolves to just
3201 * a compiler barrier which is insufficient for our purpose here.
3202 */
3203 smp_store_mb(engine->irq_posted, 0);
Chris Wilsoned454f22017-07-21 13:32:29 +01003204
Chris Wilsond1d1ebf42017-07-21 13:32:33 +01003205 if (request)
Chris Wilsonbba08692018-04-06 23:03:53 +01003206 request = i915_gem_reset_request(engine, request, stalled);
Chris Wilsond1d1ebf42017-07-21 13:32:33 +01003207
3208 if (request) {
Chris Wilsonc0dcb202017-02-07 15:24:37 +00003209 DRM_DEBUG_DRIVER("resetting %s to restart from tail of request 0x%x\n",
3210 engine->name, request->global_seqno);
Chris Wilsonc0dcb202017-02-07 15:24:37 +00003211 }
Chris Wilson821ed7d2016-09-09 14:11:53 +01003212
3213 /* Setup the CS to resume from the breadcrumb of the hung request */
3214 engine->reset_hw(engine, request);
Chris Wilson821ed7d2016-09-09 14:11:53 +01003215}
3216
Chris Wilsond0667e92018-04-06 23:03:54 +01003217void i915_gem_reset(struct drm_i915_private *dev_priv,
3218 unsigned int stalled_mask)
Chris Wilson821ed7d2016-09-09 14:11:53 +01003219{
3220 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05303221 enum intel_engine_id id;
Chris Wilson821ed7d2016-09-09 14:11:53 +01003222
Chris Wilson4c7d62c2016-10-28 13:58:32 +01003223 lockdep_assert_held(&dev_priv->drm.struct_mutex);
3224
Chris Wilsone61e0f52018-02-21 09:56:36 +00003225 i915_retire_requests(dev_priv);
Chris Wilson821ed7d2016-09-09 14:11:53 +01003226
Chris Wilson2ae55732017-02-12 17:20:02 +00003227 for_each_engine(engine, dev_priv, id) {
3228 struct i915_gem_context *ctx;
3229
Chris Wilsonbba08692018-04-06 23:03:53 +01003230 i915_gem_reset_engine(engine,
3231 engine->hangcheck.active_request,
Chris Wilsond0667e92018-04-06 23:03:54 +01003232 stalled_mask & ENGINE_MASK(id));
Chris Wilson2ae55732017-02-12 17:20:02 +00003233 ctx = fetch_and_zero(&engine->last_retired_context);
3234 if (ctx)
3235 engine->context_unpin(engine, ctx);
Chris Wilson7b6da812017-12-16 00:03:34 +00003236
3237 /*
3238 * Ostensibily, we always want a context loaded for powersaving,
3239 * so if the engine is idle after the reset, send a request
3240 * to load our scratch kernel_context.
3241 *
3242 * More mysteriously, if we leave the engine idle after a reset,
3243 * the next userspace batch may hang, with what appears to be
3244 * an incoherent read by the CS (presumably stale TLB). An
3245 * empty request appears sufficient to paper over the glitch.
3246 */
Chris Wilson01b8fdc2018-02-05 15:24:31 +00003247 if (intel_engine_is_idle(engine)) {
Chris Wilsone61e0f52018-02-21 09:56:36 +00003248 struct i915_request *rq;
Chris Wilson7b6da812017-12-16 00:03:34 +00003249
Chris Wilsone61e0f52018-02-21 09:56:36 +00003250 rq = i915_request_alloc(engine,
3251 dev_priv->kernel_context);
Chris Wilson7b6da812017-12-16 00:03:34 +00003252 if (!IS_ERR(rq))
Chris Wilsone61e0f52018-02-21 09:56:36 +00003253 __i915_request_add(rq, false);
Chris Wilson7b6da812017-12-16 00:03:34 +00003254 }
Chris Wilson2ae55732017-02-12 17:20:02 +00003255 }
Chris Wilson821ed7d2016-09-09 14:11:53 +01003256
Tvrtko Ursulin4362f4f2016-11-16 08:55:33 +00003257 i915_gem_restore_fences(dev_priv);
Chris Wilson821ed7d2016-09-09 14:11:53 +01003258}
3259
Michel Thierrya1ef70e2017-06-20 10:57:47 +01003260void i915_gem_reset_finish_engine(struct intel_engine_cs *engine)
3261{
Sagar Arun Kamblec6dce8f2017-11-16 19:02:37 +05303262 tasklet_enable(&engine->execlists.tasklet);
Michel Thierrya1ef70e2017-06-20 10:57:47 +01003263 kthread_unpark(engine->breadcrumbs.signaler);
Chris Wilson1749d902017-10-09 12:02:59 +01003264
3265 intel_uncore_forcewake_put(engine->i915, FORCEWAKE_ALL);
Michel Thierrya1ef70e2017-06-20 10:57:47 +01003266}
3267
Chris Wilsond8027092017-02-08 14:30:32 +00003268void i915_gem_reset_finish(struct drm_i915_private *dev_priv)
3269{
Chris Wilson1f7b8472017-02-08 14:30:33 +00003270 struct intel_engine_cs *engine;
3271 enum intel_engine_id id;
3272
Chris Wilsond8027092017-02-08 14:30:32 +00003273 lockdep_assert_held(&dev_priv->drm.struct_mutex);
Chris Wilson1f7b8472017-02-08 14:30:33 +00003274
Chris Wilsonfe3288b2017-02-12 17:20:01 +00003275 for_each_engine(engine, dev_priv, id) {
Michel Thierryc64992e2017-06-20 10:57:44 +01003276 engine->hangcheck.active_request = NULL;
Michel Thierrya1ef70e2017-06-20 10:57:47 +01003277 i915_gem_reset_finish_engine(engine);
Chris Wilsonfe3288b2017-02-12 17:20:01 +00003278 }
Chris Wilsond8027092017-02-08 14:30:32 +00003279}
3280
Chris Wilsone61e0f52018-02-21 09:56:36 +00003281static void nop_submit_request(struct i915_request *request)
Chris Wilson821ed7d2016-09-09 14:11:53 +01003282{
Chris Wilsond9b13c42018-03-15 13:14:50 +00003283 GEM_TRACE("%s fence %llx:%d -> -EIO\n",
3284 request->engine->name,
3285 request->fence.context, request->fence.seqno);
Daniel Vetteraf7a8ff2017-10-11 11:10:19 +02003286 dma_fence_set_error(&request->fence, -EIO);
3287
Chris Wilsone61e0f52018-02-21 09:56:36 +00003288 i915_request_submit(request);
Daniel Vetteraf7a8ff2017-10-11 11:10:19 +02003289}
3290
Chris Wilsone61e0f52018-02-21 09:56:36 +00003291static void nop_complete_submit_request(struct i915_request *request)
Daniel Vetteraf7a8ff2017-10-11 11:10:19 +02003292{
Chris Wilson8d550822017-10-06 12:56:17 +01003293 unsigned long flags;
3294
Chris Wilsond9b13c42018-03-15 13:14:50 +00003295 GEM_TRACE("%s fence %llx:%d -> -EIO\n",
3296 request->engine->name,
3297 request->fence.context, request->fence.seqno);
Chris Wilson3cd94422017-01-10 17:22:45 +00003298 dma_fence_set_error(&request->fence, -EIO);
Chris Wilson8d550822017-10-06 12:56:17 +01003299
3300 spin_lock_irqsave(&request->engine->timeline->lock, flags);
Chris Wilsone61e0f52018-02-21 09:56:36 +00003301 __i915_request_submit(request);
Chris Wilson3dcf93f2016-11-22 14:41:20 +00003302 intel_engine_init_global_seqno(request->engine, request->global_seqno);
Chris Wilson8d550822017-10-06 12:56:17 +01003303 spin_unlock_irqrestore(&request->engine->timeline->lock, flags);
Chris Wilson821ed7d2016-09-09 14:11:53 +01003304}
3305
Daniel Vetteraf7a8ff2017-10-11 11:10:19 +02003306void i915_gem_set_wedged(struct drm_i915_private *i915)
Chris Wilson821ed7d2016-09-09 14:11:53 +01003307{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00003308 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05303309 enum intel_engine_id id;
Eric Anholt673a3942008-07-30 12:06:12 -07003310
Chris Wilsond9b13c42018-03-15 13:14:50 +00003311 GEM_TRACE("start\n");
3312
Chris Wilson559e0402018-02-05 09:21:59 +00003313 if (drm_debug & DRM_UT_DRIVER) {
3314 struct drm_printer p = drm_debug_printer(__func__);
3315
3316 for_each_engine(engine, i915, id)
3317 intel_engine_dump(engine, &p, "%s\n", engine->name);
3318 }
3319
Chris Wilson0d73e7a2018-02-07 15:13:50 +00003320 set_bit(I915_WEDGED, &i915->gpu_error.flags);
3321 smp_mb__after_atomic();
3322
Daniel Vetteraf7a8ff2017-10-11 11:10:19 +02003323 /*
3324 * First, stop submission to hw, but do not yet complete requests by
3325 * rolling the global seqno forward (since this would complete requests
3326 * for which we haven't set the fence error to EIO yet).
3327 */
Chris Wilson963ddd62018-03-02 11:33:24 +00003328 for_each_engine(engine, i915, id) {
3329 i915_gem_reset_prepare_engine(engine);
Chris Wilson47650db2018-03-07 13:42:25 +00003330
Daniel Vetteraf7a8ff2017-10-11 11:10:19 +02003331 engine->submit_request = nop_submit_request;
Chris Wilson47650db2018-03-07 13:42:25 +00003332 engine->schedule = NULL;
Chris Wilson963ddd62018-03-02 11:33:24 +00003333 }
Chris Wilson47650db2018-03-07 13:42:25 +00003334 i915->caps.scheduler = 0;
Daniel Vetteraf7a8ff2017-10-11 11:10:19 +02003335
Chris Wilsonac697ae2018-03-15 15:10:15 +00003336 /* Even if the GPU reset fails, it should still stop the engines */
3337 intel_gpu_reset(i915, ALL_ENGINES);
3338
Daniel Vetteraf7a8ff2017-10-11 11:10:19 +02003339 /*
3340 * Make sure no one is running the old callback before we proceed with
3341 * cancelling requests and resetting the completion tracking. Otherwise
3342 * we might submit a request to the hardware which never completes.
3343 */
3344 synchronize_rcu();
3345
3346 for_each_engine(engine, i915, id) {
3347 /* Mark all executing requests as skipped */
3348 engine->cancel_requests(engine);
3349
3350 /*
3351 * Only once we've force-cancelled all in-flight requests can we
3352 * start to complete all requests.
3353 */
3354 engine->submit_request = nop_complete_submit_request;
3355 }
3356
3357 /*
3358 * Make sure no request can slip through without getting completed by
3359 * either this call here to intel_engine_init_global_seqno, or the one
3360 * in nop_complete_submit_request.
3361 */
3362 synchronize_rcu();
3363
3364 for_each_engine(engine, i915, id) {
3365 unsigned long flags;
3366
Chris Wilson0d73e7a2018-02-07 15:13:50 +00003367 /*
3368 * Mark all pending requests as complete so that any concurrent
Daniel Vetteraf7a8ff2017-10-11 11:10:19 +02003369 * (lockless) lookup doesn't try and wait upon the request as we
3370 * reset it.
3371 */
3372 spin_lock_irqsave(&engine->timeline->lock, flags);
3373 intel_engine_init_global_seqno(engine,
3374 intel_engine_last_submit(engine));
3375 spin_unlock_irqrestore(&engine->timeline->lock, flags);
Chris Wilson963ddd62018-03-02 11:33:24 +00003376
3377 i915_gem_reset_finish_engine(engine);
Daniel Vetteraf7a8ff2017-10-11 11:10:19 +02003378 }
Chris Wilson20e49332016-11-22 14:41:21 +00003379
Chris Wilsond9b13c42018-03-15 13:14:50 +00003380 GEM_TRACE("end\n");
3381
Chris Wilson3d7adbb2017-07-21 13:32:27 +01003382 wake_up_all(&i915->gpu_error.reset_queue);
Eric Anholt673a3942008-07-30 12:06:12 -07003383}
3384
Chris Wilson2e8f9d32017-03-16 17:13:04 +00003385bool i915_gem_unset_wedged(struct drm_i915_private *i915)
3386{
3387 struct i915_gem_timeline *tl;
3388 int i;
3389
3390 lockdep_assert_held(&i915->drm.struct_mutex);
3391 if (!test_bit(I915_WEDGED, &i915->gpu_error.flags))
3392 return true;
3393
Chris Wilsond9b13c42018-03-15 13:14:50 +00003394 GEM_TRACE("start\n");
3395
Chris Wilson2d4ecac2018-03-07 13:42:21 +00003396 /*
3397 * Before unwedging, make sure that all pending operations
Chris Wilson2e8f9d32017-03-16 17:13:04 +00003398 * are flushed and errored out - we may have requests waiting upon
3399 * third party fences. We marked all inflight requests as EIO, and
3400 * every execbuf since returned EIO, for consistency we want all
3401 * the currently pending requests to also be marked as EIO, which
3402 * is done inside our nop_submit_request - and so we must wait.
3403 *
3404 * No more can be submitted until we reset the wedged bit.
3405 */
3406 list_for_each_entry(tl, &i915->gt.timelines, link) {
3407 for (i = 0; i < ARRAY_SIZE(tl->engine); i++) {
Chris Wilsone61e0f52018-02-21 09:56:36 +00003408 struct i915_request *rq;
Chris Wilson2e8f9d32017-03-16 17:13:04 +00003409
3410 rq = i915_gem_active_peek(&tl->engine[i].last_request,
3411 &i915->drm.struct_mutex);
3412 if (!rq)
3413 continue;
3414
Chris Wilson2d4ecac2018-03-07 13:42:21 +00003415 /*
3416 * We can't use our normal waiter as we want to
Chris Wilson2e8f9d32017-03-16 17:13:04 +00003417 * avoid recursively trying to handle the current
3418 * reset. The basic dma_fence_default_wait() installs
3419 * a callback for dma_fence_signal(), which is
3420 * triggered by our nop handler (indirectly, the
3421 * callback enables the signaler thread which is
3422 * woken by the nop_submit_request() advancing the seqno
3423 * and when the seqno passes the fence, the signaler
3424 * then signals the fence waking us up).
3425 */
3426 if (dma_fence_default_wait(&rq->fence, true,
3427 MAX_SCHEDULE_TIMEOUT) < 0)
3428 return false;
3429 }
3430 }
Chris Wilson2d4ecac2018-03-07 13:42:21 +00003431 i915_retire_requests(i915);
3432 GEM_BUG_ON(i915->gt.active_requests);
Chris Wilson2e8f9d32017-03-16 17:13:04 +00003433
Chris Wilson2d4ecac2018-03-07 13:42:21 +00003434 /*
3435 * Undo nop_submit_request. We prevent all new i915 requests from
Chris Wilson2e8f9d32017-03-16 17:13:04 +00003436 * being queued (by disallowing execbuf whilst wedged) so having
3437 * waited for all active requests above, we know the system is idle
3438 * and do not have to worry about a thread being inside
3439 * engine->submit_request() as we swap over. So unlike installing
3440 * the nop_submit_request on reset, we can do this from normal
3441 * context and do not require stop_machine().
3442 */
3443 intel_engines_reset_default_submission(i915);
Chris Wilson36703e72017-06-22 11:56:25 +01003444 i915_gem_contexts_lost(i915);
Chris Wilson2e8f9d32017-03-16 17:13:04 +00003445
Chris Wilsond9b13c42018-03-15 13:14:50 +00003446 GEM_TRACE("end\n");
3447
Chris Wilson2e8f9d32017-03-16 17:13:04 +00003448 smp_mb__before_atomic(); /* complete takeover before enabling execbuf */
3449 clear_bit(I915_WEDGED, &i915->gpu_error.flags);
3450
3451 return true;
3452}
3453
Daniel Vetter75ef9da2010-08-21 00:25:16 +02003454static void
Eric Anholt673a3942008-07-30 12:06:12 -07003455i915_gem_retire_work_handler(struct work_struct *work)
3456{
Chris Wilsonb29c19b2013-09-25 17:34:56 +01003457 struct drm_i915_private *dev_priv =
Chris Wilson67d97da2016-07-04 08:08:31 +01003458 container_of(work, typeof(*dev_priv), gt.retire_work.work);
Chris Wilson91c8a322016-07-05 10:40:23 +01003459 struct drm_device *dev = &dev_priv->drm;
Eric Anholt673a3942008-07-30 12:06:12 -07003460
Chris Wilson891b48c2010-09-29 12:26:37 +01003461 /* Come back later if the device is busy... */
Chris Wilsonb29c19b2013-09-25 17:34:56 +01003462 if (mutex_trylock(&dev->struct_mutex)) {
Chris Wilsone61e0f52018-02-21 09:56:36 +00003463 i915_retire_requests(dev_priv);
Chris Wilsonb29c19b2013-09-25 17:34:56 +01003464 mutex_unlock(&dev->struct_mutex);
3465 }
Chris Wilson67d97da2016-07-04 08:08:31 +01003466
Chris Wilson88923042018-01-29 14:41:04 +00003467 /*
3468 * Keep the retire handler running until we are finally idle.
Chris Wilson67d97da2016-07-04 08:08:31 +01003469 * We do not need to do this test under locking as in the worst-case
3470 * we queue the retire worker once too often.
3471 */
Chris Wilson88923042018-01-29 14:41:04 +00003472 if (READ_ONCE(dev_priv->gt.awake))
Chris Wilson67d97da2016-07-04 08:08:31 +01003473 queue_delayed_work(dev_priv->wq,
3474 &dev_priv->gt.retire_work,
Chris Wilsonbcb45082012-10-05 17:02:57 +01003475 round_jiffies_up_relative(HZ));
Chris Wilsonb29c19b2013-09-25 17:34:56 +01003476}
Chris Wilson891b48c2010-09-29 12:26:37 +01003477
Chris Wilson84a10742018-01-24 11:36:08 +00003478static void shrink_caches(struct drm_i915_private *i915)
3479{
3480 /*
3481 * kmem_cache_shrink() discards empty slabs and reorders partially
3482 * filled slabs to prioritise allocating from the mostly full slabs,
3483 * with the aim of reducing fragmentation.
3484 */
3485 kmem_cache_shrink(i915->priorities);
3486 kmem_cache_shrink(i915->dependencies);
3487 kmem_cache_shrink(i915->requests);
3488 kmem_cache_shrink(i915->luts);
3489 kmem_cache_shrink(i915->vmas);
3490 kmem_cache_shrink(i915->objects);
3491}
3492
3493struct sleep_rcu_work {
3494 union {
3495 struct rcu_head rcu;
3496 struct work_struct work;
3497 };
3498 struct drm_i915_private *i915;
3499 unsigned int epoch;
3500};
3501
3502static inline bool
3503same_epoch(struct drm_i915_private *i915, unsigned int epoch)
3504{
3505 /*
3506 * There is a small chance that the epoch wrapped since we started
3507 * sleeping. If we assume that epoch is at least a u32, then it will
3508 * take at least 2^32 * 100ms for it to wrap, or about 326 years.
3509 */
3510 return epoch == READ_ONCE(i915->gt.epoch);
3511}
3512
3513static void __sleep_work(struct work_struct *work)
3514{
3515 struct sleep_rcu_work *s = container_of(work, typeof(*s), work);
3516 struct drm_i915_private *i915 = s->i915;
3517 unsigned int epoch = s->epoch;
3518
3519 kfree(s);
3520 if (same_epoch(i915, epoch))
3521 shrink_caches(i915);
3522}
3523
3524static void __sleep_rcu(struct rcu_head *rcu)
3525{
3526 struct sleep_rcu_work *s = container_of(rcu, typeof(*s), rcu);
3527 struct drm_i915_private *i915 = s->i915;
3528
3529 if (same_epoch(i915, s->epoch)) {
3530 INIT_WORK(&s->work, __sleep_work);
3531 queue_work(i915->wq, &s->work);
3532 } else {
3533 kfree(s);
3534 }
3535}
3536
Chris Wilson5427f202017-10-23 22:32:34 +01003537static inline bool
3538new_requests_since_last_retire(const struct drm_i915_private *i915)
3539{
3540 return (READ_ONCE(i915->gt.active_requests) ||
3541 work_pending(&i915->gt.idle_work.work));
3542}
3543
Chris Wilsonb29c19b2013-09-25 17:34:56 +01003544static void
3545i915_gem_idle_work_handler(struct work_struct *work)
3546{
3547 struct drm_i915_private *dev_priv =
Chris Wilson67d97da2016-07-04 08:08:31 +01003548 container_of(work, typeof(*dev_priv), gt.idle_work.work);
Chris Wilson84a10742018-01-24 11:36:08 +00003549 unsigned int epoch = I915_EPOCH_INVALID;
Chris Wilson67d97da2016-07-04 08:08:31 +01003550 bool rearm_hangcheck;
3551
3552 if (!READ_ONCE(dev_priv->gt.awake))
3553 return;
3554
Imre Deak0cb56702016-11-07 11:20:04 +02003555 /*
3556 * Wait for last execlists context complete, but bail out in case a
Chris Wilsonffed7bd2018-03-01 10:33:38 +00003557 * new request is submitted. As we don't trust the hardware, we
3558 * continue on if the wait times out. This is necessary to allow
3559 * the machine to suspend even if the hardware dies, and we will
3560 * try to recover in resume (after depriving the hardware of power,
3561 * it may be in a better mmod).
Imre Deak0cb56702016-11-07 11:20:04 +02003562 */
Chris Wilsonffed7bd2018-03-01 10:33:38 +00003563 __wait_for(if (new_requests_since_last_retire(dev_priv)) return,
3564 intel_engines_are_idle(dev_priv),
3565 I915_IDLE_ENGINES_TIMEOUT * 1000,
3566 10, 500);
Chris Wilson67d97da2016-07-04 08:08:31 +01003567
3568 rearm_hangcheck =
3569 cancel_delayed_work_sync(&dev_priv->gpu_error.hangcheck_work);
3570
Chris Wilson5427f202017-10-23 22:32:34 +01003571 if (!mutex_trylock(&dev_priv->drm.struct_mutex)) {
Chris Wilson67d97da2016-07-04 08:08:31 +01003572 /* Currently busy, come back later */
3573 mod_delayed_work(dev_priv->wq,
3574 &dev_priv->gt.idle_work,
3575 msecs_to_jiffies(50));
3576 goto out_rearm;
3577 }
3578
Imre Deak93c97dc2016-11-07 11:20:03 +02003579 /*
3580 * New request retired after this work handler started, extend active
3581 * period until next instance of the work.
3582 */
Chris Wilson5427f202017-10-23 22:32:34 +01003583 if (new_requests_since_last_retire(dev_priv))
Imre Deak93c97dc2016-11-07 11:20:03 +02003584 goto out_unlock;
3585
Chris Wilsone4d20062018-04-06 16:51:44 +01003586 epoch = __i915_gem_park(dev_priv);
Chris Wilsonff320d62017-10-23 22:32:35 +01003587
Chris Wilson67d97da2016-07-04 08:08:31 +01003588 rearm_hangcheck = false;
Chris Wilson67d97da2016-07-04 08:08:31 +01003589out_unlock:
Chris Wilson5427f202017-10-23 22:32:34 +01003590 mutex_unlock(&dev_priv->drm.struct_mutex);
Chris Wilson35c94182015-04-07 16:20:37 +01003591
Chris Wilson67d97da2016-07-04 08:08:31 +01003592out_rearm:
3593 if (rearm_hangcheck) {
3594 GEM_BUG_ON(!dev_priv->gt.awake);
3595 i915_queue_hangcheck(dev_priv);
Chris Wilson35c94182015-04-07 16:20:37 +01003596 }
Chris Wilson84a10742018-01-24 11:36:08 +00003597
3598 /*
3599 * When we are idle, it is an opportune time to reap our caches.
3600 * However, we have many objects that utilise RCU and the ordered
3601 * i915->wq that this work is executing on. To try and flush any
3602 * pending frees now we are idle, we first wait for an RCU grace
3603 * period, and then queue a task (that will run last on the wq) to
3604 * shrink and re-optimize the caches.
3605 */
3606 if (same_epoch(dev_priv, epoch)) {
3607 struct sleep_rcu_work *s = kmalloc(sizeof(*s), GFP_KERNEL);
3608 if (s) {
3609 s->i915 = dev_priv;
3610 s->epoch = epoch;
3611 call_rcu(&s->rcu, __sleep_rcu);
3612 }
3613 }
Eric Anholt673a3942008-07-30 12:06:12 -07003614}
3615
Chris Wilsonb1f788c2016-08-04 07:52:45 +01003616void i915_gem_close_object(struct drm_gem_object *gem, struct drm_file *file)
3617{
Chris Wilsond1b48c12017-08-16 09:52:08 +01003618 struct drm_i915_private *i915 = to_i915(gem->dev);
Chris Wilsonb1f788c2016-08-04 07:52:45 +01003619 struct drm_i915_gem_object *obj = to_intel_bo(gem);
3620 struct drm_i915_file_private *fpriv = file->driver_priv;
Chris Wilsond1b48c12017-08-16 09:52:08 +01003621 struct i915_lut_handle *lut, *ln;
Chris Wilsonb1f788c2016-08-04 07:52:45 +01003622
Chris Wilsond1b48c12017-08-16 09:52:08 +01003623 mutex_lock(&i915->drm.struct_mutex);
3624
3625 list_for_each_entry_safe(lut, ln, &obj->lut_list, obj_link) {
3626 struct i915_gem_context *ctx = lut->ctx;
3627 struct i915_vma *vma;
3628
Chris Wilson432295d2017-08-22 12:05:15 +01003629 GEM_BUG_ON(ctx->file_priv == ERR_PTR(-EBADF));
Chris Wilsond1b48c12017-08-16 09:52:08 +01003630 if (ctx->file_priv != fpriv)
3631 continue;
3632
3633 vma = radix_tree_delete(&ctx->handles_vma, lut->handle);
Chris Wilson3ffff012017-08-22 12:05:17 +01003634 GEM_BUG_ON(vma->obj != obj);
3635
3636 /* We allow the process to have multiple handles to the same
3637 * vma, in the same fd namespace, by virtue of flink/open.
3638 */
3639 GEM_BUG_ON(!vma->open_count);
3640 if (!--vma->open_count && !i915_vma_is_ggtt(vma))
Chris Wilsonb1f788c2016-08-04 07:52:45 +01003641 i915_vma_close(vma);
Chris Wilsonf8a7fde2016-10-28 13:58:29 +01003642
Chris Wilsond1b48c12017-08-16 09:52:08 +01003643 list_del(&lut->obj_link);
3644 list_del(&lut->ctx_link);
Chris Wilson4ff4b442017-06-16 15:05:16 +01003645
Chris Wilsond1b48c12017-08-16 09:52:08 +01003646 kmem_cache_free(i915->luts, lut);
3647 __i915_gem_object_release_unless_active(obj);
Chris Wilsonf8a7fde2016-10-28 13:58:29 +01003648 }
Chris Wilsond1b48c12017-08-16 09:52:08 +01003649
3650 mutex_unlock(&i915->drm.struct_mutex);
Chris Wilsonb1f788c2016-08-04 07:52:45 +01003651}
3652
Chris Wilsone95433c2016-10-28 13:58:27 +01003653static unsigned long to_wait_timeout(s64 timeout_ns)
3654{
3655 if (timeout_ns < 0)
3656 return MAX_SCHEDULE_TIMEOUT;
3657
3658 if (timeout_ns == 0)
3659 return 0;
3660
3661 return nsecs_to_jiffies_timeout(timeout_ns);
3662}
3663
Ben Widawsky5816d642012-04-11 11:18:19 -07003664/**
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07003665 * i915_gem_wait_ioctl - implements DRM_IOCTL_I915_GEM_WAIT
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01003666 * @dev: drm device pointer
3667 * @data: ioctl data blob
3668 * @file: drm file pointer
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07003669 *
3670 * Returns 0 if successful, else an error is returned with the remaining time in
3671 * the timeout parameter.
3672 * -ETIME: object is still busy after timeout
3673 * -ERESTARTSYS: signal interrupted the wait
3674 * -ENONENT: object doesn't exist
3675 * Also possible, but rare:
Chris Wilsonb8050142017-08-11 11:57:31 +01003676 * -EAGAIN: incomplete, restart syscall
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07003677 * -ENOMEM: damn
3678 * -ENODEV: Internal IRQ fail
3679 * -E?: The add request failed
3680 *
3681 * The wait ioctl with a timeout of 0 reimplements the busy ioctl. With any
3682 * non-zero timeout parameter the wait ioctl will wait for the given number of
3683 * nanoseconds on an object becoming unbusy. Since the wait itself does so
3684 * without holding struct_mutex the object may become re-busied before this
3685 * function completes. A similar but shorter * race condition exists in the busy
3686 * ioctl
3687 */
3688int
3689i915_gem_wait_ioctl(struct drm_device *dev, void *data, struct drm_file *file)
3690{
3691 struct drm_i915_gem_wait *args = data;
3692 struct drm_i915_gem_object *obj;
Chris Wilsone95433c2016-10-28 13:58:27 +01003693 ktime_t start;
3694 long ret;
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07003695
Daniel Vetter11b5d512014-09-29 15:31:26 +02003696 if (args->flags != 0)
3697 return -EINVAL;
3698
Chris Wilson03ac0642016-07-20 13:31:51 +01003699 obj = i915_gem_object_lookup(file, args->bo_handle);
Chris Wilson033d5492016-08-05 10:14:17 +01003700 if (!obj)
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07003701 return -ENOENT;
Chris Wilson033d5492016-08-05 10:14:17 +01003702
Chris Wilsone95433c2016-10-28 13:58:27 +01003703 start = ktime_get();
3704
3705 ret = i915_gem_object_wait(obj,
3706 I915_WAIT_INTERRUPTIBLE | I915_WAIT_ALL,
3707 to_wait_timeout(args->timeout_ns),
3708 to_rps_client(file));
3709
3710 if (args->timeout_ns > 0) {
3711 args->timeout_ns -= ktime_to_ns(ktime_sub(ktime_get(), start));
3712 if (args->timeout_ns < 0)
3713 args->timeout_ns = 0;
Chris Wilsonc1d20612017-02-16 12:54:41 +00003714
3715 /*
3716 * Apparently ktime isn't accurate enough and occasionally has a
3717 * bit of mismatch in the jiffies<->nsecs<->ktime loop. So patch
3718 * things up to make the test happy. We allow up to 1 jiffy.
3719 *
3720 * This is a regression from the timespec->ktime conversion.
3721 */
3722 if (ret == -ETIME && !nsecs_to_jiffies(args->timeout_ns))
3723 args->timeout_ns = 0;
Chris Wilsonb8050142017-08-11 11:57:31 +01003724
3725 /* Asked to wait beyond the jiffie/scheduler precision? */
3726 if (ret == -ETIME && args->timeout_ns)
3727 ret = -EAGAIN;
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07003728 }
3729
Chris Wilsonf0cd5182016-10-28 13:58:43 +01003730 i915_gem_object_put(obj);
John Harrisonff865882014-11-24 18:49:28 +00003731 return ret;
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07003732}
3733
Chris Wilson73cb9702016-10-28 13:58:46 +01003734static int wait_for_timeline(struct i915_gem_timeline *tl, unsigned int flags)
Daniel Vetter4df2faf2010-02-19 11:52:00 +01003735{
Chris Wilson73cb9702016-10-28 13:58:46 +01003736 int ret, i;
3737
3738 for (i = 0; i < ARRAY_SIZE(tl->engine); i++) {
3739 ret = i915_gem_active_wait(&tl->engine[i].last_request, flags);
3740 if (ret)
3741 return ret;
3742 }
3743
3744 return 0;
3745}
3746
Chris Wilson25112b62017-03-30 15:50:39 +01003747static int wait_for_engines(struct drm_i915_private *i915)
3748{
Chris Wilsonee42c002017-12-11 19:41:34 +00003749 if (wait_for(intel_engines_are_idle(i915), I915_IDLE_ENGINES_TIMEOUT)) {
Chris Wilson59e4b192017-12-11 19:41:35 +00003750 dev_err(i915->drm.dev,
3751 "Failed to idle engines, declaring wedged!\n");
Chris Wilson629820f2018-03-09 10:11:14 +00003752 GEM_TRACE_DUMP();
Chris Wilsoncad99462017-08-26 12:09:33 +01003753 i915_gem_set_wedged(i915);
3754 return -EIO;
Chris Wilson25112b62017-03-30 15:50:39 +01003755 }
3756
3757 return 0;
3758}
3759
Chris Wilson73cb9702016-10-28 13:58:46 +01003760int i915_gem_wait_for_idle(struct drm_i915_private *i915, unsigned int flags)
3761{
Dave Gordonb4ac5af2016-03-24 11:20:38 +00003762 int ret;
Daniel Vetter4df2faf2010-02-19 11:52:00 +01003763
Chris Wilson863e9fd2017-05-30 13:13:32 +01003764 /* If the device is asleep, we have no requests outstanding */
3765 if (!READ_ONCE(i915->gt.awake))
3766 return 0;
3767
Chris Wilson9caa34a2016-11-11 14:58:08 +00003768 if (flags & I915_WAIT_LOCKED) {
3769 struct i915_gem_timeline *tl;
3770
3771 lockdep_assert_held(&i915->drm.struct_mutex);
3772
3773 list_for_each_entry(tl, &i915->gt.timelines, link) {
3774 ret = wait_for_timeline(tl, flags);
3775 if (ret)
3776 return ret;
3777 }
Chris Wilsone61e0f52018-02-21 09:56:36 +00003778 i915_retire_requests(i915);
Chris Wilson25112b62017-03-30 15:50:39 +01003779
3780 ret = wait_for_engines(i915);
Chris Wilson9caa34a2016-11-11 14:58:08 +00003781 } else {
3782 ret = wait_for_timeline(&i915->gt.global_timeline, flags);
Chris Wilson1ec14ad2010-12-04 11:30:53 +00003783 }
Zou Nan haid1b851f2010-05-21 09:08:57 +08003784
Chris Wilson25112b62017-03-30 15:50:39 +01003785 return ret;
Daniel Vetter4df2faf2010-02-19 11:52:00 +01003786}
3787
Chris Wilson5a97bcc2017-02-22 11:40:46 +00003788static void __i915_gem_object_flush_for_display(struct drm_i915_gem_object *obj)
3789{
Chris Wilsone27ab732017-06-15 13:38:49 +01003790 /*
3791 * We manually flush the CPU domain so that we can override and
3792 * force the flush for the display, and perform it asyncrhonously.
3793 */
3794 flush_write_domain(obj, ~I915_GEM_DOMAIN_CPU);
3795 if (obj->cache_dirty)
3796 i915_gem_clflush_object(obj, I915_CLFLUSH_FORCE);
Christian Königc0a51fd2018-02-16 13:43:38 +01003797 obj->write_domain = 0;
Chris Wilson5a97bcc2017-02-22 11:40:46 +00003798}
3799
3800void i915_gem_object_flush_if_display(struct drm_i915_gem_object *obj)
3801{
Chris Wilsonbd3d2252017-10-13 21:26:14 +01003802 if (!READ_ONCE(obj->pin_global))
Chris Wilson5a97bcc2017-02-22 11:40:46 +00003803 return;
3804
3805 mutex_lock(&obj->base.dev->struct_mutex);
3806 __i915_gem_object_flush_for_display(obj);
3807 mutex_unlock(&obj->base.dev->struct_mutex);
3808}
3809
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003810/**
Chris Wilsone22d8e32017-04-12 12:01:11 +01003811 * Moves a single object to the WC read, and possibly write domain.
3812 * @obj: object to act on
3813 * @write: ask for write access or read only
3814 *
3815 * This function returns when the move is complete, including waiting on
3816 * flushes to occur.
3817 */
3818int
3819i915_gem_object_set_to_wc_domain(struct drm_i915_gem_object *obj, bool write)
3820{
3821 int ret;
3822
3823 lockdep_assert_held(&obj->base.dev->struct_mutex);
3824
3825 ret = i915_gem_object_wait(obj,
3826 I915_WAIT_INTERRUPTIBLE |
3827 I915_WAIT_LOCKED |
3828 (write ? I915_WAIT_ALL : 0),
3829 MAX_SCHEDULE_TIMEOUT,
3830 NULL);
3831 if (ret)
3832 return ret;
3833
Christian Königc0a51fd2018-02-16 13:43:38 +01003834 if (obj->write_domain == I915_GEM_DOMAIN_WC)
Chris Wilsone22d8e32017-04-12 12:01:11 +01003835 return 0;
3836
3837 /* Flush and acquire obj->pages so that we are coherent through
3838 * direct access in memory with previous cached writes through
3839 * shmemfs and that our cache domain tracking remains valid.
3840 * For example, if the obj->filp was moved to swap without us
3841 * being notified and releasing the pages, we would mistakenly
3842 * continue to assume that the obj remained out of the CPU cached
3843 * domain.
3844 */
3845 ret = i915_gem_object_pin_pages(obj);
3846 if (ret)
3847 return ret;
3848
3849 flush_write_domain(obj, ~I915_GEM_DOMAIN_WC);
3850
3851 /* Serialise direct access to this object with the barriers for
3852 * coherent writes from the GPU, by effectively invalidating the
3853 * WC domain upon first access.
3854 */
Christian Königc0a51fd2018-02-16 13:43:38 +01003855 if ((obj->read_domains & I915_GEM_DOMAIN_WC) == 0)
Chris Wilsone22d8e32017-04-12 12:01:11 +01003856 mb();
3857
3858 /* It should now be out of any other write domains, and we can update
3859 * the domain values for our changes.
3860 */
Christian Königc0a51fd2018-02-16 13:43:38 +01003861 GEM_BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_WC) != 0);
3862 obj->read_domains |= I915_GEM_DOMAIN_WC;
Chris Wilsone22d8e32017-04-12 12:01:11 +01003863 if (write) {
Christian Königc0a51fd2018-02-16 13:43:38 +01003864 obj->read_domains = I915_GEM_DOMAIN_WC;
3865 obj->write_domain = I915_GEM_DOMAIN_WC;
Chris Wilsone22d8e32017-04-12 12:01:11 +01003866 obj->mm.dirty = true;
3867 }
3868
3869 i915_gem_object_unpin_pages(obj);
3870 return 0;
3871}
3872
3873/**
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003874 * Moves a single object to the GTT read, and possibly write domain.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01003875 * @obj: object to act on
3876 * @write: ask for write access or read only
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003877 *
3878 * This function returns when the move is complete, including waiting on
3879 * flushes to occur.
3880 */
Jesse Barnes79e53942008-11-07 14:24:08 -08003881int
Chris Wilson20217462010-11-23 15:26:33 +00003882i915_gem_object_set_to_gtt_domain(struct drm_i915_gem_object *obj, bool write)
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003883{
Eric Anholte47c68e2008-11-14 13:35:19 -08003884 int ret;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003885
Chris Wilsone95433c2016-10-28 13:58:27 +01003886 lockdep_assert_held(&obj->base.dev->struct_mutex);
Chris Wilson4c7d62c2016-10-28 13:58:32 +01003887
Chris Wilsone95433c2016-10-28 13:58:27 +01003888 ret = i915_gem_object_wait(obj,
3889 I915_WAIT_INTERRUPTIBLE |
3890 I915_WAIT_LOCKED |
3891 (write ? I915_WAIT_ALL : 0),
3892 MAX_SCHEDULE_TIMEOUT,
3893 NULL);
Chris Wilson88241782011-01-07 17:09:48 +00003894 if (ret)
3895 return ret;
3896
Christian Königc0a51fd2018-02-16 13:43:38 +01003897 if (obj->write_domain == I915_GEM_DOMAIN_GTT)
Chris Wilsonc13d87e2016-07-20 09:21:15 +01003898 return 0;
3899
Chris Wilson43566de2015-01-02 16:29:29 +05303900 /* Flush and acquire obj->pages so that we are coherent through
3901 * direct access in memory with previous cached writes through
3902 * shmemfs and that our cache domain tracking remains valid.
3903 * For example, if the obj->filp was moved to swap without us
3904 * being notified and releasing the pages, we would mistakenly
3905 * continue to assume that the obj remained out of the CPU cached
3906 * domain.
3907 */
Chris Wilsona4f5ea62016-10-28 13:58:35 +01003908 ret = i915_gem_object_pin_pages(obj);
Chris Wilson43566de2015-01-02 16:29:29 +05303909 if (ret)
3910 return ret;
3911
Chris Wilsonef749212017-04-12 12:01:10 +01003912 flush_write_domain(obj, ~I915_GEM_DOMAIN_GTT);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003913
Chris Wilsond0a57782012-10-09 19:24:37 +01003914 /* Serialise direct access to this object with the barriers for
3915 * coherent writes from the GPU, by effectively invalidating the
3916 * GTT domain upon first access.
3917 */
Christian Königc0a51fd2018-02-16 13:43:38 +01003918 if ((obj->read_domains & I915_GEM_DOMAIN_GTT) == 0)
Chris Wilsond0a57782012-10-09 19:24:37 +01003919 mb();
3920
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003921 /* It should now be out of any other write domains, and we can update
3922 * the domain values for our changes.
3923 */
Christian Königc0a51fd2018-02-16 13:43:38 +01003924 GEM_BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_GTT) != 0);
3925 obj->read_domains |= I915_GEM_DOMAIN_GTT;
Eric Anholte47c68e2008-11-14 13:35:19 -08003926 if (write) {
Christian Königc0a51fd2018-02-16 13:43:38 +01003927 obj->read_domains = I915_GEM_DOMAIN_GTT;
3928 obj->write_domain = I915_GEM_DOMAIN_GTT;
Chris Wilsona4f5ea62016-10-28 13:58:35 +01003929 obj->mm.dirty = true;
Eric Anholte47c68e2008-11-14 13:35:19 -08003930 }
3931
Chris Wilsona4f5ea62016-10-28 13:58:35 +01003932 i915_gem_object_unpin_pages(obj);
Eric Anholte47c68e2008-11-14 13:35:19 -08003933 return 0;
3934}
3935
Chris Wilsonef55f922015-10-09 14:11:27 +01003936/**
3937 * Changes the cache-level of an object across all VMA.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01003938 * @obj: object to act on
3939 * @cache_level: new cache level to set for the object
Chris Wilsonef55f922015-10-09 14:11:27 +01003940 *
3941 * After this function returns, the object will be in the new cache-level
3942 * across all GTT and the contents of the backing storage will be coherent,
3943 * with respect to the new cache-level. In order to keep the backing storage
3944 * coherent for all users, we only allow a single cache level to be set
3945 * globally on the object and prevent it from being changed whilst the
3946 * hardware is reading from the object. That is if the object is currently
3947 * on the scanout it will be set to uncached (or equivalent display
3948 * cache coherency) and all non-MOCS GPU access will also be uncached so
3949 * that all direct access to the scanout remains coherent.
3950 */
Chris Wilsone4ffd172011-04-04 09:44:39 +01003951int i915_gem_object_set_cache_level(struct drm_i915_gem_object *obj,
3952 enum i915_cache_level cache_level)
3953{
Chris Wilsonaa653a62016-08-04 07:52:27 +01003954 struct i915_vma *vma;
Chris Wilsona6a7cc42016-11-18 21:17:46 +00003955 int ret;
Chris Wilsone4ffd172011-04-04 09:44:39 +01003956
Chris Wilson4c7d62c2016-10-28 13:58:32 +01003957 lockdep_assert_held(&obj->base.dev->struct_mutex);
3958
Chris Wilsone4ffd172011-04-04 09:44:39 +01003959 if (obj->cache_level == cache_level)
Chris Wilsona6a7cc42016-11-18 21:17:46 +00003960 return 0;
Chris Wilsone4ffd172011-04-04 09:44:39 +01003961
Chris Wilsonef55f922015-10-09 14:11:27 +01003962 /* Inspect the list of currently bound VMA and unbind any that would
3963 * be invalid given the new cache-level. This is principally to
3964 * catch the issue of the CS prefetch crossing page boundaries and
3965 * reading an invalid PTE on older architectures.
3966 */
Chris Wilsonaa653a62016-08-04 07:52:27 +01003967restart:
3968 list_for_each_entry(vma, &obj->vma_list, obj_link) {
Chris Wilsonef55f922015-10-09 14:11:27 +01003969 if (!drm_mm_node_allocated(&vma->node))
3970 continue;
3971
Chris Wilson20dfbde2016-08-04 16:32:30 +01003972 if (i915_vma_is_pinned(vma)) {
Chris Wilsonef55f922015-10-09 14:11:27 +01003973 DRM_DEBUG("can not change the cache level of pinned objects\n");
3974 return -EBUSY;
3975 }
3976
Chris Wilson010e3e62017-12-06 12:49:13 +00003977 if (!i915_vma_is_closed(vma) &&
3978 i915_gem_valid_gtt_space(vma, cache_level))
Chris Wilsonaa653a62016-08-04 07:52:27 +01003979 continue;
3980
3981 ret = i915_vma_unbind(vma);
3982 if (ret)
3983 return ret;
3984
3985 /* As unbinding may affect other elements in the
3986 * obj->vma_list (due to side-effects from retiring
3987 * an active vma), play safe and restart the iterator.
3988 */
3989 goto restart;
Chris Wilson42d6ab42012-07-26 11:49:32 +01003990 }
3991
Chris Wilsonef55f922015-10-09 14:11:27 +01003992 /* We can reuse the existing drm_mm nodes but need to change the
3993 * cache-level on the PTE. We could simply unbind them all and
3994 * rebind with the correct cache-level on next use. However since
3995 * we already have a valid slot, dma mapping, pages etc, we may as
3996 * rewrite the PTE in the belief that doing so tramples upon less
3997 * state and so involves less work.
3998 */
Chris Wilson15717de2016-08-04 07:52:26 +01003999 if (obj->bind_count) {
Chris Wilsonef55f922015-10-09 14:11:27 +01004000 /* Before we change the PTE, the GPU must not be accessing it.
4001 * If we wait upon the object, we know that all the bound
4002 * VMA are no longer active.
4003 */
Chris Wilsone95433c2016-10-28 13:58:27 +01004004 ret = i915_gem_object_wait(obj,
4005 I915_WAIT_INTERRUPTIBLE |
4006 I915_WAIT_LOCKED |
4007 I915_WAIT_ALL,
4008 MAX_SCHEDULE_TIMEOUT,
4009 NULL);
Chris Wilsone4ffd172011-04-04 09:44:39 +01004010 if (ret)
4011 return ret;
4012
Tvrtko Ursulin0031fb92016-11-04 14:42:44 +00004013 if (!HAS_LLC(to_i915(obj->base.dev)) &&
4014 cache_level != I915_CACHE_NONE) {
Chris Wilsonef55f922015-10-09 14:11:27 +01004015 /* Access to snoopable pages through the GTT is
4016 * incoherent and on some machines causes a hard
4017 * lockup. Relinquish the CPU mmaping to force
4018 * userspace to refault in the pages and we can
4019 * then double check if the GTT mapping is still
4020 * valid for that pointer access.
4021 */
4022 i915_gem_release_mmap(obj);
Chris Wilsone4ffd172011-04-04 09:44:39 +01004023
Chris Wilsonef55f922015-10-09 14:11:27 +01004024 /* As we no longer need a fence for GTT access,
4025 * we can relinquish it now (and so prevent having
4026 * to steal a fence from someone else on the next
4027 * fence request). Note GPU activity would have
4028 * dropped the fence as all snoopable access is
4029 * supposed to be linear.
4030 */
Chris Wilsone2189dd2017-12-07 21:14:07 +00004031 for_each_ggtt_vma(vma, obj) {
Chris Wilson49ef5292016-08-18 17:17:00 +01004032 ret = i915_vma_put_fence(vma);
4033 if (ret)
4034 return ret;
4035 }
Chris Wilsonef55f922015-10-09 14:11:27 +01004036 } else {
4037 /* We either have incoherent backing store and
4038 * so no GTT access or the architecture is fully
4039 * coherent. In such cases, existing GTT mmaps
4040 * ignore the cache bit in the PTE and we can
4041 * rewrite it without confusing the GPU or having
4042 * to force userspace to fault back in its mmaps.
4043 */
Chris Wilsone4ffd172011-04-04 09:44:39 +01004044 }
4045
Chris Wilson1c7f4bc2016-02-26 11:03:19 +00004046 list_for_each_entry(vma, &obj->vma_list, obj_link) {
Chris Wilsonef55f922015-10-09 14:11:27 +01004047 if (!drm_mm_node_allocated(&vma->node))
4048 continue;
4049
4050 ret = i915_vma_bind(vma, cache_level, PIN_UPDATE);
4051 if (ret)
4052 return ret;
4053 }
Chris Wilsone4ffd172011-04-04 09:44:39 +01004054 }
4055
Chris Wilson1c7f4bc2016-02-26 11:03:19 +00004056 list_for_each_entry(vma, &obj->vma_list, obj_link)
Chris Wilson2c225692013-08-09 12:26:45 +01004057 vma->node.color = cache_level;
Chris Wilsonb8f55be2017-08-11 12:11:16 +01004058 i915_gem_object_set_cache_coherency(obj, cache_level);
Chris Wilsone27ab732017-06-15 13:38:49 +01004059 obj->cache_dirty = true; /* Always invalidate stale cachelines */
Chris Wilson2c225692013-08-09 12:26:45 +01004060
Chris Wilsone4ffd172011-04-04 09:44:39 +01004061 return 0;
4062}
4063
Ben Widawsky199adf42012-09-21 17:01:20 -07004064int i915_gem_get_caching_ioctl(struct drm_device *dev, void *data,
4065 struct drm_file *file)
Chris Wilsone6994ae2012-07-10 10:27:08 +01004066{
Ben Widawsky199adf42012-09-21 17:01:20 -07004067 struct drm_i915_gem_caching *args = data;
Chris Wilsone6994ae2012-07-10 10:27:08 +01004068 struct drm_i915_gem_object *obj;
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004069 int err = 0;
Chris Wilsone6994ae2012-07-10 10:27:08 +01004070
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004071 rcu_read_lock();
4072 obj = i915_gem_object_lookup_rcu(file, args->handle);
4073 if (!obj) {
4074 err = -ENOENT;
4075 goto out;
4076 }
Chris Wilsone6994ae2012-07-10 10:27:08 +01004077
Chris Wilson651d7942013-08-08 14:41:10 +01004078 switch (obj->cache_level) {
4079 case I915_CACHE_LLC:
4080 case I915_CACHE_L3_LLC:
4081 args->caching = I915_CACHING_CACHED;
4082 break;
4083
Chris Wilson4257d3b2013-08-08 14:41:11 +01004084 case I915_CACHE_WT:
4085 args->caching = I915_CACHING_DISPLAY;
4086 break;
4087
Chris Wilson651d7942013-08-08 14:41:10 +01004088 default:
4089 args->caching = I915_CACHING_NONE;
4090 break;
4091 }
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004092out:
4093 rcu_read_unlock();
4094 return err;
Chris Wilsone6994ae2012-07-10 10:27:08 +01004095}
4096
Ben Widawsky199adf42012-09-21 17:01:20 -07004097int i915_gem_set_caching_ioctl(struct drm_device *dev, void *data,
4098 struct drm_file *file)
Chris Wilsone6994ae2012-07-10 10:27:08 +01004099{
Chris Wilson9c870d02016-10-24 13:42:15 +01004100 struct drm_i915_private *i915 = to_i915(dev);
Ben Widawsky199adf42012-09-21 17:01:20 -07004101 struct drm_i915_gem_caching *args = data;
Chris Wilsone6994ae2012-07-10 10:27:08 +01004102 struct drm_i915_gem_object *obj;
4103 enum i915_cache_level level;
Chris Wilsond65415d2017-01-19 08:22:10 +00004104 int ret = 0;
Chris Wilsone6994ae2012-07-10 10:27:08 +01004105
Ben Widawsky199adf42012-09-21 17:01:20 -07004106 switch (args->caching) {
4107 case I915_CACHING_NONE:
Chris Wilsone6994ae2012-07-10 10:27:08 +01004108 level = I915_CACHE_NONE;
4109 break;
Ben Widawsky199adf42012-09-21 17:01:20 -07004110 case I915_CACHING_CACHED:
Imre Deake5756c12015-08-14 18:43:30 +03004111 /*
4112 * Due to a HW issue on BXT A stepping, GPU stores via a
4113 * snooped mapping may leave stale data in a corresponding CPU
4114 * cacheline, whereas normally such cachelines would get
4115 * invalidated.
4116 */
Chris Wilson9c870d02016-10-24 13:42:15 +01004117 if (!HAS_LLC(i915) && !HAS_SNOOP(i915))
Imre Deake5756c12015-08-14 18:43:30 +03004118 return -ENODEV;
4119
Chris Wilsone6994ae2012-07-10 10:27:08 +01004120 level = I915_CACHE_LLC;
4121 break;
Chris Wilson4257d3b2013-08-08 14:41:11 +01004122 case I915_CACHING_DISPLAY:
Chris Wilson9c870d02016-10-24 13:42:15 +01004123 level = HAS_WT(i915) ? I915_CACHE_WT : I915_CACHE_NONE;
Chris Wilson4257d3b2013-08-08 14:41:11 +01004124 break;
Chris Wilsone6994ae2012-07-10 10:27:08 +01004125 default:
4126 return -EINVAL;
4127 }
4128
Chris Wilsond65415d2017-01-19 08:22:10 +00004129 obj = i915_gem_object_lookup(file, args->handle);
4130 if (!obj)
4131 return -ENOENT;
4132
Tina Zhanga03f3952017-11-14 10:25:13 +00004133 /*
4134 * The caching mode of proxy object is handled by its generator, and
4135 * not allowed to be changed by userspace.
4136 */
4137 if (i915_gem_object_is_proxy(obj)) {
4138 ret = -ENXIO;
4139 goto out;
4140 }
4141
Chris Wilsond65415d2017-01-19 08:22:10 +00004142 if (obj->cache_level == level)
4143 goto out;
4144
4145 ret = i915_gem_object_wait(obj,
4146 I915_WAIT_INTERRUPTIBLE,
4147 MAX_SCHEDULE_TIMEOUT,
4148 to_rps_client(file));
4149 if (ret)
4150 goto out;
4151
Ben Widawsky3bc29132012-09-26 16:15:20 -07004152 ret = i915_mutex_lock_interruptible(dev);
4153 if (ret)
Chris Wilsond65415d2017-01-19 08:22:10 +00004154 goto out;
Chris Wilsone6994ae2012-07-10 10:27:08 +01004155
4156 ret = i915_gem_object_set_cache_level(obj, level);
Chris Wilsone6994ae2012-07-10 10:27:08 +01004157 mutex_unlock(&dev->struct_mutex);
Chris Wilsond65415d2017-01-19 08:22:10 +00004158
4159out:
4160 i915_gem_object_put(obj);
Chris Wilsone6994ae2012-07-10 10:27:08 +01004161 return ret;
4162}
4163
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08004164/*
Dhinakaran Pandiyan07bcd992018-03-06 19:34:18 -08004165 * Prepare buffer for display plane (scanout, cursors, etc). Can be called from
4166 * an uninterruptible phase (modesetting) and allows any flushes to be pipelined
4167 * (for pageflips). We only flush the caches while preparing the buffer for
4168 * display, the callers are responsible for frontbuffer flush.
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08004169 */
Chris Wilson058d88c2016-08-15 10:49:06 +01004170struct i915_vma *
Chris Wilson2da3b9b2011-04-14 09:41:17 +01004171i915_gem_object_pin_to_display_plane(struct drm_i915_gem_object *obj,
4172 u32 alignment,
Chris Wilson59354852018-02-20 13:42:06 +00004173 const struct i915_ggtt_view *view,
4174 unsigned int flags)
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08004175{
Chris Wilson058d88c2016-08-15 10:49:06 +01004176 struct i915_vma *vma;
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08004177 int ret;
4178
Chris Wilson4c7d62c2016-10-28 13:58:32 +01004179 lockdep_assert_held(&obj->base.dev->struct_mutex);
4180
Chris Wilsonbd3d2252017-10-13 21:26:14 +01004181 /* Mark the global pin early so that we account for the
Chris Wilsoncc98b412013-08-09 12:25:09 +01004182 * display coherency whilst setting up the cache domains.
4183 */
Chris Wilsonbd3d2252017-10-13 21:26:14 +01004184 obj->pin_global++;
Chris Wilsoncc98b412013-08-09 12:25:09 +01004185
Eric Anholta7ef0642011-03-29 16:59:54 -07004186 /* The display engine is not coherent with the LLC cache on gen6. As
4187 * a result, we make sure that the pinning that is about to occur is
4188 * done with uncached PTEs. This is lowest common denominator for all
4189 * chipsets.
4190 *
4191 * However for gen6+, we could do better by using the GFDT bit instead
4192 * of uncaching, which would allow us to flush all the LLC-cached data
4193 * with that bit in the PTE to main memory with just one PIPE_CONTROL.
4194 */
Chris Wilson651d7942013-08-08 14:41:10 +01004195 ret = i915_gem_object_set_cache_level(obj,
Tvrtko Ursulin86527442016-10-13 11:03:00 +01004196 HAS_WT(to_i915(obj->base.dev)) ?
4197 I915_CACHE_WT : I915_CACHE_NONE);
Chris Wilson058d88c2016-08-15 10:49:06 +01004198 if (ret) {
4199 vma = ERR_PTR(ret);
Chris Wilsonbd3d2252017-10-13 21:26:14 +01004200 goto err_unpin_global;
Chris Wilson058d88c2016-08-15 10:49:06 +01004201 }
Eric Anholta7ef0642011-03-29 16:59:54 -07004202
Chris Wilson2da3b9b2011-04-14 09:41:17 +01004203 /* As the user may map the buffer once pinned in the display plane
4204 * (e.g. libkms for the bootup splash), we have to ensure that we
Chris Wilson2efb8132016-08-18 17:17:06 +01004205 * always use map_and_fenceable for all scanout buffers. However,
4206 * it may simply be too big to fit into mappable, in which case
4207 * put it anyway and hope that userspace can cope (but always first
4208 * try to preserve the existing ABI).
Chris Wilson2da3b9b2011-04-14 09:41:17 +01004209 */
Chris Wilson2efb8132016-08-18 17:17:06 +01004210 vma = ERR_PTR(-ENOSPC);
Chris Wilson59354852018-02-20 13:42:06 +00004211 if ((flags & PIN_MAPPABLE) == 0 &&
4212 (!view || view->type == I915_GGTT_VIEW_NORMAL))
Chris Wilson2efb8132016-08-18 17:17:06 +01004213 vma = i915_gem_object_ggtt_pin(obj, view, 0, alignment,
Chris Wilson59354852018-02-20 13:42:06 +00004214 flags |
4215 PIN_MAPPABLE |
4216 PIN_NONBLOCK);
4217 if (IS_ERR(vma))
Chris Wilson767a2222016-11-07 11:01:28 +00004218 vma = i915_gem_object_ggtt_pin(obj, view, 0, alignment, flags);
Chris Wilson058d88c2016-08-15 10:49:06 +01004219 if (IS_ERR(vma))
Chris Wilsonbd3d2252017-10-13 21:26:14 +01004220 goto err_unpin_global;
Chris Wilson2da3b9b2011-04-14 09:41:17 +01004221
Chris Wilsond8923dc2016-08-18 17:17:07 +01004222 vma->display_alignment = max_t(u64, vma->display_alignment, alignment);
4223
Chris Wilson5a97bcc2017-02-22 11:40:46 +00004224 __i915_gem_object_flush_for_display(obj);
Chris Wilsonb118c1e2010-05-27 13:18:14 +01004225
Chris Wilson2da3b9b2011-04-14 09:41:17 +01004226 /* It should now be out of any other write domains, and we can update
4227 * the domain values for our changes.
4228 */
Christian Königc0a51fd2018-02-16 13:43:38 +01004229 obj->read_domains |= I915_GEM_DOMAIN_GTT;
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08004230
Chris Wilson058d88c2016-08-15 10:49:06 +01004231 return vma;
Chris Wilsoncc98b412013-08-09 12:25:09 +01004232
Chris Wilsonbd3d2252017-10-13 21:26:14 +01004233err_unpin_global:
4234 obj->pin_global--;
Chris Wilson058d88c2016-08-15 10:49:06 +01004235 return vma;
Chris Wilsoncc98b412013-08-09 12:25:09 +01004236}
4237
4238void
Chris Wilson058d88c2016-08-15 10:49:06 +01004239i915_gem_object_unpin_from_display_plane(struct i915_vma *vma)
Chris Wilsoncc98b412013-08-09 12:25:09 +01004240{
Chris Wilson49d73912016-11-29 09:50:08 +00004241 lockdep_assert_held(&vma->vm->i915->drm.struct_mutex);
Chris Wilson4c7d62c2016-10-28 13:58:32 +01004242
Chris Wilsonbd3d2252017-10-13 21:26:14 +01004243 if (WARN_ON(vma->obj->pin_global == 0))
Tvrtko Ursulin8a0c39b2015-04-13 11:50:09 +01004244 return;
4245
Chris Wilsonbd3d2252017-10-13 21:26:14 +01004246 if (--vma->obj->pin_global == 0)
Chris Wilsonf51455d2017-01-10 14:47:34 +00004247 vma->display_alignment = I915_GTT_MIN_ALIGNMENT;
Tvrtko Ursuline6617332015-03-23 11:10:33 +00004248
Chris Wilson383d5822016-08-18 17:17:08 +01004249 /* Bump the LRU to try and avoid premature eviction whilst flipping */
Chris Wilsonbefedbb2017-01-19 19:26:55 +00004250 i915_gem_object_bump_inactive_ggtt(vma->obj);
Chris Wilson383d5822016-08-18 17:17:08 +01004251
Chris Wilson058d88c2016-08-15 10:49:06 +01004252 i915_vma_unpin(vma);
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08004253}
4254
Eric Anholte47c68e2008-11-14 13:35:19 -08004255/**
4256 * Moves a single object to the CPU read, and possibly write domain.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01004257 * @obj: object to act on
4258 * @write: requesting write or read-only access
Eric Anholte47c68e2008-11-14 13:35:19 -08004259 *
4260 * This function returns when the move is complete, including waiting on
4261 * flushes to occur.
4262 */
Chris Wilsondabdfe02012-03-26 10:10:27 +02004263int
Chris Wilson919926a2010-11-12 13:42:53 +00004264i915_gem_object_set_to_cpu_domain(struct drm_i915_gem_object *obj, bool write)
Eric Anholte47c68e2008-11-14 13:35:19 -08004265{
Eric Anholte47c68e2008-11-14 13:35:19 -08004266 int ret;
4267
Chris Wilsone95433c2016-10-28 13:58:27 +01004268 lockdep_assert_held(&obj->base.dev->struct_mutex);
Chris Wilson4c7d62c2016-10-28 13:58:32 +01004269
Chris Wilsone95433c2016-10-28 13:58:27 +01004270 ret = i915_gem_object_wait(obj,
4271 I915_WAIT_INTERRUPTIBLE |
4272 I915_WAIT_LOCKED |
4273 (write ? I915_WAIT_ALL : 0),
4274 MAX_SCHEDULE_TIMEOUT,
4275 NULL);
Chris Wilson88241782011-01-07 17:09:48 +00004276 if (ret)
4277 return ret;
4278
Chris Wilsonef749212017-04-12 12:01:10 +01004279 flush_write_domain(obj, ~I915_GEM_DOMAIN_CPU);
Eric Anholte47c68e2008-11-14 13:35:19 -08004280
Eric Anholte47c68e2008-11-14 13:35:19 -08004281 /* Flush the CPU cache if it's still invalid. */
Christian Königc0a51fd2018-02-16 13:43:38 +01004282 if ((obj->read_domains & I915_GEM_DOMAIN_CPU) == 0) {
Chris Wilson57822dc2017-02-22 11:40:48 +00004283 i915_gem_clflush_object(obj, I915_CLFLUSH_SYNC);
Christian Königc0a51fd2018-02-16 13:43:38 +01004284 obj->read_domains |= I915_GEM_DOMAIN_CPU;
Eric Anholte47c68e2008-11-14 13:35:19 -08004285 }
4286
4287 /* It should now be out of any other write domains, and we can update
4288 * the domain values for our changes.
4289 */
Christian Königc0a51fd2018-02-16 13:43:38 +01004290 GEM_BUG_ON(obj->write_domain & ~I915_GEM_DOMAIN_CPU);
Eric Anholte47c68e2008-11-14 13:35:19 -08004291
4292 /* If we're writing through the CPU, then the GPU read domains will
4293 * need to be invalidated at next use.
4294 */
Chris Wilsone27ab732017-06-15 13:38:49 +01004295 if (write)
4296 __start_cpu_write(obj);
Eric Anholt2ef7eea2008-11-10 10:53:25 -08004297
4298 return 0;
4299}
4300
Eric Anholt673a3942008-07-30 12:06:12 -07004301/* Throttle our rendering by waiting until the ring has completed our requests
4302 * emitted over 20 msec ago.
4303 *
Eric Anholtb9624422009-06-03 07:27:35 +00004304 * Note that if we were to use the current jiffies each time around the loop,
4305 * we wouldn't escape the function with any frames outstanding if the time to
4306 * render a frame was over 20ms.
4307 *
Eric Anholt673a3942008-07-30 12:06:12 -07004308 * This should get us reasonable parallelism between CPU and GPU but also
4309 * relatively low latency when blocking on a particular request to finish.
4310 */
4311static int
Chris Wilsonf787a5f2010-09-24 16:02:42 +01004312i915_gem_ring_throttle(struct drm_device *dev, struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07004313{
Chris Wilsonfac5e232016-07-04 11:34:36 +01004314 struct drm_i915_private *dev_priv = to_i915(dev);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01004315 struct drm_i915_file_private *file_priv = file->driver_priv;
Chris Wilsond0bc54f2015-05-21 21:01:48 +01004316 unsigned long recent_enough = jiffies - DRM_I915_THROTTLE_JIFFIES;
Chris Wilsone61e0f52018-02-21 09:56:36 +00004317 struct i915_request *request, *target = NULL;
Chris Wilsone95433c2016-10-28 13:58:27 +01004318 long ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004319
Chris Wilsonf4457ae2016-04-13 17:35:08 +01004320 /* ABI: return -EIO if already wedged */
4321 if (i915_terminally_wedged(&dev_priv->gpu_error))
4322 return -EIO;
Chris Wilsone110e8d2011-01-26 15:39:14 +00004323
Chris Wilson1c255952010-09-26 11:03:27 +01004324 spin_lock(&file_priv->mm.lock);
Chris Wilsonc8659ef2017-03-02 12:25:25 +00004325 list_for_each_entry(request, &file_priv->mm.request_list, client_link) {
Eric Anholtb9624422009-06-03 07:27:35 +00004326 if (time_after_eq(request->emitted_jiffies, recent_enough))
4327 break;
4328
Chris Wilsonc8659ef2017-03-02 12:25:25 +00004329 if (target) {
4330 list_del(&target->client_link);
4331 target->file_priv = NULL;
4332 }
John Harrisonfcfa423c2015-05-29 17:44:12 +01004333
John Harrison54fb2412014-11-24 18:49:27 +00004334 target = request;
Eric Anholtb9624422009-06-03 07:27:35 +00004335 }
John Harrisonff865882014-11-24 18:49:28 +00004336 if (target)
Chris Wilsone61e0f52018-02-21 09:56:36 +00004337 i915_request_get(target);
Chris Wilson1c255952010-09-26 11:03:27 +01004338 spin_unlock(&file_priv->mm.lock);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01004339
John Harrison54fb2412014-11-24 18:49:27 +00004340 if (target == NULL)
Chris Wilsonf787a5f2010-09-24 16:02:42 +01004341 return 0;
4342
Chris Wilsone61e0f52018-02-21 09:56:36 +00004343 ret = i915_request_wait(target,
Chris Wilsone95433c2016-10-28 13:58:27 +01004344 I915_WAIT_INTERRUPTIBLE,
4345 MAX_SCHEDULE_TIMEOUT);
Chris Wilsone61e0f52018-02-21 09:56:36 +00004346 i915_request_put(target);
John Harrisonff865882014-11-24 18:49:28 +00004347
Chris Wilsone95433c2016-10-28 13:58:27 +01004348 return ret < 0 ? ret : 0;
Eric Anholt673a3942008-07-30 12:06:12 -07004349}
4350
Chris Wilson058d88c2016-08-15 10:49:06 +01004351struct i915_vma *
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02004352i915_gem_object_ggtt_pin(struct drm_i915_gem_object *obj,
4353 const struct i915_ggtt_view *view,
Chris Wilson91b2db62016-08-04 16:32:23 +01004354 u64 size,
Chris Wilson2ffffd02016-08-04 16:32:22 +01004355 u64 alignment,
4356 u64 flags)
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02004357{
Chris Wilsonad16d2e2016-10-13 09:55:04 +01004358 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
4359 struct i915_address_space *vm = &dev_priv->ggtt.base;
Chris Wilson59bfa122016-08-04 16:32:31 +01004360 struct i915_vma *vma;
4361 int ret;
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03004362
Chris Wilson4c7d62c2016-10-28 13:58:32 +01004363 lockdep_assert_held(&obj->base.dev->struct_mutex);
4364
Chris Wilsonac87a6fd2018-02-20 13:42:05 +00004365 if (flags & PIN_MAPPABLE &&
4366 (!view || view->type == I915_GGTT_VIEW_NORMAL)) {
Chris Wilson43ae70d2017-10-09 09:44:01 +01004367 /* If the required space is larger than the available
4368 * aperture, we will not able to find a slot for the
4369 * object and unbinding the object now will be in
4370 * vain. Worse, doing so may cause us to ping-pong
4371 * the object in and out of the Global GTT and
4372 * waste a lot of cycles under the mutex.
4373 */
4374 if (obj->base.size > dev_priv->ggtt.mappable_end)
4375 return ERR_PTR(-E2BIG);
4376
4377 /* If NONBLOCK is set the caller is optimistically
4378 * trying to cache the full object within the mappable
4379 * aperture, and *must* have a fallback in place for
4380 * situations where we cannot bind the object. We
4381 * can be a little more lax here and use the fallback
4382 * more often to avoid costly migrations of ourselves
4383 * and other objects within the aperture.
4384 *
4385 * Half-the-aperture is used as a simple heuristic.
4386 * More interesting would to do search for a free
4387 * block prior to making the commitment to unbind.
4388 * That caters for the self-harm case, and with a
4389 * little more heuristics (e.g. NOFAULT, NOEVICT)
4390 * we could try to minimise harm to others.
4391 */
4392 if (flags & PIN_NONBLOCK &&
4393 obj->base.size > dev_priv->ggtt.mappable_end / 2)
4394 return ERR_PTR(-ENOSPC);
4395 }
4396
Chris Wilson718659a2017-01-16 15:21:28 +00004397 vma = i915_vma_instance(obj, vm, view);
Chris Wilsone0216b72017-01-19 19:26:57 +00004398 if (unlikely(IS_ERR(vma)))
Chris Wilson058d88c2016-08-15 10:49:06 +01004399 return vma;
Chris Wilson59bfa122016-08-04 16:32:31 +01004400
4401 if (i915_vma_misplaced(vma, size, alignment, flags)) {
Chris Wilson43ae70d2017-10-09 09:44:01 +01004402 if (flags & PIN_NONBLOCK) {
4403 if (i915_vma_is_pinned(vma) || i915_vma_is_active(vma))
4404 return ERR_PTR(-ENOSPC);
Chris Wilson59bfa122016-08-04 16:32:31 +01004405
Chris Wilson43ae70d2017-10-09 09:44:01 +01004406 if (flags & PIN_MAPPABLE &&
Chris Wilson944397f2017-01-09 16:16:11 +00004407 vma->fence_size > dev_priv->ggtt.mappable_end / 2)
Chris Wilsonad16d2e2016-10-13 09:55:04 +01004408 return ERR_PTR(-ENOSPC);
4409 }
4410
Chris Wilson59bfa122016-08-04 16:32:31 +01004411 WARN(i915_vma_is_pinned(vma),
4412 "bo is already pinned in ggtt with incorrect alignment:"
Chris Wilson05a20d02016-08-18 17:16:55 +01004413 " offset=%08x, req.alignment=%llx,"
4414 " req.map_and_fenceable=%d, vma->map_and_fenceable=%d\n",
4415 i915_ggtt_offset(vma), alignment,
Chris Wilson59bfa122016-08-04 16:32:31 +01004416 !!(flags & PIN_MAPPABLE),
Chris Wilson05a20d02016-08-18 17:16:55 +01004417 i915_vma_is_map_and_fenceable(vma));
Chris Wilson59bfa122016-08-04 16:32:31 +01004418 ret = i915_vma_unbind(vma);
4419 if (ret)
Chris Wilson058d88c2016-08-15 10:49:06 +01004420 return ERR_PTR(ret);
Chris Wilson59bfa122016-08-04 16:32:31 +01004421 }
4422
Chris Wilson058d88c2016-08-15 10:49:06 +01004423 ret = i915_vma_pin(vma, size, alignment, flags | PIN_GLOBAL);
4424 if (ret)
4425 return ERR_PTR(ret);
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02004426
Chris Wilson058d88c2016-08-15 10:49:06 +01004427 return vma;
Eric Anholt673a3942008-07-30 12:06:12 -07004428}
4429
Chris Wilsonedf6b762016-08-09 09:23:33 +01004430static __always_inline unsigned int __busy_read_flag(unsigned int id)
Chris Wilson3fdc13c2016-08-05 10:14:18 +01004431{
4432 /* Note that we could alias engines in the execbuf API, but
4433 * that would be very unwise as it prevents userspace from
4434 * fine control over engine selection. Ahem.
4435 *
4436 * This should be something like EXEC_MAX_ENGINE instead of
4437 * I915_NUM_ENGINES.
4438 */
4439 BUILD_BUG_ON(I915_NUM_ENGINES > 16);
4440 return 0x10000 << id;
4441}
4442
4443static __always_inline unsigned int __busy_write_id(unsigned int id)
4444{
Chris Wilson70cb4722016-08-09 18:08:25 +01004445 /* The uABI guarantees an active writer is also amongst the read
4446 * engines. This would be true if we accessed the activity tracking
4447 * under the lock, but as we perform the lookup of the object and
4448 * its activity locklessly we can not guarantee that the last_write
4449 * being active implies that we have set the same engine flag from
4450 * last_read - hence we always set both read and write busy for
4451 * last_write.
4452 */
4453 return id | __busy_read_flag(id);
Chris Wilson3fdc13c2016-08-05 10:14:18 +01004454}
4455
Chris Wilsonedf6b762016-08-09 09:23:33 +01004456static __always_inline unsigned int
Chris Wilsond07f0e52016-10-28 13:58:44 +01004457__busy_set_if_active(const struct dma_fence *fence,
Chris Wilson3fdc13c2016-08-05 10:14:18 +01004458 unsigned int (*flag)(unsigned int id))
4459{
Chris Wilsone61e0f52018-02-21 09:56:36 +00004460 struct i915_request *rq;
Chris Wilson12555012016-08-16 09:50:40 +01004461
Chris Wilsond07f0e52016-10-28 13:58:44 +01004462 /* We have to check the current hw status of the fence as the uABI
4463 * guarantees forward progress. We could rely on the idle worker
4464 * to eventually flush us, but to minimise latency just ask the
4465 * hardware.
4466 *
4467 * Note we only report on the status of native fences.
4468 */
4469 if (!dma_fence_is_i915(fence))
Chris Wilson12555012016-08-16 09:50:40 +01004470 return 0;
4471
Chris Wilsond07f0e52016-10-28 13:58:44 +01004472 /* opencode to_request() in order to avoid const warnings */
Chris Wilsone61e0f52018-02-21 09:56:36 +00004473 rq = container_of(fence, struct i915_request, fence);
4474 if (i915_request_completed(rq))
Chris Wilsond07f0e52016-10-28 13:58:44 +01004475 return 0;
4476
Chris Wilson1d39f282017-04-11 13:43:06 +01004477 return flag(rq->engine->uabi_id);
Chris Wilson3fdc13c2016-08-05 10:14:18 +01004478}
4479
Chris Wilsonedf6b762016-08-09 09:23:33 +01004480static __always_inline unsigned int
Chris Wilsond07f0e52016-10-28 13:58:44 +01004481busy_check_reader(const struct dma_fence *fence)
Chris Wilson3fdc13c2016-08-05 10:14:18 +01004482{
Chris Wilsond07f0e52016-10-28 13:58:44 +01004483 return __busy_set_if_active(fence, __busy_read_flag);
Chris Wilson3fdc13c2016-08-05 10:14:18 +01004484}
4485
Chris Wilsonedf6b762016-08-09 09:23:33 +01004486static __always_inline unsigned int
Chris Wilsond07f0e52016-10-28 13:58:44 +01004487busy_check_writer(const struct dma_fence *fence)
Chris Wilson3fdc13c2016-08-05 10:14:18 +01004488{
Chris Wilsond07f0e52016-10-28 13:58:44 +01004489 if (!fence)
4490 return 0;
4491
4492 return __busy_set_if_active(fence, __busy_write_id);
Chris Wilson3fdc13c2016-08-05 10:14:18 +01004493}
4494
Eric Anholt673a3942008-07-30 12:06:12 -07004495int
Eric Anholt673a3942008-07-30 12:06:12 -07004496i915_gem_busy_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00004497 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07004498{
4499 struct drm_i915_gem_busy *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00004500 struct drm_i915_gem_object *obj;
Chris Wilsond07f0e52016-10-28 13:58:44 +01004501 struct reservation_object_list *list;
4502 unsigned int seq;
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004503 int err;
Eric Anholt673a3942008-07-30 12:06:12 -07004504
Chris Wilsond07f0e52016-10-28 13:58:44 +01004505 err = -ENOENT;
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004506 rcu_read_lock();
4507 obj = i915_gem_object_lookup_rcu(file, args->handle);
Chris Wilsond07f0e52016-10-28 13:58:44 +01004508 if (!obj)
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004509 goto out;
Chris Wilsond07f0e52016-10-28 13:58:44 +01004510
4511 /* A discrepancy here is that we do not report the status of
4512 * non-i915 fences, i.e. even though we may report the object as idle,
4513 * a call to set-domain may still stall waiting for foreign rendering.
4514 * This also means that wait-ioctl may report an object as busy,
4515 * where busy-ioctl considers it idle.
4516 *
4517 * We trade the ability to warn of foreign fences to report on which
4518 * i915 engines are active for the object.
4519 *
4520 * Alternatively, we can trade that extra information on read/write
4521 * activity with
4522 * args->busy =
4523 * !reservation_object_test_signaled_rcu(obj->resv, true);
4524 * to report the overall busyness. This is what the wait-ioctl does.
4525 *
4526 */
4527retry:
4528 seq = raw_read_seqcount(&obj->resv->seq);
4529
4530 /* Translate the exclusive fence to the READ *and* WRITE engine */
4531 args->busy = busy_check_writer(rcu_dereference(obj->resv->fence_excl));
4532
4533 /* Translate shared fences to READ set of engines */
4534 list = rcu_dereference(obj->resv->fence);
4535 if (list) {
4536 unsigned int shared_count = list->shared_count, i;
4537
4538 for (i = 0; i < shared_count; ++i) {
4539 struct dma_fence *fence =
4540 rcu_dereference(list->shared[i]);
4541
4542 args->busy |= busy_check_reader(fence);
4543 }
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004544 }
Zou Nan haid1b851f2010-05-21 09:08:57 +08004545
Chris Wilsond07f0e52016-10-28 13:58:44 +01004546 if (args->busy && read_seqcount_retry(&obj->resv->seq, seq))
4547 goto retry;
Chris Wilson426960b2016-01-15 16:51:46 +00004548
Chris Wilsond07f0e52016-10-28 13:58:44 +01004549 err = 0;
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004550out:
4551 rcu_read_unlock();
4552 return err;
Eric Anholt673a3942008-07-30 12:06:12 -07004553}
4554
4555int
4556i915_gem_throttle_ioctl(struct drm_device *dev, void *data,
4557 struct drm_file *file_priv)
4558{
Akshay Joshi0206e352011-08-16 15:34:10 -04004559 return i915_gem_ring_throttle(dev, file_priv);
Eric Anholt673a3942008-07-30 12:06:12 -07004560}
4561
Chris Wilson3ef94da2009-09-14 16:50:29 +01004562int
4563i915_gem_madvise_ioctl(struct drm_device *dev, void *data,
4564 struct drm_file *file_priv)
4565{
Chris Wilsonfac5e232016-07-04 11:34:36 +01004566 struct drm_i915_private *dev_priv = to_i915(dev);
Chris Wilson3ef94da2009-09-14 16:50:29 +01004567 struct drm_i915_gem_madvise *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00004568 struct drm_i915_gem_object *obj;
Chris Wilson1233e2d2016-10-28 13:58:37 +01004569 int err;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004570
4571 switch (args->madv) {
4572 case I915_MADV_DONTNEED:
4573 case I915_MADV_WILLNEED:
4574 break;
4575 default:
4576 return -EINVAL;
4577 }
4578
Chris Wilson03ac0642016-07-20 13:31:51 +01004579 obj = i915_gem_object_lookup(file_priv, args->handle);
Chris Wilson1233e2d2016-10-28 13:58:37 +01004580 if (!obj)
4581 return -ENOENT;
4582
4583 err = mutex_lock_interruptible(&obj->mm.lock);
4584 if (err)
4585 goto out;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004586
Chris Wilsonf1fa4f42017-10-13 21:26:13 +01004587 if (i915_gem_object_has_pages(obj) &&
Chris Wilson3e510a82016-08-05 10:14:23 +01004588 i915_gem_object_is_tiled(obj) &&
Daniel Vetter656bfa32014-11-20 09:26:30 +01004589 dev_priv->quirks & QUIRK_PIN_SWIZZLED_PAGES) {
Chris Wilsonbc0629a2016-11-01 10:03:17 +00004590 if (obj->mm.madv == I915_MADV_WILLNEED) {
4591 GEM_BUG_ON(!obj->mm.quirked);
Chris Wilsona4f5ea62016-10-28 13:58:35 +01004592 __i915_gem_object_unpin_pages(obj);
Chris Wilsonbc0629a2016-11-01 10:03:17 +00004593 obj->mm.quirked = false;
4594 }
4595 if (args->madv == I915_MADV_WILLNEED) {
Chris Wilson2c3a3f42016-11-04 10:30:01 +00004596 GEM_BUG_ON(obj->mm.quirked);
Chris Wilsona4f5ea62016-10-28 13:58:35 +01004597 __i915_gem_object_pin_pages(obj);
Chris Wilsonbc0629a2016-11-01 10:03:17 +00004598 obj->mm.quirked = true;
4599 }
Daniel Vetter656bfa32014-11-20 09:26:30 +01004600 }
4601
Chris Wilsona4f5ea62016-10-28 13:58:35 +01004602 if (obj->mm.madv != __I915_MADV_PURGED)
4603 obj->mm.madv = args->madv;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004604
Chris Wilson6c085a72012-08-20 11:40:46 +02004605 /* if the object is no longer attached, discard its backing storage */
Chris Wilsonf1fa4f42017-10-13 21:26:13 +01004606 if (obj->mm.madv == I915_MADV_DONTNEED &&
4607 !i915_gem_object_has_pages(obj))
Chris Wilson2d7ef392009-09-20 23:13:10 +01004608 i915_gem_object_truncate(obj);
4609
Chris Wilsona4f5ea62016-10-28 13:58:35 +01004610 args->retained = obj->mm.madv != __I915_MADV_PURGED;
Chris Wilson1233e2d2016-10-28 13:58:37 +01004611 mutex_unlock(&obj->mm.lock);
Chris Wilsonbb6baf72009-09-22 14:24:13 +01004612
Chris Wilson1233e2d2016-10-28 13:58:37 +01004613out:
Chris Wilsonf8c417c2016-07-20 13:31:53 +01004614 i915_gem_object_put(obj);
Chris Wilson1233e2d2016-10-28 13:58:37 +01004615 return err;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004616}
4617
Chris Wilson5b8c8ae2016-11-16 19:07:04 +00004618static void
Chris Wilsone61e0f52018-02-21 09:56:36 +00004619frontbuffer_retire(struct i915_gem_active *active, struct i915_request *request)
Chris Wilson5b8c8ae2016-11-16 19:07:04 +00004620{
4621 struct drm_i915_gem_object *obj =
4622 container_of(active, typeof(*obj), frontbuffer_write);
4623
Chris Wilsond59b21e2017-02-22 11:40:49 +00004624 intel_fb_obj_flush(obj, ORIGIN_CS);
Chris Wilson5b8c8ae2016-11-16 19:07:04 +00004625}
4626
Chris Wilson37e680a2012-06-07 15:38:42 +01004627void i915_gem_object_init(struct drm_i915_gem_object *obj,
4628 const struct drm_i915_gem_object_ops *ops)
Chris Wilson0327d6b2012-08-11 15:41:06 +01004629{
Chris Wilson1233e2d2016-10-28 13:58:37 +01004630 mutex_init(&obj->mm.lock);
4631
Ben Widawsky2f633152013-07-17 12:19:03 -07004632 INIT_LIST_HEAD(&obj->vma_list);
Chris Wilsond1b48c12017-08-16 09:52:08 +01004633 INIT_LIST_HEAD(&obj->lut_list);
Chris Wilson8d9d5742015-04-07 16:20:38 +01004634 INIT_LIST_HEAD(&obj->batch_pool_link);
Chris Wilson0327d6b2012-08-11 15:41:06 +01004635
Chris Wilson37e680a2012-06-07 15:38:42 +01004636 obj->ops = ops;
4637
Chris Wilsond07f0e52016-10-28 13:58:44 +01004638 reservation_object_init(&obj->__builtin_resv);
4639 obj->resv = &obj->__builtin_resv;
4640
Chris Wilson50349242016-08-18 17:17:04 +01004641 obj->frontbuffer_ggtt_origin = ORIGIN_GTT;
Chris Wilson5b8c8ae2016-11-16 19:07:04 +00004642 init_request_active(&obj->frontbuffer_write, frontbuffer_retire);
Chris Wilsona4f5ea62016-10-28 13:58:35 +01004643
4644 obj->mm.madv = I915_MADV_WILLNEED;
4645 INIT_RADIX_TREE(&obj->mm.get_page.radix, GFP_KERNEL | __GFP_NOWARN);
4646 mutex_init(&obj->mm.get_page.lock);
Chris Wilson0327d6b2012-08-11 15:41:06 +01004647
Dave Gordonf19ec8c2016-07-04 11:34:37 +01004648 i915_gem_info_add_obj(to_i915(obj->base.dev), obj->base.size);
Chris Wilson0327d6b2012-08-11 15:41:06 +01004649}
4650
Chris Wilson37e680a2012-06-07 15:38:42 +01004651static const struct drm_i915_gem_object_ops i915_gem_object_ops = {
Tvrtko Ursulin3599a912016-11-01 14:44:10 +00004652 .flags = I915_GEM_OBJECT_HAS_STRUCT_PAGE |
4653 I915_GEM_OBJECT_IS_SHRINKABLE,
Chris Wilson7c55e2c2017-03-07 12:03:38 +00004654
Chris Wilson37e680a2012-06-07 15:38:42 +01004655 .get_pages = i915_gem_object_get_pages_gtt,
4656 .put_pages = i915_gem_object_put_pages_gtt,
Chris Wilson7c55e2c2017-03-07 12:03:38 +00004657
4658 .pwrite = i915_gem_object_pwrite_gtt,
Chris Wilson37e680a2012-06-07 15:38:42 +01004659};
4660
Matthew Auld465c4032017-10-06 23:18:14 +01004661static int i915_gem_object_create_shmem(struct drm_device *dev,
4662 struct drm_gem_object *obj,
4663 size_t size)
4664{
4665 struct drm_i915_private *i915 = to_i915(dev);
4666 unsigned long flags = VM_NORESERVE;
4667 struct file *filp;
4668
4669 drm_gem_private_object_init(dev, obj, size);
4670
4671 if (i915->mm.gemfs)
4672 filp = shmem_file_setup_with_mnt(i915->mm.gemfs, "i915", size,
4673 flags);
4674 else
4675 filp = shmem_file_setup("i915", size, flags);
4676
4677 if (IS_ERR(filp))
4678 return PTR_ERR(filp);
4679
4680 obj->filp = filp;
4681
4682 return 0;
4683}
4684
Chris Wilsonb4bcbe22016-10-18 13:02:49 +01004685struct drm_i915_gem_object *
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +00004686i915_gem_object_create(struct drm_i915_private *dev_priv, u64 size)
Daniel Vetterac52bc52010-04-09 19:05:06 +00004687{
Daniel Vetterc397b902010-04-09 19:05:07 +00004688 struct drm_i915_gem_object *obj;
Hugh Dickins5949eac2011-06-27 16:18:18 -07004689 struct address_space *mapping;
Chris Wilsonb8f55be2017-08-11 12:11:16 +01004690 unsigned int cache_level;
Daniel Vetter1a240d42012-11-29 22:18:51 +01004691 gfp_t mask;
Chris Wilsonfe3db792016-04-25 13:32:13 +01004692 int ret;
Daniel Vetterc397b902010-04-09 19:05:07 +00004693
Chris Wilsonb4bcbe22016-10-18 13:02:49 +01004694 /* There is a prevalence of the assumption that we fit the object's
4695 * page count inside a 32bit _signed_ variable. Let's document this and
4696 * catch if we ever need to fix it. In the meantime, if you do spot
4697 * such a local variable, please consider fixing!
4698 */
Tvrtko Ursulin7a3ee5d2017-03-30 17:31:30 +01004699 if (size >> PAGE_SHIFT > INT_MAX)
Chris Wilsonb4bcbe22016-10-18 13:02:49 +01004700 return ERR_PTR(-E2BIG);
4701
4702 if (overflows_type(size, obj->base.size))
4703 return ERR_PTR(-E2BIG);
4704
Tvrtko Ursulin187685c2016-12-01 14:16:36 +00004705 obj = i915_gem_object_alloc(dev_priv);
Daniel Vetterc397b902010-04-09 19:05:07 +00004706 if (obj == NULL)
Chris Wilsonfe3db792016-04-25 13:32:13 +01004707 return ERR_PTR(-ENOMEM);
Daniel Vetterc397b902010-04-09 19:05:07 +00004708
Matthew Auld465c4032017-10-06 23:18:14 +01004709 ret = i915_gem_object_create_shmem(&dev_priv->drm, &obj->base, size);
Chris Wilsonfe3db792016-04-25 13:32:13 +01004710 if (ret)
4711 goto fail;
Daniel Vetterc397b902010-04-09 19:05:07 +00004712
Chris Wilsonbed1ea92012-05-24 20:48:12 +01004713 mask = GFP_HIGHUSER | __GFP_RECLAIMABLE;
Jani Nikulac0f86832016-12-07 12:13:04 +02004714 if (IS_I965GM(dev_priv) || IS_I965G(dev_priv)) {
Chris Wilsonbed1ea92012-05-24 20:48:12 +01004715 /* 965gm cannot relocate objects above 4GiB. */
4716 mask &= ~__GFP_HIGHMEM;
4717 mask |= __GFP_DMA32;
4718 }
4719
Al Viro93c76a32015-12-04 23:45:44 -05004720 mapping = obj->base.filp->f_mapping;
Chris Wilsonbed1ea92012-05-24 20:48:12 +01004721 mapping_set_gfp_mask(mapping, mask);
Chris Wilson4846bf02017-06-09 12:03:46 +01004722 GEM_BUG_ON(!(mapping_gfp_mask(mapping) & __GFP_RECLAIM));
Hugh Dickins5949eac2011-06-27 16:18:18 -07004723
Chris Wilson37e680a2012-06-07 15:38:42 +01004724 i915_gem_object_init(obj, &i915_gem_object_ops);
Chris Wilson73aa8082010-09-30 11:46:12 +01004725
Christian Königc0a51fd2018-02-16 13:43:38 +01004726 obj->write_domain = I915_GEM_DOMAIN_CPU;
4727 obj->read_domains = I915_GEM_DOMAIN_CPU;
Daniel Vetterc397b902010-04-09 19:05:07 +00004728
Chris Wilsonb8f55be2017-08-11 12:11:16 +01004729 if (HAS_LLC(dev_priv))
Eugeni Dodonov3d29b842012-01-17 14:43:53 -02004730 /* On some devices, we can have the GPU use the LLC (the CPU
Eric Anholta1871112011-03-29 16:59:55 -07004731 * cache) for about a 10% performance improvement
4732 * compared to uncached. Graphics requests other than
4733 * display scanout are coherent with the CPU in
4734 * accessing this cache. This means in this mode we
4735 * don't need to clflush on the CPU side, and on the
4736 * GPU side we only need to flush internal caches to
4737 * get data visible to the CPU.
4738 *
4739 * However, we maintain the display planes as UC, and so
4740 * need to rebind when first used as such.
4741 */
Chris Wilsonb8f55be2017-08-11 12:11:16 +01004742 cache_level = I915_CACHE_LLC;
4743 else
4744 cache_level = I915_CACHE_NONE;
Eric Anholta1871112011-03-29 16:59:55 -07004745
Chris Wilsonb8f55be2017-08-11 12:11:16 +01004746 i915_gem_object_set_cache_coherency(obj, cache_level);
Chris Wilsone27ab732017-06-15 13:38:49 +01004747
Daniel Vetterd861e332013-07-24 23:25:03 +02004748 trace_i915_gem_object_create(obj);
4749
Chris Wilson05394f32010-11-08 19:18:58 +00004750 return obj;
Chris Wilsonfe3db792016-04-25 13:32:13 +01004751
4752fail:
4753 i915_gem_object_free(obj);
Chris Wilsonfe3db792016-04-25 13:32:13 +01004754 return ERR_PTR(ret);
Daniel Vetterac52bc52010-04-09 19:05:06 +00004755}
4756
Chris Wilson340fbd82014-05-22 09:16:52 +01004757static bool discard_backing_storage(struct drm_i915_gem_object *obj)
4758{
4759 /* If we are the last user of the backing storage (be it shmemfs
4760 * pages or stolen etc), we know that the pages are going to be
4761 * immediately released. In this case, we can then skip copying
4762 * back the contents from the GPU.
4763 */
4764
Chris Wilsona4f5ea62016-10-28 13:58:35 +01004765 if (obj->mm.madv != I915_MADV_WILLNEED)
Chris Wilson340fbd82014-05-22 09:16:52 +01004766 return false;
4767
4768 if (obj->base.filp == NULL)
4769 return true;
4770
4771 /* At first glance, this looks racy, but then again so would be
4772 * userspace racing mmap against close. However, the first external
4773 * reference to the filp can only be obtained through the
4774 * i915_gem_mmap_ioctl() which safeguards us against the user
4775 * acquiring such a reference whilst we are in the middle of
4776 * freeing the object.
4777 */
4778 return atomic_long_read(&obj->base.filp->f_count) == 1;
4779}
4780
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004781static void __i915_gem_free_objects(struct drm_i915_private *i915,
4782 struct llist_node *freed)
Chris Wilsonbe726152010-07-23 23:18:50 +01004783{
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004784 struct drm_i915_gem_object *obj, *on;
Chris Wilsonbe726152010-07-23 23:18:50 +01004785
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004786 intel_runtime_pm_get(i915);
Chris Wilsoncc731f52017-10-13 21:26:21 +01004787 llist_for_each_entry_safe(obj, on, freed, freed) {
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004788 struct i915_vma *vma, *vn;
Paulo Zanonif65c9162013-11-27 18:20:34 -02004789
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004790 trace_i915_gem_object_destroy(obj);
4791
Chris Wilsoncc731f52017-10-13 21:26:21 +01004792 mutex_lock(&i915->drm.struct_mutex);
4793
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004794 GEM_BUG_ON(i915_gem_object_is_active(obj));
4795 list_for_each_entry_safe(vma, vn,
4796 &obj->vma_list, obj_link) {
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004797 GEM_BUG_ON(i915_vma_is_active(vma));
4798 vma->flags &= ~I915_VMA_PIN_MASK;
4799 i915_vma_close(vma);
4800 }
Chris Wilsondb6c2b42016-11-01 11:54:00 +00004801 GEM_BUG_ON(!list_empty(&obj->vma_list));
4802 GEM_BUG_ON(!RB_EMPTY_ROOT(&obj->vma_tree));
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004803
Chris Wilsonf2123812017-10-16 12:40:37 +01004804 /* This serializes freeing with the shrinker. Since the free
4805 * is delayed, first by RCU then by the workqueue, we want the
4806 * shrinker to be able to free pages of unreferenced objects,
4807 * or else we may oom whilst there are plenty of deferred
4808 * freed objects.
4809 */
4810 if (i915_gem_object_has_pages(obj)) {
4811 spin_lock(&i915->mm.obj_lock);
4812 list_del_init(&obj->mm.link);
4813 spin_unlock(&i915->mm.obj_lock);
4814 }
4815
Chris Wilsoncc731f52017-10-13 21:26:21 +01004816 mutex_unlock(&i915->drm.struct_mutex);
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004817
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004818 GEM_BUG_ON(obj->bind_count);
Chris Wilsona65adaf2017-10-09 09:43:57 +01004819 GEM_BUG_ON(obj->userfault_count);
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004820 GEM_BUG_ON(atomic_read(&obj->frontbuffer_bits));
Chris Wilson67b48042017-08-22 12:05:16 +01004821 GEM_BUG_ON(!list_empty(&obj->lut_list));
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004822
4823 if (obj->ops->release)
4824 obj->ops->release(obj);
4825
4826 if (WARN_ON(i915_gem_object_has_pinned_pages(obj)))
4827 atomic_set(&obj->mm.pages_pin_count, 0);
Chris Wilson548625e2016-11-01 12:11:34 +00004828 __i915_gem_object_put_pages(obj, I915_MM_NORMAL);
Chris Wilsonf1fa4f42017-10-13 21:26:13 +01004829 GEM_BUG_ON(i915_gem_object_has_pages(obj));
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004830
4831 if (obj->base.import_attach)
4832 drm_prime_gem_destroy(&obj->base, NULL);
4833
Chris Wilsond07f0e52016-10-28 13:58:44 +01004834 reservation_object_fini(&obj->__builtin_resv);
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004835 drm_gem_object_release(&obj->base);
4836 i915_gem_info_remove_obj(i915, obj->base.size);
4837
4838 kfree(obj->bit_17);
4839 i915_gem_object_free(obj);
Chris Wilsoncc731f52017-10-13 21:26:21 +01004840
Chris Wilsonc9c70472018-02-19 22:06:31 +00004841 GEM_BUG_ON(!atomic_read(&i915->mm.free_count));
4842 atomic_dec(&i915->mm.free_count);
4843
Chris Wilsoncc731f52017-10-13 21:26:21 +01004844 if (on)
4845 cond_resched();
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004846 }
Chris Wilsoncc731f52017-10-13 21:26:21 +01004847 intel_runtime_pm_put(i915);
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004848}
4849
4850static void i915_gem_flush_free_objects(struct drm_i915_private *i915)
4851{
4852 struct llist_node *freed;
4853
Chris Wilson87701b42017-10-13 21:26:20 +01004854 /* Free the oldest, most stale object to keep the free_list short */
4855 freed = NULL;
4856 if (!llist_empty(&i915->mm.free_list)) { /* quick test for hotpath */
4857 /* Only one consumer of llist_del_first() allowed */
4858 spin_lock(&i915->mm.free_lock);
4859 freed = llist_del_first(&i915->mm.free_list);
4860 spin_unlock(&i915->mm.free_lock);
4861 }
4862 if (unlikely(freed)) {
4863 freed->next = NULL;
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004864 __i915_gem_free_objects(i915, freed);
Chris Wilson87701b42017-10-13 21:26:20 +01004865 }
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004866}
4867
4868static void __i915_gem_free_work(struct work_struct *work)
4869{
4870 struct drm_i915_private *i915 =
4871 container_of(work, struct drm_i915_private, mm.free_work);
4872 struct llist_node *freed;
Chris Wilson26e12f82011-03-20 11:20:19 +00004873
Chris Wilson2ef1e722018-01-15 20:57:59 +00004874 /*
4875 * All file-owned VMA should have been released by this point through
Chris Wilsonb1f788c2016-08-04 07:52:45 +01004876 * i915_gem_close_object(), or earlier by i915_gem_context_close().
4877 * However, the object may also be bound into the global GTT (e.g.
4878 * older GPUs without per-process support, or for direct access through
4879 * the GTT either for the user or for scanout). Those VMA still need to
4880 * unbound now.
4881 */
Chris Wilson1488fc02012-04-24 15:47:31 +01004882
Chris Wilsonf991c492017-11-06 11:15:08 +00004883 spin_lock(&i915->mm.free_lock);
Chris Wilson5ad08be2017-04-07 11:25:51 +01004884 while ((freed = llist_del_all(&i915->mm.free_list))) {
Chris Wilsonf991c492017-11-06 11:15:08 +00004885 spin_unlock(&i915->mm.free_lock);
4886
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004887 __i915_gem_free_objects(i915, freed);
Chris Wilson5ad08be2017-04-07 11:25:51 +01004888 if (need_resched())
Chris Wilsonf991c492017-11-06 11:15:08 +00004889 return;
4890
4891 spin_lock(&i915->mm.free_lock);
Chris Wilson5ad08be2017-04-07 11:25:51 +01004892 }
Chris Wilsonf991c492017-11-06 11:15:08 +00004893 spin_unlock(&i915->mm.free_lock);
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004894}
4895
4896static void __i915_gem_free_object_rcu(struct rcu_head *head)
4897{
4898 struct drm_i915_gem_object *obj =
4899 container_of(head, typeof(*obj), rcu);
4900 struct drm_i915_private *i915 = to_i915(obj->base.dev);
4901
Chris Wilson2ef1e722018-01-15 20:57:59 +00004902 /*
4903 * Since we require blocking on struct_mutex to unbind the freed
4904 * object from the GPU before releasing resources back to the
4905 * system, we can not do that directly from the RCU callback (which may
4906 * be a softirq context), but must instead then defer that work onto a
4907 * kthread. We use the RCU callback rather than move the freed object
4908 * directly onto the work queue so that we can mix between using the
4909 * worker and performing frees directly from subsequent allocations for
4910 * crude but effective memory throttling.
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004911 */
4912 if (llist_add(&obj->freed, &i915->mm.free_list))
Chris Wilsonbeacbd12018-01-15 12:28:45 +00004913 queue_work(i915->wq, &i915->mm.free_work);
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004914}
4915
4916void i915_gem_free_object(struct drm_gem_object *gem_obj)
4917{
4918 struct drm_i915_gem_object *obj = to_intel_bo(gem_obj);
4919
Chris Wilsonbc0629a2016-11-01 10:03:17 +00004920 if (obj->mm.quirked)
4921 __i915_gem_object_unpin_pages(obj);
4922
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004923 if (discard_backing_storage(obj))
4924 obj->mm.madv = I915_MADV_DONTNEED;
Daniel Vettera071fa02014-06-18 23:28:09 +02004925
Chris Wilson2ef1e722018-01-15 20:57:59 +00004926 /*
4927 * Before we free the object, make sure any pure RCU-only
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004928 * read-side critical sections are complete, e.g.
4929 * i915_gem_busy_ioctl(). For the corresponding synchronized
4930 * lookup see i915_gem_object_lookup_rcu().
4931 */
Chris Wilsonc9c70472018-02-19 22:06:31 +00004932 atomic_inc(&to_i915(obj->base.dev)->mm.free_count);
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004933 call_rcu(&obj->rcu, __i915_gem_free_object_rcu);
Chris Wilsonbe726152010-07-23 23:18:50 +01004934}
4935
Chris Wilsonf8a7fde2016-10-28 13:58:29 +01004936void __i915_gem_object_release_unless_active(struct drm_i915_gem_object *obj)
4937{
4938 lockdep_assert_held(&obj->base.dev->struct_mutex);
4939
Chris Wilsond1b48c12017-08-16 09:52:08 +01004940 if (!i915_gem_object_has_active_reference(obj) &&
4941 i915_gem_object_is_active(obj))
Chris Wilsonf8a7fde2016-10-28 13:58:29 +01004942 i915_gem_object_set_active_reference(obj);
4943 else
4944 i915_gem_object_put(obj);
4945}
4946
Chris Wilsonae6c4572017-11-10 14:26:28 +00004947static void assert_kernel_context_is_current(struct drm_i915_private *i915)
Chris Wilson3033aca2016-10-28 13:58:47 +01004948{
Chris Wilsonae6c4572017-11-10 14:26:28 +00004949 struct i915_gem_context *kernel_context = i915->kernel_context;
Chris Wilson3033aca2016-10-28 13:58:47 +01004950 struct intel_engine_cs *engine;
4951 enum intel_engine_id id;
4952
Chris Wilsonae6c4572017-11-10 14:26:28 +00004953 for_each_engine(engine, i915, id) {
4954 GEM_BUG_ON(__i915_gem_active_peek(&engine->timeline->last_request));
4955 GEM_BUG_ON(engine->last_retired_context != kernel_context);
4956 }
Chris Wilson3033aca2016-10-28 13:58:47 +01004957}
4958
Chris Wilson24145512017-01-24 11:01:35 +00004959void i915_gem_sanitize(struct drm_i915_private *i915)
4960{
Chris Wilsonf36325f2017-08-26 12:09:34 +01004961 if (i915_terminally_wedged(&i915->gpu_error)) {
4962 mutex_lock(&i915->drm.struct_mutex);
4963 i915_gem_unset_wedged(i915);
4964 mutex_unlock(&i915->drm.struct_mutex);
4965 }
4966
Chris Wilson24145512017-01-24 11:01:35 +00004967 /*
4968 * If we inherit context state from the BIOS or earlier occupants
4969 * of the GPU, the GPU may be in an inconsistent state when we
4970 * try to take over. The only way to remove the earlier state
4971 * is by resetting. However, resetting on earlier gen is tricky as
4972 * it may impact the display and we are uncertain about the stability
Joonas Lahtinenea117b82017-04-28 10:53:38 +03004973 * of the reset, so this could be applied to even earlier gen.
Chris Wilson24145512017-01-24 11:01:35 +00004974 */
Daniele Ceraolo Spurioce1599a2018-02-07 13:24:40 -08004975 if (INTEL_GEN(i915) >= 5 && intel_has_gpu_reset(i915))
4976 WARN_ON(intel_gpu_reset(i915, ALL_ENGINES));
Chris Wilson24145512017-01-24 11:01:35 +00004977}
4978
Tvrtko Ursulinbf9e8422016-12-01 14:16:38 +00004979int i915_gem_suspend(struct drm_i915_private *dev_priv)
Eric Anholt673a3942008-07-30 12:06:12 -07004980{
Tvrtko Ursulinbf9e8422016-12-01 14:16:38 +00004981 struct drm_device *dev = &dev_priv->drm;
Chris Wilsondcff85c2016-08-05 10:14:11 +01004982 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004983
Chris Wilsonc998e8a2017-03-02 08:30:29 +00004984 intel_runtime_pm_get(dev_priv);
Chris Wilson54b4f682016-07-21 21:16:19 +01004985 intel_suspend_gt_powersave(dev_priv);
4986
Chris Wilson45c5f202013-10-16 11:50:01 +01004987 mutex_lock(&dev->struct_mutex);
Chris Wilson5ab57c72016-07-15 14:56:20 +01004988
4989 /* We have to flush all the executing contexts to main memory so
4990 * that they can saved in the hibernation image. To ensure the last
4991 * context image is coherent, we have to switch away from it. That
4992 * leaves the dev_priv->kernel_context still active when
4993 * we actually suspend, and its image in memory may not match the GPU
4994 * state. Fortunately, the kernel_context is disposable and we do
4995 * not rely on its state.
4996 */
Chris Wilsonecf73eb2017-11-30 10:29:51 +00004997 if (!i915_terminally_wedged(&dev_priv->gpu_error)) {
4998 ret = i915_gem_switch_to_kernel_context(dev_priv);
4999 if (ret)
5000 goto err_unlock;
Chris Wilson5ab57c72016-07-15 14:56:20 +01005001
Chris Wilsonecf73eb2017-11-30 10:29:51 +00005002 ret = i915_gem_wait_for_idle(dev_priv,
5003 I915_WAIT_INTERRUPTIBLE |
5004 I915_WAIT_LOCKED);
5005 if (ret && ret != -EIO)
5006 goto err_unlock;
Chris Wilsonf7403342013-09-13 23:57:04 +01005007
Chris Wilsonecf73eb2017-11-30 10:29:51 +00005008 assert_kernel_context_is_current(dev_priv);
5009 }
Chris Wilson829a0af2017-06-20 12:05:45 +01005010 i915_gem_contexts_lost(dev_priv);
Chris Wilson45c5f202013-10-16 11:50:01 +01005011 mutex_unlock(&dev->struct_mutex);
5012
Michal Wajdeczko7cfca4a2018-03-02 11:15:49 +00005013 intel_uc_suspend(dev_priv);
Sagar Arun Kamble63987bf2017-04-05 15:51:50 +05305014
Chris Wilson737b1502015-01-26 18:03:03 +02005015 cancel_delayed_work_sync(&dev_priv->gpu_error.hangcheck_work);
Chris Wilson67d97da2016-07-04 08:08:31 +01005016 cancel_delayed_work_sync(&dev_priv->gt.retire_work);
Chris Wilsonbdeb9782016-12-23 14:57:56 +00005017
5018 /* As the idle_work is rearming if it detects a race, play safe and
5019 * repeat the flush until it is definitely idle.
5020 */
Chris Wilson7c262402017-10-06 11:40:38 +01005021 drain_delayed_work(&dev_priv->gt.idle_work);
Chris Wilsonbdeb9782016-12-23 14:57:56 +00005022
Chris Wilsonbdcf1202014-11-25 11:56:33 +00005023 /* Assert that we sucessfully flushed all the work and
5024 * reset the GPU back to its idle, low power state.
5025 */
Chris Wilson67d97da2016-07-04 08:08:31 +01005026 WARN_ON(dev_priv->gt.awake);
Chris Wilsonfc692bd2017-08-26 12:09:35 +01005027 if (WARN_ON(!intel_engines_are_idle(dev_priv)))
5028 i915_gem_set_wedged(dev_priv); /* no hope, discard everything */
Chris Wilsonbdcf1202014-11-25 11:56:33 +00005029
Imre Deak1c777c52016-10-12 17:46:37 +03005030 /*
5031 * Neither the BIOS, ourselves or any other kernel
5032 * expects the system to be in execlists mode on startup,
5033 * so we need to reset the GPU back to legacy mode. And the only
5034 * known way to disable logical contexts is through a GPU reset.
5035 *
5036 * So in order to leave the system in a known default configuration,
5037 * always reset the GPU upon unload and suspend. Afterwards we then
5038 * clean up the GEM state tracking, flushing off the requests and
5039 * leaving the system in a known idle state.
5040 *
5041 * Note that is of the upmost importance that the GPU is idle and
5042 * all stray writes are flushed *before* we dismantle the backing
5043 * storage for the pinned objects.
5044 *
5045 * However, since we are uncertain that resetting the GPU on older
5046 * machines is a good idea, we don't - just in case it leaves the
5047 * machine in an unusable condition.
5048 */
Michal Wajdeczkoc37d5722018-03-12 13:03:07 +00005049 intel_uc_sanitize(dev_priv);
Chris Wilson24145512017-01-24 11:01:35 +00005050 i915_gem_sanitize(dev_priv);
Chris Wilsoncad99462017-08-26 12:09:33 +01005051
5052 intel_runtime_pm_put(dev_priv);
5053 return 0;
Imre Deak1c777c52016-10-12 17:46:37 +03005054
Chris Wilsonc998e8a2017-03-02 08:30:29 +00005055err_unlock:
Chris Wilson45c5f202013-10-16 11:50:01 +01005056 mutex_unlock(&dev->struct_mutex);
Chris Wilsonc998e8a2017-03-02 08:30:29 +00005057 intel_runtime_pm_put(dev_priv);
Chris Wilson45c5f202013-10-16 11:50:01 +01005058 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07005059}
5060
Chris Wilson37cd3302017-11-12 11:27:38 +00005061void i915_gem_resume(struct drm_i915_private *i915)
Chris Wilson5ab57c72016-07-15 14:56:20 +01005062{
Chris Wilson37cd3302017-11-12 11:27:38 +00005063 WARN_ON(i915->gt.awake);
Chris Wilson5ab57c72016-07-15 14:56:20 +01005064
Chris Wilson37cd3302017-11-12 11:27:38 +00005065 mutex_lock(&i915->drm.struct_mutex);
5066 intel_uncore_forcewake_get(i915, FORCEWAKE_ALL);
Imre Deak31ab49a2016-11-07 11:20:05 +02005067
Chris Wilson37cd3302017-11-12 11:27:38 +00005068 i915_gem_restore_gtt_mappings(i915);
5069 i915_gem_restore_fences(i915);
Chris Wilson5ab57c72016-07-15 14:56:20 +01005070
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00005071 /*
5072 * As we didn't flush the kernel context before suspend, we cannot
Chris Wilson5ab57c72016-07-15 14:56:20 +01005073 * guarantee that the context image is complete. So let's just reset
5074 * it and start again.
5075 */
Chris Wilson37cd3302017-11-12 11:27:38 +00005076 i915->gt.resume(i915);
Chris Wilson5ab57c72016-07-15 14:56:20 +01005077
Chris Wilson37cd3302017-11-12 11:27:38 +00005078 if (i915_gem_init_hw(i915))
5079 goto err_wedged;
5080
Michal Wajdeczko7cfca4a2018-03-02 11:15:49 +00005081 intel_uc_resume(i915);
Chris Wilson7469c622017-11-14 13:03:00 +00005082
Chris Wilson37cd3302017-11-12 11:27:38 +00005083 /* Always reload a context for powersaving. */
5084 if (i915_gem_switch_to_kernel_context(i915))
5085 goto err_wedged;
5086
5087out_unlock:
5088 intel_uncore_forcewake_put(i915, FORCEWAKE_ALL);
5089 mutex_unlock(&i915->drm.struct_mutex);
5090 return;
5091
5092err_wedged:
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00005093 if (!i915_terminally_wedged(&i915->gpu_error)) {
5094 DRM_ERROR("failed to re-initialize GPU, declaring wedged!\n");
5095 i915_gem_set_wedged(i915);
5096 }
Chris Wilson37cd3302017-11-12 11:27:38 +00005097 goto out_unlock;
Chris Wilson5ab57c72016-07-15 14:56:20 +01005098}
5099
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00005100void i915_gem_init_swizzling(struct drm_i915_private *dev_priv)
Daniel Vetterf691e2f2012-02-02 09:58:12 +01005101{
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00005102 if (INTEL_GEN(dev_priv) < 5 ||
Daniel Vetterf691e2f2012-02-02 09:58:12 +01005103 dev_priv->mm.bit_6_swizzle_x == I915_BIT_6_SWIZZLE_NONE)
5104 return;
5105
5106 I915_WRITE(DISP_ARB_CTL, I915_READ(DISP_ARB_CTL) |
5107 DISP_TILE_SURFACE_SWIZZLING);
5108
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01005109 if (IS_GEN5(dev_priv))
Daniel Vetter11782b02012-01-31 16:47:55 +01005110 return;
5111
Daniel Vetterf691e2f2012-02-02 09:58:12 +01005112 I915_WRITE(TILECTL, I915_READ(TILECTL) | TILECTL_SWZCTL);
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01005113 if (IS_GEN6(dev_priv))
Daniel Vetter6b26c862012-04-24 14:04:12 +02005114 I915_WRITE(ARB_MODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_SNB));
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01005115 else if (IS_GEN7(dev_priv))
Daniel Vetter6b26c862012-04-24 14:04:12 +02005116 I915_WRITE(ARB_MODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_IVB));
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01005117 else if (IS_GEN8(dev_priv))
Ben Widawsky31a53362013-11-02 21:07:04 -07005118 I915_WRITE(GAMTARBMODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_BDW));
Ben Widawsky8782e262012-12-18 10:31:23 -08005119 else
5120 BUG();
Daniel Vetterf691e2f2012-02-02 09:58:12 +01005121}
Daniel Vettere21af882012-02-09 20:53:27 +01005122
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01005123static void init_unused_ring(struct drm_i915_private *dev_priv, u32 base)
Ville Syrjälä81e7f202014-08-15 01:21:55 +03005124{
Ville Syrjälä81e7f202014-08-15 01:21:55 +03005125 I915_WRITE(RING_CTL(base), 0);
5126 I915_WRITE(RING_HEAD(base), 0);
5127 I915_WRITE(RING_TAIL(base), 0);
5128 I915_WRITE(RING_START(base), 0);
5129}
5130
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01005131static void init_unused_rings(struct drm_i915_private *dev_priv)
Ville Syrjälä81e7f202014-08-15 01:21:55 +03005132{
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01005133 if (IS_I830(dev_priv)) {
5134 init_unused_ring(dev_priv, PRB1_BASE);
5135 init_unused_ring(dev_priv, SRB0_BASE);
5136 init_unused_ring(dev_priv, SRB1_BASE);
5137 init_unused_ring(dev_priv, SRB2_BASE);
5138 init_unused_ring(dev_priv, SRB3_BASE);
5139 } else if (IS_GEN2(dev_priv)) {
5140 init_unused_ring(dev_priv, SRB0_BASE);
5141 init_unused_ring(dev_priv, SRB1_BASE);
5142 } else if (IS_GEN3(dev_priv)) {
5143 init_unused_ring(dev_priv, PRB1_BASE);
5144 init_unused_ring(dev_priv, PRB2_BASE);
Ville Syrjälä81e7f202014-08-15 01:21:55 +03005145 }
5146}
5147
Chris Wilson20a8a742017-02-08 14:30:31 +00005148static int __i915_gem_restart_engines(void *data)
Ben Widawsky4fc7c972013-02-08 11:49:24 -08005149{
Chris Wilson20a8a742017-02-08 14:30:31 +00005150 struct drm_i915_private *i915 = data;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00005151 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05305152 enum intel_engine_id id;
Chris Wilson20a8a742017-02-08 14:30:31 +00005153 int err;
5154
5155 for_each_engine(engine, i915, id) {
5156 err = engine->init_hw(engine);
Chris Wilson8177e112018-02-07 11:15:45 +00005157 if (err) {
5158 DRM_ERROR("Failed to restart %s (%d)\n",
5159 engine->name, err);
Chris Wilson20a8a742017-02-08 14:30:31 +00005160 return err;
Chris Wilson8177e112018-02-07 11:15:45 +00005161 }
Chris Wilson20a8a742017-02-08 14:30:31 +00005162 }
5163
5164 return 0;
5165}
5166
5167int i915_gem_init_hw(struct drm_i915_private *dev_priv)
5168{
Chris Wilsond200cda2016-04-28 09:56:44 +01005169 int ret;
Ben Widawsky4fc7c972013-02-08 11:49:24 -08005170
Chris Wilsonde867c22016-10-25 13:16:02 +01005171 dev_priv->gt.last_init_time = ktime_get();
5172
Chris Wilson5e4f5182015-02-13 14:35:59 +00005173 /* Double layer security blanket, see i915_gem_init() */
5174 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
5175
Tvrtko Ursulin0031fb92016-11-04 14:42:44 +00005176 if (HAS_EDRAM(dev_priv) && INTEL_GEN(dev_priv) < 9)
Ben Widawsky05e21cc2013-07-04 11:02:04 -07005177 I915_WRITE(HSW_IDICR, I915_READ(HSW_IDICR) | IDIHASHMSK(0xf));
Ben Widawsky4fc7c972013-02-08 11:49:24 -08005178
Tvrtko Ursulin772c2a52016-10-13 11:03:01 +01005179 if (IS_HASWELL(dev_priv))
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01005180 I915_WRITE(MI_PREDICATE_RESULT_2, IS_HSW_GT3(dev_priv) ?
Ville Syrjälä0bf21342013-11-29 14:56:12 +02005181 LOWER_SLICE_ENABLED : LOWER_SLICE_DISABLED);
Rodrigo Vivi94353732013-08-28 16:45:46 -03005182
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01005183 if (HAS_PCH_NOP(dev_priv)) {
Tvrtko Ursulinfd6b8f42016-10-14 10:13:06 +01005184 if (IS_IVYBRIDGE(dev_priv)) {
Daniel Vetter6ba844b2014-01-22 23:39:30 +01005185 u32 temp = I915_READ(GEN7_MSG_CTL);
5186 temp &= ~(WAIT_FOR_PCH_FLR_ACK | WAIT_FOR_PCH_RESET_ACK);
5187 I915_WRITE(GEN7_MSG_CTL, temp);
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00005188 } else if (INTEL_GEN(dev_priv) >= 7) {
Daniel Vetter6ba844b2014-01-22 23:39:30 +01005189 u32 temp = I915_READ(HSW_NDE_RSTWRN_OPT);
5190 temp &= ~RESET_PCH_HANDSHAKE_ENABLE;
5191 I915_WRITE(HSW_NDE_RSTWRN_OPT, temp);
5192 }
Ben Widawsky88a2b2a2013-04-05 13:12:43 -07005193 }
5194
Oscar Mateo59b449d2018-04-10 09:12:47 -07005195 intel_gt_workarounds_apply(dev_priv);
5196
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00005197 i915_gem_init_swizzling(dev_priv);
Ben Widawsky4fc7c972013-02-08 11:49:24 -08005198
Daniel Vetterd5abdfd2014-11-20 09:45:19 +01005199 /*
5200 * At least 830 can leave some of the unused rings
5201 * "active" (ie. head != tail) after resume which
5202 * will prevent c3 entry. Makes sure all unused rings
5203 * are totally idle.
5204 */
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01005205 init_unused_rings(dev_priv);
Daniel Vetterd5abdfd2014-11-20 09:45:19 +01005206
Dave Gordoned54c1a2016-01-19 19:02:54 +00005207 BUG_ON(!dev_priv->kernel_context);
Chris Wilson6f74b362017-10-15 15:37:25 +01005208 if (i915_terminally_wedged(&dev_priv->gpu_error)) {
5209 ret = -EIO;
5210 goto out;
5211 }
John Harrison90638cc2015-05-29 17:43:37 +01005212
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00005213 ret = i915_ppgtt_init_hw(dev_priv);
John Harrison4ad2fd82015-06-18 13:11:20 +01005214 if (ret) {
Chris Wilson8177e112018-02-07 11:15:45 +00005215 DRM_ERROR("Enabling PPGTT failed (%d)\n", ret);
John Harrison4ad2fd82015-06-18 13:11:20 +01005216 goto out;
5217 }
5218
Jackie Lif08e2032018-03-13 17:32:53 -07005219 ret = intel_wopcm_init_hw(&dev_priv->wopcm);
5220 if (ret) {
5221 DRM_ERROR("Enabling WOPCM failed (%d)\n", ret);
5222 goto out;
5223 }
5224
Michał Winiarski9bdc3572017-10-25 18:25:19 +01005225 /* We can't enable contexts until all firmware is loaded */
5226 ret = intel_uc_init_hw(dev_priv);
Chris Wilson8177e112018-02-07 11:15:45 +00005227 if (ret) {
5228 DRM_ERROR("Enabling uc failed (%d)\n", ret);
Michał Winiarski9bdc3572017-10-25 18:25:19 +01005229 goto out;
Chris Wilson8177e112018-02-07 11:15:45 +00005230 }
Michał Winiarski9bdc3572017-10-25 18:25:19 +01005231
Tvrtko Ursulinbf9e8422016-12-01 14:16:38 +00005232 intel_mocs_init_l3cc_table(dev_priv);
Peter Antoine0ccdacf2016-04-13 15:03:25 +01005233
Chris Wilson136109c2017-11-02 13:14:30 +00005234 /* Only when the HW is re-initialised, can we replay the requests */
5235 ret = __i915_gem_restart_engines(dev_priv);
Chris Wilson5e4f5182015-02-13 14:35:59 +00005236out:
5237 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Ben Widawsky2fa48d82013-12-06 14:11:04 -08005238 return ret;
Zou Nan hai8187a2b2010-05-21 09:08:55 +08005239}
5240
Chris Wilsond2b4b972017-11-10 14:26:33 +00005241static int __intel_engines_record_defaults(struct drm_i915_private *i915)
5242{
5243 struct i915_gem_context *ctx;
5244 struct intel_engine_cs *engine;
5245 enum intel_engine_id id;
5246 int err;
5247
5248 /*
5249 * As we reset the gpu during very early sanitisation, the current
5250 * register state on the GPU should reflect its defaults values.
5251 * We load a context onto the hw (with restore-inhibit), then switch
5252 * over to a second context to save that default register state. We
5253 * can then prime every new context with that state so they all start
5254 * from the same default HW values.
5255 */
5256
5257 ctx = i915_gem_context_create_kernel(i915, 0);
5258 if (IS_ERR(ctx))
5259 return PTR_ERR(ctx);
5260
5261 for_each_engine(engine, i915, id) {
Chris Wilsone61e0f52018-02-21 09:56:36 +00005262 struct i915_request *rq;
Chris Wilsond2b4b972017-11-10 14:26:33 +00005263
Chris Wilsone61e0f52018-02-21 09:56:36 +00005264 rq = i915_request_alloc(engine, ctx);
Chris Wilsond2b4b972017-11-10 14:26:33 +00005265 if (IS_ERR(rq)) {
5266 err = PTR_ERR(rq);
5267 goto out_ctx;
5268 }
5269
Chris Wilson3fef5cd2017-11-20 10:20:02 +00005270 err = 0;
Chris Wilsond2b4b972017-11-10 14:26:33 +00005271 if (engine->init_context)
5272 err = engine->init_context(rq);
5273
Chris Wilsone61e0f52018-02-21 09:56:36 +00005274 __i915_request_add(rq, true);
Chris Wilsond2b4b972017-11-10 14:26:33 +00005275 if (err)
5276 goto err_active;
5277 }
5278
5279 err = i915_gem_switch_to_kernel_context(i915);
5280 if (err)
5281 goto err_active;
5282
5283 err = i915_gem_wait_for_idle(i915, I915_WAIT_LOCKED);
5284 if (err)
5285 goto err_active;
5286
5287 assert_kernel_context_is_current(i915);
5288
5289 for_each_engine(engine, i915, id) {
5290 struct i915_vma *state;
5291
5292 state = ctx->engine[id].state;
5293 if (!state)
5294 continue;
5295
5296 /*
5297 * As we will hold a reference to the logical state, it will
5298 * not be torn down with the context, and importantly the
5299 * object will hold onto its vma (making it possible for a
5300 * stray GTT write to corrupt our defaults). Unmap the vma
5301 * from the GTT to prevent such accidents and reclaim the
5302 * space.
5303 */
5304 err = i915_vma_unbind(state);
5305 if (err)
5306 goto err_active;
5307
5308 err = i915_gem_object_set_to_cpu_domain(state->obj, false);
5309 if (err)
5310 goto err_active;
5311
5312 engine->default_state = i915_gem_object_get(state->obj);
5313 }
5314
5315 if (IS_ENABLED(CONFIG_DRM_I915_DEBUG_GEM)) {
5316 unsigned int found = intel_engines_has_context_isolation(i915);
5317
5318 /*
5319 * Make sure that classes with multiple engine instances all
5320 * share the same basic configuration.
5321 */
5322 for_each_engine(engine, i915, id) {
5323 unsigned int bit = BIT(engine->uabi_class);
5324 unsigned int expected = engine->default_state ? bit : 0;
5325
5326 if ((found & bit) != expected) {
5327 DRM_ERROR("mismatching default context state for class %d on engine %s\n",
5328 engine->uabi_class, engine->name);
5329 }
5330 }
5331 }
5332
5333out_ctx:
5334 i915_gem_context_set_closed(ctx);
5335 i915_gem_context_put(ctx);
5336 return err;
5337
5338err_active:
5339 /*
5340 * If we have to abandon now, we expect the engines to be idle
5341 * and ready to be torn-down. First try to flush any remaining
5342 * request, ensure we are pointing at the kernel context and
5343 * then remove it.
5344 */
5345 if (WARN_ON(i915_gem_switch_to_kernel_context(i915)))
5346 goto out_ctx;
5347
5348 if (WARN_ON(i915_gem_wait_for_idle(i915, I915_WAIT_LOCKED)))
5349 goto out_ctx;
5350
5351 i915_gem_contexts_lost(i915);
5352 goto out_ctx;
5353}
5354
Tvrtko Ursulinbf9e8422016-12-01 14:16:38 +00005355int i915_gem_init(struct drm_i915_private *dev_priv)
Chris Wilson1070a422012-04-24 15:47:41 +01005356{
Chris Wilson1070a422012-04-24 15:47:41 +01005357 int ret;
5358
Matthew Auldda9fe3f32017-10-06 23:18:31 +01005359 /*
5360 * We need to fallback to 4K pages since gvt gtt handling doesn't
5361 * support huge page entries - we will need to check either hypervisor
5362 * mm can support huge guest page or just do emulation in gvt.
5363 */
5364 if (intel_vgpu_active(dev_priv))
5365 mkwrite_device_info(dev_priv)->page_sizes =
5366 I915_GTT_PAGE_SIZE_4K;
5367
Chris Wilson94312822017-05-03 10:39:18 +01005368 dev_priv->mm.unordered_timeline = dma_fence_context_alloc(1);
Chris Wilson57822dc2017-02-22 11:40:48 +00005369
Chris Wilsonfb5c5512017-11-20 20:55:00 +00005370 if (HAS_LOGICAL_RING_CONTEXTS(dev_priv)) {
Chris Wilson821ed7d2016-09-09 14:11:53 +01005371 dev_priv->gt.resume = intel_lr_context_resume;
Tvrtko Ursulin117897f2016-03-16 11:00:40 +00005372 dev_priv->gt.cleanup_engine = intel_logical_ring_cleanup;
Chris Wilsonfb5c5512017-11-20 20:55:00 +00005373 } else {
5374 dev_priv->gt.resume = intel_legacy_submission_resume;
5375 dev_priv->gt.cleanup_engine = intel_engine_cleanup;
Oscar Mateoa83014d2014-07-24 17:04:21 +01005376 }
5377
Chris Wilsonee487002017-11-22 17:26:21 +00005378 ret = i915_gem_init_userptr(dev_priv);
5379 if (ret)
5380 return ret;
5381
Jackie Li6b0478f2018-03-13 17:32:50 -07005382 ret = intel_wopcm_init(&dev_priv->wopcm);
5383 if (ret)
5384 return ret;
5385
Sagar Arun Kamble70deead2018-01-24 21:16:58 +05305386 ret = intel_uc_init_misc(dev_priv);
Michał Winiarski3176ff42017-12-13 23:13:47 +01005387 if (ret)
5388 return ret;
5389
Chris Wilson5e4f5182015-02-13 14:35:59 +00005390 /* This is just a security blanket to placate dragons.
5391 * On some systems, we very sporadically observe that the first TLBs
5392 * used by the CS may be stale, despite us poking the TLB reset. If
5393 * we hold the forcewake during initialisation these problems
5394 * just magically go away.
5395 */
Chris Wilsonee487002017-11-22 17:26:21 +00005396 mutex_lock(&dev_priv->drm.struct_mutex);
Chris Wilson5e4f5182015-02-13 14:35:59 +00005397 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
5398
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01005399 ret = i915_gem_init_ggtt(dev_priv);
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00005400 if (ret) {
5401 GEM_BUG_ON(ret == -EIO);
5402 goto err_unlock;
5403 }
Jesse Barnesd62b4892013-03-08 10:45:53 -08005404
Chris Wilson829a0af2017-06-20 12:05:45 +01005405 ret = i915_gem_contexts_init(dev_priv);
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00005406 if (ret) {
5407 GEM_BUG_ON(ret == -EIO);
5408 goto err_ggtt;
5409 }
Ben Widawsky2fa48d82013-12-06 14:11:04 -08005410
Tvrtko Ursulinbf9e8422016-12-01 14:16:38 +00005411 ret = intel_engines_init(dev_priv);
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00005412 if (ret) {
5413 GEM_BUG_ON(ret == -EIO);
5414 goto err_context;
5415 }
Daniel Vetter53ca26c2012-04-26 23:28:03 +02005416
Chris Wilsonf58d13d2017-11-10 14:26:29 +00005417 intel_init_gt_powersave(dev_priv);
5418
Michał Winiarski61b5c152017-12-13 23:13:48 +01005419 ret = intel_uc_init(dev_priv);
Chris Wilsoncc6a8182017-11-10 14:26:30 +00005420 if (ret)
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00005421 goto err_pm;
Chris Wilsoncc6a8182017-11-10 14:26:30 +00005422
Michał Winiarski61b5c152017-12-13 23:13:48 +01005423 ret = i915_gem_init_hw(dev_priv);
5424 if (ret)
5425 goto err_uc_init;
5426
Chris Wilsoncc6a8182017-11-10 14:26:30 +00005427 /*
5428 * Despite its name intel_init_clock_gating applies both display
5429 * clock gating workarounds; GT mmio workarounds and the occasional
5430 * GT power context workaround. Worse, sometimes it includes a context
5431 * register workaround which we need to apply before we record the
5432 * default HW state for all contexts.
5433 *
5434 * FIXME: break up the workarounds and apply them at the right time!
5435 */
5436 intel_init_clock_gating(dev_priv);
5437
Chris Wilsond2b4b972017-11-10 14:26:33 +00005438 ret = __intel_engines_record_defaults(dev_priv);
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00005439 if (ret)
5440 goto err_init_hw;
5441
5442 if (i915_inject_load_failure()) {
5443 ret = -ENODEV;
5444 goto err_init_hw;
5445 }
5446
5447 if (i915_inject_load_failure()) {
5448 ret = -EIO;
5449 goto err_init_hw;
5450 }
5451
5452 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
5453 mutex_unlock(&dev_priv->drm.struct_mutex);
5454
5455 return 0;
5456
5457 /*
5458 * Unwinding is complicated by that we want to handle -EIO to mean
5459 * disable GPU submission but keep KMS alive. We want to mark the
5460 * HW as irrevisibly wedged, but keep enough state around that the
5461 * driver doesn't explode during runtime.
5462 */
5463err_init_hw:
5464 i915_gem_wait_for_idle(dev_priv, I915_WAIT_LOCKED);
5465 i915_gem_contexts_lost(dev_priv);
5466 intel_uc_fini_hw(dev_priv);
Michał Winiarski61b5c152017-12-13 23:13:48 +01005467err_uc_init:
5468 intel_uc_fini(dev_priv);
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00005469err_pm:
5470 if (ret != -EIO) {
5471 intel_cleanup_gt_powersave(dev_priv);
5472 i915_gem_cleanup_engines(dev_priv);
5473 }
5474err_context:
5475 if (ret != -EIO)
5476 i915_gem_contexts_fini(dev_priv);
5477err_ggtt:
5478err_unlock:
5479 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
5480 mutex_unlock(&dev_priv->drm.struct_mutex);
5481
Sagar Arun Kamble70deead2018-01-24 21:16:58 +05305482 intel_uc_fini_misc(dev_priv);
Sagar Arun Kambleda943b52018-01-10 18:24:16 +05305483
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00005484 if (ret != -EIO)
5485 i915_gem_cleanup_userptr(dev_priv);
5486
Chris Wilson60990322014-04-09 09:19:42 +01005487 if (ret == -EIO) {
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00005488 /*
5489 * Allow engine initialisation to fail by marking the GPU as
Chris Wilson60990322014-04-09 09:19:42 +01005490 * wedged. But we only want to do this where the GPU is angry,
5491 * for all other failure, such as an allocation failure, bail.
5492 */
Chris Wilson6f74b362017-10-15 15:37:25 +01005493 if (!i915_terminally_wedged(&dev_priv->gpu_error)) {
5494 DRM_ERROR("Failed to initialize GPU, declaring it wedged\n");
5495 i915_gem_set_wedged(dev_priv);
5496 }
Chris Wilson60990322014-04-09 09:19:42 +01005497 ret = 0;
Chris Wilson1070a422012-04-24 15:47:41 +01005498 }
5499
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00005500 i915_gem_drain_freed_objects(dev_priv);
Chris Wilson60990322014-04-09 09:19:42 +01005501 return ret;
Chris Wilson1070a422012-04-24 15:47:41 +01005502}
5503
Chris Wilson24145512017-01-24 11:01:35 +00005504void i915_gem_init_mmio(struct drm_i915_private *i915)
5505{
5506 i915_gem_sanitize(i915);
5507}
5508
Zou Nan hai8187a2b2010-05-21 09:08:55 +08005509void
Tvrtko Ursulincb15d9f2016-12-01 14:16:39 +00005510i915_gem_cleanup_engines(struct drm_i915_private *dev_priv)
Zou Nan hai8187a2b2010-05-21 09:08:55 +08005511{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00005512 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05305513 enum intel_engine_id id;
Zou Nan hai8187a2b2010-05-21 09:08:55 +08005514
Akash Goel3b3f1652016-10-13 22:44:48 +05305515 for_each_engine(engine, dev_priv, id)
Tvrtko Ursulin117897f2016-03-16 11:00:40 +00005516 dev_priv->gt.cleanup_engine(engine);
Zou Nan hai8187a2b2010-05-21 09:08:55 +08005517}
5518
Eric Anholt673a3942008-07-30 12:06:12 -07005519void
Imre Deak40ae4e12016-03-16 14:54:03 +02005520i915_gem_load_init_fences(struct drm_i915_private *dev_priv)
5521{
Chris Wilson49ef5292016-08-18 17:17:00 +01005522 int i;
Imre Deak40ae4e12016-03-16 14:54:03 +02005523
Tvrtko Ursulinc56b89f2018-02-09 21:58:46 +00005524 if (INTEL_GEN(dev_priv) >= 7 && !IS_VALLEYVIEW(dev_priv) &&
Imre Deak40ae4e12016-03-16 14:54:03 +02005525 !IS_CHERRYVIEW(dev_priv))
5526 dev_priv->num_fence_regs = 32;
Tvrtko Ursulinc56b89f2018-02-09 21:58:46 +00005527 else if (INTEL_GEN(dev_priv) >= 4 ||
Jani Nikula73f67aa2016-12-07 22:48:09 +02005528 IS_I945G(dev_priv) || IS_I945GM(dev_priv) ||
5529 IS_G33(dev_priv) || IS_PINEVIEW(dev_priv))
Imre Deak40ae4e12016-03-16 14:54:03 +02005530 dev_priv->num_fence_regs = 16;
5531 else
5532 dev_priv->num_fence_regs = 8;
5533
Chris Wilsonc0336662016-05-06 15:40:21 +01005534 if (intel_vgpu_active(dev_priv))
Imre Deak40ae4e12016-03-16 14:54:03 +02005535 dev_priv->num_fence_regs =
5536 I915_READ(vgtif_reg(avail_rs.fence_num));
5537
5538 /* Initialize fence registers to zero */
Chris Wilson49ef5292016-08-18 17:17:00 +01005539 for (i = 0; i < dev_priv->num_fence_regs; i++) {
5540 struct drm_i915_fence_reg *fence = &dev_priv->fence_regs[i];
5541
5542 fence->i915 = dev_priv;
5543 fence->id = i;
5544 list_add_tail(&fence->link, &dev_priv->mm.fence_list);
5545 }
Tvrtko Ursulin4362f4f2016-11-16 08:55:33 +00005546 i915_gem_restore_fences(dev_priv);
Imre Deak40ae4e12016-03-16 14:54:03 +02005547
Tvrtko Ursulin4362f4f2016-11-16 08:55:33 +00005548 i915_gem_detect_bit_6_swizzle(dev_priv);
Imre Deak40ae4e12016-03-16 14:54:03 +02005549}
5550
Chris Wilson9c52d1c2017-11-10 23:24:47 +00005551static void i915_gem_init__mm(struct drm_i915_private *i915)
5552{
5553 spin_lock_init(&i915->mm.object_stat_lock);
5554 spin_lock_init(&i915->mm.obj_lock);
5555 spin_lock_init(&i915->mm.free_lock);
5556
5557 init_llist_head(&i915->mm.free_list);
5558
5559 INIT_LIST_HEAD(&i915->mm.unbound_list);
5560 INIT_LIST_HEAD(&i915->mm.bound_list);
5561 INIT_LIST_HEAD(&i915->mm.fence_list);
5562 INIT_LIST_HEAD(&i915->mm.userfault_list);
5563
5564 INIT_WORK(&i915->mm.free_work, __i915_gem_free_work);
5565}
5566
Michal Wajdeczkoa0de9082018-03-23 12:34:49 +00005567int i915_gem_init_early(struct drm_i915_private *dev_priv)
Eric Anholt673a3942008-07-30 12:06:12 -07005568{
Tvrtko Ursulina9335682016-11-02 15:14:59 +00005569 int err = -ENOMEM;
Chris Wilson42dcedd2012-11-15 11:32:30 +00005570
Tvrtko Ursulina9335682016-11-02 15:14:59 +00005571 dev_priv->objects = KMEM_CACHE(drm_i915_gem_object, SLAB_HWCACHE_ALIGN);
5572 if (!dev_priv->objects)
Chris Wilson73cb9702016-10-28 13:58:46 +01005573 goto err_out;
Chris Wilson73cb9702016-10-28 13:58:46 +01005574
Tvrtko Ursulina9335682016-11-02 15:14:59 +00005575 dev_priv->vmas = KMEM_CACHE(i915_vma, SLAB_HWCACHE_ALIGN);
5576 if (!dev_priv->vmas)
Chris Wilson73cb9702016-10-28 13:58:46 +01005577 goto err_objects;
Chris Wilson73cb9702016-10-28 13:58:46 +01005578
Chris Wilsond1b48c12017-08-16 09:52:08 +01005579 dev_priv->luts = KMEM_CACHE(i915_lut_handle, 0);
5580 if (!dev_priv->luts)
5581 goto err_vmas;
5582
Chris Wilsone61e0f52018-02-21 09:56:36 +00005583 dev_priv->requests = KMEM_CACHE(i915_request,
Tvrtko Ursulina9335682016-11-02 15:14:59 +00005584 SLAB_HWCACHE_ALIGN |
5585 SLAB_RECLAIM_ACCOUNT |
Paul E. McKenney5f0d5a32017-01-18 02:53:44 -08005586 SLAB_TYPESAFE_BY_RCU);
Tvrtko Ursulina9335682016-11-02 15:14:59 +00005587 if (!dev_priv->requests)
Chris Wilsond1b48c12017-08-16 09:52:08 +01005588 goto err_luts;
Chris Wilson73cb9702016-10-28 13:58:46 +01005589
Chris Wilson52e54202016-11-14 20:41:02 +00005590 dev_priv->dependencies = KMEM_CACHE(i915_dependency,
5591 SLAB_HWCACHE_ALIGN |
5592 SLAB_RECLAIM_ACCOUNT);
5593 if (!dev_priv->dependencies)
5594 goto err_requests;
5595
Chris Wilsonc5cf9a92017-05-17 13:10:04 +01005596 dev_priv->priorities = KMEM_CACHE(i915_priolist, SLAB_HWCACHE_ALIGN);
5597 if (!dev_priv->priorities)
5598 goto err_dependencies;
5599
Chris Wilson73cb9702016-10-28 13:58:46 +01005600 mutex_lock(&dev_priv->drm.struct_mutex);
5601 INIT_LIST_HEAD(&dev_priv->gt.timelines);
Chris Wilsonbb894852016-11-14 20:40:57 +00005602 err = i915_gem_timeline_init__global(dev_priv);
Chris Wilson73cb9702016-10-28 13:58:46 +01005603 mutex_unlock(&dev_priv->drm.struct_mutex);
5604 if (err)
Chris Wilsonc5cf9a92017-05-17 13:10:04 +01005605 goto err_priorities;
Eric Anholt673a3942008-07-30 12:06:12 -07005606
Chris Wilson9c52d1c2017-11-10 23:24:47 +00005607 i915_gem_init__mm(dev_priv);
Chris Wilsonf2123812017-10-16 12:40:37 +01005608
Chris Wilson67d97da2016-07-04 08:08:31 +01005609 INIT_DELAYED_WORK(&dev_priv->gt.retire_work,
Eric Anholt673a3942008-07-30 12:06:12 -07005610 i915_gem_retire_work_handler);
Chris Wilson67d97da2016-07-04 08:08:31 +01005611 INIT_DELAYED_WORK(&dev_priv->gt.idle_work,
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005612 i915_gem_idle_work_handler);
Chris Wilson1f15b762016-07-01 17:23:14 +01005613 init_waitqueue_head(&dev_priv->gpu_error.wait_queue);
Daniel Vetter1f83fee2012-11-15 17:17:22 +01005614 init_waitqueue_head(&dev_priv->gpu_error.reset_queue);
Chris Wilson31169712009-09-14 16:50:28 +01005615
Joonas Lahtinen6f633402016-09-01 14:58:21 +03005616 atomic_set(&dev_priv->mm.bsd_engine_dispatch_index, 0);
5617
Chris Wilsonb5add952016-08-04 16:32:36 +01005618 spin_lock_init(&dev_priv->fb_tracking.lock);
Chris Wilson73cb9702016-10-28 13:58:46 +01005619
Matthew Auld465c4032017-10-06 23:18:14 +01005620 err = i915_gemfs_init(dev_priv);
5621 if (err)
5622 DRM_NOTE("Unable to create a private tmpfs mount, hugepage support will be disabled(%d).\n", err);
5623
Chris Wilson73cb9702016-10-28 13:58:46 +01005624 return 0;
5625
Chris Wilsonc5cf9a92017-05-17 13:10:04 +01005626err_priorities:
5627 kmem_cache_destroy(dev_priv->priorities);
Chris Wilson52e54202016-11-14 20:41:02 +00005628err_dependencies:
5629 kmem_cache_destroy(dev_priv->dependencies);
Chris Wilson73cb9702016-10-28 13:58:46 +01005630err_requests:
5631 kmem_cache_destroy(dev_priv->requests);
Chris Wilsond1b48c12017-08-16 09:52:08 +01005632err_luts:
5633 kmem_cache_destroy(dev_priv->luts);
Chris Wilson73cb9702016-10-28 13:58:46 +01005634err_vmas:
5635 kmem_cache_destroy(dev_priv->vmas);
5636err_objects:
5637 kmem_cache_destroy(dev_priv->objects);
5638err_out:
5639 return err;
Eric Anholt673a3942008-07-30 12:06:12 -07005640}
Dave Airlie71acb5e2008-12-30 20:31:46 +10005641
Michal Wajdeczkoa0de9082018-03-23 12:34:49 +00005642void i915_gem_cleanup_early(struct drm_i915_private *dev_priv)
Imre Deakd64aa092016-01-19 15:26:29 +02005643{
Chris Wilsonc4d4c1c2017-02-10 16:35:23 +00005644 i915_gem_drain_freed_objects(dev_priv);
Chris Wilsonc9c70472018-02-19 22:06:31 +00005645 GEM_BUG_ON(!llist_empty(&dev_priv->mm.free_list));
5646 GEM_BUG_ON(atomic_read(&dev_priv->mm.free_count));
Chris Wilsonc4d4c1c2017-02-10 16:35:23 +00005647 WARN_ON(dev_priv->mm.object_count);
Chris Wilson7d5d59e2016-11-01 08:48:41 +00005648
Matthew Auldea84aa72016-11-17 21:04:11 +00005649 mutex_lock(&dev_priv->drm.struct_mutex);
5650 i915_gem_timeline_fini(&dev_priv->gt.global_timeline);
5651 WARN_ON(!list_empty(&dev_priv->gt.timelines));
5652 mutex_unlock(&dev_priv->drm.struct_mutex);
5653
Chris Wilsonc5cf9a92017-05-17 13:10:04 +01005654 kmem_cache_destroy(dev_priv->priorities);
Chris Wilson52e54202016-11-14 20:41:02 +00005655 kmem_cache_destroy(dev_priv->dependencies);
Imre Deakd64aa092016-01-19 15:26:29 +02005656 kmem_cache_destroy(dev_priv->requests);
Chris Wilsond1b48c12017-08-16 09:52:08 +01005657 kmem_cache_destroy(dev_priv->luts);
Imre Deakd64aa092016-01-19 15:26:29 +02005658 kmem_cache_destroy(dev_priv->vmas);
5659 kmem_cache_destroy(dev_priv->objects);
Chris Wilson0eafec62016-08-04 16:32:41 +01005660
5661 /* And ensure that our DESTROY_BY_RCU slabs are truly destroyed */
5662 rcu_barrier();
Matthew Auld465c4032017-10-06 23:18:14 +01005663
5664 i915_gemfs_fini(dev_priv);
Imre Deakd64aa092016-01-19 15:26:29 +02005665}
5666
Chris Wilson6a800ea2016-09-21 14:51:07 +01005667int i915_gem_freeze(struct drm_i915_private *dev_priv)
5668{
Chris Wilsond0aa3012017-04-07 11:25:49 +01005669 /* Discard all purgeable objects, let userspace recover those as
5670 * required after resuming.
5671 */
Chris Wilson6a800ea2016-09-21 14:51:07 +01005672 i915_gem_shrink_all(dev_priv);
Chris Wilson6a800ea2016-09-21 14:51:07 +01005673
Chris Wilson6a800ea2016-09-21 14:51:07 +01005674 return 0;
5675}
5676
Chris Wilson461fb992016-05-14 07:26:33 +01005677int i915_gem_freeze_late(struct drm_i915_private *dev_priv)
5678{
5679 struct drm_i915_gem_object *obj;
Chris Wilson7aab2d52016-09-09 20:02:18 +01005680 struct list_head *phases[] = {
5681 &dev_priv->mm.unbound_list,
5682 &dev_priv->mm.bound_list,
5683 NULL
5684 }, **p;
Chris Wilson461fb992016-05-14 07:26:33 +01005685
5686 /* Called just before we write the hibernation image.
5687 *
5688 * We need to update the domain tracking to reflect that the CPU
5689 * will be accessing all the pages to create and restore from the
5690 * hibernation, and so upon restoration those pages will be in the
5691 * CPU domain.
5692 *
5693 * To make sure the hibernation image contains the latest state,
5694 * we update that state just before writing out the image.
Chris Wilson7aab2d52016-09-09 20:02:18 +01005695 *
5696 * To try and reduce the hibernation image, we manually shrink
Chris Wilsond0aa3012017-04-07 11:25:49 +01005697 * the objects as well, see i915_gem_freeze()
Chris Wilson461fb992016-05-14 07:26:33 +01005698 */
5699
Chris Wilson912d5722017-09-06 16:19:30 -07005700 i915_gem_shrink(dev_priv, -1UL, NULL, I915_SHRINK_UNBOUND);
Chris Wilson17b93c42017-04-07 11:25:50 +01005701 i915_gem_drain_freed_objects(dev_priv);
Chris Wilson461fb992016-05-14 07:26:33 +01005702
Chris Wilsonf2123812017-10-16 12:40:37 +01005703 spin_lock(&dev_priv->mm.obj_lock);
Chris Wilson7aab2d52016-09-09 20:02:18 +01005704 for (p = phases; *p; p++) {
Chris Wilsonf2123812017-10-16 12:40:37 +01005705 list_for_each_entry(obj, *p, mm.link)
Chris Wilsone27ab732017-06-15 13:38:49 +01005706 __start_cpu_write(obj);
Chris Wilson461fb992016-05-14 07:26:33 +01005707 }
Chris Wilsonf2123812017-10-16 12:40:37 +01005708 spin_unlock(&dev_priv->mm.obj_lock);
Chris Wilson461fb992016-05-14 07:26:33 +01005709
5710 return 0;
5711}
5712
Chris Wilsonf787a5f2010-09-24 16:02:42 +01005713void i915_gem_release(struct drm_device *dev, struct drm_file *file)
Eric Anholtb9624422009-06-03 07:27:35 +00005714{
Chris Wilsonf787a5f2010-09-24 16:02:42 +01005715 struct drm_i915_file_private *file_priv = file->driver_priv;
Chris Wilsone61e0f52018-02-21 09:56:36 +00005716 struct i915_request *request;
Eric Anholtb9624422009-06-03 07:27:35 +00005717
5718 /* Clean up our request list when the client is going away, so that
5719 * later retire_requests won't dereference our soon-to-be-gone
5720 * file_priv.
5721 */
Chris Wilson1c255952010-09-26 11:03:27 +01005722 spin_lock(&file_priv->mm.lock);
Chris Wilsonc8659ef2017-03-02 12:25:25 +00005723 list_for_each_entry(request, &file_priv->mm.request_list, client_link)
Chris Wilsonf787a5f2010-09-24 16:02:42 +01005724 request->file_priv = NULL;
Chris Wilson1c255952010-09-26 11:03:27 +01005725 spin_unlock(&file_priv->mm.lock);
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005726}
5727
Chris Wilson829a0af2017-06-20 12:05:45 +01005728int i915_gem_open(struct drm_i915_private *i915, struct drm_file *file)
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005729{
5730 struct drm_i915_file_private *file_priv;
Ben Widawskye422b882013-12-06 14:10:58 -08005731 int ret;
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005732
Chris Wilsonc4c29d72016-11-09 10:45:07 +00005733 DRM_DEBUG("\n");
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005734
5735 file_priv = kzalloc(sizeof(*file_priv), GFP_KERNEL);
5736 if (!file_priv)
5737 return -ENOMEM;
5738
5739 file->driver_priv = file_priv;
Chris Wilson829a0af2017-06-20 12:05:45 +01005740 file_priv->dev_priv = i915;
Chris Wilsonab0e7ff2014-02-25 17:11:24 +02005741 file_priv->file = file;
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005742
5743 spin_lock_init(&file_priv->mm.lock);
5744 INIT_LIST_HEAD(&file_priv->mm.request_list);
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005745
Chris Wilsonc80ff162016-07-27 09:07:27 +01005746 file_priv->bsd_engine = -1;
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00005747
Chris Wilson829a0af2017-06-20 12:05:45 +01005748 ret = i915_gem_context_open(i915, file);
Ben Widawskye422b882013-12-06 14:10:58 -08005749 if (ret)
5750 kfree(file_priv);
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005751
Ben Widawskye422b882013-12-06 14:10:58 -08005752 return ret;
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005753}
5754
Daniel Vetterb680c372014-09-19 18:27:27 +02005755/**
5756 * i915_gem_track_fb - update frontbuffer tracking
Geliang Tangd9072a32015-09-15 05:58:44 -07005757 * @old: current GEM buffer for the frontbuffer slots
5758 * @new: new GEM buffer for the frontbuffer slots
5759 * @frontbuffer_bits: bitmask of frontbuffer slots
Daniel Vetterb680c372014-09-19 18:27:27 +02005760 *
5761 * This updates the frontbuffer tracking bits @frontbuffer_bits by clearing them
5762 * from @old and setting them in @new. Both @old and @new can be NULL.
5763 */
Daniel Vettera071fa02014-06-18 23:28:09 +02005764void i915_gem_track_fb(struct drm_i915_gem_object *old,
5765 struct drm_i915_gem_object *new,
5766 unsigned frontbuffer_bits)
5767{
Chris Wilsonfaf5bf02016-08-04 16:32:37 +01005768 /* Control of individual bits within the mask are guarded by
5769 * the owning plane->mutex, i.e. we can never see concurrent
5770 * manipulation of individual bits. But since the bitfield as a whole
5771 * is updated using RMW, we need to use atomics in order to update
5772 * the bits.
5773 */
5774 BUILD_BUG_ON(INTEL_FRONTBUFFER_BITS_PER_PIPE * I915_MAX_PIPES >
5775 sizeof(atomic_t) * BITS_PER_BYTE);
5776
Daniel Vettera071fa02014-06-18 23:28:09 +02005777 if (old) {
Chris Wilsonfaf5bf02016-08-04 16:32:37 +01005778 WARN_ON(!(atomic_read(&old->frontbuffer_bits) & frontbuffer_bits));
5779 atomic_andnot(frontbuffer_bits, &old->frontbuffer_bits);
Daniel Vettera071fa02014-06-18 23:28:09 +02005780 }
5781
5782 if (new) {
Chris Wilsonfaf5bf02016-08-04 16:32:37 +01005783 WARN_ON(atomic_read(&new->frontbuffer_bits) & frontbuffer_bits);
5784 atomic_or(frontbuffer_bits, &new->frontbuffer_bits);
Daniel Vettera071fa02014-06-18 23:28:09 +02005785 }
5786}
5787
Dave Gordonea702992015-07-09 19:29:02 +01005788/* Allocate a new GEM object and fill it with the supplied data */
5789struct drm_i915_gem_object *
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +00005790i915_gem_object_create_from_data(struct drm_i915_private *dev_priv,
Dave Gordonea702992015-07-09 19:29:02 +01005791 const void *data, size_t size)
5792{
5793 struct drm_i915_gem_object *obj;
Chris Wilsonbe062fa2017-03-17 19:46:48 +00005794 struct file *file;
5795 size_t offset;
5796 int err;
Dave Gordonea702992015-07-09 19:29:02 +01005797
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +00005798 obj = i915_gem_object_create(dev_priv, round_up(size, PAGE_SIZE));
Chris Wilsonfe3db792016-04-25 13:32:13 +01005799 if (IS_ERR(obj))
Dave Gordonea702992015-07-09 19:29:02 +01005800 return obj;
5801
Christian Königc0a51fd2018-02-16 13:43:38 +01005802 GEM_BUG_ON(obj->write_domain != I915_GEM_DOMAIN_CPU);
Dave Gordonea702992015-07-09 19:29:02 +01005803
Chris Wilsonbe062fa2017-03-17 19:46:48 +00005804 file = obj->base.filp;
5805 offset = 0;
5806 do {
5807 unsigned int len = min_t(typeof(size), size, PAGE_SIZE);
5808 struct page *page;
5809 void *pgdata, *vaddr;
Dave Gordonea702992015-07-09 19:29:02 +01005810
Chris Wilsonbe062fa2017-03-17 19:46:48 +00005811 err = pagecache_write_begin(file, file->f_mapping,
5812 offset, len, 0,
5813 &page, &pgdata);
5814 if (err < 0)
5815 goto fail;
Dave Gordonea702992015-07-09 19:29:02 +01005816
Chris Wilsonbe062fa2017-03-17 19:46:48 +00005817 vaddr = kmap(page);
5818 memcpy(vaddr, data, len);
5819 kunmap(page);
5820
5821 err = pagecache_write_end(file, file->f_mapping,
5822 offset, len, len,
5823 page, pgdata);
5824 if (err < 0)
5825 goto fail;
5826
5827 size -= len;
5828 data += len;
5829 offset += len;
5830 } while (size);
Dave Gordonea702992015-07-09 19:29:02 +01005831
5832 return obj;
5833
5834fail:
Chris Wilsonf8c417c2016-07-20 13:31:53 +01005835 i915_gem_object_put(obj);
Chris Wilsonbe062fa2017-03-17 19:46:48 +00005836 return ERR_PTR(err);
Dave Gordonea702992015-07-09 19:29:02 +01005837}
Chris Wilson96d77632016-10-28 13:58:33 +01005838
5839struct scatterlist *
5840i915_gem_object_get_sg(struct drm_i915_gem_object *obj,
5841 unsigned int n,
5842 unsigned int *offset)
5843{
Chris Wilsona4f5ea62016-10-28 13:58:35 +01005844 struct i915_gem_object_page_iter *iter = &obj->mm.get_page;
Chris Wilson96d77632016-10-28 13:58:33 +01005845 struct scatterlist *sg;
5846 unsigned int idx, count;
5847
5848 might_sleep();
5849 GEM_BUG_ON(n >= obj->base.size >> PAGE_SHIFT);
Chris Wilsona4f5ea62016-10-28 13:58:35 +01005850 GEM_BUG_ON(!i915_gem_object_has_pinned_pages(obj));
Chris Wilson96d77632016-10-28 13:58:33 +01005851
5852 /* As we iterate forward through the sg, we record each entry in a
5853 * radixtree for quick repeated (backwards) lookups. If we have seen
5854 * this index previously, we will have an entry for it.
5855 *
5856 * Initial lookup is O(N), but this is amortized to O(1) for
5857 * sequential page access (where each new request is consecutive
5858 * to the previous one). Repeated lookups are O(lg(obj->base.size)),
5859 * i.e. O(1) with a large constant!
5860 */
5861 if (n < READ_ONCE(iter->sg_idx))
5862 goto lookup;
5863
5864 mutex_lock(&iter->lock);
5865
5866 /* We prefer to reuse the last sg so that repeated lookup of this
5867 * (or the subsequent) sg are fast - comparing against the last
5868 * sg is faster than going through the radixtree.
5869 */
5870
5871 sg = iter->sg_pos;
5872 idx = iter->sg_idx;
5873 count = __sg_page_count(sg);
5874
5875 while (idx + count <= n) {
5876 unsigned long exception, i;
5877 int ret;
5878
5879 /* If we cannot allocate and insert this entry, or the
5880 * individual pages from this range, cancel updating the
5881 * sg_idx so that on this lookup we are forced to linearly
5882 * scan onwards, but on future lookups we will try the
5883 * insertion again (in which case we need to be careful of
5884 * the error return reporting that we have already inserted
5885 * this index).
5886 */
5887 ret = radix_tree_insert(&iter->radix, idx, sg);
5888 if (ret && ret != -EEXIST)
5889 goto scan;
5890
5891 exception =
5892 RADIX_TREE_EXCEPTIONAL_ENTRY |
5893 idx << RADIX_TREE_EXCEPTIONAL_SHIFT;
5894 for (i = 1; i < count; i++) {
5895 ret = radix_tree_insert(&iter->radix, idx + i,
5896 (void *)exception);
5897 if (ret && ret != -EEXIST)
5898 goto scan;
5899 }
5900
5901 idx += count;
5902 sg = ____sg_next(sg);
5903 count = __sg_page_count(sg);
5904 }
5905
5906scan:
5907 iter->sg_pos = sg;
5908 iter->sg_idx = idx;
5909
5910 mutex_unlock(&iter->lock);
5911
5912 if (unlikely(n < idx)) /* insertion completed by another thread */
5913 goto lookup;
5914
5915 /* In case we failed to insert the entry into the radixtree, we need
5916 * to look beyond the current sg.
5917 */
5918 while (idx + count <= n) {
5919 idx += count;
5920 sg = ____sg_next(sg);
5921 count = __sg_page_count(sg);
5922 }
5923
5924 *offset = n - idx;
5925 return sg;
5926
5927lookup:
5928 rcu_read_lock();
5929
5930 sg = radix_tree_lookup(&iter->radix, n);
5931 GEM_BUG_ON(!sg);
5932
5933 /* If this index is in the middle of multi-page sg entry,
5934 * the radixtree will contain an exceptional entry that points
5935 * to the start of that range. We will return the pointer to
5936 * the base page and the offset of this page within the
5937 * sg entry's range.
5938 */
5939 *offset = 0;
5940 if (unlikely(radix_tree_exception(sg))) {
5941 unsigned long base =
5942 (unsigned long)sg >> RADIX_TREE_EXCEPTIONAL_SHIFT;
5943
5944 sg = radix_tree_lookup(&iter->radix, base);
5945 GEM_BUG_ON(!sg);
5946
5947 *offset = n - base;
5948 }
5949
5950 rcu_read_unlock();
5951
5952 return sg;
5953}
5954
5955struct page *
5956i915_gem_object_get_page(struct drm_i915_gem_object *obj, unsigned int n)
5957{
5958 struct scatterlist *sg;
5959 unsigned int offset;
5960
5961 GEM_BUG_ON(!i915_gem_object_has_struct_page(obj));
5962
5963 sg = i915_gem_object_get_sg(obj, n, &offset);
5964 return nth_page(sg_page(sg), offset);
5965}
5966
5967/* Like i915_gem_object_get_page(), but mark the returned page dirty */
5968struct page *
5969i915_gem_object_get_dirty_page(struct drm_i915_gem_object *obj,
5970 unsigned int n)
5971{
5972 struct page *page;
5973
5974 page = i915_gem_object_get_page(obj, n);
Chris Wilsona4f5ea62016-10-28 13:58:35 +01005975 if (!obj->mm.dirty)
Chris Wilson96d77632016-10-28 13:58:33 +01005976 set_page_dirty(page);
5977
5978 return page;
5979}
5980
5981dma_addr_t
5982i915_gem_object_get_dma_address(struct drm_i915_gem_object *obj,
5983 unsigned long n)
5984{
5985 struct scatterlist *sg;
5986 unsigned int offset;
5987
5988 sg = i915_gem_object_get_sg(obj, n, &offset);
5989 return sg_dma_address(sg) + (offset << PAGE_SHIFT);
5990}
Chris Wilson935a2f72017-02-13 17:15:13 +00005991
Chris Wilson8eeb7902017-07-26 19:16:01 +01005992int i915_gem_object_attach_phys(struct drm_i915_gem_object *obj, int align)
5993{
5994 struct sg_table *pages;
5995 int err;
5996
5997 if (align > obj->base.size)
5998 return -EINVAL;
5999
6000 if (obj->ops == &i915_gem_phys_ops)
6001 return 0;
6002
6003 if (obj->ops != &i915_gem_object_ops)
6004 return -EINVAL;
6005
6006 err = i915_gem_object_unbind(obj);
6007 if (err)
6008 return err;
6009
6010 mutex_lock(&obj->mm.lock);
6011
6012 if (obj->mm.madv != I915_MADV_WILLNEED) {
6013 err = -EFAULT;
6014 goto err_unlock;
6015 }
6016
6017 if (obj->mm.quirked) {
6018 err = -EFAULT;
6019 goto err_unlock;
6020 }
6021
6022 if (obj->mm.mapping) {
6023 err = -EBUSY;
6024 goto err_unlock;
6025 }
6026
Chris Wilsonf2123812017-10-16 12:40:37 +01006027 pages = fetch_and_zero(&obj->mm.pages);
6028 if (pages) {
6029 struct drm_i915_private *i915 = to_i915(obj->base.dev);
6030
6031 __i915_gem_object_reset_page_iter(obj);
6032
6033 spin_lock(&i915->mm.obj_lock);
6034 list_del(&obj->mm.link);
6035 spin_unlock(&i915->mm.obj_lock);
6036 }
6037
Chris Wilson8eeb7902017-07-26 19:16:01 +01006038 obj->ops = &i915_gem_phys_ops;
6039
Chris Wilson8fb6a5d2017-07-26 19:16:02 +01006040 err = ____i915_gem_object_get_pages(obj);
Chris Wilson8eeb7902017-07-26 19:16:01 +01006041 if (err)
6042 goto err_xfer;
6043
6044 /* Perma-pin (until release) the physical set of pages */
6045 __i915_gem_object_pin_pages(obj);
6046
6047 if (!IS_ERR_OR_NULL(pages))
6048 i915_gem_object_ops.put_pages(obj, pages);
6049 mutex_unlock(&obj->mm.lock);
6050 return 0;
6051
6052err_xfer:
6053 obj->ops = &i915_gem_object_ops;
6054 obj->mm.pages = pages;
6055err_unlock:
6056 mutex_unlock(&obj->mm.lock);
6057 return err;
6058}
6059
Chris Wilson935a2f72017-02-13 17:15:13 +00006060#if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
6061#include "selftests/scatterlist.c"
Chris Wilson66d9cb52017-02-13 17:15:17 +00006062#include "selftests/mock_gem_device.c"
Chris Wilson44653982017-02-13 17:15:20 +00006063#include "selftests/huge_gem_object.c"
Matthew Auld40498662017-10-06 23:18:29 +01006064#include "selftests/huge_pages.c"
Chris Wilson8335fd62017-02-13 17:15:28 +00006065#include "selftests/i915_gem_object.c"
Chris Wilson17059452017-02-13 17:15:32 +00006066#include "selftests/i915_gem_coherency.c"
Chris Wilson935a2f72017-02-13 17:15:13 +00006067#endif