blob: 2b261524cfa484c80562c302ce2bd439f7be0841 [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"
45#include "i915_reset.h"
46#include "i915_trace.h"
47#include "i915_vgpu.h"
48
49#include "intel_drv.h"
50#include "intel_frontbuffer.h"
51#include "intel_mocs.h"
52#include "intel_workarounds.h"
53
Chris Wilsonfbbd37b2016-10-28 13:58:42 +010054static void i915_gem_flush_free_objects(struct drm_i915_private *i915);
Chris Wilson61050802012-04-17 15:31:31 +010055
Chris Wilson2c225692013-08-09 12:26:45 +010056static bool cpu_write_needs_clflush(struct drm_i915_gem_object *obj)
57{
Chris Wilsone27ab732017-06-15 13:38:49 +010058 if (obj->cache_dirty)
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +053059 return false;
60
Chris Wilsonb8f55be2017-08-11 12:11:16 +010061 if (!(obj->cache_coherent & I915_BO_CACHE_COHERENT_FOR_WRITE))
Chris Wilson2c225692013-08-09 12:26:45 +010062 return true;
63
Chris Wilsonbd3d2252017-10-13 21:26:14 +010064 return obj->pin_global; /* currently in use by HW, keep flushed */
Chris Wilson2c225692013-08-09 12:26:45 +010065}
66
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +053067static int
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +010068insert_mappable_node(struct i915_ggtt *ggtt,
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +053069 struct drm_mm_node *node, u32 size)
70{
71 memset(node, 0, sizeof(*node));
Chris Wilson82ad6442018-06-05 16:37:58 +010072 return drm_mm_insert_node_in_range(&ggtt->vm.mm, node,
Chris Wilson4e64e552017-02-02 21:04:38 +000073 size, 0, I915_COLOR_UNEVICTABLE,
74 0, ggtt->mappable_end,
75 DRM_MM_INSERT_LOW);
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +053076}
77
78static void
79remove_mappable_node(struct drm_mm_node *node)
80{
81 drm_mm_remove_node(node);
82}
83
Chris Wilson73aa8082010-09-30 11:46:12 +010084/* some bookkeeping */
85static void i915_gem_info_add_obj(struct drm_i915_private *dev_priv,
Chris Wilson3ef7f222016-10-18 13:02:48 +010086 u64 size)
Chris Wilson73aa8082010-09-30 11:46:12 +010087{
Daniel Vetterc20e8352013-07-24 22:40:23 +020088 spin_lock(&dev_priv->mm.object_stat_lock);
Chris Wilson73aa8082010-09-30 11:46:12 +010089 dev_priv->mm.object_count++;
90 dev_priv->mm.object_memory += size;
Daniel Vetterc20e8352013-07-24 22:40:23 +020091 spin_unlock(&dev_priv->mm.object_stat_lock);
Chris Wilson73aa8082010-09-30 11:46:12 +010092}
93
94static void i915_gem_info_remove_obj(struct drm_i915_private *dev_priv,
Chris Wilson3ef7f222016-10-18 13:02:48 +010095 u64 size)
Chris Wilson73aa8082010-09-30 11:46:12 +010096{
Daniel Vetterc20e8352013-07-24 22:40:23 +020097 spin_lock(&dev_priv->mm.object_stat_lock);
Chris Wilson73aa8082010-09-30 11:46:12 +010098 dev_priv->mm.object_count--;
99 dev_priv->mm.object_memory -= size;
Daniel Vetterc20e8352013-07-24 22:40:23 +0200100 spin_unlock(&dev_priv->mm.object_stat_lock);
Chris Wilson73aa8082010-09-30 11:46:12 +0100101}
102
Chris Wilsone4d20062018-04-06 16:51:44 +0100103static u32 __i915_gem_park(struct drm_i915_private *i915)
104{
Chris Wilson506d1f62019-01-14 14:21:11 +0000105 intel_wakeref_t wakeref;
106
Chris Wilson4dfacb02018-05-31 09:22:43 +0100107 GEM_TRACE("\n");
108
Chris Wilsone4d20062018-04-06 16:51:44 +0100109 lockdep_assert_held(&i915->drm.struct_mutex);
110 GEM_BUG_ON(i915->gt.active_requests);
Chris Wilson643b4502018-04-30 14:15:03 +0100111 GEM_BUG_ON(!list_empty(&i915->gt.active_rings));
Chris Wilsone4d20062018-04-06 16:51:44 +0100112
113 if (!i915->gt.awake)
114 return I915_EPOCH_INVALID;
115
116 GEM_BUG_ON(i915->gt.epoch == I915_EPOCH_INVALID);
117
118 /*
119 * Be paranoid and flush a concurrent interrupt to make sure
120 * we don't reactivate any irq tasklets after parking.
121 *
122 * FIXME: Note that even though we have waited for execlists to be idle,
123 * there may still be an in-flight interrupt even though the CSB
124 * is now empty. synchronize_irq() makes sure that a residual interrupt
125 * is completed before we continue, but it doesn't prevent the HW from
126 * raising a spurious interrupt later. To complete the shield we should
127 * coordinate disabling the CS irq with flushing the interrupts.
128 */
129 synchronize_irq(i915->drm.irq);
130
131 intel_engines_park(i915);
Chris Wilsona89d1f92018-05-02 17:38:39 +0100132 i915_timelines_park(i915);
Chris Wilsone4d20062018-04-06 16:51:44 +0100133
134 i915_pmu_gt_parked(i915);
Chris Wilson3365e222018-05-03 20:51:14 +0100135 i915_vma_parked(i915);
Chris Wilsone4d20062018-04-06 16:51:44 +0100136
Chris Wilson506d1f62019-01-14 14:21:11 +0000137 wakeref = fetch_and_zero(&i915->gt.awake);
138 GEM_BUG_ON(!wakeref);
Chris Wilsone4d20062018-04-06 16:51:44 +0100139
140 if (INTEL_GEN(i915) >= 6)
141 gen6_rps_idle(i915);
142
Chris Wilson8d761e72019-01-14 14:21:28 +0000143 intel_display_power_put(i915, POWER_DOMAIN_GT_IRQ, wakeref);
Chris Wilsone4d20062018-04-06 16:51:44 +0100144
145 return i915->gt.epoch;
146}
147
148void i915_gem_park(struct drm_i915_private *i915)
149{
Chris Wilson4dfacb02018-05-31 09:22:43 +0100150 GEM_TRACE("\n");
151
Chris Wilsone4d20062018-04-06 16:51:44 +0100152 lockdep_assert_held(&i915->drm.struct_mutex);
153 GEM_BUG_ON(i915->gt.active_requests);
154
155 if (!i915->gt.awake)
156 return;
157
158 /* Defer the actual call to __i915_gem_park() to prevent ping-pongs */
159 mod_delayed_work(i915->wq, &i915->gt.idle_work, msecs_to_jiffies(100));
160}
161
162void i915_gem_unpark(struct drm_i915_private *i915)
163{
Chris Wilson4dfacb02018-05-31 09:22:43 +0100164 GEM_TRACE("\n");
165
Chris Wilsone4d20062018-04-06 16:51:44 +0100166 lockdep_assert_held(&i915->drm.struct_mutex);
167 GEM_BUG_ON(!i915->gt.active_requests);
Chris Wilson8d761e72019-01-14 14:21:28 +0000168 assert_rpm_wakelock_held(i915);
Chris Wilsone4d20062018-04-06 16:51:44 +0100169
170 if (i915->gt.awake)
171 return;
172
Chris Wilsone4d20062018-04-06 16:51:44 +0100173 /*
174 * It seems that the DMC likes to transition between the DC states a lot
175 * when there are no connected displays (no active power domains) during
176 * command submission.
177 *
178 * This activity has negative impact on the performance of the chip with
179 * huge latencies observed in the interrupt handler and elsewhere.
180 *
181 * Work around it by grabbing a GT IRQ power domain whilst there is any
182 * GT activity, preventing any DC state transitions.
183 */
Chris Wilson8d761e72019-01-14 14:21:28 +0000184 i915->gt.awake = intel_display_power_get(i915, POWER_DOMAIN_GT_IRQ);
185 GEM_BUG_ON(!i915->gt.awake);
Chris Wilsone4d20062018-04-06 16:51:44 +0100186
Chris Wilsone4d20062018-04-06 16:51:44 +0100187 if (unlikely(++i915->gt.epoch == 0)) /* keep 0 as invalid */
188 i915->gt.epoch = 1;
189
190 intel_enable_gt_powersave(i915);
191 i915_update_gfx_val(i915);
192 if (INTEL_GEN(i915) >= 6)
193 gen6_rps_busy(i915);
194 i915_pmu_gt_unparked(i915);
195
196 intel_engines_unpark(i915);
197
198 i915_queue_hangcheck(i915);
199
200 queue_delayed_work(i915->wq,
201 &i915->gt.retire_work,
202 round_jiffies_up_relative(HZ));
203}
204
Eric Anholt673a3942008-07-30 12:06:12 -0700205int
Eric Anholt5a125c32008-10-22 21:40:13 -0700206i915_gem_get_aperture_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +0000207 struct drm_file *file)
Eric Anholt5a125c32008-10-22 21:40:13 -0700208{
Chris Wilson09d7e462019-01-28 10:23:53 +0000209 struct i915_ggtt *ggtt = &to_i915(dev)->ggtt;
Joonas Lahtinen72e96d62016-03-30 16:57:10 +0300210 struct drm_i915_gem_get_aperture *args = data;
Tvrtko Ursulinca1543b2015-07-01 11:51:10 +0100211 struct i915_vma *vma;
Weinan Liff8f7972017-05-31 10:35:52 +0800212 u64 pinned;
Eric Anholt5a125c32008-10-22 21:40:13 -0700213
Chris Wilson09d7e462019-01-28 10:23:53 +0000214 mutex_lock(&ggtt->vm.mutex);
215
Chris Wilson82ad6442018-06-05 16:37:58 +0100216 pinned = ggtt->vm.reserved;
Chris Wilson499197d2019-01-28 10:23:52 +0000217 list_for_each_entry(vma, &ggtt->vm.bound_list, vm_link)
Chris Wilson20dfbde2016-08-04 16:32:30 +0100218 if (i915_vma_is_pinned(vma))
Tvrtko Ursulinca1543b2015-07-01 11:51:10 +0100219 pinned += vma->node.size;
Chris Wilson09d7e462019-01-28 10:23:53 +0000220
221 mutex_unlock(&ggtt->vm.mutex);
Eric Anholt5a125c32008-10-22 21:40:13 -0700222
Chris Wilson82ad6442018-06-05 16:37:58 +0100223 args->aper_size = ggtt->vm.total;
Akshay Joshi0206e352011-08-16 15:34:10 -0400224 args->aper_available_size = args->aper_size - pinned;
Chris Wilson6299f992010-11-24 12:23:44 +0000225
Eric Anholt5a125c32008-10-22 21:40:13 -0700226 return 0;
227}
228
Matthew Auldb91b09e2017-10-06 23:18:17 +0100229static int i915_gem_object_get_pages_phys(struct drm_i915_gem_object *obj)
Chris Wilson00731152014-05-21 12:42:56 +0100230{
Al Viro93c76a32015-12-04 23:45:44 -0500231 struct address_space *mapping = obj->base.filp->f_mapping;
Chris Wilsondbb43512016-12-07 13:34:11 +0000232 drm_dma_handle_t *phys;
Chris Wilson6a2c4232014-11-04 04:51:40 -0800233 struct sg_table *st;
234 struct scatterlist *sg;
Chris Wilsondbb43512016-12-07 13:34:11 +0000235 char *vaddr;
Chris Wilson6a2c4232014-11-04 04:51:40 -0800236 int i;
Matthew Auldb91b09e2017-10-06 23:18:17 +0100237 int err;
Chris Wilson00731152014-05-21 12:42:56 +0100238
Chris Wilson6a2c4232014-11-04 04:51:40 -0800239 if (WARN_ON(i915_gem_object_needs_bit17_swizzle(obj)))
Matthew Auldb91b09e2017-10-06 23:18:17 +0100240 return -EINVAL;
Chris Wilson00731152014-05-21 12:42:56 +0100241
Chris Wilsondbb43512016-12-07 13:34:11 +0000242 /* Always aligning to the object size, allows a single allocation
243 * to handle all possible callers, and given typical object sizes,
244 * the alignment of the buddy allocation will naturally match.
245 */
246 phys = drm_pci_alloc(obj->base.dev,
Ville Syrjälä750fae22017-09-07 17:32:03 +0300247 roundup_pow_of_two(obj->base.size),
Chris Wilsondbb43512016-12-07 13:34:11 +0000248 roundup_pow_of_two(obj->base.size));
249 if (!phys)
Matthew Auldb91b09e2017-10-06 23:18:17 +0100250 return -ENOMEM;
Chris Wilsondbb43512016-12-07 13:34:11 +0000251
252 vaddr = phys->vaddr;
Chris Wilson6a2c4232014-11-04 04:51:40 -0800253 for (i = 0; i < obj->base.size / PAGE_SIZE; i++) {
254 struct page *page;
255 char *src;
256
257 page = shmem_read_mapping_page(mapping, i);
Chris Wilsondbb43512016-12-07 13:34:11 +0000258 if (IS_ERR(page)) {
Matthew Auldb91b09e2017-10-06 23:18:17 +0100259 err = PTR_ERR(page);
Chris Wilsondbb43512016-12-07 13:34:11 +0000260 goto err_phys;
261 }
Chris Wilson6a2c4232014-11-04 04:51:40 -0800262
263 src = kmap_atomic(page);
264 memcpy(vaddr, src, PAGE_SIZE);
265 drm_clflush_virt_range(vaddr, PAGE_SIZE);
266 kunmap_atomic(src);
267
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300268 put_page(page);
Chris Wilson6a2c4232014-11-04 04:51:40 -0800269 vaddr += PAGE_SIZE;
270 }
271
Chris Wilsonc0336662016-05-06 15:40:21 +0100272 i915_gem_chipset_flush(to_i915(obj->base.dev));
Chris Wilson6a2c4232014-11-04 04:51:40 -0800273
274 st = kmalloc(sizeof(*st), GFP_KERNEL);
Chris Wilsondbb43512016-12-07 13:34:11 +0000275 if (!st) {
Matthew Auldb91b09e2017-10-06 23:18:17 +0100276 err = -ENOMEM;
Chris Wilsondbb43512016-12-07 13:34:11 +0000277 goto err_phys;
278 }
Chris Wilson6a2c4232014-11-04 04:51:40 -0800279
280 if (sg_alloc_table(st, 1, GFP_KERNEL)) {
281 kfree(st);
Matthew Auldb91b09e2017-10-06 23:18:17 +0100282 err = -ENOMEM;
Chris Wilsondbb43512016-12-07 13:34:11 +0000283 goto err_phys;
Chris Wilson6a2c4232014-11-04 04:51:40 -0800284 }
285
286 sg = st->sgl;
287 sg->offset = 0;
288 sg->length = obj->base.size;
289
Chris Wilsondbb43512016-12-07 13:34:11 +0000290 sg_dma_address(sg) = phys->busaddr;
Chris Wilson6a2c4232014-11-04 04:51:40 -0800291 sg_dma_len(sg) = obj->base.size;
292
Chris Wilsondbb43512016-12-07 13:34:11 +0000293 obj->phys_handle = phys;
Matthew Auldb91b09e2017-10-06 23:18:17 +0100294
Matthew Aulda5c081662017-10-06 23:18:18 +0100295 __i915_gem_object_set_pages(obj, st, sg->length);
Matthew Auldb91b09e2017-10-06 23:18:17 +0100296
297 return 0;
Chris Wilsondbb43512016-12-07 13:34:11 +0000298
299err_phys:
300 drm_pci_free(obj->base.dev, phys);
Matthew Auldb91b09e2017-10-06 23:18:17 +0100301
302 return err;
Chris Wilson6a2c4232014-11-04 04:51:40 -0800303}
304
Chris Wilsone27ab732017-06-15 13:38:49 +0100305static void __start_cpu_write(struct drm_i915_gem_object *obj)
306{
Christian Königc0a51fd2018-02-16 13:43:38 +0100307 obj->read_domains = I915_GEM_DOMAIN_CPU;
308 obj->write_domain = I915_GEM_DOMAIN_CPU;
Chris Wilsone27ab732017-06-15 13:38:49 +0100309 if (cpu_write_needs_clflush(obj))
310 obj->cache_dirty = true;
311}
312
Chris Wilson6a2c4232014-11-04 04:51:40 -0800313static void
Chris Wilson2b3c8312016-11-11 14:58:09 +0000314__i915_gem_object_release_shmem(struct drm_i915_gem_object *obj,
Chris Wilsone5facdf2016-12-23 14:57:57 +0000315 struct sg_table *pages,
316 bool needs_clflush)
Chris Wilson6a2c4232014-11-04 04:51:40 -0800317{
Chris Wilsona4f5ea62016-10-28 13:58:35 +0100318 GEM_BUG_ON(obj->mm.madv == __I915_MADV_PURGED);
Chris Wilson6a2c4232014-11-04 04:51:40 -0800319
Chris Wilsona4f5ea62016-10-28 13:58:35 +0100320 if (obj->mm.madv == I915_MADV_DONTNEED)
321 obj->mm.dirty = false;
Chris Wilson6a2c4232014-11-04 04:51:40 -0800322
Chris Wilsone5facdf2016-12-23 14:57:57 +0000323 if (needs_clflush &&
Christian Königc0a51fd2018-02-16 13:43:38 +0100324 (obj->read_domains & I915_GEM_DOMAIN_CPU) == 0 &&
Chris Wilsonb8f55be2017-08-11 12:11:16 +0100325 !(obj->cache_coherent & I915_BO_CACHE_COHERENT_FOR_READ))
Chris Wilson2b3c8312016-11-11 14:58:09 +0000326 drm_clflush_sg(pages);
Chris Wilson03ac84f2016-10-28 13:58:36 +0100327
Chris Wilsone27ab732017-06-15 13:38:49 +0100328 __start_cpu_write(obj);
Chris Wilson03ac84f2016-10-28 13:58:36 +0100329}
330
331static void
332i915_gem_object_put_pages_phys(struct drm_i915_gem_object *obj,
333 struct sg_table *pages)
334{
Chris Wilsone5facdf2016-12-23 14:57:57 +0000335 __i915_gem_object_release_shmem(obj, pages, false);
Chris Wilson03ac84f2016-10-28 13:58:36 +0100336
Chris Wilsona4f5ea62016-10-28 13:58:35 +0100337 if (obj->mm.dirty) {
Al Viro93c76a32015-12-04 23:45:44 -0500338 struct address_space *mapping = obj->base.filp->f_mapping;
Chris Wilson6a2c4232014-11-04 04:51:40 -0800339 char *vaddr = obj->phys_handle->vaddr;
Chris Wilson00731152014-05-21 12:42:56 +0100340 int i;
341
342 for (i = 0; i < obj->base.size / PAGE_SIZE; i++) {
Chris Wilson6a2c4232014-11-04 04:51:40 -0800343 struct page *page;
344 char *dst;
Chris Wilson00731152014-05-21 12:42:56 +0100345
Chris Wilson6a2c4232014-11-04 04:51:40 -0800346 page = shmem_read_mapping_page(mapping, i);
347 if (IS_ERR(page))
348 continue;
349
350 dst = kmap_atomic(page);
351 drm_clflush_virt_range(vaddr, PAGE_SIZE);
352 memcpy(dst, vaddr, PAGE_SIZE);
353 kunmap_atomic(dst);
354
355 set_page_dirty(page);
Chris Wilsona4f5ea62016-10-28 13:58:35 +0100356 if (obj->mm.madv == I915_MADV_WILLNEED)
Chris Wilson00731152014-05-21 12:42:56 +0100357 mark_page_accessed(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300358 put_page(page);
Chris Wilson00731152014-05-21 12:42:56 +0100359 vaddr += PAGE_SIZE;
360 }
Chris Wilsona4f5ea62016-10-28 13:58:35 +0100361 obj->mm.dirty = false;
Chris Wilson00731152014-05-21 12:42:56 +0100362 }
363
Chris Wilson03ac84f2016-10-28 13:58:36 +0100364 sg_free_table(pages);
365 kfree(pages);
Chris Wilsondbb43512016-12-07 13:34:11 +0000366
367 drm_pci_free(obj->base.dev, obj->phys_handle);
Chris Wilson6a2c4232014-11-04 04:51:40 -0800368}
369
370static void
371i915_gem_object_release_phys(struct drm_i915_gem_object *obj)
372{
Chris Wilsona4f5ea62016-10-28 13:58:35 +0100373 i915_gem_object_unpin_pages(obj);
Chris Wilson6a2c4232014-11-04 04:51:40 -0800374}
375
376static const struct drm_i915_gem_object_ops i915_gem_phys_ops = {
377 .get_pages = i915_gem_object_get_pages_phys,
378 .put_pages = i915_gem_object_put_pages_phys,
379 .release = i915_gem_object_release_phys,
380};
381
Chris Wilson581ab1f2017-02-15 16:39:00 +0000382static const struct drm_i915_gem_object_ops i915_gem_object_ops;
383
Chris Wilson35a96112016-08-14 18:44:40 +0100384int i915_gem_object_unbind(struct drm_i915_gem_object *obj)
Chris Wilsonaa653a62016-08-04 07:52:27 +0100385{
386 struct i915_vma *vma;
387 LIST_HEAD(still_in_list);
Chris Wilson02bef8f2016-08-14 18:44:41 +0100388 int ret;
Chris Wilsonaa653a62016-08-04 07:52:27 +0100389
Chris Wilson02bef8f2016-08-14 18:44:41 +0100390 lockdep_assert_held(&obj->base.dev->struct_mutex);
391
392 /* Closed vma are removed from the obj->vma_list - but they may
393 * still have an active binding on the object. To remove those we
394 * must wait for all rendering to complete to the object (as unbinding
395 * must anyway), and retire the requests.
Chris Wilsonaa653a62016-08-04 07:52:27 +0100396 */
Chris Wilson5888fc92017-12-04 13:25:13 +0000397 ret = i915_gem_object_set_to_cpu_domain(obj, false);
Chris Wilson02bef8f2016-08-14 18:44:41 +0100398 if (ret)
399 return ret;
400
Chris Wilson528cbd12019-01-28 10:23:54 +0000401 spin_lock(&obj->vma.lock);
402 while (!ret && (vma = list_first_entry_or_null(&obj->vma.list,
403 struct i915_vma,
404 obj_link))) {
Chris Wilsonaa653a62016-08-04 07:52:27 +0100405 list_move_tail(&vma->obj_link, &still_in_list);
Chris Wilson528cbd12019-01-28 10:23:54 +0000406 spin_unlock(&obj->vma.lock);
407
Chris Wilsonaa653a62016-08-04 07:52:27 +0100408 ret = i915_vma_unbind(vma);
Chris Wilson528cbd12019-01-28 10:23:54 +0000409
410 spin_lock(&obj->vma.lock);
Chris Wilsonaa653a62016-08-04 07:52:27 +0100411 }
Chris Wilson528cbd12019-01-28 10:23:54 +0000412 list_splice(&still_in_list, &obj->vma.list);
413 spin_unlock(&obj->vma.lock);
Chris Wilsonaa653a62016-08-04 07:52:27 +0100414
415 return ret;
416}
417
Chris Wilsone95433c2016-10-28 13:58:27 +0100418static long
419i915_gem_object_wait_fence(struct dma_fence *fence,
420 unsigned int flags,
Chris Wilson62eb3c22019-02-13 09:25:04 +0000421 long timeout)
Chris Wilsone95433c2016-10-28 13:58:27 +0100422{
Chris Wilsone61e0f52018-02-21 09:56:36 +0000423 struct i915_request *rq;
Chris Wilsone95433c2016-10-28 13:58:27 +0100424
425 BUILD_BUG_ON(I915_WAIT_INTERRUPTIBLE != 0x1);
426
427 if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags))
428 return timeout;
429
430 if (!dma_fence_is_i915(fence))
431 return dma_fence_wait_timeout(fence,
432 flags & I915_WAIT_INTERRUPTIBLE,
433 timeout);
434
435 rq = to_request(fence);
Chris Wilsone61e0f52018-02-21 09:56:36 +0000436 if (i915_request_completed(rq))
Chris Wilsone95433c2016-10-28 13:58:27 +0100437 goto out;
438
Chris Wilsone61e0f52018-02-21 09:56:36 +0000439 timeout = i915_request_wait(rq, flags, timeout);
Chris Wilsone95433c2016-10-28 13:58:27 +0100440
441out:
Chris Wilsone61e0f52018-02-21 09:56:36 +0000442 if (flags & I915_WAIT_LOCKED && i915_request_completed(rq))
443 i915_request_retire_upto(rq);
Chris Wilsone95433c2016-10-28 13:58:27 +0100444
Chris Wilsone95433c2016-10-28 13:58:27 +0100445 return timeout;
446}
447
448static long
449i915_gem_object_wait_reservation(struct reservation_object *resv,
450 unsigned int flags,
Chris Wilson62eb3c22019-02-13 09:25:04 +0000451 long timeout)
Chris Wilsone95433c2016-10-28 13:58:27 +0100452{
Chris Wilsone54ca972017-02-17 15:13:04 +0000453 unsigned int seq = __read_seqcount_begin(&resv->seq);
Chris Wilsone95433c2016-10-28 13:58:27 +0100454 struct dma_fence *excl;
Chris Wilsone54ca972017-02-17 15:13:04 +0000455 bool prune_fences = false;
Chris Wilsone95433c2016-10-28 13:58:27 +0100456
457 if (flags & I915_WAIT_ALL) {
458 struct dma_fence **shared;
459 unsigned int count, i;
460 int ret;
461
462 ret = reservation_object_get_fences_rcu(resv,
463 &excl, &count, &shared);
464 if (ret)
465 return ret;
466
467 for (i = 0; i < count; i++) {
468 timeout = i915_gem_object_wait_fence(shared[i],
Chris Wilson62eb3c22019-02-13 09:25:04 +0000469 flags, timeout);
Chris Wilsond892e932017-02-12 21:53:43 +0000470 if (timeout < 0)
Chris Wilsone95433c2016-10-28 13:58:27 +0100471 break;
472
473 dma_fence_put(shared[i]);
474 }
475
476 for (; i < count; i++)
477 dma_fence_put(shared[i]);
478 kfree(shared);
Chris Wilsone54ca972017-02-17 15:13:04 +0000479
Chris Wilsonfa730552018-03-07 17:13:03 +0000480 /*
481 * If both shared fences and an exclusive fence exist,
482 * then by construction the shared fences must be later
483 * than the exclusive fence. If we successfully wait for
484 * all the shared fences, we know that the exclusive fence
485 * must all be signaled. If all the shared fences are
486 * signaled, we can prune the array and recover the
487 * floating references on the fences/requests.
488 */
Chris Wilsone54ca972017-02-17 15:13:04 +0000489 prune_fences = count && timeout >= 0;
Chris Wilsone95433c2016-10-28 13:58:27 +0100490 } else {
491 excl = reservation_object_get_excl_rcu(resv);
492 }
493
Chris Wilsonfa730552018-03-07 17:13:03 +0000494 if (excl && timeout >= 0)
Chris Wilson62eb3c22019-02-13 09:25:04 +0000495 timeout = i915_gem_object_wait_fence(excl, flags, timeout);
Chris Wilsone95433c2016-10-28 13:58:27 +0100496
497 dma_fence_put(excl);
498
Chris Wilsonfa730552018-03-07 17:13:03 +0000499 /*
500 * Opportunistically prune the fences iff we know they have *all* been
Chris Wilson03d1cac2017-03-08 13:26:28 +0000501 * signaled and that the reservation object has not been changed (i.e.
502 * no new fences have been added).
503 */
Chris Wilsone54ca972017-02-17 15:13:04 +0000504 if (prune_fences && !__read_seqcount_retry(&resv->seq, seq)) {
Chris Wilson03d1cac2017-03-08 13:26:28 +0000505 if (reservation_object_trylock(resv)) {
506 if (!__read_seqcount_retry(&resv->seq, seq))
507 reservation_object_add_excl_fence(resv, NULL);
508 reservation_object_unlock(resv);
509 }
Chris Wilsone54ca972017-02-17 15:13:04 +0000510 }
511
Chris Wilsone95433c2016-10-28 13:58:27 +0100512 return timeout;
513}
514
Chris Wilsonb7268c52018-04-18 19:40:52 +0100515static void __fence_set_priority(struct dma_fence *fence,
516 const struct i915_sched_attr *attr)
Chris Wilson6b5e90f2016-11-14 20:41:05 +0000517{
Chris Wilsone61e0f52018-02-21 09:56:36 +0000518 struct i915_request *rq;
Chris Wilson6b5e90f2016-11-14 20:41:05 +0000519 struct intel_engine_cs *engine;
520
Chris Wilsonc218ee02018-01-06 10:56:18 +0000521 if (dma_fence_is_signaled(fence) || !dma_fence_is_i915(fence))
Chris Wilson6b5e90f2016-11-14 20:41:05 +0000522 return;
523
524 rq = to_request(fence);
525 engine = rq->engine;
Chris Wilson6b5e90f2016-11-14 20:41:05 +0000526
Chris Wilson4f6d8fc2018-05-07 14:57:25 +0100527 local_bh_disable();
528 rcu_read_lock(); /* RCU serialisation for set-wedged protection */
Chris Wilson47650db2018-03-07 13:42:25 +0000529 if (engine->schedule)
Chris Wilsonb7268c52018-04-18 19:40:52 +0100530 engine->schedule(rq, attr);
Chris Wilson47650db2018-03-07 13:42:25 +0000531 rcu_read_unlock();
Chris Wilson4f6d8fc2018-05-07 14:57:25 +0100532 local_bh_enable(); /* kick the tasklets if queues were reprioritised */
Chris Wilson6b5e90f2016-11-14 20:41:05 +0000533}
534
Chris Wilsonb7268c52018-04-18 19:40:52 +0100535static void fence_set_priority(struct dma_fence *fence,
536 const struct i915_sched_attr *attr)
Chris Wilson6b5e90f2016-11-14 20:41:05 +0000537{
538 /* Recurse once into a fence-array */
539 if (dma_fence_is_array(fence)) {
540 struct dma_fence_array *array = to_dma_fence_array(fence);
541 int i;
542
543 for (i = 0; i < array->num_fences; i++)
Chris Wilsonb7268c52018-04-18 19:40:52 +0100544 __fence_set_priority(array->fences[i], attr);
Chris Wilson6b5e90f2016-11-14 20:41:05 +0000545 } else {
Chris Wilsonb7268c52018-04-18 19:40:52 +0100546 __fence_set_priority(fence, attr);
Chris Wilson6b5e90f2016-11-14 20:41:05 +0000547 }
548}
549
550int
551i915_gem_object_wait_priority(struct drm_i915_gem_object *obj,
552 unsigned int flags,
Chris Wilsonb7268c52018-04-18 19:40:52 +0100553 const struct i915_sched_attr *attr)
Chris Wilson6b5e90f2016-11-14 20:41:05 +0000554{
555 struct dma_fence *excl;
556
557 if (flags & I915_WAIT_ALL) {
558 struct dma_fence **shared;
559 unsigned int count, i;
560 int ret;
561
562 ret = reservation_object_get_fences_rcu(obj->resv,
563 &excl, &count, &shared);
564 if (ret)
565 return ret;
566
567 for (i = 0; i < count; i++) {
Chris Wilsonb7268c52018-04-18 19:40:52 +0100568 fence_set_priority(shared[i], attr);
Chris Wilson6b5e90f2016-11-14 20:41:05 +0000569 dma_fence_put(shared[i]);
570 }
571
572 kfree(shared);
573 } else {
574 excl = reservation_object_get_excl_rcu(obj->resv);
575 }
576
577 if (excl) {
Chris Wilsonb7268c52018-04-18 19:40:52 +0100578 fence_set_priority(excl, attr);
Chris Wilson6b5e90f2016-11-14 20:41:05 +0000579 dma_fence_put(excl);
580 }
581 return 0;
582}
583
Chris Wilson00e60f22016-08-04 16:32:40 +0100584/**
Chris Wilsone95433c2016-10-28 13:58:27 +0100585 * Waits for rendering to the object to be completed
Chris Wilson00e60f22016-08-04 16:32:40 +0100586 * @obj: i915 gem object
Chris Wilsone95433c2016-10-28 13:58:27 +0100587 * @flags: how to wait (under a lock, for all rendering or just for writes etc)
588 * @timeout: how long to wait
Chris Wilson00e60f22016-08-04 16:32:40 +0100589 */
590int
Chris Wilsone95433c2016-10-28 13:58:27 +0100591i915_gem_object_wait(struct drm_i915_gem_object *obj,
592 unsigned int flags,
Chris Wilson62eb3c22019-02-13 09:25:04 +0000593 long timeout)
Chris Wilson00e60f22016-08-04 16:32:40 +0100594{
Chris Wilsone95433c2016-10-28 13:58:27 +0100595 might_sleep();
Chris Wilsone95433c2016-10-28 13:58:27 +0100596 GEM_BUG_ON(timeout < 0);
Chris Wilson00e60f22016-08-04 16:32:40 +0100597
Chris Wilson62eb3c22019-02-13 09:25:04 +0000598 timeout = i915_gem_object_wait_reservation(obj->resv, flags, timeout);
Chris Wilsone95433c2016-10-28 13:58:27 +0100599 return timeout < 0 ? timeout : 0;
Chris Wilson00e60f22016-08-04 16:32:40 +0100600}
601
Chris Wilson00731152014-05-21 12:42:56 +0100602static int
603i915_gem_phys_pwrite(struct drm_i915_gem_object *obj,
604 struct drm_i915_gem_pwrite *args,
Chris Wilson03ac84f2016-10-28 13:58:36 +0100605 struct drm_file *file)
Chris Wilson00731152014-05-21 12:42:56 +0100606{
Chris Wilson00731152014-05-21 12:42:56 +0100607 void *vaddr = obj->phys_handle->vaddr + args->offset;
Gustavo Padovan3ed605b2016-04-26 12:32:27 -0300608 char __user *user_data = u64_to_user_ptr(args->data_ptr);
Chris Wilson6a2c4232014-11-04 04:51:40 -0800609
610 /* We manually control the domain here and pretend that it
611 * remains coherent i.e. in the GTT domain, like shmem_pwrite.
612 */
Rodrigo Vivi77a0d1c2015-06-18 11:43:24 -0700613 intel_fb_obj_invalidate(obj, ORIGIN_CPU);
Chris Wilson10466d22017-01-06 15:22:38 +0000614 if (copy_from_user(vaddr, user_data, args->size))
615 return -EFAULT;
Chris Wilson00731152014-05-21 12:42:56 +0100616
Chris Wilson6a2c4232014-11-04 04:51:40 -0800617 drm_clflush_virt_range(vaddr, args->size);
Chris Wilson10466d22017-01-06 15:22:38 +0000618 i915_gem_chipset_flush(to_i915(obj->base.dev));
Paulo Zanoni063e4e62015-02-13 17:23:45 -0200619
Chris Wilsond59b21e2017-02-22 11:40:49 +0000620 intel_fb_obj_flush(obj, ORIGIN_CPU);
Chris Wilson10466d22017-01-06 15:22:38 +0000621 return 0;
Chris Wilson00731152014-05-21 12:42:56 +0100622}
623
Tvrtko Ursulin187685c2016-12-01 14:16:36 +0000624void *i915_gem_object_alloc(struct drm_i915_private *dev_priv)
Chris Wilson42dcedd2012-11-15 11:32:30 +0000625{
Chris Wilsonefab6d82015-04-07 16:20:57 +0100626 return kmem_cache_zalloc(dev_priv->objects, GFP_KERNEL);
Chris Wilson42dcedd2012-11-15 11:32:30 +0000627}
628
629void i915_gem_object_free(struct drm_i915_gem_object *obj)
630{
Chris Wilsonfac5e232016-07-04 11:34:36 +0100631 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
Chris Wilsonefab6d82015-04-07 16:20:57 +0100632 kmem_cache_free(dev_priv->objects, obj);
Chris Wilson42dcedd2012-11-15 11:32:30 +0000633}
634
Dave Airlieff72145b2011-02-07 12:16:14 +1000635static int
636i915_gem_create(struct drm_file *file,
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +0000637 struct drm_i915_private *dev_priv,
Jani Nikula739f3ab2019-01-16 11:15:19 +0200638 u64 size,
639 u32 *handle_p)
Eric Anholt673a3942008-07-30 12:06:12 -0700640{
Chris Wilson05394f32010-11-08 19:18:58 +0000641 struct drm_i915_gem_object *obj;
Pekka Paalanena1a2d1d2009-08-23 12:40:55 +0300642 int ret;
643 u32 handle;
Eric Anholt673a3942008-07-30 12:06:12 -0700644
Dave Airlieff72145b2011-02-07 12:16:14 +1000645 size = roundup(size, PAGE_SIZE);
Chris Wilson8ffc0242011-09-14 14:14:28 +0200646 if (size == 0)
647 return -EINVAL;
Eric Anholt673a3942008-07-30 12:06:12 -0700648
649 /* Allocate the new object */
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +0000650 obj = i915_gem_object_create(dev_priv, size);
Chris Wilsonfe3db792016-04-25 13:32:13 +0100651 if (IS_ERR(obj))
652 return PTR_ERR(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700653
Chris Wilson05394f32010-11-08 19:18:58 +0000654 ret = drm_gem_handle_create(file, &obj->base, &handle);
Chris Wilson202f2fe2010-10-14 13:20:40 +0100655 /* drop reference from allocate - handle holds it now */
Chris Wilsonf0cd5182016-10-28 13:58:43 +0100656 i915_gem_object_put(obj);
Daniel Vetterd861e332013-07-24 23:25:03 +0200657 if (ret)
658 return ret;
Chris Wilson202f2fe2010-10-14 13:20:40 +0100659
Dave Airlieff72145b2011-02-07 12:16:14 +1000660 *handle_p = handle;
Eric Anholt673a3942008-07-30 12:06:12 -0700661 return 0;
662}
663
Dave Airlieff72145b2011-02-07 12:16:14 +1000664int
665i915_gem_dumb_create(struct drm_file *file,
666 struct drm_device *dev,
667 struct drm_mode_create_dumb *args)
668{
669 /* have to work out size/pitch and return them */
Paulo Zanonide45eaf2013-10-18 18:48:24 -0300670 args->pitch = ALIGN(args->width * DIV_ROUND_UP(args->bpp, 8), 64);
Dave Airlieff72145b2011-02-07 12:16:14 +1000671 args->size = args->pitch * args->height;
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +0000672 return i915_gem_create(file, to_i915(dev),
Dave Airlieda6b51d2014-12-24 13:11:17 +1000673 args->size, &args->handle);
Dave Airlieff72145b2011-02-07 12:16:14 +1000674}
675
Chris Wilsone27ab732017-06-15 13:38:49 +0100676static bool gpu_write_needs_clflush(struct drm_i915_gem_object *obj)
677{
678 return !(obj->cache_level == I915_CACHE_NONE ||
679 obj->cache_level == I915_CACHE_WT);
680}
681
Dave Airlieff72145b2011-02-07 12:16:14 +1000682/**
683 * Creates a new mm object and returns a handle to it.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +0100684 * @dev: drm device pointer
685 * @data: ioctl data blob
686 * @file: drm file pointer
Dave Airlieff72145b2011-02-07 12:16:14 +1000687 */
688int
689i915_gem_create_ioctl(struct drm_device *dev, void *data,
690 struct drm_file *file)
691{
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +0000692 struct drm_i915_private *dev_priv = to_i915(dev);
Dave Airlieff72145b2011-02-07 12:16:14 +1000693 struct drm_i915_gem_create *args = data;
Daniel Vetter63ed2cb2012-04-23 16:50:50 +0200694
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +0000695 i915_gem_flush_free_objects(dev_priv);
Chris Wilsonfbbd37b2016-10-28 13:58:42 +0100696
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +0000697 return i915_gem_create(file, dev_priv,
Dave Airlieda6b51d2014-12-24 13:11:17 +1000698 args->size, &args->handle);
Dave Airlieff72145b2011-02-07 12:16:14 +1000699}
700
Chris Wilsonef749212017-04-12 12:01:10 +0100701static inline enum fb_op_origin
702fb_write_origin(struct drm_i915_gem_object *obj, unsigned int domain)
703{
704 return (domain == I915_GEM_DOMAIN_GTT ?
705 obj->frontbuffer_ggtt_origin : ORIGIN_CPU);
706}
707
Chris Wilson7125397b2017-12-06 12:49:14 +0000708void i915_gem_flush_ggtt_writes(struct drm_i915_private *dev_priv)
Chris Wilsonef749212017-04-12 12:01:10 +0100709{
Chris Wilson538ef962019-01-14 14:21:18 +0000710 intel_wakeref_t wakeref;
711
Chris Wilson7125397b2017-12-06 12:49:14 +0000712 /*
713 * No actual flushing is required for the GTT write domain for reads
714 * from the GTT domain. Writes to it "immediately" go to main memory
715 * as far as we know, so there's no chipset flush. It also doesn't
716 * land in the GPU render cache.
Chris Wilsonef749212017-04-12 12:01:10 +0100717 *
718 * However, we do have to enforce the order so that all writes through
719 * the GTT land before any writes to the device, such as updates to
720 * the GATT itself.
721 *
722 * We also have to wait a bit for the writes to land from the GTT.
723 * An uncached read (i.e. mmio) seems to be ideal for the round-trip
724 * timing. This issue has only been observed when switching quickly
725 * between GTT writes and CPU reads from inside the kernel on recent hw,
726 * and it appears to only affect discrete GTT blocks (i.e. on LLC
Chris Wilson7125397b2017-12-06 12:49:14 +0000727 * system agents we cannot reproduce this behaviour, until Cannonlake
728 * that was!).
Chris Wilsonef749212017-04-12 12:01:10 +0100729 */
Chris Wilson7125397b2017-12-06 12:49:14 +0000730
Chris Wilson900ccf32018-07-20 11:19:10 +0100731 wmb();
732
733 if (INTEL_INFO(dev_priv)->has_coherent_ggtt)
734 return;
735
Chris Wilsona8bd3b82018-07-17 10:26:55 +0100736 i915_gem_chipset_flush(dev_priv);
Chris Wilsonef749212017-04-12 12:01:10 +0100737
Chris Wilsond4225a52019-01-14 14:21:23 +0000738 with_intel_runtime_pm(dev_priv, wakeref) {
739 spin_lock_irq(&dev_priv->uncore.lock);
Chris Wilson7125397b2017-12-06 12:49:14 +0000740
Chris Wilsond4225a52019-01-14 14:21:23 +0000741 POSTING_READ_FW(RING_HEAD(RENDER_RING_BASE));
Chris Wilson7125397b2017-12-06 12:49:14 +0000742
Chris Wilsond4225a52019-01-14 14:21:23 +0000743 spin_unlock_irq(&dev_priv->uncore.lock);
744 }
Chris Wilson7125397b2017-12-06 12:49:14 +0000745}
746
747static void
748flush_write_domain(struct drm_i915_gem_object *obj, unsigned int flush_domains)
749{
750 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
751 struct i915_vma *vma;
752
Christian Königc0a51fd2018-02-16 13:43:38 +0100753 if (!(obj->write_domain & flush_domains))
Chris Wilson7125397b2017-12-06 12:49:14 +0000754 return;
755
Christian Königc0a51fd2018-02-16 13:43:38 +0100756 switch (obj->write_domain) {
Chris Wilsonef749212017-04-12 12:01:10 +0100757 case I915_GEM_DOMAIN_GTT:
Chris Wilson7125397b2017-12-06 12:49:14 +0000758 i915_gem_flush_ggtt_writes(dev_priv);
Chris Wilsonef749212017-04-12 12:01:10 +0100759
760 intel_fb_obj_flush(obj,
761 fb_write_origin(obj, I915_GEM_DOMAIN_GTT));
Chris Wilson7125397b2017-12-06 12:49:14 +0000762
Chris Wilsone2189dd2017-12-07 21:14:07 +0000763 for_each_ggtt_vma(vma, obj) {
Chris Wilson7125397b2017-12-06 12:49:14 +0000764 if (vma->iomap)
765 continue;
766
767 i915_vma_unset_ggtt_write(vma);
768 }
Chris Wilsonef749212017-04-12 12:01:10 +0100769 break;
770
Chris Wilsonadd00e62018-07-06 12:54:02 +0100771 case I915_GEM_DOMAIN_WC:
772 wmb();
773 break;
774
Chris Wilsonef749212017-04-12 12:01:10 +0100775 case I915_GEM_DOMAIN_CPU:
776 i915_gem_clflush_object(obj, I915_CLFLUSH_SYNC);
777 break;
Chris Wilsone27ab732017-06-15 13:38:49 +0100778
779 case I915_GEM_DOMAIN_RENDER:
780 if (gpu_write_needs_clflush(obj))
781 obj->cache_dirty = true;
782 break;
Chris Wilsonef749212017-04-12 12:01:10 +0100783 }
784
Christian Königc0a51fd2018-02-16 13:43:38 +0100785 obj->write_domain = 0;
Chris Wilsonef749212017-04-12 12:01:10 +0100786}
787
Brad Volkin4c914c02014-02-18 10:15:45 -0800788/*
789 * Pins the specified object's pages and synchronizes the object with
790 * GPU accesses. Sets needs_clflush to non-zero if the caller should
791 * flush the object from the CPU cache.
792 */
793int i915_gem_obj_prepare_shmem_read(struct drm_i915_gem_object *obj,
Chris Wilson43394c72016-08-18 17:16:47 +0100794 unsigned int *needs_clflush)
Brad Volkin4c914c02014-02-18 10:15:45 -0800795{
796 int ret;
797
Chris Wilsone95433c2016-10-28 13:58:27 +0100798 lockdep_assert_held(&obj->base.dev->struct_mutex);
Brad Volkin4c914c02014-02-18 10:15:45 -0800799
Chris Wilsone95433c2016-10-28 13:58:27 +0100800 *needs_clflush = 0;
Chris Wilson43394c72016-08-18 17:16:47 +0100801 if (!i915_gem_object_has_struct_page(obj))
802 return -ENODEV;
Brad Volkin4c914c02014-02-18 10:15:45 -0800803
Chris Wilsone95433c2016-10-28 13:58:27 +0100804 ret = i915_gem_object_wait(obj,
805 I915_WAIT_INTERRUPTIBLE |
806 I915_WAIT_LOCKED,
Chris Wilson62eb3c22019-02-13 09:25:04 +0000807 MAX_SCHEDULE_TIMEOUT);
Chris Wilsonc13d87e2016-07-20 09:21:15 +0100808 if (ret)
809 return ret;
810
Chris Wilsona4f5ea62016-10-28 13:58:35 +0100811 ret = i915_gem_object_pin_pages(obj);
Chris Wilson97649512016-08-18 17:16:50 +0100812 if (ret)
813 return ret;
814
Chris Wilsonb8f55be2017-08-11 12:11:16 +0100815 if (obj->cache_coherent & I915_BO_CACHE_COHERENT_FOR_READ ||
816 !static_cpu_has(X86_FEATURE_CLFLUSH)) {
Chris Wilson7f5f95d2017-03-10 00:09:42 +0000817 ret = i915_gem_object_set_to_cpu_domain(obj, false);
818 if (ret)
819 goto err_unpin;
820 else
821 goto out;
822 }
823
Chris Wilsonef749212017-04-12 12:01:10 +0100824 flush_write_domain(obj, ~I915_GEM_DOMAIN_CPU);
Chris Wilsona314d5c2016-08-18 17:16:48 +0100825
Chris Wilson43394c72016-08-18 17:16:47 +0100826 /* If we're not in the cpu read domain, set ourself into the gtt
827 * read domain and manually flush cachelines (if required). This
828 * optimizes for the case when the gpu will dirty the data
829 * anyway again before the next pread happens.
830 */
Chris Wilsone27ab732017-06-15 13:38:49 +0100831 if (!obj->cache_dirty &&
Christian Königc0a51fd2018-02-16 13:43:38 +0100832 !(obj->read_domains & I915_GEM_DOMAIN_CPU))
Chris Wilson7f5f95d2017-03-10 00:09:42 +0000833 *needs_clflush = CLFLUSH_BEFORE;
Brad Volkin4c914c02014-02-18 10:15:45 -0800834
Chris Wilson7f5f95d2017-03-10 00:09:42 +0000835out:
Chris Wilson97649512016-08-18 17:16:50 +0100836 /* return with the pages pinned */
Chris Wilson43394c72016-08-18 17:16:47 +0100837 return 0;
Chris Wilson97649512016-08-18 17:16:50 +0100838
839err_unpin:
840 i915_gem_object_unpin_pages(obj);
841 return ret;
Chris Wilson43394c72016-08-18 17:16:47 +0100842}
843
844int i915_gem_obj_prepare_shmem_write(struct drm_i915_gem_object *obj,
845 unsigned int *needs_clflush)
846{
847 int ret;
848
Chris Wilsone95433c2016-10-28 13:58:27 +0100849 lockdep_assert_held(&obj->base.dev->struct_mutex);
850
Chris Wilson43394c72016-08-18 17:16:47 +0100851 *needs_clflush = 0;
852 if (!i915_gem_object_has_struct_page(obj))
853 return -ENODEV;
854
Chris Wilsone95433c2016-10-28 13:58:27 +0100855 ret = i915_gem_object_wait(obj,
856 I915_WAIT_INTERRUPTIBLE |
857 I915_WAIT_LOCKED |
858 I915_WAIT_ALL,
Chris Wilson62eb3c22019-02-13 09:25:04 +0000859 MAX_SCHEDULE_TIMEOUT);
Chris Wilson43394c72016-08-18 17:16:47 +0100860 if (ret)
861 return ret;
862
Chris Wilsona4f5ea62016-10-28 13:58:35 +0100863 ret = i915_gem_object_pin_pages(obj);
Chris Wilson97649512016-08-18 17:16:50 +0100864 if (ret)
865 return ret;
866
Chris Wilsonb8f55be2017-08-11 12:11:16 +0100867 if (obj->cache_coherent & I915_BO_CACHE_COHERENT_FOR_WRITE ||
868 !static_cpu_has(X86_FEATURE_CLFLUSH)) {
Chris Wilson7f5f95d2017-03-10 00:09:42 +0000869 ret = i915_gem_object_set_to_cpu_domain(obj, true);
870 if (ret)
871 goto err_unpin;
872 else
873 goto out;
874 }
875
Chris Wilsonef749212017-04-12 12:01:10 +0100876 flush_write_domain(obj, ~I915_GEM_DOMAIN_CPU);
Chris Wilsona314d5c2016-08-18 17:16:48 +0100877
Chris Wilson43394c72016-08-18 17:16:47 +0100878 /* If we're not in the cpu write domain, set ourself into the
879 * gtt write domain and manually flush cachelines (as required).
880 * This optimizes for the case when the gpu will use the data
881 * right away and we therefore have to clflush anyway.
882 */
Chris Wilsone27ab732017-06-15 13:38:49 +0100883 if (!obj->cache_dirty) {
Chris Wilson7f5f95d2017-03-10 00:09:42 +0000884 *needs_clflush |= CLFLUSH_AFTER;
Chris Wilson43394c72016-08-18 17:16:47 +0100885
Chris Wilsone27ab732017-06-15 13:38:49 +0100886 /*
887 * Same trick applies to invalidate partially written
888 * cachelines read before writing.
889 */
Christian Königc0a51fd2018-02-16 13:43:38 +0100890 if (!(obj->read_domains & I915_GEM_DOMAIN_CPU))
Chris Wilsone27ab732017-06-15 13:38:49 +0100891 *needs_clflush |= CLFLUSH_BEFORE;
892 }
Chris Wilson43394c72016-08-18 17:16:47 +0100893
Chris Wilson7f5f95d2017-03-10 00:09:42 +0000894out:
Chris Wilson43394c72016-08-18 17:16:47 +0100895 intel_fb_obj_invalidate(obj, ORIGIN_CPU);
Chris Wilsona4f5ea62016-10-28 13:58:35 +0100896 obj->mm.dirty = true;
Chris Wilson97649512016-08-18 17:16:50 +0100897 /* return with the pages pinned */
Chris Wilson43394c72016-08-18 17:16:47 +0100898 return 0;
Chris Wilson97649512016-08-18 17:16:50 +0100899
900err_unpin:
901 i915_gem_object_unpin_pages(obj);
902 return ret;
Brad Volkin4c914c02014-02-18 10:15:45 -0800903}
904
Daniel Vetterd174bd62012-03-25 19:47:40 +0200905static int
Chris Wilsonb9d126e2019-01-05 12:07:58 +0000906shmem_pread(struct page *page, int offset, int len, char __user *user_data,
907 bool needs_clflush)
Daniel Vetterd174bd62012-03-25 19:47:40 +0200908{
909 char *vaddr;
910 int ret;
911
912 vaddr = kmap(page);
Daniel Vetterd174bd62012-03-25 19:47:40 +0200913
Chris Wilsonb9d126e2019-01-05 12:07:58 +0000914 if (needs_clflush)
915 drm_clflush_virt_range(vaddr + offset, len);
916
917 ret = __copy_to_user(user_data, vaddr + offset, len);
918
Daniel Vetterd174bd62012-03-25 19:47:40 +0200919 kunmap(page);
920
Chris Wilsonb9d126e2019-01-05 12:07:58 +0000921 return ret ? -EFAULT : 0;
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100922}
923
924static int
925i915_gem_shmem_pread(struct drm_i915_gem_object *obj,
926 struct drm_i915_gem_pread *args)
927{
928 char __user *user_data;
929 u64 remain;
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100930 unsigned int needs_clflush;
931 unsigned int idx, offset;
932 int ret;
933
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100934 ret = mutex_lock_interruptible(&obj->base.dev->struct_mutex);
935 if (ret)
936 return ret;
937
938 ret = i915_gem_obj_prepare_shmem_read(obj, &needs_clflush);
939 mutex_unlock(&obj->base.dev->struct_mutex);
940 if (ret)
941 return ret;
942
943 remain = args->size;
944 user_data = u64_to_user_ptr(args->data_ptr);
945 offset = offset_in_page(args->offset);
946 for (idx = args->offset >> PAGE_SHIFT; remain; idx++) {
947 struct page *page = i915_gem_object_get_page(obj, idx);
Chris Wilsona5e856a52018-10-12 15:02:28 +0100948 unsigned int length = min_t(u64, remain, PAGE_SIZE - offset);
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100949
950 ret = shmem_pread(page, offset, length, user_data,
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100951 needs_clflush);
952 if (ret)
953 break;
954
955 remain -= length;
956 user_data += length;
957 offset = 0;
958 }
959
960 i915_gem_obj_finish_shmem_access(obj);
961 return ret;
962}
963
964static inline bool
965gtt_user_read(struct io_mapping *mapping,
966 loff_t base, int offset,
967 char __user *user_data, int length)
968{
Ville Syrjäläafe722b2017-09-01 20:12:52 +0300969 void __iomem *vaddr;
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100970 unsigned long unwritten;
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +0530971
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +0530972 /* We can use the cpu mem copy function because this is X86. */
Ville Syrjäläafe722b2017-09-01 20:12:52 +0300973 vaddr = io_mapping_map_atomic_wc(mapping, base);
974 unwritten = __copy_to_user_inatomic(user_data,
975 (void __force *)vaddr + offset,
976 length);
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100977 io_mapping_unmap_atomic(vaddr);
978 if (unwritten) {
Ville Syrjäläafe722b2017-09-01 20:12:52 +0300979 vaddr = io_mapping_map_wc(mapping, base, PAGE_SIZE);
980 unwritten = copy_to_user(user_data,
981 (void __force *)vaddr + offset,
982 length);
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100983 io_mapping_unmap(vaddr);
984 }
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +0530985 return unwritten;
986}
987
988static int
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100989i915_gem_gtt_pread(struct drm_i915_gem_object *obj,
990 const struct drm_i915_gem_pread *args)
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +0530991{
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100992 struct drm_i915_private *i915 = to_i915(obj->base.dev);
993 struct i915_ggtt *ggtt = &i915->ggtt;
Chris Wilson538ef962019-01-14 14:21:18 +0000994 intel_wakeref_t wakeref;
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +0530995 struct drm_mm_node node;
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100996 struct i915_vma *vma;
997 void __user *user_data;
998 u64 remain, offset;
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +0530999 int ret;
1000
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001001 ret = mutex_lock_interruptible(&i915->drm.struct_mutex);
1002 if (ret)
1003 return ret;
1004
Chris Wilson538ef962019-01-14 14:21:18 +00001005 wakeref = intel_runtime_pm_get(i915);
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001006 vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0,
Chris Wilsona3259ca2017-10-09 09:44:00 +01001007 PIN_MAPPABLE |
1008 PIN_NONFAULT |
1009 PIN_NONBLOCK);
Chris Wilson18034582016-08-18 17:16:45 +01001010 if (!IS_ERR(vma)) {
1011 node.start = i915_ggtt_offset(vma);
1012 node.allocated = false;
Chris Wilson49ef5292016-08-18 17:17:00 +01001013 ret = i915_vma_put_fence(vma);
Chris Wilson18034582016-08-18 17:16:45 +01001014 if (ret) {
1015 i915_vma_unpin(vma);
1016 vma = ERR_PTR(ret);
1017 }
1018 }
Chris Wilson058d88c2016-08-15 10:49:06 +01001019 if (IS_ERR(vma)) {
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001020 ret = insert_mappable_node(ggtt, &node, PAGE_SIZE);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301021 if (ret)
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001022 goto out_unlock;
1023 GEM_BUG_ON(!node.allocated);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301024 }
1025
1026 ret = i915_gem_object_set_to_gtt_domain(obj, false);
1027 if (ret)
1028 goto out_unpin;
1029
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001030 mutex_unlock(&i915->drm.struct_mutex);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301031
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001032 user_data = u64_to_user_ptr(args->data_ptr);
1033 remain = args->size;
1034 offset = args->offset;
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301035
1036 while (remain > 0) {
1037 /* Operation in this page
1038 *
1039 * page_base = page offset within aperture
1040 * page_offset = offset within page
1041 * page_length = bytes to copy for this page
1042 */
1043 u32 page_base = node.start;
1044 unsigned page_offset = offset_in_page(offset);
1045 unsigned page_length = PAGE_SIZE - page_offset;
1046 page_length = remain < page_length ? remain : page_length;
1047 if (node.allocated) {
1048 wmb();
Chris Wilson82ad6442018-06-05 16:37:58 +01001049 ggtt->vm.insert_page(&ggtt->vm,
1050 i915_gem_object_get_dma_address(obj, offset >> PAGE_SHIFT),
1051 node.start, I915_CACHE_NONE, 0);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301052 wmb();
1053 } else {
1054 page_base += offset & PAGE_MASK;
1055 }
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001056
Matthew Auld73ebd502017-12-11 15:18:20 +00001057 if (gtt_user_read(&ggtt->iomap, page_base, page_offset,
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001058 user_data, page_length)) {
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301059 ret = -EFAULT;
1060 break;
1061 }
1062
1063 remain -= page_length;
1064 user_data += page_length;
1065 offset += page_length;
1066 }
1067
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001068 mutex_lock(&i915->drm.struct_mutex);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301069out_unpin:
1070 if (node.allocated) {
1071 wmb();
Chris Wilson82ad6442018-06-05 16:37:58 +01001072 ggtt->vm.clear_range(&ggtt->vm, node.start, node.size);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301073 remove_mappable_node(&node);
1074 } else {
Chris Wilson058d88c2016-08-15 10:49:06 +01001075 i915_vma_unpin(vma);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301076 }
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001077out_unlock:
Chris Wilson538ef962019-01-14 14:21:18 +00001078 intel_runtime_pm_put(i915, wakeref);
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001079 mutex_unlock(&i915->drm.struct_mutex);
Chris Wilsonf60d7f02012-09-04 21:02:56 +01001080
Eric Anholteb014592009-03-10 11:44:52 -07001081 return ret;
1082}
1083
Eric Anholt673a3942008-07-30 12:06:12 -07001084/**
1085 * Reads data from the object referenced by handle.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01001086 * @dev: drm device pointer
1087 * @data: ioctl data blob
1088 * @file: drm file pointer
Eric Anholt673a3942008-07-30 12:06:12 -07001089 *
1090 * On error, the contents of *data are undefined.
1091 */
1092int
1093i915_gem_pread_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00001094 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001095{
1096 struct drm_i915_gem_pread *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00001097 struct drm_i915_gem_object *obj;
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001098 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07001099
Chris Wilson51311d02010-11-17 09:10:42 +00001100 if (args->size == 0)
1101 return 0;
1102
Linus Torvalds96d4f262019-01-03 18:57:57 -08001103 if (!access_ok(u64_to_user_ptr(args->data_ptr),
Chris Wilson51311d02010-11-17 09:10:42 +00001104 args->size))
1105 return -EFAULT;
1106
Chris Wilson03ac0642016-07-20 13:31:51 +01001107 obj = i915_gem_object_lookup(file, args->handle);
Chris Wilson258a5ed2016-08-05 10:14:16 +01001108 if (!obj)
1109 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07001110
Chris Wilson7dcd2492010-09-26 20:21:44 +01001111 /* Bounds check source. */
Matthew Auld966d5bf2016-12-13 20:32:22 +00001112 if (range_overflows_t(u64, args->offset, args->size, obj->base.size)) {
Chris Wilsonce9d4192010-09-26 20:50:05 +01001113 ret = -EINVAL;
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001114 goto out;
Chris Wilsonce9d4192010-09-26 20:50:05 +01001115 }
1116
Chris Wilsondb53a302011-02-03 11:57:46 +00001117 trace_i915_gem_object_pread(obj, args->offset, args->size);
1118
Chris Wilsone95433c2016-10-28 13:58:27 +01001119 ret = i915_gem_object_wait(obj,
1120 I915_WAIT_INTERRUPTIBLE,
Chris Wilson62eb3c22019-02-13 09:25:04 +00001121 MAX_SCHEDULE_TIMEOUT);
Chris Wilson258a5ed2016-08-05 10:14:16 +01001122 if (ret)
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001123 goto out;
Chris Wilson258a5ed2016-08-05 10:14:16 +01001124
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001125 ret = i915_gem_object_pin_pages(obj);
Chris Wilson258a5ed2016-08-05 10:14:16 +01001126 if (ret)
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001127 goto out;
Chris Wilson258a5ed2016-08-05 10:14:16 +01001128
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001129 ret = i915_gem_shmem_pread(obj, args);
Chris Wilson9c870d02016-10-24 13:42:15 +01001130 if (ret == -EFAULT || ret == -ENODEV)
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001131 ret = i915_gem_gtt_pread(obj, args);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301132
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001133 i915_gem_object_unpin_pages(obj);
1134out:
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001135 i915_gem_object_put(obj);
Eric Anholteb014592009-03-10 11:44:52 -07001136 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07001137}
1138
Keith Packard0839ccb2008-10-30 19:38:48 -07001139/* This is the fast write path which cannot handle
1140 * page faults in the source data
Linus Torvalds9b7530cc2008-10-20 14:16:43 -07001141 */
Linus Torvalds9b7530cc2008-10-20 14:16:43 -07001142
Chris Wilsonfe115622016-10-28 13:58:40 +01001143static inline bool
1144ggtt_write(struct io_mapping *mapping,
1145 loff_t base, int offset,
1146 char __user *user_data, int length)
Keith Packard0839ccb2008-10-30 19:38:48 -07001147{
Ville Syrjäläafe722b2017-09-01 20:12:52 +03001148 void __iomem *vaddr;
Keith Packard0839ccb2008-10-30 19:38:48 -07001149 unsigned long unwritten;
1150
Ben Widawsky4f0c7cf2012-04-16 14:07:47 -07001151 /* We can use the cpu mem copy function because this is X86. */
Ville Syrjäläafe722b2017-09-01 20:12:52 +03001152 vaddr = io_mapping_map_atomic_wc(mapping, base);
1153 unwritten = __copy_from_user_inatomic_nocache((void __force *)vaddr + offset,
Keith Packard0839ccb2008-10-30 19:38:48 -07001154 user_data, length);
Chris Wilsonfe115622016-10-28 13:58:40 +01001155 io_mapping_unmap_atomic(vaddr);
1156 if (unwritten) {
Ville Syrjäläafe722b2017-09-01 20:12:52 +03001157 vaddr = io_mapping_map_wc(mapping, base, PAGE_SIZE);
1158 unwritten = copy_from_user((void __force *)vaddr + offset,
1159 user_data, length);
Chris Wilsonfe115622016-10-28 13:58:40 +01001160 io_mapping_unmap(vaddr);
1161 }
Keith Packard0839ccb2008-10-30 19:38:48 -07001162
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001163 return unwritten;
1164}
1165
Eric Anholt3de09aa2009-03-09 09:42:23 -07001166/**
1167 * This is the fast pwrite path, where we copy the data directly from the
1168 * user into the GTT, uncached.
Chris Wilsonfe115622016-10-28 13:58:40 +01001169 * @obj: i915 GEM object
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01001170 * @args: pwrite arguments structure
Eric Anholt3de09aa2009-03-09 09:42:23 -07001171 */
Eric Anholt673a3942008-07-30 12:06:12 -07001172static int
Chris Wilsonfe115622016-10-28 13:58:40 +01001173i915_gem_gtt_pwrite_fast(struct drm_i915_gem_object *obj,
1174 const struct drm_i915_gem_pwrite *args)
Eric Anholt673a3942008-07-30 12:06:12 -07001175{
Chris Wilsonfe115622016-10-28 13:58:40 +01001176 struct drm_i915_private *i915 = to_i915(obj->base.dev);
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301177 struct i915_ggtt *ggtt = &i915->ggtt;
Chris Wilson538ef962019-01-14 14:21:18 +00001178 intel_wakeref_t wakeref;
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301179 struct drm_mm_node node;
Chris Wilsonfe115622016-10-28 13:58:40 +01001180 struct i915_vma *vma;
1181 u64 remain, offset;
1182 void __user *user_data;
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301183 int ret;
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301184
Chris Wilsonfe115622016-10-28 13:58:40 +01001185 ret = mutex_lock_interruptible(&i915->drm.struct_mutex);
1186 if (ret)
1187 return ret;
Daniel Vetter935aaa62012-03-25 19:47:35 +02001188
Chris Wilson8bd818152017-10-19 07:37:33 +01001189 if (i915_gem_object_has_struct_page(obj)) {
1190 /*
1191 * Avoid waking the device up if we can fallback, as
1192 * waking/resuming is very slow (worst-case 10-100 ms
1193 * depending on PCI sleeps and our own resume time).
1194 * This easily dwarfs any performance advantage from
1195 * using the cache bypass of indirect GGTT access.
1196 */
Chris Wilson538ef962019-01-14 14:21:18 +00001197 wakeref = intel_runtime_pm_get_if_in_use(i915);
1198 if (!wakeref) {
Chris Wilson8bd818152017-10-19 07:37:33 +01001199 ret = -EFAULT;
1200 goto out_unlock;
1201 }
1202 } else {
1203 /* No backing pages, no fallback, we must force GGTT access */
Chris Wilson538ef962019-01-14 14:21:18 +00001204 wakeref = intel_runtime_pm_get(i915);
Chris Wilson8bd818152017-10-19 07:37:33 +01001205 }
1206
Chris Wilson058d88c2016-08-15 10:49:06 +01001207 vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0,
Chris Wilsona3259ca2017-10-09 09:44:00 +01001208 PIN_MAPPABLE |
1209 PIN_NONFAULT |
1210 PIN_NONBLOCK);
Chris Wilson18034582016-08-18 17:16:45 +01001211 if (!IS_ERR(vma)) {
1212 node.start = i915_ggtt_offset(vma);
1213 node.allocated = false;
Chris Wilson49ef5292016-08-18 17:17:00 +01001214 ret = i915_vma_put_fence(vma);
Chris Wilson18034582016-08-18 17:16:45 +01001215 if (ret) {
1216 i915_vma_unpin(vma);
1217 vma = ERR_PTR(ret);
1218 }
1219 }
Chris Wilson058d88c2016-08-15 10:49:06 +01001220 if (IS_ERR(vma)) {
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001221 ret = insert_mappable_node(ggtt, &node, PAGE_SIZE);
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301222 if (ret)
Chris Wilson8bd818152017-10-19 07:37:33 +01001223 goto out_rpm;
Chris Wilsonfe115622016-10-28 13:58:40 +01001224 GEM_BUG_ON(!node.allocated);
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301225 }
Daniel Vetter935aaa62012-03-25 19:47:35 +02001226
1227 ret = i915_gem_object_set_to_gtt_domain(obj, true);
1228 if (ret)
1229 goto out_unpin;
1230
Chris Wilsonfe115622016-10-28 13:58:40 +01001231 mutex_unlock(&i915->drm.struct_mutex);
1232
Chris Wilsonb19482d2016-08-18 17:16:43 +01001233 intel_fb_obj_invalidate(obj, ORIGIN_CPU);
Paulo Zanoni063e4e62015-02-13 17:23:45 -02001234
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301235 user_data = u64_to_user_ptr(args->data_ptr);
1236 offset = args->offset;
1237 remain = args->size;
1238 while (remain) {
Eric Anholt673a3942008-07-30 12:06:12 -07001239 /* Operation in this page
1240 *
Keith Packard0839ccb2008-10-30 19:38:48 -07001241 * page_base = page offset within aperture
1242 * page_offset = offset within page
1243 * page_length = bytes to copy for this page
Eric Anholt673a3942008-07-30 12:06:12 -07001244 */
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301245 u32 page_base = node.start;
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001246 unsigned int page_offset = offset_in_page(offset);
1247 unsigned int page_length = PAGE_SIZE - page_offset;
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301248 page_length = remain < page_length ? remain : page_length;
1249 if (node.allocated) {
1250 wmb(); /* flush the write before we modify the GGTT */
Chris Wilson82ad6442018-06-05 16:37:58 +01001251 ggtt->vm.insert_page(&ggtt->vm,
1252 i915_gem_object_get_dma_address(obj, offset >> PAGE_SHIFT),
1253 node.start, I915_CACHE_NONE, 0);
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301254 wmb(); /* flush modifications to the GGTT (insert_page) */
1255 } else {
1256 page_base += offset & PAGE_MASK;
1257 }
Keith Packard0839ccb2008-10-30 19:38:48 -07001258 /* If we get a fault while copying data, then (presumably) our
Eric Anholt3de09aa2009-03-09 09:42:23 -07001259 * source page isn't available. Return the error and we'll
1260 * retry in the slow path.
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301261 * If the object is non-shmem backed, we retry again with the
1262 * path that handles page fault.
Keith Packard0839ccb2008-10-30 19:38:48 -07001263 */
Matthew Auld73ebd502017-12-11 15:18:20 +00001264 if (ggtt_write(&ggtt->iomap, page_base, page_offset,
Chris Wilsonfe115622016-10-28 13:58:40 +01001265 user_data, page_length)) {
1266 ret = -EFAULT;
1267 break;
Daniel Vetter935aaa62012-03-25 19:47:35 +02001268 }
Eric Anholt673a3942008-07-30 12:06:12 -07001269
Keith Packard0839ccb2008-10-30 19:38:48 -07001270 remain -= page_length;
1271 user_data += page_length;
1272 offset += page_length;
Eric Anholt673a3942008-07-30 12:06:12 -07001273 }
Chris Wilsond59b21e2017-02-22 11:40:49 +00001274 intel_fb_obj_flush(obj, ORIGIN_CPU);
Chris Wilsonfe115622016-10-28 13:58:40 +01001275
1276 mutex_lock(&i915->drm.struct_mutex);
Daniel Vetter935aaa62012-03-25 19:47:35 +02001277out_unpin:
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301278 if (node.allocated) {
1279 wmb();
Chris Wilson82ad6442018-06-05 16:37:58 +01001280 ggtt->vm.clear_range(&ggtt->vm, node.start, node.size);
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301281 remove_mappable_node(&node);
1282 } else {
Chris Wilson058d88c2016-08-15 10:49:06 +01001283 i915_vma_unpin(vma);
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301284 }
Chris Wilson8bd818152017-10-19 07:37:33 +01001285out_rpm:
Chris Wilson538ef962019-01-14 14:21:18 +00001286 intel_runtime_pm_put(i915, wakeref);
Chris Wilson8bd818152017-10-19 07:37:33 +01001287out_unlock:
Chris Wilsonfe115622016-10-28 13:58:40 +01001288 mutex_unlock(&i915->drm.struct_mutex);
Eric Anholt3de09aa2009-03-09 09:42:23 -07001289 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07001290}
1291
Chris Wilsonfe115622016-10-28 13:58:40 +01001292/* Per-page copy function for the shmem pwrite fastpath.
1293 * Flushes invalid cachelines before writing to the target if
1294 * needs_clflush_before is set and flushes out any written cachelines after
1295 * writing if needs_clflush is set.
1296 */
Eric Anholt40123c12009-03-09 13:42:30 -07001297static int
Chris Wilsonfe115622016-10-28 13:58:40 +01001298shmem_pwrite(struct page *page, int offset, int len, char __user *user_data,
Chris Wilsonfe115622016-10-28 13:58:40 +01001299 bool needs_clflush_before,
1300 bool needs_clflush_after)
Eric Anholt40123c12009-03-09 13:42:30 -07001301{
Chris Wilsonb9d126e2019-01-05 12:07:58 +00001302 char *vaddr;
Chris Wilsonfe115622016-10-28 13:58:40 +01001303 int ret;
Eric Anholt40123c12009-03-09 13:42:30 -07001304
Chris Wilsonb9d126e2019-01-05 12:07:58 +00001305 vaddr = kmap(page);
Chris Wilsonfe115622016-10-28 13:58:40 +01001306
Chris Wilsonb9d126e2019-01-05 12:07:58 +00001307 if (needs_clflush_before)
1308 drm_clflush_virt_range(vaddr + offset, len);
Chris Wilsonfe115622016-10-28 13:58:40 +01001309
Chris Wilsonb9d126e2019-01-05 12:07:58 +00001310 ret = __copy_from_user(vaddr + offset, user_data, len);
1311 if (!ret && needs_clflush_after)
1312 drm_clflush_virt_range(vaddr + offset, len);
Chris Wilsonfe115622016-10-28 13:58:40 +01001313
Chris Wilsonb9d126e2019-01-05 12:07:58 +00001314 kunmap(page);
1315
1316 return ret ? -EFAULT : 0;
Chris Wilsonfe115622016-10-28 13:58:40 +01001317}
1318
1319static int
1320i915_gem_shmem_pwrite(struct drm_i915_gem_object *obj,
1321 const struct drm_i915_gem_pwrite *args)
1322{
1323 struct drm_i915_private *i915 = to_i915(obj->base.dev);
1324 void __user *user_data;
1325 u64 remain;
Chris Wilsonfe115622016-10-28 13:58:40 +01001326 unsigned int partial_cacheline_write;
1327 unsigned int needs_clflush;
1328 unsigned int offset, idx;
1329 int ret;
1330
1331 ret = mutex_lock_interruptible(&i915->drm.struct_mutex);
Chris Wilson43394c72016-08-18 17:16:47 +01001332 if (ret)
1333 return ret;
Eric Anholt40123c12009-03-09 13:42:30 -07001334
Chris Wilsonfe115622016-10-28 13:58:40 +01001335 ret = i915_gem_obj_prepare_shmem_write(obj, &needs_clflush);
1336 mutex_unlock(&i915->drm.struct_mutex);
1337 if (ret)
1338 return ret;
1339
Chris Wilsonfe115622016-10-28 13:58:40 +01001340 /* If we don't overwrite a cacheline completely we need to be
1341 * careful to have up-to-date data by first clflushing. Don't
1342 * overcomplicate things and flush the entire patch.
1343 */
1344 partial_cacheline_write = 0;
1345 if (needs_clflush & CLFLUSH_BEFORE)
1346 partial_cacheline_write = boot_cpu_data.x86_clflush_size - 1;
1347
Chris Wilson43394c72016-08-18 17:16:47 +01001348 user_data = u64_to_user_ptr(args->data_ptr);
Chris Wilson43394c72016-08-18 17:16:47 +01001349 remain = args->size;
Chris Wilsonfe115622016-10-28 13:58:40 +01001350 offset = offset_in_page(args->offset);
1351 for (idx = args->offset >> PAGE_SHIFT; remain; idx++) {
1352 struct page *page = i915_gem_object_get_page(obj, idx);
Chris Wilsona5e856a52018-10-12 15:02:28 +01001353 unsigned int length = min_t(u64, remain, PAGE_SIZE - offset);
Chris Wilsone5281cc2010-10-28 13:45:36 +01001354
Chris Wilsonfe115622016-10-28 13:58:40 +01001355 ret = shmem_pwrite(page, offset, length, user_data,
Chris Wilsonfe115622016-10-28 13:58:40 +01001356 (offset | length) & partial_cacheline_write,
1357 needs_clflush & CLFLUSH_AFTER);
1358 if (ret)
Chris Wilson9da3da62012-06-01 15:20:22 +01001359 break;
1360
Chris Wilsonfe115622016-10-28 13:58:40 +01001361 remain -= length;
1362 user_data += length;
1363 offset = 0;
Eric Anholt40123c12009-03-09 13:42:30 -07001364 }
1365
Chris Wilsond59b21e2017-02-22 11:40:49 +00001366 intel_fb_obj_flush(obj, ORIGIN_CPU);
Chris Wilsonfe115622016-10-28 13:58:40 +01001367 i915_gem_obj_finish_shmem_access(obj);
Eric Anholt40123c12009-03-09 13:42:30 -07001368 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07001369}
1370
1371/**
1372 * Writes data to the object referenced by handle.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01001373 * @dev: drm device
1374 * @data: ioctl data blob
1375 * @file: drm file
Eric Anholt673a3942008-07-30 12:06:12 -07001376 *
1377 * On error, the contents of the buffer that were to be modified are undefined.
1378 */
1379int
1380i915_gem_pwrite_ioctl(struct drm_device *dev, void *data,
Chris Wilsonfbd5a262010-10-14 15:03:58 +01001381 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001382{
1383 struct drm_i915_gem_pwrite *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00001384 struct drm_i915_gem_object *obj;
Chris Wilson51311d02010-11-17 09:10:42 +00001385 int ret;
1386
1387 if (args->size == 0)
1388 return 0;
1389
Linus Torvalds96d4f262019-01-03 18:57:57 -08001390 if (!access_ok(u64_to_user_ptr(args->data_ptr), args->size))
Chris Wilson51311d02010-11-17 09:10:42 +00001391 return -EFAULT;
1392
Chris Wilson03ac0642016-07-20 13:31:51 +01001393 obj = i915_gem_object_lookup(file, args->handle);
Chris Wilson258a5ed2016-08-05 10:14:16 +01001394 if (!obj)
1395 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07001396
Chris Wilson7dcd2492010-09-26 20:21:44 +01001397 /* Bounds check destination. */
Matthew Auld966d5bf2016-12-13 20:32:22 +00001398 if (range_overflows_t(u64, args->offset, args->size, obj->base.size)) {
Chris Wilsonce9d4192010-09-26 20:50:05 +01001399 ret = -EINVAL;
Chris Wilson258a5ed2016-08-05 10:14:16 +01001400 goto err;
Chris Wilsonce9d4192010-09-26 20:50:05 +01001401 }
1402
Chris Wilsonf8c1cce2018-07-12 19:53:14 +01001403 /* Writes not allowed into this read-only object */
1404 if (i915_gem_object_is_readonly(obj)) {
1405 ret = -EINVAL;
1406 goto err;
1407 }
1408
Chris Wilsondb53a302011-02-03 11:57:46 +00001409 trace_i915_gem_object_pwrite(obj, args->offset, args->size);
1410
Chris Wilson7c55e2c2017-03-07 12:03:38 +00001411 ret = -ENODEV;
1412 if (obj->ops->pwrite)
1413 ret = obj->ops->pwrite(obj, args);
1414 if (ret != -ENODEV)
1415 goto err;
1416
Chris Wilsone95433c2016-10-28 13:58:27 +01001417 ret = i915_gem_object_wait(obj,
1418 I915_WAIT_INTERRUPTIBLE |
1419 I915_WAIT_ALL,
Chris Wilson62eb3c22019-02-13 09:25:04 +00001420 MAX_SCHEDULE_TIMEOUT);
Chris Wilson258a5ed2016-08-05 10:14:16 +01001421 if (ret)
1422 goto err;
1423
Chris Wilsonfe115622016-10-28 13:58:40 +01001424 ret = i915_gem_object_pin_pages(obj);
Chris Wilson258a5ed2016-08-05 10:14:16 +01001425 if (ret)
Chris Wilsonfe115622016-10-28 13:58:40 +01001426 goto err;
Chris Wilson258a5ed2016-08-05 10:14:16 +01001427
Daniel Vetter935aaa62012-03-25 19:47:35 +02001428 ret = -EFAULT;
Eric Anholt673a3942008-07-30 12:06:12 -07001429 /* We can only do the GTT pwrite on untiled buffers, as otherwise
1430 * it would end up going through the fenced access, and we'll get
1431 * different detiling behavior between reading and writing.
1432 * pread/pwrite currently are reading and writing from the CPU
1433 * perspective, requiring manual detiling by the client.
1434 */
Chris Wilson6eae0052016-06-20 15:05:52 +01001435 if (!i915_gem_object_has_struct_page(obj) ||
Chris Wilson9c870d02016-10-24 13:42:15 +01001436 cpu_write_needs_clflush(obj))
Daniel Vetter935aaa62012-03-25 19:47:35 +02001437 /* Note that the gtt paths might fail with non-page-backed user
1438 * pointers (e.g. gtt mappings when moving data between
Chris Wilson9c870d02016-10-24 13:42:15 +01001439 * textures). Fallback to the shmem path in that case.
1440 */
Chris Wilsonfe115622016-10-28 13:58:40 +01001441 ret = i915_gem_gtt_pwrite_fast(obj, args);
Eric Anholt673a3942008-07-30 12:06:12 -07001442
Chris Wilsond1054ee2016-07-16 18:42:36 +01001443 if (ret == -EFAULT || ret == -ENOSPC) {
Chris Wilson6a2c4232014-11-04 04:51:40 -08001444 if (obj->phys_handle)
1445 ret = i915_gem_phys_pwrite(obj, args, file);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301446 else
Chris Wilsonfe115622016-10-28 13:58:40 +01001447 ret = i915_gem_shmem_pwrite(obj, args);
Chris Wilson6a2c4232014-11-04 04:51:40 -08001448 }
Daniel Vetter5c0480f2011-12-14 13:57:30 +01001449
Chris Wilsonfe115622016-10-28 13:58:40 +01001450 i915_gem_object_unpin_pages(obj);
Chris Wilson258a5ed2016-08-05 10:14:16 +01001451err:
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001452 i915_gem_object_put(obj);
Chris Wilson258a5ed2016-08-05 10:14:16 +01001453 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07001454}
1455
Chris Wilson40e62d52016-10-28 13:58:41 +01001456static void i915_gem_object_bump_inactive_ggtt(struct drm_i915_gem_object *obj)
1457{
Chris Wilson09d7e462019-01-28 10:23:53 +00001458 struct drm_i915_private *i915 = to_i915(obj->base.dev);
Chris Wilson40e62d52016-10-28 13:58:41 +01001459 struct list_head *list;
1460 struct i915_vma *vma;
1461
Chris Wilsonf2123812017-10-16 12:40:37 +01001462 GEM_BUG_ON(!i915_gem_object_has_pinned_pages(obj));
1463
Chris Wilson09d7e462019-01-28 10:23:53 +00001464 mutex_lock(&i915->ggtt.vm.mutex);
Chris Wilsone2189dd2017-12-07 21:14:07 +00001465 for_each_ggtt_vma(vma, obj) {
Chris Wilson40e62d52016-10-28 13:58:41 +01001466 if (!drm_mm_node_allocated(&vma->node))
1467 continue;
1468
Chris Wilson499197d2019-01-28 10:23:52 +00001469 list_move_tail(&vma->vm_link, &vma->vm->bound_list);
Chris Wilson40e62d52016-10-28 13:58:41 +01001470 }
Chris Wilson09d7e462019-01-28 10:23:53 +00001471 mutex_unlock(&i915->ggtt.vm.mutex);
Chris Wilson40e62d52016-10-28 13:58:41 +01001472
Chris Wilsonf2123812017-10-16 12:40:37 +01001473 spin_lock(&i915->mm.obj_lock);
Chris Wilson40e62d52016-10-28 13:58:41 +01001474 list = obj->bind_count ? &i915->mm.bound_list : &i915->mm.unbound_list;
Chris Wilsonf2123812017-10-16 12:40:37 +01001475 list_move_tail(&obj->mm.link, list);
1476 spin_unlock(&i915->mm.obj_lock);
Chris Wilson40e62d52016-10-28 13:58:41 +01001477}
1478
Eric Anholt673a3942008-07-30 12:06:12 -07001479/**
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001480 * Called when user space prepares to use an object with the CPU, either
1481 * through the mmap ioctl's mapping or a GTT mapping.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01001482 * @dev: drm device
1483 * @data: ioctl data blob
1484 * @file: drm file
Eric Anholt673a3942008-07-30 12:06:12 -07001485 */
1486int
1487i915_gem_set_domain_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00001488 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001489{
1490 struct drm_i915_gem_set_domain *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00001491 struct drm_i915_gem_object *obj;
Jani Nikula739f3ab2019-01-16 11:15:19 +02001492 u32 read_domains = args->read_domains;
1493 u32 write_domain = args->write_domain;
Chris Wilson40e62d52016-10-28 13:58:41 +01001494 int err;
Eric Anholt673a3942008-07-30 12:06:12 -07001495
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001496 /* Only handle setting domains to types used by the CPU. */
Chris Wilsonb8f90962016-08-05 10:14:07 +01001497 if ((write_domain | read_domains) & I915_GEM_GPU_DOMAINS)
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001498 return -EINVAL;
1499
1500 /* Having something in the write domain implies it's in the read
1501 * domain, and only that read domain. Enforce that in the request.
1502 */
1503 if (write_domain != 0 && read_domains != write_domain)
1504 return -EINVAL;
1505
Chris Wilson03ac0642016-07-20 13:31:51 +01001506 obj = i915_gem_object_lookup(file, args->handle);
Chris Wilsonb8f90962016-08-05 10:14:07 +01001507 if (!obj)
1508 return -ENOENT;
Jesse Barnes652c3932009-08-17 13:31:43 -07001509
Chris Wilson3236f572012-08-24 09:35:09 +01001510 /* Try to flush the object off the GPU without holding the lock.
1511 * We will repeat the flush holding the lock in the normal manner
1512 * to catch cases where we are gazumped.
1513 */
Chris Wilson40e62d52016-10-28 13:58:41 +01001514 err = i915_gem_object_wait(obj,
Chris Wilsone95433c2016-10-28 13:58:27 +01001515 I915_WAIT_INTERRUPTIBLE |
Chris Wilsone9eaf822018-10-01 15:47:55 +01001516 I915_WAIT_PRIORITY |
Chris Wilsone95433c2016-10-28 13:58:27 +01001517 (write_domain ? I915_WAIT_ALL : 0),
Chris Wilson62eb3c22019-02-13 09:25:04 +00001518 MAX_SCHEDULE_TIMEOUT);
Chris Wilson40e62d52016-10-28 13:58:41 +01001519 if (err)
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001520 goto out;
Chris Wilsonb8f90962016-08-05 10:14:07 +01001521
Tina Zhanga03f3952017-11-14 10:25:13 +00001522 /*
1523 * Proxy objects do not control access to the backing storage, ergo
1524 * they cannot be used as a means to manipulate the cache domain
1525 * tracking for that backing storage. The proxy object is always
1526 * considered to be outside of any cache domain.
1527 */
1528 if (i915_gem_object_is_proxy(obj)) {
1529 err = -ENXIO;
1530 goto out;
1531 }
1532
1533 /*
1534 * Flush and acquire obj->pages so that we are coherent through
Chris Wilson40e62d52016-10-28 13:58:41 +01001535 * direct access in memory with previous cached writes through
1536 * shmemfs and that our cache domain tracking remains valid.
1537 * For example, if the obj->filp was moved to swap without us
1538 * being notified and releasing the pages, we would mistakenly
1539 * continue to assume that the obj remained out of the CPU cached
1540 * domain.
1541 */
1542 err = i915_gem_object_pin_pages(obj);
1543 if (err)
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001544 goto out;
Chris Wilson40e62d52016-10-28 13:58:41 +01001545
1546 err = i915_mutex_lock_interruptible(dev);
1547 if (err)
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001548 goto out_unpin;
Chris Wilson3236f572012-08-24 09:35:09 +01001549
Chris Wilsone22d8e32017-04-12 12:01:11 +01001550 if (read_domains & I915_GEM_DOMAIN_WC)
1551 err = i915_gem_object_set_to_wc_domain(obj, write_domain);
1552 else if (read_domains & I915_GEM_DOMAIN_GTT)
1553 err = i915_gem_object_set_to_gtt_domain(obj, write_domain);
Chris Wilson43566de2015-01-02 16:29:29 +05301554 else
Chris Wilsone22d8e32017-04-12 12:01:11 +01001555 err = i915_gem_object_set_to_cpu_domain(obj, write_domain);
Chris Wilson40e62d52016-10-28 13:58:41 +01001556
1557 /* And bump the LRU for this access */
1558 i915_gem_object_bump_inactive_ggtt(obj);
1559
1560 mutex_unlock(&dev->struct_mutex);
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001561
Daniel Vetter031b6982015-06-26 19:35:16 +02001562 if (write_domain != 0)
Chris Wilsonef749212017-04-12 12:01:10 +01001563 intel_fb_obj_invalidate(obj,
1564 fb_write_origin(obj, write_domain));
Daniel Vetter031b6982015-06-26 19:35:16 +02001565
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001566out_unpin:
Chris Wilson40e62d52016-10-28 13:58:41 +01001567 i915_gem_object_unpin_pages(obj);
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001568out:
1569 i915_gem_object_put(obj);
Chris Wilson40e62d52016-10-28 13:58:41 +01001570 return err;
Eric Anholt673a3942008-07-30 12:06:12 -07001571}
1572
1573/**
1574 * Called when user space has done writes to this buffer
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01001575 * @dev: drm device
1576 * @data: ioctl data blob
1577 * @file: drm file
Eric Anholt673a3942008-07-30 12:06:12 -07001578 */
1579int
1580i915_gem_sw_finish_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00001581 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001582{
1583 struct drm_i915_gem_sw_finish *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00001584 struct drm_i915_gem_object *obj;
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001585
Chris Wilson03ac0642016-07-20 13:31:51 +01001586 obj = i915_gem_object_lookup(file, args->handle);
Chris Wilsonc21724c2016-08-05 10:14:19 +01001587 if (!obj)
1588 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07001589
Tina Zhanga03f3952017-11-14 10:25:13 +00001590 /*
1591 * Proxy objects are barred from CPU access, so there is no
1592 * need to ban sw_finish as it is a nop.
1593 */
1594
Eric Anholt673a3942008-07-30 12:06:12 -07001595 /* Pinned buffers may be scanout, so flush the cache */
Chris Wilson5a97bcc2017-02-22 11:40:46 +00001596 i915_gem_object_flush_if_display(obj);
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001597 i915_gem_object_put(obj);
Chris Wilson5a97bcc2017-02-22 11:40:46 +00001598
1599 return 0;
Eric Anholt673a3942008-07-30 12:06:12 -07001600}
1601
Joonas Lahtinen5c4604e2019-02-07 10:54:53 +02001602static inline bool
1603__vma_matches(struct vm_area_struct *vma, struct file *filp,
1604 unsigned long addr, unsigned long size)
1605{
1606 if (vma->vm_file != filp)
1607 return false;
1608
1609 return vma->vm_start == addr && (vma->vm_end - vma->vm_start) == size;
1610}
1611
Eric Anholt673a3942008-07-30 12:06:12 -07001612/**
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01001613 * i915_gem_mmap_ioctl - Maps the contents of an object, returning the address
1614 * it is mapped to.
1615 * @dev: drm device
1616 * @data: ioctl data blob
1617 * @file: drm file
Eric Anholt673a3942008-07-30 12:06:12 -07001618 *
1619 * While the mapping holds a reference on the contents of the object, it doesn't
1620 * imply a ref on the object itself.
Daniel Vetter34367382014-10-16 12:28:18 +02001621 *
1622 * IMPORTANT:
1623 *
1624 * DRM driver writers who look a this function as an example for how to do GEM
1625 * mmap support, please don't implement mmap support like here. The modern way
1626 * to implement DRM mmap support is with an mmap offset ioctl (like
1627 * i915_gem_mmap_gtt) and then using the mmap syscall on the DRM fd directly.
1628 * That way debug tooling like valgrind will understand what's going on, hiding
1629 * the mmap call in a driver private ioctl will break that. The i915 driver only
1630 * does cpu mmaps this way because we didn't know better.
Eric Anholt673a3942008-07-30 12:06:12 -07001631 */
1632int
1633i915_gem_mmap_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00001634 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001635{
1636 struct drm_i915_gem_mmap *args = data;
Chris Wilson03ac0642016-07-20 13:31:51 +01001637 struct drm_i915_gem_object *obj;
Eric Anholt673a3942008-07-30 12:06:12 -07001638 unsigned long addr;
1639
Akash Goel1816f922015-01-02 16:29:30 +05301640 if (args->flags & ~(I915_MMAP_WC))
1641 return -EINVAL;
1642
Borislav Petkov568a58e2016-03-29 17:42:01 +02001643 if (args->flags & I915_MMAP_WC && !boot_cpu_has(X86_FEATURE_PAT))
Akash Goel1816f922015-01-02 16:29:30 +05301644 return -ENODEV;
1645
Chris Wilson03ac0642016-07-20 13:31:51 +01001646 obj = i915_gem_object_lookup(file, args->handle);
1647 if (!obj)
Chris Wilsonbf79cb92010-08-04 14:19:46 +01001648 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07001649
Daniel Vetter1286ff72012-05-10 15:25:09 +02001650 /* prime objects have no backing filp to GEM mmap
1651 * pages from.
1652 */
Chris Wilson03ac0642016-07-20 13:31:51 +01001653 if (!obj->base.filp) {
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001654 i915_gem_object_put(obj);
Tina Zhang274b2462017-11-14 10:25:12 +00001655 return -ENXIO;
Daniel Vetter1286ff72012-05-10 15:25:09 +02001656 }
1657
Chris Wilson03ac0642016-07-20 13:31:51 +01001658 addr = vm_mmap(obj->base.filp, 0, args->size,
Eric Anholt673a3942008-07-30 12:06:12 -07001659 PROT_READ | PROT_WRITE, MAP_SHARED,
1660 args->offset);
Joonas Lahtinenebfb6972019-02-07 10:54:54 +02001661 if (IS_ERR_VALUE(addr))
1662 goto err;
1663
Akash Goel1816f922015-01-02 16:29:30 +05301664 if (args->flags & I915_MMAP_WC) {
1665 struct mm_struct *mm = current->mm;
1666 struct vm_area_struct *vma;
1667
Michal Hocko80a89a52016-05-23 16:26:11 -07001668 if (down_write_killable(&mm->mmap_sem)) {
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001669 i915_gem_object_put(obj);
Michal Hocko80a89a52016-05-23 16:26:11 -07001670 return -EINTR;
1671 }
Akash Goel1816f922015-01-02 16:29:30 +05301672 vma = find_vma(mm, addr);
Joonas Lahtinen5c4604e2019-02-07 10:54:53 +02001673 if (vma && __vma_matches(vma, obj->base.filp, addr, args->size))
Akash Goel1816f922015-01-02 16:29:30 +05301674 vma->vm_page_prot =
1675 pgprot_writecombine(vm_get_page_prot(vma->vm_flags));
1676 else
1677 addr = -ENOMEM;
1678 up_write(&mm->mmap_sem);
Joonas Lahtinenebfb6972019-02-07 10:54:54 +02001679 if (IS_ERR_VALUE(addr))
1680 goto err;
Chris Wilsonaeecc962016-06-17 14:46:39 -03001681
1682 /* This may race, but that's ok, it only gets set */
Chris Wilson50349242016-08-18 17:17:04 +01001683 WRITE_ONCE(obj->frontbuffer_ggtt_origin, ORIGIN_CPU);
Akash Goel1816f922015-01-02 16:29:30 +05301684 }
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001685 i915_gem_object_put(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001686
Jani Nikula739f3ab2019-01-16 11:15:19 +02001687 args->addr_ptr = (u64)addr;
Eric Anholt673a3942008-07-30 12:06:12 -07001688
1689 return 0;
Joonas Lahtinenebfb6972019-02-07 10:54:54 +02001690
1691err:
1692 i915_gem_object_put(obj);
1693
1694 return addr;
Eric Anholt673a3942008-07-30 12:06:12 -07001695}
1696
Chris Wilsond899ace2018-07-25 16:54:47 +01001697static unsigned int tile_row_pages(const struct drm_i915_gem_object *obj)
Chris Wilson03af84f2016-08-18 17:17:01 +01001698{
Chris Wilson6649a0b2017-01-09 16:16:08 +00001699 return i915_gem_object_get_tile_row_size(obj) >> PAGE_SHIFT;
Chris Wilson03af84f2016-08-18 17:17:01 +01001700}
1701
Jesse Barnesde151cf2008-11-12 10:03:55 -08001702/**
Chris Wilson4cc69072016-08-25 19:05:19 +01001703 * i915_gem_mmap_gtt_version - report the current feature set for GTT mmaps
1704 *
1705 * A history of the GTT mmap interface:
1706 *
1707 * 0 - Everything had to fit into the GTT. Both parties of a memcpy had to
1708 * aligned and suitable for fencing, and still fit into the available
1709 * mappable space left by the pinned display objects. A classic problem
1710 * we called the page-fault-of-doom where we would ping-pong between
1711 * two objects that could not fit inside the GTT and so the memcpy
1712 * would page one object in at the expense of the other between every
1713 * single byte.
1714 *
1715 * 1 - Objects can be any size, and have any compatible fencing (X Y, or none
1716 * as set via i915_gem_set_tiling() [DRM_I915_GEM_SET_TILING]). If the
1717 * object is too large for the available space (or simply too large
1718 * for the mappable aperture!), a view is created instead and faulted
1719 * into userspace. (This view is aligned and sized appropriately for
1720 * fenced access.)
1721 *
Chris Wilsone22d8e32017-04-12 12:01:11 +01001722 * 2 - Recognise WC as a separate cache domain so that we can flush the
1723 * delayed writes via GTT before performing direct access via WC.
1724 *
Chris Wilson4cc69072016-08-25 19:05:19 +01001725 * Restrictions:
1726 *
1727 * * snoopable objects cannot be accessed via the GTT. It can cause machine
1728 * hangs on some architectures, corruption on others. An attempt to service
1729 * a GTT page fault from a snoopable object will generate a SIGBUS.
1730 *
1731 * * the object must be able to fit into RAM (physical memory, though no
1732 * limited to the mappable aperture).
1733 *
1734 *
1735 * Caveats:
1736 *
1737 * * a new GTT page fault will synchronize rendering from the GPU and flush
1738 * all data to system memory. Subsequent access will not be synchronized.
1739 *
1740 * * all mappings are revoked on runtime device suspend.
1741 *
1742 * * there are only 8, 16 or 32 fence registers to share between all users
1743 * (older machines require fence register for display and blitter access
1744 * as well). Contention of the fence registers will cause the previous users
1745 * to be unmapped and any new access will generate new page faults.
1746 *
1747 * * running out of memory while servicing a fault may generate a SIGBUS,
1748 * rather than the expected SIGSEGV.
1749 */
1750int i915_gem_mmap_gtt_version(void)
1751{
Chris Wilsone22d8e32017-04-12 12:01:11 +01001752 return 2;
Chris Wilson4cc69072016-08-25 19:05:19 +01001753}
1754
Chris Wilson2d4281b2017-01-10 09:56:32 +00001755static inline struct i915_ggtt_view
Chris Wilsond899ace2018-07-25 16:54:47 +01001756compute_partial_view(const struct drm_i915_gem_object *obj,
Chris Wilson2d4281b2017-01-10 09:56:32 +00001757 pgoff_t page_offset,
1758 unsigned int chunk)
1759{
1760 struct i915_ggtt_view view;
1761
1762 if (i915_gem_object_is_tiled(obj))
1763 chunk = roundup(chunk, tile_row_pages(obj));
1764
Chris Wilson2d4281b2017-01-10 09:56:32 +00001765 view.type = I915_GGTT_VIEW_PARTIAL;
Chris Wilson8bab11932017-01-14 00:28:25 +00001766 view.partial.offset = rounddown(page_offset, chunk);
1767 view.partial.size =
Chris Wilson2d4281b2017-01-10 09:56:32 +00001768 min_t(unsigned int, chunk,
Chris Wilson8bab11932017-01-14 00:28:25 +00001769 (obj->base.size >> PAGE_SHIFT) - view.partial.offset);
Chris Wilson2d4281b2017-01-10 09:56:32 +00001770
1771 /* If the partial covers the entire object, just create a normal VMA. */
1772 if (chunk >= obj->base.size >> PAGE_SHIFT)
1773 view.type = I915_GGTT_VIEW_NORMAL;
1774
1775 return view;
1776}
1777
Chris Wilson4cc69072016-08-25 19:05:19 +01001778/**
Jesse Barnesde151cf2008-11-12 10:03:55 -08001779 * i915_gem_fault - fault a page into the GTT
Geliang Tangd9072a32015-09-15 05:58:44 -07001780 * @vmf: fault info
Jesse Barnesde151cf2008-11-12 10:03:55 -08001781 *
1782 * The fault handler is set up by drm_gem_mmap() when a object is GTT mapped
1783 * from userspace. The fault handler takes care of binding the object to
1784 * the GTT (if needed), allocating and programming a fence register (again,
1785 * only if needed based on whether the old reg is still valid or the object
1786 * is tiled) and inserting a new PTE into the faulting process.
1787 *
1788 * Note that the faulting process may involve evicting existing objects
1789 * from the GTT and/or fence registers to make room. So performance may
1790 * suffer if the GTT working set is large or there are few fence registers
1791 * left.
Chris Wilson4cc69072016-08-25 19:05:19 +01001792 *
1793 * The current feature set supported by i915_gem_fault() and thus GTT mmaps
1794 * is exposed via I915_PARAM_MMAP_GTT_VERSION (see i915_gem_mmap_gtt_version).
Jesse Barnesde151cf2008-11-12 10:03:55 -08001795 */
Chris Wilson52137012018-06-06 22:45:20 +01001796vm_fault_t i915_gem_fault(struct vm_fault *vmf)
Jesse Barnesde151cf2008-11-12 10:03:55 -08001797{
Chris Wilson420980c2018-06-05 14:57:46 +01001798#define MIN_CHUNK_PAGES (SZ_1M >> PAGE_SHIFT)
Dave Jiang11bac802017-02-24 14:56:41 -08001799 struct vm_area_struct *area = vmf->vma;
Chris Wilson058d88c2016-08-15 10:49:06 +01001800 struct drm_i915_gem_object *obj = to_intel_bo(area->vm_private_data);
Chris Wilson05394f32010-11-08 19:18:58 +00001801 struct drm_device *dev = obj->base.dev;
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03001802 struct drm_i915_private *dev_priv = to_i915(dev);
1803 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Chris Wilsonaae7c062018-09-03 09:33:34 +01001804 bool write = area->vm_flags & VM_WRITE;
Chris Wilson538ef962019-01-14 14:21:18 +00001805 intel_wakeref_t wakeref;
Chris Wilson058d88c2016-08-15 10:49:06 +01001806 struct i915_vma *vma;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001807 pgoff_t page_offset;
Chris Wilson2caffbf2019-02-08 15:37:03 +00001808 int srcu;
Chris Wilsonb8f90962016-08-05 10:14:07 +01001809 int ret;
Paulo Zanonif65c9162013-11-27 18:20:34 -02001810
Chris Wilson3e977ac2018-07-12 19:53:13 +01001811 /* Sanity check that we allow writing into this object */
1812 if (i915_gem_object_is_readonly(obj) && write)
1813 return VM_FAULT_SIGBUS;
1814
Jesse Barnesde151cf2008-11-12 10:03:55 -08001815 /* We don't use vmf->pgoff since that has the fake offset */
Jan Kara1a29d852016-12-14 15:07:01 -08001816 page_offset = (vmf->address - area->vm_start) >> PAGE_SHIFT;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001817
Chris Wilsondb53a302011-02-03 11:57:46 +00001818 trace_i915_gem_object_fault(obj, page_offset, true, write);
1819
Chris Wilson6e4930f2014-02-07 18:37:06 -02001820 /* Try to flush the object off the GPU first without holding the lock.
Chris Wilsonb8f90962016-08-05 10:14:07 +01001821 * Upon acquiring the lock, we will perform our sanity checks and then
Chris Wilson6e4930f2014-02-07 18:37:06 -02001822 * repeat the flush holding the lock in the normal manner to catch cases
1823 * where we are gazumped.
1824 */
Chris Wilsone95433c2016-10-28 13:58:27 +01001825 ret = i915_gem_object_wait(obj,
1826 I915_WAIT_INTERRUPTIBLE,
Chris Wilson62eb3c22019-02-13 09:25:04 +00001827 MAX_SCHEDULE_TIMEOUT);
Chris Wilson6e4930f2014-02-07 18:37:06 -02001828 if (ret)
Chris Wilsonb8f90962016-08-05 10:14:07 +01001829 goto err;
1830
Chris Wilson40e62d52016-10-28 13:58:41 +01001831 ret = i915_gem_object_pin_pages(obj);
1832 if (ret)
1833 goto err;
1834
Chris Wilson538ef962019-01-14 14:21:18 +00001835 wakeref = intel_runtime_pm_get(dev_priv);
Chris Wilsonb8f90962016-08-05 10:14:07 +01001836
Chris Wilson43a8f682019-02-21 10:29:19 +00001837 srcu = i915_reset_trylock(dev_priv);
1838 if (srcu < 0) {
1839 ret = srcu;
1840 goto err_rpm;
1841 }
1842
Chris Wilsonb8f90962016-08-05 10:14:07 +01001843 ret = i915_mutex_lock_interruptible(dev);
1844 if (ret)
Chris Wilson43a8f682019-02-21 10:29:19 +00001845 goto err_reset;
Chris Wilson6e4930f2014-02-07 18:37:06 -02001846
Chris Wilsoneb119bd2012-12-16 12:43:36 +00001847 /* Access to snoopable pages through the GTT is incoherent. */
Tvrtko Ursulin0031fb92016-11-04 14:42:44 +00001848 if (obj->cache_level != I915_CACHE_NONE && !HAS_LLC(dev_priv)) {
Chris Wilsonddeff6e2014-05-28 16:16:41 +01001849 ret = -EFAULT;
Chris Wilsonb8f90962016-08-05 10:14:07 +01001850 goto err_unlock;
Chris Wilsoneb119bd2012-12-16 12:43:36 +00001851 }
1852
Chris Wilsona61007a2016-08-18 17:17:02 +01001853 /* Now pin it into the GTT as needed */
Chris Wilson7e7367d2018-06-30 10:05:09 +01001854 vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0,
1855 PIN_MAPPABLE |
1856 PIN_NONBLOCK |
1857 PIN_NONFAULT);
Chris Wilsona61007a2016-08-18 17:17:02 +01001858 if (IS_ERR(vma)) {
Chris Wilsona61007a2016-08-18 17:17:02 +01001859 /* Use a partial view if it is bigger than available space */
Chris Wilson2d4281b2017-01-10 09:56:32 +00001860 struct i915_ggtt_view view =
Chris Wilson8201c1f2017-01-10 09:56:33 +00001861 compute_partial_view(obj, page_offset, MIN_CHUNK_PAGES);
Chris Wilson7e7367d2018-06-30 10:05:09 +01001862 unsigned int flags;
Chris Wilsonaa136d92016-08-18 17:17:03 +01001863
Chris Wilson7e7367d2018-06-30 10:05:09 +01001864 flags = PIN_MAPPABLE;
1865 if (view.type == I915_GGTT_VIEW_NORMAL)
1866 flags |= PIN_NONBLOCK; /* avoid warnings for pinned */
1867
1868 /*
1869 * Userspace is now writing through an untracked VMA, abandon
Chris Wilson50349242016-08-18 17:17:04 +01001870 * all hope that the hardware is able to track future writes.
1871 */
1872 obj->frontbuffer_ggtt_origin = ORIGIN_CPU;
1873
Chris Wilson7e7367d2018-06-30 10:05:09 +01001874 vma = i915_gem_object_ggtt_pin(obj, &view, 0, 0, flags);
1875 if (IS_ERR(vma) && !view.type) {
1876 flags = PIN_MAPPABLE;
1877 view.type = I915_GGTT_VIEW_PARTIAL;
1878 vma = i915_gem_object_ggtt_pin(obj, &view, 0, 0, flags);
1879 }
Chris Wilsona61007a2016-08-18 17:17:02 +01001880 }
Chris Wilson058d88c2016-08-15 10:49:06 +01001881 if (IS_ERR(vma)) {
1882 ret = PTR_ERR(vma);
Chris Wilsonb8f90962016-08-05 10:14:07 +01001883 goto err_unlock;
Chris Wilson058d88c2016-08-15 10:49:06 +01001884 }
Jesse Barnesde151cf2008-11-12 10:03:55 -08001885
Chris Wilsonc9839302012-11-20 10:45:17 +00001886 ret = i915_gem_object_set_to_gtt_domain(obj, write);
1887 if (ret)
Chris Wilsonb8f90962016-08-05 10:14:07 +01001888 goto err_unpin;
Chris Wilsonc9839302012-11-20 10:45:17 +00001889
Chris Wilsonaeaaa552019-02-12 13:08:30 +00001890 ret = i915_vma_pin_fence(vma);
1891 if (ret)
1892 goto err_unpin;
1893
Chris Wilsonb90b91d2014-06-10 12:14:40 +01001894 /* Finally, remap it using the new GTT offset */
Chris Wilsonc58305a2016-08-19 16:54:28 +01001895 ret = remap_io_mapping(area,
Chris Wilson8bab11932017-01-14 00:28:25 +00001896 area->vm_start + (vma->ggtt_view.partial.offset << PAGE_SHIFT),
Matthew Auld73ebd502017-12-11 15:18:20 +00001897 (ggtt->gmadr.start + vma->node.start) >> PAGE_SHIFT,
Chris Wilsonc58305a2016-08-19 16:54:28 +01001898 min_t(u64, vma->size, area->vm_end - area->vm_start),
Matthew Auld73ebd502017-12-11 15:18:20 +00001899 &ggtt->iomap);
Chris Wilsona65adaf2017-10-09 09:43:57 +01001900 if (ret)
Chris Wilson43a8f682019-02-21 10:29:19 +00001901 goto err_fence;
Chris Wilsona61007a2016-08-18 17:17:02 +01001902
Chris Wilsona65adaf2017-10-09 09:43:57 +01001903 /* Mark as being mmapped into userspace for later revocation */
1904 assert_rpm_wakelock_held(dev_priv);
1905 if (!i915_vma_set_userfault(vma) && !obj->userfault_count++)
1906 list_add(&obj->userfault_link, &dev_priv->mm.userfault_list);
1907 GEM_BUG_ON(!obj->userfault_count);
1908
Chris Wilson7125397b2017-12-06 12:49:14 +00001909 i915_vma_set_ggtt_write(vma);
1910
Chris Wilsonaeaaa552019-02-12 13:08:30 +00001911err_fence:
1912 i915_vma_unpin_fence(vma);
Chris Wilsonb8f90962016-08-05 10:14:07 +01001913err_unpin:
Chris Wilson058d88c2016-08-15 10:49:06 +01001914 __i915_vma_unpin(vma);
Chris Wilsonb8f90962016-08-05 10:14:07 +01001915err_unlock:
Jesse Barnesde151cf2008-11-12 10:03:55 -08001916 mutex_unlock(&dev->struct_mutex);
Chris Wilson43a8f682019-02-21 10:29:19 +00001917err_reset:
1918 i915_reset_unlock(dev_priv, srcu);
Chris Wilsonb8f90962016-08-05 10:14:07 +01001919err_rpm:
Chris Wilson538ef962019-01-14 14:21:18 +00001920 intel_runtime_pm_put(dev_priv, wakeref);
Chris Wilson40e62d52016-10-28 13:58:41 +01001921 i915_gem_object_unpin_pages(obj);
Chris Wilsonb8f90962016-08-05 10:14:07 +01001922err:
Jesse Barnesde151cf2008-11-12 10:03:55 -08001923 switch (ret) {
Chris Wilsond9bc7e92011-02-07 13:09:31 +00001924 case -EIO:
Daniel Vetter2232f032014-09-04 09:36:18 +02001925 /*
1926 * We eat errors when the gpu is terminally wedged to avoid
1927 * userspace unduly crashing (gl has no provisions for mmaps to
1928 * fail). But any other -EIO isn't ours (e.g. swap in failure)
1929 * and so needs to be reported.
1930 */
Chris Wilsonc41166f2019-02-20 14:56:37 +00001931 if (!i915_terminally_wedged(dev_priv))
Chris Wilson52137012018-06-06 22:45:20 +01001932 return VM_FAULT_SIGBUS;
Gustavo A. R. Silvaf0d759f2018-06-28 17:35:41 -05001933 /* else: fall through */
Chris Wilson045e7692010-11-07 09:18:22 +00001934 case -EAGAIN:
Daniel Vetter571c6082013-09-12 17:57:28 +02001935 /*
1936 * EAGAIN means the gpu is hung and we'll wait for the error
1937 * handler to reset everything when re-faulting in
1938 * i915_mutex_lock_interruptible.
Chris Wilsond9bc7e92011-02-07 13:09:31 +00001939 */
Chris Wilsonc7150892009-09-23 00:43:56 +01001940 case 0:
1941 case -ERESTARTSYS:
Chris Wilsonbed636a2011-02-11 20:31:19 +00001942 case -EINTR:
Dmitry Rogozhkine79e0fe2012-10-03 17:15:26 +03001943 case -EBUSY:
1944 /*
1945 * EBUSY is ok: this just means that another thread
1946 * already did the job.
1947 */
Chris Wilson52137012018-06-06 22:45:20 +01001948 return VM_FAULT_NOPAGE;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001949 case -ENOMEM:
Chris Wilson52137012018-06-06 22:45:20 +01001950 return VM_FAULT_OOM;
Daniel Vettera7c2e1a2012-10-17 11:17:16 +02001951 case -ENOSPC:
Chris Wilson45d67812014-01-31 11:34:57 +00001952 case -EFAULT:
Chris Wilson52137012018-06-06 22:45:20 +01001953 return VM_FAULT_SIGBUS;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001954 default:
Daniel Vettera7c2e1a2012-10-17 11:17:16 +02001955 WARN_ONCE(ret, "unhandled error in i915_gem_fault: %i\n", ret);
Chris Wilson52137012018-06-06 22:45:20 +01001956 return VM_FAULT_SIGBUS;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001957 }
1958}
1959
Chris Wilsona65adaf2017-10-09 09:43:57 +01001960static void __i915_gem_object_release_mmap(struct drm_i915_gem_object *obj)
1961{
1962 struct i915_vma *vma;
1963
1964 GEM_BUG_ON(!obj->userfault_count);
1965
1966 obj->userfault_count = 0;
1967 list_del(&obj->userfault_link);
1968 drm_vma_node_unmap(&obj->base.vma_node,
1969 obj->base.dev->anon_inode->i_mapping);
1970
Chris Wilsone2189dd2017-12-07 21:14:07 +00001971 for_each_ggtt_vma(vma, obj)
Chris Wilsona65adaf2017-10-09 09:43:57 +01001972 i915_vma_unset_userfault(vma);
Chris Wilsona65adaf2017-10-09 09:43:57 +01001973}
1974
Jesse Barnesde151cf2008-11-12 10:03:55 -08001975/**
Chris Wilson901782b2009-07-10 08:18:50 +01001976 * i915_gem_release_mmap - remove physical page mappings
1977 * @obj: obj in question
1978 *
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02001979 * Preserve the reservation of the mmapping with the DRM core code, but
Chris Wilson901782b2009-07-10 08:18:50 +01001980 * relinquish ownership of the pages back to the system.
1981 *
1982 * It is vital that we remove the page mapping if we have mapped a tiled
1983 * object through the GTT and then lose the fence register due to
1984 * resource pressure. Similarly if the object has been moved out of the
1985 * aperture, than pages mapped into userspace must be revoked. Removing the
1986 * mapping will then trigger a page fault on the next user access, allowing
1987 * fixup by i915_gem_fault().
1988 */
Eric Anholtd05ca302009-07-10 13:02:26 -07001989void
Chris Wilson05394f32010-11-08 19:18:58 +00001990i915_gem_release_mmap(struct drm_i915_gem_object *obj)
Chris Wilson901782b2009-07-10 08:18:50 +01001991{
Chris Wilson275f0392016-10-24 13:42:14 +01001992 struct drm_i915_private *i915 = to_i915(obj->base.dev);
Chris Wilson538ef962019-01-14 14:21:18 +00001993 intel_wakeref_t wakeref;
Chris Wilson275f0392016-10-24 13:42:14 +01001994
Chris Wilson349f2cc2016-04-13 17:35:12 +01001995 /* Serialisation between user GTT access and our code depends upon
1996 * revoking the CPU's PTE whilst the mutex is held. The next user
1997 * pagefault then has to wait until we release the mutex.
Chris Wilson9c870d02016-10-24 13:42:15 +01001998 *
1999 * Note that RPM complicates somewhat by adding an additional
2000 * requirement that operations to the GGTT be made holding the RPM
2001 * wakeref.
Chris Wilson349f2cc2016-04-13 17:35:12 +01002002 */
Chris Wilson275f0392016-10-24 13:42:14 +01002003 lockdep_assert_held(&i915->drm.struct_mutex);
Chris Wilson538ef962019-01-14 14:21:18 +00002004 wakeref = intel_runtime_pm_get(i915);
Chris Wilson349f2cc2016-04-13 17:35:12 +01002005
Chris Wilsona65adaf2017-10-09 09:43:57 +01002006 if (!obj->userfault_count)
Chris Wilson9c870d02016-10-24 13:42:15 +01002007 goto out;
Chris Wilson901782b2009-07-10 08:18:50 +01002008
Chris Wilsona65adaf2017-10-09 09:43:57 +01002009 __i915_gem_object_release_mmap(obj);
Chris Wilson349f2cc2016-04-13 17:35:12 +01002010
2011 /* Ensure that the CPU's PTE are revoked and there are not outstanding
2012 * memory transactions from userspace before we return. The TLB
2013 * flushing implied above by changing the PTE above *should* be
2014 * sufficient, an extra barrier here just provides us with a bit
2015 * of paranoid documentation about our requirement to serialise
2016 * memory writes before touching registers / GSM.
2017 */
2018 wmb();
Chris Wilson9c870d02016-10-24 13:42:15 +01002019
2020out:
Chris Wilson538ef962019-01-14 14:21:18 +00002021 intel_runtime_pm_put(i915, wakeref);
Chris Wilson901782b2009-07-10 08:18:50 +01002022}
2023
Chris Wilson7c108fd2016-10-24 13:42:18 +01002024void i915_gem_runtime_suspend(struct drm_i915_private *dev_priv)
Chris Wilsoneedd10f2014-06-16 08:57:44 +01002025{
Chris Wilson3594a3e2016-10-24 13:42:16 +01002026 struct drm_i915_gem_object *obj, *on;
Chris Wilson7c108fd2016-10-24 13:42:18 +01002027 int i;
Chris Wilsoneedd10f2014-06-16 08:57:44 +01002028
Chris Wilson3594a3e2016-10-24 13:42:16 +01002029 /*
2030 * Only called during RPM suspend. All users of the userfault_list
2031 * must be holding an RPM wakeref to ensure that this can not
2032 * run concurrently with themselves (and use the struct_mutex for
2033 * protection between themselves).
2034 */
2035
2036 list_for_each_entry_safe(obj, on,
Chris Wilsona65adaf2017-10-09 09:43:57 +01002037 &dev_priv->mm.userfault_list, userfault_link)
2038 __i915_gem_object_release_mmap(obj);
Chris Wilson7c108fd2016-10-24 13:42:18 +01002039
2040 /* The fence will be lost when the device powers down. If any were
2041 * in use by hardware (i.e. they are pinned), we should not be powering
2042 * down! All other fences will be reacquired by the user upon waking.
2043 */
2044 for (i = 0; i < dev_priv->num_fence_regs; i++) {
2045 struct drm_i915_fence_reg *reg = &dev_priv->fence_regs[i];
2046
Chris Wilsone0ec3ec2017-02-03 12:57:17 +00002047 /* Ideally we want to assert that the fence register is not
2048 * live at this point (i.e. that no piece of code will be
2049 * trying to write through fence + GTT, as that both violates
2050 * our tracking of activity and associated locking/barriers,
2051 * but also is illegal given that the hw is powered down).
2052 *
2053 * Previously we used reg->pin_count as a "liveness" indicator.
2054 * That is not sufficient, and we need a more fine-grained
2055 * tool if we want to have a sanity check here.
2056 */
Chris Wilson7c108fd2016-10-24 13:42:18 +01002057
2058 if (!reg->vma)
2059 continue;
2060
Chris Wilsona65adaf2017-10-09 09:43:57 +01002061 GEM_BUG_ON(i915_vma_has_userfault(reg->vma));
Chris Wilson7c108fd2016-10-24 13:42:18 +01002062 reg->dirty = true;
2063 }
Chris Wilsoneedd10f2014-06-16 08:57:44 +01002064}
2065
Chris Wilsond8cb5082012-08-11 15:41:03 +01002066static int i915_gem_object_create_mmap_offset(struct drm_i915_gem_object *obj)
2067{
Chris Wilsonfac5e232016-07-04 11:34:36 +01002068 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
Chris Wilsonf3f61842016-08-05 10:14:14 +01002069 int err;
Chris Wilsond8cb5082012-08-11 15:41:03 +01002070
Chris Wilsonf3f61842016-08-05 10:14:14 +01002071 err = drm_gem_create_mmap_offset(&obj->base);
Chris Wilsonb42a13d2017-01-06 15:22:40 +00002072 if (likely(!err))
Chris Wilsonf3f61842016-08-05 10:14:14 +01002073 return 0;
Daniel Vetterda494d72012-12-20 15:11:16 +01002074
Chris Wilsonb42a13d2017-01-06 15:22:40 +00002075 /* Attempt to reap some mmap space from dead objects */
2076 do {
Chris Wilsonec625fb2018-07-09 13:20:42 +01002077 err = i915_gem_wait_for_idle(dev_priv,
2078 I915_WAIT_INTERRUPTIBLE,
2079 MAX_SCHEDULE_TIMEOUT);
Chris Wilsonb42a13d2017-01-06 15:22:40 +00002080 if (err)
2081 break;
Chris Wilsond8cb5082012-08-11 15:41:03 +01002082
Chris Wilsonb42a13d2017-01-06 15:22:40 +00002083 i915_gem_drain_freed_objects(dev_priv);
Chris Wilsonf3f61842016-08-05 10:14:14 +01002084 err = drm_gem_create_mmap_offset(&obj->base);
Chris Wilsonb42a13d2017-01-06 15:22:40 +00002085 if (!err)
2086 break;
2087
2088 } while (flush_delayed_work(&dev_priv->gt.retire_work));
Daniel Vetterda494d72012-12-20 15:11:16 +01002089
Chris Wilsonf3f61842016-08-05 10:14:14 +01002090 return err;
Chris Wilsond8cb5082012-08-11 15:41:03 +01002091}
2092
2093static void i915_gem_object_free_mmap_offset(struct drm_i915_gem_object *obj)
2094{
Chris Wilsond8cb5082012-08-11 15:41:03 +01002095 drm_gem_free_mmap_offset(&obj->base);
2096}
2097
Dave Airlieda6b51d2014-12-24 13:11:17 +10002098int
Dave Airlieff72145b2011-02-07 12:16:14 +10002099i915_gem_mmap_gtt(struct drm_file *file,
2100 struct drm_device *dev,
Jani Nikula739f3ab2019-01-16 11:15:19 +02002101 u32 handle,
2102 u64 *offset)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002103{
Chris Wilson05394f32010-11-08 19:18:58 +00002104 struct drm_i915_gem_object *obj;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002105 int ret;
2106
Chris Wilson03ac0642016-07-20 13:31:51 +01002107 obj = i915_gem_object_lookup(file, handle);
Chris Wilsonf3f61842016-08-05 10:14:14 +01002108 if (!obj)
2109 return -ENOENT;
Chris Wilsonab182822009-09-22 18:46:17 +01002110
Chris Wilsond8cb5082012-08-11 15:41:03 +01002111 ret = i915_gem_object_create_mmap_offset(obj);
Chris Wilsonf3f61842016-08-05 10:14:14 +01002112 if (ret == 0)
2113 *offset = drm_vma_node_offset_addr(&obj->base.vma_node);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002114
Chris Wilsonf0cd5182016-10-28 13:58:43 +01002115 i915_gem_object_put(obj);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01002116 return ret;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002117}
2118
Dave Airlieff72145b2011-02-07 12:16:14 +10002119/**
2120 * i915_gem_mmap_gtt_ioctl - prepare an object for GTT mmap'ing
2121 * @dev: DRM device
2122 * @data: GTT mapping ioctl data
2123 * @file: GEM object info
2124 *
2125 * Simply returns the fake offset to userspace so it can mmap it.
2126 * The mmap call will end up in drm_gem_mmap(), which will set things
2127 * up so we can get faults in the handler above.
2128 *
2129 * The fault handler will take care of binding the object into the GTT
2130 * (since it may have been evicted to make room for something), allocating
2131 * a fence register, and mapping the appropriate aperture address into
2132 * userspace.
2133 */
2134int
2135i915_gem_mmap_gtt_ioctl(struct drm_device *dev, void *data,
2136 struct drm_file *file)
2137{
2138 struct drm_i915_gem_mmap_gtt *args = data;
2139
Dave Airlieda6b51d2014-12-24 13:11:17 +10002140 return i915_gem_mmap_gtt(file, dev, args->handle, &args->offset);
Dave Airlieff72145b2011-02-07 12:16:14 +10002141}
2142
Daniel Vetter225067e2012-08-20 10:23:20 +02002143/* Immediately discard the backing storage */
2144static void
2145i915_gem_object_truncate(struct drm_i915_gem_object *obj)
Chris Wilsone5281cc2010-10-28 13:45:36 +01002146{
Chris Wilson4d6294bf2012-08-11 15:41:05 +01002147 i915_gem_object_free_mmap_offset(obj);
Daniel Vetter1286ff72012-05-10 15:25:09 +02002148
Chris Wilson4d6294bf2012-08-11 15:41:05 +01002149 if (obj->base.filp == NULL)
2150 return;
2151
Daniel Vetter225067e2012-08-20 10:23:20 +02002152 /* Our goal here is to return as much of the memory as
2153 * is possible back to the system as we are called from OOM.
2154 * To do this we must instruct the shmfs to drop all of its
2155 * backing pages, *now*.
Chris Wilsone5281cc2010-10-28 13:45:36 +01002156 */
Chris Wilson55372522014-03-25 13:23:06 +00002157 shmem_truncate_range(file_inode(obj->base.filp), 0, (loff_t)-1);
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002158 obj->mm.madv = __I915_MADV_PURGED;
Chris Wilson4e5462e2017-03-07 13:20:31 +00002159 obj->mm.pages = ERR_PTR(-EFAULT);
Chris Wilsone5281cc2010-10-28 13:45:36 +01002160}
Chris Wilsone5281cc2010-10-28 13:45:36 +01002161
Chris Wilson55372522014-03-25 13:23:06 +00002162/* Try to discard unwanted pages */
Chris Wilson03ac84f2016-10-28 13:58:36 +01002163void __i915_gem_object_invalidate(struct drm_i915_gem_object *obj)
Daniel Vetter225067e2012-08-20 10:23:20 +02002164{
Chris Wilson55372522014-03-25 13:23:06 +00002165 struct address_space *mapping;
2166
Chris Wilson1233e2d2016-10-28 13:58:37 +01002167 lockdep_assert_held(&obj->mm.lock);
Chris Wilsonf1fa4f42017-10-13 21:26:13 +01002168 GEM_BUG_ON(i915_gem_object_has_pages(obj));
Chris Wilson1233e2d2016-10-28 13:58:37 +01002169
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002170 switch (obj->mm.madv) {
Chris Wilson55372522014-03-25 13:23:06 +00002171 case I915_MADV_DONTNEED:
2172 i915_gem_object_truncate(obj);
2173 case __I915_MADV_PURGED:
2174 return;
2175 }
2176
2177 if (obj->base.filp == NULL)
2178 return;
2179
Al Viro93c76a32015-12-04 23:45:44 -05002180 mapping = obj->base.filp->f_mapping,
Chris Wilson55372522014-03-25 13:23:06 +00002181 invalidate_mapping_pages(mapping, 0, (loff_t)-1);
Chris Wilsone5281cc2010-10-28 13:45:36 +01002182}
2183
Kuo-Hsin Yang64e3d122018-11-06 13:23:24 +00002184/*
2185 * Move pages to appropriate lru and release the pagevec, decrementing the
2186 * ref count of those pages.
2187 */
2188static void check_release_pagevec(struct pagevec *pvec)
2189{
2190 check_move_unevictable_pages(pvec);
2191 __pagevec_release(pvec);
2192 cond_resched();
2193}
2194
Chris Wilson5cdf5882010-09-27 15:51:07 +01002195static void
Chris Wilson03ac84f2016-10-28 13:58:36 +01002196i915_gem_object_put_pages_gtt(struct drm_i915_gem_object *obj,
2197 struct sg_table *pages)
Eric Anholt673a3942008-07-30 12:06:12 -07002198{
Dave Gordon85d12252016-05-20 11:54:06 +01002199 struct sgt_iter sgt_iter;
Kuo-Hsin Yang64e3d122018-11-06 13:23:24 +00002200 struct pagevec pvec;
Dave Gordon85d12252016-05-20 11:54:06 +01002201 struct page *page;
Daniel Vetter1286ff72012-05-10 15:25:09 +02002202
Chris Wilsone5facdf2016-12-23 14:57:57 +00002203 __i915_gem_object_release_shmem(obj, pages, true);
Eric Anholt856fa192009-03-19 14:10:50 -07002204
Chris Wilson03ac84f2016-10-28 13:58:36 +01002205 i915_gem_gtt_finish_pages(obj, pages);
Imre Deake2273302015-07-09 12:59:05 +03002206
Daniel Vetter6dacfd22011-09-12 21:30:02 +02002207 if (i915_gem_object_needs_bit17_swizzle(obj))
Chris Wilson03ac84f2016-10-28 13:58:36 +01002208 i915_gem_object_save_bit_17_swizzle(obj, pages);
Eric Anholt280b7132009-03-12 16:56:27 -07002209
Kuo-Hsin Yang64e3d122018-11-06 13:23:24 +00002210 mapping_clear_unevictable(file_inode(obj->base.filp)->i_mapping);
2211
2212 pagevec_init(&pvec);
Chris Wilson03ac84f2016-10-28 13:58:36 +01002213 for_each_sgt_page(page, sgt_iter, pages) {
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002214 if (obj->mm.dirty)
Chris Wilson9da3da62012-06-01 15:20:22 +01002215 set_page_dirty(page);
Chris Wilson3ef94da2009-09-14 16:50:29 +01002216
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002217 if (obj->mm.madv == I915_MADV_WILLNEED)
Chris Wilson9da3da62012-06-01 15:20:22 +01002218 mark_page_accessed(page);
Chris Wilson3ef94da2009-09-14 16:50:29 +01002219
Kuo-Hsin Yang64e3d122018-11-06 13:23:24 +00002220 if (!pagevec_add(&pvec, page))
2221 check_release_pagevec(&pvec);
Chris Wilson3ef94da2009-09-14 16:50:29 +01002222 }
Kuo-Hsin Yang64e3d122018-11-06 13:23:24 +00002223 if (pagevec_count(&pvec))
2224 check_release_pagevec(&pvec);
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002225 obj->mm.dirty = false;
Eric Anholt673a3942008-07-30 12:06:12 -07002226
Chris Wilson03ac84f2016-10-28 13:58:36 +01002227 sg_free_table(pages);
2228 kfree(pages);
Chris Wilson37e680a2012-06-07 15:38:42 +01002229}
2230
Chris Wilson96d77632016-10-28 13:58:33 +01002231static void __i915_gem_object_reset_page_iter(struct drm_i915_gem_object *obj)
2232{
2233 struct radix_tree_iter iter;
Ville Syrjäläc23aa712017-09-01 20:12:51 +03002234 void __rcu **slot;
Chris Wilson96d77632016-10-28 13:58:33 +01002235
Chris Wilsonbea6e982017-10-26 14:00:31 +01002236 rcu_read_lock();
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002237 radix_tree_for_each_slot(slot, &obj->mm.get_page.radix, &iter, 0)
2238 radix_tree_delete(&obj->mm.get_page.radix, iter.index);
Chris Wilsonbea6e982017-10-26 14:00:31 +01002239 rcu_read_unlock();
Chris Wilson96d77632016-10-28 13:58:33 +01002240}
2241
Chris Wilsonacd1c1e2018-06-11 08:55:32 +01002242static struct sg_table *
2243__i915_gem_object_unset_pages(struct drm_i915_gem_object *obj)
Chris Wilson37e680a2012-06-07 15:38:42 +01002244{
Chris Wilsonf2123812017-10-16 12:40:37 +01002245 struct drm_i915_private *i915 = to_i915(obj->base.dev);
Chris Wilson03ac84f2016-10-28 13:58:36 +01002246 struct sg_table *pages;
Chris Wilson37e680a2012-06-07 15:38:42 +01002247
Chris Wilson03ac84f2016-10-28 13:58:36 +01002248 pages = fetch_and_zero(&obj->mm.pages);
Chris Wilson484d9a82019-01-15 12:44:42 +00002249 if (IS_ERR_OR_NULL(pages))
2250 return pages;
Chris Wilsona2165e32012-12-03 11:49:00 +00002251
Chris Wilsonf2123812017-10-16 12:40:37 +01002252 spin_lock(&i915->mm.obj_lock);
2253 list_del(&obj->mm.link);
2254 spin_unlock(&i915->mm.obj_lock);
2255
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002256 if (obj->mm.mapping) {
Chris Wilson4b30cb22016-08-18 17:16:42 +01002257 void *ptr;
2258
Chris Wilson0ce81782017-05-17 13:09:59 +01002259 ptr = page_mask_bits(obj->mm.mapping);
Chris Wilson4b30cb22016-08-18 17:16:42 +01002260 if (is_vmalloc_addr(ptr))
2261 vunmap(ptr);
Chris Wilsonfb8621d2016-04-08 12:11:14 +01002262 else
Chris Wilson4b30cb22016-08-18 17:16:42 +01002263 kunmap(kmap_to_page(ptr));
2264
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002265 obj->mm.mapping = NULL;
Chris Wilson0a798eb2016-04-08 12:11:11 +01002266 }
2267
Chris Wilson96d77632016-10-28 13:58:33 +01002268 __i915_gem_object_reset_page_iter(obj);
Chris Wilsonacd1c1e2018-06-11 08:55:32 +01002269 obj->mm.page_sizes.phys = obj->mm.page_sizes.sg = 0;
Chris Wilson96d77632016-10-28 13:58:33 +01002270
Chris Wilsonacd1c1e2018-06-11 08:55:32 +01002271 return pages;
2272}
2273
Chris Wilson484d9a82019-01-15 12:44:42 +00002274int __i915_gem_object_put_pages(struct drm_i915_gem_object *obj,
2275 enum i915_mm_subclass subclass)
Chris Wilsonacd1c1e2018-06-11 08:55:32 +01002276{
2277 struct sg_table *pages;
Chris Wilson484d9a82019-01-15 12:44:42 +00002278 int ret;
Chris Wilsonacd1c1e2018-06-11 08:55:32 +01002279
2280 if (i915_gem_object_has_pinned_pages(obj))
Chris Wilson484d9a82019-01-15 12:44:42 +00002281 return -EBUSY;
Chris Wilsonacd1c1e2018-06-11 08:55:32 +01002282
2283 GEM_BUG_ON(obj->bind_count);
Chris Wilsonacd1c1e2018-06-11 08:55:32 +01002284
2285 /* May be called by shrinker from within get_pages() (on another bo) */
2286 mutex_lock_nested(&obj->mm.lock, subclass);
Chris Wilson484d9a82019-01-15 12:44:42 +00002287 if (unlikely(atomic_read(&obj->mm.pages_pin_count))) {
2288 ret = -EBUSY;
Chris Wilsonacd1c1e2018-06-11 08:55:32 +01002289 goto unlock;
Chris Wilson484d9a82019-01-15 12:44:42 +00002290 }
Chris Wilsonacd1c1e2018-06-11 08:55:32 +01002291
2292 /*
2293 * ->put_pages might need to allocate memory for the bit17 swizzle
2294 * array, hence protect them from being reaped by removing them from gtt
2295 * lists early.
2296 */
2297 pages = __i915_gem_object_unset_pages(obj);
Chris Wilson484d9a82019-01-15 12:44:42 +00002298
2299 /*
2300 * XXX Temporary hijinx to avoid updating all backends to handle
2301 * NULL pages. In the future, when we have more asynchronous
2302 * get_pages backends we should be better able to handle the
2303 * cancellation of the async task in a more uniform manner.
2304 */
2305 if (!pages && !i915_gem_object_needs_async_cancel(obj))
2306 pages = ERR_PTR(-EINVAL);
2307
Chris Wilson4e5462e2017-03-07 13:20:31 +00002308 if (!IS_ERR(pages))
2309 obj->ops->put_pages(obj, pages);
2310
Chris Wilson484d9a82019-01-15 12:44:42 +00002311 ret = 0;
Chris Wilson1233e2d2016-10-28 13:58:37 +01002312unlock:
2313 mutex_unlock(&obj->mm.lock);
Chris Wilson484d9a82019-01-15 12:44:42 +00002314
2315 return ret;
Chris Wilson6c085a72012-08-20 11:40:46 +02002316}
2317
Tvrtko Ursulinf8e57862018-09-26 09:03:53 +01002318bool i915_sg_trim(struct sg_table *orig_st)
Tvrtko Ursulin0c40ce12016-11-09 15:13:43 +00002319{
2320 struct sg_table new_st;
2321 struct scatterlist *sg, *new_sg;
2322 unsigned int i;
2323
2324 if (orig_st->nents == orig_st->orig_nents)
Chris Wilson935a2f72017-02-13 17:15:13 +00002325 return false;
Tvrtko Ursulin0c40ce12016-11-09 15:13:43 +00002326
Chris Wilson8bfc478f2016-12-23 14:57:58 +00002327 if (sg_alloc_table(&new_st, orig_st->nents, GFP_KERNEL | __GFP_NOWARN))
Chris Wilson935a2f72017-02-13 17:15:13 +00002328 return false;
Tvrtko Ursulin0c40ce12016-11-09 15:13:43 +00002329
2330 new_sg = new_st.sgl;
2331 for_each_sg(orig_st->sgl, sg, orig_st->nents, i) {
2332 sg_set_page(new_sg, sg_page(sg), sg->length, 0);
Matthew Auldc6d22ab2018-09-20 15:27:06 +01002333 sg_dma_address(new_sg) = sg_dma_address(sg);
2334 sg_dma_len(new_sg) = sg_dma_len(sg);
2335
Tvrtko Ursulin0c40ce12016-11-09 15:13:43 +00002336 new_sg = sg_next(new_sg);
2337 }
Chris Wilsonc2dc6cc2016-12-19 12:43:46 +00002338 GEM_BUG_ON(new_sg); /* Should walk exactly nents and hit the end */
Tvrtko Ursulin0c40ce12016-11-09 15:13:43 +00002339
2340 sg_free_table(orig_st);
2341
2342 *orig_st = new_st;
Chris Wilson935a2f72017-02-13 17:15:13 +00002343 return true;
Tvrtko Ursulin0c40ce12016-11-09 15:13:43 +00002344}
2345
Matthew Auldb91b09e2017-10-06 23:18:17 +01002346static int i915_gem_object_get_pages_gtt(struct drm_i915_gem_object *obj)
Eric Anholt673a3942008-07-30 12:06:12 -07002347{
Chris Wilsonfac5e232016-07-04 11:34:36 +01002348 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
Chris Wilsond766ef52016-12-19 12:43:45 +00002349 const unsigned long page_count = obj->base.size / PAGE_SIZE;
2350 unsigned long i;
Eric Anholt673a3942008-07-30 12:06:12 -07002351 struct address_space *mapping;
Chris Wilson9da3da62012-06-01 15:20:22 +01002352 struct sg_table *st;
2353 struct scatterlist *sg;
Dave Gordon85d12252016-05-20 11:54:06 +01002354 struct sgt_iter sgt_iter;
Eric Anholt673a3942008-07-30 12:06:12 -07002355 struct page *page;
Imre Deak90797e62013-02-18 19:28:03 +02002356 unsigned long last_pfn = 0; /* suppress gcc warning */
Tvrtko Ursulin56024522017-08-03 10:14:17 +01002357 unsigned int max_segment = i915_sg_segment_size();
Matthew Auld84e89782017-10-09 12:00:24 +01002358 unsigned int sg_page_sizes;
Kuo-Hsin Yang64e3d122018-11-06 13:23:24 +00002359 struct pagevec pvec;
Chris Wilson4846bf02017-06-09 12:03:46 +01002360 gfp_t noreclaim;
Imre Deake2273302015-07-09 12:59:05 +03002361 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07002362
Chris Wilsone0ff7a72018-09-03 09:33:36 +01002363 /*
2364 * Assert that the object is not currently in any GPU domain. As it
Chris Wilson6c085a72012-08-20 11:40:46 +02002365 * wasn't in the GTT, there shouldn't be any way it could have been in
2366 * a GPU cache
2367 */
Christian Königc0a51fd2018-02-16 13:43:38 +01002368 GEM_BUG_ON(obj->read_domains & I915_GEM_GPU_DOMAINS);
2369 GEM_BUG_ON(obj->write_domain & I915_GEM_GPU_DOMAINS);
Chris Wilson6c085a72012-08-20 11:40:46 +02002370
Chris Wilsone0ff7a72018-09-03 09:33:36 +01002371 /*
2372 * If there's no chance of allocating enough pages for the whole
2373 * object, bail early.
2374 */
Arun KSca79b0c2018-12-28 00:34:29 -08002375 if (page_count > totalram_pages())
Chris Wilsone0ff7a72018-09-03 09:33:36 +01002376 return -ENOMEM;
2377
Chris Wilson9da3da62012-06-01 15:20:22 +01002378 st = kmalloc(sizeof(*st), GFP_KERNEL);
2379 if (st == NULL)
Matthew Auldb91b09e2017-10-06 23:18:17 +01002380 return -ENOMEM;
Eric Anholt673a3942008-07-30 12:06:12 -07002381
Chris Wilsond766ef52016-12-19 12:43:45 +00002382rebuild_st:
Chris Wilson9da3da62012-06-01 15:20:22 +01002383 if (sg_alloc_table(st, page_count, GFP_KERNEL)) {
Chris Wilson9da3da62012-06-01 15:20:22 +01002384 kfree(st);
Matthew Auldb91b09e2017-10-06 23:18:17 +01002385 return -ENOMEM;
Chris Wilson9da3da62012-06-01 15:20:22 +01002386 }
2387
Chris Wilsone0ff7a72018-09-03 09:33:36 +01002388 /*
2389 * Get the list of pages out of our struct file. They'll be pinned
Chris Wilson9da3da62012-06-01 15:20:22 +01002390 * at this point until we release them.
2391 *
2392 * Fail silently without starting the shrinker
2393 */
Al Viro93c76a32015-12-04 23:45:44 -05002394 mapping = obj->base.filp->f_mapping;
Kuo-Hsin Yang64e3d122018-11-06 13:23:24 +00002395 mapping_set_unevictable(mapping);
Chris Wilson0f6ab552017-06-09 12:03:48 +01002396 noreclaim = mapping_gfp_constraint(mapping, ~__GFP_RECLAIM);
Chris Wilson4846bf02017-06-09 12:03:46 +01002397 noreclaim |= __GFP_NORETRY | __GFP_NOWARN;
2398
Imre Deak90797e62013-02-18 19:28:03 +02002399 sg = st->sgl;
2400 st->nents = 0;
Matthew Auld84e89782017-10-09 12:00:24 +01002401 sg_page_sizes = 0;
Imre Deak90797e62013-02-18 19:28:03 +02002402 for (i = 0; i < page_count; i++) {
Chris Wilson4846bf02017-06-09 12:03:46 +01002403 const unsigned int shrink[] = {
2404 I915_SHRINK_BOUND | I915_SHRINK_UNBOUND | I915_SHRINK_PURGEABLE,
2405 0,
2406 }, *s = shrink;
2407 gfp_t gfp = noreclaim;
2408
2409 do {
Chris Wilsone6db7f42018-11-05 17:06:40 +00002410 cond_resched();
Chris Wilson6c085a72012-08-20 11:40:46 +02002411 page = shmem_read_mapping_page_gfp(mapping, i, gfp);
Chengguang Xu772b5402019-02-21 10:08:19 +08002412 if (!IS_ERR(page))
Chris Wilson4846bf02017-06-09 12:03:46 +01002413 break;
2414
2415 if (!*s) {
2416 ret = PTR_ERR(page);
2417 goto err_sg;
2418 }
2419
Chris Wilson912d5722017-09-06 16:19:30 -07002420 i915_gem_shrink(dev_priv, 2 * page_count, NULL, *s++);
Chris Wilson24f8e002017-03-22 11:05:21 +00002421
Chris Wilsone0ff7a72018-09-03 09:33:36 +01002422 /*
2423 * We've tried hard to allocate the memory by reaping
Chris Wilson6c085a72012-08-20 11:40:46 +02002424 * our own buffer, now let the real VM do its job and
2425 * go down in flames if truly OOM.
Chris Wilson24f8e002017-03-22 11:05:21 +00002426 *
2427 * However, since graphics tend to be disposable,
2428 * defer the oom here by reporting the ENOMEM back
2429 * to userspace.
Chris Wilson6c085a72012-08-20 11:40:46 +02002430 */
Chris Wilson4846bf02017-06-09 12:03:46 +01002431 if (!*s) {
2432 /* reclaim and warn, but no oom */
2433 gfp = mapping_gfp_mask(mapping);
Chris Wilsoneaf41802017-06-09 12:03:47 +01002434
Chris Wilsone0ff7a72018-09-03 09:33:36 +01002435 /*
2436 * Our bo are always dirty and so we require
Chris Wilsoneaf41802017-06-09 12:03:47 +01002437 * kswapd to reclaim our pages (direct reclaim
2438 * does not effectively begin pageout of our
2439 * buffers on its own). However, direct reclaim
2440 * only waits for kswapd when under allocation
2441 * congestion. So as a result __GFP_RECLAIM is
2442 * unreliable and fails to actually reclaim our
2443 * dirty pages -- unless you try over and over
2444 * again with !__GFP_NORETRY. However, we still
2445 * want to fail this allocation rather than
2446 * trigger the out-of-memory killer and for
Michal Hockodbb32952017-07-12 14:36:55 -07002447 * this we want __GFP_RETRY_MAYFAIL.
Chris Wilsoneaf41802017-06-09 12:03:47 +01002448 */
Michal Hockodbb32952017-07-12 14:36:55 -07002449 gfp |= __GFP_RETRY_MAYFAIL;
Imre Deake2273302015-07-09 12:59:05 +03002450 }
Chris Wilson4846bf02017-06-09 12:03:46 +01002451 } while (1);
2452
Chris Wilson871dfbd2016-10-11 09:20:21 +01002453 if (!i ||
2454 sg->length >= max_segment ||
2455 page_to_pfn(page) != last_pfn + 1) {
Matthew Aulda5c081662017-10-06 23:18:18 +01002456 if (i) {
Matthew Auld84e89782017-10-09 12:00:24 +01002457 sg_page_sizes |= sg->length;
Imre Deak90797e62013-02-18 19:28:03 +02002458 sg = sg_next(sg);
Matthew Aulda5c081662017-10-06 23:18:18 +01002459 }
Imre Deak90797e62013-02-18 19:28:03 +02002460 st->nents++;
2461 sg_set_page(sg, page, PAGE_SIZE, 0);
2462 } else {
2463 sg->length += PAGE_SIZE;
2464 }
2465 last_pfn = page_to_pfn(page);
Daniel Vetter3bbbe702013-10-07 17:15:45 -03002466
2467 /* Check that the i965g/gm workaround works. */
2468 WARN_ON((gfp & __GFP_DMA32) && (last_pfn >= 0x00100000UL));
Eric Anholt673a3942008-07-30 12:06:12 -07002469 }
Matthew Aulda5c081662017-10-06 23:18:18 +01002470 if (sg) { /* loop terminated early; short sg table */
Matthew Auld84e89782017-10-09 12:00:24 +01002471 sg_page_sizes |= sg->length;
Konrad Rzeszutek Wilk426729d2013-06-24 11:47:48 -04002472 sg_mark_end(sg);
Matthew Aulda5c081662017-10-06 23:18:18 +01002473 }
Chris Wilson74ce6b62012-10-19 15:51:06 +01002474
Tvrtko Ursulin0c40ce12016-11-09 15:13:43 +00002475 /* Trim unused sg entries to avoid wasting memory. */
2476 i915_sg_trim(st);
2477
Chris Wilson03ac84f2016-10-28 13:58:36 +01002478 ret = i915_gem_gtt_prepare_pages(obj, st);
Chris Wilsond766ef52016-12-19 12:43:45 +00002479 if (ret) {
Chris Wilsone0ff7a72018-09-03 09:33:36 +01002480 /*
2481 * DMA remapping failed? One possible cause is that
Chris Wilsond766ef52016-12-19 12:43:45 +00002482 * it could not reserve enough large entries, asking
2483 * for PAGE_SIZE chunks instead may be helpful.
2484 */
2485 if (max_segment > PAGE_SIZE) {
2486 for_each_sgt_page(page, sgt_iter, st)
2487 put_page(page);
2488 sg_free_table(st);
2489
2490 max_segment = PAGE_SIZE;
2491 goto rebuild_st;
2492 } else {
2493 dev_warn(&dev_priv->drm.pdev->dev,
2494 "Failed to DMA remap %lu pages\n",
2495 page_count);
2496 goto err_pages;
2497 }
2498 }
Imre Deake2273302015-07-09 12:59:05 +03002499
Eric Anholt673a3942008-07-30 12:06:12 -07002500 if (i915_gem_object_needs_bit17_swizzle(obj))
Chris Wilson03ac84f2016-10-28 13:58:36 +01002501 i915_gem_object_do_bit_17_swizzle(obj, st);
Eric Anholt673a3942008-07-30 12:06:12 -07002502
Matthew Auld84e89782017-10-09 12:00:24 +01002503 __i915_gem_object_set_pages(obj, st, sg_page_sizes);
Matthew Auldb91b09e2017-10-06 23:18:17 +01002504
2505 return 0;
Eric Anholt673a3942008-07-30 12:06:12 -07002506
Chris Wilsonb17993b2016-11-14 11:29:30 +00002507err_sg:
Imre Deak90797e62013-02-18 19:28:03 +02002508 sg_mark_end(sg);
Chris Wilsonb17993b2016-11-14 11:29:30 +00002509err_pages:
Kuo-Hsin Yang64e3d122018-11-06 13:23:24 +00002510 mapping_clear_unevictable(mapping);
2511 pagevec_init(&pvec);
2512 for_each_sgt_page(page, sgt_iter, st) {
2513 if (!pagevec_add(&pvec, page))
2514 check_release_pagevec(&pvec);
2515 }
2516 if (pagevec_count(&pvec))
2517 check_release_pagevec(&pvec);
Chris Wilson9da3da62012-06-01 15:20:22 +01002518 sg_free_table(st);
2519 kfree(st);
Chris Wilson0820baf2014-03-25 13:23:03 +00002520
Chris Wilsone0ff7a72018-09-03 09:33:36 +01002521 /*
2522 * shmemfs first checks if there is enough memory to allocate the page
Chris Wilson0820baf2014-03-25 13:23:03 +00002523 * and reports ENOSPC should there be insufficient, along with the usual
2524 * ENOMEM for a genuine allocation failure.
2525 *
2526 * We use ENOSPC in our driver to mean that we have run out of aperture
2527 * space and so want to translate the error from shmemfs back to our
2528 * usual understanding of ENOMEM.
2529 */
Imre Deake2273302015-07-09 12:59:05 +03002530 if (ret == -ENOSPC)
2531 ret = -ENOMEM;
2532
Matthew Auldb91b09e2017-10-06 23:18:17 +01002533 return ret;
Chris Wilson03ac84f2016-10-28 13:58:36 +01002534}
2535
2536void __i915_gem_object_set_pages(struct drm_i915_gem_object *obj,
Matthew Aulda5c081662017-10-06 23:18:18 +01002537 struct sg_table *pages,
Matthew Auld84e89782017-10-09 12:00:24 +01002538 unsigned int sg_page_sizes)
Chris Wilson03ac84f2016-10-28 13:58:36 +01002539{
Matthew Aulda5c081662017-10-06 23:18:18 +01002540 struct drm_i915_private *i915 = to_i915(obj->base.dev);
2541 unsigned long supported = INTEL_INFO(i915)->page_sizes;
2542 int i;
2543
Chris Wilson1233e2d2016-10-28 13:58:37 +01002544 lockdep_assert_held(&obj->mm.lock);
Chris Wilson03ac84f2016-10-28 13:58:36 +01002545
2546 obj->mm.get_page.sg_pos = pages->sgl;
2547 obj->mm.get_page.sg_idx = 0;
2548
2549 obj->mm.pages = pages;
Chris Wilson2c3a3f42016-11-04 10:30:01 +00002550
2551 if (i915_gem_object_is_tiled(obj) &&
Chris Wilsonf2123812017-10-16 12:40:37 +01002552 i915->quirks & QUIRK_PIN_SWIZZLED_PAGES) {
Chris Wilson2c3a3f42016-11-04 10:30:01 +00002553 GEM_BUG_ON(obj->mm.quirked);
2554 __i915_gem_object_pin_pages(obj);
2555 obj->mm.quirked = true;
2556 }
Matthew Aulda5c081662017-10-06 23:18:18 +01002557
Matthew Auld84e89782017-10-09 12:00:24 +01002558 GEM_BUG_ON(!sg_page_sizes);
2559 obj->mm.page_sizes.phys = sg_page_sizes;
Matthew Aulda5c081662017-10-06 23:18:18 +01002560
2561 /*
Matthew Auld84e89782017-10-09 12:00:24 +01002562 * Calculate the supported page-sizes which fit into the given
2563 * sg_page_sizes. This will give us the page-sizes which we may be able
2564 * to use opportunistically when later inserting into the GTT. For
2565 * example if phys=2G, then in theory we should be able to use 1G, 2M,
2566 * 64K or 4K pages, although in practice this will depend on a number of
2567 * other factors.
Matthew Aulda5c081662017-10-06 23:18:18 +01002568 */
2569 obj->mm.page_sizes.sg = 0;
2570 for_each_set_bit(i, &supported, ilog2(I915_GTT_MAX_PAGE_SIZE) + 1) {
2571 if (obj->mm.page_sizes.phys & ~0u << i)
2572 obj->mm.page_sizes.sg |= BIT(i);
2573 }
Matthew Aulda5c081662017-10-06 23:18:18 +01002574 GEM_BUG_ON(!HAS_PAGE_SIZES(i915, obj->mm.page_sizes.sg));
Chris Wilsonf2123812017-10-16 12:40:37 +01002575
2576 spin_lock(&i915->mm.obj_lock);
2577 list_add(&obj->mm.link, &i915->mm.unbound_list);
2578 spin_unlock(&i915->mm.obj_lock);
Chris Wilson03ac84f2016-10-28 13:58:36 +01002579}
2580
2581static int ____i915_gem_object_get_pages(struct drm_i915_gem_object *obj)
2582{
Matthew Auldb91b09e2017-10-06 23:18:17 +01002583 int err;
Chris Wilson03ac84f2016-10-28 13:58:36 +01002584
2585 if (unlikely(obj->mm.madv != I915_MADV_WILLNEED)) {
2586 DRM_DEBUG("Attempting to obtain a purgeable object\n");
2587 return -EFAULT;
2588 }
2589
Matthew Auldb91b09e2017-10-06 23:18:17 +01002590 err = obj->ops->get_pages(obj);
Matthew Auldb65a9b92017-12-18 10:38:55 +00002591 GEM_BUG_ON(!err && !i915_gem_object_has_pages(obj));
Chris Wilson03ac84f2016-10-28 13:58:36 +01002592
Matthew Auldb91b09e2017-10-06 23:18:17 +01002593 return err;
Eric Anholt673a3942008-07-30 12:06:12 -07002594}
2595
Chris Wilson37e680a2012-06-07 15:38:42 +01002596/* Ensure that the associated pages are gathered from the backing storage
Chris Wilson1233e2d2016-10-28 13:58:37 +01002597 * and pinned into our object. i915_gem_object_pin_pages() may be called
Chris Wilson37e680a2012-06-07 15:38:42 +01002598 * multiple times before they are released by a single call to
Chris Wilson1233e2d2016-10-28 13:58:37 +01002599 * i915_gem_object_unpin_pages() - once the pages are no longer referenced
Chris Wilson37e680a2012-06-07 15:38:42 +01002600 * either as a result of memory pressure (reaping pages under the shrinker)
2601 * or as the object is itself released.
2602 */
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002603int __i915_gem_object_get_pages(struct drm_i915_gem_object *obj)
Chris Wilson37e680a2012-06-07 15:38:42 +01002604{
Chris Wilson03ac84f2016-10-28 13:58:36 +01002605 int err;
Chris Wilson37e680a2012-06-07 15:38:42 +01002606
Chris Wilson1233e2d2016-10-28 13:58:37 +01002607 err = mutex_lock_interruptible(&obj->mm.lock);
2608 if (err)
2609 return err;
Chris Wilson4c7d62c2016-10-28 13:58:32 +01002610
Chris Wilsonf1fa4f42017-10-13 21:26:13 +01002611 if (unlikely(!i915_gem_object_has_pages(obj))) {
Chris Wilson88c880b2017-09-06 14:52:20 +01002612 GEM_BUG_ON(i915_gem_object_has_pinned_pages(obj));
2613
Chris Wilson2c3a3f42016-11-04 10:30:01 +00002614 err = ____i915_gem_object_get_pages(obj);
2615 if (err)
2616 goto unlock;
2617
2618 smp_mb__before_atomic();
Chris Wilson1233e2d2016-10-28 13:58:37 +01002619 }
Chris Wilson2c3a3f42016-11-04 10:30:01 +00002620 atomic_inc(&obj->mm.pages_pin_count);
Chris Wilson43e28f02013-01-08 10:53:09 +00002621
Chris Wilson1233e2d2016-10-28 13:58:37 +01002622unlock:
2623 mutex_unlock(&obj->mm.lock);
Chris Wilson03ac84f2016-10-28 13:58:36 +01002624 return err;
Eric Anholt673a3942008-07-30 12:06:12 -07002625}
2626
Dave Gordondd6034c2016-05-20 11:54:04 +01002627/* The 'mapping' part of i915_gem_object_pin_map() below */
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002628static void *i915_gem_object_map(const struct drm_i915_gem_object *obj,
2629 enum i915_map_type type)
Dave Gordondd6034c2016-05-20 11:54:04 +01002630{
2631 unsigned long n_pages = obj->base.size >> PAGE_SHIFT;
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002632 struct sg_table *sgt = obj->mm.pages;
Dave Gordon85d12252016-05-20 11:54:06 +01002633 struct sgt_iter sgt_iter;
2634 struct page *page;
Dave Gordonb338fa42016-05-20 11:54:05 +01002635 struct page *stack_pages[32];
2636 struct page **pages = stack_pages;
Dave Gordondd6034c2016-05-20 11:54:04 +01002637 unsigned long i = 0;
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002638 pgprot_t pgprot;
Dave Gordondd6034c2016-05-20 11:54:04 +01002639 void *addr;
2640
2641 /* A single page can always be kmapped */
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002642 if (n_pages == 1 && type == I915_MAP_WB)
Dave Gordondd6034c2016-05-20 11:54:04 +01002643 return kmap(sg_page(sgt->sgl));
2644
Dave Gordonb338fa42016-05-20 11:54:05 +01002645 if (n_pages > ARRAY_SIZE(stack_pages)) {
2646 /* Too big for stack -- allocate temporary array instead */
Michal Hocko0ee931c2017-09-13 16:28:29 -07002647 pages = kvmalloc_array(n_pages, sizeof(*pages), GFP_KERNEL);
Dave Gordonb338fa42016-05-20 11:54:05 +01002648 if (!pages)
2649 return NULL;
2650 }
Dave Gordondd6034c2016-05-20 11:54:04 +01002651
Dave Gordon85d12252016-05-20 11:54:06 +01002652 for_each_sgt_page(page, sgt_iter, sgt)
2653 pages[i++] = page;
Dave Gordondd6034c2016-05-20 11:54:04 +01002654
2655 /* Check that we have the expected number of pages */
2656 GEM_BUG_ON(i != n_pages);
2657
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002658 switch (type) {
Chris Wilsona575c672017-08-28 11:46:31 +01002659 default:
2660 MISSING_CASE(type);
2661 /* fallthrough to use PAGE_KERNEL anyway */
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002662 case I915_MAP_WB:
2663 pgprot = PAGE_KERNEL;
2664 break;
2665 case I915_MAP_WC:
2666 pgprot = pgprot_writecombine(PAGE_KERNEL_IO);
2667 break;
2668 }
2669 addr = vmap(pages, n_pages, 0, pgprot);
Dave Gordondd6034c2016-05-20 11:54:04 +01002670
Dave Gordonb338fa42016-05-20 11:54:05 +01002671 if (pages != stack_pages)
Michal Hocko20981052017-05-17 14:23:12 +02002672 kvfree(pages);
Dave Gordondd6034c2016-05-20 11:54:04 +01002673
2674 return addr;
2675}
2676
2677/* get, pin, and map the pages of the object into kernel space */
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002678void *i915_gem_object_pin_map(struct drm_i915_gem_object *obj,
2679 enum i915_map_type type)
Chris Wilson0a798eb2016-04-08 12:11:11 +01002680{
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002681 enum i915_map_type has_type;
2682 bool pinned;
2683 void *ptr;
Chris Wilson0a798eb2016-04-08 12:11:11 +01002684 int ret;
2685
Tina Zhanga03f3952017-11-14 10:25:13 +00002686 if (unlikely(!i915_gem_object_has_struct_page(obj)))
2687 return ERR_PTR(-ENXIO);
Chris Wilson0a798eb2016-04-08 12:11:11 +01002688
Chris Wilson1233e2d2016-10-28 13:58:37 +01002689 ret = mutex_lock_interruptible(&obj->mm.lock);
Chris Wilson0a798eb2016-04-08 12:11:11 +01002690 if (ret)
2691 return ERR_PTR(ret);
2692
Chris Wilsona575c672017-08-28 11:46:31 +01002693 pinned = !(type & I915_MAP_OVERRIDE);
2694 type &= ~I915_MAP_OVERRIDE;
2695
Chris Wilson1233e2d2016-10-28 13:58:37 +01002696 if (!atomic_inc_not_zero(&obj->mm.pages_pin_count)) {
Chris Wilsonf1fa4f42017-10-13 21:26:13 +01002697 if (unlikely(!i915_gem_object_has_pages(obj))) {
Chris Wilson88c880b2017-09-06 14:52:20 +01002698 GEM_BUG_ON(i915_gem_object_has_pinned_pages(obj));
2699
Chris Wilson2c3a3f42016-11-04 10:30:01 +00002700 ret = ____i915_gem_object_get_pages(obj);
2701 if (ret)
2702 goto err_unlock;
Chris Wilson1233e2d2016-10-28 13:58:37 +01002703
Chris Wilson2c3a3f42016-11-04 10:30:01 +00002704 smp_mb__before_atomic();
2705 }
2706 atomic_inc(&obj->mm.pages_pin_count);
Chris Wilson1233e2d2016-10-28 13:58:37 +01002707 pinned = false;
2708 }
Chris Wilsonf1fa4f42017-10-13 21:26:13 +01002709 GEM_BUG_ON(!i915_gem_object_has_pages(obj));
Chris Wilson0a798eb2016-04-08 12:11:11 +01002710
Chris Wilson0ce81782017-05-17 13:09:59 +01002711 ptr = page_unpack_bits(obj->mm.mapping, &has_type);
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002712 if (ptr && has_type != type) {
2713 if (pinned) {
2714 ret = -EBUSY;
Chris Wilson1233e2d2016-10-28 13:58:37 +01002715 goto err_unpin;
Chris Wilson0a798eb2016-04-08 12:11:11 +01002716 }
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002717
2718 if (is_vmalloc_addr(ptr))
2719 vunmap(ptr);
2720 else
2721 kunmap(kmap_to_page(ptr));
2722
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002723 ptr = obj->mm.mapping = NULL;
Chris Wilson0a798eb2016-04-08 12:11:11 +01002724 }
2725
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002726 if (!ptr) {
2727 ptr = i915_gem_object_map(obj, type);
2728 if (!ptr) {
2729 ret = -ENOMEM;
Chris Wilson1233e2d2016-10-28 13:58:37 +01002730 goto err_unpin;
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002731 }
2732
Chris Wilson0ce81782017-05-17 13:09:59 +01002733 obj->mm.mapping = page_pack_bits(ptr, type);
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002734 }
2735
Chris Wilson1233e2d2016-10-28 13:58:37 +01002736out_unlock:
2737 mutex_unlock(&obj->mm.lock);
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002738 return ptr;
2739
Chris Wilson1233e2d2016-10-28 13:58:37 +01002740err_unpin:
2741 atomic_dec(&obj->mm.pages_pin_count);
2742err_unlock:
2743 ptr = ERR_PTR(ret);
2744 goto out_unlock;
Chris Wilson0a798eb2016-04-08 12:11:11 +01002745}
2746
Chris Wilson7c55e2c2017-03-07 12:03:38 +00002747static int
2748i915_gem_object_pwrite_gtt(struct drm_i915_gem_object *obj,
2749 const struct drm_i915_gem_pwrite *arg)
2750{
2751 struct address_space *mapping = obj->base.filp->f_mapping;
2752 char __user *user_data = u64_to_user_ptr(arg->data_ptr);
2753 u64 remain, offset;
2754 unsigned int pg;
2755
2756 /* Before we instantiate/pin the backing store for our use, we
2757 * can prepopulate the shmemfs filp efficiently using a write into
2758 * the pagecache. We avoid the penalty of instantiating all the
2759 * pages, important if the user is just writing to a few and never
2760 * uses the object on the GPU, and using a direct write into shmemfs
2761 * allows it to avoid the cost of retrieving a page (either swapin
2762 * or clearing-before-use) before it is overwritten.
2763 */
Chris Wilsonf1fa4f42017-10-13 21:26:13 +01002764 if (i915_gem_object_has_pages(obj))
Chris Wilson7c55e2c2017-03-07 12:03:38 +00002765 return -ENODEV;
2766
Chris Wilsona6d65e42017-10-16 21:27:32 +01002767 if (obj->mm.madv != I915_MADV_WILLNEED)
2768 return -EFAULT;
2769
Chris Wilson7c55e2c2017-03-07 12:03:38 +00002770 /* Before the pages are instantiated the object is treated as being
2771 * in the CPU domain. The pages will be clflushed as required before
2772 * use, and we can freely write into the pages directly. If userspace
2773 * races pwrite with any other operation; corruption will ensue -
2774 * that is userspace's prerogative!
2775 */
2776
2777 remain = arg->size;
2778 offset = arg->offset;
2779 pg = offset_in_page(offset);
2780
2781 do {
2782 unsigned int len, unwritten;
2783 struct page *page;
2784 void *data, *vaddr;
2785 int err;
2786
2787 len = PAGE_SIZE - pg;
2788 if (len > remain)
2789 len = remain;
2790
2791 err = pagecache_write_begin(obj->base.filp, mapping,
2792 offset, len, 0,
2793 &page, &data);
2794 if (err < 0)
2795 return err;
2796
2797 vaddr = kmap(page);
2798 unwritten = copy_from_user(vaddr + pg, user_data, len);
2799 kunmap(page);
2800
2801 err = pagecache_write_end(obj->base.filp, mapping,
2802 offset, len, len - unwritten,
2803 page, data);
2804 if (err < 0)
2805 return err;
2806
2807 if (unwritten)
2808 return -EFAULT;
2809
2810 remain -= len;
2811 user_data += len;
2812 offset += len;
2813 pg = 0;
2814 } while (remain);
2815
2816 return 0;
2817}
2818
Chris Wilson85474442019-01-29 18:54:50 +00002819static bool match_ring(struct i915_request *rq)
2820{
2821 struct drm_i915_private *dev_priv = rq->i915;
2822 u32 ring = I915_READ(RING_START(rq->engine->mmio_base));
2823
2824 return ring == i915_ggtt_offset(rq->ring->vma);
2825}
2826
Chris Wilsone61e0f52018-02-21 09:56:36 +00002827struct i915_request *
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00002828i915_gem_find_active_request(struct intel_engine_cs *engine)
Chris Wilson9375e442010-09-19 12:21:28 +01002829{
Chris Wilsone61e0f52018-02-21 09:56:36 +00002830 struct i915_request *request, *active = NULL;
Chris Wilson754c9fd2017-02-23 07:44:14 +00002831 unsigned long flags;
Mika Kuoppalaaa60c662013-06-12 15:13:20 +03002832
Chris Wilsoncc7cc532018-05-29 14:29:18 +01002833 /*
2834 * We are called by the error capture, reset and to dump engine
2835 * state at random points in time. In particular, note that neither is
2836 * crucially ordered with an interrupt. After a hang, the GPU is dead
2837 * and we assume that no more writes can happen (we waited long enough
2838 * for all writes that were in transaction to be flushed) - adding an
Chris Wilsonf69a02c2016-07-01 17:23:16 +01002839 * extra delay for a recent interrupt is pointless. Hence, we do
2840 * not need an engine->irq_seqno_barrier() before the seqno reads.
Chris Wilsoncc7cc532018-05-29 14:29:18 +01002841 * At all other times, we must assume the GPU is still running, but
2842 * we only care about the snapshot of this moment.
Chris Wilsonf69a02c2016-07-01 17:23:16 +01002843 */
Chris Wilsona89d1f92018-05-02 17:38:39 +01002844 spin_lock_irqsave(&engine->timeline.lock, flags);
2845 list_for_each_entry(request, &engine->timeline.requests, link) {
Chris Wilson5013eb82019-01-28 18:18:11 +00002846 if (i915_request_completed(request))
Chris Wilson4db080f2013-12-04 11:37:09 +00002847 continue;
Mika Kuoppalaaa60c662013-06-12 15:13:20 +03002848
Chris Wilson85474442019-01-29 18:54:50 +00002849 if (!i915_request_started(request))
2850 break;
2851
2852 /* More than one preemptible request may match! */
2853 if (!match_ring(request))
2854 break;
2855
Chris Wilson754c9fd2017-02-23 07:44:14 +00002856 active = request;
2857 break;
2858 }
Chris Wilsona89d1f92018-05-02 17:38:39 +01002859 spin_unlock_irqrestore(&engine->timeline.lock, flags);
Chris Wilson754c9fd2017-02-23 07:44:14 +00002860
2861 return active;
Mika Kuoppalab6b0fac2014-01-30 19:04:43 +02002862}
2863
Daniel Vetter75ef9da2010-08-21 00:25:16 +02002864static void
Eric Anholt673a3942008-07-30 12:06:12 -07002865i915_gem_retire_work_handler(struct work_struct *work)
2866{
Chris Wilsonb29c19b2013-09-25 17:34:56 +01002867 struct drm_i915_private *dev_priv =
Chris Wilson67d97da2016-07-04 08:08:31 +01002868 container_of(work, typeof(*dev_priv), gt.retire_work.work);
Chris Wilson91c8a322016-07-05 10:40:23 +01002869 struct drm_device *dev = &dev_priv->drm;
Eric Anholt673a3942008-07-30 12:06:12 -07002870
Chris Wilson891b48c2010-09-29 12:26:37 +01002871 /* Come back later if the device is busy... */
Chris Wilsonb29c19b2013-09-25 17:34:56 +01002872 if (mutex_trylock(&dev->struct_mutex)) {
Chris Wilsone61e0f52018-02-21 09:56:36 +00002873 i915_retire_requests(dev_priv);
Chris Wilsonb29c19b2013-09-25 17:34:56 +01002874 mutex_unlock(&dev->struct_mutex);
2875 }
Chris Wilson67d97da2016-07-04 08:08:31 +01002876
Chris Wilson88923042018-01-29 14:41:04 +00002877 /*
2878 * Keep the retire handler running until we are finally idle.
Chris Wilson67d97da2016-07-04 08:08:31 +01002879 * We do not need to do this test under locking as in the worst-case
2880 * we queue the retire worker once too often.
2881 */
Chris Wilson88923042018-01-29 14:41:04 +00002882 if (READ_ONCE(dev_priv->gt.awake))
Chris Wilson67d97da2016-07-04 08:08:31 +01002883 queue_delayed_work(dev_priv->wq,
2884 &dev_priv->gt.retire_work,
Chris Wilsonbcb45082012-10-05 17:02:57 +01002885 round_jiffies_up_relative(HZ));
Chris Wilsonb29c19b2013-09-25 17:34:56 +01002886}
Chris Wilson891b48c2010-09-29 12:26:37 +01002887
Chris Wilson84a10742018-01-24 11:36:08 +00002888static void shrink_caches(struct drm_i915_private *i915)
2889{
2890 /*
2891 * kmem_cache_shrink() discards empty slabs and reorders partially
2892 * filled slabs to prioritise allocating from the mostly full slabs,
2893 * with the aim of reducing fragmentation.
2894 */
2895 kmem_cache_shrink(i915->priorities);
2896 kmem_cache_shrink(i915->dependencies);
2897 kmem_cache_shrink(i915->requests);
2898 kmem_cache_shrink(i915->luts);
2899 kmem_cache_shrink(i915->vmas);
2900 kmem_cache_shrink(i915->objects);
2901}
2902
2903struct sleep_rcu_work {
2904 union {
2905 struct rcu_head rcu;
2906 struct work_struct work;
2907 };
2908 struct drm_i915_private *i915;
2909 unsigned int epoch;
2910};
2911
2912static inline bool
2913same_epoch(struct drm_i915_private *i915, unsigned int epoch)
2914{
2915 /*
2916 * There is a small chance that the epoch wrapped since we started
2917 * sleeping. If we assume that epoch is at least a u32, then it will
2918 * take at least 2^32 * 100ms for it to wrap, or about 326 years.
2919 */
2920 return epoch == READ_ONCE(i915->gt.epoch);
2921}
2922
2923static void __sleep_work(struct work_struct *work)
2924{
2925 struct sleep_rcu_work *s = container_of(work, typeof(*s), work);
2926 struct drm_i915_private *i915 = s->i915;
2927 unsigned int epoch = s->epoch;
2928
2929 kfree(s);
2930 if (same_epoch(i915, epoch))
2931 shrink_caches(i915);
2932}
2933
2934static void __sleep_rcu(struct rcu_head *rcu)
2935{
2936 struct sleep_rcu_work *s = container_of(rcu, typeof(*s), rcu);
2937 struct drm_i915_private *i915 = s->i915;
2938
Chris Wilsona1db9c52018-11-08 09:21:01 +00002939 destroy_rcu_head(&s->rcu);
2940
Chris Wilson84a10742018-01-24 11:36:08 +00002941 if (same_epoch(i915, s->epoch)) {
2942 INIT_WORK(&s->work, __sleep_work);
2943 queue_work(i915->wq, &s->work);
2944 } else {
2945 kfree(s);
2946 }
2947}
2948
Chris Wilson5427f202017-10-23 22:32:34 +01002949static inline bool
2950new_requests_since_last_retire(const struct drm_i915_private *i915)
2951{
2952 return (READ_ONCE(i915->gt.active_requests) ||
2953 work_pending(&i915->gt.idle_work.work));
2954}
2955
Chris Wilson1934f5de2018-05-31 23:40:57 +01002956static void assert_kernel_context_is_current(struct drm_i915_private *i915)
2957{
2958 struct intel_engine_cs *engine;
2959 enum intel_engine_id id;
2960
Chris Wilsonc41166f2019-02-20 14:56:37 +00002961 if (i915_reset_failed(i915))
Chris Wilson1934f5de2018-05-31 23:40:57 +01002962 return;
2963
2964 GEM_BUG_ON(i915->gt.active_requests);
2965 for_each_engine(engine, i915, id) {
Chris Wilson21950ee2019-02-05 13:00:05 +00002966 GEM_BUG_ON(__i915_active_request_peek(&engine->timeline.last_request));
Chris Wilson1934f5de2018-05-31 23:40:57 +01002967 GEM_BUG_ON(engine->last_retired_context !=
2968 to_intel_context(i915->kernel_context, engine));
2969 }
2970}
2971
Chris Wilsonb29c19b2013-09-25 17:34:56 +01002972static void
2973i915_gem_idle_work_handler(struct work_struct *work)
2974{
2975 struct drm_i915_private *dev_priv =
Chris Wilson67d97da2016-07-04 08:08:31 +01002976 container_of(work, typeof(*dev_priv), gt.idle_work.work);
Chris Wilson84a10742018-01-24 11:36:08 +00002977 unsigned int epoch = I915_EPOCH_INVALID;
Chris Wilson67d97da2016-07-04 08:08:31 +01002978 bool rearm_hangcheck;
2979
2980 if (!READ_ONCE(dev_priv->gt.awake))
2981 return;
2982
Chris Wilson4dfacb02018-05-31 09:22:43 +01002983 if (READ_ONCE(dev_priv->gt.active_requests))
2984 return;
2985
2986 /*
2987 * Flush out the last user context, leaving only the pinned
2988 * kernel context resident. When we are idling on the kernel_context,
2989 * no more new requests (with a context switch) are emitted and we
2990 * can finally rest. A consequence is that the idle work handler is
2991 * always called at least twice before idling (and if the system is
2992 * idle that implies a round trip through the retire worker).
2993 */
2994 mutex_lock(&dev_priv->drm.struct_mutex);
2995 i915_gem_switch_to_kernel_context(dev_priv);
2996 mutex_unlock(&dev_priv->drm.struct_mutex);
2997
2998 GEM_TRACE("active_requests=%d (after switch-to-kernel-context)\n",
2999 READ_ONCE(dev_priv->gt.active_requests));
3000
Imre Deak0cb56702016-11-07 11:20:04 +02003001 /*
3002 * Wait for last execlists context complete, but bail out in case a
Chris Wilsonffed7bd2018-03-01 10:33:38 +00003003 * new request is submitted. As we don't trust the hardware, we
3004 * continue on if the wait times out. This is necessary to allow
3005 * the machine to suspend even if the hardware dies, and we will
3006 * try to recover in resume (after depriving the hardware of power,
3007 * it may be in a better mmod).
Imre Deak0cb56702016-11-07 11:20:04 +02003008 */
Chris Wilsonffed7bd2018-03-01 10:33:38 +00003009 __wait_for(if (new_requests_since_last_retire(dev_priv)) return,
3010 intel_engines_are_idle(dev_priv),
3011 I915_IDLE_ENGINES_TIMEOUT * 1000,
3012 10, 500);
Chris Wilson67d97da2016-07-04 08:08:31 +01003013
3014 rearm_hangcheck =
3015 cancel_delayed_work_sync(&dev_priv->gpu_error.hangcheck_work);
3016
Chris Wilson5427f202017-10-23 22:32:34 +01003017 if (!mutex_trylock(&dev_priv->drm.struct_mutex)) {
Chris Wilson67d97da2016-07-04 08:08:31 +01003018 /* Currently busy, come back later */
3019 mod_delayed_work(dev_priv->wq,
3020 &dev_priv->gt.idle_work,
3021 msecs_to_jiffies(50));
3022 goto out_rearm;
3023 }
3024
Imre Deak93c97dc2016-11-07 11:20:03 +02003025 /*
3026 * New request retired after this work handler started, extend active
3027 * period until next instance of the work.
3028 */
Chris Wilson5427f202017-10-23 22:32:34 +01003029 if (new_requests_since_last_retire(dev_priv))
Imre Deak93c97dc2016-11-07 11:20:03 +02003030 goto out_unlock;
3031
Chris Wilsone4d20062018-04-06 16:51:44 +01003032 epoch = __i915_gem_park(dev_priv);
Chris Wilsonff320d62017-10-23 22:32:35 +01003033
Chris Wilson1934f5de2018-05-31 23:40:57 +01003034 assert_kernel_context_is_current(dev_priv);
3035
Chris Wilson67d97da2016-07-04 08:08:31 +01003036 rearm_hangcheck = false;
Chris Wilson67d97da2016-07-04 08:08:31 +01003037out_unlock:
Chris Wilson5427f202017-10-23 22:32:34 +01003038 mutex_unlock(&dev_priv->drm.struct_mutex);
Chris Wilson35c94182015-04-07 16:20:37 +01003039
Chris Wilson67d97da2016-07-04 08:08:31 +01003040out_rearm:
3041 if (rearm_hangcheck) {
3042 GEM_BUG_ON(!dev_priv->gt.awake);
3043 i915_queue_hangcheck(dev_priv);
Chris Wilson35c94182015-04-07 16:20:37 +01003044 }
Chris Wilson84a10742018-01-24 11:36:08 +00003045
3046 /*
3047 * When we are idle, it is an opportune time to reap our caches.
3048 * However, we have many objects that utilise RCU and the ordered
3049 * i915->wq that this work is executing on. To try and flush any
3050 * pending frees now we are idle, we first wait for an RCU grace
3051 * period, and then queue a task (that will run last on the wq) to
3052 * shrink and re-optimize the caches.
3053 */
3054 if (same_epoch(dev_priv, epoch)) {
3055 struct sleep_rcu_work *s = kmalloc(sizeof(*s), GFP_KERNEL);
3056 if (s) {
Chris Wilsona1db9c52018-11-08 09:21:01 +00003057 init_rcu_head(&s->rcu);
Chris Wilson84a10742018-01-24 11:36:08 +00003058 s->i915 = dev_priv;
3059 s->epoch = epoch;
3060 call_rcu(&s->rcu, __sleep_rcu);
3061 }
3062 }
Eric Anholt673a3942008-07-30 12:06:12 -07003063}
3064
Chris Wilsonb1f788c2016-08-04 07:52:45 +01003065void i915_gem_close_object(struct drm_gem_object *gem, struct drm_file *file)
3066{
Chris Wilsond1b48c12017-08-16 09:52:08 +01003067 struct drm_i915_private *i915 = to_i915(gem->dev);
Chris Wilsonb1f788c2016-08-04 07:52:45 +01003068 struct drm_i915_gem_object *obj = to_intel_bo(gem);
3069 struct drm_i915_file_private *fpriv = file->driver_priv;
Chris Wilsond1b48c12017-08-16 09:52:08 +01003070 struct i915_lut_handle *lut, *ln;
Chris Wilsonb1f788c2016-08-04 07:52:45 +01003071
Chris Wilsond1b48c12017-08-16 09:52:08 +01003072 mutex_lock(&i915->drm.struct_mutex);
3073
3074 list_for_each_entry_safe(lut, ln, &obj->lut_list, obj_link) {
3075 struct i915_gem_context *ctx = lut->ctx;
3076 struct i915_vma *vma;
3077
Chris Wilson432295d2017-08-22 12:05:15 +01003078 GEM_BUG_ON(ctx->file_priv == ERR_PTR(-EBADF));
Chris Wilsond1b48c12017-08-16 09:52:08 +01003079 if (ctx->file_priv != fpriv)
3080 continue;
3081
3082 vma = radix_tree_delete(&ctx->handles_vma, lut->handle);
Chris Wilson3ffff012017-08-22 12:05:17 +01003083 GEM_BUG_ON(vma->obj != obj);
3084
3085 /* We allow the process to have multiple handles to the same
3086 * vma, in the same fd namespace, by virtue of flink/open.
3087 */
3088 GEM_BUG_ON(!vma->open_count);
3089 if (!--vma->open_count && !i915_vma_is_ggtt(vma))
Chris Wilsonb1f788c2016-08-04 07:52:45 +01003090 i915_vma_close(vma);
Chris Wilsonf8a7fde2016-10-28 13:58:29 +01003091
Chris Wilsond1b48c12017-08-16 09:52:08 +01003092 list_del(&lut->obj_link);
3093 list_del(&lut->ctx_link);
Chris Wilson4ff4b442017-06-16 15:05:16 +01003094
Chris Wilsond1b48c12017-08-16 09:52:08 +01003095 kmem_cache_free(i915->luts, lut);
3096 __i915_gem_object_release_unless_active(obj);
Chris Wilsonf8a7fde2016-10-28 13:58:29 +01003097 }
Chris Wilsond1b48c12017-08-16 09:52:08 +01003098
3099 mutex_unlock(&i915->drm.struct_mutex);
Chris Wilsonb1f788c2016-08-04 07:52:45 +01003100}
3101
Chris Wilsone95433c2016-10-28 13:58:27 +01003102static unsigned long to_wait_timeout(s64 timeout_ns)
3103{
3104 if (timeout_ns < 0)
3105 return MAX_SCHEDULE_TIMEOUT;
3106
3107 if (timeout_ns == 0)
3108 return 0;
3109
3110 return nsecs_to_jiffies_timeout(timeout_ns);
3111}
3112
Ben Widawsky5816d642012-04-11 11:18:19 -07003113/**
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07003114 * i915_gem_wait_ioctl - implements DRM_IOCTL_I915_GEM_WAIT
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01003115 * @dev: drm device pointer
3116 * @data: ioctl data blob
3117 * @file: drm file pointer
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07003118 *
3119 * Returns 0 if successful, else an error is returned with the remaining time in
3120 * the timeout parameter.
3121 * -ETIME: object is still busy after timeout
3122 * -ERESTARTSYS: signal interrupted the wait
3123 * -ENONENT: object doesn't exist
3124 * Also possible, but rare:
Chris Wilsonb8050142017-08-11 11:57:31 +01003125 * -EAGAIN: incomplete, restart syscall
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07003126 * -ENOMEM: damn
3127 * -ENODEV: Internal IRQ fail
3128 * -E?: The add request failed
3129 *
3130 * The wait ioctl with a timeout of 0 reimplements the busy ioctl. With any
3131 * non-zero timeout parameter the wait ioctl will wait for the given number of
3132 * nanoseconds on an object becoming unbusy. Since the wait itself does so
3133 * without holding struct_mutex the object may become re-busied before this
3134 * function completes. A similar but shorter * race condition exists in the busy
3135 * ioctl
3136 */
3137int
3138i915_gem_wait_ioctl(struct drm_device *dev, void *data, struct drm_file *file)
3139{
3140 struct drm_i915_gem_wait *args = data;
3141 struct drm_i915_gem_object *obj;
Chris Wilsone95433c2016-10-28 13:58:27 +01003142 ktime_t start;
3143 long ret;
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07003144
Daniel Vetter11b5d512014-09-29 15:31:26 +02003145 if (args->flags != 0)
3146 return -EINVAL;
3147
Chris Wilson03ac0642016-07-20 13:31:51 +01003148 obj = i915_gem_object_lookup(file, args->bo_handle);
Chris Wilson033d5492016-08-05 10:14:17 +01003149 if (!obj)
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07003150 return -ENOENT;
Chris Wilson033d5492016-08-05 10:14:17 +01003151
Chris Wilsone95433c2016-10-28 13:58:27 +01003152 start = ktime_get();
3153
3154 ret = i915_gem_object_wait(obj,
Chris Wilsone9eaf822018-10-01 15:47:55 +01003155 I915_WAIT_INTERRUPTIBLE |
3156 I915_WAIT_PRIORITY |
3157 I915_WAIT_ALL,
Chris Wilson62eb3c22019-02-13 09:25:04 +00003158 to_wait_timeout(args->timeout_ns));
Chris Wilsone95433c2016-10-28 13:58:27 +01003159
3160 if (args->timeout_ns > 0) {
3161 args->timeout_ns -= ktime_to_ns(ktime_sub(ktime_get(), start));
3162 if (args->timeout_ns < 0)
3163 args->timeout_ns = 0;
Chris Wilsonc1d20612017-02-16 12:54:41 +00003164
3165 /*
3166 * Apparently ktime isn't accurate enough and occasionally has a
3167 * bit of mismatch in the jiffies<->nsecs<->ktime loop. So patch
3168 * things up to make the test happy. We allow up to 1 jiffy.
3169 *
3170 * This is a regression from the timespec->ktime conversion.
3171 */
3172 if (ret == -ETIME && !nsecs_to_jiffies(args->timeout_ns))
3173 args->timeout_ns = 0;
Chris Wilsonb8050142017-08-11 11:57:31 +01003174
3175 /* Asked to wait beyond the jiffie/scheduler precision? */
3176 if (ret == -ETIME && args->timeout_ns)
3177 ret = -EAGAIN;
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07003178 }
3179
Chris Wilsonf0cd5182016-10-28 13:58:43 +01003180 i915_gem_object_put(obj);
John Harrisonff865882014-11-24 18:49:28 +00003181 return ret;
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07003182}
3183
Chris Wilson25112b62017-03-30 15:50:39 +01003184static int wait_for_engines(struct drm_i915_private *i915)
3185{
Chris Wilsonee42c002017-12-11 19:41:34 +00003186 if (wait_for(intel_engines_are_idle(i915), I915_IDLE_ENGINES_TIMEOUT)) {
Chris Wilson59e4b192017-12-11 19:41:35 +00003187 dev_err(i915->drm.dev,
3188 "Failed to idle engines, declaring wedged!\n");
Chris Wilson629820f2018-03-09 10:11:14 +00003189 GEM_TRACE_DUMP();
Chris Wilsoncad99462017-08-26 12:09:33 +01003190 i915_gem_set_wedged(i915);
3191 return -EIO;
Chris Wilson25112b62017-03-30 15:50:39 +01003192 }
3193
3194 return 0;
3195}
3196
Chris Wilson1e345562019-01-28 10:23:56 +00003197static long
3198wait_for_timelines(struct drm_i915_private *i915,
3199 unsigned int flags, long timeout)
3200{
3201 struct i915_gt_timelines *gt = &i915->gt.timelines;
3202 struct i915_timeline *tl;
3203
3204 if (!READ_ONCE(i915->gt.active_requests))
3205 return timeout;
3206
3207 mutex_lock(&gt->mutex);
Chris Wilson9407d3b2019-01-28 18:18:12 +00003208 list_for_each_entry(tl, &gt->active_list, link) {
Chris Wilson1e345562019-01-28 10:23:56 +00003209 struct i915_request *rq;
3210
Chris Wilson21950ee2019-02-05 13:00:05 +00003211 rq = i915_active_request_get_unlocked(&tl->last_request);
Chris Wilson1e345562019-01-28 10:23:56 +00003212 if (!rq)
3213 continue;
3214
3215 mutex_unlock(&gt->mutex);
3216
3217 /*
3218 * "Race-to-idle".
3219 *
3220 * Switching to the kernel context is often used a synchronous
3221 * step prior to idling, e.g. in suspend for flushing all
3222 * current operations to memory before sleeping. These we
3223 * want to complete as quickly as possible to avoid prolonged
3224 * stalls, so allow the gpu to boost to maximum clocks.
3225 */
3226 if (flags & I915_WAIT_FOR_IDLE_BOOST)
Chris Wilson62eb3c22019-02-13 09:25:04 +00003227 gen6_rps_boost(rq);
Chris Wilson1e345562019-01-28 10:23:56 +00003228
3229 timeout = i915_request_wait(rq, flags, timeout);
3230 i915_request_put(rq);
3231 if (timeout < 0)
3232 return timeout;
3233
3234 /* restart after reacquiring the lock */
3235 mutex_lock(&gt->mutex);
Chris Wilson9407d3b2019-01-28 18:18:12 +00003236 tl = list_entry(&gt->active_list, typeof(*tl), link);
Chris Wilson1e345562019-01-28 10:23:56 +00003237 }
3238 mutex_unlock(&gt->mutex);
3239
3240 return timeout;
3241}
3242
Chris Wilsonec625fb2018-07-09 13:20:42 +01003243int i915_gem_wait_for_idle(struct drm_i915_private *i915,
3244 unsigned int flags, long timeout)
Chris Wilson73cb9702016-10-28 13:58:46 +01003245{
Chris Wilsonec625fb2018-07-09 13:20:42 +01003246 GEM_TRACE("flags=%x (%s), timeout=%ld%s\n",
3247 flags, flags & I915_WAIT_LOCKED ? "locked" : "unlocked",
3248 timeout, timeout == MAX_SCHEDULE_TIMEOUT ? " (forever)" : "");
Chris Wilson09a4c022018-05-24 09:11:35 +01003249
Chris Wilson863e9fd2017-05-30 13:13:32 +01003250 /* If the device is asleep, we have no requests outstanding */
3251 if (!READ_ONCE(i915->gt.awake))
3252 return 0;
3253
Chris Wilson1e345562019-01-28 10:23:56 +00003254 timeout = wait_for_timelines(i915, flags, timeout);
3255 if (timeout < 0)
3256 return timeout;
3257
Chris Wilson9caa34a2016-11-11 14:58:08 +00003258 if (flags & I915_WAIT_LOCKED) {
Chris Wilsona89d1f92018-05-02 17:38:39 +01003259 int err;
Chris Wilson9caa34a2016-11-11 14:58:08 +00003260
3261 lockdep_assert_held(&i915->drm.struct_mutex);
3262
Chris Wilsonc1e63f62018-08-08 11:50:59 +01003263 if (GEM_SHOW_DEBUG() && !timeout) {
3264 /* Presume that timeout was non-zero to begin with! */
3265 dev_warn(&i915->drm.pdev->dev,
3266 "Missed idle-completion interrupt!\n");
3267 GEM_TRACE_DUMP();
3268 }
Chris Wilsona61b47f2018-06-27 12:53:34 +01003269
3270 err = wait_for_engines(i915);
3271 if (err)
3272 return err;
3273
Chris Wilsone61e0f52018-02-21 09:56:36 +00003274 i915_retire_requests(i915);
Chris Wilson09a4c022018-05-24 09:11:35 +01003275 GEM_BUG_ON(i915->gt.active_requests);
Chris Wilsona89d1f92018-05-02 17:38:39 +01003276 }
Chris Wilsona61b47f2018-06-27 12:53:34 +01003277
3278 return 0;
Daniel Vetter4df2faf2010-02-19 11:52:00 +01003279}
3280
Chris Wilson5a97bcc2017-02-22 11:40:46 +00003281static void __i915_gem_object_flush_for_display(struct drm_i915_gem_object *obj)
3282{
Chris Wilsone27ab732017-06-15 13:38:49 +01003283 /*
3284 * We manually flush the CPU domain so that we can override and
3285 * force the flush for the display, and perform it asyncrhonously.
3286 */
3287 flush_write_domain(obj, ~I915_GEM_DOMAIN_CPU);
3288 if (obj->cache_dirty)
3289 i915_gem_clflush_object(obj, I915_CLFLUSH_FORCE);
Christian Königc0a51fd2018-02-16 13:43:38 +01003290 obj->write_domain = 0;
Chris Wilson5a97bcc2017-02-22 11:40:46 +00003291}
3292
3293void i915_gem_object_flush_if_display(struct drm_i915_gem_object *obj)
3294{
Chris Wilsonbd3d2252017-10-13 21:26:14 +01003295 if (!READ_ONCE(obj->pin_global))
Chris Wilson5a97bcc2017-02-22 11:40:46 +00003296 return;
3297
3298 mutex_lock(&obj->base.dev->struct_mutex);
3299 __i915_gem_object_flush_for_display(obj);
3300 mutex_unlock(&obj->base.dev->struct_mutex);
3301}
3302
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003303/**
Chris Wilsone22d8e32017-04-12 12:01:11 +01003304 * Moves a single object to the WC read, and possibly write domain.
3305 * @obj: object to act on
3306 * @write: ask for write access or read only
3307 *
3308 * This function returns when the move is complete, including waiting on
3309 * flushes to occur.
3310 */
3311int
3312i915_gem_object_set_to_wc_domain(struct drm_i915_gem_object *obj, bool write)
3313{
3314 int ret;
3315
3316 lockdep_assert_held(&obj->base.dev->struct_mutex);
3317
3318 ret = i915_gem_object_wait(obj,
3319 I915_WAIT_INTERRUPTIBLE |
3320 I915_WAIT_LOCKED |
3321 (write ? I915_WAIT_ALL : 0),
Chris Wilson62eb3c22019-02-13 09:25:04 +00003322 MAX_SCHEDULE_TIMEOUT);
Chris Wilsone22d8e32017-04-12 12:01:11 +01003323 if (ret)
3324 return ret;
3325
Christian Königc0a51fd2018-02-16 13:43:38 +01003326 if (obj->write_domain == I915_GEM_DOMAIN_WC)
Chris Wilsone22d8e32017-04-12 12:01:11 +01003327 return 0;
3328
3329 /* Flush and acquire obj->pages so that we are coherent through
3330 * direct access in memory with previous cached writes through
3331 * shmemfs and that our cache domain tracking remains valid.
3332 * For example, if the obj->filp was moved to swap without us
3333 * being notified and releasing the pages, we would mistakenly
3334 * continue to assume that the obj remained out of the CPU cached
3335 * domain.
3336 */
3337 ret = i915_gem_object_pin_pages(obj);
3338 if (ret)
3339 return ret;
3340
3341 flush_write_domain(obj, ~I915_GEM_DOMAIN_WC);
3342
3343 /* Serialise direct access to this object with the barriers for
3344 * coherent writes from the GPU, by effectively invalidating the
3345 * WC domain upon first access.
3346 */
Christian Königc0a51fd2018-02-16 13:43:38 +01003347 if ((obj->read_domains & I915_GEM_DOMAIN_WC) == 0)
Chris Wilsone22d8e32017-04-12 12:01:11 +01003348 mb();
3349
3350 /* It should now be out of any other write domains, and we can update
3351 * the domain values for our changes.
3352 */
Christian Königc0a51fd2018-02-16 13:43:38 +01003353 GEM_BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_WC) != 0);
3354 obj->read_domains |= I915_GEM_DOMAIN_WC;
Chris Wilsone22d8e32017-04-12 12:01:11 +01003355 if (write) {
Christian Königc0a51fd2018-02-16 13:43:38 +01003356 obj->read_domains = I915_GEM_DOMAIN_WC;
3357 obj->write_domain = I915_GEM_DOMAIN_WC;
Chris Wilsone22d8e32017-04-12 12:01:11 +01003358 obj->mm.dirty = true;
3359 }
3360
3361 i915_gem_object_unpin_pages(obj);
3362 return 0;
3363}
3364
3365/**
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003366 * Moves a single object to the GTT read, and possibly write domain.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01003367 * @obj: object to act on
3368 * @write: ask for write access or read only
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003369 *
3370 * This function returns when the move is complete, including waiting on
3371 * flushes to occur.
3372 */
Jesse Barnes79e53942008-11-07 14:24:08 -08003373int
Chris Wilson20217462010-11-23 15:26:33 +00003374i915_gem_object_set_to_gtt_domain(struct drm_i915_gem_object *obj, bool write)
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003375{
Eric Anholte47c68e2008-11-14 13:35:19 -08003376 int ret;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003377
Chris Wilsone95433c2016-10-28 13:58:27 +01003378 lockdep_assert_held(&obj->base.dev->struct_mutex);
Chris Wilson4c7d62c2016-10-28 13:58:32 +01003379
Chris Wilsone95433c2016-10-28 13:58:27 +01003380 ret = i915_gem_object_wait(obj,
3381 I915_WAIT_INTERRUPTIBLE |
3382 I915_WAIT_LOCKED |
3383 (write ? I915_WAIT_ALL : 0),
Chris Wilson62eb3c22019-02-13 09:25:04 +00003384 MAX_SCHEDULE_TIMEOUT);
Chris Wilson88241782011-01-07 17:09:48 +00003385 if (ret)
3386 return ret;
3387
Christian Königc0a51fd2018-02-16 13:43:38 +01003388 if (obj->write_domain == I915_GEM_DOMAIN_GTT)
Chris Wilsonc13d87e2016-07-20 09:21:15 +01003389 return 0;
3390
Chris Wilson43566de2015-01-02 16:29:29 +05303391 /* Flush and acquire obj->pages so that we are coherent through
3392 * direct access in memory with previous cached writes through
3393 * shmemfs and that our cache domain tracking remains valid.
3394 * For example, if the obj->filp was moved to swap without us
3395 * being notified and releasing the pages, we would mistakenly
3396 * continue to assume that the obj remained out of the CPU cached
3397 * domain.
3398 */
Chris Wilsona4f5ea62016-10-28 13:58:35 +01003399 ret = i915_gem_object_pin_pages(obj);
Chris Wilson43566de2015-01-02 16:29:29 +05303400 if (ret)
3401 return ret;
3402
Chris Wilsonef749212017-04-12 12:01:10 +01003403 flush_write_domain(obj, ~I915_GEM_DOMAIN_GTT);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003404
Chris Wilsond0a57782012-10-09 19:24:37 +01003405 /* Serialise direct access to this object with the barriers for
3406 * coherent writes from the GPU, by effectively invalidating the
3407 * GTT domain upon first access.
3408 */
Christian Königc0a51fd2018-02-16 13:43:38 +01003409 if ((obj->read_domains & I915_GEM_DOMAIN_GTT) == 0)
Chris Wilsond0a57782012-10-09 19:24:37 +01003410 mb();
3411
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003412 /* It should now be out of any other write domains, and we can update
3413 * the domain values for our changes.
3414 */
Christian Königc0a51fd2018-02-16 13:43:38 +01003415 GEM_BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_GTT) != 0);
3416 obj->read_domains |= I915_GEM_DOMAIN_GTT;
Eric Anholte47c68e2008-11-14 13:35:19 -08003417 if (write) {
Christian Königc0a51fd2018-02-16 13:43:38 +01003418 obj->read_domains = I915_GEM_DOMAIN_GTT;
3419 obj->write_domain = I915_GEM_DOMAIN_GTT;
Chris Wilsona4f5ea62016-10-28 13:58:35 +01003420 obj->mm.dirty = true;
Eric Anholte47c68e2008-11-14 13:35:19 -08003421 }
3422
Chris Wilsona4f5ea62016-10-28 13:58:35 +01003423 i915_gem_object_unpin_pages(obj);
Eric Anholte47c68e2008-11-14 13:35:19 -08003424 return 0;
3425}
3426
Chris Wilsonef55f922015-10-09 14:11:27 +01003427/**
3428 * Changes the cache-level of an object across all VMA.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01003429 * @obj: object to act on
3430 * @cache_level: new cache level to set for the object
Chris Wilsonef55f922015-10-09 14:11:27 +01003431 *
3432 * After this function returns, the object will be in the new cache-level
3433 * across all GTT and the contents of the backing storage will be coherent,
3434 * with respect to the new cache-level. In order to keep the backing storage
3435 * coherent for all users, we only allow a single cache level to be set
3436 * globally on the object and prevent it from being changed whilst the
3437 * hardware is reading from the object. That is if the object is currently
3438 * on the scanout it will be set to uncached (or equivalent display
3439 * cache coherency) and all non-MOCS GPU access will also be uncached so
3440 * that all direct access to the scanout remains coherent.
3441 */
Chris Wilsone4ffd172011-04-04 09:44:39 +01003442int i915_gem_object_set_cache_level(struct drm_i915_gem_object *obj,
3443 enum i915_cache_level cache_level)
3444{
Chris Wilsonaa653a62016-08-04 07:52:27 +01003445 struct i915_vma *vma;
Chris Wilsona6a7cc42016-11-18 21:17:46 +00003446 int ret;
Chris Wilsone4ffd172011-04-04 09:44:39 +01003447
Chris Wilson4c7d62c2016-10-28 13:58:32 +01003448 lockdep_assert_held(&obj->base.dev->struct_mutex);
3449
Chris Wilsone4ffd172011-04-04 09:44:39 +01003450 if (obj->cache_level == cache_level)
Chris Wilsona6a7cc42016-11-18 21:17:46 +00003451 return 0;
Chris Wilsone4ffd172011-04-04 09:44:39 +01003452
Chris Wilsonef55f922015-10-09 14:11:27 +01003453 /* Inspect the list of currently bound VMA and unbind any that would
3454 * be invalid given the new cache-level. This is principally to
3455 * catch the issue of the CS prefetch crossing page boundaries and
3456 * reading an invalid PTE on older architectures.
3457 */
Chris Wilsonaa653a62016-08-04 07:52:27 +01003458restart:
Chris Wilson528cbd12019-01-28 10:23:54 +00003459 list_for_each_entry(vma, &obj->vma.list, obj_link) {
Chris Wilsonef55f922015-10-09 14:11:27 +01003460 if (!drm_mm_node_allocated(&vma->node))
3461 continue;
3462
Chris Wilson20dfbde2016-08-04 16:32:30 +01003463 if (i915_vma_is_pinned(vma)) {
Chris Wilsonef55f922015-10-09 14:11:27 +01003464 DRM_DEBUG("can not change the cache level of pinned objects\n");
3465 return -EBUSY;
3466 }
3467
Chris Wilson010e3e62017-12-06 12:49:13 +00003468 if (!i915_vma_is_closed(vma) &&
3469 i915_gem_valid_gtt_space(vma, cache_level))
Chris Wilsonaa653a62016-08-04 07:52:27 +01003470 continue;
3471
3472 ret = i915_vma_unbind(vma);
3473 if (ret)
3474 return ret;
3475
3476 /* As unbinding may affect other elements in the
3477 * obj->vma_list (due to side-effects from retiring
3478 * an active vma), play safe and restart the iterator.
3479 */
3480 goto restart;
Chris Wilson42d6ab42012-07-26 11:49:32 +01003481 }
3482
Chris Wilsonef55f922015-10-09 14:11:27 +01003483 /* We can reuse the existing drm_mm nodes but need to change the
3484 * cache-level on the PTE. We could simply unbind them all and
3485 * rebind with the correct cache-level on next use. However since
3486 * we already have a valid slot, dma mapping, pages etc, we may as
3487 * rewrite the PTE in the belief that doing so tramples upon less
3488 * state and so involves less work.
3489 */
Chris Wilson15717de2016-08-04 07:52:26 +01003490 if (obj->bind_count) {
Chris Wilsonef55f922015-10-09 14:11:27 +01003491 /* Before we change the PTE, the GPU must not be accessing it.
3492 * If we wait upon the object, we know that all the bound
3493 * VMA are no longer active.
3494 */
Chris Wilsone95433c2016-10-28 13:58:27 +01003495 ret = i915_gem_object_wait(obj,
3496 I915_WAIT_INTERRUPTIBLE |
3497 I915_WAIT_LOCKED |
3498 I915_WAIT_ALL,
Chris Wilson62eb3c22019-02-13 09:25:04 +00003499 MAX_SCHEDULE_TIMEOUT);
Chris Wilsone4ffd172011-04-04 09:44:39 +01003500 if (ret)
3501 return ret;
3502
Tvrtko Ursulin0031fb92016-11-04 14:42:44 +00003503 if (!HAS_LLC(to_i915(obj->base.dev)) &&
3504 cache_level != I915_CACHE_NONE) {
Chris Wilsonef55f922015-10-09 14:11:27 +01003505 /* Access to snoopable pages through the GTT is
3506 * incoherent and on some machines causes a hard
3507 * lockup. Relinquish the CPU mmaping to force
3508 * userspace to refault in the pages and we can
3509 * then double check if the GTT mapping is still
3510 * valid for that pointer access.
3511 */
3512 i915_gem_release_mmap(obj);
Chris Wilsone4ffd172011-04-04 09:44:39 +01003513
Chris Wilsonef55f922015-10-09 14:11:27 +01003514 /* As we no longer need a fence for GTT access,
3515 * we can relinquish it now (and so prevent having
3516 * to steal a fence from someone else on the next
3517 * fence request). Note GPU activity would have
3518 * dropped the fence as all snoopable access is
3519 * supposed to be linear.
3520 */
Chris Wilsone2189dd2017-12-07 21:14:07 +00003521 for_each_ggtt_vma(vma, obj) {
Chris Wilson49ef5292016-08-18 17:17:00 +01003522 ret = i915_vma_put_fence(vma);
3523 if (ret)
3524 return ret;
3525 }
Chris Wilsonef55f922015-10-09 14:11:27 +01003526 } else {
3527 /* We either have incoherent backing store and
3528 * so no GTT access or the architecture is fully
3529 * coherent. In such cases, existing GTT mmaps
3530 * ignore the cache bit in the PTE and we can
3531 * rewrite it without confusing the GPU or having
3532 * to force userspace to fault back in its mmaps.
3533 */
Chris Wilsone4ffd172011-04-04 09:44:39 +01003534 }
3535
Chris Wilson528cbd12019-01-28 10:23:54 +00003536 list_for_each_entry(vma, &obj->vma.list, obj_link) {
Chris Wilsonef55f922015-10-09 14:11:27 +01003537 if (!drm_mm_node_allocated(&vma->node))
3538 continue;
3539
3540 ret = i915_vma_bind(vma, cache_level, PIN_UPDATE);
3541 if (ret)
3542 return ret;
3543 }
Chris Wilsone4ffd172011-04-04 09:44:39 +01003544 }
3545
Chris Wilson528cbd12019-01-28 10:23:54 +00003546 list_for_each_entry(vma, &obj->vma.list, obj_link)
Chris Wilson2c225692013-08-09 12:26:45 +01003547 vma->node.color = cache_level;
Chris Wilsonb8f55be2017-08-11 12:11:16 +01003548 i915_gem_object_set_cache_coherency(obj, cache_level);
Chris Wilsone27ab732017-06-15 13:38:49 +01003549 obj->cache_dirty = true; /* Always invalidate stale cachelines */
Chris Wilson2c225692013-08-09 12:26:45 +01003550
Chris Wilsone4ffd172011-04-04 09:44:39 +01003551 return 0;
3552}
3553
Ben Widawsky199adf42012-09-21 17:01:20 -07003554int i915_gem_get_caching_ioctl(struct drm_device *dev, void *data,
3555 struct drm_file *file)
Chris Wilsone6994ae2012-07-10 10:27:08 +01003556{
Ben Widawsky199adf42012-09-21 17:01:20 -07003557 struct drm_i915_gem_caching *args = data;
Chris Wilsone6994ae2012-07-10 10:27:08 +01003558 struct drm_i915_gem_object *obj;
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01003559 int err = 0;
Chris Wilsone6994ae2012-07-10 10:27:08 +01003560
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01003561 rcu_read_lock();
3562 obj = i915_gem_object_lookup_rcu(file, args->handle);
3563 if (!obj) {
3564 err = -ENOENT;
3565 goto out;
3566 }
Chris Wilsone6994ae2012-07-10 10:27:08 +01003567
Chris Wilson651d7942013-08-08 14:41:10 +01003568 switch (obj->cache_level) {
3569 case I915_CACHE_LLC:
3570 case I915_CACHE_L3_LLC:
3571 args->caching = I915_CACHING_CACHED;
3572 break;
3573
Chris Wilson4257d3b2013-08-08 14:41:11 +01003574 case I915_CACHE_WT:
3575 args->caching = I915_CACHING_DISPLAY;
3576 break;
3577
Chris Wilson651d7942013-08-08 14:41:10 +01003578 default:
3579 args->caching = I915_CACHING_NONE;
3580 break;
3581 }
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01003582out:
3583 rcu_read_unlock();
3584 return err;
Chris Wilsone6994ae2012-07-10 10:27:08 +01003585}
3586
Ben Widawsky199adf42012-09-21 17:01:20 -07003587int i915_gem_set_caching_ioctl(struct drm_device *dev, void *data,
3588 struct drm_file *file)
Chris Wilsone6994ae2012-07-10 10:27:08 +01003589{
Chris Wilson9c870d02016-10-24 13:42:15 +01003590 struct drm_i915_private *i915 = to_i915(dev);
Ben Widawsky199adf42012-09-21 17:01:20 -07003591 struct drm_i915_gem_caching *args = data;
Chris Wilsone6994ae2012-07-10 10:27:08 +01003592 struct drm_i915_gem_object *obj;
3593 enum i915_cache_level level;
Chris Wilsond65415d2017-01-19 08:22:10 +00003594 int ret = 0;
Chris Wilsone6994ae2012-07-10 10:27:08 +01003595
Ben Widawsky199adf42012-09-21 17:01:20 -07003596 switch (args->caching) {
3597 case I915_CACHING_NONE:
Chris Wilsone6994ae2012-07-10 10:27:08 +01003598 level = I915_CACHE_NONE;
3599 break;
Ben Widawsky199adf42012-09-21 17:01:20 -07003600 case I915_CACHING_CACHED:
Imre Deake5756c12015-08-14 18:43:30 +03003601 /*
3602 * Due to a HW issue on BXT A stepping, GPU stores via a
3603 * snooped mapping may leave stale data in a corresponding CPU
3604 * cacheline, whereas normally such cachelines would get
3605 * invalidated.
3606 */
Chris Wilson9c870d02016-10-24 13:42:15 +01003607 if (!HAS_LLC(i915) && !HAS_SNOOP(i915))
Imre Deake5756c12015-08-14 18:43:30 +03003608 return -ENODEV;
3609
Chris Wilsone6994ae2012-07-10 10:27:08 +01003610 level = I915_CACHE_LLC;
3611 break;
Chris Wilson4257d3b2013-08-08 14:41:11 +01003612 case I915_CACHING_DISPLAY:
Chris Wilson9c870d02016-10-24 13:42:15 +01003613 level = HAS_WT(i915) ? I915_CACHE_WT : I915_CACHE_NONE;
Chris Wilson4257d3b2013-08-08 14:41:11 +01003614 break;
Chris Wilsone6994ae2012-07-10 10:27:08 +01003615 default:
3616 return -EINVAL;
3617 }
3618
Chris Wilsond65415d2017-01-19 08:22:10 +00003619 obj = i915_gem_object_lookup(file, args->handle);
3620 if (!obj)
3621 return -ENOENT;
3622
Tina Zhanga03f3952017-11-14 10:25:13 +00003623 /*
3624 * The caching mode of proxy object is handled by its generator, and
3625 * not allowed to be changed by userspace.
3626 */
3627 if (i915_gem_object_is_proxy(obj)) {
3628 ret = -ENXIO;
3629 goto out;
3630 }
3631
Chris Wilsond65415d2017-01-19 08:22:10 +00003632 if (obj->cache_level == level)
3633 goto out;
3634
3635 ret = i915_gem_object_wait(obj,
3636 I915_WAIT_INTERRUPTIBLE,
Chris Wilson62eb3c22019-02-13 09:25:04 +00003637 MAX_SCHEDULE_TIMEOUT);
Chris Wilsond65415d2017-01-19 08:22:10 +00003638 if (ret)
3639 goto out;
3640
Ben Widawsky3bc29132012-09-26 16:15:20 -07003641 ret = i915_mutex_lock_interruptible(dev);
3642 if (ret)
Chris Wilsond65415d2017-01-19 08:22:10 +00003643 goto out;
Chris Wilsone6994ae2012-07-10 10:27:08 +01003644
3645 ret = i915_gem_object_set_cache_level(obj, level);
Chris Wilsone6994ae2012-07-10 10:27:08 +01003646 mutex_unlock(&dev->struct_mutex);
Chris Wilsond65415d2017-01-19 08:22:10 +00003647
3648out:
3649 i915_gem_object_put(obj);
Chris Wilsone6994ae2012-07-10 10:27:08 +01003650 return ret;
3651}
3652
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003653/*
Dhinakaran Pandiyan07bcd992018-03-06 19:34:18 -08003654 * Prepare buffer for display plane (scanout, cursors, etc). Can be called from
3655 * an uninterruptible phase (modesetting) and allows any flushes to be pipelined
3656 * (for pageflips). We only flush the caches while preparing the buffer for
3657 * display, the callers are responsible for frontbuffer flush.
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003658 */
Chris Wilson058d88c2016-08-15 10:49:06 +01003659struct i915_vma *
Chris Wilson2da3b9b2011-04-14 09:41:17 +01003660i915_gem_object_pin_to_display_plane(struct drm_i915_gem_object *obj,
3661 u32 alignment,
Chris Wilson59354852018-02-20 13:42:06 +00003662 const struct i915_ggtt_view *view,
3663 unsigned int flags)
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003664{
Chris Wilson058d88c2016-08-15 10:49:06 +01003665 struct i915_vma *vma;
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003666 int ret;
3667
Chris Wilson4c7d62c2016-10-28 13:58:32 +01003668 lockdep_assert_held(&obj->base.dev->struct_mutex);
3669
Chris Wilsonbd3d2252017-10-13 21:26:14 +01003670 /* Mark the global pin early so that we account for the
Chris Wilsoncc98b412013-08-09 12:25:09 +01003671 * display coherency whilst setting up the cache domains.
3672 */
Chris Wilsonbd3d2252017-10-13 21:26:14 +01003673 obj->pin_global++;
Chris Wilsoncc98b412013-08-09 12:25:09 +01003674
Eric Anholta7ef0642011-03-29 16:59:54 -07003675 /* The display engine is not coherent with the LLC cache on gen6. As
3676 * a result, we make sure that the pinning that is about to occur is
3677 * done with uncached PTEs. This is lowest common denominator for all
3678 * chipsets.
3679 *
3680 * However for gen6+, we could do better by using the GFDT bit instead
3681 * of uncaching, which would allow us to flush all the LLC-cached data
3682 * with that bit in the PTE to main memory with just one PIPE_CONTROL.
3683 */
Chris Wilson651d7942013-08-08 14:41:10 +01003684 ret = i915_gem_object_set_cache_level(obj,
Tvrtko Ursulin86527442016-10-13 11:03:00 +01003685 HAS_WT(to_i915(obj->base.dev)) ?
3686 I915_CACHE_WT : I915_CACHE_NONE);
Chris Wilson058d88c2016-08-15 10:49:06 +01003687 if (ret) {
3688 vma = ERR_PTR(ret);
Chris Wilsonbd3d2252017-10-13 21:26:14 +01003689 goto err_unpin_global;
Chris Wilson058d88c2016-08-15 10:49:06 +01003690 }
Eric Anholta7ef0642011-03-29 16:59:54 -07003691
Chris Wilson2da3b9b2011-04-14 09:41:17 +01003692 /* As the user may map the buffer once pinned in the display plane
3693 * (e.g. libkms for the bootup splash), we have to ensure that we
Chris Wilson2efb8132016-08-18 17:17:06 +01003694 * always use map_and_fenceable for all scanout buffers. However,
3695 * it may simply be too big to fit into mappable, in which case
3696 * put it anyway and hope that userspace can cope (but always first
3697 * try to preserve the existing ABI).
Chris Wilson2da3b9b2011-04-14 09:41:17 +01003698 */
Chris Wilson2efb8132016-08-18 17:17:06 +01003699 vma = ERR_PTR(-ENOSPC);
Chris Wilson59354852018-02-20 13:42:06 +00003700 if ((flags & PIN_MAPPABLE) == 0 &&
3701 (!view || view->type == I915_GGTT_VIEW_NORMAL))
Chris Wilson2efb8132016-08-18 17:17:06 +01003702 vma = i915_gem_object_ggtt_pin(obj, view, 0, alignment,
Chris Wilson59354852018-02-20 13:42:06 +00003703 flags |
3704 PIN_MAPPABLE |
3705 PIN_NONBLOCK);
3706 if (IS_ERR(vma))
Chris Wilson767a2222016-11-07 11:01:28 +00003707 vma = i915_gem_object_ggtt_pin(obj, view, 0, alignment, flags);
Chris Wilson058d88c2016-08-15 10:49:06 +01003708 if (IS_ERR(vma))
Chris Wilsonbd3d2252017-10-13 21:26:14 +01003709 goto err_unpin_global;
Chris Wilson2da3b9b2011-04-14 09:41:17 +01003710
Chris Wilsond8923dc2016-08-18 17:17:07 +01003711 vma->display_alignment = max_t(u64, vma->display_alignment, alignment);
3712
Chris Wilson5a97bcc2017-02-22 11:40:46 +00003713 __i915_gem_object_flush_for_display(obj);
Chris Wilsonb118c1e2010-05-27 13:18:14 +01003714
Chris Wilson2da3b9b2011-04-14 09:41:17 +01003715 /* It should now be out of any other write domains, and we can update
3716 * the domain values for our changes.
3717 */
Christian Königc0a51fd2018-02-16 13:43:38 +01003718 obj->read_domains |= I915_GEM_DOMAIN_GTT;
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003719
Chris Wilson058d88c2016-08-15 10:49:06 +01003720 return vma;
Chris Wilsoncc98b412013-08-09 12:25:09 +01003721
Chris Wilsonbd3d2252017-10-13 21:26:14 +01003722err_unpin_global:
3723 obj->pin_global--;
Chris Wilson058d88c2016-08-15 10:49:06 +01003724 return vma;
Chris Wilsoncc98b412013-08-09 12:25:09 +01003725}
3726
3727void
Chris Wilson058d88c2016-08-15 10:49:06 +01003728i915_gem_object_unpin_from_display_plane(struct i915_vma *vma)
Chris Wilsoncc98b412013-08-09 12:25:09 +01003729{
Chris Wilson49d73912016-11-29 09:50:08 +00003730 lockdep_assert_held(&vma->vm->i915->drm.struct_mutex);
Chris Wilson4c7d62c2016-10-28 13:58:32 +01003731
Chris Wilsonbd3d2252017-10-13 21:26:14 +01003732 if (WARN_ON(vma->obj->pin_global == 0))
Tvrtko Ursulin8a0c39b2015-04-13 11:50:09 +01003733 return;
3734
Chris Wilsonbd3d2252017-10-13 21:26:14 +01003735 if (--vma->obj->pin_global == 0)
Chris Wilsonf51455d2017-01-10 14:47:34 +00003736 vma->display_alignment = I915_GTT_MIN_ALIGNMENT;
Tvrtko Ursuline6617332015-03-23 11:10:33 +00003737
Chris Wilson383d5822016-08-18 17:17:08 +01003738 /* Bump the LRU to try and avoid premature eviction whilst flipping */
Chris Wilsonbefedbb2017-01-19 19:26:55 +00003739 i915_gem_object_bump_inactive_ggtt(vma->obj);
Chris Wilson383d5822016-08-18 17:17:08 +01003740
Chris Wilson058d88c2016-08-15 10:49:06 +01003741 i915_vma_unpin(vma);
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003742}
3743
Eric Anholte47c68e2008-11-14 13:35:19 -08003744/**
3745 * Moves a single object to the CPU read, and possibly write domain.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01003746 * @obj: object to act on
3747 * @write: requesting write or read-only access
Eric Anholte47c68e2008-11-14 13:35:19 -08003748 *
3749 * This function returns when the move is complete, including waiting on
3750 * flushes to occur.
3751 */
Chris Wilsondabdfe02012-03-26 10:10:27 +02003752int
Chris Wilson919926a2010-11-12 13:42:53 +00003753i915_gem_object_set_to_cpu_domain(struct drm_i915_gem_object *obj, bool write)
Eric Anholte47c68e2008-11-14 13:35:19 -08003754{
Eric Anholte47c68e2008-11-14 13:35:19 -08003755 int ret;
3756
Chris Wilsone95433c2016-10-28 13:58:27 +01003757 lockdep_assert_held(&obj->base.dev->struct_mutex);
Chris Wilson4c7d62c2016-10-28 13:58:32 +01003758
Chris Wilsone95433c2016-10-28 13:58:27 +01003759 ret = i915_gem_object_wait(obj,
3760 I915_WAIT_INTERRUPTIBLE |
3761 I915_WAIT_LOCKED |
3762 (write ? I915_WAIT_ALL : 0),
Chris Wilson62eb3c22019-02-13 09:25:04 +00003763 MAX_SCHEDULE_TIMEOUT);
Chris Wilson88241782011-01-07 17:09:48 +00003764 if (ret)
3765 return ret;
3766
Chris Wilsonef749212017-04-12 12:01:10 +01003767 flush_write_domain(obj, ~I915_GEM_DOMAIN_CPU);
Eric Anholte47c68e2008-11-14 13:35:19 -08003768
Eric Anholte47c68e2008-11-14 13:35:19 -08003769 /* Flush the CPU cache if it's still invalid. */
Christian Königc0a51fd2018-02-16 13:43:38 +01003770 if ((obj->read_domains & I915_GEM_DOMAIN_CPU) == 0) {
Chris Wilson57822dc2017-02-22 11:40:48 +00003771 i915_gem_clflush_object(obj, I915_CLFLUSH_SYNC);
Christian Königc0a51fd2018-02-16 13:43:38 +01003772 obj->read_domains |= I915_GEM_DOMAIN_CPU;
Eric Anholte47c68e2008-11-14 13:35:19 -08003773 }
3774
3775 /* It should now be out of any other write domains, and we can update
3776 * the domain values for our changes.
3777 */
Christian Königc0a51fd2018-02-16 13:43:38 +01003778 GEM_BUG_ON(obj->write_domain & ~I915_GEM_DOMAIN_CPU);
Eric Anholte47c68e2008-11-14 13:35:19 -08003779
3780 /* If we're writing through the CPU, then the GPU read domains will
3781 * need to be invalidated at next use.
3782 */
Chris Wilsone27ab732017-06-15 13:38:49 +01003783 if (write)
3784 __start_cpu_write(obj);
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003785
3786 return 0;
3787}
3788
Eric Anholt673a3942008-07-30 12:06:12 -07003789/* Throttle our rendering by waiting until the ring has completed our requests
3790 * emitted over 20 msec ago.
3791 *
Eric Anholtb9624422009-06-03 07:27:35 +00003792 * Note that if we were to use the current jiffies each time around the loop,
3793 * we wouldn't escape the function with any frames outstanding if the time to
3794 * render a frame was over 20ms.
3795 *
Eric Anholt673a3942008-07-30 12:06:12 -07003796 * This should get us reasonable parallelism between CPU and GPU but also
3797 * relatively low latency when blocking on a particular request to finish.
3798 */
3799static int
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003800i915_gem_ring_throttle(struct drm_device *dev, struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07003801{
Chris Wilsonfac5e232016-07-04 11:34:36 +01003802 struct drm_i915_private *dev_priv = to_i915(dev);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003803 struct drm_i915_file_private *file_priv = file->driver_priv;
Chris Wilsond0bc54f2015-05-21 21:01:48 +01003804 unsigned long recent_enough = jiffies - DRM_I915_THROTTLE_JIFFIES;
Chris Wilsone61e0f52018-02-21 09:56:36 +00003805 struct i915_request *request, *target = NULL;
Chris Wilsone95433c2016-10-28 13:58:27 +01003806 long ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003807
Chris Wilsonf4457ae2016-04-13 17:35:08 +01003808 /* ABI: return -EIO if already wedged */
Chris Wilsonc41166f2019-02-20 14:56:37 +00003809 ret = i915_terminally_wedged(dev_priv);
3810 if (ret)
3811 return ret;
Chris Wilsone110e8d2011-01-26 15:39:14 +00003812
Chris Wilson1c255952010-09-26 11:03:27 +01003813 spin_lock(&file_priv->mm.lock);
Chris Wilsonc8659ef2017-03-02 12:25:25 +00003814 list_for_each_entry(request, &file_priv->mm.request_list, client_link) {
Eric Anholtb9624422009-06-03 07:27:35 +00003815 if (time_after_eq(request->emitted_jiffies, recent_enough))
3816 break;
3817
Chris Wilsonc8659ef2017-03-02 12:25:25 +00003818 if (target) {
3819 list_del(&target->client_link);
3820 target->file_priv = NULL;
3821 }
John Harrisonfcfa423c2015-05-29 17:44:12 +01003822
John Harrison54fb2412014-11-24 18:49:27 +00003823 target = request;
Eric Anholtb9624422009-06-03 07:27:35 +00003824 }
John Harrisonff865882014-11-24 18:49:28 +00003825 if (target)
Chris Wilsone61e0f52018-02-21 09:56:36 +00003826 i915_request_get(target);
Chris Wilson1c255952010-09-26 11:03:27 +01003827 spin_unlock(&file_priv->mm.lock);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003828
John Harrison54fb2412014-11-24 18:49:27 +00003829 if (target == NULL)
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003830 return 0;
3831
Chris Wilsone61e0f52018-02-21 09:56:36 +00003832 ret = i915_request_wait(target,
Chris Wilsone95433c2016-10-28 13:58:27 +01003833 I915_WAIT_INTERRUPTIBLE,
3834 MAX_SCHEDULE_TIMEOUT);
Chris Wilsone61e0f52018-02-21 09:56:36 +00003835 i915_request_put(target);
John Harrisonff865882014-11-24 18:49:28 +00003836
Chris Wilsone95433c2016-10-28 13:58:27 +01003837 return ret < 0 ? ret : 0;
Eric Anholt673a3942008-07-30 12:06:12 -07003838}
3839
Chris Wilson058d88c2016-08-15 10:49:06 +01003840struct i915_vma *
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02003841i915_gem_object_ggtt_pin(struct drm_i915_gem_object *obj,
3842 const struct i915_ggtt_view *view,
Chris Wilson91b2db62016-08-04 16:32:23 +01003843 u64 size,
Chris Wilson2ffffd02016-08-04 16:32:22 +01003844 u64 alignment,
3845 u64 flags)
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02003846{
Chris Wilsonad16d2e2016-10-13 09:55:04 +01003847 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
Chris Wilson82ad6442018-06-05 16:37:58 +01003848 struct i915_address_space *vm = &dev_priv->ggtt.vm;
Chris Wilson59bfa122016-08-04 16:32:31 +01003849 struct i915_vma *vma;
3850 int ret;
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03003851
Chris Wilson4c7d62c2016-10-28 13:58:32 +01003852 lockdep_assert_held(&obj->base.dev->struct_mutex);
3853
Chris Wilsonac87a6fd2018-02-20 13:42:05 +00003854 if (flags & PIN_MAPPABLE &&
3855 (!view || view->type == I915_GGTT_VIEW_NORMAL)) {
Chris Wilson43ae70d92017-10-09 09:44:01 +01003856 /* If the required space is larger than the available
3857 * aperture, we will not able to find a slot for the
3858 * object and unbinding the object now will be in
3859 * vain. Worse, doing so may cause us to ping-pong
3860 * the object in and out of the Global GTT and
3861 * waste a lot of cycles under the mutex.
3862 */
3863 if (obj->base.size > dev_priv->ggtt.mappable_end)
3864 return ERR_PTR(-E2BIG);
3865
3866 /* If NONBLOCK is set the caller is optimistically
3867 * trying to cache the full object within the mappable
3868 * aperture, and *must* have a fallback in place for
3869 * situations where we cannot bind the object. We
3870 * can be a little more lax here and use the fallback
3871 * more often to avoid costly migrations of ourselves
3872 * and other objects within the aperture.
3873 *
3874 * Half-the-aperture is used as a simple heuristic.
3875 * More interesting would to do search for a free
3876 * block prior to making the commitment to unbind.
3877 * That caters for the self-harm case, and with a
3878 * little more heuristics (e.g. NOFAULT, NOEVICT)
3879 * we could try to minimise harm to others.
3880 */
3881 if (flags & PIN_NONBLOCK &&
3882 obj->base.size > dev_priv->ggtt.mappable_end / 2)
3883 return ERR_PTR(-ENOSPC);
3884 }
3885
Chris Wilson718659a2017-01-16 15:21:28 +00003886 vma = i915_vma_instance(obj, vm, view);
Chengguang Xu772b5402019-02-21 10:08:19 +08003887 if (IS_ERR(vma))
Chris Wilson058d88c2016-08-15 10:49:06 +01003888 return vma;
Chris Wilson59bfa122016-08-04 16:32:31 +01003889
3890 if (i915_vma_misplaced(vma, size, alignment, flags)) {
Chris Wilson43ae70d92017-10-09 09:44:01 +01003891 if (flags & PIN_NONBLOCK) {
3892 if (i915_vma_is_pinned(vma) || i915_vma_is_active(vma))
3893 return ERR_PTR(-ENOSPC);
Chris Wilson59bfa122016-08-04 16:32:31 +01003894
Chris Wilson43ae70d92017-10-09 09:44:01 +01003895 if (flags & PIN_MAPPABLE &&
Chris Wilson944397f2017-01-09 16:16:11 +00003896 vma->fence_size > dev_priv->ggtt.mappable_end / 2)
Chris Wilsonad16d2e2016-10-13 09:55:04 +01003897 return ERR_PTR(-ENOSPC);
3898 }
3899
Chris Wilson59bfa122016-08-04 16:32:31 +01003900 WARN(i915_vma_is_pinned(vma),
3901 "bo is already pinned in ggtt with incorrect alignment:"
Chris Wilson05a20d02016-08-18 17:16:55 +01003902 " offset=%08x, req.alignment=%llx,"
3903 " req.map_and_fenceable=%d, vma->map_and_fenceable=%d\n",
3904 i915_ggtt_offset(vma), alignment,
Chris Wilson59bfa122016-08-04 16:32:31 +01003905 !!(flags & PIN_MAPPABLE),
Chris Wilson05a20d02016-08-18 17:16:55 +01003906 i915_vma_is_map_and_fenceable(vma));
Chris Wilson59bfa122016-08-04 16:32:31 +01003907 ret = i915_vma_unbind(vma);
3908 if (ret)
Chris Wilson058d88c2016-08-15 10:49:06 +01003909 return ERR_PTR(ret);
Chris Wilson59bfa122016-08-04 16:32:31 +01003910 }
3911
Chris Wilson058d88c2016-08-15 10:49:06 +01003912 ret = i915_vma_pin(vma, size, alignment, flags | PIN_GLOBAL);
3913 if (ret)
3914 return ERR_PTR(ret);
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02003915
Chris Wilson058d88c2016-08-15 10:49:06 +01003916 return vma;
Eric Anholt673a3942008-07-30 12:06:12 -07003917}
3918
Chris Wilsonedf6b762016-08-09 09:23:33 +01003919static __always_inline unsigned int __busy_read_flag(unsigned int id)
Chris Wilson3fdc13c2016-08-05 10:14:18 +01003920{
3921 /* Note that we could alias engines in the execbuf API, but
3922 * that would be very unwise as it prevents userspace from
3923 * fine control over engine selection. Ahem.
3924 *
3925 * This should be something like EXEC_MAX_ENGINE instead of
3926 * I915_NUM_ENGINES.
3927 */
3928 BUILD_BUG_ON(I915_NUM_ENGINES > 16);
3929 return 0x10000 << id;
3930}
3931
3932static __always_inline unsigned int __busy_write_id(unsigned int id)
3933{
Chris Wilson70cb4722016-08-09 18:08:25 +01003934 /* The uABI guarantees an active writer is also amongst the read
3935 * engines. This would be true if we accessed the activity tracking
3936 * under the lock, but as we perform the lookup of the object and
3937 * its activity locklessly we can not guarantee that the last_write
3938 * being active implies that we have set the same engine flag from
3939 * last_read - hence we always set both read and write busy for
3940 * last_write.
3941 */
3942 return id | __busy_read_flag(id);
Chris Wilson3fdc13c2016-08-05 10:14:18 +01003943}
3944
Chris Wilsonedf6b762016-08-09 09:23:33 +01003945static __always_inline unsigned int
Chris Wilsond07f0e52016-10-28 13:58:44 +01003946__busy_set_if_active(const struct dma_fence *fence,
Chris Wilson3fdc13c2016-08-05 10:14:18 +01003947 unsigned int (*flag)(unsigned int id))
3948{
Chris Wilsone61e0f52018-02-21 09:56:36 +00003949 struct i915_request *rq;
Chris Wilson12555012016-08-16 09:50:40 +01003950
Chris Wilsond07f0e52016-10-28 13:58:44 +01003951 /* We have to check the current hw status of the fence as the uABI
3952 * guarantees forward progress. We could rely on the idle worker
3953 * to eventually flush us, but to minimise latency just ask the
3954 * hardware.
3955 *
3956 * Note we only report on the status of native fences.
3957 */
3958 if (!dma_fence_is_i915(fence))
Chris Wilson12555012016-08-16 09:50:40 +01003959 return 0;
3960
Chris Wilsond07f0e52016-10-28 13:58:44 +01003961 /* opencode to_request() in order to avoid const warnings */
Chris Wilsone61e0f52018-02-21 09:56:36 +00003962 rq = container_of(fence, struct i915_request, fence);
3963 if (i915_request_completed(rq))
Chris Wilsond07f0e52016-10-28 13:58:44 +01003964 return 0;
3965
Chris Wilson1d39f282017-04-11 13:43:06 +01003966 return flag(rq->engine->uabi_id);
Chris Wilson3fdc13c2016-08-05 10:14:18 +01003967}
3968
Chris Wilsonedf6b762016-08-09 09:23:33 +01003969static __always_inline unsigned int
Chris Wilsond07f0e52016-10-28 13:58:44 +01003970busy_check_reader(const struct dma_fence *fence)
Chris Wilson3fdc13c2016-08-05 10:14:18 +01003971{
Chris Wilsond07f0e52016-10-28 13:58:44 +01003972 return __busy_set_if_active(fence, __busy_read_flag);
Chris Wilson3fdc13c2016-08-05 10:14:18 +01003973}
3974
Chris Wilsonedf6b762016-08-09 09:23:33 +01003975static __always_inline unsigned int
Chris Wilsond07f0e52016-10-28 13:58:44 +01003976busy_check_writer(const struct dma_fence *fence)
Chris Wilson3fdc13c2016-08-05 10:14:18 +01003977{
Chris Wilsond07f0e52016-10-28 13:58:44 +01003978 if (!fence)
3979 return 0;
3980
3981 return __busy_set_if_active(fence, __busy_write_id);
Chris Wilson3fdc13c2016-08-05 10:14:18 +01003982}
3983
Eric Anholt673a3942008-07-30 12:06:12 -07003984int
Eric Anholt673a3942008-07-30 12:06:12 -07003985i915_gem_busy_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00003986 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07003987{
3988 struct drm_i915_gem_busy *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00003989 struct drm_i915_gem_object *obj;
Chris Wilsond07f0e52016-10-28 13:58:44 +01003990 struct reservation_object_list *list;
3991 unsigned int seq;
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01003992 int err;
Eric Anholt673a3942008-07-30 12:06:12 -07003993
Chris Wilsond07f0e52016-10-28 13:58:44 +01003994 err = -ENOENT;
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01003995 rcu_read_lock();
3996 obj = i915_gem_object_lookup_rcu(file, args->handle);
Chris Wilsond07f0e52016-10-28 13:58:44 +01003997 if (!obj)
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01003998 goto out;
Chris Wilsond07f0e52016-10-28 13:58:44 +01003999
4000 /* A discrepancy here is that we do not report the status of
4001 * non-i915 fences, i.e. even though we may report the object as idle,
4002 * a call to set-domain may still stall waiting for foreign rendering.
4003 * This also means that wait-ioctl may report an object as busy,
4004 * where busy-ioctl considers it idle.
4005 *
4006 * We trade the ability to warn of foreign fences to report on which
4007 * i915 engines are active for the object.
4008 *
4009 * Alternatively, we can trade that extra information on read/write
4010 * activity with
4011 * args->busy =
4012 * !reservation_object_test_signaled_rcu(obj->resv, true);
4013 * to report the overall busyness. This is what the wait-ioctl does.
4014 *
4015 */
4016retry:
4017 seq = raw_read_seqcount(&obj->resv->seq);
4018
4019 /* Translate the exclusive fence to the READ *and* WRITE engine */
4020 args->busy = busy_check_writer(rcu_dereference(obj->resv->fence_excl));
4021
4022 /* Translate shared fences to READ set of engines */
4023 list = rcu_dereference(obj->resv->fence);
4024 if (list) {
4025 unsigned int shared_count = list->shared_count, i;
4026
4027 for (i = 0; i < shared_count; ++i) {
4028 struct dma_fence *fence =
4029 rcu_dereference(list->shared[i]);
4030
4031 args->busy |= busy_check_reader(fence);
4032 }
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004033 }
Zou Nan haid1b851f2010-05-21 09:08:57 +08004034
Chris Wilsond07f0e52016-10-28 13:58:44 +01004035 if (args->busy && read_seqcount_retry(&obj->resv->seq, seq))
4036 goto retry;
Chris Wilson426960b2016-01-15 16:51:46 +00004037
Chris Wilsond07f0e52016-10-28 13:58:44 +01004038 err = 0;
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004039out:
4040 rcu_read_unlock();
4041 return err;
Eric Anholt673a3942008-07-30 12:06:12 -07004042}
4043
4044int
4045i915_gem_throttle_ioctl(struct drm_device *dev, void *data,
4046 struct drm_file *file_priv)
4047{
Akshay Joshi0206e352011-08-16 15:34:10 -04004048 return i915_gem_ring_throttle(dev, file_priv);
Eric Anholt673a3942008-07-30 12:06:12 -07004049}
4050
Chris Wilson3ef94da2009-09-14 16:50:29 +01004051int
4052i915_gem_madvise_ioctl(struct drm_device *dev, void *data,
4053 struct drm_file *file_priv)
4054{
Chris Wilsonfac5e232016-07-04 11:34:36 +01004055 struct drm_i915_private *dev_priv = to_i915(dev);
Chris Wilson3ef94da2009-09-14 16:50:29 +01004056 struct drm_i915_gem_madvise *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00004057 struct drm_i915_gem_object *obj;
Chris Wilson1233e2d2016-10-28 13:58:37 +01004058 int err;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004059
4060 switch (args->madv) {
4061 case I915_MADV_DONTNEED:
4062 case I915_MADV_WILLNEED:
4063 break;
4064 default:
4065 return -EINVAL;
4066 }
4067
Chris Wilson03ac0642016-07-20 13:31:51 +01004068 obj = i915_gem_object_lookup(file_priv, args->handle);
Chris Wilson1233e2d2016-10-28 13:58:37 +01004069 if (!obj)
4070 return -ENOENT;
4071
4072 err = mutex_lock_interruptible(&obj->mm.lock);
4073 if (err)
4074 goto out;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004075
Chris Wilsonf1fa4f42017-10-13 21:26:13 +01004076 if (i915_gem_object_has_pages(obj) &&
Chris Wilson3e510a82016-08-05 10:14:23 +01004077 i915_gem_object_is_tiled(obj) &&
Daniel Vetter656bfa32014-11-20 09:26:30 +01004078 dev_priv->quirks & QUIRK_PIN_SWIZZLED_PAGES) {
Chris Wilsonbc0629a2016-11-01 10:03:17 +00004079 if (obj->mm.madv == I915_MADV_WILLNEED) {
4080 GEM_BUG_ON(!obj->mm.quirked);
Chris Wilsona4f5ea62016-10-28 13:58:35 +01004081 __i915_gem_object_unpin_pages(obj);
Chris Wilsonbc0629a2016-11-01 10:03:17 +00004082 obj->mm.quirked = false;
4083 }
4084 if (args->madv == I915_MADV_WILLNEED) {
Chris Wilson2c3a3f42016-11-04 10:30:01 +00004085 GEM_BUG_ON(obj->mm.quirked);
Chris Wilsona4f5ea62016-10-28 13:58:35 +01004086 __i915_gem_object_pin_pages(obj);
Chris Wilsonbc0629a2016-11-01 10:03:17 +00004087 obj->mm.quirked = true;
4088 }
Daniel Vetter656bfa32014-11-20 09:26:30 +01004089 }
4090
Chris Wilsona4f5ea62016-10-28 13:58:35 +01004091 if (obj->mm.madv != __I915_MADV_PURGED)
4092 obj->mm.madv = args->madv;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004093
Chris Wilson6c085a72012-08-20 11:40:46 +02004094 /* if the object is no longer attached, discard its backing storage */
Chris Wilsonf1fa4f42017-10-13 21:26:13 +01004095 if (obj->mm.madv == I915_MADV_DONTNEED &&
4096 !i915_gem_object_has_pages(obj))
Chris Wilson2d7ef392009-09-20 23:13:10 +01004097 i915_gem_object_truncate(obj);
4098
Chris Wilsona4f5ea62016-10-28 13:58:35 +01004099 args->retained = obj->mm.madv != __I915_MADV_PURGED;
Chris Wilson1233e2d2016-10-28 13:58:37 +01004100 mutex_unlock(&obj->mm.lock);
Chris Wilsonbb6baf72009-09-22 14:24:13 +01004101
Chris Wilson1233e2d2016-10-28 13:58:37 +01004102out:
Chris Wilsonf8c417c2016-07-20 13:31:53 +01004103 i915_gem_object_put(obj);
Chris Wilson1233e2d2016-10-28 13:58:37 +01004104 return err;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004105}
4106
Chris Wilson5b8c8ae2016-11-16 19:07:04 +00004107static void
Chris Wilson21950ee2019-02-05 13:00:05 +00004108frontbuffer_retire(struct i915_active_request *active,
4109 struct i915_request *request)
Chris Wilson5b8c8ae2016-11-16 19:07:04 +00004110{
4111 struct drm_i915_gem_object *obj =
4112 container_of(active, typeof(*obj), frontbuffer_write);
4113
Chris Wilsond59b21e2017-02-22 11:40:49 +00004114 intel_fb_obj_flush(obj, ORIGIN_CS);
Chris Wilson5b8c8ae2016-11-16 19:07:04 +00004115}
4116
Chris Wilson37e680a2012-06-07 15:38:42 +01004117void i915_gem_object_init(struct drm_i915_gem_object *obj,
4118 const struct drm_i915_gem_object_ops *ops)
Chris Wilson0327d6b2012-08-11 15:41:06 +01004119{
Chris Wilson1233e2d2016-10-28 13:58:37 +01004120 mutex_init(&obj->mm.lock);
4121
Chris Wilson528cbd12019-01-28 10:23:54 +00004122 spin_lock_init(&obj->vma.lock);
4123 INIT_LIST_HEAD(&obj->vma.list);
4124
Chris Wilsond1b48c12017-08-16 09:52:08 +01004125 INIT_LIST_HEAD(&obj->lut_list);
Chris Wilson8d9d5742015-04-07 16:20:38 +01004126 INIT_LIST_HEAD(&obj->batch_pool_link);
Chris Wilson0327d6b2012-08-11 15:41:06 +01004127
Chris Wilson8811d612018-11-09 09:03:11 +00004128 init_rcu_head(&obj->rcu);
4129
Chris Wilson37e680a2012-06-07 15:38:42 +01004130 obj->ops = ops;
4131
Chris Wilsond07f0e52016-10-28 13:58:44 +01004132 reservation_object_init(&obj->__builtin_resv);
4133 obj->resv = &obj->__builtin_resv;
4134
Chris Wilson50349242016-08-18 17:17:04 +01004135 obj->frontbuffer_ggtt_origin = ORIGIN_GTT;
Chris Wilson21950ee2019-02-05 13:00:05 +00004136 i915_active_request_init(&obj->frontbuffer_write,
4137 NULL, frontbuffer_retire);
Chris Wilsona4f5ea62016-10-28 13:58:35 +01004138
4139 obj->mm.madv = I915_MADV_WILLNEED;
4140 INIT_RADIX_TREE(&obj->mm.get_page.radix, GFP_KERNEL | __GFP_NOWARN);
4141 mutex_init(&obj->mm.get_page.lock);
Chris Wilson0327d6b2012-08-11 15:41:06 +01004142
Dave Gordonf19ec8c2016-07-04 11:34:37 +01004143 i915_gem_info_add_obj(to_i915(obj->base.dev), obj->base.size);
Chris Wilson0327d6b2012-08-11 15:41:06 +01004144}
4145
Chris Wilson37e680a2012-06-07 15:38:42 +01004146static const struct drm_i915_gem_object_ops i915_gem_object_ops = {
Tvrtko Ursulin3599a912016-11-01 14:44:10 +00004147 .flags = I915_GEM_OBJECT_HAS_STRUCT_PAGE |
4148 I915_GEM_OBJECT_IS_SHRINKABLE,
Chris Wilson7c55e2c2017-03-07 12:03:38 +00004149
Chris Wilson37e680a2012-06-07 15:38:42 +01004150 .get_pages = i915_gem_object_get_pages_gtt,
4151 .put_pages = i915_gem_object_put_pages_gtt,
Chris Wilson7c55e2c2017-03-07 12:03:38 +00004152
4153 .pwrite = i915_gem_object_pwrite_gtt,
Chris Wilson37e680a2012-06-07 15:38:42 +01004154};
4155
Matthew Auld465c4032017-10-06 23:18:14 +01004156static int i915_gem_object_create_shmem(struct drm_device *dev,
4157 struct drm_gem_object *obj,
4158 size_t size)
4159{
4160 struct drm_i915_private *i915 = to_i915(dev);
4161 unsigned long flags = VM_NORESERVE;
4162 struct file *filp;
4163
4164 drm_gem_private_object_init(dev, obj, size);
4165
4166 if (i915->mm.gemfs)
4167 filp = shmem_file_setup_with_mnt(i915->mm.gemfs, "i915", size,
4168 flags);
4169 else
4170 filp = shmem_file_setup("i915", size, flags);
4171
4172 if (IS_ERR(filp))
4173 return PTR_ERR(filp);
4174
4175 obj->filp = filp;
4176
4177 return 0;
4178}
4179
Chris Wilsonb4bcbe22016-10-18 13:02:49 +01004180struct drm_i915_gem_object *
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +00004181i915_gem_object_create(struct drm_i915_private *dev_priv, u64 size)
Daniel Vetterac52bc52010-04-09 19:05:06 +00004182{
Daniel Vetterc397b902010-04-09 19:05:07 +00004183 struct drm_i915_gem_object *obj;
Hugh Dickins5949eac2011-06-27 16:18:18 -07004184 struct address_space *mapping;
Chris Wilsonb8f55be2017-08-11 12:11:16 +01004185 unsigned int cache_level;
Daniel Vetter1a240d42012-11-29 22:18:51 +01004186 gfp_t mask;
Chris Wilsonfe3db792016-04-25 13:32:13 +01004187 int ret;
Daniel Vetterc397b902010-04-09 19:05:07 +00004188
Chris Wilsonb4bcbe22016-10-18 13:02:49 +01004189 /* There is a prevalence of the assumption that we fit the object's
4190 * page count inside a 32bit _signed_ variable. Let's document this and
4191 * catch if we ever need to fix it. In the meantime, if you do spot
4192 * such a local variable, please consider fixing!
4193 */
Tvrtko Ursulin7a3ee5d2017-03-30 17:31:30 +01004194 if (size >> PAGE_SHIFT > INT_MAX)
Chris Wilsonb4bcbe22016-10-18 13:02:49 +01004195 return ERR_PTR(-E2BIG);
4196
4197 if (overflows_type(size, obj->base.size))
4198 return ERR_PTR(-E2BIG);
4199
Tvrtko Ursulin187685c2016-12-01 14:16:36 +00004200 obj = i915_gem_object_alloc(dev_priv);
Daniel Vetterc397b902010-04-09 19:05:07 +00004201 if (obj == NULL)
Chris Wilsonfe3db792016-04-25 13:32:13 +01004202 return ERR_PTR(-ENOMEM);
Daniel Vetterc397b902010-04-09 19:05:07 +00004203
Matthew Auld465c4032017-10-06 23:18:14 +01004204 ret = i915_gem_object_create_shmem(&dev_priv->drm, &obj->base, size);
Chris Wilsonfe3db792016-04-25 13:32:13 +01004205 if (ret)
4206 goto fail;
Daniel Vetterc397b902010-04-09 19:05:07 +00004207
Chris Wilsonbed1ea92012-05-24 20:48:12 +01004208 mask = GFP_HIGHUSER | __GFP_RECLAIMABLE;
Jani Nikulac0f86832016-12-07 12:13:04 +02004209 if (IS_I965GM(dev_priv) || IS_I965G(dev_priv)) {
Chris Wilsonbed1ea92012-05-24 20:48:12 +01004210 /* 965gm cannot relocate objects above 4GiB. */
4211 mask &= ~__GFP_HIGHMEM;
4212 mask |= __GFP_DMA32;
4213 }
4214
Al Viro93c76a32015-12-04 23:45:44 -05004215 mapping = obj->base.filp->f_mapping;
Chris Wilsonbed1ea92012-05-24 20:48:12 +01004216 mapping_set_gfp_mask(mapping, mask);
Chris Wilson4846bf02017-06-09 12:03:46 +01004217 GEM_BUG_ON(!(mapping_gfp_mask(mapping) & __GFP_RECLAIM));
Hugh Dickins5949eac2011-06-27 16:18:18 -07004218
Chris Wilson37e680a2012-06-07 15:38:42 +01004219 i915_gem_object_init(obj, &i915_gem_object_ops);
Chris Wilson73aa8082010-09-30 11:46:12 +01004220
Christian Königc0a51fd2018-02-16 13:43:38 +01004221 obj->write_domain = I915_GEM_DOMAIN_CPU;
4222 obj->read_domains = I915_GEM_DOMAIN_CPU;
Daniel Vetterc397b902010-04-09 19:05:07 +00004223
Chris Wilsonb8f55be2017-08-11 12:11:16 +01004224 if (HAS_LLC(dev_priv))
Eugeni Dodonov3d29b842012-01-17 14:43:53 -02004225 /* On some devices, we can have the GPU use the LLC (the CPU
Eric Anholta1871112011-03-29 16:59:55 -07004226 * cache) for about a 10% performance improvement
4227 * compared to uncached. Graphics requests other than
4228 * display scanout are coherent with the CPU in
4229 * accessing this cache. This means in this mode we
4230 * don't need to clflush on the CPU side, and on the
4231 * GPU side we only need to flush internal caches to
4232 * get data visible to the CPU.
4233 *
4234 * However, we maintain the display planes as UC, and so
4235 * need to rebind when first used as such.
4236 */
Chris Wilsonb8f55be2017-08-11 12:11:16 +01004237 cache_level = I915_CACHE_LLC;
4238 else
4239 cache_level = I915_CACHE_NONE;
Eric Anholta1871112011-03-29 16:59:55 -07004240
Chris Wilsonb8f55be2017-08-11 12:11:16 +01004241 i915_gem_object_set_cache_coherency(obj, cache_level);
Chris Wilsone27ab732017-06-15 13:38:49 +01004242
Daniel Vetterd861e332013-07-24 23:25:03 +02004243 trace_i915_gem_object_create(obj);
4244
Chris Wilson05394f32010-11-08 19:18:58 +00004245 return obj;
Chris Wilsonfe3db792016-04-25 13:32:13 +01004246
4247fail:
4248 i915_gem_object_free(obj);
Chris Wilsonfe3db792016-04-25 13:32:13 +01004249 return ERR_PTR(ret);
Daniel Vetterac52bc52010-04-09 19:05:06 +00004250}
4251
Chris Wilson340fbd82014-05-22 09:16:52 +01004252static bool discard_backing_storage(struct drm_i915_gem_object *obj)
4253{
4254 /* If we are the last user of the backing storage (be it shmemfs
4255 * pages or stolen etc), we know that the pages are going to be
4256 * immediately released. In this case, we can then skip copying
4257 * back the contents from the GPU.
4258 */
4259
Chris Wilsona4f5ea62016-10-28 13:58:35 +01004260 if (obj->mm.madv != I915_MADV_WILLNEED)
Chris Wilson340fbd82014-05-22 09:16:52 +01004261 return false;
4262
4263 if (obj->base.filp == NULL)
4264 return true;
4265
4266 /* At first glance, this looks racy, but then again so would be
4267 * userspace racing mmap against close. However, the first external
4268 * reference to the filp can only be obtained through the
4269 * i915_gem_mmap_ioctl() which safeguards us against the user
4270 * acquiring such a reference whilst we are in the middle of
4271 * freeing the object.
4272 */
4273 return atomic_long_read(&obj->base.filp->f_count) == 1;
4274}
4275
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004276static void __i915_gem_free_objects(struct drm_i915_private *i915,
4277 struct llist_node *freed)
Chris Wilsonbe726152010-07-23 23:18:50 +01004278{
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004279 struct drm_i915_gem_object *obj, *on;
Chris Wilson538ef962019-01-14 14:21:18 +00004280 intel_wakeref_t wakeref;
Chris Wilsonbe726152010-07-23 23:18:50 +01004281
Chris Wilson538ef962019-01-14 14:21:18 +00004282 wakeref = intel_runtime_pm_get(i915);
Chris Wilsoncc731f52017-10-13 21:26:21 +01004283 llist_for_each_entry_safe(obj, on, freed, freed) {
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004284 struct i915_vma *vma, *vn;
Paulo Zanonif65c9162013-11-27 18:20:34 -02004285
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004286 trace_i915_gem_object_destroy(obj);
4287
Chris Wilsoncc731f52017-10-13 21:26:21 +01004288 mutex_lock(&i915->drm.struct_mutex);
4289
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004290 GEM_BUG_ON(i915_gem_object_is_active(obj));
Chris Wilson528cbd12019-01-28 10:23:54 +00004291 list_for_each_entry_safe(vma, vn, &obj->vma.list, obj_link) {
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004292 GEM_BUG_ON(i915_vma_is_active(vma));
4293 vma->flags &= ~I915_VMA_PIN_MASK;
Chris Wilson3365e222018-05-03 20:51:14 +01004294 i915_vma_destroy(vma);
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004295 }
Chris Wilson528cbd12019-01-28 10:23:54 +00004296 GEM_BUG_ON(!list_empty(&obj->vma.list));
4297 GEM_BUG_ON(!RB_EMPTY_ROOT(&obj->vma.tree));
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004298
Chris Wilsonf2123812017-10-16 12:40:37 +01004299 /* This serializes freeing with the shrinker. Since the free
4300 * is delayed, first by RCU then by the workqueue, we want the
4301 * shrinker to be able to free pages of unreferenced objects,
4302 * or else we may oom whilst there are plenty of deferred
4303 * freed objects.
4304 */
4305 if (i915_gem_object_has_pages(obj)) {
4306 spin_lock(&i915->mm.obj_lock);
4307 list_del_init(&obj->mm.link);
4308 spin_unlock(&i915->mm.obj_lock);
4309 }
4310
Chris Wilsoncc731f52017-10-13 21:26:21 +01004311 mutex_unlock(&i915->drm.struct_mutex);
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004312
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004313 GEM_BUG_ON(obj->bind_count);
Chris Wilsona65adaf2017-10-09 09:43:57 +01004314 GEM_BUG_ON(obj->userfault_count);
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004315 GEM_BUG_ON(atomic_read(&obj->frontbuffer_bits));
Chris Wilson67b48042017-08-22 12:05:16 +01004316 GEM_BUG_ON(!list_empty(&obj->lut_list));
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004317
4318 if (obj->ops->release)
4319 obj->ops->release(obj);
4320
4321 if (WARN_ON(i915_gem_object_has_pinned_pages(obj)))
4322 atomic_set(&obj->mm.pages_pin_count, 0);
Chris Wilson548625e2016-11-01 12:11:34 +00004323 __i915_gem_object_put_pages(obj, I915_MM_NORMAL);
Chris Wilsonf1fa4f42017-10-13 21:26:13 +01004324 GEM_BUG_ON(i915_gem_object_has_pages(obj));
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004325
4326 if (obj->base.import_attach)
4327 drm_prime_gem_destroy(&obj->base, NULL);
4328
Chris Wilsond07f0e52016-10-28 13:58:44 +01004329 reservation_object_fini(&obj->__builtin_resv);
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004330 drm_gem_object_release(&obj->base);
4331 i915_gem_info_remove_obj(i915, obj->base.size);
4332
4333 kfree(obj->bit_17);
4334 i915_gem_object_free(obj);
Chris Wilsoncc731f52017-10-13 21:26:21 +01004335
Chris Wilsonc9c704712018-02-19 22:06:31 +00004336 GEM_BUG_ON(!atomic_read(&i915->mm.free_count));
4337 atomic_dec(&i915->mm.free_count);
4338
Chris Wilsoncc731f52017-10-13 21:26:21 +01004339 if (on)
4340 cond_resched();
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004341 }
Chris Wilson538ef962019-01-14 14:21:18 +00004342 intel_runtime_pm_put(i915, wakeref);
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004343}
4344
4345static void i915_gem_flush_free_objects(struct drm_i915_private *i915)
4346{
4347 struct llist_node *freed;
4348
Chris Wilson87701b42017-10-13 21:26:20 +01004349 /* Free the oldest, most stale object to keep the free_list short */
4350 freed = NULL;
4351 if (!llist_empty(&i915->mm.free_list)) { /* quick test for hotpath */
4352 /* Only one consumer of llist_del_first() allowed */
4353 spin_lock(&i915->mm.free_lock);
4354 freed = llist_del_first(&i915->mm.free_list);
4355 spin_unlock(&i915->mm.free_lock);
4356 }
4357 if (unlikely(freed)) {
4358 freed->next = NULL;
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004359 __i915_gem_free_objects(i915, freed);
Chris Wilson87701b42017-10-13 21:26:20 +01004360 }
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004361}
4362
4363static void __i915_gem_free_work(struct work_struct *work)
4364{
4365 struct drm_i915_private *i915 =
4366 container_of(work, struct drm_i915_private, mm.free_work);
4367 struct llist_node *freed;
Chris Wilson26e12f82011-03-20 11:20:19 +00004368
Chris Wilson2ef1e722018-01-15 20:57:59 +00004369 /*
4370 * All file-owned VMA should have been released by this point through
Chris Wilsonb1f788c2016-08-04 07:52:45 +01004371 * i915_gem_close_object(), or earlier by i915_gem_context_close().
4372 * However, the object may also be bound into the global GTT (e.g.
4373 * older GPUs without per-process support, or for direct access through
4374 * the GTT either for the user or for scanout). Those VMA still need to
4375 * unbound now.
4376 */
Chris Wilson1488fc02012-04-24 15:47:31 +01004377
Chris Wilsonf991c492017-11-06 11:15:08 +00004378 spin_lock(&i915->mm.free_lock);
Chris Wilson5ad08be2017-04-07 11:25:51 +01004379 while ((freed = llist_del_all(&i915->mm.free_list))) {
Chris Wilsonf991c492017-11-06 11:15:08 +00004380 spin_unlock(&i915->mm.free_lock);
4381
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004382 __i915_gem_free_objects(i915, freed);
Chris Wilson5ad08be2017-04-07 11:25:51 +01004383 if (need_resched())
Chris Wilsonf991c492017-11-06 11:15:08 +00004384 return;
4385
4386 spin_lock(&i915->mm.free_lock);
Chris Wilson5ad08be2017-04-07 11:25:51 +01004387 }
Chris Wilsonf991c492017-11-06 11:15:08 +00004388 spin_unlock(&i915->mm.free_lock);
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004389}
4390
4391static void __i915_gem_free_object_rcu(struct rcu_head *head)
4392{
4393 struct drm_i915_gem_object *obj =
4394 container_of(head, typeof(*obj), rcu);
4395 struct drm_i915_private *i915 = to_i915(obj->base.dev);
4396
Chris Wilson2ef1e722018-01-15 20:57:59 +00004397 /*
Chris Wilson8811d612018-11-09 09:03:11 +00004398 * We reuse obj->rcu for the freed list, so we had better not treat
4399 * it like a rcu_head from this point forwards. And we expect all
4400 * objects to be freed via this path.
4401 */
4402 destroy_rcu_head(&obj->rcu);
4403
4404 /*
Chris Wilson2ef1e722018-01-15 20:57:59 +00004405 * Since we require blocking on struct_mutex to unbind the freed
4406 * object from the GPU before releasing resources back to the
4407 * system, we can not do that directly from the RCU callback (which may
4408 * be a softirq context), but must instead then defer that work onto a
4409 * kthread. We use the RCU callback rather than move the freed object
4410 * directly onto the work queue so that we can mix between using the
4411 * worker and performing frees directly from subsequent allocations for
4412 * crude but effective memory throttling.
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004413 */
4414 if (llist_add(&obj->freed, &i915->mm.free_list))
Chris Wilsonbeacbd12018-01-15 12:28:45 +00004415 queue_work(i915->wq, &i915->mm.free_work);
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004416}
4417
4418void i915_gem_free_object(struct drm_gem_object *gem_obj)
4419{
4420 struct drm_i915_gem_object *obj = to_intel_bo(gem_obj);
4421
Chris Wilsonbc0629a2016-11-01 10:03:17 +00004422 if (obj->mm.quirked)
4423 __i915_gem_object_unpin_pages(obj);
4424
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004425 if (discard_backing_storage(obj))
4426 obj->mm.madv = I915_MADV_DONTNEED;
Daniel Vettera071fa02014-06-18 23:28:09 +02004427
Chris Wilson2ef1e722018-01-15 20:57:59 +00004428 /*
4429 * Before we free the object, make sure any pure RCU-only
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004430 * read-side critical sections are complete, e.g.
4431 * i915_gem_busy_ioctl(). For the corresponding synchronized
4432 * lookup see i915_gem_object_lookup_rcu().
4433 */
Chris Wilsonc9c704712018-02-19 22:06:31 +00004434 atomic_inc(&to_i915(obj->base.dev)->mm.free_count);
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004435 call_rcu(&obj->rcu, __i915_gem_free_object_rcu);
Chris Wilsonbe726152010-07-23 23:18:50 +01004436}
4437
Chris Wilsonf8a7fde2016-10-28 13:58:29 +01004438void __i915_gem_object_release_unless_active(struct drm_i915_gem_object *obj)
4439{
4440 lockdep_assert_held(&obj->base.dev->struct_mutex);
4441
Chris Wilsond1b48c12017-08-16 09:52:08 +01004442 if (!i915_gem_object_has_active_reference(obj) &&
4443 i915_gem_object_is_active(obj))
Chris Wilsonf8a7fde2016-10-28 13:58:29 +01004444 i915_gem_object_set_active_reference(obj);
4445 else
4446 i915_gem_object_put(obj);
4447}
4448
Chris Wilson24145512017-01-24 11:01:35 +00004449void i915_gem_sanitize(struct drm_i915_private *i915)
4450{
Chris Wilson538ef962019-01-14 14:21:18 +00004451 intel_wakeref_t wakeref;
4452
Chris Wilsonc3160da2018-05-31 09:22:45 +01004453 GEM_TRACE("\n");
4454
Chris Wilson538ef962019-01-14 14:21:18 +00004455 wakeref = intel_runtime_pm_get(i915);
Chris Wilsonc3160da2018-05-31 09:22:45 +01004456 intel_uncore_forcewake_get(i915, FORCEWAKE_ALL);
4457
4458 /*
4459 * As we have just resumed the machine and woken the device up from
4460 * deep PCI sleep (presumably D3_cold), assume the HW has been reset
4461 * back to defaults, recovering from whatever wedged state we left it
4462 * in and so worth trying to use the device once more.
4463 */
Chris Wilsonc41166f2019-02-20 14:56:37 +00004464 if (i915_terminally_wedged(i915))
Chris Wilsonf36325f2017-08-26 12:09:34 +01004465 i915_gem_unset_wedged(i915);
Chris Wilsonf36325f2017-08-26 12:09:34 +01004466
Chris Wilson24145512017-01-24 11:01:35 +00004467 /*
4468 * If we inherit context state from the BIOS or earlier occupants
4469 * of the GPU, the GPU may be in an inconsistent state when we
4470 * try to take over. The only way to remove the earlier state
4471 * is by resetting. However, resetting on earlier gen is tricky as
4472 * it may impact the display and we are uncertain about the stability
Joonas Lahtinenea117b82017-04-28 10:53:38 +03004473 * of the reset, so this could be applied to even earlier gen.
Chris Wilson24145512017-01-24 11:01:35 +00004474 */
Chris Wilson55277e12019-01-03 11:21:04 +00004475 intel_engines_sanitize(i915, false);
Chris Wilsonc3160da2018-05-31 09:22:45 +01004476
4477 intel_uncore_forcewake_put(i915, FORCEWAKE_ALL);
Chris Wilson538ef962019-01-14 14:21:18 +00004478 intel_runtime_pm_put(i915, wakeref);
Chris Wilsonc3160da2018-05-31 09:22:45 +01004479
Chris Wilsoneb8d0f52019-01-25 13:22:28 +00004480 mutex_lock(&i915->drm.struct_mutex);
Chris Wilson4dfacb02018-05-31 09:22:43 +01004481 i915_gem_contexts_lost(i915);
4482 mutex_unlock(&i915->drm.struct_mutex);
Chris Wilson24145512017-01-24 11:01:35 +00004483}
4484
Chris Wilsonbf061122018-07-09 14:02:04 +01004485int i915_gem_suspend(struct drm_i915_private *i915)
Eric Anholt673a3942008-07-30 12:06:12 -07004486{
Chris Wilson538ef962019-01-14 14:21:18 +00004487 intel_wakeref_t wakeref;
Chris Wilsondcff85c2016-08-05 10:14:11 +01004488 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004489
Chris Wilson09a4c022018-05-24 09:11:35 +01004490 GEM_TRACE("\n");
4491
Chris Wilson538ef962019-01-14 14:21:18 +00004492 wakeref = intel_runtime_pm_get(i915);
Chris Wilsonbf061122018-07-09 14:02:04 +01004493 intel_suspend_gt_powersave(i915);
Chris Wilson54b4f682016-07-21 21:16:19 +01004494
Chris Wilsoneb8d0f52019-01-25 13:22:28 +00004495 flush_workqueue(i915->wq);
4496
Chris Wilsonbf061122018-07-09 14:02:04 +01004497 mutex_lock(&i915->drm.struct_mutex);
Chris Wilson5ab57c72016-07-15 14:56:20 +01004498
Chris Wilsonbf061122018-07-09 14:02:04 +01004499 /*
4500 * We have to flush all the executing contexts to main memory so
Chris Wilson5ab57c72016-07-15 14:56:20 +01004501 * that they can saved in the hibernation image. To ensure the last
4502 * context image is coherent, we have to switch away from it. That
Chris Wilsonbf061122018-07-09 14:02:04 +01004503 * leaves the i915->kernel_context still active when
Chris Wilson5ab57c72016-07-15 14:56:20 +01004504 * we actually suspend, and its image in memory may not match the GPU
4505 * state. Fortunately, the kernel_context is disposable and we do
4506 * not rely on its state.
4507 */
Chris Wilsonc41166f2019-02-20 14:56:37 +00004508 if (!i915_reset_failed(i915)) {
Chris Wilsonbf061122018-07-09 14:02:04 +01004509 ret = i915_gem_switch_to_kernel_context(i915);
Chris Wilsonecf73eb2017-11-30 10:29:51 +00004510 if (ret)
4511 goto err_unlock;
Chris Wilson5ab57c72016-07-15 14:56:20 +01004512
Chris Wilsonbf061122018-07-09 14:02:04 +01004513 ret = i915_gem_wait_for_idle(i915,
Chris Wilsonecf73eb2017-11-30 10:29:51 +00004514 I915_WAIT_INTERRUPTIBLE |
Chris Wilson06060352018-05-31 09:22:44 +01004515 I915_WAIT_LOCKED |
Chris Wilsonec625fb2018-07-09 13:20:42 +01004516 I915_WAIT_FOR_IDLE_BOOST,
4517 MAX_SCHEDULE_TIMEOUT);
Chris Wilsonecf73eb2017-11-30 10:29:51 +00004518 if (ret && ret != -EIO)
4519 goto err_unlock;
Chris Wilsonf7403342013-09-13 23:57:04 +01004520
Chris Wilsonbf061122018-07-09 14:02:04 +01004521 assert_kernel_context_is_current(i915);
Chris Wilsonecf73eb2017-11-30 10:29:51 +00004522 }
Chris Wilson01f8f332018-07-17 09:41:21 +01004523 i915_retire_requests(i915); /* ensure we flush after wedging */
4524
Chris Wilsonbf061122018-07-09 14:02:04 +01004525 mutex_unlock(&i915->drm.struct_mutex);
Chris Wilsoneb8d0f52019-01-25 13:22:28 +00004526 i915_reset_flush(i915);
Chris Wilson45c5f202013-10-16 11:50:01 +01004527
Chris Wilsoneb8d0f52019-01-25 13:22:28 +00004528 drain_delayed_work(&i915->gt.retire_work);
Chris Wilsonbdeb9782016-12-23 14:57:56 +00004529
Chris Wilsonbf061122018-07-09 14:02:04 +01004530 /*
4531 * As the idle_work is rearming if it detects a race, play safe and
Chris Wilsonbdeb9782016-12-23 14:57:56 +00004532 * repeat the flush until it is definitely idle.
4533 */
Chris Wilsonbf061122018-07-09 14:02:04 +01004534 drain_delayed_work(&i915->gt.idle_work);
Chris Wilsonbdeb9782016-12-23 14:57:56 +00004535
Chris Wilsoneb8d0f52019-01-25 13:22:28 +00004536 intel_uc_suspend(i915);
4537
Chris Wilsonbf061122018-07-09 14:02:04 +01004538 /*
4539 * Assert that we successfully flushed all the work and
Chris Wilsonbdcf1202014-11-25 11:56:33 +00004540 * reset the GPU back to its idle, low power state.
4541 */
Chris Wilsonbf061122018-07-09 14:02:04 +01004542 WARN_ON(i915->gt.awake);
4543 if (WARN_ON(!intel_engines_are_idle(i915)))
4544 i915_gem_set_wedged(i915); /* no hope, discard everything */
Chris Wilsonbdcf1202014-11-25 11:56:33 +00004545
Chris Wilson538ef962019-01-14 14:21:18 +00004546 intel_runtime_pm_put(i915, wakeref);
Chris Wilsonec92ad02018-05-31 09:22:46 +01004547 return 0;
4548
4549err_unlock:
Chris Wilsonbf061122018-07-09 14:02:04 +01004550 mutex_unlock(&i915->drm.struct_mutex);
Chris Wilson538ef962019-01-14 14:21:18 +00004551 intel_runtime_pm_put(i915, wakeref);
Chris Wilsonec92ad02018-05-31 09:22:46 +01004552 return ret;
4553}
4554
4555void i915_gem_suspend_late(struct drm_i915_private *i915)
4556{
Chris Wilson9776f472018-06-01 15:41:24 +01004557 struct drm_i915_gem_object *obj;
4558 struct list_head *phases[] = {
4559 &i915->mm.unbound_list,
4560 &i915->mm.bound_list,
4561 NULL
4562 }, **phase;
4563
Imre Deak1c777c52016-10-12 17:46:37 +03004564 /*
4565 * Neither the BIOS, ourselves or any other kernel
4566 * expects the system to be in execlists mode on startup,
4567 * so we need to reset the GPU back to legacy mode. And the only
4568 * known way to disable logical contexts is through a GPU reset.
4569 *
4570 * So in order to leave the system in a known default configuration,
4571 * always reset the GPU upon unload and suspend. Afterwards we then
4572 * clean up the GEM state tracking, flushing off the requests and
4573 * leaving the system in a known idle state.
4574 *
4575 * Note that is of the upmost importance that the GPU is idle and
4576 * all stray writes are flushed *before* we dismantle the backing
4577 * storage for the pinned objects.
4578 *
4579 * However, since we are uncertain that resetting the GPU on older
4580 * machines is a good idea, we don't - just in case it leaves the
4581 * machine in an unusable condition.
4582 */
Chris Wilsoncad99462017-08-26 12:09:33 +01004583
Chris Wilson9776f472018-06-01 15:41:24 +01004584 mutex_lock(&i915->drm.struct_mutex);
4585 for (phase = phases; *phase; phase++) {
4586 list_for_each_entry(obj, *phase, mm.link)
4587 WARN_ON(i915_gem_object_set_to_gtt_domain(obj, false));
4588 }
4589 mutex_unlock(&i915->drm.struct_mutex);
4590
Chris Wilsonec92ad02018-05-31 09:22:46 +01004591 intel_uc_sanitize(i915);
4592 i915_gem_sanitize(i915);
Eric Anholt673a3942008-07-30 12:06:12 -07004593}
4594
Chris Wilson37cd3302017-11-12 11:27:38 +00004595void i915_gem_resume(struct drm_i915_private *i915)
Chris Wilson5ab57c72016-07-15 14:56:20 +01004596{
Chris Wilson4dfacb02018-05-31 09:22:43 +01004597 GEM_TRACE("\n");
4598
Chris Wilson37cd3302017-11-12 11:27:38 +00004599 WARN_ON(i915->gt.awake);
Chris Wilson5ab57c72016-07-15 14:56:20 +01004600
Chris Wilson37cd3302017-11-12 11:27:38 +00004601 mutex_lock(&i915->drm.struct_mutex);
4602 intel_uncore_forcewake_get(i915, FORCEWAKE_ALL);
Imre Deak31ab49a2016-11-07 11:20:05 +02004603
Chris Wilson37cd3302017-11-12 11:27:38 +00004604 i915_gem_restore_gtt_mappings(i915);
4605 i915_gem_restore_fences(i915);
Chris Wilson5ab57c72016-07-15 14:56:20 +01004606
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00004607 /*
4608 * As we didn't flush the kernel context before suspend, we cannot
Chris Wilson5ab57c72016-07-15 14:56:20 +01004609 * guarantee that the context image is complete. So let's just reset
4610 * it and start again.
4611 */
Chris Wilson37cd3302017-11-12 11:27:38 +00004612 i915->gt.resume(i915);
Chris Wilson5ab57c72016-07-15 14:56:20 +01004613
Chris Wilson37cd3302017-11-12 11:27:38 +00004614 if (i915_gem_init_hw(i915))
4615 goto err_wedged;
4616
Michal Wajdeczko7cfca4a2018-03-02 11:15:49 +00004617 intel_uc_resume(i915);
Chris Wilson7469c622017-11-14 13:03:00 +00004618
Chris Wilson37cd3302017-11-12 11:27:38 +00004619 /* Always reload a context for powersaving. */
4620 if (i915_gem_switch_to_kernel_context(i915))
4621 goto err_wedged;
4622
4623out_unlock:
4624 intel_uncore_forcewake_put(i915, FORCEWAKE_ALL);
4625 mutex_unlock(&i915->drm.struct_mutex);
4626 return;
4627
4628err_wedged:
Chris Wilsonc41166f2019-02-20 14:56:37 +00004629 if (!i915_reset_failed(i915)) {
4630 dev_err(i915->drm.dev,
4631 "Failed to re-initialize GPU, declaring it wedged!\n");
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00004632 i915_gem_set_wedged(i915);
4633 }
Chris Wilson37cd3302017-11-12 11:27:38 +00004634 goto out_unlock;
Chris Wilson5ab57c72016-07-15 14:56:20 +01004635}
4636
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00004637void i915_gem_init_swizzling(struct drm_i915_private *dev_priv)
Daniel Vetterf691e2f2012-02-02 09:58:12 +01004638{
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00004639 if (INTEL_GEN(dev_priv) < 5 ||
Daniel Vetterf691e2f2012-02-02 09:58:12 +01004640 dev_priv->mm.bit_6_swizzle_x == I915_BIT_6_SWIZZLE_NONE)
4641 return;
4642
4643 I915_WRITE(DISP_ARB_CTL, I915_READ(DISP_ARB_CTL) |
4644 DISP_TILE_SURFACE_SWIZZLING);
4645
Lucas De Marchicf819ef2018-12-12 10:10:43 -08004646 if (IS_GEN(dev_priv, 5))
Daniel Vetter11782b02012-01-31 16:47:55 +01004647 return;
4648
Daniel Vetterf691e2f2012-02-02 09:58:12 +01004649 I915_WRITE(TILECTL, I915_READ(TILECTL) | TILECTL_SWZCTL);
Lucas De Marchicf819ef2018-12-12 10:10:43 -08004650 if (IS_GEN(dev_priv, 6))
Daniel Vetter6b26c862012-04-24 14:04:12 +02004651 I915_WRITE(ARB_MODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_SNB));
Lucas De Marchicf819ef2018-12-12 10:10:43 -08004652 else if (IS_GEN(dev_priv, 7))
Daniel Vetter6b26c862012-04-24 14:04:12 +02004653 I915_WRITE(ARB_MODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_IVB));
Lucas De Marchicf819ef2018-12-12 10:10:43 -08004654 else if (IS_GEN(dev_priv, 8))
Ben Widawsky31a53362013-11-02 21:07:04 -07004655 I915_WRITE(GAMTARBMODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_BDW));
Ben Widawsky8782e262012-12-18 10:31:23 -08004656 else
4657 BUG();
Daniel Vetterf691e2f2012-02-02 09:58:12 +01004658}
Daniel Vettere21af882012-02-09 20:53:27 +01004659
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01004660static void init_unused_ring(struct drm_i915_private *dev_priv, u32 base)
Ville Syrjälä81e7f202014-08-15 01:21:55 +03004661{
Ville Syrjälä81e7f202014-08-15 01:21:55 +03004662 I915_WRITE(RING_CTL(base), 0);
4663 I915_WRITE(RING_HEAD(base), 0);
4664 I915_WRITE(RING_TAIL(base), 0);
4665 I915_WRITE(RING_START(base), 0);
4666}
4667
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01004668static void init_unused_rings(struct drm_i915_private *dev_priv)
Ville Syrjälä81e7f202014-08-15 01:21:55 +03004669{
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01004670 if (IS_I830(dev_priv)) {
4671 init_unused_ring(dev_priv, PRB1_BASE);
4672 init_unused_ring(dev_priv, SRB0_BASE);
4673 init_unused_ring(dev_priv, SRB1_BASE);
4674 init_unused_ring(dev_priv, SRB2_BASE);
4675 init_unused_ring(dev_priv, SRB3_BASE);
Lucas De Marchicf819ef2018-12-12 10:10:43 -08004676 } else if (IS_GEN(dev_priv, 2)) {
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01004677 init_unused_ring(dev_priv, SRB0_BASE);
4678 init_unused_ring(dev_priv, SRB1_BASE);
Lucas De Marchicf819ef2018-12-12 10:10:43 -08004679 } else if (IS_GEN(dev_priv, 3)) {
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01004680 init_unused_ring(dev_priv, PRB1_BASE);
4681 init_unused_ring(dev_priv, PRB2_BASE);
Ville Syrjälä81e7f202014-08-15 01:21:55 +03004682 }
4683}
4684
Chris Wilson20a8a742017-02-08 14:30:31 +00004685static int __i915_gem_restart_engines(void *data)
Ben Widawsky4fc7c972013-02-08 11:49:24 -08004686{
Chris Wilson20a8a742017-02-08 14:30:31 +00004687 struct drm_i915_private *i915 = data;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00004688 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05304689 enum intel_engine_id id;
Chris Wilson20a8a742017-02-08 14:30:31 +00004690 int err;
4691
4692 for_each_engine(engine, i915, id) {
4693 err = engine->init_hw(engine);
Chris Wilson8177e112018-02-07 11:15:45 +00004694 if (err) {
4695 DRM_ERROR("Failed to restart %s (%d)\n",
4696 engine->name, err);
Chris Wilson20a8a742017-02-08 14:30:31 +00004697 return err;
Chris Wilson8177e112018-02-07 11:15:45 +00004698 }
Chris Wilson20a8a742017-02-08 14:30:31 +00004699 }
4700
4701 return 0;
4702}
4703
4704int i915_gem_init_hw(struct drm_i915_private *dev_priv)
4705{
Chris Wilsond200cda2016-04-28 09:56:44 +01004706 int ret;
Ben Widawsky4fc7c972013-02-08 11:49:24 -08004707
Chris Wilsonde867c22016-10-25 13:16:02 +01004708 dev_priv->gt.last_init_time = ktime_get();
4709
Chris Wilson5e4f5182015-02-13 14:35:59 +00004710 /* Double layer security blanket, see i915_gem_init() */
4711 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
4712
Tvrtko Ursulin0031fb92016-11-04 14:42:44 +00004713 if (HAS_EDRAM(dev_priv) && INTEL_GEN(dev_priv) < 9)
Ben Widawsky05e21cc2013-07-04 11:02:04 -07004714 I915_WRITE(HSW_IDICR, I915_READ(HSW_IDICR) | IDIHASHMSK(0xf));
Ben Widawsky4fc7c972013-02-08 11:49:24 -08004715
Tvrtko Ursulin772c2a52016-10-13 11:03:01 +01004716 if (IS_HASWELL(dev_priv))
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01004717 I915_WRITE(MI_PREDICATE_RESULT_2, IS_HSW_GT3(dev_priv) ?
Ville Syrjälä0bf21342013-11-29 14:56:12 +02004718 LOWER_SLICE_ENABLED : LOWER_SLICE_DISABLED);
Rodrigo Vivi94353732013-08-28 16:45:46 -03004719
Tvrtko Ursulin094304b2018-12-03 12:50:10 +00004720 /* Apply the GT workarounds... */
Tvrtko Ursulin25d140f2018-12-03 13:33:19 +00004721 intel_gt_apply_workarounds(dev_priv);
Tvrtko Ursulin094304b2018-12-03 12:50:10 +00004722 /* ...and determine whether they are sticking. */
4723 intel_gt_verify_workarounds(dev_priv, "init");
Oscar Mateo59b449d2018-04-10 09:12:47 -07004724
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00004725 i915_gem_init_swizzling(dev_priv);
Ben Widawsky4fc7c972013-02-08 11:49:24 -08004726
Daniel Vetterd5abdfd2014-11-20 09:45:19 +01004727 /*
4728 * At least 830 can leave some of the unused rings
4729 * "active" (ie. head != tail) after resume which
4730 * will prevent c3 entry. Makes sure all unused rings
4731 * are totally idle.
4732 */
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01004733 init_unused_rings(dev_priv);
Daniel Vetterd5abdfd2014-11-20 09:45:19 +01004734
Dave Gordoned54c1a2016-01-19 19:02:54 +00004735 BUG_ON(!dev_priv->kernel_context);
Chris Wilsonc41166f2019-02-20 14:56:37 +00004736 ret = i915_terminally_wedged(dev_priv);
4737 if (ret)
Chris Wilson6f74b362017-10-15 15:37:25 +01004738 goto out;
John Harrison90638cc2015-05-29 17:43:37 +01004739
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00004740 ret = i915_ppgtt_init_hw(dev_priv);
John Harrison4ad2fd82015-06-18 13:11:20 +01004741 if (ret) {
Chris Wilson8177e112018-02-07 11:15:45 +00004742 DRM_ERROR("Enabling PPGTT failed (%d)\n", ret);
John Harrison4ad2fd82015-06-18 13:11:20 +01004743 goto out;
4744 }
4745
Jackie Lif08e2032018-03-13 17:32:53 -07004746 ret = intel_wopcm_init_hw(&dev_priv->wopcm);
4747 if (ret) {
4748 DRM_ERROR("Enabling WOPCM failed (%d)\n", ret);
4749 goto out;
4750 }
4751
Michał Winiarski9bdc3572017-10-25 18:25:19 +01004752 /* We can't enable contexts until all firmware is loaded */
4753 ret = intel_uc_init_hw(dev_priv);
Chris Wilson8177e112018-02-07 11:15:45 +00004754 if (ret) {
4755 DRM_ERROR("Enabling uc failed (%d)\n", ret);
Michał Winiarski9bdc3572017-10-25 18:25:19 +01004756 goto out;
Chris Wilson8177e112018-02-07 11:15:45 +00004757 }
Michał Winiarski9bdc3572017-10-25 18:25:19 +01004758
Tvrtko Ursulinbf9e8422016-12-01 14:16:38 +00004759 intel_mocs_init_l3cc_table(dev_priv);
Peter Antoine0ccdacf2016-04-13 15:03:25 +01004760
Chris Wilson136109c2017-11-02 13:14:30 +00004761 /* Only when the HW is re-initialised, can we replay the requests */
4762 ret = __i915_gem_restart_engines(dev_priv);
Michal Wajdeczkob96f6eb2018-06-05 12:24:43 +00004763 if (ret)
4764 goto cleanup_uc;
Michał Winiarski60c0a662018-07-12 14:48:10 +02004765
Chris Wilson5e4f5182015-02-13 14:35:59 +00004766 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Michał Winiarski60c0a662018-07-12 14:48:10 +02004767
4768 return 0;
Michal Wajdeczkob96f6eb2018-06-05 12:24:43 +00004769
4770cleanup_uc:
4771 intel_uc_fini_hw(dev_priv);
Michał Winiarski60c0a662018-07-12 14:48:10 +02004772out:
4773 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
4774
4775 return ret;
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004776}
4777
Chris Wilsond2b4b972017-11-10 14:26:33 +00004778static int __intel_engines_record_defaults(struct drm_i915_private *i915)
4779{
4780 struct i915_gem_context *ctx;
4781 struct intel_engine_cs *engine;
4782 enum intel_engine_id id;
4783 int err;
4784
4785 /*
4786 * As we reset the gpu during very early sanitisation, the current
4787 * register state on the GPU should reflect its defaults values.
4788 * We load a context onto the hw (with restore-inhibit), then switch
4789 * over to a second context to save that default register state. We
4790 * can then prime every new context with that state so they all start
4791 * from the same default HW values.
4792 */
4793
4794 ctx = i915_gem_context_create_kernel(i915, 0);
4795 if (IS_ERR(ctx))
4796 return PTR_ERR(ctx);
4797
4798 for_each_engine(engine, i915, id) {
Chris Wilsone61e0f52018-02-21 09:56:36 +00004799 struct i915_request *rq;
Chris Wilsond2b4b972017-11-10 14:26:33 +00004800
Chris Wilsone61e0f52018-02-21 09:56:36 +00004801 rq = i915_request_alloc(engine, ctx);
Chris Wilsond2b4b972017-11-10 14:26:33 +00004802 if (IS_ERR(rq)) {
4803 err = PTR_ERR(rq);
4804 goto out_ctx;
4805 }
4806
Chris Wilson3fef5cd2017-11-20 10:20:02 +00004807 err = 0;
Chris Wilsond2b4b972017-11-10 14:26:33 +00004808 if (engine->init_context)
4809 err = engine->init_context(rq);
4810
Chris Wilson697b9a82018-06-12 11:51:35 +01004811 i915_request_add(rq);
Chris Wilsond2b4b972017-11-10 14:26:33 +00004812 if (err)
4813 goto err_active;
4814 }
4815
4816 err = i915_gem_switch_to_kernel_context(i915);
4817 if (err)
4818 goto err_active;
4819
Chris Wilson2621cef2018-07-09 13:20:43 +01004820 if (i915_gem_wait_for_idle(i915, I915_WAIT_LOCKED, HZ / 5)) {
4821 i915_gem_set_wedged(i915);
4822 err = -EIO; /* Caller will declare us wedged */
Chris Wilsond2b4b972017-11-10 14:26:33 +00004823 goto err_active;
Chris Wilson2621cef2018-07-09 13:20:43 +01004824 }
Chris Wilsond2b4b972017-11-10 14:26:33 +00004825
4826 assert_kernel_context_is_current(i915);
4827
Chris Wilson8e1cb322018-09-20 17:13:43 +01004828 /*
4829 * Immediately park the GPU so that we enable powersaving and
4830 * treat it as idle. The next time we issue a request, we will
4831 * unpark and start using the engine->pinned_default_state, otherwise
4832 * it is in limbo and an early reset may fail.
4833 */
4834 __i915_gem_park(i915);
4835
Chris Wilsond2b4b972017-11-10 14:26:33 +00004836 for_each_engine(engine, i915, id) {
4837 struct i915_vma *state;
Chris Wilson37d7c9c2018-09-14 13:35:03 +01004838 void *vaddr;
Chris Wilsond2b4b972017-11-10 14:26:33 +00004839
Chris Wilson666424a2018-09-14 13:35:04 +01004840 GEM_BUG_ON(to_intel_context(ctx, engine)->pin_count);
4841
Chris Wilsonab82a062018-04-30 14:15:01 +01004842 state = to_intel_context(ctx, engine)->state;
Chris Wilsond2b4b972017-11-10 14:26:33 +00004843 if (!state)
4844 continue;
4845
4846 /*
4847 * As we will hold a reference to the logical state, it will
4848 * not be torn down with the context, and importantly the
4849 * object will hold onto its vma (making it possible for a
4850 * stray GTT write to corrupt our defaults). Unmap the vma
4851 * from the GTT to prevent such accidents and reclaim the
4852 * space.
4853 */
4854 err = i915_vma_unbind(state);
4855 if (err)
4856 goto err_active;
4857
4858 err = i915_gem_object_set_to_cpu_domain(state->obj, false);
4859 if (err)
4860 goto err_active;
4861
4862 engine->default_state = i915_gem_object_get(state->obj);
Chris Wilson37d7c9c2018-09-14 13:35:03 +01004863
4864 /* Check we can acquire the image of the context state */
4865 vaddr = i915_gem_object_pin_map(engine->default_state,
Chris Wilson666424a2018-09-14 13:35:04 +01004866 I915_MAP_FORCE_WB);
Chris Wilson37d7c9c2018-09-14 13:35:03 +01004867 if (IS_ERR(vaddr)) {
4868 err = PTR_ERR(vaddr);
4869 goto err_active;
4870 }
4871
4872 i915_gem_object_unpin_map(engine->default_state);
Chris Wilsond2b4b972017-11-10 14:26:33 +00004873 }
4874
4875 if (IS_ENABLED(CONFIG_DRM_I915_DEBUG_GEM)) {
4876 unsigned int found = intel_engines_has_context_isolation(i915);
4877
4878 /*
4879 * Make sure that classes with multiple engine instances all
4880 * share the same basic configuration.
4881 */
4882 for_each_engine(engine, i915, id) {
4883 unsigned int bit = BIT(engine->uabi_class);
4884 unsigned int expected = engine->default_state ? bit : 0;
4885
4886 if ((found & bit) != expected) {
4887 DRM_ERROR("mismatching default context state for class %d on engine %s\n",
4888 engine->uabi_class, engine->name);
4889 }
4890 }
4891 }
4892
4893out_ctx:
4894 i915_gem_context_set_closed(ctx);
4895 i915_gem_context_put(ctx);
4896 return err;
4897
4898err_active:
4899 /*
4900 * If we have to abandon now, we expect the engines to be idle
4901 * and ready to be torn-down. First try to flush any remaining
4902 * request, ensure we are pointing at the kernel context and
4903 * then remove it.
4904 */
4905 if (WARN_ON(i915_gem_switch_to_kernel_context(i915)))
4906 goto out_ctx;
4907
Chris Wilsonec625fb2018-07-09 13:20:42 +01004908 if (WARN_ON(i915_gem_wait_for_idle(i915,
4909 I915_WAIT_LOCKED,
4910 MAX_SCHEDULE_TIMEOUT)))
Chris Wilsond2b4b972017-11-10 14:26:33 +00004911 goto out_ctx;
4912
4913 i915_gem_contexts_lost(i915);
4914 goto out_ctx;
4915}
4916
Chris Wilson51797492018-12-04 14:15:16 +00004917static int
4918i915_gem_init_scratch(struct drm_i915_private *i915, unsigned int size)
4919{
4920 struct drm_i915_gem_object *obj;
4921 struct i915_vma *vma;
4922 int ret;
4923
4924 obj = i915_gem_object_create_stolen(i915, size);
4925 if (!obj)
4926 obj = i915_gem_object_create_internal(i915, size);
4927 if (IS_ERR(obj)) {
4928 DRM_ERROR("Failed to allocate scratch page\n");
4929 return PTR_ERR(obj);
4930 }
4931
4932 vma = i915_vma_instance(obj, &i915->ggtt.vm, NULL);
4933 if (IS_ERR(vma)) {
4934 ret = PTR_ERR(vma);
4935 goto err_unref;
4936 }
4937
4938 ret = i915_vma_pin(vma, 0, 0, PIN_GLOBAL | PIN_HIGH);
4939 if (ret)
4940 goto err_unref;
4941
4942 i915->gt.scratch = vma;
4943 return 0;
4944
4945err_unref:
4946 i915_gem_object_put(obj);
4947 return ret;
4948}
4949
4950static void i915_gem_fini_scratch(struct drm_i915_private *i915)
4951{
4952 i915_vma_unpin_and_release(&i915->gt.scratch, 0);
4953}
4954
Tvrtko Ursulinbf9e8422016-12-01 14:16:38 +00004955int i915_gem_init(struct drm_i915_private *dev_priv)
Chris Wilson1070a422012-04-24 15:47:41 +01004956{
Chris Wilson1070a422012-04-24 15:47:41 +01004957 int ret;
4958
Changbin Du52b24162018-05-08 17:07:05 +08004959 /* We need to fallback to 4K pages if host doesn't support huge gtt. */
4960 if (intel_vgpu_active(dev_priv) && !intel_vgpu_has_huge_gtt(dev_priv))
Matthew Auldda9fe3f32017-10-06 23:18:31 +01004961 mkwrite_device_info(dev_priv)->page_sizes =
4962 I915_GTT_PAGE_SIZE_4K;
4963
Chris Wilson94312822017-05-03 10:39:18 +01004964 dev_priv->mm.unordered_timeline = dma_fence_context_alloc(1);
Chris Wilson57822dc2017-02-22 11:40:48 +00004965
Chris Wilsonfb5c5512017-11-20 20:55:00 +00004966 if (HAS_LOGICAL_RING_CONTEXTS(dev_priv)) {
Chris Wilson821ed7d2016-09-09 14:11:53 +01004967 dev_priv->gt.resume = intel_lr_context_resume;
Tvrtko Ursulin117897f2016-03-16 11:00:40 +00004968 dev_priv->gt.cleanup_engine = intel_logical_ring_cleanup;
Chris Wilsonfb5c5512017-11-20 20:55:00 +00004969 } else {
4970 dev_priv->gt.resume = intel_legacy_submission_resume;
4971 dev_priv->gt.cleanup_engine = intel_engine_cleanup;
Oscar Mateoa83014d2014-07-24 17:04:21 +01004972 }
4973
Chris Wilson1e345562019-01-28 10:23:56 +00004974 i915_timelines_init(dev_priv);
4975
Chris Wilsonee487002017-11-22 17:26:21 +00004976 ret = i915_gem_init_userptr(dev_priv);
4977 if (ret)
4978 return ret;
4979
Sagar Arun Kamble70deead2018-01-24 21:16:58 +05304980 ret = intel_uc_init_misc(dev_priv);
Michał Winiarski3176ff42017-12-13 23:13:47 +01004981 if (ret)
4982 return ret;
4983
Michal Wajdeczkof7dc0152018-06-28 14:15:21 +00004984 ret = intel_wopcm_init(&dev_priv->wopcm);
4985 if (ret)
4986 goto err_uc_misc;
4987
Chris Wilson5e4f5182015-02-13 14:35:59 +00004988 /* This is just a security blanket to placate dragons.
4989 * On some systems, we very sporadically observe that the first TLBs
4990 * used by the CS may be stale, despite us poking the TLB reset. If
4991 * we hold the forcewake during initialisation these problems
4992 * just magically go away.
4993 */
Chris Wilsonee487002017-11-22 17:26:21 +00004994 mutex_lock(&dev_priv->drm.struct_mutex);
Chris Wilson5e4f5182015-02-13 14:35:59 +00004995 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
4996
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01004997 ret = i915_gem_init_ggtt(dev_priv);
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00004998 if (ret) {
4999 GEM_BUG_ON(ret == -EIO);
5000 goto err_unlock;
5001 }
Jesse Barnesd62b4892013-03-08 10:45:53 -08005002
Chris Wilson51797492018-12-04 14:15:16 +00005003 ret = i915_gem_init_scratch(dev_priv,
Lucas De Marchicf819ef2018-12-12 10:10:43 -08005004 IS_GEN(dev_priv, 2) ? SZ_256K : PAGE_SIZE);
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00005005 if (ret) {
5006 GEM_BUG_ON(ret == -EIO);
5007 goto err_ggtt;
5008 }
Ben Widawsky2fa48d82013-12-06 14:11:04 -08005009
Chris Wilson51797492018-12-04 14:15:16 +00005010 ret = i915_gem_contexts_init(dev_priv);
5011 if (ret) {
5012 GEM_BUG_ON(ret == -EIO);
5013 goto err_scratch;
5014 }
5015
Tvrtko Ursulinbf9e8422016-12-01 14:16:38 +00005016 ret = intel_engines_init(dev_priv);
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00005017 if (ret) {
5018 GEM_BUG_ON(ret == -EIO);
5019 goto err_context;
5020 }
Daniel Vetter53ca26c2012-04-26 23:28:03 +02005021
Chris Wilsonf58d13d2017-11-10 14:26:29 +00005022 intel_init_gt_powersave(dev_priv);
5023
Michał Winiarski61b5c152017-12-13 23:13:48 +01005024 ret = intel_uc_init(dev_priv);
Chris Wilsoncc6a8182017-11-10 14:26:30 +00005025 if (ret)
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00005026 goto err_pm;
Chris Wilsoncc6a8182017-11-10 14:26:30 +00005027
Michał Winiarski61b5c152017-12-13 23:13:48 +01005028 ret = i915_gem_init_hw(dev_priv);
5029 if (ret)
5030 goto err_uc_init;
5031
Chris Wilsoncc6a8182017-11-10 14:26:30 +00005032 /*
5033 * Despite its name intel_init_clock_gating applies both display
5034 * clock gating workarounds; GT mmio workarounds and the occasional
5035 * GT power context workaround. Worse, sometimes it includes a context
5036 * register workaround which we need to apply before we record the
5037 * default HW state for all contexts.
5038 *
5039 * FIXME: break up the workarounds and apply them at the right time!
5040 */
5041 intel_init_clock_gating(dev_priv);
5042
Chris Wilsond2b4b972017-11-10 14:26:33 +00005043 ret = __intel_engines_record_defaults(dev_priv);
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00005044 if (ret)
5045 goto err_init_hw;
5046
5047 if (i915_inject_load_failure()) {
5048 ret = -ENODEV;
5049 goto err_init_hw;
5050 }
5051
5052 if (i915_inject_load_failure()) {
5053 ret = -EIO;
5054 goto err_init_hw;
5055 }
5056
5057 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
5058 mutex_unlock(&dev_priv->drm.struct_mutex);
5059
5060 return 0;
5061
5062 /*
5063 * Unwinding is complicated by that we want to handle -EIO to mean
5064 * disable GPU submission but keep KMS alive. We want to mark the
5065 * HW as irrevisibly wedged, but keep enough state around that the
5066 * driver doesn't explode during runtime.
5067 */
5068err_init_hw:
Chris Wilson8571a052018-06-06 15:54:41 +01005069 mutex_unlock(&dev_priv->drm.struct_mutex);
5070
5071 WARN_ON(i915_gem_suspend(dev_priv));
5072 i915_gem_suspend_late(dev_priv);
5073
Chris Wilson8bcf9f72018-07-10 10:44:20 +01005074 i915_gem_drain_workqueue(dev_priv);
5075
Chris Wilson8571a052018-06-06 15:54:41 +01005076 mutex_lock(&dev_priv->drm.struct_mutex);
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00005077 intel_uc_fini_hw(dev_priv);
Michał Winiarski61b5c152017-12-13 23:13:48 +01005078err_uc_init:
5079 intel_uc_fini(dev_priv);
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00005080err_pm:
5081 if (ret != -EIO) {
5082 intel_cleanup_gt_powersave(dev_priv);
5083 i915_gem_cleanup_engines(dev_priv);
5084 }
5085err_context:
5086 if (ret != -EIO)
5087 i915_gem_contexts_fini(dev_priv);
Chris Wilson51797492018-12-04 14:15:16 +00005088err_scratch:
5089 i915_gem_fini_scratch(dev_priv);
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00005090err_ggtt:
5091err_unlock:
5092 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
5093 mutex_unlock(&dev_priv->drm.struct_mutex);
5094
Michal Wajdeczkof7dc0152018-06-28 14:15:21 +00005095err_uc_misc:
Sagar Arun Kamble70deead2018-01-24 21:16:58 +05305096 intel_uc_fini_misc(dev_priv);
Sagar Arun Kambleda943b52018-01-10 18:24:16 +05305097
Chris Wilson1e345562019-01-28 10:23:56 +00005098 if (ret != -EIO) {
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00005099 i915_gem_cleanup_userptr(dev_priv);
Chris Wilson1e345562019-01-28 10:23:56 +00005100 i915_timelines_fini(dev_priv);
5101 }
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00005102
Chris Wilson60990322014-04-09 09:19:42 +01005103 if (ret == -EIO) {
Chris Wilson7ed43df2018-07-26 09:50:32 +01005104 mutex_lock(&dev_priv->drm.struct_mutex);
5105
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00005106 /*
5107 * Allow engine initialisation to fail by marking the GPU as
Chris Wilson60990322014-04-09 09:19:42 +01005108 * wedged. But we only want to do this where the GPU is angry,
5109 * for all other failure, such as an allocation failure, bail.
5110 */
Chris Wilsonc41166f2019-02-20 14:56:37 +00005111 if (!i915_reset_failed(dev_priv)) {
Chris Wilson51c18bf2018-06-09 12:10:58 +01005112 i915_load_error(dev_priv,
5113 "Failed to initialize GPU, declaring it wedged!\n");
Chris Wilson6f74b362017-10-15 15:37:25 +01005114 i915_gem_set_wedged(dev_priv);
5115 }
Chris Wilson7ed43df2018-07-26 09:50:32 +01005116
5117 /* Minimal basic recovery for KMS */
5118 ret = i915_ggtt_enable_hw(dev_priv);
5119 i915_gem_restore_gtt_mappings(dev_priv);
5120 i915_gem_restore_fences(dev_priv);
5121 intel_init_clock_gating(dev_priv);
5122
5123 mutex_unlock(&dev_priv->drm.struct_mutex);
Chris Wilson1070a422012-04-24 15:47:41 +01005124 }
5125
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00005126 i915_gem_drain_freed_objects(dev_priv);
Chris Wilson60990322014-04-09 09:19:42 +01005127 return ret;
Chris Wilson1070a422012-04-24 15:47:41 +01005128}
5129
Michal Wajdeczko8979187a2018-06-04 09:00:32 +00005130void i915_gem_fini(struct drm_i915_private *dev_priv)
5131{
5132 i915_gem_suspend_late(dev_priv);
Chris Wilson30b710842018-08-12 23:36:29 +01005133 intel_disable_gt_powersave(dev_priv);
Michal Wajdeczko8979187a2018-06-04 09:00:32 +00005134
5135 /* Flush any outstanding unpin_work. */
5136 i915_gem_drain_workqueue(dev_priv);
5137
5138 mutex_lock(&dev_priv->drm.struct_mutex);
5139 intel_uc_fini_hw(dev_priv);
5140 intel_uc_fini(dev_priv);
5141 i915_gem_cleanup_engines(dev_priv);
5142 i915_gem_contexts_fini(dev_priv);
Chris Wilson51797492018-12-04 14:15:16 +00005143 i915_gem_fini_scratch(dev_priv);
Michal Wajdeczko8979187a2018-06-04 09:00:32 +00005144 mutex_unlock(&dev_priv->drm.struct_mutex);
5145
Tvrtko Ursulin25d140f2018-12-03 13:33:19 +00005146 intel_wa_list_free(&dev_priv->gt_wa_list);
5147
Chris Wilson30b710842018-08-12 23:36:29 +01005148 intel_cleanup_gt_powersave(dev_priv);
5149
Michal Wajdeczko8979187a2018-06-04 09:00:32 +00005150 intel_uc_fini_misc(dev_priv);
5151 i915_gem_cleanup_userptr(dev_priv);
Chris Wilson1e345562019-01-28 10:23:56 +00005152 i915_timelines_fini(dev_priv);
Michal Wajdeczko8979187a2018-06-04 09:00:32 +00005153
5154 i915_gem_drain_freed_objects(dev_priv);
5155
5156 WARN_ON(!list_empty(&dev_priv->contexts.list));
5157}
5158
Chris Wilson24145512017-01-24 11:01:35 +00005159void i915_gem_init_mmio(struct drm_i915_private *i915)
5160{
5161 i915_gem_sanitize(i915);
5162}
5163
Zou Nan hai8187a2b2010-05-21 09:08:55 +08005164void
Tvrtko Ursulincb15d9f2016-12-01 14:16:39 +00005165i915_gem_cleanup_engines(struct drm_i915_private *dev_priv)
Zou Nan hai8187a2b2010-05-21 09:08:55 +08005166{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00005167 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05305168 enum intel_engine_id id;
Zou Nan hai8187a2b2010-05-21 09:08:55 +08005169
Akash Goel3b3f1652016-10-13 22:44:48 +05305170 for_each_engine(engine, dev_priv, id)
Tvrtko Ursulin117897f2016-03-16 11:00:40 +00005171 dev_priv->gt.cleanup_engine(engine);
Zou Nan hai8187a2b2010-05-21 09:08:55 +08005172}
5173
Eric Anholt673a3942008-07-30 12:06:12 -07005174void
Imre Deak40ae4e12016-03-16 14:54:03 +02005175i915_gem_load_init_fences(struct drm_i915_private *dev_priv)
5176{
Chris Wilson49ef5292016-08-18 17:17:00 +01005177 int i;
Imre Deak40ae4e12016-03-16 14:54:03 +02005178
Tvrtko Ursulinc56b89f2018-02-09 21:58:46 +00005179 if (INTEL_GEN(dev_priv) >= 7 && !IS_VALLEYVIEW(dev_priv) &&
Imre Deak40ae4e12016-03-16 14:54:03 +02005180 !IS_CHERRYVIEW(dev_priv))
5181 dev_priv->num_fence_regs = 32;
Tvrtko Ursulinc56b89f2018-02-09 21:58:46 +00005182 else if (INTEL_GEN(dev_priv) >= 4 ||
Jani Nikula73f67aa2016-12-07 22:48:09 +02005183 IS_I945G(dev_priv) || IS_I945GM(dev_priv) ||
5184 IS_G33(dev_priv) || IS_PINEVIEW(dev_priv))
Imre Deak40ae4e12016-03-16 14:54:03 +02005185 dev_priv->num_fence_regs = 16;
5186 else
5187 dev_priv->num_fence_regs = 8;
5188
Chris Wilsonc0336662016-05-06 15:40:21 +01005189 if (intel_vgpu_active(dev_priv))
Imre Deak40ae4e12016-03-16 14:54:03 +02005190 dev_priv->num_fence_regs =
5191 I915_READ(vgtif_reg(avail_rs.fence_num));
5192
5193 /* Initialize fence registers to zero */
Chris Wilson49ef5292016-08-18 17:17:00 +01005194 for (i = 0; i < dev_priv->num_fence_regs; i++) {
5195 struct drm_i915_fence_reg *fence = &dev_priv->fence_regs[i];
5196
5197 fence->i915 = dev_priv;
5198 fence->id = i;
5199 list_add_tail(&fence->link, &dev_priv->mm.fence_list);
5200 }
Tvrtko Ursulin4362f4f2016-11-16 08:55:33 +00005201 i915_gem_restore_fences(dev_priv);
Imre Deak40ae4e12016-03-16 14:54:03 +02005202
Tvrtko Ursulin4362f4f2016-11-16 08:55:33 +00005203 i915_gem_detect_bit_6_swizzle(dev_priv);
Imre Deak40ae4e12016-03-16 14:54:03 +02005204}
5205
Chris Wilson9c52d1c2017-11-10 23:24:47 +00005206static void i915_gem_init__mm(struct drm_i915_private *i915)
5207{
5208 spin_lock_init(&i915->mm.object_stat_lock);
5209 spin_lock_init(&i915->mm.obj_lock);
5210 spin_lock_init(&i915->mm.free_lock);
5211
5212 init_llist_head(&i915->mm.free_list);
5213
5214 INIT_LIST_HEAD(&i915->mm.unbound_list);
5215 INIT_LIST_HEAD(&i915->mm.bound_list);
5216 INIT_LIST_HEAD(&i915->mm.fence_list);
5217 INIT_LIST_HEAD(&i915->mm.userfault_list);
5218
5219 INIT_WORK(&i915->mm.free_work, __i915_gem_free_work);
5220}
5221
Michal Wajdeczkoa0de9082018-03-23 12:34:49 +00005222int i915_gem_init_early(struct drm_i915_private *dev_priv)
Eric Anholt673a3942008-07-30 12:06:12 -07005223{
Tvrtko Ursulina9335682016-11-02 15:14:59 +00005224 int err = -ENOMEM;
Chris Wilson42dcedd2012-11-15 11:32:30 +00005225
Tvrtko Ursulina9335682016-11-02 15:14:59 +00005226 dev_priv->objects = KMEM_CACHE(drm_i915_gem_object, SLAB_HWCACHE_ALIGN);
5227 if (!dev_priv->objects)
Chris Wilson73cb9702016-10-28 13:58:46 +01005228 goto err_out;
Chris Wilson73cb9702016-10-28 13:58:46 +01005229
Tvrtko Ursulina9335682016-11-02 15:14:59 +00005230 dev_priv->vmas = KMEM_CACHE(i915_vma, SLAB_HWCACHE_ALIGN);
5231 if (!dev_priv->vmas)
Chris Wilson73cb9702016-10-28 13:58:46 +01005232 goto err_objects;
Chris Wilson73cb9702016-10-28 13:58:46 +01005233
Chris Wilsond1b48c12017-08-16 09:52:08 +01005234 dev_priv->luts = KMEM_CACHE(i915_lut_handle, 0);
5235 if (!dev_priv->luts)
5236 goto err_vmas;
5237
Chris Wilsone61e0f52018-02-21 09:56:36 +00005238 dev_priv->requests = KMEM_CACHE(i915_request,
Tvrtko Ursulina9335682016-11-02 15:14:59 +00005239 SLAB_HWCACHE_ALIGN |
5240 SLAB_RECLAIM_ACCOUNT |
Paul E. McKenney5f0d5a32017-01-18 02:53:44 -08005241 SLAB_TYPESAFE_BY_RCU);
Tvrtko Ursulina9335682016-11-02 15:14:59 +00005242 if (!dev_priv->requests)
Chris Wilsond1b48c12017-08-16 09:52:08 +01005243 goto err_luts;
Chris Wilson73cb9702016-10-28 13:58:46 +01005244
Chris Wilson52e54202016-11-14 20:41:02 +00005245 dev_priv->dependencies = KMEM_CACHE(i915_dependency,
5246 SLAB_HWCACHE_ALIGN |
5247 SLAB_RECLAIM_ACCOUNT);
5248 if (!dev_priv->dependencies)
5249 goto err_requests;
5250
Chris Wilsonc5cf9a92017-05-17 13:10:04 +01005251 dev_priv->priorities = KMEM_CACHE(i915_priolist, SLAB_HWCACHE_ALIGN);
5252 if (!dev_priv->priorities)
5253 goto err_dependencies;
5254
Chris Wilson643b4502018-04-30 14:15:03 +01005255 INIT_LIST_HEAD(&dev_priv->gt.active_rings);
Chris Wilson3365e222018-05-03 20:51:14 +01005256 INIT_LIST_HEAD(&dev_priv->gt.closed_vma);
Chris Wilson643b4502018-04-30 14:15:03 +01005257
Chris Wilson9c52d1c2017-11-10 23:24:47 +00005258 i915_gem_init__mm(dev_priv);
Chris Wilsonf2123812017-10-16 12:40:37 +01005259
Chris Wilson67d97da2016-07-04 08:08:31 +01005260 INIT_DELAYED_WORK(&dev_priv->gt.retire_work,
Eric Anholt673a3942008-07-30 12:06:12 -07005261 i915_gem_retire_work_handler);
Chris Wilson67d97da2016-07-04 08:08:31 +01005262 INIT_DELAYED_WORK(&dev_priv->gt.idle_work,
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005263 i915_gem_idle_work_handler);
Chris Wilson1f15b762016-07-01 17:23:14 +01005264 init_waitqueue_head(&dev_priv->gpu_error.wait_queue);
Daniel Vetter1f83fee2012-11-15 17:17:22 +01005265 init_waitqueue_head(&dev_priv->gpu_error.reset_queue);
Chris Wilson18bb2bc2019-01-14 21:04:01 +00005266 mutex_init(&dev_priv->gpu_error.wedge_mutex);
Chris Wilson2caffbf2019-02-08 15:37:03 +00005267 init_srcu_struct(&dev_priv->gpu_error.reset_backoff_srcu);
Chris Wilson31169712009-09-14 16:50:28 +01005268
Joonas Lahtinen6f633402016-09-01 14:58:21 +03005269 atomic_set(&dev_priv->mm.bsd_engine_dispatch_index, 0);
5270
Chris Wilsonb5add952016-08-04 16:32:36 +01005271 spin_lock_init(&dev_priv->fb_tracking.lock);
Chris Wilson73cb9702016-10-28 13:58:46 +01005272
Matthew Auld465c4032017-10-06 23:18:14 +01005273 err = i915_gemfs_init(dev_priv);
5274 if (err)
5275 DRM_NOTE("Unable to create a private tmpfs mount, hugepage support will be disabled(%d).\n", err);
5276
Chris Wilson73cb9702016-10-28 13:58:46 +01005277 return 0;
5278
Chris Wilson52e54202016-11-14 20:41:02 +00005279err_dependencies:
5280 kmem_cache_destroy(dev_priv->dependencies);
Chris Wilson73cb9702016-10-28 13:58:46 +01005281err_requests:
5282 kmem_cache_destroy(dev_priv->requests);
Chris Wilsond1b48c12017-08-16 09:52:08 +01005283err_luts:
5284 kmem_cache_destroy(dev_priv->luts);
Chris Wilson73cb9702016-10-28 13:58:46 +01005285err_vmas:
5286 kmem_cache_destroy(dev_priv->vmas);
5287err_objects:
5288 kmem_cache_destroy(dev_priv->objects);
5289err_out:
5290 return err;
Eric Anholt673a3942008-07-30 12:06:12 -07005291}
Dave Airlie71acb5e2008-12-30 20:31:46 +10005292
Michal Wajdeczkoa0de9082018-03-23 12:34:49 +00005293void i915_gem_cleanup_early(struct drm_i915_private *dev_priv)
Imre Deakd64aa092016-01-19 15:26:29 +02005294{
Chris Wilsonc4d4c1c2017-02-10 16:35:23 +00005295 i915_gem_drain_freed_objects(dev_priv);
Chris Wilsonc9c704712018-02-19 22:06:31 +00005296 GEM_BUG_ON(!llist_empty(&dev_priv->mm.free_list));
5297 GEM_BUG_ON(atomic_read(&dev_priv->mm.free_count));
Chris Wilsonc4d4c1c2017-02-10 16:35:23 +00005298 WARN_ON(dev_priv->mm.object_count);
Matthew Auldea84aa72016-11-17 21:04:11 +00005299
Chris Wilson2caffbf2019-02-08 15:37:03 +00005300 cleanup_srcu_struct(&dev_priv->gpu_error.reset_backoff_srcu);
5301
Chris Wilsonc5cf9a92017-05-17 13:10:04 +01005302 kmem_cache_destroy(dev_priv->priorities);
Chris Wilson52e54202016-11-14 20:41:02 +00005303 kmem_cache_destroy(dev_priv->dependencies);
Imre Deakd64aa092016-01-19 15:26:29 +02005304 kmem_cache_destroy(dev_priv->requests);
Chris Wilsond1b48c12017-08-16 09:52:08 +01005305 kmem_cache_destroy(dev_priv->luts);
Imre Deakd64aa092016-01-19 15:26:29 +02005306 kmem_cache_destroy(dev_priv->vmas);
5307 kmem_cache_destroy(dev_priv->objects);
Chris Wilson0eafec62016-08-04 16:32:41 +01005308
5309 /* And ensure that our DESTROY_BY_RCU slabs are truly destroyed */
5310 rcu_barrier();
Matthew Auld465c4032017-10-06 23:18:14 +01005311
5312 i915_gemfs_fini(dev_priv);
Imre Deakd64aa092016-01-19 15:26:29 +02005313}
5314
Chris Wilson6a800ea2016-09-21 14:51:07 +01005315int i915_gem_freeze(struct drm_i915_private *dev_priv)
5316{
Chris Wilsond0aa3012017-04-07 11:25:49 +01005317 /* Discard all purgeable objects, let userspace recover those as
5318 * required after resuming.
5319 */
Chris Wilson6a800ea2016-09-21 14:51:07 +01005320 i915_gem_shrink_all(dev_priv);
Chris Wilson6a800ea2016-09-21 14:51:07 +01005321
Chris Wilson6a800ea2016-09-21 14:51:07 +01005322 return 0;
5323}
5324
Chris Wilson95c778d2018-06-01 15:41:25 +01005325int i915_gem_freeze_late(struct drm_i915_private *i915)
Chris Wilson461fb992016-05-14 07:26:33 +01005326{
5327 struct drm_i915_gem_object *obj;
Chris Wilson7aab2d52016-09-09 20:02:18 +01005328 struct list_head *phases[] = {
Chris Wilson95c778d2018-06-01 15:41:25 +01005329 &i915->mm.unbound_list,
5330 &i915->mm.bound_list,
Chris Wilson7aab2d52016-09-09 20:02:18 +01005331 NULL
Chris Wilson95c778d2018-06-01 15:41:25 +01005332 }, **phase;
Chris Wilson461fb992016-05-14 07:26:33 +01005333
Chris Wilson95c778d2018-06-01 15:41:25 +01005334 /*
5335 * Called just before we write the hibernation image.
Chris Wilson461fb992016-05-14 07:26:33 +01005336 *
5337 * We need to update the domain tracking to reflect that the CPU
5338 * will be accessing all the pages to create and restore from the
5339 * hibernation, and so upon restoration those pages will be in the
5340 * CPU domain.
5341 *
5342 * To make sure the hibernation image contains the latest state,
5343 * we update that state just before writing out the image.
Chris Wilson7aab2d52016-09-09 20:02:18 +01005344 *
5345 * To try and reduce the hibernation image, we manually shrink
Chris Wilsond0aa3012017-04-07 11:25:49 +01005346 * the objects as well, see i915_gem_freeze()
Chris Wilson461fb992016-05-14 07:26:33 +01005347 */
5348
Chris Wilson95c778d2018-06-01 15:41:25 +01005349 i915_gem_shrink(i915, -1UL, NULL, I915_SHRINK_UNBOUND);
5350 i915_gem_drain_freed_objects(i915);
Chris Wilson461fb992016-05-14 07:26:33 +01005351
Chris Wilson95c778d2018-06-01 15:41:25 +01005352 mutex_lock(&i915->drm.struct_mutex);
5353 for (phase = phases; *phase; phase++) {
5354 list_for_each_entry(obj, *phase, mm.link)
5355 WARN_ON(i915_gem_object_set_to_cpu_domain(obj, true));
Chris Wilson461fb992016-05-14 07:26:33 +01005356 }
Chris Wilson95c778d2018-06-01 15:41:25 +01005357 mutex_unlock(&i915->drm.struct_mutex);
Chris Wilson461fb992016-05-14 07:26:33 +01005358
5359 return 0;
5360}
5361
Chris Wilsonf787a5f2010-09-24 16:02:42 +01005362void i915_gem_release(struct drm_device *dev, struct drm_file *file)
Eric Anholtb9624422009-06-03 07:27:35 +00005363{
Chris Wilsonf787a5f2010-09-24 16:02:42 +01005364 struct drm_i915_file_private *file_priv = file->driver_priv;
Chris Wilsone61e0f52018-02-21 09:56:36 +00005365 struct i915_request *request;
Eric Anholtb9624422009-06-03 07:27:35 +00005366
5367 /* Clean up our request list when the client is going away, so that
5368 * later retire_requests won't dereference our soon-to-be-gone
5369 * file_priv.
5370 */
Chris Wilson1c255952010-09-26 11:03:27 +01005371 spin_lock(&file_priv->mm.lock);
Chris Wilsonc8659ef2017-03-02 12:25:25 +00005372 list_for_each_entry(request, &file_priv->mm.request_list, client_link)
Chris Wilsonf787a5f2010-09-24 16:02:42 +01005373 request->file_priv = NULL;
Chris Wilson1c255952010-09-26 11:03:27 +01005374 spin_unlock(&file_priv->mm.lock);
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005375}
5376
Chris Wilson829a0af2017-06-20 12:05:45 +01005377int i915_gem_open(struct drm_i915_private *i915, struct drm_file *file)
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005378{
5379 struct drm_i915_file_private *file_priv;
Ben Widawskye422b882013-12-06 14:10:58 -08005380 int ret;
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005381
Chris Wilsonc4c29d72016-11-09 10:45:07 +00005382 DRM_DEBUG("\n");
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005383
5384 file_priv = kzalloc(sizeof(*file_priv), GFP_KERNEL);
5385 if (!file_priv)
5386 return -ENOMEM;
5387
5388 file->driver_priv = file_priv;
Chris Wilson829a0af2017-06-20 12:05:45 +01005389 file_priv->dev_priv = i915;
Chris Wilsonab0e7ff2014-02-25 17:11:24 +02005390 file_priv->file = file;
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005391
5392 spin_lock_init(&file_priv->mm.lock);
5393 INIT_LIST_HEAD(&file_priv->mm.request_list);
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005394
Chris Wilsonc80ff162016-07-27 09:07:27 +01005395 file_priv->bsd_engine = -1;
Mika Kuoppala14921f32018-06-15 13:44:29 +03005396 file_priv->hang_timestamp = jiffies;
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00005397
Chris Wilson829a0af2017-06-20 12:05:45 +01005398 ret = i915_gem_context_open(i915, file);
Ben Widawskye422b882013-12-06 14:10:58 -08005399 if (ret)
5400 kfree(file_priv);
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005401
Ben Widawskye422b882013-12-06 14:10:58 -08005402 return ret;
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005403}
5404
Daniel Vetterb680c372014-09-19 18:27:27 +02005405/**
5406 * i915_gem_track_fb - update frontbuffer tracking
Geliang Tangd9072a32015-09-15 05:58:44 -07005407 * @old: current GEM buffer for the frontbuffer slots
5408 * @new: new GEM buffer for the frontbuffer slots
5409 * @frontbuffer_bits: bitmask of frontbuffer slots
Daniel Vetterb680c372014-09-19 18:27:27 +02005410 *
5411 * This updates the frontbuffer tracking bits @frontbuffer_bits by clearing them
5412 * from @old and setting them in @new. Both @old and @new can be NULL.
5413 */
Daniel Vettera071fa02014-06-18 23:28:09 +02005414void i915_gem_track_fb(struct drm_i915_gem_object *old,
5415 struct drm_i915_gem_object *new,
5416 unsigned frontbuffer_bits)
5417{
Chris Wilsonfaf5bf02016-08-04 16:32:37 +01005418 /* Control of individual bits within the mask are guarded by
5419 * the owning plane->mutex, i.e. we can never see concurrent
5420 * manipulation of individual bits. But since the bitfield as a whole
5421 * is updated using RMW, we need to use atomics in order to update
5422 * the bits.
5423 */
5424 BUILD_BUG_ON(INTEL_FRONTBUFFER_BITS_PER_PIPE * I915_MAX_PIPES >
Chris Wilson74f6e182018-09-26 11:47:07 +01005425 BITS_PER_TYPE(atomic_t));
Chris Wilsonfaf5bf02016-08-04 16:32:37 +01005426
Daniel Vettera071fa02014-06-18 23:28:09 +02005427 if (old) {
Chris Wilsonfaf5bf02016-08-04 16:32:37 +01005428 WARN_ON(!(atomic_read(&old->frontbuffer_bits) & frontbuffer_bits));
5429 atomic_andnot(frontbuffer_bits, &old->frontbuffer_bits);
Daniel Vettera071fa02014-06-18 23:28:09 +02005430 }
5431
5432 if (new) {
Chris Wilsonfaf5bf02016-08-04 16:32:37 +01005433 WARN_ON(atomic_read(&new->frontbuffer_bits) & frontbuffer_bits);
5434 atomic_or(frontbuffer_bits, &new->frontbuffer_bits);
Daniel Vettera071fa02014-06-18 23:28:09 +02005435 }
5436}
5437
Dave Gordonea702992015-07-09 19:29:02 +01005438/* Allocate a new GEM object and fill it with the supplied data */
5439struct drm_i915_gem_object *
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +00005440i915_gem_object_create_from_data(struct drm_i915_private *dev_priv,
Dave Gordonea702992015-07-09 19:29:02 +01005441 const void *data, size_t size)
5442{
5443 struct drm_i915_gem_object *obj;
Chris Wilsonbe062fa2017-03-17 19:46:48 +00005444 struct file *file;
5445 size_t offset;
5446 int err;
Dave Gordonea702992015-07-09 19:29:02 +01005447
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +00005448 obj = i915_gem_object_create(dev_priv, round_up(size, PAGE_SIZE));
Chris Wilsonfe3db792016-04-25 13:32:13 +01005449 if (IS_ERR(obj))
Dave Gordonea702992015-07-09 19:29:02 +01005450 return obj;
5451
Christian Königc0a51fd2018-02-16 13:43:38 +01005452 GEM_BUG_ON(obj->write_domain != I915_GEM_DOMAIN_CPU);
Dave Gordonea702992015-07-09 19:29:02 +01005453
Chris Wilsonbe062fa2017-03-17 19:46:48 +00005454 file = obj->base.filp;
5455 offset = 0;
5456 do {
5457 unsigned int len = min_t(typeof(size), size, PAGE_SIZE);
5458 struct page *page;
5459 void *pgdata, *vaddr;
Dave Gordonea702992015-07-09 19:29:02 +01005460
Chris Wilsonbe062fa2017-03-17 19:46:48 +00005461 err = pagecache_write_begin(file, file->f_mapping,
5462 offset, len, 0,
5463 &page, &pgdata);
5464 if (err < 0)
5465 goto fail;
Dave Gordonea702992015-07-09 19:29:02 +01005466
Chris Wilsonbe062fa2017-03-17 19:46:48 +00005467 vaddr = kmap(page);
5468 memcpy(vaddr, data, len);
5469 kunmap(page);
5470
5471 err = pagecache_write_end(file, file->f_mapping,
5472 offset, len, len,
5473 page, pgdata);
5474 if (err < 0)
5475 goto fail;
5476
5477 size -= len;
5478 data += len;
5479 offset += len;
5480 } while (size);
Dave Gordonea702992015-07-09 19:29:02 +01005481
5482 return obj;
5483
5484fail:
Chris Wilsonf8c417c2016-07-20 13:31:53 +01005485 i915_gem_object_put(obj);
Chris Wilsonbe062fa2017-03-17 19:46:48 +00005486 return ERR_PTR(err);
Dave Gordonea702992015-07-09 19:29:02 +01005487}
Chris Wilson96d77632016-10-28 13:58:33 +01005488
5489struct scatterlist *
5490i915_gem_object_get_sg(struct drm_i915_gem_object *obj,
5491 unsigned int n,
5492 unsigned int *offset)
5493{
Chris Wilsona4f5ea62016-10-28 13:58:35 +01005494 struct i915_gem_object_page_iter *iter = &obj->mm.get_page;
Chris Wilson96d77632016-10-28 13:58:33 +01005495 struct scatterlist *sg;
5496 unsigned int idx, count;
5497
5498 might_sleep();
5499 GEM_BUG_ON(n >= obj->base.size >> PAGE_SHIFT);
Chris Wilsona4f5ea62016-10-28 13:58:35 +01005500 GEM_BUG_ON(!i915_gem_object_has_pinned_pages(obj));
Chris Wilson96d77632016-10-28 13:58:33 +01005501
5502 /* As we iterate forward through the sg, we record each entry in a
5503 * radixtree for quick repeated (backwards) lookups. If we have seen
5504 * this index previously, we will have an entry for it.
5505 *
5506 * Initial lookup is O(N), but this is amortized to O(1) for
5507 * sequential page access (where each new request is consecutive
5508 * to the previous one). Repeated lookups are O(lg(obj->base.size)),
5509 * i.e. O(1) with a large constant!
5510 */
5511 if (n < READ_ONCE(iter->sg_idx))
5512 goto lookup;
5513
5514 mutex_lock(&iter->lock);
5515
5516 /* We prefer to reuse the last sg so that repeated lookup of this
5517 * (or the subsequent) sg are fast - comparing against the last
5518 * sg is faster than going through the radixtree.
5519 */
5520
5521 sg = iter->sg_pos;
5522 idx = iter->sg_idx;
5523 count = __sg_page_count(sg);
5524
5525 while (idx + count <= n) {
Matthew Wilcox3159f942017-11-03 13:30:42 -04005526 void *entry;
5527 unsigned long i;
Chris Wilson96d77632016-10-28 13:58:33 +01005528 int ret;
5529
5530 /* If we cannot allocate and insert this entry, or the
5531 * individual pages from this range, cancel updating the
5532 * sg_idx so that on this lookup we are forced to linearly
5533 * scan onwards, but on future lookups we will try the
5534 * insertion again (in which case we need to be careful of
5535 * the error return reporting that we have already inserted
5536 * this index).
5537 */
5538 ret = radix_tree_insert(&iter->radix, idx, sg);
5539 if (ret && ret != -EEXIST)
5540 goto scan;
5541
Matthew Wilcox3159f942017-11-03 13:30:42 -04005542 entry = xa_mk_value(idx);
Chris Wilson96d77632016-10-28 13:58:33 +01005543 for (i = 1; i < count; i++) {
Matthew Wilcox3159f942017-11-03 13:30:42 -04005544 ret = radix_tree_insert(&iter->radix, idx + i, entry);
Chris Wilson96d77632016-10-28 13:58:33 +01005545 if (ret && ret != -EEXIST)
5546 goto scan;
5547 }
5548
5549 idx += count;
5550 sg = ____sg_next(sg);
5551 count = __sg_page_count(sg);
5552 }
5553
5554scan:
5555 iter->sg_pos = sg;
5556 iter->sg_idx = idx;
5557
5558 mutex_unlock(&iter->lock);
5559
5560 if (unlikely(n < idx)) /* insertion completed by another thread */
5561 goto lookup;
5562
5563 /* In case we failed to insert the entry into the radixtree, we need
5564 * to look beyond the current sg.
5565 */
5566 while (idx + count <= n) {
5567 idx += count;
5568 sg = ____sg_next(sg);
5569 count = __sg_page_count(sg);
5570 }
5571
5572 *offset = n - idx;
5573 return sg;
5574
5575lookup:
5576 rcu_read_lock();
5577
5578 sg = radix_tree_lookup(&iter->radix, n);
5579 GEM_BUG_ON(!sg);
5580
5581 /* If this index is in the middle of multi-page sg entry,
Matthew Wilcox3159f942017-11-03 13:30:42 -04005582 * the radix tree will contain a value entry that points
Chris Wilson96d77632016-10-28 13:58:33 +01005583 * to the start of that range. We will return the pointer to
5584 * the base page and the offset of this page within the
5585 * sg entry's range.
5586 */
5587 *offset = 0;
Matthew Wilcox3159f942017-11-03 13:30:42 -04005588 if (unlikely(xa_is_value(sg))) {
5589 unsigned long base = xa_to_value(sg);
Chris Wilson96d77632016-10-28 13:58:33 +01005590
5591 sg = radix_tree_lookup(&iter->radix, base);
5592 GEM_BUG_ON(!sg);
5593
5594 *offset = n - base;
5595 }
5596
5597 rcu_read_unlock();
5598
5599 return sg;
5600}
5601
5602struct page *
5603i915_gem_object_get_page(struct drm_i915_gem_object *obj, unsigned int n)
5604{
5605 struct scatterlist *sg;
5606 unsigned int offset;
5607
5608 GEM_BUG_ON(!i915_gem_object_has_struct_page(obj));
5609
5610 sg = i915_gem_object_get_sg(obj, n, &offset);
5611 return nth_page(sg_page(sg), offset);
5612}
5613
5614/* Like i915_gem_object_get_page(), but mark the returned page dirty */
5615struct page *
5616i915_gem_object_get_dirty_page(struct drm_i915_gem_object *obj,
5617 unsigned int n)
5618{
5619 struct page *page;
5620
5621 page = i915_gem_object_get_page(obj, n);
Chris Wilsona4f5ea62016-10-28 13:58:35 +01005622 if (!obj->mm.dirty)
Chris Wilson96d77632016-10-28 13:58:33 +01005623 set_page_dirty(page);
5624
5625 return page;
5626}
5627
5628dma_addr_t
5629i915_gem_object_get_dma_address(struct drm_i915_gem_object *obj,
5630 unsigned long n)
5631{
5632 struct scatterlist *sg;
5633 unsigned int offset;
5634
5635 sg = i915_gem_object_get_sg(obj, n, &offset);
5636 return sg_dma_address(sg) + (offset << PAGE_SHIFT);
5637}
Chris Wilson935a2f72017-02-13 17:15:13 +00005638
Chris Wilson8eeb7902017-07-26 19:16:01 +01005639int i915_gem_object_attach_phys(struct drm_i915_gem_object *obj, int align)
5640{
5641 struct sg_table *pages;
5642 int err;
5643
5644 if (align > obj->base.size)
5645 return -EINVAL;
5646
5647 if (obj->ops == &i915_gem_phys_ops)
5648 return 0;
5649
5650 if (obj->ops != &i915_gem_object_ops)
5651 return -EINVAL;
5652
5653 err = i915_gem_object_unbind(obj);
5654 if (err)
5655 return err;
5656
5657 mutex_lock(&obj->mm.lock);
5658
5659 if (obj->mm.madv != I915_MADV_WILLNEED) {
5660 err = -EFAULT;
5661 goto err_unlock;
5662 }
5663
5664 if (obj->mm.quirked) {
5665 err = -EFAULT;
5666 goto err_unlock;
5667 }
5668
5669 if (obj->mm.mapping) {
5670 err = -EBUSY;
5671 goto err_unlock;
5672 }
5673
Chris Wilsonacd1c1e2018-06-11 08:55:32 +01005674 pages = __i915_gem_object_unset_pages(obj);
Chris Wilsonf2123812017-10-16 12:40:37 +01005675
Chris Wilson8eeb7902017-07-26 19:16:01 +01005676 obj->ops = &i915_gem_phys_ops;
5677
Chris Wilson8fb6a5d2017-07-26 19:16:02 +01005678 err = ____i915_gem_object_get_pages(obj);
Chris Wilson8eeb7902017-07-26 19:16:01 +01005679 if (err)
5680 goto err_xfer;
5681
5682 /* Perma-pin (until release) the physical set of pages */
5683 __i915_gem_object_pin_pages(obj);
5684
5685 if (!IS_ERR_OR_NULL(pages))
5686 i915_gem_object_ops.put_pages(obj, pages);
5687 mutex_unlock(&obj->mm.lock);
5688 return 0;
5689
5690err_xfer:
5691 obj->ops = &i915_gem_object_ops;
Chris Wilsonacd1c1e2018-06-11 08:55:32 +01005692 if (!IS_ERR_OR_NULL(pages)) {
5693 unsigned int sg_page_sizes = i915_sg_page_sizes(pages->sgl);
5694
5695 __i915_gem_object_set_pages(obj, pages, sg_page_sizes);
5696 }
Chris Wilson8eeb7902017-07-26 19:16:01 +01005697err_unlock:
5698 mutex_unlock(&obj->mm.lock);
5699 return err;
5700}
5701
Chris Wilson935a2f72017-02-13 17:15:13 +00005702#if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
5703#include "selftests/scatterlist.c"
Chris Wilson66d9cb52017-02-13 17:15:17 +00005704#include "selftests/mock_gem_device.c"
Chris Wilson44653982017-02-13 17:15:20 +00005705#include "selftests/huge_gem_object.c"
Matthew Auld40498662017-10-06 23:18:29 +01005706#include "selftests/huge_pages.c"
Chris Wilson8335fd62017-02-13 17:15:28 +00005707#include "selftests/i915_gem_object.c"
Chris Wilson17059452017-02-13 17:15:32 +00005708#include "selftests/i915_gem_coherency.c"
Chris Wilson3f51b7e12018-08-30 14:48:06 +01005709#include "selftests/i915_gem.c"
Chris Wilson935a2f72017-02-13 17:15:13 +00005710#endif