blob: 565b2fa1607d9c6db6de33b49d29e74c6669b196 [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 Herrmann0de23972013-07-24 21:07:52 +020028#include <drm/drm_vma_manager.h>
David Howells760285e2012-10-02 18:01:07 +010029#include <drm/i915_drm.h>
Eric Anholt673a3942008-07-30 12:06:12 -070030#include "i915_drv.h"
Chris Wilson57822dc2017-02-22 11:40:48 +000031#include "i915_gem_clflush.h"
Yu Zhangeb822892015-02-10 19:05:49 +080032#include "i915_vgpu.h"
Chris Wilson1c5d22f2009-08-25 11:15:50 +010033#include "i915_trace.h"
Jesse Barnes652c3932009-08-17 13:31:43 -070034#include "intel_drv.h"
Chris Wilson5d723d72016-08-04 16:32:35 +010035#include "intel_frontbuffer.h"
Peter Antoine0ccdacf2016-04-13 15:03:25 +010036#include "intel_mocs.h"
Oscar Mateo59b449d2018-04-10 09:12:47 -070037#include "intel_workarounds.h"
Matthew Auld465c4032017-10-06 23:18:14 +010038#include "i915_gemfs.h"
Chris Wilson6b5e90f2016-11-14 20:41:05 +000039#include <linux/dma-fence-array.h>
Chris Wilsonfe3288b2017-02-12 17:20:01 +000040#include <linux/kthread.h>
Chris Wilsonc13d87e2016-07-20 09:21:15 +010041#include <linux/reservation.h>
Hugh Dickins5949eac2011-06-27 16:18:18 -070042#include <linux/shmem_fs.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090043#include <linux/slab.h>
Chris Wilson20e49332016-11-22 14:41:21 +000044#include <linux/stop_machine.h>
Eric Anholt673a3942008-07-30 12:06:12 -070045#include <linux/swap.h>
Jesse Barnes79e53942008-11-07 14:24:08 -080046#include <linux/pci.h>
Daniel Vetter1286ff72012-05-10 15:25:09 +020047#include <linux/dma-buf.h>
Eric Anholt673a3942008-07-30 12:06:12 -070048
Chris Wilsonfbbd37b2016-10-28 13:58:42 +010049static void i915_gem_flush_free_objects(struct drm_i915_private *i915);
Chris Wilson61050802012-04-17 15:31:31 +010050
Chris Wilson2c225692013-08-09 12:26:45 +010051static bool cpu_write_needs_clflush(struct drm_i915_gem_object *obj)
52{
Chris Wilsone27ab732017-06-15 13:38:49 +010053 if (obj->cache_dirty)
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +053054 return false;
55
Chris Wilsonb8f55be2017-08-11 12:11:16 +010056 if (!(obj->cache_coherent & I915_BO_CACHE_COHERENT_FOR_WRITE))
Chris Wilson2c225692013-08-09 12:26:45 +010057 return true;
58
Chris Wilsonbd3d2252017-10-13 21:26:14 +010059 return obj->pin_global; /* currently in use by HW, keep flushed */
Chris Wilson2c225692013-08-09 12:26:45 +010060}
61
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +053062static int
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +010063insert_mappable_node(struct i915_ggtt *ggtt,
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +053064 struct drm_mm_node *node, u32 size)
65{
66 memset(node, 0, sizeof(*node));
Chris Wilson82ad6442018-06-05 16:37:58 +010067 return drm_mm_insert_node_in_range(&ggtt->vm.mm, node,
Chris Wilson4e64e552017-02-02 21:04:38 +000068 size, 0, I915_COLOR_UNEVICTABLE,
69 0, ggtt->mappable_end,
70 DRM_MM_INSERT_LOW);
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +053071}
72
73static void
74remove_mappable_node(struct drm_mm_node *node)
75{
76 drm_mm_remove_node(node);
77}
78
Chris Wilson73aa8082010-09-30 11:46:12 +010079/* some bookkeeping */
80static void i915_gem_info_add_obj(struct drm_i915_private *dev_priv,
Chris Wilson3ef7f222016-10-18 13:02:48 +010081 u64 size)
Chris Wilson73aa8082010-09-30 11:46:12 +010082{
Daniel Vetterc20e8352013-07-24 22:40:23 +020083 spin_lock(&dev_priv->mm.object_stat_lock);
Chris Wilson73aa8082010-09-30 11:46:12 +010084 dev_priv->mm.object_count++;
85 dev_priv->mm.object_memory += size;
Daniel Vetterc20e8352013-07-24 22:40:23 +020086 spin_unlock(&dev_priv->mm.object_stat_lock);
Chris Wilson73aa8082010-09-30 11:46:12 +010087}
88
89static void i915_gem_info_remove_obj(struct drm_i915_private *dev_priv,
Chris Wilson3ef7f222016-10-18 13:02:48 +010090 u64 size)
Chris Wilson73aa8082010-09-30 11:46:12 +010091{
Daniel Vetterc20e8352013-07-24 22:40:23 +020092 spin_lock(&dev_priv->mm.object_stat_lock);
Chris Wilson73aa8082010-09-30 11:46:12 +010093 dev_priv->mm.object_count--;
94 dev_priv->mm.object_memory -= size;
Daniel Vetterc20e8352013-07-24 22:40:23 +020095 spin_unlock(&dev_priv->mm.object_stat_lock);
Chris Wilson73aa8082010-09-30 11:46:12 +010096}
97
Chris Wilson21dd3732011-01-26 15:55:56 +000098static int
Daniel Vetter33196de2012-11-14 17:14:05 +010099i915_gem_wait_for_error(struct i915_gpu_error *error)
Chris Wilson30dbf0c2010-09-25 10:19:17 +0100100{
Chris Wilson30dbf0c2010-09-25 10:19:17 +0100101 int ret;
102
Chris Wilson4c7d62c2016-10-28 13:58:32 +0100103 might_sleep();
104
Daniel Vetter0a6759c2012-07-04 22:18:41 +0200105 /*
106 * Only wait 10 seconds for the gpu reset to complete to avoid hanging
107 * userspace. If it takes that long something really bad is going on and
108 * we should simply try to bail out and fail as gracefully as possible.
109 */
Daniel Vetter1f83fee2012-11-15 17:17:22 +0100110 ret = wait_event_interruptible_timeout(error->reset_queue,
Chris Wilson8c185ec2017-03-16 17:13:02 +0000111 !i915_reset_backoff(error),
Chris Wilsonb52992c2016-10-28 13:58:24 +0100112 I915_RESET_TIMEOUT);
Daniel Vetter0a6759c2012-07-04 22:18:41 +0200113 if (ret == 0) {
114 DRM_ERROR("Timed out waiting for the gpu reset to complete\n");
115 return -EIO;
116 } else if (ret < 0) {
Chris Wilson30dbf0c2010-09-25 10:19:17 +0100117 return ret;
Chris Wilsond98c52c2016-04-13 17:35:05 +0100118 } else {
119 return 0;
Daniel Vetter0a6759c2012-07-04 22:18:41 +0200120 }
Chris Wilson30dbf0c2010-09-25 10:19:17 +0100121}
122
Chris Wilson54cf91d2010-11-25 18:00:26 +0000123int i915_mutex_lock_interruptible(struct drm_device *dev)
Chris Wilson76c1dec2010-09-25 11:22:51 +0100124{
Chris Wilsonfac5e232016-07-04 11:34:36 +0100125 struct drm_i915_private *dev_priv = to_i915(dev);
Chris Wilson76c1dec2010-09-25 11:22:51 +0100126 int ret;
127
Daniel Vetter33196de2012-11-14 17:14:05 +0100128 ret = i915_gem_wait_for_error(&dev_priv->gpu_error);
Chris Wilson76c1dec2010-09-25 11:22:51 +0100129 if (ret)
130 return ret;
131
132 ret = mutex_lock_interruptible(&dev->struct_mutex);
133 if (ret)
134 return ret;
135
Chris Wilson76c1dec2010-09-25 11:22:51 +0100136 return 0;
137}
Chris Wilson30dbf0c2010-09-25 10:19:17 +0100138
Chris Wilsone4d20062018-04-06 16:51:44 +0100139static u32 __i915_gem_park(struct drm_i915_private *i915)
140{
Chris Wilson506d1f62019-01-14 14:21:11 +0000141 intel_wakeref_t wakeref;
142
Chris Wilson4dfacb02018-05-31 09:22:43 +0100143 GEM_TRACE("\n");
144
Chris Wilsone4d20062018-04-06 16:51:44 +0100145 lockdep_assert_held(&i915->drm.struct_mutex);
146 GEM_BUG_ON(i915->gt.active_requests);
Chris Wilson643b4502018-04-30 14:15:03 +0100147 GEM_BUG_ON(!list_empty(&i915->gt.active_rings));
Chris Wilsone4d20062018-04-06 16:51:44 +0100148
149 if (!i915->gt.awake)
150 return I915_EPOCH_INVALID;
151
152 GEM_BUG_ON(i915->gt.epoch == I915_EPOCH_INVALID);
153
154 /*
155 * Be paranoid and flush a concurrent interrupt to make sure
156 * we don't reactivate any irq tasklets after parking.
157 *
158 * FIXME: Note that even though we have waited for execlists to be idle,
159 * there may still be an in-flight interrupt even though the CSB
160 * is now empty. synchronize_irq() makes sure that a residual interrupt
161 * is completed before we continue, but it doesn't prevent the HW from
162 * raising a spurious interrupt later. To complete the shield we should
163 * coordinate disabling the CS irq with flushing the interrupts.
164 */
165 synchronize_irq(i915->drm.irq);
166
167 intel_engines_park(i915);
Chris Wilsona89d1f92018-05-02 17:38:39 +0100168 i915_timelines_park(i915);
Chris Wilsone4d20062018-04-06 16:51:44 +0100169
170 i915_pmu_gt_parked(i915);
Chris Wilson3365e222018-05-03 20:51:14 +0100171 i915_vma_parked(i915);
Chris Wilsone4d20062018-04-06 16:51:44 +0100172
Chris Wilson506d1f62019-01-14 14:21:11 +0000173 wakeref = fetch_and_zero(&i915->gt.awake);
174 GEM_BUG_ON(!wakeref);
Chris Wilsone4d20062018-04-06 16:51:44 +0100175
176 if (INTEL_GEN(i915) >= 6)
177 gen6_rps_idle(i915);
178
Chris Wilson8d761e72019-01-14 14:21:28 +0000179 intel_display_power_put(i915, POWER_DOMAIN_GT_IRQ, wakeref);
Chris Wilsone4d20062018-04-06 16:51:44 +0100180
181 return i915->gt.epoch;
182}
183
184void i915_gem_park(struct drm_i915_private *i915)
185{
Chris Wilson4dfacb02018-05-31 09:22:43 +0100186 GEM_TRACE("\n");
187
Chris Wilsone4d20062018-04-06 16:51:44 +0100188 lockdep_assert_held(&i915->drm.struct_mutex);
189 GEM_BUG_ON(i915->gt.active_requests);
190
191 if (!i915->gt.awake)
192 return;
193
194 /* Defer the actual call to __i915_gem_park() to prevent ping-pongs */
195 mod_delayed_work(i915->wq, &i915->gt.idle_work, msecs_to_jiffies(100));
196}
197
198void i915_gem_unpark(struct drm_i915_private *i915)
199{
Chris Wilson4dfacb02018-05-31 09:22:43 +0100200 GEM_TRACE("\n");
201
Chris Wilsone4d20062018-04-06 16:51:44 +0100202 lockdep_assert_held(&i915->drm.struct_mutex);
203 GEM_BUG_ON(!i915->gt.active_requests);
Chris Wilson8d761e72019-01-14 14:21:28 +0000204 assert_rpm_wakelock_held(i915);
Chris Wilsone4d20062018-04-06 16:51:44 +0100205
206 if (i915->gt.awake)
207 return;
208
Chris Wilsone4d20062018-04-06 16:51:44 +0100209 /*
210 * It seems that the DMC likes to transition between the DC states a lot
211 * when there are no connected displays (no active power domains) during
212 * command submission.
213 *
214 * This activity has negative impact on the performance of the chip with
215 * huge latencies observed in the interrupt handler and elsewhere.
216 *
217 * Work around it by grabbing a GT IRQ power domain whilst there is any
218 * GT activity, preventing any DC state transitions.
219 */
Chris Wilson8d761e72019-01-14 14:21:28 +0000220 i915->gt.awake = intel_display_power_get(i915, POWER_DOMAIN_GT_IRQ);
221 GEM_BUG_ON(!i915->gt.awake);
Chris Wilsone4d20062018-04-06 16:51:44 +0100222
Chris Wilsone4d20062018-04-06 16:51:44 +0100223 if (unlikely(++i915->gt.epoch == 0)) /* keep 0 as invalid */
224 i915->gt.epoch = 1;
225
226 intel_enable_gt_powersave(i915);
227 i915_update_gfx_val(i915);
228 if (INTEL_GEN(i915) >= 6)
229 gen6_rps_busy(i915);
230 i915_pmu_gt_unparked(i915);
231
232 intel_engines_unpark(i915);
233
234 i915_queue_hangcheck(i915);
235
236 queue_delayed_work(i915->wq,
237 &i915->gt.retire_work,
238 round_jiffies_up_relative(HZ));
239}
240
Eric Anholt673a3942008-07-30 12:06:12 -0700241int
Eric Anholt5a125c32008-10-22 21:40:13 -0700242i915_gem_get_aperture_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +0000243 struct drm_file *file)
Eric Anholt5a125c32008-10-22 21:40:13 -0700244{
Joonas Lahtinen72e96d62016-03-30 16:57:10 +0300245 struct drm_i915_private *dev_priv = to_i915(dev);
Joonas Lahtinen62106b42016-03-18 10:42:57 +0200246 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Joonas Lahtinen72e96d62016-03-30 16:57:10 +0300247 struct drm_i915_gem_get_aperture *args = data;
Tvrtko Ursulinca1543b2015-07-01 11:51:10 +0100248 struct i915_vma *vma;
Weinan Liff8f7972017-05-31 10:35:52 +0800249 u64 pinned;
Eric Anholt5a125c32008-10-22 21:40:13 -0700250
Chris Wilson82ad6442018-06-05 16:37:58 +0100251 pinned = ggtt->vm.reserved;
Chris Wilson73aa8082010-09-30 11:46:12 +0100252 mutex_lock(&dev->struct_mutex);
Chris Wilson82ad6442018-06-05 16:37:58 +0100253 list_for_each_entry(vma, &ggtt->vm.active_list, vm_link)
Chris Wilson20dfbde2016-08-04 16:32:30 +0100254 if (i915_vma_is_pinned(vma))
Tvrtko Ursulinca1543b2015-07-01 11:51:10 +0100255 pinned += vma->node.size;
Chris Wilson82ad6442018-06-05 16:37:58 +0100256 list_for_each_entry(vma, &ggtt->vm.inactive_list, vm_link)
Chris Wilson20dfbde2016-08-04 16:32:30 +0100257 if (i915_vma_is_pinned(vma))
Tvrtko Ursulinca1543b2015-07-01 11:51:10 +0100258 pinned += vma->node.size;
Chris Wilson73aa8082010-09-30 11:46:12 +0100259 mutex_unlock(&dev->struct_mutex);
Eric Anholt5a125c32008-10-22 21:40:13 -0700260
Chris Wilson82ad6442018-06-05 16:37:58 +0100261 args->aper_size = ggtt->vm.total;
Akshay Joshi0206e352011-08-16 15:34:10 -0400262 args->aper_available_size = args->aper_size - pinned;
Chris Wilson6299f992010-11-24 12:23:44 +0000263
Eric Anholt5a125c32008-10-22 21:40:13 -0700264 return 0;
265}
266
Matthew Auldb91b09e2017-10-06 23:18:17 +0100267static int i915_gem_object_get_pages_phys(struct drm_i915_gem_object *obj)
Chris Wilson00731152014-05-21 12:42:56 +0100268{
Al Viro93c76a32015-12-04 23:45:44 -0500269 struct address_space *mapping = obj->base.filp->f_mapping;
Chris Wilsondbb43512016-12-07 13:34:11 +0000270 drm_dma_handle_t *phys;
Chris Wilson6a2c4232014-11-04 04:51:40 -0800271 struct sg_table *st;
272 struct scatterlist *sg;
Chris Wilsondbb43512016-12-07 13:34:11 +0000273 char *vaddr;
Chris Wilson6a2c4232014-11-04 04:51:40 -0800274 int i;
Matthew Auldb91b09e2017-10-06 23:18:17 +0100275 int err;
Chris Wilson00731152014-05-21 12:42:56 +0100276
Chris Wilson6a2c4232014-11-04 04:51:40 -0800277 if (WARN_ON(i915_gem_object_needs_bit17_swizzle(obj)))
Matthew Auldb91b09e2017-10-06 23:18:17 +0100278 return -EINVAL;
Chris Wilson00731152014-05-21 12:42:56 +0100279
Chris Wilsondbb43512016-12-07 13:34:11 +0000280 /* Always aligning to the object size, allows a single allocation
281 * to handle all possible callers, and given typical object sizes,
282 * the alignment of the buddy allocation will naturally match.
283 */
284 phys = drm_pci_alloc(obj->base.dev,
Ville Syrjälä750fae22017-09-07 17:32:03 +0300285 roundup_pow_of_two(obj->base.size),
Chris Wilsondbb43512016-12-07 13:34:11 +0000286 roundup_pow_of_two(obj->base.size));
287 if (!phys)
Matthew Auldb91b09e2017-10-06 23:18:17 +0100288 return -ENOMEM;
Chris Wilsondbb43512016-12-07 13:34:11 +0000289
290 vaddr = phys->vaddr;
Chris Wilson6a2c4232014-11-04 04:51:40 -0800291 for (i = 0; i < obj->base.size / PAGE_SIZE; i++) {
292 struct page *page;
293 char *src;
294
295 page = shmem_read_mapping_page(mapping, i);
Chris Wilsondbb43512016-12-07 13:34:11 +0000296 if (IS_ERR(page)) {
Matthew Auldb91b09e2017-10-06 23:18:17 +0100297 err = PTR_ERR(page);
Chris Wilsondbb43512016-12-07 13:34:11 +0000298 goto err_phys;
299 }
Chris Wilson6a2c4232014-11-04 04:51:40 -0800300
301 src = kmap_atomic(page);
302 memcpy(vaddr, src, PAGE_SIZE);
303 drm_clflush_virt_range(vaddr, PAGE_SIZE);
304 kunmap_atomic(src);
305
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300306 put_page(page);
Chris Wilson6a2c4232014-11-04 04:51:40 -0800307 vaddr += PAGE_SIZE;
308 }
309
Chris Wilsonc0336662016-05-06 15:40:21 +0100310 i915_gem_chipset_flush(to_i915(obj->base.dev));
Chris Wilson6a2c4232014-11-04 04:51:40 -0800311
312 st = kmalloc(sizeof(*st), GFP_KERNEL);
Chris Wilsondbb43512016-12-07 13:34:11 +0000313 if (!st) {
Matthew Auldb91b09e2017-10-06 23:18:17 +0100314 err = -ENOMEM;
Chris Wilsondbb43512016-12-07 13:34:11 +0000315 goto err_phys;
316 }
Chris Wilson6a2c4232014-11-04 04:51:40 -0800317
318 if (sg_alloc_table(st, 1, GFP_KERNEL)) {
319 kfree(st);
Matthew Auldb91b09e2017-10-06 23:18:17 +0100320 err = -ENOMEM;
Chris Wilsondbb43512016-12-07 13:34:11 +0000321 goto err_phys;
Chris Wilson6a2c4232014-11-04 04:51:40 -0800322 }
323
324 sg = st->sgl;
325 sg->offset = 0;
326 sg->length = obj->base.size;
327
Chris Wilsondbb43512016-12-07 13:34:11 +0000328 sg_dma_address(sg) = phys->busaddr;
Chris Wilson6a2c4232014-11-04 04:51:40 -0800329 sg_dma_len(sg) = obj->base.size;
330
Chris Wilsondbb43512016-12-07 13:34:11 +0000331 obj->phys_handle = phys;
Matthew Auldb91b09e2017-10-06 23:18:17 +0100332
Matthew Aulda5c081662017-10-06 23:18:18 +0100333 __i915_gem_object_set_pages(obj, st, sg->length);
Matthew Auldb91b09e2017-10-06 23:18:17 +0100334
335 return 0;
Chris Wilsondbb43512016-12-07 13:34:11 +0000336
337err_phys:
338 drm_pci_free(obj->base.dev, phys);
Matthew Auldb91b09e2017-10-06 23:18:17 +0100339
340 return err;
Chris Wilson6a2c4232014-11-04 04:51:40 -0800341}
342
Chris Wilsone27ab732017-06-15 13:38:49 +0100343static void __start_cpu_write(struct drm_i915_gem_object *obj)
344{
Christian Königc0a51fd2018-02-16 13:43:38 +0100345 obj->read_domains = I915_GEM_DOMAIN_CPU;
346 obj->write_domain = I915_GEM_DOMAIN_CPU;
Chris Wilsone27ab732017-06-15 13:38:49 +0100347 if (cpu_write_needs_clflush(obj))
348 obj->cache_dirty = true;
349}
350
Chris Wilson6a2c4232014-11-04 04:51:40 -0800351static void
Chris Wilson2b3c8312016-11-11 14:58:09 +0000352__i915_gem_object_release_shmem(struct drm_i915_gem_object *obj,
Chris Wilsone5facdf2016-12-23 14:57:57 +0000353 struct sg_table *pages,
354 bool needs_clflush)
Chris Wilson6a2c4232014-11-04 04:51:40 -0800355{
Chris Wilsona4f5ea62016-10-28 13:58:35 +0100356 GEM_BUG_ON(obj->mm.madv == __I915_MADV_PURGED);
Chris Wilson6a2c4232014-11-04 04:51:40 -0800357
Chris Wilsona4f5ea62016-10-28 13:58:35 +0100358 if (obj->mm.madv == I915_MADV_DONTNEED)
359 obj->mm.dirty = false;
Chris Wilson6a2c4232014-11-04 04:51:40 -0800360
Chris Wilsone5facdf2016-12-23 14:57:57 +0000361 if (needs_clflush &&
Christian Königc0a51fd2018-02-16 13:43:38 +0100362 (obj->read_domains & I915_GEM_DOMAIN_CPU) == 0 &&
Chris Wilsonb8f55be2017-08-11 12:11:16 +0100363 !(obj->cache_coherent & I915_BO_CACHE_COHERENT_FOR_READ))
Chris Wilson2b3c8312016-11-11 14:58:09 +0000364 drm_clflush_sg(pages);
Chris Wilson03ac84f2016-10-28 13:58:36 +0100365
Chris Wilsone27ab732017-06-15 13:38:49 +0100366 __start_cpu_write(obj);
Chris Wilson03ac84f2016-10-28 13:58:36 +0100367}
368
369static void
370i915_gem_object_put_pages_phys(struct drm_i915_gem_object *obj,
371 struct sg_table *pages)
372{
Chris Wilsone5facdf2016-12-23 14:57:57 +0000373 __i915_gem_object_release_shmem(obj, pages, false);
Chris Wilson03ac84f2016-10-28 13:58:36 +0100374
Chris Wilsona4f5ea62016-10-28 13:58:35 +0100375 if (obj->mm.dirty) {
Al Viro93c76a32015-12-04 23:45:44 -0500376 struct address_space *mapping = obj->base.filp->f_mapping;
Chris Wilson6a2c4232014-11-04 04:51:40 -0800377 char *vaddr = obj->phys_handle->vaddr;
Chris Wilson00731152014-05-21 12:42:56 +0100378 int i;
379
380 for (i = 0; i < obj->base.size / PAGE_SIZE; i++) {
Chris Wilson6a2c4232014-11-04 04:51:40 -0800381 struct page *page;
382 char *dst;
Chris Wilson00731152014-05-21 12:42:56 +0100383
Chris Wilson6a2c4232014-11-04 04:51:40 -0800384 page = shmem_read_mapping_page(mapping, i);
385 if (IS_ERR(page))
386 continue;
387
388 dst = kmap_atomic(page);
389 drm_clflush_virt_range(vaddr, PAGE_SIZE);
390 memcpy(dst, vaddr, PAGE_SIZE);
391 kunmap_atomic(dst);
392
393 set_page_dirty(page);
Chris Wilsona4f5ea62016-10-28 13:58:35 +0100394 if (obj->mm.madv == I915_MADV_WILLNEED)
Chris Wilson00731152014-05-21 12:42:56 +0100395 mark_page_accessed(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300396 put_page(page);
Chris Wilson00731152014-05-21 12:42:56 +0100397 vaddr += PAGE_SIZE;
398 }
Chris Wilsona4f5ea62016-10-28 13:58:35 +0100399 obj->mm.dirty = false;
Chris Wilson00731152014-05-21 12:42:56 +0100400 }
401
Chris Wilson03ac84f2016-10-28 13:58:36 +0100402 sg_free_table(pages);
403 kfree(pages);
Chris Wilsondbb43512016-12-07 13:34:11 +0000404
405 drm_pci_free(obj->base.dev, obj->phys_handle);
Chris Wilson6a2c4232014-11-04 04:51:40 -0800406}
407
408static void
409i915_gem_object_release_phys(struct drm_i915_gem_object *obj)
410{
Chris Wilsona4f5ea62016-10-28 13:58:35 +0100411 i915_gem_object_unpin_pages(obj);
Chris Wilson6a2c4232014-11-04 04:51:40 -0800412}
413
414static const struct drm_i915_gem_object_ops i915_gem_phys_ops = {
415 .get_pages = i915_gem_object_get_pages_phys,
416 .put_pages = i915_gem_object_put_pages_phys,
417 .release = i915_gem_object_release_phys,
418};
419
Chris Wilson581ab1f2017-02-15 16:39:00 +0000420static const struct drm_i915_gem_object_ops i915_gem_object_ops;
421
Chris Wilson35a96112016-08-14 18:44:40 +0100422int i915_gem_object_unbind(struct drm_i915_gem_object *obj)
Chris Wilsonaa653a62016-08-04 07:52:27 +0100423{
424 struct i915_vma *vma;
425 LIST_HEAD(still_in_list);
Chris Wilson02bef8f2016-08-14 18:44:41 +0100426 int ret;
Chris Wilsonaa653a62016-08-04 07:52:27 +0100427
Chris Wilson02bef8f2016-08-14 18:44:41 +0100428 lockdep_assert_held(&obj->base.dev->struct_mutex);
429
430 /* Closed vma are removed from the obj->vma_list - but they may
431 * still have an active binding on the object. To remove those we
432 * must wait for all rendering to complete to the object (as unbinding
433 * must anyway), and retire the requests.
Chris Wilsonaa653a62016-08-04 07:52:27 +0100434 */
Chris Wilson5888fc92017-12-04 13:25:13 +0000435 ret = i915_gem_object_set_to_cpu_domain(obj, false);
Chris Wilson02bef8f2016-08-14 18:44:41 +0100436 if (ret)
437 return ret;
438
Chris Wilsonaa653a62016-08-04 07:52:27 +0100439 while ((vma = list_first_entry_or_null(&obj->vma_list,
440 struct i915_vma,
441 obj_link))) {
442 list_move_tail(&vma->obj_link, &still_in_list);
443 ret = i915_vma_unbind(vma);
444 if (ret)
445 break;
446 }
447 list_splice(&still_in_list, &obj->vma_list);
448
449 return ret;
450}
451
Chris Wilsone95433c2016-10-28 13:58:27 +0100452static long
453i915_gem_object_wait_fence(struct dma_fence *fence,
454 unsigned int flags,
455 long timeout,
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +0100456 struct intel_rps_client *rps_client)
Chris Wilsone95433c2016-10-28 13:58:27 +0100457{
Chris Wilsone61e0f52018-02-21 09:56:36 +0000458 struct i915_request *rq;
Chris Wilsone95433c2016-10-28 13:58:27 +0100459
460 BUILD_BUG_ON(I915_WAIT_INTERRUPTIBLE != 0x1);
461
462 if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags))
463 return timeout;
464
465 if (!dma_fence_is_i915(fence))
466 return dma_fence_wait_timeout(fence,
467 flags & I915_WAIT_INTERRUPTIBLE,
468 timeout);
469
470 rq = to_request(fence);
Chris Wilsone61e0f52018-02-21 09:56:36 +0000471 if (i915_request_completed(rq))
Chris Wilsone95433c2016-10-28 13:58:27 +0100472 goto out;
473
Chris Wilsone9af4ea2018-01-18 13:16:09 +0000474 /*
475 * This client is about to stall waiting for the GPU. In many cases
Chris Wilsone95433c2016-10-28 13:58:27 +0100476 * this is undesirable and limits the throughput of the system, as
477 * many clients cannot continue processing user input/output whilst
478 * blocked. RPS autotuning may take tens of milliseconds to respond
479 * to the GPU load and thus incurs additional latency for the client.
480 * We can circumvent that by promoting the GPU frequency to maximum
481 * before we wait. This makes the GPU throttle up much more quickly
482 * (good for benchmarks and user experience, e.g. window animations),
483 * but at a cost of spending more power processing the workload
484 * (bad for battery). Not all clients even want their results
485 * immediately and for them we should just let the GPU select its own
486 * frequency to maximise efficiency. To prevent a single client from
487 * forcing the clocks too high for the whole system, we only allow
488 * each client to waitboost once in a busy period.
489 */
Chris Wilsone61e0f52018-02-21 09:56:36 +0000490 if (rps_client && !i915_request_started(rq)) {
Chris Wilsone95433c2016-10-28 13:58:27 +0100491 if (INTEL_GEN(rq->i915) >= 6)
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +0100492 gen6_rps_boost(rq, rps_client);
Chris Wilsone95433c2016-10-28 13:58:27 +0100493 }
494
Chris Wilsone61e0f52018-02-21 09:56:36 +0000495 timeout = i915_request_wait(rq, flags, timeout);
Chris Wilsone95433c2016-10-28 13:58:27 +0100496
497out:
Chris Wilsone61e0f52018-02-21 09:56:36 +0000498 if (flags & I915_WAIT_LOCKED && i915_request_completed(rq))
499 i915_request_retire_upto(rq);
Chris Wilsone95433c2016-10-28 13:58:27 +0100500
Chris Wilsone95433c2016-10-28 13:58:27 +0100501 return timeout;
502}
503
504static long
505i915_gem_object_wait_reservation(struct reservation_object *resv,
506 unsigned int flags,
507 long timeout,
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +0100508 struct intel_rps_client *rps_client)
Chris Wilsone95433c2016-10-28 13:58:27 +0100509{
Chris Wilsone54ca972017-02-17 15:13:04 +0000510 unsigned int seq = __read_seqcount_begin(&resv->seq);
Chris Wilsone95433c2016-10-28 13:58:27 +0100511 struct dma_fence *excl;
Chris Wilsone54ca972017-02-17 15:13:04 +0000512 bool prune_fences = false;
Chris Wilsone95433c2016-10-28 13:58:27 +0100513
514 if (flags & I915_WAIT_ALL) {
515 struct dma_fence **shared;
516 unsigned int count, i;
517 int ret;
518
519 ret = reservation_object_get_fences_rcu(resv,
520 &excl, &count, &shared);
521 if (ret)
522 return ret;
523
524 for (i = 0; i < count; i++) {
525 timeout = i915_gem_object_wait_fence(shared[i],
526 flags, timeout,
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +0100527 rps_client);
Chris Wilsond892e932017-02-12 21:53:43 +0000528 if (timeout < 0)
Chris Wilsone95433c2016-10-28 13:58:27 +0100529 break;
530
531 dma_fence_put(shared[i]);
532 }
533
534 for (; i < count; i++)
535 dma_fence_put(shared[i]);
536 kfree(shared);
Chris Wilsone54ca972017-02-17 15:13:04 +0000537
Chris Wilsonfa730552018-03-07 17:13:03 +0000538 /*
539 * If both shared fences and an exclusive fence exist,
540 * then by construction the shared fences must be later
541 * than the exclusive fence. If we successfully wait for
542 * all the shared fences, we know that the exclusive fence
543 * must all be signaled. If all the shared fences are
544 * signaled, we can prune the array and recover the
545 * floating references on the fences/requests.
546 */
Chris Wilsone54ca972017-02-17 15:13:04 +0000547 prune_fences = count && timeout >= 0;
Chris Wilsone95433c2016-10-28 13:58:27 +0100548 } else {
549 excl = reservation_object_get_excl_rcu(resv);
550 }
551
Chris Wilsonfa730552018-03-07 17:13:03 +0000552 if (excl && timeout >= 0)
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +0100553 timeout = i915_gem_object_wait_fence(excl, flags, timeout,
554 rps_client);
Chris Wilsone95433c2016-10-28 13:58:27 +0100555
556 dma_fence_put(excl);
557
Chris Wilsonfa730552018-03-07 17:13:03 +0000558 /*
559 * Opportunistically prune the fences iff we know they have *all* been
Chris Wilson03d1cac2017-03-08 13:26:28 +0000560 * signaled and that the reservation object has not been changed (i.e.
561 * no new fences have been added).
562 */
Chris Wilsone54ca972017-02-17 15:13:04 +0000563 if (prune_fences && !__read_seqcount_retry(&resv->seq, seq)) {
Chris Wilson03d1cac2017-03-08 13:26:28 +0000564 if (reservation_object_trylock(resv)) {
565 if (!__read_seqcount_retry(&resv->seq, seq))
566 reservation_object_add_excl_fence(resv, NULL);
567 reservation_object_unlock(resv);
568 }
Chris Wilsone54ca972017-02-17 15:13:04 +0000569 }
570
Chris Wilsone95433c2016-10-28 13:58:27 +0100571 return timeout;
572}
573
Chris Wilsonb7268c52018-04-18 19:40:52 +0100574static void __fence_set_priority(struct dma_fence *fence,
575 const struct i915_sched_attr *attr)
Chris Wilson6b5e90f2016-11-14 20:41:05 +0000576{
Chris Wilsone61e0f52018-02-21 09:56:36 +0000577 struct i915_request *rq;
Chris Wilson6b5e90f2016-11-14 20:41:05 +0000578 struct intel_engine_cs *engine;
579
Chris Wilsonc218ee02018-01-06 10:56:18 +0000580 if (dma_fence_is_signaled(fence) || !dma_fence_is_i915(fence))
Chris Wilson6b5e90f2016-11-14 20:41:05 +0000581 return;
582
583 rq = to_request(fence);
584 engine = rq->engine;
Chris Wilson6b5e90f2016-11-14 20:41:05 +0000585
Chris Wilson4f6d8fc2018-05-07 14:57:25 +0100586 local_bh_disable();
587 rcu_read_lock(); /* RCU serialisation for set-wedged protection */
Chris Wilson47650db2018-03-07 13:42:25 +0000588 if (engine->schedule)
Chris Wilsonb7268c52018-04-18 19:40:52 +0100589 engine->schedule(rq, attr);
Chris Wilson47650db2018-03-07 13:42:25 +0000590 rcu_read_unlock();
Chris Wilson4f6d8fc2018-05-07 14:57:25 +0100591 local_bh_enable(); /* kick the tasklets if queues were reprioritised */
Chris Wilson6b5e90f2016-11-14 20:41:05 +0000592}
593
Chris Wilsonb7268c52018-04-18 19:40:52 +0100594static void fence_set_priority(struct dma_fence *fence,
595 const struct i915_sched_attr *attr)
Chris Wilson6b5e90f2016-11-14 20:41:05 +0000596{
597 /* Recurse once into a fence-array */
598 if (dma_fence_is_array(fence)) {
599 struct dma_fence_array *array = to_dma_fence_array(fence);
600 int i;
601
602 for (i = 0; i < array->num_fences; i++)
Chris Wilsonb7268c52018-04-18 19:40:52 +0100603 __fence_set_priority(array->fences[i], attr);
Chris Wilson6b5e90f2016-11-14 20:41:05 +0000604 } else {
Chris Wilsonb7268c52018-04-18 19:40:52 +0100605 __fence_set_priority(fence, attr);
Chris Wilson6b5e90f2016-11-14 20:41:05 +0000606 }
607}
608
609int
610i915_gem_object_wait_priority(struct drm_i915_gem_object *obj,
611 unsigned int flags,
Chris Wilsonb7268c52018-04-18 19:40:52 +0100612 const struct i915_sched_attr *attr)
Chris Wilson6b5e90f2016-11-14 20:41:05 +0000613{
614 struct dma_fence *excl;
615
616 if (flags & I915_WAIT_ALL) {
617 struct dma_fence **shared;
618 unsigned int count, i;
619 int ret;
620
621 ret = reservation_object_get_fences_rcu(obj->resv,
622 &excl, &count, &shared);
623 if (ret)
624 return ret;
625
626 for (i = 0; i < count; i++) {
Chris Wilsonb7268c52018-04-18 19:40:52 +0100627 fence_set_priority(shared[i], attr);
Chris Wilson6b5e90f2016-11-14 20:41:05 +0000628 dma_fence_put(shared[i]);
629 }
630
631 kfree(shared);
632 } else {
633 excl = reservation_object_get_excl_rcu(obj->resv);
634 }
635
636 if (excl) {
Chris Wilsonb7268c52018-04-18 19:40:52 +0100637 fence_set_priority(excl, attr);
Chris Wilson6b5e90f2016-11-14 20:41:05 +0000638 dma_fence_put(excl);
639 }
640 return 0;
641}
642
Chris Wilson00e60f22016-08-04 16:32:40 +0100643/**
Chris Wilsone95433c2016-10-28 13:58:27 +0100644 * Waits for rendering to the object to be completed
Chris Wilson00e60f22016-08-04 16:32:40 +0100645 * @obj: i915 gem object
Chris Wilsone95433c2016-10-28 13:58:27 +0100646 * @flags: how to wait (under a lock, for all rendering or just for writes etc)
647 * @timeout: how long to wait
Chris Wilsona0a8b1c2017-11-09 14:06:44 +0000648 * @rps_client: client (user process) to charge for any waitboosting
Chris Wilson00e60f22016-08-04 16:32:40 +0100649 */
650int
Chris Wilsone95433c2016-10-28 13:58:27 +0100651i915_gem_object_wait(struct drm_i915_gem_object *obj,
652 unsigned int flags,
653 long timeout,
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +0100654 struct intel_rps_client *rps_client)
Chris Wilson00e60f22016-08-04 16:32:40 +0100655{
Chris Wilsone95433c2016-10-28 13:58:27 +0100656 might_sleep();
657#if IS_ENABLED(CONFIG_LOCKDEP)
658 GEM_BUG_ON(debug_locks &&
659 !!lockdep_is_held(&obj->base.dev->struct_mutex) !=
660 !!(flags & I915_WAIT_LOCKED));
661#endif
662 GEM_BUG_ON(timeout < 0);
Chris Wilson00e60f22016-08-04 16:32:40 +0100663
Chris Wilsond07f0e52016-10-28 13:58:44 +0100664 timeout = i915_gem_object_wait_reservation(obj->resv,
665 flags, timeout,
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +0100666 rps_client);
Chris Wilsone95433c2016-10-28 13:58:27 +0100667 return timeout < 0 ? timeout : 0;
Chris Wilson00e60f22016-08-04 16:32:40 +0100668}
669
670static struct intel_rps_client *to_rps_client(struct drm_file *file)
671{
672 struct drm_i915_file_private *fpriv = file->driver_priv;
673
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +0100674 return &fpriv->rps_client;
Chris Wilson00e60f22016-08-04 16:32:40 +0100675}
676
Chris Wilson00731152014-05-21 12:42:56 +0100677static int
678i915_gem_phys_pwrite(struct drm_i915_gem_object *obj,
679 struct drm_i915_gem_pwrite *args,
Chris Wilson03ac84f2016-10-28 13:58:36 +0100680 struct drm_file *file)
Chris Wilson00731152014-05-21 12:42:56 +0100681{
Chris Wilson00731152014-05-21 12:42:56 +0100682 void *vaddr = obj->phys_handle->vaddr + args->offset;
Gustavo Padovan3ed605b2016-04-26 12:32:27 -0300683 char __user *user_data = u64_to_user_ptr(args->data_ptr);
Chris Wilson6a2c4232014-11-04 04:51:40 -0800684
685 /* We manually control the domain here and pretend that it
686 * remains coherent i.e. in the GTT domain, like shmem_pwrite.
687 */
Rodrigo Vivi77a0d1c2015-06-18 11:43:24 -0700688 intel_fb_obj_invalidate(obj, ORIGIN_CPU);
Chris Wilson10466d22017-01-06 15:22:38 +0000689 if (copy_from_user(vaddr, user_data, args->size))
690 return -EFAULT;
Chris Wilson00731152014-05-21 12:42:56 +0100691
Chris Wilson6a2c4232014-11-04 04:51:40 -0800692 drm_clflush_virt_range(vaddr, args->size);
Chris Wilson10466d22017-01-06 15:22:38 +0000693 i915_gem_chipset_flush(to_i915(obj->base.dev));
Paulo Zanoni063e4e62015-02-13 17:23:45 -0200694
Chris Wilsond59b21e2017-02-22 11:40:49 +0000695 intel_fb_obj_flush(obj, ORIGIN_CPU);
Chris Wilson10466d22017-01-06 15:22:38 +0000696 return 0;
Chris Wilson00731152014-05-21 12:42:56 +0100697}
698
Tvrtko Ursulin187685c2016-12-01 14:16:36 +0000699void *i915_gem_object_alloc(struct drm_i915_private *dev_priv)
Chris Wilson42dcedd2012-11-15 11:32:30 +0000700{
Chris Wilsonefab6d82015-04-07 16:20:57 +0100701 return kmem_cache_zalloc(dev_priv->objects, GFP_KERNEL);
Chris Wilson42dcedd2012-11-15 11:32:30 +0000702}
703
704void i915_gem_object_free(struct drm_i915_gem_object *obj)
705{
Chris Wilsonfac5e232016-07-04 11:34:36 +0100706 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
Chris Wilsonefab6d82015-04-07 16:20:57 +0100707 kmem_cache_free(dev_priv->objects, obj);
Chris Wilson42dcedd2012-11-15 11:32:30 +0000708}
709
Dave Airlieff72145b2011-02-07 12:16:14 +1000710static int
711i915_gem_create(struct drm_file *file,
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +0000712 struct drm_i915_private *dev_priv,
Dave Airlieff72145b2011-02-07 12:16:14 +1000713 uint64_t size,
714 uint32_t *handle_p)
Eric Anholt673a3942008-07-30 12:06:12 -0700715{
Chris Wilson05394f32010-11-08 19:18:58 +0000716 struct drm_i915_gem_object *obj;
Pekka Paalanena1a2d1d2009-08-23 12:40:55 +0300717 int ret;
718 u32 handle;
Eric Anholt673a3942008-07-30 12:06:12 -0700719
Dave Airlieff72145b2011-02-07 12:16:14 +1000720 size = roundup(size, PAGE_SIZE);
Chris Wilson8ffc0242011-09-14 14:14:28 +0200721 if (size == 0)
722 return -EINVAL;
Eric Anholt673a3942008-07-30 12:06:12 -0700723
724 /* Allocate the new object */
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +0000725 obj = i915_gem_object_create(dev_priv, size);
Chris Wilsonfe3db792016-04-25 13:32:13 +0100726 if (IS_ERR(obj))
727 return PTR_ERR(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700728
Chris Wilson05394f32010-11-08 19:18:58 +0000729 ret = drm_gem_handle_create(file, &obj->base, &handle);
Chris Wilson202f2fe2010-10-14 13:20:40 +0100730 /* drop reference from allocate - handle holds it now */
Chris Wilsonf0cd5182016-10-28 13:58:43 +0100731 i915_gem_object_put(obj);
Daniel Vetterd861e332013-07-24 23:25:03 +0200732 if (ret)
733 return ret;
Chris Wilson202f2fe2010-10-14 13:20:40 +0100734
Dave Airlieff72145b2011-02-07 12:16:14 +1000735 *handle_p = handle;
Eric Anholt673a3942008-07-30 12:06:12 -0700736 return 0;
737}
738
Dave Airlieff72145b2011-02-07 12:16:14 +1000739int
740i915_gem_dumb_create(struct drm_file *file,
741 struct drm_device *dev,
742 struct drm_mode_create_dumb *args)
743{
744 /* have to work out size/pitch and return them */
Paulo Zanonide45eaf2013-10-18 18:48:24 -0300745 args->pitch = ALIGN(args->width * DIV_ROUND_UP(args->bpp, 8), 64);
Dave Airlieff72145b2011-02-07 12:16:14 +1000746 args->size = args->pitch * args->height;
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +0000747 return i915_gem_create(file, to_i915(dev),
Dave Airlieda6b51d2014-12-24 13:11:17 +1000748 args->size, &args->handle);
Dave Airlieff72145b2011-02-07 12:16:14 +1000749}
750
Chris Wilsone27ab732017-06-15 13:38:49 +0100751static bool gpu_write_needs_clflush(struct drm_i915_gem_object *obj)
752{
753 return !(obj->cache_level == I915_CACHE_NONE ||
754 obj->cache_level == I915_CACHE_WT);
755}
756
Dave Airlieff72145b2011-02-07 12:16:14 +1000757/**
758 * Creates a new mm object and returns a handle to it.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +0100759 * @dev: drm device pointer
760 * @data: ioctl data blob
761 * @file: drm file pointer
Dave Airlieff72145b2011-02-07 12:16:14 +1000762 */
763int
764i915_gem_create_ioctl(struct drm_device *dev, void *data,
765 struct drm_file *file)
766{
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +0000767 struct drm_i915_private *dev_priv = to_i915(dev);
Dave Airlieff72145b2011-02-07 12:16:14 +1000768 struct drm_i915_gem_create *args = data;
Daniel Vetter63ed2cb2012-04-23 16:50:50 +0200769
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +0000770 i915_gem_flush_free_objects(dev_priv);
Chris Wilsonfbbd37b2016-10-28 13:58:42 +0100771
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +0000772 return i915_gem_create(file, dev_priv,
Dave Airlieda6b51d2014-12-24 13:11:17 +1000773 args->size, &args->handle);
Dave Airlieff72145b2011-02-07 12:16:14 +1000774}
775
Chris Wilsonef749212017-04-12 12:01:10 +0100776static inline enum fb_op_origin
777fb_write_origin(struct drm_i915_gem_object *obj, unsigned int domain)
778{
779 return (domain == I915_GEM_DOMAIN_GTT ?
780 obj->frontbuffer_ggtt_origin : ORIGIN_CPU);
781}
782
Chris Wilson7125397b2017-12-06 12:49:14 +0000783void i915_gem_flush_ggtt_writes(struct drm_i915_private *dev_priv)
Chris Wilsonef749212017-04-12 12:01:10 +0100784{
Chris Wilson538ef962019-01-14 14:21:18 +0000785 intel_wakeref_t wakeref;
786
Chris Wilson7125397b2017-12-06 12:49:14 +0000787 /*
788 * No actual flushing is required for the GTT write domain for reads
789 * from the GTT domain. Writes to it "immediately" go to main memory
790 * as far as we know, so there's no chipset flush. It also doesn't
791 * land in the GPU render cache.
Chris Wilsonef749212017-04-12 12:01:10 +0100792 *
793 * However, we do have to enforce the order so that all writes through
794 * the GTT land before any writes to the device, such as updates to
795 * the GATT itself.
796 *
797 * We also have to wait a bit for the writes to land from the GTT.
798 * An uncached read (i.e. mmio) seems to be ideal for the round-trip
799 * timing. This issue has only been observed when switching quickly
800 * between GTT writes and CPU reads from inside the kernel on recent hw,
801 * and it appears to only affect discrete GTT blocks (i.e. on LLC
Chris Wilson7125397b2017-12-06 12:49:14 +0000802 * system agents we cannot reproduce this behaviour, until Cannonlake
803 * that was!).
Chris Wilsonef749212017-04-12 12:01:10 +0100804 */
Chris Wilson7125397b2017-12-06 12:49:14 +0000805
Chris Wilson900ccf32018-07-20 11:19:10 +0100806 wmb();
807
808 if (INTEL_INFO(dev_priv)->has_coherent_ggtt)
809 return;
810
Chris Wilsona8bd3b82018-07-17 10:26:55 +0100811 i915_gem_chipset_flush(dev_priv);
Chris Wilsonef749212017-04-12 12:01:10 +0100812
Chris Wilsond4225a52019-01-14 14:21:23 +0000813 with_intel_runtime_pm(dev_priv, wakeref) {
814 spin_lock_irq(&dev_priv->uncore.lock);
Chris Wilson7125397b2017-12-06 12:49:14 +0000815
Chris Wilsond4225a52019-01-14 14:21:23 +0000816 POSTING_READ_FW(RING_HEAD(RENDER_RING_BASE));
Chris Wilson7125397b2017-12-06 12:49:14 +0000817
Chris Wilsond4225a52019-01-14 14:21:23 +0000818 spin_unlock_irq(&dev_priv->uncore.lock);
819 }
Chris Wilson7125397b2017-12-06 12:49:14 +0000820}
821
822static void
823flush_write_domain(struct drm_i915_gem_object *obj, unsigned int flush_domains)
824{
825 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
826 struct i915_vma *vma;
827
Christian Königc0a51fd2018-02-16 13:43:38 +0100828 if (!(obj->write_domain & flush_domains))
Chris Wilson7125397b2017-12-06 12:49:14 +0000829 return;
830
Christian Königc0a51fd2018-02-16 13:43:38 +0100831 switch (obj->write_domain) {
Chris Wilsonef749212017-04-12 12:01:10 +0100832 case I915_GEM_DOMAIN_GTT:
Chris Wilson7125397b2017-12-06 12:49:14 +0000833 i915_gem_flush_ggtt_writes(dev_priv);
Chris Wilsonef749212017-04-12 12:01:10 +0100834
835 intel_fb_obj_flush(obj,
836 fb_write_origin(obj, I915_GEM_DOMAIN_GTT));
Chris Wilson7125397b2017-12-06 12:49:14 +0000837
Chris Wilsone2189dd2017-12-07 21:14:07 +0000838 for_each_ggtt_vma(vma, obj) {
Chris Wilson7125397b2017-12-06 12:49:14 +0000839 if (vma->iomap)
840 continue;
841
842 i915_vma_unset_ggtt_write(vma);
843 }
Chris Wilsonef749212017-04-12 12:01:10 +0100844 break;
845
Chris Wilsonadd00e62018-07-06 12:54:02 +0100846 case I915_GEM_DOMAIN_WC:
847 wmb();
848 break;
849
Chris Wilsonef749212017-04-12 12:01:10 +0100850 case I915_GEM_DOMAIN_CPU:
851 i915_gem_clflush_object(obj, I915_CLFLUSH_SYNC);
852 break;
Chris Wilsone27ab732017-06-15 13:38:49 +0100853
854 case I915_GEM_DOMAIN_RENDER:
855 if (gpu_write_needs_clflush(obj))
856 obj->cache_dirty = true;
857 break;
Chris Wilsonef749212017-04-12 12:01:10 +0100858 }
859
Christian Königc0a51fd2018-02-16 13:43:38 +0100860 obj->write_domain = 0;
Chris Wilsonef749212017-04-12 12:01:10 +0100861}
862
Brad Volkin4c914c02014-02-18 10:15:45 -0800863/*
864 * Pins the specified object's pages and synchronizes the object with
865 * GPU accesses. Sets needs_clflush to non-zero if the caller should
866 * flush the object from the CPU cache.
867 */
868int i915_gem_obj_prepare_shmem_read(struct drm_i915_gem_object *obj,
Chris Wilson43394c72016-08-18 17:16:47 +0100869 unsigned int *needs_clflush)
Brad Volkin4c914c02014-02-18 10:15:45 -0800870{
871 int ret;
872
Chris Wilsone95433c2016-10-28 13:58:27 +0100873 lockdep_assert_held(&obj->base.dev->struct_mutex);
Brad Volkin4c914c02014-02-18 10:15:45 -0800874
Chris Wilsone95433c2016-10-28 13:58:27 +0100875 *needs_clflush = 0;
Chris Wilson43394c72016-08-18 17:16:47 +0100876 if (!i915_gem_object_has_struct_page(obj))
877 return -ENODEV;
Brad Volkin4c914c02014-02-18 10:15:45 -0800878
Chris Wilsone95433c2016-10-28 13:58:27 +0100879 ret = i915_gem_object_wait(obj,
880 I915_WAIT_INTERRUPTIBLE |
881 I915_WAIT_LOCKED,
882 MAX_SCHEDULE_TIMEOUT,
883 NULL);
Chris Wilsonc13d87e2016-07-20 09:21:15 +0100884 if (ret)
885 return ret;
886
Chris Wilsona4f5ea62016-10-28 13:58:35 +0100887 ret = i915_gem_object_pin_pages(obj);
Chris Wilson97649512016-08-18 17:16:50 +0100888 if (ret)
889 return ret;
890
Chris Wilsonb8f55be2017-08-11 12:11:16 +0100891 if (obj->cache_coherent & I915_BO_CACHE_COHERENT_FOR_READ ||
892 !static_cpu_has(X86_FEATURE_CLFLUSH)) {
Chris Wilson7f5f95d2017-03-10 00:09:42 +0000893 ret = i915_gem_object_set_to_cpu_domain(obj, false);
894 if (ret)
895 goto err_unpin;
896 else
897 goto out;
898 }
899
Chris Wilsonef749212017-04-12 12:01:10 +0100900 flush_write_domain(obj, ~I915_GEM_DOMAIN_CPU);
Chris Wilsona314d5c2016-08-18 17:16:48 +0100901
Chris Wilson43394c72016-08-18 17:16:47 +0100902 /* If we're not in the cpu read domain, set ourself into the gtt
903 * read domain and manually flush cachelines (if required). This
904 * optimizes for the case when the gpu will dirty the data
905 * anyway again before the next pread happens.
906 */
Chris Wilsone27ab732017-06-15 13:38:49 +0100907 if (!obj->cache_dirty &&
Christian Königc0a51fd2018-02-16 13:43:38 +0100908 !(obj->read_domains & I915_GEM_DOMAIN_CPU))
Chris Wilson7f5f95d2017-03-10 00:09:42 +0000909 *needs_clflush = CLFLUSH_BEFORE;
Brad Volkin4c914c02014-02-18 10:15:45 -0800910
Chris Wilson7f5f95d2017-03-10 00:09:42 +0000911out:
Chris Wilson97649512016-08-18 17:16:50 +0100912 /* return with the pages pinned */
Chris Wilson43394c72016-08-18 17:16:47 +0100913 return 0;
Chris Wilson97649512016-08-18 17:16:50 +0100914
915err_unpin:
916 i915_gem_object_unpin_pages(obj);
917 return ret;
Chris Wilson43394c72016-08-18 17:16:47 +0100918}
919
920int i915_gem_obj_prepare_shmem_write(struct drm_i915_gem_object *obj,
921 unsigned int *needs_clflush)
922{
923 int ret;
924
Chris Wilsone95433c2016-10-28 13:58:27 +0100925 lockdep_assert_held(&obj->base.dev->struct_mutex);
926
Chris Wilson43394c72016-08-18 17:16:47 +0100927 *needs_clflush = 0;
928 if (!i915_gem_object_has_struct_page(obj))
929 return -ENODEV;
930
Chris Wilsone95433c2016-10-28 13:58:27 +0100931 ret = i915_gem_object_wait(obj,
932 I915_WAIT_INTERRUPTIBLE |
933 I915_WAIT_LOCKED |
934 I915_WAIT_ALL,
935 MAX_SCHEDULE_TIMEOUT,
936 NULL);
Chris Wilson43394c72016-08-18 17:16:47 +0100937 if (ret)
938 return ret;
939
Chris Wilsona4f5ea62016-10-28 13:58:35 +0100940 ret = i915_gem_object_pin_pages(obj);
Chris Wilson97649512016-08-18 17:16:50 +0100941 if (ret)
942 return ret;
943
Chris Wilsonb8f55be2017-08-11 12:11:16 +0100944 if (obj->cache_coherent & I915_BO_CACHE_COHERENT_FOR_WRITE ||
945 !static_cpu_has(X86_FEATURE_CLFLUSH)) {
Chris Wilson7f5f95d2017-03-10 00:09:42 +0000946 ret = i915_gem_object_set_to_cpu_domain(obj, true);
947 if (ret)
948 goto err_unpin;
949 else
950 goto out;
951 }
952
Chris Wilsonef749212017-04-12 12:01:10 +0100953 flush_write_domain(obj, ~I915_GEM_DOMAIN_CPU);
Chris Wilsona314d5c2016-08-18 17:16:48 +0100954
Chris Wilson43394c72016-08-18 17:16:47 +0100955 /* If we're not in the cpu write domain, set ourself into the
956 * gtt write domain and manually flush cachelines (as required).
957 * This optimizes for the case when the gpu will use the data
958 * right away and we therefore have to clflush anyway.
959 */
Chris Wilsone27ab732017-06-15 13:38:49 +0100960 if (!obj->cache_dirty) {
Chris Wilson7f5f95d2017-03-10 00:09:42 +0000961 *needs_clflush |= CLFLUSH_AFTER;
Chris Wilson43394c72016-08-18 17:16:47 +0100962
Chris Wilsone27ab732017-06-15 13:38:49 +0100963 /*
964 * Same trick applies to invalidate partially written
965 * cachelines read before writing.
966 */
Christian Königc0a51fd2018-02-16 13:43:38 +0100967 if (!(obj->read_domains & I915_GEM_DOMAIN_CPU))
Chris Wilsone27ab732017-06-15 13:38:49 +0100968 *needs_clflush |= CLFLUSH_BEFORE;
969 }
Chris Wilson43394c72016-08-18 17:16:47 +0100970
Chris Wilson7f5f95d2017-03-10 00:09:42 +0000971out:
Chris Wilson43394c72016-08-18 17:16:47 +0100972 intel_fb_obj_invalidate(obj, ORIGIN_CPU);
Chris Wilsona4f5ea62016-10-28 13:58:35 +0100973 obj->mm.dirty = true;
Chris Wilson97649512016-08-18 17:16:50 +0100974 /* return with the pages pinned */
Chris Wilson43394c72016-08-18 17:16:47 +0100975 return 0;
Chris Wilson97649512016-08-18 17:16:50 +0100976
977err_unpin:
978 i915_gem_object_unpin_pages(obj);
979 return ret;
Brad Volkin4c914c02014-02-18 10:15:45 -0800980}
981
Daniel Vetterd174bd62012-03-25 19:47:40 +0200982static int
Chris Wilsonb9d126e2019-01-05 12:07:58 +0000983shmem_pread(struct page *page, int offset, int len, char __user *user_data,
984 bool needs_clflush)
Daniel Vetterd174bd62012-03-25 19:47:40 +0200985{
986 char *vaddr;
987 int ret;
988
989 vaddr = kmap(page);
Daniel Vetterd174bd62012-03-25 19:47:40 +0200990
Chris Wilsonb9d126e2019-01-05 12:07:58 +0000991 if (needs_clflush)
992 drm_clflush_virt_range(vaddr + offset, len);
993
994 ret = __copy_to_user(user_data, vaddr + offset, len);
995
Daniel Vetterd174bd62012-03-25 19:47:40 +0200996 kunmap(page);
997
Chris Wilsonb9d126e2019-01-05 12:07:58 +0000998 return ret ? -EFAULT : 0;
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100999}
1000
1001static int
1002i915_gem_shmem_pread(struct drm_i915_gem_object *obj,
1003 struct drm_i915_gem_pread *args)
1004{
1005 char __user *user_data;
1006 u64 remain;
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001007 unsigned int needs_clflush;
1008 unsigned int idx, offset;
1009 int ret;
1010
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001011 ret = mutex_lock_interruptible(&obj->base.dev->struct_mutex);
1012 if (ret)
1013 return ret;
1014
1015 ret = i915_gem_obj_prepare_shmem_read(obj, &needs_clflush);
1016 mutex_unlock(&obj->base.dev->struct_mutex);
1017 if (ret)
1018 return ret;
1019
1020 remain = args->size;
1021 user_data = u64_to_user_ptr(args->data_ptr);
1022 offset = offset_in_page(args->offset);
1023 for (idx = args->offset >> PAGE_SHIFT; remain; idx++) {
1024 struct page *page = i915_gem_object_get_page(obj, idx);
Chris Wilsona5e856a52018-10-12 15:02:28 +01001025 unsigned int length = min_t(u64, remain, PAGE_SIZE - offset);
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001026
1027 ret = shmem_pread(page, offset, length, user_data,
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001028 needs_clflush);
1029 if (ret)
1030 break;
1031
1032 remain -= length;
1033 user_data += length;
1034 offset = 0;
1035 }
1036
1037 i915_gem_obj_finish_shmem_access(obj);
1038 return ret;
1039}
1040
1041static inline bool
1042gtt_user_read(struct io_mapping *mapping,
1043 loff_t base, int offset,
1044 char __user *user_data, int length)
1045{
Ville Syrjäläafe722b2017-09-01 20:12:52 +03001046 void __iomem *vaddr;
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001047 unsigned long unwritten;
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301048
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301049 /* We can use the cpu mem copy function because this is X86. */
Ville Syrjäläafe722b2017-09-01 20:12:52 +03001050 vaddr = io_mapping_map_atomic_wc(mapping, base);
1051 unwritten = __copy_to_user_inatomic(user_data,
1052 (void __force *)vaddr + offset,
1053 length);
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001054 io_mapping_unmap_atomic(vaddr);
1055 if (unwritten) {
Ville Syrjäläafe722b2017-09-01 20:12:52 +03001056 vaddr = io_mapping_map_wc(mapping, base, PAGE_SIZE);
1057 unwritten = copy_to_user(user_data,
1058 (void __force *)vaddr + offset,
1059 length);
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001060 io_mapping_unmap(vaddr);
1061 }
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301062 return unwritten;
1063}
1064
1065static int
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001066i915_gem_gtt_pread(struct drm_i915_gem_object *obj,
1067 const struct drm_i915_gem_pread *args)
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301068{
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001069 struct drm_i915_private *i915 = to_i915(obj->base.dev);
1070 struct i915_ggtt *ggtt = &i915->ggtt;
Chris Wilson538ef962019-01-14 14:21:18 +00001071 intel_wakeref_t wakeref;
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301072 struct drm_mm_node node;
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001073 struct i915_vma *vma;
1074 void __user *user_data;
1075 u64 remain, offset;
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301076 int ret;
1077
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001078 ret = mutex_lock_interruptible(&i915->drm.struct_mutex);
1079 if (ret)
1080 return ret;
1081
Chris Wilson538ef962019-01-14 14:21:18 +00001082 wakeref = intel_runtime_pm_get(i915);
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001083 vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0,
Chris Wilsona3259ca2017-10-09 09:44:00 +01001084 PIN_MAPPABLE |
1085 PIN_NONFAULT |
1086 PIN_NONBLOCK);
Chris Wilson18034582016-08-18 17:16:45 +01001087 if (!IS_ERR(vma)) {
1088 node.start = i915_ggtt_offset(vma);
1089 node.allocated = false;
Chris Wilson49ef5292016-08-18 17:17:00 +01001090 ret = i915_vma_put_fence(vma);
Chris Wilson18034582016-08-18 17:16:45 +01001091 if (ret) {
1092 i915_vma_unpin(vma);
1093 vma = ERR_PTR(ret);
1094 }
1095 }
Chris Wilson058d88c2016-08-15 10:49:06 +01001096 if (IS_ERR(vma)) {
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001097 ret = insert_mappable_node(ggtt, &node, PAGE_SIZE);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301098 if (ret)
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001099 goto out_unlock;
1100 GEM_BUG_ON(!node.allocated);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301101 }
1102
1103 ret = i915_gem_object_set_to_gtt_domain(obj, false);
1104 if (ret)
1105 goto out_unpin;
1106
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001107 mutex_unlock(&i915->drm.struct_mutex);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301108
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001109 user_data = u64_to_user_ptr(args->data_ptr);
1110 remain = args->size;
1111 offset = args->offset;
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301112
1113 while (remain > 0) {
1114 /* Operation in this page
1115 *
1116 * page_base = page offset within aperture
1117 * page_offset = offset within page
1118 * page_length = bytes to copy for this page
1119 */
1120 u32 page_base = node.start;
1121 unsigned page_offset = offset_in_page(offset);
1122 unsigned page_length = PAGE_SIZE - page_offset;
1123 page_length = remain < page_length ? remain : page_length;
1124 if (node.allocated) {
1125 wmb();
Chris Wilson82ad6442018-06-05 16:37:58 +01001126 ggtt->vm.insert_page(&ggtt->vm,
1127 i915_gem_object_get_dma_address(obj, offset >> PAGE_SHIFT),
1128 node.start, I915_CACHE_NONE, 0);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301129 wmb();
1130 } else {
1131 page_base += offset & PAGE_MASK;
1132 }
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001133
Matthew Auld73ebd502017-12-11 15:18:20 +00001134 if (gtt_user_read(&ggtt->iomap, page_base, page_offset,
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001135 user_data, page_length)) {
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301136 ret = -EFAULT;
1137 break;
1138 }
1139
1140 remain -= page_length;
1141 user_data += page_length;
1142 offset += page_length;
1143 }
1144
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001145 mutex_lock(&i915->drm.struct_mutex);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301146out_unpin:
1147 if (node.allocated) {
1148 wmb();
Chris Wilson82ad6442018-06-05 16:37:58 +01001149 ggtt->vm.clear_range(&ggtt->vm, node.start, node.size);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301150 remove_mappable_node(&node);
1151 } else {
Chris Wilson058d88c2016-08-15 10:49:06 +01001152 i915_vma_unpin(vma);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301153 }
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001154out_unlock:
Chris Wilson538ef962019-01-14 14:21:18 +00001155 intel_runtime_pm_put(i915, wakeref);
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001156 mutex_unlock(&i915->drm.struct_mutex);
Chris Wilsonf60d7f02012-09-04 21:02:56 +01001157
Eric Anholteb014592009-03-10 11:44:52 -07001158 return ret;
1159}
1160
Eric Anholt673a3942008-07-30 12:06:12 -07001161/**
1162 * Reads data from the object referenced by handle.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01001163 * @dev: drm device pointer
1164 * @data: ioctl data blob
1165 * @file: drm file pointer
Eric Anholt673a3942008-07-30 12:06:12 -07001166 *
1167 * On error, the contents of *data are undefined.
1168 */
1169int
1170i915_gem_pread_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00001171 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001172{
1173 struct drm_i915_gem_pread *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00001174 struct drm_i915_gem_object *obj;
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001175 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07001176
Chris Wilson51311d02010-11-17 09:10:42 +00001177 if (args->size == 0)
1178 return 0;
1179
Linus Torvalds96d4f262019-01-03 18:57:57 -08001180 if (!access_ok(u64_to_user_ptr(args->data_ptr),
Chris Wilson51311d02010-11-17 09:10:42 +00001181 args->size))
1182 return -EFAULT;
1183
Chris Wilson03ac0642016-07-20 13:31:51 +01001184 obj = i915_gem_object_lookup(file, args->handle);
Chris Wilson258a5ed2016-08-05 10:14:16 +01001185 if (!obj)
1186 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07001187
Chris Wilson7dcd2492010-09-26 20:21:44 +01001188 /* Bounds check source. */
Matthew Auld966d5bf2016-12-13 20:32:22 +00001189 if (range_overflows_t(u64, args->offset, args->size, obj->base.size)) {
Chris Wilsonce9d4192010-09-26 20:50:05 +01001190 ret = -EINVAL;
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001191 goto out;
Chris Wilsonce9d4192010-09-26 20:50:05 +01001192 }
1193
Chris Wilsondb53a302011-02-03 11:57:46 +00001194 trace_i915_gem_object_pread(obj, args->offset, args->size);
1195
Chris Wilsone95433c2016-10-28 13:58:27 +01001196 ret = i915_gem_object_wait(obj,
1197 I915_WAIT_INTERRUPTIBLE,
1198 MAX_SCHEDULE_TIMEOUT,
1199 to_rps_client(file));
Chris Wilson258a5ed2016-08-05 10:14:16 +01001200 if (ret)
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001201 goto out;
Chris Wilson258a5ed2016-08-05 10:14:16 +01001202
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001203 ret = i915_gem_object_pin_pages(obj);
Chris Wilson258a5ed2016-08-05 10:14:16 +01001204 if (ret)
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001205 goto out;
Chris Wilson258a5ed2016-08-05 10:14:16 +01001206
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001207 ret = i915_gem_shmem_pread(obj, args);
Chris Wilson9c870d02016-10-24 13:42:15 +01001208 if (ret == -EFAULT || ret == -ENODEV)
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001209 ret = i915_gem_gtt_pread(obj, args);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301210
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001211 i915_gem_object_unpin_pages(obj);
1212out:
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001213 i915_gem_object_put(obj);
Eric Anholteb014592009-03-10 11:44:52 -07001214 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07001215}
1216
Keith Packard0839ccb2008-10-30 19:38:48 -07001217/* This is the fast write path which cannot handle
1218 * page faults in the source data
Linus Torvalds9b7530cc2008-10-20 14:16:43 -07001219 */
Linus Torvalds9b7530cc2008-10-20 14:16:43 -07001220
Chris Wilsonfe115622016-10-28 13:58:40 +01001221static inline bool
1222ggtt_write(struct io_mapping *mapping,
1223 loff_t base, int offset,
1224 char __user *user_data, int length)
Keith Packard0839ccb2008-10-30 19:38:48 -07001225{
Ville Syrjäläafe722b2017-09-01 20:12:52 +03001226 void __iomem *vaddr;
Keith Packard0839ccb2008-10-30 19:38:48 -07001227 unsigned long unwritten;
1228
Ben Widawsky4f0c7cf2012-04-16 14:07:47 -07001229 /* We can use the cpu mem copy function because this is X86. */
Ville Syrjäläafe722b2017-09-01 20:12:52 +03001230 vaddr = io_mapping_map_atomic_wc(mapping, base);
1231 unwritten = __copy_from_user_inatomic_nocache((void __force *)vaddr + offset,
Keith Packard0839ccb2008-10-30 19:38:48 -07001232 user_data, length);
Chris Wilsonfe115622016-10-28 13:58:40 +01001233 io_mapping_unmap_atomic(vaddr);
1234 if (unwritten) {
Ville Syrjäläafe722b2017-09-01 20:12:52 +03001235 vaddr = io_mapping_map_wc(mapping, base, PAGE_SIZE);
1236 unwritten = copy_from_user((void __force *)vaddr + offset,
1237 user_data, length);
Chris Wilsonfe115622016-10-28 13:58:40 +01001238 io_mapping_unmap(vaddr);
1239 }
Keith Packard0839ccb2008-10-30 19:38:48 -07001240
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001241 return unwritten;
1242}
1243
Eric Anholt3de09aa2009-03-09 09:42:23 -07001244/**
1245 * This is the fast pwrite path, where we copy the data directly from the
1246 * user into the GTT, uncached.
Chris Wilsonfe115622016-10-28 13:58:40 +01001247 * @obj: i915 GEM object
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01001248 * @args: pwrite arguments structure
Eric Anholt3de09aa2009-03-09 09:42:23 -07001249 */
Eric Anholt673a3942008-07-30 12:06:12 -07001250static int
Chris Wilsonfe115622016-10-28 13:58:40 +01001251i915_gem_gtt_pwrite_fast(struct drm_i915_gem_object *obj,
1252 const struct drm_i915_gem_pwrite *args)
Eric Anholt673a3942008-07-30 12:06:12 -07001253{
Chris Wilsonfe115622016-10-28 13:58:40 +01001254 struct drm_i915_private *i915 = to_i915(obj->base.dev);
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301255 struct i915_ggtt *ggtt = &i915->ggtt;
Chris Wilson538ef962019-01-14 14:21:18 +00001256 intel_wakeref_t wakeref;
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301257 struct drm_mm_node node;
Chris Wilsonfe115622016-10-28 13:58:40 +01001258 struct i915_vma *vma;
1259 u64 remain, offset;
1260 void __user *user_data;
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301261 int ret;
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301262
Chris Wilsonfe115622016-10-28 13:58:40 +01001263 ret = mutex_lock_interruptible(&i915->drm.struct_mutex);
1264 if (ret)
1265 return ret;
Daniel Vetter935aaa62012-03-25 19:47:35 +02001266
Chris Wilson8bd818152017-10-19 07:37:33 +01001267 if (i915_gem_object_has_struct_page(obj)) {
1268 /*
1269 * Avoid waking the device up if we can fallback, as
1270 * waking/resuming is very slow (worst-case 10-100 ms
1271 * depending on PCI sleeps and our own resume time).
1272 * This easily dwarfs any performance advantage from
1273 * using the cache bypass of indirect GGTT access.
1274 */
Chris Wilson538ef962019-01-14 14:21:18 +00001275 wakeref = intel_runtime_pm_get_if_in_use(i915);
1276 if (!wakeref) {
Chris Wilson8bd818152017-10-19 07:37:33 +01001277 ret = -EFAULT;
1278 goto out_unlock;
1279 }
1280 } else {
1281 /* No backing pages, no fallback, we must force GGTT access */
Chris Wilson538ef962019-01-14 14:21:18 +00001282 wakeref = intel_runtime_pm_get(i915);
Chris Wilson8bd818152017-10-19 07:37:33 +01001283 }
1284
Chris Wilson058d88c2016-08-15 10:49:06 +01001285 vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0,
Chris Wilsona3259ca2017-10-09 09:44:00 +01001286 PIN_MAPPABLE |
1287 PIN_NONFAULT |
1288 PIN_NONBLOCK);
Chris Wilson18034582016-08-18 17:16:45 +01001289 if (!IS_ERR(vma)) {
1290 node.start = i915_ggtt_offset(vma);
1291 node.allocated = false;
Chris Wilson49ef5292016-08-18 17:17:00 +01001292 ret = i915_vma_put_fence(vma);
Chris Wilson18034582016-08-18 17:16:45 +01001293 if (ret) {
1294 i915_vma_unpin(vma);
1295 vma = ERR_PTR(ret);
1296 }
1297 }
Chris Wilson058d88c2016-08-15 10:49:06 +01001298 if (IS_ERR(vma)) {
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001299 ret = insert_mappable_node(ggtt, &node, PAGE_SIZE);
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301300 if (ret)
Chris Wilson8bd818152017-10-19 07:37:33 +01001301 goto out_rpm;
Chris Wilsonfe115622016-10-28 13:58:40 +01001302 GEM_BUG_ON(!node.allocated);
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301303 }
Daniel Vetter935aaa62012-03-25 19:47:35 +02001304
1305 ret = i915_gem_object_set_to_gtt_domain(obj, true);
1306 if (ret)
1307 goto out_unpin;
1308
Chris Wilsonfe115622016-10-28 13:58:40 +01001309 mutex_unlock(&i915->drm.struct_mutex);
1310
Chris Wilsonb19482d2016-08-18 17:16:43 +01001311 intel_fb_obj_invalidate(obj, ORIGIN_CPU);
Paulo Zanoni063e4e62015-02-13 17:23:45 -02001312
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301313 user_data = u64_to_user_ptr(args->data_ptr);
1314 offset = args->offset;
1315 remain = args->size;
1316 while (remain) {
Eric Anholt673a3942008-07-30 12:06:12 -07001317 /* Operation in this page
1318 *
Keith Packard0839ccb2008-10-30 19:38:48 -07001319 * page_base = page offset within aperture
1320 * page_offset = offset within page
1321 * page_length = bytes to copy for this page
Eric Anholt673a3942008-07-30 12:06:12 -07001322 */
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301323 u32 page_base = node.start;
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001324 unsigned int page_offset = offset_in_page(offset);
1325 unsigned int page_length = PAGE_SIZE - page_offset;
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301326 page_length = remain < page_length ? remain : page_length;
1327 if (node.allocated) {
1328 wmb(); /* flush the write before we modify the GGTT */
Chris Wilson82ad6442018-06-05 16:37:58 +01001329 ggtt->vm.insert_page(&ggtt->vm,
1330 i915_gem_object_get_dma_address(obj, offset >> PAGE_SHIFT),
1331 node.start, I915_CACHE_NONE, 0);
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301332 wmb(); /* flush modifications to the GGTT (insert_page) */
1333 } else {
1334 page_base += offset & PAGE_MASK;
1335 }
Keith Packard0839ccb2008-10-30 19:38:48 -07001336 /* If we get a fault while copying data, then (presumably) our
Eric Anholt3de09aa2009-03-09 09:42:23 -07001337 * source page isn't available. Return the error and we'll
1338 * retry in the slow path.
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301339 * If the object is non-shmem backed, we retry again with the
1340 * path that handles page fault.
Keith Packard0839ccb2008-10-30 19:38:48 -07001341 */
Matthew Auld73ebd502017-12-11 15:18:20 +00001342 if (ggtt_write(&ggtt->iomap, page_base, page_offset,
Chris Wilsonfe115622016-10-28 13:58:40 +01001343 user_data, page_length)) {
1344 ret = -EFAULT;
1345 break;
Daniel Vetter935aaa62012-03-25 19:47:35 +02001346 }
Eric Anholt673a3942008-07-30 12:06:12 -07001347
Keith Packard0839ccb2008-10-30 19:38:48 -07001348 remain -= page_length;
1349 user_data += page_length;
1350 offset += page_length;
Eric Anholt673a3942008-07-30 12:06:12 -07001351 }
Chris Wilsond59b21e2017-02-22 11:40:49 +00001352 intel_fb_obj_flush(obj, ORIGIN_CPU);
Chris Wilsonfe115622016-10-28 13:58:40 +01001353
1354 mutex_lock(&i915->drm.struct_mutex);
Daniel Vetter935aaa62012-03-25 19:47:35 +02001355out_unpin:
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301356 if (node.allocated) {
1357 wmb();
Chris Wilson82ad6442018-06-05 16:37:58 +01001358 ggtt->vm.clear_range(&ggtt->vm, node.start, node.size);
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301359 remove_mappable_node(&node);
1360 } else {
Chris Wilson058d88c2016-08-15 10:49:06 +01001361 i915_vma_unpin(vma);
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301362 }
Chris Wilson8bd818152017-10-19 07:37:33 +01001363out_rpm:
Chris Wilson538ef962019-01-14 14:21:18 +00001364 intel_runtime_pm_put(i915, wakeref);
Chris Wilson8bd818152017-10-19 07:37:33 +01001365out_unlock:
Chris Wilsonfe115622016-10-28 13:58:40 +01001366 mutex_unlock(&i915->drm.struct_mutex);
Eric Anholt3de09aa2009-03-09 09:42:23 -07001367 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07001368}
1369
Chris Wilsonfe115622016-10-28 13:58:40 +01001370/* Per-page copy function for the shmem pwrite fastpath.
1371 * Flushes invalid cachelines before writing to the target if
1372 * needs_clflush_before is set and flushes out any written cachelines after
1373 * writing if needs_clflush is set.
1374 */
Eric Anholt40123c12009-03-09 13:42:30 -07001375static int
Chris Wilsonfe115622016-10-28 13:58:40 +01001376shmem_pwrite(struct page *page, int offset, int len, char __user *user_data,
Chris Wilsonfe115622016-10-28 13:58:40 +01001377 bool needs_clflush_before,
1378 bool needs_clflush_after)
Eric Anholt40123c12009-03-09 13:42:30 -07001379{
Chris Wilsonb9d126e2019-01-05 12:07:58 +00001380 char *vaddr;
Chris Wilsonfe115622016-10-28 13:58:40 +01001381 int ret;
Eric Anholt40123c12009-03-09 13:42:30 -07001382
Chris Wilsonb9d126e2019-01-05 12:07:58 +00001383 vaddr = kmap(page);
Chris Wilsonfe115622016-10-28 13:58:40 +01001384
Chris Wilsonb9d126e2019-01-05 12:07:58 +00001385 if (needs_clflush_before)
1386 drm_clflush_virt_range(vaddr + offset, len);
Chris Wilsonfe115622016-10-28 13:58:40 +01001387
Chris Wilsonb9d126e2019-01-05 12:07:58 +00001388 ret = __copy_from_user(vaddr + offset, user_data, len);
1389 if (!ret && needs_clflush_after)
1390 drm_clflush_virt_range(vaddr + offset, len);
Chris Wilsonfe115622016-10-28 13:58:40 +01001391
Chris Wilsonb9d126e2019-01-05 12:07:58 +00001392 kunmap(page);
1393
1394 return ret ? -EFAULT : 0;
Chris Wilsonfe115622016-10-28 13:58:40 +01001395}
1396
1397static int
1398i915_gem_shmem_pwrite(struct drm_i915_gem_object *obj,
1399 const struct drm_i915_gem_pwrite *args)
1400{
1401 struct drm_i915_private *i915 = to_i915(obj->base.dev);
1402 void __user *user_data;
1403 u64 remain;
Chris Wilsonfe115622016-10-28 13:58:40 +01001404 unsigned int partial_cacheline_write;
1405 unsigned int needs_clflush;
1406 unsigned int offset, idx;
1407 int ret;
1408
1409 ret = mutex_lock_interruptible(&i915->drm.struct_mutex);
Chris Wilson43394c72016-08-18 17:16:47 +01001410 if (ret)
1411 return ret;
Eric Anholt40123c12009-03-09 13:42:30 -07001412
Chris Wilsonfe115622016-10-28 13:58:40 +01001413 ret = i915_gem_obj_prepare_shmem_write(obj, &needs_clflush);
1414 mutex_unlock(&i915->drm.struct_mutex);
1415 if (ret)
1416 return ret;
1417
Chris Wilsonfe115622016-10-28 13:58:40 +01001418 /* If we don't overwrite a cacheline completely we need to be
1419 * careful to have up-to-date data by first clflushing. Don't
1420 * overcomplicate things and flush the entire patch.
1421 */
1422 partial_cacheline_write = 0;
1423 if (needs_clflush & CLFLUSH_BEFORE)
1424 partial_cacheline_write = boot_cpu_data.x86_clflush_size - 1;
1425
Chris Wilson43394c72016-08-18 17:16:47 +01001426 user_data = u64_to_user_ptr(args->data_ptr);
Chris Wilson43394c72016-08-18 17:16:47 +01001427 remain = args->size;
Chris Wilsonfe115622016-10-28 13:58:40 +01001428 offset = offset_in_page(args->offset);
1429 for (idx = args->offset >> PAGE_SHIFT; remain; idx++) {
1430 struct page *page = i915_gem_object_get_page(obj, idx);
Chris Wilsona5e856a52018-10-12 15:02:28 +01001431 unsigned int length = min_t(u64, remain, PAGE_SIZE - offset);
Chris Wilsone5281cc2010-10-28 13:45:36 +01001432
Chris Wilsonfe115622016-10-28 13:58:40 +01001433 ret = shmem_pwrite(page, offset, length, user_data,
Chris Wilsonfe115622016-10-28 13:58:40 +01001434 (offset | length) & partial_cacheline_write,
1435 needs_clflush & CLFLUSH_AFTER);
1436 if (ret)
Chris Wilson9da3da62012-06-01 15:20:22 +01001437 break;
1438
Chris Wilsonfe115622016-10-28 13:58:40 +01001439 remain -= length;
1440 user_data += length;
1441 offset = 0;
Eric Anholt40123c12009-03-09 13:42:30 -07001442 }
1443
Chris Wilsond59b21e2017-02-22 11:40:49 +00001444 intel_fb_obj_flush(obj, ORIGIN_CPU);
Chris Wilsonfe115622016-10-28 13:58:40 +01001445 i915_gem_obj_finish_shmem_access(obj);
Eric Anholt40123c12009-03-09 13:42:30 -07001446 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07001447}
1448
1449/**
1450 * Writes data to the object referenced by handle.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01001451 * @dev: drm device
1452 * @data: ioctl data blob
1453 * @file: drm file
Eric Anholt673a3942008-07-30 12:06:12 -07001454 *
1455 * On error, the contents of the buffer that were to be modified are undefined.
1456 */
1457int
1458i915_gem_pwrite_ioctl(struct drm_device *dev, void *data,
Chris Wilsonfbd5a262010-10-14 15:03:58 +01001459 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001460{
1461 struct drm_i915_gem_pwrite *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00001462 struct drm_i915_gem_object *obj;
Chris Wilson51311d02010-11-17 09:10:42 +00001463 int ret;
1464
1465 if (args->size == 0)
1466 return 0;
1467
Linus Torvalds96d4f262019-01-03 18:57:57 -08001468 if (!access_ok(u64_to_user_ptr(args->data_ptr), args->size))
Chris Wilson51311d02010-11-17 09:10:42 +00001469 return -EFAULT;
1470
Chris Wilson03ac0642016-07-20 13:31:51 +01001471 obj = i915_gem_object_lookup(file, args->handle);
Chris Wilson258a5ed2016-08-05 10:14:16 +01001472 if (!obj)
1473 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07001474
Chris Wilson7dcd2492010-09-26 20:21:44 +01001475 /* Bounds check destination. */
Matthew Auld966d5bf2016-12-13 20:32:22 +00001476 if (range_overflows_t(u64, args->offset, args->size, obj->base.size)) {
Chris Wilsonce9d4192010-09-26 20:50:05 +01001477 ret = -EINVAL;
Chris Wilson258a5ed2016-08-05 10:14:16 +01001478 goto err;
Chris Wilsonce9d4192010-09-26 20:50:05 +01001479 }
1480
Chris Wilsonf8c1cce2018-07-12 19:53:14 +01001481 /* Writes not allowed into this read-only object */
1482 if (i915_gem_object_is_readonly(obj)) {
1483 ret = -EINVAL;
1484 goto err;
1485 }
1486
Chris Wilsondb53a302011-02-03 11:57:46 +00001487 trace_i915_gem_object_pwrite(obj, args->offset, args->size);
1488
Chris Wilson7c55e2c2017-03-07 12:03:38 +00001489 ret = -ENODEV;
1490 if (obj->ops->pwrite)
1491 ret = obj->ops->pwrite(obj, args);
1492 if (ret != -ENODEV)
1493 goto err;
1494
Chris Wilsone95433c2016-10-28 13:58:27 +01001495 ret = i915_gem_object_wait(obj,
1496 I915_WAIT_INTERRUPTIBLE |
1497 I915_WAIT_ALL,
1498 MAX_SCHEDULE_TIMEOUT,
1499 to_rps_client(file));
Chris Wilson258a5ed2016-08-05 10:14:16 +01001500 if (ret)
1501 goto err;
1502
Chris Wilsonfe115622016-10-28 13:58:40 +01001503 ret = i915_gem_object_pin_pages(obj);
Chris Wilson258a5ed2016-08-05 10:14:16 +01001504 if (ret)
Chris Wilsonfe115622016-10-28 13:58:40 +01001505 goto err;
Chris Wilson258a5ed2016-08-05 10:14:16 +01001506
Daniel Vetter935aaa62012-03-25 19:47:35 +02001507 ret = -EFAULT;
Eric Anholt673a3942008-07-30 12:06:12 -07001508 /* We can only do the GTT pwrite on untiled buffers, as otherwise
1509 * it would end up going through the fenced access, and we'll get
1510 * different detiling behavior between reading and writing.
1511 * pread/pwrite currently are reading and writing from the CPU
1512 * perspective, requiring manual detiling by the client.
1513 */
Chris Wilson6eae0052016-06-20 15:05:52 +01001514 if (!i915_gem_object_has_struct_page(obj) ||
Chris Wilson9c870d02016-10-24 13:42:15 +01001515 cpu_write_needs_clflush(obj))
Daniel Vetter935aaa62012-03-25 19:47:35 +02001516 /* Note that the gtt paths might fail with non-page-backed user
1517 * pointers (e.g. gtt mappings when moving data between
Chris Wilson9c870d02016-10-24 13:42:15 +01001518 * textures). Fallback to the shmem path in that case.
1519 */
Chris Wilsonfe115622016-10-28 13:58:40 +01001520 ret = i915_gem_gtt_pwrite_fast(obj, args);
Eric Anholt673a3942008-07-30 12:06:12 -07001521
Chris Wilsond1054ee2016-07-16 18:42:36 +01001522 if (ret == -EFAULT || ret == -ENOSPC) {
Chris Wilson6a2c4232014-11-04 04:51:40 -08001523 if (obj->phys_handle)
1524 ret = i915_gem_phys_pwrite(obj, args, file);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301525 else
Chris Wilsonfe115622016-10-28 13:58:40 +01001526 ret = i915_gem_shmem_pwrite(obj, args);
Chris Wilson6a2c4232014-11-04 04:51:40 -08001527 }
Daniel Vetter5c0480f2011-12-14 13:57:30 +01001528
Chris Wilsonfe115622016-10-28 13:58:40 +01001529 i915_gem_object_unpin_pages(obj);
Chris Wilson258a5ed2016-08-05 10:14:16 +01001530err:
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001531 i915_gem_object_put(obj);
Chris Wilson258a5ed2016-08-05 10:14:16 +01001532 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07001533}
1534
Chris Wilson40e62d52016-10-28 13:58:41 +01001535static void i915_gem_object_bump_inactive_ggtt(struct drm_i915_gem_object *obj)
1536{
1537 struct drm_i915_private *i915;
1538 struct list_head *list;
1539 struct i915_vma *vma;
1540
Chris Wilsonf2123812017-10-16 12:40:37 +01001541 GEM_BUG_ON(!i915_gem_object_has_pinned_pages(obj));
1542
Chris Wilsone2189dd2017-12-07 21:14:07 +00001543 for_each_ggtt_vma(vma, obj) {
Chris Wilson40e62d52016-10-28 13:58:41 +01001544 if (i915_vma_is_active(vma))
1545 continue;
1546
1547 if (!drm_mm_node_allocated(&vma->node))
1548 continue;
1549
1550 list_move_tail(&vma->vm_link, &vma->vm->inactive_list);
1551 }
1552
1553 i915 = to_i915(obj->base.dev);
Chris Wilsonf2123812017-10-16 12:40:37 +01001554 spin_lock(&i915->mm.obj_lock);
Chris Wilson40e62d52016-10-28 13:58:41 +01001555 list = obj->bind_count ? &i915->mm.bound_list : &i915->mm.unbound_list;
Chris Wilsonf2123812017-10-16 12:40:37 +01001556 list_move_tail(&obj->mm.link, list);
1557 spin_unlock(&i915->mm.obj_lock);
Chris Wilson40e62d52016-10-28 13:58:41 +01001558}
1559
Eric Anholt673a3942008-07-30 12:06:12 -07001560/**
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001561 * Called when user space prepares to use an object with the CPU, either
1562 * through the mmap ioctl's mapping or a GTT mapping.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01001563 * @dev: drm device
1564 * @data: ioctl data blob
1565 * @file: drm file
Eric Anholt673a3942008-07-30 12:06:12 -07001566 */
1567int
1568i915_gem_set_domain_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00001569 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001570{
1571 struct drm_i915_gem_set_domain *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00001572 struct drm_i915_gem_object *obj;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001573 uint32_t read_domains = args->read_domains;
1574 uint32_t write_domain = args->write_domain;
Chris Wilson40e62d52016-10-28 13:58:41 +01001575 int err;
Eric Anholt673a3942008-07-30 12:06:12 -07001576
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001577 /* Only handle setting domains to types used by the CPU. */
Chris Wilsonb8f90962016-08-05 10:14:07 +01001578 if ((write_domain | read_domains) & I915_GEM_GPU_DOMAINS)
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001579 return -EINVAL;
1580
1581 /* Having something in the write domain implies it's in the read
1582 * domain, and only that read domain. Enforce that in the request.
1583 */
1584 if (write_domain != 0 && read_domains != write_domain)
1585 return -EINVAL;
1586
Chris Wilson03ac0642016-07-20 13:31:51 +01001587 obj = i915_gem_object_lookup(file, args->handle);
Chris Wilsonb8f90962016-08-05 10:14:07 +01001588 if (!obj)
1589 return -ENOENT;
Jesse Barnes652c3932009-08-17 13:31:43 -07001590
Chris Wilson3236f572012-08-24 09:35:09 +01001591 /* Try to flush the object off the GPU without holding the lock.
1592 * We will repeat the flush holding the lock in the normal manner
1593 * to catch cases where we are gazumped.
1594 */
Chris Wilson40e62d52016-10-28 13:58:41 +01001595 err = i915_gem_object_wait(obj,
Chris Wilsone95433c2016-10-28 13:58:27 +01001596 I915_WAIT_INTERRUPTIBLE |
Chris Wilsone9eaf822018-10-01 15:47:55 +01001597 I915_WAIT_PRIORITY |
Chris Wilsone95433c2016-10-28 13:58:27 +01001598 (write_domain ? I915_WAIT_ALL : 0),
1599 MAX_SCHEDULE_TIMEOUT,
1600 to_rps_client(file));
Chris Wilson40e62d52016-10-28 13:58:41 +01001601 if (err)
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001602 goto out;
Chris Wilsonb8f90962016-08-05 10:14:07 +01001603
Tina Zhanga03f3952017-11-14 10:25:13 +00001604 /*
1605 * Proxy objects do not control access to the backing storage, ergo
1606 * they cannot be used as a means to manipulate the cache domain
1607 * tracking for that backing storage. The proxy object is always
1608 * considered to be outside of any cache domain.
1609 */
1610 if (i915_gem_object_is_proxy(obj)) {
1611 err = -ENXIO;
1612 goto out;
1613 }
1614
1615 /*
1616 * Flush and acquire obj->pages so that we are coherent through
Chris Wilson40e62d52016-10-28 13:58:41 +01001617 * direct access in memory with previous cached writes through
1618 * shmemfs and that our cache domain tracking remains valid.
1619 * For example, if the obj->filp was moved to swap without us
1620 * being notified and releasing the pages, we would mistakenly
1621 * continue to assume that the obj remained out of the CPU cached
1622 * domain.
1623 */
1624 err = i915_gem_object_pin_pages(obj);
1625 if (err)
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001626 goto out;
Chris Wilson40e62d52016-10-28 13:58:41 +01001627
1628 err = i915_mutex_lock_interruptible(dev);
1629 if (err)
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001630 goto out_unpin;
Chris Wilson3236f572012-08-24 09:35:09 +01001631
Chris Wilsone22d8e32017-04-12 12:01:11 +01001632 if (read_domains & I915_GEM_DOMAIN_WC)
1633 err = i915_gem_object_set_to_wc_domain(obj, write_domain);
1634 else if (read_domains & I915_GEM_DOMAIN_GTT)
1635 err = i915_gem_object_set_to_gtt_domain(obj, write_domain);
Chris Wilson43566de2015-01-02 16:29:29 +05301636 else
Chris Wilsone22d8e32017-04-12 12:01:11 +01001637 err = i915_gem_object_set_to_cpu_domain(obj, write_domain);
Chris Wilson40e62d52016-10-28 13:58:41 +01001638
1639 /* And bump the LRU for this access */
1640 i915_gem_object_bump_inactive_ggtt(obj);
1641
1642 mutex_unlock(&dev->struct_mutex);
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001643
Daniel Vetter031b6982015-06-26 19:35:16 +02001644 if (write_domain != 0)
Chris Wilsonef749212017-04-12 12:01:10 +01001645 intel_fb_obj_invalidate(obj,
1646 fb_write_origin(obj, write_domain));
Daniel Vetter031b6982015-06-26 19:35:16 +02001647
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001648out_unpin:
Chris Wilson40e62d52016-10-28 13:58:41 +01001649 i915_gem_object_unpin_pages(obj);
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001650out:
1651 i915_gem_object_put(obj);
Chris Wilson40e62d52016-10-28 13:58:41 +01001652 return err;
Eric Anholt673a3942008-07-30 12:06:12 -07001653}
1654
1655/**
1656 * Called when user space has done writes to this buffer
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01001657 * @dev: drm device
1658 * @data: ioctl data blob
1659 * @file: drm file
Eric Anholt673a3942008-07-30 12:06:12 -07001660 */
1661int
1662i915_gem_sw_finish_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00001663 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001664{
1665 struct drm_i915_gem_sw_finish *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00001666 struct drm_i915_gem_object *obj;
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001667
Chris Wilson03ac0642016-07-20 13:31:51 +01001668 obj = i915_gem_object_lookup(file, args->handle);
Chris Wilsonc21724c2016-08-05 10:14:19 +01001669 if (!obj)
1670 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07001671
Tina Zhanga03f3952017-11-14 10:25:13 +00001672 /*
1673 * Proxy objects are barred from CPU access, so there is no
1674 * need to ban sw_finish as it is a nop.
1675 */
1676
Eric Anholt673a3942008-07-30 12:06:12 -07001677 /* Pinned buffers may be scanout, so flush the cache */
Chris Wilson5a97bcc2017-02-22 11:40:46 +00001678 i915_gem_object_flush_if_display(obj);
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001679 i915_gem_object_put(obj);
Chris Wilson5a97bcc2017-02-22 11:40:46 +00001680
1681 return 0;
Eric Anholt673a3942008-07-30 12:06:12 -07001682}
1683
1684/**
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01001685 * i915_gem_mmap_ioctl - Maps the contents of an object, returning the address
1686 * it is mapped to.
1687 * @dev: drm device
1688 * @data: ioctl data blob
1689 * @file: drm file
Eric Anholt673a3942008-07-30 12:06:12 -07001690 *
1691 * While the mapping holds a reference on the contents of the object, it doesn't
1692 * imply a ref on the object itself.
Daniel Vetter34367382014-10-16 12:28:18 +02001693 *
1694 * IMPORTANT:
1695 *
1696 * DRM driver writers who look a this function as an example for how to do GEM
1697 * mmap support, please don't implement mmap support like here. The modern way
1698 * to implement DRM mmap support is with an mmap offset ioctl (like
1699 * i915_gem_mmap_gtt) and then using the mmap syscall on the DRM fd directly.
1700 * That way debug tooling like valgrind will understand what's going on, hiding
1701 * the mmap call in a driver private ioctl will break that. The i915 driver only
1702 * does cpu mmaps this way because we didn't know better.
Eric Anholt673a3942008-07-30 12:06:12 -07001703 */
1704int
1705i915_gem_mmap_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00001706 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001707{
1708 struct drm_i915_gem_mmap *args = data;
Chris Wilson03ac0642016-07-20 13:31:51 +01001709 struct drm_i915_gem_object *obj;
Eric Anholt673a3942008-07-30 12:06:12 -07001710 unsigned long addr;
1711
Akash Goel1816f922015-01-02 16:29:30 +05301712 if (args->flags & ~(I915_MMAP_WC))
1713 return -EINVAL;
1714
Borislav Petkov568a58e2016-03-29 17:42:01 +02001715 if (args->flags & I915_MMAP_WC && !boot_cpu_has(X86_FEATURE_PAT))
Akash Goel1816f922015-01-02 16:29:30 +05301716 return -ENODEV;
1717
Chris Wilson03ac0642016-07-20 13:31:51 +01001718 obj = i915_gem_object_lookup(file, args->handle);
1719 if (!obj)
Chris Wilsonbf79cb92010-08-04 14:19:46 +01001720 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07001721
Daniel Vetter1286ff72012-05-10 15:25:09 +02001722 /* prime objects have no backing filp to GEM mmap
1723 * pages from.
1724 */
Chris Wilson03ac0642016-07-20 13:31:51 +01001725 if (!obj->base.filp) {
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001726 i915_gem_object_put(obj);
Tina Zhang274b2462017-11-14 10:25:12 +00001727 return -ENXIO;
Daniel Vetter1286ff72012-05-10 15:25:09 +02001728 }
1729
Chris Wilson03ac0642016-07-20 13:31:51 +01001730 addr = vm_mmap(obj->base.filp, 0, args->size,
Eric Anholt673a3942008-07-30 12:06:12 -07001731 PROT_READ | PROT_WRITE, MAP_SHARED,
1732 args->offset);
Akash Goel1816f922015-01-02 16:29:30 +05301733 if (args->flags & I915_MMAP_WC) {
1734 struct mm_struct *mm = current->mm;
1735 struct vm_area_struct *vma;
1736
Michal Hocko80a89a52016-05-23 16:26:11 -07001737 if (down_write_killable(&mm->mmap_sem)) {
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001738 i915_gem_object_put(obj);
Michal Hocko80a89a52016-05-23 16:26:11 -07001739 return -EINTR;
1740 }
Akash Goel1816f922015-01-02 16:29:30 +05301741 vma = find_vma(mm, addr);
1742 if (vma)
1743 vma->vm_page_prot =
1744 pgprot_writecombine(vm_get_page_prot(vma->vm_flags));
1745 else
1746 addr = -ENOMEM;
1747 up_write(&mm->mmap_sem);
Chris Wilsonaeecc962016-06-17 14:46:39 -03001748
1749 /* This may race, but that's ok, it only gets set */
Chris Wilson50349242016-08-18 17:17:04 +01001750 WRITE_ONCE(obj->frontbuffer_ggtt_origin, ORIGIN_CPU);
Akash Goel1816f922015-01-02 16:29:30 +05301751 }
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001752 i915_gem_object_put(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001753 if (IS_ERR((void *)addr))
1754 return addr;
1755
1756 args->addr_ptr = (uint64_t) addr;
1757
1758 return 0;
1759}
1760
Chris Wilsond899ace2018-07-25 16:54:47 +01001761static unsigned int tile_row_pages(const struct drm_i915_gem_object *obj)
Chris Wilson03af84f2016-08-18 17:17:01 +01001762{
Chris Wilson6649a0b2017-01-09 16:16:08 +00001763 return i915_gem_object_get_tile_row_size(obj) >> PAGE_SHIFT;
Chris Wilson03af84f2016-08-18 17:17:01 +01001764}
1765
Jesse Barnesde151cf2008-11-12 10:03:55 -08001766/**
Chris Wilson4cc69072016-08-25 19:05:19 +01001767 * i915_gem_mmap_gtt_version - report the current feature set for GTT mmaps
1768 *
1769 * A history of the GTT mmap interface:
1770 *
1771 * 0 - Everything had to fit into the GTT. Both parties of a memcpy had to
1772 * aligned and suitable for fencing, and still fit into the available
1773 * mappable space left by the pinned display objects. A classic problem
1774 * we called the page-fault-of-doom where we would ping-pong between
1775 * two objects that could not fit inside the GTT and so the memcpy
1776 * would page one object in at the expense of the other between every
1777 * single byte.
1778 *
1779 * 1 - Objects can be any size, and have any compatible fencing (X Y, or none
1780 * as set via i915_gem_set_tiling() [DRM_I915_GEM_SET_TILING]). If the
1781 * object is too large for the available space (or simply too large
1782 * for the mappable aperture!), a view is created instead and faulted
1783 * into userspace. (This view is aligned and sized appropriately for
1784 * fenced access.)
1785 *
Chris Wilsone22d8e32017-04-12 12:01:11 +01001786 * 2 - Recognise WC as a separate cache domain so that we can flush the
1787 * delayed writes via GTT before performing direct access via WC.
1788 *
Chris Wilson4cc69072016-08-25 19:05:19 +01001789 * Restrictions:
1790 *
1791 * * snoopable objects cannot be accessed via the GTT. It can cause machine
1792 * hangs on some architectures, corruption on others. An attempt to service
1793 * a GTT page fault from a snoopable object will generate a SIGBUS.
1794 *
1795 * * the object must be able to fit into RAM (physical memory, though no
1796 * limited to the mappable aperture).
1797 *
1798 *
1799 * Caveats:
1800 *
1801 * * a new GTT page fault will synchronize rendering from the GPU and flush
1802 * all data to system memory. Subsequent access will not be synchronized.
1803 *
1804 * * all mappings are revoked on runtime device suspend.
1805 *
1806 * * there are only 8, 16 or 32 fence registers to share between all users
1807 * (older machines require fence register for display and blitter access
1808 * as well). Contention of the fence registers will cause the previous users
1809 * to be unmapped and any new access will generate new page faults.
1810 *
1811 * * running out of memory while servicing a fault may generate a SIGBUS,
1812 * rather than the expected SIGSEGV.
1813 */
1814int i915_gem_mmap_gtt_version(void)
1815{
Chris Wilsone22d8e32017-04-12 12:01:11 +01001816 return 2;
Chris Wilson4cc69072016-08-25 19:05:19 +01001817}
1818
Chris Wilson2d4281b2017-01-10 09:56:32 +00001819static inline struct i915_ggtt_view
Chris Wilsond899ace2018-07-25 16:54:47 +01001820compute_partial_view(const struct drm_i915_gem_object *obj,
Chris Wilson2d4281b2017-01-10 09:56:32 +00001821 pgoff_t page_offset,
1822 unsigned int chunk)
1823{
1824 struct i915_ggtt_view view;
1825
1826 if (i915_gem_object_is_tiled(obj))
1827 chunk = roundup(chunk, tile_row_pages(obj));
1828
Chris Wilson2d4281b2017-01-10 09:56:32 +00001829 view.type = I915_GGTT_VIEW_PARTIAL;
Chris Wilson8bab11932017-01-14 00:28:25 +00001830 view.partial.offset = rounddown(page_offset, chunk);
1831 view.partial.size =
Chris Wilson2d4281b2017-01-10 09:56:32 +00001832 min_t(unsigned int, chunk,
Chris Wilson8bab11932017-01-14 00:28:25 +00001833 (obj->base.size >> PAGE_SHIFT) - view.partial.offset);
Chris Wilson2d4281b2017-01-10 09:56:32 +00001834
1835 /* If the partial covers the entire object, just create a normal VMA. */
1836 if (chunk >= obj->base.size >> PAGE_SHIFT)
1837 view.type = I915_GGTT_VIEW_NORMAL;
1838
1839 return view;
1840}
1841
Chris Wilson4cc69072016-08-25 19:05:19 +01001842/**
Jesse Barnesde151cf2008-11-12 10:03:55 -08001843 * i915_gem_fault - fault a page into the GTT
Geliang Tangd9072a32015-09-15 05:58:44 -07001844 * @vmf: fault info
Jesse Barnesde151cf2008-11-12 10:03:55 -08001845 *
1846 * The fault handler is set up by drm_gem_mmap() when a object is GTT mapped
1847 * from userspace. The fault handler takes care of binding the object to
1848 * the GTT (if needed), allocating and programming a fence register (again,
1849 * only if needed based on whether the old reg is still valid or the object
1850 * is tiled) and inserting a new PTE into the faulting process.
1851 *
1852 * Note that the faulting process may involve evicting existing objects
1853 * from the GTT and/or fence registers to make room. So performance may
1854 * suffer if the GTT working set is large or there are few fence registers
1855 * left.
Chris Wilson4cc69072016-08-25 19:05:19 +01001856 *
1857 * The current feature set supported by i915_gem_fault() and thus GTT mmaps
1858 * is exposed via I915_PARAM_MMAP_GTT_VERSION (see i915_gem_mmap_gtt_version).
Jesse Barnesde151cf2008-11-12 10:03:55 -08001859 */
Chris Wilson52137012018-06-06 22:45:20 +01001860vm_fault_t i915_gem_fault(struct vm_fault *vmf)
Jesse Barnesde151cf2008-11-12 10:03:55 -08001861{
Chris Wilson420980c2018-06-05 14:57:46 +01001862#define MIN_CHUNK_PAGES (SZ_1M >> PAGE_SHIFT)
Dave Jiang11bac802017-02-24 14:56:41 -08001863 struct vm_area_struct *area = vmf->vma;
Chris Wilson058d88c2016-08-15 10:49:06 +01001864 struct drm_i915_gem_object *obj = to_intel_bo(area->vm_private_data);
Chris Wilson05394f32010-11-08 19:18:58 +00001865 struct drm_device *dev = obj->base.dev;
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03001866 struct drm_i915_private *dev_priv = to_i915(dev);
1867 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Chris Wilsonaae7c062018-09-03 09:33:34 +01001868 bool write = area->vm_flags & VM_WRITE;
Chris Wilson538ef962019-01-14 14:21:18 +00001869 intel_wakeref_t wakeref;
Chris Wilson058d88c2016-08-15 10:49:06 +01001870 struct i915_vma *vma;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001871 pgoff_t page_offset;
Chris Wilsonb8f90962016-08-05 10:14:07 +01001872 int ret;
Paulo Zanonif65c9162013-11-27 18:20:34 -02001873
Chris Wilson3e977ac2018-07-12 19:53:13 +01001874 /* Sanity check that we allow writing into this object */
1875 if (i915_gem_object_is_readonly(obj) && write)
1876 return VM_FAULT_SIGBUS;
1877
Jesse Barnesde151cf2008-11-12 10:03:55 -08001878 /* We don't use vmf->pgoff since that has the fake offset */
Jan Kara1a29d852016-12-14 15:07:01 -08001879 page_offset = (vmf->address - area->vm_start) >> PAGE_SHIFT;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001880
Chris Wilsondb53a302011-02-03 11:57:46 +00001881 trace_i915_gem_object_fault(obj, page_offset, true, write);
1882
Chris Wilson6e4930f2014-02-07 18:37:06 -02001883 /* Try to flush the object off the GPU first without holding the lock.
Chris Wilsonb8f90962016-08-05 10:14:07 +01001884 * Upon acquiring the lock, we will perform our sanity checks and then
Chris Wilson6e4930f2014-02-07 18:37:06 -02001885 * repeat the flush holding the lock in the normal manner to catch cases
1886 * where we are gazumped.
1887 */
Chris Wilsone95433c2016-10-28 13:58:27 +01001888 ret = i915_gem_object_wait(obj,
1889 I915_WAIT_INTERRUPTIBLE,
1890 MAX_SCHEDULE_TIMEOUT,
1891 NULL);
Chris Wilson6e4930f2014-02-07 18:37:06 -02001892 if (ret)
Chris Wilsonb8f90962016-08-05 10:14:07 +01001893 goto err;
1894
Chris Wilson40e62d52016-10-28 13:58:41 +01001895 ret = i915_gem_object_pin_pages(obj);
1896 if (ret)
1897 goto err;
1898
Chris Wilson538ef962019-01-14 14:21:18 +00001899 wakeref = intel_runtime_pm_get(dev_priv);
Chris Wilsonb8f90962016-08-05 10:14:07 +01001900
1901 ret = i915_mutex_lock_interruptible(dev);
1902 if (ret)
1903 goto err_rpm;
Chris Wilson6e4930f2014-02-07 18:37:06 -02001904
Chris Wilsoneb119bd2012-12-16 12:43:36 +00001905 /* Access to snoopable pages through the GTT is incoherent. */
Tvrtko Ursulin0031fb92016-11-04 14:42:44 +00001906 if (obj->cache_level != I915_CACHE_NONE && !HAS_LLC(dev_priv)) {
Chris Wilsonddeff6e2014-05-28 16:16:41 +01001907 ret = -EFAULT;
Chris Wilsonb8f90962016-08-05 10:14:07 +01001908 goto err_unlock;
Chris Wilsoneb119bd2012-12-16 12:43:36 +00001909 }
1910
Chris Wilson82118872016-08-18 17:17:05 +01001911
Chris Wilsona61007a2016-08-18 17:17:02 +01001912 /* Now pin it into the GTT as needed */
Chris Wilson7e7367d2018-06-30 10:05:09 +01001913 vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0,
1914 PIN_MAPPABLE |
1915 PIN_NONBLOCK |
1916 PIN_NONFAULT);
Chris Wilsona61007a2016-08-18 17:17:02 +01001917 if (IS_ERR(vma)) {
Chris Wilsona61007a2016-08-18 17:17:02 +01001918 /* Use a partial view if it is bigger than available space */
Chris Wilson2d4281b2017-01-10 09:56:32 +00001919 struct i915_ggtt_view view =
Chris Wilson8201c1f2017-01-10 09:56:33 +00001920 compute_partial_view(obj, page_offset, MIN_CHUNK_PAGES);
Chris Wilson7e7367d2018-06-30 10:05:09 +01001921 unsigned int flags;
Chris Wilsonaa136d92016-08-18 17:17:03 +01001922
Chris Wilson7e7367d2018-06-30 10:05:09 +01001923 flags = PIN_MAPPABLE;
1924 if (view.type == I915_GGTT_VIEW_NORMAL)
1925 flags |= PIN_NONBLOCK; /* avoid warnings for pinned */
1926
1927 /*
1928 * Userspace is now writing through an untracked VMA, abandon
Chris Wilson50349242016-08-18 17:17:04 +01001929 * all hope that the hardware is able to track future writes.
1930 */
1931 obj->frontbuffer_ggtt_origin = ORIGIN_CPU;
1932
Chris Wilson7e7367d2018-06-30 10:05:09 +01001933 vma = i915_gem_object_ggtt_pin(obj, &view, 0, 0, flags);
1934 if (IS_ERR(vma) && !view.type) {
1935 flags = PIN_MAPPABLE;
1936 view.type = I915_GGTT_VIEW_PARTIAL;
1937 vma = i915_gem_object_ggtt_pin(obj, &view, 0, 0, flags);
1938 }
Chris Wilsona61007a2016-08-18 17:17:02 +01001939 }
Chris Wilson058d88c2016-08-15 10:49:06 +01001940 if (IS_ERR(vma)) {
1941 ret = PTR_ERR(vma);
Chris Wilsonb8f90962016-08-05 10:14:07 +01001942 goto err_unlock;
Chris Wilson058d88c2016-08-15 10:49:06 +01001943 }
Jesse Barnesde151cf2008-11-12 10:03:55 -08001944
Chris Wilsonc9839302012-11-20 10:45:17 +00001945 ret = i915_gem_object_set_to_gtt_domain(obj, write);
1946 if (ret)
Chris Wilsonb8f90962016-08-05 10:14:07 +01001947 goto err_unpin;
Chris Wilsonc9839302012-11-20 10:45:17 +00001948
Chris Wilson3bd40732017-10-09 09:43:56 +01001949 ret = i915_vma_pin_fence(vma);
Chris Wilsonc9839302012-11-20 10:45:17 +00001950 if (ret)
Chris Wilsonb8f90962016-08-05 10:14:07 +01001951 goto err_unpin;
Chris Wilson7d1c4802010-08-07 21:45:03 +01001952
Chris Wilsonb90b91d2014-06-10 12:14:40 +01001953 /* Finally, remap it using the new GTT offset */
Chris Wilsonc58305a2016-08-19 16:54:28 +01001954 ret = remap_io_mapping(area,
Chris Wilson8bab11932017-01-14 00:28:25 +00001955 area->vm_start + (vma->ggtt_view.partial.offset << PAGE_SHIFT),
Matthew Auld73ebd502017-12-11 15:18:20 +00001956 (ggtt->gmadr.start + vma->node.start) >> PAGE_SHIFT,
Chris Wilsonc58305a2016-08-19 16:54:28 +01001957 min_t(u64, vma->size, area->vm_end - area->vm_start),
Matthew Auld73ebd502017-12-11 15:18:20 +00001958 &ggtt->iomap);
Chris Wilsona65adaf2017-10-09 09:43:57 +01001959 if (ret)
1960 goto err_fence;
Chris Wilsona61007a2016-08-18 17:17:02 +01001961
Chris Wilsona65adaf2017-10-09 09:43:57 +01001962 /* Mark as being mmapped into userspace for later revocation */
1963 assert_rpm_wakelock_held(dev_priv);
1964 if (!i915_vma_set_userfault(vma) && !obj->userfault_count++)
1965 list_add(&obj->userfault_link, &dev_priv->mm.userfault_list);
1966 GEM_BUG_ON(!obj->userfault_count);
1967
Chris Wilson7125397b2017-12-06 12:49:14 +00001968 i915_vma_set_ggtt_write(vma);
1969
Chris Wilsona65adaf2017-10-09 09:43:57 +01001970err_fence:
Chris Wilson3bd40732017-10-09 09:43:56 +01001971 i915_vma_unpin_fence(vma);
Chris Wilsonb8f90962016-08-05 10:14:07 +01001972err_unpin:
Chris Wilson058d88c2016-08-15 10:49:06 +01001973 __i915_vma_unpin(vma);
Chris Wilsonb8f90962016-08-05 10:14:07 +01001974err_unlock:
Jesse Barnesde151cf2008-11-12 10:03:55 -08001975 mutex_unlock(&dev->struct_mutex);
Chris Wilsonb8f90962016-08-05 10:14:07 +01001976err_rpm:
Chris Wilson538ef962019-01-14 14:21:18 +00001977 intel_runtime_pm_put(dev_priv, wakeref);
Chris Wilson40e62d52016-10-28 13:58:41 +01001978 i915_gem_object_unpin_pages(obj);
Chris Wilsonb8f90962016-08-05 10:14:07 +01001979err:
Jesse Barnesde151cf2008-11-12 10:03:55 -08001980 switch (ret) {
Chris Wilsond9bc7e92011-02-07 13:09:31 +00001981 case -EIO:
Daniel Vetter2232f032014-09-04 09:36:18 +02001982 /*
1983 * We eat errors when the gpu is terminally wedged to avoid
1984 * userspace unduly crashing (gl has no provisions for mmaps to
1985 * fail). But any other -EIO isn't ours (e.g. swap in failure)
1986 * and so needs to be reported.
1987 */
Chris Wilson52137012018-06-06 22:45:20 +01001988 if (!i915_terminally_wedged(&dev_priv->gpu_error))
1989 return VM_FAULT_SIGBUS;
Gustavo A. R. Silvaf0d759f2018-06-28 17:35:41 -05001990 /* else: fall through */
Chris Wilson045e7692010-11-07 09:18:22 +00001991 case -EAGAIN:
Daniel Vetter571c6082013-09-12 17:57:28 +02001992 /*
1993 * EAGAIN means the gpu is hung and we'll wait for the error
1994 * handler to reset everything when re-faulting in
1995 * i915_mutex_lock_interruptible.
Chris Wilsond9bc7e92011-02-07 13:09:31 +00001996 */
Chris Wilsonc7150892009-09-23 00:43:56 +01001997 case 0:
1998 case -ERESTARTSYS:
Chris Wilsonbed636a2011-02-11 20:31:19 +00001999 case -EINTR:
Dmitry Rogozhkine79e0fe2012-10-03 17:15:26 +03002000 case -EBUSY:
2001 /*
2002 * EBUSY is ok: this just means that another thread
2003 * already did the job.
2004 */
Chris Wilson52137012018-06-06 22:45:20 +01002005 return VM_FAULT_NOPAGE;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002006 case -ENOMEM:
Chris Wilson52137012018-06-06 22:45:20 +01002007 return VM_FAULT_OOM;
Daniel Vettera7c2e1a2012-10-17 11:17:16 +02002008 case -ENOSPC:
Chris Wilson45d67812014-01-31 11:34:57 +00002009 case -EFAULT:
Chris Wilson52137012018-06-06 22:45:20 +01002010 return VM_FAULT_SIGBUS;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002011 default:
Daniel Vettera7c2e1a2012-10-17 11:17:16 +02002012 WARN_ONCE(ret, "unhandled error in i915_gem_fault: %i\n", ret);
Chris Wilson52137012018-06-06 22:45:20 +01002013 return VM_FAULT_SIGBUS;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002014 }
2015}
2016
Chris Wilsona65adaf2017-10-09 09:43:57 +01002017static void __i915_gem_object_release_mmap(struct drm_i915_gem_object *obj)
2018{
2019 struct i915_vma *vma;
2020
2021 GEM_BUG_ON(!obj->userfault_count);
2022
2023 obj->userfault_count = 0;
2024 list_del(&obj->userfault_link);
2025 drm_vma_node_unmap(&obj->base.vma_node,
2026 obj->base.dev->anon_inode->i_mapping);
2027
Chris Wilsone2189dd2017-12-07 21:14:07 +00002028 for_each_ggtt_vma(vma, obj)
Chris Wilsona65adaf2017-10-09 09:43:57 +01002029 i915_vma_unset_userfault(vma);
Chris Wilsona65adaf2017-10-09 09:43:57 +01002030}
2031
Jesse Barnesde151cf2008-11-12 10:03:55 -08002032/**
Chris Wilson901782b2009-07-10 08:18:50 +01002033 * i915_gem_release_mmap - remove physical page mappings
2034 * @obj: obj in question
2035 *
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02002036 * Preserve the reservation of the mmapping with the DRM core code, but
Chris Wilson901782b2009-07-10 08:18:50 +01002037 * relinquish ownership of the pages back to the system.
2038 *
2039 * It is vital that we remove the page mapping if we have mapped a tiled
2040 * object through the GTT and then lose the fence register due to
2041 * resource pressure. Similarly if the object has been moved out of the
2042 * aperture, than pages mapped into userspace must be revoked. Removing the
2043 * mapping will then trigger a page fault on the next user access, allowing
2044 * fixup by i915_gem_fault().
2045 */
Eric Anholtd05ca302009-07-10 13:02:26 -07002046void
Chris Wilson05394f32010-11-08 19:18:58 +00002047i915_gem_release_mmap(struct drm_i915_gem_object *obj)
Chris Wilson901782b2009-07-10 08:18:50 +01002048{
Chris Wilson275f0392016-10-24 13:42:14 +01002049 struct drm_i915_private *i915 = to_i915(obj->base.dev);
Chris Wilson538ef962019-01-14 14:21:18 +00002050 intel_wakeref_t wakeref;
Chris Wilson275f0392016-10-24 13:42:14 +01002051
Chris Wilson349f2cc2016-04-13 17:35:12 +01002052 /* Serialisation between user GTT access and our code depends upon
2053 * revoking the CPU's PTE whilst the mutex is held. The next user
2054 * pagefault then has to wait until we release the mutex.
Chris Wilson9c870d02016-10-24 13:42:15 +01002055 *
2056 * Note that RPM complicates somewhat by adding an additional
2057 * requirement that operations to the GGTT be made holding the RPM
2058 * wakeref.
Chris Wilson349f2cc2016-04-13 17:35:12 +01002059 */
Chris Wilson275f0392016-10-24 13:42:14 +01002060 lockdep_assert_held(&i915->drm.struct_mutex);
Chris Wilson538ef962019-01-14 14:21:18 +00002061 wakeref = intel_runtime_pm_get(i915);
Chris Wilson349f2cc2016-04-13 17:35:12 +01002062
Chris Wilsona65adaf2017-10-09 09:43:57 +01002063 if (!obj->userfault_count)
Chris Wilson9c870d02016-10-24 13:42:15 +01002064 goto out;
Chris Wilson901782b2009-07-10 08:18:50 +01002065
Chris Wilsona65adaf2017-10-09 09:43:57 +01002066 __i915_gem_object_release_mmap(obj);
Chris Wilson349f2cc2016-04-13 17:35:12 +01002067
2068 /* Ensure that the CPU's PTE are revoked and there are not outstanding
2069 * memory transactions from userspace before we return. The TLB
2070 * flushing implied above by changing the PTE above *should* be
2071 * sufficient, an extra barrier here just provides us with a bit
2072 * of paranoid documentation about our requirement to serialise
2073 * memory writes before touching registers / GSM.
2074 */
2075 wmb();
Chris Wilson9c870d02016-10-24 13:42:15 +01002076
2077out:
Chris Wilson538ef962019-01-14 14:21:18 +00002078 intel_runtime_pm_put(i915, wakeref);
Chris Wilson901782b2009-07-10 08:18:50 +01002079}
2080
Chris Wilson7c108fd2016-10-24 13:42:18 +01002081void i915_gem_runtime_suspend(struct drm_i915_private *dev_priv)
Chris Wilsoneedd10f2014-06-16 08:57:44 +01002082{
Chris Wilson3594a3e2016-10-24 13:42:16 +01002083 struct drm_i915_gem_object *obj, *on;
Chris Wilson7c108fd2016-10-24 13:42:18 +01002084 int i;
Chris Wilsoneedd10f2014-06-16 08:57:44 +01002085
Chris Wilson3594a3e2016-10-24 13:42:16 +01002086 /*
2087 * Only called during RPM suspend. All users of the userfault_list
2088 * must be holding an RPM wakeref to ensure that this can not
2089 * run concurrently with themselves (and use the struct_mutex for
2090 * protection between themselves).
2091 */
2092
2093 list_for_each_entry_safe(obj, on,
Chris Wilsona65adaf2017-10-09 09:43:57 +01002094 &dev_priv->mm.userfault_list, userfault_link)
2095 __i915_gem_object_release_mmap(obj);
Chris Wilson7c108fd2016-10-24 13:42:18 +01002096
2097 /* The fence will be lost when the device powers down. If any were
2098 * in use by hardware (i.e. they are pinned), we should not be powering
2099 * down! All other fences will be reacquired by the user upon waking.
2100 */
2101 for (i = 0; i < dev_priv->num_fence_regs; i++) {
2102 struct drm_i915_fence_reg *reg = &dev_priv->fence_regs[i];
2103
Chris Wilsone0ec3ec2017-02-03 12:57:17 +00002104 /* Ideally we want to assert that the fence register is not
2105 * live at this point (i.e. that no piece of code will be
2106 * trying to write through fence + GTT, as that both violates
2107 * our tracking of activity and associated locking/barriers,
2108 * but also is illegal given that the hw is powered down).
2109 *
2110 * Previously we used reg->pin_count as a "liveness" indicator.
2111 * That is not sufficient, and we need a more fine-grained
2112 * tool if we want to have a sanity check here.
2113 */
Chris Wilson7c108fd2016-10-24 13:42:18 +01002114
2115 if (!reg->vma)
2116 continue;
2117
Chris Wilsona65adaf2017-10-09 09:43:57 +01002118 GEM_BUG_ON(i915_vma_has_userfault(reg->vma));
Chris Wilson7c108fd2016-10-24 13:42:18 +01002119 reg->dirty = true;
2120 }
Chris Wilsoneedd10f2014-06-16 08:57:44 +01002121}
2122
Chris Wilsond8cb5082012-08-11 15:41:03 +01002123static int i915_gem_object_create_mmap_offset(struct drm_i915_gem_object *obj)
2124{
Chris Wilsonfac5e232016-07-04 11:34:36 +01002125 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
Chris Wilsonf3f61842016-08-05 10:14:14 +01002126 int err;
Chris Wilsond8cb5082012-08-11 15:41:03 +01002127
Chris Wilsonf3f61842016-08-05 10:14:14 +01002128 err = drm_gem_create_mmap_offset(&obj->base);
Chris Wilsonb42a13d2017-01-06 15:22:40 +00002129 if (likely(!err))
Chris Wilsonf3f61842016-08-05 10:14:14 +01002130 return 0;
Daniel Vetterda494d72012-12-20 15:11:16 +01002131
Chris Wilsonb42a13d2017-01-06 15:22:40 +00002132 /* Attempt to reap some mmap space from dead objects */
2133 do {
Chris Wilsonec625fb2018-07-09 13:20:42 +01002134 err = i915_gem_wait_for_idle(dev_priv,
2135 I915_WAIT_INTERRUPTIBLE,
2136 MAX_SCHEDULE_TIMEOUT);
Chris Wilsonb42a13d2017-01-06 15:22:40 +00002137 if (err)
2138 break;
Chris Wilsond8cb5082012-08-11 15:41:03 +01002139
Chris Wilsonb42a13d2017-01-06 15:22:40 +00002140 i915_gem_drain_freed_objects(dev_priv);
Chris Wilsonf3f61842016-08-05 10:14:14 +01002141 err = drm_gem_create_mmap_offset(&obj->base);
Chris Wilsonb42a13d2017-01-06 15:22:40 +00002142 if (!err)
2143 break;
2144
2145 } while (flush_delayed_work(&dev_priv->gt.retire_work));
Daniel Vetterda494d72012-12-20 15:11:16 +01002146
Chris Wilsonf3f61842016-08-05 10:14:14 +01002147 return err;
Chris Wilsond8cb5082012-08-11 15:41:03 +01002148}
2149
2150static void i915_gem_object_free_mmap_offset(struct drm_i915_gem_object *obj)
2151{
Chris Wilsond8cb5082012-08-11 15:41:03 +01002152 drm_gem_free_mmap_offset(&obj->base);
2153}
2154
Dave Airlieda6b51d2014-12-24 13:11:17 +10002155int
Dave Airlieff72145b2011-02-07 12:16:14 +10002156i915_gem_mmap_gtt(struct drm_file *file,
2157 struct drm_device *dev,
Dave Airlieda6b51d2014-12-24 13:11:17 +10002158 uint32_t handle,
Dave Airlieff72145b2011-02-07 12:16:14 +10002159 uint64_t *offset)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002160{
Chris Wilson05394f32010-11-08 19:18:58 +00002161 struct drm_i915_gem_object *obj;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002162 int ret;
2163
Chris Wilson03ac0642016-07-20 13:31:51 +01002164 obj = i915_gem_object_lookup(file, handle);
Chris Wilsonf3f61842016-08-05 10:14:14 +01002165 if (!obj)
2166 return -ENOENT;
Chris Wilsonab182822009-09-22 18:46:17 +01002167
Chris Wilsond8cb5082012-08-11 15:41:03 +01002168 ret = i915_gem_object_create_mmap_offset(obj);
Chris Wilsonf3f61842016-08-05 10:14:14 +01002169 if (ret == 0)
2170 *offset = drm_vma_node_offset_addr(&obj->base.vma_node);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002171
Chris Wilsonf0cd5182016-10-28 13:58:43 +01002172 i915_gem_object_put(obj);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01002173 return ret;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002174}
2175
Dave Airlieff72145b2011-02-07 12:16:14 +10002176/**
2177 * i915_gem_mmap_gtt_ioctl - prepare an object for GTT mmap'ing
2178 * @dev: DRM device
2179 * @data: GTT mapping ioctl data
2180 * @file: GEM object info
2181 *
2182 * Simply returns the fake offset to userspace so it can mmap it.
2183 * The mmap call will end up in drm_gem_mmap(), which will set things
2184 * up so we can get faults in the handler above.
2185 *
2186 * The fault handler will take care of binding the object into the GTT
2187 * (since it may have been evicted to make room for something), allocating
2188 * a fence register, and mapping the appropriate aperture address into
2189 * userspace.
2190 */
2191int
2192i915_gem_mmap_gtt_ioctl(struct drm_device *dev, void *data,
2193 struct drm_file *file)
2194{
2195 struct drm_i915_gem_mmap_gtt *args = data;
2196
Dave Airlieda6b51d2014-12-24 13:11:17 +10002197 return i915_gem_mmap_gtt(file, dev, args->handle, &args->offset);
Dave Airlieff72145b2011-02-07 12:16:14 +10002198}
2199
Daniel Vetter225067e2012-08-20 10:23:20 +02002200/* Immediately discard the backing storage */
2201static void
2202i915_gem_object_truncate(struct drm_i915_gem_object *obj)
Chris Wilsone5281cc2010-10-28 13:45:36 +01002203{
Chris Wilson4d6294bf2012-08-11 15:41:05 +01002204 i915_gem_object_free_mmap_offset(obj);
Daniel Vetter1286ff72012-05-10 15:25:09 +02002205
Chris Wilson4d6294bf2012-08-11 15:41:05 +01002206 if (obj->base.filp == NULL)
2207 return;
2208
Daniel Vetter225067e2012-08-20 10:23:20 +02002209 /* Our goal here is to return as much of the memory as
2210 * is possible back to the system as we are called from OOM.
2211 * To do this we must instruct the shmfs to drop all of its
2212 * backing pages, *now*.
Chris Wilsone5281cc2010-10-28 13:45:36 +01002213 */
Chris Wilson55372522014-03-25 13:23:06 +00002214 shmem_truncate_range(file_inode(obj->base.filp), 0, (loff_t)-1);
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002215 obj->mm.madv = __I915_MADV_PURGED;
Chris Wilson4e5462e2017-03-07 13:20:31 +00002216 obj->mm.pages = ERR_PTR(-EFAULT);
Chris Wilsone5281cc2010-10-28 13:45:36 +01002217}
Chris Wilsone5281cc2010-10-28 13:45:36 +01002218
Chris Wilson55372522014-03-25 13:23:06 +00002219/* Try to discard unwanted pages */
Chris Wilson03ac84f2016-10-28 13:58:36 +01002220void __i915_gem_object_invalidate(struct drm_i915_gem_object *obj)
Daniel Vetter225067e2012-08-20 10:23:20 +02002221{
Chris Wilson55372522014-03-25 13:23:06 +00002222 struct address_space *mapping;
2223
Chris Wilson1233e2d2016-10-28 13:58:37 +01002224 lockdep_assert_held(&obj->mm.lock);
Chris Wilsonf1fa4f42017-10-13 21:26:13 +01002225 GEM_BUG_ON(i915_gem_object_has_pages(obj));
Chris Wilson1233e2d2016-10-28 13:58:37 +01002226
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002227 switch (obj->mm.madv) {
Chris Wilson55372522014-03-25 13:23:06 +00002228 case I915_MADV_DONTNEED:
2229 i915_gem_object_truncate(obj);
2230 case __I915_MADV_PURGED:
2231 return;
2232 }
2233
2234 if (obj->base.filp == NULL)
2235 return;
2236
Al Viro93c76a32015-12-04 23:45:44 -05002237 mapping = obj->base.filp->f_mapping,
Chris Wilson55372522014-03-25 13:23:06 +00002238 invalidate_mapping_pages(mapping, 0, (loff_t)-1);
Chris Wilsone5281cc2010-10-28 13:45:36 +01002239}
2240
Kuo-Hsin Yang64e3d122018-11-06 13:23:24 +00002241/*
2242 * Move pages to appropriate lru and release the pagevec, decrementing the
2243 * ref count of those pages.
2244 */
2245static void check_release_pagevec(struct pagevec *pvec)
2246{
2247 check_move_unevictable_pages(pvec);
2248 __pagevec_release(pvec);
2249 cond_resched();
2250}
2251
Chris Wilson5cdf5882010-09-27 15:51:07 +01002252static void
Chris Wilson03ac84f2016-10-28 13:58:36 +01002253i915_gem_object_put_pages_gtt(struct drm_i915_gem_object *obj,
2254 struct sg_table *pages)
Eric Anholt673a3942008-07-30 12:06:12 -07002255{
Dave Gordon85d12252016-05-20 11:54:06 +01002256 struct sgt_iter sgt_iter;
Kuo-Hsin Yang64e3d122018-11-06 13:23:24 +00002257 struct pagevec pvec;
Dave Gordon85d12252016-05-20 11:54:06 +01002258 struct page *page;
Daniel Vetter1286ff72012-05-10 15:25:09 +02002259
Chris Wilsone5facdf2016-12-23 14:57:57 +00002260 __i915_gem_object_release_shmem(obj, pages, true);
Eric Anholt856fa192009-03-19 14:10:50 -07002261
Chris Wilson03ac84f2016-10-28 13:58:36 +01002262 i915_gem_gtt_finish_pages(obj, pages);
Imre Deake2273302015-07-09 12:59:05 +03002263
Daniel Vetter6dacfd22011-09-12 21:30:02 +02002264 if (i915_gem_object_needs_bit17_swizzle(obj))
Chris Wilson03ac84f2016-10-28 13:58:36 +01002265 i915_gem_object_save_bit_17_swizzle(obj, pages);
Eric Anholt280b7132009-03-12 16:56:27 -07002266
Kuo-Hsin Yang64e3d122018-11-06 13:23:24 +00002267 mapping_clear_unevictable(file_inode(obj->base.filp)->i_mapping);
2268
2269 pagevec_init(&pvec);
Chris Wilson03ac84f2016-10-28 13:58:36 +01002270 for_each_sgt_page(page, sgt_iter, pages) {
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002271 if (obj->mm.dirty)
Chris Wilson9da3da62012-06-01 15:20:22 +01002272 set_page_dirty(page);
Chris Wilson3ef94da2009-09-14 16:50:29 +01002273
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002274 if (obj->mm.madv == I915_MADV_WILLNEED)
Chris Wilson9da3da62012-06-01 15:20:22 +01002275 mark_page_accessed(page);
Chris Wilson3ef94da2009-09-14 16:50:29 +01002276
Kuo-Hsin Yang64e3d122018-11-06 13:23:24 +00002277 if (!pagevec_add(&pvec, page))
2278 check_release_pagevec(&pvec);
Chris Wilson3ef94da2009-09-14 16:50:29 +01002279 }
Kuo-Hsin Yang64e3d122018-11-06 13:23:24 +00002280 if (pagevec_count(&pvec))
2281 check_release_pagevec(&pvec);
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002282 obj->mm.dirty = false;
Eric Anholt673a3942008-07-30 12:06:12 -07002283
Chris Wilson03ac84f2016-10-28 13:58:36 +01002284 sg_free_table(pages);
2285 kfree(pages);
Chris Wilson37e680a2012-06-07 15:38:42 +01002286}
2287
Chris Wilson96d77632016-10-28 13:58:33 +01002288static void __i915_gem_object_reset_page_iter(struct drm_i915_gem_object *obj)
2289{
2290 struct radix_tree_iter iter;
Ville Syrjäläc23aa712017-09-01 20:12:51 +03002291 void __rcu **slot;
Chris Wilson96d77632016-10-28 13:58:33 +01002292
Chris Wilsonbea6e982017-10-26 14:00:31 +01002293 rcu_read_lock();
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002294 radix_tree_for_each_slot(slot, &obj->mm.get_page.radix, &iter, 0)
2295 radix_tree_delete(&obj->mm.get_page.radix, iter.index);
Chris Wilsonbea6e982017-10-26 14:00:31 +01002296 rcu_read_unlock();
Chris Wilson96d77632016-10-28 13:58:33 +01002297}
2298
Chris Wilsonacd1c1e2018-06-11 08:55:32 +01002299static struct sg_table *
2300__i915_gem_object_unset_pages(struct drm_i915_gem_object *obj)
Chris Wilson37e680a2012-06-07 15:38:42 +01002301{
Chris Wilsonf2123812017-10-16 12:40:37 +01002302 struct drm_i915_private *i915 = to_i915(obj->base.dev);
Chris Wilson03ac84f2016-10-28 13:58:36 +01002303 struct sg_table *pages;
Chris Wilson37e680a2012-06-07 15:38:42 +01002304
Chris Wilson03ac84f2016-10-28 13:58:36 +01002305 pages = fetch_and_zero(&obj->mm.pages);
Chris Wilson484d9a82019-01-15 12:44:42 +00002306 if (IS_ERR_OR_NULL(pages))
2307 return pages;
Chris Wilsona2165e32012-12-03 11:49:00 +00002308
Chris Wilsonf2123812017-10-16 12:40:37 +01002309 spin_lock(&i915->mm.obj_lock);
2310 list_del(&obj->mm.link);
2311 spin_unlock(&i915->mm.obj_lock);
2312
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002313 if (obj->mm.mapping) {
Chris Wilson4b30cb22016-08-18 17:16:42 +01002314 void *ptr;
2315
Chris Wilson0ce81782017-05-17 13:09:59 +01002316 ptr = page_mask_bits(obj->mm.mapping);
Chris Wilson4b30cb22016-08-18 17:16:42 +01002317 if (is_vmalloc_addr(ptr))
2318 vunmap(ptr);
Chris Wilsonfb8621d2016-04-08 12:11:14 +01002319 else
Chris Wilson4b30cb22016-08-18 17:16:42 +01002320 kunmap(kmap_to_page(ptr));
2321
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002322 obj->mm.mapping = NULL;
Chris Wilson0a798eb2016-04-08 12:11:11 +01002323 }
2324
Chris Wilson96d77632016-10-28 13:58:33 +01002325 __i915_gem_object_reset_page_iter(obj);
Chris Wilsonacd1c1e2018-06-11 08:55:32 +01002326 obj->mm.page_sizes.phys = obj->mm.page_sizes.sg = 0;
Chris Wilson96d77632016-10-28 13:58:33 +01002327
Chris Wilsonacd1c1e2018-06-11 08:55:32 +01002328 return pages;
2329}
2330
Chris Wilson484d9a82019-01-15 12:44:42 +00002331int __i915_gem_object_put_pages(struct drm_i915_gem_object *obj,
2332 enum i915_mm_subclass subclass)
Chris Wilsonacd1c1e2018-06-11 08:55:32 +01002333{
2334 struct sg_table *pages;
Chris Wilson484d9a82019-01-15 12:44:42 +00002335 int ret;
Chris Wilsonacd1c1e2018-06-11 08:55:32 +01002336
2337 if (i915_gem_object_has_pinned_pages(obj))
Chris Wilson484d9a82019-01-15 12:44:42 +00002338 return -EBUSY;
Chris Wilsonacd1c1e2018-06-11 08:55:32 +01002339
2340 GEM_BUG_ON(obj->bind_count);
Chris Wilsonacd1c1e2018-06-11 08:55:32 +01002341
2342 /* May be called by shrinker from within get_pages() (on another bo) */
2343 mutex_lock_nested(&obj->mm.lock, subclass);
Chris Wilson484d9a82019-01-15 12:44:42 +00002344 if (unlikely(atomic_read(&obj->mm.pages_pin_count))) {
2345 ret = -EBUSY;
Chris Wilsonacd1c1e2018-06-11 08:55:32 +01002346 goto unlock;
Chris Wilson484d9a82019-01-15 12:44:42 +00002347 }
Chris Wilsonacd1c1e2018-06-11 08:55:32 +01002348
2349 /*
2350 * ->put_pages might need to allocate memory for the bit17 swizzle
2351 * array, hence protect them from being reaped by removing them from gtt
2352 * lists early.
2353 */
2354 pages = __i915_gem_object_unset_pages(obj);
Chris Wilson484d9a82019-01-15 12:44:42 +00002355
2356 /*
2357 * XXX Temporary hijinx to avoid updating all backends to handle
2358 * NULL pages. In the future, when we have more asynchronous
2359 * get_pages backends we should be better able to handle the
2360 * cancellation of the async task in a more uniform manner.
2361 */
2362 if (!pages && !i915_gem_object_needs_async_cancel(obj))
2363 pages = ERR_PTR(-EINVAL);
2364
Chris Wilson4e5462e2017-03-07 13:20:31 +00002365 if (!IS_ERR(pages))
2366 obj->ops->put_pages(obj, pages);
2367
Chris Wilson484d9a82019-01-15 12:44:42 +00002368 ret = 0;
Chris Wilson1233e2d2016-10-28 13:58:37 +01002369unlock:
2370 mutex_unlock(&obj->mm.lock);
Chris Wilson484d9a82019-01-15 12:44:42 +00002371
2372 return ret;
Chris Wilson6c085a72012-08-20 11:40:46 +02002373}
2374
Tvrtko Ursulinf8e57862018-09-26 09:03:53 +01002375bool i915_sg_trim(struct sg_table *orig_st)
Tvrtko Ursulin0c40ce12016-11-09 15:13:43 +00002376{
2377 struct sg_table new_st;
2378 struct scatterlist *sg, *new_sg;
2379 unsigned int i;
2380
2381 if (orig_st->nents == orig_st->orig_nents)
Chris Wilson935a2f72017-02-13 17:15:13 +00002382 return false;
Tvrtko Ursulin0c40ce12016-11-09 15:13:43 +00002383
Chris Wilson8bfc478f2016-12-23 14:57:58 +00002384 if (sg_alloc_table(&new_st, orig_st->nents, GFP_KERNEL | __GFP_NOWARN))
Chris Wilson935a2f72017-02-13 17:15:13 +00002385 return false;
Tvrtko Ursulin0c40ce12016-11-09 15:13:43 +00002386
2387 new_sg = new_st.sgl;
2388 for_each_sg(orig_st->sgl, sg, orig_st->nents, i) {
2389 sg_set_page(new_sg, sg_page(sg), sg->length, 0);
Matthew Auldc6d22ab2018-09-20 15:27:06 +01002390 sg_dma_address(new_sg) = sg_dma_address(sg);
2391 sg_dma_len(new_sg) = sg_dma_len(sg);
2392
Tvrtko Ursulin0c40ce12016-11-09 15:13:43 +00002393 new_sg = sg_next(new_sg);
2394 }
Chris Wilsonc2dc6cc2016-12-19 12:43:46 +00002395 GEM_BUG_ON(new_sg); /* Should walk exactly nents and hit the end */
Tvrtko Ursulin0c40ce12016-11-09 15:13:43 +00002396
2397 sg_free_table(orig_st);
2398
2399 *orig_st = new_st;
Chris Wilson935a2f72017-02-13 17:15:13 +00002400 return true;
Tvrtko Ursulin0c40ce12016-11-09 15:13:43 +00002401}
2402
Matthew Auldb91b09e2017-10-06 23:18:17 +01002403static int i915_gem_object_get_pages_gtt(struct drm_i915_gem_object *obj)
Eric Anholt673a3942008-07-30 12:06:12 -07002404{
Chris Wilsonfac5e232016-07-04 11:34:36 +01002405 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
Chris Wilsond766ef52016-12-19 12:43:45 +00002406 const unsigned long page_count = obj->base.size / PAGE_SIZE;
2407 unsigned long i;
Eric Anholt673a3942008-07-30 12:06:12 -07002408 struct address_space *mapping;
Chris Wilson9da3da62012-06-01 15:20:22 +01002409 struct sg_table *st;
2410 struct scatterlist *sg;
Dave Gordon85d12252016-05-20 11:54:06 +01002411 struct sgt_iter sgt_iter;
Eric Anholt673a3942008-07-30 12:06:12 -07002412 struct page *page;
Imre Deak90797e62013-02-18 19:28:03 +02002413 unsigned long last_pfn = 0; /* suppress gcc warning */
Tvrtko Ursulin56024522017-08-03 10:14:17 +01002414 unsigned int max_segment = i915_sg_segment_size();
Matthew Auld84e89782017-10-09 12:00:24 +01002415 unsigned int sg_page_sizes;
Kuo-Hsin Yang64e3d122018-11-06 13:23:24 +00002416 struct pagevec pvec;
Chris Wilson4846bf02017-06-09 12:03:46 +01002417 gfp_t noreclaim;
Imre Deake2273302015-07-09 12:59:05 +03002418 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07002419
Chris Wilsone0ff7a72018-09-03 09:33:36 +01002420 /*
2421 * Assert that the object is not currently in any GPU domain. As it
Chris Wilson6c085a72012-08-20 11:40:46 +02002422 * wasn't in the GTT, there shouldn't be any way it could have been in
2423 * a GPU cache
2424 */
Christian Königc0a51fd2018-02-16 13:43:38 +01002425 GEM_BUG_ON(obj->read_domains & I915_GEM_GPU_DOMAINS);
2426 GEM_BUG_ON(obj->write_domain & I915_GEM_GPU_DOMAINS);
Chris Wilson6c085a72012-08-20 11:40:46 +02002427
Chris Wilsone0ff7a72018-09-03 09:33:36 +01002428 /*
2429 * If there's no chance of allocating enough pages for the whole
2430 * object, bail early.
2431 */
Arun KSca79b0c2018-12-28 00:34:29 -08002432 if (page_count > totalram_pages())
Chris Wilsone0ff7a72018-09-03 09:33:36 +01002433 return -ENOMEM;
2434
Chris Wilson9da3da62012-06-01 15:20:22 +01002435 st = kmalloc(sizeof(*st), GFP_KERNEL);
2436 if (st == NULL)
Matthew Auldb91b09e2017-10-06 23:18:17 +01002437 return -ENOMEM;
Eric Anholt673a3942008-07-30 12:06:12 -07002438
Chris Wilsond766ef52016-12-19 12:43:45 +00002439rebuild_st:
Chris Wilson9da3da62012-06-01 15:20:22 +01002440 if (sg_alloc_table(st, page_count, GFP_KERNEL)) {
Chris Wilson9da3da62012-06-01 15:20:22 +01002441 kfree(st);
Matthew Auldb91b09e2017-10-06 23:18:17 +01002442 return -ENOMEM;
Chris Wilson9da3da62012-06-01 15:20:22 +01002443 }
2444
Chris Wilsone0ff7a72018-09-03 09:33:36 +01002445 /*
2446 * Get the list of pages out of our struct file. They'll be pinned
Chris Wilson9da3da62012-06-01 15:20:22 +01002447 * at this point until we release them.
2448 *
2449 * Fail silently without starting the shrinker
2450 */
Al Viro93c76a32015-12-04 23:45:44 -05002451 mapping = obj->base.filp->f_mapping;
Kuo-Hsin Yang64e3d122018-11-06 13:23:24 +00002452 mapping_set_unevictable(mapping);
Chris Wilson0f6ab552017-06-09 12:03:48 +01002453 noreclaim = mapping_gfp_constraint(mapping, ~__GFP_RECLAIM);
Chris Wilson4846bf02017-06-09 12:03:46 +01002454 noreclaim |= __GFP_NORETRY | __GFP_NOWARN;
2455
Imre Deak90797e62013-02-18 19:28:03 +02002456 sg = st->sgl;
2457 st->nents = 0;
Matthew Auld84e89782017-10-09 12:00:24 +01002458 sg_page_sizes = 0;
Imre Deak90797e62013-02-18 19:28:03 +02002459 for (i = 0; i < page_count; i++) {
Chris Wilson4846bf02017-06-09 12:03:46 +01002460 const unsigned int shrink[] = {
2461 I915_SHRINK_BOUND | I915_SHRINK_UNBOUND | I915_SHRINK_PURGEABLE,
2462 0,
2463 }, *s = shrink;
2464 gfp_t gfp = noreclaim;
2465
2466 do {
Chris Wilsone6db7f42018-11-05 17:06:40 +00002467 cond_resched();
Chris Wilson6c085a72012-08-20 11:40:46 +02002468 page = shmem_read_mapping_page_gfp(mapping, i, gfp);
Chris Wilson4846bf02017-06-09 12:03:46 +01002469 if (likely(!IS_ERR(page)))
2470 break;
2471
2472 if (!*s) {
2473 ret = PTR_ERR(page);
2474 goto err_sg;
2475 }
2476
Chris Wilson912d5722017-09-06 16:19:30 -07002477 i915_gem_shrink(dev_priv, 2 * page_count, NULL, *s++);
Chris Wilson24f8e002017-03-22 11:05:21 +00002478
Chris Wilsone0ff7a72018-09-03 09:33:36 +01002479 /*
2480 * We've tried hard to allocate the memory by reaping
Chris Wilson6c085a72012-08-20 11:40:46 +02002481 * our own buffer, now let the real VM do its job and
2482 * go down in flames if truly OOM.
Chris Wilson24f8e002017-03-22 11:05:21 +00002483 *
2484 * However, since graphics tend to be disposable,
2485 * defer the oom here by reporting the ENOMEM back
2486 * to userspace.
Chris Wilson6c085a72012-08-20 11:40:46 +02002487 */
Chris Wilson4846bf02017-06-09 12:03:46 +01002488 if (!*s) {
2489 /* reclaim and warn, but no oom */
2490 gfp = mapping_gfp_mask(mapping);
Chris Wilsoneaf41802017-06-09 12:03:47 +01002491
Chris Wilsone0ff7a72018-09-03 09:33:36 +01002492 /*
2493 * Our bo are always dirty and so we require
Chris Wilsoneaf41802017-06-09 12:03:47 +01002494 * kswapd to reclaim our pages (direct reclaim
2495 * does not effectively begin pageout of our
2496 * buffers on its own). However, direct reclaim
2497 * only waits for kswapd when under allocation
2498 * congestion. So as a result __GFP_RECLAIM is
2499 * unreliable and fails to actually reclaim our
2500 * dirty pages -- unless you try over and over
2501 * again with !__GFP_NORETRY. However, we still
2502 * want to fail this allocation rather than
2503 * trigger the out-of-memory killer and for
Michal Hockodbb32952017-07-12 14:36:55 -07002504 * this we want __GFP_RETRY_MAYFAIL.
Chris Wilsoneaf41802017-06-09 12:03:47 +01002505 */
Michal Hockodbb32952017-07-12 14:36:55 -07002506 gfp |= __GFP_RETRY_MAYFAIL;
Imre Deake2273302015-07-09 12:59:05 +03002507 }
Chris Wilson4846bf02017-06-09 12:03:46 +01002508 } while (1);
2509
Chris Wilson871dfbd2016-10-11 09:20:21 +01002510 if (!i ||
2511 sg->length >= max_segment ||
2512 page_to_pfn(page) != last_pfn + 1) {
Matthew Aulda5c081662017-10-06 23:18:18 +01002513 if (i) {
Matthew Auld84e89782017-10-09 12:00:24 +01002514 sg_page_sizes |= sg->length;
Imre Deak90797e62013-02-18 19:28:03 +02002515 sg = sg_next(sg);
Matthew Aulda5c081662017-10-06 23:18:18 +01002516 }
Imre Deak90797e62013-02-18 19:28:03 +02002517 st->nents++;
2518 sg_set_page(sg, page, PAGE_SIZE, 0);
2519 } else {
2520 sg->length += PAGE_SIZE;
2521 }
2522 last_pfn = page_to_pfn(page);
Daniel Vetter3bbbe702013-10-07 17:15:45 -03002523
2524 /* Check that the i965g/gm workaround works. */
2525 WARN_ON((gfp & __GFP_DMA32) && (last_pfn >= 0x00100000UL));
Eric Anholt673a3942008-07-30 12:06:12 -07002526 }
Matthew Aulda5c081662017-10-06 23:18:18 +01002527 if (sg) { /* loop terminated early; short sg table */
Matthew Auld84e89782017-10-09 12:00:24 +01002528 sg_page_sizes |= sg->length;
Konrad Rzeszutek Wilk426729d2013-06-24 11:47:48 -04002529 sg_mark_end(sg);
Matthew Aulda5c081662017-10-06 23:18:18 +01002530 }
Chris Wilson74ce6b62012-10-19 15:51:06 +01002531
Tvrtko Ursulin0c40ce12016-11-09 15:13:43 +00002532 /* Trim unused sg entries to avoid wasting memory. */
2533 i915_sg_trim(st);
2534
Chris Wilson03ac84f2016-10-28 13:58:36 +01002535 ret = i915_gem_gtt_prepare_pages(obj, st);
Chris Wilsond766ef52016-12-19 12:43:45 +00002536 if (ret) {
Chris Wilsone0ff7a72018-09-03 09:33:36 +01002537 /*
2538 * DMA remapping failed? One possible cause is that
Chris Wilsond766ef52016-12-19 12:43:45 +00002539 * it could not reserve enough large entries, asking
2540 * for PAGE_SIZE chunks instead may be helpful.
2541 */
2542 if (max_segment > PAGE_SIZE) {
2543 for_each_sgt_page(page, sgt_iter, st)
2544 put_page(page);
2545 sg_free_table(st);
2546
2547 max_segment = PAGE_SIZE;
2548 goto rebuild_st;
2549 } else {
2550 dev_warn(&dev_priv->drm.pdev->dev,
2551 "Failed to DMA remap %lu pages\n",
2552 page_count);
2553 goto err_pages;
2554 }
2555 }
Imre Deake2273302015-07-09 12:59:05 +03002556
Eric Anholt673a3942008-07-30 12:06:12 -07002557 if (i915_gem_object_needs_bit17_swizzle(obj))
Chris Wilson03ac84f2016-10-28 13:58:36 +01002558 i915_gem_object_do_bit_17_swizzle(obj, st);
Eric Anholt673a3942008-07-30 12:06:12 -07002559
Matthew Auld84e89782017-10-09 12:00:24 +01002560 __i915_gem_object_set_pages(obj, st, sg_page_sizes);
Matthew Auldb91b09e2017-10-06 23:18:17 +01002561
2562 return 0;
Eric Anholt673a3942008-07-30 12:06:12 -07002563
Chris Wilsonb17993b2016-11-14 11:29:30 +00002564err_sg:
Imre Deak90797e62013-02-18 19:28:03 +02002565 sg_mark_end(sg);
Chris Wilsonb17993b2016-11-14 11:29:30 +00002566err_pages:
Kuo-Hsin Yang64e3d122018-11-06 13:23:24 +00002567 mapping_clear_unevictable(mapping);
2568 pagevec_init(&pvec);
2569 for_each_sgt_page(page, sgt_iter, st) {
2570 if (!pagevec_add(&pvec, page))
2571 check_release_pagevec(&pvec);
2572 }
2573 if (pagevec_count(&pvec))
2574 check_release_pagevec(&pvec);
Chris Wilson9da3da62012-06-01 15:20:22 +01002575 sg_free_table(st);
2576 kfree(st);
Chris Wilson0820baf2014-03-25 13:23:03 +00002577
Chris Wilsone0ff7a72018-09-03 09:33:36 +01002578 /*
2579 * shmemfs first checks if there is enough memory to allocate the page
Chris Wilson0820baf2014-03-25 13:23:03 +00002580 * and reports ENOSPC should there be insufficient, along with the usual
2581 * ENOMEM for a genuine allocation failure.
2582 *
2583 * We use ENOSPC in our driver to mean that we have run out of aperture
2584 * space and so want to translate the error from shmemfs back to our
2585 * usual understanding of ENOMEM.
2586 */
Imre Deake2273302015-07-09 12:59:05 +03002587 if (ret == -ENOSPC)
2588 ret = -ENOMEM;
2589
Matthew Auldb91b09e2017-10-06 23:18:17 +01002590 return ret;
Chris Wilson03ac84f2016-10-28 13:58:36 +01002591}
2592
2593void __i915_gem_object_set_pages(struct drm_i915_gem_object *obj,
Matthew Aulda5c081662017-10-06 23:18:18 +01002594 struct sg_table *pages,
Matthew Auld84e89782017-10-09 12:00:24 +01002595 unsigned int sg_page_sizes)
Chris Wilson03ac84f2016-10-28 13:58:36 +01002596{
Matthew Aulda5c081662017-10-06 23:18:18 +01002597 struct drm_i915_private *i915 = to_i915(obj->base.dev);
2598 unsigned long supported = INTEL_INFO(i915)->page_sizes;
2599 int i;
2600
Chris Wilson1233e2d2016-10-28 13:58:37 +01002601 lockdep_assert_held(&obj->mm.lock);
Chris Wilson03ac84f2016-10-28 13:58:36 +01002602
2603 obj->mm.get_page.sg_pos = pages->sgl;
2604 obj->mm.get_page.sg_idx = 0;
2605
2606 obj->mm.pages = pages;
Chris Wilson2c3a3f42016-11-04 10:30:01 +00002607
2608 if (i915_gem_object_is_tiled(obj) &&
Chris Wilsonf2123812017-10-16 12:40:37 +01002609 i915->quirks & QUIRK_PIN_SWIZZLED_PAGES) {
Chris Wilson2c3a3f42016-11-04 10:30:01 +00002610 GEM_BUG_ON(obj->mm.quirked);
2611 __i915_gem_object_pin_pages(obj);
2612 obj->mm.quirked = true;
2613 }
Matthew Aulda5c081662017-10-06 23:18:18 +01002614
Matthew Auld84e89782017-10-09 12:00:24 +01002615 GEM_BUG_ON(!sg_page_sizes);
2616 obj->mm.page_sizes.phys = sg_page_sizes;
Matthew Aulda5c081662017-10-06 23:18:18 +01002617
2618 /*
Matthew Auld84e89782017-10-09 12:00:24 +01002619 * Calculate the supported page-sizes which fit into the given
2620 * sg_page_sizes. This will give us the page-sizes which we may be able
2621 * to use opportunistically when later inserting into the GTT. For
2622 * example if phys=2G, then in theory we should be able to use 1G, 2M,
2623 * 64K or 4K pages, although in practice this will depend on a number of
2624 * other factors.
Matthew Aulda5c081662017-10-06 23:18:18 +01002625 */
2626 obj->mm.page_sizes.sg = 0;
2627 for_each_set_bit(i, &supported, ilog2(I915_GTT_MAX_PAGE_SIZE) + 1) {
2628 if (obj->mm.page_sizes.phys & ~0u << i)
2629 obj->mm.page_sizes.sg |= BIT(i);
2630 }
Matthew Aulda5c081662017-10-06 23:18:18 +01002631 GEM_BUG_ON(!HAS_PAGE_SIZES(i915, obj->mm.page_sizes.sg));
Chris Wilsonf2123812017-10-16 12:40:37 +01002632
2633 spin_lock(&i915->mm.obj_lock);
2634 list_add(&obj->mm.link, &i915->mm.unbound_list);
2635 spin_unlock(&i915->mm.obj_lock);
Chris Wilson03ac84f2016-10-28 13:58:36 +01002636}
2637
2638static int ____i915_gem_object_get_pages(struct drm_i915_gem_object *obj)
2639{
Matthew Auldb91b09e2017-10-06 23:18:17 +01002640 int err;
Chris Wilson03ac84f2016-10-28 13:58:36 +01002641
2642 if (unlikely(obj->mm.madv != I915_MADV_WILLNEED)) {
2643 DRM_DEBUG("Attempting to obtain a purgeable object\n");
2644 return -EFAULT;
2645 }
2646
Matthew Auldb91b09e2017-10-06 23:18:17 +01002647 err = obj->ops->get_pages(obj);
Matthew Auldb65a9b92017-12-18 10:38:55 +00002648 GEM_BUG_ON(!err && !i915_gem_object_has_pages(obj));
Chris Wilson03ac84f2016-10-28 13:58:36 +01002649
Matthew Auldb91b09e2017-10-06 23:18:17 +01002650 return err;
Eric Anholt673a3942008-07-30 12:06:12 -07002651}
2652
Chris Wilson37e680a2012-06-07 15:38:42 +01002653/* Ensure that the associated pages are gathered from the backing storage
Chris Wilson1233e2d2016-10-28 13:58:37 +01002654 * and pinned into our object. i915_gem_object_pin_pages() may be called
Chris Wilson37e680a2012-06-07 15:38:42 +01002655 * multiple times before they are released by a single call to
Chris Wilson1233e2d2016-10-28 13:58:37 +01002656 * i915_gem_object_unpin_pages() - once the pages are no longer referenced
Chris Wilson37e680a2012-06-07 15:38:42 +01002657 * either as a result of memory pressure (reaping pages under the shrinker)
2658 * or as the object is itself released.
2659 */
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002660int __i915_gem_object_get_pages(struct drm_i915_gem_object *obj)
Chris Wilson37e680a2012-06-07 15:38:42 +01002661{
Chris Wilson03ac84f2016-10-28 13:58:36 +01002662 int err;
Chris Wilson37e680a2012-06-07 15:38:42 +01002663
Chris Wilson1233e2d2016-10-28 13:58:37 +01002664 err = mutex_lock_interruptible(&obj->mm.lock);
2665 if (err)
2666 return err;
Chris Wilson4c7d62c2016-10-28 13:58:32 +01002667
Chris Wilsonf1fa4f42017-10-13 21:26:13 +01002668 if (unlikely(!i915_gem_object_has_pages(obj))) {
Chris Wilson88c880b2017-09-06 14:52:20 +01002669 GEM_BUG_ON(i915_gem_object_has_pinned_pages(obj));
2670
Chris Wilson2c3a3f42016-11-04 10:30:01 +00002671 err = ____i915_gem_object_get_pages(obj);
2672 if (err)
2673 goto unlock;
2674
2675 smp_mb__before_atomic();
Chris Wilson1233e2d2016-10-28 13:58:37 +01002676 }
Chris Wilson2c3a3f42016-11-04 10:30:01 +00002677 atomic_inc(&obj->mm.pages_pin_count);
Chris Wilson43e28f02013-01-08 10:53:09 +00002678
Chris Wilson1233e2d2016-10-28 13:58:37 +01002679unlock:
2680 mutex_unlock(&obj->mm.lock);
Chris Wilson03ac84f2016-10-28 13:58:36 +01002681 return err;
Eric Anholt673a3942008-07-30 12:06:12 -07002682}
2683
Dave Gordondd6034c2016-05-20 11:54:04 +01002684/* The 'mapping' part of i915_gem_object_pin_map() below */
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002685static void *i915_gem_object_map(const struct drm_i915_gem_object *obj,
2686 enum i915_map_type type)
Dave Gordondd6034c2016-05-20 11:54:04 +01002687{
2688 unsigned long n_pages = obj->base.size >> PAGE_SHIFT;
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002689 struct sg_table *sgt = obj->mm.pages;
Dave Gordon85d12252016-05-20 11:54:06 +01002690 struct sgt_iter sgt_iter;
2691 struct page *page;
Dave Gordonb338fa42016-05-20 11:54:05 +01002692 struct page *stack_pages[32];
2693 struct page **pages = stack_pages;
Dave Gordondd6034c2016-05-20 11:54:04 +01002694 unsigned long i = 0;
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002695 pgprot_t pgprot;
Dave Gordondd6034c2016-05-20 11:54:04 +01002696 void *addr;
2697
2698 /* A single page can always be kmapped */
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002699 if (n_pages == 1 && type == I915_MAP_WB)
Dave Gordondd6034c2016-05-20 11:54:04 +01002700 return kmap(sg_page(sgt->sgl));
2701
Dave Gordonb338fa42016-05-20 11:54:05 +01002702 if (n_pages > ARRAY_SIZE(stack_pages)) {
2703 /* Too big for stack -- allocate temporary array instead */
Michal Hocko0ee931c2017-09-13 16:28:29 -07002704 pages = kvmalloc_array(n_pages, sizeof(*pages), GFP_KERNEL);
Dave Gordonb338fa42016-05-20 11:54:05 +01002705 if (!pages)
2706 return NULL;
2707 }
Dave Gordondd6034c2016-05-20 11:54:04 +01002708
Dave Gordon85d12252016-05-20 11:54:06 +01002709 for_each_sgt_page(page, sgt_iter, sgt)
2710 pages[i++] = page;
Dave Gordondd6034c2016-05-20 11:54:04 +01002711
2712 /* Check that we have the expected number of pages */
2713 GEM_BUG_ON(i != n_pages);
2714
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002715 switch (type) {
Chris Wilsona575c672017-08-28 11:46:31 +01002716 default:
2717 MISSING_CASE(type);
2718 /* fallthrough to use PAGE_KERNEL anyway */
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002719 case I915_MAP_WB:
2720 pgprot = PAGE_KERNEL;
2721 break;
2722 case I915_MAP_WC:
2723 pgprot = pgprot_writecombine(PAGE_KERNEL_IO);
2724 break;
2725 }
2726 addr = vmap(pages, n_pages, 0, pgprot);
Dave Gordondd6034c2016-05-20 11:54:04 +01002727
Dave Gordonb338fa42016-05-20 11:54:05 +01002728 if (pages != stack_pages)
Michal Hocko20981052017-05-17 14:23:12 +02002729 kvfree(pages);
Dave Gordondd6034c2016-05-20 11:54:04 +01002730
2731 return addr;
2732}
2733
2734/* get, pin, and map the pages of the object into kernel space */
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002735void *i915_gem_object_pin_map(struct drm_i915_gem_object *obj,
2736 enum i915_map_type type)
Chris Wilson0a798eb2016-04-08 12:11:11 +01002737{
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002738 enum i915_map_type has_type;
2739 bool pinned;
2740 void *ptr;
Chris Wilson0a798eb2016-04-08 12:11:11 +01002741 int ret;
2742
Tina Zhanga03f3952017-11-14 10:25:13 +00002743 if (unlikely(!i915_gem_object_has_struct_page(obj)))
2744 return ERR_PTR(-ENXIO);
Chris Wilson0a798eb2016-04-08 12:11:11 +01002745
Chris Wilson1233e2d2016-10-28 13:58:37 +01002746 ret = mutex_lock_interruptible(&obj->mm.lock);
Chris Wilson0a798eb2016-04-08 12:11:11 +01002747 if (ret)
2748 return ERR_PTR(ret);
2749
Chris Wilsona575c672017-08-28 11:46:31 +01002750 pinned = !(type & I915_MAP_OVERRIDE);
2751 type &= ~I915_MAP_OVERRIDE;
2752
Chris Wilson1233e2d2016-10-28 13:58:37 +01002753 if (!atomic_inc_not_zero(&obj->mm.pages_pin_count)) {
Chris Wilsonf1fa4f42017-10-13 21:26:13 +01002754 if (unlikely(!i915_gem_object_has_pages(obj))) {
Chris Wilson88c880b2017-09-06 14:52:20 +01002755 GEM_BUG_ON(i915_gem_object_has_pinned_pages(obj));
2756
Chris Wilson2c3a3f42016-11-04 10:30:01 +00002757 ret = ____i915_gem_object_get_pages(obj);
2758 if (ret)
2759 goto err_unlock;
Chris Wilson1233e2d2016-10-28 13:58:37 +01002760
Chris Wilson2c3a3f42016-11-04 10:30:01 +00002761 smp_mb__before_atomic();
2762 }
2763 atomic_inc(&obj->mm.pages_pin_count);
Chris Wilson1233e2d2016-10-28 13:58:37 +01002764 pinned = false;
2765 }
Chris Wilsonf1fa4f42017-10-13 21:26:13 +01002766 GEM_BUG_ON(!i915_gem_object_has_pages(obj));
Chris Wilson0a798eb2016-04-08 12:11:11 +01002767
Chris Wilson0ce81782017-05-17 13:09:59 +01002768 ptr = page_unpack_bits(obj->mm.mapping, &has_type);
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002769 if (ptr && has_type != type) {
2770 if (pinned) {
2771 ret = -EBUSY;
Chris Wilson1233e2d2016-10-28 13:58:37 +01002772 goto err_unpin;
Chris Wilson0a798eb2016-04-08 12:11:11 +01002773 }
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002774
2775 if (is_vmalloc_addr(ptr))
2776 vunmap(ptr);
2777 else
2778 kunmap(kmap_to_page(ptr));
2779
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002780 ptr = obj->mm.mapping = NULL;
Chris Wilson0a798eb2016-04-08 12:11:11 +01002781 }
2782
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002783 if (!ptr) {
2784 ptr = i915_gem_object_map(obj, type);
2785 if (!ptr) {
2786 ret = -ENOMEM;
Chris Wilson1233e2d2016-10-28 13:58:37 +01002787 goto err_unpin;
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002788 }
2789
Chris Wilson0ce81782017-05-17 13:09:59 +01002790 obj->mm.mapping = page_pack_bits(ptr, type);
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002791 }
2792
Chris Wilson1233e2d2016-10-28 13:58:37 +01002793out_unlock:
2794 mutex_unlock(&obj->mm.lock);
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002795 return ptr;
2796
Chris Wilson1233e2d2016-10-28 13:58:37 +01002797err_unpin:
2798 atomic_dec(&obj->mm.pages_pin_count);
2799err_unlock:
2800 ptr = ERR_PTR(ret);
2801 goto out_unlock;
Chris Wilson0a798eb2016-04-08 12:11:11 +01002802}
2803
Chris Wilson7c55e2c2017-03-07 12:03:38 +00002804static int
2805i915_gem_object_pwrite_gtt(struct drm_i915_gem_object *obj,
2806 const struct drm_i915_gem_pwrite *arg)
2807{
2808 struct address_space *mapping = obj->base.filp->f_mapping;
2809 char __user *user_data = u64_to_user_ptr(arg->data_ptr);
2810 u64 remain, offset;
2811 unsigned int pg;
2812
2813 /* Before we instantiate/pin the backing store for our use, we
2814 * can prepopulate the shmemfs filp efficiently using a write into
2815 * the pagecache. We avoid the penalty of instantiating all the
2816 * pages, important if the user is just writing to a few and never
2817 * uses the object on the GPU, and using a direct write into shmemfs
2818 * allows it to avoid the cost of retrieving a page (either swapin
2819 * or clearing-before-use) before it is overwritten.
2820 */
Chris Wilsonf1fa4f42017-10-13 21:26:13 +01002821 if (i915_gem_object_has_pages(obj))
Chris Wilson7c55e2c2017-03-07 12:03:38 +00002822 return -ENODEV;
2823
Chris Wilsona6d65e42017-10-16 21:27:32 +01002824 if (obj->mm.madv != I915_MADV_WILLNEED)
2825 return -EFAULT;
2826
Chris Wilson7c55e2c2017-03-07 12:03:38 +00002827 /* Before the pages are instantiated the object is treated as being
2828 * in the CPU domain. The pages will be clflushed as required before
2829 * use, and we can freely write into the pages directly. If userspace
2830 * races pwrite with any other operation; corruption will ensue -
2831 * that is userspace's prerogative!
2832 */
2833
2834 remain = arg->size;
2835 offset = arg->offset;
2836 pg = offset_in_page(offset);
2837
2838 do {
2839 unsigned int len, unwritten;
2840 struct page *page;
2841 void *data, *vaddr;
2842 int err;
2843
2844 len = PAGE_SIZE - pg;
2845 if (len > remain)
2846 len = remain;
2847
2848 err = pagecache_write_begin(obj->base.filp, mapping,
2849 offset, len, 0,
2850 &page, &data);
2851 if (err < 0)
2852 return err;
2853
2854 vaddr = kmap(page);
2855 unwritten = copy_from_user(vaddr + pg, user_data, len);
2856 kunmap(page);
2857
2858 err = pagecache_write_end(obj->base.filp, mapping,
2859 offset, len, len - unwritten,
2860 page, data);
2861 if (err < 0)
2862 return err;
2863
2864 if (unwritten)
2865 return -EFAULT;
2866
2867 remain -= len;
2868 user_data += len;
2869 offset += len;
2870 pg = 0;
2871 } while (remain);
2872
2873 return 0;
2874}
2875
Mika Kuoppala14921f32018-06-15 13:44:29 +03002876static void i915_gem_client_mark_guilty(struct drm_i915_file_private *file_priv,
2877 const struct i915_gem_context *ctx)
2878{
2879 unsigned int score;
2880 unsigned long prev_hang;
2881
2882 if (i915_gem_context_is_banned(ctx))
2883 score = I915_CLIENT_SCORE_CONTEXT_BAN;
2884 else
2885 score = 0;
2886
2887 prev_hang = xchg(&file_priv->hang_timestamp, jiffies);
2888 if (time_before(jiffies, prev_hang + I915_CLIENT_FAST_HANG_JIFFIES))
2889 score += I915_CLIENT_SCORE_HANG_FAST;
2890
2891 if (score) {
2892 atomic_add(score, &file_priv->ban_score);
2893
2894 DRM_DEBUG_DRIVER("client %s: gained %u ban score, now %u\n",
2895 ctx->name, score,
2896 atomic_read(&file_priv->ban_score));
2897 }
2898}
2899
Mika Kuoppalae5e1fc42016-11-16 17:20:31 +02002900static void i915_gem_context_mark_guilty(struct i915_gem_context *ctx)
Mika Kuoppalaaa60c662013-06-12 15:13:20 +03002901{
Mika Kuoppala14921f32018-06-15 13:44:29 +03002902 unsigned int score;
2903 bool banned, bannable;
Mika Kuoppalab083a082016-11-18 15:10:47 +02002904
Chris Wilson77b25a92017-07-21 13:32:30 +01002905 atomic_inc(&ctx->guilty_count);
2906
Mika Kuoppala14921f32018-06-15 13:44:29 +03002907 bannable = i915_gem_context_is_bannable(ctx);
2908 score = atomic_add_return(CONTEXT_SCORE_GUILTY, &ctx->ban_score);
2909 banned = score >= CONTEXT_SCORE_BAN_THRESHOLD;
Chris Wilson24eae082018-02-05 09:22:01 +00002910
Mika Kuoppala14921f32018-06-15 13:44:29 +03002911 /* Cool contexts don't accumulate client ban score */
2912 if (!bannable)
Mika Kuoppalab083a082016-11-18 15:10:47 +02002913 return;
2914
Chris Wilsonbcc26612018-06-18 08:31:35 +01002915 if (banned) {
2916 DRM_DEBUG_DRIVER("context %s: guilty %d, score %u, banned\n",
2917 ctx->name, atomic_read(&ctx->guilty_count),
2918 score);
Mika Kuoppala14921f32018-06-15 13:44:29 +03002919 i915_gem_context_set_banned(ctx);
Chris Wilsonbcc26612018-06-18 08:31:35 +01002920 }
Mika Kuoppala14921f32018-06-15 13:44:29 +03002921
2922 if (!IS_ERR_OR_NULL(ctx->file_priv))
2923 i915_gem_client_mark_guilty(ctx->file_priv, ctx);
Mika Kuoppalae5e1fc42016-11-16 17:20:31 +02002924}
2925
2926static void i915_gem_context_mark_innocent(struct i915_gem_context *ctx)
2927{
Chris Wilson77b25a92017-07-21 13:32:30 +01002928 atomic_inc(&ctx->active_count);
Mika Kuoppalaaa60c662013-06-12 15:13:20 +03002929}
2930
Chris Wilsone61e0f52018-02-21 09:56:36 +00002931struct i915_request *
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00002932i915_gem_find_active_request(struct intel_engine_cs *engine)
Chris Wilson9375e442010-09-19 12:21:28 +01002933{
Chris Wilsone61e0f52018-02-21 09:56:36 +00002934 struct i915_request *request, *active = NULL;
Chris Wilson754c9fd2017-02-23 07:44:14 +00002935 unsigned long flags;
Mika Kuoppalaaa60c662013-06-12 15:13:20 +03002936
Chris Wilsoncc7cc532018-05-29 14:29:18 +01002937 /*
2938 * We are called by the error capture, reset and to dump engine
2939 * state at random points in time. In particular, note that neither is
2940 * crucially ordered with an interrupt. After a hang, the GPU is dead
2941 * and we assume that no more writes can happen (we waited long enough
2942 * for all writes that were in transaction to be flushed) - adding an
Chris Wilsonf69a02c2016-07-01 17:23:16 +01002943 * extra delay for a recent interrupt is pointless. Hence, we do
2944 * not need an engine->irq_seqno_barrier() before the seqno reads.
Chris Wilsoncc7cc532018-05-29 14:29:18 +01002945 * At all other times, we must assume the GPU is still running, but
2946 * we only care about the snapshot of this moment.
Chris Wilsonf69a02c2016-07-01 17:23:16 +01002947 */
Chris Wilsona89d1f92018-05-02 17:38:39 +01002948 spin_lock_irqsave(&engine->timeline.lock, flags);
2949 list_for_each_entry(request, &engine->timeline.requests, link) {
Chris Wilsone61e0f52018-02-21 09:56:36 +00002950 if (__i915_request_completed(request, request->global_seqno))
Chris Wilson4db080f2013-12-04 11:37:09 +00002951 continue;
Mika Kuoppalaaa60c662013-06-12 15:13:20 +03002952
Chris Wilson754c9fd2017-02-23 07:44:14 +00002953 active = request;
2954 break;
2955 }
Chris Wilsona89d1f92018-05-02 17:38:39 +01002956 spin_unlock_irqrestore(&engine->timeline.lock, flags);
Chris Wilson754c9fd2017-02-23 07:44:14 +00002957
2958 return active;
Mika Kuoppalab6b0fac2014-01-30 19:04:43 +02002959}
2960
Michel Thierrya1ef70e2017-06-20 10:57:47 +01002961/*
2962 * Ensure irq handler finishes, and not run again.
2963 * Also return the active request so that we only search for it once.
2964 */
Chris Wilsone61e0f52018-02-21 09:56:36 +00002965struct i915_request *
Michel Thierrya1ef70e2017-06-20 10:57:47 +01002966i915_gem_reset_prepare_engine(struct intel_engine_cs *engine)
2967{
Chris Wilson5adfb772018-05-16 19:33:51 +01002968 struct i915_request *request;
Michel Thierrya1ef70e2017-06-20 10:57:47 +01002969
Chris Wilson1749d902017-10-09 12:02:59 +01002970 /*
2971 * During the reset sequence, we must prevent the engine from
2972 * entering RC6. As the context state is undefined until we restart
2973 * the engine, if it does enter RC6 during the reset, the state
2974 * written to the powercontext is undefined and so we may lose
2975 * GPU state upon resume, i.e. fail to restart after a reset.
2976 */
2977 intel_uncore_forcewake_get(engine->i915, FORCEWAKE_ALL);
2978
Chris Wilson5adfb772018-05-16 19:33:51 +01002979 request = engine->reset.prepare(engine);
Chris Wilsond1d1ebf42017-07-21 13:32:33 +01002980 if (request && request->fence.error == -EIO)
2981 request = ERR_PTR(-EIO); /* Previous reset failed! */
Michel Thierrya1ef70e2017-06-20 10:57:47 +01002982
2983 return request;
2984}
2985
Chris Wilson0e178ae2017-01-17 17:59:06 +02002986int i915_gem_reset_prepare(struct drm_i915_private *dev_priv)
Chris Wilson4c965542017-01-17 17:59:01 +02002987{
2988 struct intel_engine_cs *engine;
Chris Wilsone61e0f52018-02-21 09:56:36 +00002989 struct i915_request *request;
Chris Wilson4c965542017-01-17 17:59:01 +02002990 enum intel_engine_id id;
Chris Wilson0e178ae2017-01-17 17:59:06 +02002991 int err = 0;
Chris Wilson4c965542017-01-17 17:59:01 +02002992
Chris Wilson0e178ae2017-01-17 17:59:06 +02002993 for_each_engine(engine, dev_priv, id) {
Michel Thierrya1ef70e2017-06-20 10:57:47 +01002994 request = i915_gem_reset_prepare_engine(engine);
2995 if (IS_ERR(request)) {
2996 err = PTR_ERR(request);
2997 continue;
Chris Wilson0e178ae2017-01-17 17:59:06 +02002998 }
Michel Thierryc64992e2017-06-20 10:57:44 +01002999
3000 engine->hangcheck.active_request = request;
Chris Wilson0e178ae2017-01-17 17:59:06 +02003001 }
3002
Chris Wilson4c965542017-01-17 17:59:01 +02003003 i915_gem_revoke_fences(dev_priv);
Michal Wajdeczkoc37d5722018-03-12 13:03:07 +00003004 intel_uc_sanitize(dev_priv);
Chris Wilson0e178ae2017-01-17 17:59:06 +02003005
3006 return err;
Chris Wilson4c965542017-01-17 17:59:01 +02003007}
3008
Chris Wilsone61e0f52018-02-21 09:56:36 +00003009static void engine_skip_context(struct i915_request *request)
Mika Kuoppala36193ac2017-01-17 17:59:02 +02003010{
3011 struct intel_engine_cs *engine = request->engine;
Chris Wilson4e0d64d2018-05-17 22:26:30 +01003012 struct i915_gem_context *hung_ctx = request->gem_context;
Chris Wilsona89d1f92018-05-02 17:38:39 +01003013 struct i915_timeline *timeline = request->timeline;
Mika Kuoppala36193ac2017-01-17 17:59:02 +02003014 unsigned long flags;
3015
Chris Wilsona89d1f92018-05-02 17:38:39 +01003016 GEM_BUG_ON(timeline == &engine->timeline);
Mika Kuoppala36193ac2017-01-17 17:59:02 +02003017
Chris Wilsona89d1f92018-05-02 17:38:39 +01003018 spin_lock_irqsave(&engine->timeline.lock, flags);
Chris Wilson890fd182018-07-06 22:07:10 +01003019 spin_lock(&timeline->lock);
Mika Kuoppala36193ac2017-01-17 17:59:02 +02003020
Chris Wilsona89d1f92018-05-02 17:38:39 +01003021 list_for_each_entry_continue(request, &engine->timeline.requests, link)
Chris Wilson4e0d64d2018-05-17 22:26:30 +01003022 if (request->gem_context == hung_ctx)
Chris Wilson6dd75262018-07-06 11:39:43 +01003023 i915_request_skip(request, -EIO);
Mika Kuoppala36193ac2017-01-17 17:59:02 +02003024
3025 list_for_each_entry(request, &timeline->requests, link)
Chris Wilson6dd75262018-07-06 11:39:43 +01003026 i915_request_skip(request, -EIO);
Mika Kuoppala36193ac2017-01-17 17:59:02 +02003027
3028 spin_unlock(&timeline->lock);
Chris Wilsona89d1f92018-05-02 17:38:39 +01003029 spin_unlock_irqrestore(&engine->timeline.lock, flags);
Mika Kuoppala36193ac2017-01-17 17:59:02 +02003030}
3031
Chris Wilsond1d1ebf42017-07-21 13:32:33 +01003032/* Returns the request if it was guilty of the hang */
Chris Wilsone61e0f52018-02-21 09:56:36 +00003033static struct i915_request *
Chris Wilsond1d1ebf42017-07-21 13:32:33 +01003034i915_gem_reset_request(struct intel_engine_cs *engine,
Chris Wilsonbba08692018-04-06 23:03:53 +01003035 struct i915_request *request,
3036 bool stalled)
Mika Kuoppala61da5362017-01-17 17:59:05 +02003037{
Mika Kuoppala71895a02017-01-17 17:59:07 +02003038 /* The guilty request will get skipped on a hung engine.
3039 *
3040 * Users of client default contexts do not rely on logical
3041 * state preserved between batches so it is safe to execute
3042 * queued requests following the hang. Non default contexts
3043 * rely on preserved state, so skipping a batch loses the
3044 * evolution of the state and it needs to be considered corrupted.
3045 * Executing more queued batches on top of corrupted state is
3046 * risky. But we take the risk by trying to advance through
3047 * the queued requests in order to make the client behaviour
3048 * more predictable around resets, by not throwing away random
3049 * amount of batches it has prepared for execution. Sophisticated
3050 * clients can use gem_reset_stats_ioctl and dma fence status
3051 * (exported via sync_file info ioctl on explicit fences) to observe
3052 * when it loses the context state and should rebuild accordingly.
3053 *
3054 * The context ban, and ultimately the client ban, mechanism are safety
3055 * valves if client submission ends up resulting in nothing more than
3056 * subsequent hangs.
3057 */
3058
Chris Wilsonbba08692018-04-06 23:03:53 +01003059 if (i915_request_completed(request)) {
3060 GEM_TRACE("%s pardoned global=%d (fence %llx:%d), current %d\n",
3061 engine->name, request->global_seqno,
3062 request->fence.context, request->fence.seqno,
3063 intel_engine_get_seqno(engine));
3064 stalled = false;
3065 }
3066
3067 if (stalled) {
Chris Wilson4e0d64d2018-05-17 22:26:30 +01003068 i915_gem_context_mark_guilty(request->gem_context);
Chris Wilson6dd75262018-07-06 11:39:43 +01003069 i915_request_skip(request, -EIO);
Chris Wilsond1d1ebf42017-07-21 13:32:33 +01003070
3071 /* If this context is now banned, skip all pending requests. */
Chris Wilson4e0d64d2018-05-17 22:26:30 +01003072 if (i915_gem_context_is_banned(request->gem_context))
Chris Wilsond1d1ebf42017-07-21 13:32:33 +01003073 engine_skip_context(request);
Mika Kuoppala61da5362017-01-17 17:59:05 +02003074 } else {
Chris Wilsond1d1ebf42017-07-21 13:32:33 +01003075 /*
3076 * Since this is not the hung engine, it may have advanced
3077 * since the hang declaration. Double check by refinding
3078 * the active request at the time of the reset.
3079 */
3080 request = i915_gem_find_active_request(engine);
3081 if (request) {
Chris Wilson042ed2d2018-06-15 10:31:36 +01003082 unsigned long flags;
3083
Chris Wilson4e0d64d2018-05-17 22:26:30 +01003084 i915_gem_context_mark_innocent(request->gem_context);
Chris Wilsond1d1ebf42017-07-21 13:32:33 +01003085 dma_fence_set_error(&request->fence, -EAGAIN);
3086
3087 /* Rewind the engine to replay the incomplete rq */
Chris Wilson042ed2d2018-06-15 10:31:36 +01003088 spin_lock_irqsave(&engine->timeline.lock, flags);
Chris Wilsond1d1ebf42017-07-21 13:32:33 +01003089 request = list_prev_entry(request, link);
Chris Wilsona89d1f92018-05-02 17:38:39 +01003090 if (&request->link == &engine->timeline.requests)
Chris Wilsond1d1ebf42017-07-21 13:32:33 +01003091 request = NULL;
Chris Wilson042ed2d2018-06-15 10:31:36 +01003092 spin_unlock_irqrestore(&engine->timeline.lock, flags);
Chris Wilsond1d1ebf42017-07-21 13:32:33 +01003093 }
Mika Kuoppala61da5362017-01-17 17:59:05 +02003094 }
3095
Chris Wilsond1d1ebf42017-07-21 13:32:33 +01003096 return request;
Mika Kuoppala61da5362017-01-17 17:59:05 +02003097}
3098
Michel Thierrya1ef70e2017-06-20 10:57:47 +01003099void i915_gem_reset_engine(struct intel_engine_cs *engine,
Chris Wilsonbba08692018-04-06 23:03:53 +01003100 struct i915_request *request,
3101 bool stalled)
Chris Wilson4db080f2013-12-04 11:37:09 +00003102{
Chris Wilsond1d1ebf42017-07-21 13:32:33 +01003103 if (request)
Chris Wilsonbba08692018-04-06 23:03:53 +01003104 request = i915_gem_reset_request(engine, request, stalled);
Chris Wilsond1d1ebf42017-07-21 13:32:33 +01003105
Chris Wilson821ed7d2016-09-09 14:11:53 +01003106 /* Setup the CS to resume from the breadcrumb of the hung request */
Chris Wilson5adfb772018-05-16 19:33:51 +01003107 engine->reset.reset(engine, request);
Chris Wilson821ed7d2016-09-09 14:11:53 +01003108}
3109
Chris Wilsond0667e92018-04-06 23:03:54 +01003110void i915_gem_reset(struct drm_i915_private *dev_priv,
3111 unsigned int stalled_mask)
Chris Wilson821ed7d2016-09-09 14:11:53 +01003112{
3113 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05303114 enum intel_engine_id id;
Chris Wilson821ed7d2016-09-09 14:11:53 +01003115
Chris Wilson4c7d62c2016-10-28 13:58:32 +01003116 lockdep_assert_held(&dev_priv->drm.struct_mutex);
3117
Chris Wilsone61e0f52018-02-21 09:56:36 +00003118 i915_retire_requests(dev_priv);
Chris Wilson821ed7d2016-09-09 14:11:53 +01003119
Chris Wilson2ae55732017-02-12 17:20:02 +00003120 for_each_engine(engine, dev_priv, id) {
Chris Wilson1fc44d92018-05-17 22:26:32 +01003121 struct intel_context *ce;
Chris Wilson2ae55732017-02-12 17:20:02 +00003122
Chris Wilsonbba08692018-04-06 23:03:53 +01003123 i915_gem_reset_engine(engine,
3124 engine->hangcheck.active_request,
Chris Wilsond0667e92018-04-06 23:03:54 +01003125 stalled_mask & ENGINE_MASK(id));
Chris Wilson1fc44d92018-05-17 22:26:32 +01003126 ce = fetch_and_zero(&engine->last_retired_context);
3127 if (ce)
3128 intel_context_unpin(ce);
Chris Wilson7b6da812017-12-16 00:03:34 +00003129
3130 /*
3131 * Ostensibily, we always want a context loaded for powersaving,
3132 * so if the engine is idle after the reset, send a request
3133 * to load our scratch kernel_context.
3134 *
3135 * More mysteriously, if we leave the engine idle after a reset,
3136 * the next userspace batch may hang, with what appears to be
3137 * an incoherent read by the CS (presumably stale TLB). An
3138 * empty request appears sufficient to paper over the glitch.
3139 */
Chris Wilson01b8fdc2018-02-05 15:24:31 +00003140 if (intel_engine_is_idle(engine)) {
Chris Wilsone61e0f52018-02-21 09:56:36 +00003141 struct i915_request *rq;
Chris Wilson7b6da812017-12-16 00:03:34 +00003142
Chris Wilsone61e0f52018-02-21 09:56:36 +00003143 rq = i915_request_alloc(engine,
3144 dev_priv->kernel_context);
Chris Wilson7b6da812017-12-16 00:03:34 +00003145 if (!IS_ERR(rq))
Chris Wilson697b9a82018-06-12 11:51:35 +01003146 i915_request_add(rq);
Chris Wilson7b6da812017-12-16 00:03:34 +00003147 }
Chris Wilson2ae55732017-02-12 17:20:02 +00003148 }
Chris Wilson821ed7d2016-09-09 14:11:53 +01003149
Tvrtko Ursulin4362f4f2016-11-16 08:55:33 +00003150 i915_gem_restore_fences(dev_priv);
Chris Wilson821ed7d2016-09-09 14:11:53 +01003151}
3152
Michel Thierrya1ef70e2017-06-20 10:57:47 +01003153void i915_gem_reset_finish_engine(struct intel_engine_cs *engine)
3154{
Chris Wilson5adfb772018-05-16 19:33:51 +01003155 engine->reset.finish(engine);
3156
Chris Wilson1749d902017-10-09 12:02:59 +01003157 intel_uncore_forcewake_put(engine->i915, FORCEWAKE_ALL);
Michel Thierrya1ef70e2017-06-20 10:57:47 +01003158}
3159
Chris Wilsond8027092017-02-08 14:30:32 +00003160void i915_gem_reset_finish(struct drm_i915_private *dev_priv)
3161{
Chris Wilson1f7b8472017-02-08 14:30:33 +00003162 struct intel_engine_cs *engine;
3163 enum intel_engine_id id;
3164
Chris Wilsond8027092017-02-08 14:30:32 +00003165 lockdep_assert_held(&dev_priv->drm.struct_mutex);
Chris Wilson1f7b8472017-02-08 14:30:33 +00003166
Chris Wilsonfe3288b2017-02-12 17:20:01 +00003167 for_each_engine(engine, dev_priv, id) {
Michel Thierryc64992e2017-06-20 10:57:44 +01003168 engine->hangcheck.active_request = NULL;
Michel Thierrya1ef70e2017-06-20 10:57:47 +01003169 i915_gem_reset_finish_engine(engine);
Chris Wilsonfe3288b2017-02-12 17:20:01 +00003170 }
Chris Wilsond8027092017-02-08 14:30:32 +00003171}
3172
Chris Wilsone61e0f52018-02-21 09:56:36 +00003173static void nop_submit_request(struct i915_request *request)
Chris Wilson821ed7d2016-09-09 14:11:53 +01003174{
Chris Wilson8d550822017-10-06 12:56:17 +01003175 unsigned long flags;
3176
Chris Wilsond9b13c42018-03-15 13:14:50 +00003177 GEM_TRACE("%s fence %llx:%d -> -EIO\n",
3178 request->engine->name,
3179 request->fence.context, request->fence.seqno);
Chris Wilson3cd94422017-01-10 17:22:45 +00003180 dma_fence_set_error(&request->fence, -EIO);
Chris Wilson8d550822017-10-06 12:56:17 +01003181
Chris Wilsona89d1f92018-05-02 17:38:39 +01003182 spin_lock_irqsave(&request->engine->timeline.lock, flags);
Chris Wilsone61e0f52018-02-21 09:56:36 +00003183 __i915_request_submit(request);
Chris Wilson6faf5912018-12-28 14:07:35 +00003184 intel_engine_write_global_seqno(request->engine, request->global_seqno);
Chris Wilsona89d1f92018-05-02 17:38:39 +01003185 spin_unlock_irqrestore(&request->engine->timeline.lock, flags);
Chris Wilson821ed7d2016-09-09 14:11:53 +01003186}
3187
Daniel Vetteraf7a8ff2017-10-11 11:10:19 +02003188void i915_gem_set_wedged(struct drm_i915_private *i915)
Chris Wilson821ed7d2016-09-09 14:11:53 +01003189{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00003190 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05303191 enum intel_engine_id id;
Eric Anholt673a3942008-07-30 12:06:12 -07003192
Chris Wilsond9b13c42018-03-15 13:14:50 +00003193 GEM_TRACE("start\n");
3194
Chris Wilsondecd29e2019-01-15 12:20:57 +00003195 if (GEM_SHOW_DEBUG() && !intel_engines_are_idle(i915)) {
Chris Wilson559e0402018-02-05 09:21:59 +00003196 struct drm_printer p = drm_debug_printer(__func__);
3197
3198 for_each_engine(engine, i915, id)
3199 intel_engine_dump(engine, &p, "%s\n", engine->name);
3200 }
3201
Chris Wilson3970c652018-07-23 15:53:35 +01003202 if (test_and_set_bit(I915_WEDGED, &i915->gpu_error.flags))
3203 goto out;
Chris Wilson0d73e7a2018-02-07 15:13:50 +00003204
Daniel Vetteraf7a8ff2017-10-11 11:10:19 +02003205 /*
3206 * First, stop submission to hw, but do not yet complete requests by
3207 * rolling the global seqno forward (since this would complete requests
3208 * for which we haven't set the fence error to EIO yet).
3209 */
Chris Wilson38009602018-12-03 11:36:55 +00003210 for_each_engine(engine, i915, id)
Chris Wilson963ddd62018-03-02 11:33:24 +00003211 i915_gem_reset_prepare_engine(engine);
Chris Wilson47650db2018-03-07 13:42:25 +00003212
Chris Wilsonac697ae2018-03-15 15:10:15 +00003213 /* Even if the GPU reset fails, it should still stop the engines */
Chris Wilsonec5b65a2018-07-26 09:50:33 +01003214 if (INTEL_GEN(i915) >= 5)
3215 intel_gpu_reset(i915, ALL_ENGINES);
Chris Wilsonac697ae2018-03-15 15:10:15 +00003216
Daniel Vetteraf7a8ff2017-10-11 11:10:19 +02003217 for_each_engine(engine, i915, id) {
Chris Wilson38009602018-12-03 11:36:55 +00003218 engine->submit_request = nop_submit_request;
3219 engine->schedule = NULL;
Daniel Vetteraf7a8ff2017-10-11 11:10:19 +02003220 }
Chris Wilson38009602018-12-03 11:36:55 +00003221 i915->caps.scheduler = 0;
Daniel Vetteraf7a8ff2017-10-11 11:10:19 +02003222
3223 /*
3224 * Make sure no request can slip through without getting completed by
Chris Wilson6faf5912018-12-28 14:07:35 +00003225 * either this call here to intel_engine_write_global_seqno, or the one
Chris Wilson38009602018-12-03 11:36:55 +00003226 * in nop_submit_request.
Daniel Vetteraf7a8ff2017-10-11 11:10:19 +02003227 */
3228 synchronize_rcu();
3229
Chris Wilson38009602018-12-03 11:36:55 +00003230 /* Mark all executing requests as skipped */
3231 for_each_engine(engine, i915, id)
3232 engine->cancel_requests(engine);
3233
Daniel Vetteraf7a8ff2017-10-11 11:10:19 +02003234 for_each_engine(engine, i915, id) {
Chris Wilson963ddd62018-03-02 11:33:24 +00003235 i915_gem_reset_finish_engine(engine);
Chris Wilson38009602018-12-03 11:36:55 +00003236 intel_engine_wakeup(engine);
Daniel Vetteraf7a8ff2017-10-11 11:10:19 +02003237 }
Chris Wilson20e49332016-11-22 14:41:21 +00003238
Chris Wilson3970c652018-07-23 15:53:35 +01003239out:
Chris Wilsond9b13c42018-03-15 13:14:50 +00003240 GEM_TRACE("end\n");
3241
Chris Wilson3d7adbb2017-07-21 13:32:27 +01003242 wake_up_all(&i915->gpu_error.reset_queue);
Eric Anholt673a3942008-07-30 12:06:12 -07003243}
3244
Chris Wilson2e8f9d32017-03-16 17:13:04 +00003245bool i915_gem_unset_wedged(struct drm_i915_private *i915)
3246{
Chris Wilsona89d1f92018-05-02 17:38:39 +01003247 struct i915_timeline *tl;
Chris Wilson2e8f9d32017-03-16 17:13:04 +00003248
3249 lockdep_assert_held(&i915->drm.struct_mutex);
3250 if (!test_bit(I915_WEDGED, &i915->gpu_error.flags))
3251 return true;
3252
Chris Wilson55c15512019-01-03 21:33:40 +00003253 if (!i915->gt.scratch) /* Never full initialised, recovery impossible */
3254 return false;
3255
Chris Wilsond9b13c42018-03-15 13:14:50 +00003256 GEM_TRACE("start\n");
3257
Chris Wilson2d4ecac2018-03-07 13:42:21 +00003258 /*
3259 * Before unwedging, make sure that all pending operations
Chris Wilson2e8f9d32017-03-16 17:13:04 +00003260 * are flushed and errored out - we may have requests waiting upon
3261 * third party fences. We marked all inflight requests as EIO, and
3262 * every execbuf since returned EIO, for consistency we want all
3263 * the currently pending requests to also be marked as EIO, which
3264 * is done inside our nop_submit_request - and so we must wait.
3265 *
3266 * No more can be submitted until we reset the wedged bit.
3267 */
3268 list_for_each_entry(tl, &i915->gt.timelines, link) {
Chris Wilsona89d1f92018-05-02 17:38:39 +01003269 struct i915_request *rq;
Chris Wilson2e8f9d32017-03-16 17:13:04 +00003270
Chris Wilsona89d1f92018-05-02 17:38:39 +01003271 rq = i915_gem_active_peek(&tl->last_request,
3272 &i915->drm.struct_mutex);
3273 if (!rq)
3274 continue;
Chris Wilson2e8f9d32017-03-16 17:13:04 +00003275
Chris Wilsona89d1f92018-05-02 17:38:39 +01003276 /*
3277 * We can't use our normal waiter as we want to
3278 * avoid recursively trying to handle the current
3279 * reset. The basic dma_fence_default_wait() installs
3280 * a callback for dma_fence_signal(), which is
3281 * triggered by our nop handler (indirectly, the
3282 * callback enables the signaler thread which is
3283 * woken by the nop_submit_request() advancing the seqno
3284 * and when the seqno passes the fence, the signaler
3285 * then signals the fence waking us up).
3286 */
3287 if (dma_fence_default_wait(&rq->fence, true,
3288 MAX_SCHEDULE_TIMEOUT) < 0)
3289 return false;
Chris Wilson2e8f9d32017-03-16 17:13:04 +00003290 }
Chris Wilson2d4ecac2018-03-07 13:42:21 +00003291 i915_retire_requests(i915);
3292 GEM_BUG_ON(i915->gt.active_requests);
Chris Wilson2e8f9d32017-03-16 17:13:04 +00003293
Chris Wilson55277e12019-01-03 11:21:04 +00003294 intel_engines_sanitize(i915, false);
Chris Wilson8db601f2018-09-14 09:00:17 +01003295
Chris Wilson2d4ecac2018-03-07 13:42:21 +00003296 /*
3297 * Undo nop_submit_request. We prevent all new i915 requests from
Chris Wilson2e8f9d32017-03-16 17:13:04 +00003298 * being queued (by disallowing execbuf whilst wedged) so having
3299 * waited for all active requests above, we know the system is idle
3300 * and do not have to worry about a thread being inside
3301 * engine->submit_request() as we swap over. So unlike installing
3302 * the nop_submit_request on reset, we can do this from normal
3303 * context and do not require stop_machine().
3304 */
3305 intel_engines_reset_default_submission(i915);
Chris Wilson36703e72017-06-22 11:56:25 +01003306 i915_gem_contexts_lost(i915);
Chris Wilson2e8f9d32017-03-16 17:13:04 +00003307
Chris Wilsond9b13c42018-03-15 13:14:50 +00003308 GEM_TRACE("end\n");
3309
Chris Wilson2e8f9d32017-03-16 17:13:04 +00003310 smp_mb__before_atomic(); /* complete takeover before enabling execbuf */
3311 clear_bit(I915_WEDGED, &i915->gpu_error.flags);
3312
3313 return true;
3314}
3315
Daniel Vetter75ef9da2010-08-21 00:25:16 +02003316static void
Eric Anholt673a3942008-07-30 12:06:12 -07003317i915_gem_retire_work_handler(struct work_struct *work)
3318{
Chris Wilsonb29c19b2013-09-25 17:34:56 +01003319 struct drm_i915_private *dev_priv =
Chris Wilson67d97da2016-07-04 08:08:31 +01003320 container_of(work, typeof(*dev_priv), gt.retire_work.work);
Chris Wilson91c8a322016-07-05 10:40:23 +01003321 struct drm_device *dev = &dev_priv->drm;
Eric Anholt673a3942008-07-30 12:06:12 -07003322
Chris Wilson891b48c2010-09-29 12:26:37 +01003323 /* Come back later if the device is busy... */
Chris Wilsonb29c19b2013-09-25 17:34:56 +01003324 if (mutex_trylock(&dev->struct_mutex)) {
Chris Wilsone61e0f52018-02-21 09:56:36 +00003325 i915_retire_requests(dev_priv);
Chris Wilsonb29c19b2013-09-25 17:34:56 +01003326 mutex_unlock(&dev->struct_mutex);
3327 }
Chris Wilson67d97da2016-07-04 08:08:31 +01003328
Chris Wilson88923042018-01-29 14:41:04 +00003329 /*
3330 * Keep the retire handler running until we are finally idle.
Chris Wilson67d97da2016-07-04 08:08:31 +01003331 * We do not need to do this test under locking as in the worst-case
3332 * we queue the retire worker once too often.
3333 */
Chris Wilson88923042018-01-29 14:41:04 +00003334 if (READ_ONCE(dev_priv->gt.awake))
Chris Wilson67d97da2016-07-04 08:08:31 +01003335 queue_delayed_work(dev_priv->wq,
3336 &dev_priv->gt.retire_work,
Chris Wilsonbcb45082012-10-05 17:02:57 +01003337 round_jiffies_up_relative(HZ));
Chris Wilsonb29c19b2013-09-25 17:34:56 +01003338}
Chris Wilson891b48c2010-09-29 12:26:37 +01003339
Chris Wilson84a10742018-01-24 11:36:08 +00003340static void shrink_caches(struct drm_i915_private *i915)
3341{
3342 /*
3343 * kmem_cache_shrink() discards empty slabs and reorders partially
3344 * filled slabs to prioritise allocating from the mostly full slabs,
3345 * with the aim of reducing fragmentation.
3346 */
3347 kmem_cache_shrink(i915->priorities);
3348 kmem_cache_shrink(i915->dependencies);
3349 kmem_cache_shrink(i915->requests);
3350 kmem_cache_shrink(i915->luts);
3351 kmem_cache_shrink(i915->vmas);
3352 kmem_cache_shrink(i915->objects);
3353}
3354
3355struct sleep_rcu_work {
3356 union {
3357 struct rcu_head rcu;
3358 struct work_struct work;
3359 };
3360 struct drm_i915_private *i915;
3361 unsigned int epoch;
3362};
3363
3364static inline bool
3365same_epoch(struct drm_i915_private *i915, unsigned int epoch)
3366{
3367 /*
3368 * There is a small chance that the epoch wrapped since we started
3369 * sleeping. If we assume that epoch is at least a u32, then it will
3370 * take at least 2^32 * 100ms for it to wrap, or about 326 years.
3371 */
3372 return epoch == READ_ONCE(i915->gt.epoch);
3373}
3374
3375static void __sleep_work(struct work_struct *work)
3376{
3377 struct sleep_rcu_work *s = container_of(work, typeof(*s), work);
3378 struct drm_i915_private *i915 = s->i915;
3379 unsigned int epoch = s->epoch;
3380
3381 kfree(s);
3382 if (same_epoch(i915, epoch))
3383 shrink_caches(i915);
3384}
3385
3386static void __sleep_rcu(struct rcu_head *rcu)
3387{
3388 struct sleep_rcu_work *s = container_of(rcu, typeof(*s), rcu);
3389 struct drm_i915_private *i915 = s->i915;
3390
Chris Wilsona1db9c52018-11-08 09:21:01 +00003391 destroy_rcu_head(&s->rcu);
3392
Chris Wilson84a10742018-01-24 11:36:08 +00003393 if (same_epoch(i915, s->epoch)) {
3394 INIT_WORK(&s->work, __sleep_work);
3395 queue_work(i915->wq, &s->work);
3396 } else {
3397 kfree(s);
3398 }
3399}
3400
Chris Wilson5427f202017-10-23 22:32:34 +01003401static inline bool
3402new_requests_since_last_retire(const struct drm_i915_private *i915)
3403{
3404 return (READ_ONCE(i915->gt.active_requests) ||
3405 work_pending(&i915->gt.idle_work.work));
3406}
3407
Chris Wilson1934f5de2018-05-31 23:40:57 +01003408static void assert_kernel_context_is_current(struct drm_i915_private *i915)
3409{
3410 struct intel_engine_cs *engine;
3411 enum intel_engine_id id;
3412
3413 if (i915_terminally_wedged(&i915->gpu_error))
3414 return;
3415
3416 GEM_BUG_ON(i915->gt.active_requests);
3417 for_each_engine(engine, i915, id) {
3418 GEM_BUG_ON(__i915_gem_active_peek(&engine->timeline.last_request));
3419 GEM_BUG_ON(engine->last_retired_context !=
3420 to_intel_context(i915->kernel_context, engine));
3421 }
3422}
3423
Chris Wilsonb29c19b2013-09-25 17:34:56 +01003424static void
3425i915_gem_idle_work_handler(struct work_struct *work)
3426{
3427 struct drm_i915_private *dev_priv =
Chris Wilson67d97da2016-07-04 08:08:31 +01003428 container_of(work, typeof(*dev_priv), gt.idle_work.work);
Chris Wilson84a10742018-01-24 11:36:08 +00003429 unsigned int epoch = I915_EPOCH_INVALID;
Chris Wilson67d97da2016-07-04 08:08:31 +01003430 bool rearm_hangcheck;
3431
3432 if (!READ_ONCE(dev_priv->gt.awake))
3433 return;
3434
Chris Wilson4dfacb02018-05-31 09:22:43 +01003435 if (READ_ONCE(dev_priv->gt.active_requests))
3436 return;
3437
3438 /*
3439 * Flush out the last user context, leaving only the pinned
3440 * kernel context resident. When we are idling on the kernel_context,
3441 * no more new requests (with a context switch) are emitted and we
3442 * can finally rest. A consequence is that the idle work handler is
3443 * always called at least twice before idling (and if the system is
3444 * idle that implies a round trip through the retire worker).
3445 */
3446 mutex_lock(&dev_priv->drm.struct_mutex);
3447 i915_gem_switch_to_kernel_context(dev_priv);
3448 mutex_unlock(&dev_priv->drm.struct_mutex);
3449
3450 GEM_TRACE("active_requests=%d (after switch-to-kernel-context)\n",
3451 READ_ONCE(dev_priv->gt.active_requests));
3452
Imre Deak0cb56702016-11-07 11:20:04 +02003453 /*
3454 * Wait for last execlists context complete, but bail out in case a
Chris Wilsonffed7bd2018-03-01 10:33:38 +00003455 * new request is submitted. As we don't trust the hardware, we
3456 * continue on if the wait times out. This is necessary to allow
3457 * the machine to suspend even if the hardware dies, and we will
3458 * try to recover in resume (after depriving the hardware of power,
3459 * it may be in a better mmod).
Imre Deak0cb56702016-11-07 11:20:04 +02003460 */
Chris Wilsonffed7bd2018-03-01 10:33:38 +00003461 __wait_for(if (new_requests_since_last_retire(dev_priv)) return,
3462 intel_engines_are_idle(dev_priv),
3463 I915_IDLE_ENGINES_TIMEOUT * 1000,
3464 10, 500);
Chris Wilson67d97da2016-07-04 08:08:31 +01003465
3466 rearm_hangcheck =
3467 cancel_delayed_work_sync(&dev_priv->gpu_error.hangcheck_work);
3468
Chris Wilson5427f202017-10-23 22:32:34 +01003469 if (!mutex_trylock(&dev_priv->drm.struct_mutex)) {
Chris Wilson67d97da2016-07-04 08:08:31 +01003470 /* Currently busy, come back later */
3471 mod_delayed_work(dev_priv->wq,
3472 &dev_priv->gt.idle_work,
3473 msecs_to_jiffies(50));
3474 goto out_rearm;
3475 }
3476
Imre Deak93c97dc2016-11-07 11:20:03 +02003477 /*
3478 * New request retired after this work handler started, extend active
3479 * period until next instance of the work.
3480 */
Chris Wilson5427f202017-10-23 22:32:34 +01003481 if (new_requests_since_last_retire(dev_priv))
Imre Deak93c97dc2016-11-07 11:20:03 +02003482 goto out_unlock;
3483
Chris Wilsone4d20062018-04-06 16:51:44 +01003484 epoch = __i915_gem_park(dev_priv);
Chris Wilsonff320d62017-10-23 22:32:35 +01003485
Chris Wilson1934f5de2018-05-31 23:40:57 +01003486 assert_kernel_context_is_current(dev_priv);
3487
Chris Wilson67d97da2016-07-04 08:08:31 +01003488 rearm_hangcheck = false;
Chris Wilson67d97da2016-07-04 08:08:31 +01003489out_unlock:
Chris Wilson5427f202017-10-23 22:32:34 +01003490 mutex_unlock(&dev_priv->drm.struct_mutex);
Chris Wilson35c94182015-04-07 16:20:37 +01003491
Chris Wilson67d97da2016-07-04 08:08:31 +01003492out_rearm:
3493 if (rearm_hangcheck) {
3494 GEM_BUG_ON(!dev_priv->gt.awake);
3495 i915_queue_hangcheck(dev_priv);
Chris Wilson35c94182015-04-07 16:20:37 +01003496 }
Chris Wilson84a10742018-01-24 11:36:08 +00003497
3498 /*
3499 * When we are idle, it is an opportune time to reap our caches.
3500 * However, we have many objects that utilise RCU and the ordered
3501 * i915->wq that this work is executing on. To try and flush any
3502 * pending frees now we are idle, we first wait for an RCU grace
3503 * period, and then queue a task (that will run last on the wq) to
3504 * shrink and re-optimize the caches.
3505 */
3506 if (same_epoch(dev_priv, epoch)) {
3507 struct sleep_rcu_work *s = kmalloc(sizeof(*s), GFP_KERNEL);
3508 if (s) {
Chris Wilsona1db9c52018-11-08 09:21:01 +00003509 init_rcu_head(&s->rcu);
Chris Wilson84a10742018-01-24 11:36:08 +00003510 s->i915 = dev_priv;
3511 s->epoch = epoch;
3512 call_rcu(&s->rcu, __sleep_rcu);
3513 }
3514 }
Eric Anholt673a3942008-07-30 12:06:12 -07003515}
3516
Chris Wilsonb1f788c2016-08-04 07:52:45 +01003517void i915_gem_close_object(struct drm_gem_object *gem, struct drm_file *file)
3518{
Chris Wilsond1b48c12017-08-16 09:52:08 +01003519 struct drm_i915_private *i915 = to_i915(gem->dev);
Chris Wilsonb1f788c2016-08-04 07:52:45 +01003520 struct drm_i915_gem_object *obj = to_intel_bo(gem);
3521 struct drm_i915_file_private *fpriv = file->driver_priv;
Chris Wilsond1b48c12017-08-16 09:52:08 +01003522 struct i915_lut_handle *lut, *ln;
Chris Wilsonb1f788c2016-08-04 07:52:45 +01003523
Chris Wilsond1b48c12017-08-16 09:52:08 +01003524 mutex_lock(&i915->drm.struct_mutex);
3525
3526 list_for_each_entry_safe(lut, ln, &obj->lut_list, obj_link) {
3527 struct i915_gem_context *ctx = lut->ctx;
3528 struct i915_vma *vma;
3529
Chris Wilson432295d2017-08-22 12:05:15 +01003530 GEM_BUG_ON(ctx->file_priv == ERR_PTR(-EBADF));
Chris Wilsond1b48c12017-08-16 09:52:08 +01003531 if (ctx->file_priv != fpriv)
3532 continue;
3533
3534 vma = radix_tree_delete(&ctx->handles_vma, lut->handle);
Chris Wilson3ffff012017-08-22 12:05:17 +01003535 GEM_BUG_ON(vma->obj != obj);
3536
3537 /* We allow the process to have multiple handles to the same
3538 * vma, in the same fd namespace, by virtue of flink/open.
3539 */
3540 GEM_BUG_ON(!vma->open_count);
3541 if (!--vma->open_count && !i915_vma_is_ggtt(vma))
Chris Wilsonb1f788c2016-08-04 07:52:45 +01003542 i915_vma_close(vma);
Chris Wilsonf8a7fde2016-10-28 13:58:29 +01003543
Chris Wilsond1b48c12017-08-16 09:52:08 +01003544 list_del(&lut->obj_link);
3545 list_del(&lut->ctx_link);
Chris Wilson4ff4b442017-06-16 15:05:16 +01003546
Chris Wilsond1b48c12017-08-16 09:52:08 +01003547 kmem_cache_free(i915->luts, lut);
3548 __i915_gem_object_release_unless_active(obj);
Chris Wilsonf8a7fde2016-10-28 13:58:29 +01003549 }
Chris Wilsond1b48c12017-08-16 09:52:08 +01003550
3551 mutex_unlock(&i915->drm.struct_mutex);
Chris Wilsonb1f788c2016-08-04 07:52:45 +01003552}
3553
Chris Wilsone95433c2016-10-28 13:58:27 +01003554static unsigned long to_wait_timeout(s64 timeout_ns)
3555{
3556 if (timeout_ns < 0)
3557 return MAX_SCHEDULE_TIMEOUT;
3558
3559 if (timeout_ns == 0)
3560 return 0;
3561
3562 return nsecs_to_jiffies_timeout(timeout_ns);
3563}
3564
Ben Widawsky5816d642012-04-11 11:18:19 -07003565/**
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07003566 * i915_gem_wait_ioctl - implements DRM_IOCTL_I915_GEM_WAIT
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01003567 * @dev: drm device pointer
3568 * @data: ioctl data blob
3569 * @file: drm file pointer
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07003570 *
3571 * Returns 0 if successful, else an error is returned with the remaining time in
3572 * the timeout parameter.
3573 * -ETIME: object is still busy after timeout
3574 * -ERESTARTSYS: signal interrupted the wait
3575 * -ENONENT: object doesn't exist
3576 * Also possible, but rare:
Chris Wilsonb8050142017-08-11 11:57:31 +01003577 * -EAGAIN: incomplete, restart syscall
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07003578 * -ENOMEM: damn
3579 * -ENODEV: Internal IRQ fail
3580 * -E?: The add request failed
3581 *
3582 * The wait ioctl with a timeout of 0 reimplements the busy ioctl. With any
3583 * non-zero timeout parameter the wait ioctl will wait for the given number of
3584 * nanoseconds on an object becoming unbusy. Since the wait itself does so
3585 * without holding struct_mutex the object may become re-busied before this
3586 * function completes. A similar but shorter * race condition exists in the busy
3587 * ioctl
3588 */
3589int
3590i915_gem_wait_ioctl(struct drm_device *dev, void *data, struct drm_file *file)
3591{
3592 struct drm_i915_gem_wait *args = data;
3593 struct drm_i915_gem_object *obj;
Chris Wilsone95433c2016-10-28 13:58:27 +01003594 ktime_t start;
3595 long ret;
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07003596
Daniel Vetter11b5d512014-09-29 15:31:26 +02003597 if (args->flags != 0)
3598 return -EINVAL;
3599
Chris Wilson03ac0642016-07-20 13:31:51 +01003600 obj = i915_gem_object_lookup(file, args->bo_handle);
Chris Wilson033d5492016-08-05 10:14:17 +01003601 if (!obj)
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07003602 return -ENOENT;
Chris Wilson033d5492016-08-05 10:14:17 +01003603
Chris Wilsone95433c2016-10-28 13:58:27 +01003604 start = ktime_get();
3605
3606 ret = i915_gem_object_wait(obj,
Chris Wilsone9eaf822018-10-01 15:47:55 +01003607 I915_WAIT_INTERRUPTIBLE |
3608 I915_WAIT_PRIORITY |
3609 I915_WAIT_ALL,
Chris Wilsone95433c2016-10-28 13:58:27 +01003610 to_wait_timeout(args->timeout_ns),
3611 to_rps_client(file));
3612
3613 if (args->timeout_ns > 0) {
3614 args->timeout_ns -= ktime_to_ns(ktime_sub(ktime_get(), start));
3615 if (args->timeout_ns < 0)
3616 args->timeout_ns = 0;
Chris Wilsonc1d20612017-02-16 12:54:41 +00003617
3618 /*
3619 * Apparently ktime isn't accurate enough and occasionally has a
3620 * bit of mismatch in the jiffies<->nsecs<->ktime loop. So patch
3621 * things up to make the test happy. We allow up to 1 jiffy.
3622 *
3623 * This is a regression from the timespec->ktime conversion.
3624 */
3625 if (ret == -ETIME && !nsecs_to_jiffies(args->timeout_ns))
3626 args->timeout_ns = 0;
Chris Wilsonb8050142017-08-11 11:57:31 +01003627
3628 /* Asked to wait beyond the jiffie/scheduler precision? */
3629 if (ret == -ETIME && args->timeout_ns)
3630 ret = -EAGAIN;
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07003631 }
3632
Chris Wilsonf0cd5182016-10-28 13:58:43 +01003633 i915_gem_object_put(obj);
John Harrisonff865882014-11-24 18:49:28 +00003634 return ret;
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07003635}
3636
Chris Wilsonec625fb2018-07-09 13:20:42 +01003637static long wait_for_timeline(struct i915_timeline *tl,
3638 unsigned int flags, long timeout)
Daniel Vetter4df2faf2010-02-19 11:52:00 +01003639{
Chris Wilson06060352018-05-31 09:22:44 +01003640 struct i915_request *rq;
Chris Wilson06060352018-05-31 09:22:44 +01003641
3642 rq = i915_gem_active_get_unlocked(&tl->last_request);
3643 if (!rq)
Chris Wilsonec625fb2018-07-09 13:20:42 +01003644 return timeout;
Chris Wilson06060352018-05-31 09:22:44 +01003645
3646 /*
3647 * "Race-to-idle".
3648 *
3649 * Switching to the kernel context is often used a synchronous
3650 * step prior to idling, e.g. in suspend for flushing all
3651 * current operations to memory before sleeping. These we
3652 * want to complete as quickly as possible to avoid prolonged
3653 * stalls, so allow the gpu to boost to maximum clocks.
3654 */
3655 if (flags & I915_WAIT_FOR_IDLE_BOOST)
3656 gen6_rps_boost(rq, NULL);
3657
Chris Wilsonec625fb2018-07-09 13:20:42 +01003658 timeout = i915_request_wait(rq, flags, timeout);
Chris Wilson06060352018-05-31 09:22:44 +01003659 i915_request_put(rq);
3660
Chris Wilsonec625fb2018-07-09 13:20:42 +01003661 return timeout;
Chris Wilson73cb9702016-10-28 13:58:46 +01003662}
3663
Chris Wilson25112b62017-03-30 15:50:39 +01003664static int wait_for_engines(struct drm_i915_private *i915)
3665{
Chris Wilsonee42c002017-12-11 19:41:34 +00003666 if (wait_for(intel_engines_are_idle(i915), I915_IDLE_ENGINES_TIMEOUT)) {
Chris Wilson59e4b192017-12-11 19:41:35 +00003667 dev_err(i915->drm.dev,
3668 "Failed to idle engines, declaring wedged!\n");
Chris Wilson629820f2018-03-09 10:11:14 +00003669 GEM_TRACE_DUMP();
Chris Wilsoncad99462017-08-26 12:09:33 +01003670 i915_gem_set_wedged(i915);
3671 return -EIO;
Chris Wilson25112b62017-03-30 15:50:39 +01003672 }
3673
3674 return 0;
3675}
3676
Chris Wilsonec625fb2018-07-09 13:20:42 +01003677int i915_gem_wait_for_idle(struct drm_i915_private *i915,
3678 unsigned int flags, long timeout)
Chris Wilson73cb9702016-10-28 13:58:46 +01003679{
Chris Wilsonec625fb2018-07-09 13:20:42 +01003680 GEM_TRACE("flags=%x (%s), timeout=%ld%s\n",
3681 flags, flags & I915_WAIT_LOCKED ? "locked" : "unlocked",
3682 timeout, timeout == MAX_SCHEDULE_TIMEOUT ? " (forever)" : "");
Chris Wilson09a4c022018-05-24 09:11:35 +01003683
Chris Wilson863e9fd2017-05-30 13:13:32 +01003684 /* If the device is asleep, we have no requests outstanding */
3685 if (!READ_ONCE(i915->gt.awake))
3686 return 0;
3687
Chris Wilson9caa34a2016-11-11 14:58:08 +00003688 if (flags & I915_WAIT_LOCKED) {
Chris Wilsona89d1f92018-05-02 17:38:39 +01003689 struct i915_timeline *tl;
3690 int err;
Chris Wilson9caa34a2016-11-11 14:58:08 +00003691
3692 lockdep_assert_held(&i915->drm.struct_mutex);
3693
3694 list_for_each_entry(tl, &i915->gt.timelines, link) {
Chris Wilsonec625fb2018-07-09 13:20:42 +01003695 timeout = wait_for_timeline(tl, flags, timeout);
3696 if (timeout < 0)
3697 return timeout;
Chris Wilson9caa34a2016-11-11 14:58:08 +00003698 }
Chris Wilsonc1e63f62018-08-08 11:50:59 +01003699 if (GEM_SHOW_DEBUG() && !timeout) {
3700 /* Presume that timeout was non-zero to begin with! */
3701 dev_warn(&i915->drm.pdev->dev,
3702 "Missed idle-completion interrupt!\n");
3703 GEM_TRACE_DUMP();
3704 }
Chris Wilsona61b47f2018-06-27 12:53:34 +01003705
3706 err = wait_for_engines(i915);
3707 if (err)
3708 return err;
3709
Chris Wilsone61e0f52018-02-21 09:56:36 +00003710 i915_retire_requests(i915);
Chris Wilson09a4c022018-05-24 09:11:35 +01003711 GEM_BUG_ON(i915->gt.active_requests);
Chris Wilson9caa34a2016-11-11 14:58:08 +00003712 } else {
Chris Wilsona89d1f92018-05-02 17:38:39 +01003713 struct intel_engine_cs *engine;
3714 enum intel_engine_id id;
Zou Nan haid1b851f2010-05-21 09:08:57 +08003715
Chris Wilsona89d1f92018-05-02 17:38:39 +01003716 for_each_engine(engine, i915, id) {
Chris Wilsonec625fb2018-07-09 13:20:42 +01003717 struct i915_timeline *tl = &engine->timeline;
3718
3719 timeout = wait_for_timeline(tl, flags, timeout);
3720 if (timeout < 0)
3721 return timeout;
Chris Wilsona89d1f92018-05-02 17:38:39 +01003722 }
Chris Wilsona89d1f92018-05-02 17:38:39 +01003723 }
Chris Wilsona61b47f2018-06-27 12:53:34 +01003724
3725 return 0;
Daniel Vetter4df2faf2010-02-19 11:52:00 +01003726}
3727
Chris Wilson5a97bcc2017-02-22 11:40:46 +00003728static void __i915_gem_object_flush_for_display(struct drm_i915_gem_object *obj)
3729{
Chris Wilsone27ab732017-06-15 13:38:49 +01003730 /*
3731 * We manually flush the CPU domain so that we can override and
3732 * force the flush for the display, and perform it asyncrhonously.
3733 */
3734 flush_write_domain(obj, ~I915_GEM_DOMAIN_CPU);
3735 if (obj->cache_dirty)
3736 i915_gem_clflush_object(obj, I915_CLFLUSH_FORCE);
Christian Königc0a51fd2018-02-16 13:43:38 +01003737 obj->write_domain = 0;
Chris Wilson5a97bcc2017-02-22 11:40:46 +00003738}
3739
3740void i915_gem_object_flush_if_display(struct drm_i915_gem_object *obj)
3741{
Chris Wilsonbd3d2252017-10-13 21:26:14 +01003742 if (!READ_ONCE(obj->pin_global))
Chris Wilson5a97bcc2017-02-22 11:40:46 +00003743 return;
3744
3745 mutex_lock(&obj->base.dev->struct_mutex);
3746 __i915_gem_object_flush_for_display(obj);
3747 mutex_unlock(&obj->base.dev->struct_mutex);
3748}
3749
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003750/**
Chris Wilsone22d8e32017-04-12 12:01:11 +01003751 * Moves a single object to the WC read, and possibly write domain.
3752 * @obj: object to act on
3753 * @write: ask for write access or read only
3754 *
3755 * This function returns when the move is complete, including waiting on
3756 * flushes to occur.
3757 */
3758int
3759i915_gem_object_set_to_wc_domain(struct drm_i915_gem_object *obj, bool write)
3760{
3761 int ret;
3762
3763 lockdep_assert_held(&obj->base.dev->struct_mutex);
3764
3765 ret = i915_gem_object_wait(obj,
3766 I915_WAIT_INTERRUPTIBLE |
3767 I915_WAIT_LOCKED |
3768 (write ? I915_WAIT_ALL : 0),
3769 MAX_SCHEDULE_TIMEOUT,
3770 NULL);
3771 if (ret)
3772 return ret;
3773
Christian Königc0a51fd2018-02-16 13:43:38 +01003774 if (obj->write_domain == I915_GEM_DOMAIN_WC)
Chris Wilsone22d8e32017-04-12 12:01:11 +01003775 return 0;
3776
3777 /* Flush and acquire obj->pages so that we are coherent through
3778 * direct access in memory with previous cached writes through
3779 * shmemfs and that our cache domain tracking remains valid.
3780 * For example, if the obj->filp was moved to swap without us
3781 * being notified and releasing the pages, we would mistakenly
3782 * continue to assume that the obj remained out of the CPU cached
3783 * domain.
3784 */
3785 ret = i915_gem_object_pin_pages(obj);
3786 if (ret)
3787 return ret;
3788
3789 flush_write_domain(obj, ~I915_GEM_DOMAIN_WC);
3790
3791 /* Serialise direct access to this object with the barriers for
3792 * coherent writes from the GPU, by effectively invalidating the
3793 * WC domain upon first access.
3794 */
Christian Königc0a51fd2018-02-16 13:43:38 +01003795 if ((obj->read_domains & I915_GEM_DOMAIN_WC) == 0)
Chris Wilsone22d8e32017-04-12 12:01:11 +01003796 mb();
3797
3798 /* It should now be out of any other write domains, and we can update
3799 * the domain values for our changes.
3800 */
Christian Königc0a51fd2018-02-16 13:43:38 +01003801 GEM_BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_WC) != 0);
3802 obj->read_domains |= I915_GEM_DOMAIN_WC;
Chris Wilsone22d8e32017-04-12 12:01:11 +01003803 if (write) {
Christian Königc0a51fd2018-02-16 13:43:38 +01003804 obj->read_domains = I915_GEM_DOMAIN_WC;
3805 obj->write_domain = I915_GEM_DOMAIN_WC;
Chris Wilsone22d8e32017-04-12 12:01:11 +01003806 obj->mm.dirty = true;
3807 }
3808
3809 i915_gem_object_unpin_pages(obj);
3810 return 0;
3811}
3812
3813/**
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003814 * Moves a single object to the GTT read, and possibly write domain.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01003815 * @obj: object to act on
3816 * @write: ask for write access or read only
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003817 *
3818 * This function returns when the move is complete, including waiting on
3819 * flushes to occur.
3820 */
Jesse Barnes79e53942008-11-07 14:24:08 -08003821int
Chris Wilson20217462010-11-23 15:26:33 +00003822i915_gem_object_set_to_gtt_domain(struct drm_i915_gem_object *obj, bool write)
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003823{
Eric Anholte47c68e2008-11-14 13:35:19 -08003824 int ret;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003825
Chris Wilsone95433c2016-10-28 13:58:27 +01003826 lockdep_assert_held(&obj->base.dev->struct_mutex);
Chris Wilson4c7d62c2016-10-28 13:58:32 +01003827
Chris Wilsone95433c2016-10-28 13:58:27 +01003828 ret = i915_gem_object_wait(obj,
3829 I915_WAIT_INTERRUPTIBLE |
3830 I915_WAIT_LOCKED |
3831 (write ? I915_WAIT_ALL : 0),
3832 MAX_SCHEDULE_TIMEOUT,
3833 NULL);
Chris Wilson88241782011-01-07 17:09:48 +00003834 if (ret)
3835 return ret;
3836
Christian Königc0a51fd2018-02-16 13:43:38 +01003837 if (obj->write_domain == I915_GEM_DOMAIN_GTT)
Chris Wilsonc13d87e2016-07-20 09:21:15 +01003838 return 0;
3839
Chris Wilson43566de2015-01-02 16:29:29 +05303840 /* Flush and acquire obj->pages so that we are coherent through
3841 * direct access in memory with previous cached writes through
3842 * shmemfs and that our cache domain tracking remains valid.
3843 * For example, if the obj->filp was moved to swap without us
3844 * being notified and releasing the pages, we would mistakenly
3845 * continue to assume that the obj remained out of the CPU cached
3846 * domain.
3847 */
Chris Wilsona4f5ea62016-10-28 13:58:35 +01003848 ret = i915_gem_object_pin_pages(obj);
Chris Wilson43566de2015-01-02 16:29:29 +05303849 if (ret)
3850 return ret;
3851
Chris Wilsonef749212017-04-12 12:01:10 +01003852 flush_write_domain(obj, ~I915_GEM_DOMAIN_GTT);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003853
Chris Wilsond0a57782012-10-09 19:24:37 +01003854 /* Serialise direct access to this object with the barriers for
3855 * coherent writes from the GPU, by effectively invalidating the
3856 * GTT domain upon first access.
3857 */
Christian Königc0a51fd2018-02-16 13:43:38 +01003858 if ((obj->read_domains & I915_GEM_DOMAIN_GTT) == 0)
Chris Wilsond0a57782012-10-09 19:24:37 +01003859 mb();
3860
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003861 /* It should now be out of any other write domains, and we can update
3862 * the domain values for our changes.
3863 */
Christian Königc0a51fd2018-02-16 13:43:38 +01003864 GEM_BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_GTT) != 0);
3865 obj->read_domains |= I915_GEM_DOMAIN_GTT;
Eric Anholte47c68e2008-11-14 13:35:19 -08003866 if (write) {
Christian Königc0a51fd2018-02-16 13:43:38 +01003867 obj->read_domains = I915_GEM_DOMAIN_GTT;
3868 obj->write_domain = I915_GEM_DOMAIN_GTT;
Chris Wilsona4f5ea62016-10-28 13:58:35 +01003869 obj->mm.dirty = true;
Eric Anholte47c68e2008-11-14 13:35:19 -08003870 }
3871
Chris Wilsona4f5ea62016-10-28 13:58:35 +01003872 i915_gem_object_unpin_pages(obj);
Eric Anholte47c68e2008-11-14 13:35:19 -08003873 return 0;
3874}
3875
Chris Wilsonef55f922015-10-09 14:11:27 +01003876/**
3877 * Changes the cache-level of an object across all VMA.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01003878 * @obj: object to act on
3879 * @cache_level: new cache level to set for the object
Chris Wilsonef55f922015-10-09 14:11:27 +01003880 *
3881 * After this function returns, the object will be in the new cache-level
3882 * across all GTT and the contents of the backing storage will be coherent,
3883 * with respect to the new cache-level. In order to keep the backing storage
3884 * coherent for all users, we only allow a single cache level to be set
3885 * globally on the object and prevent it from being changed whilst the
3886 * hardware is reading from the object. That is if the object is currently
3887 * on the scanout it will be set to uncached (or equivalent display
3888 * cache coherency) and all non-MOCS GPU access will also be uncached so
3889 * that all direct access to the scanout remains coherent.
3890 */
Chris Wilsone4ffd172011-04-04 09:44:39 +01003891int i915_gem_object_set_cache_level(struct drm_i915_gem_object *obj,
3892 enum i915_cache_level cache_level)
3893{
Chris Wilsonaa653a62016-08-04 07:52:27 +01003894 struct i915_vma *vma;
Chris Wilsona6a7cc42016-11-18 21:17:46 +00003895 int ret;
Chris Wilsone4ffd172011-04-04 09:44:39 +01003896
Chris Wilson4c7d62c2016-10-28 13:58:32 +01003897 lockdep_assert_held(&obj->base.dev->struct_mutex);
3898
Chris Wilsone4ffd172011-04-04 09:44:39 +01003899 if (obj->cache_level == cache_level)
Chris Wilsona6a7cc42016-11-18 21:17:46 +00003900 return 0;
Chris Wilsone4ffd172011-04-04 09:44:39 +01003901
Chris Wilsonef55f922015-10-09 14:11:27 +01003902 /* Inspect the list of currently bound VMA and unbind any that would
3903 * be invalid given the new cache-level. This is principally to
3904 * catch the issue of the CS prefetch crossing page boundaries and
3905 * reading an invalid PTE on older architectures.
3906 */
Chris Wilsonaa653a62016-08-04 07:52:27 +01003907restart:
3908 list_for_each_entry(vma, &obj->vma_list, obj_link) {
Chris Wilsonef55f922015-10-09 14:11:27 +01003909 if (!drm_mm_node_allocated(&vma->node))
3910 continue;
3911
Chris Wilson20dfbde2016-08-04 16:32:30 +01003912 if (i915_vma_is_pinned(vma)) {
Chris Wilsonef55f922015-10-09 14:11:27 +01003913 DRM_DEBUG("can not change the cache level of pinned objects\n");
3914 return -EBUSY;
3915 }
3916
Chris Wilson010e3e62017-12-06 12:49:13 +00003917 if (!i915_vma_is_closed(vma) &&
3918 i915_gem_valid_gtt_space(vma, cache_level))
Chris Wilsonaa653a62016-08-04 07:52:27 +01003919 continue;
3920
3921 ret = i915_vma_unbind(vma);
3922 if (ret)
3923 return ret;
3924
3925 /* As unbinding may affect other elements in the
3926 * obj->vma_list (due to side-effects from retiring
3927 * an active vma), play safe and restart the iterator.
3928 */
3929 goto restart;
Chris Wilson42d6ab42012-07-26 11:49:32 +01003930 }
3931
Chris Wilsonef55f922015-10-09 14:11:27 +01003932 /* We can reuse the existing drm_mm nodes but need to change the
3933 * cache-level on the PTE. We could simply unbind them all and
3934 * rebind with the correct cache-level on next use. However since
3935 * we already have a valid slot, dma mapping, pages etc, we may as
3936 * rewrite the PTE in the belief that doing so tramples upon less
3937 * state and so involves less work.
3938 */
Chris Wilson15717de2016-08-04 07:52:26 +01003939 if (obj->bind_count) {
Chris Wilsonef55f922015-10-09 14:11:27 +01003940 /* Before we change the PTE, the GPU must not be accessing it.
3941 * If we wait upon the object, we know that all the bound
3942 * VMA are no longer active.
3943 */
Chris Wilsone95433c2016-10-28 13:58:27 +01003944 ret = i915_gem_object_wait(obj,
3945 I915_WAIT_INTERRUPTIBLE |
3946 I915_WAIT_LOCKED |
3947 I915_WAIT_ALL,
3948 MAX_SCHEDULE_TIMEOUT,
3949 NULL);
Chris Wilsone4ffd172011-04-04 09:44:39 +01003950 if (ret)
3951 return ret;
3952
Tvrtko Ursulin0031fb92016-11-04 14:42:44 +00003953 if (!HAS_LLC(to_i915(obj->base.dev)) &&
3954 cache_level != I915_CACHE_NONE) {
Chris Wilsonef55f922015-10-09 14:11:27 +01003955 /* Access to snoopable pages through the GTT is
3956 * incoherent and on some machines causes a hard
3957 * lockup. Relinquish the CPU mmaping to force
3958 * userspace to refault in the pages and we can
3959 * then double check if the GTT mapping is still
3960 * valid for that pointer access.
3961 */
3962 i915_gem_release_mmap(obj);
Chris Wilsone4ffd172011-04-04 09:44:39 +01003963
Chris Wilsonef55f922015-10-09 14:11:27 +01003964 /* As we no longer need a fence for GTT access,
3965 * we can relinquish it now (and so prevent having
3966 * to steal a fence from someone else on the next
3967 * fence request). Note GPU activity would have
3968 * dropped the fence as all snoopable access is
3969 * supposed to be linear.
3970 */
Chris Wilsone2189dd2017-12-07 21:14:07 +00003971 for_each_ggtt_vma(vma, obj) {
Chris Wilson49ef5292016-08-18 17:17:00 +01003972 ret = i915_vma_put_fence(vma);
3973 if (ret)
3974 return ret;
3975 }
Chris Wilsonef55f922015-10-09 14:11:27 +01003976 } else {
3977 /* We either have incoherent backing store and
3978 * so no GTT access or the architecture is fully
3979 * coherent. In such cases, existing GTT mmaps
3980 * ignore the cache bit in the PTE and we can
3981 * rewrite it without confusing the GPU or having
3982 * to force userspace to fault back in its mmaps.
3983 */
Chris Wilsone4ffd172011-04-04 09:44:39 +01003984 }
3985
Chris Wilson1c7f4bc2016-02-26 11:03:19 +00003986 list_for_each_entry(vma, &obj->vma_list, obj_link) {
Chris Wilsonef55f922015-10-09 14:11:27 +01003987 if (!drm_mm_node_allocated(&vma->node))
3988 continue;
3989
3990 ret = i915_vma_bind(vma, cache_level, PIN_UPDATE);
3991 if (ret)
3992 return ret;
3993 }
Chris Wilsone4ffd172011-04-04 09:44:39 +01003994 }
3995
Chris Wilson1c7f4bc2016-02-26 11:03:19 +00003996 list_for_each_entry(vma, &obj->vma_list, obj_link)
Chris Wilson2c225692013-08-09 12:26:45 +01003997 vma->node.color = cache_level;
Chris Wilsonb8f55be2017-08-11 12:11:16 +01003998 i915_gem_object_set_cache_coherency(obj, cache_level);
Chris Wilsone27ab732017-06-15 13:38:49 +01003999 obj->cache_dirty = true; /* Always invalidate stale cachelines */
Chris Wilson2c225692013-08-09 12:26:45 +01004000
Chris Wilsone4ffd172011-04-04 09:44:39 +01004001 return 0;
4002}
4003
Ben Widawsky199adf42012-09-21 17:01:20 -07004004int i915_gem_get_caching_ioctl(struct drm_device *dev, void *data,
4005 struct drm_file *file)
Chris Wilsone6994ae2012-07-10 10:27:08 +01004006{
Ben Widawsky199adf42012-09-21 17:01:20 -07004007 struct drm_i915_gem_caching *args = data;
Chris Wilsone6994ae2012-07-10 10:27:08 +01004008 struct drm_i915_gem_object *obj;
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004009 int err = 0;
Chris Wilsone6994ae2012-07-10 10:27:08 +01004010
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004011 rcu_read_lock();
4012 obj = i915_gem_object_lookup_rcu(file, args->handle);
4013 if (!obj) {
4014 err = -ENOENT;
4015 goto out;
4016 }
Chris Wilsone6994ae2012-07-10 10:27:08 +01004017
Chris Wilson651d7942013-08-08 14:41:10 +01004018 switch (obj->cache_level) {
4019 case I915_CACHE_LLC:
4020 case I915_CACHE_L3_LLC:
4021 args->caching = I915_CACHING_CACHED;
4022 break;
4023
Chris Wilson4257d3b2013-08-08 14:41:11 +01004024 case I915_CACHE_WT:
4025 args->caching = I915_CACHING_DISPLAY;
4026 break;
4027
Chris Wilson651d7942013-08-08 14:41:10 +01004028 default:
4029 args->caching = I915_CACHING_NONE;
4030 break;
4031 }
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004032out:
4033 rcu_read_unlock();
4034 return err;
Chris Wilsone6994ae2012-07-10 10:27:08 +01004035}
4036
Ben Widawsky199adf42012-09-21 17:01:20 -07004037int i915_gem_set_caching_ioctl(struct drm_device *dev, void *data,
4038 struct drm_file *file)
Chris Wilsone6994ae2012-07-10 10:27:08 +01004039{
Chris Wilson9c870d02016-10-24 13:42:15 +01004040 struct drm_i915_private *i915 = to_i915(dev);
Ben Widawsky199adf42012-09-21 17:01:20 -07004041 struct drm_i915_gem_caching *args = data;
Chris Wilsone6994ae2012-07-10 10:27:08 +01004042 struct drm_i915_gem_object *obj;
4043 enum i915_cache_level level;
Chris Wilsond65415d2017-01-19 08:22:10 +00004044 int ret = 0;
Chris Wilsone6994ae2012-07-10 10:27:08 +01004045
Ben Widawsky199adf42012-09-21 17:01:20 -07004046 switch (args->caching) {
4047 case I915_CACHING_NONE:
Chris Wilsone6994ae2012-07-10 10:27:08 +01004048 level = I915_CACHE_NONE;
4049 break;
Ben Widawsky199adf42012-09-21 17:01:20 -07004050 case I915_CACHING_CACHED:
Imre Deake5756c12015-08-14 18:43:30 +03004051 /*
4052 * Due to a HW issue on BXT A stepping, GPU stores via a
4053 * snooped mapping may leave stale data in a corresponding CPU
4054 * cacheline, whereas normally such cachelines would get
4055 * invalidated.
4056 */
Chris Wilson9c870d02016-10-24 13:42:15 +01004057 if (!HAS_LLC(i915) && !HAS_SNOOP(i915))
Imre Deake5756c12015-08-14 18:43:30 +03004058 return -ENODEV;
4059
Chris Wilsone6994ae2012-07-10 10:27:08 +01004060 level = I915_CACHE_LLC;
4061 break;
Chris Wilson4257d3b2013-08-08 14:41:11 +01004062 case I915_CACHING_DISPLAY:
Chris Wilson9c870d02016-10-24 13:42:15 +01004063 level = HAS_WT(i915) ? I915_CACHE_WT : I915_CACHE_NONE;
Chris Wilson4257d3b2013-08-08 14:41:11 +01004064 break;
Chris Wilsone6994ae2012-07-10 10:27:08 +01004065 default:
4066 return -EINVAL;
4067 }
4068
Chris Wilsond65415d2017-01-19 08:22:10 +00004069 obj = i915_gem_object_lookup(file, args->handle);
4070 if (!obj)
4071 return -ENOENT;
4072
Tina Zhanga03f3952017-11-14 10:25:13 +00004073 /*
4074 * The caching mode of proxy object is handled by its generator, and
4075 * not allowed to be changed by userspace.
4076 */
4077 if (i915_gem_object_is_proxy(obj)) {
4078 ret = -ENXIO;
4079 goto out;
4080 }
4081
Chris Wilsond65415d2017-01-19 08:22:10 +00004082 if (obj->cache_level == level)
4083 goto out;
4084
4085 ret = i915_gem_object_wait(obj,
4086 I915_WAIT_INTERRUPTIBLE,
4087 MAX_SCHEDULE_TIMEOUT,
4088 to_rps_client(file));
4089 if (ret)
4090 goto out;
4091
Ben Widawsky3bc29132012-09-26 16:15:20 -07004092 ret = i915_mutex_lock_interruptible(dev);
4093 if (ret)
Chris Wilsond65415d2017-01-19 08:22:10 +00004094 goto out;
Chris Wilsone6994ae2012-07-10 10:27:08 +01004095
4096 ret = i915_gem_object_set_cache_level(obj, level);
Chris Wilsone6994ae2012-07-10 10:27:08 +01004097 mutex_unlock(&dev->struct_mutex);
Chris Wilsond65415d2017-01-19 08:22:10 +00004098
4099out:
4100 i915_gem_object_put(obj);
Chris Wilsone6994ae2012-07-10 10:27:08 +01004101 return ret;
4102}
4103
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08004104/*
Dhinakaran Pandiyan07bcd992018-03-06 19:34:18 -08004105 * Prepare buffer for display plane (scanout, cursors, etc). Can be called from
4106 * an uninterruptible phase (modesetting) and allows any flushes to be pipelined
4107 * (for pageflips). We only flush the caches while preparing the buffer for
4108 * display, the callers are responsible for frontbuffer flush.
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08004109 */
Chris Wilson058d88c2016-08-15 10:49:06 +01004110struct i915_vma *
Chris Wilson2da3b9b2011-04-14 09:41:17 +01004111i915_gem_object_pin_to_display_plane(struct drm_i915_gem_object *obj,
4112 u32 alignment,
Chris Wilson59354852018-02-20 13:42:06 +00004113 const struct i915_ggtt_view *view,
4114 unsigned int flags)
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08004115{
Chris Wilson058d88c2016-08-15 10:49:06 +01004116 struct i915_vma *vma;
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08004117 int ret;
4118
Chris Wilson4c7d62c2016-10-28 13:58:32 +01004119 lockdep_assert_held(&obj->base.dev->struct_mutex);
4120
Chris Wilsonbd3d2252017-10-13 21:26:14 +01004121 /* Mark the global pin early so that we account for the
Chris Wilsoncc98b412013-08-09 12:25:09 +01004122 * display coherency whilst setting up the cache domains.
4123 */
Chris Wilsonbd3d2252017-10-13 21:26:14 +01004124 obj->pin_global++;
Chris Wilsoncc98b412013-08-09 12:25:09 +01004125
Eric Anholta7ef0642011-03-29 16:59:54 -07004126 /* The display engine is not coherent with the LLC cache on gen6. As
4127 * a result, we make sure that the pinning that is about to occur is
4128 * done with uncached PTEs. This is lowest common denominator for all
4129 * chipsets.
4130 *
4131 * However for gen6+, we could do better by using the GFDT bit instead
4132 * of uncaching, which would allow us to flush all the LLC-cached data
4133 * with that bit in the PTE to main memory with just one PIPE_CONTROL.
4134 */
Chris Wilson651d7942013-08-08 14:41:10 +01004135 ret = i915_gem_object_set_cache_level(obj,
Tvrtko Ursulin86527442016-10-13 11:03:00 +01004136 HAS_WT(to_i915(obj->base.dev)) ?
4137 I915_CACHE_WT : I915_CACHE_NONE);
Chris Wilson058d88c2016-08-15 10:49:06 +01004138 if (ret) {
4139 vma = ERR_PTR(ret);
Chris Wilsonbd3d2252017-10-13 21:26:14 +01004140 goto err_unpin_global;
Chris Wilson058d88c2016-08-15 10:49:06 +01004141 }
Eric Anholta7ef0642011-03-29 16:59:54 -07004142
Chris Wilson2da3b9b2011-04-14 09:41:17 +01004143 /* As the user may map the buffer once pinned in the display plane
4144 * (e.g. libkms for the bootup splash), we have to ensure that we
Chris Wilson2efb8132016-08-18 17:17:06 +01004145 * always use map_and_fenceable for all scanout buffers. However,
4146 * it may simply be too big to fit into mappable, in which case
4147 * put it anyway and hope that userspace can cope (but always first
4148 * try to preserve the existing ABI).
Chris Wilson2da3b9b2011-04-14 09:41:17 +01004149 */
Chris Wilson2efb8132016-08-18 17:17:06 +01004150 vma = ERR_PTR(-ENOSPC);
Chris Wilson59354852018-02-20 13:42:06 +00004151 if ((flags & PIN_MAPPABLE) == 0 &&
4152 (!view || view->type == I915_GGTT_VIEW_NORMAL))
Chris Wilson2efb8132016-08-18 17:17:06 +01004153 vma = i915_gem_object_ggtt_pin(obj, view, 0, alignment,
Chris Wilson59354852018-02-20 13:42:06 +00004154 flags |
4155 PIN_MAPPABLE |
4156 PIN_NONBLOCK);
4157 if (IS_ERR(vma))
Chris Wilson767a2222016-11-07 11:01:28 +00004158 vma = i915_gem_object_ggtt_pin(obj, view, 0, alignment, flags);
Chris Wilson058d88c2016-08-15 10:49:06 +01004159 if (IS_ERR(vma))
Chris Wilsonbd3d2252017-10-13 21:26:14 +01004160 goto err_unpin_global;
Chris Wilson2da3b9b2011-04-14 09:41:17 +01004161
Chris Wilsond8923dc2016-08-18 17:17:07 +01004162 vma->display_alignment = max_t(u64, vma->display_alignment, alignment);
4163
Chris Wilson5a97bcc2017-02-22 11:40:46 +00004164 __i915_gem_object_flush_for_display(obj);
Chris Wilsonb118c1e2010-05-27 13:18:14 +01004165
Chris Wilson2da3b9b2011-04-14 09:41:17 +01004166 /* It should now be out of any other write domains, and we can update
4167 * the domain values for our changes.
4168 */
Christian Königc0a51fd2018-02-16 13:43:38 +01004169 obj->read_domains |= I915_GEM_DOMAIN_GTT;
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08004170
Chris Wilson058d88c2016-08-15 10:49:06 +01004171 return vma;
Chris Wilsoncc98b412013-08-09 12:25:09 +01004172
Chris Wilsonbd3d2252017-10-13 21:26:14 +01004173err_unpin_global:
4174 obj->pin_global--;
Chris Wilson058d88c2016-08-15 10:49:06 +01004175 return vma;
Chris Wilsoncc98b412013-08-09 12:25:09 +01004176}
4177
4178void
Chris Wilson058d88c2016-08-15 10:49:06 +01004179i915_gem_object_unpin_from_display_plane(struct i915_vma *vma)
Chris Wilsoncc98b412013-08-09 12:25:09 +01004180{
Chris Wilson49d73912016-11-29 09:50:08 +00004181 lockdep_assert_held(&vma->vm->i915->drm.struct_mutex);
Chris Wilson4c7d62c2016-10-28 13:58:32 +01004182
Chris Wilsonbd3d2252017-10-13 21:26:14 +01004183 if (WARN_ON(vma->obj->pin_global == 0))
Tvrtko Ursulin8a0c39b2015-04-13 11:50:09 +01004184 return;
4185
Chris Wilsonbd3d2252017-10-13 21:26:14 +01004186 if (--vma->obj->pin_global == 0)
Chris Wilsonf51455d2017-01-10 14:47:34 +00004187 vma->display_alignment = I915_GTT_MIN_ALIGNMENT;
Tvrtko Ursuline6617332015-03-23 11:10:33 +00004188
Chris Wilson383d5822016-08-18 17:17:08 +01004189 /* Bump the LRU to try and avoid premature eviction whilst flipping */
Chris Wilsonbefedbb2017-01-19 19:26:55 +00004190 i915_gem_object_bump_inactive_ggtt(vma->obj);
Chris Wilson383d5822016-08-18 17:17:08 +01004191
Chris Wilson058d88c2016-08-15 10:49:06 +01004192 i915_vma_unpin(vma);
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08004193}
4194
Eric Anholte47c68e2008-11-14 13:35:19 -08004195/**
4196 * Moves a single object to the CPU read, and possibly write domain.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01004197 * @obj: object to act on
4198 * @write: requesting write or read-only access
Eric Anholte47c68e2008-11-14 13:35:19 -08004199 *
4200 * This function returns when the move is complete, including waiting on
4201 * flushes to occur.
4202 */
Chris Wilsondabdfe02012-03-26 10:10:27 +02004203int
Chris Wilson919926a2010-11-12 13:42:53 +00004204i915_gem_object_set_to_cpu_domain(struct drm_i915_gem_object *obj, bool write)
Eric Anholte47c68e2008-11-14 13:35:19 -08004205{
Eric Anholte47c68e2008-11-14 13:35:19 -08004206 int ret;
4207
Chris Wilsone95433c2016-10-28 13:58:27 +01004208 lockdep_assert_held(&obj->base.dev->struct_mutex);
Chris Wilson4c7d62c2016-10-28 13:58:32 +01004209
Chris Wilsone95433c2016-10-28 13:58:27 +01004210 ret = i915_gem_object_wait(obj,
4211 I915_WAIT_INTERRUPTIBLE |
4212 I915_WAIT_LOCKED |
4213 (write ? I915_WAIT_ALL : 0),
4214 MAX_SCHEDULE_TIMEOUT,
4215 NULL);
Chris Wilson88241782011-01-07 17:09:48 +00004216 if (ret)
4217 return ret;
4218
Chris Wilsonef749212017-04-12 12:01:10 +01004219 flush_write_domain(obj, ~I915_GEM_DOMAIN_CPU);
Eric Anholte47c68e2008-11-14 13:35:19 -08004220
Eric Anholte47c68e2008-11-14 13:35:19 -08004221 /* Flush the CPU cache if it's still invalid. */
Christian Königc0a51fd2018-02-16 13:43:38 +01004222 if ((obj->read_domains & I915_GEM_DOMAIN_CPU) == 0) {
Chris Wilson57822dc2017-02-22 11:40:48 +00004223 i915_gem_clflush_object(obj, I915_CLFLUSH_SYNC);
Christian Königc0a51fd2018-02-16 13:43:38 +01004224 obj->read_domains |= I915_GEM_DOMAIN_CPU;
Eric Anholte47c68e2008-11-14 13:35:19 -08004225 }
4226
4227 /* It should now be out of any other write domains, and we can update
4228 * the domain values for our changes.
4229 */
Christian Königc0a51fd2018-02-16 13:43:38 +01004230 GEM_BUG_ON(obj->write_domain & ~I915_GEM_DOMAIN_CPU);
Eric Anholte47c68e2008-11-14 13:35:19 -08004231
4232 /* If we're writing through the CPU, then the GPU read domains will
4233 * need to be invalidated at next use.
4234 */
Chris Wilsone27ab732017-06-15 13:38:49 +01004235 if (write)
4236 __start_cpu_write(obj);
Eric Anholt2ef7eea2008-11-10 10:53:25 -08004237
4238 return 0;
4239}
4240
Eric Anholt673a3942008-07-30 12:06:12 -07004241/* Throttle our rendering by waiting until the ring has completed our requests
4242 * emitted over 20 msec ago.
4243 *
Eric Anholtb9624422009-06-03 07:27:35 +00004244 * Note that if we were to use the current jiffies each time around the loop,
4245 * we wouldn't escape the function with any frames outstanding if the time to
4246 * render a frame was over 20ms.
4247 *
Eric Anholt673a3942008-07-30 12:06:12 -07004248 * This should get us reasonable parallelism between CPU and GPU but also
4249 * relatively low latency when blocking on a particular request to finish.
4250 */
4251static int
Chris Wilsonf787a5f2010-09-24 16:02:42 +01004252i915_gem_ring_throttle(struct drm_device *dev, struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07004253{
Chris Wilsonfac5e232016-07-04 11:34:36 +01004254 struct drm_i915_private *dev_priv = to_i915(dev);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01004255 struct drm_i915_file_private *file_priv = file->driver_priv;
Chris Wilsond0bc54f2015-05-21 21:01:48 +01004256 unsigned long recent_enough = jiffies - DRM_I915_THROTTLE_JIFFIES;
Chris Wilsone61e0f52018-02-21 09:56:36 +00004257 struct i915_request *request, *target = NULL;
Chris Wilsone95433c2016-10-28 13:58:27 +01004258 long ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004259
Chris Wilsonf4457ae2016-04-13 17:35:08 +01004260 /* ABI: return -EIO if already wedged */
4261 if (i915_terminally_wedged(&dev_priv->gpu_error))
4262 return -EIO;
Chris Wilsone110e8d2011-01-26 15:39:14 +00004263
Chris Wilson1c255952010-09-26 11:03:27 +01004264 spin_lock(&file_priv->mm.lock);
Chris Wilsonc8659ef2017-03-02 12:25:25 +00004265 list_for_each_entry(request, &file_priv->mm.request_list, client_link) {
Eric Anholtb9624422009-06-03 07:27:35 +00004266 if (time_after_eq(request->emitted_jiffies, recent_enough))
4267 break;
4268
Chris Wilsonc8659ef2017-03-02 12:25:25 +00004269 if (target) {
4270 list_del(&target->client_link);
4271 target->file_priv = NULL;
4272 }
John Harrisonfcfa423c2015-05-29 17:44:12 +01004273
John Harrison54fb2412014-11-24 18:49:27 +00004274 target = request;
Eric Anholtb9624422009-06-03 07:27:35 +00004275 }
John Harrisonff865882014-11-24 18:49:28 +00004276 if (target)
Chris Wilsone61e0f52018-02-21 09:56:36 +00004277 i915_request_get(target);
Chris Wilson1c255952010-09-26 11:03:27 +01004278 spin_unlock(&file_priv->mm.lock);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01004279
John Harrison54fb2412014-11-24 18:49:27 +00004280 if (target == NULL)
Chris Wilsonf787a5f2010-09-24 16:02:42 +01004281 return 0;
4282
Chris Wilsone61e0f52018-02-21 09:56:36 +00004283 ret = i915_request_wait(target,
Chris Wilsone95433c2016-10-28 13:58:27 +01004284 I915_WAIT_INTERRUPTIBLE,
4285 MAX_SCHEDULE_TIMEOUT);
Chris Wilsone61e0f52018-02-21 09:56:36 +00004286 i915_request_put(target);
John Harrisonff865882014-11-24 18:49:28 +00004287
Chris Wilsone95433c2016-10-28 13:58:27 +01004288 return ret < 0 ? ret : 0;
Eric Anholt673a3942008-07-30 12:06:12 -07004289}
4290
Chris Wilson058d88c2016-08-15 10:49:06 +01004291struct i915_vma *
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02004292i915_gem_object_ggtt_pin(struct drm_i915_gem_object *obj,
4293 const struct i915_ggtt_view *view,
Chris Wilson91b2db62016-08-04 16:32:23 +01004294 u64 size,
Chris Wilson2ffffd02016-08-04 16:32:22 +01004295 u64 alignment,
4296 u64 flags)
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02004297{
Chris Wilsonad16d2e2016-10-13 09:55:04 +01004298 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
Chris Wilson82ad6442018-06-05 16:37:58 +01004299 struct i915_address_space *vm = &dev_priv->ggtt.vm;
Chris Wilson59bfa122016-08-04 16:32:31 +01004300 struct i915_vma *vma;
4301 int ret;
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03004302
Chris Wilson4c7d62c2016-10-28 13:58:32 +01004303 lockdep_assert_held(&obj->base.dev->struct_mutex);
4304
Chris Wilsonac87a6fd2018-02-20 13:42:05 +00004305 if (flags & PIN_MAPPABLE &&
4306 (!view || view->type == I915_GGTT_VIEW_NORMAL)) {
Chris Wilson43ae70d92017-10-09 09:44:01 +01004307 /* If the required space is larger than the available
4308 * aperture, we will not able to find a slot for the
4309 * object and unbinding the object now will be in
4310 * vain. Worse, doing so may cause us to ping-pong
4311 * the object in and out of the Global GTT and
4312 * waste a lot of cycles under the mutex.
4313 */
4314 if (obj->base.size > dev_priv->ggtt.mappable_end)
4315 return ERR_PTR(-E2BIG);
4316
4317 /* If NONBLOCK is set the caller is optimistically
4318 * trying to cache the full object within the mappable
4319 * aperture, and *must* have a fallback in place for
4320 * situations where we cannot bind the object. We
4321 * can be a little more lax here and use the fallback
4322 * more often to avoid costly migrations of ourselves
4323 * and other objects within the aperture.
4324 *
4325 * Half-the-aperture is used as a simple heuristic.
4326 * More interesting would to do search for a free
4327 * block prior to making the commitment to unbind.
4328 * That caters for the self-harm case, and with a
4329 * little more heuristics (e.g. NOFAULT, NOEVICT)
4330 * we could try to minimise harm to others.
4331 */
4332 if (flags & PIN_NONBLOCK &&
4333 obj->base.size > dev_priv->ggtt.mappable_end / 2)
4334 return ERR_PTR(-ENOSPC);
4335 }
4336
Chris Wilson718659a2017-01-16 15:21:28 +00004337 vma = i915_vma_instance(obj, vm, view);
Chris Wilsone0216b72017-01-19 19:26:57 +00004338 if (unlikely(IS_ERR(vma)))
Chris Wilson058d88c2016-08-15 10:49:06 +01004339 return vma;
Chris Wilson59bfa122016-08-04 16:32:31 +01004340
4341 if (i915_vma_misplaced(vma, size, alignment, flags)) {
Chris Wilson43ae70d92017-10-09 09:44:01 +01004342 if (flags & PIN_NONBLOCK) {
4343 if (i915_vma_is_pinned(vma) || i915_vma_is_active(vma))
4344 return ERR_PTR(-ENOSPC);
Chris Wilson59bfa122016-08-04 16:32:31 +01004345
Chris Wilson43ae70d92017-10-09 09:44:01 +01004346 if (flags & PIN_MAPPABLE &&
Chris Wilson944397f2017-01-09 16:16:11 +00004347 vma->fence_size > dev_priv->ggtt.mappable_end / 2)
Chris Wilsonad16d2e2016-10-13 09:55:04 +01004348 return ERR_PTR(-ENOSPC);
4349 }
4350
Chris Wilson59bfa122016-08-04 16:32:31 +01004351 WARN(i915_vma_is_pinned(vma),
4352 "bo is already pinned in ggtt with incorrect alignment:"
Chris Wilson05a20d02016-08-18 17:16:55 +01004353 " offset=%08x, req.alignment=%llx,"
4354 " req.map_and_fenceable=%d, vma->map_and_fenceable=%d\n",
4355 i915_ggtt_offset(vma), alignment,
Chris Wilson59bfa122016-08-04 16:32:31 +01004356 !!(flags & PIN_MAPPABLE),
Chris Wilson05a20d02016-08-18 17:16:55 +01004357 i915_vma_is_map_and_fenceable(vma));
Chris Wilson59bfa122016-08-04 16:32:31 +01004358 ret = i915_vma_unbind(vma);
4359 if (ret)
Chris Wilson058d88c2016-08-15 10:49:06 +01004360 return ERR_PTR(ret);
Chris Wilson59bfa122016-08-04 16:32:31 +01004361 }
4362
Chris Wilson058d88c2016-08-15 10:49:06 +01004363 ret = i915_vma_pin(vma, size, alignment, flags | PIN_GLOBAL);
4364 if (ret)
4365 return ERR_PTR(ret);
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02004366
Chris Wilson058d88c2016-08-15 10:49:06 +01004367 return vma;
Eric Anholt673a3942008-07-30 12:06:12 -07004368}
4369
Chris Wilsonedf6b762016-08-09 09:23:33 +01004370static __always_inline unsigned int __busy_read_flag(unsigned int id)
Chris Wilson3fdc13c2016-08-05 10:14:18 +01004371{
4372 /* Note that we could alias engines in the execbuf API, but
4373 * that would be very unwise as it prevents userspace from
4374 * fine control over engine selection. Ahem.
4375 *
4376 * This should be something like EXEC_MAX_ENGINE instead of
4377 * I915_NUM_ENGINES.
4378 */
4379 BUILD_BUG_ON(I915_NUM_ENGINES > 16);
4380 return 0x10000 << id;
4381}
4382
4383static __always_inline unsigned int __busy_write_id(unsigned int id)
4384{
Chris Wilson70cb4722016-08-09 18:08:25 +01004385 /* The uABI guarantees an active writer is also amongst the read
4386 * engines. This would be true if we accessed the activity tracking
4387 * under the lock, but as we perform the lookup of the object and
4388 * its activity locklessly we can not guarantee that the last_write
4389 * being active implies that we have set the same engine flag from
4390 * last_read - hence we always set both read and write busy for
4391 * last_write.
4392 */
4393 return id | __busy_read_flag(id);
Chris Wilson3fdc13c2016-08-05 10:14:18 +01004394}
4395
Chris Wilsonedf6b762016-08-09 09:23:33 +01004396static __always_inline unsigned int
Chris Wilsond07f0e52016-10-28 13:58:44 +01004397__busy_set_if_active(const struct dma_fence *fence,
Chris Wilson3fdc13c2016-08-05 10:14:18 +01004398 unsigned int (*flag)(unsigned int id))
4399{
Chris Wilsone61e0f52018-02-21 09:56:36 +00004400 struct i915_request *rq;
Chris Wilson12555012016-08-16 09:50:40 +01004401
Chris Wilsond07f0e52016-10-28 13:58:44 +01004402 /* We have to check the current hw status of the fence as the uABI
4403 * guarantees forward progress. We could rely on the idle worker
4404 * to eventually flush us, but to minimise latency just ask the
4405 * hardware.
4406 *
4407 * Note we only report on the status of native fences.
4408 */
4409 if (!dma_fence_is_i915(fence))
Chris Wilson12555012016-08-16 09:50:40 +01004410 return 0;
4411
Chris Wilsond07f0e52016-10-28 13:58:44 +01004412 /* opencode to_request() in order to avoid const warnings */
Chris Wilsone61e0f52018-02-21 09:56:36 +00004413 rq = container_of(fence, struct i915_request, fence);
4414 if (i915_request_completed(rq))
Chris Wilsond07f0e52016-10-28 13:58:44 +01004415 return 0;
4416
Chris Wilson1d39f282017-04-11 13:43:06 +01004417 return flag(rq->engine->uabi_id);
Chris Wilson3fdc13c2016-08-05 10:14:18 +01004418}
4419
Chris Wilsonedf6b762016-08-09 09:23:33 +01004420static __always_inline unsigned int
Chris Wilsond07f0e52016-10-28 13:58:44 +01004421busy_check_reader(const struct dma_fence *fence)
Chris Wilson3fdc13c2016-08-05 10:14:18 +01004422{
Chris Wilsond07f0e52016-10-28 13:58:44 +01004423 return __busy_set_if_active(fence, __busy_read_flag);
Chris Wilson3fdc13c2016-08-05 10:14:18 +01004424}
4425
Chris Wilsonedf6b762016-08-09 09:23:33 +01004426static __always_inline unsigned int
Chris Wilsond07f0e52016-10-28 13:58:44 +01004427busy_check_writer(const struct dma_fence *fence)
Chris Wilson3fdc13c2016-08-05 10:14:18 +01004428{
Chris Wilsond07f0e52016-10-28 13:58:44 +01004429 if (!fence)
4430 return 0;
4431
4432 return __busy_set_if_active(fence, __busy_write_id);
Chris Wilson3fdc13c2016-08-05 10:14:18 +01004433}
4434
Eric Anholt673a3942008-07-30 12:06:12 -07004435int
Eric Anholt673a3942008-07-30 12:06:12 -07004436i915_gem_busy_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00004437 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07004438{
4439 struct drm_i915_gem_busy *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00004440 struct drm_i915_gem_object *obj;
Chris Wilsond07f0e52016-10-28 13:58:44 +01004441 struct reservation_object_list *list;
4442 unsigned int seq;
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004443 int err;
Eric Anholt673a3942008-07-30 12:06:12 -07004444
Chris Wilsond07f0e52016-10-28 13:58:44 +01004445 err = -ENOENT;
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004446 rcu_read_lock();
4447 obj = i915_gem_object_lookup_rcu(file, args->handle);
Chris Wilsond07f0e52016-10-28 13:58:44 +01004448 if (!obj)
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004449 goto out;
Chris Wilsond07f0e52016-10-28 13:58:44 +01004450
4451 /* A discrepancy here is that we do not report the status of
4452 * non-i915 fences, i.e. even though we may report the object as idle,
4453 * a call to set-domain may still stall waiting for foreign rendering.
4454 * This also means that wait-ioctl may report an object as busy,
4455 * where busy-ioctl considers it idle.
4456 *
4457 * We trade the ability to warn of foreign fences to report on which
4458 * i915 engines are active for the object.
4459 *
4460 * Alternatively, we can trade that extra information on read/write
4461 * activity with
4462 * args->busy =
4463 * !reservation_object_test_signaled_rcu(obj->resv, true);
4464 * to report the overall busyness. This is what the wait-ioctl does.
4465 *
4466 */
4467retry:
4468 seq = raw_read_seqcount(&obj->resv->seq);
4469
4470 /* Translate the exclusive fence to the READ *and* WRITE engine */
4471 args->busy = busy_check_writer(rcu_dereference(obj->resv->fence_excl));
4472
4473 /* Translate shared fences to READ set of engines */
4474 list = rcu_dereference(obj->resv->fence);
4475 if (list) {
4476 unsigned int shared_count = list->shared_count, i;
4477
4478 for (i = 0; i < shared_count; ++i) {
4479 struct dma_fence *fence =
4480 rcu_dereference(list->shared[i]);
4481
4482 args->busy |= busy_check_reader(fence);
4483 }
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004484 }
Zou Nan haid1b851f2010-05-21 09:08:57 +08004485
Chris Wilsond07f0e52016-10-28 13:58:44 +01004486 if (args->busy && read_seqcount_retry(&obj->resv->seq, seq))
4487 goto retry;
Chris Wilson426960b2016-01-15 16:51:46 +00004488
Chris Wilsond07f0e52016-10-28 13:58:44 +01004489 err = 0;
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004490out:
4491 rcu_read_unlock();
4492 return err;
Eric Anholt673a3942008-07-30 12:06:12 -07004493}
4494
4495int
4496i915_gem_throttle_ioctl(struct drm_device *dev, void *data,
4497 struct drm_file *file_priv)
4498{
Akshay Joshi0206e352011-08-16 15:34:10 -04004499 return i915_gem_ring_throttle(dev, file_priv);
Eric Anholt673a3942008-07-30 12:06:12 -07004500}
4501
Chris Wilson3ef94da2009-09-14 16:50:29 +01004502int
4503i915_gem_madvise_ioctl(struct drm_device *dev, void *data,
4504 struct drm_file *file_priv)
4505{
Chris Wilsonfac5e232016-07-04 11:34:36 +01004506 struct drm_i915_private *dev_priv = to_i915(dev);
Chris Wilson3ef94da2009-09-14 16:50:29 +01004507 struct drm_i915_gem_madvise *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00004508 struct drm_i915_gem_object *obj;
Chris Wilson1233e2d2016-10-28 13:58:37 +01004509 int err;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004510
4511 switch (args->madv) {
4512 case I915_MADV_DONTNEED:
4513 case I915_MADV_WILLNEED:
4514 break;
4515 default:
4516 return -EINVAL;
4517 }
4518
Chris Wilson03ac0642016-07-20 13:31:51 +01004519 obj = i915_gem_object_lookup(file_priv, args->handle);
Chris Wilson1233e2d2016-10-28 13:58:37 +01004520 if (!obj)
4521 return -ENOENT;
4522
4523 err = mutex_lock_interruptible(&obj->mm.lock);
4524 if (err)
4525 goto out;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004526
Chris Wilsonf1fa4f42017-10-13 21:26:13 +01004527 if (i915_gem_object_has_pages(obj) &&
Chris Wilson3e510a82016-08-05 10:14:23 +01004528 i915_gem_object_is_tiled(obj) &&
Daniel Vetter656bfa32014-11-20 09:26:30 +01004529 dev_priv->quirks & QUIRK_PIN_SWIZZLED_PAGES) {
Chris Wilsonbc0629a2016-11-01 10:03:17 +00004530 if (obj->mm.madv == I915_MADV_WILLNEED) {
4531 GEM_BUG_ON(!obj->mm.quirked);
Chris Wilsona4f5ea62016-10-28 13:58:35 +01004532 __i915_gem_object_unpin_pages(obj);
Chris Wilsonbc0629a2016-11-01 10:03:17 +00004533 obj->mm.quirked = false;
4534 }
4535 if (args->madv == I915_MADV_WILLNEED) {
Chris Wilson2c3a3f42016-11-04 10:30:01 +00004536 GEM_BUG_ON(obj->mm.quirked);
Chris Wilsona4f5ea62016-10-28 13:58:35 +01004537 __i915_gem_object_pin_pages(obj);
Chris Wilsonbc0629a2016-11-01 10:03:17 +00004538 obj->mm.quirked = true;
4539 }
Daniel Vetter656bfa32014-11-20 09:26:30 +01004540 }
4541
Chris Wilsona4f5ea62016-10-28 13:58:35 +01004542 if (obj->mm.madv != __I915_MADV_PURGED)
4543 obj->mm.madv = args->madv;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004544
Chris Wilson6c085a72012-08-20 11:40:46 +02004545 /* if the object is no longer attached, discard its backing storage */
Chris Wilsonf1fa4f42017-10-13 21:26:13 +01004546 if (obj->mm.madv == I915_MADV_DONTNEED &&
4547 !i915_gem_object_has_pages(obj))
Chris Wilson2d7ef392009-09-20 23:13:10 +01004548 i915_gem_object_truncate(obj);
4549
Chris Wilsona4f5ea62016-10-28 13:58:35 +01004550 args->retained = obj->mm.madv != __I915_MADV_PURGED;
Chris Wilson1233e2d2016-10-28 13:58:37 +01004551 mutex_unlock(&obj->mm.lock);
Chris Wilsonbb6baf72009-09-22 14:24:13 +01004552
Chris Wilson1233e2d2016-10-28 13:58:37 +01004553out:
Chris Wilsonf8c417c2016-07-20 13:31:53 +01004554 i915_gem_object_put(obj);
Chris Wilson1233e2d2016-10-28 13:58:37 +01004555 return err;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004556}
4557
Chris Wilson5b8c8ae2016-11-16 19:07:04 +00004558static void
Chris Wilsone61e0f52018-02-21 09:56:36 +00004559frontbuffer_retire(struct i915_gem_active *active, struct i915_request *request)
Chris Wilson5b8c8ae2016-11-16 19:07:04 +00004560{
4561 struct drm_i915_gem_object *obj =
4562 container_of(active, typeof(*obj), frontbuffer_write);
4563
Chris Wilsond59b21e2017-02-22 11:40:49 +00004564 intel_fb_obj_flush(obj, ORIGIN_CS);
Chris Wilson5b8c8ae2016-11-16 19:07:04 +00004565}
4566
Chris Wilson37e680a2012-06-07 15:38:42 +01004567void i915_gem_object_init(struct drm_i915_gem_object *obj,
4568 const struct drm_i915_gem_object_ops *ops)
Chris Wilson0327d6b2012-08-11 15:41:06 +01004569{
Chris Wilson1233e2d2016-10-28 13:58:37 +01004570 mutex_init(&obj->mm.lock);
4571
Ben Widawsky2f633152013-07-17 12:19:03 -07004572 INIT_LIST_HEAD(&obj->vma_list);
Chris Wilsond1b48c12017-08-16 09:52:08 +01004573 INIT_LIST_HEAD(&obj->lut_list);
Chris Wilson8d9d5742015-04-07 16:20:38 +01004574 INIT_LIST_HEAD(&obj->batch_pool_link);
Chris Wilson0327d6b2012-08-11 15:41:06 +01004575
Chris Wilson8811d612018-11-09 09:03:11 +00004576 init_rcu_head(&obj->rcu);
4577
Chris Wilson37e680a2012-06-07 15:38:42 +01004578 obj->ops = ops;
4579
Chris Wilsond07f0e52016-10-28 13:58:44 +01004580 reservation_object_init(&obj->__builtin_resv);
4581 obj->resv = &obj->__builtin_resv;
4582
Chris Wilson50349242016-08-18 17:17:04 +01004583 obj->frontbuffer_ggtt_origin = ORIGIN_GTT;
Chris Wilson5b8c8ae2016-11-16 19:07:04 +00004584 init_request_active(&obj->frontbuffer_write, frontbuffer_retire);
Chris Wilsona4f5ea62016-10-28 13:58:35 +01004585
4586 obj->mm.madv = I915_MADV_WILLNEED;
4587 INIT_RADIX_TREE(&obj->mm.get_page.radix, GFP_KERNEL | __GFP_NOWARN);
4588 mutex_init(&obj->mm.get_page.lock);
Chris Wilson0327d6b2012-08-11 15:41:06 +01004589
Dave Gordonf19ec8c2016-07-04 11:34:37 +01004590 i915_gem_info_add_obj(to_i915(obj->base.dev), obj->base.size);
Chris Wilson0327d6b2012-08-11 15:41:06 +01004591}
4592
Chris Wilson37e680a2012-06-07 15:38:42 +01004593static const struct drm_i915_gem_object_ops i915_gem_object_ops = {
Tvrtko Ursulin3599a912016-11-01 14:44:10 +00004594 .flags = I915_GEM_OBJECT_HAS_STRUCT_PAGE |
4595 I915_GEM_OBJECT_IS_SHRINKABLE,
Chris Wilson7c55e2c2017-03-07 12:03:38 +00004596
Chris Wilson37e680a2012-06-07 15:38:42 +01004597 .get_pages = i915_gem_object_get_pages_gtt,
4598 .put_pages = i915_gem_object_put_pages_gtt,
Chris Wilson7c55e2c2017-03-07 12:03:38 +00004599
4600 .pwrite = i915_gem_object_pwrite_gtt,
Chris Wilson37e680a2012-06-07 15:38:42 +01004601};
4602
Matthew Auld465c4032017-10-06 23:18:14 +01004603static int i915_gem_object_create_shmem(struct drm_device *dev,
4604 struct drm_gem_object *obj,
4605 size_t size)
4606{
4607 struct drm_i915_private *i915 = to_i915(dev);
4608 unsigned long flags = VM_NORESERVE;
4609 struct file *filp;
4610
4611 drm_gem_private_object_init(dev, obj, size);
4612
4613 if (i915->mm.gemfs)
4614 filp = shmem_file_setup_with_mnt(i915->mm.gemfs, "i915", size,
4615 flags);
4616 else
4617 filp = shmem_file_setup("i915", size, flags);
4618
4619 if (IS_ERR(filp))
4620 return PTR_ERR(filp);
4621
4622 obj->filp = filp;
4623
4624 return 0;
4625}
4626
Chris Wilsonb4bcbe22016-10-18 13:02:49 +01004627struct drm_i915_gem_object *
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +00004628i915_gem_object_create(struct drm_i915_private *dev_priv, u64 size)
Daniel Vetterac52bc52010-04-09 19:05:06 +00004629{
Daniel Vetterc397b902010-04-09 19:05:07 +00004630 struct drm_i915_gem_object *obj;
Hugh Dickins5949eac2011-06-27 16:18:18 -07004631 struct address_space *mapping;
Chris Wilsonb8f55be2017-08-11 12:11:16 +01004632 unsigned int cache_level;
Daniel Vetter1a240d42012-11-29 22:18:51 +01004633 gfp_t mask;
Chris Wilsonfe3db792016-04-25 13:32:13 +01004634 int ret;
Daniel Vetterc397b902010-04-09 19:05:07 +00004635
Chris Wilsonb4bcbe22016-10-18 13:02:49 +01004636 /* There is a prevalence of the assumption that we fit the object's
4637 * page count inside a 32bit _signed_ variable. Let's document this and
4638 * catch if we ever need to fix it. In the meantime, if you do spot
4639 * such a local variable, please consider fixing!
4640 */
Tvrtko Ursulin7a3ee5d2017-03-30 17:31:30 +01004641 if (size >> PAGE_SHIFT > INT_MAX)
Chris Wilsonb4bcbe22016-10-18 13:02:49 +01004642 return ERR_PTR(-E2BIG);
4643
4644 if (overflows_type(size, obj->base.size))
4645 return ERR_PTR(-E2BIG);
4646
Tvrtko Ursulin187685c2016-12-01 14:16:36 +00004647 obj = i915_gem_object_alloc(dev_priv);
Daniel Vetterc397b902010-04-09 19:05:07 +00004648 if (obj == NULL)
Chris Wilsonfe3db792016-04-25 13:32:13 +01004649 return ERR_PTR(-ENOMEM);
Daniel Vetterc397b902010-04-09 19:05:07 +00004650
Matthew Auld465c4032017-10-06 23:18:14 +01004651 ret = i915_gem_object_create_shmem(&dev_priv->drm, &obj->base, size);
Chris Wilsonfe3db792016-04-25 13:32:13 +01004652 if (ret)
4653 goto fail;
Daniel Vetterc397b902010-04-09 19:05:07 +00004654
Chris Wilsonbed1ea92012-05-24 20:48:12 +01004655 mask = GFP_HIGHUSER | __GFP_RECLAIMABLE;
Jani Nikulac0f86832016-12-07 12:13:04 +02004656 if (IS_I965GM(dev_priv) || IS_I965G(dev_priv)) {
Chris Wilsonbed1ea92012-05-24 20:48:12 +01004657 /* 965gm cannot relocate objects above 4GiB. */
4658 mask &= ~__GFP_HIGHMEM;
4659 mask |= __GFP_DMA32;
4660 }
4661
Al Viro93c76a32015-12-04 23:45:44 -05004662 mapping = obj->base.filp->f_mapping;
Chris Wilsonbed1ea92012-05-24 20:48:12 +01004663 mapping_set_gfp_mask(mapping, mask);
Chris Wilson4846bf02017-06-09 12:03:46 +01004664 GEM_BUG_ON(!(mapping_gfp_mask(mapping) & __GFP_RECLAIM));
Hugh Dickins5949eac2011-06-27 16:18:18 -07004665
Chris Wilson37e680a2012-06-07 15:38:42 +01004666 i915_gem_object_init(obj, &i915_gem_object_ops);
Chris Wilson73aa8082010-09-30 11:46:12 +01004667
Christian Königc0a51fd2018-02-16 13:43:38 +01004668 obj->write_domain = I915_GEM_DOMAIN_CPU;
4669 obj->read_domains = I915_GEM_DOMAIN_CPU;
Daniel Vetterc397b902010-04-09 19:05:07 +00004670
Chris Wilsonb8f55be2017-08-11 12:11:16 +01004671 if (HAS_LLC(dev_priv))
Eugeni Dodonov3d29b842012-01-17 14:43:53 -02004672 /* On some devices, we can have the GPU use the LLC (the CPU
Eric Anholta1871112011-03-29 16:59:55 -07004673 * cache) for about a 10% performance improvement
4674 * compared to uncached. Graphics requests other than
4675 * display scanout are coherent with the CPU in
4676 * accessing this cache. This means in this mode we
4677 * don't need to clflush on the CPU side, and on the
4678 * GPU side we only need to flush internal caches to
4679 * get data visible to the CPU.
4680 *
4681 * However, we maintain the display planes as UC, and so
4682 * need to rebind when first used as such.
4683 */
Chris Wilsonb8f55be2017-08-11 12:11:16 +01004684 cache_level = I915_CACHE_LLC;
4685 else
4686 cache_level = I915_CACHE_NONE;
Eric Anholta1871112011-03-29 16:59:55 -07004687
Chris Wilsonb8f55be2017-08-11 12:11:16 +01004688 i915_gem_object_set_cache_coherency(obj, cache_level);
Chris Wilsone27ab732017-06-15 13:38:49 +01004689
Daniel Vetterd861e332013-07-24 23:25:03 +02004690 trace_i915_gem_object_create(obj);
4691
Chris Wilson05394f32010-11-08 19:18:58 +00004692 return obj;
Chris Wilsonfe3db792016-04-25 13:32:13 +01004693
4694fail:
4695 i915_gem_object_free(obj);
Chris Wilsonfe3db792016-04-25 13:32:13 +01004696 return ERR_PTR(ret);
Daniel Vetterac52bc52010-04-09 19:05:06 +00004697}
4698
Chris Wilson340fbd82014-05-22 09:16:52 +01004699static bool discard_backing_storage(struct drm_i915_gem_object *obj)
4700{
4701 /* If we are the last user of the backing storage (be it shmemfs
4702 * pages or stolen etc), we know that the pages are going to be
4703 * immediately released. In this case, we can then skip copying
4704 * back the contents from the GPU.
4705 */
4706
Chris Wilsona4f5ea62016-10-28 13:58:35 +01004707 if (obj->mm.madv != I915_MADV_WILLNEED)
Chris Wilson340fbd82014-05-22 09:16:52 +01004708 return false;
4709
4710 if (obj->base.filp == NULL)
4711 return true;
4712
4713 /* At first glance, this looks racy, but then again so would be
4714 * userspace racing mmap against close. However, the first external
4715 * reference to the filp can only be obtained through the
4716 * i915_gem_mmap_ioctl() which safeguards us against the user
4717 * acquiring such a reference whilst we are in the middle of
4718 * freeing the object.
4719 */
4720 return atomic_long_read(&obj->base.filp->f_count) == 1;
4721}
4722
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004723static void __i915_gem_free_objects(struct drm_i915_private *i915,
4724 struct llist_node *freed)
Chris Wilsonbe726152010-07-23 23:18:50 +01004725{
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004726 struct drm_i915_gem_object *obj, *on;
Chris Wilson538ef962019-01-14 14:21:18 +00004727 intel_wakeref_t wakeref;
Chris Wilsonbe726152010-07-23 23:18:50 +01004728
Chris Wilson538ef962019-01-14 14:21:18 +00004729 wakeref = intel_runtime_pm_get(i915);
Chris Wilsoncc731f52017-10-13 21:26:21 +01004730 llist_for_each_entry_safe(obj, on, freed, freed) {
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004731 struct i915_vma *vma, *vn;
Paulo Zanonif65c9162013-11-27 18:20:34 -02004732
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004733 trace_i915_gem_object_destroy(obj);
4734
Chris Wilsoncc731f52017-10-13 21:26:21 +01004735 mutex_lock(&i915->drm.struct_mutex);
4736
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004737 GEM_BUG_ON(i915_gem_object_is_active(obj));
4738 list_for_each_entry_safe(vma, vn,
4739 &obj->vma_list, obj_link) {
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004740 GEM_BUG_ON(i915_vma_is_active(vma));
4741 vma->flags &= ~I915_VMA_PIN_MASK;
Chris Wilson3365e222018-05-03 20:51:14 +01004742 i915_vma_destroy(vma);
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004743 }
Chris Wilsondb6c2b42016-11-01 11:54:00 +00004744 GEM_BUG_ON(!list_empty(&obj->vma_list));
4745 GEM_BUG_ON(!RB_EMPTY_ROOT(&obj->vma_tree));
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004746
Chris Wilsonf2123812017-10-16 12:40:37 +01004747 /* This serializes freeing with the shrinker. Since the free
4748 * is delayed, first by RCU then by the workqueue, we want the
4749 * shrinker to be able to free pages of unreferenced objects,
4750 * or else we may oom whilst there are plenty of deferred
4751 * freed objects.
4752 */
4753 if (i915_gem_object_has_pages(obj)) {
4754 spin_lock(&i915->mm.obj_lock);
4755 list_del_init(&obj->mm.link);
4756 spin_unlock(&i915->mm.obj_lock);
4757 }
4758
Chris Wilsoncc731f52017-10-13 21:26:21 +01004759 mutex_unlock(&i915->drm.struct_mutex);
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004760
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004761 GEM_BUG_ON(obj->bind_count);
Chris Wilsona65adaf2017-10-09 09:43:57 +01004762 GEM_BUG_ON(obj->userfault_count);
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004763 GEM_BUG_ON(atomic_read(&obj->frontbuffer_bits));
Chris Wilson67b48042017-08-22 12:05:16 +01004764 GEM_BUG_ON(!list_empty(&obj->lut_list));
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004765
4766 if (obj->ops->release)
4767 obj->ops->release(obj);
4768
4769 if (WARN_ON(i915_gem_object_has_pinned_pages(obj)))
4770 atomic_set(&obj->mm.pages_pin_count, 0);
Chris Wilson548625e2016-11-01 12:11:34 +00004771 __i915_gem_object_put_pages(obj, I915_MM_NORMAL);
Chris Wilsonf1fa4f42017-10-13 21:26:13 +01004772 GEM_BUG_ON(i915_gem_object_has_pages(obj));
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004773
4774 if (obj->base.import_attach)
4775 drm_prime_gem_destroy(&obj->base, NULL);
4776
Chris Wilsond07f0e52016-10-28 13:58:44 +01004777 reservation_object_fini(&obj->__builtin_resv);
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004778 drm_gem_object_release(&obj->base);
4779 i915_gem_info_remove_obj(i915, obj->base.size);
4780
4781 kfree(obj->bit_17);
4782 i915_gem_object_free(obj);
Chris Wilsoncc731f52017-10-13 21:26:21 +01004783
Chris Wilsonc9c704712018-02-19 22:06:31 +00004784 GEM_BUG_ON(!atomic_read(&i915->mm.free_count));
4785 atomic_dec(&i915->mm.free_count);
4786
Chris Wilsoncc731f52017-10-13 21:26:21 +01004787 if (on)
4788 cond_resched();
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004789 }
Chris Wilson538ef962019-01-14 14:21:18 +00004790 intel_runtime_pm_put(i915, wakeref);
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004791}
4792
4793static void i915_gem_flush_free_objects(struct drm_i915_private *i915)
4794{
4795 struct llist_node *freed;
4796
Chris Wilson87701b42017-10-13 21:26:20 +01004797 /* Free the oldest, most stale object to keep the free_list short */
4798 freed = NULL;
4799 if (!llist_empty(&i915->mm.free_list)) { /* quick test for hotpath */
4800 /* Only one consumer of llist_del_first() allowed */
4801 spin_lock(&i915->mm.free_lock);
4802 freed = llist_del_first(&i915->mm.free_list);
4803 spin_unlock(&i915->mm.free_lock);
4804 }
4805 if (unlikely(freed)) {
4806 freed->next = NULL;
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004807 __i915_gem_free_objects(i915, freed);
Chris Wilson87701b42017-10-13 21:26:20 +01004808 }
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004809}
4810
4811static void __i915_gem_free_work(struct work_struct *work)
4812{
4813 struct drm_i915_private *i915 =
4814 container_of(work, struct drm_i915_private, mm.free_work);
4815 struct llist_node *freed;
Chris Wilson26e12f82011-03-20 11:20:19 +00004816
Chris Wilson2ef1e722018-01-15 20:57:59 +00004817 /*
4818 * All file-owned VMA should have been released by this point through
Chris Wilsonb1f788c2016-08-04 07:52:45 +01004819 * i915_gem_close_object(), or earlier by i915_gem_context_close().
4820 * However, the object may also be bound into the global GTT (e.g.
4821 * older GPUs without per-process support, or for direct access through
4822 * the GTT either for the user or for scanout). Those VMA still need to
4823 * unbound now.
4824 */
Chris Wilson1488fc02012-04-24 15:47:31 +01004825
Chris Wilsonf991c492017-11-06 11:15:08 +00004826 spin_lock(&i915->mm.free_lock);
Chris Wilson5ad08be2017-04-07 11:25:51 +01004827 while ((freed = llist_del_all(&i915->mm.free_list))) {
Chris Wilsonf991c492017-11-06 11:15:08 +00004828 spin_unlock(&i915->mm.free_lock);
4829
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004830 __i915_gem_free_objects(i915, freed);
Chris Wilson5ad08be2017-04-07 11:25:51 +01004831 if (need_resched())
Chris Wilsonf991c492017-11-06 11:15:08 +00004832 return;
4833
4834 spin_lock(&i915->mm.free_lock);
Chris Wilson5ad08be2017-04-07 11:25:51 +01004835 }
Chris Wilsonf991c492017-11-06 11:15:08 +00004836 spin_unlock(&i915->mm.free_lock);
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004837}
4838
4839static void __i915_gem_free_object_rcu(struct rcu_head *head)
4840{
4841 struct drm_i915_gem_object *obj =
4842 container_of(head, typeof(*obj), rcu);
4843 struct drm_i915_private *i915 = to_i915(obj->base.dev);
4844
Chris Wilson2ef1e722018-01-15 20:57:59 +00004845 /*
Chris Wilson8811d612018-11-09 09:03:11 +00004846 * We reuse obj->rcu for the freed list, so we had better not treat
4847 * it like a rcu_head from this point forwards. And we expect all
4848 * objects to be freed via this path.
4849 */
4850 destroy_rcu_head(&obj->rcu);
4851
4852 /*
Chris Wilson2ef1e722018-01-15 20:57:59 +00004853 * Since we require blocking on struct_mutex to unbind the freed
4854 * object from the GPU before releasing resources back to the
4855 * system, we can not do that directly from the RCU callback (which may
4856 * be a softirq context), but must instead then defer that work onto a
4857 * kthread. We use the RCU callback rather than move the freed object
4858 * directly onto the work queue so that we can mix between using the
4859 * worker and performing frees directly from subsequent allocations for
4860 * crude but effective memory throttling.
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004861 */
4862 if (llist_add(&obj->freed, &i915->mm.free_list))
Chris Wilsonbeacbd12018-01-15 12:28:45 +00004863 queue_work(i915->wq, &i915->mm.free_work);
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004864}
4865
4866void i915_gem_free_object(struct drm_gem_object *gem_obj)
4867{
4868 struct drm_i915_gem_object *obj = to_intel_bo(gem_obj);
4869
Chris Wilsonbc0629a2016-11-01 10:03:17 +00004870 if (obj->mm.quirked)
4871 __i915_gem_object_unpin_pages(obj);
4872
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004873 if (discard_backing_storage(obj))
4874 obj->mm.madv = I915_MADV_DONTNEED;
Daniel Vettera071fa02014-06-18 23:28:09 +02004875
Chris Wilson2ef1e722018-01-15 20:57:59 +00004876 /*
4877 * Before we free the object, make sure any pure RCU-only
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004878 * read-side critical sections are complete, e.g.
4879 * i915_gem_busy_ioctl(). For the corresponding synchronized
4880 * lookup see i915_gem_object_lookup_rcu().
4881 */
Chris Wilsonc9c704712018-02-19 22:06:31 +00004882 atomic_inc(&to_i915(obj->base.dev)->mm.free_count);
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004883 call_rcu(&obj->rcu, __i915_gem_free_object_rcu);
Chris Wilsonbe726152010-07-23 23:18:50 +01004884}
4885
Chris Wilsonf8a7fde2016-10-28 13:58:29 +01004886void __i915_gem_object_release_unless_active(struct drm_i915_gem_object *obj)
4887{
4888 lockdep_assert_held(&obj->base.dev->struct_mutex);
4889
Chris Wilsond1b48c12017-08-16 09:52:08 +01004890 if (!i915_gem_object_has_active_reference(obj) &&
4891 i915_gem_object_is_active(obj))
Chris Wilsonf8a7fde2016-10-28 13:58:29 +01004892 i915_gem_object_set_active_reference(obj);
4893 else
4894 i915_gem_object_put(obj);
4895}
4896
Chris Wilson24145512017-01-24 11:01:35 +00004897void i915_gem_sanitize(struct drm_i915_private *i915)
4898{
Chris Wilson538ef962019-01-14 14:21:18 +00004899 intel_wakeref_t wakeref;
4900
Chris Wilsonc3160da2018-05-31 09:22:45 +01004901 GEM_TRACE("\n");
4902
Chris Wilson4dfacb02018-05-31 09:22:43 +01004903 mutex_lock(&i915->drm.struct_mutex);
Chris Wilsonc3160da2018-05-31 09:22:45 +01004904
Chris Wilson538ef962019-01-14 14:21:18 +00004905 wakeref = intel_runtime_pm_get(i915);
Chris Wilsonc3160da2018-05-31 09:22:45 +01004906 intel_uncore_forcewake_get(i915, FORCEWAKE_ALL);
4907
4908 /*
4909 * As we have just resumed the machine and woken the device up from
4910 * deep PCI sleep (presumably D3_cold), assume the HW has been reset
4911 * back to defaults, recovering from whatever wedged state we left it
4912 * in and so worth trying to use the device once more.
4913 */
Chris Wilson4dfacb02018-05-31 09:22:43 +01004914 if (i915_terminally_wedged(&i915->gpu_error))
Chris Wilsonf36325f2017-08-26 12:09:34 +01004915 i915_gem_unset_wedged(i915);
Chris Wilsonf36325f2017-08-26 12:09:34 +01004916
Chris Wilson24145512017-01-24 11:01:35 +00004917 /*
4918 * If we inherit context state from the BIOS or earlier occupants
4919 * of the GPU, the GPU may be in an inconsistent state when we
4920 * try to take over. The only way to remove the earlier state
4921 * is by resetting. However, resetting on earlier gen is tricky as
4922 * it may impact the display and we are uncertain about the stability
Joonas Lahtinenea117b82017-04-28 10:53:38 +03004923 * of the reset, so this could be applied to even earlier gen.
Chris Wilson24145512017-01-24 11:01:35 +00004924 */
Chris Wilson55277e12019-01-03 11:21:04 +00004925 intel_engines_sanitize(i915, false);
Chris Wilsonc3160da2018-05-31 09:22:45 +01004926
4927 intel_uncore_forcewake_put(i915, FORCEWAKE_ALL);
Chris Wilson538ef962019-01-14 14:21:18 +00004928 intel_runtime_pm_put(i915, wakeref);
Chris Wilsonc3160da2018-05-31 09:22:45 +01004929
Chris Wilson4dfacb02018-05-31 09:22:43 +01004930 i915_gem_contexts_lost(i915);
4931 mutex_unlock(&i915->drm.struct_mutex);
Chris Wilson24145512017-01-24 11:01:35 +00004932}
4933
Chris Wilsonbf061122018-07-09 14:02:04 +01004934int i915_gem_suspend(struct drm_i915_private *i915)
Eric Anholt673a3942008-07-30 12:06:12 -07004935{
Chris Wilson538ef962019-01-14 14:21:18 +00004936 intel_wakeref_t wakeref;
Chris Wilsondcff85c2016-08-05 10:14:11 +01004937 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004938
Chris Wilson09a4c022018-05-24 09:11:35 +01004939 GEM_TRACE("\n");
4940
Chris Wilson538ef962019-01-14 14:21:18 +00004941 wakeref = intel_runtime_pm_get(i915);
Chris Wilsonbf061122018-07-09 14:02:04 +01004942 intel_suspend_gt_powersave(i915);
Chris Wilson54b4f682016-07-21 21:16:19 +01004943
Chris Wilsonbf061122018-07-09 14:02:04 +01004944 mutex_lock(&i915->drm.struct_mutex);
Chris Wilson5ab57c72016-07-15 14:56:20 +01004945
Chris Wilsonbf061122018-07-09 14:02:04 +01004946 /*
4947 * We have to flush all the executing contexts to main memory so
Chris Wilson5ab57c72016-07-15 14:56:20 +01004948 * that they can saved in the hibernation image. To ensure the last
4949 * context image is coherent, we have to switch away from it. That
Chris Wilsonbf061122018-07-09 14:02:04 +01004950 * leaves the i915->kernel_context still active when
Chris Wilson5ab57c72016-07-15 14:56:20 +01004951 * we actually suspend, and its image in memory may not match the GPU
4952 * state. Fortunately, the kernel_context is disposable and we do
4953 * not rely on its state.
4954 */
Chris Wilsonbf061122018-07-09 14:02:04 +01004955 if (!i915_terminally_wedged(&i915->gpu_error)) {
4956 ret = i915_gem_switch_to_kernel_context(i915);
Chris Wilsonecf73eb2017-11-30 10:29:51 +00004957 if (ret)
4958 goto err_unlock;
Chris Wilson5ab57c72016-07-15 14:56:20 +01004959
Chris Wilsonbf061122018-07-09 14:02:04 +01004960 ret = i915_gem_wait_for_idle(i915,
Chris Wilsonecf73eb2017-11-30 10:29:51 +00004961 I915_WAIT_INTERRUPTIBLE |
Chris Wilson06060352018-05-31 09:22:44 +01004962 I915_WAIT_LOCKED |
Chris Wilsonec625fb2018-07-09 13:20:42 +01004963 I915_WAIT_FOR_IDLE_BOOST,
4964 MAX_SCHEDULE_TIMEOUT);
Chris Wilsonecf73eb2017-11-30 10:29:51 +00004965 if (ret && ret != -EIO)
4966 goto err_unlock;
Chris Wilsonf7403342013-09-13 23:57:04 +01004967
Chris Wilsonbf061122018-07-09 14:02:04 +01004968 assert_kernel_context_is_current(i915);
Chris Wilsonecf73eb2017-11-30 10:29:51 +00004969 }
Chris Wilson01f8f332018-07-17 09:41:21 +01004970 i915_retire_requests(i915); /* ensure we flush after wedging */
4971
Chris Wilsonbf061122018-07-09 14:02:04 +01004972 mutex_unlock(&i915->drm.struct_mutex);
Chris Wilson45c5f202013-10-16 11:50:01 +01004973
Chris Wilsonbf061122018-07-09 14:02:04 +01004974 intel_uc_suspend(i915);
Sagar Arun Kamble63987bf2017-04-05 15:51:50 +05304975
Chris Wilsonbf061122018-07-09 14:02:04 +01004976 cancel_delayed_work_sync(&i915->gpu_error.hangcheck_work);
4977 cancel_delayed_work_sync(&i915->gt.retire_work);
Chris Wilsonbdeb9782016-12-23 14:57:56 +00004978
Chris Wilsonbf061122018-07-09 14:02:04 +01004979 /*
4980 * As the idle_work is rearming if it detects a race, play safe and
Chris Wilsonbdeb9782016-12-23 14:57:56 +00004981 * repeat the flush until it is definitely idle.
4982 */
Chris Wilsonbf061122018-07-09 14:02:04 +01004983 drain_delayed_work(&i915->gt.idle_work);
Chris Wilsonbdeb9782016-12-23 14:57:56 +00004984
Chris Wilsonbf061122018-07-09 14:02:04 +01004985 /*
4986 * Assert that we successfully flushed all the work and
Chris Wilsonbdcf1202014-11-25 11:56:33 +00004987 * reset the GPU back to its idle, low power state.
4988 */
Chris Wilsonbf061122018-07-09 14:02:04 +01004989 WARN_ON(i915->gt.awake);
4990 if (WARN_ON(!intel_engines_are_idle(i915)))
4991 i915_gem_set_wedged(i915); /* no hope, discard everything */
Chris Wilsonbdcf1202014-11-25 11:56:33 +00004992
Chris Wilson538ef962019-01-14 14:21:18 +00004993 intel_runtime_pm_put(i915, wakeref);
Chris Wilsonec92ad02018-05-31 09:22:46 +01004994 return 0;
4995
4996err_unlock:
Chris Wilsonbf061122018-07-09 14:02:04 +01004997 mutex_unlock(&i915->drm.struct_mutex);
Chris Wilson538ef962019-01-14 14:21:18 +00004998 intel_runtime_pm_put(i915, wakeref);
Chris Wilsonec92ad02018-05-31 09:22:46 +01004999 return ret;
5000}
5001
5002void i915_gem_suspend_late(struct drm_i915_private *i915)
5003{
Chris Wilson9776f472018-06-01 15:41:24 +01005004 struct drm_i915_gem_object *obj;
5005 struct list_head *phases[] = {
5006 &i915->mm.unbound_list,
5007 &i915->mm.bound_list,
5008 NULL
5009 }, **phase;
5010
Imre Deak1c777c52016-10-12 17:46:37 +03005011 /*
5012 * Neither the BIOS, ourselves or any other kernel
5013 * expects the system to be in execlists mode on startup,
5014 * so we need to reset the GPU back to legacy mode. And the only
5015 * known way to disable logical contexts is through a GPU reset.
5016 *
5017 * So in order to leave the system in a known default configuration,
5018 * always reset the GPU upon unload and suspend. Afterwards we then
5019 * clean up the GEM state tracking, flushing off the requests and
5020 * leaving the system in a known idle state.
5021 *
5022 * Note that is of the upmost importance that the GPU is idle and
5023 * all stray writes are flushed *before* we dismantle the backing
5024 * storage for the pinned objects.
5025 *
5026 * However, since we are uncertain that resetting the GPU on older
5027 * machines is a good idea, we don't - just in case it leaves the
5028 * machine in an unusable condition.
5029 */
Chris Wilsoncad99462017-08-26 12:09:33 +01005030
Chris Wilson9776f472018-06-01 15:41:24 +01005031 mutex_lock(&i915->drm.struct_mutex);
5032 for (phase = phases; *phase; phase++) {
5033 list_for_each_entry(obj, *phase, mm.link)
5034 WARN_ON(i915_gem_object_set_to_gtt_domain(obj, false));
5035 }
5036 mutex_unlock(&i915->drm.struct_mutex);
5037
Chris Wilsonec92ad02018-05-31 09:22:46 +01005038 intel_uc_sanitize(i915);
5039 i915_gem_sanitize(i915);
Eric Anholt673a3942008-07-30 12:06:12 -07005040}
5041
Chris Wilson37cd3302017-11-12 11:27:38 +00005042void i915_gem_resume(struct drm_i915_private *i915)
Chris Wilson5ab57c72016-07-15 14:56:20 +01005043{
Chris Wilson4dfacb02018-05-31 09:22:43 +01005044 GEM_TRACE("\n");
5045
Chris Wilson37cd3302017-11-12 11:27:38 +00005046 WARN_ON(i915->gt.awake);
Chris Wilson5ab57c72016-07-15 14:56:20 +01005047
Chris Wilson37cd3302017-11-12 11:27:38 +00005048 mutex_lock(&i915->drm.struct_mutex);
5049 intel_uncore_forcewake_get(i915, FORCEWAKE_ALL);
Imre Deak31ab49a2016-11-07 11:20:05 +02005050
Chris Wilson37cd3302017-11-12 11:27:38 +00005051 i915_gem_restore_gtt_mappings(i915);
5052 i915_gem_restore_fences(i915);
Chris Wilson5ab57c72016-07-15 14:56:20 +01005053
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00005054 /*
5055 * As we didn't flush the kernel context before suspend, we cannot
Chris Wilson5ab57c72016-07-15 14:56:20 +01005056 * guarantee that the context image is complete. So let's just reset
5057 * it and start again.
5058 */
Chris Wilson37cd3302017-11-12 11:27:38 +00005059 i915->gt.resume(i915);
Chris Wilson5ab57c72016-07-15 14:56:20 +01005060
Chris Wilson37cd3302017-11-12 11:27:38 +00005061 if (i915_gem_init_hw(i915))
5062 goto err_wedged;
5063
Michal Wajdeczko7cfca4a2018-03-02 11:15:49 +00005064 intel_uc_resume(i915);
Chris Wilson7469c622017-11-14 13:03:00 +00005065
Chris Wilson37cd3302017-11-12 11:27:38 +00005066 /* Always reload a context for powersaving. */
5067 if (i915_gem_switch_to_kernel_context(i915))
5068 goto err_wedged;
5069
5070out_unlock:
5071 intel_uncore_forcewake_put(i915, FORCEWAKE_ALL);
5072 mutex_unlock(&i915->drm.struct_mutex);
5073 return;
5074
5075err_wedged:
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00005076 if (!i915_terminally_wedged(&i915->gpu_error)) {
5077 DRM_ERROR("failed to re-initialize GPU, declaring wedged!\n");
5078 i915_gem_set_wedged(i915);
5079 }
Chris Wilson37cd3302017-11-12 11:27:38 +00005080 goto out_unlock;
Chris Wilson5ab57c72016-07-15 14:56:20 +01005081}
5082
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00005083void i915_gem_init_swizzling(struct drm_i915_private *dev_priv)
Daniel Vetterf691e2f2012-02-02 09:58:12 +01005084{
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00005085 if (INTEL_GEN(dev_priv) < 5 ||
Daniel Vetterf691e2f2012-02-02 09:58:12 +01005086 dev_priv->mm.bit_6_swizzle_x == I915_BIT_6_SWIZZLE_NONE)
5087 return;
5088
5089 I915_WRITE(DISP_ARB_CTL, I915_READ(DISP_ARB_CTL) |
5090 DISP_TILE_SURFACE_SWIZZLING);
5091
Lucas De Marchicf819ef2018-12-12 10:10:43 -08005092 if (IS_GEN(dev_priv, 5))
Daniel Vetter11782b02012-01-31 16:47:55 +01005093 return;
5094
Daniel Vetterf691e2f2012-02-02 09:58:12 +01005095 I915_WRITE(TILECTL, I915_READ(TILECTL) | TILECTL_SWZCTL);
Lucas De Marchicf819ef2018-12-12 10:10:43 -08005096 if (IS_GEN(dev_priv, 6))
Daniel Vetter6b26c862012-04-24 14:04:12 +02005097 I915_WRITE(ARB_MODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_SNB));
Lucas De Marchicf819ef2018-12-12 10:10:43 -08005098 else if (IS_GEN(dev_priv, 7))
Daniel Vetter6b26c862012-04-24 14:04:12 +02005099 I915_WRITE(ARB_MODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_IVB));
Lucas De Marchicf819ef2018-12-12 10:10:43 -08005100 else if (IS_GEN(dev_priv, 8))
Ben Widawsky31a53362013-11-02 21:07:04 -07005101 I915_WRITE(GAMTARBMODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_BDW));
Ben Widawsky8782e262012-12-18 10:31:23 -08005102 else
5103 BUG();
Daniel Vetterf691e2f2012-02-02 09:58:12 +01005104}
Daniel Vettere21af882012-02-09 20:53:27 +01005105
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01005106static void init_unused_ring(struct drm_i915_private *dev_priv, u32 base)
Ville Syrjälä81e7f202014-08-15 01:21:55 +03005107{
Ville Syrjälä81e7f202014-08-15 01:21:55 +03005108 I915_WRITE(RING_CTL(base), 0);
5109 I915_WRITE(RING_HEAD(base), 0);
5110 I915_WRITE(RING_TAIL(base), 0);
5111 I915_WRITE(RING_START(base), 0);
5112}
5113
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01005114static void init_unused_rings(struct drm_i915_private *dev_priv)
Ville Syrjälä81e7f202014-08-15 01:21:55 +03005115{
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01005116 if (IS_I830(dev_priv)) {
5117 init_unused_ring(dev_priv, PRB1_BASE);
5118 init_unused_ring(dev_priv, SRB0_BASE);
5119 init_unused_ring(dev_priv, SRB1_BASE);
5120 init_unused_ring(dev_priv, SRB2_BASE);
5121 init_unused_ring(dev_priv, SRB3_BASE);
Lucas De Marchicf819ef2018-12-12 10:10:43 -08005122 } else if (IS_GEN(dev_priv, 2)) {
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01005123 init_unused_ring(dev_priv, SRB0_BASE);
5124 init_unused_ring(dev_priv, SRB1_BASE);
Lucas De Marchicf819ef2018-12-12 10:10:43 -08005125 } else if (IS_GEN(dev_priv, 3)) {
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01005126 init_unused_ring(dev_priv, PRB1_BASE);
5127 init_unused_ring(dev_priv, PRB2_BASE);
Ville Syrjälä81e7f202014-08-15 01:21:55 +03005128 }
5129}
5130
Chris Wilson20a8a742017-02-08 14:30:31 +00005131static int __i915_gem_restart_engines(void *data)
Ben Widawsky4fc7c972013-02-08 11:49:24 -08005132{
Chris Wilson20a8a742017-02-08 14:30:31 +00005133 struct drm_i915_private *i915 = data;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00005134 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05305135 enum intel_engine_id id;
Chris Wilson20a8a742017-02-08 14:30:31 +00005136 int err;
5137
5138 for_each_engine(engine, i915, id) {
5139 err = engine->init_hw(engine);
Chris Wilson8177e112018-02-07 11:15:45 +00005140 if (err) {
5141 DRM_ERROR("Failed to restart %s (%d)\n",
5142 engine->name, err);
Chris Wilson20a8a742017-02-08 14:30:31 +00005143 return err;
Chris Wilson8177e112018-02-07 11:15:45 +00005144 }
Chris Wilson20a8a742017-02-08 14:30:31 +00005145 }
5146
5147 return 0;
5148}
5149
5150int i915_gem_init_hw(struct drm_i915_private *dev_priv)
5151{
Chris Wilsond200cda2016-04-28 09:56:44 +01005152 int ret;
Ben Widawsky4fc7c972013-02-08 11:49:24 -08005153
Chris Wilsonde867c22016-10-25 13:16:02 +01005154 dev_priv->gt.last_init_time = ktime_get();
5155
Chris Wilson5e4f5182015-02-13 14:35:59 +00005156 /* Double layer security blanket, see i915_gem_init() */
5157 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
5158
Tvrtko Ursulin0031fb92016-11-04 14:42:44 +00005159 if (HAS_EDRAM(dev_priv) && INTEL_GEN(dev_priv) < 9)
Ben Widawsky05e21cc2013-07-04 11:02:04 -07005160 I915_WRITE(HSW_IDICR, I915_READ(HSW_IDICR) | IDIHASHMSK(0xf));
Ben Widawsky4fc7c972013-02-08 11:49:24 -08005161
Tvrtko Ursulin772c2a52016-10-13 11:03:01 +01005162 if (IS_HASWELL(dev_priv))
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01005163 I915_WRITE(MI_PREDICATE_RESULT_2, IS_HSW_GT3(dev_priv) ?
Ville Syrjälä0bf21342013-11-29 14:56:12 +02005164 LOWER_SLICE_ENABLED : LOWER_SLICE_DISABLED);
Rodrigo Vivi94353732013-08-28 16:45:46 -03005165
Tvrtko Ursulin094304b2018-12-03 12:50:10 +00005166 /* Apply the GT workarounds... */
Tvrtko Ursulin25d140f2018-12-03 13:33:19 +00005167 intel_gt_apply_workarounds(dev_priv);
Tvrtko Ursulin094304b2018-12-03 12:50:10 +00005168 /* ...and determine whether they are sticking. */
5169 intel_gt_verify_workarounds(dev_priv, "init");
Oscar Mateo59b449d2018-04-10 09:12:47 -07005170
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00005171 i915_gem_init_swizzling(dev_priv);
Ben Widawsky4fc7c972013-02-08 11:49:24 -08005172
Daniel Vetterd5abdfd2014-11-20 09:45:19 +01005173 /*
5174 * At least 830 can leave some of the unused rings
5175 * "active" (ie. head != tail) after resume which
5176 * will prevent c3 entry. Makes sure all unused rings
5177 * are totally idle.
5178 */
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01005179 init_unused_rings(dev_priv);
Daniel Vetterd5abdfd2014-11-20 09:45:19 +01005180
Dave Gordoned54c1a2016-01-19 19:02:54 +00005181 BUG_ON(!dev_priv->kernel_context);
Chris Wilson6f74b362017-10-15 15:37:25 +01005182 if (i915_terminally_wedged(&dev_priv->gpu_error)) {
5183 ret = -EIO;
5184 goto out;
5185 }
John Harrison90638cc2015-05-29 17:43:37 +01005186
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00005187 ret = i915_ppgtt_init_hw(dev_priv);
John Harrison4ad2fd82015-06-18 13:11:20 +01005188 if (ret) {
Chris Wilson8177e112018-02-07 11:15:45 +00005189 DRM_ERROR("Enabling PPGTT failed (%d)\n", ret);
John Harrison4ad2fd82015-06-18 13:11:20 +01005190 goto out;
5191 }
5192
Jackie Lif08e2032018-03-13 17:32:53 -07005193 ret = intel_wopcm_init_hw(&dev_priv->wopcm);
5194 if (ret) {
5195 DRM_ERROR("Enabling WOPCM failed (%d)\n", ret);
5196 goto out;
5197 }
5198
Michał Winiarski9bdc3572017-10-25 18:25:19 +01005199 /* We can't enable contexts until all firmware is loaded */
5200 ret = intel_uc_init_hw(dev_priv);
Chris Wilson8177e112018-02-07 11:15:45 +00005201 if (ret) {
5202 DRM_ERROR("Enabling uc failed (%d)\n", ret);
Michał Winiarski9bdc3572017-10-25 18:25:19 +01005203 goto out;
Chris Wilson8177e112018-02-07 11:15:45 +00005204 }
Michał Winiarski9bdc3572017-10-25 18:25:19 +01005205
Tvrtko Ursulinbf9e8422016-12-01 14:16:38 +00005206 intel_mocs_init_l3cc_table(dev_priv);
Peter Antoine0ccdacf2016-04-13 15:03:25 +01005207
Chris Wilson136109c2017-11-02 13:14:30 +00005208 /* Only when the HW is re-initialised, can we replay the requests */
5209 ret = __i915_gem_restart_engines(dev_priv);
Michal Wajdeczkob96f6eb2018-06-05 12:24:43 +00005210 if (ret)
5211 goto cleanup_uc;
Michał Winiarski60c0a662018-07-12 14:48:10 +02005212
Chris Wilson5e4f5182015-02-13 14:35:59 +00005213 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Michał Winiarski60c0a662018-07-12 14:48:10 +02005214
5215 return 0;
Michal Wajdeczkob96f6eb2018-06-05 12:24:43 +00005216
5217cleanup_uc:
5218 intel_uc_fini_hw(dev_priv);
Michał Winiarski60c0a662018-07-12 14:48:10 +02005219out:
5220 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
5221
5222 return ret;
Zou Nan hai8187a2b2010-05-21 09:08:55 +08005223}
5224
Chris Wilsond2b4b972017-11-10 14:26:33 +00005225static int __intel_engines_record_defaults(struct drm_i915_private *i915)
5226{
5227 struct i915_gem_context *ctx;
5228 struct intel_engine_cs *engine;
5229 enum intel_engine_id id;
5230 int err;
5231
5232 /*
5233 * As we reset the gpu during very early sanitisation, the current
5234 * register state on the GPU should reflect its defaults values.
5235 * We load a context onto the hw (with restore-inhibit), then switch
5236 * over to a second context to save that default register state. We
5237 * can then prime every new context with that state so they all start
5238 * from the same default HW values.
5239 */
5240
5241 ctx = i915_gem_context_create_kernel(i915, 0);
5242 if (IS_ERR(ctx))
5243 return PTR_ERR(ctx);
5244
5245 for_each_engine(engine, i915, id) {
Chris Wilsone61e0f52018-02-21 09:56:36 +00005246 struct i915_request *rq;
Chris Wilsond2b4b972017-11-10 14:26:33 +00005247
Chris Wilsone61e0f52018-02-21 09:56:36 +00005248 rq = i915_request_alloc(engine, ctx);
Chris Wilsond2b4b972017-11-10 14:26:33 +00005249 if (IS_ERR(rq)) {
5250 err = PTR_ERR(rq);
5251 goto out_ctx;
5252 }
5253
Chris Wilson3fef5cd2017-11-20 10:20:02 +00005254 err = 0;
Chris Wilsond2b4b972017-11-10 14:26:33 +00005255 if (engine->init_context)
5256 err = engine->init_context(rq);
5257
Chris Wilson697b9a82018-06-12 11:51:35 +01005258 i915_request_add(rq);
Chris Wilsond2b4b972017-11-10 14:26:33 +00005259 if (err)
5260 goto err_active;
5261 }
5262
5263 err = i915_gem_switch_to_kernel_context(i915);
5264 if (err)
5265 goto err_active;
5266
Chris Wilson2621cef2018-07-09 13:20:43 +01005267 if (i915_gem_wait_for_idle(i915, I915_WAIT_LOCKED, HZ / 5)) {
5268 i915_gem_set_wedged(i915);
5269 err = -EIO; /* Caller will declare us wedged */
Chris Wilsond2b4b972017-11-10 14:26:33 +00005270 goto err_active;
Chris Wilson2621cef2018-07-09 13:20:43 +01005271 }
Chris Wilsond2b4b972017-11-10 14:26:33 +00005272
5273 assert_kernel_context_is_current(i915);
5274
Chris Wilson8e1cb322018-09-20 17:13:43 +01005275 /*
5276 * Immediately park the GPU so that we enable powersaving and
5277 * treat it as idle. The next time we issue a request, we will
5278 * unpark and start using the engine->pinned_default_state, otherwise
5279 * it is in limbo and an early reset may fail.
5280 */
5281 __i915_gem_park(i915);
5282
Chris Wilsond2b4b972017-11-10 14:26:33 +00005283 for_each_engine(engine, i915, id) {
5284 struct i915_vma *state;
Chris Wilson37d7c9c2018-09-14 13:35:03 +01005285 void *vaddr;
Chris Wilsond2b4b972017-11-10 14:26:33 +00005286
Chris Wilson666424a2018-09-14 13:35:04 +01005287 GEM_BUG_ON(to_intel_context(ctx, engine)->pin_count);
5288
Chris Wilsonab82a062018-04-30 14:15:01 +01005289 state = to_intel_context(ctx, engine)->state;
Chris Wilsond2b4b972017-11-10 14:26:33 +00005290 if (!state)
5291 continue;
5292
5293 /*
5294 * As we will hold a reference to the logical state, it will
5295 * not be torn down with the context, and importantly the
5296 * object will hold onto its vma (making it possible for a
5297 * stray GTT write to corrupt our defaults). Unmap the vma
5298 * from the GTT to prevent such accidents and reclaim the
5299 * space.
5300 */
5301 err = i915_vma_unbind(state);
5302 if (err)
5303 goto err_active;
5304
5305 err = i915_gem_object_set_to_cpu_domain(state->obj, false);
5306 if (err)
5307 goto err_active;
5308
5309 engine->default_state = i915_gem_object_get(state->obj);
Chris Wilson37d7c9c2018-09-14 13:35:03 +01005310
5311 /* Check we can acquire the image of the context state */
5312 vaddr = i915_gem_object_pin_map(engine->default_state,
Chris Wilson666424a2018-09-14 13:35:04 +01005313 I915_MAP_FORCE_WB);
Chris Wilson37d7c9c2018-09-14 13:35:03 +01005314 if (IS_ERR(vaddr)) {
5315 err = PTR_ERR(vaddr);
5316 goto err_active;
5317 }
5318
5319 i915_gem_object_unpin_map(engine->default_state);
Chris Wilsond2b4b972017-11-10 14:26:33 +00005320 }
5321
5322 if (IS_ENABLED(CONFIG_DRM_I915_DEBUG_GEM)) {
5323 unsigned int found = intel_engines_has_context_isolation(i915);
5324
5325 /*
5326 * Make sure that classes with multiple engine instances all
5327 * share the same basic configuration.
5328 */
5329 for_each_engine(engine, i915, id) {
5330 unsigned int bit = BIT(engine->uabi_class);
5331 unsigned int expected = engine->default_state ? bit : 0;
5332
5333 if ((found & bit) != expected) {
5334 DRM_ERROR("mismatching default context state for class %d on engine %s\n",
5335 engine->uabi_class, engine->name);
5336 }
5337 }
5338 }
5339
5340out_ctx:
5341 i915_gem_context_set_closed(ctx);
5342 i915_gem_context_put(ctx);
5343 return err;
5344
5345err_active:
5346 /*
5347 * If we have to abandon now, we expect the engines to be idle
5348 * and ready to be torn-down. First try to flush any remaining
5349 * request, ensure we are pointing at the kernel context and
5350 * then remove it.
5351 */
5352 if (WARN_ON(i915_gem_switch_to_kernel_context(i915)))
5353 goto out_ctx;
5354
Chris Wilsonec625fb2018-07-09 13:20:42 +01005355 if (WARN_ON(i915_gem_wait_for_idle(i915,
5356 I915_WAIT_LOCKED,
5357 MAX_SCHEDULE_TIMEOUT)))
Chris Wilsond2b4b972017-11-10 14:26:33 +00005358 goto out_ctx;
5359
5360 i915_gem_contexts_lost(i915);
5361 goto out_ctx;
5362}
5363
Chris Wilson51797492018-12-04 14:15:16 +00005364static int
5365i915_gem_init_scratch(struct drm_i915_private *i915, unsigned int size)
5366{
5367 struct drm_i915_gem_object *obj;
5368 struct i915_vma *vma;
5369 int ret;
5370
5371 obj = i915_gem_object_create_stolen(i915, size);
5372 if (!obj)
5373 obj = i915_gem_object_create_internal(i915, size);
5374 if (IS_ERR(obj)) {
5375 DRM_ERROR("Failed to allocate scratch page\n");
5376 return PTR_ERR(obj);
5377 }
5378
5379 vma = i915_vma_instance(obj, &i915->ggtt.vm, NULL);
5380 if (IS_ERR(vma)) {
5381 ret = PTR_ERR(vma);
5382 goto err_unref;
5383 }
5384
5385 ret = i915_vma_pin(vma, 0, 0, PIN_GLOBAL | PIN_HIGH);
5386 if (ret)
5387 goto err_unref;
5388
5389 i915->gt.scratch = vma;
5390 return 0;
5391
5392err_unref:
5393 i915_gem_object_put(obj);
5394 return ret;
5395}
5396
5397static void i915_gem_fini_scratch(struct drm_i915_private *i915)
5398{
5399 i915_vma_unpin_and_release(&i915->gt.scratch, 0);
5400}
5401
Tvrtko Ursulinbf9e8422016-12-01 14:16:38 +00005402int i915_gem_init(struct drm_i915_private *dev_priv)
Chris Wilson1070a422012-04-24 15:47:41 +01005403{
Chris Wilson1070a422012-04-24 15:47:41 +01005404 int ret;
5405
Changbin Du52b24162018-05-08 17:07:05 +08005406 /* We need to fallback to 4K pages if host doesn't support huge gtt. */
5407 if (intel_vgpu_active(dev_priv) && !intel_vgpu_has_huge_gtt(dev_priv))
Matthew Auldda9fe3f32017-10-06 23:18:31 +01005408 mkwrite_device_info(dev_priv)->page_sizes =
5409 I915_GTT_PAGE_SIZE_4K;
5410
Chris Wilson94312822017-05-03 10:39:18 +01005411 dev_priv->mm.unordered_timeline = dma_fence_context_alloc(1);
Chris Wilson57822dc2017-02-22 11:40:48 +00005412
Chris Wilsonfb5c5512017-11-20 20:55:00 +00005413 if (HAS_LOGICAL_RING_CONTEXTS(dev_priv)) {
Chris Wilson821ed7d2016-09-09 14:11:53 +01005414 dev_priv->gt.resume = intel_lr_context_resume;
Tvrtko Ursulin117897f2016-03-16 11:00:40 +00005415 dev_priv->gt.cleanup_engine = intel_logical_ring_cleanup;
Chris Wilsonfb5c5512017-11-20 20:55:00 +00005416 } else {
5417 dev_priv->gt.resume = intel_legacy_submission_resume;
5418 dev_priv->gt.cleanup_engine = intel_engine_cleanup;
Oscar Mateoa83014d2014-07-24 17:04:21 +01005419 }
5420
Chris Wilsonee487002017-11-22 17:26:21 +00005421 ret = i915_gem_init_userptr(dev_priv);
5422 if (ret)
5423 return ret;
5424
Sagar Arun Kamble70deead2018-01-24 21:16:58 +05305425 ret = intel_uc_init_misc(dev_priv);
Michał Winiarski3176ff42017-12-13 23:13:47 +01005426 if (ret)
5427 return ret;
5428
Michal Wajdeczkof7dc0152018-06-28 14:15:21 +00005429 ret = intel_wopcm_init(&dev_priv->wopcm);
5430 if (ret)
5431 goto err_uc_misc;
5432
Chris Wilson5e4f5182015-02-13 14:35:59 +00005433 /* This is just a security blanket to placate dragons.
5434 * On some systems, we very sporadically observe that the first TLBs
5435 * used by the CS may be stale, despite us poking the TLB reset. If
5436 * we hold the forcewake during initialisation these problems
5437 * just magically go away.
5438 */
Chris Wilsonee487002017-11-22 17:26:21 +00005439 mutex_lock(&dev_priv->drm.struct_mutex);
Chris Wilson5e4f5182015-02-13 14:35:59 +00005440 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
5441
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01005442 ret = i915_gem_init_ggtt(dev_priv);
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00005443 if (ret) {
5444 GEM_BUG_ON(ret == -EIO);
5445 goto err_unlock;
5446 }
Jesse Barnesd62b4892013-03-08 10:45:53 -08005447
Chris Wilson51797492018-12-04 14:15:16 +00005448 ret = i915_gem_init_scratch(dev_priv,
Lucas De Marchicf819ef2018-12-12 10:10:43 -08005449 IS_GEN(dev_priv, 2) ? SZ_256K : PAGE_SIZE);
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00005450 if (ret) {
5451 GEM_BUG_ON(ret == -EIO);
5452 goto err_ggtt;
5453 }
Ben Widawsky2fa48d82013-12-06 14:11:04 -08005454
Chris Wilson51797492018-12-04 14:15:16 +00005455 ret = i915_gem_contexts_init(dev_priv);
5456 if (ret) {
5457 GEM_BUG_ON(ret == -EIO);
5458 goto err_scratch;
5459 }
5460
Tvrtko Ursulinbf9e8422016-12-01 14:16:38 +00005461 ret = intel_engines_init(dev_priv);
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00005462 if (ret) {
5463 GEM_BUG_ON(ret == -EIO);
5464 goto err_context;
5465 }
Daniel Vetter53ca26c2012-04-26 23:28:03 +02005466
Chris Wilsonf58d13d2017-11-10 14:26:29 +00005467 intel_init_gt_powersave(dev_priv);
5468
Michał Winiarski61b5c152017-12-13 23:13:48 +01005469 ret = intel_uc_init(dev_priv);
Chris Wilsoncc6a8182017-11-10 14:26:30 +00005470 if (ret)
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00005471 goto err_pm;
Chris Wilsoncc6a8182017-11-10 14:26:30 +00005472
Michał Winiarski61b5c152017-12-13 23:13:48 +01005473 ret = i915_gem_init_hw(dev_priv);
5474 if (ret)
5475 goto err_uc_init;
5476
Chris Wilsoncc6a8182017-11-10 14:26:30 +00005477 /*
5478 * Despite its name intel_init_clock_gating applies both display
5479 * clock gating workarounds; GT mmio workarounds and the occasional
5480 * GT power context workaround. Worse, sometimes it includes a context
5481 * register workaround which we need to apply before we record the
5482 * default HW state for all contexts.
5483 *
5484 * FIXME: break up the workarounds and apply them at the right time!
5485 */
5486 intel_init_clock_gating(dev_priv);
5487
Chris Wilsond2b4b972017-11-10 14:26:33 +00005488 ret = __intel_engines_record_defaults(dev_priv);
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00005489 if (ret)
5490 goto err_init_hw;
5491
5492 if (i915_inject_load_failure()) {
5493 ret = -ENODEV;
5494 goto err_init_hw;
5495 }
5496
5497 if (i915_inject_load_failure()) {
5498 ret = -EIO;
5499 goto err_init_hw;
5500 }
5501
5502 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
5503 mutex_unlock(&dev_priv->drm.struct_mutex);
5504
5505 return 0;
5506
5507 /*
5508 * Unwinding is complicated by that we want to handle -EIO to mean
5509 * disable GPU submission but keep KMS alive. We want to mark the
5510 * HW as irrevisibly wedged, but keep enough state around that the
5511 * driver doesn't explode during runtime.
5512 */
5513err_init_hw:
Chris Wilson8571a052018-06-06 15:54:41 +01005514 mutex_unlock(&dev_priv->drm.struct_mutex);
5515
5516 WARN_ON(i915_gem_suspend(dev_priv));
5517 i915_gem_suspend_late(dev_priv);
5518
Chris Wilson8bcf9f72018-07-10 10:44:20 +01005519 i915_gem_drain_workqueue(dev_priv);
5520
Chris Wilson8571a052018-06-06 15:54:41 +01005521 mutex_lock(&dev_priv->drm.struct_mutex);
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00005522 intel_uc_fini_hw(dev_priv);
Michał Winiarski61b5c152017-12-13 23:13:48 +01005523err_uc_init:
5524 intel_uc_fini(dev_priv);
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00005525err_pm:
5526 if (ret != -EIO) {
5527 intel_cleanup_gt_powersave(dev_priv);
5528 i915_gem_cleanup_engines(dev_priv);
5529 }
5530err_context:
5531 if (ret != -EIO)
5532 i915_gem_contexts_fini(dev_priv);
Chris Wilson51797492018-12-04 14:15:16 +00005533err_scratch:
5534 i915_gem_fini_scratch(dev_priv);
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00005535err_ggtt:
5536err_unlock:
5537 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
5538 mutex_unlock(&dev_priv->drm.struct_mutex);
5539
Michal Wajdeczkof7dc0152018-06-28 14:15:21 +00005540err_uc_misc:
Sagar Arun Kamble70deead2018-01-24 21:16:58 +05305541 intel_uc_fini_misc(dev_priv);
Sagar Arun Kambleda943b52018-01-10 18:24:16 +05305542
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00005543 if (ret != -EIO)
5544 i915_gem_cleanup_userptr(dev_priv);
5545
Chris Wilson60990322014-04-09 09:19:42 +01005546 if (ret == -EIO) {
Chris Wilson7ed43df2018-07-26 09:50:32 +01005547 mutex_lock(&dev_priv->drm.struct_mutex);
5548
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00005549 /*
5550 * Allow engine initialisation to fail by marking the GPU as
Chris Wilson60990322014-04-09 09:19:42 +01005551 * wedged. But we only want to do this where the GPU is angry,
5552 * for all other failure, such as an allocation failure, bail.
5553 */
Chris Wilson6f74b362017-10-15 15:37:25 +01005554 if (!i915_terminally_wedged(&dev_priv->gpu_error)) {
Chris Wilson51c18bf2018-06-09 12:10:58 +01005555 i915_load_error(dev_priv,
5556 "Failed to initialize GPU, declaring it wedged!\n");
Chris Wilson6f74b362017-10-15 15:37:25 +01005557 i915_gem_set_wedged(dev_priv);
5558 }
Chris Wilson7ed43df2018-07-26 09:50:32 +01005559
5560 /* Minimal basic recovery for KMS */
5561 ret = i915_ggtt_enable_hw(dev_priv);
5562 i915_gem_restore_gtt_mappings(dev_priv);
5563 i915_gem_restore_fences(dev_priv);
5564 intel_init_clock_gating(dev_priv);
5565
5566 mutex_unlock(&dev_priv->drm.struct_mutex);
Chris Wilson1070a422012-04-24 15:47:41 +01005567 }
5568
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00005569 i915_gem_drain_freed_objects(dev_priv);
Chris Wilson60990322014-04-09 09:19:42 +01005570 return ret;
Chris Wilson1070a422012-04-24 15:47:41 +01005571}
5572
Michal Wajdeczko8979187a2018-06-04 09:00:32 +00005573void i915_gem_fini(struct drm_i915_private *dev_priv)
5574{
5575 i915_gem_suspend_late(dev_priv);
Chris Wilson30b710842018-08-12 23:36:29 +01005576 intel_disable_gt_powersave(dev_priv);
Michal Wajdeczko8979187a2018-06-04 09:00:32 +00005577
5578 /* Flush any outstanding unpin_work. */
5579 i915_gem_drain_workqueue(dev_priv);
5580
5581 mutex_lock(&dev_priv->drm.struct_mutex);
5582 intel_uc_fini_hw(dev_priv);
5583 intel_uc_fini(dev_priv);
5584 i915_gem_cleanup_engines(dev_priv);
5585 i915_gem_contexts_fini(dev_priv);
Chris Wilson51797492018-12-04 14:15:16 +00005586 i915_gem_fini_scratch(dev_priv);
Michal Wajdeczko8979187a2018-06-04 09:00:32 +00005587 mutex_unlock(&dev_priv->drm.struct_mutex);
5588
Tvrtko Ursulin25d140f2018-12-03 13:33:19 +00005589 intel_wa_list_free(&dev_priv->gt_wa_list);
5590
Chris Wilson30b710842018-08-12 23:36:29 +01005591 intel_cleanup_gt_powersave(dev_priv);
5592
Michal Wajdeczko8979187a2018-06-04 09:00:32 +00005593 intel_uc_fini_misc(dev_priv);
5594 i915_gem_cleanup_userptr(dev_priv);
5595
5596 i915_gem_drain_freed_objects(dev_priv);
5597
5598 WARN_ON(!list_empty(&dev_priv->contexts.list));
5599}
5600
Chris Wilson24145512017-01-24 11:01:35 +00005601void i915_gem_init_mmio(struct drm_i915_private *i915)
5602{
5603 i915_gem_sanitize(i915);
5604}
5605
Zou Nan hai8187a2b2010-05-21 09:08:55 +08005606void
Tvrtko Ursulincb15d9f2016-12-01 14:16:39 +00005607i915_gem_cleanup_engines(struct drm_i915_private *dev_priv)
Zou Nan hai8187a2b2010-05-21 09:08:55 +08005608{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00005609 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05305610 enum intel_engine_id id;
Zou Nan hai8187a2b2010-05-21 09:08:55 +08005611
Akash Goel3b3f1652016-10-13 22:44:48 +05305612 for_each_engine(engine, dev_priv, id)
Tvrtko Ursulin117897f2016-03-16 11:00:40 +00005613 dev_priv->gt.cleanup_engine(engine);
Zou Nan hai8187a2b2010-05-21 09:08:55 +08005614}
5615
Eric Anholt673a3942008-07-30 12:06:12 -07005616void
Imre Deak40ae4e12016-03-16 14:54:03 +02005617i915_gem_load_init_fences(struct drm_i915_private *dev_priv)
5618{
Chris Wilson49ef5292016-08-18 17:17:00 +01005619 int i;
Imre Deak40ae4e12016-03-16 14:54:03 +02005620
Tvrtko Ursulinc56b89f2018-02-09 21:58:46 +00005621 if (INTEL_GEN(dev_priv) >= 7 && !IS_VALLEYVIEW(dev_priv) &&
Imre Deak40ae4e12016-03-16 14:54:03 +02005622 !IS_CHERRYVIEW(dev_priv))
5623 dev_priv->num_fence_regs = 32;
Tvrtko Ursulinc56b89f2018-02-09 21:58:46 +00005624 else if (INTEL_GEN(dev_priv) >= 4 ||
Jani Nikula73f67aa2016-12-07 22:48:09 +02005625 IS_I945G(dev_priv) || IS_I945GM(dev_priv) ||
5626 IS_G33(dev_priv) || IS_PINEVIEW(dev_priv))
Imre Deak40ae4e12016-03-16 14:54:03 +02005627 dev_priv->num_fence_regs = 16;
5628 else
5629 dev_priv->num_fence_regs = 8;
5630
Chris Wilsonc0336662016-05-06 15:40:21 +01005631 if (intel_vgpu_active(dev_priv))
Imre Deak40ae4e12016-03-16 14:54:03 +02005632 dev_priv->num_fence_regs =
5633 I915_READ(vgtif_reg(avail_rs.fence_num));
5634
5635 /* Initialize fence registers to zero */
Chris Wilson49ef5292016-08-18 17:17:00 +01005636 for (i = 0; i < dev_priv->num_fence_regs; i++) {
5637 struct drm_i915_fence_reg *fence = &dev_priv->fence_regs[i];
5638
5639 fence->i915 = dev_priv;
5640 fence->id = i;
5641 list_add_tail(&fence->link, &dev_priv->mm.fence_list);
5642 }
Tvrtko Ursulin4362f4f2016-11-16 08:55:33 +00005643 i915_gem_restore_fences(dev_priv);
Imre Deak40ae4e12016-03-16 14:54:03 +02005644
Tvrtko Ursulin4362f4f2016-11-16 08:55:33 +00005645 i915_gem_detect_bit_6_swizzle(dev_priv);
Imre Deak40ae4e12016-03-16 14:54:03 +02005646}
5647
Chris Wilson9c52d1c2017-11-10 23:24:47 +00005648static void i915_gem_init__mm(struct drm_i915_private *i915)
5649{
5650 spin_lock_init(&i915->mm.object_stat_lock);
5651 spin_lock_init(&i915->mm.obj_lock);
5652 spin_lock_init(&i915->mm.free_lock);
5653
5654 init_llist_head(&i915->mm.free_list);
5655
5656 INIT_LIST_HEAD(&i915->mm.unbound_list);
5657 INIT_LIST_HEAD(&i915->mm.bound_list);
5658 INIT_LIST_HEAD(&i915->mm.fence_list);
5659 INIT_LIST_HEAD(&i915->mm.userfault_list);
5660
5661 INIT_WORK(&i915->mm.free_work, __i915_gem_free_work);
5662}
5663
Michal Wajdeczkoa0de9082018-03-23 12:34:49 +00005664int i915_gem_init_early(struct drm_i915_private *dev_priv)
Eric Anholt673a3942008-07-30 12:06:12 -07005665{
Tvrtko Ursulina9335682016-11-02 15:14:59 +00005666 int err = -ENOMEM;
Chris Wilson42dcedd2012-11-15 11:32:30 +00005667
Tvrtko Ursulina9335682016-11-02 15:14:59 +00005668 dev_priv->objects = KMEM_CACHE(drm_i915_gem_object, SLAB_HWCACHE_ALIGN);
5669 if (!dev_priv->objects)
Chris Wilson73cb9702016-10-28 13:58:46 +01005670 goto err_out;
Chris Wilson73cb9702016-10-28 13:58:46 +01005671
Tvrtko Ursulina9335682016-11-02 15:14:59 +00005672 dev_priv->vmas = KMEM_CACHE(i915_vma, SLAB_HWCACHE_ALIGN);
5673 if (!dev_priv->vmas)
Chris Wilson73cb9702016-10-28 13:58:46 +01005674 goto err_objects;
Chris Wilson73cb9702016-10-28 13:58:46 +01005675
Chris Wilsond1b48c12017-08-16 09:52:08 +01005676 dev_priv->luts = KMEM_CACHE(i915_lut_handle, 0);
5677 if (!dev_priv->luts)
5678 goto err_vmas;
5679
Chris Wilsone61e0f52018-02-21 09:56:36 +00005680 dev_priv->requests = KMEM_CACHE(i915_request,
Tvrtko Ursulina9335682016-11-02 15:14:59 +00005681 SLAB_HWCACHE_ALIGN |
5682 SLAB_RECLAIM_ACCOUNT |
Paul E. McKenney5f0d5a32017-01-18 02:53:44 -08005683 SLAB_TYPESAFE_BY_RCU);
Tvrtko Ursulina9335682016-11-02 15:14:59 +00005684 if (!dev_priv->requests)
Chris Wilsond1b48c12017-08-16 09:52:08 +01005685 goto err_luts;
Chris Wilson73cb9702016-10-28 13:58:46 +01005686
Chris Wilson52e54202016-11-14 20:41:02 +00005687 dev_priv->dependencies = KMEM_CACHE(i915_dependency,
5688 SLAB_HWCACHE_ALIGN |
5689 SLAB_RECLAIM_ACCOUNT);
5690 if (!dev_priv->dependencies)
5691 goto err_requests;
5692
Chris Wilsonc5cf9a92017-05-17 13:10:04 +01005693 dev_priv->priorities = KMEM_CACHE(i915_priolist, SLAB_HWCACHE_ALIGN);
5694 if (!dev_priv->priorities)
5695 goto err_dependencies;
5696
Chris Wilson73cb9702016-10-28 13:58:46 +01005697 INIT_LIST_HEAD(&dev_priv->gt.timelines);
Chris Wilson643b4502018-04-30 14:15:03 +01005698 INIT_LIST_HEAD(&dev_priv->gt.active_rings);
Chris Wilson3365e222018-05-03 20:51:14 +01005699 INIT_LIST_HEAD(&dev_priv->gt.closed_vma);
Chris Wilson643b4502018-04-30 14:15:03 +01005700
Chris Wilson9c52d1c2017-11-10 23:24:47 +00005701 i915_gem_init__mm(dev_priv);
Chris Wilsonf2123812017-10-16 12:40:37 +01005702
Chris Wilson67d97da2016-07-04 08:08:31 +01005703 INIT_DELAYED_WORK(&dev_priv->gt.retire_work,
Eric Anholt673a3942008-07-30 12:06:12 -07005704 i915_gem_retire_work_handler);
Chris Wilson67d97da2016-07-04 08:08:31 +01005705 INIT_DELAYED_WORK(&dev_priv->gt.idle_work,
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005706 i915_gem_idle_work_handler);
Chris Wilson1f15b762016-07-01 17:23:14 +01005707 init_waitqueue_head(&dev_priv->gpu_error.wait_queue);
Daniel Vetter1f83fee2012-11-15 17:17:22 +01005708 init_waitqueue_head(&dev_priv->gpu_error.reset_queue);
Chris Wilson31169712009-09-14 16:50:28 +01005709
Joonas Lahtinen6f633402016-09-01 14:58:21 +03005710 atomic_set(&dev_priv->mm.bsd_engine_dispatch_index, 0);
5711
Chris Wilsonb5add952016-08-04 16:32:36 +01005712 spin_lock_init(&dev_priv->fb_tracking.lock);
Chris Wilson73cb9702016-10-28 13:58:46 +01005713
Matthew Auld465c4032017-10-06 23:18:14 +01005714 err = i915_gemfs_init(dev_priv);
5715 if (err)
5716 DRM_NOTE("Unable to create a private tmpfs mount, hugepage support will be disabled(%d).\n", err);
5717
Chris Wilson73cb9702016-10-28 13:58:46 +01005718 return 0;
5719
Chris Wilson52e54202016-11-14 20:41:02 +00005720err_dependencies:
5721 kmem_cache_destroy(dev_priv->dependencies);
Chris Wilson73cb9702016-10-28 13:58:46 +01005722err_requests:
5723 kmem_cache_destroy(dev_priv->requests);
Chris Wilsond1b48c12017-08-16 09:52:08 +01005724err_luts:
5725 kmem_cache_destroy(dev_priv->luts);
Chris Wilson73cb9702016-10-28 13:58:46 +01005726err_vmas:
5727 kmem_cache_destroy(dev_priv->vmas);
5728err_objects:
5729 kmem_cache_destroy(dev_priv->objects);
5730err_out:
5731 return err;
Eric Anholt673a3942008-07-30 12:06:12 -07005732}
Dave Airlie71acb5e2008-12-30 20:31:46 +10005733
Michal Wajdeczkoa0de9082018-03-23 12:34:49 +00005734void i915_gem_cleanup_early(struct drm_i915_private *dev_priv)
Imre Deakd64aa092016-01-19 15:26:29 +02005735{
Chris Wilsonc4d4c1c2017-02-10 16:35:23 +00005736 i915_gem_drain_freed_objects(dev_priv);
Chris Wilsonc9c704712018-02-19 22:06:31 +00005737 GEM_BUG_ON(!llist_empty(&dev_priv->mm.free_list));
5738 GEM_BUG_ON(atomic_read(&dev_priv->mm.free_count));
Chris Wilsonc4d4c1c2017-02-10 16:35:23 +00005739 WARN_ON(dev_priv->mm.object_count);
Matthew Auldea84aa72016-11-17 21:04:11 +00005740 WARN_ON(!list_empty(&dev_priv->gt.timelines));
Matthew Auldea84aa72016-11-17 21:04:11 +00005741
Chris Wilsonc5cf9a92017-05-17 13:10:04 +01005742 kmem_cache_destroy(dev_priv->priorities);
Chris Wilson52e54202016-11-14 20:41:02 +00005743 kmem_cache_destroy(dev_priv->dependencies);
Imre Deakd64aa092016-01-19 15:26:29 +02005744 kmem_cache_destroy(dev_priv->requests);
Chris Wilsond1b48c12017-08-16 09:52:08 +01005745 kmem_cache_destroy(dev_priv->luts);
Imre Deakd64aa092016-01-19 15:26:29 +02005746 kmem_cache_destroy(dev_priv->vmas);
5747 kmem_cache_destroy(dev_priv->objects);
Chris Wilson0eafec62016-08-04 16:32:41 +01005748
5749 /* And ensure that our DESTROY_BY_RCU slabs are truly destroyed */
5750 rcu_barrier();
Matthew Auld465c4032017-10-06 23:18:14 +01005751
5752 i915_gemfs_fini(dev_priv);
Imre Deakd64aa092016-01-19 15:26:29 +02005753}
5754
Chris Wilson6a800ea2016-09-21 14:51:07 +01005755int i915_gem_freeze(struct drm_i915_private *dev_priv)
5756{
Chris Wilsond0aa3012017-04-07 11:25:49 +01005757 /* Discard all purgeable objects, let userspace recover those as
5758 * required after resuming.
5759 */
Chris Wilson6a800ea2016-09-21 14:51:07 +01005760 i915_gem_shrink_all(dev_priv);
Chris Wilson6a800ea2016-09-21 14:51:07 +01005761
Chris Wilson6a800ea2016-09-21 14:51:07 +01005762 return 0;
5763}
5764
Chris Wilson95c778d2018-06-01 15:41:25 +01005765int i915_gem_freeze_late(struct drm_i915_private *i915)
Chris Wilson461fb992016-05-14 07:26:33 +01005766{
5767 struct drm_i915_gem_object *obj;
Chris Wilson7aab2d52016-09-09 20:02:18 +01005768 struct list_head *phases[] = {
Chris Wilson95c778d2018-06-01 15:41:25 +01005769 &i915->mm.unbound_list,
5770 &i915->mm.bound_list,
Chris Wilson7aab2d52016-09-09 20:02:18 +01005771 NULL
Chris Wilson95c778d2018-06-01 15:41:25 +01005772 }, **phase;
Chris Wilson461fb992016-05-14 07:26:33 +01005773
Chris Wilson95c778d2018-06-01 15:41:25 +01005774 /*
5775 * Called just before we write the hibernation image.
Chris Wilson461fb992016-05-14 07:26:33 +01005776 *
5777 * We need to update the domain tracking to reflect that the CPU
5778 * will be accessing all the pages to create and restore from the
5779 * hibernation, and so upon restoration those pages will be in the
5780 * CPU domain.
5781 *
5782 * To make sure the hibernation image contains the latest state,
5783 * we update that state just before writing out the image.
Chris Wilson7aab2d52016-09-09 20:02:18 +01005784 *
5785 * To try and reduce the hibernation image, we manually shrink
Chris Wilsond0aa3012017-04-07 11:25:49 +01005786 * the objects as well, see i915_gem_freeze()
Chris Wilson461fb992016-05-14 07:26:33 +01005787 */
5788
Chris Wilson95c778d2018-06-01 15:41:25 +01005789 i915_gem_shrink(i915, -1UL, NULL, I915_SHRINK_UNBOUND);
5790 i915_gem_drain_freed_objects(i915);
Chris Wilson461fb992016-05-14 07:26:33 +01005791
Chris Wilson95c778d2018-06-01 15:41:25 +01005792 mutex_lock(&i915->drm.struct_mutex);
5793 for (phase = phases; *phase; phase++) {
5794 list_for_each_entry(obj, *phase, mm.link)
5795 WARN_ON(i915_gem_object_set_to_cpu_domain(obj, true));
Chris Wilson461fb992016-05-14 07:26:33 +01005796 }
Chris Wilson95c778d2018-06-01 15:41:25 +01005797 mutex_unlock(&i915->drm.struct_mutex);
Chris Wilson461fb992016-05-14 07:26:33 +01005798
5799 return 0;
5800}
5801
Chris Wilsonf787a5f2010-09-24 16:02:42 +01005802void i915_gem_release(struct drm_device *dev, struct drm_file *file)
Eric Anholtb9624422009-06-03 07:27:35 +00005803{
Chris Wilsonf787a5f2010-09-24 16:02:42 +01005804 struct drm_i915_file_private *file_priv = file->driver_priv;
Chris Wilsone61e0f52018-02-21 09:56:36 +00005805 struct i915_request *request;
Eric Anholtb9624422009-06-03 07:27:35 +00005806
5807 /* Clean up our request list when the client is going away, so that
5808 * later retire_requests won't dereference our soon-to-be-gone
5809 * file_priv.
5810 */
Chris Wilson1c255952010-09-26 11:03:27 +01005811 spin_lock(&file_priv->mm.lock);
Chris Wilsonc8659ef2017-03-02 12:25:25 +00005812 list_for_each_entry(request, &file_priv->mm.request_list, client_link)
Chris Wilsonf787a5f2010-09-24 16:02:42 +01005813 request->file_priv = NULL;
Chris Wilson1c255952010-09-26 11:03:27 +01005814 spin_unlock(&file_priv->mm.lock);
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005815}
5816
Chris Wilson829a0af2017-06-20 12:05:45 +01005817int i915_gem_open(struct drm_i915_private *i915, struct drm_file *file)
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005818{
5819 struct drm_i915_file_private *file_priv;
Ben Widawskye422b882013-12-06 14:10:58 -08005820 int ret;
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005821
Chris Wilsonc4c29d72016-11-09 10:45:07 +00005822 DRM_DEBUG("\n");
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005823
5824 file_priv = kzalloc(sizeof(*file_priv), GFP_KERNEL);
5825 if (!file_priv)
5826 return -ENOMEM;
5827
5828 file->driver_priv = file_priv;
Chris Wilson829a0af2017-06-20 12:05:45 +01005829 file_priv->dev_priv = i915;
Chris Wilsonab0e7ff2014-02-25 17:11:24 +02005830 file_priv->file = file;
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005831
5832 spin_lock_init(&file_priv->mm.lock);
5833 INIT_LIST_HEAD(&file_priv->mm.request_list);
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005834
Chris Wilsonc80ff162016-07-27 09:07:27 +01005835 file_priv->bsd_engine = -1;
Mika Kuoppala14921f32018-06-15 13:44:29 +03005836 file_priv->hang_timestamp = jiffies;
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00005837
Chris Wilson829a0af2017-06-20 12:05:45 +01005838 ret = i915_gem_context_open(i915, file);
Ben Widawskye422b882013-12-06 14:10:58 -08005839 if (ret)
5840 kfree(file_priv);
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005841
Ben Widawskye422b882013-12-06 14:10:58 -08005842 return ret;
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005843}
5844
Daniel Vetterb680c372014-09-19 18:27:27 +02005845/**
5846 * i915_gem_track_fb - update frontbuffer tracking
Geliang Tangd9072a32015-09-15 05:58:44 -07005847 * @old: current GEM buffer for the frontbuffer slots
5848 * @new: new GEM buffer for the frontbuffer slots
5849 * @frontbuffer_bits: bitmask of frontbuffer slots
Daniel Vetterb680c372014-09-19 18:27:27 +02005850 *
5851 * This updates the frontbuffer tracking bits @frontbuffer_bits by clearing them
5852 * from @old and setting them in @new. Both @old and @new can be NULL.
5853 */
Daniel Vettera071fa02014-06-18 23:28:09 +02005854void i915_gem_track_fb(struct drm_i915_gem_object *old,
5855 struct drm_i915_gem_object *new,
5856 unsigned frontbuffer_bits)
5857{
Chris Wilsonfaf5bf02016-08-04 16:32:37 +01005858 /* Control of individual bits within the mask are guarded by
5859 * the owning plane->mutex, i.e. we can never see concurrent
5860 * manipulation of individual bits. But since the bitfield as a whole
5861 * is updated using RMW, we need to use atomics in order to update
5862 * the bits.
5863 */
5864 BUILD_BUG_ON(INTEL_FRONTBUFFER_BITS_PER_PIPE * I915_MAX_PIPES >
Chris Wilson74f6e182018-09-26 11:47:07 +01005865 BITS_PER_TYPE(atomic_t));
Chris Wilsonfaf5bf02016-08-04 16:32:37 +01005866
Daniel Vettera071fa02014-06-18 23:28:09 +02005867 if (old) {
Chris Wilsonfaf5bf02016-08-04 16:32:37 +01005868 WARN_ON(!(atomic_read(&old->frontbuffer_bits) & frontbuffer_bits));
5869 atomic_andnot(frontbuffer_bits, &old->frontbuffer_bits);
Daniel Vettera071fa02014-06-18 23:28:09 +02005870 }
5871
5872 if (new) {
Chris Wilsonfaf5bf02016-08-04 16:32:37 +01005873 WARN_ON(atomic_read(&new->frontbuffer_bits) & frontbuffer_bits);
5874 atomic_or(frontbuffer_bits, &new->frontbuffer_bits);
Daniel Vettera071fa02014-06-18 23:28:09 +02005875 }
5876}
5877
Dave Gordonea702992015-07-09 19:29:02 +01005878/* Allocate a new GEM object and fill it with the supplied data */
5879struct drm_i915_gem_object *
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +00005880i915_gem_object_create_from_data(struct drm_i915_private *dev_priv,
Dave Gordonea702992015-07-09 19:29:02 +01005881 const void *data, size_t size)
5882{
5883 struct drm_i915_gem_object *obj;
Chris Wilsonbe062fa2017-03-17 19:46:48 +00005884 struct file *file;
5885 size_t offset;
5886 int err;
Dave Gordonea702992015-07-09 19:29:02 +01005887
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +00005888 obj = i915_gem_object_create(dev_priv, round_up(size, PAGE_SIZE));
Chris Wilsonfe3db792016-04-25 13:32:13 +01005889 if (IS_ERR(obj))
Dave Gordonea702992015-07-09 19:29:02 +01005890 return obj;
5891
Christian Königc0a51fd2018-02-16 13:43:38 +01005892 GEM_BUG_ON(obj->write_domain != I915_GEM_DOMAIN_CPU);
Dave Gordonea702992015-07-09 19:29:02 +01005893
Chris Wilsonbe062fa2017-03-17 19:46:48 +00005894 file = obj->base.filp;
5895 offset = 0;
5896 do {
5897 unsigned int len = min_t(typeof(size), size, PAGE_SIZE);
5898 struct page *page;
5899 void *pgdata, *vaddr;
Dave Gordonea702992015-07-09 19:29:02 +01005900
Chris Wilsonbe062fa2017-03-17 19:46:48 +00005901 err = pagecache_write_begin(file, file->f_mapping,
5902 offset, len, 0,
5903 &page, &pgdata);
5904 if (err < 0)
5905 goto fail;
Dave Gordonea702992015-07-09 19:29:02 +01005906
Chris Wilsonbe062fa2017-03-17 19:46:48 +00005907 vaddr = kmap(page);
5908 memcpy(vaddr, data, len);
5909 kunmap(page);
5910
5911 err = pagecache_write_end(file, file->f_mapping,
5912 offset, len, len,
5913 page, pgdata);
5914 if (err < 0)
5915 goto fail;
5916
5917 size -= len;
5918 data += len;
5919 offset += len;
5920 } while (size);
Dave Gordonea702992015-07-09 19:29:02 +01005921
5922 return obj;
5923
5924fail:
Chris Wilsonf8c417c2016-07-20 13:31:53 +01005925 i915_gem_object_put(obj);
Chris Wilsonbe062fa2017-03-17 19:46:48 +00005926 return ERR_PTR(err);
Dave Gordonea702992015-07-09 19:29:02 +01005927}
Chris Wilson96d77632016-10-28 13:58:33 +01005928
5929struct scatterlist *
5930i915_gem_object_get_sg(struct drm_i915_gem_object *obj,
5931 unsigned int n,
5932 unsigned int *offset)
5933{
Chris Wilsona4f5ea62016-10-28 13:58:35 +01005934 struct i915_gem_object_page_iter *iter = &obj->mm.get_page;
Chris Wilson96d77632016-10-28 13:58:33 +01005935 struct scatterlist *sg;
5936 unsigned int idx, count;
5937
5938 might_sleep();
5939 GEM_BUG_ON(n >= obj->base.size >> PAGE_SHIFT);
Chris Wilsona4f5ea62016-10-28 13:58:35 +01005940 GEM_BUG_ON(!i915_gem_object_has_pinned_pages(obj));
Chris Wilson96d77632016-10-28 13:58:33 +01005941
5942 /* As we iterate forward through the sg, we record each entry in a
5943 * radixtree for quick repeated (backwards) lookups. If we have seen
5944 * this index previously, we will have an entry for it.
5945 *
5946 * Initial lookup is O(N), but this is amortized to O(1) for
5947 * sequential page access (where each new request is consecutive
5948 * to the previous one). Repeated lookups are O(lg(obj->base.size)),
5949 * i.e. O(1) with a large constant!
5950 */
5951 if (n < READ_ONCE(iter->sg_idx))
5952 goto lookup;
5953
5954 mutex_lock(&iter->lock);
5955
5956 /* We prefer to reuse the last sg so that repeated lookup of this
5957 * (or the subsequent) sg are fast - comparing against the last
5958 * sg is faster than going through the radixtree.
5959 */
5960
5961 sg = iter->sg_pos;
5962 idx = iter->sg_idx;
5963 count = __sg_page_count(sg);
5964
5965 while (idx + count <= n) {
Matthew Wilcox3159f942017-11-03 13:30:42 -04005966 void *entry;
5967 unsigned long i;
Chris Wilson96d77632016-10-28 13:58:33 +01005968 int ret;
5969
5970 /* If we cannot allocate and insert this entry, or the
5971 * individual pages from this range, cancel updating the
5972 * sg_idx so that on this lookup we are forced to linearly
5973 * scan onwards, but on future lookups we will try the
5974 * insertion again (in which case we need to be careful of
5975 * the error return reporting that we have already inserted
5976 * this index).
5977 */
5978 ret = radix_tree_insert(&iter->radix, idx, sg);
5979 if (ret && ret != -EEXIST)
5980 goto scan;
5981
Matthew Wilcox3159f942017-11-03 13:30:42 -04005982 entry = xa_mk_value(idx);
Chris Wilson96d77632016-10-28 13:58:33 +01005983 for (i = 1; i < count; i++) {
Matthew Wilcox3159f942017-11-03 13:30:42 -04005984 ret = radix_tree_insert(&iter->radix, idx + i, entry);
Chris Wilson96d77632016-10-28 13:58:33 +01005985 if (ret && ret != -EEXIST)
5986 goto scan;
5987 }
5988
5989 idx += count;
5990 sg = ____sg_next(sg);
5991 count = __sg_page_count(sg);
5992 }
5993
5994scan:
5995 iter->sg_pos = sg;
5996 iter->sg_idx = idx;
5997
5998 mutex_unlock(&iter->lock);
5999
6000 if (unlikely(n < idx)) /* insertion completed by another thread */
6001 goto lookup;
6002
6003 /* In case we failed to insert the entry into the radixtree, we need
6004 * to look beyond the current sg.
6005 */
6006 while (idx + count <= n) {
6007 idx += count;
6008 sg = ____sg_next(sg);
6009 count = __sg_page_count(sg);
6010 }
6011
6012 *offset = n - idx;
6013 return sg;
6014
6015lookup:
6016 rcu_read_lock();
6017
6018 sg = radix_tree_lookup(&iter->radix, n);
6019 GEM_BUG_ON(!sg);
6020
6021 /* If this index is in the middle of multi-page sg entry,
Matthew Wilcox3159f942017-11-03 13:30:42 -04006022 * the radix tree will contain a value entry that points
Chris Wilson96d77632016-10-28 13:58:33 +01006023 * to the start of that range. We will return the pointer to
6024 * the base page and the offset of this page within the
6025 * sg entry's range.
6026 */
6027 *offset = 0;
Matthew Wilcox3159f942017-11-03 13:30:42 -04006028 if (unlikely(xa_is_value(sg))) {
6029 unsigned long base = xa_to_value(sg);
Chris Wilson96d77632016-10-28 13:58:33 +01006030
6031 sg = radix_tree_lookup(&iter->radix, base);
6032 GEM_BUG_ON(!sg);
6033
6034 *offset = n - base;
6035 }
6036
6037 rcu_read_unlock();
6038
6039 return sg;
6040}
6041
6042struct page *
6043i915_gem_object_get_page(struct drm_i915_gem_object *obj, unsigned int n)
6044{
6045 struct scatterlist *sg;
6046 unsigned int offset;
6047
6048 GEM_BUG_ON(!i915_gem_object_has_struct_page(obj));
6049
6050 sg = i915_gem_object_get_sg(obj, n, &offset);
6051 return nth_page(sg_page(sg), offset);
6052}
6053
6054/* Like i915_gem_object_get_page(), but mark the returned page dirty */
6055struct page *
6056i915_gem_object_get_dirty_page(struct drm_i915_gem_object *obj,
6057 unsigned int n)
6058{
6059 struct page *page;
6060
6061 page = i915_gem_object_get_page(obj, n);
Chris Wilsona4f5ea62016-10-28 13:58:35 +01006062 if (!obj->mm.dirty)
Chris Wilson96d77632016-10-28 13:58:33 +01006063 set_page_dirty(page);
6064
6065 return page;
6066}
6067
6068dma_addr_t
6069i915_gem_object_get_dma_address(struct drm_i915_gem_object *obj,
6070 unsigned long n)
6071{
6072 struct scatterlist *sg;
6073 unsigned int offset;
6074
6075 sg = i915_gem_object_get_sg(obj, n, &offset);
6076 return sg_dma_address(sg) + (offset << PAGE_SHIFT);
6077}
Chris Wilson935a2f72017-02-13 17:15:13 +00006078
Chris Wilson8eeb7902017-07-26 19:16:01 +01006079int i915_gem_object_attach_phys(struct drm_i915_gem_object *obj, int align)
6080{
6081 struct sg_table *pages;
6082 int err;
6083
6084 if (align > obj->base.size)
6085 return -EINVAL;
6086
6087 if (obj->ops == &i915_gem_phys_ops)
6088 return 0;
6089
6090 if (obj->ops != &i915_gem_object_ops)
6091 return -EINVAL;
6092
6093 err = i915_gem_object_unbind(obj);
6094 if (err)
6095 return err;
6096
6097 mutex_lock(&obj->mm.lock);
6098
6099 if (obj->mm.madv != I915_MADV_WILLNEED) {
6100 err = -EFAULT;
6101 goto err_unlock;
6102 }
6103
6104 if (obj->mm.quirked) {
6105 err = -EFAULT;
6106 goto err_unlock;
6107 }
6108
6109 if (obj->mm.mapping) {
6110 err = -EBUSY;
6111 goto err_unlock;
6112 }
6113
Chris Wilsonacd1c1e2018-06-11 08:55:32 +01006114 pages = __i915_gem_object_unset_pages(obj);
Chris Wilsonf2123812017-10-16 12:40:37 +01006115
Chris Wilson8eeb7902017-07-26 19:16:01 +01006116 obj->ops = &i915_gem_phys_ops;
6117
Chris Wilson8fb6a5d2017-07-26 19:16:02 +01006118 err = ____i915_gem_object_get_pages(obj);
Chris Wilson8eeb7902017-07-26 19:16:01 +01006119 if (err)
6120 goto err_xfer;
6121
6122 /* Perma-pin (until release) the physical set of pages */
6123 __i915_gem_object_pin_pages(obj);
6124
6125 if (!IS_ERR_OR_NULL(pages))
6126 i915_gem_object_ops.put_pages(obj, pages);
6127 mutex_unlock(&obj->mm.lock);
6128 return 0;
6129
6130err_xfer:
6131 obj->ops = &i915_gem_object_ops;
Chris Wilsonacd1c1e2018-06-11 08:55:32 +01006132 if (!IS_ERR_OR_NULL(pages)) {
6133 unsigned int sg_page_sizes = i915_sg_page_sizes(pages->sgl);
6134
6135 __i915_gem_object_set_pages(obj, pages, sg_page_sizes);
6136 }
Chris Wilson8eeb7902017-07-26 19:16:01 +01006137err_unlock:
6138 mutex_unlock(&obj->mm.lock);
6139 return err;
6140}
6141
Chris Wilson935a2f72017-02-13 17:15:13 +00006142#if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
6143#include "selftests/scatterlist.c"
Chris Wilson66d9cb52017-02-13 17:15:17 +00006144#include "selftests/mock_gem_device.c"
Chris Wilson44653982017-02-13 17:15:20 +00006145#include "selftests/huge_gem_object.c"
Matthew Auld40498662017-10-06 23:18:29 +01006146#include "selftests/huge_pages.c"
Chris Wilson8335fd62017-02-13 17:15:28 +00006147#include "selftests/i915_gem_object.c"
Chris Wilson17059452017-02-13 17:15:32 +00006148#include "selftests/i915_gem_coherency.c"
Chris Wilson3f51b7e12018-08-30 14:48:06 +01006149#include "selftests/i915_gem.c"
Chris Wilson935a2f72017-02-13 17:15:13 +00006150#endif