blob: 538fa5404603396e70f05a27da9598712229f61c [file] [log] [blame]
Eric Anholt673a3942008-07-30 12:06:12 -07001/*
Daniel Vetterbe6a0372015-03-18 10:46:04 +01002 * Copyright © 2008-2015 Intel Corporation
Eric Anholt673a3942008-07-30 12:06:12 -07003 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 *
23 * Authors:
24 * Eric Anholt <eric@anholt.net>
25 *
26 */
27
David Herrmann0de23972013-07-24 21:07:52 +020028#include <drm/drm_vma_manager.h>
David Howells760285e2012-10-02 18:01:07 +010029#include <drm/i915_drm.h>
Chris Wilson6b5e90f2016-11-14 20:41:05 +000030#include <linux/dma-fence-array.h>
Chris Wilsonfe3288b2017-02-12 17:20:01 +000031#include <linux/kthread.h>
Chris Wilsonc13d87e2016-07-20 09:21:15 +010032#include <linux/reservation.h>
Hugh Dickins5949eac2011-06-27 16:18:18 -070033#include <linux/shmem_fs.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090034#include <linux/slab.h>
Chris Wilson20e49332016-11-22 14:41:21 +000035#include <linux/stop_machine.h>
Eric Anholt673a3942008-07-30 12:06:12 -070036#include <linux/swap.h>
Jesse Barnes79e53942008-11-07 14:24:08 -080037#include <linux/pci.h>
Daniel Vetter1286ff72012-05-10 15:25:09 +020038#include <linux/dma-buf.h>
Eric Anholt673a3942008-07-30 12:06:12 -070039
Chris Wilson9f588922019-01-16 15:33:04 +000040#include "i915_drv.h"
41#include "i915_gem_clflush.h"
42#include "i915_gemfs.h"
43#include "i915_reset.h"
44#include "i915_trace.h"
45#include "i915_vgpu.h"
46
47#include "intel_drv.h"
48#include "intel_frontbuffer.h"
49#include "intel_mocs.h"
50#include "intel_workarounds.h"
51
Chris Wilsonfbbd37b2016-10-28 13:58:42 +010052static void i915_gem_flush_free_objects(struct drm_i915_private *i915);
Chris Wilson61050802012-04-17 15:31:31 +010053
Chris Wilson2c225692013-08-09 12:26:45 +010054static bool cpu_write_needs_clflush(struct drm_i915_gem_object *obj)
55{
Chris Wilsone27ab732017-06-15 13:38:49 +010056 if (obj->cache_dirty)
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +053057 return false;
58
Chris Wilsonb8f55be2017-08-11 12:11:16 +010059 if (!(obj->cache_coherent & I915_BO_CACHE_COHERENT_FOR_WRITE))
Chris Wilson2c225692013-08-09 12:26:45 +010060 return true;
61
Chris Wilsonbd3d2252017-10-13 21:26:14 +010062 return obj->pin_global; /* currently in use by HW, keep flushed */
Chris Wilson2c225692013-08-09 12:26:45 +010063}
64
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +053065static int
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +010066insert_mappable_node(struct i915_ggtt *ggtt,
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +053067 struct drm_mm_node *node, u32 size)
68{
69 memset(node, 0, sizeof(*node));
Chris Wilson82ad6442018-06-05 16:37:58 +010070 return drm_mm_insert_node_in_range(&ggtt->vm.mm, node,
Chris Wilson4e64e552017-02-02 21:04:38 +000071 size, 0, I915_COLOR_UNEVICTABLE,
72 0, ggtt->mappable_end,
73 DRM_MM_INSERT_LOW);
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +053074}
75
76static void
77remove_mappable_node(struct drm_mm_node *node)
78{
79 drm_mm_remove_node(node);
80}
81
Chris Wilson73aa8082010-09-30 11:46:12 +010082/* some bookkeeping */
83static void i915_gem_info_add_obj(struct drm_i915_private *dev_priv,
Chris Wilson3ef7f222016-10-18 13:02:48 +010084 u64 size)
Chris Wilson73aa8082010-09-30 11:46:12 +010085{
Daniel Vetterc20e8352013-07-24 22:40:23 +020086 spin_lock(&dev_priv->mm.object_stat_lock);
Chris Wilson73aa8082010-09-30 11:46:12 +010087 dev_priv->mm.object_count++;
88 dev_priv->mm.object_memory += size;
Daniel Vetterc20e8352013-07-24 22:40:23 +020089 spin_unlock(&dev_priv->mm.object_stat_lock);
Chris Wilson73aa8082010-09-30 11:46:12 +010090}
91
92static void i915_gem_info_remove_obj(struct drm_i915_private *dev_priv,
Chris Wilson3ef7f222016-10-18 13:02:48 +010093 u64 size)
Chris Wilson73aa8082010-09-30 11:46:12 +010094{
Daniel Vetterc20e8352013-07-24 22:40:23 +020095 spin_lock(&dev_priv->mm.object_stat_lock);
Chris Wilson73aa8082010-09-30 11:46:12 +010096 dev_priv->mm.object_count--;
97 dev_priv->mm.object_memory -= size;
Daniel Vetterc20e8352013-07-24 22:40:23 +020098 spin_unlock(&dev_priv->mm.object_stat_lock);
Chris Wilson73aa8082010-09-30 11:46:12 +010099}
100
Chris Wilson21dd3732011-01-26 15:55:56 +0000101static int
Daniel Vetter33196de2012-11-14 17:14:05 +0100102i915_gem_wait_for_error(struct i915_gpu_error *error)
Chris Wilson30dbf0c2010-09-25 10:19:17 +0100103{
Chris Wilson30dbf0c2010-09-25 10:19:17 +0100104 int ret;
105
Chris Wilson4c7d62c2016-10-28 13:58:32 +0100106 might_sleep();
107
Daniel Vetter0a6759c2012-07-04 22:18:41 +0200108 /*
109 * Only wait 10 seconds for the gpu reset to complete to avoid hanging
110 * userspace. If it takes that long something really bad is going on and
111 * we should simply try to bail out and fail as gracefully as possible.
112 */
Daniel Vetter1f83fee2012-11-15 17:17:22 +0100113 ret = wait_event_interruptible_timeout(error->reset_queue,
Chris Wilson8c185ec2017-03-16 17:13:02 +0000114 !i915_reset_backoff(error),
Chris Wilsonb52992c2016-10-28 13:58:24 +0100115 I915_RESET_TIMEOUT);
Daniel Vetter0a6759c2012-07-04 22:18:41 +0200116 if (ret == 0) {
117 DRM_ERROR("Timed out waiting for the gpu reset to complete\n");
118 return -EIO;
119 } else if (ret < 0) {
Chris Wilson30dbf0c2010-09-25 10:19:17 +0100120 return ret;
Chris Wilsond98c52c2016-04-13 17:35:05 +0100121 } else {
122 return 0;
Daniel Vetter0a6759c2012-07-04 22:18:41 +0200123 }
Chris Wilson30dbf0c2010-09-25 10:19:17 +0100124}
125
Chris Wilson54cf91d2010-11-25 18:00:26 +0000126int i915_mutex_lock_interruptible(struct drm_device *dev)
Chris Wilson76c1dec2010-09-25 11:22:51 +0100127{
Chris Wilsonfac5e232016-07-04 11:34:36 +0100128 struct drm_i915_private *dev_priv = to_i915(dev);
Chris Wilson76c1dec2010-09-25 11:22:51 +0100129 int ret;
130
Daniel Vetter33196de2012-11-14 17:14:05 +0100131 ret = i915_gem_wait_for_error(&dev_priv->gpu_error);
Chris Wilson76c1dec2010-09-25 11:22:51 +0100132 if (ret)
133 return ret;
134
135 ret = mutex_lock_interruptible(&dev->struct_mutex);
136 if (ret)
137 return ret;
138
Chris Wilson76c1dec2010-09-25 11:22:51 +0100139 return 0;
140}
Chris Wilson30dbf0c2010-09-25 10:19:17 +0100141
Chris Wilsone4d20062018-04-06 16:51:44 +0100142static u32 __i915_gem_park(struct drm_i915_private *i915)
143{
Chris Wilson506d1f62019-01-14 14:21:11 +0000144 intel_wakeref_t wakeref;
145
Chris Wilson4dfacb02018-05-31 09:22:43 +0100146 GEM_TRACE("\n");
147
Chris Wilsone4d20062018-04-06 16:51:44 +0100148 lockdep_assert_held(&i915->drm.struct_mutex);
149 GEM_BUG_ON(i915->gt.active_requests);
Chris Wilson643b4502018-04-30 14:15:03 +0100150 GEM_BUG_ON(!list_empty(&i915->gt.active_rings));
Chris Wilsone4d20062018-04-06 16:51:44 +0100151
152 if (!i915->gt.awake)
153 return I915_EPOCH_INVALID;
154
155 GEM_BUG_ON(i915->gt.epoch == I915_EPOCH_INVALID);
156
157 /*
158 * Be paranoid and flush a concurrent interrupt to make sure
159 * we don't reactivate any irq tasklets after parking.
160 *
161 * FIXME: Note that even though we have waited for execlists to be idle,
162 * there may still be an in-flight interrupt even though the CSB
163 * is now empty. synchronize_irq() makes sure that a residual interrupt
164 * is completed before we continue, but it doesn't prevent the HW from
165 * raising a spurious interrupt later. To complete the shield we should
166 * coordinate disabling the CS irq with flushing the interrupts.
167 */
168 synchronize_irq(i915->drm.irq);
169
170 intel_engines_park(i915);
Chris Wilsona89d1f92018-05-02 17:38:39 +0100171 i915_timelines_park(i915);
Chris Wilsone4d20062018-04-06 16:51:44 +0100172
173 i915_pmu_gt_parked(i915);
Chris Wilson3365e222018-05-03 20:51:14 +0100174 i915_vma_parked(i915);
Chris Wilsone4d20062018-04-06 16:51:44 +0100175
Chris Wilson506d1f62019-01-14 14:21:11 +0000176 wakeref = fetch_and_zero(&i915->gt.awake);
177 GEM_BUG_ON(!wakeref);
Chris Wilsone4d20062018-04-06 16:51:44 +0100178
179 if (INTEL_GEN(i915) >= 6)
180 gen6_rps_idle(i915);
181
Chris Wilson8d761e72019-01-14 14:21:28 +0000182 intel_display_power_put(i915, POWER_DOMAIN_GT_IRQ, wakeref);
Chris Wilsone4d20062018-04-06 16:51:44 +0100183
184 return i915->gt.epoch;
185}
186
187void i915_gem_park(struct drm_i915_private *i915)
188{
Chris Wilson4dfacb02018-05-31 09:22:43 +0100189 GEM_TRACE("\n");
190
Chris Wilsone4d20062018-04-06 16:51:44 +0100191 lockdep_assert_held(&i915->drm.struct_mutex);
192 GEM_BUG_ON(i915->gt.active_requests);
193
194 if (!i915->gt.awake)
195 return;
196
197 /* Defer the actual call to __i915_gem_park() to prevent ping-pongs */
198 mod_delayed_work(i915->wq, &i915->gt.idle_work, msecs_to_jiffies(100));
199}
200
201void i915_gem_unpark(struct drm_i915_private *i915)
202{
Chris Wilson4dfacb02018-05-31 09:22:43 +0100203 GEM_TRACE("\n");
204
Chris Wilsone4d20062018-04-06 16:51:44 +0100205 lockdep_assert_held(&i915->drm.struct_mutex);
206 GEM_BUG_ON(!i915->gt.active_requests);
Chris Wilson8d761e72019-01-14 14:21:28 +0000207 assert_rpm_wakelock_held(i915);
Chris Wilsone4d20062018-04-06 16:51:44 +0100208
209 if (i915->gt.awake)
210 return;
211
Chris Wilsone4d20062018-04-06 16:51:44 +0100212 /*
213 * It seems that the DMC likes to transition between the DC states a lot
214 * when there are no connected displays (no active power domains) during
215 * command submission.
216 *
217 * This activity has negative impact on the performance of the chip with
218 * huge latencies observed in the interrupt handler and elsewhere.
219 *
220 * Work around it by grabbing a GT IRQ power domain whilst there is any
221 * GT activity, preventing any DC state transitions.
222 */
Chris Wilson8d761e72019-01-14 14:21:28 +0000223 i915->gt.awake = intel_display_power_get(i915, POWER_DOMAIN_GT_IRQ);
224 GEM_BUG_ON(!i915->gt.awake);
Chris Wilsone4d20062018-04-06 16:51:44 +0100225
Chris Wilsone4d20062018-04-06 16:51:44 +0100226 if (unlikely(++i915->gt.epoch == 0)) /* keep 0 as invalid */
227 i915->gt.epoch = 1;
228
229 intel_enable_gt_powersave(i915);
230 i915_update_gfx_val(i915);
231 if (INTEL_GEN(i915) >= 6)
232 gen6_rps_busy(i915);
233 i915_pmu_gt_unparked(i915);
234
235 intel_engines_unpark(i915);
236
237 i915_queue_hangcheck(i915);
238
239 queue_delayed_work(i915->wq,
240 &i915->gt.retire_work,
241 round_jiffies_up_relative(HZ));
242}
243
Eric Anholt673a3942008-07-30 12:06:12 -0700244int
Eric Anholt5a125c32008-10-22 21:40:13 -0700245i915_gem_get_aperture_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +0000246 struct drm_file *file)
Eric Anholt5a125c32008-10-22 21:40:13 -0700247{
Chris Wilson09d7e462019-01-28 10:23:53 +0000248 struct i915_ggtt *ggtt = &to_i915(dev)->ggtt;
Joonas Lahtinen72e96d62016-03-30 16:57:10 +0300249 struct drm_i915_gem_get_aperture *args = data;
Tvrtko Ursulinca1543b2015-07-01 11:51:10 +0100250 struct i915_vma *vma;
Weinan Liff8f7972017-05-31 10:35:52 +0800251 u64 pinned;
Eric Anholt5a125c32008-10-22 21:40:13 -0700252
Chris Wilson09d7e462019-01-28 10:23:53 +0000253 mutex_lock(&ggtt->vm.mutex);
254
Chris Wilson82ad6442018-06-05 16:37:58 +0100255 pinned = ggtt->vm.reserved;
Chris Wilson499197d2019-01-28 10:23:52 +0000256 list_for_each_entry(vma, &ggtt->vm.bound_list, vm_link)
Chris Wilson20dfbde2016-08-04 16:32:30 +0100257 if (i915_vma_is_pinned(vma))
Tvrtko Ursulinca1543b2015-07-01 11:51:10 +0100258 pinned += vma->node.size;
Chris Wilson09d7e462019-01-28 10:23:53 +0000259
260 mutex_unlock(&ggtt->vm.mutex);
Eric Anholt5a125c32008-10-22 21:40:13 -0700261
Chris Wilson82ad6442018-06-05 16:37:58 +0100262 args->aper_size = ggtt->vm.total;
Akshay Joshi0206e352011-08-16 15:34:10 -0400263 args->aper_available_size = args->aper_size - pinned;
Chris Wilson6299f992010-11-24 12:23:44 +0000264
Eric Anholt5a125c32008-10-22 21:40:13 -0700265 return 0;
266}
267
Matthew Auldb91b09e2017-10-06 23:18:17 +0100268static int i915_gem_object_get_pages_phys(struct drm_i915_gem_object *obj)
Chris Wilson00731152014-05-21 12:42:56 +0100269{
Al Viro93c76a32015-12-04 23:45:44 -0500270 struct address_space *mapping = obj->base.filp->f_mapping;
Chris Wilsondbb43512016-12-07 13:34:11 +0000271 drm_dma_handle_t *phys;
Chris Wilson6a2c4232014-11-04 04:51:40 -0800272 struct sg_table *st;
273 struct scatterlist *sg;
Chris Wilsondbb43512016-12-07 13:34:11 +0000274 char *vaddr;
Chris Wilson6a2c4232014-11-04 04:51:40 -0800275 int i;
Matthew Auldb91b09e2017-10-06 23:18:17 +0100276 int err;
Chris Wilson00731152014-05-21 12:42:56 +0100277
Chris Wilson6a2c4232014-11-04 04:51:40 -0800278 if (WARN_ON(i915_gem_object_needs_bit17_swizzle(obj)))
Matthew Auldb91b09e2017-10-06 23:18:17 +0100279 return -EINVAL;
Chris Wilson00731152014-05-21 12:42:56 +0100280
Chris Wilsondbb43512016-12-07 13:34:11 +0000281 /* Always aligning to the object size, allows a single allocation
282 * to handle all possible callers, and given typical object sizes,
283 * the alignment of the buddy allocation will naturally match.
284 */
285 phys = drm_pci_alloc(obj->base.dev,
Ville Syrjälä750fae22017-09-07 17:32:03 +0300286 roundup_pow_of_two(obj->base.size),
Chris Wilsondbb43512016-12-07 13:34:11 +0000287 roundup_pow_of_two(obj->base.size));
288 if (!phys)
Matthew Auldb91b09e2017-10-06 23:18:17 +0100289 return -ENOMEM;
Chris Wilsondbb43512016-12-07 13:34:11 +0000290
291 vaddr = phys->vaddr;
Chris Wilson6a2c4232014-11-04 04:51:40 -0800292 for (i = 0; i < obj->base.size / PAGE_SIZE; i++) {
293 struct page *page;
294 char *src;
295
296 page = shmem_read_mapping_page(mapping, i);
Chris Wilsondbb43512016-12-07 13:34:11 +0000297 if (IS_ERR(page)) {
Matthew Auldb91b09e2017-10-06 23:18:17 +0100298 err = PTR_ERR(page);
Chris Wilsondbb43512016-12-07 13:34:11 +0000299 goto err_phys;
300 }
Chris Wilson6a2c4232014-11-04 04:51:40 -0800301
302 src = kmap_atomic(page);
303 memcpy(vaddr, src, PAGE_SIZE);
304 drm_clflush_virt_range(vaddr, PAGE_SIZE);
305 kunmap_atomic(src);
306
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300307 put_page(page);
Chris Wilson6a2c4232014-11-04 04:51:40 -0800308 vaddr += PAGE_SIZE;
309 }
310
Chris Wilsonc0336662016-05-06 15:40:21 +0100311 i915_gem_chipset_flush(to_i915(obj->base.dev));
Chris Wilson6a2c4232014-11-04 04:51:40 -0800312
313 st = kmalloc(sizeof(*st), GFP_KERNEL);
Chris Wilsondbb43512016-12-07 13:34:11 +0000314 if (!st) {
Matthew Auldb91b09e2017-10-06 23:18:17 +0100315 err = -ENOMEM;
Chris Wilsondbb43512016-12-07 13:34:11 +0000316 goto err_phys;
317 }
Chris Wilson6a2c4232014-11-04 04:51:40 -0800318
319 if (sg_alloc_table(st, 1, GFP_KERNEL)) {
320 kfree(st);
Matthew Auldb91b09e2017-10-06 23:18:17 +0100321 err = -ENOMEM;
Chris Wilsondbb43512016-12-07 13:34:11 +0000322 goto err_phys;
Chris Wilson6a2c4232014-11-04 04:51:40 -0800323 }
324
325 sg = st->sgl;
326 sg->offset = 0;
327 sg->length = obj->base.size;
328
Chris Wilsondbb43512016-12-07 13:34:11 +0000329 sg_dma_address(sg) = phys->busaddr;
Chris Wilson6a2c4232014-11-04 04:51:40 -0800330 sg_dma_len(sg) = obj->base.size;
331
Chris Wilsondbb43512016-12-07 13:34:11 +0000332 obj->phys_handle = phys;
Matthew Auldb91b09e2017-10-06 23:18:17 +0100333
Matthew Aulda5c081662017-10-06 23:18:18 +0100334 __i915_gem_object_set_pages(obj, st, sg->length);
Matthew Auldb91b09e2017-10-06 23:18:17 +0100335
336 return 0;
Chris Wilsondbb43512016-12-07 13:34:11 +0000337
338err_phys:
339 drm_pci_free(obj->base.dev, phys);
Matthew Auldb91b09e2017-10-06 23:18:17 +0100340
341 return err;
Chris Wilson6a2c4232014-11-04 04:51:40 -0800342}
343
Chris Wilsone27ab732017-06-15 13:38:49 +0100344static void __start_cpu_write(struct drm_i915_gem_object *obj)
345{
Christian Königc0a51fd2018-02-16 13:43:38 +0100346 obj->read_domains = I915_GEM_DOMAIN_CPU;
347 obj->write_domain = I915_GEM_DOMAIN_CPU;
Chris Wilsone27ab732017-06-15 13:38:49 +0100348 if (cpu_write_needs_clflush(obj))
349 obj->cache_dirty = true;
350}
351
Chris Wilson6a2c4232014-11-04 04:51:40 -0800352static void
Chris Wilson2b3c8312016-11-11 14:58:09 +0000353__i915_gem_object_release_shmem(struct drm_i915_gem_object *obj,
Chris Wilsone5facdf2016-12-23 14:57:57 +0000354 struct sg_table *pages,
355 bool needs_clflush)
Chris Wilson6a2c4232014-11-04 04:51:40 -0800356{
Chris Wilsona4f5ea62016-10-28 13:58:35 +0100357 GEM_BUG_ON(obj->mm.madv == __I915_MADV_PURGED);
Chris Wilson6a2c4232014-11-04 04:51:40 -0800358
Chris Wilsona4f5ea62016-10-28 13:58:35 +0100359 if (obj->mm.madv == I915_MADV_DONTNEED)
360 obj->mm.dirty = false;
Chris Wilson6a2c4232014-11-04 04:51:40 -0800361
Chris Wilsone5facdf2016-12-23 14:57:57 +0000362 if (needs_clflush &&
Christian Königc0a51fd2018-02-16 13:43:38 +0100363 (obj->read_domains & I915_GEM_DOMAIN_CPU) == 0 &&
Chris Wilsonb8f55be2017-08-11 12:11:16 +0100364 !(obj->cache_coherent & I915_BO_CACHE_COHERENT_FOR_READ))
Chris Wilson2b3c8312016-11-11 14:58:09 +0000365 drm_clflush_sg(pages);
Chris Wilson03ac84f2016-10-28 13:58:36 +0100366
Chris Wilsone27ab732017-06-15 13:38:49 +0100367 __start_cpu_write(obj);
Chris Wilson03ac84f2016-10-28 13:58:36 +0100368}
369
370static void
371i915_gem_object_put_pages_phys(struct drm_i915_gem_object *obj,
372 struct sg_table *pages)
373{
Chris Wilsone5facdf2016-12-23 14:57:57 +0000374 __i915_gem_object_release_shmem(obj, pages, false);
Chris Wilson03ac84f2016-10-28 13:58:36 +0100375
Chris Wilsona4f5ea62016-10-28 13:58:35 +0100376 if (obj->mm.dirty) {
Al Viro93c76a32015-12-04 23:45:44 -0500377 struct address_space *mapping = obj->base.filp->f_mapping;
Chris Wilson6a2c4232014-11-04 04:51:40 -0800378 char *vaddr = obj->phys_handle->vaddr;
Chris Wilson00731152014-05-21 12:42:56 +0100379 int i;
380
381 for (i = 0; i < obj->base.size / PAGE_SIZE; i++) {
Chris Wilson6a2c4232014-11-04 04:51:40 -0800382 struct page *page;
383 char *dst;
Chris Wilson00731152014-05-21 12:42:56 +0100384
Chris Wilson6a2c4232014-11-04 04:51:40 -0800385 page = shmem_read_mapping_page(mapping, i);
386 if (IS_ERR(page))
387 continue;
388
389 dst = kmap_atomic(page);
390 drm_clflush_virt_range(vaddr, PAGE_SIZE);
391 memcpy(dst, vaddr, PAGE_SIZE);
392 kunmap_atomic(dst);
393
394 set_page_dirty(page);
Chris Wilsona4f5ea62016-10-28 13:58:35 +0100395 if (obj->mm.madv == I915_MADV_WILLNEED)
Chris Wilson00731152014-05-21 12:42:56 +0100396 mark_page_accessed(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300397 put_page(page);
Chris Wilson00731152014-05-21 12:42:56 +0100398 vaddr += PAGE_SIZE;
399 }
Chris Wilsona4f5ea62016-10-28 13:58:35 +0100400 obj->mm.dirty = false;
Chris Wilson00731152014-05-21 12:42:56 +0100401 }
402
Chris Wilson03ac84f2016-10-28 13:58:36 +0100403 sg_free_table(pages);
404 kfree(pages);
Chris Wilsondbb43512016-12-07 13:34:11 +0000405
406 drm_pci_free(obj->base.dev, obj->phys_handle);
Chris Wilson6a2c4232014-11-04 04:51:40 -0800407}
408
409static void
410i915_gem_object_release_phys(struct drm_i915_gem_object *obj)
411{
Chris Wilsona4f5ea62016-10-28 13:58:35 +0100412 i915_gem_object_unpin_pages(obj);
Chris Wilson6a2c4232014-11-04 04:51:40 -0800413}
414
415static const struct drm_i915_gem_object_ops i915_gem_phys_ops = {
416 .get_pages = i915_gem_object_get_pages_phys,
417 .put_pages = i915_gem_object_put_pages_phys,
418 .release = i915_gem_object_release_phys,
419};
420
Chris Wilson581ab1f2017-02-15 16:39:00 +0000421static const struct drm_i915_gem_object_ops i915_gem_object_ops;
422
Chris Wilson35a96112016-08-14 18:44:40 +0100423int i915_gem_object_unbind(struct drm_i915_gem_object *obj)
Chris Wilsonaa653a62016-08-04 07:52:27 +0100424{
425 struct i915_vma *vma;
426 LIST_HEAD(still_in_list);
Chris Wilson02bef8f2016-08-14 18:44:41 +0100427 int ret;
Chris Wilsonaa653a62016-08-04 07:52:27 +0100428
Chris Wilson02bef8f2016-08-14 18:44:41 +0100429 lockdep_assert_held(&obj->base.dev->struct_mutex);
430
431 /* Closed vma are removed from the obj->vma_list - but they may
432 * still have an active binding on the object. To remove those we
433 * must wait for all rendering to complete to the object (as unbinding
434 * must anyway), and retire the requests.
Chris Wilsonaa653a62016-08-04 07:52:27 +0100435 */
Chris Wilson5888fc92017-12-04 13:25:13 +0000436 ret = i915_gem_object_set_to_cpu_domain(obj, false);
Chris Wilson02bef8f2016-08-14 18:44:41 +0100437 if (ret)
438 return ret;
439
Chris Wilsonaa653a62016-08-04 07:52:27 +0100440 while ((vma = list_first_entry_or_null(&obj->vma_list,
441 struct i915_vma,
442 obj_link))) {
443 list_move_tail(&vma->obj_link, &still_in_list);
444 ret = i915_vma_unbind(vma);
445 if (ret)
446 break;
447 }
448 list_splice(&still_in_list, &obj->vma_list);
449
450 return ret;
451}
452
Chris Wilsone95433c2016-10-28 13:58:27 +0100453static long
454i915_gem_object_wait_fence(struct dma_fence *fence,
455 unsigned int flags,
456 long timeout,
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +0100457 struct intel_rps_client *rps_client)
Chris Wilsone95433c2016-10-28 13:58:27 +0100458{
Chris Wilsone61e0f52018-02-21 09:56:36 +0000459 struct i915_request *rq;
Chris Wilsone95433c2016-10-28 13:58:27 +0100460
461 BUILD_BUG_ON(I915_WAIT_INTERRUPTIBLE != 0x1);
462
463 if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags))
464 return timeout;
465
466 if (!dma_fence_is_i915(fence))
467 return dma_fence_wait_timeout(fence,
468 flags & I915_WAIT_INTERRUPTIBLE,
469 timeout);
470
471 rq = to_request(fence);
Chris Wilsone61e0f52018-02-21 09:56:36 +0000472 if (i915_request_completed(rq))
Chris Wilsone95433c2016-10-28 13:58:27 +0100473 goto out;
474
Chris Wilsone9af4ea2018-01-18 13:16:09 +0000475 /*
476 * This client is about to stall waiting for the GPU. In many cases
Chris Wilsone95433c2016-10-28 13:58:27 +0100477 * this is undesirable and limits the throughput of the system, as
478 * many clients cannot continue processing user input/output whilst
479 * blocked. RPS autotuning may take tens of milliseconds to respond
480 * to the GPU load and thus incurs additional latency for the client.
481 * We can circumvent that by promoting the GPU frequency to maximum
482 * before we wait. This makes the GPU throttle up much more quickly
483 * (good for benchmarks and user experience, e.g. window animations),
484 * but at a cost of spending more power processing the workload
485 * (bad for battery). Not all clients even want their results
486 * immediately and for them we should just let the GPU select its own
487 * frequency to maximise efficiency. To prevent a single client from
488 * forcing the clocks too high for the whole system, we only allow
489 * each client to waitboost once in a busy period.
490 */
Chris Wilsone61e0f52018-02-21 09:56:36 +0000491 if (rps_client && !i915_request_started(rq)) {
Chris Wilsone95433c2016-10-28 13:58:27 +0100492 if (INTEL_GEN(rq->i915) >= 6)
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +0100493 gen6_rps_boost(rq, rps_client);
Chris Wilsone95433c2016-10-28 13:58:27 +0100494 }
495
Chris Wilsone61e0f52018-02-21 09:56:36 +0000496 timeout = i915_request_wait(rq, flags, timeout);
Chris Wilsone95433c2016-10-28 13:58:27 +0100497
498out:
Chris Wilsone61e0f52018-02-21 09:56:36 +0000499 if (flags & I915_WAIT_LOCKED && i915_request_completed(rq))
500 i915_request_retire_upto(rq);
Chris Wilsone95433c2016-10-28 13:58:27 +0100501
Chris Wilsone95433c2016-10-28 13:58:27 +0100502 return timeout;
503}
504
505static long
506i915_gem_object_wait_reservation(struct reservation_object *resv,
507 unsigned int flags,
508 long timeout,
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +0100509 struct intel_rps_client *rps_client)
Chris Wilsone95433c2016-10-28 13:58:27 +0100510{
Chris Wilsone54ca972017-02-17 15:13:04 +0000511 unsigned int seq = __read_seqcount_begin(&resv->seq);
Chris Wilsone95433c2016-10-28 13:58:27 +0100512 struct dma_fence *excl;
Chris Wilsone54ca972017-02-17 15:13:04 +0000513 bool prune_fences = false;
Chris Wilsone95433c2016-10-28 13:58:27 +0100514
515 if (flags & I915_WAIT_ALL) {
516 struct dma_fence **shared;
517 unsigned int count, i;
518 int ret;
519
520 ret = reservation_object_get_fences_rcu(resv,
521 &excl, &count, &shared);
522 if (ret)
523 return ret;
524
525 for (i = 0; i < count; i++) {
526 timeout = i915_gem_object_wait_fence(shared[i],
527 flags, timeout,
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +0100528 rps_client);
Chris Wilsond892e932017-02-12 21:53:43 +0000529 if (timeout < 0)
Chris Wilsone95433c2016-10-28 13:58:27 +0100530 break;
531
532 dma_fence_put(shared[i]);
533 }
534
535 for (; i < count; i++)
536 dma_fence_put(shared[i]);
537 kfree(shared);
Chris Wilsone54ca972017-02-17 15:13:04 +0000538
Chris Wilsonfa730552018-03-07 17:13:03 +0000539 /*
540 * If both shared fences and an exclusive fence exist,
541 * then by construction the shared fences must be later
542 * than the exclusive fence. If we successfully wait for
543 * all the shared fences, we know that the exclusive fence
544 * must all be signaled. If all the shared fences are
545 * signaled, we can prune the array and recover the
546 * floating references on the fences/requests.
547 */
Chris Wilsone54ca972017-02-17 15:13:04 +0000548 prune_fences = count && timeout >= 0;
Chris Wilsone95433c2016-10-28 13:58:27 +0100549 } else {
550 excl = reservation_object_get_excl_rcu(resv);
551 }
552
Chris Wilsonfa730552018-03-07 17:13:03 +0000553 if (excl && timeout >= 0)
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +0100554 timeout = i915_gem_object_wait_fence(excl, flags, timeout,
555 rps_client);
Chris Wilsone95433c2016-10-28 13:58:27 +0100556
557 dma_fence_put(excl);
558
Chris Wilsonfa730552018-03-07 17:13:03 +0000559 /*
560 * Opportunistically prune the fences iff we know they have *all* been
Chris Wilson03d1cac2017-03-08 13:26:28 +0000561 * signaled and that the reservation object has not been changed (i.e.
562 * no new fences have been added).
563 */
Chris Wilsone54ca972017-02-17 15:13:04 +0000564 if (prune_fences && !__read_seqcount_retry(&resv->seq, seq)) {
Chris Wilson03d1cac2017-03-08 13:26:28 +0000565 if (reservation_object_trylock(resv)) {
566 if (!__read_seqcount_retry(&resv->seq, seq))
567 reservation_object_add_excl_fence(resv, NULL);
568 reservation_object_unlock(resv);
569 }
Chris Wilsone54ca972017-02-17 15:13:04 +0000570 }
571
Chris Wilsone95433c2016-10-28 13:58:27 +0100572 return timeout;
573}
574
Chris Wilsonb7268c52018-04-18 19:40:52 +0100575static void __fence_set_priority(struct dma_fence *fence,
576 const struct i915_sched_attr *attr)
Chris Wilson6b5e90f2016-11-14 20:41:05 +0000577{
Chris Wilsone61e0f52018-02-21 09:56:36 +0000578 struct i915_request *rq;
Chris Wilson6b5e90f2016-11-14 20:41:05 +0000579 struct intel_engine_cs *engine;
580
Chris Wilsonc218ee02018-01-06 10:56:18 +0000581 if (dma_fence_is_signaled(fence) || !dma_fence_is_i915(fence))
Chris Wilson6b5e90f2016-11-14 20:41:05 +0000582 return;
583
584 rq = to_request(fence);
585 engine = rq->engine;
Chris Wilson6b5e90f2016-11-14 20:41:05 +0000586
Chris Wilson4f6d8fc2018-05-07 14:57:25 +0100587 local_bh_disable();
588 rcu_read_lock(); /* RCU serialisation for set-wedged protection */
Chris Wilson47650db2018-03-07 13:42:25 +0000589 if (engine->schedule)
Chris Wilsonb7268c52018-04-18 19:40:52 +0100590 engine->schedule(rq, attr);
Chris Wilson47650db2018-03-07 13:42:25 +0000591 rcu_read_unlock();
Chris Wilson4f6d8fc2018-05-07 14:57:25 +0100592 local_bh_enable(); /* kick the tasklets if queues were reprioritised */
Chris Wilson6b5e90f2016-11-14 20:41:05 +0000593}
594
Chris Wilsonb7268c52018-04-18 19:40:52 +0100595static void fence_set_priority(struct dma_fence *fence,
596 const struct i915_sched_attr *attr)
Chris Wilson6b5e90f2016-11-14 20:41:05 +0000597{
598 /* Recurse once into a fence-array */
599 if (dma_fence_is_array(fence)) {
600 struct dma_fence_array *array = to_dma_fence_array(fence);
601 int i;
602
603 for (i = 0; i < array->num_fences; i++)
Chris Wilsonb7268c52018-04-18 19:40:52 +0100604 __fence_set_priority(array->fences[i], attr);
Chris Wilson6b5e90f2016-11-14 20:41:05 +0000605 } else {
Chris Wilsonb7268c52018-04-18 19:40:52 +0100606 __fence_set_priority(fence, attr);
Chris Wilson6b5e90f2016-11-14 20:41:05 +0000607 }
608}
609
610int
611i915_gem_object_wait_priority(struct drm_i915_gem_object *obj,
612 unsigned int flags,
Chris Wilsonb7268c52018-04-18 19:40:52 +0100613 const struct i915_sched_attr *attr)
Chris Wilson6b5e90f2016-11-14 20:41:05 +0000614{
615 struct dma_fence *excl;
616
617 if (flags & I915_WAIT_ALL) {
618 struct dma_fence **shared;
619 unsigned int count, i;
620 int ret;
621
622 ret = reservation_object_get_fences_rcu(obj->resv,
623 &excl, &count, &shared);
624 if (ret)
625 return ret;
626
627 for (i = 0; i < count; i++) {
Chris Wilsonb7268c52018-04-18 19:40:52 +0100628 fence_set_priority(shared[i], attr);
Chris Wilson6b5e90f2016-11-14 20:41:05 +0000629 dma_fence_put(shared[i]);
630 }
631
632 kfree(shared);
633 } else {
634 excl = reservation_object_get_excl_rcu(obj->resv);
635 }
636
637 if (excl) {
Chris Wilsonb7268c52018-04-18 19:40:52 +0100638 fence_set_priority(excl, attr);
Chris Wilson6b5e90f2016-11-14 20:41:05 +0000639 dma_fence_put(excl);
640 }
641 return 0;
642}
643
Chris Wilson00e60f22016-08-04 16:32:40 +0100644/**
Chris Wilsone95433c2016-10-28 13:58:27 +0100645 * Waits for rendering to the object to be completed
Chris Wilson00e60f22016-08-04 16:32:40 +0100646 * @obj: i915 gem object
Chris Wilsone95433c2016-10-28 13:58:27 +0100647 * @flags: how to wait (under a lock, for all rendering or just for writes etc)
648 * @timeout: how long to wait
Chris Wilsona0a8b1c2017-11-09 14:06:44 +0000649 * @rps_client: client (user process) to charge for any waitboosting
Chris Wilson00e60f22016-08-04 16:32:40 +0100650 */
651int
Chris Wilsone95433c2016-10-28 13:58:27 +0100652i915_gem_object_wait(struct drm_i915_gem_object *obj,
653 unsigned int flags,
654 long timeout,
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +0100655 struct intel_rps_client *rps_client)
Chris Wilson00e60f22016-08-04 16:32:40 +0100656{
Chris Wilsone95433c2016-10-28 13:58:27 +0100657 might_sleep();
Chris Wilsone95433c2016-10-28 13:58:27 +0100658 GEM_BUG_ON(timeout < 0);
Chris Wilson00e60f22016-08-04 16:32:40 +0100659
Chris Wilsond07f0e52016-10-28 13:58:44 +0100660 timeout = i915_gem_object_wait_reservation(obj->resv,
661 flags, timeout,
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +0100662 rps_client);
Chris Wilsone95433c2016-10-28 13:58:27 +0100663 return timeout < 0 ? timeout : 0;
Chris Wilson00e60f22016-08-04 16:32:40 +0100664}
665
666static struct intel_rps_client *to_rps_client(struct drm_file *file)
667{
668 struct drm_i915_file_private *fpriv = file->driver_priv;
669
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +0100670 return &fpriv->rps_client;
Chris Wilson00e60f22016-08-04 16:32:40 +0100671}
672
Chris Wilson00731152014-05-21 12:42:56 +0100673static int
674i915_gem_phys_pwrite(struct drm_i915_gem_object *obj,
675 struct drm_i915_gem_pwrite *args,
Chris Wilson03ac84f2016-10-28 13:58:36 +0100676 struct drm_file *file)
Chris Wilson00731152014-05-21 12:42:56 +0100677{
Chris Wilson00731152014-05-21 12:42:56 +0100678 void *vaddr = obj->phys_handle->vaddr + args->offset;
Gustavo Padovan3ed605b2016-04-26 12:32:27 -0300679 char __user *user_data = u64_to_user_ptr(args->data_ptr);
Chris Wilson6a2c4232014-11-04 04:51:40 -0800680
681 /* We manually control the domain here and pretend that it
682 * remains coherent i.e. in the GTT domain, like shmem_pwrite.
683 */
Rodrigo Vivi77a0d1c2015-06-18 11:43:24 -0700684 intel_fb_obj_invalidate(obj, ORIGIN_CPU);
Chris Wilson10466d22017-01-06 15:22:38 +0000685 if (copy_from_user(vaddr, user_data, args->size))
686 return -EFAULT;
Chris Wilson00731152014-05-21 12:42:56 +0100687
Chris Wilson6a2c4232014-11-04 04:51:40 -0800688 drm_clflush_virt_range(vaddr, args->size);
Chris Wilson10466d22017-01-06 15:22:38 +0000689 i915_gem_chipset_flush(to_i915(obj->base.dev));
Paulo Zanoni063e4e62015-02-13 17:23:45 -0200690
Chris Wilsond59b21e2017-02-22 11:40:49 +0000691 intel_fb_obj_flush(obj, ORIGIN_CPU);
Chris Wilson10466d22017-01-06 15:22:38 +0000692 return 0;
Chris Wilson00731152014-05-21 12:42:56 +0100693}
694
Tvrtko Ursulin187685c2016-12-01 14:16:36 +0000695void *i915_gem_object_alloc(struct drm_i915_private *dev_priv)
Chris Wilson42dcedd2012-11-15 11:32:30 +0000696{
Chris Wilsonefab6d82015-04-07 16:20:57 +0100697 return kmem_cache_zalloc(dev_priv->objects, GFP_KERNEL);
Chris Wilson42dcedd2012-11-15 11:32:30 +0000698}
699
700void i915_gem_object_free(struct drm_i915_gem_object *obj)
701{
Chris Wilsonfac5e232016-07-04 11:34:36 +0100702 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
Chris Wilsonefab6d82015-04-07 16:20:57 +0100703 kmem_cache_free(dev_priv->objects, obj);
Chris Wilson42dcedd2012-11-15 11:32:30 +0000704}
705
Dave Airlieff72145b2011-02-07 12:16:14 +1000706static int
707i915_gem_create(struct drm_file *file,
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +0000708 struct drm_i915_private *dev_priv,
Jani Nikula739f3ab2019-01-16 11:15:19 +0200709 u64 size,
710 u32 *handle_p)
Eric Anholt673a3942008-07-30 12:06:12 -0700711{
Chris Wilson05394f32010-11-08 19:18:58 +0000712 struct drm_i915_gem_object *obj;
Pekka Paalanena1a2d1d2009-08-23 12:40:55 +0300713 int ret;
714 u32 handle;
Eric Anholt673a3942008-07-30 12:06:12 -0700715
Dave Airlieff72145b2011-02-07 12:16:14 +1000716 size = roundup(size, PAGE_SIZE);
Chris Wilson8ffc0242011-09-14 14:14:28 +0200717 if (size == 0)
718 return -EINVAL;
Eric Anholt673a3942008-07-30 12:06:12 -0700719
720 /* Allocate the new object */
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +0000721 obj = i915_gem_object_create(dev_priv, size);
Chris Wilsonfe3db792016-04-25 13:32:13 +0100722 if (IS_ERR(obj))
723 return PTR_ERR(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700724
Chris Wilson05394f32010-11-08 19:18:58 +0000725 ret = drm_gem_handle_create(file, &obj->base, &handle);
Chris Wilson202f2fe2010-10-14 13:20:40 +0100726 /* drop reference from allocate - handle holds it now */
Chris Wilsonf0cd5182016-10-28 13:58:43 +0100727 i915_gem_object_put(obj);
Daniel Vetterd861e332013-07-24 23:25:03 +0200728 if (ret)
729 return ret;
Chris Wilson202f2fe2010-10-14 13:20:40 +0100730
Dave Airlieff72145b2011-02-07 12:16:14 +1000731 *handle_p = handle;
Eric Anholt673a3942008-07-30 12:06:12 -0700732 return 0;
733}
734
Dave Airlieff72145b2011-02-07 12:16:14 +1000735int
736i915_gem_dumb_create(struct drm_file *file,
737 struct drm_device *dev,
738 struct drm_mode_create_dumb *args)
739{
740 /* have to work out size/pitch and return them */
Paulo Zanonide45eaf2013-10-18 18:48:24 -0300741 args->pitch = ALIGN(args->width * DIV_ROUND_UP(args->bpp, 8), 64);
Dave Airlieff72145b2011-02-07 12:16:14 +1000742 args->size = args->pitch * args->height;
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +0000743 return i915_gem_create(file, to_i915(dev),
Dave Airlieda6b51d2014-12-24 13:11:17 +1000744 args->size, &args->handle);
Dave Airlieff72145b2011-02-07 12:16:14 +1000745}
746
Chris Wilsone27ab732017-06-15 13:38:49 +0100747static bool gpu_write_needs_clflush(struct drm_i915_gem_object *obj)
748{
749 return !(obj->cache_level == I915_CACHE_NONE ||
750 obj->cache_level == I915_CACHE_WT);
751}
752
Dave Airlieff72145b2011-02-07 12:16:14 +1000753/**
754 * Creates a new mm object and returns a handle to it.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +0100755 * @dev: drm device pointer
756 * @data: ioctl data blob
757 * @file: drm file pointer
Dave Airlieff72145b2011-02-07 12:16:14 +1000758 */
759int
760i915_gem_create_ioctl(struct drm_device *dev, void *data,
761 struct drm_file *file)
762{
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +0000763 struct drm_i915_private *dev_priv = to_i915(dev);
Dave Airlieff72145b2011-02-07 12:16:14 +1000764 struct drm_i915_gem_create *args = data;
Daniel Vetter63ed2cb2012-04-23 16:50:50 +0200765
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +0000766 i915_gem_flush_free_objects(dev_priv);
Chris Wilsonfbbd37b2016-10-28 13:58:42 +0100767
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +0000768 return i915_gem_create(file, dev_priv,
Dave Airlieda6b51d2014-12-24 13:11:17 +1000769 args->size, &args->handle);
Dave Airlieff72145b2011-02-07 12:16:14 +1000770}
771
Chris Wilsonef749212017-04-12 12:01:10 +0100772static inline enum fb_op_origin
773fb_write_origin(struct drm_i915_gem_object *obj, unsigned int domain)
774{
775 return (domain == I915_GEM_DOMAIN_GTT ?
776 obj->frontbuffer_ggtt_origin : ORIGIN_CPU);
777}
778
Chris Wilson7125397b2017-12-06 12:49:14 +0000779void i915_gem_flush_ggtt_writes(struct drm_i915_private *dev_priv)
Chris Wilsonef749212017-04-12 12:01:10 +0100780{
Chris Wilson538ef962019-01-14 14:21:18 +0000781 intel_wakeref_t wakeref;
782
Chris Wilson7125397b2017-12-06 12:49:14 +0000783 /*
784 * No actual flushing is required for the GTT write domain for reads
785 * from the GTT domain. Writes to it "immediately" go to main memory
786 * as far as we know, so there's no chipset flush. It also doesn't
787 * land in the GPU render cache.
Chris Wilsonef749212017-04-12 12:01:10 +0100788 *
789 * However, we do have to enforce the order so that all writes through
790 * the GTT land before any writes to the device, such as updates to
791 * the GATT itself.
792 *
793 * We also have to wait a bit for the writes to land from the GTT.
794 * An uncached read (i.e. mmio) seems to be ideal for the round-trip
795 * timing. This issue has only been observed when switching quickly
796 * between GTT writes and CPU reads from inside the kernel on recent hw,
797 * and it appears to only affect discrete GTT blocks (i.e. on LLC
Chris Wilson7125397b2017-12-06 12:49:14 +0000798 * system agents we cannot reproduce this behaviour, until Cannonlake
799 * that was!).
Chris Wilsonef749212017-04-12 12:01:10 +0100800 */
Chris Wilson7125397b2017-12-06 12:49:14 +0000801
Chris Wilson900ccf32018-07-20 11:19:10 +0100802 wmb();
803
804 if (INTEL_INFO(dev_priv)->has_coherent_ggtt)
805 return;
806
Chris Wilsona8bd3b82018-07-17 10:26:55 +0100807 i915_gem_chipset_flush(dev_priv);
Chris Wilsonef749212017-04-12 12:01:10 +0100808
Chris Wilsond4225a52019-01-14 14:21:23 +0000809 with_intel_runtime_pm(dev_priv, wakeref) {
810 spin_lock_irq(&dev_priv->uncore.lock);
Chris Wilson7125397b2017-12-06 12:49:14 +0000811
Chris Wilsond4225a52019-01-14 14:21:23 +0000812 POSTING_READ_FW(RING_HEAD(RENDER_RING_BASE));
Chris Wilson7125397b2017-12-06 12:49:14 +0000813
Chris Wilsond4225a52019-01-14 14:21:23 +0000814 spin_unlock_irq(&dev_priv->uncore.lock);
815 }
Chris Wilson7125397b2017-12-06 12:49:14 +0000816}
817
818static void
819flush_write_domain(struct drm_i915_gem_object *obj, unsigned int flush_domains)
820{
821 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
822 struct i915_vma *vma;
823
Christian Königc0a51fd2018-02-16 13:43:38 +0100824 if (!(obj->write_domain & flush_domains))
Chris Wilson7125397b2017-12-06 12:49:14 +0000825 return;
826
Christian Königc0a51fd2018-02-16 13:43:38 +0100827 switch (obj->write_domain) {
Chris Wilsonef749212017-04-12 12:01:10 +0100828 case I915_GEM_DOMAIN_GTT:
Chris Wilson7125397b2017-12-06 12:49:14 +0000829 i915_gem_flush_ggtt_writes(dev_priv);
Chris Wilsonef749212017-04-12 12:01:10 +0100830
831 intel_fb_obj_flush(obj,
832 fb_write_origin(obj, I915_GEM_DOMAIN_GTT));
Chris Wilson7125397b2017-12-06 12:49:14 +0000833
Chris Wilsone2189dd2017-12-07 21:14:07 +0000834 for_each_ggtt_vma(vma, obj) {
Chris Wilson7125397b2017-12-06 12:49:14 +0000835 if (vma->iomap)
836 continue;
837
838 i915_vma_unset_ggtt_write(vma);
839 }
Chris Wilsonef749212017-04-12 12:01:10 +0100840 break;
841
Chris Wilsonadd00e62018-07-06 12:54:02 +0100842 case I915_GEM_DOMAIN_WC:
843 wmb();
844 break;
845
Chris Wilsonef749212017-04-12 12:01:10 +0100846 case I915_GEM_DOMAIN_CPU:
847 i915_gem_clflush_object(obj, I915_CLFLUSH_SYNC);
848 break;
Chris Wilsone27ab732017-06-15 13:38:49 +0100849
850 case I915_GEM_DOMAIN_RENDER:
851 if (gpu_write_needs_clflush(obj))
852 obj->cache_dirty = true;
853 break;
Chris Wilsonef749212017-04-12 12:01:10 +0100854 }
855
Christian Königc0a51fd2018-02-16 13:43:38 +0100856 obj->write_domain = 0;
Chris Wilsonef749212017-04-12 12:01:10 +0100857}
858
Brad Volkin4c914c02014-02-18 10:15:45 -0800859/*
860 * Pins the specified object's pages and synchronizes the object with
861 * GPU accesses. Sets needs_clflush to non-zero if the caller should
862 * flush the object from the CPU cache.
863 */
864int i915_gem_obj_prepare_shmem_read(struct drm_i915_gem_object *obj,
Chris Wilson43394c72016-08-18 17:16:47 +0100865 unsigned int *needs_clflush)
Brad Volkin4c914c02014-02-18 10:15:45 -0800866{
867 int ret;
868
Chris Wilsone95433c2016-10-28 13:58:27 +0100869 lockdep_assert_held(&obj->base.dev->struct_mutex);
Brad Volkin4c914c02014-02-18 10:15:45 -0800870
Chris Wilsone95433c2016-10-28 13:58:27 +0100871 *needs_clflush = 0;
Chris Wilson43394c72016-08-18 17:16:47 +0100872 if (!i915_gem_object_has_struct_page(obj))
873 return -ENODEV;
Brad Volkin4c914c02014-02-18 10:15:45 -0800874
Chris Wilsone95433c2016-10-28 13:58:27 +0100875 ret = i915_gem_object_wait(obj,
876 I915_WAIT_INTERRUPTIBLE |
877 I915_WAIT_LOCKED,
878 MAX_SCHEDULE_TIMEOUT,
879 NULL);
Chris Wilsonc13d87e2016-07-20 09:21:15 +0100880 if (ret)
881 return ret;
882
Chris Wilsona4f5ea62016-10-28 13:58:35 +0100883 ret = i915_gem_object_pin_pages(obj);
Chris Wilson97649512016-08-18 17:16:50 +0100884 if (ret)
885 return ret;
886
Chris Wilsonb8f55be2017-08-11 12:11:16 +0100887 if (obj->cache_coherent & I915_BO_CACHE_COHERENT_FOR_READ ||
888 !static_cpu_has(X86_FEATURE_CLFLUSH)) {
Chris Wilson7f5f95d2017-03-10 00:09:42 +0000889 ret = i915_gem_object_set_to_cpu_domain(obj, false);
890 if (ret)
891 goto err_unpin;
892 else
893 goto out;
894 }
895
Chris Wilsonef749212017-04-12 12:01:10 +0100896 flush_write_domain(obj, ~I915_GEM_DOMAIN_CPU);
Chris Wilsona314d5c2016-08-18 17:16:48 +0100897
Chris Wilson43394c72016-08-18 17:16:47 +0100898 /* If we're not in the cpu read domain, set ourself into the gtt
899 * read domain and manually flush cachelines (if required). This
900 * optimizes for the case when the gpu will dirty the data
901 * anyway again before the next pread happens.
902 */
Chris Wilsone27ab732017-06-15 13:38:49 +0100903 if (!obj->cache_dirty &&
Christian Königc0a51fd2018-02-16 13:43:38 +0100904 !(obj->read_domains & I915_GEM_DOMAIN_CPU))
Chris Wilson7f5f95d2017-03-10 00:09:42 +0000905 *needs_clflush = CLFLUSH_BEFORE;
Brad Volkin4c914c02014-02-18 10:15:45 -0800906
Chris Wilson7f5f95d2017-03-10 00:09:42 +0000907out:
Chris Wilson97649512016-08-18 17:16:50 +0100908 /* return with the pages pinned */
Chris Wilson43394c72016-08-18 17:16:47 +0100909 return 0;
Chris Wilson97649512016-08-18 17:16:50 +0100910
911err_unpin:
912 i915_gem_object_unpin_pages(obj);
913 return ret;
Chris Wilson43394c72016-08-18 17:16:47 +0100914}
915
916int i915_gem_obj_prepare_shmem_write(struct drm_i915_gem_object *obj,
917 unsigned int *needs_clflush)
918{
919 int ret;
920
Chris Wilsone95433c2016-10-28 13:58:27 +0100921 lockdep_assert_held(&obj->base.dev->struct_mutex);
922
Chris Wilson43394c72016-08-18 17:16:47 +0100923 *needs_clflush = 0;
924 if (!i915_gem_object_has_struct_page(obj))
925 return -ENODEV;
926
Chris Wilsone95433c2016-10-28 13:58:27 +0100927 ret = i915_gem_object_wait(obj,
928 I915_WAIT_INTERRUPTIBLE |
929 I915_WAIT_LOCKED |
930 I915_WAIT_ALL,
931 MAX_SCHEDULE_TIMEOUT,
932 NULL);
Chris Wilson43394c72016-08-18 17:16:47 +0100933 if (ret)
934 return ret;
935
Chris Wilsona4f5ea62016-10-28 13:58:35 +0100936 ret = i915_gem_object_pin_pages(obj);
Chris Wilson97649512016-08-18 17:16:50 +0100937 if (ret)
938 return ret;
939
Chris Wilsonb8f55be2017-08-11 12:11:16 +0100940 if (obj->cache_coherent & I915_BO_CACHE_COHERENT_FOR_WRITE ||
941 !static_cpu_has(X86_FEATURE_CLFLUSH)) {
Chris Wilson7f5f95d2017-03-10 00:09:42 +0000942 ret = i915_gem_object_set_to_cpu_domain(obj, true);
943 if (ret)
944 goto err_unpin;
945 else
946 goto out;
947 }
948
Chris Wilsonef749212017-04-12 12:01:10 +0100949 flush_write_domain(obj, ~I915_GEM_DOMAIN_CPU);
Chris Wilsona314d5c2016-08-18 17:16:48 +0100950
Chris Wilson43394c72016-08-18 17:16:47 +0100951 /* If we're not in the cpu write domain, set ourself into the
952 * gtt write domain and manually flush cachelines (as required).
953 * This optimizes for the case when the gpu will use the data
954 * right away and we therefore have to clflush anyway.
955 */
Chris Wilsone27ab732017-06-15 13:38:49 +0100956 if (!obj->cache_dirty) {
Chris Wilson7f5f95d2017-03-10 00:09:42 +0000957 *needs_clflush |= CLFLUSH_AFTER;
Chris Wilson43394c72016-08-18 17:16:47 +0100958
Chris Wilsone27ab732017-06-15 13:38:49 +0100959 /*
960 * Same trick applies to invalidate partially written
961 * cachelines read before writing.
962 */
Christian Königc0a51fd2018-02-16 13:43:38 +0100963 if (!(obj->read_domains & I915_GEM_DOMAIN_CPU))
Chris Wilsone27ab732017-06-15 13:38:49 +0100964 *needs_clflush |= CLFLUSH_BEFORE;
965 }
Chris Wilson43394c72016-08-18 17:16:47 +0100966
Chris Wilson7f5f95d2017-03-10 00:09:42 +0000967out:
Chris Wilson43394c72016-08-18 17:16:47 +0100968 intel_fb_obj_invalidate(obj, ORIGIN_CPU);
Chris Wilsona4f5ea62016-10-28 13:58:35 +0100969 obj->mm.dirty = true;
Chris Wilson97649512016-08-18 17:16:50 +0100970 /* return with the pages pinned */
Chris Wilson43394c72016-08-18 17:16:47 +0100971 return 0;
Chris Wilson97649512016-08-18 17:16:50 +0100972
973err_unpin:
974 i915_gem_object_unpin_pages(obj);
975 return ret;
Brad Volkin4c914c02014-02-18 10:15:45 -0800976}
977
Daniel Vetterd174bd62012-03-25 19:47:40 +0200978static int
Chris Wilsonb9d126e2019-01-05 12:07:58 +0000979shmem_pread(struct page *page, int offset, int len, char __user *user_data,
980 bool needs_clflush)
Daniel Vetterd174bd62012-03-25 19:47:40 +0200981{
982 char *vaddr;
983 int ret;
984
985 vaddr = kmap(page);
Daniel Vetterd174bd62012-03-25 19:47:40 +0200986
Chris Wilsonb9d126e2019-01-05 12:07:58 +0000987 if (needs_clflush)
988 drm_clflush_virt_range(vaddr + offset, len);
989
990 ret = __copy_to_user(user_data, vaddr + offset, len);
991
Daniel Vetterd174bd62012-03-25 19:47:40 +0200992 kunmap(page);
993
Chris Wilsonb9d126e2019-01-05 12:07:58 +0000994 return ret ? -EFAULT : 0;
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100995}
996
997static int
998i915_gem_shmem_pread(struct drm_i915_gem_object *obj,
999 struct drm_i915_gem_pread *args)
1000{
1001 char __user *user_data;
1002 u64 remain;
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001003 unsigned int needs_clflush;
1004 unsigned int idx, offset;
1005 int ret;
1006
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001007 ret = mutex_lock_interruptible(&obj->base.dev->struct_mutex);
1008 if (ret)
1009 return ret;
1010
1011 ret = i915_gem_obj_prepare_shmem_read(obj, &needs_clflush);
1012 mutex_unlock(&obj->base.dev->struct_mutex);
1013 if (ret)
1014 return ret;
1015
1016 remain = args->size;
1017 user_data = u64_to_user_ptr(args->data_ptr);
1018 offset = offset_in_page(args->offset);
1019 for (idx = args->offset >> PAGE_SHIFT; remain; idx++) {
1020 struct page *page = i915_gem_object_get_page(obj, idx);
Chris Wilsona5e856a52018-10-12 15:02:28 +01001021 unsigned int length = min_t(u64, remain, PAGE_SIZE - offset);
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001022
1023 ret = shmem_pread(page, offset, length, user_data,
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001024 needs_clflush);
1025 if (ret)
1026 break;
1027
1028 remain -= length;
1029 user_data += length;
1030 offset = 0;
1031 }
1032
1033 i915_gem_obj_finish_shmem_access(obj);
1034 return ret;
1035}
1036
1037static inline bool
1038gtt_user_read(struct io_mapping *mapping,
1039 loff_t base, int offset,
1040 char __user *user_data, int length)
1041{
Ville Syrjäläafe722b2017-09-01 20:12:52 +03001042 void __iomem *vaddr;
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001043 unsigned long unwritten;
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301044
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301045 /* We can use the cpu mem copy function because this is X86. */
Ville Syrjäläafe722b2017-09-01 20:12:52 +03001046 vaddr = io_mapping_map_atomic_wc(mapping, base);
1047 unwritten = __copy_to_user_inatomic(user_data,
1048 (void __force *)vaddr + offset,
1049 length);
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001050 io_mapping_unmap_atomic(vaddr);
1051 if (unwritten) {
Ville Syrjäläafe722b2017-09-01 20:12:52 +03001052 vaddr = io_mapping_map_wc(mapping, base, PAGE_SIZE);
1053 unwritten = copy_to_user(user_data,
1054 (void __force *)vaddr + offset,
1055 length);
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001056 io_mapping_unmap(vaddr);
1057 }
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301058 return unwritten;
1059}
1060
1061static int
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001062i915_gem_gtt_pread(struct drm_i915_gem_object *obj,
1063 const struct drm_i915_gem_pread *args)
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301064{
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001065 struct drm_i915_private *i915 = to_i915(obj->base.dev);
1066 struct i915_ggtt *ggtt = &i915->ggtt;
Chris Wilson538ef962019-01-14 14:21:18 +00001067 intel_wakeref_t wakeref;
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301068 struct drm_mm_node node;
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001069 struct i915_vma *vma;
1070 void __user *user_data;
1071 u64 remain, offset;
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301072 int ret;
1073
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001074 ret = mutex_lock_interruptible(&i915->drm.struct_mutex);
1075 if (ret)
1076 return ret;
1077
Chris Wilson538ef962019-01-14 14:21:18 +00001078 wakeref = intel_runtime_pm_get(i915);
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001079 vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0,
Chris Wilsona3259ca2017-10-09 09:44:00 +01001080 PIN_MAPPABLE |
1081 PIN_NONFAULT |
1082 PIN_NONBLOCK);
Chris Wilson18034582016-08-18 17:16:45 +01001083 if (!IS_ERR(vma)) {
1084 node.start = i915_ggtt_offset(vma);
1085 node.allocated = false;
Chris Wilson49ef5292016-08-18 17:17:00 +01001086 ret = i915_vma_put_fence(vma);
Chris Wilson18034582016-08-18 17:16:45 +01001087 if (ret) {
1088 i915_vma_unpin(vma);
1089 vma = ERR_PTR(ret);
1090 }
1091 }
Chris Wilson058d88c2016-08-15 10:49:06 +01001092 if (IS_ERR(vma)) {
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001093 ret = insert_mappable_node(ggtt, &node, PAGE_SIZE);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301094 if (ret)
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001095 goto out_unlock;
1096 GEM_BUG_ON(!node.allocated);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301097 }
1098
1099 ret = i915_gem_object_set_to_gtt_domain(obj, false);
1100 if (ret)
1101 goto out_unpin;
1102
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001103 mutex_unlock(&i915->drm.struct_mutex);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301104
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001105 user_data = u64_to_user_ptr(args->data_ptr);
1106 remain = args->size;
1107 offset = args->offset;
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301108
1109 while (remain > 0) {
1110 /* Operation in this page
1111 *
1112 * page_base = page offset within aperture
1113 * page_offset = offset within page
1114 * page_length = bytes to copy for this page
1115 */
1116 u32 page_base = node.start;
1117 unsigned page_offset = offset_in_page(offset);
1118 unsigned page_length = PAGE_SIZE - page_offset;
1119 page_length = remain < page_length ? remain : page_length;
1120 if (node.allocated) {
1121 wmb();
Chris Wilson82ad6442018-06-05 16:37:58 +01001122 ggtt->vm.insert_page(&ggtt->vm,
1123 i915_gem_object_get_dma_address(obj, offset >> PAGE_SHIFT),
1124 node.start, I915_CACHE_NONE, 0);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301125 wmb();
1126 } else {
1127 page_base += offset & PAGE_MASK;
1128 }
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001129
Matthew Auld73ebd502017-12-11 15:18:20 +00001130 if (gtt_user_read(&ggtt->iomap, page_base, page_offset,
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001131 user_data, page_length)) {
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301132 ret = -EFAULT;
1133 break;
1134 }
1135
1136 remain -= page_length;
1137 user_data += page_length;
1138 offset += page_length;
1139 }
1140
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001141 mutex_lock(&i915->drm.struct_mutex);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301142out_unpin:
1143 if (node.allocated) {
1144 wmb();
Chris Wilson82ad6442018-06-05 16:37:58 +01001145 ggtt->vm.clear_range(&ggtt->vm, node.start, node.size);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301146 remove_mappable_node(&node);
1147 } else {
Chris Wilson058d88c2016-08-15 10:49:06 +01001148 i915_vma_unpin(vma);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301149 }
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001150out_unlock:
Chris Wilson538ef962019-01-14 14:21:18 +00001151 intel_runtime_pm_put(i915, wakeref);
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001152 mutex_unlock(&i915->drm.struct_mutex);
Chris Wilsonf60d7f02012-09-04 21:02:56 +01001153
Eric Anholteb014592009-03-10 11:44:52 -07001154 return ret;
1155}
1156
Eric Anholt673a3942008-07-30 12:06:12 -07001157/**
1158 * Reads data from the object referenced by handle.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01001159 * @dev: drm device pointer
1160 * @data: ioctl data blob
1161 * @file: drm file pointer
Eric Anholt673a3942008-07-30 12:06:12 -07001162 *
1163 * On error, the contents of *data are undefined.
1164 */
1165int
1166i915_gem_pread_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00001167 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001168{
1169 struct drm_i915_gem_pread *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00001170 struct drm_i915_gem_object *obj;
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001171 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07001172
Chris Wilson51311d02010-11-17 09:10:42 +00001173 if (args->size == 0)
1174 return 0;
1175
Linus Torvalds96d4f262019-01-03 18:57:57 -08001176 if (!access_ok(u64_to_user_ptr(args->data_ptr),
Chris Wilson51311d02010-11-17 09:10:42 +00001177 args->size))
1178 return -EFAULT;
1179
Chris Wilson03ac0642016-07-20 13:31:51 +01001180 obj = i915_gem_object_lookup(file, args->handle);
Chris Wilson258a5ed2016-08-05 10:14:16 +01001181 if (!obj)
1182 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07001183
Chris Wilson7dcd2492010-09-26 20:21:44 +01001184 /* Bounds check source. */
Matthew Auld966d5bf2016-12-13 20:32:22 +00001185 if (range_overflows_t(u64, args->offset, args->size, obj->base.size)) {
Chris Wilsonce9d4192010-09-26 20:50:05 +01001186 ret = -EINVAL;
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001187 goto out;
Chris Wilsonce9d4192010-09-26 20:50:05 +01001188 }
1189
Chris Wilsondb53a302011-02-03 11:57:46 +00001190 trace_i915_gem_object_pread(obj, args->offset, args->size);
1191
Chris Wilsone95433c2016-10-28 13:58:27 +01001192 ret = i915_gem_object_wait(obj,
1193 I915_WAIT_INTERRUPTIBLE,
1194 MAX_SCHEDULE_TIMEOUT,
1195 to_rps_client(file));
Chris Wilson258a5ed2016-08-05 10:14:16 +01001196 if (ret)
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001197 goto out;
Chris Wilson258a5ed2016-08-05 10:14:16 +01001198
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001199 ret = i915_gem_object_pin_pages(obj);
Chris Wilson258a5ed2016-08-05 10:14:16 +01001200 if (ret)
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001201 goto out;
Chris Wilson258a5ed2016-08-05 10:14:16 +01001202
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001203 ret = i915_gem_shmem_pread(obj, args);
Chris Wilson9c870d02016-10-24 13:42:15 +01001204 if (ret == -EFAULT || ret == -ENODEV)
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001205 ret = i915_gem_gtt_pread(obj, args);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301206
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001207 i915_gem_object_unpin_pages(obj);
1208out:
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001209 i915_gem_object_put(obj);
Eric Anholteb014592009-03-10 11:44:52 -07001210 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07001211}
1212
Keith Packard0839ccb2008-10-30 19:38:48 -07001213/* This is the fast write path which cannot handle
1214 * page faults in the source data
Linus Torvalds9b7530cc2008-10-20 14:16:43 -07001215 */
Linus Torvalds9b7530cc2008-10-20 14:16:43 -07001216
Chris Wilsonfe115622016-10-28 13:58:40 +01001217static inline bool
1218ggtt_write(struct io_mapping *mapping,
1219 loff_t base, int offset,
1220 char __user *user_data, int length)
Keith Packard0839ccb2008-10-30 19:38:48 -07001221{
Ville Syrjäläafe722b2017-09-01 20:12:52 +03001222 void __iomem *vaddr;
Keith Packard0839ccb2008-10-30 19:38:48 -07001223 unsigned long unwritten;
1224
Ben Widawsky4f0c7cf2012-04-16 14:07:47 -07001225 /* We can use the cpu mem copy function because this is X86. */
Ville Syrjäläafe722b2017-09-01 20:12:52 +03001226 vaddr = io_mapping_map_atomic_wc(mapping, base);
1227 unwritten = __copy_from_user_inatomic_nocache((void __force *)vaddr + offset,
Keith Packard0839ccb2008-10-30 19:38:48 -07001228 user_data, length);
Chris Wilsonfe115622016-10-28 13:58:40 +01001229 io_mapping_unmap_atomic(vaddr);
1230 if (unwritten) {
Ville Syrjäläafe722b2017-09-01 20:12:52 +03001231 vaddr = io_mapping_map_wc(mapping, base, PAGE_SIZE);
1232 unwritten = copy_from_user((void __force *)vaddr + offset,
1233 user_data, length);
Chris Wilsonfe115622016-10-28 13:58:40 +01001234 io_mapping_unmap(vaddr);
1235 }
Keith Packard0839ccb2008-10-30 19:38:48 -07001236
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001237 return unwritten;
1238}
1239
Eric Anholt3de09aa2009-03-09 09:42:23 -07001240/**
1241 * This is the fast pwrite path, where we copy the data directly from the
1242 * user into the GTT, uncached.
Chris Wilsonfe115622016-10-28 13:58:40 +01001243 * @obj: i915 GEM object
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01001244 * @args: pwrite arguments structure
Eric Anholt3de09aa2009-03-09 09:42:23 -07001245 */
Eric Anholt673a3942008-07-30 12:06:12 -07001246static int
Chris Wilsonfe115622016-10-28 13:58:40 +01001247i915_gem_gtt_pwrite_fast(struct drm_i915_gem_object *obj,
1248 const struct drm_i915_gem_pwrite *args)
Eric Anholt673a3942008-07-30 12:06:12 -07001249{
Chris Wilsonfe115622016-10-28 13:58:40 +01001250 struct drm_i915_private *i915 = to_i915(obj->base.dev);
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301251 struct i915_ggtt *ggtt = &i915->ggtt;
Chris Wilson538ef962019-01-14 14:21:18 +00001252 intel_wakeref_t wakeref;
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301253 struct drm_mm_node node;
Chris Wilsonfe115622016-10-28 13:58:40 +01001254 struct i915_vma *vma;
1255 u64 remain, offset;
1256 void __user *user_data;
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301257 int ret;
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301258
Chris Wilsonfe115622016-10-28 13:58:40 +01001259 ret = mutex_lock_interruptible(&i915->drm.struct_mutex);
1260 if (ret)
1261 return ret;
Daniel Vetter935aaa62012-03-25 19:47:35 +02001262
Chris Wilson8bd818152017-10-19 07:37:33 +01001263 if (i915_gem_object_has_struct_page(obj)) {
1264 /*
1265 * Avoid waking the device up if we can fallback, as
1266 * waking/resuming is very slow (worst-case 10-100 ms
1267 * depending on PCI sleeps and our own resume time).
1268 * This easily dwarfs any performance advantage from
1269 * using the cache bypass of indirect GGTT access.
1270 */
Chris Wilson538ef962019-01-14 14:21:18 +00001271 wakeref = intel_runtime_pm_get_if_in_use(i915);
1272 if (!wakeref) {
Chris Wilson8bd818152017-10-19 07:37:33 +01001273 ret = -EFAULT;
1274 goto out_unlock;
1275 }
1276 } else {
1277 /* No backing pages, no fallback, we must force GGTT access */
Chris Wilson538ef962019-01-14 14:21:18 +00001278 wakeref = intel_runtime_pm_get(i915);
Chris Wilson8bd818152017-10-19 07:37:33 +01001279 }
1280
Chris Wilson058d88c2016-08-15 10:49:06 +01001281 vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0,
Chris Wilsona3259ca2017-10-09 09:44:00 +01001282 PIN_MAPPABLE |
1283 PIN_NONFAULT |
1284 PIN_NONBLOCK);
Chris Wilson18034582016-08-18 17:16:45 +01001285 if (!IS_ERR(vma)) {
1286 node.start = i915_ggtt_offset(vma);
1287 node.allocated = false;
Chris Wilson49ef5292016-08-18 17:17:00 +01001288 ret = i915_vma_put_fence(vma);
Chris Wilson18034582016-08-18 17:16:45 +01001289 if (ret) {
1290 i915_vma_unpin(vma);
1291 vma = ERR_PTR(ret);
1292 }
1293 }
Chris Wilson058d88c2016-08-15 10:49:06 +01001294 if (IS_ERR(vma)) {
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001295 ret = insert_mappable_node(ggtt, &node, PAGE_SIZE);
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301296 if (ret)
Chris Wilson8bd818152017-10-19 07:37:33 +01001297 goto out_rpm;
Chris Wilsonfe115622016-10-28 13:58:40 +01001298 GEM_BUG_ON(!node.allocated);
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301299 }
Daniel Vetter935aaa62012-03-25 19:47:35 +02001300
1301 ret = i915_gem_object_set_to_gtt_domain(obj, true);
1302 if (ret)
1303 goto out_unpin;
1304
Chris Wilsonfe115622016-10-28 13:58:40 +01001305 mutex_unlock(&i915->drm.struct_mutex);
1306
Chris Wilsonb19482d2016-08-18 17:16:43 +01001307 intel_fb_obj_invalidate(obj, ORIGIN_CPU);
Paulo Zanoni063e4e62015-02-13 17:23:45 -02001308
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301309 user_data = u64_to_user_ptr(args->data_ptr);
1310 offset = args->offset;
1311 remain = args->size;
1312 while (remain) {
Eric Anholt673a3942008-07-30 12:06:12 -07001313 /* Operation in this page
1314 *
Keith Packard0839ccb2008-10-30 19:38:48 -07001315 * page_base = page offset within aperture
1316 * page_offset = offset within page
1317 * page_length = bytes to copy for this page
Eric Anholt673a3942008-07-30 12:06:12 -07001318 */
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301319 u32 page_base = node.start;
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001320 unsigned int page_offset = offset_in_page(offset);
1321 unsigned int page_length = PAGE_SIZE - page_offset;
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301322 page_length = remain < page_length ? remain : page_length;
1323 if (node.allocated) {
1324 wmb(); /* flush the write before we modify the GGTT */
Chris Wilson82ad6442018-06-05 16:37:58 +01001325 ggtt->vm.insert_page(&ggtt->vm,
1326 i915_gem_object_get_dma_address(obj, offset >> PAGE_SHIFT),
1327 node.start, I915_CACHE_NONE, 0);
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301328 wmb(); /* flush modifications to the GGTT (insert_page) */
1329 } else {
1330 page_base += offset & PAGE_MASK;
1331 }
Keith Packard0839ccb2008-10-30 19:38:48 -07001332 /* If we get a fault while copying data, then (presumably) our
Eric Anholt3de09aa2009-03-09 09:42:23 -07001333 * source page isn't available. Return the error and we'll
1334 * retry in the slow path.
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301335 * If the object is non-shmem backed, we retry again with the
1336 * path that handles page fault.
Keith Packard0839ccb2008-10-30 19:38:48 -07001337 */
Matthew Auld73ebd502017-12-11 15:18:20 +00001338 if (ggtt_write(&ggtt->iomap, page_base, page_offset,
Chris Wilsonfe115622016-10-28 13:58:40 +01001339 user_data, page_length)) {
1340 ret = -EFAULT;
1341 break;
Daniel Vetter935aaa62012-03-25 19:47:35 +02001342 }
Eric Anholt673a3942008-07-30 12:06:12 -07001343
Keith Packard0839ccb2008-10-30 19:38:48 -07001344 remain -= page_length;
1345 user_data += page_length;
1346 offset += page_length;
Eric Anholt673a3942008-07-30 12:06:12 -07001347 }
Chris Wilsond59b21e2017-02-22 11:40:49 +00001348 intel_fb_obj_flush(obj, ORIGIN_CPU);
Chris Wilsonfe115622016-10-28 13:58:40 +01001349
1350 mutex_lock(&i915->drm.struct_mutex);
Daniel Vetter935aaa62012-03-25 19:47:35 +02001351out_unpin:
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301352 if (node.allocated) {
1353 wmb();
Chris Wilson82ad6442018-06-05 16:37:58 +01001354 ggtt->vm.clear_range(&ggtt->vm, node.start, node.size);
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301355 remove_mappable_node(&node);
1356 } else {
Chris Wilson058d88c2016-08-15 10:49:06 +01001357 i915_vma_unpin(vma);
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301358 }
Chris Wilson8bd818152017-10-19 07:37:33 +01001359out_rpm:
Chris Wilson538ef962019-01-14 14:21:18 +00001360 intel_runtime_pm_put(i915, wakeref);
Chris Wilson8bd818152017-10-19 07:37:33 +01001361out_unlock:
Chris Wilsonfe115622016-10-28 13:58:40 +01001362 mutex_unlock(&i915->drm.struct_mutex);
Eric Anholt3de09aa2009-03-09 09:42:23 -07001363 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07001364}
1365
Chris Wilsonfe115622016-10-28 13:58:40 +01001366/* Per-page copy function for the shmem pwrite fastpath.
1367 * Flushes invalid cachelines before writing to the target if
1368 * needs_clflush_before is set and flushes out any written cachelines after
1369 * writing if needs_clflush is set.
1370 */
Eric Anholt40123c12009-03-09 13:42:30 -07001371static int
Chris Wilsonfe115622016-10-28 13:58:40 +01001372shmem_pwrite(struct page *page, int offset, int len, char __user *user_data,
Chris Wilsonfe115622016-10-28 13:58:40 +01001373 bool needs_clflush_before,
1374 bool needs_clflush_after)
Eric Anholt40123c12009-03-09 13:42:30 -07001375{
Chris Wilsonb9d126e2019-01-05 12:07:58 +00001376 char *vaddr;
Chris Wilsonfe115622016-10-28 13:58:40 +01001377 int ret;
Eric Anholt40123c12009-03-09 13:42:30 -07001378
Chris Wilsonb9d126e2019-01-05 12:07:58 +00001379 vaddr = kmap(page);
Chris Wilsonfe115622016-10-28 13:58:40 +01001380
Chris Wilsonb9d126e2019-01-05 12:07:58 +00001381 if (needs_clflush_before)
1382 drm_clflush_virt_range(vaddr + offset, len);
Chris Wilsonfe115622016-10-28 13:58:40 +01001383
Chris Wilsonb9d126e2019-01-05 12:07:58 +00001384 ret = __copy_from_user(vaddr + offset, user_data, len);
1385 if (!ret && needs_clflush_after)
1386 drm_clflush_virt_range(vaddr + offset, len);
Chris Wilsonfe115622016-10-28 13:58:40 +01001387
Chris Wilsonb9d126e2019-01-05 12:07:58 +00001388 kunmap(page);
1389
1390 return ret ? -EFAULT : 0;
Chris Wilsonfe115622016-10-28 13:58:40 +01001391}
1392
1393static int
1394i915_gem_shmem_pwrite(struct drm_i915_gem_object *obj,
1395 const struct drm_i915_gem_pwrite *args)
1396{
1397 struct drm_i915_private *i915 = to_i915(obj->base.dev);
1398 void __user *user_data;
1399 u64 remain;
Chris Wilsonfe115622016-10-28 13:58:40 +01001400 unsigned int partial_cacheline_write;
1401 unsigned int needs_clflush;
1402 unsigned int offset, idx;
1403 int ret;
1404
1405 ret = mutex_lock_interruptible(&i915->drm.struct_mutex);
Chris Wilson43394c72016-08-18 17:16:47 +01001406 if (ret)
1407 return ret;
Eric Anholt40123c12009-03-09 13:42:30 -07001408
Chris Wilsonfe115622016-10-28 13:58:40 +01001409 ret = i915_gem_obj_prepare_shmem_write(obj, &needs_clflush);
1410 mutex_unlock(&i915->drm.struct_mutex);
1411 if (ret)
1412 return ret;
1413
Chris Wilsonfe115622016-10-28 13:58:40 +01001414 /* If we don't overwrite a cacheline completely we need to be
1415 * careful to have up-to-date data by first clflushing. Don't
1416 * overcomplicate things and flush the entire patch.
1417 */
1418 partial_cacheline_write = 0;
1419 if (needs_clflush & CLFLUSH_BEFORE)
1420 partial_cacheline_write = boot_cpu_data.x86_clflush_size - 1;
1421
Chris Wilson43394c72016-08-18 17:16:47 +01001422 user_data = u64_to_user_ptr(args->data_ptr);
Chris Wilson43394c72016-08-18 17:16:47 +01001423 remain = args->size;
Chris Wilsonfe115622016-10-28 13:58:40 +01001424 offset = offset_in_page(args->offset);
1425 for (idx = args->offset >> PAGE_SHIFT; remain; idx++) {
1426 struct page *page = i915_gem_object_get_page(obj, idx);
Chris Wilsona5e856a52018-10-12 15:02:28 +01001427 unsigned int length = min_t(u64, remain, PAGE_SIZE - offset);
Chris Wilsone5281cc2010-10-28 13:45:36 +01001428
Chris Wilsonfe115622016-10-28 13:58:40 +01001429 ret = shmem_pwrite(page, offset, length, user_data,
Chris Wilsonfe115622016-10-28 13:58:40 +01001430 (offset | length) & partial_cacheline_write,
1431 needs_clflush & CLFLUSH_AFTER);
1432 if (ret)
Chris Wilson9da3da62012-06-01 15:20:22 +01001433 break;
1434
Chris Wilsonfe115622016-10-28 13:58:40 +01001435 remain -= length;
1436 user_data += length;
1437 offset = 0;
Eric Anholt40123c12009-03-09 13:42:30 -07001438 }
1439
Chris Wilsond59b21e2017-02-22 11:40:49 +00001440 intel_fb_obj_flush(obj, ORIGIN_CPU);
Chris Wilsonfe115622016-10-28 13:58:40 +01001441 i915_gem_obj_finish_shmem_access(obj);
Eric Anholt40123c12009-03-09 13:42:30 -07001442 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07001443}
1444
1445/**
1446 * Writes data to the object referenced by handle.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01001447 * @dev: drm device
1448 * @data: ioctl data blob
1449 * @file: drm file
Eric Anholt673a3942008-07-30 12:06:12 -07001450 *
1451 * On error, the contents of the buffer that were to be modified are undefined.
1452 */
1453int
1454i915_gem_pwrite_ioctl(struct drm_device *dev, void *data,
Chris Wilsonfbd5a262010-10-14 15:03:58 +01001455 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001456{
1457 struct drm_i915_gem_pwrite *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00001458 struct drm_i915_gem_object *obj;
Chris Wilson51311d02010-11-17 09:10:42 +00001459 int ret;
1460
1461 if (args->size == 0)
1462 return 0;
1463
Linus Torvalds96d4f262019-01-03 18:57:57 -08001464 if (!access_ok(u64_to_user_ptr(args->data_ptr), args->size))
Chris Wilson51311d02010-11-17 09:10:42 +00001465 return -EFAULT;
1466
Chris Wilson03ac0642016-07-20 13:31:51 +01001467 obj = i915_gem_object_lookup(file, args->handle);
Chris Wilson258a5ed2016-08-05 10:14:16 +01001468 if (!obj)
1469 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07001470
Chris Wilson7dcd2492010-09-26 20:21:44 +01001471 /* Bounds check destination. */
Matthew Auld966d5bf2016-12-13 20:32:22 +00001472 if (range_overflows_t(u64, args->offset, args->size, obj->base.size)) {
Chris Wilsonce9d4192010-09-26 20:50:05 +01001473 ret = -EINVAL;
Chris Wilson258a5ed2016-08-05 10:14:16 +01001474 goto err;
Chris Wilsonce9d4192010-09-26 20:50:05 +01001475 }
1476
Chris Wilsonf8c1cce2018-07-12 19:53:14 +01001477 /* Writes not allowed into this read-only object */
1478 if (i915_gem_object_is_readonly(obj)) {
1479 ret = -EINVAL;
1480 goto err;
1481 }
1482
Chris Wilsondb53a302011-02-03 11:57:46 +00001483 trace_i915_gem_object_pwrite(obj, args->offset, args->size);
1484
Chris Wilson7c55e2c2017-03-07 12:03:38 +00001485 ret = -ENODEV;
1486 if (obj->ops->pwrite)
1487 ret = obj->ops->pwrite(obj, args);
1488 if (ret != -ENODEV)
1489 goto err;
1490
Chris Wilsone95433c2016-10-28 13:58:27 +01001491 ret = i915_gem_object_wait(obj,
1492 I915_WAIT_INTERRUPTIBLE |
1493 I915_WAIT_ALL,
1494 MAX_SCHEDULE_TIMEOUT,
1495 to_rps_client(file));
Chris Wilson258a5ed2016-08-05 10:14:16 +01001496 if (ret)
1497 goto err;
1498
Chris Wilsonfe115622016-10-28 13:58:40 +01001499 ret = i915_gem_object_pin_pages(obj);
Chris Wilson258a5ed2016-08-05 10:14:16 +01001500 if (ret)
Chris Wilsonfe115622016-10-28 13:58:40 +01001501 goto err;
Chris Wilson258a5ed2016-08-05 10:14:16 +01001502
Daniel Vetter935aaa62012-03-25 19:47:35 +02001503 ret = -EFAULT;
Eric Anholt673a3942008-07-30 12:06:12 -07001504 /* We can only do the GTT pwrite on untiled buffers, as otherwise
1505 * it would end up going through the fenced access, and we'll get
1506 * different detiling behavior between reading and writing.
1507 * pread/pwrite currently are reading and writing from the CPU
1508 * perspective, requiring manual detiling by the client.
1509 */
Chris Wilson6eae0052016-06-20 15:05:52 +01001510 if (!i915_gem_object_has_struct_page(obj) ||
Chris Wilson9c870d02016-10-24 13:42:15 +01001511 cpu_write_needs_clflush(obj))
Daniel Vetter935aaa62012-03-25 19:47:35 +02001512 /* Note that the gtt paths might fail with non-page-backed user
1513 * pointers (e.g. gtt mappings when moving data between
Chris Wilson9c870d02016-10-24 13:42:15 +01001514 * textures). Fallback to the shmem path in that case.
1515 */
Chris Wilsonfe115622016-10-28 13:58:40 +01001516 ret = i915_gem_gtt_pwrite_fast(obj, args);
Eric Anholt673a3942008-07-30 12:06:12 -07001517
Chris Wilsond1054ee2016-07-16 18:42:36 +01001518 if (ret == -EFAULT || ret == -ENOSPC) {
Chris Wilson6a2c4232014-11-04 04:51:40 -08001519 if (obj->phys_handle)
1520 ret = i915_gem_phys_pwrite(obj, args, file);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301521 else
Chris Wilsonfe115622016-10-28 13:58:40 +01001522 ret = i915_gem_shmem_pwrite(obj, args);
Chris Wilson6a2c4232014-11-04 04:51:40 -08001523 }
Daniel Vetter5c0480f2011-12-14 13:57:30 +01001524
Chris Wilsonfe115622016-10-28 13:58:40 +01001525 i915_gem_object_unpin_pages(obj);
Chris Wilson258a5ed2016-08-05 10:14:16 +01001526err:
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001527 i915_gem_object_put(obj);
Chris Wilson258a5ed2016-08-05 10:14:16 +01001528 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07001529}
1530
Chris Wilson40e62d52016-10-28 13:58:41 +01001531static void i915_gem_object_bump_inactive_ggtt(struct drm_i915_gem_object *obj)
1532{
Chris Wilson09d7e462019-01-28 10:23:53 +00001533 struct drm_i915_private *i915 = to_i915(obj->base.dev);
Chris Wilson40e62d52016-10-28 13:58:41 +01001534 struct list_head *list;
1535 struct i915_vma *vma;
1536
Chris Wilsonf2123812017-10-16 12:40:37 +01001537 GEM_BUG_ON(!i915_gem_object_has_pinned_pages(obj));
1538
Chris Wilson09d7e462019-01-28 10:23:53 +00001539 mutex_lock(&i915->ggtt.vm.mutex);
Chris Wilsone2189dd2017-12-07 21:14:07 +00001540 for_each_ggtt_vma(vma, obj) {
Chris Wilson40e62d52016-10-28 13:58:41 +01001541 if (!drm_mm_node_allocated(&vma->node))
1542 continue;
1543
Chris Wilson499197d2019-01-28 10:23:52 +00001544 list_move_tail(&vma->vm_link, &vma->vm->bound_list);
Chris Wilson40e62d52016-10-28 13:58:41 +01001545 }
Chris Wilson09d7e462019-01-28 10:23:53 +00001546 mutex_unlock(&i915->ggtt.vm.mutex);
Chris Wilson40e62d52016-10-28 13:58:41 +01001547
Chris Wilsonf2123812017-10-16 12:40:37 +01001548 spin_lock(&i915->mm.obj_lock);
Chris Wilson40e62d52016-10-28 13:58:41 +01001549 list = obj->bind_count ? &i915->mm.bound_list : &i915->mm.unbound_list;
Chris Wilsonf2123812017-10-16 12:40:37 +01001550 list_move_tail(&obj->mm.link, list);
1551 spin_unlock(&i915->mm.obj_lock);
Chris Wilson40e62d52016-10-28 13:58:41 +01001552}
1553
Eric Anholt673a3942008-07-30 12:06:12 -07001554/**
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001555 * Called when user space prepares to use an object with the CPU, either
1556 * through the mmap ioctl's mapping or a GTT mapping.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01001557 * @dev: drm device
1558 * @data: ioctl data blob
1559 * @file: drm file
Eric Anholt673a3942008-07-30 12:06:12 -07001560 */
1561int
1562i915_gem_set_domain_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00001563 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001564{
1565 struct drm_i915_gem_set_domain *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00001566 struct drm_i915_gem_object *obj;
Jani Nikula739f3ab2019-01-16 11:15:19 +02001567 u32 read_domains = args->read_domains;
1568 u32 write_domain = args->write_domain;
Chris Wilson40e62d52016-10-28 13:58:41 +01001569 int err;
Eric Anholt673a3942008-07-30 12:06:12 -07001570
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001571 /* Only handle setting domains to types used by the CPU. */
Chris Wilsonb8f90962016-08-05 10:14:07 +01001572 if ((write_domain | read_domains) & I915_GEM_GPU_DOMAINS)
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001573 return -EINVAL;
1574
1575 /* Having something in the write domain implies it's in the read
1576 * domain, and only that read domain. Enforce that in the request.
1577 */
1578 if (write_domain != 0 && read_domains != write_domain)
1579 return -EINVAL;
1580
Chris Wilson03ac0642016-07-20 13:31:51 +01001581 obj = i915_gem_object_lookup(file, args->handle);
Chris Wilsonb8f90962016-08-05 10:14:07 +01001582 if (!obj)
1583 return -ENOENT;
Jesse Barnes652c3932009-08-17 13:31:43 -07001584
Chris Wilson3236f572012-08-24 09:35:09 +01001585 /* Try to flush the object off the GPU without holding the lock.
1586 * We will repeat the flush holding the lock in the normal manner
1587 * to catch cases where we are gazumped.
1588 */
Chris Wilson40e62d52016-10-28 13:58:41 +01001589 err = i915_gem_object_wait(obj,
Chris Wilsone95433c2016-10-28 13:58:27 +01001590 I915_WAIT_INTERRUPTIBLE |
Chris Wilsone9eaf822018-10-01 15:47:55 +01001591 I915_WAIT_PRIORITY |
Chris Wilsone95433c2016-10-28 13:58:27 +01001592 (write_domain ? I915_WAIT_ALL : 0),
1593 MAX_SCHEDULE_TIMEOUT,
1594 to_rps_client(file));
Chris Wilson40e62d52016-10-28 13:58:41 +01001595 if (err)
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001596 goto out;
Chris Wilsonb8f90962016-08-05 10:14:07 +01001597
Tina Zhanga03f3952017-11-14 10:25:13 +00001598 /*
1599 * Proxy objects do not control access to the backing storage, ergo
1600 * they cannot be used as a means to manipulate the cache domain
1601 * tracking for that backing storage. The proxy object is always
1602 * considered to be outside of any cache domain.
1603 */
1604 if (i915_gem_object_is_proxy(obj)) {
1605 err = -ENXIO;
1606 goto out;
1607 }
1608
1609 /*
1610 * Flush and acquire obj->pages so that we are coherent through
Chris Wilson40e62d52016-10-28 13:58:41 +01001611 * direct access in memory with previous cached writes through
1612 * shmemfs and that our cache domain tracking remains valid.
1613 * For example, if the obj->filp was moved to swap without us
1614 * being notified and releasing the pages, we would mistakenly
1615 * continue to assume that the obj remained out of the CPU cached
1616 * domain.
1617 */
1618 err = i915_gem_object_pin_pages(obj);
1619 if (err)
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001620 goto out;
Chris Wilson40e62d52016-10-28 13:58:41 +01001621
1622 err = i915_mutex_lock_interruptible(dev);
1623 if (err)
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001624 goto out_unpin;
Chris Wilson3236f572012-08-24 09:35:09 +01001625
Chris Wilsone22d8e32017-04-12 12:01:11 +01001626 if (read_domains & I915_GEM_DOMAIN_WC)
1627 err = i915_gem_object_set_to_wc_domain(obj, write_domain);
1628 else if (read_domains & I915_GEM_DOMAIN_GTT)
1629 err = i915_gem_object_set_to_gtt_domain(obj, write_domain);
Chris Wilson43566de2015-01-02 16:29:29 +05301630 else
Chris Wilsone22d8e32017-04-12 12:01:11 +01001631 err = i915_gem_object_set_to_cpu_domain(obj, write_domain);
Chris Wilson40e62d52016-10-28 13:58:41 +01001632
1633 /* And bump the LRU for this access */
1634 i915_gem_object_bump_inactive_ggtt(obj);
1635
1636 mutex_unlock(&dev->struct_mutex);
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001637
Daniel Vetter031b6982015-06-26 19:35:16 +02001638 if (write_domain != 0)
Chris Wilsonef749212017-04-12 12:01:10 +01001639 intel_fb_obj_invalidate(obj,
1640 fb_write_origin(obj, write_domain));
Daniel Vetter031b6982015-06-26 19:35:16 +02001641
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001642out_unpin:
Chris Wilson40e62d52016-10-28 13:58:41 +01001643 i915_gem_object_unpin_pages(obj);
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001644out:
1645 i915_gem_object_put(obj);
Chris Wilson40e62d52016-10-28 13:58:41 +01001646 return err;
Eric Anholt673a3942008-07-30 12:06:12 -07001647}
1648
1649/**
1650 * Called when user space has done writes to this buffer
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01001651 * @dev: drm device
1652 * @data: ioctl data blob
1653 * @file: drm file
Eric Anholt673a3942008-07-30 12:06:12 -07001654 */
1655int
1656i915_gem_sw_finish_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00001657 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001658{
1659 struct drm_i915_gem_sw_finish *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00001660 struct drm_i915_gem_object *obj;
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001661
Chris Wilson03ac0642016-07-20 13:31:51 +01001662 obj = i915_gem_object_lookup(file, args->handle);
Chris Wilsonc21724c2016-08-05 10:14:19 +01001663 if (!obj)
1664 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07001665
Tina Zhanga03f3952017-11-14 10:25:13 +00001666 /*
1667 * Proxy objects are barred from CPU access, so there is no
1668 * need to ban sw_finish as it is a nop.
1669 */
1670
Eric Anholt673a3942008-07-30 12:06:12 -07001671 /* Pinned buffers may be scanout, so flush the cache */
Chris Wilson5a97bcc2017-02-22 11:40:46 +00001672 i915_gem_object_flush_if_display(obj);
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001673 i915_gem_object_put(obj);
Chris Wilson5a97bcc2017-02-22 11:40:46 +00001674
1675 return 0;
Eric Anholt673a3942008-07-30 12:06:12 -07001676}
1677
1678/**
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01001679 * i915_gem_mmap_ioctl - Maps the contents of an object, returning the address
1680 * it is mapped to.
1681 * @dev: drm device
1682 * @data: ioctl data blob
1683 * @file: drm file
Eric Anholt673a3942008-07-30 12:06:12 -07001684 *
1685 * While the mapping holds a reference on the contents of the object, it doesn't
1686 * imply a ref on the object itself.
Daniel Vetter34367382014-10-16 12:28:18 +02001687 *
1688 * IMPORTANT:
1689 *
1690 * DRM driver writers who look a this function as an example for how to do GEM
1691 * mmap support, please don't implement mmap support like here. The modern way
1692 * to implement DRM mmap support is with an mmap offset ioctl (like
1693 * i915_gem_mmap_gtt) and then using the mmap syscall on the DRM fd directly.
1694 * That way debug tooling like valgrind will understand what's going on, hiding
1695 * the mmap call in a driver private ioctl will break that. The i915 driver only
1696 * does cpu mmaps this way because we didn't know better.
Eric Anholt673a3942008-07-30 12:06:12 -07001697 */
1698int
1699i915_gem_mmap_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00001700 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001701{
1702 struct drm_i915_gem_mmap *args = data;
Chris Wilson03ac0642016-07-20 13:31:51 +01001703 struct drm_i915_gem_object *obj;
Eric Anholt673a3942008-07-30 12:06:12 -07001704 unsigned long addr;
1705
Akash Goel1816f922015-01-02 16:29:30 +05301706 if (args->flags & ~(I915_MMAP_WC))
1707 return -EINVAL;
1708
Borislav Petkov568a58e2016-03-29 17:42:01 +02001709 if (args->flags & I915_MMAP_WC && !boot_cpu_has(X86_FEATURE_PAT))
Akash Goel1816f922015-01-02 16:29:30 +05301710 return -ENODEV;
1711
Chris Wilson03ac0642016-07-20 13:31:51 +01001712 obj = i915_gem_object_lookup(file, args->handle);
1713 if (!obj)
Chris Wilsonbf79cb92010-08-04 14:19:46 +01001714 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07001715
Daniel Vetter1286ff72012-05-10 15:25:09 +02001716 /* prime objects have no backing filp to GEM mmap
1717 * pages from.
1718 */
Chris Wilson03ac0642016-07-20 13:31:51 +01001719 if (!obj->base.filp) {
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001720 i915_gem_object_put(obj);
Tina Zhang274b2462017-11-14 10:25:12 +00001721 return -ENXIO;
Daniel Vetter1286ff72012-05-10 15:25:09 +02001722 }
1723
Chris Wilson03ac0642016-07-20 13:31:51 +01001724 addr = vm_mmap(obj->base.filp, 0, args->size,
Eric Anholt673a3942008-07-30 12:06:12 -07001725 PROT_READ | PROT_WRITE, MAP_SHARED,
1726 args->offset);
Akash Goel1816f922015-01-02 16:29:30 +05301727 if (args->flags & I915_MMAP_WC) {
1728 struct mm_struct *mm = current->mm;
1729 struct vm_area_struct *vma;
1730
Michal Hocko80a89a52016-05-23 16:26:11 -07001731 if (down_write_killable(&mm->mmap_sem)) {
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001732 i915_gem_object_put(obj);
Michal Hocko80a89a52016-05-23 16:26:11 -07001733 return -EINTR;
1734 }
Akash Goel1816f922015-01-02 16:29:30 +05301735 vma = find_vma(mm, addr);
1736 if (vma)
1737 vma->vm_page_prot =
1738 pgprot_writecombine(vm_get_page_prot(vma->vm_flags));
1739 else
1740 addr = -ENOMEM;
1741 up_write(&mm->mmap_sem);
Chris Wilsonaeecc962016-06-17 14:46:39 -03001742
1743 /* This may race, but that's ok, it only gets set */
Chris Wilson50349242016-08-18 17:17:04 +01001744 WRITE_ONCE(obj->frontbuffer_ggtt_origin, ORIGIN_CPU);
Akash Goel1816f922015-01-02 16:29:30 +05301745 }
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001746 i915_gem_object_put(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001747 if (IS_ERR((void *)addr))
1748 return addr;
1749
Jani Nikula739f3ab2019-01-16 11:15:19 +02001750 args->addr_ptr = (u64)addr;
Eric Anholt673a3942008-07-30 12:06:12 -07001751
1752 return 0;
1753}
1754
Chris Wilsond899ace2018-07-25 16:54:47 +01001755static unsigned int tile_row_pages(const struct drm_i915_gem_object *obj)
Chris Wilson03af84f2016-08-18 17:17:01 +01001756{
Chris Wilson6649a0b2017-01-09 16:16:08 +00001757 return i915_gem_object_get_tile_row_size(obj) >> PAGE_SHIFT;
Chris Wilson03af84f2016-08-18 17:17:01 +01001758}
1759
Jesse Barnesde151cf2008-11-12 10:03:55 -08001760/**
Chris Wilson4cc69072016-08-25 19:05:19 +01001761 * i915_gem_mmap_gtt_version - report the current feature set for GTT mmaps
1762 *
1763 * A history of the GTT mmap interface:
1764 *
1765 * 0 - Everything had to fit into the GTT. Both parties of a memcpy had to
1766 * aligned and suitable for fencing, and still fit into the available
1767 * mappable space left by the pinned display objects. A classic problem
1768 * we called the page-fault-of-doom where we would ping-pong between
1769 * two objects that could not fit inside the GTT and so the memcpy
1770 * would page one object in at the expense of the other between every
1771 * single byte.
1772 *
1773 * 1 - Objects can be any size, and have any compatible fencing (X Y, or none
1774 * as set via i915_gem_set_tiling() [DRM_I915_GEM_SET_TILING]). If the
1775 * object is too large for the available space (or simply too large
1776 * for the mappable aperture!), a view is created instead and faulted
1777 * into userspace. (This view is aligned and sized appropriately for
1778 * fenced access.)
1779 *
Chris Wilsone22d8e32017-04-12 12:01:11 +01001780 * 2 - Recognise WC as a separate cache domain so that we can flush the
1781 * delayed writes via GTT before performing direct access via WC.
1782 *
Chris Wilson4cc69072016-08-25 19:05:19 +01001783 * Restrictions:
1784 *
1785 * * snoopable objects cannot be accessed via the GTT. It can cause machine
1786 * hangs on some architectures, corruption on others. An attempt to service
1787 * a GTT page fault from a snoopable object will generate a SIGBUS.
1788 *
1789 * * the object must be able to fit into RAM (physical memory, though no
1790 * limited to the mappable aperture).
1791 *
1792 *
1793 * Caveats:
1794 *
1795 * * a new GTT page fault will synchronize rendering from the GPU and flush
1796 * all data to system memory. Subsequent access will not be synchronized.
1797 *
1798 * * all mappings are revoked on runtime device suspend.
1799 *
1800 * * there are only 8, 16 or 32 fence registers to share between all users
1801 * (older machines require fence register for display and blitter access
1802 * as well). Contention of the fence registers will cause the previous users
1803 * to be unmapped and any new access will generate new page faults.
1804 *
1805 * * running out of memory while servicing a fault may generate a SIGBUS,
1806 * rather than the expected SIGSEGV.
1807 */
1808int i915_gem_mmap_gtt_version(void)
1809{
Chris Wilsone22d8e32017-04-12 12:01:11 +01001810 return 2;
Chris Wilson4cc69072016-08-25 19:05:19 +01001811}
1812
Chris Wilson2d4281b2017-01-10 09:56:32 +00001813static inline struct i915_ggtt_view
Chris Wilsond899ace2018-07-25 16:54:47 +01001814compute_partial_view(const struct drm_i915_gem_object *obj,
Chris Wilson2d4281b2017-01-10 09:56:32 +00001815 pgoff_t page_offset,
1816 unsigned int chunk)
1817{
1818 struct i915_ggtt_view view;
1819
1820 if (i915_gem_object_is_tiled(obj))
1821 chunk = roundup(chunk, tile_row_pages(obj));
1822
Chris Wilson2d4281b2017-01-10 09:56:32 +00001823 view.type = I915_GGTT_VIEW_PARTIAL;
Chris Wilson8bab11932017-01-14 00:28:25 +00001824 view.partial.offset = rounddown(page_offset, chunk);
1825 view.partial.size =
Chris Wilson2d4281b2017-01-10 09:56:32 +00001826 min_t(unsigned int, chunk,
Chris Wilson8bab11932017-01-14 00:28:25 +00001827 (obj->base.size >> PAGE_SHIFT) - view.partial.offset);
Chris Wilson2d4281b2017-01-10 09:56:32 +00001828
1829 /* If the partial covers the entire object, just create a normal VMA. */
1830 if (chunk >= obj->base.size >> PAGE_SHIFT)
1831 view.type = I915_GGTT_VIEW_NORMAL;
1832
1833 return view;
1834}
1835
Chris Wilson4cc69072016-08-25 19:05:19 +01001836/**
Jesse Barnesde151cf2008-11-12 10:03:55 -08001837 * i915_gem_fault - fault a page into the GTT
Geliang Tangd9072a32015-09-15 05:58:44 -07001838 * @vmf: fault info
Jesse Barnesde151cf2008-11-12 10:03:55 -08001839 *
1840 * The fault handler is set up by drm_gem_mmap() when a object is GTT mapped
1841 * from userspace. The fault handler takes care of binding the object to
1842 * the GTT (if needed), allocating and programming a fence register (again,
1843 * only if needed based on whether the old reg is still valid or the object
1844 * is tiled) and inserting a new PTE into the faulting process.
1845 *
1846 * Note that the faulting process may involve evicting existing objects
1847 * from the GTT and/or fence registers to make room. So performance may
1848 * suffer if the GTT working set is large or there are few fence registers
1849 * left.
Chris Wilson4cc69072016-08-25 19:05:19 +01001850 *
1851 * The current feature set supported by i915_gem_fault() and thus GTT mmaps
1852 * is exposed via I915_PARAM_MMAP_GTT_VERSION (see i915_gem_mmap_gtt_version).
Jesse Barnesde151cf2008-11-12 10:03:55 -08001853 */
Chris Wilson52137012018-06-06 22:45:20 +01001854vm_fault_t i915_gem_fault(struct vm_fault *vmf)
Jesse Barnesde151cf2008-11-12 10:03:55 -08001855{
Chris Wilson420980c2018-06-05 14:57:46 +01001856#define MIN_CHUNK_PAGES (SZ_1M >> PAGE_SHIFT)
Dave Jiang11bac802017-02-24 14:56:41 -08001857 struct vm_area_struct *area = vmf->vma;
Chris Wilson058d88c2016-08-15 10:49:06 +01001858 struct drm_i915_gem_object *obj = to_intel_bo(area->vm_private_data);
Chris Wilson05394f32010-11-08 19:18:58 +00001859 struct drm_device *dev = obj->base.dev;
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03001860 struct drm_i915_private *dev_priv = to_i915(dev);
1861 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Chris Wilsonaae7c062018-09-03 09:33:34 +01001862 bool write = area->vm_flags & VM_WRITE;
Chris Wilson538ef962019-01-14 14:21:18 +00001863 intel_wakeref_t wakeref;
Chris Wilson058d88c2016-08-15 10:49:06 +01001864 struct i915_vma *vma;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001865 pgoff_t page_offset;
Chris Wilsonb8f90962016-08-05 10:14:07 +01001866 int ret;
Paulo Zanonif65c9162013-11-27 18:20:34 -02001867
Chris Wilson3e977ac2018-07-12 19:53:13 +01001868 /* Sanity check that we allow writing into this object */
1869 if (i915_gem_object_is_readonly(obj) && write)
1870 return VM_FAULT_SIGBUS;
1871
Jesse Barnesde151cf2008-11-12 10:03:55 -08001872 /* We don't use vmf->pgoff since that has the fake offset */
Jan Kara1a29d852016-12-14 15:07:01 -08001873 page_offset = (vmf->address - area->vm_start) >> PAGE_SHIFT;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001874
Chris Wilsondb53a302011-02-03 11:57:46 +00001875 trace_i915_gem_object_fault(obj, page_offset, true, write);
1876
Chris Wilson6e4930f2014-02-07 18:37:06 -02001877 /* Try to flush the object off the GPU first without holding the lock.
Chris Wilsonb8f90962016-08-05 10:14:07 +01001878 * Upon acquiring the lock, we will perform our sanity checks and then
Chris Wilson6e4930f2014-02-07 18:37:06 -02001879 * repeat the flush holding the lock in the normal manner to catch cases
1880 * where we are gazumped.
1881 */
Chris Wilsone95433c2016-10-28 13:58:27 +01001882 ret = i915_gem_object_wait(obj,
1883 I915_WAIT_INTERRUPTIBLE,
1884 MAX_SCHEDULE_TIMEOUT,
1885 NULL);
Chris Wilson6e4930f2014-02-07 18:37:06 -02001886 if (ret)
Chris Wilsonb8f90962016-08-05 10:14:07 +01001887 goto err;
1888
Chris Wilson40e62d52016-10-28 13:58:41 +01001889 ret = i915_gem_object_pin_pages(obj);
1890 if (ret)
1891 goto err;
1892
Chris Wilson538ef962019-01-14 14:21:18 +00001893 wakeref = intel_runtime_pm_get(dev_priv);
Chris Wilsonb8f90962016-08-05 10:14:07 +01001894
1895 ret = i915_mutex_lock_interruptible(dev);
1896 if (ret)
1897 goto err_rpm;
Chris Wilson6e4930f2014-02-07 18:37:06 -02001898
Chris Wilsoneb119bd2012-12-16 12:43:36 +00001899 /* Access to snoopable pages through the GTT is incoherent. */
Tvrtko Ursulin0031fb92016-11-04 14:42:44 +00001900 if (obj->cache_level != I915_CACHE_NONE && !HAS_LLC(dev_priv)) {
Chris Wilsonddeff6e2014-05-28 16:16:41 +01001901 ret = -EFAULT;
Chris Wilsonb8f90962016-08-05 10:14:07 +01001902 goto err_unlock;
Chris Wilsoneb119bd2012-12-16 12:43:36 +00001903 }
1904
Chris Wilson82118872016-08-18 17:17:05 +01001905
Chris Wilsona61007a2016-08-18 17:17:02 +01001906 /* Now pin it into the GTT as needed */
Chris Wilson7e7367d2018-06-30 10:05:09 +01001907 vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0,
1908 PIN_MAPPABLE |
1909 PIN_NONBLOCK |
1910 PIN_NONFAULT);
Chris Wilsona61007a2016-08-18 17:17:02 +01001911 if (IS_ERR(vma)) {
Chris Wilsona61007a2016-08-18 17:17:02 +01001912 /* Use a partial view if it is bigger than available space */
Chris Wilson2d4281b2017-01-10 09:56:32 +00001913 struct i915_ggtt_view view =
Chris Wilson8201c1f2017-01-10 09:56:33 +00001914 compute_partial_view(obj, page_offset, MIN_CHUNK_PAGES);
Chris Wilson7e7367d2018-06-30 10:05:09 +01001915 unsigned int flags;
Chris Wilsonaa136d92016-08-18 17:17:03 +01001916
Chris Wilson7e7367d2018-06-30 10:05:09 +01001917 flags = PIN_MAPPABLE;
1918 if (view.type == I915_GGTT_VIEW_NORMAL)
1919 flags |= PIN_NONBLOCK; /* avoid warnings for pinned */
1920
1921 /*
1922 * Userspace is now writing through an untracked VMA, abandon
Chris Wilson50349242016-08-18 17:17:04 +01001923 * all hope that the hardware is able to track future writes.
1924 */
1925 obj->frontbuffer_ggtt_origin = ORIGIN_CPU;
1926
Chris Wilson7e7367d2018-06-30 10:05:09 +01001927 vma = i915_gem_object_ggtt_pin(obj, &view, 0, 0, flags);
1928 if (IS_ERR(vma) && !view.type) {
1929 flags = PIN_MAPPABLE;
1930 view.type = I915_GGTT_VIEW_PARTIAL;
1931 vma = i915_gem_object_ggtt_pin(obj, &view, 0, 0, flags);
1932 }
Chris Wilsona61007a2016-08-18 17:17:02 +01001933 }
Chris Wilson058d88c2016-08-15 10:49:06 +01001934 if (IS_ERR(vma)) {
1935 ret = PTR_ERR(vma);
Chris Wilsonb8f90962016-08-05 10:14:07 +01001936 goto err_unlock;
Chris Wilson058d88c2016-08-15 10:49:06 +01001937 }
Jesse Barnesde151cf2008-11-12 10:03:55 -08001938
Chris Wilsonc9839302012-11-20 10:45:17 +00001939 ret = i915_gem_object_set_to_gtt_domain(obj, write);
1940 if (ret)
Chris Wilsonb8f90962016-08-05 10:14:07 +01001941 goto err_unpin;
Chris Wilsonc9839302012-11-20 10:45:17 +00001942
Chris Wilson3bd40732017-10-09 09:43:56 +01001943 ret = i915_vma_pin_fence(vma);
Chris Wilsonc9839302012-11-20 10:45:17 +00001944 if (ret)
Chris Wilsonb8f90962016-08-05 10:14:07 +01001945 goto err_unpin;
Chris Wilson7d1c4802010-08-07 21:45:03 +01001946
Chris Wilsonb90b91d2014-06-10 12:14:40 +01001947 /* Finally, remap it using the new GTT offset */
Chris Wilsonc58305a2016-08-19 16:54:28 +01001948 ret = remap_io_mapping(area,
Chris Wilson8bab11932017-01-14 00:28:25 +00001949 area->vm_start + (vma->ggtt_view.partial.offset << PAGE_SHIFT),
Matthew Auld73ebd502017-12-11 15:18:20 +00001950 (ggtt->gmadr.start + vma->node.start) >> PAGE_SHIFT,
Chris Wilsonc58305a2016-08-19 16:54:28 +01001951 min_t(u64, vma->size, area->vm_end - area->vm_start),
Matthew Auld73ebd502017-12-11 15:18:20 +00001952 &ggtt->iomap);
Chris Wilsona65adaf2017-10-09 09:43:57 +01001953 if (ret)
1954 goto err_fence;
Chris Wilsona61007a2016-08-18 17:17:02 +01001955
Chris Wilsona65adaf2017-10-09 09:43:57 +01001956 /* Mark as being mmapped into userspace for later revocation */
1957 assert_rpm_wakelock_held(dev_priv);
1958 if (!i915_vma_set_userfault(vma) && !obj->userfault_count++)
1959 list_add(&obj->userfault_link, &dev_priv->mm.userfault_list);
1960 GEM_BUG_ON(!obj->userfault_count);
1961
Chris Wilson7125397b2017-12-06 12:49:14 +00001962 i915_vma_set_ggtt_write(vma);
1963
Chris Wilsona65adaf2017-10-09 09:43:57 +01001964err_fence:
Chris Wilson3bd40732017-10-09 09:43:56 +01001965 i915_vma_unpin_fence(vma);
Chris Wilsonb8f90962016-08-05 10:14:07 +01001966err_unpin:
Chris Wilson058d88c2016-08-15 10:49:06 +01001967 __i915_vma_unpin(vma);
Chris Wilsonb8f90962016-08-05 10:14:07 +01001968err_unlock:
Jesse Barnesde151cf2008-11-12 10:03:55 -08001969 mutex_unlock(&dev->struct_mutex);
Chris Wilsonb8f90962016-08-05 10:14:07 +01001970err_rpm:
Chris Wilson538ef962019-01-14 14:21:18 +00001971 intel_runtime_pm_put(dev_priv, wakeref);
Chris Wilson40e62d52016-10-28 13:58:41 +01001972 i915_gem_object_unpin_pages(obj);
Chris Wilsonb8f90962016-08-05 10:14:07 +01001973err:
Jesse Barnesde151cf2008-11-12 10:03:55 -08001974 switch (ret) {
Chris Wilsond9bc7e92011-02-07 13:09:31 +00001975 case -EIO:
Daniel Vetter2232f032014-09-04 09:36:18 +02001976 /*
1977 * We eat errors when the gpu is terminally wedged to avoid
1978 * userspace unduly crashing (gl has no provisions for mmaps to
1979 * fail). But any other -EIO isn't ours (e.g. swap in failure)
1980 * and so needs to be reported.
1981 */
Chris Wilson52137012018-06-06 22:45:20 +01001982 if (!i915_terminally_wedged(&dev_priv->gpu_error))
1983 return VM_FAULT_SIGBUS;
Gustavo A. R. Silvaf0d759f2018-06-28 17:35:41 -05001984 /* else: fall through */
Chris Wilson045e7692010-11-07 09:18:22 +00001985 case -EAGAIN:
Daniel Vetter571c6082013-09-12 17:57:28 +02001986 /*
1987 * EAGAIN means the gpu is hung and we'll wait for the error
1988 * handler to reset everything when re-faulting in
1989 * i915_mutex_lock_interruptible.
Chris Wilsond9bc7e92011-02-07 13:09:31 +00001990 */
Chris Wilsonc7150892009-09-23 00:43:56 +01001991 case 0:
1992 case -ERESTARTSYS:
Chris Wilsonbed636a2011-02-11 20:31:19 +00001993 case -EINTR:
Dmitry Rogozhkine79e0fe2012-10-03 17:15:26 +03001994 case -EBUSY:
1995 /*
1996 * EBUSY is ok: this just means that another thread
1997 * already did the job.
1998 */
Chris Wilson52137012018-06-06 22:45:20 +01001999 return VM_FAULT_NOPAGE;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002000 case -ENOMEM:
Chris Wilson52137012018-06-06 22:45:20 +01002001 return VM_FAULT_OOM;
Daniel Vettera7c2e1a2012-10-17 11:17:16 +02002002 case -ENOSPC:
Chris Wilson45d67812014-01-31 11:34:57 +00002003 case -EFAULT:
Chris Wilson52137012018-06-06 22:45:20 +01002004 return VM_FAULT_SIGBUS;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002005 default:
Daniel Vettera7c2e1a2012-10-17 11:17:16 +02002006 WARN_ONCE(ret, "unhandled error in i915_gem_fault: %i\n", ret);
Chris Wilson52137012018-06-06 22:45:20 +01002007 return VM_FAULT_SIGBUS;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002008 }
2009}
2010
Chris Wilsona65adaf2017-10-09 09:43:57 +01002011static void __i915_gem_object_release_mmap(struct drm_i915_gem_object *obj)
2012{
2013 struct i915_vma *vma;
2014
2015 GEM_BUG_ON(!obj->userfault_count);
2016
2017 obj->userfault_count = 0;
2018 list_del(&obj->userfault_link);
2019 drm_vma_node_unmap(&obj->base.vma_node,
2020 obj->base.dev->anon_inode->i_mapping);
2021
Chris Wilsone2189dd2017-12-07 21:14:07 +00002022 for_each_ggtt_vma(vma, obj)
Chris Wilsona65adaf2017-10-09 09:43:57 +01002023 i915_vma_unset_userfault(vma);
Chris Wilsona65adaf2017-10-09 09:43:57 +01002024}
2025
Jesse Barnesde151cf2008-11-12 10:03:55 -08002026/**
Chris Wilson901782b2009-07-10 08:18:50 +01002027 * i915_gem_release_mmap - remove physical page mappings
2028 * @obj: obj in question
2029 *
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02002030 * Preserve the reservation of the mmapping with the DRM core code, but
Chris Wilson901782b2009-07-10 08:18:50 +01002031 * relinquish ownership of the pages back to the system.
2032 *
2033 * It is vital that we remove the page mapping if we have mapped a tiled
2034 * object through the GTT and then lose the fence register due to
2035 * resource pressure. Similarly if the object has been moved out of the
2036 * aperture, than pages mapped into userspace must be revoked. Removing the
2037 * mapping will then trigger a page fault on the next user access, allowing
2038 * fixup by i915_gem_fault().
2039 */
Eric Anholtd05ca302009-07-10 13:02:26 -07002040void
Chris Wilson05394f32010-11-08 19:18:58 +00002041i915_gem_release_mmap(struct drm_i915_gem_object *obj)
Chris Wilson901782b2009-07-10 08:18:50 +01002042{
Chris Wilson275f0392016-10-24 13:42:14 +01002043 struct drm_i915_private *i915 = to_i915(obj->base.dev);
Chris Wilson538ef962019-01-14 14:21:18 +00002044 intel_wakeref_t wakeref;
Chris Wilson275f0392016-10-24 13:42:14 +01002045
Chris Wilson349f2cc2016-04-13 17:35:12 +01002046 /* Serialisation between user GTT access and our code depends upon
2047 * revoking the CPU's PTE whilst the mutex is held. The next user
2048 * pagefault then has to wait until we release the mutex.
Chris Wilson9c870d02016-10-24 13:42:15 +01002049 *
2050 * Note that RPM complicates somewhat by adding an additional
2051 * requirement that operations to the GGTT be made holding the RPM
2052 * wakeref.
Chris Wilson349f2cc2016-04-13 17:35:12 +01002053 */
Chris Wilson275f0392016-10-24 13:42:14 +01002054 lockdep_assert_held(&i915->drm.struct_mutex);
Chris Wilson538ef962019-01-14 14:21:18 +00002055 wakeref = intel_runtime_pm_get(i915);
Chris Wilson349f2cc2016-04-13 17:35:12 +01002056
Chris Wilsona65adaf2017-10-09 09:43:57 +01002057 if (!obj->userfault_count)
Chris Wilson9c870d02016-10-24 13:42:15 +01002058 goto out;
Chris Wilson901782b2009-07-10 08:18:50 +01002059
Chris Wilsona65adaf2017-10-09 09:43:57 +01002060 __i915_gem_object_release_mmap(obj);
Chris Wilson349f2cc2016-04-13 17:35:12 +01002061
2062 /* Ensure that the CPU's PTE are revoked and there are not outstanding
2063 * memory transactions from userspace before we return. The TLB
2064 * flushing implied above by changing the PTE above *should* be
2065 * sufficient, an extra barrier here just provides us with a bit
2066 * of paranoid documentation about our requirement to serialise
2067 * memory writes before touching registers / GSM.
2068 */
2069 wmb();
Chris Wilson9c870d02016-10-24 13:42:15 +01002070
2071out:
Chris Wilson538ef962019-01-14 14:21:18 +00002072 intel_runtime_pm_put(i915, wakeref);
Chris Wilson901782b2009-07-10 08:18:50 +01002073}
2074
Chris Wilson7c108fd2016-10-24 13:42:18 +01002075void i915_gem_runtime_suspend(struct drm_i915_private *dev_priv)
Chris Wilsoneedd10f2014-06-16 08:57:44 +01002076{
Chris Wilson3594a3e2016-10-24 13:42:16 +01002077 struct drm_i915_gem_object *obj, *on;
Chris Wilson7c108fd2016-10-24 13:42:18 +01002078 int i;
Chris Wilsoneedd10f2014-06-16 08:57:44 +01002079
Chris Wilson3594a3e2016-10-24 13:42:16 +01002080 /*
2081 * Only called during RPM suspend. All users of the userfault_list
2082 * must be holding an RPM wakeref to ensure that this can not
2083 * run concurrently with themselves (and use the struct_mutex for
2084 * protection between themselves).
2085 */
2086
2087 list_for_each_entry_safe(obj, on,
Chris Wilsona65adaf2017-10-09 09:43:57 +01002088 &dev_priv->mm.userfault_list, userfault_link)
2089 __i915_gem_object_release_mmap(obj);
Chris Wilson7c108fd2016-10-24 13:42:18 +01002090
2091 /* The fence will be lost when the device powers down. If any were
2092 * in use by hardware (i.e. they are pinned), we should not be powering
2093 * down! All other fences will be reacquired by the user upon waking.
2094 */
2095 for (i = 0; i < dev_priv->num_fence_regs; i++) {
2096 struct drm_i915_fence_reg *reg = &dev_priv->fence_regs[i];
2097
Chris Wilsone0ec3ec2017-02-03 12:57:17 +00002098 /* Ideally we want to assert that the fence register is not
2099 * live at this point (i.e. that no piece of code will be
2100 * trying to write through fence + GTT, as that both violates
2101 * our tracking of activity and associated locking/barriers,
2102 * but also is illegal given that the hw is powered down).
2103 *
2104 * Previously we used reg->pin_count as a "liveness" indicator.
2105 * That is not sufficient, and we need a more fine-grained
2106 * tool if we want to have a sanity check here.
2107 */
Chris Wilson7c108fd2016-10-24 13:42:18 +01002108
2109 if (!reg->vma)
2110 continue;
2111
Chris Wilsona65adaf2017-10-09 09:43:57 +01002112 GEM_BUG_ON(i915_vma_has_userfault(reg->vma));
Chris Wilson7c108fd2016-10-24 13:42:18 +01002113 reg->dirty = true;
2114 }
Chris Wilsoneedd10f2014-06-16 08:57:44 +01002115}
2116
Chris Wilsond8cb5082012-08-11 15:41:03 +01002117static int i915_gem_object_create_mmap_offset(struct drm_i915_gem_object *obj)
2118{
Chris Wilsonfac5e232016-07-04 11:34:36 +01002119 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
Chris Wilsonf3f61842016-08-05 10:14:14 +01002120 int err;
Chris Wilsond8cb5082012-08-11 15:41:03 +01002121
Chris Wilsonf3f61842016-08-05 10:14:14 +01002122 err = drm_gem_create_mmap_offset(&obj->base);
Chris Wilsonb42a13d2017-01-06 15:22:40 +00002123 if (likely(!err))
Chris Wilsonf3f61842016-08-05 10:14:14 +01002124 return 0;
Daniel Vetterda494d72012-12-20 15:11:16 +01002125
Chris Wilsonb42a13d2017-01-06 15:22:40 +00002126 /* Attempt to reap some mmap space from dead objects */
2127 do {
Chris Wilsonec625fb2018-07-09 13:20:42 +01002128 err = i915_gem_wait_for_idle(dev_priv,
2129 I915_WAIT_INTERRUPTIBLE,
2130 MAX_SCHEDULE_TIMEOUT);
Chris Wilsonb42a13d2017-01-06 15:22:40 +00002131 if (err)
2132 break;
Chris Wilsond8cb5082012-08-11 15:41:03 +01002133
Chris Wilsonb42a13d2017-01-06 15:22:40 +00002134 i915_gem_drain_freed_objects(dev_priv);
Chris Wilsonf3f61842016-08-05 10:14:14 +01002135 err = drm_gem_create_mmap_offset(&obj->base);
Chris Wilsonb42a13d2017-01-06 15:22:40 +00002136 if (!err)
2137 break;
2138
2139 } while (flush_delayed_work(&dev_priv->gt.retire_work));
Daniel Vetterda494d72012-12-20 15:11:16 +01002140
Chris Wilsonf3f61842016-08-05 10:14:14 +01002141 return err;
Chris Wilsond8cb5082012-08-11 15:41:03 +01002142}
2143
2144static void i915_gem_object_free_mmap_offset(struct drm_i915_gem_object *obj)
2145{
Chris Wilsond8cb5082012-08-11 15:41:03 +01002146 drm_gem_free_mmap_offset(&obj->base);
2147}
2148
Dave Airlieda6b51d2014-12-24 13:11:17 +10002149int
Dave Airlieff72145b2011-02-07 12:16:14 +10002150i915_gem_mmap_gtt(struct drm_file *file,
2151 struct drm_device *dev,
Jani Nikula739f3ab2019-01-16 11:15:19 +02002152 u32 handle,
2153 u64 *offset)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002154{
Chris Wilson05394f32010-11-08 19:18:58 +00002155 struct drm_i915_gem_object *obj;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002156 int ret;
2157
Chris Wilson03ac0642016-07-20 13:31:51 +01002158 obj = i915_gem_object_lookup(file, handle);
Chris Wilsonf3f61842016-08-05 10:14:14 +01002159 if (!obj)
2160 return -ENOENT;
Chris Wilsonab182822009-09-22 18:46:17 +01002161
Chris Wilsond8cb5082012-08-11 15:41:03 +01002162 ret = i915_gem_object_create_mmap_offset(obj);
Chris Wilsonf3f61842016-08-05 10:14:14 +01002163 if (ret == 0)
2164 *offset = drm_vma_node_offset_addr(&obj->base.vma_node);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002165
Chris Wilsonf0cd5182016-10-28 13:58:43 +01002166 i915_gem_object_put(obj);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01002167 return ret;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002168}
2169
Dave Airlieff72145b2011-02-07 12:16:14 +10002170/**
2171 * i915_gem_mmap_gtt_ioctl - prepare an object for GTT mmap'ing
2172 * @dev: DRM device
2173 * @data: GTT mapping ioctl data
2174 * @file: GEM object info
2175 *
2176 * Simply returns the fake offset to userspace so it can mmap it.
2177 * The mmap call will end up in drm_gem_mmap(), which will set things
2178 * up so we can get faults in the handler above.
2179 *
2180 * The fault handler will take care of binding the object into the GTT
2181 * (since it may have been evicted to make room for something), allocating
2182 * a fence register, and mapping the appropriate aperture address into
2183 * userspace.
2184 */
2185int
2186i915_gem_mmap_gtt_ioctl(struct drm_device *dev, void *data,
2187 struct drm_file *file)
2188{
2189 struct drm_i915_gem_mmap_gtt *args = data;
2190
Dave Airlieda6b51d2014-12-24 13:11:17 +10002191 return i915_gem_mmap_gtt(file, dev, args->handle, &args->offset);
Dave Airlieff72145b2011-02-07 12:16:14 +10002192}
2193
Daniel Vetter225067e2012-08-20 10:23:20 +02002194/* Immediately discard the backing storage */
2195static void
2196i915_gem_object_truncate(struct drm_i915_gem_object *obj)
Chris Wilsone5281cc2010-10-28 13:45:36 +01002197{
Chris Wilson4d6294bf2012-08-11 15:41:05 +01002198 i915_gem_object_free_mmap_offset(obj);
Daniel Vetter1286ff72012-05-10 15:25:09 +02002199
Chris Wilson4d6294bf2012-08-11 15:41:05 +01002200 if (obj->base.filp == NULL)
2201 return;
2202
Daniel Vetter225067e2012-08-20 10:23:20 +02002203 /* Our goal here is to return as much of the memory as
2204 * is possible back to the system as we are called from OOM.
2205 * To do this we must instruct the shmfs to drop all of its
2206 * backing pages, *now*.
Chris Wilsone5281cc2010-10-28 13:45:36 +01002207 */
Chris Wilson55372522014-03-25 13:23:06 +00002208 shmem_truncate_range(file_inode(obj->base.filp), 0, (loff_t)-1);
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002209 obj->mm.madv = __I915_MADV_PURGED;
Chris Wilson4e5462e2017-03-07 13:20:31 +00002210 obj->mm.pages = ERR_PTR(-EFAULT);
Chris Wilsone5281cc2010-10-28 13:45:36 +01002211}
Chris Wilsone5281cc2010-10-28 13:45:36 +01002212
Chris Wilson55372522014-03-25 13:23:06 +00002213/* Try to discard unwanted pages */
Chris Wilson03ac84f2016-10-28 13:58:36 +01002214void __i915_gem_object_invalidate(struct drm_i915_gem_object *obj)
Daniel Vetter225067e2012-08-20 10:23:20 +02002215{
Chris Wilson55372522014-03-25 13:23:06 +00002216 struct address_space *mapping;
2217
Chris Wilson1233e2d2016-10-28 13:58:37 +01002218 lockdep_assert_held(&obj->mm.lock);
Chris Wilsonf1fa4f42017-10-13 21:26:13 +01002219 GEM_BUG_ON(i915_gem_object_has_pages(obj));
Chris Wilson1233e2d2016-10-28 13:58:37 +01002220
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002221 switch (obj->mm.madv) {
Chris Wilson55372522014-03-25 13:23:06 +00002222 case I915_MADV_DONTNEED:
2223 i915_gem_object_truncate(obj);
2224 case __I915_MADV_PURGED:
2225 return;
2226 }
2227
2228 if (obj->base.filp == NULL)
2229 return;
2230
Al Viro93c76a32015-12-04 23:45:44 -05002231 mapping = obj->base.filp->f_mapping,
Chris Wilson55372522014-03-25 13:23:06 +00002232 invalidate_mapping_pages(mapping, 0, (loff_t)-1);
Chris Wilsone5281cc2010-10-28 13:45:36 +01002233}
2234
Kuo-Hsin Yang64e3d122018-11-06 13:23:24 +00002235/*
2236 * Move pages to appropriate lru and release the pagevec, decrementing the
2237 * ref count of those pages.
2238 */
2239static void check_release_pagevec(struct pagevec *pvec)
2240{
2241 check_move_unevictable_pages(pvec);
2242 __pagevec_release(pvec);
2243 cond_resched();
2244}
2245
Chris Wilson5cdf5882010-09-27 15:51:07 +01002246static void
Chris Wilson03ac84f2016-10-28 13:58:36 +01002247i915_gem_object_put_pages_gtt(struct drm_i915_gem_object *obj,
2248 struct sg_table *pages)
Eric Anholt673a3942008-07-30 12:06:12 -07002249{
Dave Gordon85d12252016-05-20 11:54:06 +01002250 struct sgt_iter sgt_iter;
Kuo-Hsin Yang64e3d122018-11-06 13:23:24 +00002251 struct pagevec pvec;
Dave Gordon85d12252016-05-20 11:54:06 +01002252 struct page *page;
Daniel Vetter1286ff72012-05-10 15:25:09 +02002253
Chris Wilsone5facdf2016-12-23 14:57:57 +00002254 __i915_gem_object_release_shmem(obj, pages, true);
Eric Anholt856fa192009-03-19 14:10:50 -07002255
Chris Wilson03ac84f2016-10-28 13:58:36 +01002256 i915_gem_gtt_finish_pages(obj, pages);
Imre Deake2273302015-07-09 12:59:05 +03002257
Daniel Vetter6dacfd22011-09-12 21:30:02 +02002258 if (i915_gem_object_needs_bit17_swizzle(obj))
Chris Wilson03ac84f2016-10-28 13:58:36 +01002259 i915_gem_object_save_bit_17_swizzle(obj, pages);
Eric Anholt280b7132009-03-12 16:56:27 -07002260
Kuo-Hsin Yang64e3d122018-11-06 13:23:24 +00002261 mapping_clear_unevictable(file_inode(obj->base.filp)->i_mapping);
2262
2263 pagevec_init(&pvec);
Chris Wilson03ac84f2016-10-28 13:58:36 +01002264 for_each_sgt_page(page, sgt_iter, pages) {
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002265 if (obj->mm.dirty)
Chris Wilson9da3da62012-06-01 15:20:22 +01002266 set_page_dirty(page);
Chris Wilson3ef94da2009-09-14 16:50:29 +01002267
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002268 if (obj->mm.madv == I915_MADV_WILLNEED)
Chris Wilson9da3da62012-06-01 15:20:22 +01002269 mark_page_accessed(page);
Chris Wilson3ef94da2009-09-14 16:50:29 +01002270
Kuo-Hsin Yang64e3d122018-11-06 13:23:24 +00002271 if (!pagevec_add(&pvec, page))
2272 check_release_pagevec(&pvec);
Chris Wilson3ef94da2009-09-14 16:50:29 +01002273 }
Kuo-Hsin Yang64e3d122018-11-06 13:23:24 +00002274 if (pagevec_count(&pvec))
2275 check_release_pagevec(&pvec);
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002276 obj->mm.dirty = false;
Eric Anholt673a3942008-07-30 12:06:12 -07002277
Chris Wilson03ac84f2016-10-28 13:58:36 +01002278 sg_free_table(pages);
2279 kfree(pages);
Chris Wilson37e680a2012-06-07 15:38:42 +01002280}
2281
Chris Wilson96d77632016-10-28 13:58:33 +01002282static void __i915_gem_object_reset_page_iter(struct drm_i915_gem_object *obj)
2283{
2284 struct radix_tree_iter iter;
Ville Syrjäläc23aa712017-09-01 20:12:51 +03002285 void __rcu **slot;
Chris Wilson96d77632016-10-28 13:58:33 +01002286
Chris Wilsonbea6e982017-10-26 14:00:31 +01002287 rcu_read_lock();
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002288 radix_tree_for_each_slot(slot, &obj->mm.get_page.radix, &iter, 0)
2289 radix_tree_delete(&obj->mm.get_page.radix, iter.index);
Chris Wilsonbea6e982017-10-26 14:00:31 +01002290 rcu_read_unlock();
Chris Wilson96d77632016-10-28 13:58:33 +01002291}
2292
Chris Wilsonacd1c1e2018-06-11 08:55:32 +01002293static struct sg_table *
2294__i915_gem_object_unset_pages(struct drm_i915_gem_object *obj)
Chris Wilson37e680a2012-06-07 15:38:42 +01002295{
Chris Wilsonf2123812017-10-16 12:40:37 +01002296 struct drm_i915_private *i915 = to_i915(obj->base.dev);
Chris Wilson03ac84f2016-10-28 13:58:36 +01002297 struct sg_table *pages;
Chris Wilson37e680a2012-06-07 15:38:42 +01002298
Chris Wilson03ac84f2016-10-28 13:58:36 +01002299 pages = fetch_and_zero(&obj->mm.pages);
Chris Wilson484d9a82019-01-15 12:44:42 +00002300 if (IS_ERR_OR_NULL(pages))
2301 return pages;
Chris Wilsona2165e32012-12-03 11:49:00 +00002302
Chris Wilsonf2123812017-10-16 12:40:37 +01002303 spin_lock(&i915->mm.obj_lock);
2304 list_del(&obj->mm.link);
2305 spin_unlock(&i915->mm.obj_lock);
2306
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002307 if (obj->mm.mapping) {
Chris Wilson4b30cb22016-08-18 17:16:42 +01002308 void *ptr;
2309
Chris Wilson0ce81782017-05-17 13:09:59 +01002310 ptr = page_mask_bits(obj->mm.mapping);
Chris Wilson4b30cb22016-08-18 17:16:42 +01002311 if (is_vmalloc_addr(ptr))
2312 vunmap(ptr);
Chris Wilsonfb8621d2016-04-08 12:11:14 +01002313 else
Chris Wilson4b30cb22016-08-18 17:16:42 +01002314 kunmap(kmap_to_page(ptr));
2315
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002316 obj->mm.mapping = NULL;
Chris Wilson0a798eb2016-04-08 12:11:11 +01002317 }
2318
Chris Wilson96d77632016-10-28 13:58:33 +01002319 __i915_gem_object_reset_page_iter(obj);
Chris Wilsonacd1c1e2018-06-11 08:55:32 +01002320 obj->mm.page_sizes.phys = obj->mm.page_sizes.sg = 0;
Chris Wilson96d77632016-10-28 13:58:33 +01002321
Chris Wilsonacd1c1e2018-06-11 08:55:32 +01002322 return pages;
2323}
2324
Chris Wilson484d9a82019-01-15 12:44:42 +00002325int __i915_gem_object_put_pages(struct drm_i915_gem_object *obj,
2326 enum i915_mm_subclass subclass)
Chris Wilsonacd1c1e2018-06-11 08:55:32 +01002327{
2328 struct sg_table *pages;
Chris Wilson484d9a82019-01-15 12:44:42 +00002329 int ret;
Chris Wilsonacd1c1e2018-06-11 08:55:32 +01002330
2331 if (i915_gem_object_has_pinned_pages(obj))
Chris Wilson484d9a82019-01-15 12:44:42 +00002332 return -EBUSY;
Chris Wilsonacd1c1e2018-06-11 08:55:32 +01002333
2334 GEM_BUG_ON(obj->bind_count);
Chris Wilsonacd1c1e2018-06-11 08:55:32 +01002335
2336 /* May be called by shrinker from within get_pages() (on another bo) */
2337 mutex_lock_nested(&obj->mm.lock, subclass);
Chris Wilson484d9a82019-01-15 12:44:42 +00002338 if (unlikely(atomic_read(&obj->mm.pages_pin_count))) {
2339 ret = -EBUSY;
Chris Wilsonacd1c1e2018-06-11 08:55:32 +01002340 goto unlock;
Chris Wilson484d9a82019-01-15 12:44:42 +00002341 }
Chris Wilsonacd1c1e2018-06-11 08:55:32 +01002342
2343 /*
2344 * ->put_pages might need to allocate memory for the bit17 swizzle
2345 * array, hence protect them from being reaped by removing them from gtt
2346 * lists early.
2347 */
2348 pages = __i915_gem_object_unset_pages(obj);
Chris Wilson484d9a82019-01-15 12:44:42 +00002349
2350 /*
2351 * XXX Temporary hijinx to avoid updating all backends to handle
2352 * NULL pages. In the future, when we have more asynchronous
2353 * get_pages backends we should be better able to handle the
2354 * cancellation of the async task in a more uniform manner.
2355 */
2356 if (!pages && !i915_gem_object_needs_async_cancel(obj))
2357 pages = ERR_PTR(-EINVAL);
2358
Chris Wilson4e5462e2017-03-07 13:20:31 +00002359 if (!IS_ERR(pages))
2360 obj->ops->put_pages(obj, pages);
2361
Chris Wilson484d9a82019-01-15 12:44:42 +00002362 ret = 0;
Chris Wilson1233e2d2016-10-28 13:58:37 +01002363unlock:
2364 mutex_unlock(&obj->mm.lock);
Chris Wilson484d9a82019-01-15 12:44:42 +00002365
2366 return ret;
Chris Wilson6c085a72012-08-20 11:40:46 +02002367}
2368
Tvrtko Ursulinf8e57862018-09-26 09:03:53 +01002369bool i915_sg_trim(struct sg_table *orig_st)
Tvrtko Ursulin0c40ce12016-11-09 15:13:43 +00002370{
2371 struct sg_table new_st;
2372 struct scatterlist *sg, *new_sg;
2373 unsigned int i;
2374
2375 if (orig_st->nents == orig_st->orig_nents)
Chris Wilson935a2f72017-02-13 17:15:13 +00002376 return false;
Tvrtko Ursulin0c40ce12016-11-09 15:13:43 +00002377
Chris Wilson8bfc478f2016-12-23 14:57:58 +00002378 if (sg_alloc_table(&new_st, orig_st->nents, GFP_KERNEL | __GFP_NOWARN))
Chris Wilson935a2f72017-02-13 17:15:13 +00002379 return false;
Tvrtko Ursulin0c40ce12016-11-09 15:13:43 +00002380
2381 new_sg = new_st.sgl;
2382 for_each_sg(orig_st->sgl, sg, orig_st->nents, i) {
2383 sg_set_page(new_sg, sg_page(sg), sg->length, 0);
Matthew Auldc6d22ab2018-09-20 15:27:06 +01002384 sg_dma_address(new_sg) = sg_dma_address(sg);
2385 sg_dma_len(new_sg) = sg_dma_len(sg);
2386
Tvrtko Ursulin0c40ce12016-11-09 15:13:43 +00002387 new_sg = sg_next(new_sg);
2388 }
Chris Wilsonc2dc6cc2016-12-19 12:43:46 +00002389 GEM_BUG_ON(new_sg); /* Should walk exactly nents and hit the end */
Tvrtko Ursulin0c40ce12016-11-09 15:13:43 +00002390
2391 sg_free_table(orig_st);
2392
2393 *orig_st = new_st;
Chris Wilson935a2f72017-02-13 17:15:13 +00002394 return true;
Tvrtko Ursulin0c40ce12016-11-09 15:13:43 +00002395}
2396
Matthew Auldb91b09e2017-10-06 23:18:17 +01002397static int i915_gem_object_get_pages_gtt(struct drm_i915_gem_object *obj)
Eric Anholt673a3942008-07-30 12:06:12 -07002398{
Chris Wilsonfac5e232016-07-04 11:34:36 +01002399 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
Chris Wilsond766ef52016-12-19 12:43:45 +00002400 const unsigned long page_count = obj->base.size / PAGE_SIZE;
2401 unsigned long i;
Eric Anholt673a3942008-07-30 12:06:12 -07002402 struct address_space *mapping;
Chris Wilson9da3da62012-06-01 15:20:22 +01002403 struct sg_table *st;
2404 struct scatterlist *sg;
Dave Gordon85d12252016-05-20 11:54:06 +01002405 struct sgt_iter sgt_iter;
Eric Anholt673a3942008-07-30 12:06:12 -07002406 struct page *page;
Imre Deak90797e62013-02-18 19:28:03 +02002407 unsigned long last_pfn = 0; /* suppress gcc warning */
Tvrtko Ursulin56024522017-08-03 10:14:17 +01002408 unsigned int max_segment = i915_sg_segment_size();
Matthew Auld84e89782017-10-09 12:00:24 +01002409 unsigned int sg_page_sizes;
Kuo-Hsin Yang64e3d122018-11-06 13:23:24 +00002410 struct pagevec pvec;
Chris Wilson4846bf02017-06-09 12:03:46 +01002411 gfp_t noreclaim;
Imre Deake2273302015-07-09 12:59:05 +03002412 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07002413
Chris Wilsone0ff7a72018-09-03 09:33:36 +01002414 /*
2415 * Assert that the object is not currently in any GPU domain. As it
Chris Wilson6c085a72012-08-20 11:40:46 +02002416 * wasn't in the GTT, there shouldn't be any way it could have been in
2417 * a GPU cache
2418 */
Christian Königc0a51fd2018-02-16 13:43:38 +01002419 GEM_BUG_ON(obj->read_domains & I915_GEM_GPU_DOMAINS);
2420 GEM_BUG_ON(obj->write_domain & I915_GEM_GPU_DOMAINS);
Chris Wilson6c085a72012-08-20 11:40:46 +02002421
Chris Wilsone0ff7a72018-09-03 09:33:36 +01002422 /*
2423 * If there's no chance of allocating enough pages for the whole
2424 * object, bail early.
2425 */
Arun KSca79b0c2018-12-28 00:34:29 -08002426 if (page_count > totalram_pages())
Chris Wilsone0ff7a72018-09-03 09:33:36 +01002427 return -ENOMEM;
2428
Chris Wilson9da3da62012-06-01 15:20:22 +01002429 st = kmalloc(sizeof(*st), GFP_KERNEL);
2430 if (st == NULL)
Matthew Auldb91b09e2017-10-06 23:18:17 +01002431 return -ENOMEM;
Eric Anholt673a3942008-07-30 12:06:12 -07002432
Chris Wilsond766ef52016-12-19 12:43:45 +00002433rebuild_st:
Chris Wilson9da3da62012-06-01 15:20:22 +01002434 if (sg_alloc_table(st, page_count, GFP_KERNEL)) {
Chris Wilson9da3da62012-06-01 15:20:22 +01002435 kfree(st);
Matthew Auldb91b09e2017-10-06 23:18:17 +01002436 return -ENOMEM;
Chris Wilson9da3da62012-06-01 15:20:22 +01002437 }
2438
Chris Wilsone0ff7a72018-09-03 09:33:36 +01002439 /*
2440 * Get the list of pages out of our struct file. They'll be pinned
Chris Wilson9da3da62012-06-01 15:20:22 +01002441 * at this point until we release them.
2442 *
2443 * Fail silently without starting the shrinker
2444 */
Al Viro93c76a32015-12-04 23:45:44 -05002445 mapping = obj->base.filp->f_mapping;
Kuo-Hsin Yang64e3d122018-11-06 13:23:24 +00002446 mapping_set_unevictable(mapping);
Chris Wilson0f6ab552017-06-09 12:03:48 +01002447 noreclaim = mapping_gfp_constraint(mapping, ~__GFP_RECLAIM);
Chris Wilson4846bf02017-06-09 12:03:46 +01002448 noreclaim |= __GFP_NORETRY | __GFP_NOWARN;
2449
Imre Deak90797e62013-02-18 19:28:03 +02002450 sg = st->sgl;
2451 st->nents = 0;
Matthew Auld84e89782017-10-09 12:00:24 +01002452 sg_page_sizes = 0;
Imre Deak90797e62013-02-18 19:28:03 +02002453 for (i = 0; i < page_count; i++) {
Chris Wilson4846bf02017-06-09 12:03:46 +01002454 const unsigned int shrink[] = {
2455 I915_SHRINK_BOUND | I915_SHRINK_UNBOUND | I915_SHRINK_PURGEABLE,
2456 0,
2457 }, *s = shrink;
2458 gfp_t gfp = noreclaim;
2459
2460 do {
Chris Wilsone6db7f42018-11-05 17:06:40 +00002461 cond_resched();
Chris Wilson6c085a72012-08-20 11:40:46 +02002462 page = shmem_read_mapping_page_gfp(mapping, i, gfp);
Chris Wilson4846bf02017-06-09 12:03:46 +01002463 if (likely(!IS_ERR(page)))
2464 break;
2465
2466 if (!*s) {
2467 ret = PTR_ERR(page);
2468 goto err_sg;
2469 }
2470
Chris Wilson912d5722017-09-06 16:19:30 -07002471 i915_gem_shrink(dev_priv, 2 * page_count, NULL, *s++);
Chris Wilson24f8e002017-03-22 11:05:21 +00002472
Chris Wilsone0ff7a72018-09-03 09:33:36 +01002473 /*
2474 * We've tried hard to allocate the memory by reaping
Chris Wilson6c085a72012-08-20 11:40:46 +02002475 * our own buffer, now let the real VM do its job and
2476 * go down in flames if truly OOM.
Chris Wilson24f8e002017-03-22 11:05:21 +00002477 *
2478 * However, since graphics tend to be disposable,
2479 * defer the oom here by reporting the ENOMEM back
2480 * to userspace.
Chris Wilson6c085a72012-08-20 11:40:46 +02002481 */
Chris Wilson4846bf02017-06-09 12:03:46 +01002482 if (!*s) {
2483 /* reclaim and warn, but no oom */
2484 gfp = mapping_gfp_mask(mapping);
Chris Wilsoneaf41802017-06-09 12:03:47 +01002485
Chris Wilsone0ff7a72018-09-03 09:33:36 +01002486 /*
2487 * Our bo are always dirty and so we require
Chris Wilsoneaf41802017-06-09 12:03:47 +01002488 * kswapd to reclaim our pages (direct reclaim
2489 * does not effectively begin pageout of our
2490 * buffers on its own). However, direct reclaim
2491 * only waits for kswapd when under allocation
2492 * congestion. So as a result __GFP_RECLAIM is
2493 * unreliable and fails to actually reclaim our
2494 * dirty pages -- unless you try over and over
2495 * again with !__GFP_NORETRY. However, we still
2496 * want to fail this allocation rather than
2497 * trigger the out-of-memory killer and for
Michal Hockodbb32952017-07-12 14:36:55 -07002498 * this we want __GFP_RETRY_MAYFAIL.
Chris Wilsoneaf41802017-06-09 12:03:47 +01002499 */
Michal Hockodbb32952017-07-12 14:36:55 -07002500 gfp |= __GFP_RETRY_MAYFAIL;
Imre Deake2273302015-07-09 12:59:05 +03002501 }
Chris Wilson4846bf02017-06-09 12:03:46 +01002502 } while (1);
2503
Chris Wilson871dfbd2016-10-11 09:20:21 +01002504 if (!i ||
2505 sg->length >= max_segment ||
2506 page_to_pfn(page) != last_pfn + 1) {
Matthew Aulda5c081662017-10-06 23:18:18 +01002507 if (i) {
Matthew Auld84e89782017-10-09 12:00:24 +01002508 sg_page_sizes |= sg->length;
Imre Deak90797e62013-02-18 19:28:03 +02002509 sg = sg_next(sg);
Matthew Aulda5c081662017-10-06 23:18:18 +01002510 }
Imre Deak90797e62013-02-18 19:28:03 +02002511 st->nents++;
2512 sg_set_page(sg, page, PAGE_SIZE, 0);
2513 } else {
2514 sg->length += PAGE_SIZE;
2515 }
2516 last_pfn = page_to_pfn(page);
Daniel Vetter3bbbe702013-10-07 17:15:45 -03002517
2518 /* Check that the i965g/gm workaround works. */
2519 WARN_ON((gfp & __GFP_DMA32) && (last_pfn >= 0x00100000UL));
Eric Anholt673a3942008-07-30 12:06:12 -07002520 }
Matthew Aulda5c081662017-10-06 23:18:18 +01002521 if (sg) { /* loop terminated early; short sg table */
Matthew Auld84e89782017-10-09 12:00:24 +01002522 sg_page_sizes |= sg->length;
Konrad Rzeszutek Wilk426729d2013-06-24 11:47:48 -04002523 sg_mark_end(sg);
Matthew Aulda5c081662017-10-06 23:18:18 +01002524 }
Chris Wilson74ce6b62012-10-19 15:51:06 +01002525
Tvrtko Ursulin0c40ce12016-11-09 15:13:43 +00002526 /* Trim unused sg entries to avoid wasting memory. */
2527 i915_sg_trim(st);
2528
Chris Wilson03ac84f2016-10-28 13:58:36 +01002529 ret = i915_gem_gtt_prepare_pages(obj, st);
Chris Wilsond766ef52016-12-19 12:43:45 +00002530 if (ret) {
Chris Wilsone0ff7a72018-09-03 09:33:36 +01002531 /*
2532 * DMA remapping failed? One possible cause is that
Chris Wilsond766ef52016-12-19 12:43:45 +00002533 * it could not reserve enough large entries, asking
2534 * for PAGE_SIZE chunks instead may be helpful.
2535 */
2536 if (max_segment > PAGE_SIZE) {
2537 for_each_sgt_page(page, sgt_iter, st)
2538 put_page(page);
2539 sg_free_table(st);
2540
2541 max_segment = PAGE_SIZE;
2542 goto rebuild_st;
2543 } else {
2544 dev_warn(&dev_priv->drm.pdev->dev,
2545 "Failed to DMA remap %lu pages\n",
2546 page_count);
2547 goto err_pages;
2548 }
2549 }
Imre Deake2273302015-07-09 12:59:05 +03002550
Eric Anholt673a3942008-07-30 12:06:12 -07002551 if (i915_gem_object_needs_bit17_swizzle(obj))
Chris Wilson03ac84f2016-10-28 13:58:36 +01002552 i915_gem_object_do_bit_17_swizzle(obj, st);
Eric Anholt673a3942008-07-30 12:06:12 -07002553
Matthew Auld84e89782017-10-09 12:00:24 +01002554 __i915_gem_object_set_pages(obj, st, sg_page_sizes);
Matthew Auldb91b09e2017-10-06 23:18:17 +01002555
2556 return 0;
Eric Anholt673a3942008-07-30 12:06:12 -07002557
Chris Wilsonb17993b2016-11-14 11:29:30 +00002558err_sg:
Imre Deak90797e62013-02-18 19:28:03 +02002559 sg_mark_end(sg);
Chris Wilsonb17993b2016-11-14 11:29:30 +00002560err_pages:
Kuo-Hsin Yang64e3d122018-11-06 13:23:24 +00002561 mapping_clear_unevictable(mapping);
2562 pagevec_init(&pvec);
2563 for_each_sgt_page(page, sgt_iter, st) {
2564 if (!pagevec_add(&pvec, page))
2565 check_release_pagevec(&pvec);
2566 }
2567 if (pagevec_count(&pvec))
2568 check_release_pagevec(&pvec);
Chris Wilson9da3da62012-06-01 15:20:22 +01002569 sg_free_table(st);
2570 kfree(st);
Chris Wilson0820baf2014-03-25 13:23:03 +00002571
Chris Wilsone0ff7a72018-09-03 09:33:36 +01002572 /*
2573 * shmemfs first checks if there is enough memory to allocate the page
Chris Wilson0820baf2014-03-25 13:23:03 +00002574 * and reports ENOSPC should there be insufficient, along with the usual
2575 * ENOMEM for a genuine allocation failure.
2576 *
2577 * We use ENOSPC in our driver to mean that we have run out of aperture
2578 * space and so want to translate the error from shmemfs back to our
2579 * usual understanding of ENOMEM.
2580 */
Imre Deake2273302015-07-09 12:59:05 +03002581 if (ret == -ENOSPC)
2582 ret = -ENOMEM;
2583
Matthew Auldb91b09e2017-10-06 23:18:17 +01002584 return ret;
Chris Wilson03ac84f2016-10-28 13:58:36 +01002585}
2586
2587void __i915_gem_object_set_pages(struct drm_i915_gem_object *obj,
Matthew Aulda5c081662017-10-06 23:18:18 +01002588 struct sg_table *pages,
Matthew Auld84e89782017-10-09 12:00:24 +01002589 unsigned int sg_page_sizes)
Chris Wilson03ac84f2016-10-28 13:58:36 +01002590{
Matthew Aulda5c081662017-10-06 23:18:18 +01002591 struct drm_i915_private *i915 = to_i915(obj->base.dev);
2592 unsigned long supported = INTEL_INFO(i915)->page_sizes;
2593 int i;
2594
Chris Wilson1233e2d2016-10-28 13:58:37 +01002595 lockdep_assert_held(&obj->mm.lock);
Chris Wilson03ac84f2016-10-28 13:58:36 +01002596
2597 obj->mm.get_page.sg_pos = pages->sgl;
2598 obj->mm.get_page.sg_idx = 0;
2599
2600 obj->mm.pages = pages;
Chris Wilson2c3a3f42016-11-04 10:30:01 +00002601
2602 if (i915_gem_object_is_tiled(obj) &&
Chris Wilsonf2123812017-10-16 12:40:37 +01002603 i915->quirks & QUIRK_PIN_SWIZZLED_PAGES) {
Chris Wilson2c3a3f42016-11-04 10:30:01 +00002604 GEM_BUG_ON(obj->mm.quirked);
2605 __i915_gem_object_pin_pages(obj);
2606 obj->mm.quirked = true;
2607 }
Matthew Aulda5c081662017-10-06 23:18:18 +01002608
Matthew Auld84e89782017-10-09 12:00:24 +01002609 GEM_BUG_ON(!sg_page_sizes);
2610 obj->mm.page_sizes.phys = sg_page_sizes;
Matthew Aulda5c081662017-10-06 23:18:18 +01002611
2612 /*
Matthew Auld84e89782017-10-09 12:00:24 +01002613 * Calculate the supported page-sizes which fit into the given
2614 * sg_page_sizes. This will give us the page-sizes which we may be able
2615 * to use opportunistically when later inserting into the GTT. For
2616 * example if phys=2G, then in theory we should be able to use 1G, 2M,
2617 * 64K or 4K pages, although in practice this will depend on a number of
2618 * other factors.
Matthew Aulda5c081662017-10-06 23:18:18 +01002619 */
2620 obj->mm.page_sizes.sg = 0;
2621 for_each_set_bit(i, &supported, ilog2(I915_GTT_MAX_PAGE_SIZE) + 1) {
2622 if (obj->mm.page_sizes.phys & ~0u << i)
2623 obj->mm.page_sizes.sg |= BIT(i);
2624 }
Matthew Aulda5c081662017-10-06 23:18:18 +01002625 GEM_BUG_ON(!HAS_PAGE_SIZES(i915, obj->mm.page_sizes.sg));
Chris Wilsonf2123812017-10-16 12:40:37 +01002626
2627 spin_lock(&i915->mm.obj_lock);
2628 list_add(&obj->mm.link, &i915->mm.unbound_list);
2629 spin_unlock(&i915->mm.obj_lock);
Chris Wilson03ac84f2016-10-28 13:58:36 +01002630}
2631
2632static int ____i915_gem_object_get_pages(struct drm_i915_gem_object *obj)
2633{
Matthew Auldb91b09e2017-10-06 23:18:17 +01002634 int err;
Chris Wilson03ac84f2016-10-28 13:58:36 +01002635
2636 if (unlikely(obj->mm.madv != I915_MADV_WILLNEED)) {
2637 DRM_DEBUG("Attempting to obtain a purgeable object\n");
2638 return -EFAULT;
2639 }
2640
Matthew Auldb91b09e2017-10-06 23:18:17 +01002641 err = obj->ops->get_pages(obj);
Matthew Auldb65a9b92017-12-18 10:38:55 +00002642 GEM_BUG_ON(!err && !i915_gem_object_has_pages(obj));
Chris Wilson03ac84f2016-10-28 13:58:36 +01002643
Matthew Auldb91b09e2017-10-06 23:18:17 +01002644 return err;
Eric Anholt673a3942008-07-30 12:06:12 -07002645}
2646
Chris Wilson37e680a2012-06-07 15:38:42 +01002647/* Ensure that the associated pages are gathered from the backing storage
Chris Wilson1233e2d2016-10-28 13:58:37 +01002648 * and pinned into our object. i915_gem_object_pin_pages() may be called
Chris Wilson37e680a2012-06-07 15:38:42 +01002649 * multiple times before they are released by a single call to
Chris Wilson1233e2d2016-10-28 13:58:37 +01002650 * i915_gem_object_unpin_pages() - once the pages are no longer referenced
Chris Wilson37e680a2012-06-07 15:38:42 +01002651 * either as a result of memory pressure (reaping pages under the shrinker)
2652 * or as the object is itself released.
2653 */
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002654int __i915_gem_object_get_pages(struct drm_i915_gem_object *obj)
Chris Wilson37e680a2012-06-07 15:38:42 +01002655{
Chris Wilson03ac84f2016-10-28 13:58:36 +01002656 int err;
Chris Wilson37e680a2012-06-07 15:38:42 +01002657
Chris Wilson1233e2d2016-10-28 13:58:37 +01002658 err = mutex_lock_interruptible(&obj->mm.lock);
2659 if (err)
2660 return err;
Chris Wilson4c7d62c2016-10-28 13:58:32 +01002661
Chris Wilsonf1fa4f42017-10-13 21:26:13 +01002662 if (unlikely(!i915_gem_object_has_pages(obj))) {
Chris Wilson88c880b2017-09-06 14:52:20 +01002663 GEM_BUG_ON(i915_gem_object_has_pinned_pages(obj));
2664
Chris Wilson2c3a3f42016-11-04 10:30:01 +00002665 err = ____i915_gem_object_get_pages(obj);
2666 if (err)
2667 goto unlock;
2668
2669 smp_mb__before_atomic();
Chris Wilson1233e2d2016-10-28 13:58:37 +01002670 }
Chris Wilson2c3a3f42016-11-04 10:30:01 +00002671 atomic_inc(&obj->mm.pages_pin_count);
Chris Wilson43e28f02013-01-08 10:53:09 +00002672
Chris Wilson1233e2d2016-10-28 13:58:37 +01002673unlock:
2674 mutex_unlock(&obj->mm.lock);
Chris Wilson03ac84f2016-10-28 13:58:36 +01002675 return err;
Eric Anholt673a3942008-07-30 12:06:12 -07002676}
2677
Dave Gordondd6034c2016-05-20 11:54:04 +01002678/* The 'mapping' part of i915_gem_object_pin_map() below */
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002679static void *i915_gem_object_map(const struct drm_i915_gem_object *obj,
2680 enum i915_map_type type)
Dave Gordondd6034c2016-05-20 11:54:04 +01002681{
2682 unsigned long n_pages = obj->base.size >> PAGE_SHIFT;
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002683 struct sg_table *sgt = obj->mm.pages;
Dave Gordon85d12252016-05-20 11:54:06 +01002684 struct sgt_iter sgt_iter;
2685 struct page *page;
Dave Gordonb338fa42016-05-20 11:54:05 +01002686 struct page *stack_pages[32];
2687 struct page **pages = stack_pages;
Dave Gordondd6034c2016-05-20 11:54:04 +01002688 unsigned long i = 0;
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002689 pgprot_t pgprot;
Dave Gordondd6034c2016-05-20 11:54:04 +01002690 void *addr;
2691
2692 /* A single page can always be kmapped */
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002693 if (n_pages == 1 && type == I915_MAP_WB)
Dave Gordondd6034c2016-05-20 11:54:04 +01002694 return kmap(sg_page(sgt->sgl));
2695
Dave Gordonb338fa42016-05-20 11:54:05 +01002696 if (n_pages > ARRAY_SIZE(stack_pages)) {
2697 /* Too big for stack -- allocate temporary array instead */
Michal Hocko0ee931c2017-09-13 16:28:29 -07002698 pages = kvmalloc_array(n_pages, sizeof(*pages), GFP_KERNEL);
Dave Gordonb338fa42016-05-20 11:54:05 +01002699 if (!pages)
2700 return NULL;
2701 }
Dave Gordondd6034c2016-05-20 11:54:04 +01002702
Dave Gordon85d12252016-05-20 11:54:06 +01002703 for_each_sgt_page(page, sgt_iter, sgt)
2704 pages[i++] = page;
Dave Gordondd6034c2016-05-20 11:54:04 +01002705
2706 /* Check that we have the expected number of pages */
2707 GEM_BUG_ON(i != n_pages);
2708
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002709 switch (type) {
Chris Wilsona575c672017-08-28 11:46:31 +01002710 default:
2711 MISSING_CASE(type);
2712 /* fallthrough to use PAGE_KERNEL anyway */
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002713 case I915_MAP_WB:
2714 pgprot = PAGE_KERNEL;
2715 break;
2716 case I915_MAP_WC:
2717 pgprot = pgprot_writecombine(PAGE_KERNEL_IO);
2718 break;
2719 }
2720 addr = vmap(pages, n_pages, 0, pgprot);
Dave Gordondd6034c2016-05-20 11:54:04 +01002721
Dave Gordonb338fa42016-05-20 11:54:05 +01002722 if (pages != stack_pages)
Michal Hocko20981052017-05-17 14:23:12 +02002723 kvfree(pages);
Dave Gordondd6034c2016-05-20 11:54:04 +01002724
2725 return addr;
2726}
2727
2728/* get, pin, and map the pages of the object into kernel space */
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002729void *i915_gem_object_pin_map(struct drm_i915_gem_object *obj,
2730 enum i915_map_type type)
Chris Wilson0a798eb2016-04-08 12:11:11 +01002731{
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002732 enum i915_map_type has_type;
2733 bool pinned;
2734 void *ptr;
Chris Wilson0a798eb2016-04-08 12:11:11 +01002735 int ret;
2736
Tina Zhanga03f3952017-11-14 10:25:13 +00002737 if (unlikely(!i915_gem_object_has_struct_page(obj)))
2738 return ERR_PTR(-ENXIO);
Chris Wilson0a798eb2016-04-08 12:11:11 +01002739
Chris Wilson1233e2d2016-10-28 13:58:37 +01002740 ret = mutex_lock_interruptible(&obj->mm.lock);
Chris Wilson0a798eb2016-04-08 12:11:11 +01002741 if (ret)
2742 return ERR_PTR(ret);
2743
Chris Wilsona575c672017-08-28 11:46:31 +01002744 pinned = !(type & I915_MAP_OVERRIDE);
2745 type &= ~I915_MAP_OVERRIDE;
2746
Chris Wilson1233e2d2016-10-28 13:58:37 +01002747 if (!atomic_inc_not_zero(&obj->mm.pages_pin_count)) {
Chris Wilsonf1fa4f42017-10-13 21:26:13 +01002748 if (unlikely(!i915_gem_object_has_pages(obj))) {
Chris Wilson88c880b2017-09-06 14:52:20 +01002749 GEM_BUG_ON(i915_gem_object_has_pinned_pages(obj));
2750
Chris Wilson2c3a3f42016-11-04 10:30:01 +00002751 ret = ____i915_gem_object_get_pages(obj);
2752 if (ret)
2753 goto err_unlock;
Chris Wilson1233e2d2016-10-28 13:58:37 +01002754
Chris Wilson2c3a3f42016-11-04 10:30:01 +00002755 smp_mb__before_atomic();
2756 }
2757 atomic_inc(&obj->mm.pages_pin_count);
Chris Wilson1233e2d2016-10-28 13:58:37 +01002758 pinned = false;
2759 }
Chris Wilsonf1fa4f42017-10-13 21:26:13 +01002760 GEM_BUG_ON(!i915_gem_object_has_pages(obj));
Chris Wilson0a798eb2016-04-08 12:11:11 +01002761
Chris Wilson0ce81782017-05-17 13:09:59 +01002762 ptr = page_unpack_bits(obj->mm.mapping, &has_type);
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002763 if (ptr && has_type != type) {
2764 if (pinned) {
2765 ret = -EBUSY;
Chris Wilson1233e2d2016-10-28 13:58:37 +01002766 goto err_unpin;
Chris Wilson0a798eb2016-04-08 12:11:11 +01002767 }
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002768
2769 if (is_vmalloc_addr(ptr))
2770 vunmap(ptr);
2771 else
2772 kunmap(kmap_to_page(ptr));
2773
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002774 ptr = obj->mm.mapping = NULL;
Chris Wilson0a798eb2016-04-08 12:11:11 +01002775 }
2776
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002777 if (!ptr) {
2778 ptr = i915_gem_object_map(obj, type);
2779 if (!ptr) {
2780 ret = -ENOMEM;
Chris Wilson1233e2d2016-10-28 13:58:37 +01002781 goto err_unpin;
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002782 }
2783
Chris Wilson0ce81782017-05-17 13:09:59 +01002784 obj->mm.mapping = page_pack_bits(ptr, type);
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002785 }
2786
Chris Wilson1233e2d2016-10-28 13:58:37 +01002787out_unlock:
2788 mutex_unlock(&obj->mm.lock);
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002789 return ptr;
2790
Chris Wilson1233e2d2016-10-28 13:58:37 +01002791err_unpin:
2792 atomic_dec(&obj->mm.pages_pin_count);
2793err_unlock:
2794 ptr = ERR_PTR(ret);
2795 goto out_unlock;
Chris Wilson0a798eb2016-04-08 12:11:11 +01002796}
2797
Chris Wilson7c55e2c2017-03-07 12:03:38 +00002798static int
2799i915_gem_object_pwrite_gtt(struct drm_i915_gem_object *obj,
2800 const struct drm_i915_gem_pwrite *arg)
2801{
2802 struct address_space *mapping = obj->base.filp->f_mapping;
2803 char __user *user_data = u64_to_user_ptr(arg->data_ptr);
2804 u64 remain, offset;
2805 unsigned int pg;
2806
2807 /* Before we instantiate/pin the backing store for our use, we
2808 * can prepopulate the shmemfs filp efficiently using a write into
2809 * the pagecache. We avoid the penalty of instantiating all the
2810 * pages, important if the user is just writing to a few and never
2811 * uses the object on the GPU, and using a direct write into shmemfs
2812 * allows it to avoid the cost of retrieving a page (either swapin
2813 * or clearing-before-use) before it is overwritten.
2814 */
Chris Wilsonf1fa4f42017-10-13 21:26:13 +01002815 if (i915_gem_object_has_pages(obj))
Chris Wilson7c55e2c2017-03-07 12:03:38 +00002816 return -ENODEV;
2817
Chris Wilsona6d65e42017-10-16 21:27:32 +01002818 if (obj->mm.madv != I915_MADV_WILLNEED)
2819 return -EFAULT;
2820
Chris Wilson7c55e2c2017-03-07 12:03:38 +00002821 /* Before the pages are instantiated the object is treated as being
2822 * in the CPU domain. The pages will be clflushed as required before
2823 * use, and we can freely write into the pages directly. If userspace
2824 * races pwrite with any other operation; corruption will ensue -
2825 * that is userspace's prerogative!
2826 */
2827
2828 remain = arg->size;
2829 offset = arg->offset;
2830 pg = offset_in_page(offset);
2831
2832 do {
2833 unsigned int len, unwritten;
2834 struct page *page;
2835 void *data, *vaddr;
2836 int err;
2837
2838 len = PAGE_SIZE - pg;
2839 if (len > remain)
2840 len = remain;
2841
2842 err = pagecache_write_begin(obj->base.filp, mapping,
2843 offset, len, 0,
2844 &page, &data);
2845 if (err < 0)
2846 return err;
2847
2848 vaddr = kmap(page);
2849 unwritten = copy_from_user(vaddr + pg, user_data, len);
2850 kunmap(page);
2851
2852 err = pagecache_write_end(obj->base.filp, mapping,
2853 offset, len, len - unwritten,
2854 page, data);
2855 if (err < 0)
2856 return err;
2857
2858 if (unwritten)
2859 return -EFAULT;
2860
2861 remain -= len;
2862 user_data += len;
2863 offset += len;
2864 pg = 0;
2865 } while (remain);
2866
2867 return 0;
2868}
2869
Chris Wilsone61e0f52018-02-21 09:56:36 +00002870struct i915_request *
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00002871i915_gem_find_active_request(struct intel_engine_cs *engine)
Chris Wilson9375e442010-09-19 12:21:28 +01002872{
Chris Wilsone61e0f52018-02-21 09:56:36 +00002873 struct i915_request *request, *active = NULL;
Chris Wilson754c9fd2017-02-23 07:44:14 +00002874 unsigned long flags;
Mika Kuoppalaaa60c662013-06-12 15:13:20 +03002875
Chris Wilsoncc7cc532018-05-29 14:29:18 +01002876 /*
2877 * We are called by the error capture, reset and to dump engine
2878 * state at random points in time. In particular, note that neither is
2879 * crucially ordered with an interrupt. After a hang, the GPU is dead
2880 * and we assume that no more writes can happen (we waited long enough
2881 * for all writes that were in transaction to be flushed) - adding an
Chris Wilsonf69a02c2016-07-01 17:23:16 +01002882 * extra delay for a recent interrupt is pointless. Hence, we do
2883 * not need an engine->irq_seqno_barrier() before the seqno reads.
Chris Wilsoncc7cc532018-05-29 14:29:18 +01002884 * At all other times, we must assume the GPU is still running, but
2885 * we only care about the snapshot of this moment.
Chris Wilsonf69a02c2016-07-01 17:23:16 +01002886 */
Chris Wilsona89d1f92018-05-02 17:38:39 +01002887 spin_lock_irqsave(&engine->timeline.lock, flags);
2888 list_for_each_entry(request, &engine->timeline.requests, link) {
Chris Wilsone61e0f52018-02-21 09:56:36 +00002889 if (__i915_request_completed(request, request->global_seqno))
Chris Wilson4db080f2013-12-04 11:37:09 +00002890 continue;
Mika Kuoppalaaa60c662013-06-12 15:13:20 +03002891
Chris Wilson754c9fd2017-02-23 07:44:14 +00002892 active = request;
2893 break;
2894 }
Chris Wilsona89d1f92018-05-02 17:38:39 +01002895 spin_unlock_irqrestore(&engine->timeline.lock, flags);
Chris Wilson754c9fd2017-02-23 07:44:14 +00002896
2897 return active;
Mika Kuoppalab6b0fac2014-01-30 19:04:43 +02002898}
2899
Daniel Vetter75ef9da2010-08-21 00:25:16 +02002900static void
Eric Anholt673a3942008-07-30 12:06:12 -07002901i915_gem_retire_work_handler(struct work_struct *work)
2902{
Chris Wilsonb29c19b2013-09-25 17:34:56 +01002903 struct drm_i915_private *dev_priv =
Chris Wilson67d97da2016-07-04 08:08:31 +01002904 container_of(work, typeof(*dev_priv), gt.retire_work.work);
Chris Wilson91c8a322016-07-05 10:40:23 +01002905 struct drm_device *dev = &dev_priv->drm;
Eric Anholt673a3942008-07-30 12:06:12 -07002906
Chris Wilson891b48c2010-09-29 12:26:37 +01002907 /* Come back later if the device is busy... */
Chris Wilsonb29c19b2013-09-25 17:34:56 +01002908 if (mutex_trylock(&dev->struct_mutex)) {
Chris Wilsone61e0f52018-02-21 09:56:36 +00002909 i915_retire_requests(dev_priv);
Chris Wilsonb29c19b2013-09-25 17:34:56 +01002910 mutex_unlock(&dev->struct_mutex);
2911 }
Chris Wilson67d97da2016-07-04 08:08:31 +01002912
Chris Wilson88923042018-01-29 14:41:04 +00002913 /*
2914 * Keep the retire handler running until we are finally idle.
Chris Wilson67d97da2016-07-04 08:08:31 +01002915 * We do not need to do this test under locking as in the worst-case
2916 * we queue the retire worker once too often.
2917 */
Chris Wilson88923042018-01-29 14:41:04 +00002918 if (READ_ONCE(dev_priv->gt.awake))
Chris Wilson67d97da2016-07-04 08:08:31 +01002919 queue_delayed_work(dev_priv->wq,
2920 &dev_priv->gt.retire_work,
Chris Wilsonbcb45082012-10-05 17:02:57 +01002921 round_jiffies_up_relative(HZ));
Chris Wilsonb29c19b2013-09-25 17:34:56 +01002922}
Chris Wilson891b48c2010-09-29 12:26:37 +01002923
Chris Wilson84a10742018-01-24 11:36:08 +00002924static void shrink_caches(struct drm_i915_private *i915)
2925{
2926 /*
2927 * kmem_cache_shrink() discards empty slabs and reorders partially
2928 * filled slabs to prioritise allocating from the mostly full slabs,
2929 * with the aim of reducing fragmentation.
2930 */
2931 kmem_cache_shrink(i915->priorities);
2932 kmem_cache_shrink(i915->dependencies);
2933 kmem_cache_shrink(i915->requests);
2934 kmem_cache_shrink(i915->luts);
2935 kmem_cache_shrink(i915->vmas);
2936 kmem_cache_shrink(i915->objects);
2937}
2938
2939struct sleep_rcu_work {
2940 union {
2941 struct rcu_head rcu;
2942 struct work_struct work;
2943 };
2944 struct drm_i915_private *i915;
2945 unsigned int epoch;
2946};
2947
2948static inline bool
2949same_epoch(struct drm_i915_private *i915, unsigned int epoch)
2950{
2951 /*
2952 * There is a small chance that the epoch wrapped since we started
2953 * sleeping. If we assume that epoch is at least a u32, then it will
2954 * take at least 2^32 * 100ms for it to wrap, or about 326 years.
2955 */
2956 return epoch == READ_ONCE(i915->gt.epoch);
2957}
2958
2959static void __sleep_work(struct work_struct *work)
2960{
2961 struct sleep_rcu_work *s = container_of(work, typeof(*s), work);
2962 struct drm_i915_private *i915 = s->i915;
2963 unsigned int epoch = s->epoch;
2964
2965 kfree(s);
2966 if (same_epoch(i915, epoch))
2967 shrink_caches(i915);
2968}
2969
2970static void __sleep_rcu(struct rcu_head *rcu)
2971{
2972 struct sleep_rcu_work *s = container_of(rcu, typeof(*s), rcu);
2973 struct drm_i915_private *i915 = s->i915;
2974
Chris Wilsona1db9c52018-11-08 09:21:01 +00002975 destroy_rcu_head(&s->rcu);
2976
Chris Wilson84a10742018-01-24 11:36:08 +00002977 if (same_epoch(i915, s->epoch)) {
2978 INIT_WORK(&s->work, __sleep_work);
2979 queue_work(i915->wq, &s->work);
2980 } else {
2981 kfree(s);
2982 }
2983}
2984
Chris Wilson5427f202017-10-23 22:32:34 +01002985static inline bool
2986new_requests_since_last_retire(const struct drm_i915_private *i915)
2987{
2988 return (READ_ONCE(i915->gt.active_requests) ||
2989 work_pending(&i915->gt.idle_work.work));
2990}
2991
Chris Wilson1934f5de2018-05-31 23:40:57 +01002992static void assert_kernel_context_is_current(struct drm_i915_private *i915)
2993{
2994 struct intel_engine_cs *engine;
2995 enum intel_engine_id id;
2996
2997 if (i915_terminally_wedged(&i915->gpu_error))
2998 return;
2999
3000 GEM_BUG_ON(i915->gt.active_requests);
3001 for_each_engine(engine, i915, id) {
3002 GEM_BUG_ON(__i915_gem_active_peek(&engine->timeline.last_request));
3003 GEM_BUG_ON(engine->last_retired_context !=
3004 to_intel_context(i915->kernel_context, engine));
3005 }
3006}
3007
Chris Wilsonb29c19b2013-09-25 17:34:56 +01003008static void
3009i915_gem_idle_work_handler(struct work_struct *work)
3010{
3011 struct drm_i915_private *dev_priv =
Chris Wilson67d97da2016-07-04 08:08:31 +01003012 container_of(work, typeof(*dev_priv), gt.idle_work.work);
Chris Wilson84a10742018-01-24 11:36:08 +00003013 unsigned int epoch = I915_EPOCH_INVALID;
Chris Wilson67d97da2016-07-04 08:08:31 +01003014 bool rearm_hangcheck;
3015
3016 if (!READ_ONCE(dev_priv->gt.awake))
3017 return;
3018
Chris Wilson4dfacb02018-05-31 09:22:43 +01003019 if (READ_ONCE(dev_priv->gt.active_requests))
3020 return;
3021
3022 /*
3023 * Flush out the last user context, leaving only the pinned
3024 * kernel context resident. When we are idling on the kernel_context,
3025 * no more new requests (with a context switch) are emitted and we
3026 * can finally rest. A consequence is that the idle work handler is
3027 * always called at least twice before idling (and if the system is
3028 * idle that implies a round trip through the retire worker).
3029 */
3030 mutex_lock(&dev_priv->drm.struct_mutex);
3031 i915_gem_switch_to_kernel_context(dev_priv);
3032 mutex_unlock(&dev_priv->drm.struct_mutex);
3033
3034 GEM_TRACE("active_requests=%d (after switch-to-kernel-context)\n",
3035 READ_ONCE(dev_priv->gt.active_requests));
3036
Imre Deak0cb56702016-11-07 11:20:04 +02003037 /*
3038 * Wait for last execlists context complete, but bail out in case a
Chris Wilsonffed7bd2018-03-01 10:33:38 +00003039 * new request is submitted. As we don't trust the hardware, we
3040 * continue on if the wait times out. This is necessary to allow
3041 * the machine to suspend even if the hardware dies, and we will
3042 * try to recover in resume (after depriving the hardware of power,
3043 * it may be in a better mmod).
Imre Deak0cb56702016-11-07 11:20:04 +02003044 */
Chris Wilsonffed7bd2018-03-01 10:33:38 +00003045 __wait_for(if (new_requests_since_last_retire(dev_priv)) return,
3046 intel_engines_are_idle(dev_priv),
3047 I915_IDLE_ENGINES_TIMEOUT * 1000,
3048 10, 500);
Chris Wilson67d97da2016-07-04 08:08:31 +01003049
3050 rearm_hangcheck =
3051 cancel_delayed_work_sync(&dev_priv->gpu_error.hangcheck_work);
3052
Chris Wilson5427f202017-10-23 22:32:34 +01003053 if (!mutex_trylock(&dev_priv->drm.struct_mutex)) {
Chris Wilson67d97da2016-07-04 08:08:31 +01003054 /* Currently busy, come back later */
3055 mod_delayed_work(dev_priv->wq,
3056 &dev_priv->gt.idle_work,
3057 msecs_to_jiffies(50));
3058 goto out_rearm;
3059 }
3060
Imre Deak93c97dc2016-11-07 11:20:03 +02003061 /*
3062 * New request retired after this work handler started, extend active
3063 * period until next instance of the work.
3064 */
Chris Wilson5427f202017-10-23 22:32:34 +01003065 if (new_requests_since_last_retire(dev_priv))
Imre Deak93c97dc2016-11-07 11:20:03 +02003066 goto out_unlock;
3067
Chris Wilsone4d20062018-04-06 16:51:44 +01003068 epoch = __i915_gem_park(dev_priv);
Chris Wilsonff320d62017-10-23 22:32:35 +01003069
Chris Wilson1934f5de2018-05-31 23:40:57 +01003070 assert_kernel_context_is_current(dev_priv);
3071
Chris Wilson67d97da2016-07-04 08:08:31 +01003072 rearm_hangcheck = false;
Chris Wilson67d97da2016-07-04 08:08:31 +01003073out_unlock:
Chris Wilson5427f202017-10-23 22:32:34 +01003074 mutex_unlock(&dev_priv->drm.struct_mutex);
Chris Wilson35c94182015-04-07 16:20:37 +01003075
Chris Wilson67d97da2016-07-04 08:08:31 +01003076out_rearm:
3077 if (rearm_hangcheck) {
3078 GEM_BUG_ON(!dev_priv->gt.awake);
3079 i915_queue_hangcheck(dev_priv);
Chris Wilson35c94182015-04-07 16:20:37 +01003080 }
Chris Wilson84a10742018-01-24 11:36:08 +00003081
3082 /*
3083 * When we are idle, it is an opportune time to reap our caches.
3084 * However, we have many objects that utilise RCU and the ordered
3085 * i915->wq that this work is executing on. To try and flush any
3086 * pending frees now we are idle, we first wait for an RCU grace
3087 * period, and then queue a task (that will run last on the wq) to
3088 * shrink and re-optimize the caches.
3089 */
3090 if (same_epoch(dev_priv, epoch)) {
3091 struct sleep_rcu_work *s = kmalloc(sizeof(*s), GFP_KERNEL);
3092 if (s) {
Chris Wilsona1db9c52018-11-08 09:21:01 +00003093 init_rcu_head(&s->rcu);
Chris Wilson84a10742018-01-24 11:36:08 +00003094 s->i915 = dev_priv;
3095 s->epoch = epoch;
3096 call_rcu(&s->rcu, __sleep_rcu);
3097 }
3098 }
Eric Anholt673a3942008-07-30 12:06:12 -07003099}
3100
Chris Wilsonb1f788c2016-08-04 07:52:45 +01003101void i915_gem_close_object(struct drm_gem_object *gem, struct drm_file *file)
3102{
Chris Wilsond1b48c12017-08-16 09:52:08 +01003103 struct drm_i915_private *i915 = to_i915(gem->dev);
Chris Wilsonb1f788c2016-08-04 07:52:45 +01003104 struct drm_i915_gem_object *obj = to_intel_bo(gem);
3105 struct drm_i915_file_private *fpriv = file->driver_priv;
Chris Wilsond1b48c12017-08-16 09:52:08 +01003106 struct i915_lut_handle *lut, *ln;
Chris Wilsonb1f788c2016-08-04 07:52:45 +01003107
Chris Wilsond1b48c12017-08-16 09:52:08 +01003108 mutex_lock(&i915->drm.struct_mutex);
3109
3110 list_for_each_entry_safe(lut, ln, &obj->lut_list, obj_link) {
3111 struct i915_gem_context *ctx = lut->ctx;
3112 struct i915_vma *vma;
3113
Chris Wilson432295d2017-08-22 12:05:15 +01003114 GEM_BUG_ON(ctx->file_priv == ERR_PTR(-EBADF));
Chris Wilsond1b48c12017-08-16 09:52:08 +01003115 if (ctx->file_priv != fpriv)
3116 continue;
3117
3118 vma = radix_tree_delete(&ctx->handles_vma, lut->handle);
Chris Wilson3ffff012017-08-22 12:05:17 +01003119 GEM_BUG_ON(vma->obj != obj);
3120
3121 /* We allow the process to have multiple handles to the same
3122 * vma, in the same fd namespace, by virtue of flink/open.
3123 */
3124 GEM_BUG_ON(!vma->open_count);
3125 if (!--vma->open_count && !i915_vma_is_ggtt(vma))
Chris Wilsonb1f788c2016-08-04 07:52:45 +01003126 i915_vma_close(vma);
Chris Wilsonf8a7fde2016-10-28 13:58:29 +01003127
Chris Wilsond1b48c12017-08-16 09:52:08 +01003128 list_del(&lut->obj_link);
3129 list_del(&lut->ctx_link);
Chris Wilson4ff4b442017-06-16 15:05:16 +01003130
Chris Wilsond1b48c12017-08-16 09:52:08 +01003131 kmem_cache_free(i915->luts, lut);
3132 __i915_gem_object_release_unless_active(obj);
Chris Wilsonf8a7fde2016-10-28 13:58:29 +01003133 }
Chris Wilsond1b48c12017-08-16 09:52:08 +01003134
3135 mutex_unlock(&i915->drm.struct_mutex);
Chris Wilsonb1f788c2016-08-04 07:52:45 +01003136}
3137
Chris Wilsone95433c2016-10-28 13:58:27 +01003138static unsigned long to_wait_timeout(s64 timeout_ns)
3139{
3140 if (timeout_ns < 0)
3141 return MAX_SCHEDULE_TIMEOUT;
3142
3143 if (timeout_ns == 0)
3144 return 0;
3145
3146 return nsecs_to_jiffies_timeout(timeout_ns);
3147}
3148
Ben Widawsky5816d642012-04-11 11:18:19 -07003149/**
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07003150 * i915_gem_wait_ioctl - implements DRM_IOCTL_I915_GEM_WAIT
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01003151 * @dev: drm device pointer
3152 * @data: ioctl data blob
3153 * @file: drm file pointer
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07003154 *
3155 * Returns 0 if successful, else an error is returned with the remaining time in
3156 * the timeout parameter.
3157 * -ETIME: object is still busy after timeout
3158 * -ERESTARTSYS: signal interrupted the wait
3159 * -ENONENT: object doesn't exist
3160 * Also possible, but rare:
Chris Wilsonb8050142017-08-11 11:57:31 +01003161 * -EAGAIN: incomplete, restart syscall
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07003162 * -ENOMEM: damn
3163 * -ENODEV: Internal IRQ fail
3164 * -E?: The add request failed
3165 *
3166 * The wait ioctl with a timeout of 0 reimplements the busy ioctl. With any
3167 * non-zero timeout parameter the wait ioctl will wait for the given number of
3168 * nanoseconds on an object becoming unbusy. Since the wait itself does so
3169 * without holding struct_mutex the object may become re-busied before this
3170 * function completes. A similar but shorter * race condition exists in the busy
3171 * ioctl
3172 */
3173int
3174i915_gem_wait_ioctl(struct drm_device *dev, void *data, struct drm_file *file)
3175{
3176 struct drm_i915_gem_wait *args = data;
3177 struct drm_i915_gem_object *obj;
Chris Wilsone95433c2016-10-28 13:58:27 +01003178 ktime_t start;
3179 long ret;
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07003180
Daniel Vetter11b5d512014-09-29 15:31:26 +02003181 if (args->flags != 0)
3182 return -EINVAL;
3183
Chris Wilson03ac0642016-07-20 13:31:51 +01003184 obj = i915_gem_object_lookup(file, args->bo_handle);
Chris Wilson033d5492016-08-05 10:14:17 +01003185 if (!obj)
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07003186 return -ENOENT;
Chris Wilson033d5492016-08-05 10:14:17 +01003187
Chris Wilsone95433c2016-10-28 13:58:27 +01003188 start = ktime_get();
3189
3190 ret = i915_gem_object_wait(obj,
Chris Wilsone9eaf822018-10-01 15:47:55 +01003191 I915_WAIT_INTERRUPTIBLE |
3192 I915_WAIT_PRIORITY |
3193 I915_WAIT_ALL,
Chris Wilsone95433c2016-10-28 13:58:27 +01003194 to_wait_timeout(args->timeout_ns),
3195 to_rps_client(file));
3196
3197 if (args->timeout_ns > 0) {
3198 args->timeout_ns -= ktime_to_ns(ktime_sub(ktime_get(), start));
3199 if (args->timeout_ns < 0)
3200 args->timeout_ns = 0;
Chris Wilsonc1d20612017-02-16 12:54:41 +00003201
3202 /*
3203 * Apparently ktime isn't accurate enough and occasionally has a
3204 * bit of mismatch in the jiffies<->nsecs<->ktime loop. So patch
3205 * things up to make the test happy. We allow up to 1 jiffy.
3206 *
3207 * This is a regression from the timespec->ktime conversion.
3208 */
3209 if (ret == -ETIME && !nsecs_to_jiffies(args->timeout_ns))
3210 args->timeout_ns = 0;
Chris Wilsonb8050142017-08-11 11:57:31 +01003211
3212 /* Asked to wait beyond the jiffie/scheduler precision? */
3213 if (ret == -ETIME && args->timeout_ns)
3214 ret = -EAGAIN;
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07003215 }
3216
Chris Wilsonf0cd5182016-10-28 13:58:43 +01003217 i915_gem_object_put(obj);
John Harrisonff865882014-11-24 18:49:28 +00003218 return ret;
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07003219}
3220
Chris Wilsonec625fb2018-07-09 13:20:42 +01003221static long wait_for_timeline(struct i915_timeline *tl,
3222 unsigned int flags, long timeout)
Daniel Vetter4df2faf2010-02-19 11:52:00 +01003223{
Chris Wilson06060352018-05-31 09:22:44 +01003224 struct i915_request *rq;
Chris Wilson06060352018-05-31 09:22:44 +01003225
3226 rq = i915_gem_active_get_unlocked(&tl->last_request);
3227 if (!rq)
Chris Wilsonec625fb2018-07-09 13:20:42 +01003228 return timeout;
Chris Wilson06060352018-05-31 09:22:44 +01003229
3230 /*
3231 * "Race-to-idle".
3232 *
3233 * Switching to the kernel context is often used a synchronous
3234 * step prior to idling, e.g. in suspend for flushing all
3235 * current operations to memory before sleeping. These we
3236 * want to complete as quickly as possible to avoid prolonged
3237 * stalls, so allow the gpu to boost to maximum clocks.
3238 */
3239 if (flags & I915_WAIT_FOR_IDLE_BOOST)
3240 gen6_rps_boost(rq, NULL);
3241
Chris Wilsonec625fb2018-07-09 13:20:42 +01003242 timeout = i915_request_wait(rq, flags, timeout);
Chris Wilson06060352018-05-31 09:22:44 +01003243 i915_request_put(rq);
3244
Chris Wilsonec625fb2018-07-09 13:20:42 +01003245 return timeout;
Chris Wilson73cb9702016-10-28 13:58:46 +01003246}
3247
Chris Wilson25112b62017-03-30 15:50:39 +01003248static int wait_for_engines(struct drm_i915_private *i915)
3249{
Chris Wilsonee42c002017-12-11 19:41:34 +00003250 if (wait_for(intel_engines_are_idle(i915), I915_IDLE_ENGINES_TIMEOUT)) {
Chris Wilson59e4b192017-12-11 19:41:35 +00003251 dev_err(i915->drm.dev,
3252 "Failed to idle engines, declaring wedged!\n");
Chris Wilson629820f2018-03-09 10:11:14 +00003253 GEM_TRACE_DUMP();
Chris Wilsoncad99462017-08-26 12:09:33 +01003254 i915_gem_set_wedged(i915);
3255 return -EIO;
Chris Wilson25112b62017-03-30 15:50:39 +01003256 }
3257
3258 return 0;
3259}
3260
Chris Wilsonec625fb2018-07-09 13:20:42 +01003261int i915_gem_wait_for_idle(struct drm_i915_private *i915,
3262 unsigned int flags, long timeout)
Chris Wilson73cb9702016-10-28 13:58:46 +01003263{
Chris Wilsonec625fb2018-07-09 13:20:42 +01003264 GEM_TRACE("flags=%x (%s), timeout=%ld%s\n",
3265 flags, flags & I915_WAIT_LOCKED ? "locked" : "unlocked",
3266 timeout, timeout == MAX_SCHEDULE_TIMEOUT ? " (forever)" : "");
Chris Wilson09a4c022018-05-24 09:11:35 +01003267
Chris Wilson863e9fd2017-05-30 13:13:32 +01003268 /* If the device is asleep, we have no requests outstanding */
3269 if (!READ_ONCE(i915->gt.awake))
3270 return 0;
3271
Chris Wilson9caa34a2016-11-11 14:58:08 +00003272 if (flags & I915_WAIT_LOCKED) {
Chris Wilsona89d1f92018-05-02 17:38:39 +01003273 struct i915_timeline *tl;
3274 int err;
Chris Wilson9caa34a2016-11-11 14:58:08 +00003275
3276 lockdep_assert_held(&i915->drm.struct_mutex);
3277
3278 list_for_each_entry(tl, &i915->gt.timelines, link) {
Chris Wilsonec625fb2018-07-09 13:20:42 +01003279 timeout = wait_for_timeline(tl, flags, timeout);
3280 if (timeout < 0)
3281 return timeout;
Chris Wilson9caa34a2016-11-11 14:58:08 +00003282 }
Chris Wilsonc1e63f62018-08-08 11:50:59 +01003283 if (GEM_SHOW_DEBUG() && !timeout) {
3284 /* Presume that timeout was non-zero to begin with! */
3285 dev_warn(&i915->drm.pdev->dev,
3286 "Missed idle-completion interrupt!\n");
3287 GEM_TRACE_DUMP();
3288 }
Chris Wilsona61b47f2018-06-27 12:53:34 +01003289
3290 err = wait_for_engines(i915);
3291 if (err)
3292 return err;
3293
Chris Wilsone61e0f52018-02-21 09:56:36 +00003294 i915_retire_requests(i915);
Chris Wilson09a4c022018-05-24 09:11:35 +01003295 GEM_BUG_ON(i915->gt.active_requests);
Chris Wilson9caa34a2016-11-11 14:58:08 +00003296 } else {
Chris Wilsona89d1f92018-05-02 17:38:39 +01003297 struct intel_engine_cs *engine;
3298 enum intel_engine_id id;
Zou Nan haid1b851f2010-05-21 09:08:57 +08003299
Chris Wilsona89d1f92018-05-02 17:38:39 +01003300 for_each_engine(engine, i915, id) {
Chris Wilsonec625fb2018-07-09 13:20:42 +01003301 struct i915_timeline *tl = &engine->timeline;
3302
3303 timeout = wait_for_timeline(tl, flags, timeout);
3304 if (timeout < 0)
3305 return timeout;
Chris Wilsona89d1f92018-05-02 17:38:39 +01003306 }
Chris Wilsona89d1f92018-05-02 17:38:39 +01003307 }
Chris Wilsona61b47f2018-06-27 12:53:34 +01003308
3309 return 0;
Daniel Vetter4df2faf2010-02-19 11:52:00 +01003310}
3311
Chris Wilson5a97bcc2017-02-22 11:40:46 +00003312static void __i915_gem_object_flush_for_display(struct drm_i915_gem_object *obj)
3313{
Chris Wilsone27ab732017-06-15 13:38:49 +01003314 /*
3315 * We manually flush the CPU domain so that we can override and
3316 * force the flush for the display, and perform it asyncrhonously.
3317 */
3318 flush_write_domain(obj, ~I915_GEM_DOMAIN_CPU);
3319 if (obj->cache_dirty)
3320 i915_gem_clflush_object(obj, I915_CLFLUSH_FORCE);
Christian Königc0a51fd2018-02-16 13:43:38 +01003321 obj->write_domain = 0;
Chris Wilson5a97bcc2017-02-22 11:40:46 +00003322}
3323
3324void i915_gem_object_flush_if_display(struct drm_i915_gem_object *obj)
3325{
Chris Wilsonbd3d2252017-10-13 21:26:14 +01003326 if (!READ_ONCE(obj->pin_global))
Chris Wilson5a97bcc2017-02-22 11:40:46 +00003327 return;
3328
3329 mutex_lock(&obj->base.dev->struct_mutex);
3330 __i915_gem_object_flush_for_display(obj);
3331 mutex_unlock(&obj->base.dev->struct_mutex);
3332}
3333
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003334/**
Chris Wilsone22d8e32017-04-12 12:01:11 +01003335 * Moves a single object to the WC read, and possibly write domain.
3336 * @obj: object to act on
3337 * @write: ask for write access or read only
3338 *
3339 * This function returns when the move is complete, including waiting on
3340 * flushes to occur.
3341 */
3342int
3343i915_gem_object_set_to_wc_domain(struct drm_i915_gem_object *obj, bool write)
3344{
3345 int ret;
3346
3347 lockdep_assert_held(&obj->base.dev->struct_mutex);
3348
3349 ret = i915_gem_object_wait(obj,
3350 I915_WAIT_INTERRUPTIBLE |
3351 I915_WAIT_LOCKED |
3352 (write ? I915_WAIT_ALL : 0),
3353 MAX_SCHEDULE_TIMEOUT,
3354 NULL);
3355 if (ret)
3356 return ret;
3357
Christian Königc0a51fd2018-02-16 13:43:38 +01003358 if (obj->write_domain == I915_GEM_DOMAIN_WC)
Chris Wilsone22d8e32017-04-12 12:01:11 +01003359 return 0;
3360
3361 /* Flush and acquire obj->pages so that we are coherent through
3362 * direct access in memory with previous cached writes through
3363 * shmemfs and that our cache domain tracking remains valid.
3364 * For example, if the obj->filp was moved to swap without us
3365 * being notified and releasing the pages, we would mistakenly
3366 * continue to assume that the obj remained out of the CPU cached
3367 * domain.
3368 */
3369 ret = i915_gem_object_pin_pages(obj);
3370 if (ret)
3371 return ret;
3372
3373 flush_write_domain(obj, ~I915_GEM_DOMAIN_WC);
3374
3375 /* Serialise direct access to this object with the barriers for
3376 * coherent writes from the GPU, by effectively invalidating the
3377 * WC domain upon first access.
3378 */
Christian Königc0a51fd2018-02-16 13:43:38 +01003379 if ((obj->read_domains & I915_GEM_DOMAIN_WC) == 0)
Chris Wilsone22d8e32017-04-12 12:01:11 +01003380 mb();
3381
3382 /* It should now be out of any other write domains, and we can update
3383 * the domain values for our changes.
3384 */
Christian Königc0a51fd2018-02-16 13:43:38 +01003385 GEM_BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_WC) != 0);
3386 obj->read_domains |= I915_GEM_DOMAIN_WC;
Chris Wilsone22d8e32017-04-12 12:01:11 +01003387 if (write) {
Christian Königc0a51fd2018-02-16 13:43:38 +01003388 obj->read_domains = I915_GEM_DOMAIN_WC;
3389 obj->write_domain = I915_GEM_DOMAIN_WC;
Chris Wilsone22d8e32017-04-12 12:01:11 +01003390 obj->mm.dirty = true;
3391 }
3392
3393 i915_gem_object_unpin_pages(obj);
3394 return 0;
3395}
3396
3397/**
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003398 * Moves a single object to the GTT read, and possibly write domain.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01003399 * @obj: object to act on
3400 * @write: ask for write access or read only
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003401 *
3402 * This function returns when the move is complete, including waiting on
3403 * flushes to occur.
3404 */
Jesse Barnes79e53942008-11-07 14:24:08 -08003405int
Chris Wilson20217462010-11-23 15:26:33 +00003406i915_gem_object_set_to_gtt_domain(struct drm_i915_gem_object *obj, bool write)
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003407{
Eric Anholte47c68e2008-11-14 13:35:19 -08003408 int ret;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003409
Chris Wilsone95433c2016-10-28 13:58:27 +01003410 lockdep_assert_held(&obj->base.dev->struct_mutex);
Chris Wilson4c7d62c2016-10-28 13:58:32 +01003411
Chris Wilsone95433c2016-10-28 13:58:27 +01003412 ret = i915_gem_object_wait(obj,
3413 I915_WAIT_INTERRUPTIBLE |
3414 I915_WAIT_LOCKED |
3415 (write ? I915_WAIT_ALL : 0),
3416 MAX_SCHEDULE_TIMEOUT,
3417 NULL);
Chris Wilson88241782011-01-07 17:09:48 +00003418 if (ret)
3419 return ret;
3420
Christian Königc0a51fd2018-02-16 13:43:38 +01003421 if (obj->write_domain == I915_GEM_DOMAIN_GTT)
Chris Wilsonc13d87e2016-07-20 09:21:15 +01003422 return 0;
3423
Chris Wilson43566de2015-01-02 16:29:29 +05303424 /* Flush and acquire obj->pages so that we are coherent through
3425 * direct access in memory with previous cached writes through
3426 * shmemfs and that our cache domain tracking remains valid.
3427 * For example, if the obj->filp was moved to swap without us
3428 * being notified and releasing the pages, we would mistakenly
3429 * continue to assume that the obj remained out of the CPU cached
3430 * domain.
3431 */
Chris Wilsona4f5ea62016-10-28 13:58:35 +01003432 ret = i915_gem_object_pin_pages(obj);
Chris Wilson43566de2015-01-02 16:29:29 +05303433 if (ret)
3434 return ret;
3435
Chris Wilsonef749212017-04-12 12:01:10 +01003436 flush_write_domain(obj, ~I915_GEM_DOMAIN_GTT);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003437
Chris Wilsond0a57782012-10-09 19:24:37 +01003438 /* Serialise direct access to this object with the barriers for
3439 * coherent writes from the GPU, by effectively invalidating the
3440 * GTT domain upon first access.
3441 */
Christian Königc0a51fd2018-02-16 13:43:38 +01003442 if ((obj->read_domains & I915_GEM_DOMAIN_GTT) == 0)
Chris Wilsond0a57782012-10-09 19:24:37 +01003443 mb();
3444
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003445 /* It should now be out of any other write domains, and we can update
3446 * the domain values for our changes.
3447 */
Christian Königc0a51fd2018-02-16 13:43:38 +01003448 GEM_BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_GTT) != 0);
3449 obj->read_domains |= I915_GEM_DOMAIN_GTT;
Eric Anholte47c68e2008-11-14 13:35:19 -08003450 if (write) {
Christian Königc0a51fd2018-02-16 13:43:38 +01003451 obj->read_domains = I915_GEM_DOMAIN_GTT;
3452 obj->write_domain = I915_GEM_DOMAIN_GTT;
Chris Wilsona4f5ea62016-10-28 13:58:35 +01003453 obj->mm.dirty = true;
Eric Anholte47c68e2008-11-14 13:35:19 -08003454 }
3455
Chris Wilsona4f5ea62016-10-28 13:58:35 +01003456 i915_gem_object_unpin_pages(obj);
Eric Anholte47c68e2008-11-14 13:35:19 -08003457 return 0;
3458}
3459
Chris Wilsonef55f922015-10-09 14:11:27 +01003460/**
3461 * Changes the cache-level of an object across all VMA.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01003462 * @obj: object to act on
3463 * @cache_level: new cache level to set for the object
Chris Wilsonef55f922015-10-09 14:11:27 +01003464 *
3465 * After this function returns, the object will be in the new cache-level
3466 * across all GTT and the contents of the backing storage will be coherent,
3467 * with respect to the new cache-level. In order to keep the backing storage
3468 * coherent for all users, we only allow a single cache level to be set
3469 * globally on the object and prevent it from being changed whilst the
3470 * hardware is reading from the object. That is if the object is currently
3471 * on the scanout it will be set to uncached (or equivalent display
3472 * cache coherency) and all non-MOCS GPU access will also be uncached so
3473 * that all direct access to the scanout remains coherent.
3474 */
Chris Wilsone4ffd172011-04-04 09:44:39 +01003475int i915_gem_object_set_cache_level(struct drm_i915_gem_object *obj,
3476 enum i915_cache_level cache_level)
3477{
Chris Wilsonaa653a62016-08-04 07:52:27 +01003478 struct i915_vma *vma;
Chris Wilsona6a7cc42016-11-18 21:17:46 +00003479 int ret;
Chris Wilsone4ffd172011-04-04 09:44:39 +01003480
Chris Wilson4c7d62c2016-10-28 13:58:32 +01003481 lockdep_assert_held(&obj->base.dev->struct_mutex);
3482
Chris Wilsone4ffd172011-04-04 09:44:39 +01003483 if (obj->cache_level == cache_level)
Chris Wilsona6a7cc42016-11-18 21:17:46 +00003484 return 0;
Chris Wilsone4ffd172011-04-04 09:44:39 +01003485
Chris Wilsonef55f922015-10-09 14:11:27 +01003486 /* Inspect the list of currently bound VMA and unbind any that would
3487 * be invalid given the new cache-level. This is principally to
3488 * catch the issue of the CS prefetch crossing page boundaries and
3489 * reading an invalid PTE on older architectures.
3490 */
Chris Wilsonaa653a62016-08-04 07:52:27 +01003491restart:
3492 list_for_each_entry(vma, &obj->vma_list, obj_link) {
Chris Wilsonef55f922015-10-09 14:11:27 +01003493 if (!drm_mm_node_allocated(&vma->node))
3494 continue;
3495
Chris Wilson20dfbde2016-08-04 16:32:30 +01003496 if (i915_vma_is_pinned(vma)) {
Chris Wilsonef55f922015-10-09 14:11:27 +01003497 DRM_DEBUG("can not change the cache level of pinned objects\n");
3498 return -EBUSY;
3499 }
3500
Chris Wilson010e3e62017-12-06 12:49:13 +00003501 if (!i915_vma_is_closed(vma) &&
3502 i915_gem_valid_gtt_space(vma, cache_level))
Chris Wilsonaa653a62016-08-04 07:52:27 +01003503 continue;
3504
3505 ret = i915_vma_unbind(vma);
3506 if (ret)
3507 return ret;
3508
3509 /* As unbinding may affect other elements in the
3510 * obj->vma_list (due to side-effects from retiring
3511 * an active vma), play safe and restart the iterator.
3512 */
3513 goto restart;
Chris Wilson42d6ab42012-07-26 11:49:32 +01003514 }
3515
Chris Wilsonef55f922015-10-09 14:11:27 +01003516 /* We can reuse the existing drm_mm nodes but need to change the
3517 * cache-level on the PTE. We could simply unbind them all and
3518 * rebind with the correct cache-level on next use. However since
3519 * we already have a valid slot, dma mapping, pages etc, we may as
3520 * rewrite the PTE in the belief that doing so tramples upon less
3521 * state and so involves less work.
3522 */
Chris Wilson15717de2016-08-04 07:52:26 +01003523 if (obj->bind_count) {
Chris Wilsonef55f922015-10-09 14:11:27 +01003524 /* Before we change the PTE, the GPU must not be accessing it.
3525 * If we wait upon the object, we know that all the bound
3526 * VMA are no longer active.
3527 */
Chris Wilsone95433c2016-10-28 13:58:27 +01003528 ret = i915_gem_object_wait(obj,
3529 I915_WAIT_INTERRUPTIBLE |
3530 I915_WAIT_LOCKED |
3531 I915_WAIT_ALL,
3532 MAX_SCHEDULE_TIMEOUT,
3533 NULL);
Chris Wilsone4ffd172011-04-04 09:44:39 +01003534 if (ret)
3535 return ret;
3536
Tvrtko Ursulin0031fb92016-11-04 14:42:44 +00003537 if (!HAS_LLC(to_i915(obj->base.dev)) &&
3538 cache_level != I915_CACHE_NONE) {
Chris Wilsonef55f922015-10-09 14:11:27 +01003539 /* Access to snoopable pages through the GTT is
3540 * incoherent and on some machines causes a hard
3541 * lockup. Relinquish the CPU mmaping to force
3542 * userspace to refault in the pages and we can
3543 * then double check if the GTT mapping is still
3544 * valid for that pointer access.
3545 */
3546 i915_gem_release_mmap(obj);
Chris Wilsone4ffd172011-04-04 09:44:39 +01003547
Chris Wilsonef55f922015-10-09 14:11:27 +01003548 /* As we no longer need a fence for GTT access,
3549 * we can relinquish it now (and so prevent having
3550 * to steal a fence from someone else on the next
3551 * fence request). Note GPU activity would have
3552 * dropped the fence as all snoopable access is
3553 * supposed to be linear.
3554 */
Chris Wilsone2189dd2017-12-07 21:14:07 +00003555 for_each_ggtt_vma(vma, obj) {
Chris Wilson49ef5292016-08-18 17:17:00 +01003556 ret = i915_vma_put_fence(vma);
3557 if (ret)
3558 return ret;
3559 }
Chris Wilsonef55f922015-10-09 14:11:27 +01003560 } else {
3561 /* We either have incoherent backing store and
3562 * so no GTT access or the architecture is fully
3563 * coherent. In such cases, existing GTT mmaps
3564 * ignore the cache bit in the PTE and we can
3565 * rewrite it without confusing the GPU or having
3566 * to force userspace to fault back in its mmaps.
3567 */
Chris Wilsone4ffd172011-04-04 09:44:39 +01003568 }
3569
Chris Wilson1c7f4bc2016-02-26 11:03:19 +00003570 list_for_each_entry(vma, &obj->vma_list, obj_link) {
Chris Wilsonef55f922015-10-09 14:11:27 +01003571 if (!drm_mm_node_allocated(&vma->node))
3572 continue;
3573
3574 ret = i915_vma_bind(vma, cache_level, PIN_UPDATE);
3575 if (ret)
3576 return ret;
3577 }
Chris Wilsone4ffd172011-04-04 09:44:39 +01003578 }
3579
Chris Wilson1c7f4bc2016-02-26 11:03:19 +00003580 list_for_each_entry(vma, &obj->vma_list, obj_link)
Chris Wilson2c225692013-08-09 12:26:45 +01003581 vma->node.color = cache_level;
Chris Wilsonb8f55be2017-08-11 12:11:16 +01003582 i915_gem_object_set_cache_coherency(obj, cache_level);
Chris Wilsone27ab732017-06-15 13:38:49 +01003583 obj->cache_dirty = true; /* Always invalidate stale cachelines */
Chris Wilson2c225692013-08-09 12:26:45 +01003584
Chris Wilsone4ffd172011-04-04 09:44:39 +01003585 return 0;
3586}
3587
Ben Widawsky199adf42012-09-21 17:01:20 -07003588int i915_gem_get_caching_ioctl(struct drm_device *dev, void *data,
3589 struct drm_file *file)
Chris Wilsone6994ae2012-07-10 10:27:08 +01003590{
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;
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01003593 int err = 0;
Chris Wilsone6994ae2012-07-10 10:27:08 +01003594
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01003595 rcu_read_lock();
3596 obj = i915_gem_object_lookup_rcu(file, args->handle);
3597 if (!obj) {
3598 err = -ENOENT;
3599 goto out;
3600 }
Chris Wilsone6994ae2012-07-10 10:27:08 +01003601
Chris Wilson651d7942013-08-08 14:41:10 +01003602 switch (obj->cache_level) {
3603 case I915_CACHE_LLC:
3604 case I915_CACHE_L3_LLC:
3605 args->caching = I915_CACHING_CACHED;
3606 break;
3607
Chris Wilson4257d3b2013-08-08 14:41:11 +01003608 case I915_CACHE_WT:
3609 args->caching = I915_CACHING_DISPLAY;
3610 break;
3611
Chris Wilson651d7942013-08-08 14:41:10 +01003612 default:
3613 args->caching = I915_CACHING_NONE;
3614 break;
3615 }
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01003616out:
3617 rcu_read_unlock();
3618 return err;
Chris Wilsone6994ae2012-07-10 10:27:08 +01003619}
3620
Ben Widawsky199adf42012-09-21 17:01:20 -07003621int i915_gem_set_caching_ioctl(struct drm_device *dev, void *data,
3622 struct drm_file *file)
Chris Wilsone6994ae2012-07-10 10:27:08 +01003623{
Chris Wilson9c870d02016-10-24 13:42:15 +01003624 struct drm_i915_private *i915 = to_i915(dev);
Ben Widawsky199adf42012-09-21 17:01:20 -07003625 struct drm_i915_gem_caching *args = data;
Chris Wilsone6994ae2012-07-10 10:27:08 +01003626 struct drm_i915_gem_object *obj;
3627 enum i915_cache_level level;
Chris Wilsond65415d2017-01-19 08:22:10 +00003628 int ret = 0;
Chris Wilsone6994ae2012-07-10 10:27:08 +01003629
Ben Widawsky199adf42012-09-21 17:01:20 -07003630 switch (args->caching) {
3631 case I915_CACHING_NONE:
Chris Wilsone6994ae2012-07-10 10:27:08 +01003632 level = I915_CACHE_NONE;
3633 break;
Ben Widawsky199adf42012-09-21 17:01:20 -07003634 case I915_CACHING_CACHED:
Imre Deake5756c12015-08-14 18:43:30 +03003635 /*
3636 * Due to a HW issue on BXT A stepping, GPU stores via a
3637 * snooped mapping may leave stale data in a corresponding CPU
3638 * cacheline, whereas normally such cachelines would get
3639 * invalidated.
3640 */
Chris Wilson9c870d02016-10-24 13:42:15 +01003641 if (!HAS_LLC(i915) && !HAS_SNOOP(i915))
Imre Deake5756c12015-08-14 18:43:30 +03003642 return -ENODEV;
3643
Chris Wilsone6994ae2012-07-10 10:27:08 +01003644 level = I915_CACHE_LLC;
3645 break;
Chris Wilson4257d3b2013-08-08 14:41:11 +01003646 case I915_CACHING_DISPLAY:
Chris Wilson9c870d02016-10-24 13:42:15 +01003647 level = HAS_WT(i915) ? I915_CACHE_WT : I915_CACHE_NONE;
Chris Wilson4257d3b2013-08-08 14:41:11 +01003648 break;
Chris Wilsone6994ae2012-07-10 10:27:08 +01003649 default:
3650 return -EINVAL;
3651 }
3652
Chris Wilsond65415d2017-01-19 08:22:10 +00003653 obj = i915_gem_object_lookup(file, args->handle);
3654 if (!obj)
3655 return -ENOENT;
3656
Tina Zhanga03f3952017-11-14 10:25:13 +00003657 /*
3658 * The caching mode of proxy object is handled by its generator, and
3659 * not allowed to be changed by userspace.
3660 */
3661 if (i915_gem_object_is_proxy(obj)) {
3662 ret = -ENXIO;
3663 goto out;
3664 }
3665
Chris Wilsond65415d2017-01-19 08:22:10 +00003666 if (obj->cache_level == level)
3667 goto out;
3668
3669 ret = i915_gem_object_wait(obj,
3670 I915_WAIT_INTERRUPTIBLE,
3671 MAX_SCHEDULE_TIMEOUT,
3672 to_rps_client(file));
3673 if (ret)
3674 goto out;
3675
Ben Widawsky3bc29132012-09-26 16:15:20 -07003676 ret = i915_mutex_lock_interruptible(dev);
3677 if (ret)
Chris Wilsond65415d2017-01-19 08:22:10 +00003678 goto out;
Chris Wilsone6994ae2012-07-10 10:27:08 +01003679
3680 ret = i915_gem_object_set_cache_level(obj, level);
Chris Wilsone6994ae2012-07-10 10:27:08 +01003681 mutex_unlock(&dev->struct_mutex);
Chris Wilsond65415d2017-01-19 08:22:10 +00003682
3683out:
3684 i915_gem_object_put(obj);
Chris Wilsone6994ae2012-07-10 10:27:08 +01003685 return ret;
3686}
3687
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003688/*
Dhinakaran Pandiyan07bcd992018-03-06 19:34:18 -08003689 * Prepare buffer for display plane (scanout, cursors, etc). Can be called from
3690 * an uninterruptible phase (modesetting) and allows any flushes to be pipelined
3691 * (for pageflips). We only flush the caches while preparing the buffer for
3692 * display, the callers are responsible for frontbuffer flush.
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003693 */
Chris Wilson058d88c2016-08-15 10:49:06 +01003694struct i915_vma *
Chris Wilson2da3b9b2011-04-14 09:41:17 +01003695i915_gem_object_pin_to_display_plane(struct drm_i915_gem_object *obj,
3696 u32 alignment,
Chris Wilson59354852018-02-20 13:42:06 +00003697 const struct i915_ggtt_view *view,
3698 unsigned int flags)
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003699{
Chris Wilson058d88c2016-08-15 10:49:06 +01003700 struct i915_vma *vma;
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003701 int ret;
3702
Chris Wilson4c7d62c2016-10-28 13:58:32 +01003703 lockdep_assert_held(&obj->base.dev->struct_mutex);
3704
Chris Wilsonbd3d2252017-10-13 21:26:14 +01003705 /* Mark the global pin early so that we account for the
Chris Wilsoncc98b412013-08-09 12:25:09 +01003706 * display coherency whilst setting up the cache domains.
3707 */
Chris Wilsonbd3d2252017-10-13 21:26:14 +01003708 obj->pin_global++;
Chris Wilsoncc98b412013-08-09 12:25:09 +01003709
Eric Anholta7ef0642011-03-29 16:59:54 -07003710 /* The display engine is not coherent with the LLC cache on gen6. As
3711 * a result, we make sure that the pinning that is about to occur is
3712 * done with uncached PTEs. This is lowest common denominator for all
3713 * chipsets.
3714 *
3715 * However for gen6+, we could do better by using the GFDT bit instead
3716 * of uncaching, which would allow us to flush all the LLC-cached data
3717 * with that bit in the PTE to main memory with just one PIPE_CONTROL.
3718 */
Chris Wilson651d7942013-08-08 14:41:10 +01003719 ret = i915_gem_object_set_cache_level(obj,
Tvrtko Ursulin86527442016-10-13 11:03:00 +01003720 HAS_WT(to_i915(obj->base.dev)) ?
3721 I915_CACHE_WT : I915_CACHE_NONE);
Chris Wilson058d88c2016-08-15 10:49:06 +01003722 if (ret) {
3723 vma = ERR_PTR(ret);
Chris Wilsonbd3d2252017-10-13 21:26:14 +01003724 goto err_unpin_global;
Chris Wilson058d88c2016-08-15 10:49:06 +01003725 }
Eric Anholta7ef0642011-03-29 16:59:54 -07003726
Chris Wilson2da3b9b2011-04-14 09:41:17 +01003727 /* As the user may map the buffer once pinned in the display plane
3728 * (e.g. libkms for the bootup splash), we have to ensure that we
Chris Wilson2efb8132016-08-18 17:17:06 +01003729 * always use map_and_fenceable for all scanout buffers. However,
3730 * it may simply be too big to fit into mappable, in which case
3731 * put it anyway and hope that userspace can cope (but always first
3732 * try to preserve the existing ABI).
Chris Wilson2da3b9b2011-04-14 09:41:17 +01003733 */
Chris Wilson2efb8132016-08-18 17:17:06 +01003734 vma = ERR_PTR(-ENOSPC);
Chris Wilson59354852018-02-20 13:42:06 +00003735 if ((flags & PIN_MAPPABLE) == 0 &&
3736 (!view || view->type == I915_GGTT_VIEW_NORMAL))
Chris Wilson2efb8132016-08-18 17:17:06 +01003737 vma = i915_gem_object_ggtt_pin(obj, view, 0, alignment,
Chris Wilson59354852018-02-20 13:42:06 +00003738 flags |
3739 PIN_MAPPABLE |
3740 PIN_NONBLOCK);
3741 if (IS_ERR(vma))
Chris Wilson767a2222016-11-07 11:01:28 +00003742 vma = i915_gem_object_ggtt_pin(obj, view, 0, alignment, flags);
Chris Wilson058d88c2016-08-15 10:49:06 +01003743 if (IS_ERR(vma))
Chris Wilsonbd3d2252017-10-13 21:26:14 +01003744 goto err_unpin_global;
Chris Wilson2da3b9b2011-04-14 09:41:17 +01003745
Chris Wilsond8923dc2016-08-18 17:17:07 +01003746 vma->display_alignment = max_t(u64, vma->display_alignment, alignment);
3747
Chris Wilson5a97bcc2017-02-22 11:40:46 +00003748 __i915_gem_object_flush_for_display(obj);
Chris Wilsonb118c1e2010-05-27 13:18:14 +01003749
Chris Wilson2da3b9b2011-04-14 09:41:17 +01003750 /* It should now be out of any other write domains, and we can update
3751 * the domain values for our changes.
3752 */
Christian Königc0a51fd2018-02-16 13:43:38 +01003753 obj->read_domains |= I915_GEM_DOMAIN_GTT;
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003754
Chris Wilson058d88c2016-08-15 10:49:06 +01003755 return vma;
Chris Wilsoncc98b412013-08-09 12:25:09 +01003756
Chris Wilsonbd3d2252017-10-13 21:26:14 +01003757err_unpin_global:
3758 obj->pin_global--;
Chris Wilson058d88c2016-08-15 10:49:06 +01003759 return vma;
Chris Wilsoncc98b412013-08-09 12:25:09 +01003760}
3761
3762void
Chris Wilson058d88c2016-08-15 10:49:06 +01003763i915_gem_object_unpin_from_display_plane(struct i915_vma *vma)
Chris Wilsoncc98b412013-08-09 12:25:09 +01003764{
Chris Wilson49d73912016-11-29 09:50:08 +00003765 lockdep_assert_held(&vma->vm->i915->drm.struct_mutex);
Chris Wilson4c7d62c2016-10-28 13:58:32 +01003766
Chris Wilsonbd3d2252017-10-13 21:26:14 +01003767 if (WARN_ON(vma->obj->pin_global == 0))
Tvrtko Ursulin8a0c39b2015-04-13 11:50:09 +01003768 return;
3769
Chris Wilsonbd3d2252017-10-13 21:26:14 +01003770 if (--vma->obj->pin_global == 0)
Chris Wilsonf51455d2017-01-10 14:47:34 +00003771 vma->display_alignment = I915_GTT_MIN_ALIGNMENT;
Tvrtko Ursuline6617332015-03-23 11:10:33 +00003772
Chris Wilson383d5822016-08-18 17:17:08 +01003773 /* Bump the LRU to try and avoid premature eviction whilst flipping */
Chris Wilsonbefedbb2017-01-19 19:26:55 +00003774 i915_gem_object_bump_inactive_ggtt(vma->obj);
Chris Wilson383d5822016-08-18 17:17:08 +01003775
Chris Wilson058d88c2016-08-15 10:49:06 +01003776 i915_vma_unpin(vma);
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003777}
3778
Eric Anholte47c68e2008-11-14 13:35:19 -08003779/**
3780 * Moves a single object to the CPU read, and possibly write domain.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01003781 * @obj: object to act on
3782 * @write: requesting write or read-only access
Eric Anholte47c68e2008-11-14 13:35:19 -08003783 *
3784 * This function returns when the move is complete, including waiting on
3785 * flushes to occur.
3786 */
Chris Wilsondabdfe02012-03-26 10:10:27 +02003787int
Chris Wilson919926a2010-11-12 13:42:53 +00003788i915_gem_object_set_to_cpu_domain(struct drm_i915_gem_object *obj, bool write)
Eric Anholte47c68e2008-11-14 13:35:19 -08003789{
Eric Anholte47c68e2008-11-14 13:35:19 -08003790 int ret;
3791
Chris Wilsone95433c2016-10-28 13:58:27 +01003792 lockdep_assert_held(&obj->base.dev->struct_mutex);
Chris Wilson4c7d62c2016-10-28 13:58:32 +01003793
Chris Wilsone95433c2016-10-28 13:58:27 +01003794 ret = i915_gem_object_wait(obj,
3795 I915_WAIT_INTERRUPTIBLE |
3796 I915_WAIT_LOCKED |
3797 (write ? I915_WAIT_ALL : 0),
3798 MAX_SCHEDULE_TIMEOUT,
3799 NULL);
Chris Wilson88241782011-01-07 17:09:48 +00003800 if (ret)
3801 return ret;
3802
Chris Wilsonef749212017-04-12 12:01:10 +01003803 flush_write_domain(obj, ~I915_GEM_DOMAIN_CPU);
Eric Anholte47c68e2008-11-14 13:35:19 -08003804
Eric Anholte47c68e2008-11-14 13:35:19 -08003805 /* Flush the CPU cache if it's still invalid. */
Christian Königc0a51fd2018-02-16 13:43:38 +01003806 if ((obj->read_domains & I915_GEM_DOMAIN_CPU) == 0) {
Chris Wilson57822dc2017-02-22 11:40:48 +00003807 i915_gem_clflush_object(obj, I915_CLFLUSH_SYNC);
Christian Königc0a51fd2018-02-16 13:43:38 +01003808 obj->read_domains |= I915_GEM_DOMAIN_CPU;
Eric Anholte47c68e2008-11-14 13:35:19 -08003809 }
3810
3811 /* It should now be out of any other write domains, and we can update
3812 * the domain values for our changes.
3813 */
Christian Königc0a51fd2018-02-16 13:43:38 +01003814 GEM_BUG_ON(obj->write_domain & ~I915_GEM_DOMAIN_CPU);
Eric Anholte47c68e2008-11-14 13:35:19 -08003815
3816 /* If we're writing through the CPU, then the GPU read domains will
3817 * need to be invalidated at next use.
3818 */
Chris Wilsone27ab732017-06-15 13:38:49 +01003819 if (write)
3820 __start_cpu_write(obj);
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003821
3822 return 0;
3823}
3824
Eric Anholt673a3942008-07-30 12:06:12 -07003825/* Throttle our rendering by waiting until the ring has completed our requests
3826 * emitted over 20 msec ago.
3827 *
Eric Anholtb9624422009-06-03 07:27:35 +00003828 * Note that if we were to use the current jiffies each time around the loop,
3829 * we wouldn't escape the function with any frames outstanding if the time to
3830 * render a frame was over 20ms.
3831 *
Eric Anholt673a3942008-07-30 12:06:12 -07003832 * This should get us reasonable parallelism between CPU and GPU but also
3833 * relatively low latency when blocking on a particular request to finish.
3834 */
3835static int
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003836i915_gem_ring_throttle(struct drm_device *dev, struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07003837{
Chris Wilsonfac5e232016-07-04 11:34:36 +01003838 struct drm_i915_private *dev_priv = to_i915(dev);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003839 struct drm_i915_file_private *file_priv = file->driver_priv;
Chris Wilsond0bc54f2015-05-21 21:01:48 +01003840 unsigned long recent_enough = jiffies - DRM_I915_THROTTLE_JIFFIES;
Chris Wilsone61e0f52018-02-21 09:56:36 +00003841 struct i915_request *request, *target = NULL;
Chris Wilsone95433c2016-10-28 13:58:27 +01003842 long ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003843
Chris Wilsonf4457ae2016-04-13 17:35:08 +01003844 /* ABI: return -EIO if already wedged */
3845 if (i915_terminally_wedged(&dev_priv->gpu_error))
3846 return -EIO;
Chris Wilsone110e8d2011-01-26 15:39:14 +00003847
Chris Wilson1c255952010-09-26 11:03:27 +01003848 spin_lock(&file_priv->mm.lock);
Chris Wilsonc8659ef2017-03-02 12:25:25 +00003849 list_for_each_entry(request, &file_priv->mm.request_list, client_link) {
Eric Anholtb9624422009-06-03 07:27:35 +00003850 if (time_after_eq(request->emitted_jiffies, recent_enough))
3851 break;
3852
Chris Wilsonc8659ef2017-03-02 12:25:25 +00003853 if (target) {
3854 list_del(&target->client_link);
3855 target->file_priv = NULL;
3856 }
John Harrisonfcfa423c2015-05-29 17:44:12 +01003857
John Harrison54fb2412014-11-24 18:49:27 +00003858 target = request;
Eric Anholtb9624422009-06-03 07:27:35 +00003859 }
John Harrisonff865882014-11-24 18:49:28 +00003860 if (target)
Chris Wilsone61e0f52018-02-21 09:56:36 +00003861 i915_request_get(target);
Chris Wilson1c255952010-09-26 11:03:27 +01003862 spin_unlock(&file_priv->mm.lock);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003863
John Harrison54fb2412014-11-24 18:49:27 +00003864 if (target == NULL)
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003865 return 0;
3866
Chris Wilsone61e0f52018-02-21 09:56:36 +00003867 ret = i915_request_wait(target,
Chris Wilsone95433c2016-10-28 13:58:27 +01003868 I915_WAIT_INTERRUPTIBLE,
3869 MAX_SCHEDULE_TIMEOUT);
Chris Wilsone61e0f52018-02-21 09:56:36 +00003870 i915_request_put(target);
John Harrisonff865882014-11-24 18:49:28 +00003871
Chris Wilsone95433c2016-10-28 13:58:27 +01003872 return ret < 0 ? ret : 0;
Eric Anholt673a3942008-07-30 12:06:12 -07003873}
3874
Chris Wilson058d88c2016-08-15 10:49:06 +01003875struct i915_vma *
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02003876i915_gem_object_ggtt_pin(struct drm_i915_gem_object *obj,
3877 const struct i915_ggtt_view *view,
Chris Wilson91b2db62016-08-04 16:32:23 +01003878 u64 size,
Chris Wilson2ffffd02016-08-04 16:32:22 +01003879 u64 alignment,
3880 u64 flags)
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02003881{
Chris Wilsonad16d2e2016-10-13 09:55:04 +01003882 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
Chris Wilson82ad6442018-06-05 16:37:58 +01003883 struct i915_address_space *vm = &dev_priv->ggtt.vm;
Chris Wilson59bfa122016-08-04 16:32:31 +01003884 struct i915_vma *vma;
3885 int ret;
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03003886
Chris Wilson4c7d62c2016-10-28 13:58:32 +01003887 lockdep_assert_held(&obj->base.dev->struct_mutex);
3888
Chris Wilsonac87a6fd2018-02-20 13:42:05 +00003889 if (flags & PIN_MAPPABLE &&
3890 (!view || view->type == I915_GGTT_VIEW_NORMAL)) {
Chris Wilson43ae70d92017-10-09 09:44:01 +01003891 /* If the required space is larger than the available
3892 * aperture, we will not able to find a slot for the
3893 * object and unbinding the object now will be in
3894 * vain. Worse, doing so may cause us to ping-pong
3895 * the object in and out of the Global GTT and
3896 * waste a lot of cycles under the mutex.
3897 */
3898 if (obj->base.size > dev_priv->ggtt.mappable_end)
3899 return ERR_PTR(-E2BIG);
3900
3901 /* If NONBLOCK is set the caller is optimistically
3902 * trying to cache the full object within the mappable
3903 * aperture, and *must* have a fallback in place for
3904 * situations where we cannot bind the object. We
3905 * can be a little more lax here and use the fallback
3906 * more often to avoid costly migrations of ourselves
3907 * and other objects within the aperture.
3908 *
3909 * Half-the-aperture is used as a simple heuristic.
3910 * More interesting would to do search for a free
3911 * block prior to making the commitment to unbind.
3912 * That caters for the self-harm case, and with a
3913 * little more heuristics (e.g. NOFAULT, NOEVICT)
3914 * we could try to minimise harm to others.
3915 */
3916 if (flags & PIN_NONBLOCK &&
3917 obj->base.size > dev_priv->ggtt.mappable_end / 2)
3918 return ERR_PTR(-ENOSPC);
3919 }
3920
Chris Wilson718659a2017-01-16 15:21:28 +00003921 vma = i915_vma_instance(obj, vm, view);
Chris Wilsone0216b72017-01-19 19:26:57 +00003922 if (unlikely(IS_ERR(vma)))
Chris Wilson058d88c2016-08-15 10:49:06 +01003923 return vma;
Chris Wilson59bfa122016-08-04 16:32:31 +01003924
3925 if (i915_vma_misplaced(vma, size, alignment, flags)) {
Chris Wilson43ae70d92017-10-09 09:44:01 +01003926 if (flags & PIN_NONBLOCK) {
3927 if (i915_vma_is_pinned(vma) || i915_vma_is_active(vma))
3928 return ERR_PTR(-ENOSPC);
Chris Wilson59bfa122016-08-04 16:32:31 +01003929
Chris Wilson43ae70d92017-10-09 09:44:01 +01003930 if (flags & PIN_MAPPABLE &&
Chris Wilson944397f2017-01-09 16:16:11 +00003931 vma->fence_size > dev_priv->ggtt.mappable_end / 2)
Chris Wilsonad16d2e2016-10-13 09:55:04 +01003932 return ERR_PTR(-ENOSPC);
3933 }
3934
Chris Wilson59bfa122016-08-04 16:32:31 +01003935 WARN(i915_vma_is_pinned(vma),
3936 "bo is already pinned in ggtt with incorrect alignment:"
Chris Wilson05a20d02016-08-18 17:16:55 +01003937 " offset=%08x, req.alignment=%llx,"
3938 " req.map_and_fenceable=%d, vma->map_and_fenceable=%d\n",
3939 i915_ggtt_offset(vma), alignment,
Chris Wilson59bfa122016-08-04 16:32:31 +01003940 !!(flags & PIN_MAPPABLE),
Chris Wilson05a20d02016-08-18 17:16:55 +01003941 i915_vma_is_map_and_fenceable(vma));
Chris Wilson59bfa122016-08-04 16:32:31 +01003942 ret = i915_vma_unbind(vma);
3943 if (ret)
Chris Wilson058d88c2016-08-15 10:49:06 +01003944 return ERR_PTR(ret);
Chris Wilson59bfa122016-08-04 16:32:31 +01003945 }
3946
Chris Wilson058d88c2016-08-15 10:49:06 +01003947 ret = i915_vma_pin(vma, size, alignment, flags | PIN_GLOBAL);
3948 if (ret)
3949 return ERR_PTR(ret);
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02003950
Chris Wilson058d88c2016-08-15 10:49:06 +01003951 return vma;
Eric Anholt673a3942008-07-30 12:06:12 -07003952}
3953
Chris Wilsonedf6b762016-08-09 09:23:33 +01003954static __always_inline unsigned int __busy_read_flag(unsigned int id)
Chris Wilson3fdc13c2016-08-05 10:14:18 +01003955{
3956 /* Note that we could alias engines in the execbuf API, but
3957 * that would be very unwise as it prevents userspace from
3958 * fine control over engine selection. Ahem.
3959 *
3960 * This should be something like EXEC_MAX_ENGINE instead of
3961 * I915_NUM_ENGINES.
3962 */
3963 BUILD_BUG_ON(I915_NUM_ENGINES > 16);
3964 return 0x10000 << id;
3965}
3966
3967static __always_inline unsigned int __busy_write_id(unsigned int id)
3968{
Chris Wilson70cb4722016-08-09 18:08:25 +01003969 /* The uABI guarantees an active writer is also amongst the read
3970 * engines. This would be true if we accessed the activity tracking
3971 * under the lock, but as we perform the lookup of the object and
3972 * its activity locklessly we can not guarantee that the last_write
3973 * being active implies that we have set the same engine flag from
3974 * last_read - hence we always set both read and write busy for
3975 * last_write.
3976 */
3977 return id | __busy_read_flag(id);
Chris Wilson3fdc13c2016-08-05 10:14:18 +01003978}
3979
Chris Wilsonedf6b762016-08-09 09:23:33 +01003980static __always_inline unsigned int
Chris Wilsond07f0e52016-10-28 13:58:44 +01003981__busy_set_if_active(const struct dma_fence *fence,
Chris Wilson3fdc13c2016-08-05 10:14:18 +01003982 unsigned int (*flag)(unsigned int id))
3983{
Chris Wilsone61e0f52018-02-21 09:56:36 +00003984 struct i915_request *rq;
Chris Wilson12555012016-08-16 09:50:40 +01003985
Chris Wilsond07f0e52016-10-28 13:58:44 +01003986 /* We have to check the current hw status of the fence as the uABI
3987 * guarantees forward progress. We could rely on the idle worker
3988 * to eventually flush us, but to minimise latency just ask the
3989 * hardware.
3990 *
3991 * Note we only report on the status of native fences.
3992 */
3993 if (!dma_fence_is_i915(fence))
Chris Wilson12555012016-08-16 09:50:40 +01003994 return 0;
3995
Chris Wilsond07f0e52016-10-28 13:58:44 +01003996 /* opencode to_request() in order to avoid const warnings */
Chris Wilsone61e0f52018-02-21 09:56:36 +00003997 rq = container_of(fence, struct i915_request, fence);
3998 if (i915_request_completed(rq))
Chris Wilsond07f0e52016-10-28 13:58:44 +01003999 return 0;
4000
Chris Wilson1d39f282017-04-11 13:43:06 +01004001 return flag(rq->engine->uabi_id);
Chris Wilson3fdc13c2016-08-05 10:14:18 +01004002}
4003
Chris Wilsonedf6b762016-08-09 09:23:33 +01004004static __always_inline unsigned int
Chris Wilsond07f0e52016-10-28 13:58:44 +01004005busy_check_reader(const struct dma_fence *fence)
Chris Wilson3fdc13c2016-08-05 10:14:18 +01004006{
Chris Wilsond07f0e52016-10-28 13:58:44 +01004007 return __busy_set_if_active(fence, __busy_read_flag);
Chris Wilson3fdc13c2016-08-05 10:14:18 +01004008}
4009
Chris Wilsonedf6b762016-08-09 09:23:33 +01004010static __always_inline unsigned int
Chris Wilsond07f0e52016-10-28 13:58:44 +01004011busy_check_writer(const struct dma_fence *fence)
Chris Wilson3fdc13c2016-08-05 10:14:18 +01004012{
Chris Wilsond07f0e52016-10-28 13:58:44 +01004013 if (!fence)
4014 return 0;
4015
4016 return __busy_set_if_active(fence, __busy_write_id);
Chris Wilson3fdc13c2016-08-05 10:14:18 +01004017}
4018
Eric Anholt673a3942008-07-30 12:06:12 -07004019int
Eric Anholt673a3942008-07-30 12:06:12 -07004020i915_gem_busy_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00004021 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07004022{
4023 struct drm_i915_gem_busy *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00004024 struct drm_i915_gem_object *obj;
Chris Wilsond07f0e52016-10-28 13:58:44 +01004025 struct reservation_object_list *list;
4026 unsigned int seq;
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004027 int err;
Eric Anholt673a3942008-07-30 12:06:12 -07004028
Chris Wilsond07f0e52016-10-28 13:58:44 +01004029 err = -ENOENT;
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004030 rcu_read_lock();
4031 obj = i915_gem_object_lookup_rcu(file, args->handle);
Chris Wilsond07f0e52016-10-28 13:58:44 +01004032 if (!obj)
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004033 goto out;
Chris Wilsond07f0e52016-10-28 13:58:44 +01004034
4035 /* A discrepancy here is that we do not report the status of
4036 * non-i915 fences, i.e. even though we may report the object as idle,
4037 * a call to set-domain may still stall waiting for foreign rendering.
4038 * This also means that wait-ioctl may report an object as busy,
4039 * where busy-ioctl considers it idle.
4040 *
4041 * We trade the ability to warn of foreign fences to report on which
4042 * i915 engines are active for the object.
4043 *
4044 * Alternatively, we can trade that extra information on read/write
4045 * activity with
4046 * args->busy =
4047 * !reservation_object_test_signaled_rcu(obj->resv, true);
4048 * to report the overall busyness. This is what the wait-ioctl does.
4049 *
4050 */
4051retry:
4052 seq = raw_read_seqcount(&obj->resv->seq);
4053
4054 /* Translate the exclusive fence to the READ *and* WRITE engine */
4055 args->busy = busy_check_writer(rcu_dereference(obj->resv->fence_excl));
4056
4057 /* Translate shared fences to READ set of engines */
4058 list = rcu_dereference(obj->resv->fence);
4059 if (list) {
4060 unsigned int shared_count = list->shared_count, i;
4061
4062 for (i = 0; i < shared_count; ++i) {
4063 struct dma_fence *fence =
4064 rcu_dereference(list->shared[i]);
4065
4066 args->busy |= busy_check_reader(fence);
4067 }
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004068 }
Zou Nan haid1b851f2010-05-21 09:08:57 +08004069
Chris Wilsond07f0e52016-10-28 13:58:44 +01004070 if (args->busy && read_seqcount_retry(&obj->resv->seq, seq))
4071 goto retry;
Chris Wilson426960b2016-01-15 16:51:46 +00004072
Chris Wilsond07f0e52016-10-28 13:58:44 +01004073 err = 0;
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004074out:
4075 rcu_read_unlock();
4076 return err;
Eric Anholt673a3942008-07-30 12:06:12 -07004077}
4078
4079int
4080i915_gem_throttle_ioctl(struct drm_device *dev, void *data,
4081 struct drm_file *file_priv)
4082{
Akshay Joshi0206e352011-08-16 15:34:10 -04004083 return i915_gem_ring_throttle(dev, file_priv);
Eric Anholt673a3942008-07-30 12:06:12 -07004084}
4085
Chris Wilson3ef94da2009-09-14 16:50:29 +01004086int
4087i915_gem_madvise_ioctl(struct drm_device *dev, void *data,
4088 struct drm_file *file_priv)
4089{
Chris Wilsonfac5e232016-07-04 11:34:36 +01004090 struct drm_i915_private *dev_priv = to_i915(dev);
Chris Wilson3ef94da2009-09-14 16:50:29 +01004091 struct drm_i915_gem_madvise *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00004092 struct drm_i915_gem_object *obj;
Chris Wilson1233e2d2016-10-28 13:58:37 +01004093 int err;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004094
4095 switch (args->madv) {
4096 case I915_MADV_DONTNEED:
4097 case I915_MADV_WILLNEED:
4098 break;
4099 default:
4100 return -EINVAL;
4101 }
4102
Chris Wilson03ac0642016-07-20 13:31:51 +01004103 obj = i915_gem_object_lookup(file_priv, args->handle);
Chris Wilson1233e2d2016-10-28 13:58:37 +01004104 if (!obj)
4105 return -ENOENT;
4106
4107 err = mutex_lock_interruptible(&obj->mm.lock);
4108 if (err)
4109 goto out;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004110
Chris Wilsonf1fa4f42017-10-13 21:26:13 +01004111 if (i915_gem_object_has_pages(obj) &&
Chris Wilson3e510a82016-08-05 10:14:23 +01004112 i915_gem_object_is_tiled(obj) &&
Daniel Vetter656bfa32014-11-20 09:26:30 +01004113 dev_priv->quirks & QUIRK_PIN_SWIZZLED_PAGES) {
Chris Wilsonbc0629a2016-11-01 10:03:17 +00004114 if (obj->mm.madv == I915_MADV_WILLNEED) {
4115 GEM_BUG_ON(!obj->mm.quirked);
Chris Wilsona4f5ea62016-10-28 13:58:35 +01004116 __i915_gem_object_unpin_pages(obj);
Chris Wilsonbc0629a2016-11-01 10:03:17 +00004117 obj->mm.quirked = false;
4118 }
4119 if (args->madv == I915_MADV_WILLNEED) {
Chris Wilson2c3a3f42016-11-04 10:30:01 +00004120 GEM_BUG_ON(obj->mm.quirked);
Chris Wilsona4f5ea62016-10-28 13:58:35 +01004121 __i915_gem_object_pin_pages(obj);
Chris Wilsonbc0629a2016-11-01 10:03:17 +00004122 obj->mm.quirked = true;
4123 }
Daniel Vetter656bfa32014-11-20 09:26:30 +01004124 }
4125
Chris Wilsona4f5ea62016-10-28 13:58:35 +01004126 if (obj->mm.madv != __I915_MADV_PURGED)
4127 obj->mm.madv = args->madv;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004128
Chris Wilson6c085a72012-08-20 11:40:46 +02004129 /* if the object is no longer attached, discard its backing storage */
Chris Wilsonf1fa4f42017-10-13 21:26:13 +01004130 if (obj->mm.madv == I915_MADV_DONTNEED &&
4131 !i915_gem_object_has_pages(obj))
Chris Wilson2d7ef392009-09-20 23:13:10 +01004132 i915_gem_object_truncate(obj);
4133
Chris Wilsona4f5ea62016-10-28 13:58:35 +01004134 args->retained = obj->mm.madv != __I915_MADV_PURGED;
Chris Wilson1233e2d2016-10-28 13:58:37 +01004135 mutex_unlock(&obj->mm.lock);
Chris Wilsonbb6baf72009-09-22 14:24:13 +01004136
Chris Wilson1233e2d2016-10-28 13:58:37 +01004137out:
Chris Wilsonf8c417c2016-07-20 13:31:53 +01004138 i915_gem_object_put(obj);
Chris Wilson1233e2d2016-10-28 13:58:37 +01004139 return err;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004140}
4141
Chris Wilson5b8c8ae2016-11-16 19:07:04 +00004142static void
Chris Wilsone61e0f52018-02-21 09:56:36 +00004143frontbuffer_retire(struct i915_gem_active *active, struct i915_request *request)
Chris Wilson5b8c8ae2016-11-16 19:07:04 +00004144{
4145 struct drm_i915_gem_object *obj =
4146 container_of(active, typeof(*obj), frontbuffer_write);
4147
Chris Wilsond59b21e2017-02-22 11:40:49 +00004148 intel_fb_obj_flush(obj, ORIGIN_CS);
Chris Wilson5b8c8ae2016-11-16 19:07:04 +00004149}
4150
Chris Wilson37e680a2012-06-07 15:38:42 +01004151void i915_gem_object_init(struct drm_i915_gem_object *obj,
4152 const struct drm_i915_gem_object_ops *ops)
Chris Wilson0327d6b2012-08-11 15:41:06 +01004153{
Chris Wilson1233e2d2016-10-28 13:58:37 +01004154 mutex_init(&obj->mm.lock);
4155
Ben Widawsky2f633152013-07-17 12:19:03 -07004156 INIT_LIST_HEAD(&obj->vma_list);
Chris Wilsond1b48c12017-08-16 09:52:08 +01004157 INIT_LIST_HEAD(&obj->lut_list);
Chris Wilson8d9d5742015-04-07 16:20:38 +01004158 INIT_LIST_HEAD(&obj->batch_pool_link);
Chris Wilson0327d6b2012-08-11 15:41:06 +01004159
Chris Wilson8811d612018-11-09 09:03:11 +00004160 init_rcu_head(&obj->rcu);
4161
Chris Wilson37e680a2012-06-07 15:38:42 +01004162 obj->ops = ops;
4163
Chris Wilsond07f0e52016-10-28 13:58:44 +01004164 reservation_object_init(&obj->__builtin_resv);
4165 obj->resv = &obj->__builtin_resv;
4166
Chris Wilson50349242016-08-18 17:17:04 +01004167 obj->frontbuffer_ggtt_origin = ORIGIN_GTT;
Chris Wilson5b8c8ae2016-11-16 19:07:04 +00004168 init_request_active(&obj->frontbuffer_write, frontbuffer_retire);
Chris Wilsona4f5ea62016-10-28 13:58:35 +01004169
4170 obj->mm.madv = I915_MADV_WILLNEED;
4171 INIT_RADIX_TREE(&obj->mm.get_page.radix, GFP_KERNEL | __GFP_NOWARN);
4172 mutex_init(&obj->mm.get_page.lock);
Chris Wilson0327d6b2012-08-11 15:41:06 +01004173
Dave Gordonf19ec8c2016-07-04 11:34:37 +01004174 i915_gem_info_add_obj(to_i915(obj->base.dev), obj->base.size);
Chris Wilson0327d6b2012-08-11 15:41:06 +01004175}
4176
Chris Wilson37e680a2012-06-07 15:38:42 +01004177static const struct drm_i915_gem_object_ops i915_gem_object_ops = {
Tvrtko Ursulin3599a912016-11-01 14:44:10 +00004178 .flags = I915_GEM_OBJECT_HAS_STRUCT_PAGE |
4179 I915_GEM_OBJECT_IS_SHRINKABLE,
Chris Wilson7c55e2c2017-03-07 12:03:38 +00004180
Chris Wilson37e680a2012-06-07 15:38:42 +01004181 .get_pages = i915_gem_object_get_pages_gtt,
4182 .put_pages = i915_gem_object_put_pages_gtt,
Chris Wilson7c55e2c2017-03-07 12:03:38 +00004183
4184 .pwrite = i915_gem_object_pwrite_gtt,
Chris Wilson37e680a2012-06-07 15:38:42 +01004185};
4186
Matthew Auld465c4032017-10-06 23:18:14 +01004187static int i915_gem_object_create_shmem(struct drm_device *dev,
4188 struct drm_gem_object *obj,
4189 size_t size)
4190{
4191 struct drm_i915_private *i915 = to_i915(dev);
4192 unsigned long flags = VM_NORESERVE;
4193 struct file *filp;
4194
4195 drm_gem_private_object_init(dev, obj, size);
4196
4197 if (i915->mm.gemfs)
4198 filp = shmem_file_setup_with_mnt(i915->mm.gemfs, "i915", size,
4199 flags);
4200 else
4201 filp = shmem_file_setup("i915", size, flags);
4202
4203 if (IS_ERR(filp))
4204 return PTR_ERR(filp);
4205
4206 obj->filp = filp;
4207
4208 return 0;
4209}
4210
Chris Wilsonb4bcbe22016-10-18 13:02:49 +01004211struct drm_i915_gem_object *
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +00004212i915_gem_object_create(struct drm_i915_private *dev_priv, u64 size)
Daniel Vetterac52bc52010-04-09 19:05:06 +00004213{
Daniel Vetterc397b902010-04-09 19:05:07 +00004214 struct drm_i915_gem_object *obj;
Hugh Dickins5949eac2011-06-27 16:18:18 -07004215 struct address_space *mapping;
Chris Wilsonb8f55be2017-08-11 12:11:16 +01004216 unsigned int cache_level;
Daniel Vetter1a240d42012-11-29 22:18:51 +01004217 gfp_t mask;
Chris Wilsonfe3db792016-04-25 13:32:13 +01004218 int ret;
Daniel Vetterc397b902010-04-09 19:05:07 +00004219
Chris Wilsonb4bcbe22016-10-18 13:02:49 +01004220 /* There is a prevalence of the assumption that we fit the object's
4221 * page count inside a 32bit _signed_ variable. Let's document this and
4222 * catch if we ever need to fix it. In the meantime, if you do spot
4223 * such a local variable, please consider fixing!
4224 */
Tvrtko Ursulin7a3ee5d2017-03-30 17:31:30 +01004225 if (size >> PAGE_SHIFT > INT_MAX)
Chris Wilsonb4bcbe22016-10-18 13:02:49 +01004226 return ERR_PTR(-E2BIG);
4227
4228 if (overflows_type(size, obj->base.size))
4229 return ERR_PTR(-E2BIG);
4230
Tvrtko Ursulin187685c2016-12-01 14:16:36 +00004231 obj = i915_gem_object_alloc(dev_priv);
Daniel Vetterc397b902010-04-09 19:05:07 +00004232 if (obj == NULL)
Chris Wilsonfe3db792016-04-25 13:32:13 +01004233 return ERR_PTR(-ENOMEM);
Daniel Vetterc397b902010-04-09 19:05:07 +00004234
Matthew Auld465c4032017-10-06 23:18:14 +01004235 ret = i915_gem_object_create_shmem(&dev_priv->drm, &obj->base, size);
Chris Wilsonfe3db792016-04-25 13:32:13 +01004236 if (ret)
4237 goto fail;
Daniel Vetterc397b902010-04-09 19:05:07 +00004238
Chris Wilsonbed1ea92012-05-24 20:48:12 +01004239 mask = GFP_HIGHUSER | __GFP_RECLAIMABLE;
Jani Nikulac0f86832016-12-07 12:13:04 +02004240 if (IS_I965GM(dev_priv) || IS_I965G(dev_priv)) {
Chris Wilsonbed1ea92012-05-24 20:48:12 +01004241 /* 965gm cannot relocate objects above 4GiB. */
4242 mask &= ~__GFP_HIGHMEM;
4243 mask |= __GFP_DMA32;
4244 }
4245
Al Viro93c76a32015-12-04 23:45:44 -05004246 mapping = obj->base.filp->f_mapping;
Chris Wilsonbed1ea92012-05-24 20:48:12 +01004247 mapping_set_gfp_mask(mapping, mask);
Chris Wilson4846bf02017-06-09 12:03:46 +01004248 GEM_BUG_ON(!(mapping_gfp_mask(mapping) & __GFP_RECLAIM));
Hugh Dickins5949eac2011-06-27 16:18:18 -07004249
Chris Wilson37e680a2012-06-07 15:38:42 +01004250 i915_gem_object_init(obj, &i915_gem_object_ops);
Chris Wilson73aa8082010-09-30 11:46:12 +01004251
Christian Königc0a51fd2018-02-16 13:43:38 +01004252 obj->write_domain = I915_GEM_DOMAIN_CPU;
4253 obj->read_domains = I915_GEM_DOMAIN_CPU;
Daniel Vetterc397b902010-04-09 19:05:07 +00004254
Chris Wilsonb8f55be2017-08-11 12:11:16 +01004255 if (HAS_LLC(dev_priv))
Eugeni Dodonov3d29b842012-01-17 14:43:53 -02004256 /* On some devices, we can have the GPU use the LLC (the CPU
Eric Anholta1871112011-03-29 16:59:55 -07004257 * cache) for about a 10% performance improvement
4258 * compared to uncached. Graphics requests other than
4259 * display scanout are coherent with the CPU in
4260 * accessing this cache. This means in this mode we
4261 * don't need to clflush on the CPU side, and on the
4262 * GPU side we only need to flush internal caches to
4263 * get data visible to the CPU.
4264 *
4265 * However, we maintain the display planes as UC, and so
4266 * need to rebind when first used as such.
4267 */
Chris Wilsonb8f55be2017-08-11 12:11:16 +01004268 cache_level = I915_CACHE_LLC;
4269 else
4270 cache_level = I915_CACHE_NONE;
Eric Anholta1871112011-03-29 16:59:55 -07004271
Chris Wilsonb8f55be2017-08-11 12:11:16 +01004272 i915_gem_object_set_cache_coherency(obj, cache_level);
Chris Wilsone27ab732017-06-15 13:38:49 +01004273
Daniel Vetterd861e332013-07-24 23:25:03 +02004274 trace_i915_gem_object_create(obj);
4275
Chris Wilson05394f32010-11-08 19:18:58 +00004276 return obj;
Chris Wilsonfe3db792016-04-25 13:32:13 +01004277
4278fail:
4279 i915_gem_object_free(obj);
Chris Wilsonfe3db792016-04-25 13:32:13 +01004280 return ERR_PTR(ret);
Daniel Vetterac52bc52010-04-09 19:05:06 +00004281}
4282
Chris Wilson340fbd82014-05-22 09:16:52 +01004283static bool discard_backing_storage(struct drm_i915_gem_object *obj)
4284{
4285 /* If we are the last user of the backing storage (be it shmemfs
4286 * pages or stolen etc), we know that the pages are going to be
4287 * immediately released. In this case, we can then skip copying
4288 * back the contents from the GPU.
4289 */
4290
Chris Wilsona4f5ea62016-10-28 13:58:35 +01004291 if (obj->mm.madv != I915_MADV_WILLNEED)
Chris Wilson340fbd82014-05-22 09:16:52 +01004292 return false;
4293
4294 if (obj->base.filp == NULL)
4295 return true;
4296
4297 /* At first glance, this looks racy, but then again so would be
4298 * userspace racing mmap against close. However, the first external
4299 * reference to the filp can only be obtained through the
4300 * i915_gem_mmap_ioctl() which safeguards us against the user
4301 * acquiring such a reference whilst we are in the middle of
4302 * freeing the object.
4303 */
4304 return atomic_long_read(&obj->base.filp->f_count) == 1;
4305}
4306
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004307static void __i915_gem_free_objects(struct drm_i915_private *i915,
4308 struct llist_node *freed)
Chris Wilsonbe726152010-07-23 23:18:50 +01004309{
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004310 struct drm_i915_gem_object *obj, *on;
Chris Wilson538ef962019-01-14 14:21:18 +00004311 intel_wakeref_t wakeref;
Chris Wilsonbe726152010-07-23 23:18:50 +01004312
Chris Wilson538ef962019-01-14 14:21:18 +00004313 wakeref = intel_runtime_pm_get(i915);
Chris Wilsoncc731f52017-10-13 21:26:21 +01004314 llist_for_each_entry_safe(obj, on, freed, freed) {
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004315 struct i915_vma *vma, *vn;
Paulo Zanonif65c9162013-11-27 18:20:34 -02004316
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004317 trace_i915_gem_object_destroy(obj);
4318
Chris Wilsoncc731f52017-10-13 21:26:21 +01004319 mutex_lock(&i915->drm.struct_mutex);
4320
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004321 GEM_BUG_ON(i915_gem_object_is_active(obj));
4322 list_for_each_entry_safe(vma, vn,
4323 &obj->vma_list, obj_link) {
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004324 GEM_BUG_ON(i915_vma_is_active(vma));
4325 vma->flags &= ~I915_VMA_PIN_MASK;
Chris Wilson3365e222018-05-03 20:51:14 +01004326 i915_vma_destroy(vma);
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004327 }
Chris Wilsondb6c2b42016-11-01 11:54:00 +00004328 GEM_BUG_ON(!list_empty(&obj->vma_list));
4329 GEM_BUG_ON(!RB_EMPTY_ROOT(&obj->vma_tree));
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004330
Chris Wilsonf2123812017-10-16 12:40:37 +01004331 /* This serializes freeing with the shrinker. Since the free
4332 * is delayed, first by RCU then by the workqueue, we want the
4333 * shrinker to be able to free pages of unreferenced objects,
4334 * or else we may oom whilst there are plenty of deferred
4335 * freed objects.
4336 */
4337 if (i915_gem_object_has_pages(obj)) {
4338 spin_lock(&i915->mm.obj_lock);
4339 list_del_init(&obj->mm.link);
4340 spin_unlock(&i915->mm.obj_lock);
4341 }
4342
Chris Wilsoncc731f52017-10-13 21:26:21 +01004343 mutex_unlock(&i915->drm.struct_mutex);
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004344
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004345 GEM_BUG_ON(obj->bind_count);
Chris Wilsona65adaf2017-10-09 09:43:57 +01004346 GEM_BUG_ON(obj->userfault_count);
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004347 GEM_BUG_ON(atomic_read(&obj->frontbuffer_bits));
Chris Wilson67b48042017-08-22 12:05:16 +01004348 GEM_BUG_ON(!list_empty(&obj->lut_list));
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004349
4350 if (obj->ops->release)
4351 obj->ops->release(obj);
4352
4353 if (WARN_ON(i915_gem_object_has_pinned_pages(obj)))
4354 atomic_set(&obj->mm.pages_pin_count, 0);
Chris Wilson548625e2016-11-01 12:11:34 +00004355 __i915_gem_object_put_pages(obj, I915_MM_NORMAL);
Chris Wilsonf1fa4f42017-10-13 21:26:13 +01004356 GEM_BUG_ON(i915_gem_object_has_pages(obj));
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004357
4358 if (obj->base.import_attach)
4359 drm_prime_gem_destroy(&obj->base, NULL);
4360
Chris Wilsond07f0e52016-10-28 13:58:44 +01004361 reservation_object_fini(&obj->__builtin_resv);
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004362 drm_gem_object_release(&obj->base);
4363 i915_gem_info_remove_obj(i915, obj->base.size);
4364
4365 kfree(obj->bit_17);
4366 i915_gem_object_free(obj);
Chris Wilsoncc731f52017-10-13 21:26:21 +01004367
Chris Wilsonc9c704712018-02-19 22:06:31 +00004368 GEM_BUG_ON(!atomic_read(&i915->mm.free_count));
4369 atomic_dec(&i915->mm.free_count);
4370
Chris Wilsoncc731f52017-10-13 21:26:21 +01004371 if (on)
4372 cond_resched();
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004373 }
Chris Wilson538ef962019-01-14 14:21:18 +00004374 intel_runtime_pm_put(i915, wakeref);
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004375}
4376
4377static void i915_gem_flush_free_objects(struct drm_i915_private *i915)
4378{
4379 struct llist_node *freed;
4380
Chris Wilson87701b42017-10-13 21:26:20 +01004381 /* Free the oldest, most stale object to keep the free_list short */
4382 freed = NULL;
4383 if (!llist_empty(&i915->mm.free_list)) { /* quick test for hotpath */
4384 /* Only one consumer of llist_del_first() allowed */
4385 spin_lock(&i915->mm.free_lock);
4386 freed = llist_del_first(&i915->mm.free_list);
4387 spin_unlock(&i915->mm.free_lock);
4388 }
4389 if (unlikely(freed)) {
4390 freed->next = NULL;
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004391 __i915_gem_free_objects(i915, freed);
Chris Wilson87701b42017-10-13 21:26:20 +01004392 }
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004393}
4394
4395static void __i915_gem_free_work(struct work_struct *work)
4396{
4397 struct drm_i915_private *i915 =
4398 container_of(work, struct drm_i915_private, mm.free_work);
4399 struct llist_node *freed;
Chris Wilson26e12f82011-03-20 11:20:19 +00004400
Chris Wilson2ef1e722018-01-15 20:57:59 +00004401 /*
4402 * All file-owned VMA should have been released by this point through
Chris Wilsonb1f788c2016-08-04 07:52:45 +01004403 * i915_gem_close_object(), or earlier by i915_gem_context_close().
4404 * However, the object may also be bound into the global GTT (e.g.
4405 * older GPUs without per-process support, or for direct access through
4406 * the GTT either for the user or for scanout). Those VMA still need to
4407 * unbound now.
4408 */
Chris Wilson1488fc02012-04-24 15:47:31 +01004409
Chris Wilsonf991c492017-11-06 11:15:08 +00004410 spin_lock(&i915->mm.free_lock);
Chris Wilson5ad08be2017-04-07 11:25:51 +01004411 while ((freed = llist_del_all(&i915->mm.free_list))) {
Chris Wilsonf991c492017-11-06 11:15:08 +00004412 spin_unlock(&i915->mm.free_lock);
4413
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004414 __i915_gem_free_objects(i915, freed);
Chris Wilson5ad08be2017-04-07 11:25:51 +01004415 if (need_resched())
Chris Wilsonf991c492017-11-06 11:15:08 +00004416 return;
4417
4418 spin_lock(&i915->mm.free_lock);
Chris Wilson5ad08be2017-04-07 11:25:51 +01004419 }
Chris Wilsonf991c492017-11-06 11:15:08 +00004420 spin_unlock(&i915->mm.free_lock);
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004421}
4422
4423static void __i915_gem_free_object_rcu(struct rcu_head *head)
4424{
4425 struct drm_i915_gem_object *obj =
4426 container_of(head, typeof(*obj), rcu);
4427 struct drm_i915_private *i915 = to_i915(obj->base.dev);
4428
Chris Wilson2ef1e722018-01-15 20:57:59 +00004429 /*
Chris Wilson8811d612018-11-09 09:03:11 +00004430 * We reuse obj->rcu for the freed list, so we had better not treat
4431 * it like a rcu_head from this point forwards. And we expect all
4432 * objects to be freed via this path.
4433 */
4434 destroy_rcu_head(&obj->rcu);
4435
4436 /*
Chris Wilson2ef1e722018-01-15 20:57:59 +00004437 * Since we require blocking on struct_mutex to unbind the freed
4438 * object from the GPU before releasing resources back to the
4439 * system, we can not do that directly from the RCU callback (which may
4440 * be a softirq context), but must instead then defer that work onto a
4441 * kthread. We use the RCU callback rather than move the freed object
4442 * directly onto the work queue so that we can mix between using the
4443 * worker and performing frees directly from subsequent allocations for
4444 * crude but effective memory throttling.
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004445 */
4446 if (llist_add(&obj->freed, &i915->mm.free_list))
Chris Wilsonbeacbd12018-01-15 12:28:45 +00004447 queue_work(i915->wq, &i915->mm.free_work);
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004448}
4449
4450void i915_gem_free_object(struct drm_gem_object *gem_obj)
4451{
4452 struct drm_i915_gem_object *obj = to_intel_bo(gem_obj);
4453
Chris Wilsonbc0629a2016-11-01 10:03:17 +00004454 if (obj->mm.quirked)
4455 __i915_gem_object_unpin_pages(obj);
4456
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004457 if (discard_backing_storage(obj))
4458 obj->mm.madv = I915_MADV_DONTNEED;
Daniel Vettera071fa02014-06-18 23:28:09 +02004459
Chris Wilson2ef1e722018-01-15 20:57:59 +00004460 /*
4461 * Before we free the object, make sure any pure RCU-only
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004462 * read-side critical sections are complete, e.g.
4463 * i915_gem_busy_ioctl(). For the corresponding synchronized
4464 * lookup see i915_gem_object_lookup_rcu().
4465 */
Chris Wilsonc9c704712018-02-19 22:06:31 +00004466 atomic_inc(&to_i915(obj->base.dev)->mm.free_count);
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004467 call_rcu(&obj->rcu, __i915_gem_free_object_rcu);
Chris Wilsonbe726152010-07-23 23:18:50 +01004468}
4469
Chris Wilsonf8a7fde2016-10-28 13:58:29 +01004470void __i915_gem_object_release_unless_active(struct drm_i915_gem_object *obj)
4471{
4472 lockdep_assert_held(&obj->base.dev->struct_mutex);
4473
Chris Wilsond1b48c12017-08-16 09:52:08 +01004474 if (!i915_gem_object_has_active_reference(obj) &&
4475 i915_gem_object_is_active(obj))
Chris Wilsonf8a7fde2016-10-28 13:58:29 +01004476 i915_gem_object_set_active_reference(obj);
4477 else
4478 i915_gem_object_put(obj);
4479}
4480
Chris Wilson24145512017-01-24 11:01:35 +00004481void i915_gem_sanitize(struct drm_i915_private *i915)
4482{
Chris Wilson538ef962019-01-14 14:21:18 +00004483 intel_wakeref_t wakeref;
4484
Chris Wilsonc3160da2018-05-31 09:22:45 +01004485 GEM_TRACE("\n");
4486
Chris Wilson538ef962019-01-14 14:21:18 +00004487 wakeref = intel_runtime_pm_get(i915);
Chris Wilsonc3160da2018-05-31 09:22:45 +01004488 intel_uncore_forcewake_get(i915, FORCEWAKE_ALL);
4489
4490 /*
4491 * As we have just resumed the machine and woken the device up from
4492 * deep PCI sleep (presumably D3_cold), assume the HW has been reset
4493 * back to defaults, recovering from whatever wedged state we left it
4494 * in and so worth trying to use the device once more.
4495 */
Chris Wilson4dfacb02018-05-31 09:22:43 +01004496 if (i915_terminally_wedged(&i915->gpu_error))
Chris Wilsonf36325f2017-08-26 12:09:34 +01004497 i915_gem_unset_wedged(i915);
Chris Wilsonf36325f2017-08-26 12:09:34 +01004498
Chris Wilson24145512017-01-24 11:01:35 +00004499 /*
4500 * If we inherit context state from the BIOS or earlier occupants
4501 * of the GPU, the GPU may be in an inconsistent state when we
4502 * try to take over. The only way to remove the earlier state
4503 * is by resetting. However, resetting on earlier gen is tricky as
4504 * it may impact the display and we are uncertain about the stability
Joonas Lahtinenea117b82017-04-28 10:53:38 +03004505 * of the reset, so this could be applied to even earlier gen.
Chris Wilson24145512017-01-24 11:01:35 +00004506 */
Chris Wilson55277e12019-01-03 11:21:04 +00004507 intel_engines_sanitize(i915, false);
Chris Wilsonc3160da2018-05-31 09:22:45 +01004508
4509 intel_uncore_forcewake_put(i915, FORCEWAKE_ALL);
Chris Wilson538ef962019-01-14 14:21:18 +00004510 intel_runtime_pm_put(i915, wakeref);
Chris Wilsonc3160da2018-05-31 09:22:45 +01004511
Chris Wilsoneb8d0f52019-01-25 13:22:28 +00004512 mutex_lock(&i915->drm.struct_mutex);
Chris Wilson4dfacb02018-05-31 09:22:43 +01004513 i915_gem_contexts_lost(i915);
4514 mutex_unlock(&i915->drm.struct_mutex);
Chris Wilson24145512017-01-24 11:01:35 +00004515}
4516
Chris Wilsonbf061122018-07-09 14:02:04 +01004517int i915_gem_suspend(struct drm_i915_private *i915)
Eric Anholt673a3942008-07-30 12:06:12 -07004518{
Chris Wilson538ef962019-01-14 14:21:18 +00004519 intel_wakeref_t wakeref;
Chris Wilsondcff85c2016-08-05 10:14:11 +01004520 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004521
Chris Wilson09a4c022018-05-24 09:11:35 +01004522 GEM_TRACE("\n");
4523
Chris Wilson538ef962019-01-14 14:21:18 +00004524 wakeref = intel_runtime_pm_get(i915);
Chris Wilsonbf061122018-07-09 14:02:04 +01004525 intel_suspend_gt_powersave(i915);
Chris Wilson54b4f682016-07-21 21:16:19 +01004526
Chris Wilsoneb8d0f52019-01-25 13:22:28 +00004527 flush_workqueue(i915->wq);
4528
Chris Wilsonbf061122018-07-09 14:02:04 +01004529 mutex_lock(&i915->drm.struct_mutex);
Chris Wilson5ab57c72016-07-15 14:56:20 +01004530
Chris Wilsonbf061122018-07-09 14:02:04 +01004531 /*
4532 * We have to flush all the executing contexts to main memory so
Chris Wilson5ab57c72016-07-15 14:56:20 +01004533 * that they can saved in the hibernation image. To ensure the last
4534 * context image is coherent, we have to switch away from it. That
Chris Wilsonbf061122018-07-09 14:02:04 +01004535 * leaves the i915->kernel_context still active when
Chris Wilson5ab57c72016-07-15 14:56:20 +01004536 * we actually suspend, and its image in memory may not match the GPU
4537 * state. Fortunately, the kernel_context is disposable and we do
4538 * not rely on its state.
4539 */
Chris Wilsonbf061122018-07-09 14:02:04 +01004540 if (!i915_terminally_wedged(&i915->gpu_error)) {
4541 ret = i915_gem_switch_to_kernel_context(i915);
Chris Wilsonecf73eb2017-11-30 10:29:51 +00004542 if (ret)
4543 goto err_unlock;
Chris Wilson5ab57c72016-07-15 14:56:20 +01004544
Chris Wilsonbf061122018-07-09 14:02:04 +01004545 ret = i915_gem_wait_for_idle(i915,
Chris Wilsonecf73eb2017-11-30 10:29:51 +00004546 I915_WAIT_INTERRUPTIBLE |
Chris Wilson06060352018-05-31 09:22:44 +01004547 I915_WAIT_LOCKED |
Chris Wilsonec625fb2018-07-09 13:20:42 +01004548 I915_WAIT_FOR_IDLE_BOOST,
4549 MAX_SCHEDULE_TIMEOUT);
Chris Wilsonecf73eb2017-11-30 10:29:51 +00004550 if (ret && ret != -EIO)
4551 goto err_unlock;
Chris Wilsonf7403342013-09-13 23:57:04 +01004552
Chris Wilsonbf061122018-07-09 14:02:04 +01004553 assert_kernel_context_is_current(i915);
Chris Wilsonecf73eb2017-11-30 10:29:51 +00004554 }
Chris Wilson01f8f332018-07-17 09:41:21 +01004555 i915_retire_requests(i915); /* ensure we flush after wedging */
4556
Chris Wilsonbf061122018-07-09 14:02:04 +01004557 mutex_unlock(&i915->drm.struct_mutex);
Chris Wilsoneb8d0f52019-01-25 13:22:28 +00004558 i915_reset_flush(i915);
Chris Wilson45c5f202013-10-16 11:50:01 +01004559
Chris Wilsoneb8d0f52019-01-25 13:22:28 +00004560 drain_delayed_work(&i915->gt.retire_work);
Chris Wilsonbdeb9782016-12-23 14:57:56 +00004561
Chris Wilsonbf061122018-07-09 14:02:04 +01004562 /*
4563 * As the idle_work is rearming if it detects a race, play safe and
Chris Wilsonbdeb9782016-12-23 14:57:56 +00004564 * repeat the flush until it is definitely idle.
4565 */
Chris Wilsonbf061122018-07-09 14:02:04 +01004566 drain_delayed_work(&i915->gt.idle_work);
Chris Wilsonbdeb9782016-12-23 14:57:56 +00004567
Chris Wilsoneb8d0f52019-01-25 13:22:28 +00004568 intel_uc_suspend(i915);
4569
Chris Wilsonbf061122018-07-09 14:02:04 +01004570 /*
4571 * Assert that we successfully flushed all the work and
Chris Wilsonbdcf1202014-11-25 11:56:33 +00004572 * reset the GPU back to its idle, low power state.
4573 */
Chris Wilsonbf061122018-07-09 14:02:04 +01004574 WARN_ON(i915->gt.awake);
4575 if (WARN_ON(!intel_engines_are_idle(i915)))
4576 i915_gem_set_wedged(i915); /* no hope, discard everything */
Chris Wilsonbdcf1202014-11-25 11:56:33 +00004577
Chris Wilson538ef962019-01-14 14:21:18 +00004578 intel_runtime_pm_put(i915, wakeref);
Chris Wilsonec92ad02018-05-31 09:22:46 +01004579 return 0;
4580
4581err_unlock:
Chris Wilsonbf061122018-07-09 14:02:04 +01004582 mutex_unlock(&i915->drm.struct_mutex);
Chris Wilson538ef962019-01-14 14:21:18 +00004583 intel_runtime_pm_put(i915, wakeref);
Chris Wilsonec92ad02018-05-31 09:22:46 +01004584 return ret;
4585}
4586
4587void i915_gem_suspend_late(struct drm_i915_private *i915)
4588{
Chris Wilson9776f472018-06-01 15:41:24 +01004589 struct drm_i915_gem_object *obj;
4590 struct list_head *phases[] = {
4591 &i915->mm.unbound_list,
4592 &i915->mm.bound_list,
4593 NULL
4594 }, **phase;
4595
Imre Deak1c777c52016-10-12 17:46:37 +03004596 /*
4597 * Neither the BIOS, ourselves or any other kernel
4598 * expects the system to be in execlists mode on startup,
4599 * so we need to reset the GPU back to legacy mode. And the only
4600 * known way to disable logical contexts is through a GPU reset.
4601 *
4602 * So in order to leave the system in a known default configuration,
4603 * always reset the GPU upon unload and suspend. Afterwards we then
4604 * clean up the GEM state tracking, flushing off the requests and
4605 * leaving the system in a known idle state.
4606 *
4607 * Note that is of the upmost importance that the GPU is idle and
4608 * all stray writes are flushed *before* we dismantle the backing
4609 * storage for the pinned objects.
4610 *
4611 * However, since we are uncertain that resetting the GPU on older
4612 * machines is a good idea, we don't - just in case it leaves the
4613 * machine in an unusable condition.
4614 */
Chris Wilsoncad99462017-08-26 12:09:33 +01004615
Chris Wilson9776f472018-06-01 15:41:24 +01004616 mutex_lock(&i915->drm.struct_mutex);
4617 for (phase = phases; *phase; phase++) {
4618 list_for_each_entry(obj, *phase, mm.link)
4619 WARN_ON(i915_gem_object_set_to_gtt_domain(obj, false));
4620 }
4621 mutex_unlock(&i915->drm.struct_mutex);
4622
Chris Wilsonec92ad02018-05-31 09:22:46 +01004623 intel_uc_sanitize(i915);
4624 i915_gem_sanitize(i915);
Eric Anholt673a3942008-07-30 12:06:12 -07004625}
4626
Chris Wilson37cd3302017-11-12 11:27:38 +00004627void i915_gem_resume(struct drm_i915_private *i915)
Chris Wilson5ab57c72016-07-15 14:56:20 +01004628{
Chris Wilson4dfacb02018-05-31 09:22:43 +01004629 GEM_TRACE("\n");
4630
Chris Wilson37cd3302017-11-12 11:27:38 +00004631 WARN_ON(i915->gt.awake);
Chris Wilson5ab57c72016-07-15 14:56:20 +01004632
Chris Wilson37cd3302017-11-12 11:27:38 +00004633 mutex_lock(&i915->drm.struct_mutex);
4634 intel_uncore_forcewake_get(i915, FORCEWAKE_ALL);
Imre Deak31ab49a2016-11-07 11:20:05 +02004635
Chris Wilson37cd3302017-11-12 11:27:38 +00004636 i915_gem_restore_gtt_mappings(i915);
4637 i915_gem_restore_fences(i915);
Chris Wilson5ab57c72016-07-15 14:56:20 +01004638
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00004639 /*
4640 * As we didn't flush the kernel context before suspend, we cannot
Chris Wilson5ab57c72016-07-15 14:56:20 +01004641 * guarantee that the context image is complete. So let's just reset
4642 * it and start again.
4643 */
Chris Wilson37cd3302017-11-12 11:27:38 +00004644 i915->gt.resume(i915);
Chris Wilson5ab57c72016-07-15 14:56:20 +01004645
Chris Wilson37cd3302017-11-12 11:27:38 +00004646 if (i915_gem_init_hw(i915))
4647 goto err_wedged;
4648
Michal Wajdeczko7cfca4a2018-03-02 11:15:49 +00004649 intel_uc_resume(i915);
Chris Wilson7469c622017-11-14 13:03:00 +00004650
Chris Wilson37cd3302017-11-12 11:27:38 +00004651 /* Always reload a context for powersaving. */
4652 if (i915_gem_switch_to_kernel_context(i915))
4653 goto err_wedged;
4654
4655out_unlock:
4656 intel_uncore_forcewake_put(i915, FORCEWAKE_ALL);
4657 mutex_unlock(&i915->drm.struct_mutex);
4658 return;
4659
4660err_wedged:
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00004661 if (!i915_terminally_wedged(&i915->gpu_error)) {
4662 DRM_ERROR("failed to re-initialize GPU, declaring wedged!\n");
4663 i915_gem_set_wedged(i915);
4664 }
Chris Wilson37cd3302017-11-12 11:27:38 +00004665 goto out_unlock;
Chris Wilson5ab57c72016-07-15 14:56:20 +01004666}
4667
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00004668void i915_gem_init_swizzling(struct drm_i915_private *dev_priv)
Daniel Vetterf691e2f2012-02-02 09:58:12 +01004669{
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00004670 if (INTEL_GEN(dev_priv) < 5 ||
Daniel Vetterf691e2f2012-02-02 09:58:12 +01004671 dev_priv->mm.bit_6_swizzle_x == I915_BIT_6_SWIZZLE_NONE)
4672 return;
4673
4674 I915_WRITE(DISP_ARB_CTL, I915_READ(DISP_ARB_CTL) |
4675 DISP_TILE_SURFACE_SWIZZLING);
4676
Lucas De Marchicf819ef2018-12-12 10:10:43 -08004677 if (IS_GEN(dev_priv, 5))
Daniel Vetter11782b02012-01-31 16:47:55 +01004678 return;
4679
Daniel Vetterf691e2f2012-02-02 09:58:12 +01004680 I915_WRITE(TILECTL, I915_READ(TILECTL) | TILECTL_SWZCTL);
Lucas De Marchicf819ef2018-12-12 10:10:43 -08004681 if (IS_GEN(dev_priv, 6))
Daniel Vetter6b26c862012-04-24 14:04:12 +02004682 I915_WRITE(ARB_MODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_SNB));
Lucas De Marchicf819ef2018-12-12 10:10:43 -08004683 else if (IS_GEN(dev_priv, 7))
Daniel Vetter6b26c862012-04-24 14:04:12 +02004684 I915_WRITE(ARB_MODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_IVB));
Lucas De Marchicf819ef2018-12-12 10:10:43 -08004685 else if (IS_GEN(dev_priv, 8))
Ben Widawsky31a53362013-11-02 21:07:04 -07004686 I915_WRITE(GAMTARBMODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_BDW));
Ben Widawsky8782e262012-12-18 10:31:23 -08004687 else
4688 BUG();
Daniel Vetterf691e2f2012-02-02 09:58:12 +01004689}
Daniel Vettere21af882012-02-09 20:53:27 +01004690
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01004691static void init_unused_ring(struct drm_i915_private *dev_priv, u32 base)
Ville Syrjälä81e7f202014-08-15 01:21:55 +03004692{
Ville Syrjälä81e7f202014-08-15 01:21:55 +03004693 I915_WRITE(RING_CTL(base), 0);
4694 I915_WRITE(RING_HEAD(base), 0);
4695 I915_WRITE(RING_TAIL(base), 0);
4696 I915_WRITE(RING_START(base), 0);
4697}
4698
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01004699static void init_unused_rings(struct drm_i915_private *dev_priv)
Ville Syrjälä81e7f202014-08-15 01:21:55 +03004700{
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01004701 if (IS_I830(dev_priv)) {
4702 init_unused_ring(dev_priv, PRB1_BASE);
4703 init_unused_ring(dev_priv, SRB0_BASE);
4704 init_unused_ring(dev_priv, SRB1_BASE);
4705 init_unused_ring(dev_priv, SRB2_BASE);
4706 init_unused_ring(dev_priv, SRB3_BASE);
Lucas De Marchicf819ef2018-12-12 10:10:43 -08004707 } else if (IS_GEN(dev_priv, 2)) {
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01004708 init_unused_ring(dev_priv, SRB0_BASE);
4709 init_unused_ring(dev_priv, SRB1_BASE);
Lucas De Marchicf819ef2018-12-12 10:10:43 -08004710 } else if (IS_GEN(dev_priv, 3)) {
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01004711 init_unused_ring(dev_priv, PRB1_BASE);
4712 init_unused_ring(dev_priv, PRB2_BASE);
Ville Syrjälä81e7f202014-08-15 01:21:55 +03004713 }
4714}
4715
Chris Wilson20a8a742017-02-08 14:30:31 +00004716static int __i915_gem_restart_engines(void *data)
Ben Widawsky4fc7c972013-02-08 11:49:24 -08004717{
Chris Wilson20a8a742017-02-08 14:30:31 +00004718 struct drm_i915_private *i915 = data;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00004719 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05304720 enum intel_engine_id id;
Chris Wilson20a8a742017-02-08 14:30:31 +00004721 int err;
4722
4723 for_each_engine(engine, i915, id) {
4724 err = engine->init_hw(engine);
Chris Wilson8177e112018-02-07 11:15:45 +00004725 if (err) {
4726 DRM_ERROR("Failed to restart %s (%d)\n",
4727 engine->name, err);
Chris Wilson20a8a742017-02-08 14:30:31 +00004728 return err;
Chris Wilson8177e112018-02-07 11:15:45 +00004729 }
Chris Wilson20a8a742017-02-08 14:30:31 +00004730 }
4731
4732 return 0;
4733}
4734
4735int i915_gem_init_hw(struct drm_i915_private *dev_priv)
4736{
Chris Wilsond200cda2016-04-28 09:56:44 +01004737 int ret;
Ben Widawsky4fc7c972013-02-08 11:49:24 -08004738
Chris Wilsonde867c22016-10-25 13:16:02 +01004739 dev_priv->gt.last_init_time = ktime_get();
4740
Chris Wilson5e4f5182015-02-13 14:35:59 +00004741 /* Double layer security blanket, see i915_gem_init() */
4742 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
4743
Tvrtko Ursulin0031fb92016-11-04 14:42:44 +00004744 if (HAS_EDRAM(dev_priv) && INTEL_GEN(dev_priv) < 9)
Ben Widawsky05e21cc2013-07-04 11:02:04 -07004745 I915_WRITE(HSW_IDICR, I915_READ(HSW_IDICR) | IDIHASHMSK(0xf));
Ben Widawsky4fc7c972013-02-08 11:49:24 -08004746
Tvrtko Ursulin772c2a52016-10-13 11:03:01 +01004747 if (IS_HASWELL(dev_priv))
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01004748 I915_WRITE(MI_PREDICATE_RESULT_2, IS_HSW_GT3(dev_priv) ?
Ville Syrjälä0bf21342013-11-29 14:56:12 +02004749 LOWER_SLICE_ENABLED : LOWER_SLICE_DISABLED);
Rodrigo Vivi94353732013-08-28 16:45:46 -03004750
Tvrtko Ursulin094304b2018-12-03 12:50:10 +00004751 /* Apply the GT workarounds... */
Tvrtko Ursulin25d140f2018-12-03 13:33:19 +00004752 intel_gt_apply_workarounds(dev_priv);
Tvrtko Ursulin094304b2018-12-03 12:50:10 +00004753 /* ...and determine whether they are sticking. */
4754 intel_gt_verify_workarounds(dev_priv, "init");
Oscar Mateo59b449d2018-04-10 09:12:47 -07004755
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00004756 i915_gem_init_swizzling(dev_priv);
Ben Widawsky4fc7c972013-02-08 11:49:24 -08004757
Daniel Vetterd5abdfd2014-11-20 09:45:19 +01004758 /*
4759 * At least 830 can leave some of the unused rings
4760 * "active" (ie. head != tail) after resume which
4761 * will prevent c3 entry. Makes sure all unused rings
4762 * are totally idle.
4763 */
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01004764 init_unused_rings(dev_priv);
Daniel Vetterd5abdfd2014-11-20 09:45:19 +01004765
Dave Gordoned54c1a2016-01-19 19:02:54 +00004766 BUG_ON(!dev_priv->kernel_context);
Chris Wilson6f74b362017-10-15 15:37:25 +01004767 if (i915_terminally_wedged(&dev_priv->gpu_error)) {
4768 ret = -EIO;
4769 goto out;
4770 }
John Harrison90638cc2015-05-29 17:43:37 +01004771
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00004772 ret = i915_ppgtt_init_hw(dev_priv);
John Harrison4ad2fd82015-06-18 13:11:20 +01004773 if (ret) {
Chris Wilson8177e112018-02-07 11:15:45 +00004774 DRM_ERROR("Enabling PPGTT failed (%d)\n", ret);
John Harrison4ad2fd82015-06-18 13:11:20 +01004775 goto out;
4776 }
4777
Jackie Lif08e2032018-03-13 17:32:53 -07004778 ret = intel_wopcm_init_hw(&dev_priv->wopcm);
4779 if (ret) {
4780 DRM_ERROR("Enabling WOPCM failed (%d)\n", ret);
4781 goto out;
4782 }
4783
Michał Winiarski9bdc3572017-10-25 18:25:19 +01004784 /* We can't enable contexts until all firmware is loaded */
4785 ret = intel_uc_init_hw(dev_priv);
Chris Wilson8177e112018-02-07 11:15:45 +00004786 if (ret) {
4787 DRM_ERROR("Enabling uc failed (%d)\n", ret);
Michał Winiarski9bdc3572017-10-25 18:25:19 +01004788 goto out;
Chris Wilson8177e112018-02-07 11:15:45 +00004789 }
Michał Winiarski9bdc3572017-10-25 18:25:19 +01004790
Tvrtko Ursulinbf9e8422016-12-01 14:16:38 +00004791 intel_mocs_init_l3cc_table(dev_priv);
Peter Antoine0ccdacf2016-04-13 15:03:25 +01004792
Chris Wilson136109c2017-11-02 13:14:30 +00004793 /* Only when the HW is re-initialised, can we replay the requests */
4794 ret = __i915_gem_restart_engines(dev_priv);
Michal Wajdeczkob96f6eb2018-06-05 12:24:43 +00004795 if (ret)
4796 goto cleanup_uc;
Michał Winiarski60c0a662018-07-12 14:48:10 +02004797
Chris Wilson5e4f5182015-02-13 14:35:59 +00004798 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Michał Winiarski60c0a662018-07-12 14:48:10 +02004799
4800 return 0;
Michal Wajdeczkob96f6eb2018-06-05 12:24:43 +00004801
4802cleanup_uc:
4803 intel_uc_fini_hw(dev_priv);
Michał Winiarski60c0a662018-07-12 14:48:10 +02004804out:
4805 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
4806
4807 return ret;
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004808}
4809
Chris Wilsond2b4b972017-11-10 14:26:33 +00004810static int __intel_engines_record_defaults(struct drm_i915_private *i915)
4811{
4812 struct i915_gem_context *ctx;
4813 struct intel_engine_cs *engine;
4814 enum intel_engine_id id;
4815 int err;
4816
4817 /*
4818 * As we reset the gpu during very early sanitisation, the current
4819 * register state on the GPU should reflect its defaults values.
4820 * We load a context onto the hw (with restore-inhibit), then switch
4821 * over to a second context to save that default register state. We
4822 * can then prime every new context with that state so they all start
4823 * from the same default HW values.
4824 */
4825
4826 ctx = i915_gem_context_create_kernel(i915, 0);
4827 if (IS_ERR(ctx))
4828 return PTR_ERR(ctx);
4829
4830 for_each_engine(engine, i915, id) {
Chris Wilsone61e0f52018-02-21 09:56:36 +00004831 struct i915_request *rq;
Chris Wilsond2b4b972017-11-10 14:26:33 +00004832
Chris Wilsone61e0f52018-02-21 09:56:36 +00004833 rq = i915_request_alloc(engine, ctx);
Chris Wilsond2b4b972017-11-10 14:26:33 +00004834 if (IS_ERR(rq)) {
4835 err = PTR_ERR(rq);
4836 goto out_ctx;
4837 }
4838
Chris Wilson3fef5cd2017-11-20 10:20:02 +00004839 err = 0;
Chris Wilsond2b4b972017-11-10 14:26:33 +00004840 if (engine->init_context)
4841 err = engine->init_context(rq);
4842
Chris Wilson697b9a82018-06-12 11:51:35 +01004843 i915_request_add(rq);
Chris Wilsond2b4b972017-11-10 14:26:33 +00004844 if (err)
4845 goto err_active;
4846 }
4847
4848 err = i915_gem_switch_to_kernel_context(i915);
4849 if (err)
4850 goto err_active;
4851
Chris Wilson2621cef2018-07-09 13:20:43 +01004852 if (i915_gem_wait_for_idle(i915, I915_WAIT_LOCKED, HZ / 5)) {
4853 i915_gem_set_wedged(i915);
4854 err = -EIO; /* Caller will declare us wedged */
Chris Wilsond2b4b972017-11-10 14:26:33 +00004855 goto err_active;
Chris Wilson2621cef2018-07-09 13:20:43 +01004856 }
Chris Wilsond2b4b972017-11-10 14:26:33 +00004857
4858 assert_kernel_context_is_current(i915);
4859
Chris Wilson8e1cb322018-09-20 17:13:43 +01004860 /*
4861 * Immediately park the GPU so that we enable powersaving and
4862 * treat it as idle. The next time we issue a request, we will
4863 * unpark and start using the engine->pinned_default_state, otherwise
4864 * it is in limbo and an early reset may fail.
4865 */
4866 __i915_gem_park(i915);
4867
Chris Wilsond2b4b972017-11-10 14:26:33 +00004868 for_each_engine(engine, i915, id) {
4869 struct i915_vma *state;
Chris Wilson37d7c9c2018-09-14 13:35:03 +01004870 void *vaddr;
Chris Wilsond2b4b972017-11-10 14:26:33 +00004871
Chris Wilson666424a2018-09-14 13:35:04 +01004872 GEM_BUG_ON(to_intel_context(ctx, engine)->pin_count);
4873
Chris Wilsonab82a062018-04-30 14:15:01 +01004874 state = to_intel_context(ctx, engine)->state;
Chris Wilsond2b4b972017-11-10 14:26:33 +00004875 if (!state)
4876 continue;
4877
4878 /*
4879 * As we will hold a reference to the logical state, it will
4880 * not be torn down with the context, and importantly the
4881 * object will hold onto its vma (making it possible for a
4882 * stray GTT write to corrupt our defaults). Unmap the vma
4883 * from the GTT to prevent such accidents and reclaim the
4884 * space.
4885 */
4886 err = i915_vma_unbind(state);
4887 if (err)
4888 goto err_active;
4889
4890 err = i915_gem_object_set_to_cpu_domain(state->obj, false);
4891 if (err)
4892 goto err_active;
4893
4894 engine->default_state = i915_gem_object_get(state->obj);
Chris Wilson37d7c9c2018-09-14 13:35:03 +01004895
4896 /* Check we can acquire the image of the context state */
4897 vaddr = i915_gem_object_pin_map(engine->default_state,
Chris Wilson666424a2018-09-14 13:35:04 +01004898 I915_MAP_FORCE_WB);
Chris Wilson37d7c9c2018-09-14 13:35:03 +01004899 if (IS_ERR(vaddr)) {
4900 err = PTR_ERR(vaddr);
4901 goto err_active;
4902 }
4903
4904 i915_gem_object_unpin_map(engine->default_state);
Chris Wilsond2b4b972017-11-10 14:26:33 +00004905 }
4906
4907 if (IS_ENABLED(CONFIG_DRM_I915_DEBUG_GEM)) {
4908 unsigned int found = intel_engines_has_context_isolation(i915);
4909
4910 /*
4911 * Make sure that classes with multiple engine instances all
4912 * share the same basic configuration.
4913 */
4914 for_each_engine(engine, i915, id) {
4915 unsigned int bit = BIT(engine->uabi_class);
4916 unsigned int expected = engine->default_state ? bit : 0;
4917
4918 if ((found & bit) != expected) {
4919 DRM_ERROR("mismatching default context state for class %d on engine %s\n",
4920 engine->uabi_class, engine->name);
4921 }
4922 }
4923 }
4924
4925out_ctx:
4926 i915_gem_context_set_closed(ctx);
4927 i915_gem_context_put(ctx);
4928 return err;
4929
4930err_active:
4931 /*
4932 * If we have to abandon now, we expect the engines to be idle
4933 * and ready to be torn-down. First try to flush any remaining
4934 * request, ensure we are pointing at the kernel context and
4935 * then remove it.
4936 */
4937 if (WARN_ON(i915_gem_switch_to_kernel_context(i915)))
4938 goto out_ctx;
4939
Chris Wilsonec625fb2018-07-09 13:20:42 +01004940 if (WARN_ON(i915_gem_wait_for_idle(i915,
4941 I915_WAIT_LOCKED,
4942 MAX_SCHEDULE_TIMEOUT)))
Chris Wilsond2b4b972017-11-10 14:26:33 +00004943 goto out_ctx;
4944
4945 i915_gem_contexts_lost(i915);
4946 goto out_ctx;
4947}
4948
Chris Wilson51797492018-12-04 14:15:16 +00004949static int
4950i915_gem_init_scratch(struct drm_i915_private *i915, unsigned int size)
4951{
4952 struct drm_i915_gem_object *obj;
4953 struct i915_vma *vma;
4954 int ret;
4955
4956 obj = i915_gem_object_create_stolen(i915, size);
4957 if (!obj)
4958 obj = i915_gem_object_create_internal(i915, size);
4959 if (IS_ERR(obj)) {
4960 DRM_ERROR("Failed to allocate scratch page\n");
4961 return PTR_ERR(obj);
4962 }
4963
4964 vma = i915_vma_instance(obj, &i915->ggtt.vm, NULL);
4965 if (IS_ERR(vma)) {
4966 ret = PTR_ERR(vma);
4967 goto err_unref;
4968 }
4969
4970 ret = i915_vma_pin(vma, 0, 0, PIN_GLOBAL | PIN_HIGH);
4971 if (ret)
4972 goto err_unref;
4973
4974 i915->gt.scratch = vma;
4975 return 0;
4976
4977err_unref:
4978 i915_gem_object_put(obj);
4979 return ret;
4980}
4981
4982static void i915_gem_fini_scratch(struct drm_i915_private *i915)
4983{
4984 i915_vma_unpin_and_release(&i915->gt.scratch, 0);
4985}
4986
Tvrtko Ursulinbf9e8422016-12-01 14:16:38 +00004987int i915_gem_init(struct drm_i915_private *dev_priv)
Chris Wilson1070a422012-04-24 15:47:41 +01004988{
Chris Wilson1070a422012-04-24 15:47:41 +01004989 int ret;
4990
Changbin Du52b24162018-05-08 17:07:05 +08004991 /* We need to fallback to 4K pages if host doesn't support huge gtt. */
4992 if (intel_vgpu_active(dev_priv) && !intel_vgpu_has_huge_gtt(dev_priv))
Matthew Auldda9fe3f32017-10-06 23:18:31 +01004993 mkwrite_device_info(dev_priv)->page_sizes =
4994 I915_GTT_PAGE_SIZE_4K;
4995
Chris Wilson94312822017-05-03 10:39:18 +01004996 dev_priv->mm.unordered_timeline = dma_fence_context_alloc(1);
Chris Wilson57822dc2017-02-22 11:40:48 +00004997
Chris Wilsonfb5c5512017-11-20 20:55:00 +00004998 if (HAS_LOGICAL_RING_CONTEXTS(dev_priv)) {
Chris Wilson821ed7d2016-09-09 14:11:53 +01004999 dev_priv->gt.resume = intel_lr_context_resume;
Tvrtko Ursulin117897f2016-03-16 11:00:40 +00005000 dev_priv->gt.cleanup_engine = intel_logical_ring_cleanup;
Chris Wilsonfb5c5512017-11-20 20:55:00 +00005001 } else {
5002 dev_priv->gt.resume = intel_legacy_submission_resume;
5003 dev_priv->gt.cleanup_engine = intel_engine_cleanup;
Oscar Mateoa83014d2014-07-24 17:04:21 +01005004 }
5005
Chris Wilsonee487002017-11-22 17:26:21 +00005006 ret = i915_gem_init_userptr(dev_priv);
5007 if (ret)
5008 return ret;
5009
Sagar Arun Kamble70deead2018-01-24 21:16:58 +05305010 ret = intel_uc_init_misc(dev_priv);
Michał Winiarski3176ff42017-12-13 23:13:47 +01005011 if (ret)
5012 return ret;
5013
Michal Wajdeczkof7dc0152018-06-28 14:15:21 +00005014 ret = intel_wopcm_init(&dev_priv->wopcm);
5015 if (ret)
5016 goto err_uc_misc;
5017
Chris Wilson5e4f5182015-02-13 14:35:59 +00005018 /* This is just a security blanket to placate dragons.
5019 * On some systems, we very sporadically observe that the first TLBs
5020 * used by the CS may be stale, despite us poking the TLB reset. If
5021 * we hold the forcewake during initialisation these problems
5022 * just magically go away.
5023 */
Chris Wilsonee487002017-11-22 17:26:21 +00005024 mutex_lock(&dev_priv->drm.struct_mutex);
Chris Wilson5e4f5182015-02-13 14:35:59 +00005025 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
5026
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01005027 ret = i915_gem_init_ggtt(dev_priv);
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00005028 if (ret) {
5029 GEM_BUG_ON(ret == -EIO);
5030 goto err_unlock;
5031 }
Jesse Barnesd62b4892013-03-08 10:45:53 -08005032
Chris Wilson51797492018-12-04 14:15:16 +00005033 ret = i915_gem_init_scratch(dev_priv,
Lucas De Marchicf819ef2018-12-12 10:10:43 -08005034 IS_GEN(dev_priv, 2) ? SZ_256K : PAGE_SIZE);
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00005035 if (ret) {
5036 GEM_BUG_ON(ret == -EIO);
5037 goto err_ggtt;
5038 }
Ben Widawsky2fa48d82013-12-06 14:11:04 -08005039
Chris Wilson51797492018-12-04 14:15:16 +00005040 ret = i915_gem_contexts_init(dev_priv);
5041 if (ret) {
5042 GEM_BUG_ON(ret == -EIO);
5043 goto err_scratch;
5044 }
5045
Tvrtko Ursulinbf9e8422016-12-01 14:16:38 +00005046 ret = intel_engines_init(dev_priv);
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00005047 if (ret) {
5048 GEM_BUG_ON(ret == -EIO);
5049 goto err_context;
5050 }
Daniel Vetter53ca26c2012-04-26 23:28:03 +02005051
Chris Wilsonf58d13d2017-11-10 14:26:29 +00005052 intel_init_gt_powersave(dev_priv);
5053
Michał Winiarski61b5c152017-12-13 23:13:48 +01005054 ret = intel_uc_init(dev_priv);
Chris Wilsoncc6a8182017-11-10 14:26:30 +00005055 if (ret)
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00005056 goto err_pm;
Chris Wilsoncc6a8182017-11-10 14:26:30 +00005057
Michał Winiarski61b5c152017-12-13 23:13:48 +01005058 ret = i915_gem_init_hw(dev_priv);
5059 if (ret)
5060 goto err_uc_init;
5061
Chris Wilsoncc6a8182017-11-10 14:26:30 +00005062 /*
5063 * Despite its name intel_init_clock_gating applies both display
5064 * clock gating workarounds; GT mmio workarounds and the occasional
5065 * GT power context workaround. Worse, sometimes it includes a context
5066 * register workaround which we need to apply before we record the
5067 * default HW state for all contexts.
5068 *
5069 * FIXME: break up the workarounds and apply them at the right time!
5070 */
5071 intel_init_clock_gating(dev_priv);
5072
Chris Wilsond2b4b972017-11-10 14:26:33 +00005073 ret = __intel_engines_record_defaults(dev_priv);
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00005074 if (ret)
5075 goto err_init_hw;
5076
5077 if (i915_inject_load_failure()) {
5078 ret = -ENODEV;
5079 goto err_init_hw;
5080 }
5081
5082 if (i915_inject_load_failure()) {
5083 ret = -EIO;
5084 goto err_init_hw;
5085 }
5086
5087 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
5088 mutex_unlock(&dev_priv->drm.struct_mutex);
5089
5090 return 0;
5091
5092 /*
5093 * Unwinding is complicated by that we want to handle -EIO to mean
5094 * disable GPU submission but keep KMS alive. We want to mark the
5095 * HW as irrevisibly wedged, but keep enough state around that the
5096 * driver doesn't explode during runtime.
5097 */
5098err_init_hw:
Chris Wilson8571a052018-06-06 15:54:41 +01005099 mutex_unlock(&dev_priv->drm.struct_mutex);
5100
5101 WARN_ON(i915_gem_suspend(dev_priv));
5102 i915_gem_suspend_late(dev_priv);
5103
Chris Wilson8bcf9f72018-07-10 10:44:20 +01005104 i915_gem_drain_workqueue(dev_priv);
5105
Chris Wilson8571a052018-06-06 15:54:41 +01005106 mutex_lock(&dev_priv->drm.struct_mutex);
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00005107 intel_uc_fini_hw(dev_priv);
Michał Winiarski61b5c152017-12-13 23:13:48 +01005108err_uc_init:
5109 intel_uc_fini(dev_priv);
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00005110err_pm:
5111 if (ret != -EIO) {
5112 intel_cleanup_gt_powersave(dev_priv);
5113 i915_gem_cleanup_engines(dev_priv);
5114 }
5115err_context:
5116 if (ret != -EIO)
5117 i915_gem_contexts_fini(dev_priv);
Chris Wilson51797492018-12-04 14:15:16 +00005118err_scratch:
5119 i915_gem_fini_scratch(dev_priv);
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00005120err_ggtt:
5121err_unlock:
5122 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
5123 mutex_unlock(&dev_priv->drm.struct_mutex);
5124
Michal Wajdeczkof7dc0152018-06-28 14:15:21 +00005125err_uc_misc:
Sagar Arun Kamble70deead2018-01-24 21:16:58 +05305126 intel_uc_fini_misc(dev_priv);
Sagar Arun Kambleda943b52018-01-10 18:24:16 +05305127
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00005128 if (ret != -EIO)
5129 i915_gem_cleanup_userptr(dev_priv);
5130
Chris Wilson60990322014-04-09 09:19:42 +01005131 if (ret == -EIO) {
Chris Wilson7ed43df2018-07-26 09:50:32 +01005132 mutex_lock(&dev_priv->drm.struct_mutex);
5133
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00005134 /*
5135 * Allow engine initialisation to fail by marking the GPU as
Chris Wilson60990322014-04-09 09:19:42 +01005136 * wedged. But we only want to do this where the GPU is angry,
5137 * for all other failure, such as an allocation failure, bail.
5138 */
Chris Wilson6f74b362017-10-15 15:37:25 +01005139 if (!i915_terminally_wedged(&dev_priv->gpu_error)) {
Chris Wilson51c18bf2018-06-09 12:10:58 +01005140 i915_load_error(dev_priv,
5141 "Failed to initialize GPU, declaring it wedged!\n");
Chris Wilson6f74b362017-10-15 15:37:25 +01005142 i915_gem_set_wedged(dev_priv);
5143 }
Chris Wilson7ed43df2018-07-26 09:50:32 +01005144
5145 /* Minimal basic recovery for KMS */
5146 ret = i915_ggtt_enable_hw(dev_priv);
5147 i915_gem_restore_gtt_mappings(dev_priv);
5148 i915_gem_restore_fences(dev_priv);
5149 intel_init_clock_gating(dev_priv);
5150
5151 mutex_unlock(&dev_priv->drm.struct_mutex);
Chris Wilson1070a422012-04-24 15:47:41 +01005152 }
5153
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00005154 i915_gem_drain_freed_objects(dev_priv);
Chris Wilson60990322014-04-09 09:19:42 +01005155 return ret;
Chris Wilson1070a422012-04-24 15:47:41 +01005156}
5157
Michal Wajdeczko8979187a2018-06-04 09:00:32 +00005158void i915_gem_fini(struct drm_i915_private *dev_priv)
5159{
5160 i915_gem_suspend_late(dev_priv);
Chris Wilson30b710842018-08-12 23:36:29 +01005161 intel_disable_gt_powersave(dev_priv);
Michal Wajdeczko8979187a2018-06-04 09:00:32 +00005162
5163 /* Flush any outstanding unpin_work. */
5164 i915_gem_drain_workqueue(dev_priv);
5165
5166 mutex_lock(&dev_priv->drm.struct_mutex);
5167 intel_uc_fini_hw(dev_priv);
5168 intel_uc_fini(dev_priv);
5169 i915_gem_cleanup_engines(dev_priv);
5170 i915_gem_contexts_fini(dev_priv);
Chris Wilson51797492018-12-04 14:15:16 +00005171 i915_gem_fini_scratch(dev_priv);
Michal Wajdeczko8979187a2018-06-04 09:00:32 +00005172 mutex_unlock(&dev_priv->drm.struct_mutex);
5173
Tvrtko Ursulin25d140f2018-12-03 13:33:19 +00005174 intel_wa_list_free(&dev_priv->gt_wa_list);
5175
Chris Wilson30b710842018-08-12 23:36:29 +01005176 intel_cleanup_gt_powersave(dev_priv);
5177
Michal Wajdeczko8979187a2018-06-04 09:00:32 +00005178 intel_uc_fini_misc(dev_priv);
5179 i915_gem_cleanup_userptr(dev_priv);
5180
5181 i915_gem_drain_freed_objects(dev_priv);
5182
5183 WARN_ON(!list_empty(&dev_priv->contexts.list));
5184}
5185
Chris Wilson24145512017-01-24 11:01:35 +00005186void i915_gem_init_mmio(struct drm_i915_private *i915)
5187{
5188 i915_gem_sanitize(i915);
5189}
5190
Zou Nan hai8187a2b2010-05-21 09:08:55 +08005191void
Tvrtko Ursulincb15d9f2016-12-01 14:16:39 +00005192i915_gem_cleanup_engines(struct drm_i915_private *dev_priv)
Zou Nan hai8187a2b2010-05-21 09:08:55 +08005193{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00005194 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05305195 enum intel_engine_id id;
Zou Nan hai8187a2b2010-05-21 09:08:55 +08005196
Akash Goel3b3f1652016-10-13 22:44:48 +05305197 for_each_engine(engine, dev_priv, id)
Tvrtko Ursulin117897f2016-03-16 11:00:40 +00005198 dev_priv->gt.cleanup_engine(engine);
Zou Nan hai8187a2b2010-05-21 09:08:55 +08005199}
5200
Eric Anholt673a3942008-07-30 12:06:12 -07005201void
Imre Deak40ae4e12016-03-16 14:54:03 +02005202i915_gem_load_init_fences(struct drm_i915_private *dev_priv)
5203{
Chris Wilson49ef5292016-08-18 17:17:00 +01005204 int i;
Imre Deak40ae4e12016-03-16 14:54:03 +02005205
Tvrtko Ursulinc56b89f2018-02-09 21:58:46 +00005206 if (INTEL_GEN(dev_priv) >= 7 && !IS_VALLEYVIEW(dev_priv) &&
Imre Deak40ae4e12016-03-16 14:54:03 +02005207 !IS_CHERRYVIEW(dev_priv))
5208 dev_priv->num_fence_regs = 32;
Tvrtko Ursulinc56b89f2018-02-09 21:58:46 +00005209 else if (INTEL_GEN(dev_priv) >= 4 ||
Jani Nikula73f67aa2016-12-07 22:48:09 +02005210 IS_I945G(dev_priv) || IS_I945GM(dev_priv) ||
5211 IS_G33(dev_priv) || IS_PINEVIEW(dev_priv))
Imre Deak40ae4e12016-03-16 14:54:03 +02005212 dev_priv->num_fence_regs = 16;
5213 else
5214 dev_priv->num_fence_regs = 8;
5215
Chris Wilsonc0336662016-05-06 15:40:21 +01005216 if (intel_vgpu_active(dev_priv))
Imre Deak40ae4e12016-03-16 14:54:03 +02005217 dev_priv->num_fence_regs =
5218 I915_READ(vgtif_reg(avail_rs.fence_num));
5219
5220 /* Initialize fence registers to zero */
Chris Wilson49ef5292016-08-18 17:17:00 +01005221 for (i = 0; i < dev_priv->num_fence_regs; i++) {
5222 struct drm_i915_fence_reg *fence = &dev_priv->fence_regs[i];
5223
5224 fence->i915 = dev_priv;
5225 fence->id = i;
5226 list_add_tail(&fence->link, &dev_priv->mm.fence_list);
5227 }
Tvrtko Ursulin4362f4f2016-11-16 08:55:33 +00005228 i915_gem_restore_fences(dev_priv);
Imre Deak40ae4e12016-03-16 14:54:03 +02005229
Tvrtko Ursulin4362f4f2016-11-16 08:55:33 +00005230 i915_gem_detect_bit_6_swizzle(dev_priv);
Imre Deak40ae4e12016-03-16 14:54:03 +02005231}
5232
Chris Wilson9c52d1c2017-11-10 23:24:47 +00005233static void i915_gem_init__mm(struct drm_i915_private *i915)
5234{
5235 spin_lock_init(&i915->mm.object_stat_lock);
5236 spin_lock_init(&i915->mm.obj_lock);
5237 spin_lock_init(&i915->mm.free_lock);
5238
5239 init_llist_head(&i915->mm.free_list);
5240
5241 INIT_LIST_HEAD(&i915->mm.unbound_list);
5242 INIT_LIST_HEAD(&i915->mm.bound_list);
5243 INIT_LIST_HEAD(&i915->mm.fence_list);
5244 INIT_LIST_HEAD(&i915->mm.userfault_list);
5245
5246 INIT_WORK(&i915->mm.free_work, __i915_gem_free_work);
5247}
5248
Michal Wajdeczkoa0de9082018-03-23 12:34:49 +00005249int i915_gem_init_early(struct drm_i915_private *dev_priv)
Eric Anholt673a3942008-07-30 12:06:12 -07005250{
Tvrtko Ursulina9335682016-11-02 15:14:59 +00005251 int err = -ENOMEM;
Chris Wilson42dcedd2012-11-15 11:32:30 +00005252
Tvrtko Ursulina9335682016-11-02 15:14:59 +00005253 dev_priv->objects = KMEM_CACHE(drm_i915_gem_object, SLAB_HWCACHE_ALIGN);
5254 if (!dev_priv->objects)
Chris Wilson73cb9702016-10-28 13:58:46 +01005255 goto err_out;
Chris Wilson73cb9702016-10-28 13:58:46 +01005256
Tvrtko Ursulina9335682016-11-02 15:14:59 +00005257 dev_priv->vmas = KMEM_CACHE(i915_vma, SLAB_HWCACHE_ALIGN);
5258 if (!dev_priv->vmas)
Chris Wilson73cb9702016-10-28 13:58:46 +01005259 goto err_objects;
Chris Wilson73cb9702016-10-28 13:58:46 +01005260
Chris Wilsond1b48c12017-08-16 09:52:08 +01005261 dev_priv->luts = KMEM_CACHE(i915_lut_handle, 0);
5262 if (!dev_priv->luts)
5263 goto err_vmas;
5264
Chris Wilsone61e0f52018-02-21 09:56:36 +00005265 dev_priv->requests = KMEM_CACHE(i915_request,
Tvrtko Ursulina9335682016-11-02 15:14:59 +00005266 SLAB_HWCACHE_ALIGN |
5267 SLAB_RECLAIM_ACCOUNT |
Paul E. McKenney5f0d5a32017-01-18 02:53:44 -08005268 SLAB_TYPESAFE_BY_RCU);
Tvrtko Ursulina9335682016-11-02 15:14:59 +00005269 if (!dev_priv->requests)
Chris Wilsond1b48c12017-08-16 09:52:08 +01005270 goto err_luts;
Chris Wilson73cb9702016-10-28 13:58:46 +01005271
Chris Wilson52e54202016-11-14 20:41:02 +00005272 dev_priv->dependencies = KMEM_CACHE(i915_dependency,
5273 SLAB_HWCACHE_ALIGN |
5274 SLAB_RECLAIM_ACCOUNT);
5275 if (!dev_priv->dependencies)
5276 goto err_requests;
5277
Chris Wilsonc5cf9a92017-05-17 13:10:04 +01005278 dev_priv->priorities = KMEM_CACHE(i915_priolist, SLAB_HWCACHE_ALIGN);
5279 if (!dev_priv->priorities)
5280 goto err_dependencies;
5281
Chris Wilson73cb9702016-10-28 13:58:46 +01005282 INIT_LIST_HEAD(&dev_priv->gt.timelines);
Chris Wilson643b4502018-04-30 14:15:03 +01005283 INIT_LIST_HEAD(&dev_priv->gt.active_rings);
Chris Wilson3365e222018-05-03 20:51:14 +01005284 INIT_LIST_HEAD(&dev_priv->gt.closed_vma);
Chris Wilson643b4502018-04-30 14:15:03 +01005285
Chris Wilson9c52d1c2017-11-10 23:24:47 +00005286 i915_gem_init__mm(dev_priv);
Chris Wilsonf2123812017-10-16 12:40:37 +01005287
Chris Wilson67d97da2016-07-04 08:08:31 +01005288 INIT_DELAYED_WORK(&dev_priv->gt.retire_work,
Eric Anholt673a3942008-07-30 12:06:12 -07005289 i915_gem_retire_work_handler);
Chris Wilson67d97da2016-07-04 08:08:31 +01005290 INIT_DELAYED_WORK(&dev_priv->gt.idle_work,
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005291 i915_gem_idle_work_handler);
Chris Wilson1f15b762016-07-01 17:23:14 +01005292 init_waitqueue_head(&dev_priv->gpu_error.wait_queue);
Daniel Vetter1f83fee2012-11-15 17:17:22 +01005293 init_waitqueue_head(&dev_priv->gpu_error.reset_queue);
Chris Wilson18bb2bc2019-01-14 21:04:01 +00005294 mutex_init(&dev_priv->gpu_error.wedge_mutex);
Chris Wilson31169712009-09-14 16:50:28 +01005295
Joonas Lahtinen6f633402016-09-01 14:58:21 +03005296 atomic_set(&dev_priv->mm.bsd_engine_dispatch_index, 0);
5297
Chris Wilsonb5add952016-08-04 16:32:36 +01005298 spin_lock_init(&dev_priv->fb_tracking.lock);
Chris Wilson73cb9702016-10-28 13:58:46 +01005299
Matthew Auld465c4032017-10-06 23:18:14 +01005300 err = i915_gemfs_init(dev_priv);
5301 if (err)
5302 DRM_NOTE("Unable to create a private tmpfs mount, hugepage support will be disabled(%d).\n", err);
5303
Chris Wilson73cb9702016-10-28 13:58:46 +01005304 return 0;
5305
Chris Wilson52e54202016-11-14 20:41:02 +00005306err_dependencies:
5307 kmem_cache_destroy(dev_priv->dependencies);
Chris Wilson73cb9702016-10-28 13:58:46 +01005308err_requests:
5309 kmem_cache_destroy(dev_priv->requests);
Chris Wilsond1b48c12017-08-16 09:52:08 +01005310err_luts:
5311 kmem_cache_destroy(dev_priv->luts);
Chris Wilson73cb9702016-10-28 13:58:46 +01005312err_vmas:
5313 kmem_cache_destroy(dev_priv->vmas);
5314err_objects:
5315 kmem_cache_destroy(dev_priv->objects);
5316err_out:
5317 return err;
Eric Anholt673a3942008-07-30 12:06:12 -07005318}
Dave Airlie71acb5e2008-12-30 20:31:46 +10005319
Michal Wajdeczkoa0de9082018-03-23 12:34:49 +00005320void i915_gem_cleanup_early(struct drm_i915_private *dev_priv)
Imre Deakd64aa092016-01-19 15:26:29 +02005321{
Chris Wilsonc4d4c1c2017-02-10 16:35:23 +00005322 i915_gem_drain_freed_objects(dev_priv);
Chris Wilsonc9c704712018-02-19 22:06:31 +00005323 GEM_BUG_ON(!llist_empty(&dev_priv->mm.free_list));
5324 GEM_BUG_ON(atomic_read(&dev_priv->mm.free_count));
Chris Wilsonc4d4c1c2017-02-10 16:35:23 +00005325 WARN_ON(dev_priv->mm.object_count);
Matthew Auldea84aa72016-11-17 21:04:11 +00005326 WARN_ON(!list_empty(&dev_priv->gt.timelines));
Matthew Auldea84aa72016-11-17 21:04:11 +00005327
Chris Wilsonc5cf9a92017-05-17 13:10:04 +01005328 kmem_cache_destroy(dev_priv->priorities);
Chris Wilson52e54202016-11-14 20:41:02 +00005329 kmem_cache_destroy(dev_priv->dependencies);
Imre Deakd64aa092016-01-19 15:26:29 +02005330 kmem_cache_destroy(dev_priv->requests);
Chris Wilsond1b48c12017-08-16 09:52:08 +01005331 kmem_cache_destroy(dev_priv->luts);
Imre Deakd64aa092016-01-19 15:26:29 +02005332 kmem_cache_destroy(dev_priv->vmas);
5333 kmem_cache_destroy(dev_priv->objects);
Chris Wilson0eafec62016-08-04 16:32:41 +01005334
5335 /* And ensure that our DESTROY_BY_RCU slabs are truly destroyed */
5336 rcu_barrier();
Matthew Auld465c4032017-10-06 23:18:14 +01005337
5338 i915_gemfs_fini(dev_priv);
Imre Deakd64aa092016-01-19 15:26:29 +02005339}
5340
Chris Wilson6a800ea2016-09-21 14:51:07 +01005341int i915_gem_freeze(struct drm_i915_private *dev_priv)
5342{
Chris Wilsond0aa3012017-04-07 11:25:49 +01005343 /* Discard all purgeable objects, let userspace recover those as
5344 * required after resuming.
5345 */
Chris Wilson6a800ea2016-09-21 14:51:07 +01005346 i915_gem_shrink_all(dev_priv);
Chris Wilson6a800ea2016-09-21 14:51:07 +01005347
Chris Wilson6a800ea2016-09-21 14:51:07 +01005348 return 0;
5349}
5350
Chris Wilson95c778d2018-06-01 15:41:25 +01005351int i915_gem_freeze_late(struct drm_i915_private *i915)
Chris Wilson461fb992016-05-14 07:26:33 +01005352{
5353 struct drm_i915_gem_object *obj;
Chris Wilson7aab2d52016-09-09 20:02:18 +01005354 struct list_head *phases[] = {
Chris Wilson95c778d2018-06-01 15:41:25 +01005355 &i915->mm.unbound_list,
5356 &i915->mm.bound_list,
Chris Wilson7aab2d52016-09-09 20:02:18 +01005357 NULL
Chris Wilson95c778d2018-06-01 15:41:25 +01005358 }, **phase;
Chris Wilson461fb992016-05-14 07:26:33 +01005359
Chris Wilson95c778d2018-06-01 15:41:25 +01005360 /*
5361 * Called just before we write the hibernation image.
Chris Wilson461fb992016-05-14 07:26:33 +01005362 *
5363 * We need to update the domain tracking to reflect that the CPU
5364 * will be accessing all the pages to create and restore from the
5365 * hibernation, and so upon restoration those pages will be in the
5366 * CPU domain.
5367 *
5368 * To make sure the hibernation image contains the latest state,
5369 * we update that state just before writing out the image.
Chris Wilson7aab2d52016-09-09 20:02:18 +01005370 *
5371 * To try and reduce the hibernation image, we manually shrink
Chris Wilsond0aa3012017-04-07 11:25:49 +01005372 * the objects as well, see i915_gem_freeze()
Chris Wilson461fb992016-05-14 07:26:33 +01005373 */
5374
Chris Wilson95c778d2018-06-01 15:41:25 +01005375 i915_gem_shrink(i915, -1UL, NULL, I915_SHRINK_UNBOUND);
5376 i915_gem_drain_freed_objects(i915);
Chris Wilson461fb992016-05-14 07:26:33 +01005377
Chris Wilson95c778d2018-06-01 15:41:25 +01005378 mutex_lock(&i915->drm.struct_mutex);
5379 for (phase = phases; *phase; phase++) {
5380 list_for_each_entry(obj, *phase, mm.link)
5381 WARN_ON(i915_gem_object_set_to_cpu_domain(obj, true));
Chris Wilson461fb992016-05-14 07:26:33 +01005382 }
Chris Wilson95c778d2018-06-01 15:41:25 +01005383 mutex_unlock(&i915->drm.struct_mutex);
Chris Wilson461fb992016-05-14 07:26:33 +01005384
5385 return 0;
5386}
5387
Chris Wilsonf787a5f2010-09-24 16:02:42 +01005388void i915_gem_release(struct drm_device *dev, struct drm_file *file)
Eric Anholtb9624422009-06-03 07:27:35 +00005389{
Chris Wilsonf787a5f2010-09-24 16:02:42 +01005390 struct drm_i915_file_private *file_priv = file->driver_priv;
Chris Wilsone61e0f52018-02-21 09:56:36 +00005391 struct i915_request *request;
Eric Anholtb9624422009-06-03 07:27:35 +00005392
5393 /* Clean up our request list when the client is going away, so that
5394 * later retire_requests won't dereference our soon-to-be-gone
5395 * file_priv.
5396 */
Chris Wilson1c255952010-09-26 11:03:27 +01005397 spin_lock(&file_priv->mm.lock);
Chris Wilsonc8659ef2017-03-02 12:25:25 +00005398 list_for_each_entry(request, &file_priv->mm.request_list, client_link)
Chris Wilsonf787a5f2010-09-24 16:02:42 +01005399 request->file_priv = NULL;
Chris Wilson1c255952010-09-26 11:03:27 +01005400 spin_unlock(&file_priv->mm.lock);
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005401}
5402
Chris Wilson829a0af2017-06-20 12:05:45 +01005403int i915_gem_open(struct drm_i915_private *i915, struct drm_file *file)
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005404{
5405 struct drm_i915_file_private *file_priv;
Ben Widawskye422b882013-12-06 14:10:58 -08005406 int ret;
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005407
Chris Wilsonc4c29d72016-11-09 10:45:07 +00005408 DRM_DEBUG("\n");
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005409
5410 file_priv = kzalloc(sizeof(*file_priv), GFP_KERNEL);
5411 if (!file_priv)
5412 return -ENOMEM;
5413
5414 file->driver_priv = file_priv;
Chris Wilson829a0af2017-06-20 12:05:45 +01005415 file_priv->dev_priv = i915;
Chris Wilsonab0e7ff2014-02-25 17:11:24 +02005416 file_priv->file = file;
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005417
5418 spin_lock_init(&file_priv->mm.lock);
5419 INIT_LIST_HEAD(&file_priv->mm.request_list);
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005420
Chris Wilsonc80ff162016-07-27 09:07:27 +01005421 file_priv->bsd_engine = -1;
Mika Kuoppala14921f32018-06-15 13:44:29 +03005422 file_priv->hang_timestamp = jiffies;
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00005423
Chris Wilson829a0af2017-06-20 12:05:45 +01005424 ret = i915_gem_context_open(i915, file);
Ben Widawskye422b882013-12-06 14:10:58 -08005425 if (ret)
5426 kfree(file_priv);
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005427
Ben Widawskye422b882013-12-06 14:10:58 -08005428 return ret;
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005429}
5430
Daniel Vetterb680c372014-09-19 18:27:27 +02005431/**
5432 * i915_gem_track_fb - update frontbuffer tracking
Geliang Tangd9072a32015-09-15 05:58:44 -07005433 * @old: current GEM buffer for the frontbuffer slots
5434 * @new: new GEM buffer for the frontbuffer slots
5435 * @frontbuffer_bits: bitmask of frontbuffer slots
Daniel Vetterb680c372014-09-19 18:27:27 +02005436 *
5437 * This updates the frontbuffer tracking bits @frontbuffer_bits by clearing them
5438 * from @old and setting them in @new. Both @old and @new can be NULL.
5439 */
Daniel Vettera071fa02014-06-18 23:28:09 +02005440void i915_gem_track_fb(struct drm_i915_gem_object *old,
5441 struct drm_i915_gem_object *new,
5442 unsigned frontbuffer_bits)
5443{
Chris Wilsonfaf5bf02016-08-04 16:32:37 +01005444 /* Control of individual bits within the mask are guarded by
5445 * the owning plane->mutex, i.e. we can never see concurrent
5446 * manipulation of individual bits. But since the bitfield as a whole
5447 * is updated using RMW, we need to use atomics in order to update
5448 * the bits.
5449 */
5450 BUILD_BUG_ON(INTEL_FRONTBUFFER_BITS_PER_PIPE * I915_MAX_PIPES >
Chris Wilson74f6e182018-09-26 11:47:07 +01005451 BITS_PER_TYPE(atomic_t));
Chris Wilsonfaf5bf02016-08-04 16:32:37 +01005452
Daniel Vettera071fa02014-06-18 23:28:09 +02005453 if (old) {
Chris Wilsonfaf5bf02016-08-04 16:32:37 +01005454 WARN_ON(!(atomic_read(&old->frontbuffer_bits) & frontbuffer_bits));
5455 atomic_andnot(frontbuffer_bits, &old->frontbuffer_bits);
Daniel Vettera071fa02014-06-18 23:28:09 +02005456 }
5457
5458 if (new) {
Chris Wilsonfaf5bf02016-08-04 16:32:37 +01005459 WARN_ON(atomic_read(&new->frontbuffer_bits) & frontbuffer_bits);
5460 atomic_or(frontbuffer_bits, &new->frontbuffer_bits);
Daniel Vettera071fa02014-06-18 23:28:09 +02005461 }
5462}
5463
Dave Gordonea702992015-07-09 19:29:02 +01005464/* Allocate a new GEM object and fill it with the supplied data */
5465struct drm_i915_gem_object *
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +00005466i915_gem_object_create_from_data(struct drm_i915_private *dev_priv,
Dave Gordonea702992015-07-09 19:29:02 +01005467 const void *data, size_t size)
5468{
5469 struct drm_i915_gem_object *obj;
Chris Wilsonbe062fa2017-03-17 19:46:48 +00005470 struct file *file;
5471 size_t offset;
5472 int err;
Dave Gordonea702992015-07-09 19:29:02 +01005473
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +00005474 obj = i915_gem_object_create(dev_priv, round_up(size, PAGE_SIZE));
Chris Wilsonfe3db792016-04-25 13:32:13 +01005475 if (IS_ERR(obj))
Dave Gordonea702992015-07-09 19:29:02 +01005476 return obj;
5477
Christian Königc0a51fd2018-02-16 13:43:38 +01005478 GEM_BUG_ON(obj->write_domain != I915_GEM_DOMAIN_CPU);
Dave Gordonea702992015-07-09 19:29:02 +01005479
Chris Wilsonbe062fa2017-03-17 19:46:48 +00005480 file = obj->base.filp;
5481 offset = 0;
5482 do {
5483 unsigned int len = min_t(typeof(size), size, PAGE_SIZE);
5484 struct page *page;
5485 void *pgdata, *vaddr;
Dave Gordonea702992015-07-09 19:29:02 +01005486
Chris Wilsonbe062fa2017-03-17 19:46:48 +00005487 err = pagecache_write_begin(file, file->f_mapping,
5488 offset, len, 0,
5489 &page, &pgdata);
5490 if (err < 0)
5491 goto fail;
Dave Gordonea702992015-07-09 19:29:02 +01005492
Chris Wilsonbe062fa2017-03-17 19:46:48 +00005493 vaddr = kmap(page);
5494 memcpy(vaddr, data, len);
5495 kunmap(page);
5496
5497 err = pagecache_write_end(file, file->f_mapping,
5498 offset, len, len,
5499 page, pgdata);
5500 if (err < 0)
5501 goto fail;
5502
5503 size -= len;
5504 data += len;
5505 offset += len;
5506 } while (size);
Dave Gordonea702992015-07-09 19:29:02 +01005507
5508 return obj;
5509
5510fail:
Chris Wilsonf8c417c2016-07-20 13:31:53 +01005511 i915_gem_object_put(obj);
Chris Wilsonbe062fa2017-03-17 19:46:48 +00005512 return ERR_PTR(err);
Dave Gordonea702992015-07-09 19:29:02 +01005513}
Chris Wilson96d77632016-10-28 13:58:33 +01005514
5515struct scatterlist *
5516i915_gem_object_get_sg(struct drm_i915_gem_object *obj,
5517 unsigned int n,
5518 unsigned int *offset)
5519{
Chris Wilsona4f5ea62016-10-28 13:58:35 +01005520 struct i915_gem_object_page_iter *iter = &obj->mm.get_page;
Chris Wilson96d77632016-10-28 13:58:33 +01005521 struct scatterlist *sg;
5522 unsigned int idx, count;
5523
5524 might_sleep();
5525 GEM_BUG_ON(n >= obj->base.size >> PAGE_SHIFT);
Chris Wilsona4f5ea62016-10-28 13:58:35 +01005526 GEM_BUG_ON(!i915_gem_object_has_pinned_pages(obj));
Chris Wilson96d77632016-10-28 13:58:33 +01005527
5528 /* As we iterate forward through the sg, we record each entry in a
5529 * radixtree for quick repeated (backwards) lookups. If we have seen
5530 * this index previously, we will have an entry for it.
5531 *
5532 * Initial lookup is O(N), but this is amortized to O(1) for
5533 * sequential page access (where each new request is consecutive
5534 * to the previous one). Repeated lookups are O(lg(obj->base.size)),
5535 * i.e. O(1) with a large constant!
5536 */
5537 if (n < READ_ONCE(iter->sg_idx))
5538 goto lookup;
5539
5540 mutex_lock(&iter->lock);
5541
5542 /* We prefer to reuse the last sg so that repeated lookup of this
5543 * (or the subsequent) sg are fast - comparing against the last
5544 * sg is faster than going through the radixtree.
5545 */
5546
5547 sg = iter->sg_pos;
5548 idx = iter->sg_idx;
5549 count = __sg_page_count(sg);
5550
5551 while (idx + count <= n) {
Matthew Wilcox3159f942017-11-03 13:30:42 -04005552 void *entry;
5553 unsigned long i;
Chris Wilson96d77632016-10-28 13:58:33 +01005554 int ret;
5555
5556 /* If we cannot allocate and insert this entry, or the
5557 * individual pages from this range, cancel updating the
5558 * sg_idx so that on this lookup we are forced to linearly
5559 * scan onwards, but on future lookups we will try the
5560 * insertion again (in which case we need to be careful of
5561 * the error return reporting that we have already inserted
5562 * this index).
5563 */
5564 ret = radix_tree_insert(&iter->radix, idx, sg);
5565 if (ret && ret != -EEXIST)
5566 goto scan;
5567
Matthew Wilcox3159f942017-11-03 13:30:42 -04005568 entry = xa_mk_value(idx);
Chris Wilson96d77632016-10-28 13:58:33 +01005569 for (i = 1; i < count; i++) {
Matthew Wilcox3159f942017-11-03 13:30:42 -04005570 ret = radix_tree_insert(&iter->radix, idx + i, entry);
Chris Wilson96d77632016-10-28 13:58:33 +01005571 if (ret && ret != -EEXIST)
5572 goto scan;
5573 }
5574
5575 idx += count;
5576 sg = ____sg_next(sg);
5577 count = __sg_page_count(sg);
5578 }
5579
5580scan:
5581 iter->sg_pos = sg;
5582 iter->sg_idx = idx;
5583
5584 mutex_unlock(&iter->lock);
5585
5586 if (unlikely(n < idx)) /* insertion completed by another thread */
5587 goto lookup;
5588
5589 /* In case we failed to insert the entry into the radixtree, we need
5590 * to look beyond the current sg.
5591 */
5592 while (idx + count <= n) {
5593 idx += count;
5594 sg = ____sg_next(sg);
5595 count = __sg_page_count(sg);
5596 }
5597
5598 *offset = n - idx;
5599 return sg;
5600
5601lookup:
5602 rcu_read_lock();
5603
5604 sg = radix_tree_lookup(&iter->radix, n);
5605 GEM_BUG_ON(!sg);
5606
5607 /* If this index is in the middle of multi-page sg entry,
Matthew Wilcox3159f942017-11-03 13:30:42 -04005608 * the radix tree will contain a value entry that points
Chris Wilson96d77632016-10-28 13:58:33 +01005609 * to the start of that range. We will return the pointer to
5610 * the base page and the offset of this page within the
5611 * sg entry's range.
5612 */
5613 *offset = 0;
Matthew Wilcox3159f942017-11-03 13:30:42 -04005614 if (unlikely(xa_is_value(sg))) {
5615 unsigned long base = xa_to_value(sg);
Chris Wilson96d77632016-10-28 13:58:33 +01005616
5617 sg = radix_tree_lookup(&iter->radix, base);
5618 GEM_BUG_ON(!sg);
5619
5620 *offset = n - base;
5621 }
5622
5623 rcu_read_unlock();
5624
5625 return sg;
5626}
5627
5628struct page *
5629i915_gem_object_get_page(struct drm_i915_gem_object *obj, unsigned int n)
5630{
5631 struct scatterlist *sg;
5632 unsigned int offset;
5633
5634 GEM_BUG_ON(!i915_gem_object_has_struct_page(obj));
5635
5636 sg = i915_gem_object_get_sg(obj, n, &offset);
5637 return nth_page(sg_page(sg), offset);
5638}
5639
5640/* Like i915_gem_object_get_page(), but mark the returned page dirty */
5641struct page *
5642i915_gem_object_get_dirty_page(struct drm_i915_gem_object *obj,
5643 unsigned int n)
5644{
5645 struct page *page;
5646
5647 page = i915_gem_object_get_page(obj, n);
Chris Wilsona4f5ea62016-10-28 13:58:35 +01005648 if (!obj->mm.dirty)
Chris Wilson96d77632016-10-28 13:58:33 +01005649 set_page_dirty(page);
5650
5651 return page;
5652}
5653
5654dma_addr_t
5655i915_gem_object_get_dma_address(struct drm_i915_gem_object *obj,
5656 unsigned long n)
5657{
5658 struct scatterlist *sg;
5659 unsigned int offset;
5660
5661 sg = i915_gem_object_get_sg(obj, n, &offset);
5662 return sg_dma_address(sg) + (offset << PAGE_SHIFT);
5663}
Chris Wilson935a2f72017-02-13 17:15:13 +00005664
Chris Wilson8eeb7902017-07-26 19:16:01 +01005665int i915_gem_object_attach_phys(struct drm_i915_gem_object *obj, int align)
5666{
5667 struct sg_table *pages;
5668 int err;
5669
5670 if (align > obj->base.size)
5671 return -EINVAL;
5672
5673 if (obj->ops == &i915_gem_phys_ops)
5674 return 0;
5675
5676 if (obj->ops != &i915_gem_object_ops)
5677 return -EINVAL;
5678
5679 err = i915_gem_object_unbind(obj);
5680 if (err)
5681 return err;
5682
5683 mutex_lock(&obj->mm.lock);
5684
5685 if (obj->mm.madv != I915_MADV_WILLNEED) {
5686 err = -EFAULT;
5687 goto err_unlock;
5688 }
5689
5690 if (obj->mm.quirked) {
5691 err = -EFAULT;
5692 goto err_unlock;
5693 }
5694
5695 if (obj->mm.mapping) {
5696 err = -EBUSY;
5697 goto err_unlock;
5698 }
5699
Chris Wilsonacd1c1e2018-06-11 08:55:32 +01005700 pages = __i915_gem_object_unset_pages(obj);
Chris Wilsonf2123812017-10-16 12:40:37 +01005701
Chris Wilson8eeb7902017-07-26 19:16:01 +01005702 obj->ops = &i915_gem_phys_ops;
5703
Chris Wilson8fb6a5d2017-07-26 19:16:02 +01005704 err = ____i915_gem_object_get_pages(obj);
Chris Wilson8eeb7902017-07-26 19:16:01 +01005705 if (err)
5706 goto err_xfer;
5707
5708 /* Perma-pin (until release) the physical set of pages */
5709 __i915_gem_object_pin_pages(obj);
5710
5711 if (!IS_ERR_OR_NULL(pages))
5712 i915_gem_object_ops.put_pages(obj, pages);
5713 mutex_unlock(&obj->mm.lock);
5714 return 0;
5715
5716err_xfer:
5717 obj->ops = &i915_gem_object_ops;
Chris Wilsonacd1c1e2018-06-11 08:55:32 +01005718 if (!IS_ERR_OR_NULL(pages)) {
5719 unsigned int sg_page_sizes = i915_sg_page_sizes(pages->sgl);
5720
5721 __i915_gem_object_set_pages(obj, pages, sg_page_sizes);
5722 }
Chris Wilson8eeb7902017-07-26 19:16:01 +01005723err_unlock:
5724 mutex_unlock(&obj->mm.lock);
5725 return err;
5726}
5727
Chris Wilson935a2f72017-02-13 17:15:13 +00005728#if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
5729#include "selftests/scatterlist.c"
Chris Wilson66d9cb52017-02-13 17:15:17 +00005730#include "selftests/mock_gem_device.c"
Chris Wilson44653982017-02-13 17:15:20 +00005731#include "selftests/huge_gem_object.c"
Matthew Auld40498662017-10-06 23:18:29 +01005732#include "selftests/huge_pages.c"
Chris Wilson8335fd62017-02-13 17:15:28 +00005733#include "selftests/i915_gem_object.c"
Chris Wilson17059452017-02-13 17:15:32 +00005734#include "selftests/i915_gem_coherency.c"
Chris Wilson3f51b7e12018-08-30 14:48:06 +01005735#include "selftests/i915_gem.c"
Chris Wilson935a2f72017-02-13 17:15:13 +00005736#endif