blob: f6fe10fce0ecf339377afa5800fdc8d3c2ddcb1e [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>
Daniel Vetterfcd70cd2019-01-17 22:03:34 +010029#include <drm/drm_pci.h>
David Howells760285e2012-10-02 18:01:07 +010030#include <drm/i915_drm.h>
Chris Wilson6b5e90f2016-11-14 20:41:05 +000031#include <linux/dma-fence-array.h>
Chris Wilsonfe3288b2017-02-12 17:20:01 +000032#include <linux/kthread.h>
Chris Wilsonc13d87e2016-07-20 09:21:15 +010033#include <linux/reservation.h>
Hugh Dickins5949eac2011-06-27 16:18:18 -070034#include <linux/shmem_fs.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090035#include <linux/slab.h>
Chris Wilson20e49332016-11-22 14:41:21 +000036#include <linux/stop_machine.h>
Eric Anholt673a3942008-07-30 12:06:12 -070037#include <linux/swap.h>
Jesse Barnes79e53942008-11-07 14:24:08 -080038#include <linux/pci.h>
Daniel Vetter1286ff72012-05-10 15:25:09 +020039#include <linux/dma-buf.h>
Daniel Vetterfcd70cd2019-01-17 22:03:34 +010040#include <linux/mman.h>
Eric Anholt673a3942008-07-30 12:06:12 -070041
Chris Wilson9f588922019-01-16 15:33:04 +000042#include "i915_drv.h"
43#include "i915_gem_clflush.h"
44#include "i915_gemfs.h"
Chris Wilson32eb6bc2019-02-28 10:20:33 +000045#include "i915_globals.h"
Chris Wilson9f588922019-01-16 15:33:04 +000046#include "i915_reset.h"
47#include "i915_trace.h"
48#include "i915_vgpu.h"
49
50#include "intel_drv.h"
51#include "intel_frontbuffer.h"
52#include "intel_mocs.h"
53#include "intel_workarounds.h"
54
Chris Wilsonfbbd37b2016-10-28 13:58:42 +010055static void i915_gem_flush_free_objects(struct drm_i915_private *i915);
Chris Wilson61050802012-04-17 15:31:31 +010056
Chris Wilson2c225692013-08-09 12:26:45 +010057static bool cpu_write_needs_clflush(struct drm_i915_gem_object *obj)
58{
Chris Wilsone27ab732017-06-15 13:38:49 +010059 if (obj->cache_dirty)
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +053060 return false;
61
Chris Wilsonb8f55be2017-08-11 12:11:16 +010062 if (!(obj->cache_coherent & I915_BO_CACHE_COHERENT_FOR_WRITE))
Chris Wilson2c225692013-08-09 12:26:45 +010063 return true;
64
Chris Wilsonbd3d2252017-10-13 21:26:14 +010065 return obj->pin_global; /* currently in use by HW, keep flushed */
Chris Wilson2c225692013-08-09 12:26:45 +010066}
67
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +053068static int
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +010069insert_mappable_node(struct i915_ggtt *ggtt,
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +053070 struct drm_mm_node *node, u32 size)
71{
72 memset(node, 0, sizeof(*node));
Chris Wilson82ad6442018-06-05 16:37:58 +010073 return drm_mm_insert_node_in_range(&ggtt->vm.mm, node,
Chris Wilson4e64e552017-02-02 21:04:38 +000074 size, 0, I915_COLOR_UNEVICTABLE,
75 0, ggtt->mappable_end,
76 DRM_MM_INSERT_LOW);
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +053077}
78
79static void
80remove_mappable_node(struct drm_mm_node *node)
81{
82 drm_mm_remove_node(node);
83}
84
Chris Wilson73aa8082010-09-30 11:46:12 +010085/* some bookkeeping */
86static void i915_gem_info_add_obj(struct drm_i915_private *dev_priv,
Chris Wilson3ef7f222016-10-18 13:02:48 +010087 u64 size)
Chris Wilson73aa8082010-09-30 11:46:12 +010088{
Daniel Vetterc20e8352013-07-24 22:40:23 +020089 spin_lock(&dev_priv->mm.object_stat_lock);
Chris Wilson73aa8082010-09-30 11:46:12 +010090 dev_priv->mm.object_count++;
91 dev_priv->mm.object_memory += size;
Daniel Vetterc20e8352013-07-24 22:40:23 +020092 spin_unlock(&dev_priv->mm.object_stat_lock);
Chris Wilson73aa8082010-09-30 11:46:12 +010093}
94
95static void i915_gem_info_remove_obj(struct drm_i915_private *dev_priv,
Chris Wilson3ef7f222016-10-18 13:02:48 +010096 u64 size)
Chris Wilson73aa8082010-09-30 11:46:12 +010097{
Daniel Vetterc20e8352013-07-24 22:40:23 +020098 spin_lock(&dev_priv->mm.object_stat_lock);
Chris Wilson73aa8082010-09-30 11:46:12 +010099 dev_priv->mm.object_count--;
100 dev_priv->mm.object_memory -= size;
Daniel Vetterc20e8352013-07-24 22:40:23 +0200101 spin_unlock(&dev_priv->mm.object_stat_lock);
Chris Wilson73aa8082010-09-30 11:46:12 +0100102}
103
Chris Wilsone4d20062018-04-06 16:51:44 +0100104static u32 __i915_gem_park(struct drm_i915_private *i915)
105{
Chris Wilson506d1f62019-01-14 14:21:11 +0000106 intel_wakeref_t wakeref;
107
Chris Wilson4dfacb02018-05-31 09:22:43 +0100108 GEM_TRACE("\n");
109
Chris Wilsone4d20062018-04-06 16:51:44 +0100110 lockdep_assert_held(&i915->drm.struct_mutex);
111 GEM_BUG_ON(i915->gt.active_requests);
Chris Wilson643b4502018-04-30 14:15:03 +0100112 GEM_BUG_ON(!list_empty(&i915->gt.active_rings));
Chris Wilsone4d20062018-04-06 16:51:44 +0100113
114 if (!i915->gt.awake)
115 return I915_EPOCH_INVALID;
116
117 GEM_BUG_ON(i915->gt.epoch == I915_EPOCH_INVALID);
118
119 /*
120 * Be paranoid and flush a concurrent interrupt to make sure
121 * we don't reactivate any irq tasklets after parking.
122 *
123 * FIXME: Note that even though we have waited for execlists to be idle,
124 * there may still be an in-flight interrupt even though the CSB
125 * is now empty. synchronize_irq() makes sure that a residual interrupt
126 * is completed before we continue, but it doesn't prevent the HW from
127 * raising a spurious interrupt later. To complete the shield we should
128 * coordinate disabling the CS irq with flushing the interrupts.
129 */
130 synchronize_irq(i915->drm.irq);
131
132 intel_engines_park(i915);
Chris Wilsona89d1f92018-05-02 17:38:39 +0100133 i915_timelines_park(i915);
Chris Wilsone4d20062018-04-06 16:51:44 +0100134
135 i915_pmu_gt_parked(i915);
Chris Wilson3365e222018-05-03 20:51:14 +0100136 i915_vma_parked(i915);
Chris Wilsone4d20062018-04-06 16:51:44 +0100137
Chris Wilson506d1f62019-01-14 14:21:11 +0000138 wakeref = fetch_and_zero(&i915->gt.awake);
139 GEM_BUG_ON(!wakeref);
Chris Wilsone4d20062018-04-06 16:51:44 +0100140
141 if (INTEL_GEN(i915) >= 6)
142 gen6_rps_idle(i915);
143
Chris Wilson8d761e72019-01-14 14:21:28 +0000144 intel_display_power_put(i915, POWER_DOMAIN_GT_IRQ, wakeref);
Chris Wilsone4d20062018-04-06 16:51:44 +0100145
146 return i915->gt.epoch;
147}
148
149void i915_gem_park(struct drm_i915_private *i915)
150{
Chris Wilson4dfacb02018-05-31 09:22:43 +0100151 GEM_TRACE("\n");
152
Chris Wilsone4d20062018-04-06 16:51:44 +0100153 lockdep_assert_held(&i915->drm.struct_mutex);
154 GEM_BUG_ON(i915->gt.active_requests);
155
156 if (!i915->gt.awake)
157 return;
158
159 /* Defer the actual call to __i915_gem_park() to prevent ping-pongs */
160 mod_delayed_work(i915->wq, &i915->gt.idle_work, msecs_to_jiffies(100));
161}
162
163void i915_gem_unpark(struct drm_i915_private *i915)
164{
Chris Wilson4dfacb02018-05-31 09:22:43 +0100165 GEM_TRACE("\n");
166
Chris Wilsone4d20062018-04-06 16:51:44 +0100167 lockdep_assert_held(&i915->drm.struct_mutex);
168 GEM_BUG_ON(!i915->gt.active_requests);
Chris Wilson8d761e72019-01-14 14:21:28 +0000169 assert_rpm_wakelock_held(i915);
Chris Wilsone4d20062018-04-06 16:51:44 +0100170
171 if (i915->gt.awake)
172 return;
173
Chris Wilsone4d20062018-04-06 16:51:44 +0100174 /*
175 * It seems that the DMC likes to transition between the DC states a lot
176 * when there are no connected displays (no active power domains) during
177 * command submission.
178 *
179 * This activity has negative impact on the performance of the chip with
180 * huge latencies observed in the interrupt handler and elsewhere.
181 *
182 * Work around it by grabbing a GT IRQ power domain whilst there is any
183 * GT activity, preventing any DC state transitions.
184 */
Chris Wilson8d761e72019-01-14 14:21:28 +0000185 i915->gt.awake = intel_display_power_get(i915, POWER_DOMAIN_GT_IRQ);
186 GEM_BUG_ON(!i915->gt.awake);
Chris Wilsone4d20062018-04-06 16:51:44 +0100187
Chris Wilsone4d20062018-04-06 16:51:44 +0100188 if (unlikely(++i915->gt.epoch == 0)) /* keep 0 as invalid */
189 i915->gt.epoch = 1;
190
Chris Wilson32eb6bc2019-02-28 10:20:33 +0000191 i915_globals_unpark();
192
Chris Wilsone4d20062018-04-06 16:51:44 +0100193 intel_enable_gt_powersave(i915);
194 i915_update_gfx_val(i915);
195 if (INTEL_GEN(i915) >= 6)
196 gen6_rps_busy(i915);
197 i915_pmu_gt_unparked(i915);
198
199 intel_engines_unpark(i915);
200
201 i915_queue_hangcheck(i915);
202
203 queue_delayed_work(i915->wq,
204 &i915->gt.retire_work,
205 round_jiffies_up_relative(HZ));
206}
207
Eric Anholt673a3942008-07-30 12:06:12 -0700208int
Eric Anholt5a125c32008-10-22 21:40:13 -0700209i915_gem_get_aperture_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +0000210 struct drm_file *file)
Eric Anholt5a125c32008-10-22 21:40:13 -0700211{
Chris Wilson09d7e462019-01-28 10:23:53 +0000212 struct i915_ggtt *ggtt = &to_i915(dev)->ggtt;
Joonas Lahtinen72e96d62016-03-30 16:57:10 +0300213 struct drm_i915_gem_get_aperture *args = data;
Tvrtko Ursulinca1543b2015-07-01 11:51:10 +0100214 struct i915_vma *vma;
Weinan Liff8f7972017-05-31 10:35:52 +0800215 u64 pinned;
Eric Anholt5a125c32008-10-22 21:40:13 -0700216
Chris Wilson09d7e462019-01-28 10:23:53 +0000217 mutex_lock(&ggtt->vm.mutex);
218
Chris Wilson82ad6442018-06-05 16:37:58 +0100219 pinned = ggtt->vm.reserved;
Chris Wilson499197d2019-01-28 10:23:52 +0000220 list_for_each_entry(vma, &ggtt->vm.bound_list, vm_link)
Chris Wilson20dfbde2016-08-04 16:32:30 +0100221 if (i915_vma_is_pinned(vma))
Tvrtko Ursulinca1543b2015-07-01 11:51:10 +0100222 pinned += vma->node.size;
Chris Wilson09d7e462019-01-28 10:23:53 +0000223
224 mutex_unlock(&ggtt->vm.mutex);
Eric Anholt5a125c32008-10-22 21:40:13 -0700225
Chris Wilson82ad6442018-06-05 16:37:58 +0100226 args->aper_size = ggtt->vm.total;
Akshay Joshi0206e352011-08-16 15:34:10 -0400227 args->aper_available_size = args->aper_size - pinned;
Chris Wilson6299f992010-11-24 12:23:44 +0000228
Eric Anholt5a125c32008-10-22 21:40:13 -0700229 return 0;
230}
231
Matthew Auldb91b09e2017-10-06 23:18:17 +0100232static int i915_gem_object_get_pages_phys(struct drm_i915_gem_object *obj)
Chris Wilson00731152014-05-21 12:42:56 +0100233{
Al Viro93c76a32015-12-04 23:45:44 -0500234 struct address_space *mapping = obj->base.filp->f_mapping;
Chris Wilsondbb43512016-12-07 13:34:11 +0000235 drm_dma_handle_t *phys;
Chris Wilson6a2c4232014-11-04 04:51:40 -0800236 struct sg_table *st;
237 struct scatterlist *sg;
Chris Wilsondbb43512016-12-07 13:34:11 +0000238 char *vaddr;
Chris Wilson6a2c4232014-11-04 04:51:40 -0800239 int i;
Matthew Auldb91b09e2017-10-06 23:18:17 +0100240 int err;
Chris Wilson00731152014-05-21 12:42:56 +0100241
Chris Wilson6a2c4232014-11-04 04:51:40 -0800242 if (WARN_ON(i915_gem_object_needs_bit17_swizzle(obj)))
Matthew Auldb91b09e2017-10-06 23:18:17 +0100243 return -EINVAL;
Chris Wilson00731152014-05-21 12:42:56 +0100244
Chris Wilsondbb43512016-12-07 13:34:11 +0000245 /* Always aligning to the object size, allows a single allocation
246 * to handle all possible callers, and given typical object sizes,
247 * the alignment of the buddy allocation will naturally match.
248 */
249 phys = drm_pci_alloc(obj->base.dev,
Ville Syrjälä750fae22017-09-07 17:32:03 +0300250 roundup_pow_of_two(obj->base.size),
Chris Wilsondbb43512016-12-07 13:34:11 +0000251 roundup_pow_of_two(obj->base.size));
252 if (!phys)
Matthew Auldb91b09e2017-10-06 23:18:17 +0100253 return -ENOMEM;
Chris Wilsondbb43512016-12-07 13:34:11 +0000254
255 vaddr = phys->vaddr;
Chris Wilson6a2c4232014-11-04 04:51:40 -0800256 for (i = 0; i < obj->base.size / PAGE_SIZE; i++) {
257 struct page *page;
258 char *src;
259
260 page = shmem_read_mapping_page(mapping, i);
Chris Wilsondbb43512016-12-07 13:34:11 +0000261 if (IS_ERR(page)) {
Matthew Auldb91b09e2017-10-06 23:18:17 +0100262 err = PTR_ERR(page);
Chris Wilsondbb43512016-12-07 13:34:11 +0000263 goto err_phys;
264 }
Chris Wilson6a2c4232014-11-04 04:51:40 -0800265
266 src = kmap_atomic(page);
267 memcpy(vaddr, src, PAGE_SIZE);
268 drm_clflush_virt_range(vaddr, PAGE_SIZE);
269 kunmap_atomic(src);
270
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300271 put_page(page);
Chris Wilson6a2c4232014-11-04 04:51:40 -0800272 vaddr += PAGE_SIZE;
273 }
274
Chris Wilsonc0336662016-05-06 15:40:21 +0100275 i915_gem_chipset_flush(to_i915(obj->base.dev));
Chris Wilson6a2c4232014-11-04 04:51:40 -0800276
277 st = kmalloc(sizeof(*st), GFP_KERNEL);
Chris Wilsondbb43512016-12-07 13:34:11 +0000278 if (!st) {
Matthew Auldb91b09e2017-10-06 23:18:17 +0100279 err = -ENOMEM;
Chris Wilsondbb43512016-12-07 13:34:11 +0000280 goto err_phys;
281 }
Chris Wilson6a2c4232014-11-04 04:51:40 -0800282
283 if (sg_alloc_table(st, 1, GFP_KERNEL)) {
284 kfree(st);
Matthew Auldb91b09e2017-10-06 23:18:17 +0100285 err = -ENOMEM;
Chris Wilsondbb43512016-12-07 13:34:11 +0000286 goto err_phys;
Chris Wilson6a2c4232014-11-04 04:51:40 -0800287 }
288
289 sg = st->sgl;
290 sg->offset = 0;
291 sg->length = obj->base.size;
292
Chris Wilsondbb43512016-12-07 13:34:11 +0000293 sg_dma_address(sg) = phys->busaddr;
Chris Wilson6a2c4232014-11-04 04:51:40 -0800294 sg_dma_len(sg) = obj->base.size;
295
Chris Wilsondbb43512016-12-07 13:34:11 +0000296 obj->phys_handle = phys;
Matthew Auldb91b09e2017-10-06 23:18:17 +0100297
Matthew Aulda5c081662017-10-06 23:18:18 +0100298 __i915_gem_object_set_pages(obj, st, sg->length);
Matthew Auldb91b09e2017-10-06 23:18:17 +0100299
300 return 0;
Chris Wilsondbb43512016-12-07 13:34:11 +0000301
302err_phys:
303 drm_pci_free(obj->base.dev, phys);
Matthew Auldb91b09e2017-10-06 23:18:17 +0100304
305 return err;
Chris Wilson6a2c4232014-11-04 04:51:40 -0800306}
307
Chris Wilsone27ab732017-06-15 13:38:49 +0100308static void __start_cpu_write(struct drm_i915_gem_object *obj)
309{
Christian Königc0a51fd2018-02-16 13:43:38 +0100310 obj->read_domains = I915_GEM_DOMAIN_CPU;
311 obj->write_domain = I915_GEM_DOMAIN_CPU;
Chris Wilsone27ab732017-06-15 13:38:49 +0100312 if (cpu_write_needs_clflush(obj))
313 obj->cache_dirty = true;
314}
315
Chris Wilson6a2c4232014-11-04 04:51:40 -0800316static void
Chris Wilson2b3c8312016-11-11 14:58:09 +0000317__i915_gem_object_release_shmem(struct drm_i915_gem_object *obj,
Chris Wilsone5facdf2016-12-23 14:57:57 +0000318 struct sg_table *pages,
319 bool needs_clflush)
Chris Wilson6a2c4232014-11-04 04:51:40 -0800320{
Chris Wilsona4f5ea62016-10-28 13:58:35 +0100321 GEM_BUG_ON(obj->mm.madv == __I915_MADV_PURGED);
Chris Wilson6a2c4232014-11-04 04:51:40 -0800322
Chris Wilsona4f5ea62016-10-28 13:58:35 +0100323 if (obj->mm.madv == I915_MADV_DONTNEED)
324 obj->mm.dirty = false;
Chris Wilson6a2c4232014-11-04 04:51:40 -0800325
Chris Wilsone5facdf2016-12-23 14:57:57 +0000326 if (needs_clflush &&
Christian Königc0a51fd2018-02-16 13:43:38 +0100327 (obj->read_domains & I915_GEM_DOMAIN_CPU) == 0 &&
Chris Wilsonb8f55be2017-08-11 12:11:16 +0100328 !(obj->cache_coherent & I915_BO_CACHE_COHERENT_FOR_READ))
Chris Wilson2b3c8312016-11-11 14:58:09 +0000329 drm_clflush_sg(pages);
Chris Wilson03ac84f2016-10-28 13:58:36 +0100330
Chris Wilsone27ab732017-06-15 13:38:49 +0100331 __start_cpu_write(obj);
Chris Wilson03ac84f2016-10-28 13:58:36 +0100332}
333
334static void
335i915_gem_object_put_pages_phys(struct drm_i915_gem_object *obj,
336 struct sg_table *pages)
337{
Chris Wilsone5facdf2016-12-23 14:57:57 +0000338 __i915_gem_object_release_shmem(obj, pages, false);
Chris Wilson03ac84f2016-10-28 13:58:36 +0100339
Chris Wilsona4f5ea62016-10-28 13:58:35 +0100340 if (obj->mm.dirty) {
Al Viro93c76a32015-12-04 23:45:44 -0500341 struct address_space *mapping = obj->base.filp->f_mapping;
Chris Wilson6a2c4232014-11-04 04:51:40 -0800342 char *vaddr = obj->phys_handle->vaddr;
Chris Wilson00731152014-05-21 12:42:56 +0100343 int i;
344
345 for (i = 0; i < obj->base.size / PAGE_SIZE; i++) {
Chris Wilson6a2c4232014-11-04 04:51:40 -0800346 struct page *page;
347 char *dst;
Chris Wilson00731152014-05-21 12:42:56 +0100348
Chris Wilson6a2c4232014-11-04 04:51:40 -0800349 page = shmem_read_mapping_page(mapping, i);
350 if (IS_ERR(page))
351 continue;
352
353 dst = kmap_atomic(page);
354 drm_clflush_virt_range(vaddr, PAGE_SIZE);
355 memcpy(dst, vaddr, PAGE_SIZE);
356 kunmap_atomic(dst);
357
358 set_page_dirty(page);
Chris Wilsona4f5ea62016-10-28 13:58:35 +0100359 if (obj->mm.madv == I915_MADV_WILLNEED)
Chris Wilson00731152014-05-21 12:42:56 +0100360 mark_page_accessed(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300361 put_page(page);
Chris Wilson00731152014-05-21 12:42:56 +0100362 vaddr += PAGE_SIZE;
363 }
Chris Wilsona4f5ea62016-10-28 13:58:35 +0100364 obj->mm.dirty = false;
Chris Wilson00731152014-05-21 12:42:56 +0100365 }
366
Chris Wilson03ac84f2016-10-28 13:58:36 +0100367 sg_free_table(pages);
368 kfree(pages);
Chris Wilsondbb43512016-12-07 13:34:11 +0000369
370 drm_pci_free(obj->base.dev, obj->phys_handle);
Chris Wilson6a2c4232014-11-04 04:51:40 -0800371}
372
373static void
374i915_gem_object_release_phys(struct drm_i915_gem_object *obj)
375{
Chris Wilsona4f5ea62016-10-28 13:58:35 +0100376 i915_gem_object_unpin_pages(obj);
Chris Wilson6a2c4232014-11-04 04:51:40 -0800377}
378
379static const struct drm_i915_gem_object_ops i915_gem_phys_ops = {
380 .get_pages = i915_gem_object_get_pages_phys,
381 .put_pages = i915_gem_object_put_pages_phys,
382 .release = i915_gem_object_release_phys,
383};
384
Chris Wilson581ab1f2017-02-15 16:39:00 +0000385static const struct drm_i915_gem_object_ops i915_gem_object_ops;
386
Chris Wilson35a96112016-08-14 18:44:40 +0100387int i915_gem_object_unbind(struct drm_i915_gem_object *obj)
Chris Wilsonaa653a62016-08-04 07:52:27 +0100388{
389 struct i915_vma *vma;
390 LIST_HEAD(still_in_list);
Chris Wilson02bef8f2016-08-14 18:44:41 +0100391 int ret;
Chris Wilsonaa653a62016-08-04 07:52:27 +0100392
Chris Wilson02bef8f2016-08-14 18:44:41 +0100393 lockdep_assert_held(&obj->base.dev->struct_mutex);
394
395 /* Closed vma are removed from the obj->vma_list - but they may
396 * still have an active binding on the object. To remove those we
397 * must wait for all rendering to complete to the object (as unbinding
398 * must anyway), and retire the requests.
Chris Wilsonaa653a62016-08-04 07:52:27 +0100399 */
Chris Wilson5888fc92017-12-04 13:25:13 +0000400 ret = i915_gem_object_set_to_cpu_domain(obj, false);
Chris Wilson02bef8f2016-08-14 18:44:41 +0100401 if (ret)
402 return ret;
403
Chris Wilson528cbd12019-01-28 10:23:54 +0000404 spin_lock(&obj->vma.lock);
405 while (!ret && (vma = list_first_entry_or_null(&obj->vma.list,
406 struct i915_vma,
407 obj_link))) {
Chris Wilsonaa653a62016-08-04 07:52:27 +0100408 list_move_tail(&vma->obj_link, &still_in_list);
Chris Wilson528cbd12019-01-28 10:23:54 +0000409 spin_unlock(&obj->vma.lock);
410
Chris Wilsonaa653a62016-08-04 07:52:27 +0100411 ret = i915_vma_unbind(vma);
Chris Wilson528cbd12019-01-28 10:23:54 +0000412
413 spin_lock(&obj->vma.lock);
Chris Wilsonaa653a62016-08-04 07:52:27 +0100414 }
Chris Wilson528cbd12019-01-28 10:23:54 +0000415 list_splice(&still_in_list, &obj->vma.list);
416 spin_unlock(&obj->vma.lock);
Chris Wilsonaa653a62016-08-04 07:52:27 +0100417
418 return ret;
419}
420
Chris Wilsone95433c2016-10-28 13:58:27 +0100421static long
422i915_gem_object_wait_fence(struct dma_fence *fence,
423 unsigned int flags,
Chris Wilson62eb3c22019-02-13 09:25:04 +0000424 long timeout)
Chris Wilsone95433c2016-10-28 13:58:27 +0100425{
Chris Wilsone61e0f52018-02-21 09:56:36 +0000426 struct i915_request *rq;
Chris Wilsone95433c2016-10-28 13:58:27 +0100427
428 BUILD_BUG_ON(I915_WAIT_INTERRUPTIBLE != 0x1);
429
430 if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags))
431 return timeout;
432
433 if (!dma_fence_is_i915(fence))
434 return dma_fence_wait_timeout(fence,
435 flags & I915_WAIT_INTERRUPTIBLE,
436 timeout);
437
438 rq = to_request(fence);
Chris Wilsone61e0f52018-02-21 09:56:36 +0000439 if (i915_request_completed(rq))
Chris Wilsone95433c2016-10-28 13:58:27 +0100440 goto out;
441
Chris Wilsone61e0f52018-02-21 09:56:36 +0000442 timeout = i915_request_wait(rq, flags, timeout);
Chris Wilsone95433c2016-10-28 13:58:27 +0100443
444out:
Chris Wilsone61e0f52018-02-21 09:56:36 +0000445 if (flags & I915_WAIT_LOCKED && i915_request_completed(rq))
446 i915_request_retire_upto(rq);
Chris Wilsone95433c2016-10-28 13:58:27 +0100447
Chris Wilsone95433c2016-10-28 13:58:27 +0100448 return timeout;
449}
450
451static long
452i915_gem_object_wait_reservation(struct reservation_object *resv,
453 unsigned int flags,
Chris Wilson62eb3c22019-02-13 09:25:04 +0000454 long timeout)
Chris Wilsone95433c2016-10-28 13:58:27 +0100455{
Chris Wilsone54ca972017-02-17 15:13:04 +0000456 unsigned int seq = __read_seqcount_begin(&resv->seq);
Chris Wilsone95433c2016-10-28 13:58:27 +0100457 struct dma_fence *excl;
Chris Wilsone54ca972017-02-17 15:13:04 +0000458 bool prune_fences = false;
Chris Wilsone95433c2016-10-28 13:58:27 +0100459
460 if (flags & I915_WAIT_ALL) {
461 struct dma_fence **shared;
462 unsigned int count, i;
463 int ret;
464
465 ret = reservation_object_get_fences_rcu(resv,
466 &excl, &count, &shared);
467 if (ret)
468 return ret;
469
470 for (i = 0; i < count; i++) {
471 timeout = i915_gem_object_wait_fence(shared[i],
Chris Wilson62eb3c22019-02-13 09:25:04 +0000472 flags, timeout);
Chris Wilsond892e932017-02-12 21:53:43 +0000473 if (timeout < 0)
Chris Wilsone95433c2016-10-28 13:58:27 +0100474 break;
475
476 dma_fence_put(shared[i]);
477 }
478
479 for (; i < count; i++)
480 dma_fence_put(shared[i]);
481 kfree(shared);
Chris Wilsone54ca972017-02-17 15:13:04 +0000482
Chris Wilsonfa730552018-03-07 17:13:03 +0000483 /*
484 * If both shared fences and an exclusive fence exist,
485 * then by construction the shared fences must be later
486 * than the exclusive fence. If we successfully wait for
487 * all the shared fences, we know that the exclusive fence
488 * must all be signaled. If all the shared fences are
489 * signaled, we can prune the array and recover the
490 * floating references on the fences/requests.
491 */
Chris Wilsone54ca972017-02-17 15:13:04 +0000492 prune_fences = count && timeout >= 0;
Chris Wilsone95433c2016-10-28 13:58:27 +0100493 } else {
494 excl = reservation_object_get_excl_rcu(resv);
495 }
496
Chris Wilsonfa730552018-03-07 17:13:03 +0000497 if (excl && timeout >= 0)
Chris Wilson62eb3c22019-02-13 09:25:04 +0000498 timeout = i915_gem_object_wait_fence(excl, flags, timeout);
Chris Wilsone95433c2016-10-28 13:58:27 +0100499
500 dma_fence_put(excl);
501
Chris Wilsonfa730552018-03-07 17:13:03 +0000502 /*
503 * Opportunistically prune the fences iff we know they have *all* been
Chris Wilson03d1cac2017-03-08 13:26:28 +0000504 * signaled and that the reservation object has not been changed (i.e.
505 * no new fences have been added).
506 */
Chris Wilsone54ca972017-02-17 15:13:04 +0000507 if (prune_fences && !__read_seqcount_retry(&resv->seq, seq)) {
Chris Wilson03d1cac2017-03-08 13:26:28 +0000508 if (reservation_object_trylock(resv)) {
509 if (!__read_seqcount_retry(&resv->seq, seq))
510 reservation_object_add_excl_fence(resv, NULL);
511 reservation_object_unlock(resv);
512 }
Chris Wilsone54ca972017-02-17 15:13:04 +0000513 }
514
Chris Wilsone95433c2016-10-28 13:58:27 +0100515 return timeout;
516}
517
Chris Wilsonb7268c52018-04-18 19:40:52 +0100518static void __fence_set_priority(struct dma_fence *fence,
519 const struct i915_sched_attr *attr)
Chris Wilson6b5e90f2016-11-14 20:41:05 +0000520{
Chris Wilsone61e0f52018-02-21 09:56:36 +0000521 struct i915_request *rq;
Chris Wilson6b5e90f2016-11-14 20:41:05 +0000522 struct intel_engine_cs *engine;
523
Chris Wilsonc218ee02018-01-06 10:56:18 +0000524 if (dma_fence_is_signaled(fence) || !dma_fence_is_i915(fence))
Chris Wilson6b5e90f2016-11-14 20:41:05 +0000525 return;
526
527 rq = to_request(fence);
528 engine = rq->engine;
Chris Wilson6b5e90f2016-11-14 20:41:05 +0000529
Chris Wilson4f6d8fc2018-05-07 14:57:25 +0100530 local_bh_disable();
531 rcu_read_lock(); /* RCU serialisation for set-wedged protection */
Chris Wilson47650db2018-03-07 13:42:25 +0000532 if (engine->schedule)
Chris Wilsonb7268c52018-04-18 19:40:52 +0100533 engine->schedule(rq, attr);
Chris Wilson47650db2018-03-07 13:42:25 +0000534 rcu_read_unlock();
Chris Wilson4f6d8fc2018-05-07 14:57:25 +0100535 local_bh_enable(); /* kick the tasklets if queues were reprioritised */
Chris Wilson6b5e90f2016-11-14 20:41:05 +0000536}
537
Chris Wilsonb7268c52018-04-18 19:40:52 +0100538static void fence_set_priority(struct dma_fence *fence,
539 const struct i915_sched_attr *attr)
Chris Wilson6b5e90f2016-11-14 20:41:05 +0000540{
541 /* Recurse once into a fence-array */
542 if (dma_fence_is_array(fence)) {
543 struct dma_fence_array *array = to_dma_fence_array(fence);
544 int i;
545
546 for (i = 0; i < array->num_fences; i++)
Chris Wilsonb7268c52018-04-18 19:40:52 +0100547 __fence_set_priority(array->fences[i], attr);
Chris Wilson6b5e90f2016-11-14 20:41:05 +0000548 } else {
Chris Wilsonb7268c52018-04-18 19:40:52 +0100549 __fence_set_priority(fence, attr);
Chris Wilson6b5e90f2016-11-14 20:41:05 +0000550 }
551}
552
553int
554i915_gem_object_wait_priority(struct drm_i915_gem_object *obj,
555 unsigned int flags,
Chris Wilsonb7268c52018-04-18 19:40:52 +0100556 const struct i915_sched_attr *attr)
Chris Wilson6b5e90f2016-11-14 20:41:05 +0000557{
558 struct dma_fence *excl;
559
560 if (flags & I915_WAIT_ALL) {
561 struct dma_fence **shared;
562 unsigned int count, i;
563 int ret;
564
565 ret = reservation_object_get_fences_rcu(obj->resv,
566 &excl, &count, &shared);
567 if (ret)
568 return ret;
569
570 for (i = 0; i < count; i++) {
Chris Wilsonb7268c52018-04-18 19:40:52 +0100571 fence_set_priority(shared[i], attr);
Chris Wilson6b5e90f2016-11-14 20:41:05 +0000572 dma_fence_put(shared[i]);
573 }
574
575 kfree(shared);
576 } else {
577 excl = reservation_object_get_excl_rcu(obj->resv);
578 }
579
580 if (excl) {
Chris Wilsonb7268c52018-04-18 19:40:52 +0100581 fence_set_priority(excl, attr);
Chris Wilson6b5e90f2016-11-14 20:41:05 +0000582 dma_fence_put(excl);
583 }
584 return 0;
585}
586
Chris Wilson00e60f22016-08-04 16:32:40 +0100587/**
Chris Wilsone95433c2016-10-28 13:58:27 +0100588 * Waits for rendering to the object to be completed
Chris Wilson00e60f22016-08-04 16:32:40 +0100589 * @obj: i915 gem object
Chris Wilsone95433c2016-10-28 13:58:27 +0100590 * @flags: how to wait (under a lock, for all rendering or just for writes etc)
591 * @timeout: how long to wait
Chris Wilson00e60f22016-08-04 16:32:40 +0100592 */
593int
Chris Wilsone95433c2016-10-28 13:58:27 +0100594i915_gem_object_wait(struct drm_i915_gem_object *obj,
595 unsigned int flags,
Chris Wilson62eb3c22019-02-13 09:25:04 +0000596 long timeout)
Chris Wilson00e60f22016-08-04 16:32:40 +0100597{
Chris Wilsone95433c2016-10-28 13:58:27 +0100598 might_sleep();
Chris Wilsone95433c2016-10-28 13:58:27 +0100599 GEM_BUG_ON(timeout < 0);
Chris Wilson00e60f22016-08-04 16:32:40 +0100600
Chris Wilson62eb3c22019-02-13 09:25:04 +0000601 timeout = i915_gem_object_wait_reservation(obj->resv, flags, timeout);
Chris Wilsone95433c2016-10-28 13:58:27 +0100602 return timeout < 0 ? timeout : 0;
Chris Wilson00e60f22016-08-04 16:32:40 +0100603}
604
Chris Wilson00731152014-05-21 12:42:56 +0100605static int
606i915_gem_phys_pwrite(struct drm_i915_gem_object *obj,
607 struct drm_i915_gem_pwrite *args,
Chris Wilson03ac84f2016-10-28 13:58:36 +0100608 struct drm_file *file)
Chris Wilson00731152014-05-21 12:42:56 +0100609{
Chris Wilson00731152014-05-21 12:42:56 +0100610 void *vaddr = obj->phys_handle->vaddr + args->offset;
Gustavo Padovan3ed605b2016-04-26 12:32:27 -0300611 char __user *user_data = u64_to_user_ptr(args->data_ptr);
Chris Wilson6a2c4232014-11-04 04:51:40 -0800612
613 /* We manually control the domain here and pretend that it
614 * remains coherent i.e. in the GTT domain, like shmem_pwrite.
615 */
Rodrigo Vivi77a0d1c2015-06-18 11:43:24 -0700616 intel_fb_obj_invalidate(obj, ORIGIN_CPU);
Chris Wilson10466d22017-01-06 15:22:38 +0000617 if (copy_from_user(vaddr, user_data, args->size))
618 return -EFAULT;
Chris Wilson00731152014-05-21 12:42:56 +0100619
Chris Wilson6a2c4232014-11-04 04:51:40 -0800620 drm_clflush_virt_range(vaddr, args->size);
Chris Wilson10466d22017-01-06 15:22:38 +0000621 i915_gem_chipset_flush(to_i915(obj->base.dev));
Paulo Zanoni063e4e62015-02-13 17:23:45 -0200622
Chris Wilsond59b21e2017-02-22 11:40:49 +0000623 intel_fb_obj_flush(obj, ORIGIN_CPU);
Chris Wilson10466d22017-01-06 15:22:38 +0000624 return 0;
Chris Wilson00731152014-05-21 12:42:56 +0100625}
626
Tvrtko Ursulin187685c2016-12-01 14:16:36 +0000627void *i915_gem_object_alloc(struct drm_i915_private *dev_priv)
Chris Wilson42dcedd2012-11-15 11:32:30 +0000628{
Chris Wilsonefab6d82015-04-07 16:20:57 +0100629 return kmem_cache_zalloc(dev_priv->objects, GFP_KERNEL);
Chris Wilson42dcedd2012-11-15 11:32:30 +0000630}
631
632void i915_gem_object_free(struct drm_i915_gem_object *obj)
633{
Chris Wilsonfac5e232016-07-04 11:34:36 +0100634 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
Chris Wilsonefab6d82015-04-07 16:20:57 +0100635 kmem_cache_free(dev_priv->objects, obj);
Chris Wilson42dcedd2012-11-15 11:32:30 +0000636}
637
Dave Airlieff72145b2011-02-07 12:16:14 +1000638static int
639i915_gem_create(struct drm_file *file,
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +0000640 struct drm_i915_private *dev_priv,
Jani Nikula739f3ab2019-01-16 11:15:19 +0200641 u64 size,
642 u32 *handle_p)
Eric Anholt673a3942008-07-30 12:06:12 -0700643{
Chris Wilson05394f32010-11-08 19:18:58 +0000644 struct drm_i915_gem_object *obj;
Pekka Paalanena1a2d1d2009-08-23 12:40:55 +0300645 int ret;
646 u32 handle;
Eric Anholt673a3942008-07-30 12:06:12 -0700647
Dave Airlieff72145b2011-02-07 12:16:14 +1000648 size = roundup(size, PAGE_SIZE);
Chris Wilson8ffc0242011-09-14 14:14:28 +0200649 if (size == 0)
650 return -EINVAL;
Eric Anholt673a3942008-07-30 12:06:12 -0700651
652 /* Allocate the new object */
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +0000653 obj = i915_gem_object_create(dev_priv, size);
Chris Wilsonfe3db792016-04-25 13:32:13 +0100654 if (IS_ERR(obj))
655 return PTR_ERR(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700656
Chris Wilson05394f32010-11-08 19:18:58 +0000657 ret = drm_gem_handle_create(file, &obj->base, &handle);
Chris Wilson202f2fe2010-10-14 13:20:40 +0100658 /* drop reference from allocate - handle holds it now */
Chris Wilsonf0cd5182016-10-28 13:58:43 +0100659 i915_gem_object_put(obj);
Daniel Vetterd861e332013-07-24 23:25:03 +0200660 if (ret)
661 return ret;
Chris Wilson202f2fe2010-10-14 13:20:40 +0100662
Dave Airlieff72145b2011-02-07 12:16:14 +1000663 *handle_p = handle;
Eric Anholt673a3942008-07-30 12:06:12 -0700664 return 0;
665}
666
Dave Airlieff72145b2011-02-07 12:16:14 +1000667int
668i915_gem_dumb_create(struct drm_file *file,
669 struct drm_device *dev,
670 struct drm_mode_create_dumb *args)
671{
672 /* have to work out size/pitch and return them */
Paulo Zanonide45eaf2013-10-18 18:48:24 -0300673 args->pitch = ALIGN(args->width * DIV_ROUND_UP(args->bpp, 8), 64);
Dave Airlieff72145b2011-02-07 12:16:14 +1000674 args->size = args->pitch * args->height;
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +0000675 return i915_gem_create(file, to_i915(dev),
Dave Airlieda6b51d2014-12-24 13:11:17 +1000676 args->size, &args->handle);
Dave Airlieff72145b2011-02-07 12:16:14 +1000677}
678
Chris Wilsone27ab732017-06-15 13:38:49 +0100679static bool gpu_write_needs_clflush(struct drm_i915_gem_object *obj)
680{
681 return !(obj->cache_level == I915_CACHE_NONE ||
682 obj->cache_level == I915_CACHE_WT);
683}
684
Dave Airlieff72145b2011-02-07 12:16:14 +1000685/**
686 * Creates a new mm object and returns a handle to it.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +0100687 * @dev: drm device pointer
688 * @data: ioctl data blob
689 * @file: drm file pointer
Dave Airlieff72145b2011-02-07 12:16:14 +1000690 */
691int
692i915_gem_create_ioctl(struct drm_device *dev, void *data,
693 struct drm_file *file)
694{
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +0000695 struct drm_i915_private *dev_priv = to_i915(dev);
Dave Airlieff72145b2011-02-07 12:16:14 +1000696 struct drm_i915_gem_create *args = data;
Daniel Vetter63ed2cb2012-04-23 16:50:50 +0200697
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +0000698 i915_gem_flush_free_objects(dev_priv);
Chris Wilsonfbbd37b2016-10-28 13:58:42 +0100699
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +0000700 return i915_gem_create(file, dev_priv,
Dave Airlieda6b51d2014-12-24 13:11:17 +1000701 args->size, &args->handle);
Dave Airlieff72145b2011-02-07 12:16:14 +1000702}
703
Chris Wilsonef749212017-04-12 12:01:10 +0100704static inline enum fb_op_origin
705fb_write_origin(struct drm_i915_gem_object *obj, unsigned int domain)
706{
707 return (domain == I915_GEM_DOMAIN_GTT ?
708 obj->frontbuffer_ggtt_origin : ORIGIN_CPU);
709}
710
Chris Wilson7125397b2017-12-06 12:49:14 +0000711void i915_gem_flush_ggtt_writes(struct drm_i915_private *dev_priv)
Chris Wilsonef749212017-04-12 12:01:10 +0100712{
Chris Wilson538ef962019-01-14 14:21:18 +0000713 intel_wakeref_t wakeref;
714
Chris Wilson7125397b2017-12-06 12:49:14 +0000715 /*
716 * No actual flushing is required for the GTT write domain for reads
717 * from the GTT domain. Writes to it "immediately" go to main memory
718 * as far as we know, so there's no chipset flush. It also doesn't
719 * land in the GPU render cache.
Chris Wilsonef749212017-04-12 12:01:10 +0100720 *
721 * However, we do have to enforce the order so that all writes through
722 * the GTT land before any writes to the device, such as updates to
723 * the GATT itself.
724 *
725 * We also have to wait a bit for the writes to land from the GTT.
726 * An uncached read (i.e. mmio) seems to be ideal for the round-trip
727 * timing. This issue has only been observed when switching quickly
728 * between GTT writes and CPU reads from inside the kernel on recent hw,
729 * and it appears to only affect discrete GTT blocks (i.e. on LLC
Chris Wilson7125397b2017-12-06 12:49:14 +0000730 * system agents we cannot reproduce this behaviour, until Cannonlake
731 * that was!).
Chris Wilsonef749212017-04-12 12:01:10 +0100732 */
Chris Wilson7125397b2017-12-06 12:49:14 +0000733
Chris Wilson900ccf32018-07-20 11:19:10 +0100734 wmb();
735
736 if (INTEL_INFO(dev_priv)->has_coherent_ggtt)
737 return;
738
Chris Wilsona8bd3b82018-07-17 10:26:55 +0100739 i915_gem_chipset_flush(dev_priv);
Chris Wilsonef749212017-04-12 12:01:10 +0100740
Chris Wilsond4225a52019-01-14 14:21:23 +0000741 with_intel_runtime_pm(dev_priv, wakeref) {
742 spin_lock_irq(&dev_priv->uncore.lock);
Chris Wilson7125397b2017-12-06 12:49:14 +0000743
Chris Wilsond4225a52019-01-14 14:21:23 +0000744 POSTING_READ_FW(RING_HEAD(RENDER_RING_BASE));
Chris Wilson7125397b2017-12-06 12:49:14 +0000745
Chris Wilsond4225a52019-01-14 14:21:23 +0000746 spin_unlock_irq(&dev_priv->uncore.lock);
747 }
Chris Wilson7125397b2017-12-06 12:49:14 +0000748}
749
750static void
751flush_write_domain(struct drm_i915_gem_object *obj, unsigned int flush_domains)
752{
753 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
754 struct i915_vma *vma;
755
Christian Königc0a51fd2018-02-16 13:43:38 +0100756 if (!(obj->write_domain & flush_domains))
Chris Wilson7125397b2017-12-06 12:49:14 +0000757 return;
758
Christian Königc0a51fd2018-02-16 13:43:38 +0100759 switch (obj->write_domain) {
Chris Wilsonef749212017-04-12 12:01:10 +0100760 case I915_GEM_DOMAIN_GTT:
Chris Wilson7125397b2017-12-06 12:49:14 +0000761 i915_gem_flush_ggtt_writes(dev_priv);
Chris Wilsonef749212017-04-12 12:01:10 +0100762
763 intel_fb_obj_flush(obj,
764 fb_write_origin(obj, I915_GEM_DOMAIN_GTT));
Chris Wilson7125397b2017-12-06 12:49:14 +0000765
Chris Wilsone2189dd2017-12-07 21:14:07 +0000766 for_each_ggtt_vma(vma, obj) {
Chris Wilson7125397b2017-12-06 12:49:14 +0000767 if (vma->iomap)
768 continue;
769
770 i915_vma_unset_ggtt_write(vma);
771 }
Chris Wilsonef749212017-04-12 12:01:10 +0100772 break;
773
Chris Wilsonadd00e62018-07-06 12:54:02 +0100774 case I915_GEM_DOMAIN_WC:
775 wmb();
776 break;
777
Chris Wilsonef749212017-04-12 12:01:10 +0100778 case I915_GEM_DOMAIN_CPU:
779 i915_gem_clflush_object(obj, I915_CLFLUSH_SYNC);
780 break;
Chris Wilsone27ab732017-06-15 13:38:49 +0100781
782 case I915_GEM_DOMAIN_RENDER:
783 if (gpu_write_needs_clflush(obj))
784 obj->cache_dirty = true;
785 break;
Chris Wilsonef749212017-04-12 12:01:10 +0100786 }
787
Christian Königc0a51fd2018-02-16 13:43:38 +0100788 obj->write_domain = 0;
Chris Wilsonef749212017-04-12 12:01:10 +0100789}
790
Brad Volkin4c914c02014-02-18 10:15:45 -0800791/*
792 * Pins the specified object's pages and synchronizes the object with
793 * GPU accesses. Sets needs_clflush to non-zero if the caller should
794 * flush the object from the CPU cache.
795 */
796int i915_gem_obj_prepare_shmem_read(struct drm_i915_gem_object *obj,
Chris Wilson43394c72016-08-18 17:16:47 +0100797 unsigned int *needs_clflush)
Brad Volkin4c914c02014-02-18 10:15:45 -0800798{
799 int ret;
800
Chris Wilsone95433c2016-10-28 13:58:27 +0100801 lockdep_assert_held(&obj->base.dev->struct_mutex);
Brad Volkin4c914c02014-02-18 10:15:45 -0800802
Chris Wilsone95433c2016-10-28 13:58:27 +0100803 *needs_clflush = 0;
Chris Wilson43394c72016-08-18 17:16:47 +0100804 if (!i915_gem_object_has_struct_page(obj))
805 return -ENODEV;
Brad Volkin4c914c02014-02-18 10:15:45 -0800806
Chris Wilsone95433c2016-10-28 13:58:27 +0100807 ret = i915_gem_object_wait(obj,
808 I915_WAIT_INTERRUPTIBLE |
809 I915_WAIT_LOCKED,
Chris Wilson62eb3c22019-02-13 09:25:04 +0000810 MAX_SCHEDULE_TIMEOUT);
Chris Wilsonc13d87e2016-07-20 09:21:15 +0100811 if (ret)
812 return ret;
813
Chris Wilsona4f5ea62016-10-28 13:58:35 +0100814 ret = i915_gem_object_pin_pages(obj);
Chris Wilson97649512016-08-18 17:16:50 +0100815 if (ret)
816 return ret;
817
Chris Wilsonb8f55be2017-08-11 12:11:16 +0100818 if (obj->cache_coherent & I915_BO_CACHE_COHERENT_FOR_READ ||
819 !static_cpu_has(X86_FEATURE_CLFLUSH)) {
Chris Wilson7f5f95d2017-03-10 00:09:42 +0000820 ret = i915_gem_object_set_to_cpu_domain(obj, false);
821 if (ret)
822 goto err_unpin;
823 else
824 goto out;
825 }
826
Chris Wilsonef749212017-04-12 12:01:10 +0100827 flush_write_domain(obj, ~I915_GEM_DOMAIN_CPU);
Chris Wilsona314d5c2016-08-18 17:16:48 +0100828
Chris Wilson43394c72016-08-18 17:16:47 +0100829 /* If we're not in the cpu read domain, set ourself into the gtt
830 * read domain and manually flush cachelines (if required). This
831 * optimizes for the case when the gpu will dirty the data
832 * anyway again before the next pread happens.
833 */
Chris Wilsone27ab732017-06-15 13:38:49 +0100834 if (!obj->cache_dirty &&
Christian Königc0a51fd2018-02-16 13:43:38 +0100835 !(obj->read_domains & I915_GEM_DOMAIN_CPU))
Chris Wilson7f5f95d2017-03-10 00:09:42 +0000836 *needs_clflush = CLFLUSH_BEFORE;
Brad Volkin4c914c02014-02-18 10:15:45 -0800837
Chris Wilson7f5f95d2017-03-10 00:09:42 +0000838out:
Chris Wilson97649512016-08-18 17:16:50 +0100839 /* return with the pages pinned */
Chris Wilson43394c72016-08-18 17:16:47 +0100840 return 0;
Chris Wilson97649512016-08-18 17:16:50 +0100841
842err_unpin:
843 i915_gem_object_unpin_pages(obj);
844 return ret;
Chris Wilson43394c72016-08-18 17:16:47 +0100845}
846
847int i915_gem_obj_prepare_shmem_write(struct drm_i915_gem_object *obj,
848 unsigned int *needs_clflush)
849{
850 int ret;
851
Chris Wilsone95433c2016-10-28 13:58:27 +0100852 lockdep_assert_held(&obj->base.dev->struct_mutex);
853
Chris Wilson43394c72016-08-18 17:16:47 +0100854 *needs_clflush = 0;
855 if (!i915_gem_object_has_struct_page(obj))
856 return -ENODEV;
857
Chris Wilsone95433c2016-10-28 13:58:27 +0100858 ret = i915_gem_object_wait(obj,
859 I915_WAIT_INTERRUPTIBLE |
860 I915_WAIT_LOCKED |
861 I915_WAIT_ALL,
Chris Wilson62eb3c22019-02-13 09:25:04 +0000862 MAX_SCHEDULE_TIMEOUT);
Chris Wilson43394c72016-08-18 17:16:47 +0100863 if (ret)
864 return ret;
865
Chris Wilsona4f5ea62016-10-28 13:58:35 +0100866 ret = i915_gem_object_pin_pages(obj);
Chris Wilson97649512016-08-18 17:16:50 +0100867 if (ret)
868 return ret;
869
Chris Wilsonb8f55be2017-08-11 12:11:16 +0100870 if (obj->cache_coherent & I915_BO_CACHE_COHERENT_FOR_WRITE ||
871 !static_cpu_has(X86_FEATURE_CLFLUSH)) {
Chris Wilson7f5f95d2017-03-10 00:09:42 +0000872 ret = i915_gem_object_set_to_cpu_domain(obj, true);
873 if (ret)
874 goto err_unpin;
875 else
876 goto out;
877 }
878
Chris Wilsonef749212017-04-12 12:01:10 +0100879 flush_write_domain(obj, ~I915_GEM_DOMAIN_CPU);
Chris Wilsona314d5c2016-08-18 17:16:48 +0100880
Chris Wilson43394c72016-08-18 17:16:47 +0100881 /* If we're not in the cpu write domain, set ourself into the
882 * gtt write domain and manually flush cachelines (as required).
883 * This optimizes for the case when the gpu will use the data
884 * right away and we therefore have to clflush anyway.
885 */
Chris Wilsone27ab732017-06-15 13:38:49 +0100886 if (!obj->cache_dirty) {
Chris Wilson7f5f95d2017-03-10 00:09:42 +0000887 *needs_clflush |= CLFLUSH_AFTER;
Chris Wilson43394c72016-08-18 17:16:47 +0100888
Chris Wilsone27ab732017-06-15 13:38:49 +0100889 /*
890 * Same trick applies to invalidate partially written
891 * cachelines read before writing.
892 */
Christian Königc0a51fd2018-02-16 13:43:38 +0100893 if (!(obj->read_domains & I915_GEM_DOMAIN_CPU))
Chris Wilsone27ab732017-06-15 13:38:49 +0100894 *needs_clflush |= CLFLUSH_BEFORE;
895 }
Chris Wilson43394c72016-08-18 17:16:47 +0100896
Chris Wilson7f5f95d2017-03-10 00:09:42 +0000897out:
Chris Wilson43394c72016-08-18 17:16:47 +0100898 intel_fb_obj_invalidate(obj, ORIGIN_CPU);
Chris Wilsona4f5ea62016-10-28 13:58:35 +0100899 obj->mm.dirty = true;
Chris Wilson97649512016-08-18 17:16:50 +0100900 /* return with the pages pinned */
Chris Wilson43394c72016-08-18 17:16:47 +0100901 return 0;
Chris Wilson97649512016-08-18 17:16:50 +0100902
903err_unpin:
904 i915_gem_object_unpin_pages(obj);
905 return ret;
Brad Volkin4c914c02014-02-18 10:15:45 -0800906}
907
Daniel Vetterd174bd62012-03-25 19:47:40 +0200908static int
Chris Wilsonb9d126e2019-01-05 12:07:58 +0000909shmem_pread(struct page *page, int offset, int len, char __user *user_data,
910 bool needs_clflush)
Daniel Vetterd174bd62012-03-25 19:47:40 +0200911{
912 char *vaddr;
913 int ret;
914
915 vaddr = kmap(page);
Daniel Vetterd174bd62012-03-25 19:47:40 +0200916
Chris Wilsonb9d126e2019-01-05 12:07:58 +0000917 if (needs_clflush)
918 drm_clflush_virt_range(vaddr + offset, len);
919
920 ret = __copy_to_user(user_data, vaddr + offset, len);
921
Daniel Vetterd174bd62012-03-25 19:47:40 +0200922 kunmap(page);
923
Chris Wilsonb9d126e2019-01-05 12:07:58 +0000924 return ret ? -EFAULT : 0;
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100925}
926
927static int
928i915_gem_shmem_pread(struct drm_i915_gem_object *obj,
929 struct drm_i915_gem_pread *args)
930{
931 char __user *user_data;
932 u64 remain;
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100933 unsigned int needs_clflush;
934 unsigned int idx, offset;
935 int ret;
936
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100937 ret = mutex_lock_interruptible(&obj->base.dev->struct_mutex);
938 if (ret)
939 return ret;
940
941 ret = i915_gem_obj_prepare_shmem_read(obj, &needs_clflush);
942 mutex_unlock(&obj->base.dev->struct_mutex);
943 if (ret)
944 return ret;
945
946 remain = args->size;
947 user_data = u64_to_user_ptr(args->data_ptr);
948 offset = offset_in_page(args->offset);
949 for (idx = args->offset >> PAGE_SHIFT; remain; idx++) {
950 struct page *page = i915_gem_object_get_page(obj, idx);
Chris Wilsona5e856a52018-10-12 15:02:28 +0100951 unsigned int length = min_t(u64, remain, PAGE_SIZE - offset);
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100952
953 ret = shmem_pread(page, offset, length, user_data,
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100954 needs_clflush);
955 if (ret)
956 break;
957
958 remain -= length;
959 user_data += length;
960 offset = 0;
961 }
962
963 i915_gem_obj_finish_shmem_access(obj);
964 return ret;
965}
966
967static inline bool
968gtt_user_read(struct io_mapping *mapping,
969 loff_t base, int offset,
970 char __user *user_data, int length)
971{
Ville Syrjäläafe722b2017-09-01 20:12:52 +0300972 void __iomem *vaddr;
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100973 unsigned long unwritten;
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +0530974
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +0530975 /* We can use the cpu mem copy function because this is X86. */
Ville Syrjäläafe722b2017-09-01 20:12:52 +0300976 vaddr = io_mapping_map_atomic_wc(mapping, base);
977 unwritten = __copy_to_user_inatomic(user_data,
978 (void __force *)vaddr + offset,
979 length);
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100980 io_mapping_unmap_atomic(vaddr);
981 if (unwritten) {
Ville Syrjäläafe722b2017-09-01 20:12:52 +0300982 vaddr = io_mapping_map_wc(mapping, base, PAGE_SIZE);
983 unwritten = copy_to_user(user_data,
984 (void __force *)vaddr + offset,
985 length);
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100986 io_mapping_unmap(vaddr);
987 }
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +0530988 return unwritten;
989}
990
991static int
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100992i915_gem_gtt_pread(struct drm_i915_gem_object *obj,
993 const struct drm_i915_gem_pread *args)
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +0530994{
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100995 struct drm_i915_private *i915 = to_i915(obj->base.dev);
996 struct i915_ggtt *ggtt = &i915->ggtt;
Chris Wilson538ef962019-01-14 14:21:18 +0000997 intel_wakeref_t wakeref;
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +0530998 struct drm_mm_node node;
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100999 struct i915_vma *vma;
1000 void __user *user_data;
1001 u64 remain, offset;
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301002 int ret;
1003
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001004 ret = mutex_lock_interruptible(&i915->drm.struct_mutex);
1005 if (ret)
1006 return ret;
1007
Chris Wilson538ef962019-01-14 14:21:18 +00001008 wakeref = intel_runtime_pm_get(i915);
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001009 vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0,
Chris Wilsona3259ca2017-10-09 09:44:00 +01001010 PIN_MAPPABLE |
1011 PIN_NONFAULT |
1012 PIN_NONBLOCK);
Chris Wilson18034582016-08-18 17:16:45 +01001013 if (!IS_ERR(vma)) {
1014 node.start = i915_ggtt_offset(vma);
1015 node.allocated = false;
Chris Wilson49ef5292016-08-18 17:17:00 +01001016 ret = i915_vma_put_fence(vma);
Chris Wilson18034582016-08-18 17:16:45 +01001017 if (ret) {
1018 i915_vma_unpin(vma);
1019 vma = ERR_PTR(ret);
1020 }
1021 }
Chris Wilson058d88c2016-08-15 10:49:06 +01001022 if (IS_ERR(vma)) {
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001023 ret = insert_mappable_node(ggtt, &node, PAGE_SIZE);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301024 if (ret)
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001025 goto out_unlock;
1026 GEM_BUG_ON(!node.allocated);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301027 }
1028
1029 ret = i915_gem_object_set_to_gtt_domain(obj, false);
1030 if (ret)
1031 goto out_unpin;
1032
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001033 mutex_unlock(&i915->drm.struct_mutex);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301034
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001035 user_data = u64_to_user_ptr(args->data_ptr);
1036 remain = args->size;
1037 offset = args->offset;
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301038
1039 while (remain > 0) {
1040 /* Operation in this page
1041 *
1042 * page_base = page offset within aperture
1043 * page_offset = offset within page
1044 * page_length = bytes to copy for this page
1045 */
1046 u32 page_base = node.start;
1047 unsigned page_offset = offset_in_page(offset);
1048 unsigned page_length = PAGE_SIZE - page_offset;
1049 page_length = remain < page_length ? remain : page_length;
1050 if (node.allocated) {
1051 wmb();
Chris Wilson82ad6442018-06-05 16:37:58 +01001052 ggtt->vm.insert_page(&ggtt->vm,
1053 i915_gem_object_get_dma_address(obj, offset >> PAGE_SHIFT),
1054 node.start, I915_CACHE_NONE, 0);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301055 wmb();
1056 } else {
1057 page_base += offset & PAGE_MASK;
1058 }
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001059
Matthew Auld73ebd502017-12-11 15:18:20 +00001060 if (gtt_user_read(&ggtt->iomap, page_base, page_offset,
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001061 user_data, page_length)) {
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301062 ret = -EFAULT;
1063 break;
1064 }
1065
1066 remain -= page_length;
1067 user_data += page_length;
1068 offset += page_length;
1069 }
1070
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001071 mutex_lock(&i915->drm.struct_mutex);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301072out_unpin:
1073 if (node.allocated) {
1074 wmb();
Chris Wilson82ad6442018-06-05 16:37:58 +01001075 ggtt->vm.clear_range(&ggtt->vm, node.start, node.size);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301076 remove_mappable_node(&node);
1077 } else {
Chris Wilson058d88c2016-08-15 10:49:06 +01001078 i915_vma_unpin(vma);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301079 }
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001080out_unlock:
Chris Wilson538ef962019-01-14 14:21:18 +00001081 intel_runtime_pm_put(i915, wakeref);
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001082 mutex_unlock(&i915->drm.struct_mutex);
Chris Wilsonf60d7f02012-09-04 21:02:56 +01001083
Eric Anholteb014592009-03-10 11:44:52 -07001084 return ret;
1085}
1086
Eric Anholt673a3942008-07-30 12:06:12 -07001087/**
1088 * Reads data from the object referenced by handle.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01001089 * @dev: drm device pointer
1090 * @data: ioctl data blob
1091 * @file: drm file pointer
Eric Anholt673a3942008-07-30 12:06:12 -07001092 *
1093 * On error, the contents of *data are undefined.
1094 */
1095int
1096i915_gem_pread_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00001097 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001098{
1099 struct drm_i915_gem_pread *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00001100 struct drm_i915_gem_object *obj;
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001101 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07001102
Chris Wilson51311d02010-11-17 09:10:42 +00001103 if (args->size == 0)
1104 return 0;
1105
Linus Torvalds96d4f262019-01-03 18:57:57 -08001106 if (!access_ok(u64_to_user_ptr(args->data_ptr),
Chris Wilson51311d02010-11-17 09:10:42 +00001107 args->size))
1108 return -EFAULT;
1109
Chris Wilson03ac0642016-07-20 13:31:51 +01001110 obj = i915_gem_object_lookup(file, args->handle);
Chris Wilson258a5ed2016-08-05 10:14:16 +01001111 if (!obj)
1112 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07001113
Chris Wilson7dcd2492010-09-26 20:21:44 +01001114 /* Bounds check source. */
Matthew Auld966d5bf2016-12-13 20:32:22 +00001115 if (range_overflows_t(u64, args->offset, args->size, obj->base.size)) {
Chris Wilsonce9d4192010-09-26 20:50:05 +01001116 ret = -EINVAL;
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001117 goto out;
Chris Wilsonce9d4192010-09-26 20:50:05 +01001118 }
1119
Chris Wilsondb53a302011-02-03 11:57:46 +00001120 trace_i915_gem_object_pread(obj, args->offset, args->size);
1121
Chris Wilsone95433c2016-10-28 13:58:27 +01001122 ret = i915_gem_object_wait(obj,
1123 I915_WAIT_INTERRUPTIBLE,
Chris Wilson62eb3c22019-02-13 09:25:04 +00001124 MAX_SCHEDULE_TIMEOUT);
Chris Wilson258a5ed2016-08-05 10:14:16 +01001125 if (ret)
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001126 goto out;
Chris Wilson258a5ed2016-08-05 10:14:16 +01001127
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001128 ret = i915_gem_object_pin_pages(obj);
Chris Wilson258a5ed2016-08-05 10:14:16 +01001129 if (ret)
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001130 goto out;
Chris Wilson258a5ed2016-08-05 10:14:16 +01001131
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001132 ret = i915_gem_shmem_pread(obj, args);
Chris Wilson9c870d02016-10-24 13:42:15 +01001133 if (ret == -EFAULT || ret == -ENODEV)
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001134 ret = i915_gem_gtt_pread(obj, args);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301135
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001136 i915_gem_object_unpin_pages(obj);
1137out:
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001138 i915_gem_object_put(obj);
Eric Anholteb014592009-03-10 11:44:52 -07001139 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07001140}
1141
Keith Packard0839ccb2008-10-30 19:38:48 -07001142/* This is the fast write path which cannot handle
1143 * page faults in the source data
Linus Torvalds9b7530cc2008-10-20 14:16:43 -07001144 */
Linus Torvalds9b7530cc2008-10-20 14:16:43 -07001145
Chris Wilsonfe115622016-10-28 13:58:40 +01001146static inline bool
1147ggtt_write(struct io_mapping *mapping,
1148 loff_t base, int offset,
1149 char __user *user_data, int length)
Keith Packard0839ccb2008-10-30 19:38:48 -07001150{
Ville Syrjäläafe722b2017-09-01 20:12:52 +03001151 void __iomem *vaddr;
Keith Packard0839ccb2008-10-30 19:38:48 -07001152 unsigned long unwritten;
1153
Ben Widawsky4f0c7cf2012-04-16 14:07:47 -07001154 /* We can use the cpu mem copy function because this is X86. */
Ville Syrjäläafe722b2017-09-01 20:12:52 +03001155 vaddr = io_mapping_map_atomic_wc(mapping, base);
1156 unwritten = __copy_from_user_inatomic_nocache((void __force *)vaddr + offset,
Keith Packard0839ccb2008-10-30 19:38:48 -07001157 user_data, length);
Chris Wilsonfe115622016-10-28 13:58:40 +01001158 io_mapping_unmap_atomic(vaddr);
1159 if (unwritten) {
Ville Syrjäläafe722b2017-09-01 20:12:52 +03001160 vaddr = io_mapping_map_wc(mapping, base, PAGE_SIZE);
1161 unwritten = copy_from_user((void __force *)vaddr + offset,
1162 user_data, length);
Chris Wilsonfe115622016-10-28 13:58:40 +01001163 io_mapping_unmap(vaddr);
1164 }
Keith Packard0839ccb2008-10-30 19:38:48 -07001165
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001166 return unwritten;
1167}
1168
Eric Anholt3de09aa2009-03-09 09:42:23 -07001169/**
1170 * This is the fast pwrite path, where we copy the data directly from the
1171 * user into the GTT, uncached.
Chris Wilsonfe115622016-10-28 13:58:40 +01001172 * @obj: i915 GEM object
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01001173 * @args: pwrite arguments structure
Eric Anholt3de09aa2009-03-09 09:42:23 -07001174 */
Eric Anholt673a3942008-07-30 12:06:12 -07001175static int
Chris Wilsonfe115622016-10-28 13:58:40 +01001176i915_gem_gtt_pwrite_fast(struct drm_i915_gem_object *obj,
1177 const struct drm_i915_gem_pwrite *args)
Eric Anholt673a3942008-07-30 12:06:12 -07001178{
Chris Wilsonfe115622016-10-28 13:58:40 +01001179 struct drm_i915_private *i915 = to_i915(obj->base.dev);
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301180 struct i915_ggtt *ggtt = &i915->ggtt;
Chris Wilson538ef962019-01-14 14:21:18 +00001181 intel_wakeref_t wakeref;
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301182 struct drm_mm_node node;
Chris Wilsonfe115622016-10-28 13:58:40 +01001183 struct i915_vma *vma;
1184 u64 remain, offset;
1185 void __user *user_data;
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301186 int ret;
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301187
Chris Wilsonfe115622016-10-28 13:58:40 +01001188 ret = mutex_lock_interruptible(&i915->drm.struct_mutex);
1189 if (ret)
1190 return ret;
Daniel Vetter935aaa62012-03-25 19:47:35 +02001191
Chris Wilson8bd818152017-10-19 07:37:33 +01001192 if (i915_gem_object_has_struct_page(obj)) {
1193 /*
1194 * Avoid waking the device up if we can fallback, as
1195 * waking/resuming is very slow (worst-case 10-100 ms
1196 * depending on PCI sleeps and our own resume time).
1197 * This easily dwarfs any performance advantage from
1198 * using the cache bypass of indirect GGTT access.
1199 */
Chris Wilson538ef962019-01-14 14:21:18 +00001200 wakeref = intel_runtime_pm_get_if_in_use(i915);
1201 if (!wakeref) {
Chris Wilson8bd818152017-10-19 07:37:33 +01001202 ret = -EFAULT;
1203 goto out_unlock;
1204 }
1205 } else {
1206 /* No backing pages, no fallback, we must force GGTT access */
Chris Wilson538ef962019-01-14 14:21:18 +00001207 wakeref = intel_runtime_pm_get(i915);
Chris Wilson8bd818152017-10-19 07:37:33 +01001208 }
1209
Chris Wilson058d88c2016-08-15 10:49:06 +01001210 vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0,
Chris Wilsona3259ca2017-10-09 09:44:00 +01001211 PIN_MAPPABLE |
1212 PIN_NONFAULT |
1213 PIN_NONBLOCK);
Chris Wilson18034582016-08-18 17:16:45 +01001214 if (!IS_ERR(vma)) {
1215 node.start = i915_ggtt_offset(vma);
1216 node.allocated = false;
Chris Wilson49ef5292016-08-18 17:17:00 +01001217 ret = i915_vma_put_fence(vma);
Chris Wilson18034582016-08-18 17:16:45 +01001218 if (ret) {
1219 i915_vma_unpin(vma);
1220 vma = ERR_PTR(ret);
1221 }
1222 }
Chris Wilson058d88c2016-08-15 10:49:06 +01001223 if (IS_ERR(vma)) {
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001224 ret = insert_mappable_node(ggtt, &node, PAGE_SIZE);
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301225 if (ret)
Chris Wilson8bd818152017-10-19 07:37:33 +01001226 goto out_rpm;
Chris Wilsonfe115622016-10-28 13:58:40 +01001227 GEM_BUG_ON(!node.allocated);
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301228 }
Daniel Vetter935aaa62012-03-25 19:47:35 +02001229
1230 ret = i915_gem_object_set_to_gtt_domain(obj, true);
1231 if (ret)
1232 goto out_unpin;
1233
Chris Wilsonfe115622016-10-28 13:58:40 +01001234 mutex_unlock(&i915->drm.struct_mutex);
1235
Chris Wilsonb19482d2016-08-18 17:16:43 +01001236 intel_fb_obj_invalidate(obj, ORIGIN_CPU);
Paulo Zanoni063e4e62015-02-13 17:23:45 -02001237
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301238 user_data = u64_to_user_ptr(args->data_ptr);
1239 offset = args->offset;
1240 remain = args->size;
1241 while (remain) {
Eric Anholt673a3942008-07-30 12:06:12 -07001242 /* Operation in this page
1243 *
Keith Packard0839ccb2008-10-30 19:38:48 -07001244 * page_base = page offset within aperture
1245 * page_offset = offset within page
1246 * page_length = bytes to copy for this page
Eric Anholt673a3942008-07-30 12:06:12 -07001247 */
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301248 u32 page_base = node.start;
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001249 unsigned int page_offset = offset_in_page(offset);
1250 unsigned int page_length = PAGE_SIZE - page_offset;
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301251 page_length = remain < page_length ? remain : page_length;
1252 if (node.allocated) {
1253 wmb(); /* flush the write before we modify the GGTT */
Chris Wilson82ad6442018-06-05 16:37:58 +01001254 ggtt->vm.insert_page(&ggtt->vm,
1255 i915_gem_object_get_dma_address(obj, offset >> PAGE_SHIFT),
1256 node.start, I915_CACHE_NONE, 0);
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301257 wmb(); /* flush modifications to the GGTT (insert_page) */
1258 } else {
1259 page_base += offset & PAGE_MASK;
1260 }
Keith Packard0839ccb2008-10-30 19:38:48 -07001261 /* If we get a fault while copying data, then (presumably) our
Eric Anholt3de09aa2009-03-09 09:42:23 -07001262 * source page isn't available. Return the error and we'll
1263 * retry in the slow path.
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301264 * If the object is non-shmem backed, we retry again with the
1265 * path that handles page fault.
Keith Packard0839ccb2008-10-30 19:38:48 -07001266 */
Matthew Auld73ebd502017-12-11 15:18:20 +00001267 if (ggtt_write(&ggtt->iomap, page_base, page_offset,
Chris Wilsonfe115622016-10-28 13:58:40 +01001268 user_data, page_length)) {
1269 ret = -EFAULT;
1270 break;
Daniel Vetter935aaa62012-03-25 19:47:35 +02001271 }
Eric Anholt673a3942008-07-30 12:06:12 -07001272
Keith Packard0839ccb2008-10-30 19:38:48 -07001273 remain -= page_length;
1274 user_data += page_length;
1275 offset += page_length;
Eric Anholt673a3942008-07-30 12:06:12 -07001276 }
Chris Wilsond59b21e2017-02-22 11:40:49 +00001277 intel_fb_obj_flush(obj, ORIGIN_CPU);
Chris Wilsonfe115622016-10-28 13:58:40 +01001278
1279 mutex_lock(&i915->drm.struct_mutex);
Daniel Vetter935aaa62012-03-25 19:47:35 +02001280out_unpin:
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301281 if (node.allocated) {
1282 wmb();
Chris Wilson82ad6442018-06-05 16:37:58 +01001283 ggtt->vm.clear_range(&ggtt->vm, node.start, node.size);
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301284 remove_mappable_node(&node);
1285 } else {
Chris Wilson058d88c2016-08-15 10:49:06 +01001286 i915_vma_unpin(vma);
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301287 }
Chris Wilson8bd818152017-10-19 07:37:33 +01001288out_rpm:
Chris Wilson538ef962019-01-14 14:21:18 +00001289 intel_runtime_pm_put(i915, wakeref);
Chris Wilson8bd818152017-10-19 07:37:33 +01001290out_unlock:
Chris Wilsonfe115622016-10-28 13:58:40 +01001291 mutex_unlock(&i915->drm.struct_mutex);
Eric Anholt3de09aa2009-03-09 09:42:23 -07001292 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07001293}
1294
Chris Wilsonfe115622016-10-28 13:58:40 +01001295/* Per-page copy function for the shmem pwrite fastpath.
1296 * Flushes invalid cachelines before writing to the target if
1297 * needs_clflush_before is set and flushes out any written cachelines after
1298 * writing if needs_clflush is set.
1299 */
Eric Anholt40123c12009-03-09 13:42:30 -07001300static int
Chris Wilsonfe115622016-10-28 13:58:40 +01001301shmem_pwrite(struct page *page, int offset, int len, char __user *user_data,
Chris Wilsonfe115622016-10-28 13:58:40 +01001302 bool needs_clflush_before,
1303 bool needs_clflush_after)
Eric Anholt40123c12009-03-09 13:42:30 -07001304{
Chris Wilsonb9d126e2019-01-05 12:07:58 +00001305 char *vaddr;
Chris Wilsonfe115622016-10-28 13:58:40 +01001306 int ret;
Eric Anholt40123c12009-03-09 13:42:30 -07001307
Chris Wilsonb9d126e2019-01-05 12:07:58 +00001308 vaddr = kmap(page);
Chris Wilsonfe115622016-10-28 13:58:40 +01001309
Chris Wilsonb9d126e2019-01-05 12:07:58 +00001310 if (needs_clflush_before)
1311 drm_clflush_virt_range(vaddr + offset, len);
Chris Wilsonfe115622016-10-28 13:58:40 +01001312
Chris Wilsonb9d126e2019-01-05 12:07:58 +00001313 ret = __copy_from_user(vaddr + offset, user_data, len);
1314 if (!ret && needs_clflush_after)
1315 drm_clflush_virt_range(vaddr + offset, len);
Chris Wilsonfe115622016-10-28 13:58:40 +01001316
Chris Wilsonb9d126e2019-01-05 12:07:58 +00001317 kunmap(page);
1318
1319 return ret ? -EFAULT : 0;
Chris Wilsonfe115622016-10-28 13:58:40 +01001320}
1321
1322static int
1323i915_gem_shmem_pwrite(struct drm_i915_gem_object *obj,
1324 const struct drm_i915_gem_pwrite *args)
1325{
1326 struct drm_i915_private *i915 = to_i915(obj->base.dev);
1327 void __user *user_data;
1328 u64 remain;
Chris Wilsonfe115622016-10-28 13:58:40 +01001329 unsigned int partial_cacheline_write;
1330 unsigned int needs_clflush;
1331 unsigned int offset, idx;
1332 int ret;
1333
1334 ret = mutex_lock_interruptible(&i915->drm.struct_mutex);
Chris Wilson43394c72016-08-18 17:16:47 +01001335 if (ret)
1336 return ret;
Eric Anholt40123c12009-03-09 13:42:30 -07001337
Chris Wilsonfe115622016-10-28 13:58:40 +01001338 ret = i915_gem_obj_prepare_shmem_write(obj, &needs_clflush);
1339 mutex_unlock(&i915->drm.struct_mutex);
1340 if (ret)
1341 return ret;
1342
Chris Wilsonfe115622016-10-28 13:58:40 +01001343 /* If we don't overwrite a cacheline completely we need to be
1344 * careful to have up-to-date data by first clflushing. Don't
1345 * overcomplicate things and flush the entire patch.
1346 */
1347 partial_cacheline_write = 0;
1348 if (needs_clflush & CLFLUSH_BEFORE)
1349 partial_cacheline_write = boot_cpu_data.x86_clflush_size - 1;
1350
Chris Wilson43394c72016-08-18 17:16:47 +01001351 user_data = u64_to_user_ptr(args->data_ptr);
Chris Wilson43394c72016-08-18 17:16:47 +01001352 remain = args->size;
Chris Wilsonfe115622016-10-28 13:58:40 +01001353 offset = offset_in_page(args->offset);
1354 for (idx = args->offset >> PAGE_SHIFT; remain; idx++) {
1355 struct page *page = i915_gem_object_get_page(obj, idx);
Chris Wilsona5e856a52018-10-12 15:02:28 +01001356 unsigned int length = min_t(u64, remain, PAGE_SIZE - offset);
Chris Wilsone5281cc2010-10-28 13:45:36 +01001357
Chris Wilsonfe115622016-10-28 13:58:40 +01001358 ret = shmem_pwrite(page, offset, length, user_data,
Chris Wilsonfe115622016-10-28 13:58:40 +01001359 (offset | length) & partial_cacheline_write,
1360 needs_clflush & CLFLUSH_AFTER);
1361 if (ret)
Chris Wilson9da3da62012-06-01 15:20:22 +01001362 break;
1363
Chris Wilsonfe115622016-10-28 13:58:40 +01001364 remain -= length;
1365 user_data += length;
1366 offset = 0;
Eric Anholt40123c12009-03-09 13:42:30 -07001367 }
1368
Chris Wilsond59b21e2017-02-22 11:40:49 +00001369 intel_fb_obj_flush(obj, ORIGIN_CPU);
Chris Wilsonfe115622016-10-28 13:58:40 +01001370 i915_gem_obj_finish_shmem_access(obj);
Eric Anholt40123c12009-03-09 13:42:30 -07001371 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07001372}
1373
1374/**
1375 * Writes data to the object referenced by handle.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01001376 * @dev: drm device
1377 * @data: ioctl data blob
1378 * @file: drm file
Eric Anholt673a3942008-07-30 12:06:12 -07001379 *
1380 * On error, the contents of the buffer that were to be modified are undefined.
1381 */
1382int
1383i915_gem_pwrite_ioctl(struct drm_device *dev, void *data,
Chris Wilsonfbd5a262010-10-14 15:03:58 +01001384 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001385{
1386 struct drm_i915_gem_pwrite *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00001387 struct drm_i915_gem_object *obj;
Chris Wilson51311d02010-11-17 09:10:42 +00001388 int ret;
1389
1390 if (args->size == 0)
1391 return 0;
1392
Linus Torvalds96d4f262019-01-03 18:57:57 -08001393 if (!access_ok(u64_to_user_ptr(args->data_ptr), args->size))
Chris Wilson51311d02010-11-17 09:10:42 +00001394 return -EFAULT;
1395
Chris Wilson03ac0642016-07-20 13:31:51 +01001396 obj = i915_gem_object_lookup(file, args->handle);
Chris Wilson258a5ed2016-08-05 10:14:16 +01001397 if (!obj)
1398 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07001399
Chris Wilson7dcd2492010-09-26 20:21:44 +01001400 /* Bounds check destination. */
Matthew Auld966d5bf2016-12-13 20:32:22 +00001401 if (range_overflows_t(u64, args->offset, args->size, obj->base.size)) {
Chris Wilsonce9d4192010-09-26 20:50:05 +01001402 ret = -EINVAL;
Chris Wilson258a5ed2016-08-05 10:14:16 +01001403 goto err;
Chris Wilsonce9d4192010-09-26 20:50:05 +01001404 }
1405
Chris Wilsonf8c1cce2018-07-12 19:53:14 +01001406 /* Writes not allowed into this read-only object */
1407 if (i915_gem_object_is_readonly(obj)) {
1408 ret = -EINVAL;
1409 goto err;
1410 }
1411
Chris Wilsondb53a302011-02-03 11:57:46 +00001412 trace_i915_gem_object_pwrite(obj, args->offset, args->size);
1413
Chris Wilson7c55e2c2017-03-07 12:03:38 +00001414 ret = -ENODEV;
1415 if (obj->ops->pwrite)
1416 ret = obj->ops->pwrite(obj, args);
1417 if (ret != -ENODEV)
1418 goto err;
1419
Chris Wilsone95433c2016-10-28 13:58:27 +01001420 ret = i915_gem_object_wait(obj,
1421 I915_WAIT_INTERRUPTIBLE |
1422 I915_WAIT_ALL,
Chris Wilson62eb3c22019-02-13 09:25:04 +00001423 MAX_SCHEDULE_TIMEOUT);
Chris Wilson258a5ed2016-08-05 10:14:16 +01001424 if (ret)
1425 goto err;
1426
Chris Wilsonfe115622016-10-28 13:58:40 +01001427 ret = i915_gem_object_pin_pages(obj);
Chris Wilson258a5ed2016-08-05 10:14:16 +01001428 if (ret)
Chris Wilsonfe115622016-10-28 13:58:40 +01001429 goto err;
Chris Wilson258a5ed2016-08-05 10:14:16 +01001430
Daniel Vetter935aaa62012-03-25 19:47:35 +02001431 ret = -EFAULT;
Eric Anholt673a3942008-07-30 12:06:12 -07001432 /* We can only do the GTT pwrite on untiled buffers, as otherwise
1433 * it would end up going through the fenced access, and we'll get
1434 * different detiling behavior between reading and writing.
1435 * pread/pwrite currently are reading and writing from the CPU
1436 * perspective, requiring manual detiling by the client.
1437 */
Chris Wilson6eae0052016-06-20 15:05:52 +01001438 if (!i915_gem_object_has_struct_page(obj) ||
Chris Wilson9c870d02016-10-24 13:42:15 +01001439 cpu_write_needs_clflush(obj))
Daniel Vetter935aaa62012-03-25 19:47:35 +02001440 /* Note that the gtt paths might fail with non-page-backed user
1441 * pointers (e.g. gtt mappings when moving data between
Chris Wilson9c870d02016-10-24 13:42:15 +01001442 * textures). Fallback to the shmem path in that case.
1443 */
Chris Wilsonfe115622016-10-28 13:58:40 +01001444 ret = i915_gem_gtt_pwrite_fast(obj, args);
Eric Anholt673a3942008-07-30 12:06:12 -07001445
Chris Wilsond1054ee2016-07-16 18:42:36 +01001446 if (ret == -EFAULT || ret == -ENOSPC) {
Chris Wilson6a2c4232014-11-04 04:51:40 -08001447 if (obj->phys_handle)
1448 ret = i915_gem_phys_pwrite(obj, args, file);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301449 else
Chris Wilsonfe115622016-10-28 13:58:40 +01001450 ret = i915_gem_shmem_pwrite(obj, args);
Chris Wilson6a2c4232014-11-04 04:51:40 -08001451 }
Daniel Vetter5c0480f2011-12-14 13:57:30 +01001452
Chris Wilsonfe115622016-10-28 13:58:40 +01001453 i915_gem_object_unpin_pages(obj);
Chris Wilson258a5ed2016-08-05 10:14:16 +01001454err:
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001455 i915_gem_object_put(obj);
Chris Wilson258a5ed2016-08-05 10:14:16 +01001456 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07001457}
1458
Chris Wilson40e62d52016-10-28 13:58:41 +01001459static void i915_gem_object_bump_inactive_ggtt(struct drm_i915_gem_object *obj)
1460{
Chris Wilson09d7e462019-01-28 10:23:53 +00001461 struct drm_i915_private *i915 = to_i915(obj->base.dev);
Chris Wilson40e62d52016-10-28 13:58:41 +01001462 struct list_head *list;
1463 struct i915_vma *vma;
1464
Chris Wilsonf2123812017-10-16 12:40:37 +01001465 GEM_BUG_ON(!i915_gem_object_has_pinned_pages(obj));
1466
Chris Wilson09d7e462019-01-28 10:23:53 +00001467 mutex_lock(&i915->ggtt.vm.mutex);
Chris Wilsone2189dd2017-12-07 21:14:07 +00001468 for_each_ggtt_vma(vma, obj) {
Chris Wilson40e62d52016-10-28 13:58:41 +01001469 if (!drm_mm_node_allocated(&vma->node))
1470 continue;
1471
Chris Wilson499197d2019-01-28 10:23:52 +00001472 list_move_tail(&vma->vm_link, &vma->vm->bound_list);
Chris Wilson40e62d52016-10-28 13:58:41 +01001473 }
Chris Wilson09d7e462019-01-28 10:23:53 +00001474 mutex_unlock(&i915->ggtt.vm.mutex);
Chris Wilson40e62d52016-10-28 13:58:41 +01001475
Chris Wilsonf2123812017-10-16 12:40:37 +01001476 spin_lock(&i915->mm.obj_lock);
Chris Wilson40e62d52016-10-28 13:58:41 +01001477 list = obj->bind_count ? &i915->mm.bound_list : &i915->mm.unbound_list;
Chris Wilsonf2123812017-10-16 12:40:37 +01001478 list_move_tail(&obj->mm.link, list);
1479 spin_unlock(&i915->mm.obj_lock);
Chris Wilson40e62d52016-10-28 13:58:41 +01001480}
1481
Eric Anholt673a3942008-07-30 12:06:12 -07001482/**
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001483 * Called when user space prepares to use an object with the CPU, either
1484 * through the mmap ioctl's mapping or a GTT mapping.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01001485 * @dev: drm device
1486 * @data: ioctl data blob
1487 * @file: drm file
Eric Anholt673a3942008-07-30 12:06:12 -07001488 */
1489int
1490i915_gem_set_domain_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00001491 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001492{
1493 struct drm_i915_gem_set_domain *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00001494 struct drm_i915_gem_object *obj;
Jani Nikula739f3ab2019-01-16 11:15:19 +02001495 u32 read_domains = args->read_domains;
1496 u32 write_domain = args->write_domain;
Chris Wilson40e62d52016-10-28 13:58:41 +01001497 int err;
Eric Anholt673a3942008-07-30 12:06:12 -07001498
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001499 /* Only handle setting domains to types used by the CPU. */
Chris Wilsonb8f90962016-08-05 10:14:07 +01001500 if ((write_domain | read_domains) & I915_GEM_GPU_DOMAINS)
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001501 return -EINVAL;
1502
1503 /* Having something in the write domain implies it's in the read
1504 * domain, and only that read domain. Enforce that in the request.
1505 */
1506 if (write_domain != 0 && read_domains != write_domain)
1507 return -EINVAL;
1508
Chris Wilson03ac0642016-07-20 13:31:51 +01001509 obj = i915_gem_object_lookup(file, args->handle);
Chris Wilsonb8f90962016-08-05 10:14:07 +01001510 if (!obj)
1511 return -ENOENT;
Jesse Barnes652c3932009-08-17 13:31:43 -07001512
Chris Wilson3236f572012-08-24 09:35:09 +01001513 /* Try to flush the object off the GPU without holding the lock.
1514 * We will repeat the flush holding the lock in the normal manner
1515 * to catch cases where we are gazumped.
1516 */
Chris Wilson40e62d52016-10-28 13:58:41 +01001517 err = i915_gem_object_wait(obj,
Chris Wilsone95433c2016-10-28 13:58:27 +01001518 I915_WAIT_INTERRUPTIBLE |
Chris Wilsone9eaf822018-10-01 15:47:55 +01001519 I915_WAIT_PRIORITY |
Chris Wilsone95433c2016-10-28 13:58:27 +01001520 (write_domain ? I915_WAIT_ALL : 0),
Chris Wilson62eb3c22019-02-13 09:25:04 +00001521 MAX_SCHEDULE_TIMEOUT);
Chris Wilson40e62d52016-10-28 13:58:41 +01001522 if (err)
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001523 goto out;
Chris Wilsonb8f90962016-08-05 10:14:07 +01001524
Tina Zhanga03f3952017-11-14 10:25:13 +00001525 /*
1526 * Proxy objects do not control access to the backing storage, ergo
1527 * they cannot be used as a means to manipulate the cache domain
1528 * tracking for that backing storage. The proxy object is always
1529 * considered to be outside of any cache domain.
1530 */
1531 if (i915_gem_object_is_proxy(obj)) {
1532 err = -ENXIO;
1533 goto out;
1534 }
1535
1536 /*
1537 * Flush and acquire obj->pages so that we are coherent through
Chris Wilson40e62d52016-10-28 13:58:41 +01001538 * direct access in memory with previous cached writes through
1539 * shmemfs and that our cache domain tracking remains valid.
1540 * For example, if the obj->filp was moved to swap without us
1541 * being notified and releasing the pages, we would mistakenly
1542 * continue to assume that the obj remained out of the CPU cached
1543 * domain.
1544 */
1545 err = i915_gem_object_pin_pages(obj);
1546 if (err)
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001547 goto out;
Chris Wilson40e62d52016-10-28 13:58:41 +01001548
1549 err = i915_mutex_lock_interruptible(dev);
1550 if (err)
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001551 goto out_unpin;
Chris Wilson3236f572012-08-24 09:35:09 +01001552
Chris Wilsone22d8e32017-04-12 12:01:11 +01001553 if (read_domains & I915_GEM_DOMAIN_WC)
1554 err = i915_gem_object_set_to_wc_domain(obj, write_domain);
1555 else if (read_domains & I915_GEM_DOMAIN_GTT)
1556 err = i915_gem_object_set_to_gtt_domain(obj, write_domain);
Chris Wilson43566de2015-01-02 16:29:29 +05301557 else
Chris Wilsone22d8e32017-04-12 12:01:11 +01001558 err = i915_gem_object_set_to_cpu_domain(obj, write_domain);
Chris Wilson40e62d52016-10-28 13:58:41 +01001559
1560 /* And bump the LRU for this access */
1561 i915_gem_object_bump_inactive_ggtt(obj);
1562
1563 mutex_unlock(&dev->struct_mutex);
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001564
Daniel Vetter031b6982015-06-26 19:35:16 +02001565 if (write_domain != 0)
Chris Wilsonef749212017-04-12 12:01:10 +01001566 intel_fb_obj_invalidate(obj,
1567 fb_write_origin(obj, write_domain));
Daniel Vetter031b6982015-06-26 19:35:16 +02001568
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001569out_unpin:
Chris Wilson40e62d52016-10-28 13:58:41 +01001570 i915_gem_object_unpin_pages(obj);
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001571out:
1572 i915_gem_object_put(obj);
Chris Wilson40e62d52016-10-28 13:58:41 +01001573 return err;
Eric Anholt673a3942008-07-30 12:06:12 -07001574}
1575
1576/**
1577 * Called when user space has done writes to this buffer
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01001578 * @dev: drm device
1579 * @data: ioctl data blob
1580 * @file: drm file
Eric Anholt673a3942008-07-30 12:06:12 -07001581 */
1582int
1583i915_gem_sw_finish_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00001584 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001585{
1586 struct drm_i915_gem_sw_finish *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00001587 struct drm_i915_gem_object *obj;
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001588
Chris Wilson03ac0642016-07-20 13:31:51 +01001589 obj = i915_gem_object_lookup(file, args->handle);
Chris Wilsonc21724c2016-08-05 10:14:19 +01001590 if (!obj)
1591 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07001592
Tina Zhanga03f3952017-11-14 10:25:13 +00001593 /*
1594 * Proxy objects are barred from CPU access, so there is no
1595 * need to ban sw_finish as it is a nop.
1596 */
1597
Eric Anholt673a3942008-07-30 12:06:12 -07001598 /* Pinned buffers may be scanout, so flush the cache */
Chris Wilson5a97bcc2017-02-22 11:40:46 +00001599 i915_gem_object_flush_if_display(obj);
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001600 i915_gem_object_put(obj);
Chris Wilson5a97bcc2017-02-22 11:40:46 +00001601
1602 return 0;
Eric Anholt673a3942008-07-30 12:06:12 -07001603}
1604
Joonas Lahtinen5c4604e2019-02-07 10:54:53 +02001605static inline bool
1606__vma_matches(struct vm_area_struct *vma, struct file *filp,
1607 unsigned long addr, unsigned long size)
1608{
1609 if (vma->vm_file != filp)
1610 return false;
1611
1612 return vma->vm_start == addr && (vma->vm_end - vma->vm_start) == size;
1613}
1614
Eric Anholt673a3942008-07-30 12:06:12 -07001615/**
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01001616 * i915_gem_mmap_ioctl - Maps the contents of an object, returning the address
1617 * it is mapped to.
1618 * @dev: drm device
1619 * @data: ioctl data blob
1620 * @file: drm file
Eric Anholt673a3942008-07-30 12:06:12 -07001621 *
1622 * While the mapping holds a reference on the contents of the object, it doesn't
1623 * imply a ref on the object itself.
Daniel Vetter34367382014-10-16 12:28:18 +02001624 *
1625 * IMPORTANT:
1626 *
1627 * DRM driver writers who look a this function as an example for how to do GEM
1628 * mmap support, please don't implement mmap support like here. The modern way
1629 * to implement DRM mmap support is with an mmap offset ioctl (like
1630 * i915_gem_mmap_gtt) and then using the mmap syscall on the DRM fd directly.
1631 * That way debug tooling like valgrind will understand what's going on, hiding
1632 * the mmap call in a driver private ioctl will break that. The i915 driver only
1633 * does cpu mmaps this way because we didn't know better.
Eric Anholt673a3942008-07-30 12:06:12 -07001634 */
1635int
1636i915_gem_mmap_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00001637 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001638{
1639 struct drm_i915_gem_mmap *args = data;
Chris Wilson03ac0642016-07-20 13:31:51 +01001640 struct drm_i915_gem_object *obj;
Eric Anholt673a3942008-07-30 12:06:12 -07001641 unsigned long addr;
1642
Akash Goel1816f922015-01-02 16:29:30 +05301643 if (args->flags & ~(I915_MMAP_WC))
1644 return -EINVAL;
1645
Borislav Petkov568a58e2016-03-29 17:42:01 +02001646 if (args->flags & I915_MMAP_WC && !boot_cpu_has(X86_FEATURE_PAT))
Akash Goel1816f922015-01-02 16:29:30 +05301647 return -ENODEV;
1648
Chris Wilson03ac0642016-07-20 13:31:51 +01001649 obj = i915_gem_object_lookup(file, args->handle);
1650 if (!obj)
Chris Wilsonbf79cb92010-08-04 14:19:46 +01001651 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07001652
Daniel Vetter1286ff72012-05-10 15:25:09 +02001653 /* prime objects have no backing filp to GEM mmap
1654 * pages from.
1655 */
Chris Wilson03ac0642016-07-20 13:31:51 +01001656 if (!obj->base.filp) {
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001657 i915_gem_object_put(obj);
Tina Zhang274b2462017-11-14 10:25:12 +00001658 return -ENXIO;
Daniel Vetter1286ff72012-05-10 15:25:09 +02001659 }
1660
Chris Wilson03ac0642016-07-20 13:31:51 +01001661 addr = vm_mmap(obj->base.filp, 0, args->size,
Eric Anholt673a3942008-07-30 12:06:12 -07001662 PROT_READ | PROT_WRITE, MAP_SHARED,
1663 args->offset);
Joonas Lahtinenebfb6972019-02-07 10:54:54 +02001664 if (IS_ERR_VALUE(addr))
1665 goto err;
1666
Akash Goel1816f922015-01-02 16:29:30 +05301667 if (args->flags & I915_MMAP_WC) {
1668 struct mm_struct *mm = current->mm;
1669 struct vm_area_struct *vma;
1670
Michal Hocko80a89a52016-05-23 16:26:11 -07001671 if (down_write_killable(&mm->mmap_sem)) {
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001672 i915_gem_object_put(obj);
Michal Hocko80a89a52016-05-23 16:26:11 -07001673 return -EINTR;
1674 }
Akash Goel1816f922015-01-02 16:29:30 +05301675 vma = find_vma(mm, addr);
Joonas Lahtinen5c4604e2019-02-07 10:54:53 +02001676 if (vma && __vma_matches(vma, obj->base.filp, addr, args->size))
Akash Goel1816f922015-01-02 16:29:30 +05301677 vma->vm_page_prot =
1678 pgprot_writecombine(vm_get_page_prot(vma->vm_flags));
1679 else
1680 addr = -ENOMEM;
1681 up_write(&mm->mmap_sem);
Joonas Lahtinenebfb6972019-02-07 10:54:54 +02001682 if (IS_ERR_VALUE(addr))
1683 goto err;
Chris Wilsonaeecc962016-06-17 14:46:39 -03001684
1685 /* This may race, but that's ok, it only gets set */
Chris Wilson50349242016-08-18 17:17:04 +01001686 WRITE_ONCE(obj->frontbuffer_ggtt_origin, ORIGIN_CPU);
Akash Goel1816f922015-01-02 16:29:30 +05301687 }
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001688 i915_gem_object_put(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001689
Jani Nikula739f3ab2019-01-16 11:15:19 +02001690 args->addr_ptr = (u64)addr;
Eric Anholt673a3942008-07-30 12:06:12 -07001691
1692 return 0;
Joonas Lahtinenebfb6972019-02-07 10:54:54 +02001693
1694err:
1695 i915_gem_object_put(obj);
1696
1697 return addr;
Eric Anholt673a3942008-07-30 12:06:12 -07001698}
1699
Chris Wilsond899ace2018-07-25 16:54:47 +01001700static unsigned int tile_row_pages(const struct drm_i915_gem_object *obj)
Chris Wilson03af84f2016-08-18 17:17:01 +01001701{
Chris Wilson6649a0b2017-01-09 16:16:08 +00001702 return i915_gem_object_get_tile_row_size(obj) >> PAGE_SHIFT;
Chris Wilson03af84f2016-08-18 17:17:01 +01001703}
1704
Jesse Barnesde151cf2008-11-12 10:03:55 -08001705/**
Chris Wilson4cc69072016-08-25 19:05:19 +01001706 * i915_gem_mmap_gtt_version - report the current feature set for GTT mmaps
1707 *
1708 * A history of the GTT mmap interface:
1709 *
1710 * 0 - Everything had to fit into the GTT. Both parties of a memcpy had to
1711 * aligned and suitable for fencing, and still fit into the available
1712 * mappable space left by the pinned display objects. A classic problem
1713 * we called the page-fault-of-doom where we would ping-pong between
1714 * two objects that could not fit inside the GTT and so the memcpy
1715 * would page one object in at the expense of the other between every
1716 * single byte.
1717 *
1718 * 1 - Objects can be any size, and have any compatible fencing (X Y, or none
1719 * as set via i915_gem_set_tiling() [DRM_I915_GEM_SET_TILING]). If the
1720 * object is too large for the available space (or simply too large
1721 * for the mappable aperture!), a view is created instead and faulted
1722 * into userspace. (This view is aligned and sized appropriately for
1723 * fenced access.)
1724 *
Chris Wilsone22d8e32017-04-12 12:01:11 +01001725 * 2 - Recognise WC as a separate cache domain so that we can flush the
1726 * delayed writes via GTT before performing direct access via WC.
1727 *
Chris Wilson4cc69072016-08-25 19:05:19 +01001728 * Restrictions:
1729 *
1730 * * snoopable objects cannot be accessed via the GTT. It can cause machine
1731 * hangs on some architectures, corruption on others. An attempt to service
1732 * a GTT page fault from a snoopable object will generate a SIGBUS.
1733 *
1734 * * the object must be able to fit into RAM (physical memory, though no
1735 * limited to the mappable aperture).
1736 *
1737 *
1738 * Caveats:
1739 *
1740 * * a new GTT page fault will synchronize rendering from the GPU and flush
1741 * all data to system memory. Subsequent access will not be synchronized.
1742 *
1743 * * all mappings are revoked on runtime device suspend.
1744 *
1745 * * there are only 8, 16 or 32 fence registers to share between all users
1746 * (older machines require fence register for display and blitter access
1747 * as well). Contention of the fence registers will cause the previous users
1748 * to be unmapped and any new access will generate new page faults.
1749 *
1750 * * running out of memory while servicing a fault may generate a SIGBUS,
1751 * rather than the expected SIGSEGV.
1752 */
1753int i915_gem_mmap_gtt_version(void)
1754{
Chris Wilsone22d8e32017-04-12 12:01:11 +01001755 return 2;
Chris Wilson4cc69072016-08-25 19:05:19 +01001756}
1757
Chris Wilson2d4281b2017-01-10 09:56:32 +00001758static inline struct i915_ggtt_view
Chris Wilsond899ace2018-07-25 16:54:47 +01001759compute_partial_view(const struct drm_i915_gem_object *obj,
Chris Wilson2d4281b2017-01-10 09:56:32 +00001760 pgoff_t page_offset,
1761 unsigned int chunk)
1762{
1763 struct i915_ggtt_view view;
1764
1765 if (i915_gem_object_is_tiled(obj))
1766 chunk = roundup(chunk, tile_row_pages(obj));
1767
Chris Wilson2d4281b2017-01-10 09:56:32 +00001768 view.type = I915_GGTT_VIEW_PARTIAL;
Chris Wilson8bab11932017-01-14 00:28:25 +00001769 view.partial.offset = rounddown(page_offset, chunk);
1770 view.partial.size =
Chris Wilson2d4281b2017-01-10 09:56:32 +00001771 min_t(unsigned int, chunk,
Chris Wilson8bab11932017-01-14 00:28:25 +00001772 (obj->base.size >> PAGE_SHIFT) - view.partial.offset);
Chris Wilson2d4281b2017-01-10 09:56:32 +00001773
1774 /* If the partial covers the entire object, just create a normal VMA. */
1775 if (chunk >= obj->base.size >> PAGE_SHIFT)
1776 view.type = I915_GGTT_VIEW_NORMAL;
1777
1778 return view;
1779}
1780
Chris Wilson4cc69072016-08-25 19:05:19 +01001781/**
Jesse Barnesde151cf2008-11-12 10:03:55 -08001782 * i915_gem_fault - fault a page into the GTT
Geliang Tangd9072a32015-09-15 05:58:44 -07001783 * @vmf: fault info
Jesse Barnesde151cf2008-11-12 10:03:55 -08001784 *
1785 * The fault handler is set up by drm_gem_mmap() when a object is GTT mapped
1786 * from userspace. The fault handler takes care of binding the object to
1787 * the GTT (if needed), allocating and programming a fence register (again,
1788 * only if needed based on whether the old reg is still valid or the object
1789 * is tiled) and inserting a new PTE into the faulting process.
1790 *
1791 * Note that the faulting process may involve evicting existing objects
1792 * from the GTT and/or fence registers to make room. So performance may
1793 * suffer if the GTT working set is large or there are few fence registers
1794 * left.
Chris Wilson4cc69072016-08-25 19:05:19 +01001795 *
1796 * The current feature set supported by i915_gem_fault() and thus GTT mmaps
1797 * is exposed via I915_PARAM_MMAP_GTT_VERSION (see i915_gem_mmap_gtt_version).
Jesse Barnesde151cf2008-11-12 10:03:55 -08001798 */
Chris Wilson52137012018-06-06 22:45:20 +01001799vm_fault_t i915_gem_fault(struct vm_fault *vmf)
Jesse Barnesde151cf2008-11-12 10:03:55 -08001800{
Chris Wilson420980c2018-06-05 14:57:46 +01001801#define MIN_CHUNK_PAGES (SZ_1M >> PAGE_SHIFT)
Dave Jiang11bac802017-02-24 14:56:41 -08001802 struct vm_area_struct *area = vmf->vma;
Chris Wilson058d88c2016-08-15 10:49:06 +01001803 struct drm_i915_gem_object *obj = to_intel_bo(area->vm_private_data);
Chris Wilson05394f32010-11-08 19:18:58 +00001804 struct drm_device *dev = obj->base.dev;
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03001805 struct drm_i915_private *dev_priv = to_i915(dev);
1806 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Chris Wilsonaae7c062018-09-03 09:33:34 +01001807 bool write = area->vm_flags & VM_WRITE;
Chris Wilson538ef962019-01-14 14:21:18 +00001808 intel_wakeref_t wakeref;
Chris Wilson058d88c2016-08-15 10:49:06 +01001809 struct i915_vma *vma;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001810 pgoff_t page_offset;
Chris Wilson2caffbf2019-02-08 15:37:03 +00001811 int srcu;
Chris Wilsonb8f90962016-08-05 10:14:07 +01001812 int ret;
Paulo Zanonif65c9162013-11-27 18:20:34 -02001813
Chris Wilson3e977ac2018-07-12 19:53:13 +01001814 /* Sanity check that we allow writing into this object */
1815 if (i915_gem_object_is_readonly(obj) && write)
1816 return VM_FAULT_SIGBUS;
1817
Jesse Barnesde151cf2008-11-12 10:03:55 -08001818 /* We don't use vmf->pgoff since that has the fake offset */
Jan Kara1a29d852016-12-14 15:07:01 -08001819 page_offset = (vmf->address - area->vm_start) >> PAGE_SHIFT;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001820
Chris Wilsondb53a302011-02-03 11:57:46 +00001821 trace_i915_gem_object_fault(obj, page_offset, true, write);
1822
Chris Wilson6e4930f2014-02-07 18:37:06 -02001823 /* Try to flush the object off the GPU first without holding the lock.
Chris Wilsonb8f90962016-08-05 10:14:07 +01001824 * Upon acquiring the lock, we will perform our sanity checks and then
Chris Wilson6e4930f2014-02-07 18:37:06 -02001825 * repeat the flush holding the lock in the normal manner to catch cases
1826 * where we are gazumped.
1827 */
Chris Wilsone95433c2016-10-28 13:58:27 +01001828 ret = i915_gem_object_wait(obj,
1829 I915_WAIT_INTERRUPTIBLE,
Chris Wilson62eb3c22019-02-13 09:25:04 +00001830 MAX_SCHEDULE_TIMEOUT);
Chris Wilson6e4930f2014-02-07 18:37:06 -02001831 if (ret)
Chris Wilsonb8f90962016-08-05 10:14:07 +01001832 goto err;
1833
Chris Wilson40e62d52016-10-28 13:58:41 +01001834 ret = i915_gem_object_pin_pages(obj);
1835 if (ret)
1836 goto err;
1837
Chris Wilson538ef962019-01-14 14:21:18 +00001838 wakeref = intel_runtime_pm_get(dev_priv);
Chris Wilsonb8f90962016-08-05 10:14:07 +01001839
Chris Wilson43a8f682019-02-21 10:29:19 +00001840 srcu = i915_reset_trylock(dev_priv);
1841 if (srcu < 0) {
1842 ret = srcu;
1843 goto err_rpm;
1844 }
1845
Chris Wilsonb8f90962016-08-05 10:14:07 +01001846 ret = i915_mutex_lock_interruptible(dev);
1847 if (ret)
Chris Wilson43a8f682019-02-21 10:29:19 +00001848 goto err_reset;
Chris Wilson6e4930f2014-02-07 18:37:06 -02001849
Chris Wilsoneb119bd2012-12-16 12:43:36 +00001850 /* Access to snoopable pages through the GTT is incoherent. */
Tvrtko Ursulin0031fb92016-11-04 14:42:44 +00001851 if (obj->cache_level != I915_CACHE_NONE && !HAS_LLC(dev_priv)) {
Chris Wilsonddeff6e2014-05-28 16:16:41 +01001852 ret = -EFAULT;
Chris Wilsonb8f90962016-08-05 10:14:07 +01001853 goto err_unlock;
Chris Wilsoneb119bd2012-12-16 12:43:36 +00001854 }
1855
Chris Wilsona61007a2016-08-18 17:17:02 +01001856 /* Now pin it into the GTT as needed */
Chris Wilson7e7367d2018-06-30 10:05:09 +01001857 vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0,
1858 PIN_MAPPABLE |
1859 PIN_NONBLOCK |
1860 PIN_NONFAULT);
Chris Wilsona61007a2016-08-18 17:17:02 +01001861 if (IS_ERR(vma)) {
Chris Wilsona61007a2016-08-18 17:17:02 +01001862 /* Use a partial view if it is bigger than available space */
Chris Wilson2d4281b2017-01-10 09:56:32 +00001863 struct i915_ggtt_view view =
Chris Wilson8201c1f2017-01-10 09:56:33 +00001864 compute_partial_view(obj, page_offset, MIN_CHUNK_PAGES);
Chris Wilson7e7367d2018-06-30 10:05:09 +01001865 unsigned int flags;
Chris Wilsonaa136d92016-08-18 17:17:03 +01001866
Chris Wilson7e7367d2018-06-30 10:05:09 +01001867 flags = PIN_MAPPABLE;
1868 if (view.type == I915_GGTT_VIEW_NORMAL)
1869 flags |= PIN_NONBLOCK; /* avoid warnings for pinned */
1870
1871 /*
1872 * Userspace is now writing through an untracked VMA, abandon
Chris Wilson50349242016-08-18 17:17:04 +01001873 * all hope that the hardware is able to track future writes.
1874 */
1875 obj->frontbuffer_ggtt_origin = ORIGIN_CPU;
1876
Chris Wilson7e7367d2018-06-30 10:05:09 +01001877 vma = i915_gem_object_ggtt_pin(obj, &view, 0, 0, flags);
1878 if (IS_ERR(vma) && !view.type) {
1879 flags = PIN_MAPPABLE;
1880 view.type = I915_GGTT_VIEW_PARTIAL;
1881 vma = i915_gem_object_ggtt_pin(obj, &view, 0, 0, flags);
1882 }
Chris Wilsona61007a2016-08-18 17:17:02 +01001883 }
Chris Wilson058d88c2016-08-15 10:49:06 +01001884 if (IS_ERR(vma)) {
1885 ret = PTR_ERR(vma);
Chris Wilsonb8f90962016-08-05 10:14:07 +01001886 goto err_unlock;
Chris Wilson058d88c2016-08-15 10:49:06 +01001887 }
Jesse Barnesde151cf2008-11-12 10:03:55 -08001888
Chris Wilsonc9839302012-11-20 10:45:17 +00001889 ret = i915_gem_object_set_to_gtt_domain(obj, write);
1890 if (ret)
Chris Wilsonb8f90962016-08-05 10:14:07 +01001891 goto err_unpin;
Chris Wilsonc9839302012-11-20 10:45:17 +00001892
Chris Wilsonaeaaa552019-02-12 13:08:30 +00001893 ret = i915_vma_pin_fence(vma);
1894 if (ret)
1895 goto err_unpin;
1896
Chris Wilsonb90b91d2014-06-10 12:14:40 +01001897 /* Finally, remap it using the new GTT offset */
Chris Wilsonc58305a2016-08-19 16:54:28 +01001898 ret = remap_io_mapping(area,
Chris Wilson8bab11932017-01-14 00:28:25 +00001899 area->vm_start + (vma->ggtt_view.partial.offset << PAGE_SHIFT),
Matthew Auld73ebd502017-12-11 15:18:20 +00001900 (ggtt->gmadr.start + vma->node.start) >> PAGE_SHIFT,
Chris Wilsonc58305a2016-08-19 16:54:28 +01001901 min_t(u64, vma->size, area->vm_end - area->vm_start),
Matthew Auld73ebd502017-12-11 15:18:20 +00001902 &ggtt->iomap);
Chris Wilsona65adaf2017-10-09 09:43:57 +01001903 if (ret)
Chris Wilson43a8f682019-02-21 10:29:19 +00001904 goto err_fence;
Chris Wilsona61007a2016-08-18 17:17:02 +01001905
Chris Wilsona65adaf2017-10-09 09:43:57 +01001906 /* Mark as being mmapped into userspace for later revocation */
1907 assert_rpm_wakelock_held(dev_priv);
1908 if (!i915_vma_set_userfault(vma) && !obj->userfault_count++)
1909 list_add(&obj->userfault_link, &dev_priv->mm.userfault_list);
1910 GEM_BUG_ON(!obj->userfault_count);
1911
Chris Wilson7125397b2017-12-06 12:49:14 +00001912 i915_vma_set_ggtt_write(vma);
1913
Chris Wilsonaeaaa552019-02-12 13:08:30 +00001914err_fence:
1915 i915_vma_unpin_fence(vma);
Chris Wilsonb8f90962016-08-05 10:14:07 +01001916err_unpin:
Chris Wilson058d88c2016-08-15 10:49:06 +01001917 __i915_vma_unpin(vma);
Chris Wilsonb8f90962016-08-05 10:14:07 +01001918err_unlock:
Jesse Barnesde151cf2008-11-12 10:03:55 -08001919 mutex_unlock(&dev->struct_mutex);
Chris Wilson43a8f682019-02-21 10:29:19 +00001920err_reset:
1921 i915_reset_unlock(dev_priv, srcu);
Chris Wilsonb8f90962016-08-05 10:14:07 +01001922err_rpm:
Chris Wilson538ef962019-01-14 14:21:18 +00001923 intel_runtime_pm_put(dev_priv, wakeref);
Chris Wilson40e62d52016-10-28 13:58:41 +01001924 i915_gem_object_unpin_pages(obj);
Chris Wilsonb8f90962016-08-05 10:14:07 +01001925err:
Jesse Barnesde151cf2008-11-12 10:03:55 -08001926 switch (ret) {
Chris Wilsond9bc7e92011-02-07 13:09:31 +00001927 case -EIO:
Daniel Vetter2232f032014-09-04 09:36:18 +02001928 /*
1929 * We eat errors when the gpu is terminally wedged to avoid
1930 * userspace unduly crashing (gl has no provisions for mmaps to
1931 * fail). But any other -EIO isn't ours (e.g. swap in failure)
1932 * and so needs to be reported.
1933 */
Chris Wilsonc41166f2019-02-20 14:56:37 +00001934 if (!i915_terminally_wedged(dev_priv))
Chris Wilson52137012018-06-06 22:45:20 +01001935 return VM_FAULT_SIGBUS;
Gustavo A. R. Silvaf0d759f2018-06-28 17:35:41 -05001936 /* else: fall through */
Chris Wilson045e7692010-11-07 09:18:22 +00001937 case -EAGAIN:
Daniel Vetter571c6082013-09-12 17:57:28 +02001938 /*
1939 * EAGAIN means the gpu is hung and we'll wait for the error
1940 * handler to reset everything when re-faulting in
1941 * i915_mutex_lock_interruptible.
Chris Wilsond9bc7e92011-02-07 13:09:31 +00001942 */
Chris Wilsonc7150892009-09-23 00:43:56 +01001943 case 0:
1944 case -ERESTARTSYS:
Chris Wilsonbed636a2011-02-11 20:31:19 +00001945 case -EINTR:
Dmitry Rogozhkine79e0fe2012-10-03 17:15:26 +03001946 case -EBUSY:
1947 /*
1948 * EBUSY is ok: this just means that another thread
1949 * already did the job.
1950 */
Chris Wilson52137012018-06-06 22:45:20 +01001951 return VM_FAULT_NOPAGE;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001952 case -ENOMEM:
Chris Wilson52137012018-06-06 22:45:20 +01001953 return VM_FAULT_OOM;
Daniel Vettera7c2e1a2012-10-17 11:17:16 +02001954 case -ENOSPC:
Chris Wilson45d67812014-01-31 11:34:57 +00001955 case -EFAULT:
Chris Wilson52137012018-06-06 22:45:20 +01001956 return VM_FAULT_SIGBUS;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001957 default:
Daniel Vettera7c2e1a2012-10-17 11:17:16 +02001958 WARN_ONCE(ret, "unhandled error in i915_gem_fault: %i\n", ret);
Chris Wilson52137012018-06-06 22:45:20 +01001959 return VM_FAULT_SIGBUS;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001960 }
1961}
1962
Chris Wilsona65adaf2017-10-09 09:43:57 +01001963static void __i915_gem_object_release_mmap(struct drm_i915_gem_object *obj)
1964{
1965 struct i915_vma *vma;
1966
1967 GEM_BUG_ON(!obj->userfault_count);
1968
1969 obj->userfault_count = 0;
1970 list_del(&obj->userfault_link);
1971 drm_vma_node_unmap(&obj->base.vma_node,
1972 obj->base.dev->anon_inode->i_mapping);
1973
Chris Wilsone2189dd2017-12-07 21:14:07 +00001974 for_each_ggtt_vma(vma, obj)
Chris Wilsona65adaf2017-10-09 09:43:57 +01001975 i915_vma_unset_userfault(vma);
Chris Wilsona65adaf2017-10-09 09:43:57 +01001976}
1977
Jesse Barnesde151cf2008-11-12 10:03:55 -08001978/**
Chris Wilson901782b2009-07-10 08:18:50 +01001979 * i915_gem_release_mmap - remove physical page mappings
1980 * @obj: obj in question
1981 *
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02001982 * Preserve the reservation of the mmapping with the DRM core code, but
Chris Wilson901782b2009-07-10 08:18:50 +01001983 * relinquish ownership of the pages back to the system.
1984 *
1985 * It is vital that we remove the page mapping if we have mapped a tiled
1986 * object through the GTT and then lose the fence register due to
1987 * resource pressure. Similarly if the object has been moved out of the
1988 * aperture, than pages mapped into userspace must be revoked. Removing the
1989 * mapping will then trigger a page fault on the next user access, allowing
1990 * fixup by i915_gem_fault().
1991 */
Eric Anholtd05ca302009-07-10 13:02:26 -07001992void
Chris Wilson05394f32010-11-08 19:18:58 +00001993i915_gem_release_mmap(struct drm_i915_gem_object *obj)
Chris Wilson901782b2009-07-10 08:18:50 +01001994{
Chris Wilson275f0392016-10-24 13:42:14 +01001995 struct drm_i915_private *i915 = to_i915(obj->base.dev);
Chris Wilson538ef962019-01-14 14:21:18 +00001996 intel_wakeref_t wakeref;
Chris Wilson275f0392016-10-24 13:42:14 +01001997
Chris Wilson349f2cc2016-04-13 17:35:12 +01001998 /* Serialisation between user GTT access and our code depends upon
1999 * revoking the CPU's PTE whilst the mutex is held. The next user
2000 * pagefault then has to wait until we release the mutex.
Chris Wilson9c870d02016-10-24 13:42:15 +01002001 *
2002 * Note that RPM complicates somewhat by adding an additional
2003 * requirement that operations to the GGTT be made holding the RPM
2004 * wakeref.
Chris Wilson349f2cc2016-04-13 17:35:12 +01002005 */
Chris Wilson275f0392016-10-24 13:42:14 +01002006 lockdep_assert_held(&i915->drm.struct_mutex);
Chris Wilson538ef962019-01-14 14:21:18 +00002007 wakeref = intel_runtime_pm_get(i915);
Chris Wilson349f2cc2016-04-13 17:35:12 +01002008
Chris Wilsona65adaf2017-10-09 09:43:57 +01002009 if (!obj->userfault_count)
Chris Wilson9c870d02016-10-24 13:42:15 +01002010 goto out;
Chris Wilson901782b2009-07-10 08:18:50 +01002011
Chris Wilsona65adaf2017-10-09 09:43:57 +01002012 __i915_gem_object_release_mmap(obj);
Chris Wilson349f2cc2016-04-13 17:35:12 +01002013
2014 /* Ensure that the CPU's PTE are revoked and there are not outstanding
2015 * memory transactions from userspace before we return. The TLB
2016 * flushing implied above by changing the PTE above *should* be
2017 * sufficient, an extra barrier here just provides us with a bit
2018 * of paranoid documentation about our requirement to serialise
2019 * memory writes before touching registers / GSM.
2020 */
2021 wmb();
Chris Wilson9c870d02016-10-24 13:42:15 +01002022
2023out:
Chris Wilson538ef962019-01-14 14:21:18 +00002024 intel_runtime_pm_put(i915, wakeref);
Chris Wilson901782b2009-07-10 08:18:50 +01002025}
2026
Chris Wilson7c108fd2016-10-24 13:42:18 +01002027void i915_gem_runtime_suspend(struct drm_i915_private *dev_priv)
Chris Wilsoneedd10f2014-06-16 08:57:44 +01002028{
Chris Wilson3594a3e2016-10-24 13:42:16 +01002029 struct drm_i915_gem_object *obj, *on;
Chris Wilson7c108fd2016-10-24 13:42:18 +01002030 int i;
Chris Wilsoneedd10f2014-06-16 08:57:44 +01002031
Chris Wilson3594a3e2016-10-24 13:42:16 +01002032 /*
2033 * Only called during RPM suspend. All users of the userfault_list
2034 * must be holding an RPM wakeref to ensure that this can not
2035 * run concurrently with themselves (and use the struct_mutex for
2036 * protection between themselves).
2037 */
2038
2039 list_for_each_entry_safe(obj, on,
Chris Wilsona65adaf2017-10-09 09:43:57 +01002040 &dev_priv->mm.userfault_list, userfault_link)
2041 __i915_gem_object_release_mmap(obj);
Chris Wilson7c108fd2016-10-24 13:42:18 +01002042
2043 /* The fence will be lost when the device powers down. If any were
2044 * in use by hardware (i.e. they are pinned), we should not be powering
2045 * down! All other fences will be reacquired by the user upon waking.
2046 */
2047 for (i = 0; i < dev_priv->num_fence_regs; i++) {
2048 struct drm_i915_fence_reg *reg = &dev_priv->fence_regs[i];
2049
Chris Wilsone0ec3ec2017-02-03 12:57:17 +00002050 /* Ideally we want to assert that the fence register is not
2051 * live at this point (i.e. that no piece of code will be
2052 * trying to write through fence + GTT, as that both violates
2053 * our tracking of activity and associated locking/barriers,
2054 * but also is illegal given that the hw is powered down).
2055 *
2056 * Previously we used reg->pin_count as a "liveness" indicator.
2057 * That is not sufficient, and we need a more fine-grained
2058 * tool if we want to have a sanity check here.
2059 */
Chris Wilson7c108fd2016-10-24 13:42:18 +01002060
2061 if (!reg->vma)
2062 continue;
2063
Chris Wilsona65adaf2017-10-09 09:43:57 +01002064 GEM_BUG_ON(i915_vma_has_userfault(reg->vma));
Chris Wilson7c108fd2016-10-24 13:42:18 +01002065 reg->dirty = true;
2066 }
Chris Wilsoneedd10f2014-06-16 08:57:44 +01002067}
2068
Chris Wilsond8cb5082012-08-11 15:41:03 +01002069static int i915_gem_object_create_mmap_offset(struct drm_i915_gem_object *obj)
2070{
Chris Wilsonfac5e232016-07-04 11:34:36 +01002071 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
Chris Wilsonf3f61842016-08-05 10:14:14 +01002072 int err;
Chris Wilsond8cb5082012-08-11 15:41:03 +01002073
Chris Wilsonf3f61842016-08-05 10:14:14 +01002074 err = drm_gem_create_mmap_offset(&obj->base);
Chris Wilsonb42a13d2017-01-06 15:22:40 +00002075 if (likely(!err))
Chris Wilsonf3f61842016-08-05 10:14:14 +01002076 return 0;
Daniel Vetterda494d72012-12-20 15:11:16 +01002077
Chris Wilsonb42a13d2017-01-06 15:22:40 +00002078 /* Attempt to reap some mmap space from dead objects */
2079 do {
Chris Wilsonec625fb2018-07-09 13:20:42 +01002080 err = i915_gem_wait_for_idle(dev_priv,
2081 I915_WAIT_INTERRUPTIBLE,
2082 MAX_SCHEDULE_TIMEOUT);
Chris Wilsonb42a13d2017-01-06 15:22:40 +00002083 if (err)
2084 break;
Chris Wilsond8cb5082012-08-11 15:41:03 +01002085
Chris Wilsonb42a13d2017-01-06 15:22:40 +00002086 i915_gem_drain_freed_objects(dev_priv);
Chris Wilsonf3f61842016-08-05 10:14:14 +01002087 err = drm_gem_create_mmap_offset(&obj->base);
Chris Wilsonb42a13d2017-01-06 15:22:40 +00002088 if (!err)
2089 break;
2090
2091 } while (flush_delayed_work(&dev_priv->gt.retire_work));
Daniel Vetterda494d72012-12-20 15:11:16 +01002092
Chris Wilsonf3f61842016-08-05 10:14:14 +01002093 return err;
Chris Wilsond8cb5082012-08-11 15:41:03 +01002094}
2095
2096static void i915_gem_object_free_mmap_offset(struct drm_i915_gem_object *obj)
2097{
Chris Wilsond8cb5082012-08-11 15:41:03 +01002098 drm_gem_free_mmap_offset(&obj->base);
2099}
2100
Dave Airlieda6b51d2014-12-24 13:11:17 +10002101int
Dave Airlieff72145b2011-02-07 12:16:14 +10002102i915_gem_mmap_gtt(struct drm_file *file,
2103 struct drm_device *dev,
Jani Nikula739f3ab2019-01-16 11:15:19 +02002104 u32 handle,
2105 u64 *offset)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002106{
Chris Wilson05394f32010-11-08 19:18:58 +00002107 struct drm_i915_gem_object *obj;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002108 int ret;
2109
Chris Wilson03ac0642016-07-20 13:31:51 +01002110 obj = i915_gem_object_lookup(file, handle);
Chris Wilsonf3f61842016-08-05 10:14:14 +01002111 if (!obj)
2112 return -ENOENT;
Chris Wilsonab182822009-09-22 18:46:17 +01002113
Chris Wilsond8cb5082012-08-11 15:41:03 +01002114 ret = i915_gem_object_create_mmap_offset(obj);
Chris Wilsonf3f61842016-08-05 10:14:14 +01002115 if (ret == 0)
2116 *offset = drm_vma_node_offset_addr(&obj->base.vma_node);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002117
Chris Wilsonf0cd5182016-10-28 13:58:43 +01002118 i915_gem_object_put(obj);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01002119 return ret;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002120}
2121
Dave Airlieff72145b2011-02-07 12:16:14 +10002122/**
2123 * i915_gem_mmap_gtt_ioctl - prepare an object for GTT mmap'ing
2124 * @dev: DRM device
2125 * @data: GTT mapping ioctl data
2126 * @file: GEM object info
2127 *
2128 * Simply returns the fake offset to userspace so it can mmap it.
2129 * The mmap call will end up in drm_gem_mmap(), which will set things
2130 * up so we can get faults in the handler above.
2131 *
2132 * The fault handler will take care of binding the object into the GTT
2133 * (since it may have been evicted to make room for something), allocating
2134 * a fence register, and mapping the appropriate aperture address into
2135 * userspace.
2136 */
2137int
2138i915_gem_mmap_gtt_ioctl(struct drm_device *dev, void *data,
2139 struct drm_file *file)
2140{
2141 struct drm_i915_gem_mmap_gtt *args = data;
2142
Dave Airlieda6b51d2014-12-24 13:11:17 +10002143 return i915_gem_mmap_gtt(file, dev, args->handle, &args->offset);
Dave Airlieff72145b2011-02-07 12:16:14 +10002144}
2145
Daniel Vetter225067e2012-08-20 10:23:20 +02002146/* Immediately discard the backing storage */
2147static void
2148i915_gem_object_truncate(struct drm_i915_gem_object *obj)
Chris Wilsone5281cc2010-10-28 13:45:36 +01002149{
Chris Wilson4d6294bf2012-08-11 15:41:05 +01002150 i915_gem_object_free_mmap_offset(obj);
Daniel Vetter1286ff72012-05-10 15:25:09 +02002151
Chris Wilson4d6294bf2012-08-11 15:41:05 +01002152 if (obj->base.filp == NULL)
2153 return;
2154
Daniel Vetter225067e2012-08-20 10:23:20 +02002155 /* Our goal here is to return as much of the memory as
2156 * is possible back to the system as we are called from OOM.
2157 * To do this we must instruct the shmfs to drop all of its
2158 * backing pages, *now*.
Chris Wilsone5281cc2010-10-28 13:45:36 +01002159 */
Chris Wilson55372522014-03-25 13:23:06 +00002160 shmem_truncate_range(file_inode(obj->base.filp), 0, (loff_t)-1);
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002161 obj->mm.madv = __I915_MADV_PURGED;
Chris Wilson4e5462e2017-03-07 13:20:31 +00002162 obj->mm.pages = ERR_PTR(-EFAULT);
Chris Wilsone5281cc2010-10-28 13:45:36 +01002163}
Chris Wilsone5281cc2010-10-28 13:45:36 +01002164
Chris Wilson55372522014-03-25 13:23:06 +00002165/* Try to discard unwanted pages */
Chris Wilson03ac84f2016-10-28 13:58:36 +01002166void __i915_gem_object_invalidate(struct drm_i915_gem_object *obj)
Daniel Vetter225067e2012-08-20 10:23:20 +02002167{
Chris Wilson55372522014-03-25 13:23:06 +00002168 struct address_space *mapping;
2169
Chris Wilson1233e2d2016-10-28 13:58:37 +01002170 lockdep_assert_held(&obj->mm.lock);
Chris Wilsonf1fa4f42017-10-13 21:26:13 +01002171 GEM_BUG_ON(i915_gem_object_has_pages(obj));
Chris Wilson1233e2d2016-10-28 13:58:37 +01002172
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002173 switch (obj->mm.madv) {
Chris Wilson55372522014-03-25 13:23:06 +00002174 case I915_MADV_DONTNEED:
2175 i915_gem_object_truncate(obj);
2176 case __I915_MADV_PURGED:
2177 return;
2178 }
2179
2180 if (obj->base.filp == NULL)
2181 return;
2182
Al Viro93c76a32015-12-04 23:45:44 -05002183 mapping = obj->base.filp->f_mapping,
Chris Wilson55372522014-03-25 13:23:06 +00002184 invalidate_mapping_pages(mapping, 0, (loff_t)-1);
Chris Wilsone5281cc2010-10-28 13:45:36 +01002185}
2186
Kuo-Hsin Yang64e3d122018-11-06 13:23:24 +00002187/*
2188 * Move pages to appropriate lru and release the pagevec, decrementing the
2189 * ref count of those pages.
2190 */
2191static void check_release_pagevec(struct pagevec *pvec)
2192{
2193 check_move_unevictable_pages(pvec);
2194 __pagevec_release(pvec);
2195 cond_resched();
2196}
2197
Chris Wilson5cdf5882010-09-27 15:51:07 +01002198static void
Chris Wilson03ac84f2016-10-28 13:58:36 +01002199i915_gem_object_put_pages_gtt(struct drm_i915_gem_object *obj,
2200 struct sg_table *pages)
Eric Anholt673a3942008-07-30 12:06:12 -07002201{
Dave Gordon85d12252016-05-20 11:54:06 +01002202 struct sgt_iter sgt_iter;
Kuo-Hsin Yang64e3d122018-11-06 13:23:24 +00002203 struct pagevec pvec;
Dave Gordon85d12252016-05-20 11:54:06 +01002204 struct page *page;
Daniel Vetter1286ff72012-05-10 15:25:09 +02002205
Chris Wilsone5facdf2016-12-23 14:57:57 +00002206 __i915_gem_object_release_shmem(obj, pages, true);
Eric Anholt856fa192009-03-19 14:10:50 -07002207
Chris Wilson03ac84f2016-10-28 13:58:36 +01002208 i915_gem_gtt_finish_pages(obj, pages);
Imre Deake2273302015-07-09 12:59:05 +03002209
Daniel Vetter6dacfd22011-09-12 21:30:02 +02002210 if (i915_gem_object_needs_bit17_swizzle(obj))
Chris Wilson03ac84f2016-10-28 13:58:36 +01002211 i915_gem_object_save_bit_17_swizzle(obj, pages);
Eric Anholt280b7132009-03-12 16:56:27 -07002212
Kuo-Hsin Yang64e3d122018-11-06 13:23:24 +00002213 mapping_clear_unevictable(file_inode(obj->base.filp)->i_mapping);
2214
2215 pagevec_init(&pvec);
Chris Wilson03ac84f2016-10-28 13:58:36 +01002216 for_each_sgt_page(page, sgt_iter, pages) {
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002217 if (obj->mm.dirty)
Chris Wilson9da3da62012-06-01 15:20:22 +01002218 set_page_dirty(page);
Chris Wilson3ef94da2009-09-14 16:50:29 +01002219
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002220 if (obj->mm.madv == I915_MADV_WILLNEED)
Chris Wilson9da3da62012-06-01 15:20:22 +01002221 mark_page_accessed(page);
Chris Wilson3ef94da2009-09-14 16:50:29 +01002222
Kuo-Hsin Yang64e3d122018-11-06 13:23:24 +00002223 if (!pagevec_add(&pvec, page))
2224 check_release_pagevec(&pvec);
Chris Wilson3ef94da2009-09-14 16:50:29 +01002225 }
Kuo-Hsin Yang64e3d122018-11-06 13:23:24 +00002226 if (pagevec_count(&pvec))
2227 check_release_pagevec(&pvec);
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002228 obj->mm.dirty = false;
Eric Anholt673a3942008-07-30 12:06:12 -07002229
Chris Wilson03ac84f2016-10-28 13:58:36 +01002230 sg_free_table(pages);
2231 kfree(pages);
Chris Wilson37e680a2012-06-07 15:38:42 +01002232}
2233
Chris Wilson96d77632016-10-28 13:58:33 +01002234static void __i915_gem_object_reset_page_iter(struct drm_i915_gem_object *obj)
2235{
2236 struct radix_tree_iter iter;
Ville Syrjäläc23aa712017-09-01 20:12:51 +03002237 void __rcu **slot;
Chris Wilson96d77632016-10-28 13:58:33 +01002238
Chris Wilsonbea6e982017-10-26 14:00:31 +01002239 rcu_read_lock();
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002240 radix_tree_for_each_slot(slot, &obj->mm.get_page.radix, &iter, 0)
2241 radix_tree_delete(&obj->mm.get_page.radix, iter.index);
Chris Wilsonbea6e982017-10-26 14:00:31 +01002242 rcu_read_unlock();
Chris Wilson96d77632016-10-28 13:58:33 +01002243}
2244
Chris Wilsonacd1c1e2018-06-11 08:55:32 +01002245static struct sg_table *
2246__i915_gem_object_unset_pages(struct drm_i915_gem_object *obj)
Chris Wilson37e680a2012-06-07 15:38:42 +01002247{
Chris Wilsonf2123812017-10-16 12:40:37 +01002248 struct drm_i915_private *i915 = to_i915(obj->base.dev);
Chris Wilson03ac84f2016-10-28 13:58:36 +01002249 struct sg_table *pages;
Chris Wilson37e680a2012-06-07 15:38:42 +01002250
Chris Wilson03ac84f2016-10-28 13:58:36 +01002251 pages = fetch_and_zero(&obj->mm.pages);
Chris Wilson484d9a82019-01-15 12:44:42 +00002252 if (IS_ERR_OR_NULL(pages))
2253 return pages;
Chris Wilsona2165e32012-12-03 11:49:00 +00002254
Chris Wilsonf2123812017-10-16 12:40:37 +01002255 spin_lock(&i915->mm.obj_lock);
2256 list_del(&obj->mm.link);
2257 spin_unlock(&i915->mm.obj_lock);
2258
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002259 if (obj->mm.mapping) {
Chris Wilson4b30cb22016-08-18 17:16:42 +01002260 void *ptr;
2261
Chris Wilson0ce81782017-05-17 13:09:59 +01002262 ptr = page_mask_bits(obj->mm.mapping);
Chris Wilson4b30cb22016-08-18 17:16:42 +01002263 if (is_vmalloc_addr(ptr))
2264 vunmap(ptr);
Chris Wilsonfb8621d2016-04-08 12:11:14 +01002265 else
Chris Wilson4b30cb22016-08-18 17:16:42 +01002266 kunmap(kmap_to_page(ptr));
2267
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002268 obj->mm.mapping = NULL;
Chris Wilson0a798eb2016-04-08 12:11:11 +01002269 }
2270
Chris Wilson96d77632016-10-28 13:58:33 +01002271 __i915_gem_object_reset_page_iter(obj);
Chris Wilsonacd1c1e2018-06-11 08:55:32 +01002272 obj->mm.page_sizes.phys = obj->mm.page_sizes.sg = 0;
Chris Wilson96d77632016-10-28 13:58:33 +01002273
Chris Wilsonacd1c1e2018-06-11 08:55:32 +01002274 return pages;
2275}
2276
Chris Wilson484d9a82019-01-15 12:44:42 +00002277int __i915_gem_object_put_pages(struct drm_i915_gem_object *obj,
2278 enum i915_mm_subclass subclass)
Chris Wilsonacd1c1e2018-06-11 08:55:32 +01002279{
2280 struct sg_table *pages;
Chris Wilson484d9a82019-01-15 12:44:42 +00002281 int ret;
Chris Wilsonacd1c1e2018-06-11 08:55:32 +01002282
2283 if (i915_gem_object_has_pinned_pages(obj))
Chris Wilson484d9a82019-01-15 12:44:42 +00002284 return -EBUSY;
Chris Wilsonacd1c1e2018-06-11 08:55:32 +01002285
2286 GEM_BUG_ON(obj->bind_count);
Chris Wilsonacd1c1e2018-06-11 08:55:32 +01002287
2288 /* May be called by shrinker from within get_pages() (on another bo) */
2289 mutex_lock_nested(&obj->mm.lock, subclass);
Chris Wilson484d9a82019-01-15 12:44:42 +00002290 if (unlikely(atomic_read(&obj->mm.pages_pin_count))) {
2291 ret = -EBUSY;
Chris Wilsonacd1c1e2018-06-11 08:55:32 +01002292 goto unlock;
Chris Wilson484d9a82019-01-15 12:44:42 +00002293 }
Chris Wilsonacd1c1e2018-06-11 08:55:32 +01002294
2295 /*
2296 * ->put_pages might need to allocate memory for the bit17 swizzle
2297 * array, hence protect them from being reaped by removing them from gtt
2298 * lists early.
2299 */
2300 pages = __i915_gem_object_unset_pages(obj);
Chris Wilson484d9a82019-01-15 12:44:42 +00002301
2302 /*
2303 * XXX Temporary hijinx to avoid updating all backends to handle
2304 * NULL pages. In the future, when we have more asynchronous
2305 * get_pages backends we should be better able to handle the
2306 * cancellation of the async task in a more uniform manner.
2307 */
2308 if (!pages && !i915_gem_object_needs_async_cancel(obj))
2309 pages = ERR_PTR(-EINVAL);
2310
Chris Wilson4e5462e2017-03-07 13:20:31 +00002311 if (!IS_ERR(pages))
2312 obj->ops->put_pages(obj, pages);
2313
Chris Wilson484d9a82019-01-15 12:44:42 +00002314 ret = 0;
Chris Wilson1233e2d2016-10-28 13:58:37 +01002315unlock:
2316 mutex_unlock(&obj->mm.lock);
Chris Wilson484d9a82019-01-15 12:44:42 +00002317
2318 return ret;
Chris Wilson6c085a72012-08-20 11:40:46 +02002319}
2320
Tvrtko Ursulinf8e57862018-09-26 09:03:53 +01002321bool i915_sg_trim(struct sg_table *orig_st)
Tvrtko Ursulin0c40ce12016-11-09 15:13:43 +00002322{
2323 struct sg_table new_st;
2324 struct scatterlist *sg, *new_sg;
2325 unsigned int i;
2326
2327 if (orig_st->nents == orig_st->orig_nents)
Chris Wilson935a2f72017-02-13 17:15:13 +00002328 return false;
Tvrtko Ursulin0c40ce12016-11-09 15:13:43 +00002329
Chris Wilson8bfc478f2016-12-23 14:57:58 +00002330 if (sg_alloc_table(&new_st, orig_st->nents, GFP_KERNEL | __GFP_NOWARN))
Chris Wilson935a2f72017-02-13 17:15:13 +00002331 return false;
Tvrtko Ursulin0c40ce12016-11-09 15:13:43 +00002332
2333 new_sg = new_st.sgl;
2334 for_each_sg(orig_st->sgl, sg, orig_st->nents, i) {
2335 sg_set_page(new_sg, sg_page(sg), sg->length, 0);
Matthew Auldc6d22ab2018-09-20 15:27:06 +01002336 sg_dma_address(new_sg) = sg_dma_address(sg);
2337 sg_dma_len(new_sg) = sg_dma_len(sg);
2338
Tvrtko Ursulin0c40ce12016-11-09 15:13:43 +00002339 new_sg = sg_next(new_sg);
2340 }
Chris Wilsonc2dc6cc2016-12-19 12:43:46 +00002341 GEM_BUG_ON(new_sg); /* Should walk exactly nents and hit the end */
Tvrtko Ursulin0c40ce12016-11-09 15:13:43 +00002342
2343 sg_free_table(orig_st);
2344
2345 *orig_st = new_st;
Chris Wilson935a2f72017-02-13 17:15:13 +00002346 return true;
Tvrtko Ursulin0c40ce12016-11-09 15:13:43 +00002347}
2348
Matthew Auldb91b09e2017-10-06 23:18:17 +01002349static int i915_gem_object_get_pages_gtt(struct drm_i915_gem_object *obj)
Eric Anholt673a3942008-07-30 12:06:12 -07002350{
Chris Wilsonfac5e232016-07-04 11:34:36 +01002351 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
Chris Wilsond766ef52016-12-19 12:43:45 +00002352 const unsigned long page_count = obj->base.size / PAGE_SIZE;
2353 unsigned long i;
Eric Anholt673a3942008-07-30 12:06:12 -07002354 struct address_space *mapping;
Chris Wilson9da3da62012-06-01 15:20:22 +01002355 struct sg_table *st;
2356 struct scatterlist *sg;
Dave Gordon85d12252016-05-20 11:54:06 +01002357 struct sgt_iter sgt_iter;
Eric Anholt673a3942008-07-30 12:06:12 -07002358 struct page *page;
Imre Deak90797e62013-02-18 19:28:03 +02002359 unsigned long last_pfn = 0; /* suppress gcc warning */
Tvrtko Ursulin56024522017-08-03 10:14:17 +01002360 unsigned int max_segment = i915_sg_segment_size();
Matthew Auld84e89782017-10-09 12:00:24 +01002361 unsigned int sg_page_sizes;
Kuo-Hsin Yang64e3d122018-11-06 13:23:24 +00002362 struct pagevec pvec;
Chris Wilson4846bf02017-06-09 12:03:46 +01002363 gfp_t noreclaim;
Imre Deake2273302015-07-09 12:59:05 +03002364 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07002365
Chris Wilsone0ff7a72018-09-03 09:33:36 +01002366 /*
2367 * Assert that the object is not currently in any GPU domain. As it
Chris Wilson6c085a72012-08-20 11:40:46 +02002368 * wasn't in the GTT, there shouldn't be any way it could have been in
2369 * a GPU cache
2370 */
Christian Königc0a51fd2018-02-16 13:43:38 +01002371 GEM_BUG_ON(obj->read_domains & I915_GEM_GPU_DOMAINS);
2372 GEM_BUG_ON(obj->write_domain & I915_GEM_GPU_DOMAINS);
Chris Wilson6c085a72012-08-20 11:40:46 +02002373
Chris Wilsone0ff7a72018-09-03 09:33:36 +01002374 /*
2375 * If there's no chance of allocating enough pages for the whole
2376 * object, bail early.
2377 */
Arun KSca79b0c2018-12-28 00:34:29 -08002378 if (page_count > totalram_pages())
Chris Wilsone0ff7a72018-09-03 09:33:36 +01002379 return -ENOMEM;
2380
Chris Wilson9da3da62012-06-01 15:20:22 +01002381 st = kmalloc(sizeof(*st), GFP_KERNEL);
2382 if (st == NULL)
Matthew Auldb91b09e2017-10-06 23:18:17 +01002383 return -ENOMEM;
Eric Anholt673a3942008-07-30 12:06:12 -07002384
Chris Wilsond766ef52016-12-19 12:43:45 +00002385rebuild_st:
Chris Wilson9da3da62012-06-01 15:20:22 +01002386 if (sg_alloc_table(st, page_count, GFP_KERNEL)) {
Chris Wilson9da3da62012-06-01 15:20:22 +01002387 kfree(st);
Matthew Auldb91b09e2017-10-06 23:18:17 +01002388 return -ENOMEM;
Chris Wilson9da3da62012-06-01 15:20:22 +01002389 }
2390
Chris Wilsone0ff7a72018-09-03 09:33:36 +01002391 /*
2392 * Get the list of pages out of our struct file. They'll be pinned
Chris Wilson9da3da62012-06-01 15:20:22 +01002393 * at this point until we release them.
2394 *
2395 * Fail silently without starting the shrinker
2396 */
Al Viro93c76a32015-12-04 23:45:44 -05002397 mapping = obj->base.filp->f_mapping;
Kuo-Hsin Yang64e3d122018-11-06 13:23:24 +00002398 mapping_set_unevictable(mapping);
Chris Wilson0f6ab552017-06-09 12:03:48 +01002399 noreclaim = mapping_gfp_constraint(mapping, ~__GFP_RECLAIM);
Chris Wilson4846bf02017-06-09 12:03:46 +01002400 noreclaim |= __GFP_NORETRY | __GFP_NOWARN;
2401
Imre Deak90797e62013-02-18 19:28:03 +02002402 sg = st->sgl;
2403 st->nents = 0;
Matthew Auld84e89782017-10-09 12:00:24 +01002404 sg_page_sizes = 0;
Imre Deak90797e62013-02-18 19:28:03 +02002405 for (i = 0; i < page_count; i++) {
Chris Wilson4846bf02017-06-09 12:03:46 +01002406 const unsigned int shrink[] = {
2407 I915_SHRINK_BOUND | I915_SHRINK_UNBOUND | I915_SHRINK_PURGEABLE,
2408 0,
2409 }, *s = shrink;
2410 gfp_t gfp = noreclaim;
2411
2412 do {
Chris Wilsone6db7f42018-11-05 17:06:40 +00002413 cond_resched();
Chris Wilson6c085a72012-08-20 11:40:46 +02002414 page = shmem_read_mapping_page_gfp(mapping, i, gfp);
Chengguang Xu772b5402019-02-21 10:08:19 +08002415 if (!IS_ERR(page))
Chris Wilson4846bf02017-06-09 12:03:46 +01002416 break;
2417
2418 if (!*s) {
2419 ret = PTR_ERR(page);
2420 goto err_sg;
2421 }
2422
Chris Wilson912d5722017-09-06 16:19:30 -07002423 i915_gem_shrink(dev_priv, 2 * page_count, NULL, *s++);
Chris Wilson24f8e002017-03-22 11:05:21 +00002424
Chris Wilsone0ff7a72018-09-03 09:33:36 +01002425 /*
2426 * We've tried hard to allocate the memory by reaping
Chris Wilson6c085a72012-08-20 11:40:46 +02002427 * our own buffer, now let the real VM do its job and
2428 * go down in flames if truly OOM.
Chris Wilson24f8e002017-03-22 11:05:21 +00002429 *
2430 * However, since graphics tend to be disposable,
2431 * defer the oom here by reporting the ENOMEM back
2432 * to userspace.
Chris Wilson6c085a72012-08-20 11:40:46 +02002433 */
Chris Wilson4846bf02017-06-09 12:03:46 +01002434 if (!*s) {
2435 /* reclaim and warn, but no oom */
2436 gfp = mapping_gfp_mask(mapping);
Chris Wilsoneaf41802017-06-09 12:03:47 +01002437
Chris Wilsone0ff7a72018-09-03 09:33:36 +01002438 /*
2439 * Our bo are always dirty and so we require
Chris Wilsoneaf41802017-06-09 12:03:47 +01002440 * kswapd to reclaim our pages (direct reclaim
2441 * does not effectively begin pageout of our
2442 * buffers on its own). However, direct reclaim
2443 * only waits for kswapd when under allocation
2444 * congestion. So as a result __GFP_RECLAIM is
2445 * unreliable and fails to actually reclaim our
2446 * dirty pages -- unless you try over and over
2447 * again with !__GFP_NORETRY. However, we still
2448 * want to fail this allocation rather than
2449 * trigger the out-of-memory killer and for
Michal Hockodbb32952017-07-12 14:36:55 -07002450 * this we want __GFP_RETRY_MAYFAIL.
Chris Wilsoneaf41802017-06-09 12:03:47 +01002451 */
Michal Hockodbb32952017-07-12 14:36:55 -07002452 gfp |= __GFP_RETRY_MAYFAIL;
Imre Deake2273302015-07-09 12:59:05 +03002453 }
Chris Wilson4846bf02017-06-09 12:03:46 +01002454 } while (1);
2455
Chris Wilson871dfbd2016-10-11 09:20:21 +01002456 if (!i ||
2457 sg->length >= max_segment ||
2458 page_to_pfn(page) != last_pfn + 1) {
Matthew Aulda5c081662017-10-06 23:18:18 +01002459 if (i) {
Matthew Auld84e89782017-10-09 12:00:24 +01002460 sg_page_sizes |= sg->length;
Imre Deak90797e62013-02-18 19:28:03 +02002461 sg = sg_next(sg);
Matthew Aulda5c081662017-10-06 23:18:18 +01002462 }
Imre Deak90797e62013-02-18 19:28:03 +02002463 st->nents++;
2464 sg_set_page(sg, page, PAGE_SIZE, 0);
2465 } else {
2466 sg->length += PAGE_SIZE;
2467 }
2468 last_pfn = page_to_pfn(page);
Daniel Vetter3bbbe702013-10-07 17:15:45 -03002469
2470 /* Check that the i965g/gm workaround works. */
2471 WARN_ON((gfp & __GFP_DMA32) && (last_pfn >= 0x00100000UL));
Eric Anholt673a3942008-07-30 12:06:12 -07002472 }
Matthew Aulda5c081662017-10-06 23:18:18 +01002473 if (sg) { /* loop terminated early; short sg table */
Matthew Auld84e89782017-10-09 12:00:24 +01002474 sg_page_sizes |= sg->length;
Konrad Rzeszutek Wilk426729d2013-06-24 11:47:48 -04002475 sg_mark_end(sg);
Matthew Aulda5c081662017-10-06 23:18:18 +01002476 }
Chris Wilson74ce6b62012-10-19 15:51:06 +01002477
Tvrtko Ursulin0c40ce12016-11-09 15:13:43 +00002478 /* Trim unused sg entries to avoid wasting memory. */
2479 i915_sg_trim(st);
2480
Chris Wilson03ac84f2016-10-28 13:58:36 +01002481 ret = i915_gem_gtt_prepare_pages(obj, st);
Chris Wilsond766ef52016-12-19 12:43:45 +00002482 if (ret) {
Chris Wilsone0ff7a72018-09-03 09:33:36 +01002483 /*
2484 * DMA remapping failed? One possible cause is that
Chris Wilsond766ef52016-12-19 12:43:45 +00002485 * it could not reserve enough large entries, asking
2486 * for PAGE_SIZE chunks instead may be helpful.
2487 */
2488 if (max_segment > PAGE_SIZE) {
2489 for_each_sgt_page(page, sgt_iter, st)
2490 put_page(page);
2491 sg_free_table(st);
2492
2493 max_segment = PAGE_SIZE;
2494 goto rebuild_st;
2495 } else {
2496 dev_warn(&dev_priv->drm.pdev->dev,
2497 "Failed to DMA remap %lu pages\n",
2498 page_count);
2499 goto err_pages;
2500 }
2501 }
Imre Deake2273302015-07-09 12:59:05 +03002502
Eric Anholt673a3942008-07-30 12:06:12 -07002503 if (i915_gem_object_needs_bit17_swizzle(obj))
Chris Wilson03ac84f2016-10-28 13:58:36 +01002504 i915_gem_object_do_bit_17_swizzle(obj, st);
Eric Anholt673a3942008-07-30 12:06:12 -07002505
Matthew Auld84e89782017-10-09 12:00:24 +01002506 __i915_gem_object_set_pages(obj, st, sg_page_sizes);
Matthew Auldb91b09e2017-10-06 23:18:17 +01002507
2508 return 0;
Eric Anholt673a3942008-07-30 12:06:12 -07002509
Chris Wilsonb17993b2016-11-14 11:29:30 +00002510err_sg:
Imre Deak90797e62013-02-18 19:28:03 +02002511 sg_mark_end(sg);
Chris Wilsonb17993b2016-11-14 11:29:30 +00002512err_pages:
Kuo-Hsin Yang64e3d122018-11-06 13:23:24 +00002513 mapping_clear_unevictable(mapping);
2514 pagevec_init(&pvec);
2515 for_each_sgt_page(page, sgt_iter, st) {
2516 if (!pagevec_add(&pvec, page))
2517 check_release_pagevec(&pvec);
2518 }
2519 if (pagevec_count(&pvec))
2520 check_release_pagevec(&pvec);
Chris Wilson9da3da62012-06-01 15:20:22 +01002521 sg_free_table(st);
2522 kfree(st);
Chris Wilson0820baf2014-03-25 13:23:03 +00002523
Chris Wilsone0ff7a72018-09-03 09:33:36 +01002524 /*
2525 * shmemfs first checks if there is enough memory to allocate the page
Chris Wilson0820baf2014-03-25 13:23:03 +00002526 * and reports ENOSPC should there be insufficient, along with the usual
2527 * ENOMEM for a genuine allocation failure.
2528 *
2529 * We use ENOSPC in our driver to mean that we have run out of aperture
2530 * space and so want to translate the error from shmemfs back to our
2531 * usual understanding of ENOMEM.
2532 */
Imre Deake2273302015-07-09 12:59:05 +03002533 if (ret == -ENOSPC)
2534 ret = -ENOMEM;
2535
Matthew Auldb91b09e2017-10-06 23:18:17 +01002536 return ret;
Chris Wilson03ac84f2016-10-28 13:58:36 +01002537}
2538
2539void __i915_gem_object_set_pages(struct drm_i915_gem_object *obj,
Matthew Aulda5c081662017-10-06 23:18:18 +01002540 struct sg_table *pages,
Matthew Auld84e89782017-10-09 12:00:24 +01002541 unsigned int sg_page_sizes)
Chris Wilson03ac84f2016-10-28 13:58:36 +01002542{
Matthew Aulda5c081662017-10-06 23:18:18 +01002543 struct drm_i915_private *i915 = to_i915(obj->base.dev);
2544 unsigned long supported = INTEL_INFO(i915)->page_sizes;
2545 int i;
2546
Chris Wilson1233e2d2016-10-28 13:58:37 +01002547 lockdep_assert_held(&obj->mm.lock);
Chris Wilson03ac84f2016-10-28 13:58:36 +01002548
2549 obj->mm.get_page.sg_pos = pages->sgl;
2550 obj->mm.get_page.sg_idx = 0;
2551
2552 obj->mm.pages = pages;
Chris Wilson2c3a3f42016-11-04 10:30:01 +00002553
2554 if (i915_gem_object_is_tiled(obj) &&
Chris Wilsonf2123812017-10-16 12:40:37 +01002555 i915->quirks & QUIRK_PIN_SWIZZLED_PAGES) {
Chris Wilson2c3a3f42016-11-04 10:30:01 +00002556 GEM_BUG_ON(obj->mm.quirked);
2557 __i915_gem_object_pin_pages(obj);
2558 obj->mm.quirked = true;
2559 }
Matthew Aulda5c081662017-10-06 23:18:18 +01002560
Matthew Auld84e89782017-10-09 12:00:24 +01002561 GEM_BUG_ON(!sg_page_sizes);
2562 obj->mm.page_sizes.phys = sg_page_sizes;
Matthew Aulda5c081662017-10-06 23:18:18 +01002563
2564 /*
Matthew Auld84e89782017-10-09 12:00:24 +01002565 * Calculate the supported page-sizes which fit into the given
2566 * sg_page_sizes. This will give us the page-sizes which we may be able
2567 * to use opportunistically when later inserting into the GTT. For
2568 * example if phys=2G, then in theory we should be able to use 1G, 2M,
2569 * 64K or 4K pages, although in practice this will depend on a number of
2570 * other factors.
Matthew Aulda5c081662017-10-06 23:18:18 +01002571 */
2572 obj->mm.page_sizes.sg = 0;
2573 for_each_set_bit(i, &supported, ilog2(I915_GTT_MAX_PAGE_SIZE) + 1) {
2574 if (obj->mm.page_sizes.phys & ~0u << i)
2575 obj->mm.page_sizes.sg |= BIT(i);
2576 }
Matthew Aulda5c081662017-10-06 23:18:18 +01002577 GEM_BUG_ON(!HAS_PAGE_SIZES(i915, obj->mm.page_sizes.sg));
Chris Wilsonf2123812017-10-16 12:40:37 +01002578
2579 spin_lock(&i915->mm.obj_lock);
2580 list_add(&obj->mm.link, &i915->mm.unbound_list);
2581 spin_unlock(&i915->mm.obj_lock);
Chris Wilson03ac84f2016-10-28 13:58:36 +01002582}
2583
2584static int ____i915_gem_object_get_pages(struct drm_i915_gem_object *obj)
2585{
Matthew Auldb91b09e2017-10-06 23:18:17 +01002586 int err;
Chris Wilson03ac84f2016-10-28 13:58:36 +01002587
2588 if (unlikely(obj->mm.madv != I915_MADV_WILLNEED)) {
2589 DRM_DEBUG("Attempting to obtain a purgeable object\n");
2590 return -EFAULT;
2591 }
2592
Matthew Auldb91b09e2017-10-06 23:18:17 +01002593 err = obj->ops->get_pages(obj);
Matthew Auldb65a9b92017-12-18 10:38:55 +00002594 GEM_BUG_ON(!err && !i915_gem_object_has_pages(obj));
Chris Wilson03ac84f2016-10-28 13:58:36 +01002595
Matthew Auldb91b09e2017-10-06 23:18:17 +01002596 return err;
Eric Anholt673a3942008-07-30 12:06:12 -07002597}
2598
Chris Wilson37e680a2012-06-07 15:38:42 +01002599/* Ensure that the associated pages are gathered from the backing storage
Chris Wilson1233e2d2016-10-28 13:58:37 +01002600 * and pinned into our object. i915_gem_object_pin_pages() may be called
Chris Wilson37e680a2012-06-07 15:38:42 +01002601 * multiple times before they are released by a single call to
Chris Wilson1233e2d2016-10-28 13:58:37 +01002602 * i915_gem_object_unpin_pages() - once the pages are no longer referenced
Chris Wilson37e680a2012-06-07 15:38:42 +01002603 * either as a result of memory pressure (reaping pages under the shrinker)
2604 * or as the object is itself released.
2605 */
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002606int __i915_gem_object_get_pages(struct drm_i915_gem_object *obj)
Chris Wilson37e680a2012-06-07 15:38:42 +01002607{
Chris Wilson03ac84f2016-10-28 13:58:36 +01002608 int err;
Chris Wilson37e680a2012-06-07 15:38:42 +01002609
Chris Wilson1233e2d2016-10-28 13:58:37 +01002610 err = mutex_lock_interruptible(&obj->mm.lock);
2611 if (err)
2612 return err;
Chris Wilson4c7d62c2016-10-28 13:58:32 +01002613
Chris Wilsonf1fa4f42017-10-13 21:26:13 +01002614 if (unlikely(!i915_gem_object_has_pages(obj))) {
Chris Wilson88c880b2017-09-06 14:52:20 +01002615 GEM_BUG_ON(i915_gem_object_has_pinned_pages(obj));
2616
Chris Wilson2c3a3f42016-11-04 10:30:01 +00002617 err = ____i915_gem_object_get_pages(obj);
2618 if (err)
2619 goto unlock;
2620
2621 smp_mb__before_atomic();
Chris Wilson1233e2d2016-10-28 13:58:37 +01002622 }
Chris Wilson2c3a3f42016-11-04 10:30:01 +00002623 atomic_inc(&obj->mm.pages_pin_count);
Chris Wilson43e28f02013-01-08 10:53:09 +00002624
Chris Wilson1233e2d2016-10-28 13:58:37 +01002625unlock:
2626 mutex_unlock(&obj->mm.lock);
Chris Wilson03ac84f2016-10-28 13:58:36 +01002627 return err;
Eric Anholt673a3942008-07-30 12:06:12 -07002628}
2629
Dave Gordondd6034c2016-05-20 11:54:04 +01002630/* The 'mapping' part of i915_gem_object_pin_map() below */
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002631static void *i915_gem_object_map(const struct drm_i915_gem_object *obj,
2632 enum i915_map_type type)
Dave Gordondd6034c2016-05-20 11:54:04 +01002633{
2634 unsigned long n_pages = obj->base.size >> PAGE_SHIFT;
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002635 struct sg_table *sgt = obj->mm.pages;
Dave Gordon85d12252016-05-20 11:54:06 +01002636 struct sgt_iter sgt_iter;
2637 struct page *page;
Dave Gordonb338fa42016-05-20 11:54:05 +01002638 struct page *stack_pages[32];
2639 struct page **pages = stack_pages;
Dave Gordondd6034c2016-05-20 11:54:04 +01002640 unsigned long i = 0;
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002641 pgprot_t pgprot;
Dave Gordondd6034c2016-05-20 11:54:04 +01002642 void *addr;
2643
2644 /* A single page can always be kmapped */
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002645 if (n_pages == 1 && type == I915_MAP_WB)
Dave Gordondd6034c2016-05-20 11:54:04 +01002646 return kmap(sg_page(sgt->sgl));
2647
Dave Gordonb338fa42016-05-20 11:54:05 +01002648 if (n_pages > ARRAY_SIZE(stack_pages)) {
2649 /* Too big for stack -- allocate temporary array instead */
Michal Hocko0ee931c2017-09-13 16:28:29 -07002650 pages = kvmalloc_array(n_pages, sizeof(*pages), GFP_KERNEL);
Dave Gordonb338fa42016-05-20 11:54:05 +01002651 if (!pages)
2652 return NULL;
2653 }
Dave Gordondd6034c2016-05-20 11:54:04 +01002654
Dave Gordon85d12252016-05-20 11:54:06 +01002655 for_each_sgt_page(page, sgt_iter, sgt)
2656 pages[i++] = page;
Dave Gordondd6034c2016-05-20 11:54:04 +01002657
2658 /* Check that we have the expected number of pages */
2659 GEM_BUG_ON(i != n_pages);
2660
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002661 switch (type) {
Chris Wilsona575c672017-08-28 11:46:31 +01002662 default:
2663 MISSING_CASE(type);
2664 /* fallthrough to use PAGE_KERNEL anyway */
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002665 case I915_MAP_WB:
2666 pgprot = PAGE_KERNEL;
2667 break;
2668 case I915_MAP_WC:
2669 pgprot = pgprot_writecombine(PAGE_KERNEL_IO);
2670 break;
2671 }
2672 addr = vmap(pages, n_pages, 0, pgprot);
Dave Gordondd6034c2016-05-20 11:54:04 +01002673
Dave Gordonb338fa42016-05-20 11:54:05 +01002674 if (pages != stack_pages)
Michal Hocko20981052017-05-17 14:23:12 +02002675 kvfree(pages);
Dave Gordondd6034c2016-05-20 11:54:04 +01002676
2677 return addr;
2678}
2679
2680/* get, pin, and map the pages of the object into kernel space */
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002681void *i915_gem_object_pin_map(struct drm_i915_gem_object *obj,
2682 enum i915_map_type type)
Chris Wilson0a798eb2016-04-08 12:11:11 +01002683{
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002684 enum i915_map_type has_type;
2685 bool pinned;
2686 void *ptr;
Chris Wilson0a798eb2016-04-08 12:11:11 +01002687 int ret;
2688
Tina Zhanga03f3952017-11-14 10:25:13 +00002689 if (unlikely(!i915_gem_object_has_struct_page(obj)))
2690 return ERR_PTR(-ENXIO);
Chris Wilson0a798eb2016-04-08 12:11:11 +01002691
Chris Wilson1233e2d2016-10-28 13:58:37 +01002692 ret = mutex_lock_interruptible(&obj->mm.lock);
Chris Wilson0a798eb2016-04-08 12:11:11 +01002693 if (ret)
2694 return ERR_PTR(ret);
2695
Chris Wilsona575c672017-08-28 11:46:31 +01002696 pinned = !(type & I915_MAP_OVERRIDE);
2697 type &= ~I915_MAP_OVERRIDE;
2698
Chris Wilson1233e2d2016-10-28 13:58:37 +01002699 if (!atomic_inc_not_zero(&obj->mm.pages_pin_count)) {
Chris Wilsonf1fa4f42017-10-13 21:26:13 +01002700 if (unlikely(!i915_gem_object_has_pages(obj))) {
Chris Wilson88c880b2017-09-06 14:52:20 +01002701 GEM_BUG_ON(i915_gem_object_has_pinned_pages(obj));
2702
Chris Wilson2c3a3f42016-11-04 10:30:01 +00002703 ret = ____i915_gem_object_get_pages(obj);
2704 if (ret)
2705 goto err_unlock;
Chris Wilson1233e2d2016-10-28 13:58:37 +01002706
Chris Wilson2c3a3f42016-11-04 10:30:01 +00002707 smp_mb__before_atomic();
2708 }
2709 atomic_inc(&obj->mm.pages_pin_count);
Chris Wilson1233e2d2016-10-28 13:58:37 +01002710 pinned = false;
2711 }
Chris Wilsonf1fa4f42017-10-13 21:26:13 +01002712 GEM_BUG_ON(!i915_gem_object_has_pages(obj));
Chris Wilson0a798eb2016-04-08 12:11:11 +01002713
Chris Wilson0ce81782017-05-17 13:09:59 +01002714 ptr = page_unpack_bits(obj->mm.mapping, &has_type);
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002715 if (ptr && has_type != type) {
2716 if (pinned) {
2717 ret = -EBUSY;
Chris Wilson1233e2d2016-10-28 13:58:37 +01002718 goto err_unpin;
Chris Wilson0a798eb2016-04-08 12:11:11 +01002719 }
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002720
2721 if (is_vmalloc_addr(ptr))
2722 vunmap(ptr);
2723 else
2724 kunmap(kmap_to_page(ptr));
2725
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002726 ptr = obj->mm.mapping = NULL;
Chris Wilson0a798eb2016-04-08 12:11:11 +01002727 }
2728
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002729 if (!ptr) {
2730 ptr = i915_gem_object_map(obj, type);
2731 if (!ptr) {
2732 ret = -ENOMEM;
Chris Wilson1233e2d2016-10-28 13:58:37 +01002733 goto err_unpin;
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002734 }
2735
Chris Wilson0ce81782017-05-17 13:09:59 +01002736 obj->mm.mapping = page_pack_bits(ptr, type);
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002737 }
2738
Chris Wilson1233e2d2016-10-28 13:58:37 +01002739out_unlock:
2740 mutex_unlock(&obj->mm.lock);
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002741 return ptr;
2742
Chris Wilson1233e2d2016-10-28 13:58:37 +01002743err_unpin:
2744 atomic_dec(&obj->mm.pages_pin_count);
2745err_unlock:
2746 ptr = ERR_PTR(ret);
2747 goto out_unlock;
Chris Wilson0a798eb2016-04-08 12:11:11 +01002748}
2749
Chris Wilson7c55e2c2017-03-07 12:03:38 +00002750static int
2751i915_gem_object_pwrite_gtt(struct drm_i915_gem_object *obj,
2752 const struct drm_i915_gem_pwrite *arg)
2753{
2754 struct address_space *mapping = obj->base.filp->f_mapping;
2755 char __user *user_data = u64_to_user_ptr(arg->data_ptr);
2756 u64 remain, offset;
2757 unsigned int pg;
2758
2759 /* Before we instantiate/pin the backing store for our use, we
2760 * can prepopulate the shmemfs filp efficiently using a write into
2761 * the pagecache. We avoid the penalty of instantiating all the
2762 * pages, important if the user is just writing to a few and never
2763 * uses the object on the GPU, and using a direct write into shmemfs
2764 * allows it to avoid the cost of retrieving a page (either swapin
2765 * or clearing-before-use) before it is overwritten.
2766 */
Chris Wilsonf1fa4f42017-10-13 21:26:13 +01002767 if (i915_gem_object_has_pages(obj))
Chris Wilson7c55e2c2017-03-07 12:03:38 +00002768 return -ENODEV;
2769
Chris Wilsona6d65e42017-10-16 21:27:32 +01002770 if (obj->mm.madv != I915_MADV_WILLNEED)
2771 return -EFAULT;
2772
Chris Wilson7c55e2c2017-03-07 12:03:38 +00002773 /* Before the pages are instantiated the object is treated as being
2774 * in the CPU domain. The pages will be clflushed as required before
2775 * use, and we can freely write into the pages directly. If userspace
2776 * races pwrite with any other operation; corruption will ensue -
2777 * that is userspace's prerogative!
2778 */
2779
2780 remain = arg->size;
2781 offset = arg->offset;
2782 pg = offset_in_page(offset);
2783
2784 do {
2785 unsigned int len, unwritten;
2786 struct page *page;
2787 void *data, *vaddr;
2788 int err;
2789
2790 len = PAGE_SIZE - pg;
2791 if (len > remain)
2792 len = remain;
2793
2794 err = pagecache_write_begin(obj->base.filp, mapping,
2795 offset, len, 0,
2796 &page, &data);
2797 if (err < 0)
2798 return err;
2799
2800 vaddr = kmap(page);
2801 unwritten = copy_from_user(vaddr + pg, user_data, len);
2802 kunmap(page);
2803
2804 err = pagecache_write_end(obj->base.filp, mapping,
2805 offset, len, len - unwritten,
2806 page, data);
2807 if (err < 0)
2808 return err;
2809
2810 if (unwritten)
2811 return -EFAULT;
2812
2813 remain -= len;
2814 user_data += len;
2815 offset += len;
2816 pg = 0;
2817 } while (remain);
2818
2819 return 0;
2820}
2821
Chris Wilson85474442019-01-29 18:54:50 +00002822static bool match_ring(struct i915_request *rq)
2823{
2824 struct drm_i915_private *dev_priv = rq->i915;
2825 u32 ring = I915_READ(RING_START(rq->engine->mmio_base));
2826
2827 return ring == i915_ggtt_offset(rq->ring->vma);
2828}
2829
Chris Wilsone61e0f52018-02-21 09:56:36 +00002830struct i915_request *
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00002831i915_gem_find_active_request(struct intel_engine_cs *engine)
Chris Wilson9375e442010-09-19 12:21:28 +01002832{
Chris Wilsone61e0f52018-02-21 09:56:36 +00002833 struct i915_request *request, *active = NULL;
Chris Wilson754c9fd2017-02-23 07:44:14 +00002834 unsigned long flags;
Mika Kuoppalaaa60c662013-06-12 15:13:20 +03002835
Chris Wilsoncc7cc532018-05-29 14:29:18 +01002836 /*
2837 * We are called by the error capture, reset and to dump engine
2838 * state at random points in time. In particular, note that neither is
2839 * crucially ordered with an interrupt. After a hang, the GPU is dead
2840 * and we assume that no more writes can happen (we waited long enough
2841 * for all writes that were in transaction to be flushed) - adding an
Chris Wilsonf69a02c2016-07-01 17:23:16 +01002842 * extra delay for a recent interrupt is pointless. Hence, we do
2843 * not need an engine->irq_seqno_barrier() before the seqno reads.
Chris Wilsoncc7cc532018-05-29 14:29:18 +01002844 * At all other times, we must assume the GPU is still running, but
2845 * we only care about the snapshot of this moment.
Chris Wilsonf69a02c2016-07-01 17:23:16 +01002846 */
Chris Wilsona89d1f92018-05-02 17:38:39 +01002847 spin_lock_irqsave(&engine->timeline.lock, flags);
2848 list_for_each_entry(request, &engine->timeline.requests, link) {
Chris Wilson5013eb82019-01-28 18:18:11 +00002849 if (i915_request_completed(request))
Chris Wilson4db080f2013-12-04 11:37:09 +00002850 continue;
Mika Kuoppalaaa60c662013-06-12 15:13:20 +03002851
Chris Wilson85474442019-01-29 18:54:50 +00002852 if (!i915_request_started(request))
2853 break;
2854
2855 /* More than one preemptible request may match! */
2856 if (!match_ring(request))
2857 break;
2858
Chris Wilson754c9fd2017-02-23 07:44:14 +00002859 active = request;
2860 break;
2861 }
Chris Wilsona89d1f92018-05-02 17:38:39 +01002862 spin_unlock_irqrestore(&engine->timeline.lock, flags);
Chris Wilson754c9fd2017-02-23 07:44:14 +00002863
2864 return active;
Mika Kuoppalab6b0fac2014-01-30 19:04:43 +02002865}
2866
Daniel Vetter75ef9da2010-08-21 00:25:16 +02002867static void
Eric Anholt673a3942008-07-30 12:06:12 -07002868i915_gem_retire_work_handler(struct work_struct *work)
2869{
Chris Wilsonb29c19b2013-09-25 17:34:56 +01002870 struct drm_i915_private *dev_priv =
Chris Wilson67d97da2016-07-04 08:08:31 +01002871 container_of(work, typeof(*dev_priv), gt.retire_work.work);
Chris Wilson91c8a322016-07-05 10:40:23 +01002872 struct drm_device *dev = &dev_priv->drm;
Eric Anholt673a3942008-07-30 12:06:12 -07002873
Chris Wilson891b48c2010-09-29 12:26:37 +01002874 /* Come back later if the device is busy... */
Chris Wilsonb29c19b2013-09-25 17:34:56 +01002875 if (mutex_trylock(&dev->struct_mutex)) {
Chris Wilsone61e0f52018-02-21 09:56:36 +00002876 i915_retire_requests(dev_priv);
Chris Wilsonb29c19b2013-09-25 17:34:56 +01002877 mutex_unlock(&dev->struct_mutex);
2878 }
Chris Wilson67d97da2016-07-04 08:08:31 +01002879
Chris Wilson88923042018-01-29 14:41:04 +00002880 /*
2881 * Keep the retire handler running until we are finally idle.
Chris Wilson67d97da2016-07-04 08:08:31 +01002882 * We do not need to do this test under locking as in the worst-case
2883 * we queue the retire worker once too often.
2884 */
Chris Wilson88923042018-01-29 14:41:04 +00002885 if (READ_ONCE(dev_priv->gt.awake))
Chris Wilson67d97da2016-07-04 08:08:31 +01002886 queue_delayed_work(dev_priv->wq,
2887 &dev_priv->gt.retire_work,
Chris Wilsonbcb45082012-10-05 17:02:57 +01002888 round_jiffies_up_relative(HZ));
Chris Wilsonb29c19b2013-09-25 17:34:56 +01002889}
Chris Wilson891b48c2010-09-29 12:26:37 +01002890
Chris Wilson84a10742018-01-24 11:36:08 +00002891static void shrink_caches(struct drm_i915_private *i915)
2892{
2893 /*
2894 * kmem_cache_shrink() discards empty slabs and reorders partially
2895 * filled slabs to prioritise allocating from the mostly full slabs,
2896 * with the aim of reducing fragmentation.
2897 */
Chris Wilson84a10742018-01-24 11:36:08 +00002898 kmem_cache_shrink(i915->luts);
2899 kmem_cache_shrink(i915->vmas);
2900 kmem_cache_shrink(i915->objects);
Chris Wilson32eb6bc2019-02-28 10:20:33 +00002901
2902 i915_globals_park();
Chris Wilson84a10742018-01-24 11:36:08 +00002903}
2904
2905struct sleep_rcu_work {
2906 union {
2907 struct rcu_head rcu;
2908 struct work_struct work;
2909 };
2910 struct drm_i915_private *i915;
2911 unsigned int epoch;
2912};
2913
2914static inline bool
2915same_epoch(struct drm_i915_private *i915, unsigned int epoch)
2916{
2917 /*
2918 * There is a small chance that the epoch wrapped since we started
2919 * sleeping. If we assume that epoch is at least a u32, then it will
2920 * take at least 2^32 * 100ms for it to wrap, or about 326 years.
2921 */
2922 return epoch == READ_ONCE(i915->gt.epoch);
2923}
2924
2925static void __sleep_work(struct work_struct *work)
2926{
2927 struct sleep_rcu_work *s = container_of(work, typeof(*s), work);
2928 struct drm_i915_private *i915 = s->i915;
2929 unsigned int epoch = s->epoch;
2930
2931 kfree(s);
2932 if (same_epoch(i915, epoch))
2933 shrink_caches(i915);
2934}
2935
2936static void __sleep_rcu(struct rcu_head *rcu)
2937{
2938 struct sleep_rcu_work *s = container_of(rcu, typeof(*s), rcu);
2939 struct drm_i915_private *i915 = s->i915;
2940
Chris Wilsona1db9c52018-11-08 09:21:01 +00002941 destroy_rcu_head(&s->rcu);
2942
Chris Wilson84a10742018-01-24 11:36:08 +00002943 if (same_epoch(i915, s->epoch)) {
2944 INIT_WORK(&s->work, __sleep_work);
2945 queue_work(i915->wq, &s->work);
2946 } else {
2947 kfree(s);
2948 }
2949}
2950
Chris Wilson5427f202017-10-23 22:32:34 +01002951static inline bool
2952new_requests_since_last_retire(const struct drm_i915_private *i915)
2953{
2954 return (READ_ONCE(i915->gt.active_requests) ||
2955 work_pending(&i915->gt.idle_work.work));
2956}
2957
Chris Wilson1934f5de2018-05-31 23:40:57 +01002958static void assert_kernel_context_is_current(struct drm_i915_private *i915)
2959{
2960 struct intel_engine_cs *engine;
2961 enum intel_engine_id id;
2962
Chris Wilsonc41166f2019-02-20 14:56:37 +00002963 if (i915_reset_failed(i915))
Chris Wilson1934f5de2018-05-31 23:40:57 +01002964 return;
2965
2966 GEM_BUG_ON(i915->gt.active_requests);
2967 for_each_engine(engine, i915, id) {
Chris Wilson21950ee2019-02-05 13:00:05 +00002968 GEM_BUG_ON(__i915_active_request_peek(&engine->timeline.last_request));
Chris Wilson1934f5de2018-05-31 23:40:57 +01002969 GEM_BUG_ON(engine->last_retired_context !=
2970 to_intel_context(i915->kernel_context, engine));
2971 }
2972}
2973
Chris Wilsonb29c19b2013-09-25 17:34:56 +01002974static void
2975i915_gem_idle_work_handler(struct work_struct *work)
2976{
2977 struct drm_i915_private *dev_priv =
Chris Wilson67d97da2016-07-04 08:08:31 +01002978 container_of(work, typeof(*dev_priv), gt.idle_work.work);
Chris Wilson84a10742018-01-24 11:36:08 +00002979 unsigned int epoch = I915_EPOCH_INVALID;
Chris Wilson67d97da2016-07-04 08:08:31 +01002980 bool rearm_hangcheck;
2981
2982 if (!READ_ONCE(dev_priv->gt.awake))
2983 return;
2984
Chris Wilson4dfacb02018-05-31 09:22:43 +01002985 if (READ_ONCE(dev_priv->gt.active_requests))
2986 return;
2987
2988 /*
2989 * Flush out the last user context, leaving only the pinned
2990 * kernel context resident. When we are idling on the kernel_context,
2991 * no more new requests (with a context switch) are emitted and we
2992 * can finally rest. A consequence is that the idle work handler is
2993 * always called at least twice before idling (and if the system is
2994 * idle that implies a round trip through the retire worker).
2995 */
2996 mutex_lock(&dev_priv->drm.struct_mutex);
2997 i915_gem_switch_to_kernel_context(dev_priv);
2998 mutex_unlock(&dev_priv->drm.struct_mutex);
2999
3000 GEM_TRACE("active_requests=%d (after switch-to-kernel-context)\n",
3001 READ_ONCE(dev_priv->gt.active_requests));
3002
Imre Deak0cb56702016-11-07 11:20:04 +02003003 /*
3004 * Wait for last execlists context complete, but bail out in case a
Chris Wilsonffed7bd2018-03-01 10:33:38 +00003005 * new request is submitted. As we don't trust the hardware, we
3006 * continue on if the wait times out. This is necessary to allow
3007 * the machine to suspend even if the hardware dies, and we will
3008 * try to recover in resume (after depriving the hardware of power,
3009 * it may be in a better mmod).
Imre Deak0cb56702016-11-07 11:20:04 +02003010 */
Chris Wilsonffed7bd2018-03-01 10:33:38 +00003011 __wait_for(if (new_requests_since_last_retire(dev_priv)) return,
3012 intel_engines_are_idle(dev_priv),
3013 I915_IDLE_ENGINES_TIMEOUT * 1000,
3014 10, 500);
Chris Wilson67d97da2016-07-04 08:08:31 +01003015
3016 rearm_hangcheck =
3017 cancel_delayed_work_sync(&dev_priv->gpu_error.hangcheck_work);
3018
Chris Wilson5427f202017-10-23 22:32:34 +01003019 if (!mutex_trylock(&dev_priv->drm.struct_mutex)) {
Chris Wilson67d97da2016-07-04 08:08:31 +01003020 /* Currently busy, come back later */
3021 mod_delayed_work(dev_priv->wq,
3022 &dev_priv->gt.idle_work,
3023 msecs_to_jiffies(50));
3024 goto out_rearm;
3025 }
3026
Imre Deak93c97dc2016-11-07 11:20:03 +02003027 /*
3028 * New request retired after this work handler started, extend active
3029 * period until next instance of the work.
3030 */
Chris Wilson5427f202017-10-23 22:32:34 +01003031 if (new_requests_since_last_retire(dev_priv))
Imre Deak93c97dc2016-11-07 11:20:03 +02003032 goto out_unlock;
3033
Chris Wilsone4d20062018-04-06 16:51:44 +01003034 epoch = __i915_gem_park(dev_priv);
Chris Wilsonff320d62017-10-23 22:32:35 +01003035
Chris Wilson1934f5de2018-05-31 23:40:57 +01003036 assert_kernel_context_is_current(dev_priv);
3037
Chris Wilson67d97da2016-07-04 08:08:31 +01003038 rearm_hangcheck = false;
Chris Wilson67d97da2016-07-04 08:08:31 +01003039out_unlock:
Chris Wilson5427f202017-10-23 22:32:34 +01003040 mutex_unlock(&dev_priv->drm.struct_mutex);
Chris Wilson35c94182015-04-07 16:20:37 +01003041
Chris Wilson67d97da2016-07-04 08:08:31 +01003042out_rearm:
3043 if (rearm_hangcheck) {
3044 GEM_BUG_ON(!dev_priv->gt.awake);
3045 i915_queue_hangcheck(dev_priv);
Chris Wilson35c94182015-04-07 16:20:37 +01003046 }
Chris Wilson84a10742018-01-24 11:36:08 +00003047
3048 /*
3049 * When we are idle, it is an opportune time to reap our caches.
3050 * However, we have many objects that utilise RCU and the ordered
3051 * i915->wq that this work is executing on. To try and flush any
3052 * pending frees now we are idle, we first wait for an RCU grace
3053 * period, and then queue a task (that will run last on the wq) to
3054 * shrink and re-optimize the caches.
3055 */
3056 if (same_epoch(dev_priv, epoch)) {
3057 struct sleep_rcu_work *s = kmalloc(sizeof(*s), GFP_KERNEL);
3058 if (s) {
Chris Wilsona1db9c52018-11-08 09:21:01 +00003059 init_rcu_head(&s->rcu);
Chris Wilson84a10742018-01-24 11:36:08 +00003060 s->i915 = dev_priv;
3061 s->epoch = epoch;
3062 call_rcu(&s->rcu, __sleep_rcu);
3063 }
3064 }
Eric Anholt673a3942008-07-30 12:06:12 -07003065}
3066
Chris Wilsonb1f788c2016-08-04 07:52:45 +01003067void i915_gem_close_object(struct drm_gem_object *gem, struct drm_file *file)
3068{
Chris Wilsond1b48c12017-08-16 09:52:08 +01003069 struct drm_i915_private *i915 = to_i915(gem->dev);
Chris Wilsonb1f788c2016-08-04 07:52:45 +01003070 struct drm_i915_gem_object *obj = to_intel_bo(gem);
3071 struct drm_i915_file_private *fpriv = file->driver_priv;
Chris Wilsond1b48c12017-08-16 09:52:08 +01003072 struct i915_lut_handle *lut, *ln;
Chris Wilsonb1f788c2016-08-04 07:52:45 +01003073
Chris Wilsond1b48c12017-08-16 09:52:08 +01003074 mutex_lock(&i915->drm.struct_mutex);
3075
3076 list_for_each_entry_safe(lut, ln, &obj->lut_list, obj_link) {
3077 struct i915_gem_context *ctx = lut->ctx;
3078 struct i915_vma *vma;
3079
Chris Wilson432295d2017-08-22 12:05:15 +01003080 GEM_BUG_ON(ctx->file_priv == ERR_PTR(-EBADF));
Chris Wilsond1b48c12017-08-16 09:52:08 +01003081 if (ctx->file_priv != fpriv)
3082 continue;
3083
3084 vma = radix_tree_delete(&ctx->handles_vma, lut->handle);
Chris Wilson3ffff012017-08-22 12:05:17 +01003085 GEM_BUG_ON(vma->obj != obj);
3086
3087 /* We allow the process to have multiple handles to the same
3088 * vma, in the same fd namespace, by virtue of flink/open.
3089 */
3090 GEM_BUG_ON(!vma->open_count);
3091 if (!--vma->open_count && !i915_vma_is_ggtt(vma))
Chris Wilsonb1f788c2016-08-04 07:52:45 +01003092 i915_vma_close(vma);
Chris Wilsonf8a7fde2016-10-28 13:58:29 +01003093
Chris Wilsond1b48c12017-08-16 09:52:08 +01003094 list_del(&lut->obj_link);
3095 list_del(&lut->ctx_link);
Chris Wilson4ff4b442017-06-16 15:05:16 +01003096
Chris Wilsond1b48c12017-08-16 09:52:08 +01003097 kmem_cache_free(i915->luts, lut);
3098 __i915_gem_object_release_unless_active(obj);
Chris Wilsonf8a7fde2016-10-28 13:58:29 +01003099 }
Chris Wilsond1b48c12017-08-16 09:52:08 +01003100
3101 mutex_unlock(&i915->drm.struct_mutex);
Chris Wilsonb1f788c2016-08-04 07:52:45 +01003102}
3103
Chris Wilsone95433c2016-10-28 13:58:27 +01003104static unsigned long to_wait_timeout(s64 timeout_ns)
3105{
3106 if (timeout_ns < 0)
3107 return MAX_SCHEDULE_TIMEOUT;
3108
3109 if (timeout_ns == 0)
3110 return 0;
3111
3112 return nsecs_to_jiffies_timeout(timeout_ns);
3113}
3114
Ben Widawsky5816d642012-04-11 11:18:19 -07003115/**
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07003116 * i915_gem_wait_ioctl - implements DRM_IOCTL_I915_GEM_WAIT
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01003117 * @dev: drm device pointer
3118 * @data: ioctl data blob
3119 * @file: drm file pointer
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07003120 *
3121 * Returns 0 if successful, else an error is returned with the remaining time in
3122 * the timeout parameter.
3123 * -ETIME: object is still busy after timeout
3124 * -ERESTARTSYS: signal interrupted the wait
3125 * -ENONENT: object doesn't exist
3126 * Also possible, but rare:
Chris Wilsonb8050142017-08-11 11:57:31 +01003127 * -EAGAIN: incomplete, restart syscall
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07003128 * -ENOMEM: damn
3129 * -ENODEV: Internal IRQ fail
3130 * -E?: The add request failed
3131 *
3132 * The wait ioctl with a timeout of 0 reimplements the busy ioctl. With any
3133 * non-zero timeout parameter the wait ioctl will wait for the given number of
3134 * nanoseconds on an object becoming unbusy. Since the wait itself does so
3135 * without holding struct_mutex the object may become re-busied before this
3136 * function completes. A similar but shorter * race condition exists in the busy
3137 * ioctl
3138 */
3139int
3140i915_gem_wait_ioctl(struct drm_device *dev, void *data, struct drm_file *file)
3141{
3142 struct drm_i915_gem_wait *args = data;
3143 struct drm_i915_gem_object *obj;
Chris Wilsone95433c2016-10-28 13:58:27 +01003144 ktime_t start;
3145 long ret;
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07003146
Daniel Vetter11b5d512014-09-29 15:31:26 +02003147 if (args->flags != 0)
3148 return -EINVAL;
3149
Chris Wilson03ac0642016-07-20 13:31:51 +01003150 obj = i915_gem_object_lookup(file, args->bo_handle);
Chris Wilson033d5492016-08-05 10:14:17 +01003151 if (!obj)
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07003152 return -ENOENT;
Chris Wilson033d5492016-08-05 10:14:17 +01003153
Chris Wilsone95433c2016-10-28 13:58:27 +01003154 start = ktime_get();
3155
3156 ret = i915_gem_object_wait(obj,
Chris Wilsone9eaf822018-10-01 15:47:55 +01003157 I915_WAIT_INTERRUPTIBLE |
3158 I915_WAIT_PRIORITY |
3159 I915_WAIT_ALL,
Chris Wilson62eb3c22019-02-13 09:25:04 +00003160 to_wait_timeout(args->timeout_ns));
Chris Wilsone95433c2016-10-28 13:58:27 +01003161
3162 if (args->timeout_ns > 0) {
3163 args->timeout_ns -= ktime_to_ns(ktime_sub(ktime_get(), start));
3164 if (args->timeout_ns < 0)
3165 args->timeout_ns = 0;
Chris Wilsonc1d20612017-02-16 12:54:41 +00003166
3167 /*
3168 * Apparently ktime isn't accurate enough and occasionally has a
3169 * bit of mismatch in the jiffies<->nsecs<->ktime loop. So patch
3170 * things up to make the test happy. We allow up to 1 jiffy.
3171 *
3172 * This is a regression from the timespec->ktime conversion.
3173 */
3174 if (ret == -ETIME && !nsecs_to_jiffies(args->timeout_ns))
3175 args->timeout_ns = 0;
Chris Wilsonb8050142017-08-11 11:57:31 +01003176
3177 /* Asked to wait beyond the jiffie/scheduler precision? */
3178 if (ret == -ETIME && args->timeout_ns)
3179 ret = -EAGAIN;
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07003180 }
3181
Chris Wilsonf0cd5182016-10-28 13:58:43 +01003182 i915_gem_object_put(obj);
John Harrisonff865882014-11-24 18:49:28 +00003183 return ret;
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07003184}
3185
Chris Wilson25112b62017-03-30 15:50:39 +01003186static int wait_for_engines(struct drm_i915_private *i915)
3187{
Chris Wilsonee42c002017-12-11 19:41:34 +00003188 if (wait_for(intel_engines_are_idle(i915), I915_IDLE_ENGINES_TIMEOUT)) {
Chris Wilson59e4b192017-12-11 19:41:35 +00003189 dev_err(i915->drm.dev,
3190 "Failed to idle engines, declaring wedged!\n");
Chris Wilson629820f2018-03-09 10:11:14 +00003191 GEM_TRACE_DUMP();
Chris Wilsoncad99462017-08-26 12:09:33 +01003192 i915_gem_set_wedged(i915);
3193 return -EIO;
Chris Wilson25112b62017-03-30 15:50:39 +01003194 }
3195
3196 return 0;
3197}
3198
Chris Wilson1e345562019-01-28 10:23:56 +00003199static long
3200wait_for_timelines(struct drm_i915_private *i915,
3201 unsigned int flags, long timeout)
3202{
3203 struct i915_gt_timelines *gt = &i915->gt.timelines;
3204 struct i915_timeline *tl;
3205
3206 if (!READ_ONCE(i915->gt.active_requests))
3207 return timeout;
3208
3209 mutex_lock(&gt->mutex);
Chris Wilson9407d3b2019-01-28 18:18:12 +00003210 list_for_each_entry(tl, &gt->active_list, link) {
Chris Wilson1e345562019-01-28 10:23:56 +00003211 struct i915_request *rq;
3212
Chris Wilson21950ee2019-02-05 13:00:05 +00003213 rq = i915_active_request_get_unlocked(&tl->last_request);
Chris Wilson1e345562019-01-28 10:23:56 +00003214 if (!rq)
3215 continue;
3216
3217 mutex_unlock(&gt->mutex);
3218
3219 /*
3220 * "Race-to-idle".
3221 *
3222 * Switching to the kernel context is often used a synchronous
3223 * step prior to idling, e.g. in suspend for flushing all
3224 * current operations to memory before sleeping. These we
3225 * want to complete as quickly as possible to avoid prolonged
3226 * stalls, so allow the gpu to boost to maximum clocks.
3227 */
3228 if (flags & I915_WAIT_FOR_IDLE_BOOST)
Chris Wilson62eb3c22019-02-13 09:25:04 +00003229 gen6_rps_boost(rq);
Chris Wilson1e345562019-01-28 10:23:56 +00003230
3231 timeout = i915_request_wait(rq, flags, timeout);
3232 i915_request_put(rq);
3233 if (timeout < 0)
3234 return timeout;
3235
3236 /* restart after reacquiring the lock */
3237 mutex_lock(&gt->mutex);
Chris Wilson9407d3b2019-01-28 18:18:12 +00003238 tl = list_entry(&gt->active_list, typeof(*tl), link);
Chris Wilson1e345562019-01-28 10:23:56 +00003239 }
3240 mutex_unlock(&gt->mutex);
3241
3242 return timeout;
3243}
3244
Chris Wilsonec625fb2018-07-09 13:20:42 +01003245int i915_gem_wait_for_idle(struct drm_i915_private *i915,
3246 unsigned int flags, long timeout)
Chris Wilson73cb9702016-10-28 13:58:46 +01003247{
Chris Wilsonec625fb2018-07-09 13:20:42 +01003248 GEM_TRACE("flags=%x (%s), timeout=%ld%s\n",
3249 flags, flags & I915_WAIT_LOCKED ? "locked" : "unlocked",
3250 timeout, timeout == MAX_SCHEDULE_TIMEOUT ? " (forever)" : "");
Chris Wilson09a4c022018-05-24 09:11:35 +01003251
Chris Wilson863e9fd2017-05-30 13:13:32 +01003252 /* If the device is asleep, we have no requests outstanding */
3253 if (!READ_ONCE(i915->gt.awake))
3254 return 0;
3255
Chris Wilson1e345562019-01-28 10:23:56 +00003256 timeout = wait_for_timelines(i915, flags, timeout);
3257 if (timeout < 0)
3258 return timeout;
3259
Chris Wilson9caa34a2016-11-11 14:58:08 +00003260 if (flags & I915_WAIT_LOCKED) {
Chris Wilsona89d1f92018-05-02 17:38:39 +01003261 int err;
Chris Wilson9caa34a2016-11-11 14:58:08 +00003262
3263 lockdep_assert_held(&i915->drm.struct_mutex);
3264
Chris Wilsonc1e63f62018-08-08 11:50:59 +01003265 if (GEM_SHOW_DEBUG() && !timeout) {
3266 /* Presume that timeout was non-zero to begin with! */
3267 dev_warn(&i915->drm.pdev->dev,
3268 "Missed idle-completion interrupt!\n");
3269 GEM_TRACE_DUMP();
3270 }
Chris Wilsona61b47f2018-06-27 12:53:34 +01003271
3272 err = wait_for_engines(i915);
3273 if (err)
3274 return err;
3275
Chris Wilsone61e0f52018-02-21 09:56:36 +00003276 i915_retire_requests(i915);
Chris Wilson09a4c022018-05-24 09:11:35 +01003277 GEM_BUG_ON(i915->gt.active_requests);
Chris Wilsona89d1f92018-05-02 17:38:39 +01003278 }
Chris Wilsona61b47f2018-06-27 12:53:34 +01003279
3280 return 0;
Daniel Vetter4df2faf2010-02-19 11:52:00 +01003281}
3282
Chris Wilson5a97bcc2017-02-22 11:40:46 +00003283static void __i915_gem_object_flush_for_display(struct drm_i915_gem_object *obj)
3284{
Chris Wilsone27ab732017-06-15 13:38:49 +01003285 /*
3286 * We manually flush the CPU domain so that we can override and
3287 * force the flush for the display, and perform it asyncrhonously.
3288 */
3289 flush_write_domain(obj, ~I915_GEM_DOMAIN_CPU);
3290 if (obj->cache_dirty)
3291 i915_gem_clflush_object(obj, I915_CLFLUSH_FORCE);
Christian Königc0a51fd2018-02-16 13:43:38 +01003292 obj->write_domain = 0;
Chris Wilson5a97bcc2017-02-22 11:40:46 +00003293}
3294
3295void i915_gem_object_flush_if_display(struct drm_i915_gem_object *obj)
3296{
Chris Wilsonbd3d2252017-10-13 21:26:14 +01003297 if (!READ_ONCE(obj->pin_global))
Chris Wilson5a97bcc2017-02-22 11:40:46 +00003298 return;
3299
3300 mutex_lock(&obj->base.dev->struct_mutex);
3301 __i915_gem_object_flush_for_display(obj);
3302 mutex_unlock(&obj->base.dev->struct_mutex);
3303}
3304
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003305/**
Chris Wilsone22d8e32017-04-12 12:01:11 +01003306 * Moves a single object to the WC read, and possibly write domain.
3307 * @obj: object to act on
3308 * @write: ask for write access or read only
3309 *
3310 * This function returns when the move is complete, including waiting on
3311 * flushes to occur.
3312 */
3313int
3314i915_gem_object_set_to_wc_domain(struct drm_i915_gem_object *obj, bool write)
3315{
3316 int ret;
3317
3318 lockdep_assert_held(&obj->base.dev->struct_mutex);
3319
3320 ret = i915_gem_object_wait(obj,
3321 I915_WAIT_INTERRUPTIBLE |
3322 I915_WAIT_LOCKED |
3323 (write ? I915_WAIT_ALL : 0),
Chris Wilson62eb3c22019-02-13 09:25:04 +00003324 MAX_SCHEDULE_TIMEOUT);
Chris Wilsone22d8e32017-04-12 12:01:11 +01003325 if (ret)
3326 return ret;
3327
Christian Königc0a51fd2018-02-16 13:43:38 +01003328 if (obj->write_domain == I915_GEM_DOMAIN_WC)
Chris Wilsone22d8e32017-04-12 12:01:11 +01003329 return 0;
3330
3331 /* Flush and acquire obj->pages so that we are coherent through
3332 * direct access in memory with previous cached writes through
3333 * shmemfs and that our cache domain tracking remains valid.
3334 * For example, if the obj->filp was moved to swap without us
3335 * being notified and releasing the pages, we would mistakenly
3336 * continue to assume that the obj remained out of the CPU cached
3337 * domain.
3338 */
3339 ret = i915_gem_object_pin_pages(obj);
3340 if (ret)
3341 return ret;
3342
3343 flush_write_domain(obj, ~I915_GEM_DOMAIN_WC);
3344
3345 /* Serialise direct access to this object with the barriers for
3346 * coherent writes from the GPU, by effectively invalidating the
3347 * WC domain upon first access.
3348 */
Christian Königc0a51fd2018-02-16 13:43:38 +01003349 if ((obj->read_domains & I915_GEM_DOMAIN_WC) == 0)
Chris Wilsone22d8e32017-04-12 12:01:11 +01003350 mb();
3351
3352 /* It should now be out of any other write domains, and we can update
3353 * the domain values for our changes.
3354 */
Christian Königc0a51fd2018-02-16 13:43:38 +01003355 GEM_BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_WC) != 0);
3356 obj->read_domains |= I915_GEM_DOMAIN_WC;
Chris Wilsone22d8e32017-04-12 12:01:11 +01003357 if (write) {
Christian Königc0a51fd2018-02-16 13:43:38 +01003358 obj->read_domains = I915_GEM_DOMAIN_WC;
3359 obj->write_domain = I915_GEM_DOMAIN_WC;
Chris Wilsone22d8e32017-04-12 12:01:11 +01003360 obj->mm.dirty = true;
3361 }
3362
3363 i915_gem_object_unpin_pages(obj);
3364 return 0;
3365}
3366
3367/**
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003368 * Moves a single object to the GTT read, and possibly write domain.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01003369 * @obj: object to act on
3370 * @write: ask for write access or read only
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003371 *
3372 * This function returns when the move is complete, including waiting on
3373 * flushes to occur.
3374 */
Jesse Barnes79e53942008-11-07 14:24:08 -08003375int
Chris Wilson20217462010-11-23 15:26:33 +00003376i915_gem_object_set_to_gtt_domain(struct drm_i915_gem_object *obj, bool write)
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003377{
Eric Anholte47c68e2008-11-14 13:35:19 -08003378 int ret;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003379
Chris Wilsone95433c2016-10-28 13:58:27 +01003380 lockdep_assert_held(&obj->base.dev->struct_mutex);
Chris Wilson4c7d62c2016-10-28 13:58:32 +01003381
Chris Wilsone95433c2016-10-28 13:58:27 +01003382 ret = i915_gem_object_wait(obj,
3383 I915_WAIT_INTERRUPTIBLE |
3384 I915_WAIT_LOCKED |
3385 (write ? I915_WAIT_ALL : 0),
Chris Wilson62eb3c22019-02-13 09:25:04 +00003386 MAX_SCHEDULE_TIMEOUT);
Chris Wilson88241782011-01-07 17:09:48 +00003387 if (ret)
3388 return ret;
3389
Christian Königc0a51fd2018-02-16 13:43:38 +01003390 if (obj->write_domain == I915_GEM_DOMAIN_GTT)
Chris Wilsonc13d87e2016-07-20 09:21:15 +01003391 return 0;
3392
Chris Wilson43566de2015-01-02 16:29:29 +05303393 /* Flush and acquire obj->pages so that we are coherent through
3394 * direct access in memory with previous cached writes through
3395 * shmemfs and that our cache domain tracking remains valid.
3396 * For example, if the obj->filp was moved to swap without us
3397 * being notified and releasing the pages, we would mistakenly
3398 * continue to assume that the obj remained out of the CPU cached
3399 * domain.
3400 */
Chris Wilsona4f5ea62016-10-28 13:58:35 +01003401 ret = i915_gem_object_pin_pages(obj);
Chris Wilson43566de2015-01-02 16:29:29 +05303402 if (ret)
3403 return ret;
3404
Chris Wilsonef749212017-04-12 12:01:10 +01003405 flush_write_domain(obj, ~I915_GEM_DOMAIN_GTT);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003406
Chris Wilsond0a57782012-10-09 19:24:37 +01003407 /* Serialise direct access to this object with the barriers for
3408 * coherent writes from the GPU, by effectively invalidating the
3409 * GTT domain upon first access.
3410 */
Christian Königc0a51fd2018-02-16 13:43:38 +01003411 if ((obj->read_domains & I915_GEM_DOMAIN_GTT) == 0)
Chris Wilsond0a57782012-10-09 19:24:37 +01003412 mb();
3413
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003414 /* It should now be out of any other write domains, and we can update
3415 * the domain values for our changes.
3416 */
Christian Königc0a51fd2018-02-16 13:43:38 +01003417 GEM_BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_GTT) != 0);
3418 obj->read_domains |= I915_GEM_DOMAIN_GTT;
Eric Anholte47c68e2008-11-14 13:35:19 -08003419 if (write) {
Christian Königc0a51fd2018-02-16 13:43:38 +01003420 obj->read_domains = I915_GEM_DOMAIN_GTT;
3421 obj->write_domain = I915_GEM_DOMAIN_GTT;
Chris Wilsona4f5ea62016-10-28 13:58:35 +01003422 obj->mm.dirty = true;
Eric Anholte47c68e2008-11-14 13:35:19 -08003423 }
3424
Chris Wilsona4f5ea62016-10-28 13:58:35 +01003425 i915_gem_object_unpin_pages(obj);
Eric Anholte47c68e2008-11-14 13:35:19 -08003426 return 0;
3427}
3428
Chris Wilsonef55f922015-10-09 14:11:27 +01003429/**
3430 * Changes the cache-level of an object across all VMA.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01003431 * @obj: object to act on
3432 * @cache_level: new cache level to set for the object
Chris Wilsonef55f922015-10-09 14:11:27 +01003433 *
3434 * After this function returns, the object will be in the new cache-level
3435 * across all GTT and the contents of the backing storage will be coherent,
3436 * with respect to the new cache-level. In order to keep the backing storage
3437 * coherent for all users, we only allow a single cache level to be set
3438 * globally on the object and prevent it from being changed whilst the
3439 * hardware is reading from the object. That is if the object is currently
3440 * on the scanout it will be set to uncached (or equivalent display
3441 * cache coherency) and all non-MOCS GPU access will also be uncached so
3442 * that all direct access to the scanout remains coherent.
3443 */
Chris Wilsone4ffd172011-04-04 09:44:39 +01003444int i915_gem_object_set_cache_level(struct drm_i915_gem_object *obj,
3445 enum i915_cache_level cache_level)
3446{
Chris Wilsonaa653a62016-08-04 07:52:27 +01003447 struct i915_vma *vma;
Chris Wilsona6a7cc42016-11-18 21:17:46 +00003448 int ret;
Chris Wilsone4ffd172011-04-04 09:44:39 +01003449
Chris Wilson4c7d62c2016-10-28 13:58:32 +01003450 lockdep_assert_held(&obj->base.dev->struct_mutex);
3451
Chris Wilsone4ffd172011-04-04 09:44:39 +01003452 if (obj->cache_level == cache_level)
Chris Wilsona6a7cc42016-11-18 21:17:46 +00003453 return 0;
Chris Wilsone4ffd172011-04-04 09:44:39 +01003454
Chris Wilsonef55f922015-10-09 14:11:27 +01003455 /* Inspect the list of currently bound VMA and unbind any that would
3456 * be invalid given the new cache-level. This is principally to
3457 * catch the issue of the CS prefetch crossing page boundaries and
3458 * reading an invalid PTE on older architectures.
3459 */
Chris Wilsonaa653a62016-08-04 07:52:27 +01003460restart:
Chris Wilson528cbd12019-01-28 10:23:54 +00003461 list_for_each_entry(vma, &obj->vma.list, obj_link) {
Chris Wilsonef55f922015-10-09 14:11:27 +01003462 if (!drm_mm_node_allocated(&vma->node))
3463 continue;
3464
Chris Wilson20dfbde2016-08-04 16:32:30 +01003465 if (i915_vma_is_pinned(vma)) {
Chris Wilsonef55f922015-10-09 14:11:27 +01003466 DRM_DEBUG("can not change the cache level of pinned objects\n");
3467 return -EBUSY;
3468 }
3469
Chris Wilson010e3e62017-12-06 12:49:13 +00003470 if (!i915_vma_is_closed(vma) &&
3471 i915_gem_valid_gtt_space(vma, cache_level))
Chris Wilsonaa653a62016-08-04 07:52:27 +01003472 continue;
3473
3474 ret = i915_vma_unbind(vma);
3475 if (ret)
3476 return ret;
3477
3478 /* As unbinding may affect other elements in the
3479 * obj->vma_list (due to side-effects from retiring
3480 * an active vma), play safe and restart the iterator.
3481 */
3482 goto restart;
Chris Wilson42d6ab42012-07-26 11:49:32 +01003483 }
3484
Chris Wilsonef55f922015-10-09 14:11:27 +01003485 /* We can reuse the existing drm_mm nodes but need to change the
3486 * cache-level on the PTE. We could simply unbind them all and
3487 * rebind with the correct cache-level on next use. However since
3488 * we already have a valid slot, dma mapping, pages etc, we may as
3489 * rewrite the PTE in the belief that doing so tramples upon less
3490 * state and so involves less work.
3491 */
Chris Wilson15717de2016-08-04 07:52:26 +01003492 if (obj->bind_count) {
Chris Wilsonef55f922015-10-09 14:11:27 +01003493 /* Before we change the PTE, the GPU must not be accessing it.
3494 * If we wait upon the object, we know that all the bound
3495 * VMA are no longer active.
3496 */
Chris Wilsone95433c2016-10-28 13:58:27 +01003497 ret = i915_gem_object_wait(obj,
3498 I915_WAIT_INTERRUPTIBLE |
3499 I915_WAIT_LOCKED |
3500 I915_WAIT_ALL,
Chris Wilson62eb3c22019-02-13 09:25:04 +00003501 MAX_SCHEDULE_TIMEOUT);
Chris Wilsone4ffd172011-04-04 09:44:39 +01003502 if (ret)
3503 return ret;
3504
Tvrtko Ursulin0031fb92016-11-04 14:42:44 +00003505 if (!HAS_LLC(to_i915(obj->base.dev)) &&
3506 cache_level != I915_CACHE_NONE) {
Chris Wilsonef55f922015-10-09 14:11:27 +01003507 /* Access to snoopable pages through the GTT is
3508 * incoherent and on some machines causes a hard
3509 * lockup. Relinquish the CPU mmaping to force
3510 * userspace to refault in the pages and we can
3511 * then double check if the GTT mapping is still
3512 * valid for that pointer access.
3513 */
3514 i915_gem_release_mmap(obj);
Chris Wilsone4ffd172011-04-04 09:44:39 +01003515
Chris Wilsonef55f922015-10-09 14:11:27 +01003516 /* As we no longer need a fence for GTT access,
3517 * we can relinquish it now (and so prevent having
3518 * to steal a fence from someone else on the next
3519 * fence request). Note GPU activity would have
3520 * dropped the fence as all snoopable access is
3521 * supposed to be linear.
3522 */
Chris Wilsone2189dd2017-12-07 21:14:07 +00003523 for_each_ggtt_vma(vma, obj) {
Chris Wilson49ef5292016-08-18 17:17:00 +01003524 ret = i915_vma_put_fence(vma);
3525 if (ret)
3526 return ret;
3527 }
Chris Wilsonef55f922015-10-09 14:11:27 +01003528 } else {
3529 /* We either have incoherent backing store and
3530 * so no GTT access or the architecture is fully
3531 * coherent. In such cases, existing GTT mmaps
3532 * ignore the cache bit in the PTE and we can
3533 * rewrite it without confusing the GPU or having
3534 * to force userspace to fault back in its mmaps.
3535 */
Chris Wilsone4ffd172011-04-04 09:44:39 +01003536 }
3537
Chris Wilson528cbd12019-01-28 10:23:54 +00003538 list_for_each_entry(vma, &obj->vma.list, obj_link) {
Chris Wilsonef55f922015-10-09 14:11:27 +01003539 if (!drm_mm_node_allocated(&vma->node))
3540 continue;
3541
3542 ret = i915_vma_bind(vma, cache_level, PIN_UPDATE);
3543 if (ret)
3544 return ret;
3545 }
Chris Wilsone4ffd172011-04-04 09:44:39 +01003546 }
3547
Chris Wilson528cbd12019-01-28 10:23:54 +00003548 list_for_each_entry(vma, &obj->vma.list, obj_link)
Chris Wilson2c225692013-08-09 12:26:45 +01003549 vma->node.color = cache_level;
Chris Wilsonb8f55be2017-08-11 12:11:16 +01003550 i915_gem_object_set_cache_coherency(obj, cache_level);
Chris Wilsone27ab732017-06-15 13:38:49 +01003551 obj->cache_dirty = true; /* Always invalidate stale cachelines */
Chris Wilson2c225692013-08-09 12:26:45 +01003552
Chris Wilsone4ffd172011-04-04 09:44:39 +01003553 return 0;
3554}
3555
Ben Widawsky199adf42012-09-21 17:01:20 -07003556int i915_gem_get_caching_ioctl(struct drm_device *dev, void *data,
3557 struct drm_file *file)
Chris Wilsone6994ae2012-07-10 10:27:08 +01003558{
Ben Widawsky199adf42012-09-21 17:01:20 -07003559 struct drm_i915_gem_caching *args = data;
Chris Wilsone6994ae2012-07-10 10:27:08 +01003560 struct drm_i915_gem_object *obj;
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01003561 int err = 0;
Chris Wilsone6994ae2012-07-10 10:27:08 +01003562
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01003563 rcu_read_lock();
3564 obj = i915_gem_object_lookup_rcu(file, args->handle);
3565 if (!obj) {
3566 err = -ENOENT;
3567 goto out;
3568 }
Chris Wilsone6994ae2012-07-10 10:27:08 +01003569
Chris Wilson651d7942013-08-08 14:41:10 +01003570 switch (obj->cache_level) {
3571 case I915_CACHE_LLC:
3572 case I915_CACHE_L3_LLC:
3573 args->caching = I915_CACHING_CACHED;
3574 break;
3575
Chris Wilson4257d3b2013-08-08 14:41:11 +01003576 case I915_CACHE_WT:
3577 args->caching = I915_CACHING_DISPLAY;
3578 break;
3579
Chris Wilson651d7942013-08-08 14:41:10 +01003580 default:
3581 args->caching = I915_CACHING_NONE;
3582 break;
3583 }
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01003584out:
3585 rcu_read_unlock();
3586 return err;
Chris Wilsone6994ae2012-07-10 10:27:08 +01003587}
3588
Ben Widawsky199adf42012-09-21 17:01:20 -07003589int i915_gem_set_caching_ioctl(struct drm_device *dev, void *data,
3590 struct drm_file *file)
Chris Wilsone6994ae2012-07-10 10:27:08 +01003591{
Chris Wilson9c870d02016-10-24 13:42:15 +01003592 struct drm_i915_private *i915 = to_i915(dev);
Ben Widawsky199adf42012-09-21 17:01:20 -07003593 struct drm_i915_gem_caching *args = data;
Chris Wilsone6994ae2012-07-10 10:27:08 +01003594 struct drm_i915_gem_object *obj;
3595 enum i915_cache_level level;
Chris Wilsond65415d2017-01-19 08:22:10 +00003596 int ret = 0;
Chris Wilsone6994ae2012-07-10 10:27:08 +01003597
Ben Widawsky199adf42012-09-21 17:01:20 -07003598 switch (args->caching) {
3599 case I915_CACHING_NONE:
Chris Wilsone6994ae2012-07-10 10:27:08 +01003600 level = I915_CACHE_NONE;
3601 break;
Ben Widawsky199adf42012-09-21 17:01:20 -07003602 case I915_CACHING_CACHED:
Imre Deake5756c12015-08-14 18:43:30 +03003603 /*
3604 * Due to a HW issue on BXT A stepping, GPU stores via a
3605 * snooped mapping may leave stale data in a corresponding CPU
3606 * cacheline, whereas normally such cachelines would get
3607 * invalidated.
3608 */
Chris Wilson9c870d02016-10-24 13:42:15 +01003609 if (!HAS_LLC(i915) && !HAS_SNOOP(i915))
Imre Deake5756c12015-08-14 18:43:30 +03003610 return -ENODEV;
3611
Chris Wilsone6994ae2012-07-10 10:27:08 +01003612 level = I915_CACHE_LLC;
3613 break;
Chris Wilson4257d3b2013-08-08 14:41:11 +01003614 case I915_CACHING_DISPLAY:
Chris Wilson9c870d02016-10-24 13:42:15 +01003615 level = HAS_WT(i915) ? I915_CACHE_WT : I915_CACHE_NONE;
Chris Wilson4257d3b2013-08-08 14:41:11 +01003616 break;
Chris Wilsone6994ae2012-07-10 10:27:08 +01003617 default:
3618 return -EINVAL;
3619 }
3620
Chris Wilsond65415d2017-01-19 08:22:10 +00003621 obj = i915_gem_object_lookup(file, args->handle);
3622 if (!obj)
3623 return -ENOENT;
3624
Tina Zhanga03f3952017-11-14 10:25:13 +00003625 /*
3626 * The caching mode of proxy object is handled by its generator, and
3627 * not allowed to be changed by userspace.
3628 */
3629 if (i915_gem_object_is_proxy(obj)) {
3630 ret = -ENXIO;
3631 goto out;
3632 }
3633
Chris Wilsond65415d2017-01-19 08:22:10 +00003634 if (obj->cache_level == level)
3635 goto out;
3636
3637 ret = i915_gem_object_wait(obj,
3638 I915_WAIT_INTERRUPTIBLE,
Chris Wilson62eb3c22019-02-13 09:25:04 +00003639 MAX_SCHEDULE_TIMEOUT);
Chris Wilsond65415d2017-01-19 08:22:10 +00003640 if (ret)
3641 goto out;
3642
Ben Widawsky3bc29132012-09-26 16:15:20 -07003643 ret = i915_mutex_lock_interruptible(dev);
3644 if (ret)
Chris Wilsond65415d2017-01-19 08:22:10 +00003645 goto out;
Chris Wilsone6994ae2012-07-10 10:27:08 +01003646
3647 ret = i915_gem_object_set_cache_level(obj, level);
Chris Wilsone6994ae2012-07-10 10:27:08 +01003648 mutex_unlock(&dev->struct_mutex);
Chris Wilsond65415d2017-01-19 08:22:10 +00003649
3650out:
3651 i915_gem_object_put(obj);
Chris Wilsone6994ae2012-07-10 10:27:08 +01003652 return ret;
3653}
3654
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003655/*
Dhinakaran Pandiyan07bcd992018-03-06 19:34:18 -08003656 * Prepare buffer for display plane (scanout, cursors, etc). Can be called from
3657 * an uninterruptible phase (modesetting) and allows any flushes to be pipelined
3658 * (for pageflips). We only flush the caches while preparing the buffer for
3659 * display, the callers are responsible for frontbuffer flush.
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003660 */
Chris Wilson058d88c2016-08-15 10:49:06 +01003661struct i915_vma *
Chris Wilson2da3b9b2011-04-14 09:41:17 +01003662i915_gem_object_pin_to_display_plane(struct drm_i915_gem_object *obj,
3663 u32 alignment,
Chris Wilson59354852018-02-20 13:42:06 +00003664 const struct i915_ggtt_view *view,
3665 unsigned int flags)
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003666{
Chris Wilson058d88c2016-08-15 10:49:06 +01003667 struct i915_vma *vma;
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003668 int ret;
3669
Chris Wilson4c7d62c2016-10-28 13:58:32 +01003670 lockdep_assert_held(&obj->base.dev->struct_mutex);
3671
Chris Wilsonbd3d2252017-10-13 21:26:14 +01003672 /* Mark the global pin early so that we account for the
Chris Wilsoncc98b412013-08-09 12:25:09 +01003673 * display coherency whilst setting up the cache domains.
3674 */
Chris Wilsonbd3d2252017-10-13 21:26:14 +01003675 obj->pin_global++;
Chris Wilsoncc98b412013-08-09 12:25:09 +01003676
Eric Anholta7ef0642011-03-29 16:59:54 -07003677 /* The display engine is not coherent with the LLC cache on gen6. As
3678 * a result, we make sure that the pinning that is about to occur is
3679 * done with uncached PTEs. This is lowest common denominator for all
3680 * chipsets.
3681 *
3682 * However for gen6+, we could do better by using the GFDT bit instead
3683 * of uncaching, which would allow us to flush all the LLC-cached data
3684 * with that bit in the PTE to main memory with just one PIPE_CONTROL.
3685 */
Chris Wilson651d7942013-08-08 14:41:10 +01003686 ret = i915_gem_object_set_cache_level(obj,
Tvrtko Ursulin86527442016-10-13 11:03:00 +01003687 HAS_WT(to_i915(obj->base.dev)) ?
3688 I915_CACHE_WT : I915_CACHE_NONE);
Chris Wilson058d88c2016-08-15 10:49:06 +01003689 if (ret) {
3690 vma = ERR_PTR(ret);
Chris Wilsonbd3d2252017-10-13 21:26:14 +01003691 goto err_unpin_global;
Chris Wilson058d88c2016-08-15 10:49:06 +01003692 }
Eric Anholta7ef0642011-03-29 16:59:54 -07003693
Chris Wilson2da3b9b2011-04-14 09:41:17 +01003694 /* As the user may map the buffer once pinned in the display plane
3695 * (e.g. libkms for the bootup splash), we have to ensure that we
Chris Wilson2efb8132016-08-18 17:17:06 +01003696 * always use map_and_fenceable for all scanout buffers. However,
3697 * it may simply be too big to fit into mappable, in which case
3698 * put it anyway and hope that userspace can cope (but always first
3699 * try to preserve the existing ABI).
Chris Wilson2da3b9b2011-04-14 09:41:17 +01003700 */
Chris Wilson2efb8132016-08-18 17:17:06 +01003701 vma = ERR_PTR(-ENOSPC);
Chris Wilson59354852018-02-20 13:42:06 +00003702 if ((flags & PIN_MAPPABLE) == 0 &&
3703 (!view || view->type == I915_GGTT_VIEW_NORMAL))
Chris Wilson2efb8132016-08-18 17:17:06 +01003704 vma = i915_gem_object_ggtt_pin(obj, view, 0, alignment,
Chris Wilson59354852018-02-20 13:42:06 +00003705 flags |
3706 PIN_MAPPABLE |
3707 PIN_NONBLOCK);
3708 if (IS_ERR(vma))
Chris Wilson767a2222016-11-07 11:01:28 +00003709 vma = i915_gem_object_ggtt_pin(obj, view, 0, alignment, flags);
Chris Wilson058d88c2016-08-15 10:49:06 +01003710 if (IS_ERR(vma))
Chris Wilsonbd3d2252017-10-13 21:26:14 +01003711 goto err_unpin_global;
Chris Wilson2da3b9b2011-04-14 09:41:17 +01003712
Chris Wilsond8923dc2016-08-18 17:17:07 +01003713 vma->display_alignment = max_t(u64, vma->display_alignment, alignment);
3714
Chris Wilson5a97bcc2017-02-22 11:40:46 +00003715 __i915_gem_object_flush_for_display(obj);
Chris Wilsonb118c1e2010-05-27 13:18:14 +01003716
Chris Wilson2da3b9b2011-04-14 09:41:17 +01003717 /* It should now be out of any other write domains, and we can update
3718 * the domain values for our changes.
3719 */
Christian Königc0a51fd2018-02-16 13:43:38 +01003720 obj->read_domains |= I915_GEM_DOMAIN_GTT;
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003721
Chris Wilson058d88c2016-08-15 10:49:06 +01003722 return vma;
Chris Wilsoncc98b412013-08-09 12:25:09 +01003723
Chris Wilsonbd3d2252017-10-13 21:26:14 +01003724err_unpin_global:
3725 obj->pin_global--;
Chris Wilson058d88c2016-08-15 10:49:06 +01003726 return vma;
Chris Wilsoncc98b412013-08-09 12:25:09 +01003727}
3728
3729void
Chris Wilson058d88c2016-08-15 10:49:06 +01003730i915_gem_object_unpin_from_display_plane(struct i915_vma *vma)
Chris Wilsoncc98b412013-08-09 12:25:09 +01003731{
Chris Wilson49d73912016-11-29 09:50:08 +00003732 lockdep_assert_held(&vma->vm->i915->drm.struct_mutex);
Chris Wilson4c7d62c2016-10-28 13:58:32 +01003733
Chris Wilsonbd3d2252017-10-13 21:26:14 +01003734 if (WARN_ON(vma->obj->pin_global == 0))
Tvrtko Ursulin8a0c39b2015-04-13 11:50:09 +01003735 return;
3736
Chris Wilsonbd3d2252017-10-13 21:26:14 +01003737 if (--vma->obj->pin_global == 0)
Chris Wilsonf51455d2017-01-10 14:47:34 +00003738 vma->display_alignment = I915_GTT_MIN_ALIGNMENT;
Tvrtko Ursuline6617332015-03-23 11:10:33 +00003739
Chris Wilson383d5822016-08-18 17:17:08 +01003740 /* Bump the LRU to try and avoid premature eviction whilst flipping */
Chris Wilsonbefedbb2017-01-19 19:26:55 +00003741 i915_gem_object_bump_inactive_ggtt(vma->obj);
Chris Wilson383d5822016-08-18 17:17:08 +01003742
Chris Wilson058d88c2016-08-15 10:49:06 +01003743 i915_vma_unpin(vma);
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003744}
3745
Eric Anholte47c68e2008-11-14 13:35:19 -08003746/**
3747 * Moves a single object to the CPU read, and possibly write domain.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01003748 * @obj: object to act on
3749 * @write: requesting write or read-only access
Eric Anholte47c68e2008-11-14 13:35:19 -08003750 *
3751 * This function returns when the move is complete, including waiting on
3752 * flushes to occur.
3753 */
Chris Wilsondabdfe02012-03-26 10:10:27 +02003754int
Chris Wilson919926a2010-11-12 13:42:53 +00003755i915_gem_object_set_to_cpu_domain(struct drm_i915_gem_object *obj, bool write)
Eric Anholte47c68e2008-11-14 13:35:19 -08003756{
Eric Anholte47c68e2008-11-14 13:35:19 -08003757 int ret;
3758
Chris Wilsone95433c2016-10-28 13:58:27 +01003759 lockdep_assert_held(&obj->base.dev->struct_mutex);
Chris Wilson4c7d62c2016-10-28 13:58:32 +01003760
Chris Wilsone95433c2016-10-28 13:58:27 +01003761 ret = i915_gem_object_wait(obj,
3762 I915_WAIT_INTERRUPTIBLE |
3763 I915_WAIT_LOCKED |
3764 (write ? I915_WAIT_ALL : 0),
Chris Wilson62eb3c22019-02-13 09:25:04 +00003765 MAX_SCHEDULE_TIMEOUT);
Chris Wilson88241782011-01-07 17:09:48 +00003766 if (ret)
3767 return ret;
3768
Chris Wilsonef749212017-04-12 12:01:10 +01003769 flush_write_domain(obj, ~I915_GEM_DOMAIN_CPU);
Eric Anholte47c68e2008-11-14 13:35:19 -08003770
Eric Anholte47c68e2008-11-14 13:35:19 -08003771 /* Flush the CPU cache if it's still invalid. */
Christian Königc0a51fd2018-02-16 13:43:38 +01003772 if ((obj->read_domains & I915_GEM_DOMAIN_CPU) == 0) {
Chris Wilson57822dc2017-02-22 11:40:48 +00003773 i915_gem_clflush_object(obj, I915_CLFLUSH_SYNC);
Christian Königc0a51fd2018-02-16 13:43:38 +01003774 obj->read_domains |= I915_GEM_DOMAIN_CPU;
Eric Anholte47c68e2008-11-14 13:35:19 -08003775 }
3776
3777 /* It should now be out of any other write domains, and we can update
3778 * the domain values for our changes.
3779 */
Christian Königc0a51fd2018-02-16 13:43:38 +01003780 GEM_BUG_ON(obj->write_domain & ~I915_GEM_DOMAIN_CPU);
Eric Anholte47c68e2008-11-14 13:35:19 -08003781
3782 /* If we're writing through the CPU, then the GPU read domains will
3783 * need to be invalidated at next use.
3784 */
Chris Wilsone27ab732017-06-15 13:38:49 +01003785 if (write)
3786 __start_cpu_write(obj);
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003787
3788 return 0;
3789}
3790
Eric Anholt673a3942008-07-30 12:06:12 -07003791/* Throttle our rendering by waiting until the ring has completed our requests
3792 * emitted over 20 msec ago.
3793 *
Eric Anholtb9624422009-06-03 07:27:35 +00003794 * Note that if we were to use the current jiffies each time around the loop,
3795 * we wouldn't escape the function with any frames outstanding if the time to
3796 * render a frame was over 20ms.
3797 *
Eric Anholt673a3942008-07-30 12:06:12 -07003798 * This should get us reasonable parallelism between CPU and GPU but also
3799 * relatively low latency when blocking on a particular request to finish.
3800 */
3801static int
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003802i915_gem_ring_throttle(struct drm_device *dev, struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07003803{
Chris Wilsonfac5e232016-07-04 11:34:36 +01003804 struct drm_i915_private *dev_priv = to_i915(dev);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003805 struct drm_i915_file_private *file_priv = file->driver_priv;
Chris Wilsond0bc54f2015-05-21 21:01:48 +01003806 unsigned long recent_enough = jiffies - DRM_I915_THROTTLE_JIFFIES;
Chris Wilsone61e0f52018-02-21 09:56:36 +00003807 struct i915_request *request, *target = NULL;
Chris Wilsone95433c2016-10-28 13:58:27 +01003808 long ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003809
Chris Wilsonf4457ae2016-04-13 17:35:08 +01003810 /* ABI: return -EIO if already wedged */
Chris Wilsonc41166f2019-02-20 14:56:37 +00003811 ret = i915_terminally_wedged(dev_priv);
3812 if (ret)
3813 return ret;
Chris Wilsone110e8d2011-01-26 15:39:14 +00003814
Chris Wilson1c255952010-09-26 11:03:27 +01003815 spin_lock(&file_priv->mm.lock);
Chris Wilsonc8659ef2017-03-02 12:25:25 +00003816 list_for_each_entry(request, &file_priv->mm.request_list, client_link) {
Eric Anholtb9624422009-06-03 07:27:35 +00003817 if (time_after_eq(request->emitted_jiffies, recent_enough))
3818 break;
3819
Chris Wilsonc8659ef2017-03-02 12:25:25 +00003820 if (target) {
3821 list_del(&target->client_link);
3822 target->file_priv = NULL;
3823 }
John Harrisonfcfa423c2015-05-29 17:44:12 +01003824
John Harrison54fb2412014-11-24 18:49:27 +00003825 target = request;
Eric Anholtb9624422009-06-03 07:27:35 +00003826 }
John Harrisonff865882014-11-24 18:49:28 +00003827 if (target)
Chris Wilsone61e0f52018-02-21 09:56:36 +00003828 i915_request_get(target);
Chris Wilson1c255952010-09-26 11:03:27 +01003829 spin_unlock(&file_priv->mm.lock);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003830
John Harrison54fb2412014-11-24 18:49:27 +00003831 if (target == NULL)
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003832 return 0;
3833
Chris Wilsone61e0f52018-02-21 09:56:36 +00003834 ret = i915_request_wait(target,
Chris Wilsone95433c2016-10-28 13:58:27 +01003835 I915_WAIT_INTERRUPTIBLE,
3836 MAX_SCHEDULE_TIMEOUT);
Chris Wilsone61e0f52018-02-21 09:56:36 +00003837 i915_request_put(target);
John Harrisonff865882014-11-24 18:49:28 +00003838
Chris Wilsone95433c2016-10-28 13:58:27 +01003839 return ret < 0 ? ret : 0;
Eric Anholt673a3942008-07-30 12:06:12 -07003840}
3841
Chris Wilson058d88c2016-08-15 10:49:06 +01003842struct i915_vma *
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02003843i915_gem_object_ggtt_pin(struct drm_i915_gem_object *obj,
3844 const struct i915_ggtt_view *view,
Chris Wilson91b2db62016-08-04 16:32:23 +01003845 u64 size,
Chris Wilson2ffffd02016-08-04 16:32:22 +01003846 u64 alignment,
3847 u64 flags)
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02003848{
Chris Wilsonad16d2e2016-10-13 09:55:04 +01003849 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
Chris Wilson82ad6442018-06-05 16:37:58 +01003850 struct i915_address_space *vm = &dev_priv->ggtt.vm;
Chris Wilson59bfa122016-08-04 16:32:31 +01003851 struct i915_vma *vma;
3852 int ret;
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03003853
Chris Wilson4c7d62c2016-10-28 13:58:32 +01003854 lockdep_assert_held(&obj->base.dev->struct_mutex);
3855
Chris Wilsonac87a6fd2018-02-20 13:42:05 +00003856 if (flags & PIN_MAPPABLE &&
3857 (!view || view->type == I915_GGTT_VIEW_NORMAL)) {
Chris Wilson43ae70d92017-10-09 09:44:01 +01003858 /* If the required space is larger than the available
3859 * aperture, we will not able to find a slot for the
3860 * object and unbinding the object now will be in
3861 * vain. Worse, doing so may cause us to ping-pong
3862 * the object in and out of the Global GTT and
3863 * waste a lot of cycles under the mutex.
3864 */
3865 if (obj->base.size > dev_priv->ggtt.mappable_end)
3866 return ERR_PTR(-E2BIG);
3867
3868 /* If NONBLOCK is set the caller is optimistically
3869 * trying to cache the full object within the mappable
3870 * aperture, and *must* have a fallback in place for
3871 * situations where we cannot bind the object. We
3872 * can be a little more lax here and use the fallback
3873 * more often to avoid costly migrations of ourselves
3874 * and other objects within the aperture.
3875 *
3876 * Half-the-aperture is used as a simple heuristic.
3877 * More interesting would to do search for a free
3878 * block prior to making the commitment to unbind.
3879 * That caters for the self-harm case, and with a
3880 * little more heuristics (e.g. NOFAULT, NOEVICT)
3881 * we could try to minimise harm to others.
3882 */
3883 if (flags & PIN_NONBLOCK &&
3884 obj->base.size > dev_priv->ggtt.mappable_end / 2)
3885 return ERR_PTR(-ENOSPC);
3886 }
3887
Chris Wilson718659a2017-01-16 15:21:28 +00003888 vma = i915_vma_instance(obj, vm, view);
Chengguang Xu772b5402019-02-21 10:08:19 +08003889 if (IS_ERR(vma))
Chris Wilson058d88c2016-08-15 10:49:06 +01003890 return vma;
Chris Wilson59bfa122016-08-04 16:32:31 +01003891
3892 if (i915_vma_misplaced(vma, size, alignment, flags)) {
Chris Wilson43ae70d92017-10-09 09:44:01 +01003893 if (flags & PIN_NONBLOCK) {
3894 if (i915_vma_is_pinned(vma) || i915_vma_is_active(vma))
3895 return ERR_PTR(-ENOSPC);
Chris Wilson59bfa122016-08-04 16:32:31 +01003896
Chris Wilson43ae70d92017-10-09 09:44:01 +01003897 if (flags & PIN_MAPPABLE &&
Chris Wilson944397f2017-01-09 16:16:11 +00003898 vma->fence_size > dev_priv->ggtt.mappable_end / 2)
Chris Wilsonad16d2e2016-10-13 09:55:04 +01003899 return ERR_PTR(-ENOSPC);
3900 }
3901
Chris Wilson59bfa122016-08-04 16:32:31 +01003902 WARN(i915_vma_is_pinned(vma),
3903 "bo is already pinned in ggtt with incorrect alignment:"
Chris Wilson05a20d02016-08-18 17:16:55 +01003904 " offset=%08x, req.alignment=%llx,"
3905 " req.map_and_fenceable=%d, vma->map_and_fenceable=%d\n",
3906 i915_ggtt_offset(vma), alignment,
Chris Wilson59bfa122016-08-04 16:32:31 +01003907 !!(flags & PIN_MAPPABLE),
Chris Wilson05a20d02016-08-18 17:16:55 +01003908 i915_vma_is_map_and_fenceable(vma));
Chris Wilson59bfa122016-08-04 16:32:31 +01003909 ret = i915_vma_unbind(vma);
3910 if (ret)
Chris Wilson058d88c2016-08-15 10:49:06 +01003911 return ERR_PTR(ret);
Chris Wilson59bfa122016-08-04 16:32:31 +01003912 }
3913
Chris Wilson058d88c2016-08-15 10:49:06 +01003914 ret = i915_vma_pin(vma, size, alignment, flags | PIN_GLOBAL);
3915 if (ret)
3916 return ERR_PTR(ret);
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02003917
Chris Wilson058d88c2016-08-15 10:49:06 +01003918 return vma;
Eric Anholt673a3942008-07-30 12:06:12 -07003919}
3920
Chris Wilsonedf6b762016-08-09 09:23:33 +01003921static __always_inline unsigned int __busy_read_flag(unsigned int id)
Chris Wilson3fdc13c2016-08-05 10:14:18 +01003922{
3923 /* Note that we could alias engines in the execbuf API, but
3924 * that would be very unwise as it prevents userspace from
3925 * fine control over engine selection. Ahem.
3926 *
3927 * This should be something like EXEC_MAX_ENGINE instead of
3928 * I915_NUM_ENGINES.
3929 */
3930 BUILD_BUG_ON(I915_NUM_ENGINES > 16);
3931 return 0x10000 << id;
3932}
3933
3934static __always_inline unsigned int __busy_write_id(unsigned int id)
3935{
Chris Wilson70cb4722016-08-09 18:08:25 +01003936 /* The uABI guarantees an active writer is also amongst the read
3937 * engines. This would be true if we accessed the activity tracking
3938 * under the lock, but as we perform the lookup of the object and
3939 * its activity locklessly we can not guarantee that the last_write
3940 * being active implies that we have set the same engine flag from
3941 * last_read - hence we always set both read and write busy for
3942 * last_write.
3943 */
3944 return id | __busy_read_flag(id);
Chris Wilson3fdc13c2016-08-05 10:14:18 +01003945}
3946
Chris Wilsonedf6b762016-08-09 09:23:33 +01003947static __always_inline unsigned int
Chris Wilsond07f0e52016-10-28 13:58:44 +01003948__busy_set_if_active(const struct dma_fence *fence,
Chris Wilson3fdc13c2016-08-05 10:14:18 +01003949 unsigned int (*flag)(unsigned int id))
3950{
Chris Wilsone61e0f52018-02-21 09:56:36 +00003951 struct i915_request *rq;
Chris Wilson12555012016-08-16 09:50:40 +01003952
Chris Wilsond07f0e52016-10-28 13:58:44 +01003953 /* We have to check the current hw status of the fence as the uABI
3954 * guarantees forward progress. We could rely on the idle worker
3955 * to eventually flush us, but to minimise latency just ask the
3956 * hardware.
3957 *
3958 * Note we only report on the status of native fences.
3959 */
3960 if (!dma_fence_is_i915(fence))
Chris Wilson12555012016-08-16 09:50:40 +01003961 return 0;
3962
Chris Wilsond07f0e52016-10-28 13:58:44 +01003963 /* opencode to_request() in order to avoid const warnings */
Chris Wilsone61e0f52018-02-21 09:56:36 +00003964 rq = container_of(fence, struct i915_request, fence);
3965 if (i915_request_completed(rq))
Chris Wilsond07f0e52016-10-28 13:58:44 +01003966 return 0;
3967
Chris Wilson1d39f282017-04-11 13:43:06 +01003968 return flag(rq->engine->uabi_id);
Chris Wilson3fdc13c2016-08-05 10:14:18 +01003969}
3970
Chris Wilsonedf6b762016-08-09 09:23:33 +01003971static __always_inline unsigned int
Chris Wilsond07f0e52016-10-28 13:58:44 +01003972busy_check_reader(const struct dma_fence *fence)
Chris Wilson3fdc13c2016-08-05 10:14:18 +01003973{
Chris Wilsond07f0e52016-10-28 13:58:44 +01003974 return __busy_set_if_active(fence, __busy_read_flag);
Chris Wilson3fdc13c2016-08-05 10:14:18 +01003975}
3976
Chris Wilsonedf6b762016-08-09 09:23:33 +01003977static __always_inline unsigned int
Chris Wilsond07f0e52016-10-28 13:58:44 +01003978busy_check_writer(const struct dma_fence *fence)
Chris Wilson3fdc13c2016-08-05 10:14:18 +01003979{
Chris Wilsond07f0e52016-10-28 13:58:44 +01003980 if (!fence)
3981 return 0;
3982
3983 return __busy_set_if_active(fence, __busy_write_id);
Chris Wilson3fdc13c2016-08-05 10:14:18 +01003984}
3985
Eric Anholt673a3942008-07-30 12:06:12 -07003986int
Eric Anholt673a3942008-07-30 12:06:12 -07003987i915_gem_busy_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00003988 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07003989{
3990 struct drm_i915_gem_busy *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00003991 struct drm_i915_gem_object *obj;
Chris Wilsond07f0e52016-10-28 13:58:44 +01003992 struct reservation_object_list *list;
3993 unsigned int seq;
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01003994 int err;
Eric Anholt673a3942008-07-30 12:06:12 -07003995
Chris Wilsond07f0e52016-10-28 13:58:44 +01003996 err = -ENOENT;
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01003997 rcu_read_lock();
3998 obj = i915_gem_object_lookup_rcu(file, args->handle);
Chris Wilsond07f0e52016-10-28 13:58:44 +01003999 if (!obj)
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004000 goto out;
Chris Wilsond07f0e52016-10-28 13:58:44 +01004001
4002 /* A discrepancy here is that we do not report the status of
4003 * non-i915 fences, i.e. even though we may report the object as idle,
4004 * a call to set-domain may still stall waiting for foreign rendering.
4005 * This also means that wait-ioctl may report an object as busy,
4006 * where busy-ioctl considers it idle.
4007 *
4008 * We trade the ability to warn of foreign fences to report on which
4009 * i915 engines are active for the object.
4010 *
4011 * Alternatively, we can trade that extra information on read/write
4012 * activity with
4013 * args->busy =
4014 * !reservation_object_test_signaled_rcu(obj->resv, true);
4015 * to report the overall busyness. This is what the wait-ioctl does.
4016 *
4017 */
4018retry:
4019 seq = raw_read_seqcount(&obj->resv->seq);
4020
4021 /* Translate the exclusive fence to the READ *and* WRITE engine */
4022 args->busy = busy_check_writer(rcu_dereference(obj->resv->fence_excl));
4023
4024 /* Translate shared fences to READ set of engines */
4025 list = rcu_dereference(obj->resv->fence);
4026 if (list) {
4027 unsigned int shared_count = list->shared_count, i;
4028
4029 for (i = 0; i < shared_count; ++i) {
4030 struct dma_fence *fence =
4031 rcu_dereference(list->shared[i]);
4032
4033 args->busy |= busy_check_reader(fence);
4034 }
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004035 }
Zou Nan haid1b851f2010-05-21 09:08:57 +08004036
Chris Wilsond07f0e52016-10-28 13:58:44 +01004037 if (args->busy && read_seqcount_retry(&obj->resv->seq, seq))
4038 goto retry;
Chris Wilson426960b2016-01-15 16:51:46 +00004039
Chris Wilsond07f0e52016-10-28 13:58:44 +01004040 err = 0;
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004041out:
4042 rcu_read_unlock();
4043 return err;
Eric Anholt673a3942008-07-30 12:06:12 -07004044}
4045
4046int
4047i915_gem_throttle_ioctl(struct drm_device *dev, void *data,
4048 struct drm_file *file_priv)
4049{
Akshay Joshi0206e352011-08-16 15:34:10 -04004050 return i915_gem_ring_throttle(dev, file_priv);
Eric Anholt673a3942008-07-30 12:06:12 -07004051}
4052
Chris Wilson3ef94da2009-09-14 16:50:29 +01004053int
4054i915_gem_madvise_ioctl(struct drm_device *dev, void *data,
4055 struct drm_file *file_priv)
4056{
Chris Wilsonfac5e232016-07-04 11:34:36 +01004057 struct drm_i915_private *dev_priv = to_i915(dev);
Chris Wilson3ef94da2009-09-14 16:50:29 +01004058 struct drm_i915_gem_madvise *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00004059 struct drm_i915_gem_object *obj;
Chris Wilson1233e2d2016-10-28 13:58:37 +01004060 int err;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004061
4062 switch (args->madv) {
4063 case I915_MADV_DONTNEED:
4064 case I915_MADV_WILLNEED:
4065 break;
4066 default:
4067 return -EINVAL;
4068 }
4069
Chris Wilson03ac0642016-07-20 13:31:51 +01004070 obj = i915_gem_object_lookup(file_priv, args->handle);
Chris Wilson1233e2d2016-10-28 13:58:37 +01004071 if (!obj)
4072 return -ENOENT;
4073
4074 err = mutex_lock_interruptible(&obj->mm.lock);
4075 if (err)
4076 goto out;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004077
Chris Wilsonf1fa4f42017-10-13 21:26:13 +01004078 if (i915_gem_object_has_pages(obj) &&
Chris Wilson3e510a82016-08-05 10:14:23 +01004079 i915_gem_object_is_tiled(obj) &&
Daniel Vetter656bfa32014-11-20 09:26:30 +01004080 dev_priv->quirks & QUIRK_PIN_SWIZZLED_PAGES) {
Chris Wilsonbc0629a2016-11-01 10:03:17 +00004081 if (obj->mm.madv == I915_MADV_WILLNEED) {
4082 GEM_BUG_ON(!obj->mm.quirked);
Chris Wilsona4f5ea62016-10-28 13:58:35 +01004083 __i915_gem_object_unpin_pages(obj);
Chris Wilsonbc0629a2016-11-01 10:03:17 +00004084 obj->mm.quirked = false;
4085 }
4086 if (args->madv == I915_MADV_WILLNEED) {
Chris Wilson2c3a3f42016-11-04 10:30:01 +00004087 GEM_BUG_ON(obj->mm.quirked);
Chris Wilsona4f5ea62016-10-28 13:58:35 +01004088 __i915_gem_object_pin_pages(obj);
Chris Wilsonbc0629a2016-11-01 10:03:17 +00004089 obj->mm.quirked = true;
4090 }
Daniel Vetter656bfa32014-11-20 09:26:30 +01004091 }
4092
Chris Wilsona4f5ea62016-10-28 13:58:35 +01004093 if (obj->mm.madv != __I915_MADV_PURGED)
4094 obj->mm.madv = args->madv;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004095
Chris Wilson6c085a72012-08-20 11:40:46 +02004096 /* if the object is no longer attached, discard its backing storage */
Chris Wilsonf1fa4f42017-10-13 21:26:13 +01004097 if (obj->mm.madv == I915_MADV_DONTNEED &&
4098 !i915_gem_object_has_pages(obj))
Chris Wilson2d7ef392009-09-20 23:13:10 +01004099 i915_gem_object_truncate(obj);
4100
Chris Wilsona4f5ea62016-10-28 13:58:35 +01004101 args->retained = obj->mm.madv != __I915_MADV_PURGED;
Chris Wilson1233e2d2016-10-28 13:58:37 +01004102 mutex_unlock(&obj->mm.lock);
Chris Wilsonbb6baf72009-09-22 14:24:13 +01004103
Chris Wilson1233e2d2016-10-28 13:58:37 +01004104out:
Chris Wilsonf8c417c2016-07-20 13:31:53 +01004105 i915_gem_object_put(obj);
Chris Wilson1233e2d2016-10-28 13:58:37 +01004106 return err;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004107}
4108
Chris Wilson5b8c8ae2016-11-16 19:07:04 +00004109static void
Chris Wilson21950ee2019-02-05 13:00:05 +00004110frontbuffer_retire(struct i915_active_request *active,
4111 struct i915_request *request)
Chris Wilson5b8c8ae2016-11-16 19:07:04 +00004112{
4113 struct drm_i915_gem_object *obj =
4114 container_of(active, typeof(*obj), frontbuffer_write);
4115
Chris Wilsond59b21e2017-02-22 11:40:49 +00004116 intel_fb_obj_flush(obj, ORIGIN_CS);
Chris Wilson5b8c8ae2016-11-16 19:07:04 +00004117}
4118
Chris Wilson37e680a2012-06-07 15:38:42 +01004119void i915_gem_object_init(struct drm_i915_gem_object *obj,
4120 const struct drm_i915_gem_object_ops *ops)
Chris Wilson0327d6b2012-08-11 15:41:06 +01004121{
Chris Wilson1233e2d2016-10-28 13:58:37 +01004122 mutex_init(&obj->mm.lock);
4123
Chris Wilson528cbd12019-01-28 10:23:54 +00004124 spin_lock_init(&obj->vma.lock);
4125 INIT_LIST_HEAD(&obj->vma.list);
4126
Chris Wilsond1b48c12017-08-16 09:52:08 +01004127 INIT_LIST_HEAD(&obj->lut_list);
Chris Wilson8d9d5742015-04-07 16:20:38 +01004128 INIT_LIST_HEAD(&obj->batch_pool_link);
Chris Wilson0327d6b2012-08-11 15:41:06 +01004129
Chris Wilson8811d612018-11-09 09:03:11 +00004130 init_rcu_head(&obj->rcu);
4131
Chris Wilson37e680a2012-06-07 15:38:42 +01004132 obj->ops = ops;
4133
Chris Wilsond07f0e52016-10-28 13:58:44 +01004134 reservation_object_init(&obj->__builtin_resv);
4135 obj->resv = &obj->__builtin_resv;
4136
Chris Wilson50349242016-08-18 17:17:04 +01004137 obj->frontbuffer_ggtt_origin = ORIGIN_GTT;
Chris Wilson21950ee2019-02-05 13:00:05 +00004138 i915_active_request_init(&obj->frontbuffer_write,
4139 NULL, frontbuffer_retire);
Chris Wilsona4f5ea62016-10-28 13:58:35 +01004140
4141 obj->mm.madv = I915_MADV_WILLNEED;
4142 INIT_RADIX_TREE(&obj->mm.get_page.radix, GFP_KERNEL | __GFP_NOWARN);
4143 mutex_init(&obj->mm.get_page.lock);
Chris Wilson0327d6b2012-08-11 15:41:06 +01004144
Dave Gordonf19ec8c2016-07-04 11:34:37 +01004145 i915_gem_info_add_obj(to_i915(obj->base.dev), obj->base.size);
Chris Wilson0327d6b2012-08-11 15:41:06 +01004146}
4147
Chris Wilson37e680a2012-06-07 15:38:42 +01004148static const struct drm_i915_gem_object_ops i915_gem_object_ops = {
Tvrtko Ursulin3599a912016-11-01 14:44:10 +00004149 .flags = I915_GEM_OBJECT_HAS_STRUCT_PAGE |
4150 I915_GEM_OBJECT_IS_SHRINKABLE,
Chris Wilson7c55e2c2017-03-07 12:03:38 +00004151
Chris Wilson37e680a2012-06-07 15:38:42 +01004152 .get_pages = i915_gem_object_get_pages_gtt,
4153 .put_pages = i915_gem_object_put_pages_gtt,
Chris Wilson7c55e2c2017-03-07 12:03:38 +00004154
4155 .pwrite = i915_gem_object_pwrite_gtt,
Chris Wilson37e680a2012-06-07 15:38:42 +01004156};
4157
Matthew Auld465c4032017-10-06 23:18:14 +01004158static int i915_gem_object_create_shmem(struct drm_device *dev,
4159 struct drm_gem_object *obj,
4160 size_t size)
4161{
4162 struct drm_i915_private *i915 = to_i915(dev);
4163 unsigned long flags = VM_NORESERVE;
4164 struct file *filp;
4165
4166 drm_gem_private_object_init(dev, obj, size);
4167
4168 if (i915->mm.gemfs)
4169 filp = shmem_file_setup_with_mnt(i915->mm.gemfs, "i915", size,
4170 flags);
4171 else
4172 filp = shmem_file_setup("i915", size, flags);
4173
4174 if (IS_ERR(filp))
4175 return PTR_ERR(filp);
4176
4177 obj->filp = filp;
4178
4179 return 0;
4180}
4181
Chris Wilsonb4bcbe22016-10-18 13:02:49 +01004182struct drm_i915_gem_object *
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +00004183i915_gem_object_create(struct drm_i915_private *dev_priv, u64 size)
Daniel Vetterac52bc52010-04-09 19:05:06 +00004184{
Daniel Vetterc397b902010-04-09 19:05:07 +00004185 struct drm_i915_gem_object *obj;
Hugh Dickins5949eac2011-06-27 16:18:18 -07004186 struct address_space *mapping;
Chris Wilsonb8f55be2017-08-11 12:11:16 +01004187 unsigned int cache_level;
Daniel Vetter1a240d42012-11-29 22:18:51 +01004188 gfp_t mask;
Chris Wilsonfe3db792016-04-25 13:32:13 +01004189 int ret;
Daniel Vetterc397b902010-04-09 19:05:07 +00004190
Chris Wilsonb4bcbe22016-10-18 13:02:49 +01004191 /* There is a prevalence of the assumption that we fit the object's
4192 * page count inside a 32bit _signed_ variable. Let's document this and
4193 * catch if we ever need to fix it. In the meantime, if you do spot
4194 * such a local variable, please consider fixing!
4195 */
Tvrtko Ursulin7a3ee5d2017-03-30 17:31:30 +01004196 if (size >> PAGE_SHIFT > INT_MAX)
Chris Wilsonb4bcbe22016-10-18 13:02:49 +01004197 return ERR_PTR(-E2BIG);
4198
4199 if (overflows_type(size, obj->base.size))
4200 return ERR_PTR(-E2BIG);
4201
Tvrtko Ursulin187685c2016-12-01 14:16:36 +00004202 obj = i915_gem_object_alloc(dev_priv);
Daniel Vetterc397b902010-04-09 19:05:07 +00004203 if (obj == NULL)
Chris Wilsonfe3db792016-04-25 13:32:13 +01004204 return ERR_PTR(-ENOMEM);
Daniel Vetterc397b902010-04-09 19:05:07 +00004205
Matthew Auld465c4032017-10-06 23:18:14 +01004206 ret = i915_gem_object_create_shmem(&dev_priv->drm, &obj->base, size);
Chris Wilsonfe3db792016-04-25 13:32:13 +01004207 if (ret)
4208 goto fail;
Daniel Vetterc397b902010-04-09 19:05:07 +00004209
Chris Wilsonbed1ea92012-05-24 20:48:12 +01004210 mask = GFP_HIGHUSER | __GFP_RECLAIMABLE;
Jani Nikulac0f86832016-12-07 12:13:04 +02004211 if (IS_I965GM(dev_priv) || IS_I965G(dev_priv)) {
Chris Wilsonbed1ea92012-05-24 20:48:12 +01004212 /* 965gm cannot relocate objects above 4GiB. */
4213 mask &= ~__GFP_HIGHMEM;
4214 mask |= __GFP_DMA32;
4215 }
4216
Al Viro93c76a32015-12-04 23:45:44 -05004217 mapping = obj->base.filp->f_mapping;
Chris Wilsonbed1ea92012-05-24 20:48:12 +01004218 mapping_set_gfp_mask(mapping, mask);
Chris Wilson4846bf02017-06-09 12:03:46 +01004219 GEM_BUG_ON(!(mapping_gfp_mask(mapping) & __GFP_RECLAIM));
Hugh Dickins5949eac2011-06-27 16:18:18 -07004220
Chris Wilson37e680a2012-06-07 15:38:42 +01004221 i915_gem_object_init(obj, &i915_gem_object_ops);
Chris Wilson73aa8082010-09-30 11:46:12 +01004222
Christian Königc0a51fd2018-02-16 13:43:38 +01004223 obj->write_domain = I915_GEM_DOMAIN_CPU;
4224 obj->read_domains = I915_GEM_DOMAIN_CPU;
Daniel Vetterc397b902010-04-09 19:05:07 +00004225
Chris Wilsonb8f55be2017-08-11 12:11:16 +01004226 if (HAS_LLC(dev_priv))
Eugeni Dodonov3d29b842012-01-17 14:43:53 -02004227 /* On some devices, we can have the GPU use the LLC (the CPU
Eric Anholta1871112011-03-29 16:59:55 -07004228 * cache) for about a 10% performance improvement
4229 * compared to uncached. Graphics requests other than
4230 * display scanout are coherent with the CPU in
4231 * accessing this cache. This means in this mode we
4232 * don't need to clflush on the CPU side, and on the
4233 * GPU side we only need to flush internal caches to
4234 * get data visible to the CPU.
4235 *
4236 * However, we maintain the display planes as UC, and so
4237 * need to rebind when first used as such.
4238 */
Chris Wilsonb8f55be2017-08-11 12:11:16 +01004239 cache_level = I915_CACHE_LLC;
4240 else
4241 cache_level = I915_CACHE_NONE;
Eric Anholta1871112011-03-29 16:59:55 -07004242
Chris Wilsonb8f55be2017-08-11 12:11:16 +01004243 i915_gem_object_set_cache_coherency(obj, cache_level);
Chris Wilsone27ab732017-06-15 13:38:49 +01004244
Daniel Vetterd861e332013-07-24 23:25:03 +02004245 trace_i915_gem_object_create(obj);
4246
Chris Wilson05394f32010-11-08 19:18:58 +00004247 return obj;
Chris Wilsonfe3db792016-04-25 13:32:13 +01004248
4249fail:
4250 i915_gem_object_free(obj);
Chris Wilsonfe3db792016-04-25 13:32:13 +01004251 return ERR_PTR(ret);
Daniel Vetterac52bc52010-04-09 19:05:06 +00004252}
4253
Chris Wilson340fbd82014-05-22 09:16:52 +01004254static bool discard_backing_storage(struct drm_i915_gem_object *obj)
4255{
4256 /* If we are the last user of the backing storage (be it shmemfs
4257 * pages or stolen etc), we know that the pages are going to be
4258 * immediately released. In this case, we can then skip copying
4259 * back the contents from the GPU.
4260 */
4261
Chris Wilsona4f5ea62016-10-28 13:58:35 +01004262 if (obj->mm.madv != I915_MADV_WILLNEED)
Chris Wilson340fbd82014-05-22 09:16:52 +01004263 return false;
4264
4265 if (obj->base.filp == NULL)
4266 return true;
4267
4268 /* At first glance, this looks racy, but then again so would be
4269 * userspace racing mmap against close. However, the first external
4270 * reference to the filp can only be obtained through the
4271 * i915_gem_mmap_ioctl() which safeguards us against the user
4272 * acquiring such a reference whilst we are in the middle of
4273 * freeing the object.
4274 */
4275 return atomic_long_read(&obj->base.filp->f_count) == 1;
4276}
4277
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004278static void __i915_gem_free_objects(struct drm_i915_private *i915,
4279 struct llist_node *freed)
Chris Wilsonbe726152010-07-23 23:18:50 +01004280{
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004281 struct drm_i915_gem_object *obj, *on;
Chris Wilson538ef962019-01-14 14:21:18 +00004282 intel_wakeref_t wakeref;
Chris Wilsonbe726152010-07-23 23:18:50 +01004283
Chris Wilson538ef962019-01-14 14:21:18 +00004284 wakeref = intel_runtime_pm_get(i915);
Chris Wilsoncc731f52017-10-13 21:26:21 +01004285 llist_for_each_entry_safe(obj, on, freed, freed) {
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004286 struct i915_vma *vma, *vn;
Paulo Zanonif65c9162013-11-27 18:20:34 -02004287
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004288 trace_i915_gem_object_destroy(obj);
4289
Chris Wilsoncc731f52017-10-13 21:26:21 +01004290 mutex_lock(&i915->drm.struct_mutex);
4291
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004292 GEM_BUG_ON(i915_gem_object_is_active(obj));
Chris Wilson528cbd12019-01-28 10:23:54 +00004293 list_for_each_entry_safe(vma, vn, &obj->vma.list, obj_link) {
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004294 GEM_BUG_ON(i915_vma_is_active(vma));
4295 vma->flags &= ~I915_VMA_PIN_MASK;
Chris Wilson3365e222018-05-03 20:51:14 +01004296 i915_vma_destroy(vma);
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004297 }
Chris Wilson528cbd12019-01-28 10:23:54 +00004298 GEM_BUG_ON(!list_empty(&obj->vma.list));
4299 GEM_BUG_ON(!RB_EMPTY_ROOT(&obj->vma.tree));
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004300
Chris Wilsonf2123812017-10-16 12:40:37 +01004301 /* This serializes freeing with the shrinker. Since the free
4302 * is delayed, first by RCU then by the workqueue, we want the
4303 * shrinker to be able to free pages of unreferenced objects,
4304 * or else we may oom whilst there are plenty of deferred
4305 * freed objects.
4306 */
4307 if (i915_gem_object_has_pages(obj)) {
4308 spin_lock(&i915->mm.obj_lock);
4309 list_del_init(&obj->mm.link);
4310 spin_unlock(&i915->mm.obj_lock);
4311 }
4312
Chris Wilsoncc731f52017-10-13 21:26:21 +01004313 mutex_unlock(&i915->drm.struct_mutex);
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004314
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004315 GEM_BUG_ON(obj->bind_count);
Chris Wilsona65adaf2017-10-09 09:43:57 +01004316 GEM_BUG_ON(obj->userfault_count);
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004317 GEM_BUG_ON(atomic_read(&obj->frontbuffer_bits));
Chris Wilson67b48042017-08-22 12:05:16 +01004318 GEM_BUG_ON(!list_empty(&obj->lut_list));
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004319
4320 if (obj->ops->release)
4321 obj->ops->release(obj);
4322
4323 if (WARN_ON(i915_gem_object_has_pinned_pages(obj)))
4324 atomic_set(&obj->mm.pages_pin_count, 0);
Chris Wilson548625e2016-11-01 12:11:34 +00004325 __i915_gem_object_put_pages(obj, I915_MM_NORMAL);
Chris Wilsonf1fa4f42017-10-13 21:26:13 +01004326 GEM_BUG_ON(i915_gem_object_has_pages(obj));
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004327
4328 if (obj->base.import_attach)
4329 drm_prime_gem_destroy(&obj->base, NULL);
4330
Chris Wilsond07f0e52016-10-28 13:58:44 +01004331 reservation_object_fini(&obj->__builtin_resv);
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004332 drm_gem_object_release(&obj->base);
4333 i915_gem_info_remove_obj(i915, obj->base.size);
4334
4335 kfree(obj->bit_17);
4336 i915_gem_object_free(obj);
Chris Wilsoncc731f52017-10-13 21:26:21 +01004337
Chris Wilsonc9c704712018-02-19 22:06:31 +00004338 GEM_BUG_ON(!atomic_read(&i915->mm.free_count));
4339 atomic_dec(&i915->mm.free_count);
4340
Chris Wilsoncc731f52017-10-13 21:26:21 +01004341 if (on)
4342 cond_resched();
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004343 }
Chris Wilson538ef962019-01-14 14:21:18 +00004344 intel_runtime_pm_put(i915, wakeref);
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004345}
4346
4347static void i915_gem_flush_free_objects(struct drm_i915_private *i915)
4348{
4349 struct llist_node *freed;
4350
Chris Wilson87701b42017-10-13 21:26:20 +01004351 /* Free the oldest, most stale object to keep the free_list short */
4352 freed = NULL;
4353 if (!llist_empty(&i915->mm.free_list)) { /* quick test for hotpath */
4354 /* Only one consumer of llist_del_first() allowed */
4355 spin_lock(&i915->mm.free_lock);
4356 freed = llist_del_first(&i915->mm.free_list);
4357 spin_unlock(&i915->mm.free_lock);
4358 }
4359 if (unlikely(freed)) {
4360 freed->next = NULL;
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004361 __i915_gem_free_objects(i915, freed);
Chris Wilson87701b42017-10-13 21:26:20 +01004362 }
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004363}
4364
4365static void __i915_gem_free_work(struct work_struct *work)
4366{
4367 struct drm_i915_private *i915 =
4368 container_of(work, struct drm_i915_private, mm.free_work);
4369 struct llist_node *freed;
Chris Wilson26e12f82011-03-20 11:20:19 +00004370
Chris Wilson2ef1e722018-01-15 20:57:59 +00004371 /*
4372 * All file-owned VMA should have been released by this point through
Chris Wilsonb1f788c2016-08-04 07:52:45 +01004373 * i915_gem_close_object(), or earlier by i915_gem_context_close().
4374 * However, the object may also be bound into the global GTT (e.g.
4375 * older GPUs without per-process support, or for direct access through
4376 * the GTT either for the user or for scanout). Those VMA still need to
4377 * unbound now.
4378 */
Chris Wilson1488fc02012-04-24 15:47:31 +01004379
Chris Wilsonf991c492017-11-06 11:15:08 +00004380 spin_lock(&i915->mm.free_lock);
Chris Wilson5ad08be2017-04-07 11:25:51 +01004381 while ((freed = llist_del_all(&i915->mm.free_list))) {
Chris Wilsonf991c492017-11-06 11:15:08 +00004382 spin_unlock(&i915->mm.free_lock);
4383
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004384 __i915_gem_free_objects(i915, freed);
Chris Wilson5ad08be2017-04-07 11:25:51 +01004385 if (need_resched())
Chris Wilsonf991c492017-11-06 11:15:08 +00004386 return;
4387
4388 spin_lock(&i915->mm.free_lock);
Chris Wilson5ad08be2017-04-07 11:25:51 +01004389 }
Chris Wilsonf991c492017-11-06 11:15:08 +00004390 spin_unlock(&i915->mm.free_lock);
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004391}
4392
4393static void __i915_gem_free_object_rcu(struct rcu_head *head)
4394{
4395 struct drm_i915_gem_object *obj =
4396 container_of(head, typeof(*obj), rcu);
4397 struct drm_i915_private *i915 = to_i915(obj->base.dev);
4398
Chris Wilson2ef1e722018-01-15 20:57:59 +00004399 /*
Chris Wilson8811d612018-11-09 09:03:11 +00004400 * We reuse obj->rcu for the freed list, so we had better not treat
4401 * it like a rcu_head from this point forwards. And we expect all
4402 * objects to be freed via this path.
4403 */
4404 destroy_rcu_head(&obj->rcu);
4405
4406 /*
Chris Wilson2ef1e722018-01-15 20:57:59 +00004407 * Since we require blocking on struct_mutex to unbind the freed
4408 * object from the GPU before releasing resources back to the
4409 * system, we can not do that directly from the RCU callback (which may
4410 * be a softirq context), but must instead then defer that work onto a
4411 * kthread. We use the RCU callback rather than move the freed object
4412 * directly onto the work queue so that we can mix between using the
4413 * worker and performing frees directly from subsequent allocations for
4414 * crude but effective memory throttling.
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004415 */
4416 if (llist_add(&obj->freed, &i915->mm.free_list))
Chris Wilsonbeacbd12018-01-15 12:28:45 +00004417 queue_work(i915->wq, &i915->mm.free_work);
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004418}
4419
4420void i915_gem_free_object(struct drm_gem_object *gem_obj)
4421{
4422 struct drm_i915_gem_object *obj = to_intel_bo(gem_obj);
4423
Chris Wilsonbc0629a2016-11-01 10:03:17 +00004424 if (obj->mm.quirked)
4425 __i915_gem_object_unpin_pages(obj);
4426
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004427 if (discard_backing_storage(obj))
4428 obj->mm.madv = I915_MADV_DONTNEED;
Daniel Vettera071fa02014-06-18 23:28:09 +02004429
Chris Wilson2ef1e722018-01-15 20:57:59 +00004430 /*
4431 * Before we free the object, make sure any pure RCU-only
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004432 * read-side critical sections are complete, e.g.
4433 * i915_gem_busy_ioctl(). For the corresponding synchronized
4434 * lookup see i915_gem_object_lookup_rcu().
4435 */
Chris Wilsonc9c704712018-02-19 22:06:31 +00004436 atomic_inc(&to_i915(obj->base.dev)->mm.free_count);
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004437 call_rcu(&obj->rcu, __i915_gem_free_object_rcu);
Chris Wilsonbe726152010-07-23 23:18:50 +01004438}
4439
Chris Wilsonf8a7fde2016-10-28 13:58:29 +01004440void __i915_gem_object_release_unless_active(struct drm_i915_gem_object *obj)
4441{
4442 lockdep_assert_held(&obj->base.dev->struct_mutex);
4443
Chris Wilsond1b48c12017-08-16 09:52:08 +01004444 if (!i915_gem_object_has_active_reference(obj) &&
4445 i915_gem_object_is_active(obj))
Chris Wilsonf8a7fde2016-10-28 13:58:29 +01004446 i915_gem_object_set_active_reference(obj);
4447 else
4448 i915_gem_object_put(obj);
4449}
4450
Chris Wilson24145512017-01-24 11:01:35 +00004451void i915_gem_sanitize(struct drm_i915_private *i915)
4452{
Chris Wilson538ef962019-01-14 14:21:18 +00004453 intel_wakeref_t wakeref;
4454
Chris Wilsonc3160da2018-05-31 09:22:45 +01004455 GEM_TRACE("\n");
4456
Chris Wilson538ef962019-01-14 14:21:18 +00004457 wakeref = intel_runtime_pm_get(i915);
Chris Wilsonc3160da2018-05-31 09:22:45 +01004458 intel_uncore_forcewake_get(i915, FORCEWAKE_ALL);
4459
4460 /*
4461 * As we have just resumed the machine and woken the device up from
4462 * deep PCI sleep (presumably D3_cold), assume the HW has been reset
4463 * back to defaults, recovering from whatever wedged state we left it
4464 * in and so worth trying to use the device once more.
4465 */
Chris Wilsonc41166f2019-02-20 14:56:37 +00004466 if (i915_terminally_wedged(i915))
Chris Wilsonf36325f2017-08-26 12:09:34 +01004467 i915_gem_unset_wedged(i915);
Chris Wilsonf36325f2017-08-26 12:09:34 +01004468
Chris Wilson24145512017-01-24 11:01:35 +00004469 /*
4470 * If we inherit context state from the BIOS or earlier occupants
4471 * of the GPU, the GPU may be in an inconsistent state when we
4472 * try to take over. The only way to remove the earlier state
4473 * is by resetting. However, resetting on earlier gen is tricky as
4474 * it may impact the display and we are uncertain about the stability
Joonas Lahtinenea117b82017-04-28 10:53:38 +03004475 * of the reset, so this could be applied to even earlier gen.
Chris Wilson24145512017-01-24 11:01:35 +00004476 */
Chris Wilson55277e12019-01-03 11:21:04 +00004477 intel_engines_sanitize(i915, false);
Chris Wilsonc3160da2018-05-31 09:22:45 +01004478
4479 intel_uncore_forcewake_put(i915, FORCEWAKE_ALL);
Chris Wilson538ef962019-01-14 14:21:18 +00004480 intel_runtime_pm_put(i915, wakeref);
Chris Wilsonc3160da2018-05-31 09:22:45 +01004481
Chris Wilsoneb8d0f52019-01-25 13:22:28 +00004482 mutex_lock(&i915->drm.struct_mutex);
Chris Wilson4dfacb02018-05-31 09:22:43 +01004483 i915_gem_contexts_lost(i915);
4484 mutex_unlock(&i915->drm.struct_mutex);
Chris Wilson24145512017-01-24 11:01:35 +00004485}
4486
Chris Wilsonbf061122018-07-09 14:02:04 +01004487int i915_gem_suspend(struct drm_i915_private *i915)
Eric Anholt673a3942008-07-30 12:06:12 -07004488{
Chris Wilson538ef962019-01-14 14:21:18 +00004489 intel_wakeref_t wakeref;
Chris Wilsondcff85c2016-08-05 10:14:11 +01004490 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004491
Chris Wilson09a4c022018-05-24 09:11:35 +01004492 GEM_TRACE("\n");
4493
Chris Wilson538ef962019-01-14 14:21:18 +00004494 wakeref = intel_runtime_pm_get(i915);
Chris Wilsonbf061122018-07-09 14:02:04 +01004495 intel_suspend_gt_powersave(i915);
Chris Wilson54b4f682016-07-21 21:16:19 +01004496
Chris Wilsoneb8d0f52019-01-25 13:22:28 +00004497 flush_workqueue(i915->wq);
4498
Chris Wilsonbf061122018-07-09 14:02:04 +01004499 mutex_lock(&i915->drm.struct_mutex);
Chris Wilson5ab57c72016-07-15 14:56:20 +01004500
Chris Wilsonbf061122018-07-09 14:02:04 +01004501 /*
4502 * We have to flush all the executing contexts to main memory so
Chris Wilson5ab57c72016-07-15 14:56:20 +01004503 * that they can saved in the hibernation image. To ensure the last
4504 * context image is coherent, we have to switch away from it. That
Chris Wilsonbf061122018-07-09 14:02:04 +01004505 * leaves the i915->kernel_context still active when
Chris Wilson5ab57c72016-07-15 14:56:20 +01004506 * we actually suspend, and its image in memory may not match the GPU
4507 * state. Fortunately, the kernel_context is disposable and we do
4508 * not rely on its state.
4509 */
Chris Wilsonc41166f2019-02-20 14:56:37 +00004510 if (!i915_reset_failed(i915)) {
Chris Wilsonbf061122018-07-09 14:02:04 +01004511 ret = i915_gem_switch_to_kernel_context(i915);
Chris Wilsonecf73eb2017-11-30 10:29:51 +00004512 if (ret)
4513 goto err_unlock;
Chris Wilson5ab57c72016-07-15 14:56:20 +01004514
Chris Wilsonbf061122018-07-09 14:02:04 +01004515 ret = i915_gem_wait_for_idle(i915,
Chris Wilsonecf73eb2017-11-30 10:29:51 +00004516 I915_WAIT_INTERRUPTIBLE |
Chris Wilson06060352018-05-31 09:22:44 +01004517 I915_WAIT_LOCKED |
Chris Wilsonec625fb2018-07-09 13:20:42 +01004518 I915_WAIT_FOR_IDLE_BOOST,
4519 MAX_SCHEDULE_TIMEOUT);
Chris Wilsonecf73eb2017-11-30 10:29:51 +00004520 if (ret && ret != -EIO)
4521 goto err_unlock;
Chris Wilsonf7403342013-09-13 23:57:04 +01004522
Chris Wilsonbf061122018-07-09 14:02:04 +01004523 assert_kernel_context_is_current(i915);
Chris Wilsonecf73eb2017-11-30 10:29:51 +00004524 }
Chris Wilson01f8f332018-07-17 09:41:21 +01004525 i915_retire_requests(i915); /* ensure we flush after wedging */
4526
Chris Wilsonbf061122018-07-09 14:02:04 +01004527 mutex_unlock(&i915->drm.struct_mutex);
Chris Wilsoneb8d0f52019-01-25 13:22:28 +00004528 i915_reset_flush(i915);
Chris Wilson45c5f202013-10-16 11:50:01 +01004529
Chris Wilsoneb8d0f52019-01-25 13:22:28 +00004530 drain_delayed_work(&i915->gt.retire_work);
Chris Wilsonbdeb9782016-12-23 14:57:56 +00004531
Chris Wilsonbf061122018-07-09 14:02:04 +01004532 /*
4533 * As the idle_work is rearming if it detects a race, play safe and
Chris Wilsonbdeb9782016-12-23 14:57:56 +00004534 * repeat the flush until it is definitely idle.
4535 */
Chris Wilsonbf061122018-07-09 14:02:04 +01004536 drain_delayed_work(&i915->gt.idle_work);
Chris Wilsonbdeb9782016-12-23 14:57:56 +00004537
Chris Wilsoneb8d0f52019-01-25 13:22:28 +00004538 intel_uc_suspend(i915);
4539
Chris Wilsonbf061122018-07-09 14:02:04 +01004540 /*
4541 * Assert that we successfully flushed all the work and
Chris Wilsonbdcf1202014-11-25 11:56:33 +00004542 * reset the GPU back to its idle, low power state.
4543 */
Chris Wilsonbf061122018-07-09 14:02:04 +01004544 WARN_ON(i915->gt.awake);
4545 if (WARN_ON(!intel_engines_are_idle(i915)))
4546 i915_gem_set_wedged(i915); /* no hope, discard everything */
Chris Wilsonbdcf1202014-11-25 11:56:33 +00004547
Chris Wilson538ef962019-01-14 14:21:18 +00004548 intel_runtime_pm_put(i915, wakeref);
Chris Wilsonec92ad02018-05-31 09:22:46 +01004549 return 0;
4550
4551err_unlock:
Chris Wilsonbf061122018-07-09 14:02:04 +01004552 mutex_unlock(&i915->drm.struct_mutex);
Chris Wilson538ef962019-01-14 14:21:18 +00004553 intel_runtime_pm_put(i915, wakeref);
Chris Wilsonec92ad02018-05-31 09:22:46 +01004554 return ret;
4555}
4556
4557void i915_gem_suspend_late(struct drm_i915_private *i915)
4558{
Chris Wilson9776f472018-06-01 15:41:24 +01004559 struct drm_i915_gem_object *obj;
4560 struct list_head *phases[] = {
4561 &i915->mm.unbound_list,
4562 &i915->mm.bound_list,
4563 NULL
4564 }, **phase;
4565
Imre Deak1c777c52016-10-12 17:46:37 +03004566 /*
4567 * Neither the BIOS, ourselves or any other kernel
4568 * expects the system to be in execlists mode on startup,
4569 * so we need to reset the GPU back to legacy mode. And the only
4570 * known way to disable logical contexts is through a GPU reset.
4571 *
4572 * So in order to leave the system in a known default configuration,
4573 * always reset the GPU upon unload and suspend. Afterwards we then
4574 * clean up the GEM state tracking, flushing off the requests and
4575 * leaving the system in a known idle state.
4576 *
4577 * Note that is of the upmost importance that the GPU is idle and
4578 * all stray writes are flushed *before* we dismantle the backing
4579 * storage for the pinned objects.
4580 *
4581 * However, since we are uncertain that resetting the GPU on older
4582 * machines is a good idea, we don't - just in case it leaves the
4583 * machine in an unusable condition.
4584 */
Chris Wilsoncad99462017-08-26 12:09:33 +01004585
Chris Wilson9776f472018-06-01 15:41:24 +01004586 mutex_lock(&i915->drm.struct_mutex);
4587 for (phase = phases; *phase; phase++) {
4588 list_for_each_entry(obj, *phase, mm.link)
4589 WARN_ON(i915_gem_object_set_to_gtt_domain(obj, false));
4590 }
4591 mutex_unlock(&i915->drm.struct_mutex);
4592
Chris Wilsonec92ad02018-05-31 09:22:46 +01004593 intel_uc_sanitize(i915);
4594 i915_gem_sanitize(i915);
Eric Anholt673a3942008-07-30 12:06:12 -07004595}
4596
Chris Wilson37cd3302017-11-12 11:27:38 +00004597void i915_gem_resume(struct drm_i915_private *i915)
Chris Wilson5ab57c72016-07-15 14:56:20 +01004598{
Chris Wilson4dfacb02018-05-31 09:22:43 +01004599 GEM_TRACE("\n");
4600
Chris Wilson37cd3302017-11-12 11:27:38 +00004601 WARN_ON(i915->gt.awake);
Chris Wilson5ab57c72016-07-15 14:56:20 +01004602
Chris Wilson37cd3302017-11-12 11:27:38 +00004603 mutex_lock(&i915->drm.struct_mutex);
4604 intel_uncore_forcewake_get(i915, FORCEWAKE_ALL);
Imre Deak31ab49a2016-11-07 11:20:05 +02004605
Chris Wilson37cd3302017-11-12 11:27:38 +00004606 i915_gem_restore_gtt_mappings(i915);
4607 i915_gem_restore_fences(i915);
Chris Wilson5ab57c72016-07-15 14:56:20 +01004608
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00004609 /*
4610 * As we didn't flush the kernel context before suspend, we cannot
Chris Wilson5ab57c72016-07-15 14:56:20 +01004611 * guarantee that the context image is complete. So let's just reset
4612 * it and start again.
4613 */
Chris Wilson37cd3302017-11-12 11:27:38 +00004614 i915->gt.resume(i915);
Chris Wilson5ab57c72016-07-15 14:56:20 +01004615
Chris Wilson37cd3302017-11-12 11:27:38 +00004616 if (i915_gem_init_hw(i915))
4617 goto err_wedged;
4618
Michal Wajdeczko7cfca4a2018-03-02 11:15:49 +00004619 intel_uc_resume(i915);
Chris Wilson7469c622017-11-14 13:03:00 +00004620
Chris Wilson37cd3302017-11-12 11:27:38 +00004621 /* Always reload a context for powersaving. */
4622 if (i915_gem_switch_to_kernel_context(i915))
4623 goto err_wedged;
4624
4625out_unlock:
4626 intel_uncore_forcewake_put(i915, FORCEWAKE_ALL);
4627 mutex_unlock(&i915->drm.struct_mutex);
4628 return;
4629
4630err_wedged:
Chris Wilsonc41166f2019-02-20 14:56:37 +00004631 if (!i915_reset_failed(i915)) {
4632 dev_err(i915->drm.dev,
4633 "Failed to re-initialize GPU, declaring it wedged!\n");
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00004634 i915_gem_set_wedged(i915);
4635 }
Chris Wilson37cd3302017-11-12 11:27:38 +00004636 goto out_unlock;
Chris Wilson5ab57c72016-07-15 14:56:20 +01004637}
4638
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00004639void i915_gem_init_swizzling(struct drm_i915_private *dev_priv)
Daniel Vetterf691e2f2012-02-02 09:58:12 +01004640{
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00004641 if (INTEL_GEN(dev_priv) < 5 ||
Daniel Vetterf691e2f2012-02-02 09:58:12 +01004642 dev_priv->mm.bit_6_swizzle_x == I915_BIT_6_SWIZZLE_NONE)
4643 return;
4644
4645 I915_WRITE(DISP_ARB_CTL, I915_READ(DISP_ARB_CTL) |
4646 DISP_TILE_SURFACE_SWIZZLING);
4647
Lucas De Marchicf819ef2018-12-12 10:10:43 -08004648 if (IS_GEN(dev_priv, 5))
Daniel Vetter11782b02012-01-31 16:47:55 +01004649 return;
4650
Daniel Vetterf691e2f2012-02-02 09:58:12 +01004651 I915_WRITE(TILECTL, I915_READ(TILECTL) | TILECTL_SWZCTL);
Lucas De Marchicf819ef2018-12-12 10:10:43 -08004652 if (IS_GEN(dev_priv, 6))
Daniel Vetter6b26c862012-04-24 14:04:12 +02004653 I915_WRITE(ARB_MODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_SNB));
Lucas De Marchicf819ef2018-12-12 10:10:43 -08004654 else if (IS_GEN(dev_priv, 7))
Daniel Vetter6b26c862012-04-24 14:04:12 +02004655 I915_WRITE(ARB_MODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_IVB));
Lucas De Marchicf819ef2018-12-12 10:10:43 -08004656 else if (IS_GEN(dev_priv, 8))
Ben Widawsky31a53362013-11-02 21:07:04 -07004657 I915_WRITE(GAMTARBMODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_BDW));
Ben Widawsky8782e262012-12-18 10:31:23 -08004658 else
4659 BUG();
Daniel Vetterf691e2f2012-02-02 09:58:12 +01004660}
Daniel Vettere21af882012-02-09 20:53:27 +01004661
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01004662static void init_unused_ring(struct drm_i915_private *dev_priv, u32 base)
Ville Syrjälä81e7f202014-08-15 01:21:55 +03004663{
Ville Syrjälä81e7f202014-08-15 01:21:55 +03004664 I915_WRITE(RING_CTL(base), 0);
4665 I915_WRITE(RING_HEAD(base), 0);
4666 I915_WRITE(RING_TAIL(base), 0);
4667 I915_WRITE(RING_START(base), 0);
4668}
4669
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01004670static void init_unused_rings(struct drm_i915_private *dev_priv)
Ville Syrjälä81e7f202014-08-15 01:21:55 +03004671{
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01004672 if (IS_I830(dev_priv)) {
4673 init_unused_ring(dev_priv, PRB1_BASE);
4674 init_unused_ring(dev_priv, SRB0_BASE);
4675 init_unused_ring(dev_priv, SRB1_BASE);
4676 init_unused_ring(dev_priv, SRB2_BASE);
4677 init_unused_ring(dev_priv, SRB3_BASE);
Lucas De Marchicf819ef2018-12-12 10:10:43 -08004678 } else if (IS_GEN(dev_priv, 2)) {
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01004679 init_unused_ring(dev_priv, SRB0_BASE);
4680 init_unused_ring(dev_priv, SRB1_BASE);
Lucas De Marchicf819ef2018-12-12 10:10:43 -08004681 } else if (IS_GEN(dev_priv, 3)) {
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01004682 init_unused_ring(dev_priv, PRB1_BASE);
4683 init_unused_ring(dev_priv, PRB2_BASE);
Ville Syrjälä81e7f202014-08-15 01:21:55 +03004684 }
4685}
4686
Chris Wilson20a8a742017-02-08 14:30:31 +00004687static int __i915_gem_restart_engines(void *data)
Ben Widawsky4fc7c972013-02-08 11:49:24 -08004688{
Chris Wilson20a8a742017-02-08 14:30:31 +00004689 struct drm_i915_private *i915 = data;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00004690 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05304691 enum intel_engine_id id;
Chris Wilson20a8a742017-02-08 14:30:31 +00004692 int err;
4693
4694 for_each_engine(engine, i915, id) {
4695 err = engine->init_hw(engine);
Chris Wilson8177e112018-02-07 11:15:45 +00004696 if (err) {
4697 DRM_ERROR("Failed to restart %s (%d)\n",
4698 engine->name, err);
Chris Wilson20a8a742017-02-08 14:30:31 +00004699 return err;
Chris Wilson8177e112018-02-07 11:15:45 +00004700 }
Chris Wilson20a8a742017-02-08 14:30:31 +00004701 }
4702
Chris Wilson2d5eaad2019-02-26 10:24:00 +00004703 intel_engines_set_scheduler_caps(i915);
4704
Chris Wilson20a8a742017-02-08 14:30:31 +00004705 return 0;
4706}
4707
4708int i915_gem_init_hw(struct drm_i915_private *dev_priv)
4709{
Chris Wilsond200cda2016-04-28 09:56:44 +01004710 int ret;
Ben Widawsky4fc7c972013-02-08 11:49:24 -08004711
Chris Wilsonde867c22016-10-25 13:16:02 +01004712 dev_priv->gt.last_init_time = ktime_get();
4713
Chris Wilson5e4f5182015-02-13 14:35:59 +00004714 /* Double layer security blanket, see i915_gem_init() */
4715 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
4716
Tvrtko Ursulin0031fb92016-11-04 14:42:44 +00004717 if (HAS_EDRAM(dev_priv) && INTEL_GEN(dev_priv) < 9)
Ben Widawsky05e21cc2013-07-04 11:02:04 -07004718 I915_WRITE(HSW_IDICR, I915_READ(HSW_IDICR) | IDIHASHMSK(0xf));
Ben Widawsky4fc7c972013-02-08 11:49:24 -08004719
Tvrtko Ursulin772c2a52016-10-13 11:03:01 +01004720 if (IS_HASWELL(dev_priv))
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01004721 I915_WRITE(MI_PREDICATE_RESULT_2, IS_HSW_GT3(dev_priv) ?
Ville Syrjälä0bf21342013-11-29 14:56:12 +02004722 LOWER_SLICE_ENABLED : LOWER_SLICE_DISABLED);
Rodrigo Vivi94353732013-08-28 16:45:46 -03004723
Tvrtko Ursulin094304b2018-12-03 12:50:10 +00004724 /* Apply the GT workarounds... */
Tvrtko Ursulin25d140f2018-12-03 13:33:19 +00004725 intel_gt_apply_workarounds(dev_priv);
Tvrtko Ursulin094304b2018-12-03 12:50:10 +00004726 /* ...and determine whether they are sticking. */
4727 intel_gt_verify_workarounds(dev_priv, "init");
Oscar Mateo59b449d2018-04-10 09:12:47 -07004728
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00004729 i915_gem_init_swizzling(dev_priv);
Ben Widawsky4fc7c972013-02-08 11:49:24 -08004730
Daniel Vetterd5abdfd2014-11-20 09:45:19 +01004731 /*
4732 * At least 830 can leave some of the unused rings
4733 * "active" (ie. head != tail) after resume which
4734 * will prevent c3 entry. Makes sure all unused rings
4735 * are totally idle.
4736 */
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01004737 init_unused_rings(dev_priv);
Daniel Vetterd5abdfd2014-11-20 09:45:19 +01004738
Dave Gordoned54c1a2016-01-19 19:02:54 +00004739 BUG_ON(!dev_priv->kernel_context);
Chris Wilsonc41166f2019-02-20 14:56:37 +00004740 ret = i915_terminally_wedged(dev_priv);
4741 if (ret)
Chris Wilson6f74b362017-10-15 15:37:25 +01004742 goto out;
John Harrison90638cc2015-05-29 17:43:37 +01004743
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00004744 ret = i915_ppgtt_init_hw(dev_priv);
John Harrison4ad2fd82015-06-18 13:11:20 +01004745 if (ret) {
Chris Wilson8177e112018-02-07 11:15:45 +00004746 DRM_ERROR("Enabling PPGTT failed (%d)\n", ret);
John Harrison4ad2fd82015-06-18 13:11:20 +01004747 goto out;
4748 }
4749
Jackie Lif08e2032018-03-13 17:32:53 -07004750 ret = intel_wopcm_init_hw(&dev_priv->wopcm);
4751 if (ret) {
4752 DRM_ERROR("Enabling WOPCM failed (%d)\n", ret);
4753 goto out;
4754 }
4755
Michał Winiarski9bdc3572017-10-25 18:25:19 +01004756 /* We can't enable contexts until all firmware is loaded */
4757 ret = intel_uc_init_hw(dev_priv);
Chris Wilson8177e112018-02-07 11:15:45 +00004758 if (ret) {
4759 DRM_ERROR("Enabling uc failed (%d)\n", ret);
Michał Winiarski9bdc3572017-10-25 18:25:19 +01004760 goto out;
Chris Wilson8177e112018-02-07 11:15:45 +00004761 }
Michał Winiarski9bdc3572017-10-25 18:25:19 +01004762
Tvrtko Ursulinbf9e8422016-12-01 14:16:38 +00004763 intel_mocs_init_l3cc_table(dev_priv);
Peter Antoine0ccdacf2016-04-13 15:03:25 +01004764
Chris Wilson136109c2017-11-02 13:14:30 +00004765 /* Only when the HW is re-initialised, can we replay the requests */
4766 ret = __i915_gem_restart_engines(dev_priv);
Michal Wajdeczkob96f6eb2018-06-05 12:24:43 +00004767 if (ret)
4768 goto cleanup_uc;
Michał Winiarski60c0a662018-07-12 14:48:10 +02004769
Chris Wilson5e4f5182015-02-13 14:35:59 +00004770 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Michał Winiarski60c0a662018-07-12 14:48:10 +02004771
4772 return 0;
Michal Wajdeczkob96f6eb2018-06-05 12:24:43 +00004773
4774cleanup_uc:
4775 intel_uc_fini_hw(dev_priv);
Michał Winiarski60c0a662018-07-12 14:48:10 +02004776out:
4777 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
4778
4779 return ret;
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004780}
4781
Chris Wilsond2b4b972017-11-10 14:26:33 +00004782static int __intel_engines_record_defaults(struct drm_i915_private *i915)
4783{
4784 struct i915_gem_context *ctx;
4785 struct intel_engine_cs *engine;
4786 enum intel_engine_id id;
4787 int err;
4788
4789 /*
4790 * As we reset the gpu during very early sanitisation, the current
4791 * register state on the GPU should reflect its defaults values.
4792 * We load a context onto the hw (with restore-inhibit), then switch
4793 * over to a second context to save that default register state. We
4794 * can then prime every new context with that state so they all start
4795 * from the same default HW values.
4796 */
4797
4798 ctx = i915_gem_context_create_kernel(i915, 0);
4799 if (IS_ERR(ctx))
4800 return PTR_ERR(ctx);
4801
4802 for_each_engine(engine, i915, id) {
Chris Wilsone61e0f52018-02-21 09:56:36 +00004803 struct i915_request *rq;
Chris Wilsond2b4b972017-11-10 14:26:33 +00004804
Chris Wilsone61e0f52018-02-21 09:56:36 +00004805 rq = i915_request_alloc(engine, ctx);
Chris Wilsond2b4b972017-11-10 14:26:33 +00004806 if (IS_ERR(rq)) {
4807 err = PTR_ERR(rq);
4808 goto out_ctx;
4809 }
4810
Chris Wilson3fef5cd2017-11-20 10:20:02 +00004811 err = 0;
Chris Wilsond2b4b972017-11-10 14:26:33 +00004812 if (engine->init_context)
4813 err = engine->init_context(rq);
4814
Chris Wilson697b9a82018-06-12 11:51:35 +01004815 i915_request_add(rq);
Chris Wilsond2b4b972017-11-10 14:26:33 +00004816 if (err)
4817 goto err_active;
4818 }
4819
4820 err = i915_gem_switch_to_kernel_context(i915);
4821 if (err)
4822 goto err_active;
4823
Chris Wilson2621cef2018-07-09 13:20:43 +01004824 if (i915_gem_wait_for_idle(i915, I915_WAIT_LOCKED, HZ / 5)) {
4825 i915_gem_set_wedged(i915);
4826 err = -EIO; /* Caller will declare us wedged */
Chris Wilsond2b4b972017-11-10 14:26:33 +00004827 goto err_active;
Chris Wilson2621cef2018-07-09 13:20:43 +01004828 }
Chris Wilsond2b4b972017-11-10 14:26:33 +00004829
4830 assert_kernel_context_is_current(i915);
4831
Chris Wilson8e1cb322018-09-20 17:13:43 +01004832 /*
4833 * Immediately park the GPU so that we enable powersaving and
4834 * treat it as idle. The next time we issue a request, we will
4835 * unpark and start using the engine->pinned_default_state, otherwise
4836 * it is in limbo and an early reset may fail.
4837 */
4838 __i915_gem_park(i915);
4839
Chris Wilsond2b4b972017-11-10 14:26:33 +00004840 for_each_engine(engine, i915, id) {
4841 struct i915_vma *state;
Chris Wilson37d7c9c2018-09-14 13:35:03 +01004842 void *vaddr;
Chris Wilsond2b4b972017-11-10 14:26:33 +00004843
Chris Wilson666424a2018-09-14 13:35:04 +01004844 GEM_BUG_ON(to_intel_context(ctx, engine)->pin_count);
4845
Chris Wilsonab82a062018-04-30 14:15:01 +01004846 state = to_intel_context(ctx, engine)->state;
Chris Wilsond2b4b972017-11-10 14:26:33 +00004847 if (!state)
4848 continue;
4849
4850 /*
4851 * As we will hold a reference to the logical state, it will
4852 * not be torn down with the context, and importantly the
4853 * object will hold onto its vma (making it possible for a
4854 * stray GTT write to corrupt our defaults). Unmap the vma
4855 * from the GTT to prevent such accidents and reclaim the
4856 * space.
4857 */
4858 err = i915_vma_unbind(state);
4859 if (err)
4860 goto err_active;
4861
4862 err = i915_gem_object_set_to_cpu_domain(state->obj, false);
4863 if (err)
4864 goto err_active;
4865
4866 engine->default_state = i915_gem_object_get(state->obj);
Chris Wilson37d7c9c2018-09-14 13:35:03 +01004867
4868 /* Check we can acquire the image of the context state */
4869 vaddr = i915_gem_object_pin_map(engine->default_state,
Chris Wilson666424a2018-09-14 13:35:04 +01004870 I915_MAP_FORCE_WB);
Chris Wilson37d7c9c2018-09-14 13:35:03 +01004871 if (IS_ERR(vaddr)) {
4872 err = PTR_ERR(vaddr);
4873 goto err_active;
4874 }
4875
4876 i915_gem_object_unpin_map(engine->default_state);
Chris Wilsond2b4b972017-11-10 14:26:33 +00004877 }
4878
4879 if (IS_ENABLED(CONFIG_DRM_I915_DEBUG_GEM)) {
4880 unsigned int found = intel_engines_has_context_isolation(i915);
4881
4882 /*
4883 * Make sure that classes with multiple engine instances all
4884 * share the same basic configuration.
4885 */
4886 for_each_engine(engine, i915, id) {
4887 unsigned int bit = BIT(engine->uabi_class);
4888 unsigned int expected = engine->default_state ? bit : 0;
4889
4890 if ((found & bit) != expected) {
4891 DRM_ERROR("mismatching default context state for class %d on engine %s\n",
4892 engine->uabi_class, engine->name);
4893 }
4894 }
4895 }
4896
4897out_ctx:
4898 i915_gem_context_set_closed(ctx);
4899 i915_gem_context_put(ctx);
4900 return err;
4901
4902err_active:
4903 /*
4904 * If we have to abandon now, we expect the engines to be idle
4905 * and ready to be torn-down. First try to flush any remaining
4906 * request, ensure we are pointing at the kernel context and
4907 * then remove it.
4908 */
4909 if (WARN_ON(i915_gem_switch_to_kernel_context(i915)))
4910 goto out_ctx;
4911
Chris Wilsonec625fb2018-07-09 13:20:42 +01004912 if (WARN_ON(i915_gem_wait_for_idle(i915,
4913 I915_WAIT_LOCKED,
4914 MAX_SCHEDULE_TIMEOUT)))
Chris Wilsond2b4b972017-11-10 14:26:33 +00004915 goto out_ctx;
4916
4917 i915_gem_contexts_lost(i915);
4918 goto out_ctx;
4919}
4920
Chris Wilson51797492018-12-04 14:15:16 +00004921static int
4922i915_gem_init_scratch(struct drm_i915_private *i915, unsigned int size)
4923{
4924 struct drm_i915_gem_object *obj;
4925 struct i915_vma *vma;
4926 int ret;
4927
4928 obj = i915_gem_object_create_stolen(i915, size);
4929 if (!obj)
4930 obj = i915_gem_object_create_internal(i915, size);
4931 if (IS_ERR(obj)) {
4932 DRM_ERROR("Failed to allocate scratch page\n");
4933 return PTR_ERR(obj);
4934 }
4935
4936 vma = i915_vma_instance(obj, &i915->ggtt.vm, NULL);
4937 if (IS_ERR(vma)) {
4938 ret = PTR_ERR(vma);
4939 goto err_unref;
4940 }
4941
4942 ret = i915_vma_pin(vma, 0, 0, PIN_GLOBAL | PIN_HIGH);
4943 if (ret)
4944 goto err_unref;
4945
4946 i915->gt.scratch = vma;
4947 return 0;
4948
4949err_unref:
4950 i915_gem_object_put(obj);
4951 return ret;
4952}
4953
4954static void i915_gem_fini_scratch(struct drm_i915_private *i915)
4955{
4956 i915_vma_unpin_and_release(&i915->gt.scratch, 0);
4957}
4958
Tvrtko Ursulinbf9e8422016-12-01 14:16:38 +00004959int i915_gem_init(struct drm_i915_private *dev_priv)
Chris Wilson1070a422012-04-24 15:47:41 +01004960{
Chris Wilson1070a422012-04-24 15:47:41 +01004961 int ret;
4962
Changbin Du52b24162018-05-08 17:07:05 +08004963 /* We need to fallback to 4K pages if host doesn't support huge gtt. */
4964 if (intel_vgpu_active(dev_priv) && !intel_vgpu_has_huge_gtt(dev_priv))
Matthew Auldda9fe3f32017-10-06 23:18:31 +01004965 mkwrite_device_info(dev_priv)->page_sizes =
4966 I915_GTT_PAGE_SIZE_4K;
4967
Chris Wilson94312822017-05-03 10:39:18 +01004968 dev_priv->mm.unordered_timeline = dma_fence_context_alloc(1);
Chris Wilson57822dc2017-02-22 11:40:48 +00004969
Chris Wilsonfb5c5512017-11-20 20:55:00 +00004970 if (HAS_LOGICAL_RING_CONTEXTS(dev_priv)) {
Chris Wilson821ed7d2016-09-09 14:11:53 +01004971 dev_priv->gt.resume = intel_lr_context_resume;
Tvrtko Ursulin117897f2016-03-16 11:00:40 +00004972 dev_priv->gt.cleanup_engine = intel_logical_ring_cleanup;
Chris Wilsonfb5c5512017-11-20 20:55:00 +00004973 } else {
4974 dev_priv->gt.resume = intel_legacy_submission_resume;
4975 dev_priv->gt.cleanup_engine = intel_engine_cleanup;
Oscar Mateoa83014d2014-07-24 17:04:21 +01004976 }
4977
Chris Wilson1e345562019-01-28 10:23:56 +00004978 i915_timelines_init(dev_priv);
4979
Chris Wilsonee487002017-11-22 17:26:21 +00004980 ret = i915_gem_init_userptr(dev_priv);
4981 if (ret)
4982 return ret;
4983
Sagar Arun Kamble70deead2018-01-24 21:16:58 +05304984 ret = intel_uc_init_misc(dev_priv);
Michał Winiarski3176ff42017-12-13 23:13:47 +01004985 if (ret)
4986 return ret;
4987
Michal Wajdeczkof7dc0152018-06-28 14:15:21 +00004988 ret = intel_wopcm_init(&dev_priv->wopcm);
4989 if (ret)
4990 goto err_uc_misc;
4991
Chris Wilson5e4f5182015-02-13 14:35:59 +00004992 /* This is just a security blanket to placate dragons.
4993 * On some systems, we very sporadically observe that the first TLBs
4994 * used by the CS may be stale, despite us poking the TLB reset. If
4995 * we hold the forcewake during initialisation these problems
4996 * just magically go away.
4997 */
Chris Wilsonee487002017-11-22 17:26:21 +00004998 mutex_lock(&dev_priv->drm.struct_mutex);
Chris Wilson5e4f5182015-02-13 14:35:59 +00004999 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
5000
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01005001 ret = i915_gem_init_ggtt(dev_priv);
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00005002 if (ret) {
5003 GEM_BUG_ON(ret == -EIO);
5004 goto err_unlock;
5005 }
Jesse Barnesd62b4892013-03-08 10:45:53 -08005006
Chris Wilson51797492018-12-04 14:15:16 +00005007 ret = i915_gem_init_scratch(dev_priv,
Lucas De Marchicf819ef2018-12-12 10:10:43 -08005008 IS_GEN(dev_priv, 2) ? SZ_256K : PAGE_SIZE);
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00005009 if (ret) {
5010 GEM_BUG_ON(ret == -EIO);
5011 goto err_ggtt;
5012 }
Ben Widawsky2fa48d82013-12-06 14:11:04 -08005013
Chris Wilson51797492018-12-04 14:15:16 +00005014 ret = i915_gem_contexts_init(dev_priv);
5015 if (ret) {
5016 GEM_BUG_ON(ret == -EIO);
5017 goto err_scratch;
5018 }
5019
Tvrtko Ursulinbf9e8422016-12-01 14:16:38 +00005020 ret = intel_engines_init(dev_priv);
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00005021 if (ret) {
5022 GEM_BUG_ON(ret == -EIO);
5023 goto err_context;
5024 }
Daniel Vetter53ca26c2012-04-26 23:28:03 +02005025
Chris Wilsonf58d13d2017-11-10 14:26:29 +00005026 intel_init_gt_powersave(dev_priv);
5027
Michał Winiarski61b5c152017-12-13 23:13:48 +01005028 ret = intel_uc_init(dev_priv);
Chris Wilsoncc6a8182017-11-10 14:26:30 +00005029 if (ret)
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00005030 goto err_pm;
Chris Wilsoncc6a8182017-11-10 14:26:30 +00005031
Michał Winiarski61b5c152017-12-13 23:13:48 +01005032 ret = i915_gem_init_hw(dev_priv);
5033 if (ret)
5034 goto err_uc_init;
5035
Chris Wilsoncc6a8182017-11-10 14:26:30 +00005036 /*
5037 * Despite its name intel_init_clock_gating applies both display
5038 * clock gating workarounds; GT mmio workarounds and the occasional
5039 * GT power context workaround. Worse, sometimes it includes a context
5040 * register workaround which we need to apply before we record the
5041 * default HW state for all contexts.
5042 *
5043 * FIXME: break up the workarounds and apply them at the right time!
5044 */
5045 intel_init_clock_gating(dev_priv);
5046
Chris Wilsond2b4b972017-11-10 14:26:33 +00005047 ret = __intel_engines_record_defaults(dev_priv);
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00005048 if (ret)
5049 goto err_init_hw;
5050
5051 if (i915_inject_load_failure()) {
5052 ret = -ENODEV;
5053 goto err_init_hw;
5054 }
5055
5056 if (i915_inject_load_failure()) {
5057 ret = -EIO;
5058 goto err_init_hw;
5059 }
5060
5061 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
5062 mutex_unlock(&dev_priv->drm.struct_mutex);
5063
5064 return 0;
5065
5066 /*
5067 * Unwinding is complicated by that we want to handle -EIO to mean
5068 * disable GPU submission but keep KMS alive. We want to mark the
5069 * HW as irrevisibly wedged, but keep enough state around that the
5070 * driver doesn't explode during runtime.
5071 */
5072err_init_hw:
Chris Wilson8571a052018-06-06 15:54:41 +01005073 mutex_unlock(&dev_priv->drm.struct_mutex);
5074
5075 WARN_ON(i915_gem_suspend(dev_priv));
5076 i915_gem_suspend_late(dev_priv);
5077
Chris Wilson8bcf9f72018-07-10 10:44:20 +01005078 i915_gem_drain_workqueue(dev_priv);
5079
Chris Wilson8571a052018-06-06 15:54:41 +01005080 mutex_lock(&dev_priv->drm.struct_mutex);
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00005081 intel_uc_fini_hw(dev_priv);
Michał Winiarski61b5c152017-12-13 23:13:48 +01005082err_uc_init:
5083 intel_uc_fini(dev_priv);
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00005084err_pm:
5085 if (ret != -EIO) {
5086 intel_cleanup_gt_powersave(dev_priv);
5087 i915_gem_cleanup_engines(dev_priv);
5088 }
5089err_context:
5090 if (ret != -EIO)
5091 i915_gem_contexts_fini(dev_priv);
Chris Wilson51797492018-12-04 14:15:16 +00005092err_scratch:
5093 i915_gem_fini_scratch(dev_priv);
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00005094err_ggtt:
5095err_unlock:
5096 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
5097 mutex_unlock(&dev_priv->drm.struct_mutex);
5098
Michal Wajdeczkof7dc0152018-06-28 14:15:21 +00005099err_uc_misc:
Sagar Arun Kamble70deead2018-01-24 21:16:58 +05305100 intel_uc_fini_misc(dev_priv);
Sagar Arun Kambleda943b52018-01-10 18:24:16 +05305101
Chris Wilson1e345562019-01-28 10:23:56 +00005102 if (ret != -EIO) {
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00005103 i915_gem_cleanup_userptr(dev_priv);
Chris Wilson1e345562019-01-28 10:23:56 +00005104 i915_timelines_fini(dev_priv);
5105 }
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00005106
Chris Wilson60990322014-04-09 09:19:42 +01005107 if (ret == -EIO) {
Chris Wilson7ed43df2018-07-26 09:50:32 +01005108 mutex_lock(&dev_priv->drm.struct_mutex);
5109
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00005110 /*
5111 * Allow engine initialisation to fail by marking the GPU as
Chris Wilson60990322014-04-09 09:19:42 +01005112 * wedged. But we only want to do this where the GPU is angry,
5113 * for all other failure, such as an allocation failure, bail.
5114 */
Chris Wilsonc41166f2019-02-20 14:56:37 +00005115 if (!i915_reset_failed(dev_priv)) {
Chris Wilson51c18bf2018-06-09 12:10:58 +01005116 i915_load_error(dev_priv,
5117 "Failed to initialize GPU, declaring it wedged!\n");
Chris Wilson6f74b362017-10-15 15:37:25 +01005118 i915_gem_set_wedged(dev_priv);
5119 }
Chris Wilson7ed43df2018-07-26 09:50:32 +01005120
5121 /* Minimal basic recovery for KMS */
5122 ret = i915_ggtt_enable_hw(dev_priv);
5123 i915_gem_restore_gtt_mappings(dev_priv);
5124 i915_gem_restore_fences(dev_priv);
5125 intel_init_clock_gating(dev_priv);
5126
5127 mutex_unlock(&dev_priv->drm.struct_mutex);
Chris Wilson1070a422012-04-24 15:47:41 +01005128 }
5129
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00005130 i915_gem_drain_freed_objects(dev_priv);
Chris Wilson60990322014-04-09 09:19:42 +01005131 return ret;
Chris Wilson1070a422012-04-24 15:47:41 +01005132}
5133
Michal Wajdeczko8979187a2018-06-04 09:00:32 +00005134void i915_gem_fini(struct drm_i915_private *dev_priv)
5135{
5136 i915_gem_suspend_late(dev_priv);
Chris Wilson30b710842018-08-12 23:36:29 +01005137 intel_disable_gt_powersave(dev_priv);
Michal Wajdeczko8979187a2018-06-04 09:00:32 +00005138
5139 /* Flush any outstanding unpin_work. */
5140 i915_gem_drain_workqueue(dev_priv);
5141
5142 mutex_lock(&dev_priv->drm.struct_mutex);
5143 intel_uc_fini_hw(dev_priv);
5144 intel_uc_fini(dev_priv);
5145 i915_gem_cleanup_engines(dev_priv);
5146 i915_gem_contexts_fini(dev_priv);
Chris Wilson51797492018-12-04 14:15:16 +00005147 i915_gem_fini_scratch(dev_priv);
Michal Wajdeczko8979187a2018-06-04 09:00:32 +00005148 mutex_unlock(&dev_priv->drm.struct_mutex);
5149
Tvrtko Ursulin25d140f2018-12-03 13:33:19 +00005150 intel_wa_list_free(&dev_priv->gt_wa_list);
5151
Chris Wilson30b710842018-08-12 23:36:29 +01005152 intel_cleanup_gt_powersave(dev_priv);
5153
Michal Wajdeczko8979187a2018-06-04 09:00:32 +00005154 intel_uc_fini_misc(dev_priv);
5155 i915_gem_cleanup_userptr(dev_priv);
Chris Wilson1e345562019-01-28 10:23:56 +00005156 i915_timelines_fini(dev_priv);
Michal Wajdeczko8979187a2018-06-04 09:00:32 +00005157
5158 i915_gem_drain_freed_objects(dev_priv);
5159
5160 WARN_ON(!list_empty(&dev_priv->contexts.list));
5161}
5162
Chris Wilson24145512017-01-24 11:01:35 +00005163void i915_gem_init_mmio(struct drm_i915_private *i915)
5164{
5165 i915_gem_sanitize(i915);
5166}
5167
Zou Nan hai8187a2b2010-05-21 09:08:55 +08005168void
Tvrtko Ursulincb15d9f2016-12-01 14:16:39 +00005169i915_gem_cleanup_engines(struct drm_i915_private *dev_priv)
Zou Nan hai8187a2b2010-05-21 09:08:55 +08005170{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00005171 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05305172 enum intel_engine_id id;
Zou Nan hai8187a2b2010-05-21 09:08:55 +08005173
Akash Goel3b3f1652016-10-13 22:44:48 +05305174 for_each_engine(engine, dev_priv, id)
Tvrtko Ursulin117897f2016-03-16 11:00:40 +00005175 dev_priv->gt.cleanup_engine(engine);
Zou Nan hai8187a2b2010-05-21 09:08:55 +08005176}
5177
Eric Anholt673a3942008-07-30 12:06:12 -07005178void
Imre Deak40ae4e12016-03-16 14:54:03 +02005179i915_gem_load_init_fences(struct drm_i915_private *dev_priv)
5180{
Chris Wilson49ef5292016-08-18 17:17:00 +01005181 int i;
Imre Deak40ae4e12016-03-16 14:54:03 +02005182
Tvrtko Ursulinc56b89f2018-02-09 21:58:46 +00005183 if (INTEL_GEN(dev_priv) >= 7 && !IS_VALLEYVIEW(dev_priv) &&
Imre Deak40ae4e12016-03-16 14:54:03 +02005184 !IS_CHERRYVIEW(dev_priv))
5185 dev_priv->num_fence_regs = 32;
Tvrtko Ursulinc56b89f2018-02-09 21:58:46 +00005186 else if (INTEL_GEN(dev_priv) >= 4 ||
Jani Nikula73f67aa2016-12-07 22:48:09 +02005187 IS_I945G(dev_priv) || IS_I945GM(dev_priv) ||
5188 IS_G33(dev_priv) || IS_PINEVIEW(dev_priv))
Imre Deak40ae4e12016-03-16 14:54:03 +02005189 dev_priv->num_fence_regs = 16;
5190 else
5191 dev_priv->num_fence_regs = 8;
5192
Chris Wilsonc0336662016-05-06 15:40:21 +01005193 if (intel_vgpu_active(dev_priv))
Imre Deak40ae4e12016-03-16 14:54:03 +02005194 dev_priv->num_fence_regs =
5195 I915_READ(vgtif_reg(avail_rs.fence_num));
5196
5197 /* Initialize fence registers to zero */
Chris Wilson49ef5292016-08-18 17:17:00 +01005198 for (i = 0; i < dev_priv->num_fence_regs; i++) {
5199 struct drm_i915_fence_reg *fence = &dev_priv->fence_regs[i];
5200
5201 fence->i915 = dev_priv;
5202 fence->id = i;
5203 list_add_tail(&fence->link, &dev_priv->mm.fence_list);
5204 }
Tvrtko Ursulin4362f4f2016-11-16 08:55:33 +00005205 i915_gem_restore_fences(dev_priv);
Imre Deak40ae4e12016-03-16 14:54:03 +02005206
Tvrtko Ursulin4362f4f2016-11-16 08:55:33 +00005207 i915_gem_detect_bit_6_swizzle(dev_priv);
Imre Deak40ae4e12016-03-16 14:54:03 +02005208}
5209
Chris Wilson9c52d1c2017-11-10 23:24:47 +00005210static void i915_gem_init__mm(struct drm_i915_private *i915)
5211{
5212 spin_lock_init(&i915->mm.object_stat_lock);
5213 spin_lock_init(&i915->mm.obj_lock);
5214 spin_lock_init(&i915->mm.free_lock);
5215
5216 init_llist_head(&i915->mm.free_list);
5217
5218 INIT_LIST_HEAD(&i915->mm.unbound_list);
5219 INIT_LIST_HEAD(&i915->mm.bound_list);
5220 INIT_LIST_HEAD(&i915->mm.fence_list);
5221 INIT_LIST_HEAD(&i915->mm.userfault_list);
5222
5223 INIT_WORK(&i915->mm.free_work, __i915_gem_free_work);
5224}
5225
Michal Wajdeczkoa0de9082018-03-23 12:34:49 +00005226int i915_gem_init_early(struct drm_i915_private *dev_priv)
Eric Anholt673a3942008-07-30 12:06:12 -07005227{
Tvrtko Ursulina9335682016-11-02 15:14:59 +00005228 int err = -ENOMEM;
Chris Wilson42dcedd2012-11-15 11:32:30 +00005229
Tvrtko Ursulina9335682016-11-02 15:14:59 +00005230 dev_priv->objects = KMEM_CACHE(drm_i915_gem_object, SLAB_HWCACHE_ALIGN);
5231 if (!dev_priv->objects)
Chris Wilson73cb9702016-10-28 13:58:46 +01005232 goto err_out;
Chris Wilson73cb9702016-10-28 13:58:46 +01005233
Tvrtko Ursulina9335682016-11-02 15:14:59 +00005234 dev_priv->vmas = KMEM_CACHE(i915_vma, SLAB_HWCACHE_ALIGN);
5235 if (!dev_priv->vmas)
Chris Wilson73cb9702016-10-28 13:58:46 +01005236 goto err_objects;
Chris Wilson73cb9702016-10-28 13:58:46 +01005237
Chris Wilsond1b48c12017-08-16 09:52:08 +01005238 dev_priv->luts = KMEM_CACHE(i915_lut_handle, 0);
5239 if (!dev_priv->luts)
5240 goto err_vmas;
5241
Chris Wilson643b4502018-04-30 14:15:03 +01005242 INIT_LIST_HEAD(&dev_priv->gt.active_rings);
Chris Wilson3365e222018-05-03 20:51:14 +01005243 INIT_LIST_HEAD(&dev_priv->gt.closed_vma);
Chris Wilson643b4502018-04-30 14:15:03 +01005244
Chris Wilson9c52d1c2017-11-10 23:24:47 +00005245 i915_gem_init__mm(dev_priv);
Chris Wilsonf2123812017-10-16 12:40:37 +01005246
Chris Wilson67d97da2016-07-04 08:08:31 +01005247 INIT_DELAYED_WORK(&dev_priv->gt.retire_work,
Eric Anholt673a3942008-07-30 12:06:12 -07005248 i915_gem_retire_work_handler);
Chris Wilson67d97da2016-07-04 08:08:31 +01005249 INIT_DELAYED_WORK(&dev_priv->gt.idle_work,
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005250 i915_gem_idle_work_handler);
Chris Wilson1f15b762016-07-01 17:23:14 +01005251 init_waitqueue_head(&dev_priv->gpu_error.wait_queue);
Daniel Vetter1f83fee2012-11-15 17:17:22 +01005252 init_waitqueue_head(&dev_priv->gpu_error.reset_queue);
Chris Wilson18bb2bc2019-01-14 21:04:01 +00005253 mutex_init(&dev_priv->gpu_error.wedge_mutex);
Chris Wilson2caffbf2019-02-08 15:37:03 +00005254 init_srcu_struct(&dev_priv->gpu_error.reset_backoff_srcu);
Chris Wilson31169712009-09-14 16:50:28 +01005255
Joonas Lahtinen6f633402016-09-01 14:58:21 +03005256 atomic_set(&dev_priv->mm.bsd_engine_dispatch_index, 0);
5257
Chris Wilsonb5add952016-08-04 16:32:36 +01005258 spin_lock_init(&dev_priv->fb_tracking.lock);
Chris Wilson73cb9702016-10-28 13:58:46 +01005259
Matthew Auld465c4032017-10-06 23:18:14 +01005260 err = i915_gemfs_init(dev_priv);
5261 if (err)
5262 DRM_NOTE("Unable to create a private tmpfs mount, hugepage support will be disabled(%d).\n", err);
5263
Chris Wilson73cb9702016-10-28 13:58:46 +01005264 return 0;
5265
Chris Wilson73cb9702016-10-28 13:58:46 +01005266err_vmas:
5267 kmem_cache_destroy(dev_priv->vmas);
5268err_objects:
5269 kmem_cache_destroy(dev_priv->objects);
5270err_out:
5271 return err;
Eric Anholt673a3942008-07-30 12:06:12 -07005272}
Dave Airlie71acb5e2008-12-30 20:31:46 +10005273
Michal Wajdeczkoa0de9082018-03-23 12:34:49 +00005274void i915_gem_cleanup_early(struct drm_i915_private *dev_priv)
Imre Deakd64aa092016-01-19 15:26:29 +02005275{
Chris Wilsonc4d4c1c2017-02-10 16:35:23 +00005276 i915_gem_drain_freed_objects(dev_priv);
Chris Wilsonc9c704712018-02-19 22:06:31 +00005277 GEM_BUG_ON(!llist_empty(&dev_priv->mm.free_list));
5278 GEM_BUG_ON(atomic_read(&dev_priv->mm.free_count));
Chris Wilsonc4d4c1c2017-02-10 16:35:23 +00005279 WARN_ON(dev_priv->mm.object_count);
Matthew Auldea84aa72016-11-17 21:04:11 +00005280
Chris Wilson2caffbf2019-02-08 15:37:03 +00005281 cleanup_srcu_struct(&dev_priv->gpu_error.reset_backoff_srcu);
5282
Chris Wilsond1b48c12017-08-16 09:52:08 +01005283 kmem_cache_destroy(dev_priv->luts);
Imre Deakd64aa092016-01-19 15:26:29 +02005284 kmem_cache_destroy(dev_priv->vmas);
5285 kmem_cache_destroy(dev_priv->objects);
Chris Wilson0eafec62016-08-04 16:32:41 +01005286
5287 /* And ensure that our DESTROY_BY_RCU slabs are truly destroyed */
5288 rcu_barrier();
Matthew Auld465c4032017-10-06 23:18:14 +01005289
5290 i915_gemfs_fini(dev_priv);
Imre Deakd64aa092016-01-19 15:26:29 +02005291}
5292
Chris Wilson6a800ea2016-09-21 14:51:07 +01005293int i915_gem_freeze(struct drm_i915_private *dev_priv)
5294{
Chris Wilsond0aa3012017-04-07 11:25:49 +01005295 /* Discard all purgeable objects, let userspace recover those as
5296 * required after resuming.
5297 */
Chris Wilson6a800ea2016-09-21 14:51:07 +01005298 i915_gem_shrink_all(dev_priv);
Chris Wilson6a800ea2016-09-21 14:51:07 +01005299
Chris Wilson6a800ea2016-09-21 14:51:07 +01005300 return 0;
5301}
5302
Chris Wilson95c778d2018-06-01 15:41:25 +01005303int i915_gem_freeze_late(struct drm_i915_private *i915)
Chris Wilson461fb992016-05-14 07:26:33 +01005304{
5305 struct drm_i915_gem_object *obj;
Chris Wilson7aab2d52016-09-09 20:02:18 +01005306 struct list_head *phases[] = {
Chris Wilson95c778d2018-06-01 15:41:25 +01005307 &i915->mm.unbound_list,
5308 &i915->mm.bound_list,
Chris Wilson7aab2d52016-09-09 20:02:18 +01005309 NULL
Chris Wilson95c778d2018-06-01 15:41:25 +01005310 }, **phase;
Chris Wilson461fb992016-05-14 07:26:33 +01005311
Chris Wilson95c778d2018-06-01 15:41:25 +01005312 /*
5313 * Called just before we write the hibernation image.
Chris Wilson461fb992016-05-14 07:26:33 +01005314 *
5315 * We need to update the domain tracking to reflect that the CPU
5316 * will be accessing all the pages to create and restore from the
5317 * hibernation, and so upon restoration those pages will be in the
5318 * CPU domain.
5319 *
5320 * To make sure the hibernation image contains the latest state,
5321 * we update that state just before writing out the image.
Chris Wilson7aab2d52016-09-09 20:02:18 +01005322 *
5323 * To try and reduce the hibernation image, we manually shrink
Chris Wilsond0aa3012017-04-07 11:25:49 +01005324 * the objects as well, see i915_gem_freeze()
Chris Wilson461fb992016-05-14 07:26:33 +01005325 */
5326
Chris Wilson95c778d2018-06-01 15:41:25 +01005327 i915_gem_shrink(i915, -1UL, NULL, I915_SHRINK_UNBOUND);
5328 i915_gem_drain_freed_objects(i915);
Chris Wilson461fb992016-05-14 07:26:33 +01005329
Chris Wilson95c778d2018-06-01 15:41:25 +01005330 mutex_lock(&i915->drm.struct_mutex);
5331 for (phase = phases; *phase; phase++) {
5332 list_for_each_entry(obj, *phase, mm.link)
5333 WARN_ON(i915_gem_object_set_to_cpu_domain(obj, true));
Chris Wilson461fb992016-05-14 07:26:33 +01005334 }
Chris Wilson95c778d2018-06-01 15:41:25 +01005335 mutex_unlock(&i915->drm.struct_mutex);
Chris Wilson461fb992016-05-14 07:26:33 +01005336
5337 return 0;
5338}
5339
Chris Wilsonf787a5f2010-09-24 16:02:42 +01005340void i915_gem_release(struct drm_device *dev, struct drm_file *file)
Eric Anholtb9624422009-06-03 07:27:35 +00005341{
Chris Wilsonf787a5f2010-09-24 16:02:42 +01005342 struct drm_i915_file_private *file_priv = file->driver_priv;
Chris Wilsone61e0f52018-02-21 09:56:36 +00005343 struct i915_request *request;
Eric Anholtb9624422009-06-03 07:27:35 +00005344
5345 /* Clean up our request list when the client is going away, so that
5346 * later retire_requests won't dereference our soon-to-be-gone
5347 * file_priv.
5348 */
Chris Wilson1c255952010-09-26 11:03:27 +01005349 spin_lock(&file_priv->mm.lock);
Chris Wilsonc8659ef2017-03-02 12:25:25 +00005350 list_for_each_entry(request, &file_priv->mm.request_list, client_link)
Chris Wilsonf787a5f2010-09-24 16:02:42 +01005351 request->file_priv = NULL;
Chris Wilson1c255952010-09-26 11:03:27 +01005352 spin_unlock(&file_priv->mm.lock);
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005353}
5354
Chris Wilson829a0af2017-06-20 12:05:45 +01005355int i915_gem_open(struct drm_i915_private *i915, struct drm_file *file)
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005356{
5357 struct drm_i915_file_private *file_priv;
Ben Widawskye422b882013-12-06 14:10:58 -08005358 int ret;
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005359
Chris Wilsonc4c29d72016-11-09 10:45:07 +00005360 DRM_DEBUG("\n");
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005361
5362 file_priv = kzalloc(sizeof(*file_priv), GFP_KERNEL);
5363 if (!file_priv)
5364 return -ENOMEM;
5365
5366 file->driver_priv = file_priv;
Chris Wilson829a0af2017-06-20 12:05:45 +01005367 file_priv->dev_priv = i915;
Chris Wilsonab0e7ff2014-02-25 17:11:24 +02005368 file_priv->file = file;
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005369
5370 spin_lock_init(&file_priv->mm.lock);
5371 INIT_LIST_HEAD(&file_priv->mm.request_list);
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005372
Chris Wilsonc80ff162016-07-27 09:07:27 +01005373 file_priv->bsd_engine = -1;
Mika Kuoppala14921f32018-06-15 13:44:29 +03005374 file_priv->hang_timestamp = jiffies;
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00005375
Chris Wilson829a0af2017-06-20 12:05:45 +01005376 ret = i915_gem_context_open(i915, file);
Ben Widawskye422b882013-12-06 14:10:58 -08005377 if (ret)
5378 kfree(file_priv);
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005379
Ben Widawskye422b882013-12-06 14:10:58 -08005380 return ret;
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005381}
5382
Daniel Vetterb680c372014-09-19 18:27:27 +02005383/**
5384 * i915_gem_track_fb - update frontbuffer tracking
Geliang Tangd9072a32015-09-15 05:58:44 -07005385 * @old: current GEM buffer for the frontbuffer slots
5386 * @new: new GEM buffer for the frontbuffer slots
5387 * @frontbuffer_bits: bitmask of frontbuffer slots
Daniel Vetterb680c372014-09-19 18:27:27 +02005388 *
5389 * This updates the frontbuffer tracking bits @frontbuffer_bits by clearing them
5390 * from @old and setting them in @new. Both @old and @new can be NULL.
5391 */
Daniel Vettera071fa02014-06-18 23:28:09 +02005392void i915_gem_track_fb(struct drm_i915_gem_object *old,
5393 struct drm_i915_gem_object *new,
5394 unsigned frontbuffer_bits)
5395{
Chris Wilsonfaf5bf02016-08-04 16:32:37 +01005396 /* Control of individual bits within the mask are guarded by
5397 * the owning plane->mutex, i.e. we can never see concurrent
5398 * manipulation of individual bits. But since the bitfield as a whole
5399 * is updated using RMW, we need to use atomics in order to update
5400 * the bits.
5401 */
5402 BUILD_BUG_ON(INTEL_FRONTBUFFER_BITS_PER_PIPE * I915_MAX_PIPES >
Chris Wilson74f6e182018-09-26 11:47:07 +01005403 BITS_PER_TYPE(atomic_t));
Chris Wilsonfaf5bf02016-08-04 16:32:37 +01005404
Daniel Vettera071fa02014-06-18 23:28:09 +02005405 if (old) {
Chris Wilsonfaf5bf02016-08-04 16:32:37 +01005406 WARN_ON(!(atomic_read(&old->frontbuffer_bits) & frontbuffer_bits));
5407 atomic_andnot(frontbuffer_bits, &old->frontbuffer_bits);
Daniel Vettera071fa02014-06-18 23:28:09 +02005408 }
5409
5410 if (new) {
Chris Wilsonfaf5bf02016-08-04 16:32:37 +01005411 WARN_ON(atomic_read(&new->frontbuffer_bits) & frontbuffer_bits);
5412 atomic_or(frontbuffer_bits, &new->frontbuffer_bits);
Daniel Vettera071fa02014-06-18 23:28:09 +02005413 }
5414}
5415
Dave Gordonea702992015-07-09 19:29:02 +01005416/* Allocate a new GEM object and fill it with the supplied data */
5417struct drm_i915_gem_object *
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +00005418i915_gem_object_create_from_data(struct drm_i915_private *dev_priv,
Dave Gordonea702992015-07-09 19:29:02 +01005419 const void *data, size_t size)
5420{
5421 struct drm_i915_gem_object *obj;
Chris Wilsonbe062fa2017-03-17 19:46:48 +00005422 struct file *file;
5423 size_t offset;
5424 int err;
Dave Gordonea702992015-07-09 19:29:02 +01005425
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +00005426 obj = i915_gem_object_create(dev_priv, round_up(size, PAGE_SIZE));
Chris Wilsonfe3db792016-04-25 13:32:13 +01005427 if (IS_ERR(obj))
Dave Gordonea702992015-07-09 19:29:02 +01005428 return obj;
5429
Christian Königc0a51fd2018-02-16 13:43:38 +01005430 GEM_BUG_ON(obj->write_domain != I915_GEM_DOMAIN_CPU);
Dave Gordonea702992015-07-09 19:29:02 +01005431
Chris Wilsonbe062fa2017-03-17 19:46:48 +00005432 file = obj->base.filp;
5433 offset = 0;
5434 do {
5435 unsigned int len = min_t(typeof(size), size, PAGE_SIZE);
5436 struct page *page;
5437 void *pgdata, *vaddr;
Dave Gordonea702992015-07-09 19:29:02 +01005438
Chris Wilsonbe062fa2017-03-17 19:46:48 +00005439 err = pagecache_write_begin(file, file->f_mapping,
5440 offset, len, 0,
5441 &page, &pgdata);
5442 if (err < 0)
5443 goto fail;
Dave Gordonea702992015-07-09 19:29:02 +01005444
Chris Wilsonbe062fa2017-03-17 19:46:48 +00005445 vaddr = kmap(page);
5446 memcpy(vaddr, data, len);
5447 kunmap(page);
5448
5449 err = pagecache_write_end(file, file->f_mapping,
5450 offset, len, len,
5451 page, pgdata);
5452 if (err < 0)
5453 goto fail;
5454
5455 size -= len;
5456 data += len;
5457 offset += len;
5458 } while (size);
Dave Gordonea702992015-07-09 19:29:02 +01005459
5460 return obj;
5461
5462fail:
Chris Wilsonf8c417c2016-07-20 13:31:53 +01005463 i915_gem_object_put(obj);
Chris Wilsonbe062fa2017-03-17 19:46:48 +00005464 return ERR_PTR(err);
Dave Gordonea702992015-07-09 19:29:02 +01005465}
Chris Wilson96d77632016-10-28 13:58:33 +01005466
5467struct scatterlist *
5468i915_gem_object_get_sg(struct drm_i915_gem_object *obj,
5469 unsigned int n,
5470 unsigned int *offset)
5471{
Chris Wilsona4f5ea62016-10-28 13:58:35 +01005472 struct i915_gem_object_page_iter *iter = &obj->mm.get_page;
Chris Wilson96d77632016-10-28 13:58:33 +01005473 struct scatterlist *sg;
5474 unsigned int idx, count;
5475
5476 might_sleep();
5477 GEM_BUG_ON(n >= obj->base.size >> PAGE_SHIFT);
Chris Wilsona4f5ea62016-10-28 13:58:35 +01005478 GEM_BUG_ON(!i915_gem_object_has_pinned_pages(obj));
Chris Wilson96d77632016-10-28 13:58:33 +01005479
5480 /* As we iterate forward through the sg, we record each entry in a
5481 * radixtree for quick repeated (backwards) lookups. If we have seen
5482 * this index previously, we will have an entry for it.
5483 *
5484 * Initial lookup is O(N), but this is amortized to O(1) for
5485 * sequential page access (where each new request is consecutive
5486 * to the previous one). Repeated lookups are O(lg(obj->base.size)),
5487 * i.e. O(1) with a large constant!
5488 */
5489 if (n < READ_ONCE(iter->sg_idx))
5490 goto lookup;
5491
5492 mutex_lock(&iter->lock);
5493
5494 /* We prefer to reuse the last sg so that repeated lookup of this
5495 * (or the subsequent) sg are fast - comparing against the last
5496 * sg is faster than going through the radixtree.
5497 */
5498
5499 sg = iter->sg_pos;
5500 idx = iter->sg_idx;
5501 count = __sg_page_count(sg);
5502
5503 while (idx + count <= n) {
Matthew Wilcox3159f942017-11-03 13:30:42 -04005504 void *entry;
5505 unsigned long i;
Chris Wilson96d77632016-10-28 13:58:33 +01005506 int ret;
5507
5508 /* If we cannot allocate and insert this entry, or the
5509 * individual pages from this range, cancel updating the
5510 * sg_idx so that on this lookup we are forced to linearly
5511 * scan onwards, but on future lookups we will try the
5512 * insertion again (in which case we need to be careful of
5513 * the error return reporting that we have already inserted
5514 * this index).
5515 */
5516 ret = radix_tree_insert(&iter->radix, idx, sg);
5517 if (ret && ret != -EEXIST)
5518 goto scan;
5519
Matthew Wilcox3159f942017-11-03 13:30:42 -04005520 entry = xa_mk_value(idx);
Chris Wilson96d77632016-10-28 13:58:33 +01005521 for (i = 1; i < count; i++) {
Matthew Wilcox3159f942017-11-03 13:30:42 -04005522 ret = radix_tree_insert(&iter->radix, idx + i, entry);
Chris Wilson96d77632016-10-28 13:58:33 +01005523 if (ret && ret != -EEXIST)
5524 goto scan;
5525 }
5526
5527 idx += count;
5528 sg = ____sg_next(sg);
5529 count = __sg_page_count(sg);
5530 }
5531
5532scan:
5533 iter->sg_pos = sg;
5534 iter->sg_idx = idx;
5535
5536 mutex_unlock(&iter->lock);
5537
5538 if (unlikely(n < idx)) /* insertion completed by another thread */
5539 goto lookup;
5540
5541 /* In case we failed to insert the entry into the radixtree, we need
5542 * to look beyond the current sg.
5543 */
5544 while (idx + count <= n) {
5545 idx += count;
5546 sg = ____sg_next(sg);
5547 count = __sg_page_count(sg);
5548 }
5549
5550 *offset = n - idx;
5551 return sg;
5552
5553lookup:
5554 rcu_read_lock();
5555
5556 sg = radix_tree_lookup(&iter->radix, n);
5557 GEM_BUG_ON(!sg);
5558
5559 /* If this index is in the middle of multi-page sg entry,
Matthew Wilcox3159f942017-11-03 13:30:42 -04005560 * the radix tree will contain a value entry that points
Chris Wilson96d77632016-10-28 13:58:33 +01005561 * to the start of that range. We will return the pointer to
5562 * the base page and the offset of this page within the
5563 * sg entry's range.
5564 */
5565 *offset = 0;
Matthew Wilcox3159f942017-11-03 13:30:42 -04005566 if (unlikely(xa_is_value(sg))) {
5567 unsigned long base = xa_to_value(sg);
Chris Wilson96d77632016-10-28 13:58:33 +01005568
5569 sg = radix_tree_lookup(&iter->radix, base);
5570 GEM_BUG_ON(!sg);
5571
5572 *offset = n - base;
5573 }
5574
5575 rcu_read_unlock();
5576
5577 return sg;
5578}
5579
5580struct page *
5581i915_gem_object_get_page(struct drm_i915_gem_object *obj, unsigned int n)
5582{
5583 struct scatterlist *sg;
5584 unsigned int offset;
5585
5586 GEM_BUG_ON(!i915_gem_object_has_struct_page(obj));
5587
5588 sg = i915_gem_object_get_sg(obj, n, &offset);
5589 return nth_page(sg_page(sg), offset);
5590}
5591
5592/* Like i915_gem_object_get_page(), but mark the returned page dirty */
5593struct page *
5594i915_gem_object_get_dirty_page(struct drm_i915_gem_object *obj,
5595 unsigned int n)
5596{
5597 struct page *page;
5598
5599 page = i915_gem_object_get_page(obj, n);
Chris Wilsona4f5ea62016-10-28 13:58:35 +01005600 if (!obj->mm.dirty)
Chris Wilson96d77632016-10-28 13:58:33 +01005601 set_page_dirty(page);
5602
5603 return page;
5604}
5605
5606dma_addr_t
5607i915_gem_object_get_dma_address(struct drm_i915_gem_object *obj,
5608 unsigned long n)
5609{
5610 struct scatterlist *sg;
5611 unsigned int offset;
5612
5613 sg = i915_gem_object_get_sg(obj, n, &offset);
5614 return sg_dma_address(sg) + (offset << PAGE_SHIFT);
5615}
Chris Wilson935a2f72017-02-13 17:15:13 +00005616
Chris Wilson8eeb7902017-07-26 19:16:01 +01005617int i915_gem_object_attach_phys(struct drm_i915_gem_object *obj, int align)
5618{
5619 struct sg_table *pages;
5620 int err;
5621
5622 if (align > obj->base.size)
5623 return -EINVAL;
5624
5625 if (obj->ops == &i915_gem_phys_ops)
5626 return 0;
5627
5628 if (obj->ops != &i915_gem_object_ops)
5629 return -EINVAL;
5630
5631 err = i915_gem_object_unbind(obj);
5632 if (err)
5633 return err;
5634
5635 mutex_lock(&obj->mm.lock);
5636
5637 if (obj->mm.madv != I915_MADV_WILLNEED) {
5638 err = -EFAULT;
5639 goto err_unlock;
5640 }
5641
5642 if (obj->mm.quirked) {
5643 err = -EFAULT;
5644 goto err_unlock;
5645 }
5646
5647 if (obj->mm.mapping) {
5648 err = -EBUSY;
5649 goto err_unlock;
5650 }
5651
Chris Wilsonacd1c1e2018-06-11 08:55:32 +01005652 pages = __i915_gem_object_unset_pages(obj);
Chris Wilsonf2123812017-10-16 12:40:37 +01005653
Chris Wilson8eeb7902017-07-26 19:16:01 +01005654 obj->ops = &i915_gem_phys_ops;
5655
Chris Wilson8fb6a5d2017-07-26 19:16:02 +01005656 err = ____i915_gem_object_get_pages(obj);
Chris Wilson8eeb7902017-07-26 19:16:01 +01005657 if (err)
5658 goto err_xfer;
5659
5660 /* Perma-pin (until release) the physical set of pages */
5661 __i915_gem_object_pin_pages(obj);
5662
5663 if (!IS_ERR_OR_NULL(pages))
5664 i915_gem_object_ops.put_pages(obj, pages);
5665 mutex_unlock(&obj->mm.lock);
5666 return 0;
5667
5668err_xfer:
5669 obj->ops = &i915_gem_object_ops;
Chris Wilsonacd1c1e2018-06-11 08:55:32 +01005670 if (!IS_ERR_OR_NULL(pages)) {
5671 unsigned int sg_page_sizes = i915_sg_page_sizes(pages->sgl);
5672
5673 __i915_gem_object_set_pages(obj, pages, sg_page_sizes);
5674 }
Chris Wilson8eeb7902017-07-26 19:16:01 +01005675err_unlock:
5676 mutex_unlock(&obj->mm.lock);
5677 return err;
5678}
5679
Chris Wilson935a2f72017-02-13 17:15:13 +00005680#if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
5681#include "selftests/scatterlist.c"
Chris Wilson66d9cb52017-02-13 17:15:17 +00005682#include "selftests/mock_gem_device.c"
Chris Wilson44653982017-02-13 17:15:20 +00005683#include "selftests/huge_gem_object.c"
Matthew Auld40498662017-10-06 23:18:29 +01005684#include "selftests/huge_pages.c"
Chris Wilson8335fd62017-02-13 17:15:28 +00005685#include "selftests/i915_gem_object.c"
Chris Wilson17059452017-02-13 17:15:32 +00005686#include "selftests/i915_gem_coherency.c"
Chris Wilson3f51b7e12018-08-30 14:48:06 +01005687#include "selftests/i915_gem.c"
Chris Wilson935a2f72017-02-13 17:15:13 +00005688#endif