blob: c3b4ec52e1b7a0af08deb31f9b9c6385a4e2a609 [file] [log] [blame]
Eric Anholt673a3942008-07-30 12:06:12 -07001/*
Daniel Vetterbe6a0372015-03-18 10:46:04 +01002 * Copyright © 2008-2015 Intel Corporation
Eric Anholt673a3942008-07-30 12:06:12 -07003 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 *
23 * Authors:
24 * Eric Anholt <eric@anholt.net>
25 *
26 */
27
David Herrmann0de23972013-07-24 21:07:52 +020028#include <drm/drm_vma_manager.h>
Daniel Vetterfcd70cd2019-01-17 22:03:34 +010029#include <drm/drm_pci.h>
David Howells760285e2012-10-02 18:01:07 +010030#include <drm/i915_drm.h>
Chris Wilson6b5e90f2016-11-14 20:41:05 +000031#include <linux/dma-fence-array.h>
Chris Wilsonfe3288b2017-02-12 17:20:01 +000032#include <linux/kthread.h>
Chris Wilsonc13d87e2016-07-20 09:21:15 +010033#include <linux/reservation.h>
Hugh Dickins5949eac2011-06-27 16:18:18 -070034#include <linux/shmem_fs.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090035#include <linux/slab.h>
Chris Wilson20e49332016-11-22 14:41:21 +000036#include <linux/stop_machine.h>
Eric Anholt673a3942008-07-30 12:06:12 -070037#include <linux/swap.h>
Jesse Barnes79e53942008-11-07 14:24:08 -080038#include <linux/pci.h>
Daniel Vetter1286ff72012-05-10 15:25:09 +020039#include <linux/dma-buf.h>
Daniel Vetterfcd70cd2019-01-17 22:03:34 +010040#include <linux/mman.h>
Eric Anholt673a3942008-07-30 12:06:12 -070041
Chris Wilson9f588922019-01-16 15:33:04 +000042#include "i915_drv.h"
43#include "i915_gem_clflush.h"
44#include "i915_gemfs.h"
Chris Wilson32eb6bc2019-02-28 10:20:33 +000045#include "i915_globals.h"
Chris Wilson9f588922019-01-16 15:33:04 +000046#include "i915_reset.h"
47#include "i915_trace.h"
48#include "i915_vgpu.h"
49
50#include "intel_drv.h"
51#include "intel_frontbuffer.h"
52#include "intel_mocs.h"
53#include "intel_workarounds.h"
54
Chris Wilsonfbbd37b2016-10-28 13:58:42 +010055static void i915_gem_flush_free_objects(struct drm_i915_private *i915);
Chris Wilson61050802012-04-17 15:31:31 +010056
Chris Wilson2c225692013-08-09 12:26:45 +010057static bool cpu_write_needs_clflush(struct drm_i915_gem_object *obj)
58{
Chris Wilsone27ab732017-06-15 13:38:49 +010059 if (obj->cache_dirty)
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +053060 return false;
61
Chris Wilsonb8f55be2017-08-11 12:11:16 +010062 if (!(obj->cache_coherent & I915_BO_CACHE_COHERENT_FOR_WRITE))
Chris Wilson2c225692013-08-09 12:26:45 +010063 return true;
64
Chris Wilsonbd3d2252017-10-13 21:26:14 +010065 return obj->pin_global; /* currently in use by HW, keep flushed */
Chris Wilson2c225692013-08-09 12:26:45 +010066}
67
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +053068static int
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +010069insert_mappable_node(struct i915_ggtt *ggtt,
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +053070 struct drm_mm_node *node, u32 size)
71{
72 memset(node, 0, sizeof(*node));
Chris Wilson82ad6442018-06-05 16:37:58 +010073 return drm_mm_insert_node_in_range(&ggtt->vm.mm, node,
Chris Wilson4e64e552017-02-02 21:04:38 +000074 size, 0, I915_COLOR_UNEVICTABLE,
75 0, ggtt->mappable_end,
76 DRM_MM_INSERT_LOW);
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +053077}
78
79static void
80remove_mappable_node(struct drm_mm_node *node)
81{
82 drm_mm_remove_node(node);
83}
84
Chris Wilson73aa8082010-09-30 11:46:12 +010085/* some bookkeeping */
86static void i915_gem_info_add_obj(struct drm_i915_private *dev_priv,
Chris Wilson3ef7f222016-10-18 13:02:48 +010087 u64 size)
Chris Wilson73aa8082010-09-30 11:46:12 +010088{
Daniel Vetterc20e8352013-07-24 22:40:23 +020089 spin_lock(&dev_priv->mm.object_stat_lock);
Chris Wilson73aa8082010-09-30 11:46:12 +010090 dev_priv->mm.object_count++;
91 dev_priv->mm.object_memory += size;
Daniel Vetterc20e8352013-07-24 22:40:23 +020092 spin_unlock(&dev_priv->mm.object_stat_lock);
Chris Wilson73aa8082010-09-30 11:46:12 +010093}
94
95static void i915_gem_info_remove_obj(struct drm_i915_private *dev_priv,
Chris Wilson3ef7f222016-10-18 13:02:48 +010096 u64 size)
Chris Wilson73aa8082010-09-30 11:46:12 +010097{
Daniel Vetterc20e8352013-07-24 22:40:23 +020098 spin_lock(&dev_priv->mm.object_stat_lock);
Chris Wilson73aa8082010-09-30 11:46:12 +010099 dev_priv->mm.object_count--;
100 dev_priv->mm.object_memory -= size;
Daniel Vetterc20e8352013-07-24 22:40:23 +0200101 spin_unlock(&dev_priv->mm.object_stat_lock);
Chris Wilson73aa8082010-09-30 11:46:12 +0100102}
103
Chris Wilsond9948a12019-02-28 10:20:35 +0000104static void __i915_gem_park(struct drm_i915_private *i915)
Chris Wilsone4d20062018-04-06 16:51:44 +0100105{
Chris Wilson506d1f62019-01-14 14:21:11 +0000106 intel_wakeref_t wakeref;
107
Chris Wilson4dfacb02018-05-31 09:22:43 +0100108 GEM_TRACE("\n");
109
Chris Wilsone4d20062018-04-06 16:51:44 +0100110 lockdep_assert_held(&i915->drm.struct_mutex);
111 GEM_BUG_ON(i915->gt.active_requests);
Chris Wilson643b4502018-04-30 14:15:03 +0100112 GEM_BUG_ON(!list_empty(&i915->gt.active_rings));
Chris Wilsone4d20062018-04-06 16:51:44 +0100113
114 if (!i915->gt.awake)
Chris Wilsond9948a12019-02-28 10:20:35 +0000115 return;
Chris Wilsone4d20062018-04-06 16:51:44 +0100116
117 /*
118 * Be paranoid and flush a concurrent interrupt to make sure
119 * we don't reactivate any irq tasklets after parking.
120 *
121 * FIXME: Note that even though we have waited for execlists to be idle,
122 * there may still be an in-flight interrupt even though the CSB
123 * is now empty. synchronize_irq() makes sure that a residual interrupt
124 * is completed before we continue, but it doesn't prevent the HW from
125 * raising a spurious interrupt later. To complete the shield we should
126 * coordinate disabling the CS irq with flushing the interrupts.
127 */
128 synchronize_irq(i915->drm.irq);
129
130 intel_engines_park(i915);
Chris Wilsona89d1f92018-05-02 17:38:39 +0100131 i915_timelines_park(i915);
Chris Wilsone4d20062018-04-06 16:51:44 +0100132
133 i915_pmu_gt_parked(i915);
Chris Wilson3365e222018-05-03 20:51:14 +0100134 i915_vma_parked(i915);
Chris Wilsone4d20062018-04-06 16:51:44 +0100135
Chris Wilson506d1f62019-01-14 14:21:11 +0000136 wakeref = fetch_and_zero(&i915->gt.awake);
137 GEM_BUG_ON(!wakeref);
Chris Wilsone4d20062018-04-06 16:51:44 +0100138
139 if (INTEL_GEN(i915) >= 6)
140 gen6_rps_idle(i915);
141
Chris Wilson8d761e72019-01-14 14:21:28 +0000142 intel_display_power_put(i915, POWER_DOMAIN_GT_IRQ, wakeref);
Chris Wilsone4d20062018-04-06 16:51:44 +0100143
Chris Wilsond9948a12019-02-28 10:20:35 +0000144 i915_globals_park();
Chris Wilsone4d20062018-04-06 16:51:44 +0100145}
146
147void i915_gem_park(struct drm_i915_private *i915)
148{
Chris Wilson4dfacb02018-05-31 09:22:43 +0100149 GEM_TRACE("\n");
150
Chris Wilsone4d20062018-04-06 16:51:44 +0100151 lockdep_assert_held(&i915->drm.struct_mutex);
152 GEM_BUG_ON(i915->gt.active_requests);
153
154 if (!i915->gt.awake)
155 return;
156
157 /* Defer the actual call to __i915_gem_park() to prevent ping-pongs */
158 mod_delayed_work(i915->wq, &i915->gt.idle_work, msecs_to_jiffies(100));
159}
160
161void i915_gem_unpark(struct drm_i915_private *i915)
162{
Chris Wilson4dfacb02018-05-31 09:22:43 +0100163 GEM_TRACE("\n");
164
Chris Wilsone4d20062018-04-06 16:51:44 +0100165 lockdep_assert_held(&i915->drm.struct_mutex);
166 GEM_BUG_ON(!i915->gt.active_requests);
Chris Wilson8d761e72019-01-14 14:21:28 +0000167 assert_rpm_wakelock_held(i915);
Chris Wilsone4d20062018-04-06 16:51:44 +0100168
169 if (i915->gt.awake)
170 return;
171
Chris Wilsone4d20062018-04-06 16:51:44 +0100172 /*
173 * It seems that the DMC likes to transition between the DC states a lot
174 * when there are no connected displays (no active power domains) during
175 * command submission.
176 *
177 * This activity has negative impact on the performance of the chip with
178 * huge latencies observed in the interrupt handler and elsewhere.
179 *
180 * Work around it by grabbing a GT IRQ power domain whilst there is any
181 * GT activity, preventing any DC state transitions.
182 */
Chris Wilson8d761e72019-01-14 14:21:28 +0000183 i915->gt.awake = intel_display_power_get(i915, POWER_DOMAIN_GT_IRQ);
184 GEM_BUG_ON(!i915->gt.awake);
Chris Wilsone4d20062018-04-06 16:51:44 +0100185
Chris Wilson32eb6bc2019-02-28 10:20:33 +0000186 i915_globals_unpark();
187
Chris Wilsone4d20062018-04-06 16:51:44 +0100188 intel_enable_gt_powersave(i915);
189 i915_update_gfx_val(i915);
190 if (INTEL_GEN(i915) >= 6)
191 gen6_rps_busy(i915);
192 i915_pmu_gt_unparked(i915);
193
194 intel_engines_unpark(i915);
195
196 i915_queue_hangcheck(i915);
197
198 queue_delayed_work(i915->wq,
199 &i915->gt.retire_work,
200 round_jiffies_up_relative(HZ));
201}
202
Eric Anholt673a3942008-07-30 12:06:12 -0700203int
Eric Anholt5a125c32008-10-22 21:40:13 -0700204i915_gem_get_aperture_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +0000205 struct drm_file *file)
Eric Anholt5a125c32008-10-22 21:40:13 -0700206{
Chris Wilson09d7e462019-01-28 10:23:53 +0000207 struct i915_ggtt *ggtt = &to_i915(dev)->ggtt;
Joonas Lahtinen72e96d62016-03-30 16:57:10 +0300208 struct drm_i915_gem_get_aperture *args = data;
Tvrtko Ursulinca1543b2015-07-01 11:51:10 +0100209 struct i915_vma *vma;
Weinan Liff8f7972017-05-31 10:35:52 +0800210 u64 pinned;
Eric Anholt5a125c32008-10-22 21:40:13 -0700211
Chris Wilson09d7e462019-01-28 10:23:53 +0000212 mutex_lock(&ggtt->vm.mutex);
213
Chris Wilson82ad6442018-06-05 16:37:58 +0100214 pinned = ggtt->vm.reserved;
Chris Wilson499197d2019-01-28 10:23:52 +0000215 list_for_each_entry(vma, &ggtt->vm.bound_list, vm_link)
Chris Wilson20dfbde2016-08-04 16:32:30 +0100216 if (i915_vma_is_pinned(vma))
Tvrtko Ursulinca1543b2015-07-01 11:51:10 +0100217 pinned += vma->node.size;
Chris Wilson09d7e462019-01-28 10:23:53 +0000218
219 mutex_unlock(&ggtt->vm.mutex);
Eric Anholt5a125c32008-10-22 21:40:13 -0700220
Chris Wilson82ad6442018-06-05 16:37:58 +0100221 args->aper_size = ggtt->vm.total;
Akshay Joshi0206e352011-08-16 15:34:10 -0400222 args->aper_available_size = args->aper_size - pinned;
Chris Wilson6299f992010-11-24 12:23:44 +0000223
Eric Anholt5a125c32008-10-22 21:40:13 -0700224 return 0;
225}
226
Matthew Auldb91b09e2017-10-06 23:18:17 +0100227static int i915_gem_object_get_pages_phys(struct drm_i915_gem_object *obj)
Chris Wilson00731152014-05-21 12:42:56 +0100228{
Al Viro93c76a32015-12-04 23:45:44 -0500229 struct address_space *mapping = obj->base.filp->f_mapping;
Chris Wilsondbb43512016-12-07 13:34:11 +0000230 drm_dma_handle_t *phys;
Chris Wilson6a2c4232014-11-04 04:51:40 -0800231 struct sg_table *st;
232 struct scatterlist *sg;
Chris Wilsondbb43512016-12-07 13:34:11 +0000233 char *vaddr;
Chris Wilson6a2c4232014-11-04 04:51:40 -0800234 int i;
Matthew Auldb91b09e2017-10-06 23:18:17 +0100235 int err;
Chris Wilson00731152014-05-21 12:42:56 +0100236
Chris Wilson6a2c4232014-11-04 04:51:40 -0800237 if (WARN_ON(i915_gem_object_needs_bit17_swizzle(obj)))
Matthew Auldb91b09e2017-10-06 23:18:17 +0100238 return -EINVAL;
Chris Wilson00731152014-05-21 12:42:56 +0100239
Chris Wilsondbb43512016-12-07 13:34:11 +0000240 /* Always aligning to the object size, allows a single allocation
241 * to handle all possible callers, and given typical object sizes,
242 * the alignment of the buddy allocation will naturally match.
243 */
244 phys = drm_pci_alloc(obj->base.dev,
Ville Syrjälä750fae22017-09-07 17:32:03 +0300245 roundup_pow_of_two(obj->base.size),
Chris Wilsondbb43512016-12-07 13:34:11 +0000246 roundup_pow_of_two(obj->base.size));
247 if (!phys)
Matthew Auldb91b09e2017-10-06 23:18:17 +0100248 return -ENOMEM;
Chris Wilsondbb43512016-12-07 13:34:11 +0000249
250 vaddr = phys->vaddr;
Chris Wilson6a2c4232014-11-04 04:51:40 -0800251 for (i = 0; i < obj->base.size / PAGE_SIZE; i++) {
252 struct page *page;
253 char *src;
254
255 page = shmem_read_mapping_page(mapping, i);
Chris Wilsondbb43512016-12-07 13:34:11 +0000256 if (IS_ERR(page)) {
Matthew Auldb91b09e2017-10-06 23:18:17 +0100257 err = PTR_ERR(page);
Chris Wilsondbb43512016-12-07 13:34:11 +0000258 goto err_phys;
259 }
Chris Wilson6a2c4232014-11-04 04:51:40 -0800260
261 src = kmap_atomic(page);
262 memcpy(vaddr, src, PAGE_SIZE);
263 drm_clflush_virt_range(vaddr, PAGE_SIZE);
264 kunmap_atomic(src);
265
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300266 put_page(page);
Chris Wilson6a2c4232014-11-04 04:51:40 -0800267 vaddr += PAGE_SIZE;
268 }
269
Chris Wilsonc0336662016-05-06 15:40:21 +0100270 i915_gem_chipset_flush(to_i915(obj->base.dev));
Chris Wilson6a2c4232014-11-04 04:51:40 -0800271
272 st = kmalloc(sizeof(*st), GFP_KERNEL);
Chris Wilsondbb43512016-12-07 13:34:11 +0000273 if (!st) {
Matthew Auldb91b09e2017-10-06 23:18:17 +0100274 err = -ENOMEM;
Chris Wilsondbb43512016-12-07 13:34:11 +0000275 goto err_phys;
276 }
Chris Wilson6a2c4232014-11-04 04:51:40 -0800277
278 if (sg_alloc_table(st, 1, GFP_KERNEL)) {
279 kfree(st);
Matthew Auldb91b09e2017-10-06 23:18:17 +0100280 err = -ENOMEM;
Chris Wilsondbb43512016-12-07 13:34:11 +0000281 goto err_phys;
Chris Wilson6a2c4232014-11-04 04:51:40 -0800282 }
283
284 sg = st->sgl;
285 sg->offset = 0;
286 sg->length = obj->base.size;
287
Chris Wilsondbb43512016-12-07 13:34:11 +0000288 sg_dma_address(sg) = phys->busaddr;
Chris Wilson6a2c4232014-11-04 04:51:40 -0800289 sg_dma_len(sg) = obj->base.size;
290
Chris Wilsondbb43512016-12-07 13:34:11 +0000291 obj->phys_handle = phys;
Matthew Auldb91b09e2017-10-06 23:18:17 +0100292
Matthew Aulda5c081662017-10-06 23:18:18 +0100293 __i915_gem_object_set_pages(obj, st, sg->length);
Matthew Auldb91b09e2017-10-06 23:18:17 +0100294
295 return 0;
Chris Wilsondbb43512016-12-07 13:34:11 +0000296
297err_phys:
298 drm_pci_free(obj->base.dev, phys);
Matthew Auldb91b09e2017-10-06 23:18:17 +0100299
300 return err;
Chris Wilson6a2c4232014-11-04 04:51:40 -0800301}
302
Chris Wilsone27ab732017-06-15 13:38:49 +0100303static void __start_cpu_write(struct drm_i915_gem_object *obj)
304{
Christian Königc0a51fd2018-02-16 13:43:38 +0100305 obj->read_domains = I915_GEM_DOMAIN_CPU;
306 obj->write_domain = I915_GEM_DOMAIN_CPU;
Chris Wilsone27ab732017-06-15 13:38:49 +0100307 if (cpu_write_needs_clflush(obj))
308 obj->cache_dirty = true;
309}
310
Chris Wilsonee8efa82019-03-31 10:46:20 +0100311void
Chris Wilson2b3c8312016-11-11 14:58:09 +0000312__i915_gem_object_release_shmem(struct drm_i915_gem_object *obj,
Chris Wilsone5facdf2016-12-23 14:57:57 +0000313 struct sg_table *pages,
314 bool needs_clflush)
Chris Wilson6a2c4232014-11-04 04:51:40 -0800315{
Chris Wilsona4f5ea62016-10-28 13:58:35 +0100316 GEM_BUG_ON(obj->mm.madv == __I915_MADV_PURGED);
Chris Wilson6a2c4232014-11-04 04:51:40 -0800317
Chris Wilsona4f5ea62016-10-28 13:58:35 +0100318 if (obj->mm.madv == I915_MADV_DONTNEED)
319 obj->mm.dirty = false;
Chris Wilson6a2c4232014-11-04 04:51:40 -0800320
Chris Wilsone5facdf2016-12-23 14:57:57 +0000321 if (needs_clflush &&
Christian Königc0a51fd2018-02-16 13:43:38 +0100322 (obj->read_domains & I915_GEM_DOMAIN_CPU) == 0 &&
Chris Wilsonb8f55be2017-08-11 12:11:16 +0100323 !(obj->cache_coherent & I915_BO_CACHE_COHERENT_FOR_READ))
Chris Wilson2b3c8312016-11-11 14:58:09 +0000324 drm_clflush_sg(pages);
Chris Wilson03ac84f2016-10-28 13:58:36 +0100325
Chris Wilsone27ab732017-06-15 13:38:49 +0100326 __start_cpu_write(obj);
Chris Wilson03ac84f2016-10-28 13:58:36 +0100327}
328
329static void
330i915_gem_object_put_pages_phys(struct drm_i915_gem_object *obj,
331 struct sg_table *pages)
332{
Chris Wilsone5facdf2016-12-23 14:57:57 +0000333 __i915_gem_object_release_shmem(obj, pages, false);
Chris Wilson03ac84f2016-10-28 13:58:36 +0100334
Chris Wilsona4f5ea62016-10-28 13:58:35 +0100335 if (obj->mm.dirty) {
Al Viro93c76a32015-12-04 23:45:44 -0500336 struct address_space *mapping = obj->base.filp->f_mapping;
Chris Wilson6a2c4232014-11-04 04:51:40 -0800337 char *vaddr = obj->phys_handle->vaddr;
Chris Wilson00731152014-05-21 12:42:56 +0100338 int i;
339
340 for (i = 0; i < obj->base.size / PAGE_SIZE; i++) {
Chris Wilson6a2c4232014-11-04 04:51:40 -0800341 struct page *page;
342 char *dst;
Chris Wilson00731152014-05-21 12:42:56 +0100343
Chris Wilson6a2c4232014-11-04 04:51:40 -0800344 page = shmem_read_mapping_page(mapping, i);
345 if (IS_ERR(page))
346 continue;
347
348 dst = kmap_atomic(page);
349 drm_clflush_virt_range(vaddr, PAGE_SIZE);
350 memcpy(dst, vaddr, PAGE_SIZE);
351 kunmap_atomic(dst);
352
353 set_page_dirty(page);
Chris Wilsona4f5ea62016-10-28 13:58:35 +0100354 if (obj->mm.madv == I915_MADV_WILLNEED)
Chris Wilson00731152014-05-21 12:42:56 +0100355 mark_page_accessed(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300356 put_page(page);
Chris Wilson00731152014-05-21 12:42:56 +0100357 vaddr += PAGE_SIZE;
358 }
Chris Wilsona4f5ea62016-10-28 13:58:35 +0100359 obj->mm.dirty = false;
Chris Wilson00731152014-05-21 12:42:56 +0100360 }
361
Chris Wilson03ac84f2016-10-28 13:58:36 +0100362 sg_free_table(pages);
363 kfree(pages);
Chris Wilsondbb43512016-12-07 13:34:11 +0000364
365 drm_pci_free(obj->base.dev, obj->phys_handle);
Chris Wilson6a2c4232014-11-04 04:51:40 -0800366}
367
368static void
369i915_gem_object_release_phys(struct drm_i915_gem_object *obj)
370{
Chris Wilsona4f5ea62016-10-28 13:58:35 +0100371 i915_gem_object_unpin_pages(obj);
Chris Wilson6a2c4232014-11-04 04:51:40 -0800372}
373
374static const struct drm_i915_gem_object_ops i915_gem_phys_ops = {
375 .get_pages = i915_gem_object_get_pages_phys,
376 .put_pages = i915_gem_object_put_pages_phys,
377 .release = i915_gem_object_release_phys,
378};
379
Chris Wilson581ab1f2017-02-15 16:39:00 +0000380static const struct drm_i915_gem_object_ops i915_gem_object_ops;
381
Chris Wilson35a96112016-08-14 18:44:40 +0100382int i915_gem_object_unbind(struct drm_i915_gem_object *obj)
Chris Wilsonaa653a62016-08-04 07:52:27 +0100383{
384 struct i915_vma *vma;
385 LIST_HEAD(still_in_list);
Chris Wilson02bef8f2016-08-14 18:44:41 +0100386 int ret;
Chris Wilsonaa653a62016-08-04 07:52:27 +0100387
Chris Wilson02bef8f2016-08-14 18:44:41 +0100388 lockdep_assert_held(&obj->base.dev->struct_mutex);
389
390 /* Closed vma are removed from the obj->vma_list - but they may
391 * still have an active binding on the object. To remove those we
392 * must wait for all rendering to complete to the object (as unbinding
393 * must anyway), and retire the requests.
Chris Wilsonaa653a62016-08-04 07:52:27 +0100394 */
Chris Wilson5888fc92017-12-04 13:25:13 +0000395 ret = i915_gem_object_set_to_cpu_domain(obj, false);
Chris Wilson02bef8f2016-08-14 18:44:41 +0100396 if (ret)
397 return ret;
398
Chris Wilson528cbd12019-01-28 10:23:54 +0000399 spin_lock(&obj->vma.lock);
400 while (!ret && (vma = list_first_entry_or_null(&obj->vma.list,
401 struct i915_vma,
402 obj_link))) {
Chris Wilsonaa653a62016-08-04 07:52:27 +0100403 list_move_tail(&vma->obj_link, &still_in_list);
Chris Wilson528cbd12019-01-28 10:23:54 +0000404 spin_unlock(&obj->vma.lock);
405
Chris Wilsonaa653a62016-08-04 07:52:27 +0100406 ret = i915_vma_unbind(vma);
Chris Wilson528cbd12019-01-28 10:23:54 +0000407
408 spin_lock(&obj->vma.lock);
Chris Wilsonaa653a62016-08-04 07:52:27 +0100409 }
Chris Wilson528cbd12019-01-28 10:23:54 +0000410 list_splice(&still_in_list, &obj->vma.list);
411 spin_unlock(&obj->vma.lock);
Chris Wilsonaa653a62016-08-04 07:52:27 +0100412
413 return ret;
414}
415
Chris Wilsone95433c2016-10-28 13:58:27 +0100416static long
417i915_gem_object_wait_fence(struct dma_fence *fence,
418 unsigned int flags,
Chris Wilson62eb3c22019-02-13 09:25:04 +0000419 long timeout)
Chris Wilsone95433c2016-10-28 13:58:27 +0100420{
Chris Wilsone61e0f52018-02-21 09:56:36 +0000421 struct i915_request *rq;
Chris Wilsone95433c2016-10-28 13:58:27 +0100422
423 BUILD_BUG_ON(I915_WAIT_INTERRUPTIBLE != 0x1);
424
425 if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags))
426 return timeout;
427
428 if (!dma_fence_is_i915(fence))
429 return dma_fence_wait_timeout(fence,
430 flags & I915_WAIT_INTERRUPTIBLE,
431 timeout);
432
433 rq = to_request(fence);
Chris Wilsone61e0f52018-02-21 09:56:36 +0000434 if (i915_request_completed(rq))
Chris Wilsone95433c2016-10-28 13:58:27 +0100435 goto out;
436
Chris Wilsone61e0f52018-02-21 09:56:36 +0000437 timeout = i915_request_wait(rq, flags, timeout);
Chris Wilsone95433c2016-10-28 13:58:27 +0100438
439out:
Chris Wilsone61e0f52018-02-21 09:56:36 +0000440 if (flags & I915_WAIT_LOCKED && i915_request_completed(rq))
441 i915_request_retire_upto(rq);
Chris Wilsone95433c2016-10-28 13:58:27 +0100442
Chris Wilsone95433c2016-10-28 13:58:27 +0100443 return timeout;
444}
445
446static long
447i915_gem_object_wait_reservation(struct reservation_object *resv,
448 unsigned int flags,
Chris Wilson62eb3c22019-02-13 09:25:04 +0000449 long timeout)
Chris Wilsone95433c2016-10-28 13:58:27 +0100450{
Chris Wilsone54ca972017-02-17 15:13:04 +0000451 unsigned int seq = __read_seqcount_begin(&resv->seq);
Chris Wilsone95433c2016-10-28 13:58:27 +0100452 struct dma_fence *excl;
Chris Wilsone54ca972017-02-17 15:13:04 +0000453 bool prune_fences = false;
Chris Wilsone95433c2016-10-28 13:58:27 +0100454
455 if (flags & I915_WAIT_ALL) {
456 struct dma_fence **shared;
457 unsigned int count, i;
458 int ret;
459
460 ret = reservation_object_get_fences_rcu(resv,
461 &excl, &count, &shared);
462 if (ret)
463 return ret;
464
465 for (i = 0; i < count; i++) {
466 timeout = i915_gem_object_wait_fence(shared[i],
Chris Wilson62eb3c22019-02-13 09:25:04 +0000467 flags, timeout);
Chris Wilsond892e932017-02-12 21:53:43 +0000468 if (timeout < 0)
Chris Wilsone95433c2016-10-28 13:58:27 +0100469 break;
470
471 dma_fence_put(shared[i]);
472 }
473
474 for (; i < count; i++)
475 dma_fence_put(shared[i]);
476 kfree(shared);
Chris Wilsone54ca972017-02-17 15:13:04 +0000477
Chris Wilsonfa730552018-03-07 17:13:03 +0000478 /*
479 * If both shared fences and an exclusive fence exist,
480 * then by construction the shared fences must be later
481 * than the exclusive fence. If we successfully wait for
482 * all the shared fences, we know that the exclusive fence
483 * must all be signaled. If all the shared fences are
484 * signaled, we can prune the array and recover the
485 * floating references on the fences/requests.
486 */
Chris Wilsone54ca972017-02-17 15:13:04 +0000487 prune_fences = count && timeout >= 0;
Chris Wilsone95433c2016-10-28 13:58:27 +0100488 } else {
489 excl = reservation_object_get_excl_rcu(resv);
490 }
491
Chris Wilsonfa730552018-03-07 17:13:03 +0000492 if (excl && timeout >= 0)
Chris Wilson62eb3c22019-02-13 09:25:04 +0000493 timeout = i915_gem_object_wait_fence(excl, flags, timeout);
Chris Wilsone95433c2016-10-28 13:58:27 +0100494
495 dma_fence_put(excl);
496
Chris Wilsonfa730552018-03-07 17:13:03 +0000497 /*
498 * Opportunistically prune the fences iff we know they have *all* been
Chris Wilson03d1cac2017-03-08 13:26:28 +0000499 * signaled and that the reservation object has not been changed (i.e.
500 * no new fences have been added).
501 */
Chris Wilsone54ca972017-02-17 15:13:04 +0000502 if (prune_fences && !__read_seqcount_retry(&resv->seq, seq)) {
Chris Wilson03d1cac2017-03-08 13:26:28 +0000503 if (reservation_object_trylock(resv)) {
504 if (!__read_seqcount_retry(&resv->seq, seq))
505 reservation_object_add_excl_fence(resv, NULL);
506 reservation_object_unlock(resv);
507 }
Chris Wilsone54ca972017-02-17 15:13:04 +0000508 }
509
Chris Wilsone95433c2016-10-28 13:58:27 +0100510 return timeout;
511}
512
Chris Wilsonb7268c52018-04-18 19:40:52 +0100513static void __fence_set_priority(struct dma_fence *fence,
514 const struct i915_sched_attr *attr)
Chris Wilson6b5e90f2016-11-14 20:41:05 +0000515{
Chris Wilsone61e0f52018-02-21 09:56:36 +0000516 struct i915_request *rq;
Chris Wilson6b5e90f2016-11-14 20:41:05 +0000517 struct intel_engine_cs *engine;
518
Chris Wilsonc218ee02018-01-06 10:56:18 +0000519 if (dma_fence_is_signaled(fence) || !dma_fence_is_i915(fence))
Chris Wilson6b5e90f2016-11-14 20:41:05 +0000520 return;
521
522 rq = to_request(fence);
523 engine = rq->engine;
Chris Wilson6b5e90f2016-11-14 20:41:05 +0000524
Chris Wilson4f6d8fc2018-05-07 14:57:25 +0100525 local_bh_disable();
526 rcu_read_lock(); /* RCU serialisation for set-wedged protection */
Chris Wilson47650db2018-03-07 13:42:25 +0000527 if (engine->schedule)
Chris Wilsonb7268c52018-04-18 19:40:52 +0100528 engine->schedule(rq, attr);
Chris Wilson47650db2018-03-07 13:42:25 +0000529 rcu_read_unlock();
Chris Wilson4f6d8fc2018-05-07 14:57:25 +0100530 local_bh_enable(); /* kick the tasklets if queues were reprioritised */
Chris Wilson6b5e90f2016-11-14 20:41:05 +0000531}
532
Chris Wilsonb7268c52018-04-18 19:40:52 +0100533static void fence_set_priority(struct dma_fence *fence,
534 const struct i915_sched_attr *attr)
Chris Wilson6b5e90f2016-11-14 20:41:05 +0000535{
536 /* Recurse once into a fence-array */
537 if (dma_fence_is_array(fence)) {
538 struct dma_fence_array *array = to_dma_fence_array(fence);
539 int i;
540
541 for (i = 0; i < array->num_fences; i++)
Chris Wilsonb7268c52018-04-18 19:40:52 +0100542 __fence_set_priority(array->fences[i], attr);
Chris Wilson6b5e90f2016-11-14 20:41:05 +0000543 } else {
Chris Wilsonb7268c52018-04-18 19:40:52 +0100544 __fence_set_priority(fence, attr);
Chris Wilson6b5e90f2016-11-14 20:41:05 +0000545 }
546}
547
548int
549i915_gem_object_wait_priority(struct drm_i915_gem_object *obj,
550 unsigned int flags,
Chris Wilsonb7268c52018-04-18 19:40:52 +0100551 const struct i915_sched_attr *attr)
Chris Wilson6b5e90f2016-11-14 20:41:05 +0000552{
553 struct dma_fence *excl;
554
555 if (flags & I915_WAIT_ALL) {
556 struct dma_fence **shared;
557 unsigned int count, i;
558 int ret;
559
560 ret = reservation_object_get_fences_rcu(obj->resv,
561 &excl, &count, &shared);
562 if (ret)
563 return ret;
564
565 for (i = 0; i < count; i++) {
Chris Wilsonb7268c52018-04-18 19:40:52 +0100566 fence_set_priority(shared[i], attr);
Chris Wilson6b5e90f2016-11-14 20:41:05 +0000567 dma_fence_put(shared[i]);
568 }
569
570 kfree(shared);
571 } else {
572 excl = reservation_object_get_excl_rcu(obj->resv);
573 }
574
575 if (excl) {
Chris Wilsonb7268c52018-04-18 19:40:52 +0100576 fence_set_priority(excl, attr);
Chris Wilson6b5e90f2016-11-14 20:41:05 +0000577 dma_fence_put(excl);
578 }
579 return 0;
580}
581
Chris Wilson00e60f22016-08-04 16:32:40 +0100582/**
Chris Wilsone95433c2016-10-28 13:58:27 +0100583 * Waits for rendering to the object to be completed
Chris Wilson00e60f22016-08-04 16:32:40 +0100584 * @obj: i915 gem object
Chris Wilsone95433c2016-10-28 13:58:27 +0100585 * @flags: how to wait (under a lock, for all rendering or just for writes etc)
586 * @timeout: how long to wait
Chris Wilson00e60f22016-08-04 16:32:40 +0100587 */
588int
Chris Wilsone95433c2016-10-28 13:58:27 +0100589i915_gem_object_wait(struct drm_i915_gem_object *obj,
590 unsigned int flags,
Chris Wilson62eb3c22019-02-13 09:25:04 +0000591 long timeout)
Chris Wilson00e60f22016-08-04 16:32:40 +0100592{
Chris Wilsone95433c2016-10-28 13:58:27 +0100593 might_sleep();
Chris Wilsone95433c2016-10-28 13:58:27 +0100594 GEM_BUG_ON(timeout < 0);
Chris Wilson00e60f22016-08-04 16:32:40 +0100595
Chris Wilson62eb3c22019-02-13 09:25:04 +0000596 timeout = i915_gem_object_wait_reservation(obj->resv, flags, timeout);
Chris Wilsone95433c2016-10-28 13:58:27 +0100597 return timeout < 0 ? timeout : 0;
Chris Wilson00e60f22016-08-04 16:32:40 +0100598}
599
Chris Wilson00731152014-05-21 12:42:56 +0100600static int
601i915_gem_phys_pwrite(struct drm_i915_gem_object *obj,
602 struct drm_i915_gem_pwrite *args,
Chris Wilson03ac84f2016-10-28 13:58:36 +0100603 struct drm_file *file)
Chris Wilson00731152014-05-21 12:42:56 +0100604{
Chris Wilson00731152014-05-21 12:42:56 +0100605 void *vaddr = obj->phys_handle->vaddr + args->offset;
Gustavo Padovan3ed605b2016-04-26 12:32:27 -0300606 char __user *user_data = u64_to_user_ptr(args->data_ptr);
Chris Wilson6a2c4232014-11-04 04:51:40 -0800607
608 /* We manually control the domain here and pretend that it
609 * remains coherent i.e. in the GTT domain, like shmem_pwrite.
610 */
Rodrigo Vivi77a0d1c2015-06-18 11:43:24 -0700611 intel_fb_obj_invalidate(obj, ORIGIN_CPU);
Chris Wilson10466d22017-01-06 15:22:38 +0000612 if (copy_from_user(vaddr, user_data, args->size))
613 return -EFAULT;
Chris Wilson00731152014-05-21 12:42:56 +0100614
Chris Wilson6a2c4232014-11-04 04:51:40 -0800615 drm_clflush_virt_range(vaddr, args->size);
Chris Wilson10466d22017-01-06 15:22:38 +0000616 i915_gem_chipset_flush(to_i915(obj->base.dev));
Paulo Zanoni063e4e62015-02-13 17:23:45 -0200617
Chris Wilsond59b21e2017-02-22 11:40:49 +0000618 intel_fb_obj_flush(obj, ORIGIN_CPU);
Chris Wilson10466d22017-01-06 15:22:38 +0000619 return 0;
Chris Wilson00731152014-05-21 12:42:56 +0100620}
621
Dave Airlieff72145b2011-02-07 12:16:14 +1000622static int
623i915_gem_create(struct drm_file *file,
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +0000624 struct drm_i915_private *dev_priv,
Michał Winiarskie1634842019-03-26 18:02:18 +0100625 u64 *size_p,
Jani Nikula739f3ab2019-01-16 11:15:19 +0200626 u32 *handle_p)
Eric Anholt673a3942008-07-30 12:06:12 -0700627{
Chris Wilson05394f32010-11-08 19:18:58 +0000628 struct drm_i915_gem_object *obj;
Pekka Paalanena1a2d1d2009-08-23 12:40:55 +0300629 u32 handle;
Michał Winiarskie1634842019-03-26 18:02:18 +0100630 u64 size;
631 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700632
Michał Winiarskie1634842019-03-26 18:02:18 +0100633 size = round_up(*size_p, PAGE_SIZE);
Chris Wilson8ffc0242011-09-14 14:14:28 +0200634 if (size == 0)
635 return -EINVAL;
Eric Anholt673a3942008-07-30 12:06:12 -0700636
637 /* Allocate the new object */
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +0000638 obj = i915_gem_object_create(dev_priv, size);
Chris Wilsonfe3db792016-04-25 13:32:13 +0100639 if (IS_ERR(obj))
640 return PTR_ERR(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700641
Chris Wilson05394f32010-11-08 19:18:58 +0000642 ret = drm_gem_handle_create(file, &obj->base, &handle);
Chris Wilson202f2fe2010-10-14 13:20:40 +0100643 /* drop reference from allocate - handle holds it now */
Chris Wilsonf0cd5182016-10-28 13:58:43 +0100644 i915_gem_object_put(obj);
Daniel Vetterd861e332013-07-24 23:25:03 +0200645 if (ret)
646 return ret;
Chris Wilson202f2fe2010-10-14 13:20:40 +0100647
Dave Airlieff72145b2011-02-07 12:16:14 +1000648 *handle_p = handle;
Michał Winiarskie1634842019-03-26 18:02:18 +0100649 *size_p = obj->base.size;
Eric Anholt673a3942008-07-30 12:06:12 -0700650 return 0;
651}
652
Dave Airlieff72145b2011-02-07 12:16:14 +1000653int
654i915_gem_dumb_create(struct drm_file *file,
655 struct drm_device *dev,
656 struct drm_mode_create_dumb *args)
657{
658 /* have to work out size/pitch and return them */
Paulo Zanonide45eaf2013-10-18 18:48:24 -0300659 args->pitch = ALIGN(args->width * DIV_ROUND_UP(args->bpp, 8), 64);
Dave Airlieff72145b2011-02-07 12:16:14 +1000660 args->size = args->pitch * args->height;
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +0000661 return i915_gem_create(file, to_i915(dev),
Michał Winiarskie1634842019-03-26 18:02:18 +0100662 &args->size, &args->handle);
Dave Airlieff72145b2011-02-07 12:16:14 +1000663}
664
Chris Wilsone27ab732017-06-15 13:38:49 +0100665static bool gpu_write_needs_clflush(struct drm_i915_gem_object *obj)
666{
667 return !(obj->cache_level == I915_CACHE_NONE ||
668 obj->cache_level == I915_CACHE_WT);
669}
670
Dave Airlieff72145b2011-02-07 12:16:14 +1000671/**
672 * Creates a new mm object and returns a handle to it.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +0100673 * @dev: drm device pointer
674 * @data: ioctl data blob
675 * @file: drm file pointer
Dave Airlieff72145b2011-02-07 12:16:14 +1000676 */
677int
678i915_gem_create_ioctl(struct drm_device *dev, void *data,
679 struct drm_file *file)
680{
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +0000681 struct drm_i915_private *dev_priv = to_i915(dev);
Dave Airlieff72145b2011-02-07 12:16:14 +1000682 struct drm_i915_gem_create *args = data;
Daniel Vetter63ed2cb2012-04-23 16:50:50 +0200683
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +0000684 i915_gem_flush_free_objects(dev_priv);
Chris Wilsonfbbd37b2016-10-28 13:58:42 +0100685
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +0000686 return i915_gem_create(file, dev_priv,
Michał Winiarskie1634842019-03-26 18:02:18 +0100687 &args->size, &args->handle);
Dave Airlieff72145b2011-02-07 12:16:14 +1000688}
689
Chris Wilsonef749212017-04-12 12:01:10 +0100690static inline enum fb_op_origin
691fb_write_origin(struct drm_i915_gem_object *obj, unsigned int domain)
692{
693 return (domain == I915_GEM_DOMAIN_GTT ?
694 obj->frontbuffer_ggtt_origin : ORIGIN_CPU);
695}
696
Chris Wilson7125397b2017-12-06 12:49:14 +0000697void i915_gem_flush_ggtt_writes(struct drm_i915_private *dev_priv)
Chris Wilsonef749212017-04-12 12:01:10 +0100698{
Chris Wilson538ef962019-01-14 14:21:18 +0000699 intel_wakeref_t wakeref;
700
Chris Wilson7125397b2017-12-06 12:49:14 +0000701 /*
702 * No actual flushing is required for the GTT write domain for reads
703 * from the GTT domain. Writes to it "immediately" go to main memory
704 * as far as we know, so there's no chipset flush. It also doesn't
705 * land in the GPU render cache.
Chris Wilsonef749212017-04-12 12:01:10 +0100706 *
707 * However, we do have to enforce the order so that all writes through
708 * the GTT land before any writes to the device, such as updates to
709 * the GATT itself.
710 *
711 * We also have to wait a bit for the writes to land from the GTT.
712 * An uncached read (i.e. mmio) seems to be ideal for the round-trip
713 * timing. This issue has only been observed when switching quickly
714 * between GTT writes and CPU reads from inside the kernel on recent hw,
715 * and it appears to only affect discrete GTT blocks (i.e. on LLC
Chris Wilson7125397b2017-12-06 12:49:14 +0000716 * system agents we cannot reproduce this behaviour, until Cannonlake
717 * that was!).
Chris Wilsonef749212017-04-12 12:01:10 +0100718 */
Chris Wilson7125397b2017-12-06 12:49:14 +0000719
Chris Wilson900ccf32018-07-20 11:19:10 +0100720 wmb();
721
722 if (INTEL_INFO(dev_priv)->has_coherent_ggtt)
723 return;
724
Chris Wilsona8bd3b82018-07-17 10:26:55 +0100725 i915_gem_chipset_flush(dev_priv);
Chris Wilsonef749212017-04-12 12:01:10 +0100726
Chris Wilsond4225a52019-01-14 14:21:23 +0000727 with_intel_runtime_pm(dev_priv, wakeref) {
728 spin_lock_irq(&dev_priv->uncore.lock);
Chris Wilson7125397b2017-12-06 12:49:14 +0000729
Chris Wilsond4225a52019-01-14 14:21:23 +0000730 POSTING_READ_FW(RING_HEAD(RENDER_RING_BASE));
Chris Wilson7125397b2017-12-06 12:49:14 +0000731
Chris Wilsond4225a52019-01-14 14:21:23 +0000732 spin_unlock_irq(&dev_priv->uncore.lock);
733 }
Chris Wilson7125397b2017-12-06 12:49:14 +0000734}
735
736static void
737flush_write_domain(struct drm_i915_gem_object *obj, unsigned int flush_domains)
738{
739 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
740 struct i915_vma *vma;
741
Christian Königc0a51fd2018-02-16 13:43:38 +0100742 if (!(obj->write_domain & flush_domains))
Chris Wilson7125397b2017-12-06 12:49:14 +0000743 return;
744
Christian Königc0a51fd2018-02-16 13:43:38 +0100745 switch (obj->write_domain) {
Chris Wilsonef749212017-04-12 12:01:10 +0100746 case I915_GEM_DOMAIN_GTT:
Chris Wilson7125397b2017-12-06 12:49:14 +0000747 i915_gem_flush_ggtt_writes(dev_priv);
Chris Wilsonef749212017-04-12 12:01:10 +0100748
749 intel_fb_obj_flush(obj,
750 fb_write_origin(obj, I915_GEM_DOMAIN_GTT));
Chris Wilson7125397b2017-12-06 12:49:14 +0000751
Chris Wilsone2189dd2017-12-07 21:14:07 +0000752 for_each_ggtt_vma(vma, obj) {
Chris Wilson7125397b2017-12-06 12:49:14 +0000753 if (vma->iomap)
754 continue;
755
756 i915_vma_unset_ggtt_write(vma);
757 }
Chris Wilsonef749212017-04-12 12:01:10 +0100758 break;
759
Chris Wilsonadd00e62018-07-06 12:54:02 +0100760 case I915_GEM_DOMAIN_WC:
761 wmb();
762 break;
763
Chris Wilsonef749212017-04-12 12:01:10 +0100764 case I915_GEM_DOMAIN_CPU:
765 i915_gem_clflush_object(obj, I915_CLFLUSH_SYNC);
766 break;
Chris Wilsone27ab732017-06-15 13:38:49 +0100767
768 case I915_GEM_DOMAIN_RENDER:
769 if (gpu_write_needs_clflush(obj))
770 obj->cache_dirty = true;
771 break;
Chris Wilsonef749212017-04-12 12:01:10 +0100772 }
773
Christian Königc0a51fd2018-02-16 13:43:38 +0100774 obj->write_domain = 0;
Chris Wilsonef749212017-04-12 12:01:10 +0100775}
776
Brad Volkin4c914c02014-02-18 10:15:45 -0800777/*
778 * Pins the specified object's pages and synchronizes the object with
779 * GPU accesses. Sets needs_clflush to non-zero if the caller should
780 * flush the object from the CPU cache.
781 */
782int i915_gem_obj_prepare_shmem_read(struct drm_i915_gem_object *obj,
Chris Wilson43394c72016-08-18 17:16:47 +0100783 unsigned int *needs_clflush)
Brad Volkin4c914c02014-02-18 10:15:45 -0800784{
785 int ret;
786
Chris Wilsone95433c2016-10-28 13:58:27 +0100787 lockdep_assert_held(&obj->base.dev->struct_mutex);
Brad Volkin4c914c02014-02-18 10:15:45 -0800788
Chris Wilsone95433c2016-10-28 13:58:27 +0100789 *needs_clflush = 0;
Chris Wilson43394c72016-08-18 17:16:47 +0100790 if (!i915_gem_object_has_struct_page(obj))
791 return -ENODEV;
Brad Volkin4c914c02014-02-18 10:15:45 -0800792
Chris Wilsone95433c2016-10-28 13:58:27 +0100793 ret = i915_gem_object_wait(obj,
794 I915_WAIT_INTERRUPTIBLE |
795 I915_WAIT_LOCKED,
Chris Wilson62eb3c22019-02-13 09:25:04 +0000796 MAX_SCHEDULE_TIMEOUT);
Chris Wilsonc13d87e2016-07-20 09:21:15 +0100797 if (ret)
798 return ret;
799
Chris Wilsona4f5ea62016-10-28 13:58:35 +0100800 ret = i915_gem_object_pin_pages(obj);
Chris Wilson97649512016-08-18 17:16:50 +0100801 if (ret)
802 return ret;
803
Chris Wilsonb8f55be2017-08-11 12:11:16 +0100804 if (obj->cache_coherent & I915_BO_CACHE_COHERENT_FOR_READ ||
805 !static_cpu_has(X86_FEATURE_CLFLUSH)) {
Chris Wilson7f5f95d2017-03-10 00:09:42 +0000806 ret = i915_gem_object_set_to_cpu_domain(obj, false);
807 if (ret)
808 goto err_unpin;
809 else
810 goto out;
811 }
812
Chris Wilsonef749212017-04-12 12:01:10 +0100813 flush_write_domain(obj, ~I915_GEM_DOMAIN_CPU);
Chris Wilsona314d5c2016-08-18 17:16:48 +0100814
Chris Wilson43394c72016-08-18 17:16:47 +0100815 /* If we're not in the cpu read domain, set ourself into the gtt
816 * read domain and manually flush cachelines (if required). This
817 * optimizes for the case when the gpu will dirty the data
818 * anyway again before the next pread happens.
819 */
Chris Wilsone27ab732017-06-15 13:38:49 +0100820 if (!obj->cache_dirty &&
Christian Königc0a51fd2018-02-16 13:43:38 +0100821 !(obj->read_domains & I915_GEM_DOMAIN_CPU))
Chris Wilson7f5f95d2017-03-10 00:09:42 +0000822 *needs_clflush = CLFLUSH_BEFORE;
Brad Volkin4c914c02014-02-18 10:15:45 -0800823
Chris Wilson7f5f95d2017-03-10 00:09:42 +0000824out:
Chris Wilson97649512016-08-18 17:16:50 +0100825 /* return with the pages pinned */
Chris Wilson43394c72016-08-18 17:16:47 +0100826 return 0;
Chris Wilson97649512016-08-18 17:16:50 +0100827
828err_unpin:
829 i915_gem_object_unpin_pages(obj);
830 return ret;
Chris Wilson43394c72016-08-18 17:16:47 +0100831}
832
833int i915_gem_obj_prepare_shmem_write(struct drm_i915_gem_object *obj,
834 unsigned int *needs_clflush)
835{
836 int ret;
837
Chris Wilsone95433c2016-10-28 13:58:27 +0100838 lockdep_assert_held(&obj->base.dev->struct_mutex);
839
Chris Wilson43394c72016-08-18 17:16:47 +0100840 *needs_clflush = 0;
841 if (!i915_gem_object_has_struct_page(obj))
842 return -ENODEV;
843
Chris Wilsone95433c2016-10-28 13:58:27 +0100844 ret = i915_gem_object_wait(obj,
845 I915_WAIT_INTERRUPTIBLE |
846 I915_WAIT_LOCKED |
847 I915_WAIT_ALL,
Chris Wilson62eb3c22019-02-13 09:25:04 +0000848 MAX_SCHEDULE_TIMEOUT);
Chris Wilson43394c72016-08-18 17:16:47 +0100849 if (ret)
850 return ret;
851
Chris Wilsona4f5ea62016-10-28 13:58:35 +0100852 ret = i915_gem_object_pin_pages(obj);
Chris Wilson97649512016-08-18 17:16:50 +0100853 if (ret)
854 return ret;
855
Chris Wilsonb8f55be2017-08-11 12:11:16 +0100856 if (obj->cache_coherent & I915_BO_CACHE_COHERENT_FOR_WRITE ||
857 !static_cpu_has(X86_FEATURE_CLFLUSH)) {
Chris Wilson7f5f95d2017-03-10 00:09:42 +0000858 ret = i915_gem_object_set_to_cpu_domain(obj, true);
859 if (ret)
860 goto err_unpin;
861 else
862 goto out;
863 }
864
Chris Wilsonef749212017-04-12 12:01:10 +0100865 flush_write_domain(obj, ~I915_GEM_DOMAIN_CPU);
Chris Wilsona314d5c2016-08-18 17:16:48 +0100866
Chris Wilson43394c72016-08-18 17:16:47 +0100867 /* If we're not in the cpu write domain, set ourself into the
868 * gtt write domain and manually flush cachelines (as required).
869 * This optimizes for the case when the gpu will use the data
870 * right away and we therefore have to clflush anyway.
871 */
Chris Wilsone27ab732017-06-15 13:38:49 +0100872 if (!obj->cache_dirty) {
Chris Wilson7f5f95d2017-03-10 00:09:42 +0000873 *needs_clflush |= CLFLUSH_AFTER;
Chris Wilson43394c72016-08-18 17:16:47 +0100874
Chris Wilsone27ab732017-06-15 13:38:49 +0100875 /*
876 * Same trick applies to invalidate partially written
877 * cachelines read before writing.
878 */
Christian Königc0a51fd2018-02-16 13:43:38 +0100879 if (!(obj->read_domains & I915_GEM_DOMAIN_CPU))
Chris Wilsone27ab732017-06-15 13:38:49 +0100880 *needs_clflush |= CLFLUSH_BEFORE;
881 }
Chris Wilson43394c72016-08-18 17:16:47 +0100882
Chris Wilson7f5f95d2017-03-10 00:09:42 +0000883out:
Chris Wilson43394c72016-08-18 17:16:47 +0100884 intel_fb_obj_invalidate(obj, ORIGIN_CPU);
Chris Wilsona4f5ea62016-10-28 13:58:35 +0100885 obj->mm.dirty = true;
Chris Wilson97649512016-08-18 17:16:50 +0100886 /* return with the pages pinned */
Chris Wilson43394c72016-08-18 17:16:47 +0100887 return 0;
Chris Wilson97649512016-08-18 17:16:50 +0100888
889err_unpin:
890 i915_gem_object_unpin_pages(obj);
891 return ret;
Brad Volkin4c914c02014-02-18 10:15:45 -0800892}
893
Daniel Vetterd174bd62012-03-25 19:47:40 +0200894static int
Chris Wilsonb9d126e2019-01-05 12:07:58 +0000895shmem_pread(struct page *page, int offset, int len, char __user *user_data,
896 bool needs_clflush)
Daniel Vetterd174bd62012-03-25 19:47:40 +0200897{
898 char *vaddr;
899 int ret;
900
901 vaddr = kmap(page);
Daniel Vetterd174bd62012-03-25 19:47:40 +0200902
Chris Wilsonb9d126e2019-01-05 12:07:58 +0000903 if (needs_clflush)
904 drm_clflush_virt_range(vaddr + offset, len);
905
906 ret = __copy_to_user(user_data, vaddr + offset, len);
907
Daniel Vetterd174bd62012-03-25 19:47:40 +0200908 kunmap(page);
909
Chris Wilsonb9d126e2019-01-05 12:07:58 +0000910 return ret ? -EFAULT : 0;
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100911}
912
913static int
914i915_gem_shmem_pread(struct drm_i915_gem_object *obj,
915 struct drm_i915_gem_pread *args)
916{
917 char __user *user_data;
918 u64 remain;
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100919 unsigned int needs_clflush;
920 unsigned int idx, offset;
921 int ret;
922
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100923 ret = mutex_lock_interruptible(&obj->base.dev->struct_mutex);
924 if (ret)
925 return ret;
926
927 ret = i915_gem_obj_prepare_shmem_read(obj, &needs_clflush);
928 mutex_unlock(&obj->base.dev->struct_mutex);
929 if (ret)
930 return ret;
931
932 remain = args->size;
933 user_data = u64_to_user_ptr(args->data_ptr);
934 offset = offset_in_page(args->offset);
935 for (idx = args->offset >> PAGE_SHIFT; remain; idx++) {
936 struct page *page = i915_gem_object_get_page(obj, idx);
Chris Wilsona5e856a52018-10-12 15:02:28 +0100937 unsigned int length = min_t(u64, remain, PAGE_SIZE - offset);
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100938
939 ret = shmem_pread(page, offset, length, user_data,
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100940 needs_clflush);
941 if (ret)
942 break;
943
944 remain -= length;
945 user_data += length;
946 offset = 0;
947 }
948
949 i915_gem_obj_finish_shmem_access(obj);
950 return ret;
951}
952
953static inline bool
954gtt_user_read(struct io_mapping *mapping,
955 loff_t base, int offset,
956 char __user *user_data, int length)
957{
Ville Syrjäläafe722b2017-09-01 20:12:52 +0300958 void __iomem *vaddr;
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100959 unsigned long unwritten;
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +0530960
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +0530961 /* We can use the cpu mem copy function because this is X86. */
Ville Syrjäläafe722b2017-09-01 20:12:52 +0300962 vaddr = io_mapping_map_atomic_wc(mapping, base);
963 unwritten = __copy_to_user_inatomic(user_data,
964 (void __force *)vaddr + offset,
965 length);
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100966 io_mapping_unmap_atomic(vaddr);
967 if (unwritten) {
Ville Syrjäläafe722b2017-09-01 20:12:52 +0300968 vaddr = io_mapping_map_wc(mapping, base, PAGE_SIZE);
969 unwritten = copy_to_user(user_data,
970 (void __force *)vaddr + offset,
971 length);
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100972 io_mapping_unmap(vaddr);
973 }
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +0530974 return unwritten;
975}
976
977static int
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100978i915_gem_gtt_pread(struct drm_i915_gem_object *obj,
979 const struct drm_i915_gem_pread *args)
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +0530980{
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100981 struct drm_i915_private *i915 = to_i915(obj->base.dev);
982 struct i915_ggtt *ggtt = &i915->ggtt;
Chris Wilson538ef962019-01-14 14:21:18 +0000983 intel_wakeref_t wakeref;
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +0530984 struct drm_mm_node node;
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100985 struct i915_vma *vma;
986 void __user *user_data;
987 u64 remain, offset;
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +0530988 int ret;
989
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100990 ret = mutex_lock_interruptible(&i915->drm.struct_mutex);
991 if (ret)
992 return ret;
993
Chris Wilson538ef962019-01-14 14:21:18 +0000994 wakeref = intel_runtime_pm_get(i915);
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100995 vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0,
Chris Wilsona3259ca2017-10-09 09:44:00 +0100996 PIN_MAPPABLE |
997 PIN_NONFAULT |
998 PIN_NONBLOCK);
Chris Wilson18034582016-08-18 17:16:45 +0100999 if (!IS_ERR(vma)) {
1000 node.start = i915_ggtt_offset(vma);
1001 node.allocated = false;
Chris Wilson49ef5292016-08-18 17:17:00 +01001002 ret = i915_vma_put_fence(vma);
Chris Wilson18034582016-08-18 17:16:45 +01001003 if (ret) {
1004 i915_vma_unpin(vma);
1005 vma = ERR_PTR(ret);
1006 }
1007 }
Chris Wilson058d88c2016-08-15 10:49:06 +01001008 if (IS_ERR(vma)) {
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001009 ret = insert_mappable_node(ggtt, &node, PAGE_SIZE);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301010 if (ret)
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001011 goto out_unlock;
1012 GEM_BUG_ON(!node.allocated);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301013 }
1014
1015 ret = i915_gem_object_set_to_gtt_domain(obj, false);
1016 if (ret)
1017 goto out_unpin;
1018
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001019 mutex_unlock(&i915->drm.struct_mutex);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301020
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001021 user_data = u64_to_user_ptr(args->data_ptr);
1022 remain = args->size;
1023 offset = args->offset;
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301024
1025 while (remain > 0) {
1026 /* Operation in this page
1027 *
1028 * page_base = page offset within aperture
1029 * page_offset = offset within page
1030 * page_length = bytes to copy for this page
1031 */
1032 u32 page_base = node.start;
1033 unsigned page_offset = offset_in_page(offset);
1034 unsigned page_length = PAGE_SIZE - page_offset;
1035 page_length = remain < page_length ? remain : page_length;
1036 if (node.allocated) {
1037 wmb();
Chris Wilson82ad6442018-06-05 16:37:58 +01001038 ggtt->vm.insert_page(&ggtt->vm,
1039 i915_gem_object_get_dma_address(obj, offset >> PAGE_SHIFT),
1040 node.start, I915_CACHE_NONE, 0);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301041 wmb();
1042 } else {
1043 page_base += offset & PAGE_MASK;
1044 }
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001045
Matthew Auld73ebd502017-12-11 15:18:20 +00001046 if (gtt_user_read(&ggtt->iomap, page_base, page_offset,
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001047 user_data, page_length)) {
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301048 ret = -EFAULT;
1049 break;
1050 }
1051
1052 remain -= page_length;
1053 user_data += page_length;
1054 offset += page_length;
1055 }
1056
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001057 mutex_lock(&i915->drm.struct_mutex);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301058out_unpin:
1059 if (node.allocated) {
1060 wmb();
Chris Wilson82ad6442018-06-05 16:37:58 +01001061 ggtt->vm.clear_range(&ggtt->vm, node.start, node.size);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301062 remove_mappable_node(&node);
1063 } else {
Chris Wilson058d88c2016-08-15 10:49:06 +01001064 i915_vma_unpin(vma);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301065 }
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001066out_unlock:
Chris Wilson538ef962019-01-14 14:21:18 +00001067 intel_runtime_pm_put(i915, wakeref);
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001068 mutex_unlock(&i915->drm.struct_mutex);
Chris Wilsonf60d7f02012-09-04 21:02:56 +01001069
Eric Anholteb014592009-03-10 11:44:52 -07001070 return ret;
1071}
1072
Eric Anholt673a3942008-07-30 12:06:12 -07001073/**
1074 * Reads data from the object referenced by handle.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01001075 * @dev: drm device pointer
1076 * @data: ioctl data blob
1077 * @file: drm file pointer
Eric Anholt673a3942008-07-30 12:06:12 -07001078 *
1079 * On error, the contents of *data are undefined.
1080 */
1081int
1082i915_gem_pread_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00001083 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001084{
1085 struct drm_i915_gem_pread *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00001086 struct drm_i915_gem_object *obj;
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001087 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07001088
Chris Wilson51311d02010-11-17 09:10:42 +00001089 if (args->size == 0)
1090 return 0;
1091
Linus Torvalds96d4f262019-01-03 18:57:57 -08001092 if (!access_ok(u64_to_user_ptr(args->data_ptr),
Chris Wilson51311d02010-11-17 09:10:42 +00001093 args->size))
1094 return -EFAULT;
1095
Chris Wilson03ac0642016-07-20 13:31:51 +01001096 obj = i915_gem_object_lookup(file, args->handle);
Chris Wilson258a5ed2016-08-05 10:14:16 +01001097 if (!obj)
1098 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07001099
Chris Wilson7dcd2492010-09-26 20:21:44 +01001100 /* Bounds check source. */
Matthew Auld966d5bf2016-12-13 20:32:22 +00001101 if (range_overflows_t(u64, args->offset, args->size, obj->base.size)) {
Chris Wilsonce9d4192010-09-26 20:50:05 +01001102 ret = -EINVAL;
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001103 goto out;
Chris Wilsonce9d4192010-09-26 20:50:05 +01001104 }
1105
Chris Wilsondb53a302011-02-03 11:57:46 +00001106 trace_i915_gem_object_pread(obj, args->offset, args->size);
1107
Chris Wilsone95433c2016-10-28 13:58:27 +01001108 ret = i915_gem_object_wait(obj,
1109 I915_WAIT_INTERRUPTIBLE,
Chris Wilson62eb3c22019-02-13 09:25:04 +00001110 MAX_SCHEDULE_TIMEOUT);
Chris Wilson258a5ed2016-08-05 10:14:16 +01001111 if (ret)
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001112 goto out;
Chris Wilson258a5ed2016-08-05 10:14:16 +01001113
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001114 ret = i915_gem_object_pin_pages(obj);
Chris Wilson258a5ed2016-08-05 10:14:16 +01001115 if (ret)
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001116 goto out;
Chris Wilson258a5ed2016-08-05 10:14:16 +01001117
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001118 ret = i915_gem_shmem_pread(obj, args);
Chris Wilson9c870d02016-10-24 13:42:15 +01001119 if (ret == -EFAULT || ret == -ENODEV)
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001120 ret = i915_gem_gtt_pread(obj, args);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301121
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001122 i915_gem_object_unpin_pages(obj);
1123out:
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001124 i915_gem_object_put(obj);
Eric Anholteb014592009-03-10 11:44:52 -07001125 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07001126}
1127
Keith Packard0839ccb2008-10-30 19:38:48 -07001128/* This is the fast write path which cannot handle
1129 * page faults in the source data
Linus Torvalds9b7530cc2008-10-20 14:16:43 -07001130 */
Linus Torvalds9b7530cc2008-10-20 14:16:43 -07001131
Chris Wilsonfe115622016-10-28 13:58:40 +01001132static inline bool
1133ggtt_write(struct io_mapping *mapping,
1134 loff_t base, int offset,
1135 char __user *user_data, int length)
Keith Packard0839ccb2008-10-30 19:38:48 -07001136{
Ville Syrjäläafe722b2017-09-01 20:12:52 +03001137 void __iomem *vaddr;
Keith Packard0839ccb2008-10-30 19:38:48 -07001138 unsigned long unwritten;
1139
Ben Widawsky4f0c7cf2012-04-16 14:07:47 -07001140 /* We can use the cpu mem copy function because this is X86. */
Ville Syrjäläafe722b2017-09-01 20:12:52 +03001141 vaddr = io_mapping_map_atomic_wc(mapping, base);
1142 unwritten = __copy_from_user_inatomic_nocache((void __force *)vaddr + offset,
Keith Packard0839ccb2008-10-30 19:38:48 -07001143 user_data, length);
Chris Wilsonfe115622016-10-28 13:58:40 +01001144 io_mapping_unmap_atomic(vaddr);
1145 if (unwritten) {
Ville Syrjäläafe722b2017-09-01 20:12:52 +03001146 vaddr = io_mapping_map_wc(mapping, base, PAGE_SIZE);
1147 unwritten = copy_from_user((void __force *)vaddr + offset,
1148 user_data, length);
Chris Wilsonfe115622016-10-28 13:58:40 +01001149 io_mapping_unmap(vaddr);
1150 }
Keith Packard0839ccb2008-10-30 19:38:48 -07001151
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001152 return unwritten;
1153}
1154
Eric Anholt3de09aa2009-03-09 09:42:23 -07001155/**
1156 * This is the fast pwrite path, where we copy the data directly from the
1157 * user into the GTT, uncached.
Chris Wilsonfe115622016-10-28 13:58:40 +01001158 * @obj: i915 GEM object
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01001159 * @args: pwrite arguments structure
Eric Anholt3de09aa2009-03-09 09:42:23 -07001160 */
Eric Anholt673a3942008-07-30 12:06:12 -07001161static int
Chris Wilsonfe115622016-10-28 13:58:40 +01001162i915_gem_gtt_pwrite_fast(struct drm_i915_gem_object *obj,
1163 const struct drm_i915_gem_pwrite *args)
Eric Anholt673a3942008-07-30 12:06:12 -07001164{
Chris Wilsonfe115622016-10-28 13:58:40 +01001165 struct drm_i915_private *i915 = to_i915(obj->base.dev);
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301166 struct i915_ggtt *ggtt = &i915->ggtt;
Chris Wilson538ef962019-01-14 14:21:18 +00001167 intel_wakeref_t wakeref;
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301168 struct drm_mm_node node;
Chris Wilsonfe115622016-10-28 13:58:40 +01001169 struct i915_vma *vma;
1170 u64 remain, offset;
1171 void __user *user_data;
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301172 int ret;
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301173
Chris Wilsonfe115622016-10-28 13:58:40 +01001174 ret = mutex_lock_interruptible(&i915->drm.struct_mutex);
1175 if (ret)
1176 return ret;
Daniel Vetter935aaa62012-03-25 19:47:35 +02001177
Chris Wilson8bd818152017-10-19 07:37:33 +01001178 if (i915_gem_object_has_struct_page(obj)) {
1179 /*
1180 * Avoid waking the device up if we can fallback, as
1181 * waking/resuming is very slow (worst-case 10-100 ms
1182 * depending on PCI sleeps and our own resume time).
1183 * This easily dwarfs any performance advantage from
1184 * using the cache bypass of indirect GGTT access.
1185 */
Chris Wilson538ef962019-01-14 14:21:18 +00001186 wakeref = intel_runtime_pm_get_if_in_use(i915);
1187 if (!wakeref) {
Chris Wilson8bd818152017-10-19 07:37:33 +01001188 ret = -EFAULT;
1189 goto out_unlock;
1190 }
1191 } else {
1192 /* No backing pages, no fallback, we must force GGTT access */
Chris Wilson538ef962019-01-14 14:21:18 +00001193 wakeref = intel_runtime_pm_get(i915);
Chris Wilson8bd818152017-10-19 07:37:33 +01001194 }
1195
Chris Wilson058d88c2016-08-15 10:49:06 +01001196 vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0,
Chris Wilsona3259ca2017-10-09 09:44:00 +01001197 PIN_MAPPABLE |
1198 PIN_NONFAULT |
1199 PIN_NONBLOCK);
Chris Wilson18034582016-08-18 17:16:45 +01001200 if (!IS_ERR(vma)) {
1201 node.start = i915_ggtt_offset(vma);
1202 node.allocated = false;
Chris Wilson49ef5292016-08-18 17:17:00 +01001203 ret = i915_vma_put_fence(vma);
Chris Wilson18034582016-08-18 17:16:45 +01001204 if (ret) {
1205 i915_vma_unpin(vma);
1206 vma = ERR_PTR(ret);
1207 }
1208 }
Chris Wilson058d88c2016-08-15 10:49:06 +01001209 if (IS_ERR(vma)) {
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001210 ret = insert_mappable_node(ggtt, &node, PAGE_SIZE);
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301211 if (ret)
Chris Wilson8bd818152017-10-19 07:37:33 +01001212 goto out_rpm;
Chris Wilsonfe115622016-10-28 13:58:40 +01001213 GEM_BUG_ON(!node.allocated);
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301214 }
Daniel Vetter935aaa62012-03-25 19:47:35 +02001215
1216 ret = i915_gem_object_set_to_gtt_domain(obj, true);
1217 if (ret)
1218 goto out_unpin;
1219
Chris Wilsonfe115622016-10-28 13:58:40 +01001220 mutex_unlock(&i915->drm.struct_mutex);
1221
Chris Wilsonb19482d2016-08-18 17:16:43 +01001222 intel_fb_obj_invalidate(obj, ORIGIN_CPU);
Paulo Zanoni063e4e62015-02-13 17:23:45 -02001223
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301224 user_data = u64_to_user_ptr(args->data_ptr);
1225 offset = args->offset;
1226 remain = args->size;
1227 while (remain) {
Eric Anholt673a3942008-07-30 12:06:12 -07001228 /* Operation in this page
1229 *
Keith Packard0839ccb2008-10-30 19:38:48 -07001230 * page_base = page offset within aperture
1231 * page_offset = offset within page
1232 * page_length = bytes to copy for this page
Eric Anholt673a3942008-07-30 12:06:12 -07001233 */
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301234 u32 page_base = node.start;
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001235 unsigned int page_offset = offset_in_page(offset);
1236 unsigned int page_length = PAGE_SIZE - page_offset;
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301237 page_length = remain < page_length ? remain : page_length;
1238 if (node.allocated) {
1239 wmb(); /* flush the write before we modify the GGTT */
Chris Wilson82ad6442018-06-05 16:37:58 +01001240 ggtt->vm.insert_page(&ggtt->vm,
1241 i915_gem_object_get_dma_address(obj, offset >> PAGE_SHIFT),
1242 node.start, I915_CACHE_NONE, 0);
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301243 wmb(); /* flush modifications to the GGTT (insert_page) */
1244 } else {
1245 page_base += offset & PAGE_MASK;
1246 }
Keith Packard0839ccb2008-10-30 19:38:48 -07001247 /* If we get a fault while copying data, then (presumably) our
Eric Anholt3de09aa2009-03-09 09:42:23 -07001248 * source page isn't available. Return the error and we'll
1249 * retry in the slow path.
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301250 * If the object is non-shmem backed, we retry again with the
1251 * path that handles page fault.
Keith Packard0839ccb2008-10-30 19:38:48 -07001252 */
Matthew Auld73ebd502017-12-11 15:18:20 +00001253 if (ggtt_write(&ggtt->iomap, page_base, page_offset,
Chris Wilsonfe115622016-10-28 13:58:40 +01001254 user_data, page_length)) {
1255 ret = -EFAULT;
1256 break;
Daniel Vetter935aaa62012-03-25 19:47:35 +02001257 }
Eric Anholt673a3942008-07-30 12:06:12 -07001258
Keith Packard0839ccb2008-10-30 19:38:48 -07001259 remain -= page_length;
1260 user_data += page_length;
1261 offset += page_length;
Eric Anholt673a3942008-07-30 12:06:12 -07001262 }
Chris Wilsond59b21e2017-02-22 11:40:49 +00001263 intel_fb_obj_flush(obj, ORIGIN_CPU);
Chris Wilsonfe115622016-10-28 13:58:40 +01001264
1265 mutex_lock(&i915->drm.struct_mutex);
Daniel Vetter935aaa62012-03-25 19:47:35 +02001266out_unpin:
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301267 if (node.allocated) {
1268 wmb();
Chris Wilson82ad6442018-06-05 16:37:58 +01001269 ggtt->vm.clear_range(&ggtt->vm, node.start, node.size);
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301270 remove_mappable_node(&node);
1271 } else {
Chris Wilson058d88c2016-08-15 10:49:06 +01001272 i915_vma_unpin(vma);
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301273 }
Chris Wilson8bd818152017-10-19 07:37:33 +01001274out_rpm:
Chris Wilson538ef962019-01-14 14:21:18 +00001275 intel_runtime_pm_put(i915, wakeref);
Chris Wilson8bd818152017-10-19 07:37:33 +01001276out_unlock:
Chris Wilsonfe115622016-10-28 13:58:40 +01001277 mutex_unlock(&i915->drm.struct_mutex);
Eric Anholt3de09aa2009-03-09 09:42:23 -07001278 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07001279}
1280
Chris Wilsonfe115622016-10-28 13:58:40 +01001281/* Per-page copy function for the shmem pwrite fastpath.
1282 * Flushes invalid cachelines before writing to the target if
1283 * needs_clflush_before is set and flushes out any written cachelines after
1284 * writing if needs_clflush is set.
1285 */
Eric Anholt40123c12009-03-09 13:42:30 -07001286static int
Chris Wilsonfe115622016-10-28 13:58:40 +01001287shmem_pwrite(struct page *page, int offset, int len, char __user *user_data,
Chris Wilsonfe115622016-10-28 13:58:40 +01001288 bool needs_clflush_before,
1289 bool needs_clflush_after)
Eric Anholt40123c12009-03-09 13:42:30 -07001290{
Chris Wilsonb9d126e2019-01-05 12:07:58 +00001291 char *vaddr;
Chris Wilsonfe115622016-10-28 13:58:40 +01001292 int ret;
Eric Anholt40123c12009-03-09 13:42:30 -07001293
Chris Wilsonb9d126e2019-01-05 12:07:58 +00001294 vaddr = kmap(page);
Chris Wilsonfe115622016-10-28 13:58:40 +01001295
Chris Wilsonb9d126e2019-01-05 12:07:58 +00001296 if (needs_clflush_before)
1297 drm_clflush_virt_range(vaddr + offset, len);
Chris Wilsonfe115622016-10-28 13:58:40 +01001298
Chris Wilsonb9d126e2019-01-05 12:07:58 +00001299 ret = __copy_from_user(vaddr + offset, user_data, len);
1300 if (!ret && needs_clflush_after)
1301 drm_clflush_virt_range(vaddr + offset, len);
Chris Wilsonfe115622016-10-28 13:58:40 +01001302
Chris Wilsonb9d126e2019-01-05 12:07:58 +00001303 kunmap(page);
1304
1305 return ret ? -EFAULT : 0;
Chris Wilsonfe115622016-10-28 13:58:40 +01001306}
1307
1308static int
1309i915_gem_shmem_pwrite(struct drm_i915_gem_object *obj,
1310 const struct drm_i915_gem_pwrite *args)
1311{
1312 struct drm_i915_private *i915 = to_i915(obj->base.dev);
1313 void __user *user_data;
1314 u64 remain;
Chris Wilsonfe115622016-10-28 13:58:40 +01001315 unsigned int partial_cacheline_write;
1316 unsigned int needs_clflush;
1317 unsigned int offset, idx;
1318 int ret;
1319
1320 ret = mutex_lock_interruptible(&i915->drm.struct_mutex);
Chris Wilson43394c72016-08-18 17:16:47 +01001321 if (ret)
1322 return ret;
Eric Anholt40123c12009-03-09 13:42:30 -07001323
Chris Wilsonfe115622016-10-28 13:58:40 +01001324 ret = i915_gem_obj_prepare_shmem_write(obj, &needs_clflush);
1325 mutex_unlock(&i915->drm.struct_mutex);
1326 if (ret)
1327 return ret;
1328
Chris Wilsonfe115622016-10-28 13:58:40 +01001329 /* If we don't overwrite a cacheline completely we need to be
1330 * careful to have up-to-date data by first clflushing. Don't
1331 * overcomplicate things and flush the entire patch.
1332 */
1333 partial_cacheline_write = 0;
1334 if (needs_clflush & CLFLUSH_BEFORE)
1335 partial_cacheline_write = boot_cpu_data.x86_clflush_size - 1;
1336
Chris Wilson43394c72016-08-18 17:16:47 +01001337 user_data = u64_to_user_ptr(args->data_ptr);
Chris Wilson43394c72016-08-18 17:16:47 +01001338 remain = args->size;
Chris Wilsonfe115622016-10-28 13:58:40 +01001339 offset = offset_in_page(args->offset);
1340 for (idx = args->offset >> PAGE_SHIFT; remain; idx++) {
1341 struct page *page = i915_gem_object_get_page(obj, idx);
Chris Wilsona5e856a52018-10-12 15:02:28 +01001342 unsigned int length = min_t(u64, remain, PAGE_SIZE - offset);
Chris Wilsone5281cc2010-10-28 13:45:36 +01001343
Chris Wilsonfe115622016-10-28 13:58:40 +01001344 ret = shmem_pwrite(page, offset, length, user_data,
Chris Wilsonfe115622016-10-28 13:58:40 +01001345 (offset | length) & partial_cacheline_write,
1346 needs_clflush & CLFLUSH_AFTER);
1347 if (ret)
Chris Wilson9da3da62012-06-01 15:20:22 +01001348 break;
1349
Chris Wilsonfe115622016-10-28 13:58:40 +01001350 remain -= length;
1351 user_data += length;
1352 offset = 0;
Eric Anholt40123c12009-03-09 13:42:30 -07001353 }
1354
Chris Wilsond59b21e2017-02-22 11:40:49 +00001355 intel_fb_obj_flush(obj, ORIGIN_CPU);
Chris Wilsonfe115622016-10-28 13:58:40 +01001356 i915_gem_obj_finish_shmem_access(obj);
Eric Anholt40123c12009-03-09 13:42:30 -07001357 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07001358}
1359
1360/**
1361 * Writes data to the object referenced by handle.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01001362 * @dev: drm device
1363 * @data: ioctl data blob
1364 * @file: drm file
Eric Anholt673a3942008-07-30 12:06:12 -07001365 *
1366 * On error, the contents of the buffer that were to be modified are undefined.
1367 */
1368int
1369i915_gem_pwrite_ioctl(struct drm_device *dev, void *data,
Chris Wilsonfbd5a262010-10-14 15:03:58 +01001370 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001371{
1372 struct drm_i915_gem_pwrite *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00001373 struct drm_i915_gem_object *obj;
Chris Wilson51311d02010-11-17 09:10:42 +00001374 int ret;
1375
1376 if (args->size == 0)
1377 return 0;
1378
Linus Torvalds96d4f262019-01-03 18:57:57 -08001379 if (!access_ok(u64_to_user_ptr(args->data_ptr), args->size))
Chris Wilson51311d02010-11-17 09:10:42 +00001380 return -EFAULT;
1381
Chris Wilson03ac0642016-07-20 13:31:51 +01001382 obj = i915_gem_object_lookup(file, args->handle);
Chris Wilson258a5ed2016-08-05 10:14:16 +01001383 if (!obj)
1384 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07001385
Chris Wilson7dcd2492010-09-26 20:21:44 +01001386 /* Bounds check destination. */
Matthew Auld966d5bf2016-12-13 20:32:22 +00001387 if (range_overflows_t(u64, args->offset, args->size, obj->base.size)) {
Chris Wilsonce9d4192010-09-26 20:50:05 +01001388 ret = -EINVAL;
Chris Wilson258a5ed2016-08-05 10:14:16 +01001389 goto err;
Chris Wilsonce9d4192010-09-26 20:50:05 +01001390 }
1391
Chris Wilsonf8c1cce2018-07-12 19:53:14 +01001392 /* Writes not allowed into this read-only object */
1393 if (i915_gem_object_is_readonly(obj)) {
1394 ret = -EINVAL;
1395 goto err;
1396 }
1397
Chris Wilsondb53a302011-02-03 11:57:46 +00001398 trace_i915_gem_object_pwrite(obj, args->offset, args->size);
1399
Chris Wilson7c55e2c2017-03-07 12:03:38 +00001400 ret = -ENODEV;
1401 if (obj->ops->pwrite)
1402 ret = obj->ops->pwrite(obj, args);
1403 if (ret != -ENODEV)
1404 goto err;
1405
Chris Wilsone95433c2016-10-28 13:58:27 +01001406 ret = i915_gem_object_wait(obj,
1407 I915_WAIT_INTERRUPTIBLE |
1408 I915_WAIT_ALL,
Chris Wilson62eb3c22019-02-13 09:25:04 +00001409 MAX_SCHEDULE_TIMEOUT);
Chris Wilson258a5ed2016-08-05 10:14:16 +01001410 if (ret)
1411 goto err;
1412
Chris Wilsonfe115622016-10-28 13:58:40 +01001413 ret = i915_gem_object_pin_pages(obj);
Chris Wilson258a5ed2016-08-05 10:14:16 +01001414 if (ret)
Chris Wilsonfe115622016-10-28 13:58:40 +01001415 goto err;
Chris Wilson258a5ed2016-08-05 10:14:16 +01001416
Daniel Vetter935aaa62012-03-25 19:47:35 +02001417 ret = -EFAULT;
Eric Anholt673a3942008-07-30 12:06:12 -07001418 /* We can only do the GTT pwrite on untiled buffers, as otherwise
1419 * it would end up going through the fenced access, and we'll get
1420 * different detiling behavior between reading and writing.
1421 * pread/pwrite currently are reading and writing from the CPU
1422 * perspective, requiring manual detiling by the client.
1423 */
Chris Wilson6eae0052016-06-20 15:05:52 +01001424 if (!i915_gem_object_has_struct_page(obj) ||
Chris Wilson9c870d02016-10-24 13:42:15 +01001425 cpu_write_needs_clflush(obj))
Daniel Vetter935aaa62012-03-25 19:47:35 +02001426 /* Note that the gtt paths might fail with non-page-backed user
1427 * pointers (e.g. gtt mappings when moving data between
Chris Wilson9c870d02016-10-24 13:42:15 +01001428 * textures). Fallback to the shmem path in that case.
1429 */
Chris Wilsonfe115622016-10-28 13:58:40 +01001430 ret = i915_gem_gtt_pwrite_fast(obj, args);
Eric Anholt673a3942008-07-30 12:06:12 -07001431
Chris Wilsond1054ee2016-07-16 18:42:36 +01001432 if (ret == -EFAULT || ret == -ENOSPC) {
Chris Wilson6a2c4232014-11-04 04:51:40 -08001433 if (obj->phys_handle)
1434 ret = i915_gem_phys_pwrite(obj, args, file);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301435 else
Chris Wilsonfe115622016-10-28 13:58:40 +01001436 ret = i915_gem_shmem_pwrite(obj, args);
Chris Wilson6a2c4232014-11-04 04:51:40 -08001437 }
Daniel Vetter5c0480f2011-12-14 13:57:30 +01001438
Chris Wilsonfe115622016-10-28 13:58:40 +01001439 i915_gem_object_unpin_pages(obj);
Chris Wilson258a5ed2016-08-05 10:14:16 +01001440err:
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001441 i915_gem_object_put(obj);
Chris Wilson258a5ed2016-08-05 10:14:16 +01001442 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07001443}
1444
Chris Wilson40e62d52016-10-28 13:58:41 +01001445static void i915_gem_object_bump_inactive_ggtt(struct drm_i915_gem_object *obj)
1446{
Chris Wilson09d7e462019-01-28 10:23:53 +00001447 struct drm_i915_private *i915 = to_i915(obj->base.dev);
Chris Wilson40e62d52016-10-28 13:58:41 +01001448 struct list_head *list;
1449 struct i915_vma *vma;
1450
Chris Wilsonf2123812017-10-16 12:40:37 +01001451 GEM_BUG_ON(!i915_gem_object_has_pinned_pages(obj));
1452
Chris Wilson09d7e462019-01-28 10:23:53 +00001453 mutex_lock(&i915->ggtt.vm.mutex);
Chris Wilsone2189dd2017-12-07 21:14:07 +00001454 for_each_ggtt_vma(vma, obj) {
Chris Wilson40e62d52016-10-28 13:58:41 +01001455 if (!drm_mm_node_allocated(&vma->node))
1456 continue;
1457
Chris Wilson499197d2019-01-28 10:23:52 +00001458 list_move_tail(&vma->vm_link, &vma->vm->bound_list);
Chris Wilson40e62d52016-10-28 13:58:41 +01001459 }
Chris Wilson09d7e462019-01-28 10:23:53 +00001460 mutex_unlock(&i915->ggtt.vm.mutex);
Chris Wilson40e62d52016-10-28 13:58:41 +01001461
Chris Wilsonf2123812017-10-16 12:40:37 +01001462 spin_lock(&i915->mm.obj_lock);
Chris Wilson40e62d52016-10-28 13:58:41 +01001463 list = obj->bind_count ? &i915->mm.bound_list : &i915->mm.unbound_list;
Chris Wilsonf2123812017-10-16 12:40:37 +01001464 list_move_tail(&obj->mm.link, list);
1465 spin_unlock(&i915->mm.obj_lock);
Chris Wilson40e62d52016-10-28 13:58:41 +01001466}
1467
Eric Anholt673a3942008-07-30 12:06:12 -07001468/**
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001469 * Called when user space prepares to use an object with the CPU, either
1470 * through the mmap ioctl's mapping or a GTT mapping.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01001471 * @dev: drm device
1472 * @data: ioctl data blob
1473 * @file: drm file
Eric Anholt673a3942008-07-30 12:06:12 -07001474 */
1475int
1476i915_gem_set_domain_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00001477 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001478{
1479 struct drm_i915_gem_set_domain *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00001480 struct drm_i915_gem_object *obj;
Jani Nikula739f3ab2019-01-16 11:15:19 +02001481 u32 read_domains = args->read_domains;
1482 u32 write_domain = args->write_domain;
Chris Wilson40e62d52016-10-28 13:58:41 +01001483 int err;
Eric Anholt673a3942008-07-30 12:06:12 -07001484
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001485 /* Only handle setting domains to types used by the CPU. */
Chris Wilsonb8f90962016-08-05 10:14:07 +01001486 if ((write_domain | read_domains) & I915_GEM_GPU_DOMAINS)
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001487 return -EINVAL;
1488
Chris Wilson754a2542019-03-21 16:19:08 +00001489 /*
1490 * Having something in the write domain implies it's in the read
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001491 * domain, and only that read domain. Enforce that in the request.
1492 */
Chris Wilson754a2542019-03-21 16:19:08 +00001493 if (write_domain && read_domains != write_domain)
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001494 return -EINVAL;
1495
Chris Wilson754a2542019-03-21 16:19:08 +00001496 if (!read_domains)
1497 return 0;
1498
Chris Wilson03ac0642016-07-20 13:31:51 +01001499 obj = i915_gem_object_lookup(file, args->handle);
Chris Wilsonb8f90962016-08-05 10:14:07 +01001500 if (!obj)
1501 return -ENOENT;
Jesse Barnes652c3932009-08-17 13:31:43 -07001502
Chris Wilson754a2542019-03-21 16:19:08 +00001503 /*
1504 * Already in the desired write domain? Nothing for us to do!
1505 *
1506 * We apply a little bit of cunning here to catch a broader set of
1507 * no-ops. If obj->write_domain is set, we must be in the same
1508 * obj->read_domains, and only that domain. Therefore, if that
1509 * obj->write_domain matches the request read_domains, we are
1510 * already in the same read/write domain and can skip the operation,
1511 * without having to further check the requested write_domain.
1512 */
1513 if (READ_ONCE(obj->write_domain) == read_domains) {
1514 err = 0;
1515 goto out;
1516 }
1517
1518 /*
1519 * Try to flush the object off the GPU without holding the lock.
Chris Wilson3236f572012-08-24 09:35:09 +01001520 * We will repeat the flush holding the lock in the normal manner
1521 * to catch cases where we are gazumped.
1522 */
Chris Wilson40e62d52016-10-28 13:58:41 +01001523 err = i915_gem_object_wait(obj,
Chris Wilsone95433c2016-10-28 13:58:27 +01001524 I915_WAIT_INTERRUPTIBLE |
Chris Wilsone9eaf822018-10-01 15:47:55 +01001525 I915_WAIT_PRIORITY |
Chris Wilsone95433c2016-10-28 13:58:27 +01001526 (write_domain ? I915_WAIT_ALL : 0),
Chris Wilson62eb3c22019-02-13 09:25:04 +00001527 MAX_SCHEDULE_TIMEOUT);
Chris Wilson40e62d52016-10-28 13:58:41 +01001528 if (err)
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001529 goto out;
Chris Wilsonb8f90962016-08-05 10:14:07 +01001530
Tina Zhanga03f3952017-11-14 10:25:13 +00001531 /*
1532 * Proxy objects do not control access to the backing storage, ergo
1533 * they cannot be used as a means to manipulate the cache domain
1534 * tracking for that backing storage. The proxy object is always
1535 * considered to be outside of any cache domain.
1536 */
1537 if (i915_gem_object_is_proxy(obj)) {
1538 err = -ENXIO;
1539 goto out;
1540 }
1541
1542 /*
1543 * Flush and acquire obj->pages so that we are coherent through
Chris Wilson40e62d52016-10-28 13:58:41 +01001544 * direct access in memory with previous cached writes through
1545 * shmemfs and that our cache domain tracking remains valid.
1546 * For example, if the obj->filp was moved to swap without us
1547 * being notified and releasing the pages, we would mistakenly
1548 * continue to assume that the obj remained out of the CPU cached
1549 * domain.
1550 */
1551 err = i915_gem_object_pin_pages(obj);
1552 if (err)
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001553 goto out;
Chris Wilson40e62d52016-10-28 13:58:41 +01001554
1555 err = i915_mutex_lock_interruptible(dev);
1556 if (err)
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001557 goto out_unpin;
Chris Wilson3236f572012-08-24 09:35:09 +01001558
Chris Wilsone22d8e32017-04-12 12:01:11 +01001559 if (read_domains & I915_GEM_DOMAIN_WC)
1560 err = i915_gem_object_set_to_wc_domain(obj, write_domain);
1561 else if (read_domains & I915_GEM_DOMAIN_GTT)
1562 err = i915_gem_object_set_to_gtt_domain(obj, write_domain);
Chris Wilson43566de2015-01-02 16:29:29 +05301563 else
Chris Wilsone22d8e32017-04-12 12:01:11 +01001564 err = i915_gem_object_set_to_cpu_domain(obj, write_domain);
Chris Wilson40e62d52016-10-28 13:58:41 +01001565
1566 /* And bump the LRU for this access */
1567 i915_gem_object_bump_inactive_ggtt(obj);
1568
1569 mutex_unlock(&dev->struct_mutex);
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001570
Daniel Vetter031b6982015-06-26 19:35:16 +02001571 if (write_domain != 0)
Chris Wilsonef749212017-04-12 12:01:10 +01001572 intel_fb_obj_invalidate(obj,
1573 fb_write_origin(obj, write_domain));
Daniel Vetter031b6982015-06-26 19:35:16 +02001574
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001575out_unpin:
Chris Wilson40e62d52016-10-28 13:58:41 +01001576 i915_gem_object_unpin_pages(obj);
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001577out:
1578 i915_gem_object_put(obj);
Chris Wilson40e62d52016-10-28 13:58:41 +01001579 return err;
Eric Anholt673a3942008-07-30 12:06:12 -07001580}
1581
1582/**
1583 * Called when user space has done writes to this buffer
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01001584 * @dev: drm device
1585 * @data: ioctl data blob
1586 * @file: drm file
Eric Anholt673a3942008-07-30 12:06:12 -07001587 */
1588int
1589i915_gem_sw_finish_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00001590 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001591{
1592 struct drm_i915_gem_sw_finish *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00001593 struct drm_i915_gem_object *obj;
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001594
Chris Wilson03ac0642016-07-20 13:31:51 +01001595 obj = i915_gem_object_lookup(file, args->handle);
Chris Wilsonc21724c2016-08-05 10:14:19 +01001596 if (!obj)
1597 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07001598
Tina Zhanga03f3952017-11-14 10:25:13 +00001599 /*
1600 * Proxy objects are barred from CPU access, so there is no
1601 * need to ban sw_finish as it is a nop.
1602 */
1603
Eric Anholt673a3942008-07-30 12:06:12 -07001604 /* Pinned buffers may be scanout, so flush the cache */
Chris Wilson5a97bcc2017-02-22 11:40:46 +00001605 i915_gem_object_flush_if_display(obj);
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001606 i915_gem_object_put(obj);
Chris Wilson5a97bcc2017-02-22 11:40:46 +00001607
1608 return 0;
Eric Anholt673a3942008-07-30 12:06:12 -07001609}
1610
Joonas Lahtinen5c4604e2019-02-07 10:54:53 +02001611static inline bool
1612__vma_matches(struct vm_area_struct *vma, struct file *filp,
1613 unsigned long addr, unsigned long size)
1614{
1615 if (vma->vm_file != filp)
1616 return false;
1617
Tvrtko Ursulina90e1942019-03-05 11:04:08 +00001618 return vma->vm_start == addr &&
1619 (vma->vm_end - vma->vm_start) == PAGE_ALIGN(size);
Joonas Lahtinen5c4604e2019-02-07 10:54:53 +02001620}
1621
Eric Anholt673a3942008-07-30 12:06:12 -07001622/**
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01001623 * i915_gem_mmap_ioctl - Maps the contents of an object, returning the address
1624 * it is mapped to.
1625 * @dev: drm device
1626 * @data: ioctl data blob
1627 * @file: drm file
Eric Anholt673a3942008-07-30 12:06:12 -07001628 *
1629 * While the mapping holds a reference on the contents of the object, it doesn't
1630 * imply a ref on the object itself.
Daniel Vetter34367382014-10-16 12:28:18 +02001631 *
1632 * IMPORTANT:
1633 *
1634 * DRM driver writers who look a this function as an example for how to do GEM
1635 * mmap support, please don't implement mmap support like here. The modern way
1636 * to implement DRM mmap support is with an mmap offset ioctl (like
1637 * i915_gem_mmap_gtt) and then using the mmap syscall on the DRM fd directly.
1638 * That way debug tooling like valgrind will understand what's going on, hiding
1639 * the mmap call in a driver private ioctl will break that. The i915 driver only
1640 * does cpu mmaps this way because we didn't know better.
Eric Anholt673a3942008-07-30 12:06:12 -07001641 */
1642int
1643i915_gem_mmap_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00001644 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001645{
1646 struct drm_i915_gem_mmap *args = data;
Chris Wilson03ac0642016-07-20 13:31:51 +01001647 struct drm_i915_gem_object *obj;
Eric Anholt673a3942008-07-30 12:06:12 -07001648 unsigned long addr;
1649
Akash Goel1816f922015-01-02 16:29:30 +05301650 if (args->flags & ~(I915_MMAP_WC))
1651 return -EINVAL;
1652
Borislav Petkov568a58e2016-03-29 17:42:01 +02001653 if (args->flags & I915_MMAP_WC && !boot_cpu_has(X86_FEATURE_PAT))
Akash Goel1816f922015-01-02 16:29:30 +05301654 return -ENODEV;
1655
Chris Wilson03ac0642016-07-20 13:31:51 +01001656 obj = i915_gem_object_lookup(file, args->handle);
1657 if (!obj)
Chris Wilsonbf79cb92010-08-04 14:19:46 +01001658 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07001659
Daniel Vetter1286ff72012-05-10 15:25:09 +02001660 /* prime objects have no backing filp to GEM mmap
1661 * pages from.
1662 */
Chris Wilson03ac0642016-07-20 13:31:51 +01001663 if (!obj->base.filp) {
Chris Wilson794a11c2019-03-14 07:58:29 +00001664 addr = -ENXIO;
1665 goto err;
1666 }
1667
1668 if (range_overflows(args->offset, args->size, (u64)obj->base.size)) {
1669 addr = -EINVAL;
1670 goto err;
Daniel Vetter1286ff72012-05-10 15:25:09 +02001671 }
1672
Chris Wilson03ac0642016-07-20 13:31:51 +01001673 addr = vm_mmap(obj->base.filp, 0, args->size,
Eric Anholt673a3942008-07-30 12:06:12 -07001674 PROT_READ | PROT_WRITE, MAP_SHARED,
1675 args->offset);
Joonas Lahtinenebfb6972019-02-07 10:54:54 +02001676 if (IS_ERR_VALUE(addr))
1677 goto err;
1678
Akash Goel1816f922015-01-02 16:29:30 +05301679 if (args->flags & I915_MMAP_WC) {
1680 struct mm_struct *mm = current->mm;
1681 struct vm_area_struct *vma;
1682
Michal Hocko80a89a52016-05-23 16:26:11 -07001683 if (down_write_killable(&mm->mmap_sem)) {
Chris Wilson794a11c2019-03-14 07:58:29 +00001684 addr = -EINTR;
1685 goto err;
Michal Hocko80a89a52016-05-23 16:26:11 -07001686 }
Akash Goel1816f922015-01-02 16:29:30 +05301687 vma = find_vma(mm, addr);
Joonas Lahtinen5c4604e2019-02-07 10:54:53 +02001688 if (vma && __vma_matches(vma, obj->base.filp, addr, args->size))
Akash Goel1816f922015-01-02 16:29:30 +05301689 vma->vm_page_prot =
1690 pgprot_writecombine(vm_get_page_prot(vma->vm_flags));
1691 else
1692 addr = -ENOMEM;
1693 up_write(&mm->mmap_sem);
Joonas Lahtinenebfb6972019-02-07 10:54:54 +02001694 if (IS_ERR_VALUE(addr))
1695 goto err;
Chris Wilsonaeecc962016-06-17 14:46:39 -03001696
1697 /* This may race, but that's ok, it only gets set */
Chris Wilson50349242016-08-18 17:17:04 +01001698 WRITE_ONCE(obj->frontbuffer_ggtt_origin, ORIGIN_CPU);
Akash Goel1816f922015-01-02 16:29:30 +05301699 }
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001700 i915_gem_object_put(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001701
Jani Nikula739f3ab2019-01-16 11:15:19 +02001702 args->addr_ptr = (u64)addr;
Eric Anholt673a3942008-07-30 12:06:12 -07001703 return 0;
Joonas Lahtinenebfb6972019-02-07 10:54:54 +02001704
1705err:
1706 i915_gem_object_put(obj);
Joonas Lahtinenebfb6972019-02-07 10:54:54 +02001707 return addr;
Eric Anholt673a3942008-07-30 12:06:12 -07001708}
1709
Chris Wilsond899ace2018-07-25 16:54:47 +01001710static unsigned int tile_row_pages(const struct drm_i915_gem_object *obj)
Chris Wilson03af84f2016-08-18 17:17:01 +01001711{
Chris Wilson6649a0b2017-01-09 16:16:08 +00001712 return i915_gem_object_get_tile_row_size(obj) >> PAGE_SHIFT;
Chris Wilson03af84f2016-08-18 17:17:01 +01001713}
1714
Jesse Barnesde151cf2008-11-12 10:03:55 -08001715/**
Chris Wilson4cc69072016-08-25 19:05:19 +01001716 * i915_gem_mmap_gtt_version - report the current feature set for GTT mmaps
1717 *
1718 * A history of the GTT mmap interface:
1719 *
1720 * 0 - Everything had to fit into the GTT. Both parties of a memcpy had to
1721 * aligned and suitable for fencing, and still fit into the available
1722 * mappable space left by the pinned display objects. A classic problem
1723 * we called the page-fault-of-doom where we would ping-pong between
1724 * two objects that could not fit inside the GTT and so the memcpy
1725 * would page one object in at the expense of the other between every
1726 * single byte.
1727 *
1728 * 1 - Objects can be any size, and have any compatible fencing (X Y, or none
1729 * as set via i915_gem_set_tiling() [DRM_I915_GEM_SET_TILING]). If the
1730 * object is too large for the available space (or simply too large
1731 * for the mappable aperture!), a view is created instead and faulted
1732 * into userspace. (This view is aligned and sized appropriately for
1733 * fenced access.)
1734 *
Chris Wilsone22d8e32017-04-12 12:01:11 +01001735 * 2 - Recognise WC as a separate cache domain so that we can flush the
1736 * delayed writes via GTT before performing direct access via WC.
1737 *
Chris Wilsona679f582019-03-21 16:19:07 +00001738 * 3 - Remove implicit set-domain(GTT) and synchronisation on initial
1739 * pagefault; swapin remains transparent.
1740 *
Chris Wilson4cc69072016-08-25 19:05:19 +01001741 * Restrictions:
1742 *
1743 * * snoopable objects cannot be accessed via the GTT. It can cause machine
1744 * hangs on some architectures, corruption on others. An attempt to service
1745 * a GTT page fault from a snoopable object will generate a SIGBUS.
1746 *
1747 * * the object must be able to fit into RAM (physical memory, though no
1748 * limited to the mappable aperture).
1749 *
1750 *
1751 * Caveats:
1752 *
1753 * * a new GTT page fault will synchronize rendering from the GPU and flush
1754 * all data to system memory. Subsequent access will not be synchronized.
1755 *
1756 * * all mappings are revoked on runtime device suspend.
1757 *
1758 * * there are only 8, 16 or 32 fence registers to share between all users
1759 * (older machines require fence register for display and blitter access
1760 * as well). Contention of the fence registers will cause the previous users
1761 * to be unmapped and any new access will generate new page faults.
1762 *
1763 * * running out of memory while servicing a fault may generate a SIGBUS,
1764 * rather than the expected SIGSEGV.
1765 */
1766int i915_gem_mmap_gtt_version(void)
1767{
Chris Wilsona679f582019-03-21 16:19:07 +00001768 return 3;
Chris Wilson4cc69072016-08-25 19:05:19 +01001769}
1770
Chris Wilson2d4281b2017-01-10 09:56:32 +00001771static inline struct i915_ggtt_view
Chris Wilsond899ace2018-07-25 16:54:47 +01001772compute_partial_view(const struct drm_i915_gem_object *obj,
Chris Wilson2d4281b2017-01-10 09:56:32 +00001773 pgoff_t page_offset,
1774 unsigned int chunk)
1775{
1776 struct i915_ggtt_view view;
1777
1778 if (i915_gem_object_is_tiled(obj))
1779 chunk = roundup(chunk, tile_row_pages(obj));
1780
Chris Wilson2d4281b2017-01-10 09:56:32 +00001781 view.type = I915_GGTT_VIEW_PARTIAL;
Chris Wilson8bab11932017-01-14 00:28:25 +00001782 view.partial.offset = rounddown(page_offset, chunk);
1783 view.partial.size =
Chris Wilson2d4281b2017-01-10 09:56:32 +00001784 min_t(unsigned int, chunk,
Chris Wilson8bab11932017-01-14 00:28:25 +00001785 (obj->base.size >> PAGE_SHIFT) - view.partial.offset);
Chris Wilson2d4281b2017-01-10 09:56:32 +00001786
1787 /* If the partial covers the entire object, just create a normal VMA. */
1788 if (chunk >= obj->base.size >> PAGE_SHIFT)
1789 view.type = I915_GGTT_VIEW_NORMAL;
1790
1791 return view;
1792}
1793
Chris Wilson4cc69072016-08-25 19:05:19 +01001794/**
Jesse Barnesde151cf2008-11-12 10:03:55 -08001795 * i915_gem_fault - fault a page into the GTT
Geliang Tangd9072a32015-09-15 05:58:44 -07001796 * @vmf: fault info
Jesse Barnesde151cf2008-11-12 10:03:55 -08001797 *
1798 * The fault handler is set up by drm_gem_mmap() when a object is GTT mapped
1799 * from userspace. The fault handler takes care of binding the object to
1800 * the GTT (if needed), allocating and programming a fence register (again,
1801 * only if needed based on whether the old reg is still valid or the object
1802 * is tiled) and inserting a new PTE into the faulting process.
1803 *
1804 * Note that the faulting process may involve evicting existing objects
1805 * from the GTT and/or fence registers to make room. So performance may
1806 * suffer if the GTT working set is large or there are few fence registers
1807 * left.
Chris Wilson4cc69072016-08-25 19:05:19 +01001808 *
1809 * The current feature set supported by i915_gem_fault() and thus GTT mmaps
1810 * is exposed via I915_PARAM_MMAP_GTT_VERSION (see i915_gem_mmap_gtt_version).
Jesse Barnesde151cf2008-11-12 10:03:55 -08001811 */
Chris Wilson52137012018-06-06 22:45:20 +01001812vm_fault_t i915_gem_fault(struct vm_fault *vmf)
Jesse Barnesde151cf2008-11-12 10:03:55 -08001813{
Chris Wilson420980c2018-06-05 14:57:46 +01001814#define MIN_CHUNK_PAGES (SZ_1M >> PAGE_SHIFT)
Dave Jiang11bac802017-02-24 14:56:41 -08001815 struct vm_area_struct *area = vmf->vma;
Chris Wilson058d88c2016-08-15 10:49:06 +01001816 struct drm_i915_gem_object *obj = to_intel_bo(area->vm_private_data);
Chris Wilson05394f32010-11-08 19:18:58 +00001817 struct drm_device *dev = obj->base.dev;
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03001818 struct drm_i915_private *dev_priv = to_i915(dev);
1819 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Chris Wilsonaae7c062018-09-03 09:33:34 +01001820 bool write = area->vm_flags & VM_WRITE;
Chris Wilson538ef962019-01-14 14:21:18 +00001821 intel_wakeref_t wakeref;
Chris Wilson058d88c2016-08-15 10:49:06 +01001822 struct i915_vma *vma;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001823 pgoff_t page_offset;
Chris Wilson2caffbf2019-02-08 15:37:03 +00001824 int srcu;
Chris Wilsonb8f90962016-08-05 10:14:07 +01001825 int ret;
Paulo Zanonif65c9162013-11-27 18:20:34 -02001826
Chris Wilson3e977ac2018-07-12 19:53:13 +01001827 /* Sanity check that we allow writing into this object */
1828 if (i915_gem_object_is_readonly(obj) && write)
1829 return VM_FAULT_SIGBUS;
1830
Jesse Barnesde151cf2008-11-12 10:03:55 -08001831 /* We don't use vmf->pgoff since that has the fake offset */
Jan Kara1a29d852016-12-14 15:07:01 -08001832 page_offset = (vmf->address - area->vm_start) >> PAGE_SHIFT;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001833
Chris Wilsondb53a302011-02-03 11:57:46 +00001834 trace_i915_gem_object_fault(obj, page_offset, true, write);
1835
Chris Wilson40e62d52016-10-28 13:58:41 +01001836 ret = i915_gem_object_pin_pages(obj);
1837 if (ret)
1838 goto err;
1839
Chris Wilson538ef962019-01-14 14:21:18 +00001840 wakeref = intel_runtime_pm_get(dev_priv);
Chris Wilsonb8f90962016-08-05 10:14:07 +01001841
Chris Wilson43a8f682019-02-21 10:29:19 +00001842 srcu = i915_reset_trylock(dev_priv);
1843 if (srcu < 0) {
1844 ret = srcu;
1845 goto err_rpm;
1846 }
1847
Chris Wilsonb8f90962016-08-05 10:14:07 +01001848 ret = i915_mutex_lock_interruptible(dev);
1849 if (ret)
Chris Wilson43a8f682019-02-21 10:29:19 +00001850 goto err_reset;
Chris Wilson6e4930f2014-02-07 18:37:06 -02001851
Chris Wilsoneb119bd2012-12-16 12:43:36 +00001852 /* Access to snoopable pages through the GTT is incoherent. */
Tvrtko Ursulin0031fb92016-11-04 14:42:44 +00001853 if (obj->cache_level != I915_CACHE_NONE && !HAS_LLC(dev_priv)) {
Chris Wilsonddeff6e2014-05-28 16:16:41 +01001854 ret = -EFAULT;
Chris Wilsonb8f90962016-08-05 10:14:07 +01001855 goto err_unlock;
Chris Wilsoneb119bd2012-12-16 12:43:36 +00001856 }
1857
Chris Wilsona61007a2016-08-18 17:17:02 +01001858 /* Now pin it into the GTT as needed */
Chris Wilson7e7367d2018-06-30 10:05:09 +01001859 vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0,
1860 PIN_MAPPABLE |
1861 PIN_NONBLOCK |
1862 PIN_NONFAULT);
Chris Wilsona61007a2016-08-18 17:17:02 +01001863 if (IS_ERR(vma)) {
Chris Wilsona61007a2016-08-18 17:17:02 +01001864 /* Use a partial view if it is bigger than available space */
Chris Wilson2d4281b2017-01-10 09:56:32 +00001865 struct i915_ggtt_view view =
Chris Wilson8201c1f2017-01-10 09:56:33 +00001866 compute_partial_view(obj, page_offset, MIN_CHUNK_PAGES);
Chris Wilson7e7367d2018-06-30 10:05:09 +01001867 unsigned int flags;
Chris Wilsonaa136d92016-08-18 17:17:03 +01001868
Chris Wilson7e7367d2018-06-30 10:05:09 +01001869 flags = PIN_MAPPABLE;
1870 if (view.type == I915_GGTT_VIEW_NORMAL)
1871 flags |= PIN_NONBLOCK; /* avoid warnings for pinned */
1872
1873 /*
1874 * Userspace is now writing through an untracked VMA, abandon
Chris Wilson50349242016-08-18 17:17:04 +01001875 * all hope that the hardware is able to track future writes.
1876 */
1877 obj->frontbuffer_ggtt_origin = ORIGIN_CPU;
1878
Chris Wilson7e7367d2018-06-30 10:05:09 +01001879 vma = i915_gem_object_ggtt_pin(obj, &view, 0, 0, flags);
1880 if (IS_ERR(vma) && !view.type) {
1881 flags = PIN_MAPPABLE;
1882 view.type = I915_GGTT_VIEW_PARTIAL;
1883 vma = i915_gem_object_ggtt_pin(obj, &view, 0, 0, flags);
1884 }
Chris Wilsona61007a2016-08-18 17:17:02 +01001885 }
Chris Wilson058d88c2016-08-15 10:49:06 +01001886 if (IS_ERR(vma)) {
1887 ret = PTR_ERR(vma);
Chris Wilsonb8f90962016-08-05 10:14:07 +01001888 goto err_unlock;
Chris Wilson058d88c2016-08-15 10:49:06 +01001889 }
Jesse Barnesde151cf2008-11-12 10:03:55 -08001890
Chris Wilsonaeaaa552019-02-12 13:08:30 +00001891 ret = i915_vma_pin_fence(vma);
1892 if (ret)
1893 goto err_unpin;
1894
Chris Wilsonb90b91d2014-06-10 12:14:40 +01001895 /* Finally, remap it using the new GTT offset */
Chris Wilsonc58305a2016-08-19 16:54:28 +01001896 ret = remap_io_mapping(area,
Chris Wilson8bab11932017-01-14 00:28:25 +00001897 area->vm_start + (vma->ggtt_view.partial.offset << PAGE_SHIFT),
Matthew Auld73ebd502017-12-11 15:18:20 +00001898 (ggtt->gmadr.start + vma->node.start) >> PAGE_SHIFT,
Chris Wilsonc58305a2016-08-19 16:54:28 +01001899 min_t(u64, vma->size, area->vm_end - area->vm_start),
Matthew Auld73ebd502017-12-11 15:18:20 +00001900 &ggtt->iomap);
Chris Wilsona65adaf2017-10-09 09:43:57 +01001901 if (ret)
Chris Wilson43a8f682019-02-21 10:29:19 +00001902 goto err_fence;
Chris Wilsona61007a2016-08-18 17:17:02 +01001903
Chris Wilsona65adaf2017-10-09 09:43:57 +01001904 /* Mark as being mmapped into userspace for later revocation */
1905 assert_rpm_wakelock_held(dev_priv);
1906 if (!i915_vma_set_userfault(vma) && !obj->userfault_count++)
1907 list_add(&obj->userfault_link, &dev_priv->mm.userfault_list);
1908 GEM_BUG_ON(!obj->userfault_count);
1909
Chris Wilson7125397b2017-12-06 12:49:14 +00001910 i915_vma_set_ggtt_write(vma);
1911
Chris Wilsonaeaaa552019-02-12 13:08:30 +00001912err_fence:
1913 i915_vma_unpin_fence(vma);
Chris Wilsonb8f90962016-08-05 10:14:07 +01001914err_unpin:
Chris Wilson058d88c2016-08-15 10:49:06 +01001915 __i915_vma_unpin(vma);
Chris Wilsonb8f90962016-08-05 10:14:07 +01001916err_unlock:
Jesse Barnesde151cf2008-11-12 10:03:55 -08001917 mutex_unlock(&dev->struct_mutex);
Chris Wilson43a8f682019-02-21 10:29:19 +00001918err_reset:
1919 i915_reset_unlock(dev_priv, srcu);
Chris Wilsonb8f90962016-08-05 10:14:07 +01001920err_rpm:
Chris Wilson538ef962019-01-14 14:21:18 +00001921 intel_runtime_pm_put(dev_priv, wakeref);
Chris Wilson40e62d52016-10-28 13:58:41 +01001922 i915_gem_object_unpin_pages(obj);
Chris Wilsonb8f90962016-08-05 10:14:07 +01001923err:
Jesse Barnesde151cf2008-11-12 10:03:55 -08001924 switch (ret) {
Chris Wilsond9bc7e92011-02-07 13:09:31 +00001925 case -EIO:
Daniel Vetter2232f032014-09-04 09:36:18 +02001926 /*
1927 * We eat errors when the gpu is terminally wedged to avoid
1928 * userspace unduly crashing (gl has no provisions for mmaps to
1929 * fail). But any other -EIO isn't ours (e.g. swap in failure)
1930 * and so needs to be reported.
1931 */
Chris Wilsonc41166f2019-02-20 14:56:37 +00001932 if (!i915_terminally_wedged(dev_priv))
Chris Wilson52137012018-06-06 22:45:20 +01001933 return VM_FAULT_SIGBUS;
Gustavo A. R. Silvaf0d759f2018-06-28 17:35:41 -05001934 /* else: fall through */
Chris Wilson045e7692010-11-07 09:18:22 +00001935 case -EAGAIN:
Daniel Vetter571c6082013-09-12 17:57:28 +02001936 /*
1937 * EAGAIN means the gpu is hung and we'll wait for the error
1938 * handler to reset everything when re-faulting in
1939 * i915_mutex_lock_interruptible.
Chris Wilsond9bc7e92011-02-07 13:09:31 +00001940 */
Chris Wilsonc7150892009-09-23 00:43:56 +01001941 case 0:
1942 case -ERESTARTSYS:
Chris Wilsonbed636a2011-02-11 20:31:19 +00001943 case -EINTR:
Dmitry Rogozhkine79e0fe2012-10-03 17:15:26 +03001944 case -EBUSY:
1945 /*
1946 * EBUSY is ok: this just means that another thread
1947 * already did the job.
1948 */
Chris Wilson52137012018-06-06 22:45:20 +01001949 return VM_FAULT_NOPAGE;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001950 case -ENOMEM:
Chris Wilson52137012018-06-06 22:45:20 +01001951 return VM_FAULT_OOM;
Daniel Vettera7c2e1a2012-10-17 11:17:16 +02001952 case -ENOSPC:
Chris Wilson45d67812014-01-31 11:34:57 +00001953 case -EFAULT:
Chris Wilson52137012018-06-06 22:45:20 +01001954 return VM_FAULT_SIGBUS;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001955 default:
Daniel Vettera7c2e1a2012-10-17 11:17:16 +02001956 WARN_ONCE(ret, "unhandled error in i915_gem_fault: %i\n", ret);
Chris Wilson52137012018-06-06 22:45:20 +01001957 return VM_FAULT_SIGBUS;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001958 }
1959}
1960
Chris Wilsona65adaf2017-10-09 09:43:57 +01001961static void __i915_gem_object_release_mmap(struct drm_i915_gem_object *obj)
1962{
1963 struct i915_vma *vma;
1964
1965 GEM_BUG_ON(!obj->userfault_count);
1966
1967 obj->userfault_count = 0;
1968 list_del(&obj->userfault_link);
1969 drm_vma_node_unmap(&obj->base.vma_node,
1970 obj->base.dev->anon_inode->i_mapping);
1971
Chris Wilsone2189dd2017-12-07 21:14:07 +00001972 for_each_ggtt_vma(vma, obj)
Chris Wilsona65adaf2017-10-09 09:43:57 +01001973 i915_vma_unset_userfault(vma);
Chris Wilsona65adaf2017-10-09 09:43:57 +01001974}
1975
Jesse Barnesde151cf2008-11-12 10:03:55 -08001976/**
Chris Wilson901782b2009-07-10 08:18:50 +01001977 * i915_gem_release_mmap - remove physical page mappings
1978 * @obj: obj in question
1979 *
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02001980 * Preserve the reservation of the mmapping with the DRM core code, but
Chris Wilson901782b2009-07-10 08:18:50 +01001981 * relinquish ownership of the pages back to the system.
1982 *
1983 * It is vital that we remove the page mapping if we have mapped a tiled
1984 * object through the GTT and then lose the fence register due to
1985 * resource pressure. Similarly if the object has been moved out of the
1986 * aperture, than pages mapped into userspace must be revoked. Removing the
1987 * mapping will then trigger a page fault on the next user access, allowing
1988 * fixup by i915_gem_fault().
1989 */
Eric Anholtd05ca302009-07-10 13:02:26 -07001990void
Chris Wilson05394f32010-11-08 19:18:58 +00001991i915_gem_release_mmap(struct drm_i915_gem_object *obj)
Chris Wilson901782b2009-07-10 08:18:50 +01001992{
Chris Wilson275f0392016-10-24 13:42:14 +01001993 struct drm_i915_private *i915 = to_i915(obj->base.dev);
Chris Wilson538ef962019-01-14 14:21:18 +00001994 intel_wakeref_t wakeref;
Chris Wilson275f0392016-10-24 13:42:14 +01001995
Chris Wilson349f2cc2016-04-13 17:35:12 +01001996 /* Serialisation between user GTT access and our code depends upon
1997 * revoking the CPU's PTE whilst the mutex is held. The next user
1998 * pagefault then has to wait until we release the mutex.
Chris Wilson9c870d02016-10-24 13:42:15 +01001999 *
2000 * Note that RPM complicates somewhat by adding an additional
2001 * requirement that operations to the GGTT be made holding the RPM
2002 * wakeref.
Chris Wilson349f2cc2016-04-13 17:35:12 +01002003 */
Chris Wilson275f0392016-10-24 13:42:14 +01002004 lockdep_assert_held(&i915->drm.struct_mutex);
Chris Wilson538ef962019-01-14 14:21:18 +00002005 wakeref = intel_runtime_pm_get(i915);
Chris Wilson349f2cc2016-04-13 17:35:12 +01002006
Chris Wilsona65adaf2017-10-09 09:43:57 +01002007 if (!obj->userfault_count)
Chris Wilson9c870d02016-10-24 13:42:15 +01002008 goto out;
Chris Wilson901782b2009-07-10 08:18:50 +01002009
Chris Wilsona65adaf2017-10-09 09:43:57 +01002010 __i915_gem_object_release_mmap(obj);
Chris Wilson349f2cc2016-04-13 17:35:12 +01002011
2012 /* Ensure that the CPU's PTE are revoked and there are not outstanding
2013 * memory transactions from userspace before we return. The TLB
2014 * flushing implied above by changing the PTE above *should* be
2015 * sufficient, an extra barrier here just provides us with a bit
2016 * of paranoid documentation about our requirement to serialise
2017 * memory writes before touching registers / GSM.
2018 */
2019 wmb();
Chris Wilson9c870d02016-10-24 13:42:15 +01002020
2021out:
Chris Wilson538ef962019-01-14 14:21:18 +00002022 intel_runtime_pm_put(i915, wakeref);
Chris Wilson901782b2009-07-10 08:18:50 +01002023}
2024
Chris Wilson7c108fd2016-10-24 13:42:18 +01002025void i915_gem_runtime_suspend(struct drm_i915_private *dev_priv)
Chris Wilsoneedd10f2014-06-16 08:57:44 +01002026{
Chris Wilson3594a3e2016-10-24 13:42:16 +01002027 struct drm_i915_gem_object *obj, *on;
Chris Wilson7c108fd2016-10-24 13:42:18 +01002028 int i;
Chris Wilsoneedd10f2014-06-16 08:57:44 +01002029
Chris Wilson3594a3e2016-10-24 13:42:16 +01002030 /*
2031 * Only called during RPM suspend. All users of the userfault_list
2032 * must be holding an RPM wakeref to ensure that this can not
2033 * run concurrently with themselves (and use the struct_mutex for
2034 * protection between themselves).
2035 */
2036
2037 list_for_each_entry_safe(obj, on,
Chris Wilsona65adaf2017-10-09 09:43:57 +01002038 &dev_priv->mm.userfault_list, userfault_link)
2039 __i915_gem_object_release_mmap(obj);
Chris Wilson7c108fd2016-10-24 13:42:18 +01002040
2041 /* The fence will be lost when the device powers down. If any were
2042 * in use by hardware (i.e. they are pinned), we should not be powering
2043 * down! All other fences will be reacquired by the user upon waking.
2044 */
2045 for (i = 0; i < dev_priv->num_fence_regs; i++) {
2046 struct drm_i915_fence_reg *reg = &dev_priv->fence_regs[i];
2047
Chris Wilsone0ec3ec2017-02-03 12:57:17 +00002048 /* Ideally we want to assert that the fence register is not
2049 * live at this point (i.e. that no piece of code will be
2050 * trying to write through fence + GTT, as that both violates
2051 * our tracking of activity and associated locking/barriers,
2052 * but also is illegal given that the hw is powered down).
2053 *
2054 * Previously we used reg->pin_count as a "liveness" indicator.
2055 * That is not sufficient, and we need a more fine-grained
2056 * tool if we want to have a sanity check here.
2057 */
Chris Wilson7c108fd2016-10-24 13:42:18 +01002058
2059 if (!reg->vma)
2060 continue;
2061
Chris Wilsona65adaf2017-10-09 09:43:57 +01002062 GEM_BUG_ON(i915_vma_has_userfault(reg->vma));
Chris Wilson7c108fd2016-10-24 13:42:18 +01002063 reg->dirty = true;
2064 }
Chris Wilsoneedd10f2014-06-16 08:57:44 +01002065}
2066
Chris Wilsond8cb5082012-08-11 15:41:03 +01002067static int i915_gem_object_create_mmap_offset(struct drm_i915_gem_object *obj)
2068{
Chris Wilsonfac5e232016-07-04 11:34:36 +01002069 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
Chris Wilsonf3f61842016-08-05 10:14:14 +01002070 int err;
Chris Wilsond8cb5082012-08-11 15:41:03 +01002071
Chris Wilsonf3f61842016-08-05 10:14:14 +01002072 err = drm_gem_create_mmap_offset(&obj->base);
Chris Wilsonb42a13d2017-01-06 15:22:40 +00002073 if (likely(!err))
Chris Wilsonf3f61842016-08-05 10:14:14 +01002074 return 0;
Daniel Vetterda494d72012-12-20 15:11:16 +01002075
Chris Wilsonb42a13d2017-01-06 15:22:40 +00002076 /* Attempt to reap some mmap space from dead objects */
2077 do {
Chris Wilsonec625fb2018-07-09 13:20:42 +01002078 err = i915_gem_wait_for_idle(dev_priv,
2079 I915_WAIT_INTERRUPTIBLE,
2080 MAX_SCHEDULE_TIMEOUT);
Chris Wilsonb42a13d2017-01-06 15:22:40 +00002081 if (err)
2082 break;
Chris Wilsond8cb5082012-08-11 15:41:03 +01002083
Chris Wilsonb42a13d2017-01-06 15:22:40 +00002084 i915_gem_drain_freed_objects(dev_priv);
Chris Wilsonf3f61842016-08-05 10:14:14 +01002085 err = drm_gem_create_mmap_offset(&obj->base);
Chris Wilsonb42a13d2017-01-06 15:22:40 +00002086 if (!err)
2087 break;
2088
2089 } while (flush_delayed_work(&dev_priv->gt.retire_work));
Daniel Vetterda494d72012-12-20 15:11:16 +01002090
Chris Wilsonf3f61842016-08-05 10:14:14 +01002091 return err;
Chris Wilsond8cb5082012-08-11 15:41:03 +01002092}
2093
2094static void i915_gem_object_free_mmap_offset(struct drm_i915_gem_object *obj)
2095{
Chris Wilsond8cb5082012-08-11 15:41:03 +01002096 drm_gem_free_mmap_offset(&obj->base);
2097}
2098
Dave Airlieda6b51d2014-12-24 13:11:17 +10002099int
Dave Airlieff72145b2011-02-07 12:16:14 +10002100i915_gem_mmap_gtt(struct drm_file *file,
2101 struct drm_device *dev,
Jani Nikula739f3ab2019-01-16 11:15:19 +02002102 u32 handle,
2103 u64 *offset)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002104{
Chris Wilson05394f32010-11-08 19:18:58 +00002105 struct drm_i915_gem_object *obj;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002106 int ret;
2107
Chris Wilson03ac0642016-07-20 13:31:51 +01002108 obj = i915_gem_object_lookup(file, handle);
Chris Wilsonf3f61842016-08-05 10:14:14 +01002109 if (!obj)
2110 return -ENOENT;
Chris Wilsonab182822009-09-22 18:46:17 +01002111
Chris Wilsond8cb5082012-08-11 15:41:03 +01002112 ret = i915_gem_object_create_mmap_offset(obj);
Chris Wilsonf3f61842016-08-05 10:14:14 +01002113 if (ret == 0)
2114 *offset = drm_vma_node_offset_addr(&obj->base.vma_node);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002115
Chris Wilsonf0cd5182016-10-28 13:58:43 +01002116 i915_gem_object_put(obj);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01002117 return ret;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002118}
2119
Dave Airlieff72145b2011-02-07 12:16:14 +10002120/**
2121 * i915_gem_mmap_gtt_ioctl - prepare an object for GTT mmap'ing
2122 * @dev: DRM device
2123 * @data: GTT mapping ioctl data
2124 * @file: GEM object info
2125 *
2126 * Simply returns the fake offset to userspace so it can mmap it.
2127 * The mmap call will end up in drm_gem_mmap(), which will set things
2128 * up so we can get faults in the handler above.
2129 *
2130 * The fault handler will take care of binding the object into the GTT
2131 * (since it may have been evicted to make room for something), allocating
2132 * a fence register, and mapping the appropriate aperture address into
2133 * userspace.
2134 */
2135int
2136i915_gem_mmap_gtt_ioctl(struct drm_device *dev, void *data,
2137 struct drm_file *file)
2138{
2139 struct drm_i915_gem_mmap_gtt *args = data;
2140
Dave Airlieda6b51d2014-12-24 13:11:17 +10002141 return i915_gem_mmap_gtt(file, dev, args->handle, &args->offset);
Dave Airlieff72145b2011-02-07 12:16:14 +10002142}
2143
Daniel Vetter225067e2012-08-20 10:23:20 +02002144/* Immediately discard the backing storage */
2145static void
2146i915_gem_object_truncate(struct drm_i915_gem_object *obj)
Chris Wilsone5281cc2010-10-28 13:45:36 +01002147{
Chris Wilson4d6294bf2012-08-11 15:41:05 +01002148 i915_gem_object_free_mmap_offset(obj);
Daniel Vetter1286ff72012-05-10 15:25:09 +02002149
Chris Wilson4d6294bf2012-08-11 15:41:05 +01002150 if (obj->base.filp == NULL)
2151 return;
2152
Daniel Vetter225067e2012-08-20 10:23:20 +02002153 /* Our goal here is to return as much of the memory as
2154 * is possible back to the system as we are called from OOM.
2155 * To do this we must instruct the shmfs to drop all of its
2156 * backing pages, *now*.
Chris Wilsone5281cc2010-10-28 13:45:36 +01002157 */
Chris Wilson55372522014-03-25 13:23:06 +00002158 shmem_truncate_range(file_inode(obj->base.filp), 0, (loff_t)-1);
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002159 obj->mm.madv = __I915_MADV_PURGED;
Chris Wilson4e5462e2017-03-07 13:20:31 +00002160 obj->mm.pages = ERR_PTR(-EFAULT);
Chris Wilsone5281cc2010-10-28 13:45:36 +01002161}
Chris Wilsone5281cc2010-10-28 13:45:36 +01002162
Chris Wilson55372522014-03-25 13:23:06 +00002163/* Try to discard unwanted pages */
Chris Wilson03ac84f2016-10-28 13:58:36 +01002164void __i915_gem_object_invalidate(struct drm_i915_gem_object *obj)
Daniel Vetter225067e2012-08-20 10:23:20 +02002165{
Chris Wilson55372522014-03-25 13:23:06 +00002166 struct address_space *mapping;
2167
Chris Wilson1233e2d2016-10-28 13:58:37 +01002168 lockdep_assert_held(&obj->mm.lock);
Chris Wilsonf1fa4f42017-10-13 21:26:13 +01002169 GEM_BUG_ON(i915_gem_object_has_pages(obj));
Chris Wilson1233e2d2016-10-28 13:58:37 +01002170
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002171 switch (obj->mm.madv) {
Chris Wilson55372522014-03-25 13:23:06 +00002172 case I915_MADV_DONTNEED:
2173 i915_gem_object_truncate(obj);
2174 case __I915_MADV_PURGED:
2175 return;
2176 }
2177
2178 if (obj->base.filp == NULL)
2179 return;
2180
Al Viro93c76a32015-12-04 23:45:44 -05002181 mapping = obj->base.filp->f_mapping,
Chris Wilson55372522014-03-25 13:23:06 +00002182 invalidate_mapping_pages(mapping, 0, (loff_t)-1);
Chris Wilsone5281cc2010-10-28 13:45:36 +01002183}
2184
Kuo-Hsin Yang64e3d122018-11-06 13:23:24 +00002185/*
2186 * Move pages to appropriate lru and release the pagevec, decrementing the
2187 * ref count of those pages.
2188 */
2189static void check_release_pagevec(struct pagevec *pvec)
2190{
2191 check_move_unevictable_pages(pvec);
2192 __pagevec_release(pvec);
2193 cond_resched();
2194}
2195
Chris Wilson5cdf5882010-09-27 15:51:07 +01002196static void
Chris Wilson03ac84f2016-10-28 13:58:36 +01002197i915_gem_object_put_pages_gtt(struct drm_i915_gem_object *obj,
2198 struct sg_table *pages)
Eric Anholt673a3942008-07-30 12:06:12 -07002199{
Dave Gordon85d12252016-05-20 11:54:06 +01002200 struct sgt_iter sgt_iter;
Kuo-Hsin Yang64e3d122018-11-06 13:23:24 +00002201 struct pagevec pvec;
Dave Gordon85d12252016-05-20 11:54:06 +01002202 struct page *page;
Daniel Vetter1286ff72012-05-10 15:25:09 +02002203
Chris Wilsone5facdf2016-12-23 14:57:57 +00002204 __i915_gem_object_release_shmem(obj, pages, true);
Chris Wilson03ac84f2016-10-28 13:58:36 +01002205 i915_gem_gtt_finish_pages(obj, pages);
Imre Deake2273302015-07-09 12:59:05 +03002206
Daniel Vetter6dacfd22011-09-12 21:30:02 +02002207 if (i915_gem_object_needs_bit17_swizzle(obj))
Chris Wilson03ac84f2016-10-28 13:58:36 +01002208 i915_gem_object_save_bit_17_swizzle(obj, pages);
Eric Anholt280b7132009-03-12 16:56:27 -07002209
Kuo-Hsin Yang64e3d122018-11-06 13:23:24 +00002210 mapping_clear_unevictable(file_inode(obj->base.filp)->i_mapping);
2211
2212 pagevec_init(&pvec);
Chris Wilson03ac84f2016-10-28 13:58:36 +01002213 for_each_sgt_page(page, sgt_iter, pages) {
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002214 if (obj->mm.dirty)
Chris Wilson9da3da62012-06-01 15:20:22 +01002215 set_page_dirty(page);
Chris Wilson3ef94da2009-09-14 16:50:29 +01002216
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002217 if (obj->mm.madv == I915_MADV_WILLNEED)
Chris Wilson9da3da62012-06-01 15:20:22 +01002218 mark_page_accessed(page);
Chris Wilson3ef94da2009-09-14 16:50:29 +01002219
Kuo-Hsin Yang64e3d122018-11-06 13:23:24 +00002220 if (!pagevec_add(&pvec, page))
2221 check_release_pagevec(&pvec);
Chris Wilson3ef94da2009-09-14 16:50:29 +01002222 }
Kuo-Hsin Yang64e3d122018-11-06 13:23:24 +00002223 if (pagevec_count(&pvec))
2224 check_release_pagevec(&pvec);
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002225 obj->mm.dirty = false;
Eric Anholt673a3942008-07-30 12:06:12 -07002226
Chris Wilson03ac84f2016-10-28 13:58:36 +01002227 sg_free_table(pages);
2228 kfree(pages);
Chris Wilson37e680a2012-06-07 15:38:42 +01002229}
2230
Chris Wilson96d77632016-10-28 13:58:33 +01002231static void __i915_gem_object_reset_page_iter(struct drm_i915_gem_object *obj)
2232{
2233 struct radix_tree_iter iter;
Ville Syrjäläc23aa712017-09-01 20:12:51 +03002234 void __rcu **slot;
Chris Wilson96d77632016-10-28 13:58:33 +01002235
Chris Wilsonbea6e982017-10-26 14:00:31 +01002236 rcu_read_lock();
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002237 radix_tree_for_each_slot(slot, &obj->mm.get_page.radix, &iter, 0)
2238 radix_tree_delete(&obj->mm.get_page.radix, iter.index);
Chris Wilsonbea6e982017-10-26 14:00:31 +01002239 rcu_read_unlock();
Chris Wilson96d77632016-10-28 13:58:33 +01002240}
2241
Chris Wilsonacd1c1e2018-06-11 08:55:32 +01002242static struct sg_table *
2243__i915_gem_object_unset_pages(struct drm_i915_gem_object *obj)
Chris Wilson37e680a2012-06-07 15:38:42 +01002244{
Chris Wilsonf2123812017-10-16 12:40:37 +01002245 struct drm_i915_private *i915 = to_i915(obj->base.dev);
Chris Wilson03ac84f2016-10-28 13:58:36 +01002246 struct sg_table *pages;
Chris Wilson37e680a2012-06-07 15:38:42 +01002247
Chris Wilson03ac84f2016-10-28 13:58:36 +01002248 pages = fetch_and_zero(&obj->mm.pages);
Chris Wilson484d9a82019-01-15 12:44:42 +00002249 if (IS_ERR_OR_NULL(pages))
2250 return pages;
Chris Wilsona2165e32012-12-03 11:49:00 +00002251
Chris Wilsonf2123812017-10-16 12:40:37 +01002252 spin_lock(&i915->mm.obj_lock);
2253 list_del(&obj->mm.link);
2254 spin_unlock(&i915->mm.obj_lock);
2255
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002256 if (obj->mm.mapping) {
Chris Wilson4b30cb22016-08-18 17:16:42 +01002257 void *ptr;
2258
Chris Wilson0ce81782017-05-17 13:09:59 +01002259 ptr = page_mask_bits(obj->mm.mapping);
Chris Wilson4b30cb22016-08-18 17:16:42 +01002260 if (is_vmalloc_addr(ptr))
2261 vunmap(ptr);
Chris Wilsonfb8621d2016-04-08 12:11:14 +01002262 else
Chris Wilson4b30cb22016-08-18 17:16:42 +01002263 kunmap(kmap_to_page(ptr));
2264
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002265 obj->mm.mapping = NULL;
Chris Wilson0a798eb2016-04-08 12:11:11 +01002266 }
2267
Chris Wilson96d77632016-10-28 13:58:33 +01002268 __i915_gem_object_reset_page_iter(obj);
Chris Wilsonacd1c1e2018-06-11 08:55:32 +01002269 obj->mm.page_sizes.phys = obj->mm.page_sizes.sg = 0;
Chris Wilson96d77632016-10-28 13:58:33 +01002270
Chris Wilsonacd1c1e2018-06-11 08:55:32 +01002271 return pages;
2272}
2273
Chris Wilson484d9a82019-01-15 12:44:42 +00002274int __i915_gem_object_put_pages(struct drm_i915_gem_object *obj,
2275 enum i915_mm_subclass subclass)
Chris Wilsonacd1c1e2018-06-11 08:55:32 +01002276{
2277 struct sg_table *pages;
Chris Wilson484d9a82019-01-15 12:44:42 +00002278 int ret;
Chris Wilsonacd1c1e2018-06-11 08:55:32 +01002279
2280 if (i915_gem_object_has_pinned_pages(obj))
Chris Wilson484d9a82019-01-15 12:44:42 +00002281 return -EBUSY;
Chris Wilsonacd1c1e2018-06-11 08:55:32 +01002282
2283 GEM_BUG_ON(obj->bind_count);
Chris Wilsonacd1c1e2018-06-11 08:55:32 +01002284
2285 /* May be called by shrinker from within get_pages() (on another bo) */
2286 mutex_lock_nested(&obj->mm.lock, subclass);
Chris Wilson484d9a82019-01-15 12:44:42 +00002287 if (unlikely(atomic_read(&obj->mm.pages_pin_count))) {
2288 ret = -EBUSY;
Chris Wilsonacd1c1e2018-06-11 08:55:32 +01002289 goto unlock;
Chris Wilson484d9a82019-01-15 12:44:42 +00002290 }
Chris Wilsonacd1c1e2018-06-11 08:55:32 +01002291
2292 /*
2293 * ->put_pages might need to allocate memory for the bit17 swizzle
2294 * array, hence protect them from being reaped by removing them from gtt
2295 * lists early.
2296 */
2297 pages = __i915_gem_object_unset_pages(obj);
Chris Wilson484d9a82019-01-15 12:44:42 +00002298
2299 /*
2300 * XXX Temporary hijinx to avoid updating all backends to handle
2301 * NULL pages. In the future, when we have more asynchronous
2302 * get_pages backends we should be better able to handle the
2303 * cancellation of the async task in a more uniform manner.
2304 */
2305 if (!pages && !i915_gem_object_needs_async_cancel(obj))
2306 pages = ERR_PTR(-EINVAL);
2307
Chris Wilson4e5462e2017-03-07 13:20:31 +00002308 if (!IS_ERR(pages))
2309 obj->ops->put_pages(obj, pages);
2310
Chris Wilson484d9a82019-01-15 12:44:42 +00002311 ret = 0;
Chris Wilson1233e2d2016-10-28 13:58:37 +01002312unlock:
2313 mutex_unlock(&obj->mm.lock);
Chris Wilson484d9a82019-01-15 12:44:42 +00002314
2315 return ret;
Chris Wilson6c085a72012-08-20 11:40:46 +02002316}
2317
Tvrtko Ursulinf8e57862018-09-26 09:03:53 +01002318bool i915_sg_trim(struct sg_table *orig_st)
Tvrtko Ursulin0c40ce12016-11-09 15:13:43 +00002319{
2320 struct sg_table new_st;
2321 struct scatterlist *sg, *new_sg;
2322 unsigned int i;
2323
2324 if (orig_st->nents == orig_st->orig_nents)
Chris Wilson935a2f72017-02-13 17:15:13 +00002325 return false;
Tvrtko Ursulin0c40ce12016-11-09 15:13:43 +00002326
Chris Wilson8bfc478f2016-12-23 14:57:58 +00002327 if (sg_alloc_table(&new_st, orig_st->nents, GFP_KERNEL | __GFP_NOWARN))
Chris Wilson935a2f72017-02-13 17:15:13 +00002328 return false;
Tvrtko Ursulin0c40ce12016-11-09 15:13:43 +00002329
2330 new_sg = new_st.sgl;
2331 for_each_sg(orig_st->sgl, sg, orig_st->nents, i) {
2332 sg_set_page(new_sg, sg_page(sg), sg->length, 0);
Matthew Auldc6d22ab2018-09-20 15:27:06 +01002333 sg_dma_address(new_sg) = sg_dma_address(sg);
2334 sg_dma_len(new_sg) = sg_dma_len(sg);
2335
Tvrtko Ursulin0c40ce12016-11-09 15:13:43 +00002336 new_sg = sg_next(new_sg);
2337 }
Chris Wilsonc2dc6cc2016-12-19 12:43:46 +00002338 GEM_BUG_ON(new_sg); /* Should walk exactly nents and hit the end */
Tvrtko Ursulin0c40ce12016-11-09 15:13:43 +00002339
2340 sg_free_table(orig_st);
2341
2342 *orig_st = new_st;
Chris Wilson935a2f72017-02-13 17:15:13 +00002343 return true;
Tvrtko Ursulin0c40ce12016-11-09 15:13:43 +00002344}
2345
Matthew Auldb91b09e2017-10-06 23:18:17 +01002346static int i915_gem_object_get_pages_gtt(struct drm_i915_gem_object *obj)
Eric Anholt673a3942008-07-30 12:06:12 -07002347{
Chris Wilsonfac5e232016-07-04 11:34:36 +01002348 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
Chris Wilsond766ef52016-12-19 12:43:45 +00002349 const unsigned long page_count = obj->base.size / PAGE_SIZE;
2350 unsigned long i;
Eric Anholt673a3942008-07-30 12:06:12 -07002351 struct address_space *mapping;
Chris Wilson9da3da62012-06-01 15:20:22 +01002352 struct sg_table *st;
2353 struct scatterlist *sg;
Dave Gordon85d12252016-05-20 11:54:06 +01002354 struct sgt_iter sgt_iter;
Eric Anholt673a3942008-07-30 12:06:12 -07002355 struct page *page;
Imre Deak90797e62013-02-18 19:28:03 +02002356 unsigned long last_pfn = 0; /* suppress gcc warning */
Tvrtko Ursulin56024522017-08-03 10:14:17 +01002357 unsigned int max_segment = i915_sg_segment_size();
Matthew Auld84e89782017-10-09 12:00:24 +01002358 unsigned int sg_page_sizes;
Kuo-Hsin Yang64e3d122018-11-06 13:23:24 +00002359 struct pagevec pvec;
Chris Wilson4846bf02017-06-09 12:03:46 +01002360 gfp_t noreclaim;
Imre Deake2273302015-07-09 12:59:05 +03002361 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07002362
Chris Wilsone0ff7a72018-09-03 09:33:36 +01002363 /*
2364 * Assert that the object is not currently in any GPU domain. As it
Chris Wilson6c085a72012-08-20 11:40:46 +02002365 * wasn't in the GTT, there shouldn't be any way it could have been in
2366 * a GPU cache
2367 */
Christian Königc0a51fd2018-02-16 13:43:38 +01002368 GEM_BUG_ON(obj->read_domains & I915_GEM_GPU_DOMAINS);
2369 GEM_BUG_ON(obj->write_domain & I915_GEM_GPU_DOMAINS);
Chris Wilson6c085a72012-08-20 11:40:46 +02002370
Chris Wilsone0ff7a72018-09-03 09:33:36 +01002371 /*
2372 * If there's no chance of allocating enough pages for the whole
2373 * object, bail early.
2374 */
Arun KSca79b0c2018-12-28 00:34:29 -08002375 if (page_count > totalram_pages())
Chris Wilsone0ff7a72018-09-03 09:33:36 +01002376 return -ENOMEM;
2377
Chris Wilson9da3da62012-06-01 15:20:22 +01002378 st = kmalloc(sizeof(*st), GFP_KERNEL);
2379 if (st == NULL)
Matthew Auldb91b09e2017-10-06 23:18:17 +01002380 return -ENOMEM;
Eric Anholt673a3942008-07-30 12:06:12 -07002381
Chris Wilsond766ef52016-12-19 12:43:45 +00002382rebuild_st:
Chris Wilson9da3da62012-06-01 15:20:22 +01002383 if (sg_alloc_table(st, page_count, GFP_KERNEL)) {
Chris Wilson9da3da62012-06-01 15:20:22 +01002384 kfree(st);
Matthew Auldb91b09e2017-10-06 23:18:17 +01002385 return -ENOMEM;
Chris Wilson9da3da62012-06-01 15:20:22 +01002386 }
2387
Chris Wilsone0ff7a72018-09-03 09:33:36 +01002388 /*
2389 * Get the list of pages out of our struct file. They'll be pinned
Chris Wilson9da3da62012-06-01 15:20:22 +01002390 * at this point until we release them.
2391 *
2392 * Fail silently without starting the shrinker
2393 */
Al Viro93c76a32015-12-04 23:45:44 -05002394 mapping = obj->base.filp->f_mapping;
Kuo-Hsin Yang64e3d122018-11-06 13:23:24 +00002395 mapping_set_unevictable(mapping);
Chris Wilson0f6ab552017-06-09 12:03:48 +01002396 noreclaim = mapping_gfp_constraint(mapping, ~__GFP_RECLAIM);
Chris Wilson4846bf02017-06-09 12:03:46 +01002397 noreclaim |= __GFP_NORETRY | __GFP_NOWARN;
2398
Imre Deak90797e62013-02-18 19:28:03 +02002399 sg = st->sgl;
2400 st->nents = 0;
Matthew Auld84e89782017-10-09 12:00:24 +01002401 sg_page_sizes = 0;
Imre Deak90797e62013-02-18 19:28:03 +02002402 for (i = 0; i < page_count; i++) {
Chris Wilson4846bf02017-06-09 12:03:46 +01002403 const unsigned int shrink[] = {
2404 I915_SHRINK_BOUND | I915_SHRINK_UNBOUND | I915_SHRINK_PURGEABLE,
2405 0,
2406 }, *s = shrink;
2407 gfp_t gfp = noreclaim;
2408
2409 do {
Chris Wilsone6db7f42018-11-05 17:06:40 +00002410 cond_resched();
Chris Wilson6c085a72012-08-20 11:40:46 +02002411 page = shmem_read_mapping_page_gfp(mapping, i, gfp);
Chengguang Xu772b5402019-02-21 10:08:19 +08002412 if (!IS_ERR(page))
Chris Wilson4846bf02017-06-09 12:03:46 +01002413 break;
2414
2415 if (!*s) {
2416 ret = PTR_ERR(page);
2417 goto err_sg;
2418 }
2419
Chris Wilson912d5722017-09-06 16:19:30 -07002420 i915_gem_shrink(dev_priv, 2 * page_count, NULL, *s++);
Chris Wilson24f8e002017-03-22 11:05:21 +00002421
Chris Wilsone0ff7a72018-09-03 09:33:36 +01002422 /*
2423 * We've tried hard to allocate the memory by reaping
Chris Wilson6c085a72012-08-20 11:40:46 +02002424 * our own buffer, now let the real VM do its job and
2425 * go down in flames if truly OOM.
Chris Wilson24f8e002017-03-22 11:05:21 +00002426 *
2427 * However, since graphics tend to be disposable,
2428 * defer the oom here by reporting the ENOMEM back
2429 * to userspace.
Chris Wilson6c085a72012-08-20 11:40:46 +02002430 */
Chris Wilson4846bf02017-06-09 12:03:46 +01002431 if (!*s) {
2432 /* reclaim and warn, but no oom */
2433 gfp = mapping_gfp_mask(mapping);
Chris Wilsoneaf41802017-06-09 12:03:47 +01002434
Chris Wilsone0ff7a72018-09-03 09:33:36 +01002435 /*
2436 * Our bo are always dirty and so we require
Chris Wilsoneaf41802017-06-09 12:03:47 +01002437 * kswapd to reclaim our pages (direct reclaim
2438 * does not effectively begin pageout of our
2439 * buffers on its own). However, direct reclaim
2440 * only waits for kswapd when under allocation
2441 * congestion. So as a result __GFP_RECLAIM is
2442 * unreliable and fails to actually reclaim our
2443 * dirty pages -- unless you try over and over
2444 * again with !__GFP_NORETRY. However, we still
2445 * want to fail this allocation rather than
2446 * trigger the out-of-memory killer and for
Michal Hockodbb32952017-07-12 14:36:55 -07002447 * this we want __GFP_RETRY_MAYFAIL.
Chris Wilsoneaf41802017-06-09 12:03:47 +01002448 */
Michal Hockodbb32952017-07-12 14:36:55 -07002449 gfp |= __GFP_RETRY_MAYFAIL;
Imre Deake2273302015-07-09 12:59:05 +03002450 }
Chris Wilson4846bf02017-06-09 12:03:46 +01002451 } while (1);
2452
Chris Wilson871dfbd2016-10-11 09:20:21 +01002453 if (!i ||
2454 sg->length >= max_segment ||
2455 page_to_pfn(page) != last_pfn + 1) {
Matthew Aulda5c081662017-10-06 23:18:18 +01002456 if (i) {
Matthew Auld84e89782017-10-09 12:00:24 +01002457 sg_page_sizes |= sg->length;
Imre Deak90797e62013-02-18 19:28:03 +02002458 sg = sg_next(sg);
Matthew Aulda5c081662017-10-06 23:18:18 +01002459 }
Imre Deak90797e62013-02-18 19:28:03 +02002460 st->nents++;
2461 sg_set_page(sg, page, PAGE_SIZE, 0);
2462 } else {
2463 sg->length += PAGE_SIZE;
2464 }
2465 last_pfn = page_to_pfn(page);
Daniel Vetter3bbbe702013-10-07 17:15:45 -03002466
2467 /* Check that the i965g/gm workaround works. */
2468 WARN_ON((gfp & __GFP_DMA32) && (last_pfn >= 0x00100000UL));
Eric Anholt673a3942008-07-30 12:06:12 -07002469 }
Matthew Aulda5c081662017-10-06 23:18:18 +01002470 if (sg) { /* loop terminated early; short sg table */
Matthew Auld84e89782017-10-09 12:00:24 +01002471 sg_page_sizes |= sg->length;
Konrad Rzeszutek Wilk426729d2013-06-24 11:47:48 -04002472 sg_mark_end(sg);
Matthew Aulda5c081662017-10-06 23:18:18 +01002473 }
Chris Wilson74ce6b62012-10-19 15:51:06 +01002474
Tvrtko Ursulin0c40ce12016-11-09 15:13:43 +00002475 /* Trim unused sg entries to avoid wasting memory. */
2476 i915_sg_trim(st);
2477
Chris Wilson03ac84f2016-10-28 13:58:36 +01002478 ret = i915_gem_gtt_prepare_pages(obj, st);
Chris Wilsond766ef52016-12-19 12:43:45 +00002479 if (ret) {
Chris Wilsone0ff7a72018-09-03 09:33:36 +01002480 /*
2481 * DMA remapping failed? One possible cause is that
Chris Wilsond766ef52016-12-19 12:43:45 +00002482 * it could not reserve enough large entries, asking
2483 * for PAGE_SIZE chunks instead may be helpful.
2484 */
2485 if (max_segment > PAGE_SIZE) {
2486 for_each_sgt_page(page, sgt_iter, st)
2487 put_page(page);
2488 sg_free_table(st);
2489
2490 max_segment = PAGE_SIZE;
2491 goto rebuild_st;
2492 } else {
2493 dev_warn(&dev_priv->drm.pdev->dev,
2494 "Failed to DMA remap %lu pages\n",
2495 page_count);
2496 goto err_pages;
2497 }
2498 }
Imre Deake2273302015-07-09 12:59:05 +03002499
Eric Anholt673a3942008-07-30 12:06:12 -07002500 if (i915_gem_object_needs_bit17_swizzle(obj))
Chris Wilson03ac84f2016-10-28 13:58:36 +01002501 i915_gem_object_do_bit_17_swizzle(obj, st);
Eric Anholt673a3942008-07-30 12:06:12 -07002502
Matthew Auld84e89782017-10-09 12:00:24 +01002503 __i915_gem_object_set_pages(obj, st, sg_page_sizes);
Matthew Auldb91b09e2017-10-06 23:18:17 +01002504
2505 return 0;
Eric Anholt673a3942008-07-30 12:06:12 -07002506
Chris Wilsonb17993b2016-11-14 11:29:30 +00002507err_sg:
Imre Deak90797e62013-02-18 19:28:03 +02002508 sg_mark_end(sg);
Chris Wilsonb17993b2016-11-14 11:29:30 +00002509err_pages:
Kuo-Hsin Yang64e3d122018-11-06 13:23:24 +00002510 mapping_clear_unevictable(mapping);
2511 pagevec_init(&pvec);
2512 for_each_sgt_page(page, sgt_iter, st) {
2513 if (!pagevec_add(&pvec, page))
2514 check_release_pagevec(&pvec);
2515 }
2516 if (pagevec_count(&pvec))
2517 check_release_pagevec(&pvec);
Chris Wilson9da3da62012-06-01 15:20:22 +01002518 sg_free_table(st);
2519 kfree(st);
Chris Wilson0820baf2014-03-25 13:23:03 +00002520
Chris Wilsone0ff7a72018-09-03 09:33:36 +01002521 /*
2522 * shmemfs first checks if there is enough memory to allocate the page
Chris Wilson0820baf2014-03-25 13:23:03 +00002523 * and reports ENOSPC should there be insufficient, along with the usual
2524 * ENOMEM for a genuine allocation failure.
2525 *
2526 * We use ENOSPC in our driver to mean that we have run out of aperture
2527 * space and so want to translate the error from shmemfs back to our
2528 * usual understanding of ENOMEM.
2529 */
Imre Deake2273302015-07-09 12:59:05 +03002530 if (ret == -ENOSPC)
2531 ret = -ENOMEM;
2532
Matthew Auldb91b09e2017-10-06 23:18:17 +01002533 return ret;
Chris Wilson03ac84f2016-10-28 13:58:36 +01002534}
2535
2536void __i915_gem_object_set_pages(struct drm_i915_gem_object *obj,
Matthew Aulda5c081662017-10-06 23:18:18 +01002537 struct sg_table *pages,
Matthew Auld84e89782017-10-09 12:00:24 +01002538 unsigned int sg_page_sizes)
Chris Wilson03ac84f2016-10-28 13:58:36 +01002539{
Matthew Aulda5c081662017-10-06 23:18:18 +01002540 struct drm_i915_private *i915 = to_i915(obj->base.dev);
2541 unsigned long supported = INTEL_INFO(i915)->page_sizes;
2542 int i;
2543
Chris Wilson1233e2d2016-10-28 13:58:37 +01002544 lockdep_assert_held(&obj->mm.lock);
Chris Wilson03ac84f2016-10-28 13:58:36 +01002545
Chris Wilsona679f582019-03-21 16:19:07 +00002546 /* Make the pages coherent with the GPU (flushing any swapin). */
2547 if (obj->cache_dirty) {
2548 obj->write_domain = 0;
2549 if (i915_gem_object_has_struct_page(obj))
2550 drm_clflush_sg(pages);
2551 obj->cache_dirty = false;
2552 }
2553
Chris Wilson03ac84f2016-10-28 13:58:36 +01002554 obj->mm.get_page.sg_pos = pages->sgl;
2555 obj->mm.get_page.sg_idx = 0;
2556
2557 obj->mm.pages = pages;
Chris Wilson2c3a3f42016-11-04 10:30:01 +00002558
2559 if (i915_gem_object_is_tiled(obj) &&
Chris Wilsonf2123812017-10-16 12:40:37 +01002560 i915->quirks & QUIRK_PIN_SWIZZLED_PAGES) {
Chris Wilson2c3a3f42016-11-04 10:30:01 +00002561 GEM_BUG_ON(obj->mm.quirked);
2562 __i915_gem_object_pin_pages(obj);
2563 obj->mm.quirked = true;
2564 }
Matthew Aulda5c081662017-10-06 23:18:18 +01002565
Matthew Auld84e89782017-10-09 12:00:24 +01002566 GEM_BUG_ON(!sg_page_sizes);
2567 obj->mm.page_sizes.phys = sg_page_sizes;
Matthew Aulda5c081662017-10-06 23:18:18 +01002568
2569 /*
Matthew Auld84e89782017-10-09 12:00:24 +01002570 * Calculate the supported page-sizes which fit into the given
2571 * sg_page_sizes. This will give us the page-sizes which we may be able
2572 * to use opportunistically when later inserting into the GTT. For
2573 * example if phys=2G, then in theory we should be able to use 1G, 2M,
2574 * 64K or 4K pages, although in practice this will depend on a number of
2575 * other factors.
Matthew Aulda5c081662017-10-06 23:18:18 +01002576 */
2577 obj->mm.page_sizes.sg = 0;
2578 for_each_set_bit(i, &supported, ilog2(I915_GTT_MAX_PAGE_SIZE) + 1) {
2579 if (obj->mm.page_sizes.phys & ~0u << i)
2580 obj->mm.page_sizes.sg |= BIT(i);
2581 }
Matthew Aulda5c081662017-10-06 23:18:18 +01002582 GEM_BUG_ON(!HAS_PAGE_SIZES(i915, obj->mm.page_sizes.sg));
Chris Wilsonf2123812017-10-16 12:40:37 +01002583
2584 spin_lock(&i915->mm.obj_lock);
2585 list_add(&obj->mm.link, &i915->mm.unbound_list);
2586 spin_unlock(&i915->mm.obj_lock);
Chris Wilson03ac84f2016-10-28 13:58:36 +01002587}
2588
2589static int ____i915_gem_object_get_pages(struct drm_i915_gem_object *obj)
2590{
Matthew Auldb91b09e2017-10-06 23:18:17 +01002591 int err;
Chris Wilson03ac84f2016-10-28 13:58:36 +01002592
2593 if (unlikely(obj->mm.madv != I915_MADV_WILLNEED)) {
2594 DRM_DEBUG("Attempting to obtain a purgeable object\n");
2595 return -EFAULT;
2596 }
2597
Matthew Auldb91b09e2017-10-06 23:18:17 +01002598 err = obj->ops->get_pages(obj);
Matthew Auldb65a9b92017-12-18 10:38:55 +00002599 GEM_BUG_ON(!err && !i915_gem_object_has_pages(obj));
Chris Wilson03ac84f2016-10-28 13:58:36 +01002600
Matthew Auldb91b09e2017-10-06 23:18:17 +01002601 return err;
Eric Anholt673a3942008-07-30 12:06:12 -07002602}
2603
Chris Wilson37e680a2012-06-07 15:38:42 +01002604/* Ensure that the associated pages are gathered from the backing storage
Chris Wilson1233e2d2016-10-28 13:58:37 +01002605 * and pinned into our object. i915_gem_object_pin_pages() may be called
Chris Wilson37e680a2012-06-07 15:38:42 +01002606 * multiple times before they are released by a single call to
Chris Wilson1233e2d2016-10-28 13:58:37 +01002607 * i915_gem_object_unpin_pages() - once the pages are no longer referenced
Chris Wilson37e680a2012-06-07 15:38:42 +01002608 * either as a result of memory pressure (reaping pages under the shrinker)
2609 * or as the object is itself released.
2610 */
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002611int __i915_gem_object_get_pages(struct drm_i915_gem_object *obj)
Chris Wilson37e680a2012-06-07 15:38:42 +01002612{
Chris Wilson03ac84f2016-10-28 13:58:36 +01002613 int err;
Chris Wilson37e680a2012-06-07 15:38:42 +01002614
Chris Wilson1233e2d2016-10-28 13:58:37 +01002615 err = mutex_lock_interruptible(&obj->mm.lock);
2616 if (err)
2617 return err;
Chris Wilson4c7d62c2016-10-28 13:58:32 +01002618
Chris Wilsonf1fa4f42017-10-13 21:26:13 +01002619 if (unlikely(!i915_gem_object_has_pages(obj))) {
Chris Wilson88c880b2017-09-06 14:52:20 +01002620 GEM_BUG_ON(i915_gem_object_has_pinned_pages(obj));
2621
Chris Wilson2c3a3f42016-11-04 10:30:01 +00002622 err = ____i915_gem_object_get_pages(obj);
2623 if (err)
2624 goto unlock;
2625
2626 smp_mb__before_atomic();
Chris Wilson1233e2d2016-10-28 13:58:37 +01002627 }
Chris Wilson2c3a3f42016-11-04 10:30:01 +00002628 atomic_inc(&obj->mm.pages_pin_count);
Chris Wilson43e28f02013-01-08 10:53:09 +00002629
Chris Wilson1233e2d2016-10-28 13:58:37 +01002630unlock:
2631 mutex_unlock(&obj->mm.lock);
Chris Wilson03ac84f2016-10-28 13:58:36 +01002632 return err;
Eric Anholt673a3942008-07-30 12:06:12 -07002633}
2634
Dave Gordondd6034c2016-05-20 11:54:04 +01002635/* The 'mapping' part of i915_gem_object_pin_map() below */
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002636static void *i915_gem_object_map(const struct drm_i915_gem_object *obj,
2637 enum i915_map_type type)
Dave Gordondd6034c2016-05-20 11:54:04 +01002638{
2639 unsigned long n_pages = obj->base.size >> PAGE_SHIFT;
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002640 struct sg_table *sgt = obj->mm.pages;
Dave Gordon85d12252016-05-20 11:54:06 +01002641 struct sgt_iter sgt_iter;
2642 struct page *page;
Dave Gordonb338fa42016-05-20 11:54:05 +01002643 struct page *stack_pages[32];
2644 struct page **pages = stack_pages;
Dave Gordondd6034c2016-05-20 11:54:04 +01002645 unsigned long i = 0;
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002646 pgprot_t pgprot;
Dave Gordondd6034c2016-05-20 11:54:04 +01002647 void *addr;
2648
2649 /* A single page can always be kmapped */
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002650 if (n_pages == 1 && type == I915_MAP_WB)
Dave Gordondd6034c2016-05-20 11:54:04 +01002651 return kmap(sg_page(sgt->sgl));
2652
Dave Gordonb338fa42016-05-20 11:54:05 +01002653 if (n_pages > ARRAY_SIZE(stack_pages)) {
2654 /* Too big for stack -- allocate temporary array instead */
Michal Hocko0ee931c2017-09-13 16:28:29 -07002655 pages = kvmalloc_array(n_pages, sizeof(*pages), GFP_KERNEL);
Dave Gordonb338fa42016-05-20 11:54:05 +01002656 if (!pages)
2657 return NULL;
2658 }
Dave Gordondd6034c2016-05-20 11:54:04 +01002659
Dave Gordon85d12252016-05-20 11:54:06 +01002660 for_each_sgt_page(page, sgt_iter, sgt)
2661 pages[i++] = page;
Dave Gordondd6034c2016-05-20 11:54:04 +01002662
2663 /* Check that we have the expected number of pages */
2664 GEM_BUG_ON(i != n_pages);
2665
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002666 switch (type) {
Chris Wilsona575c672017-08-28 11:46:31 +01002667 default:
2668 MISSING_CASE(type);
2669 /* fallthrough to use PAGE_KERNEL anyway */
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002670 case I915_MAP_WB:
2671 pgprot = PAGE_KERNEL;
2672 break;
2673 case I915_MAP_WC:
2674 pgprot = pgprot_writecombine(PAGE_KERNEL_IO);
2675 break;
2676 }
2677 addr = vmap(pages, n_pages, 0, pgprot);
Dave Gordondd6034c2016-05-20 11:54:04 +01002678
Dave Gordonb338fa42016-05-20 11:54:05 +01002679 if (pages != stack_pages)
Michal Hocko20981052017-05-17 14:23:12 +02002680 kvfree(pages);
Dave Gordondd6034c2016-05-20 11:54:04 +01002681
2682 return addr;
2683}
2684
2685/* get, pin, and map the pages of the object into kernel space */
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002686void *i915_gem_object_pin_map(struct drm_i915_gem_object *obj,
2687 enum i915_map_type type)
Chris Wilson0a798eb2016-04-08 12:11:11 +01002688{
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002689 enum i915_map_type has_type;
2690 bool pinned;
2691 void *ptr;
Chris Wilson0a798eb2016-04-08 12:11:11 +01002692 int ret;
2693
Tina Zhanga03f3952017-11-14 10:25:13 +00002694 if (unlikely(!i915_gem_object_has_struct_page(obj)))
2695 return ERR_PTR(-ENXIO);
Chris Wilson0a798eb2016-04-08 12:11:11 +01002696
Chris Wilson1233e2d2016-10-28 13:58:37 +01002697 ret = mutex_lock_interruptible(&obj->mm.lock);
Chris Wilson0a798eb2016-04-08 12:11:11 +01002698 if (ret)
2699 return ERR_PTR(ret);
2700
Chris Wilsona575c672017-08-28 11:46:31 +01002701 pinned = !(type & I915_MAP_OVERRIDE);
2702 type &= ~I915_MAP_OVERRIDE;
2703
Chris Wilson1233e2d2016-10-28 13:58:37 +01002704 if (!atomic_inc_not_zero(&obj->mm.pages_pin_count)) {
Chris Wilsonf1fa4f42017-10-13 21:26:13 +01002705 if (unlikely(!i915_gem_object_has_pages(obj))) {
Chris Wilson88c880b2017-09-06 14:52:20 +01002706 GEM_BUG_ON(i915_gem_object_has_pinned_pages(obj));
2707
Chris Wilson2c3a3f42016-11-04 10:30:01 +00002708 ret = ____i915_gem_object_get_pages(obj);
2709 if (ret)
2710 goto err_unlock;
Chris Wilson1233e2d2016-10-28 13:58:37 +01002711
Chris Wilson2c3a3f42016-11-04 10:30:01 +00002712 smp_mb__before_atomic();
2713 }
2714 atomic_inc(&obj->mm.pages_pin_count);
Chris Wilson1233e2d2016-10-28 13:58:37 +01002715 pinned = false;
2716 }
Chris Wilsonf1fa4f42017-10-13 21:26:13 +01002717 GEM_BUG_ON(!i915_gem_object_has_pages(obj));
Chris Wilson0a798eb2016-04-08 12:11:11 +01002718
Chris Wilson0ce81782017-05-17 13:09:59 +01002719 ptr = page_unpack_bits(obj->mm.mapping, &has_type);
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002720 if (ptr && has_type != type) {
2721 if (pinned) {
2722 ret = -EBUSY;
Chris Wilson1233e2d2016-10-28 13:58:37 +01002723 goto err_unpin;
Chris Wilson0a798eb2016-04-08 12:11:11 +01002724 }
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002725
2726 if (is_vmalloc_addr(ptr))
2727 vunmap(ptr);
2728 else
2729 kunmap(kmap_to_page(ptr));
2730
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002731 ptr = obj->mm.mapping = NULL;
Chris Wilson0a798eb2016-04-08 12:11:11 +01002732 }
2733
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002734 if (!ptr) {
2735 ptr = i915_gem_object_map(obj, type);
2736 if (!ptr) {
2737 ret = -ENOMEM;
Chris Wilson1233e2d2016-10-28 13:58:37 +01002738 goto err_unpin;
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002739 }
2740
Chris Wilson0ce81782017-05-17 13:09:59 +01002741 obj->mm.mapping = page_pack_bits(ptr, type);
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002742 }
2743
Chris Wilson1233e2d2016-10-28 13:58:37 +01002744out_unlock:
2745 mutex_unlock(&obj->mm.lock);
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002746 return ptr;
2747
Chris Wilson1233e2d2016-10-28 13:58:37 +01002748err_unpin:
2749 atomic_dec(&obj->mm.pages_pin_count);
2750err_unlock:
2751 ptr = ERR_PTR(ret);
2752 goto out_unlock;
Chris Wilson0a798eb2016-04-08 12:11:11 +01002753}
2754
Chris Wilsona679f582019-03-21 16:19:07 +00002755void __i915_gem_object_flush_map(struct drm_i915_gem_object *obj,
2756 unsigned long offset,
2757 unsigned long size)
2758{
2759 enum i915_map_type has_type;
2760 void *ptr;
2761
2762 GEM_BUG_ON(!i915_gem_object_has_pinned_pages(obj));
2763 GEM_BUG_ON(range_overflows_t(typeof(obj->base.size),
2764 offset, size, obj->base.size));
2765
2766 obj->mm.dirty = true;
2767
2768 if (obj->cache_coherent & I915_BO_CACHE_COHERENT_FOR_WRITE)
2769 return;
2770
2771 ptr = page_unpack_bits(obj->mm.mapping, &has_type);
2772 if (has_type == I915_MAP_WC)
2773 return;
2774
2775 drm_clflush_virt_range(ptr + offset, size);
2776 if (size == obj->base.size) {
2777 obj->write_domain &= ~I915_GEM_DOMAIN_CPU;
2778 obj->cache_dirty = false;
2779 }
2780}
2781
Chris Wilson7c55e2c2017-03-07 12:03:38 +00002782static int
2783i915_gem_object_pwrite_gtt(struct drm_i915_gem_object *obj,
2784 const struct drm_i915_gem_pwrite *arg)
2785{
2786 struct address_space *mapping = obj->base.filp->f_mapping;
2787 char __user *user_data = u64_to_user_ptr(arg->data_ptr);
2788 u64 remain, offset;
2789 unsigned int pg;
2790
2791 /* Before we instantiate/pin the backing store for our use, we
2792 * can prepopulate the shmemfs filp efficiently using a write into
2793 * the pagecache. We avoid the penalty of instantiating all the
2794 * pages, important if the user is just writing to a few and never
2795 * uses the object on the GPU, and using a direct write into shmemfs
2796 * allows it to avoid the cost of retrieving a page (either swapin
2797 * or clearing-before-use) before it is overwritten.
2798 */
Chris Wilsonf1fa4f42017-10-13 21:26:13 +01002799 if (i915_gem_object_has_pages(obj))
Chris Wilson7c55e2c2017-03-07 12:03:38 +00002800 return -ENODEV;
2801
Chris Wilsona6d65e42017-10-16 21:27:32 +01002802 if (obj->mm.madv != I915_MADV_WILLNEED)
2803 return -EFAULT;
2804
Chris Wilson7c55e2c2017-03-07 12:03:38 +00002805 /* Before the pages are instantiated the object is treated as being
2806 * in the CPU domain. The pages will be clflushed as required before
2807 * use, and we can freely write into the pages directly. If userspace
2808 * races pwrite with any other operation; corruption will ensue -
2809 * that is userspace's prerogative!
2810 */
2811
2812 remain = arg->size;
2813 offset = arg->offset;
2814 pg = offset_in_page(offset);
2815
2816 do {
2817 unsigned int len, unwritten;
2818 struct page *page;
2819 void *data, *vaddr;
2820 int err;
2821
2822 len = PAGE_SIZE - pg;
2823 if (len > remain)
2824 len = remain;
2825
2826 err = pagecache_write_begin(obj->base.filp, mapping,
2827 offset, len, 0,
2828 &page, &data);
2829 if (err < 0)
2830 return err;
2831
2832 vaddr = kmap(page);
2833 unwritten = copy_from_user(vaddr + pg, user_data, len);
2834 kunmap(page);
2835
2836 err = pagecache_write_end(obj->base.filp, mapping,
2837 offset, len, len - unwritten,
2838 page, data);
2839 if (err < 0)
2840 return err;
2841
2842 if (unwritten)
2843 return -EFAULT;
2844
2845 remain -= len;
2846 user_data += len;
2847 offset += len;
2848 pg = 0;
2849 } while (remain);
2850
2851 return 0;
2852}
2853
Daniel Vetter75ef9da2010-08-21 00:25:16 +02002854static void
Eric Anholt673a3942008-07-30 12:06:12 -07002855i915_gem_retire_work_handler(struct work_struct *work)
2856{
Chris Wilsonb29c19b2013-09-25 17:34:56 +01002857 struct drm_i915_private *dev_priv =
Chris Wilson67d97da2016-07-04 08:08:31 +01002858 container_of(work, typeof(*dev_priv), gt.retire_work.work);
Chris Wilson91c8a322016-07-05 10:40:23 +01002859 struct drm_device *dev = &dev_priv->drm;
Eric Anholt673a3942008-07-30 12:06:12 -07002860
Chris Wilson891b48c2010-09-29 12:26:37 +01002861 /* Come back later if the device is busy... */
Chris Wilsonb29c19b2013-09-25 17:34:56 +01002862 if (mutex_trylock(&dev->struct_mutex)) {
Chris Wilsone61e0f52018-02-21 09:56:36 +00002863 i915_retire_requests(dev_priv);
Chris Wilsonb29c19b2013-09-25 17:34:56 +01002864 mutex_unlock(&dev->struct_mutex);
2865 }
Chris Wilson67d97da2016-07-04 08:08:31 +01002866
Chris Wilson88923042018-01-29 14:41:04 +00002867 /*
2868 * Keep the retire handler running until we are finally idle.
Chris Wilson67d97da2016-07-04 08:08:31 +01002869 * We do not need to do this test under locking as in the worst-case
2870 * we queue the retire worker once too often.
2871 */
Chris Wilson88923042018-01-29 14:41:04 +00002872 if (READ_ONCE(dev_priv->gt.awake))
Chris Wilson67d97da2016-07-04 08:08:31 +01002873 queue_delayed_work(dev_priv->wq,
2874 &dev_priv->gt.retire_work,
Chris Wilsonbcb45082012-10-05 17:02:57 +01002875 round_jiffies_up_relative(HZ));
Chris Wilsonb29c19b2013-09-25 17:34:56 +01002876}
Chris Wilson891b48c2010-09-29 12:26:37 +01002877
Chris Wilsonc6eeb472019-03-08 09:36:56 +00002878static bool switch_to_kernel_context_sync(struct drm_i915_private *i915,
2879 unsigned long mask)
Chris Wilson5861b012019-03-08 09:36:54 +00002880{
2881 bool result = true;
2882
2883 /*
2884 * Even if we fail to switch, give whatever is running a small chance
2885 * to save itself before we report the failure. Yes, this may be a
2886 * false positive due to e.g. ENOMEM, caveat emptor!
2887 */
Chris Wilsonc6eeb472019-03-08 09:36:56 +00002888 if (i915_gem_switch_to_kernel_context(i915, mask))
Chris Wilson5861b012019-03-08 09:36:54 +00002889 result = false;
2890
2891 if (i915_gem_wait_for_idle(i915,
2892 I915_WAIT_LOCKED |
2893 I915_WAIT_FOR_IDLE_BOOST,
2894 I915_GEM_IDLE_TIMEOUT))
2895 result = false;
2896
Chris Wilson7d6ce552019-03-08 09:36:57 +00002897 if (!result) {
Chris Wilson831ebf12019-03-08 13:45:12 +00002898 if (i915_modparams.reset) { /* XXX hide warning from gem_eio */
2899 dev_err(i915->drm.dev,
2900 "Failed to idle engines, declaring wedged!\n");
2901 GEM_TRACE_DUMP();
2902 }
2903
Chris Wilson5861b012019-03-08 09:36:54 +00002904 /* Forcibly cancel outstanding work and leave the gpu quiet. */
Chris Wilson5861b012019-03-08 09:36:54 +00002905 i915_gem_set_wedged(i915);
2906 }
2907
2908 i915_retire_requests(i915); /* ensure we flush after wedging */
2909 return result;
2910}
2911
Chris Wilson604c37d2019-03-08 09:36:55 +00002912static bool load_power_context(struct drm_i915_private *i915)
2913{
Chris Wilsonc6eeb472019-03-08 09:36:56 +00002914 /* Force loading the kernel context on all engines */
2915 if (!switch_to_kernel_context_sync(i915, ALL_ENGINES))
Chris Wilson604c37d2019-03-08 09:36:55 +00002916 return false;
2917
2918 /*
2919 * Immediately park the GPU so that we enable powersaving and
2920 * treat it as idle. The next time we issue a request, we will
2921 * unpark and start using the engine->pinned_default_state, otherwise
2922 * it is in limbo and an early reset may fail.
2923 */
2924 __i915_gem_park(i915);
2925
2926 return true;
2927}
2928
Chris Wilsonb29c19b2013-09-25 17:34:56 +01002929static void
2930i915_gem_idle_work_handler(struct work_struct *work)
2931{
Chris Wilson5861b012019-03-08 09:36:54 +00002932 struct drm_i915_private *i915 =
2933 container_of(work, typeof(*i915), gt.idle_work.work);
Chris Wilson67d97da2016-07-04 08:08:31 +01002934 bool rearm_hangcheck;
2935
Chris Wilson5861b012019-03-08 09:36:54 +00002936 if (!READ_ONCE(i915->gt.awake))
Chris Wilson67d97da2016-07-04 08:08:31 +01002937 return;
2938
Chris Wilson5861b012019-03-08 09:36:54 +00002939 if (READ_ONCE(i915->gt.active_requests))
Chris Wilson4dfacb02018-05-31 09:22:43 +01002940 return;
2941
Chris Wilson67d97da2016-07-04 08:08:31 +01002942 rearm_hangcheck =
Chris Wilson5861b012019-03-08 09:36:54 +00002943 cancel_delayed_work_sync(&i915->gpu_error.hangcheck_work);
Chris Wilson67d97da2016-07-04 08:08:31 +01002944
Chris Wilson5861b012019-03-08 09:36:54 +00002945 if (!mutex_trylock(&i915->drm.struct_mutex)) {
Chris Wilson67d97da2016-07-04 08:08:31 +01002946 /* Currently busy, come back later */
Chris Wilson5861b012019-03-08 09:36:54 +00002947 mod_delayed_work(i915->wq,
2948 &i915->gt.idle_work,
Chris Wilson67d97da2016-07-04 08:08:31 +01002949 msecs_to_jiffies(50));
2950 goto out_rearm;
2951 }
2952
Imre Deak93c97dc2016-11-07 11:20:03 +02002953 /*
Chris Wilson5861b012019-03-08 09:36:54 +00002954 * Flush out the last user context, leaving only the pinned
2955 * kernel context resident. Should anything unfortunate happen
2956 * while we are idle (such as the GPU being power cycled), no users
2957 * will be harmed.
Imre Deak93c97dc2016-11-07 11:20:03 +02002958 */
Chris Wilson5861b012019-03-08 09:36:54 +00002959 if (!work_pending(&i915->gt.idle_work.work) &&
2960 !i915->gt.active_requests) {
2961 ++i915->gt.active_requests; /* don't requeue idle */
Imre Deak93c97dc2016-11-07 11:20:03 +02002962
Chris Wilsonc6eeb472019-03-08 09:36:56 +00002963 switch_to_kernel_context_sync(i915, i915->gt.active_engines);
Chris Wilsonff320d62017-10-23 22:32:35 +01002964
Chris Wilson5861b012019-03-08 09:36:54 +00002965 if (!--i915->gt.active_requests) {
2966 __i915_gem_park(i915);
2967 rearm_hangcheck = false;
2968 }
2969 }
Chris Wilson1934f5de2018-05-31 23:40:57 +01002970
Chris Wilson5861b012019-03-08 09:36:54 +00002971 mutex_unlock(&i915->drm.struct_mutex);
Chris Wilson35c94182015-04-07 16:20:37 +01002972
Chris Wilson67d97da2016-07-04 08:08:31 +01002973out_rearm:
2974 if (rearm_hangcheck) {
Chris Wilson5861b012019-03-08 09:36:54 +00002975 GEM_BUG_ON(!i915->gt.awake);
2976 i915_queue_hangcheck(i915);
Chris Wilson35c94182015-04-07 16:20:37 +01002977 }
Eric Anholt673a3942008-07-30 12:06:12 -07002978}
2979
Chris Wilsonb1f788c2016-08-04 07:52:45 +01002980void i915_gem_close_object(struct drm_gem_object *gem, struct drm_file *file)
2981{
Chris Wilsond1b48c12017-08-16 09:52:08 +01002982 struct drm_i915_private *i915 = to_i915(gem->dev);
Chris Wilsonb1f788c2016-08-04 07:52:45 +01002983 struct drm_i915_gem_object *obj = to_intel_bo(gem);
2984 struct drm_i915_file_private *fpriv = file->driver_priv;
Chris Wilsond1b48c12017-08-16 09:52:08 +01002985 struct i915_lut_handle *lut, *ln;
Chris Wilsonb1f788c2016-08-04 07:52:45 +01002986
Chris Wilsond1b48c12017-08-16 09:52:08 +01002987 mutex_lock(&i915->drm.struct_mutex);
2988
2989 list_for_each_entry_safe(lut, ln, &obj->lut_list, obj_link) {
2990 struct i915_gem_context *ctx = lut->ctx;
2991 struct i915_vma *vma;
2992
Chris Wilson432295d2017-08-22 12:05:15 +01002993 GEM_BUG_ON(ctx->file_priv == ERR_PTR(-EBADF));
Chris Wilsond1b48c12017-08-16 09:52:08 +01002994 if (ctx->file_priv != fpriv)
2995 continue;
2996
2997 vma = radix_tree_delete(&ctx->handles_vma, lut->handle);
Chris Wilson3ffff012017-08-22 12:05:17 +01002998 GEM_BUG_ON(vma->obj != obj);
2999
3000 /* We allow the process to have multiple handles to the same
3001 * vma, in the same fd namespace, by virtue of flink/open.
3002 */
3003 GEM_BUG_ON(!vma->open_count);
3004 if (!--vma->open_count && !i915_vma_is_ggtt(vma))
Chris Wilsonb1f788c2016-08-04 07:52:45 +01003005 i915_vma_close(vma);
Chris Wilsonf8a7fde2016-10-28 13:58:29 +01003006
Chris Wilsond1b48c12017-08-16 09:52:08 +01003007 list_del(&lut->obj_link);
3008 list_del(&lut->ctx_link);
Chris Wilson4ff4b442017-06-16 15:05:16 +01003009
Chris Wilson13f1bfd2019-02-28 10:20:34 +00003010 i915_lut_handle_free(lut);
Chris Wilsond1b48c12017-08-16 09:52:08 +01003011 __i915_gem_object_release_unless_active(obj);
Chris Wilsonf8a7fde2016-10-28 13:58:29 +01003012 }
Chris Wilsond1b48c12017-08-16 09:52:08 +01003013
3014 mutex_unlock(&i915->drm.struct_mutex);
Chris Wilsonb1f788c2016-08-04 07:52:45 +01003015}
3016
Chris Wilsone95433c2016-10-28 13:58:27 +01003017static unsigned long to_wait_timeout(s64 timeout_ns)
3018{
3019 if (timeout_ns < 0)
3020 return MAX_SCHEDULE_TIMEOUT;
3021
3022 if (timeout_ns == 0)
3023 return 0;
3024
3025 return nsecs_to_jiffies_timeout(timeout_ns);
3026}
3027
Ben Widawsky5816d642012-04-11 11:18:19 -07003028/**
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07003029 * i915_gem_wait_ioctl - implements DRM_IOCTL_I915_GEM_WAIT
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01003030 * @dev: drm device pointer
3031 * @data: ioctl data blob
3032 * @file: drm file pointer
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07003033 *
3034 * Returns 0 if successful, else an error is returned with the remaining time in
3035 * the timeout parameter.
3036 * -ETIME: object is still busy after timeout
3037 * -ERESTARTSYS: signal interrupted the wait
3038 * -ENONENT: object doesn't exist
3039 * Also possible, but rare:
Chris Wilsonb8050142017-08-11 11:57:31 +01003040 * -EAGAIN: incomplete, restart syscall
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07003041 * -ENOMEM: damn
3042 * -ENODEV: Internal IRQ fail
3043 * -E?: The add request failed
3044 *
3045 * The wait ioctl with a timeout of 0 reimplements the busy ioctl. With any
3046 * non-zero timeout parameter the wait ioctl will wait for the given number of
3047 * nanoseconds on an object becoming unbusy. Since the wait itself does so
3048 * without holding struct_mutex the object may become re-busied before this
3049 * function completes. A similar but shorter * race condition exists in the busy
3050 * ioctl
3051 */
3052int
3053i915_gem_wait_ioctl(struct drm_device *dev, void *data, struct drm_file *file)
3054{
3055 struct drm_i915_gem_wait *args = data;
3056 struct drm_i915_gem_object *obj;
Chris Wilsone95433c2016-10-28 13:58:27 +01003057 ktime_t start;
3058 long ret;
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07003059
Daniel Vetter11b5d512014-09-29 15:31:26 +02003060 if (args->flags != 0)
3061 return -EINVAL;
3062
Chris Wilson03ac0642016-07-20 13:31:51 +01003063 obj = i915_gem_object_lookup(file, args->bo_handle);
Chris Wilson033d5492016-08-05 10:14:17 +01003064 if (!obj)
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07003065 return -ENOENT;
Chris Wilson033d5492016-08-05 10:14:17 +01003066
Chris Wilsone95433c2016-10-28 13:58:27 +01003067 start = ktime_get();
3068
3069 ret = i915_gem_object_wait(obj,
Chris Wilsone9eaf822018-10-01 15:47:55 +01003070 I915_WAIT_INTERRUPTIBLE |
3071 I915_WAIT_PRIORITY |
3072 I915_WAIT_ALL,
Chris Wilson62eb3c22019-02-13 09:25:04 +00003073 to_wait_timeout(args->timeout_ns));
Chris Wilsone95433c2016-10-28 13:58:27 +01003074
3075 if (args->timeout_ns > 0) {
3076 args->timeout_ns -= ktime_to_ns(ktime_sub(ktime_get(), start));
3077 if (args->timeout_ns < 0)
3078 args->timeout_ns = 0;
Chris Wilsonc1d20612017-02-16 12:54:41 +00003079
3080 /*
3081 * Apparently ktime isn't accurate enough and occasionally has a
3082 * bit of mismatch in the jiffies<->nsecs<->ktime loop. So patch
3083 * things up to make the test happy. We allow up to 1 jiffy.
3084 *
3085 * This is a regression from the timespec->ktime conversion.
3086 */
3087 if (ret == -ETIME && !nsecs_to_jiffies(args->timeout_ns))
3088 args->timeout_ns = 0;
Chris Wilsonb8050142017-08-11 11:57:31 +01003089
3090 /* Asked to wait beyond the jiffie/scheduler precision? */
3091 if (ret == -ETIME && args->timeout_ns)
3092 ret = -EAGAIN;
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07003093 }
3094
Chris Wilsonf0cd5182016-10-28 13:58:43 +01003095 i915_gem_object_put(obj);
John Harrisonff865882014-11-24 18:49:28 +00003096 return ret;
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07003097}
3098
Chris Wilson25112b62017-03-30 15:50:39 +01003099static int wait_for_engines(struct drm_i915_private *i915)
3100{
Chris Wilsonee42c002017-12-11 19:41:34 +00003101 if (wait_for(intel_engines_are_idle(i915), I915_IDLE_ENGINES_TIMEOUT)) {
Chris Wilson59e4b192017-12-11 19:41:35 +00003102 dev_err(i915->drm.dev,
3103 "Failed to idle engines, declaring wedged!\n");
Chris Wilson629820f2018-03-09 10:11:14 +00003104 GEM_TRACE_DUMP();
Chris Wilsoncad99462017-08-26 12:09:33 +01003105 i915_gem_set_wedged(i915);
3106 return -EIO;
Chris Wilson25112b62017-03-30 15:50:39 +01003107 }
3108
3109 return 0;
3110}
3111
Chris Wilson1e345562019-01-28 10:23:56 +00003112static long
3113wait_for_timelines(struct drm_i915_private *i915,
3114 unsigned int flags, long timeout)
3115{
3116 struct i915_gt_timelines *gt = &i915->gt.timelines;
3117 struct i915_timeline *tl;
3118
3119 if (!READ_ONCE(i915->gt.active_requests))
3120 return timeout;
3121
3122 mutex_lock(&gt->mutex);
Chris Wilson9407d3b2019-01-28 18:18:12 +00003123 list_for_each_entry(tl, &gt->active_list, link) {
Chris Wilson1e345562019-01-28 10:23:56 +00003124 struct i915_request *rq;
3125
Chris Wilson21950ee2019-02-05 13:00:05 +00003126 rq = i915_active_request_get_unlocked(&tl->last_request);
Chris Wilson1e345562019-01-28 10:23:56 +00003127 if (!rq)
3128 continue;
3129
3130 mutex_unlock(&gt->mutex);
3131
3132 /*
3133 * "Race-to-idle".
3134 *
3135 * Switching to the kernel context is often used a synchronous
3136 * step prior to idling, e.g. in suspend for flushing all
3137 * current operations to memory before sleeping. These we
3138 * want to complete as quickly as possible to avoid prolonged
3139 * stalls, so allow the gpu to boost to maximum clocks.
3140 */
3141 if (flags & I915_WAIT_FOR_IDLE_BOOST)
Chris Wilson62eb3c22019-02-13 09:25:04 +00003142 gen6_rps_boost(rq);
Chris Wilson1e345562019-01-28 10:23:56 +00003143
3144 timeout = i915_request_wait(rq, flags, timeout);
3145 i915_request_put(rq);
3146 if (timeout < 0)
3147 return timeout;
3148
3149 /* restart after reacquiring the lock */
3150 mutex_lock(&gt->mutex);
Chris Wilson9407d3b2019-01-28 18:18:12 +00003151 tl = list_entry(&gt->active_list, typeof(*tl), link);
Chris Wilson1e345562019-01-28 10:23:56 +00003152 }
3153 mutex_unlock(&gt->mutex);
3154
3155 return timeout;
3156}
3157
Chris Wilsonec625fb2018-07-09 13:20:42 +01003158int i915_gem_wait_for_idle(struct drm_i915_private *i915,
3159 unsigned int flags, long timeout)
Chris Wilson73cb9702016-10-28 13:58:46 +01003160{
Chris Wilsonec625fb2018-07-09 13:20:42 +01003161 GEM_TRACE("flags=%x (%s), timeout=%ld%s\n",
3162 flags, flags & I915_WAIT_LOCKED ? "locked" : "unlocked",
3163 timeout, timeout == MAX_SCHEDULE_TIMEOUT ? " (forever)" : "");
Chris Wilson09a4c022018-05-24 09:11:35 +01003164
Chris Wilson863e9fd2017-05-30 13:13:32 +01003165 /* If the device is asleep, we have no requests outstanding */
3166 if (!READ_ONCE(i915->gt.awake))
3167 return 0;
3168
Chris Wilson1e345562019-01-28 10:23:56 +00003169 timeout = wait_for_timelines(i915, flags, timeout);
3170 if (timeout < 0)
3171 return timeout;
3172
Chris Wilson9caa34a2016-11-11 14:58:08 +00003173 if (flags & I915_WAIT_LOCKED) {
Chris Wilsona89d1f92018-05-02 17:38:39 +01003174 int err;
Chris Wilson9caa34a2016-11-11 14:58:08 +00003175
3176 lockdep_assert_held(&i915->drm.struct_mutex);
3177
Chris Wilsona61b47f2018-06-27 12:53:34 +01003178 err = wait_for_engines(i915);
3179 if (err)
3180 return err;
3181
Chris Wilsone61e0f52018-02-21 09:56:36 +00003182 i915_retire_requests(i915);
Chris Wilsona89d1f92018-05-02 17:38:39 +01003183 }
Chris Wilsona61b47f2018-06-27 12:53:34 +01003184
3185 return 0;
Daniel Vetter4df2faf2010-02-19 11:52:00 +01003186}
3187
Chris Wilson5a97bcc2017-02-22 11:40:46 +00003188static void __i915_gem_object_flush_for_display(struct drm_i915_gem_object *obj)
3189{
Chris Wilsone27ab732017-06-15 13:38:49 +01003190 /*
3191 * We manually flush the CPU domain so that we can override and
3192 * force the flush for the display, and perform it asyncrhonously.
3193 */
3194 flush_write_domain(obj, ~I915_GEM_DOMAIN_CPU);
3195 if (obj->cache_dirty)
3196 i915_gem_clflush_object(obj, I915_CLFLUSH_FORCE);
Christian Königc0a51fd2018-02-16 13:43:38 +01003197 obj->write_domain = 0;
Chris Wilson5a97bcc2017-02-22 11:40:46 +00003198}
3199
3200void i915_gem_object_flush_if_display(struct drm_i915_gem_object *obj)
3201{
Chris Wilsonbd3d2252017-10-13 21:26:14 +01003202 if (!READ_ONCE(obj->pin_global))
Chris Wilson5a97bcc2017-02-22 11:40:46 +00003203 return;
3204
3205 mutex_lock(&obj->base.dev->struct_mutex);
3206 __i915_gem_object_flush_for_display(obj);
3207 mutex_unlock(&obj->base.dev->struct_mutex);
3208}
3209
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003210/**
Chris Wilsone22d8e32017-04-12 12:01:11 +01003211 * Moves a single object to the WC read, and possibly write domain.
3212 * @obj: object to act on
3213 * @write: ask for write access or read only
3214 *
3215 * This function returns when the move is complete, including waiting on
3216 * flushes to occur.
3217 */
3218int
3219i915_gem_object_set_to_wc_domain(struct drm_i915_gem_object *obj, bool write)
3220{
3221 int ret;
3222
3223 lockdep_assert_held(&obj->base.dev->struct_mutex);
3224
3225 ret = i915_gem_object_wait(obj,
3226 I915_WAIT_INTERRUPTIBLE |
3227 I915_WAIT_LOCKED |
3228 (write ? I915_WAIT_ALL : 0),
Chris Wilson62eb3c22019-02-13 09:25:04 +00003229 MAX_SCHEDULE_TIMEOUT);
Chris Wilsone22d8e32017-04-12 12:01:11 +01003230 if (ret)
3231 return ret;
3232
Christian Königc0a51fd2018-02-16 13:43:38 +01003233 if (obj->write_domain == I915_GEM_DOMAIN_WC)
Chris Wilsone22d8e32017-04-12 12:01:11 +01003234 return 0;
3235
3236 /* Flush and acquire obj->pages so that we are coherent through
3237 * direct access in memory with previous cached writes through
3238 * shmemfs and that our cache domain tracking remains valid.
3239 * For example, if the obj->filp was moved to swap without us
3240 * being notified and releasing the pages, we would mistakenly
3241 * continue to assume that the obj remained out of the CPU cached
3242 * domain.
3243 */
3244 ret = i915_gem_object_pin_pages(obj);
3245 if (ret)
3246 return ret;
3247
3248 flush_write_domain(obj, ~I915_GEM_DOMAIN_WC);
3249
3250 /* Serialise direct access to this object with the barriers for
3251 * coherent writes from the GPU, by effectively invalidating the
3252 * WC domain upon first access.
3253 */
Christian Königc0a51fd2018-02-16 13:43:38 +01003254 if ((obj->read_domains & I915_GEM_DOMAIN_WC) == 0)
Chris Wilsone22d8e32017-04-12 12:01:11 +01003255 mb();
3256
3257 /* It should now be out of any other write domains, and we can update
3258 * the domain values for our changes.
3259 */
Christian Königc0a51fd2018-02-16 13:43:38 +01003260 GEM_BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_WC) != 0);
3261 obj->read_domains |= I915_GEM_DOMAIN_WC;
Chris Wilsone22d8e32017-04-12 12:01:11 +01003262 if (write) {
Christian Königc0a51fd2018-02-16 13:43:38 +01003263 obj->read_domains = I915_GEM_DOMAIN_WC;
3264 obj->write_domain = I915_GEM_DOMAIN_WC;
Chris Wilsone22d8e32017-04-12 12:01:11 +01003265 obj->mm.dirty = true;
3266 }
3267
3268 i915_gem_object_unpin_pages(obj);
3269 return 0;
3270}
3271
3272/**
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003273 * Moves a single object to the GTT read, and possibly write domain.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01003274 * @obj: object to act on
3275 * @write: ask for write access or read only
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003276 *
3277 * This function returns when the move is complete, including waiting on
3278 * flushes to occur.
3279 */
Jesse Barnes79e53942008-11-07 14:24:08 -08003280int
Chris Wilson20217462010-11-23 15:26:33 +00003281i915_gem_object_set_to_gtt_domain(struct drm_i915_gem_object *obj, bool write)
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003282{
Eric Anholte47c68e2008-11-14 13:35:19 -08003283 int ret;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003284
Chris Wilsone95433c2016-10-28 13:58:27 +01003285 lockdep_assert_held(&obj->base.dev->struct_mutex);
Chris Wilson4c7d62c2016-10-28 13:58:32 +01003286
Chris Wilsone95433c2016-10-28 13:58:27 +01003287 ret = i915_gem_object_wait(obj,
3288 I915_WAIT_INTERRUPTIBLE |
3289 I915_WAIT_LOCKED |
3290 (write ? I915_WAIT_ALL : 0),
Chris Wilson62eb3c22019-02-13 09:25:04 +00003291 MAX_SCHEDULE_TIMEOUT);
Chris Wilson88241782011-01-07 17:09:48 +00003292 if (ret)
3293 return ret;
3294
Christian Königc0a51fd2018-02-16 13:43:38 +01003295 if (obj->write_domain == I915_GEM_DOMAIN_GTT)
Chris Wilsonc13d87e2016-07-20 09:21:15 +01003296 return 0;
3297
Chris Wilson43566de2015-01-02 16:29:29 +05303298 /* Flush and acquire obj->pages so that we are coherent through
3299 * direct access in memory with previous cached writes through
3300 * shmemfs and that our cache domain tracking remains valid.
3301 * For example, if the obj->filp was moved to swap without us
3302 * being notified and releasing the pages, we would mistakenly
3303 * continue to assume that the obj remained out of the CPU cached
3304 * domain.
3305 */
Chris Wilsona4f5ea62016-10-28 13:58:35 +01003306 ret = i915_gem_object_pin_pages(obj);
Chris Wilson43566de2015-01-02 16:29:29 +05303307 if (ret)
3308 return ret;
3309
Chris Wilsonef749212017-04-12 12:01:10 +01003310 flush_write_domain(obj, ~I915_GEM_DOMAIN_GTT);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003311
Chris Wilsond0a57782012-10-09 19:24:37 +01003312 /* Serialise direct access to this object with the barriers for
3313 * coherent writes from the GPU, by effectively invalidating the
3314 * GTT domain upon first access.
3315 */
Christian Königc0a51fd2018-02-16 13:43:38 +01003316 if ((obj->read_domains & I915_GEM_DOMAIN_GTT) == 0)
Chris Wilsond0a57782012-10-09 19:24:37 +01003317 mb();
3318
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003319 /* It should now be out of any other write domains, and we can update
3320 * the domain values for our changes.
3321 */
Christian Königc0a51fd2018-02-16 13:43:38 +01003322 GEM_BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_GTT) != 0);
3323 obj->read_domains |= I915_GEM_DOMAIN_GTT;
Eric Anholte47c68e2008-11-14 13:35:19 -08003324 if (write) {
Christian Königc0a51fd2018-02-16 13:43:38 +01003325 obj->read_domains = I915_GEM_DOMAIN_GTT;
3326 obj->write_domain = I915_GEM_DOMAIN_GTT;
Chris Wilsona4f5ea62016-10-28 13:58:35 +01003327 obj->mm.dirty = true;
Eric Anholte47c68e2008-11-14 13:35:19 -08003328 }
3329
Chris Wilsona4f5ea62016-10-28 13:58:35 +01003330 i915_gem_object_unpin_pages(obj);
Eric Anholte47c68e2008-11-14 13:35:19 -08003331 return 0;
3332}
3333
Chris Wilsonef55f922015-10-09 14:11:27 +01003334/**
3335 * Changes the cache-level of an object across all VMA.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01003336 * @obj: object to act on
3337 * @cache_level: new cache level to set for the object
Chris Wilsonef55f922015-10-09 14:11:27 +01003338 *
3339 * After this function returns, the object will be in the new cache-level
3340 * across all GTT and the contents of the backing storage will be coherent,
3341 * with respect to the new cache-level. In order to keep the backing storage
3342 * coherent for all users, we only allow a single cache level to be set
3343 * globally on the object and prevent it from being changed whilst the
3344 * hardware is reading from the object. That is if the object is currently
3345 * on the scanout it will be set to uncached (or equivalent display
3346 * cache coherency) and all non-MOCS GPU access will also be uncached so
3347 * that all direct access to the scanout remains coherent.
3348 */
Chris Wilsone4ffd172011-04-04 09:44:39 +01003349int i915_gem_object_set_cache_level(struct drm_i915_gem_object *obj,
3350 enum i915_cache_level cache_level)
3351{
Chris Wilsonaa653a62016-08-04 07:52:27 +01003352 struct i915_vma *vma;
Chris Wilsona6a7cc42016-11-18 21:17:46 +00003353 int ret;
Chris Wilsone4ffd172011-04-04 09:44:39 +01003354
Chris Wilson4c7d62c2016-10-28 13:58:32 +01003355 lockdep_assert_held(&obj->base.dev->struct_mutex);
3356
Chris Wilsone4ffd172011-04-04 09:44:39 +01003357 if (obj->cache_level == cache_level)
Chris Wilsona6a7cc42016-11-18 21:17:46 +00003358 return 0;
Chris Wilsone4ffd172011-04-04 09:44:39 +01003359
Chris Wilsonef55f922015-10-09 14:11:27 +01003360 /* Inspect the list of currently bound VMA and unbind any that would
3361 * be invalid given the new cache-level. This is principally to
3362 * catch the issue of the CS prefetch crossing page boundaries and
3363 * reading an invalid PTE on older architectures.
3364 */
Chris Wilsonaa653a62016-08-04 07:52:27 +01003365restart:
Chris Wilson528cbd12019-01-28 10:23:54 +00003366 list_for_each_entry(vma, &obj->vma.list, obj_link) {
Chris Wilsonef55f922015-10-09 14:11:27 +01003367 if (!drm_mm_node_allocated(&vma->node))
3368 continue;
3369
Chris Wilson20dfbde2016-08-04 16:32:30 +01003370 if (i915_vma_is_pinned(vma)) {
Chris Wilsonef55f922015-10-09 14:11:27 +01003371 DRM_DEBUG("can not change the cache level of pinned objects\n");
3372 return -EBUSY;
3373 }
3374
Chris Wilson010e3e62017-12-06 12:49:13 +00003375 if (!i915_vma_is_closed(vma) &&
3376 i915_gem_valid_gtt_space(vma, cache_level))
Chris Wilsonaa653a62016-08-04 07:52:27 +01003377 continue;
3378
3379 ret = i915_vma_unbind(vma);
3380 if (ret)
3381 return ret;
3382
3383 /* As unbinding may affect other elements in the
3384 * obj->vma_list (due to side-effects from retiring
3385 * an active vma), play safe and restart the iterator.
3386 */
3387 goto restart;
Chris Wilson42d6ab42012-07-26 11:49:32 +01003388 }
3389
Chris Wilsonef55f922015-10-09 14:11:27 +01003390 /* We can reuse the existing drm_mm nodes but need to change the
3391 * cache-level on the PTE. We could simply unbind them all and
3392 * rebind with the correct cache-level on next use. However since
3393 * we already have a valid slot, dma mapping, pages etc, we may as
3394 * rewrite the PTE in the belief that doing so tramples upon less
3395 * state and so involves less work.
3396 */
Chris Wilson15717de2016-08-04 07:52:26 +01003397 if (obj->bind_count) {
Chris Wilsonef55f922015-10-09 14:11:27 +01003398 /* Before we change the PTE, the GPU must not be accessing it.
3399 * If we wait upon the object, we know that all the bound
3400 * VMA are no longer active.
3401 */
Chris Wilsone95433c2016-10-28 13:58:27 +01003402 ret = i915_gem_object_wait(obj,
3403 I915_WAIT_INTERRUPTIBLE |
3404 I915_WAIT_LOCKED |
3405 I915_WAIT_ALL,
Chris Wilson62eb3c22019-02-13 09:25:04 +00003406 MAX_SCHEDULE_TIMEOUT);
Chris Wilsone4ffd172011-04-04 09:44:39 +01003407 if (ret)
3408 return ret;
3409
Tvrtko Ursulin0031fb92016-11-04 14:42:44 +00003410 if (!HAS_LLC(to_i915(obj->base.dev)) &&
3411 cache_level != I915_CACHE_NONE) {
Chris Wilsonef55f922015-10-09 14:11:27 +01003412 /* Access to snoopable pages through the GTT is
3413 * incoherent and on some machines causes a hard
3414 * lockup. Relinquish the CPU mmaping to force
3415 * userspace to refault in the pages and we can
3416 * then double check if the GTT mapping is still
3417 * valid for that pointer access.
3418 */
3419 i915_gem_release_mmap(obj);
Chris Wilsone4ffd172011-04-04 09:44:39 +01003420
Chris Wilsonef55f922015-10-09 14:11:27 +01003421 /* As we no longer need a fence for GTT access,
3422 * we can relinquish it now (and so prevent having
3423 * to steal a fence from someone else on the next
3424 * fence request). Note GPU activity would have
3425 * dropped the fence as all snoopable access is
3426 * supposed to be linear.
3427 */
Chris Wilsone2189dd2017-12-07 21:14:07 +00003428 for_each_ggtt_vma(vma, obj) {
Chris Wilson49ef5292016-08-18 17:17:00 +01003429 ret = i915_vma_put_fence(vma);
3430 if (ret)
3431 return ret;
3432 }
Chris Wilsonef55f922015-10-09 14:11:27 +01003433 } else {
3434 /* We either have incoherent backing store and
3435 * so no GTT access or the architecture is fully
3436 * coherent. In such cases, existing GTT mmaps
3437 * ignore the cache bit in the PTE and we can
3438 * rewrite it without confusing the GPU or having
3439 * to force userspace to fault back in its mmaps.
3440 */
Chris Wilsone4ffd172011-04-04 09:44:39 +01003441 }
3442
Chris Wilson528cbd12019-01-28 10:23:54 +00003443 list_for_each_entry(vma, &obj->vma.list, obj_link) {
Chris Wilsonef55f922015-10-09 14:11:27 +01003444 if (!drm_mm_node_allocated(&vma->node))
3445 continue;
3446
3447 ret = i915_vma_bind(vma, cache_level, PIN_UPDATE);
3448 if (ret)
3449 return ret;
3450 }
Chris Wilsone4ffd172011-04-04 09:44:39 +01003451 }
3452
Chris Wilson528cbd12019-01-28 10:23:54 +00003453 list_for_each_entry(vma, &obj->vma.list, obj_link)
Chris Wilson2c225692013-08-09 12:26:45 +01003454 vma->node.color = cache_level;
Chris Wilsonb8f55be2017-08-11 12:11:16 +01003455 i915_gem_object_set_cache_coherency(obj, cache_level);
Chris Wilsone27ab732017-06-15 13:38:49 +01003456 obj->cache_dirty = true; /* Always invalidate stale cachelines */
Chris Wilson2c225692013-08-09 12:26:45 +01003457
Chris Wilsone4ffd172011-04-04 09:44:39 +01003458 return 0;
3459}
3460
Ben Widawsky199adf42012-09-21 17:01:20 -07003461int i915_gem_get_caching_ioctl(struct drm_device *dev, void *data,
3462 struct drm_file *file)
Chris Wilsone6994ae2012-07-10 10:27:08 +01003463{
Ben Widawsky199adf42012-09-21 17:01:20 -07003464 struct drm_i915_gem_caching *args = data;
Chris Wilsone6994ae2012-07-10 10:27:08 +01003465 struct drm_i915_gem_object *obj;
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01003466 int err = 0;
Chris Wilsone6994ae2012-07-10 10:27:08 +01003467
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01003468 rcu_read_lock();
3469 obj = i915_gem_object_lookup_rcu(file, args->handle);
3470 if (!obj) {
3471 err = -ENOENT;
3472 goto out;
3473 }
Chris Wilsone6994ae2012-07-10 10:27:08 +01003474
Chris Wilson651d7942013-08-08 14:41:10 +01003475 switch (obj->cache_level) {
3476 case I915_CACHE_LLC:
3477 case I915_CACHE_L3_LLC:
3478 args->caching = I915_CACHING_CACHED;
3479 break;
3480
Chris Wilson4257d3b2013-08-08 14:41:11 +01003481 case I915_CACHE_WT:
3482 args->caching = I915_CACHING_DISPLAY;
3483 break;
3484
Chris Wilson651d7942013-08-08 14:41:10 +01003485 default:
3486 args->caching = I915_CACHING_NONE;
3487 break;
3488 }
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01003489out:
3490 rcu_read_unlock();
3491 return err;
Chris Wilsone6994ae2012-07-10 10:27:08 +01003492}
3493
Ben Widawsky199adf42012-09-21 17:01:20 -07003494int i915_gem_set_caching_ioctl(struct drm_device *dev, void *data,
3495 struct drm_file *file)
Chris Wilsone6994ae2012-07-10 10:27:08 +01003496{
Chris Wilson9c870d02016-10-24 13:42:15 +01003497 struct drm_i915_private *i915 = to_i915(dev);
Ben Widawsky199adf42012-09-21 17:01:20 -07003498 struct drm_i915_gem_caching *args = data;
Chris Wilsone6994ae2012-07-10 10:27:08 +01003499 struct drm_i915_gem_object *obj;
3500 enum i915_cache_level level;
Chris Wilsond65415d2017-01-19 08:22:10 +00003501 int ret = 0;
Chris Wilsone6994ae2012-07-10 10:27:08 +01003502
Ben Widawsky199adf42012-09-21 17:01:20 -07003503 switch (args->caching) {
3504 case I915_CACHING_NONE:
Chris Wilsone6994ae2012-07-10 10:27:08 +01003505 level = I915_CACHE_NONE;
3506 break;
Ben Widawsky199adf42012-09-21 17:01:20 -07003507 case I915_CACHING_CACHED:
Imre Deake5756c12015-08-14 18:43:30 +03003508 /*
3509 * Due to a HW issue on BXT A stepping, GPU stores via a
3510 * snooped mapping may leave stale data in a corresponding CPU
3511 * cacheline, whereas normally such cachelines would get
3512 * invalidated.
3513 */
Chris Wilson9c870d02016-10-24 13:42:15 +01003514 if (!HAS_LLC(i915) && !HAS_SNOOP(i915))
Imre Deake5756c12015-08-14 18:43:30 +03003515 return -ENODEV;
3516
Chris Wilsone6994ae2012-07-10 10:27:08 +01003517 level = I915_CACHE_LLC;
3518 break;
Chris Wilson4257d3b2013-08-08 14:41:11 +01003519 case I915_CACHING_DISPLAY:
Chris Wilson9c870d02016-10-24 13:42:15 +01003520 level = HAS_WT(i915) ? I915_CACHE_WT : I915_CACHE_NONE;
Chris Wilson4257d3b2013-08-08 14:41:11 +01003521 break;
Chris Wilsone6994ae2012-07-10 10:27:08 +01003522 default:
3523 return -EINVAL;
3524 }
3525
Chris Wilsond65415d2017-01-19 08:22:10 +00003526 obj = i915_gem_object_lookup(file, args->handle);
3527 if (!obj)
3528 return -ENOENT;
3529
Tina Zhanga03f3952017-11-14 10:25:13 +00003530 /*
3531 * The caching mode of proxy object is handled by its generator, and
3532 * not allowed to be changed by userspace.
3533 */
3534 if (i915_gem_object_is_proxy(obj)) {
3535 ret = -ENXIO;
3536 goto out;
3537 }
3538
Chris Wilsond65415d2017-01-19 08:22:10 +00003539 if (obj->cache_level == level)
3540 goto out;
3541
3542 ret = i915_gem_object_wait(obj,
3543 I915_WAIT_INTERRUPTIBLE,
Chris Wilson62eb3c22019-02-13 09:25:04 +00003544 MAX_SCHEDULE_TIMEOUT);
Chris Wilsond65415d2017-01-19 08:22:10 +00003545 if (ret)
3546 goto out;
3547
Ben Widawsky3bc29132012-09-26 16:15:20 -07003548 ret = i915_mutex_lock_interruptible(dev);
3549 if (ret)
Chris Wilsond65415d2017-01-19 08:22:10 +00003550 goto out;
Chris Wilsone6994ae2012-07-10 10:27:08 +01003551
3552 ret = i915_gem_object_set_cache_level(obj, level);
Chris Wilsone6994ae2012-07-10 10:27:08 +01003553 mutex_unlock(&dev->struct_mutex);
Chris Wilsond65415d2017-01-19 08:22:10 +00003554
3555out:
3556 i915_gem_object_put(obj);
Chris Wilsone6994ae2012-07-10 10:27:08 +01003557 return ret;
3558}
3559
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003560/*
Dhinakaran Pandiyan07bcd992018-03-06 19:34:18 -08003561 * Prepare buffer for display plane (scanout, cursors, etc). Can be called from
3562 * an uninterruptible phase (modesetting) and allows any flushes to be pipelined
3563 * (for pageflips). We only flush the caches while preparing the buffer for
3564 * display, the callers are responsible for frontbuffer flush.
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003565 */
Chris Wilson058d88c2016-08-15 10:49:06 +01003566struct i915_vma *
Chris Wilson2da3b9b2011-04-14 09:41:17 +01003567i915_gem_object_pin_to_display_plane(struct drm_i915_gem_object *obj,
3568 u32 alignment,
Chris Wilson59354852018-02-20 13:42:06 +00003569 const struct i915_ggtt_view *view,
3570 unsigned int flags)
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003571{
Chris Wilson058d88c2016-08-15 10:49:06 +01003572 struct i915_vma *vma;
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003573 int ret;
3574
Chris Wilson4c7d62c2016-10-28 13:58:32 +01003575 lockdep_assert_held(&obj->base.dev->struct_mutex);
3576
Chris Wilsonbd3d2252017-10-13 21:26:14 +01003577 /* Mark the global pin early so that we account for the
Chris Wilsoncc98b412013-08-09 12:25:09 +01003578 * display coherency whilst setting up the cache domains.
3579 */
Chris Wilsonbd3d2252017-10-13 21:26:14 +01003580 obj->pin_global++;
Chris Wilsoncc98b412013-08-09 12:25:09 +01003581
Eric Anholta7ef0642011-03-29 16:59:54 -07003582 /* The display engine is not coherent with the LLC cache on gen6. As
3583 * a result, we make sure that the pinning that is about to occur is
3584 * done with uncached PTEs. This is lowest common denominator for all
3585 * chipsets.
3586 *
3587 * However for gen6+, we could do better by using the GFDT bit instead
3588 * of uncaching, which would allow us to flush all the LLC-cached data
3589 * with that bit in the PTE to main memory with just one PIPE_CONTROL.
3590 */
Chris Wilson651d7942013-08-08 14:41:10 +01003591 ret = i915_gem_object_set_cache_level(obj,
Tvrtko Ursulin86527442016-10-13 11:03:00 +01003592 HAS_WT(to_i915(obj->base.dev)) ?
3593 I915_CACHE_WT : I915_CACHE_NONE);
Chris Wilson058d88c2016-08-15 10:49:06 +01003594 if (ret) {
3595 vma = ERR_PTR(ret);
Chris Wilsonbd3d2252017-10-13 21:26:14 +01003596 goto err_unpin_global;
Chris Wilson058d88c2016-08-15 10:49:06 +01003597 }
Eric Anholta7ef0642011-03-29 16:59:54 -07003598
Chris Wilson2da3b9b2011-04-14 09:41:17 +01003599 /* As the user may map the buffer once pinned in the display plane
3600 * (e.g. libkms for the bootup splash), we have to ensure that we
Chris Wilson2efb8132016-08-18 17:17:06 +01003601 * always use map_and_fenceable for all scanout buffers. However,
3602 * it may simply be too big to fit into mappable, in which case
3603 * put it anyway and hope that userspace can cope (but always first
3604 * try to preserve the existing ABI).
Chris Wilson2da3b9b2011-04-14 09:41:17 +01003605 */
Chris Wilson2efb8132016-08-18 17:17:06 +01003606 vma = ERR_PTR(-ENOSPC);
Chris Wilson59354852018-02-20 13:42:06 +00003607 if ((flags & PIN_MAPPABLE) == 0 &&
3608 (!view || view->type == I915_GGTT_VIEW_NORMAL))
Chris Wilson2efb8132016-08-18 17:17:06 +01003609 vma = i915_gem_object_ggtt_pin(obj, view, 0, alignment,
Chris Wilson59354852018-02-20 13:42:06 +00003610 flags |
3611 PIN_MAPPABLE |
3612 PIN_NONBLOCK);
3613 if (IS_ERR(vma))
Chris Wilson767a2222016-11-07 11:01:28 +00003614 vma = i915_gem_object_ggtt_pin(obj, view, 0, alignment, flags);
Chris Wilson058d88c2016-08-15 10:49:06 +01003615 if (IS_ERR(vma))
Chris Wilsonbd3d2252017-10-13 21:26:14 +01003616 goto err_unpin_global;
Chris Wilson2da3b9b2011-04-14 09:41:17 +01003617
Chris Wilsond8923dc2016-08-18 17:17:07 +01003618 vma->display_alignment = max_t(u64, vma->display_alignment, alignment);
3619
Chris Wilson5a97bcc2017-02-22 11:40:46 +00003620 __i915_gem_object_flush_for_display(obj);
Chris Wilsonb118c1e2010-05-27 13:18:14 +01003621
Chris Wilson2da3b9b2011-04-14 09:41:17 +01003622 /* It should now be out of any other write domains, and we can update
3623 * the domain values for our changes.
3624 */
Christian Königc0a51fd2018-02-16 13:43:38 +01003625 obj->read_domains |= I915_GEM_DOMAIN_GTT;
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003626
Chris Wilson058d88c2016-08-15 10:49:06 +01003627 return vma;
Chris Wilsoncc98b412013-08-09 12:25:09 +01003628
Chris Wilsonbd3d2252017-10-13 21:26:14 +01003629err_unpin_global:
3630 obj->pin_global--;
Chris Wilson058d88c2016-08-15 10:49:06 +01003631 return vma;
Chris Wilsoncc98b412013-08-09 12:25:09 +01003632}
3633
3634void
Chris Wilson058d88c2016-08-15 10:49:06 +01003635i915_gem_object_unpin_from_display_plane(struct i915_vma *vma)
Chris Wilsoncc98b412013-08-09 12:25:09 +01003636{
Chris Wilson49d73912016-11-29 09:50:08 +00003637 lockdep_assert_held(&vma->vm->i915->drm.struct_mutex);
Chris Wilson4c7d62c2016-10-28 13:58:32 +01003638
Chris Wilsonbd3d2252017-10-13 21:26:14 +01003639 if (WARN_ON(vma->obj->pin_global == 0))
Tvrtko Ursulin8a0c39b2015-04-13 11:50:09 +01003640 return;
3641
Chris Wilsonbd3d2252017-10-13 21:26:14 +01003642 if (--vma->obj->pin_global == 0)
Chris Wilsonf51455d2017-01-10 14:47:34 +00003643 vma->display_alignment = I915_GTT_MIN_ALIGNMENT;
Tvrtko Ursuline6617332015-03-23 11:10:33 +00003644
Chris Wilson383d5822016-08-18 17:17:08 +01003645 /* Bump the LRU to try and avoid premature eviction whilst flipping */
Chris Wilsonbefedbb2017-01-19 19:26:55 +00003646 i915_gem_object_bump_inactive_ggtt(vma->obj);
Chris Wilson383d5822016-08-18 17:17:08 +01003647
Chris Wilson058d88c2016-08-15 10:49:06 +01003648 i915_vma_unpin(vma);
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003649}
3650
Eric Anholte47c68e2008-11-14 13:35:19 -08003651/**
3652 * Moves a single object to the CPU read, and possibly write domain.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01003653 * @obj: object to act on
3654 * @write: requesting write or read-only access
Eric Anholte47c68e2008-11-14 13:35:19 -08003655 *
3656 * This function returns when the move is complete, including waiting on
3657 * flushes to occur.
3658 */
Chris Wilsondabdfe02012-03-26 10:10:27 +02003659int
Chris Wilson919926a2010-11-12 13:42:53 +00003660i915_gem_object_set_to_cpu_domain(struct drm_i915_gem_object *obj, bool write)
Eric Anholte47c68e2008-11-14 13:35:19 -08003661{
Eric Anholte47c68e2008-11-14 13:35:19 -08003662 int ret;
3663
Chris Wilsone95433c2016-10-28 13:58:27 +01003664 lockdep_assert_held(&obj->base.dev->struct_mutex);
Chris Wilson4c7d62c2016-10-28 13:58:32 +01003665
Chris Wilsone95433c2016-10-28 13:58:27 +01003666 ret = i915_gem_object_wait(obj,
3667 I915_WAIT_INTERRUPTIBLE |
3668 I915_WAIT_LOCKED |
3669 (write ? I915_WAIT_ALL : 0),
Chris Wilson62eb3c22019-02-13 09:25:04 +00003670 MAX_SCHEDULE_TIMEOUT);
Chris Wilson88241782011-01-07 17:09:48 +00003671 if (ret)
3672 return ret;
3673
Chris Wilsonef749212017-04-12 12:01:10 +01003674 flush_write_domain(obj, ~I915_GEM_DOMAIN_CPU);
Eric Anholte47c68e2008-11-14 13:35:19 -08003675
Eric Anholte47c68e2008-11-14 13:35:19 -08003676 /* Flush the CPU cache if it's still invalid. */
Christian Königc0a51fd2018-02-16 13:43:38 +01003677 if ((obj->read_domains & I915_GEM_DOMAIN_CPU) == 0) {
Chris Wilson57822dc2017-02-22 11:40:48 +00003678 i915_gem_clflush_object(obj, I915_CLFLUSH_SYNC);
Christian Königc0a51fd2018-02-16 13:43:38 +01003679 obj->read_domains |= I915_GEM_DOMAIN_CPU;
Eric Anholte47c68e2008-11-14 13:35:19 -08003680 }
3681
3682 /* It should now be out of any other write domains, and we can update
3683 * the domain values for our changes.
3684 */
Christian Königc0a51fd2018-02-16 13:43:38 +01003685 GEM_BUG_ON(obj->write_domain & ~I915_GEM_DOMAIN_CPU);
Eric Anholte47c68e2008-11-14 13:35:19 -08003686
3687 /* If we're writing through the CPU, then the GPU read domains will
3688 * need to be invalidated at next use.
3689 */
Chris Wilsone27ab732017-06-15 13:38:49 +01003690 if (write)
3691 __start_cpu_write(obj);
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003692
3693 return 0;
3694}
3695
Eric Anholt673a3942008-07-30 12:06:12 -07003696/* Throttle our rendering by waiting until the ring has completed our requests
3697 * emitted over 20 msec ago.
3698 *
Eric Anholtb9624422009-06-03 07:27:35 +00003699 * Note that if we were to use the current jiffies each time around the loop,
3700 * we wouldn't escape the function with any frames outstanding if the time to
3701 * render a frame was over 20ms.
3702 *
Eric Anholt673a3942008-07-30 12:06:12 -07003703 * This should get us reasonable parallelism between CPU and GPU but also
3704 * relatively low latency when blocking on a particular request to finish.
3705 */
3706static int
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003707i915_gem_ring_throttle(struct drm_device *dev, struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07003708{
Chris Wilsonfac5e232016-07-04 11:34:36 +01003709 struct drm_i915_private *dev_priv = to_i915(dev);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003710 struct drm_i915_file_private *file_priv = file->driver_priv;
Chris Wilsond0bc54f2015-05-21 21:01:48 +01003711 unsigned long recent_enough = jiffies - DRM_I915_THROTTLE_JIFFIES;
Chris Wilsone61e0f52018-02-21 09:56:36 +00003712 struct i915_request *request, *target = NULL;
Chris Wilsone95433c2016-10-28 13:58:27 +01003713 long ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003714
Chris Wilsonf4457ae2016-04-13 17:35:08 +01003715 /* ABI: return -EIO if already wedged */
Chris Wilsonc41166f2019-02-20 14:56:37 +00003716 ret = i915_terminally_wedged(dev_priv);
3717 if (ret)
3718 return ret;
Chris Wilsone110e8d2011-01-26 15:39:14 +00003719
Chris Wilson1c255952010-09-26 11:03:27 +01003720 spin_lock(&file_priv->mm.lock);
Chris Wilsonc8659ef2017-03-02 12:25:25 +00003721 list_for_each_entry(request, &file_priv->mm.request_list, client_link) {
Eric Anholtb9624422009-06-03 07:27:35 +00003722 if (time_after_eq(request->emitted_jiffies, recent_enough))
3723 break;
3724
Chris Wilsonc8659ef2017-03-02 12:25:25 +00003725 if (target) {
3726 list_del(&target->client_link);
3727 target->file_priv = NULL;
3728 }
John Harrisonfcfa423c2015-05-29 17:44:12 +01003729
John Harrison54fb2412014-11-24 18:49:27 +00003730 target = request;
Eric Anholtb9624422009-06-03 07:27:35 +00003731 }
John Harrisonff865882014-11-24 18:49:28 +00003732 if (target)
Chris Wilsone61e0f52018-02-21 09:56:36 +00003733 i915_request_get(target);
Chris Wilson1c255952010-09-26 11:03:27 +01003734 spin_unlock(&file_priv->mm.lock);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003735
John Harrison54fb2412014-11-24 18:49:27 +00003736 if (target == NULL)
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003737 return 0;
3738
Chris Wilsone61e0f52018-02-21 09:56:36 +00003739 ret = i915_request_wait(target,
Chris Wilsone95433c2016-10-28 13:58:27 +01003740 I915_WAIT_INTERRUPTIBLE,
3741 MAX_SCHEDULE_TIMEOUT);
Chris Wilsone61e0f52018-02-21 09:56:36 +00003742 i915_request_put(target);
John Harrisonff865882014-11-24 18:49:28 +00003743
Chris Wilsone95433c2016-10-28 13:58:27 +01003744 return ret < 0 ? ret : 0;
Eric Anholt673a3942008-07-30 12:06:12 -07003745}
3746
Chris Wilson058d88c2016-08-15 10:49:06 +01003747struct i915_vma *
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02003748i915_gem_object_ggtt_pin(struct drm_i915_gem_object *obj,
3749 const struct i915_ggtt_view *view,
Chris Wilson91b2db62016-08-04 16:32:23 +01003750 u64 size,
Chris Wilson2ffffd02016-08-04 16:32:22 +01003751 u64 alignment,
3752 u64 flags)
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02003753{
Chris Wilsonad16d2e2016-10-13 09:55:04 +01003754 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
Chris Wilson82ad6442018-06-05 16:37:58 +01003755 struct i915_address_space *vm = &dev_priv->ggtt.vm;
Chris Wilson59bfa122016-08-04 16:32:31 +01003756 struct i915_vma *vma;
3757 int ret;
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03003758
Chris Wilson4c7d62c2016-10-28 13:58:32 +01003759 lockdep_assert_held(&obj->base.dev->struct_mutex);
3760
Chris Wilsonac87a6fd2018-02-20 13:42:05 +00003761 if (flags & PIN_MAPPABLE &&
3762 (!view || view->type == I915_GGTT_VIEW_NORMAL)) {
Chris Wilson43ae70d92017-10-09 09:44:01 +01003763 /* If the required space is larger than the available
3764 * aperture, we will not able to find a slot for the
3765 * object and unbinding the object now will be in
3766 * vain. Worse, doing so may cause us to ping-pong
3767 * the object in and out of the Global GTT and
3768 * waste a lot of cycles under the mutex.
3769 */
3770 if (obj->base.size > dev_priv->ggtt.mappable_end)
3771 return ERR_PTR(-E2BIG);
3772
3773 /* If NONBLOCK is set the caller is optimistically
3774 * trying to cache the full object within the mappable
3775 * aperture, and *must* have a fallback in place for
3776 * situations where we cannot bind the object. We
3777 * can be a little more lax here and use the fallback
3778 * more often to avoid costly migrations of ourselves
3779 * and other objects within the aperture.
3780 *
3781 * Half-the-aperture is used as a simple heuristic.
3782 * More interesting would to do search for a free
3783 * block prior to making the commitment to unbind.
3784 * That caters for the self-harm case, and with a
3785 * little more heuristics (e.g. NOFAULT, NOEVICT)
3786 * we could try to minimise harm to others.
3787 */
3788 if (flags & PIN_NONBLOCK &&
3789 obj->base.size > dev_priv->ggtt.mappable_end / 2)
3790 return ERR_PTR(-ENOSPC);
3791 }
3792
Chris Wilson718659a2017-01-16 15:21:28 +00003793 vma = i915_vma_instance(obj, vm, view);
Chengguang Xu772b5402019-02-21 10:08:19 +08003794 if (IS_ERR(vma))
Chris Wilson058d88c2016-08-15 10:49:06 +01003795 return vma;
Chris Wilson59bfa122016-08-04 16:32:31 +01003796
3797 if (i915_vma_misplaced(vma, size, alignment, flags)) {
Chris Wilson43ae70d92017-10-09 09:44:01 +01003798 if (flags & PIN_NONBLOCK) {
3799 if (i915_vma_is_pinned(vma) || i915_vma_is_active(vma))
3800 return ERR_PTR(-ENOSPC);
Chris Wilson59bfa122016-08-04 16:32:31 +01003801
Chris Wilson43ae70d92017-10-09 09:44:01 +01003802 if (flags & PIN_MAPPABLE &&
Chris Wilson944397f2017-01-09 16:16:11 +00003803 vma->fence_size > dev_priv->ggtt.mappable_end / 2)
Chris Wilsonad16d2e2016-10-13 09:55:04 +01003804 return ERR_PTR(-ENOSPC);
3805 }
3806
Chris Wilson59bfa122016-08-04 16:32:31 +01003807 WARN(i915_vma_is_pinned(vma),
3808 "bo is already pinned in ggtt with incorrect alignment:"
Chris Wilson05a20d02016-08-18 17:16:55 +01003809 " offset=%08x, req.alignment=%llx,"
3810 " req.map_and_fenceable=%d, vma->map_and_fenceable=%d\n",
3811 i915_ggtt_offset(vma), alignment,
Chris Wilson59bfa122016-08-04 16:32:31 +01003812 !!(flags & PIN_MAPPABLE),
Chris Wilson05a20d02016-08-18 17:16:55 +01003813 i915_vma_is_map_and_fenceable(vma));
Chris Wilson59bfa122016-08-04 16:32:31 +01003814 ret = i915_vma_unbind(vma);
3815 if (ret)
Chris Wilson058d88c2016-08-15 10:49:06 +01003816 return ERR_PTR(ret);
Chris Wilson59bfa122016-08-04 16:32:31 +01003817 }
3818
Chris Wilson058d88c2016-08-15 10:49:06 +01003819 ret = i915_vma_pin(vma, size, alignment, flags | PIN_GLOBAL);
3820 if (ret)
3821 return ERR_PTR(ret);
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02003822
Chris Wilson058d88c2016-08-15 10:49:06 +01003823 return vma;
Eric Anholt673a3942008-07-30 12:06:12 -07003824}
3825
Chris Wilsonedf6b762016-08-09 09:23:33 +01003826static __always_inline unsigned int __busy_read_flag(unsigned int id)
Chris Wilson3fdc13c2016-08-05 10:14:18 +01003827{
Chris Wilsonc8b50242019-03-05 16:26:43 +00003828 if (id == I915_ENGINE_CLASS_INVALID)
3829 return 0xffff0000;
3830
3831 GEM_BUG_ON(id >= 16);
Chris Wilson3fdc13c2016-08-05 10:14:18 +01003832 return 0x10000 << id;
3833}
3834
3835static __always_inline unsigned int __busy_write_id(unsigned int id)
3836{
Chris Wilsonc8b50242019-03-05 16:26:43 +00003837 /*
3838 * The uABI guarantees an active writer is also amongst the read
Chris Wilson70cb4722016-08-09 18:08:25 +01003839 * engines. This would be true if we accessed the activity tracking
3840 * under the lock, but as we perform the lookup of the object and
3841 * its activity locklessly we can not guarantee that the last_write
3842 * being active implies that we have set the same engine flag from
3843 * last_read - hence we always set both read and write busy for
3844 * last_write.
3845 */
Chris Wilsonc8b50242019-03-05 16:26:43 +00003846 if (id == I915_ENGINE_CLASS_INVALID)
3847 return 0xffffffff;
3848
3849 return (id + 1) | __busy_read_flag(id);
Chris Wilson3fdc13c2016-08-05 10:14:18 +01003850}
3851
Chris Wilsonedf6b762016-08-09 09:23:33 +01003852static __always_inline unsigned int
Chris Wilsond07f0e52016-10-28 13:58:44 +01003853__busy_set_if_active(const struct dma_fence *fence,
Chris Wilson3fdc13c2016-08-05 10:14:18 +01003854 unsigned int (*flag)(unsigned int id))
3855{
Chris Wilsonc8b50242019-03-05 16:26:43 +00003856 const struct i915_request *rq;
Chris Wilson12555012016-08-16 09:50:40 +01003857
Chris Wilsonc8b50242019-03-05 16:26:43 +00003858 /*
3859 * We have to check the current hw status of the fence as the uABI
Chris Wilsond07f0e52016-10-28 13:58:44 +01003860 * guarantees forward progress. We could rely on the idle worker
3861 * to eventually flush us, but to minimise latency just ask the
3862 * hardware.
3863 *
3864 * Note we only report on the status of native fences.
3865 */
3866 if (!dma_fence_is_i915(fence))
Chris Wilson12555012016-08-16 09:50:40 +01003867 return 0;
3868
Chris Wilsond07f0e52016-10-28 13:58:44 +01003869 /* opencode to_request() in order to avoid const warnings */
Chris Wilsonc8b50242019-03-05 16:26:43 +00003870 rq = container_of(fence, const struct i915_request, fence);
Chris Wilsone61e0f52018-02-21 09:56:36 +00003871 if (i915_request_completed(rq))
Chris Wilsond07f0e52016-10-28 13:58:44 +01003872 return 0;
3873
Chris Wilsonc8b50242019-03-05 16:26:43 +00003874 return flag(rq->engine->uabi_class);
Chris Wilson3fdc13c2016-08-05 10:14:18 +01003875}
3876
Chris Wilsonedf6b762016-08-09 09:23:33 +01003877static __always_inline unsigned int
Chris Wilsond07f0e52016-10-28 13:58:44 +01003878busy_check_reader(const struct dma_fence *fence)
Chris Wilson3fdc13c2016-08-05 10:14:18 +01003879{
Chris Wilsond07f0e52016-10-28 13:58:44 +01003880 return __busy_set_if_active(fence, __busy_read_flag);
Chris Wilson3fdc13c2016-08-05 10:14:18 +01003881}
3882
Chris Wilsonedf6b762016-08-09 09:23:33 +01003883static __always_inline unsigned int
Chris Wilsond07f0e52016-10-28 13:58:44 +01003884busy_check_writer(const struct dma_fence *fence)
Chris Wilson3fdc13c2016-08-05 10:14:18 +01003885{
Chris Wilsond07f0e52016-10-28 13:58:44 +01003886 if (!fence)
3887 return 0;
3888
3889 return __busy_set_if_active(fence, __busy_write_id);
Chris Wilson3fdc13c2016-08-05 10:14:18 +01003890}
3891
Eric Anholt673a3942008-07-30 12:06:12 -07003892int
Eric Anholt673a3942008-07-30 12:06:12 -07003893i915_gem_busy_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00003894 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07003895{
3896 struct drm_i915_gem_busy *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00003897 struct drm_i915_gem_object *obj;
Chris Wilsond07f0e52016-10-28 13:58:44 +01003898 struct reservation_object_list *list;
3899 unsigned int seq;
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01003900 int err;
Eric Anholt673a3942008-07-30 12:06:12 -07003901
Chris Wilsond07f0e52016-10-28 13:58:44 +01003902 err = -ENOENT;
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01003903 rcu_read_lock();
3904 obj = i915_gem_object_lookup_rcu(file, args->handle);
Chris Wilsond07f0e52016-10-28 13:58:44 +01003905 if (!obj)
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01003906 goto out;
Chris Wilsond07f0e52016-10-28 13:58:44 +01003907
Chris Wilsonc8b50242019-03-05 16:26:43 +00003908 /*
3909 * A discrepancy here is that we do not report the status of
Chris Wilsond07f0e52016-10-28 13:58:44 +01003910 * non-i915 fences, i.e. even though we may report the object as idle,
3911 * a call to set-domain may still stall waiting for foreign rendering.
3912 * This also means that wait-ioctl may report an object as busy,
3913 * where busy-ioctl considers it idle.
3914 *
3915 * We trade the ability to warn of foreign fences to report on which
3916 * i915 engines are active for the object.
3917 *
3918 * Alternatively, we can trade that extra information on read/write
3919 * activity with
3920 * args->busy =
3921 * !reservation_object_test_signaled_rcu(obj->resv, true);
3922 * to report the overall busyness. This is what the wait-ioctl does.
3923 *
3924 */
3925retry:
3926 seq = raw_read_seqcount(&obj->resv->seq);
3927
3928 /* Translate the exclusive fence to the READ *and* WRITE engine */
3929 args->busy = busy_check_writer(rcu_dereference(obj->resv->fence_excl));
3930
3931 /* Translate shared fences to READ set of engines */
3932 list = rcu_dereference(obj->resv->fence);
3933 if (list) {
3934 unsigned int shared_count = list->shared_count, i;
3935
3936 for (i = 0; i < shared_count; ++i) {
3937 struct dma_fence *fence =
3938 rcu_dereference(list->shared[i]);
3939
3940 args->busy |= busy_check_reader(fence);
3941 }
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01003942 }
Zou Nan haid1b851f2010-05-21 09:08:57 +08003943
Chris Wilsond07f0e52016-10-28 13:58:44 +01003944 if (args->busy && read_seqcount_retry(&obj->resv->seq, seq))
3945 goto retry;
Chris Wilson426960b2016-01-15 16:51:46 +00003946
Chris Wilsond07f0e52016-10-28 13:58:44 +01003947 err = 0;
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01003948out:
3949 rcu_read_unlock();
3950 return err;
Eric Anholt673a3942008-07-30 12:06:12 -07003951}
3952
3953int
3954i915_gem_throttle_ioctl(struct drm_device *dev, void *data,
3955 struct drm_file *file_priv)
3956{
Akshay Joshi0206e352011-08-16 15:34:10 -04003957 return i915_gem_ring_throttle(dev, file_priv);
Eric Anholt673a3942008-07-30 12:06:12 -07003958}
3959
Chris Wilson3ef94da2009-09-14 16:50:29 +01003960int
3961i915_gem_madvise_ioctl(struct drm_device *dev, void *data,
3962 struct drm_file *file_priv)
3963{
Chris Wilsonfac5e232016-07-04 11:34:36 +01003964 struct drm_i915_private *dev_priv = to_i915(dev);
Chris Wilson3ef94da2009-09-14 16:50:29 +01003965 struct drm_i915_gem_madvise *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00003966 struct drm_i915_gem_object *obj;
Chris Wilson1233e2d2016-10-28 13:58:37 +01003967 int err;
Chris Wilson3ef94da2009-09-14 16:50:29 +01003968
3969 switch (args->madv) {
3970 case I915_MADV_DONTNEED:
3971 case I915_MADV_WILLNEED:
3972 break;
3973 default:
3974 return -EINVAL;
3975 }
3976
Chris Wilson03ac0642016-07-20 13:31:51 +01003977 obj = i915_gem_object_lookup(file_priv, args->handle);
Chris Wilson1233e2d2016-10-28 13:58:37 +01003978 if (!obj)
3979 return -ENOENT;
3980
3981 err = mutex_lock_interruptible(&obj->mm.lock);
3982 if (err)
3983 goto out;
Chris Wilson3ef94da2009-09-14 16:50:29 +01003984
Chris Wilsonf1fa4f42017-10-13 21:26:13 +01003985 if (i915_gem_object_has_pages(obj) &&
Chris Wilson3e510a82016-08-05 10:14:23 +01003986 i915_gem_object_is_tiled(obj) &&
Daniel Vetter656bfa32014-11-20 09:26:30 +01003987 dev_priv->quirks & QUIRK_PIN_SWIZZLED_PAGES) {
Chris Wilsonbc0629a2016-11-01 10:03:17 +00003988 if (obj->mm.madv == I915_MADV_WILLNEED) {
3989 GEM_BUG_ON(!obj->mm.quirked);
Chris Wilsona4f5ea62016-10-28 13:58:35 +01003990 __i915_gem_object_unpin_pages(obj);
Chris Wilsonbc0629a2016-11-01 10:03:17 +00003991 obj->mm.quirked = false;
3992 }
3993 if (args->madv == I915_MADV_WILLNEED) {
Chris Wilson2c3a3f42016-11-04 10:30:01 +00003994 GEM_BUG_ON(obj->mm.quirked);
Chris Wilsona4f5ea62016-10-28 13:58:35 +01003995 __i915_gem_object_pin_pages(obj);
Chris Wilsonbc0629a2016-11-01 10:03:17 +00003996 obj->mm.quirked = true;
3997 }
Daniel Vetter656bfa32014-11-20 09:26:30 +01003998 }
3999
Chris Wilsona4f5ea62016-10-28 13:58:35 +01004000 if (obj->mm.madv != __I915_MADV_PURGED)
4001 obj->mm.madv = args->madv;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004002
Chris Wilson6c085a72012-08-20 11:40:46 +02004003 /* if the object is no longer attached, discard its backing storage */
Chris Wilsonf1fa4f42017-10-13 21:26:13 +01004004 if (obj->mm.madv == I915_MADV_DONTNEED &&
4005 !i915_gem_object_has_pages(obj))
Chris Wilson2d7ef392009-09-20 23:13:10 +01004006 i915_gem_object_truncate(obj);
4007
Chris Wilsona4f5ea62016-10-28 13:58:35 +01004008 args->retained = obj->mm.madv != __I915_MADV_PURGED;
Chris Wilson1233e2d2016-10-28 13:58:37 +01004009 mutex_unlock(&obj->mm.lock);
Chris Wilsonbb6baf72009-09-22 14:24:13 +01004010
Chris Wilson1233e2d2016-10-28 13:58:37 +01004011out:
Chris Wilsonf8c417c2016-07-20 13:31:53 +01004012 i915_gem_object_put(obj);
Chris Wilson1233e2d2016-10-28 13:58:37 +01004013 return err;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004014}
4015
Chris Wilson5b8c8ae2016-11-16 19:07:04 +00004016static void
Chris Wilson21950ee2019-02-05 13:00:05 +00004017frontbuffer_retire(struct i915_active_request *active,
4018 struct i915_request *request)
Chris Wilson5b8c8ae2016-11-16 19:07:04 +00004019{
4020 struct drm_i915_gem_object *obj =
4021 container_of(active, typeof(*obj), frontbuffer_write);
4022
Chris Wilsond59b21e2017-02-22 11:40:49 +00004023 intel_fb_obj_flush(obj, ORIGIN_CS);
Chris Wilson5b8c8ae2016-11-16 19:07:04 +00004024}
4025
Chris Wilson37e680a2012-06-07 15:38:42 +01004026void i915_gem_object_init(struct drm_i915_gem_object *obj,
4027 const struct drm_i915_gem_object_ops *ops)
Chris Wilson0327d6b2012-08-11 15:41:06 +01004028{
Chris Wilson1233e2d2016-10-28 13:58:37 +01004029 mutex_init(&obj->mm.lock);
4030
Chris Wilson528cbd12019-01-28 10:23:54 +00004031 spin_lock_init(&obj->vma.lock);
4032 INIT_LIST_HEAD(&obj->vma.list);
4033
Chris Wilsond1b48c12017-08-16 09:52:08 +01004034 INIT_LIST_HEAD(&obj->lut_list);
Chris Wilson8d9d5742015-04-07 16:20:38 +01004035 INIT_LIST_HEAD(&obj->batch_pool_link);
Chris Wilson0327d6b2012-08-11 15:41:06 +01004036
Chris Wilson8811d612018-11-09 09:03:11 +00004037 init_rcu_head(&obj->rcu);
4038
Chris Wilson37e680a2012-06-07 15:38:42 +01004039 obj->ops = ops;
4040
Chris Wilsond07f0e52016-10-28 13:58:44 +01004041 reservation_object_init(&obj->__builtin_resv);
4042 obj->resv = &obj->__builtin_resv;
4043
Chris Wilson50349242016-08-18 17:17:04 +01004044 obj->frontbuffer_ggtt_origin = ORIGIN_GTT;
Chris Wilson21950ee2019-02-05 13:00:05 +00004045 i915_active_request_init(&obj->frontbuffer_write,
4046 NULL, frontbuffer_retire);
Chris Wilsona4f5ea62016-10-28 13:58:35 +01004047
4048 obj->mm.madv = I915_MADV_WILLNEED;
4049 INIT_RADIX_TREE(&obj->mm.get_page.radix, GFP_KERNEL | __GFP_NOWARN);
4050 mutex_init(&obj->mm.get_page.lock);
Chris Wilson0327d6b2012-08-11 15:41:06 +01004051
Dave Gordonf19ec8c2016-07-04 11:34:37 +01004052 i915_gem_info_add_obj(to_i915(obj->base.dev), obj->base.size);
Chris Wilson0327d6b2012-08-11 15:41:06 +01004053}
4054
Chris Wilson37e680a2012-06-07 15:38:42 +01004055static const struct drm_i915_gem_object_ops i915_gem_object_ops = {
Tvrtko Ursulin3599a912016-11-01 14:44:10 +00004056 .flags = I915_GEM_OBJECT_HAS_STRUCT_PAGE |
4057 I915_GEM_OBJECT_IS_SHRINKABLE,
Chris Wilson7c55e2c2017-03-07 12:03:38 +00004058
Chris Wilson37e680a2012-06-07 15:38:42 +01004059 .get_pages = i915_gem_object_get_pages_gtt,
4060 .put_pages = i915_gem_object_put_pages_gtt,
Chris Wilson7c55e2c2017-03-07 12:03:38 +00004061
4062 .pwrite = i915_gem_object_pwrite_gtt,
Chris Wilson37e680a2012-06-07 15:38:42 +01004063};
4064
Matthew Auld465c4032017-10-06 23:18:14 +01004065static int i915_gem_object_create_shmem(struct drm_device *dev,
4066 struct drm_gem_object *obj,
4067 size_t size)
4068{
4069 struct drm_i915_private *i915 = to_i915(dev);
4070 unsigned long flags = VM_NORESERVE;
4071 struct file *filp;
4072
4073 drm_gem_private_object_init(dev, obj, size);
4074
4075 if (i915->mm.gemfs)
4076 filp = shmem_file_setup_with_mnt(i915->mm.gemfs, "i915", size,
4077 flags);
4078 else
4079 filp = shmem_file_setup("i915", size, flags);
4080
4081 if (IS_ERR(filp))
4082 return PTR_ERR(filp);
4083
4084 obj->filp = filp;
4085
4086 return 0;
4087}
4088
Chris Wilsonb4bcbe22016-10-18 13:02:49 +01004089struct drm_i915_gem_object *
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +00004090i915_gem_object_create(struct drm_i915_private *dev_priv, u64 size)
Daniel Vetterac52bc52010-04-09 19:05:06 +00004091{
Daniel Vetterc397b902010-04-09 19:05:07 +00004092 struct drm_i915_gem_object *obj;
Hugh Dickins5949eac2011-06-27 16:18:18 -07004093 struct address_space *mapping;
Chris Wilsonb8f55be2017-08-11 12:11:16 +01004094 unsigned int cache_level;
Daniel Vetter1a240d42012-11-29 22:18:51 +01004095 gfp_t mask;
Chris Wilsonfe3db792016-04-25 13:32:13 +01004096 int ret;
Daniel Vetterc397b902010-04-09 19:05:07 +00004097
Chris Wilsonb4bcbe22016-10-18 13:02:49 +01004098 /* There is a prevalence of the assumption that we fit the object's
4099 * page count inside a 32bit _signed_ variable. Let's document this and
4100 * catch if we ever need to fix it. In the meantime, if you do spot
4101 * such a local variable, please consider fixing!
4102 */
Tvrtko Ursulin7a3ee5d2017-03-30 17:31:30 +01004103 if (size >> PAGE_SHIFT > INT_MAX)
Chris Wilsonb4bcbe22016-10-18 13:02:49 +01004104 return ERR_PTR(-E2BIG);
4105
4106 if (overflows_type(size, obj->base.size))
4107 return ERR_PTR(-E2BIG);
4108
Chris Wilson13f1bfd2019-02-28 10:20:34 +00004109 obj = i915_gem_object_alloc();
Daniel Vetterc397b902010-04-09 19:05:07 +00004110 if (obj == NULL)
Chris Wilsonfe3db792016-04-25 13:32:13 +01004111 return ERR_PTR(-ENOMEM);
Daniel Vetterc397b902010-04-09 19:05:07 +00004112
Matthew Auld465c4032017-10-06 23:18:14 +01004113 ret = i915_gem_object_create_shmem(&dev_priv->drm, &obj->base, size);
Chris Wilsonfe3db792016-04-25 13:32:13 +01004114 if (ret)
4115 goto fail;
Daniel Vetterc397b902010-04-09 19:05:07 +00004116
Chris Wilsonbed1ea92012-05-24 20:48:12 +01004117 mask = GFP_HIGHUSER | __GFP_RECLAIMABLE;
Jani Nikulac0f86832016-12-07 12:13:04 +02004118 if (IS_I965GM(dev_priv) || IS_I965G(dev_priv)) {
Chris Wilsonbed1ea92012-05-24 20:48:12 +01004119 /* 965gm cannot relocate objects above 4GiB. */
4120 mask &= ~__GFP_HIGHMEM;
4121 mask |= __GFP_DMA32;
4122 }
4123
Al Viro93c76a32015-12-04 23:45:44 -05004124 mapping = obj->base.filp->f_mapping;
Chris Wilsonbed1ea92012-05-24 20:48:12 +01004125 mapping_set_gfp_mask(mapping, mask);
Chris Wilson4846bf02017-06-09 12:03:46 +01004126 GEM_BUG_ON(!(mapping_gfp_mask(mapping) & __GFP_RECLAIM));
Hugh Dickins5949eac2011-06-27 16:18:18 -07004127
Chris Wilson37e680a2012-06-07 15:38:42 +01004128 i915_gem_object_init(obj, &i915_gem_object_ops);
Chris Wilson73aa8082010-09-30 11:46:12 +01004129
Christian Königc0a51fd2018-02-16 13:43:38 +01004130 obj->write_domain = I915_GEM_DOMAIN_CPU;
4131 obj->read_domains = I915_GEM_DOMAIN_CPU;
Daniel Vetterc397b902010-04-09 19:05:07 +00004132
Chris Wilsonb8f55be2017-08-11 12:11:16 +01004133 if (HAS_LLC(dev_priv))
Eugeni Dodonov3d29b842012-01-17 14:43:53 -02004134 /* On some devices, we can have the GPU use the LLC (the CPU
Eric Anholta1871112011-03-29 16:59:55 -07004135 * cache) for about a 10% performance improvement
4136 * compared to uncached. Graphics requests other than
4137 * display scanout are coherent with the CPU in
4138 * accessing this cache. This means in this mode we
4139 * don't need to clflush on the CPU side, and on the
4140 * GPU side we only need to flush internal caches to
4141 * get data visible to the CPU.
4142 *
4143 * However, we maintain the display planes as UC, and so
4144 * need to rebind when first used as such.
4145 */
Chris Wilsonb8f55be2017-08-11 12:11:16 +01004146 cache_level = I915_CACHE_LLC;
4147 else
4148 cache_level = I915_CACHE_NONE;
Eric Anholta1871112011-03-29 16:59:55 -07004149
Chris Wilsonb8f55be2017-08-11 12:11:16 +01004150 i915_gem_object_set_cache_coherency(obj, cache_level);
Chris Wilsone27ab732017-06-15 13:38:49 +01004151
Daniel Vetterd861e332013-07-24 23:25:03 +02004152 trace_i915_gem_object_create(obj);
4153
Chris Wilson05394f32010-11-08 19:18:58 +00004154 return obj;
Chris Wilsonfe3db792016-04-25 13:32:13 +01004155
4156fail:
4157 i915_gem_object_free(obj);
Chris Wilsonfe3db792016-04-25 13:32:13 +01004158 return ERR_PTR(ret);
Daniel Vetterac52bc52010-04-09 19:05:06 +00004159}
4160
Chris Wilson340fbd82014-05-22 09:16:52 +01004161static bool discard_backing_storage(struct drm_i915_gem_object *obj)
4162{
4163 /* If we are the last user of the backing storage (be it shmemfs
4164 * pages or stolen etc), we know that the pages are going to be
4165 * immediately released. In this case, we can then skip copying
4166 * back the contents from the GPU.
4167 */
4168
Chris Wilsona4f5ea62016-10-28 13:58:35 +01004169 if (obj->mm.madv != I915_MADV_WILLNEED)
Chris Wilson340fbd82014-05-22 09:16:52 +01004170 return false;
4171
4172 if (obj->base.filp == NULL)
4173 return true;
4174
4175 /* At first glance, this looks racy, but then again so would be
4176 * userspace racing mmap against close. However, the first external
4177 * reference to the filp can only be obtained through the
4178 * i915_gem_mmap_ioctl() which safeguards us against the user
4179 * acquiring such a reference whilst we are in the middle of
4180 * freeing the object.
4181 */
4182 return atomic_long_read(&obj->base.filp->f_count) == 1;
4183}
4184
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004185static void __i915_gem_free_objects(struct drm_i915_private *i915,
4186 struct llist_node *freed)
Chris Wilsonbe726152010-07-23 23:18:50 +01004187{
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004188 struct drm_i915_gem_object *obj, *on;
Chris Wilson538ef962019-01-14 14:21:18 +00004189 intel_wakeref_t wakeref;
Chris Wilsonbe726152010-07-23 23:18:50 +01004190
Chris Wilson538ef962019-01-14 14:21:18 +00004191 wakeref = intel_runtime_pm_get(i915);
Chris Wilsoncc731f52017-10-13 21:26:21 +01004192 llist_for_each_entry_safe(obj, on, freed, freed) {
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004193 struct i915_vma *vma, *vn;
Paulo Zanonif65c9162013-11-27 18:20:34 -02004194
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004195 trace_i915_gem_object_destroy(obj);
4196
Chris Wilsoncc731f52017-10-13 21:26:21 +01004197 mutex_lock(&i915->drm.struct_mutex);
4198
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004199 GEM_BUG_ON(i915_gem_object_is_active(obj));
Chris Wilson528cbd12019-01-28 10:23:54 +00004200 list_for_each_entry_safe(vma, vn, &obj->vma.list, obj_link) {
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004201 GEM_BUG_ON(i915_vma_is_active(vma));
4202 vma->flags &= ~I915_VMA_PIN_MASK;
Chris Wilson3365e222018-05-03 20:51:14 +01004203 i915_vma_destroy(vma);
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004204 }
Chris Wilson528cbd12019-01-28 10:23:54 +00004205 GEM_BUG_ON(!list_empty(&obj->vma.list));
4206 GEM_BUG_ON(!RB_EMPTY_ROOT(&obj->vma.tree));
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004207
Chris Wilsonf2123812017-10-16 12:40:37 +01004208 /* This serializes freeing with the shrinker. Since the free
4209 * is delayed, first by RCU then by the workqueue, we want the
4210 * shrinker to be able to free pages of unreferenced objects,
4211 * or else we may oom whilst there are plenty of deferred
4212 * freed objects.
4213 */
4214 if (i915_gem_object_has_pages(obj)) {
4215 spin_lock(&i915->mm.obj_lock);
4216 list_del_init(&obj->mm.link);
4217 spin_unlock(&i915->mm.obj_lock);
4218 }
4219
Chris Wilsoncc731f52017-10-13 21:26:21 +01004220 mutex_unlock(&i915->drm.struct_mutex);
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004221
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004222 GEM_BUG_ON(obj->bind_count);
Chris Wilsona65adaf2017-10-09 09:43:57 +01004223 GEM_BUG_ON(obj->userfault_count);
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004224 GEM_BUG_ON(atomic_read(&obj->frontbuffer_bits));
Chris Wilson67b48042017-08-22 12:05:16 +01004225 GEM_BUG_ON(!list_empty(&obj->lut_list));
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004226
4227 if (obj->ops->release)
4228 obj->ops->release(obj);
4229
4230 if (WARN_ON(i915_gem_object_has_pinned_pages(obj)))
4231 atomic_set(&obj->mm.pages_pin_count, 0);
Chris Wilson548625e2016-11-01 12:11:34 +00004232 __i915_gem_object_put_pages(obj, I915_MM_NORMAL);
Chris Wilsonf1fa4f42017-10-13 21:26:13 +01004233 GEM_BUG_ON(i915_gem_object_has_pages(obj));
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004234
4235 if (obj->base.import_attach)
4236 drm_prime_gem_destroy(&obj->base, NULL);
4237
Chris Wilsond07f0e52016-10-28 13:58:44 +01004238 reservation_object_fini(&obj->__builtin_resv);
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004239 drm_gem_object_release(&obj->base);
4240 i915_gem_info_remove_obj(i915, obj->base.size);
4241
Andy Shevchenko6e514e32019-03-04 11:29:08 +02004242 bitmap_free(obj->bit_17);
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004243 i915_gem_object_free(obj);
Chris Wilsoncc731f52017-10-13 21:26:21 +01004244
Chris Wilsonc9c704712018-02-19 22:06:31 +00004245 GEM_BUG_ON(!atomic_read(&i915->mm.free_count));
4246 atomic_dec(&i915->mm.free_count);
4247
Chris Wilsoncc731f52017-10-13 21:26:21 +01004248 if (on)
4249 cond_resched();
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004250 }
Chris Wilson538ef962019-01-14 14:21:18 +00004251 intel_runtime_pm_put(i915, wakeref);
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004252}
4253
4254static void i915_gem_flush_free_objects(struct drm_i915_private *i915)
4255{
4256 struct llist_node *freed;
4257
Chris Wilson87701b42017-10-13 21:26:20 +01004258 /* Free the oldest, most stale object to keep the free_list short */
4259 freed = NULL;
4260 if (!llist_empty(&i915->mm.free_list)) { /* quick test for hotpath */
4261 /* Only one consumer of llist_del_first() allowed */
4262 spin_lock(&i915->mm.free_lock);
4263 freed = llist_del_first(&i915->mm.free_list);
4264 spin_unlock(&i915->mm.free_lock);
4265 }
4266 if (unlikely(freed)) {
4267 freed->next = NULL;
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004268 __i915_gem_free_objects(i915, freed);
Chris Wilson87701b42017-10-13 21:26:20 +01004269 }
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004270}
4271
4272static void __i915_gem_free_work(struct work_struct *work)
4273{
4274 struct drm_i915_private *i915 =
4275 container_of(work, struct drm_i915_private, mm.free_work);
4276 struct llist_node *freed;
Chris Wilson26e12f82011-03-20 11:20:19 +00004277
Chris Wilson2ef1e722018-01-15 20:57:59 +00004278 /*
4279 * All file-owned VMA should have been released by this point through
Chris Wilsonb1f788c2016-08-04 07:52:45 +01004280 * i915_gem_close_object(), or earlier by i915_gem_context_close().
4281 * However, the object may also be bound into the global GTT (e.g.
4282 * older GPUs without per-process support, or for direct access through
4283 * the GTT either for the user or for scanout). Those VMA still need to
4284 * unbound now.
4285 */
Chris Wilson1488fc02012-04-24 15:47:31 +01004286
Chris Wilsonf991c492017-11-06 11:15:08 +00004287 spin_lock(&i915->mm.free_lock);
Chris Wilson5ad08be2017-04-07 11:25:51 +01004288 while ((freed = llist_del_all(&i915->mm.free_list))) {
Chris Wilsonf991c492017-11-06 11:15:08 +00004289 spin_unlock(&i915->mm.free_lock);
4290
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004291 __i915_gem_free_objects(i915, freed);
Chris Wilson5ad08be2017-04-07 11:25:51 +01004292 if (need_resched())
Chris Wilsonf991c492017-11-06 11:15:08 +00004293 return;
4294
4295 spin_lock(&i915->mm.free_lock);
Chris Wilson5ad08be2017-04-07 11:25:51 +01004296 }
Chris Wilsonf991c492017-11-06 11:15:08 +00004297 spin_unlock(&i915->mm.free_lock);
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004298}
4299
4300static void __i915_gem_free_object_rcu(struct rcu_head *head)
4301{
4302 struct drm_i915_gem_object *obj =
4303 container_of(head, typeof(*obj), rcu);
4304 struct drm_i915_private *i915 = to_i915(obj->base.dev);
4305
Chris Wilson2ef1e722018-01-15 20:57:59 +00004306 /*
Chris Wilson8811d612018-11-09 09:03:11 +00004307 * We reuse obj->rcu for the freed list, so we had better not treat
4308 * it like a rcu_head from this point forwards. And we expect all
4309 * objects to be freed via this path.
4310 */
4311 destroy_rcu_head(&obj->rcu);
4312
4313 /*
Chris Wilson2ef1e722018-01-15 20:57:59 +00004314 * Since we require blocking on struct_mutex to unbind the freed
4315 * object from the GPU before releasing resources back to the
4316 * system, we can not do that directly from the RCU callback (which may
4317 * be a softirq context), but must instead then defer that work onto a
4318 * kthread. We use the RCU callback rather than move the freed object
4319 * directly onto the work queue so that we can mix between using the
4320 * worker and performing frees directly from subsequent allocations for
4321 * crude but effective memory throttling.
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004322 */
4323 if (llist_add(&obj->freed, &i915->mm.free_list))
Chris Wilsonbeacbd12018-01-15 12:28:45 +00004324 queue_work(i915->wq, &i915->mm.free_work);
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004325}
4326
4327void i915_gem_free_object(struct drm_gem_object *gem_obj)
4328{
4329 struct drm_i915_gem_object *obj = to_intel_bo(gem_obj);
4330
Chris Wilsonbc0629a2016-11-01 10:03:17 +00004331 if (obj->mm.quirked)
4332 __i915_gem_object_unpin_pages(obj);
4333
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004334 if (discard_backing_storage(obj))
4335 obj->mm.madv = I915_MADV_DONTNEED;
Daniel Vettera071fa02014-06-18 23:28:09 +02004336
Chris Wilson2ef1e722018-01-15 20:57:59 +00004337 /*
4338 * Before we free the object, make sure any pure RCU-only
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004339 * read-side critical sections are complete, e.g.
4340 * i915_gem_busy_ioctl(). For the corresponding synchronized
4341 * lookup see i915_gem_object_lookup_rcu().
4342 */
Chris Wilsonc9c704712018-02-19 22:06:31 +00004343 atomic_inc(&to_i915(obj->base.dev)->mm.free_count);
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004344 call_rcu(&obj->rcu, __i915_gem_free_object_rcu);
Chris Wilsonbe726152010-07-23 23:18:50 +01004345}
4346
Chris Wilsonf8a7fde2016-10-28 13:58:29 +01004347void __i915_gem_object_release_unless_active(struct drm_i915_gem_object *obj)
4348{
4349 lockdep_assert_held(&obj->base.dev->struct_mutex);
4350
Chris Wilsond1b48c12017-08-16 09:52:08 +01004351 if (!i915_gem_object_has_active_reference(obj) &&
4352 i915_gem_object_is_active(obj))
Chris Wilsonf8a7fde2016-10-28 13:58:29 +01004353 i915_gem_object_set_active_reference(obj);
4354 else
4355 i915_gem_object_put(obj);
4356}
4357
Chris Wilson24145512017-01-24 11:01:35 +00004358void i915_gem_sanitize(struct drm_i915_private *i915)
4359{
Chris Wilson538ef962019-01-14 14:21:18 +00004360 intel_wakeref_t wakeref;
4361
Chris Wilsonc3160da2018-05-31 09:22:45 +01004362 GEM_TRACE("\n");
4363
Chris Wilson538ef962019-01-14 14:21:18 +00004364 wakeref = intel_runtime_pm_get(i915);
Daniele Ceraolo Spurio3ceea6a2019-03-19 11:35:36 -07004365 intel_uncore_forcewake_get(&i915->uncore, FORCEWAKE_ALL);
Chris Wilsonc3160da2018-05-31 09:22:45 +01004366
4367 /*
4368 * As we have just resumed the machine and woken the device up from
4369 * deep PCI sleep (presumably D3_cold), assume the HW has been reset
4370 * back to defaults, recovering from whatever wedged state we left it
4371 * in and so worth trying to use the device once more.
4372 */
Chris Wilsonc41166f2019-02-20 14:56:37 +00004373 if (i915_terminally_wedged(i915))
Chris Wilsonf36325f2017-08-26 12:09:34 +01004374 i915_gem_unset_wedged(i915);
Chris Wilsonf36325f2017-08-26 12:09:34 +01004375
Chris Wilson24145512017-01-24 11:01:35 +00004376 /*
4377 * If we inherit context state from the BIOS or earlier occupants
4378 * of the GPU, the GPU may be in an inconsistent state when we
4379 * try to take over. The only way to remove the earlier state
4380 * is by resetting. However, resetting on earlier gen is tricky as
4381 * it may impact the display and we are uncertain about the stability
Joonas Lahtinenea117b82017-04-28 10:53:38 +03004382 * of the reset, so this could be applied to even earlier gen.
Chris Wilson24145512017-01-24 11:01:35 +00004383 */
Chris Wilson55277e12019-01-03 11:21:04 +00004384 intel_engines_sanitize(i915, false);
Chris Wilsonc3160da2018-05-31 09:22:45 +01004385
Daniele Ceraolo Spurio3ceea6a2019-03-19 11:35:36 -07004386 intel_uncore_forcewake_put(&i915->uncore, FORCEWAKE_ALL);
Chris Wilson538ef962019-01-14 14:21:18 +00004387 intel_runtime_pm_put(i915, wakeref);
Chris Wilsonc3160da2018-05-31 09:22:45 +01004388
Chris Wilsoneb8d0f52019-01-25 13:22:28 +00004389 mutex_lock(&i915->drm.struct_mutex);
Chris Wilson4dfacb02018-05-31 09:22:43 +01004390 i915_gem_contexts_lost(i915);
4391 mutex_unlock(&i915->drm.struct_mutex);
Chris Wilson24145512017-01-24 11:01:35 +00004392}
4393
Chris Wilson5861b012019-03-08 09:36:54 +00004394void i915_gem_suspend(struct drm_i915_private *i915)
Eric Anholt673a3942008-07-30 12:06:12 -07004395{
Chris Wilson538ef962019-01-14 14:21:18 +00004396 intel_wakeref_t wakeref;
Eric Anholt673a3942008-07-30 12:06:12 -07004397
Chris Wilson09a4c022018-05-24 09:11:35 +01004398 GEM_TRACE("\n");
4399
Chris Wilson538ef962019-01-14 14:21:18 +00004400 wakeref = intel_runtime_pm_get(i915);
Chris Wilson54b4f682016-07-21 21:16:19 +01004401
Chris Wilsoneb8d0f52019-01-25 13:22:28 +00004402 flush_workqueue(i915->wq);
4403
Chris Wilsonbf061122018-07-09 14:02:04 +01004404 mutex_lock(&i915->drm.struct_mutex);
Chris Wilson5ab57c72016-07-15 14:56:20 +01004405
Chris Wilsonbf061122018-07-09 14:02:04 +01004406 /*
4407 * We have to flush all the executing contexts to main memory so
Chris Wilson5ab57c72016-07-15 14:56:20 +01004408 * that they can saved in the hibernation image. To ensure the last
4409 * context image is coherent, we have to switch away from it. That
Chris Wilsonbf061122018-07-09 14:02:04 +01004410 * leaves the i915->kernel_context still active when
Chris Wilson5ab57c72016-07-15 14:56:20 +01004411 * we actually suspend, and its image in memory may not match the GPU
4412 * state. Fortunately, the kernel_context is disposable and we do
4413 * not rely on its state.
4414 */
Chris Wilsonc6eeb472019-03-08 09:36:56 +00004415 switch_to_kernel_context_sync(i915, i915->gt.active_engines);
Chris Wilson01f8f332018-07-17 09:41:21 +01004416
Chris Wilsonbf061122018-07-09 14:02:04 +01004417 mutex_unlock(&i915->drm.struct_mutex);
Chris Wilsoneb8d0f52019-01-25 13:22:28 +00004418 i915_reset_flush(i915);
Chris Wilson45c5f202013-10-16 11:50:01 +01004419
Chris Wilsoneb8d0f52019-01-25 13:22:28 +00004420 drain_delayed_work(&i915->gt.retire_work);
Chris Wilsonbdeb9782016-12-23 14:57:56 +00004421
Chris Wilsonbf061122018-07-09 14:02:04 +01004422 /*
4423 * As the idle_work is rearming if it detects a race, play safe and
Chris Wilsonbdeb9782016-12-23 14:57:56 +00004424 * repeat the flush until it is definitely idle.
4425 */
Chris Wilsonbf061122018-07-09 14:02:04 +01004426 drain_delayed_work(&i915->gt.idle_work);
Chris Wilsonbdeb9782016-12-23 14:57:56 +00004427
Chris Wilsonbf061122018-07-09 14:02:04 +01004428 /*
4429 * Assert that we successfully flushed all the work and
Chris Wilsonbdcf1202014-11-25 11:56:33 +00004430 * reset the GPU back to its idle, low power state.
4431 */
Chris Wilson50b022a2019-03-07 10:45:30 +00004432 GEM_BUG_ON(i915->gt.awake);
Chris Wilsonbdcf1202014-11-25 11:56:33 +00004433
Sujaritha Sundaresanb9d52d32019-03-21 13:38:04 -07004434 intel_uc_suspend(i915);
4435
Chris Wilson538ef962019-01-14 14:21:18 +00004436 intel_runtime_pm_put(i915, wakeref);
Chris Wilsonec92ad02018-05-31 09:22:46 +01004437}
4438
4439void i915_gem_suspend_late(struct drm_i915_private *i915)
4440{
Chris Wilson9776f472018-06-01 15:41:24 +01004441 struct drm_i915_gem_object *obj;
4442 struct list_head *phases[] = {
4443 &i915->mm.unbound_list,
4444 &i915->mm.bound_list,
4445 NULL
4446 }, **phase;
4447
Imre Deak1c777c52016-10-12 17:46:37 +03004448 /*
4449 * Neither the BIOS, ourselves or any other kernel
4450 * expects the system to be in execlists mode on startup,
4451 * so we need to reset the GPU back to legacy mode. And the only
4452 * known way to disable logical contexts is through a GPU reset.
4453 *
4454 * So in order to leave the system in a known default configuration,
4455 * always reset the GPU upon unload and suspend. Afterwards we then
4456 * clean up the GEM state tracking, flushing off the requests and
4457 * leaving the system in a known idle state.
4458 *
4459 * Note that is of the upmost importance that the GPU is idle and
4460 * all stray writes are flushed *before* we dismantle the backing
4461 * storage for the pinned objects.
4462 *
4463 * However, since we are uncertain that resetting the GPU on older
4464 * machines is a good idea, we don't - just in case it leaves the
4465 * machine in an unusable condition.
4466 */
Chris Wilsoncad99462017-08-26 12:09:33 +01004467
Chris Wilson9776f472018-06-01 15:41:24 +01004468 mutex_lock(&i915->drm.struct_mutex);
4469 for (phase = phases; *phase; phase++) {
4470 list_for_each_entry(obj, *phase, mm.link)
4471 WARN_ON(i915_gem_object_set_to_gtt_domain(obj, false));
4472 }
4473 mutex_unlock(&i915->drm.struct_mutex);
4474
Chris Wilsonec92ad02018-05-31 09:22:46 +01004475 intel_uc_sanitize(i915);
4476 i915_gem_sanitize(i915);
Eric Anholt673a3942008-07-30 12:06:12 -07004477}
4478
Chris Wilson37cd3302017-11-12 11:27:38 +00004479void i915_gem_resume(struct drm_i915_private *i915)
Chris Wilson5ab57c72016-07-15 14:56:20 +01004480{
Chris Wilson4dfacb02018-05-31 09:22:43 +01004481 GEM_TRACE("\n");
4482
Chris Wilson37cd3302017-11-12 11:27:38 +00004483 WARN_ON(i915->gt.awake);
Chris Wilson5ab57c72016-07-15 14:56:20 +01004484
Chris Wilson37cd3302017-11-12 11:27:38 +00004485 mutex_lock(&i915->drm.struct_mutex);
Daniele Ceraolo Spurio3ceea6a2019-03-19 11:35:36 -07004486 intel_uncore_forcewake_get(&i915->uncore, FORCEWAKE_ALL);
Imre Deak31ab49a2016-11-07 11:20:05 +02004487
Chris Wilson37cd3302017-11-12 11:27:38 +00004488 i915_gem_restore_gtt_mappings(i915);
4489 i915_gem_restore_fences(i915);
Chris Wilson5ab57c72016-07-15 14:56:20 +01004490
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00004491 /*
4492 * As we didn't flush the kernel context before suspend, we cannot
Chris Wilson5ab57c72016-07-15 14:56:20 +01004493 * guarantee that the context image is complete. So let's just reset
4494 * it and start again.
4495 */
Chris Wilson37cd3302017-11-12 11:27:38 +00004496 i915->gt.resume(i915);
Chris Wilson5ab57c72016-07-15 14:56:20 +01004497
Chris Wilson37cd3302017-11-12 11:27:38 +00004498 if (i915_gem_init_hw(i915))
4499 goto err_wedged;
4500
Michal Wajdeczko7cfca4a2018-03-02 11:15:49 +00004501 intel_uc_resume(i915);
Chris Wilson7469c622017-11-14 13:03:00 +00004502
Chris Wilson37cd3302017-11-12 11:27:38 +00004503 /* Always reload a context for powersaving. */
Chris Wilson604c37d2019-03-08 09:36:55 +00004504 if (!load_power_context(i915))
Chris Wilson37cd3302017-11-12 11:27:38 +00004505 goto err_wedged;
4506
4507out_unlock:
Daniele Ceraolo Spurio3ceea6a2019-03-19 11:35:36 -07004508 intel_uncore_forcewake_put(&i915->uncore, FORCEWAKE_ALL);
Chris Wilson37cd3302017-11-12 11:27:38 +00004509 mutex_unlock(&i915->drm.struct_mutex);
4510 return;
4511
4512err_wedged:
Chris Wilsonc41166f2019-02-20 14:56:37 +00004513 if (!i915_reset_failed(i915)) {
4514 dev_err(i915->drm.dev,
4515 "Failed to re-initialize GPU, declaring it wedged!\n");
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00004516 i915_gem_set_wedged(i915);
4517 }
Chris Wilson37cd3302017-11-12 11:27:38 +00004518 goto out_unlock;
Chris Wilson5ab57c72016-07-15 14:56:20 +01004519}
4520
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00004521void i915_gem_init_swizzling(struct drm_i915_private *dev_priv)
Daniel Vetterf691e2f2012-02-02 09:58:12 +01004522{
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00004523 if (INTEL_GEN(dev_priv) < 5 ||
Daniel Vetterf691e2f2012-02-02 09:58:12 +01004524 dev_priv->mm.bit_6_swizzle_x == I915_BIT_6_SWIZZLE_NONE)
4525 return;
4526
4527 I915_WRITE(DISP_ARB_CTL, I915_READ(DISP_ARB_CTL) |
4528 DISP_TILE_SURFACE_SWIZZLING);
4529
Lucas De Marchicf819ef2018-12-12 10:10:43 -08004530 if (IS_GEN(dev_priv, 5))
Daniel Vetter11782b02012-01-31 16:47:55 +01004531 return;
4532
Daniel Vetterf691e2f2012-02-02 09:58:12 +01004533 I915_WRITE(TILECTL, I915_READ(TILECTL) | TILECTL_SWZCTL);
Lucas De Marchicf819ef2018-12-12 10:10:43 -08004534 if (IS_GEN(dev_priv, 6))
Daniel Vetter6b26c862012-04-24 14:04:12 +02004535 I915_WRITE(ARB_MODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_SNB));
Lucas De Marchicf819ef2018-12-12 10:10:43 -08004536 else if (IS_GEN(dev_priv, 7))
Daniel Vetter6b26c862012-04-24 14:04:12 +02004537 I915_WRITE(ARB_MODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_IVB));
Lucas De Marchicf819ef2018-12-12 10:10:43 -08004538 else if (IS_GEN(dev_priv, 8))
Ben Widawsky31a53362013-11-02 21:07:04 -07004539 I915_WRITE(GAMTARBMODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_BDW));
Ben Widawsky8782e262012-12-18 10:31:23 -08004540 else
4541 BUG();
Daniel Vetterf691e2f2012-02-02 09:58:12 +01004542}
Daniel Vettere21af882012-02-09 20:53:27 +01004543
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01004544static void init_unused_ring(struct drm_i915_private *dev_priv, u32 base)
Ville Syrjälä81e7f202014-08-15 01:21:55 +03004545{
Ville Syrjälä81e7f202014-08-15 01:21:55 +03004546 I915_WRITE(RING_CTL(base), 0);
4547 I915_WRITE(RING_HEAD(base), 0);
4548 I915_WRITE(RING_TAIL(base), 0);
4549 I915_WRITE(RING_START(base), 0);
4550}
4551
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01004552static void init_unused_rings(struct drm_i915_private *dev_priv)
Ville Syrjälä81e7f202014-08-15 01:21:55 +03004553{
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01004554 if (IS_I830(dev_priv)) {
4555 init_unused_ring(dev_priv, PRB1_BASE);
4556 init_unused_ring(dev_priv, SRB0_BASE);
4557 init_unused_ring(dev_priv, SRB1_BASE);
4558 init_unused_ring(dev_priv, SRB2_BASE);
4559 init_unused_ring(dev_priv, SRB3_BASE);
Lucas De Marchicf819ef2018-12-12 10:10:43 -08004560 } else if (IS_GEN(dev_priv, 2)) {
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01004561 init_unused_ring(dev_priv, SRB0_BASE);
4562 init_unused_ring(dev_priv, SRB1_BASE);
Lucas De Marchicf819ef2018-12-12 10:10:43 -08004563 } else if (IS_GEN(dev_priv, 3)) {
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01004564 init_unused_ring(dev_priv, PRB1_BASE);
4565 init_unused_ring(dev_priv, PRB2_BASE);
Ville Syrjälä81e7f202014-08-15 01:21:55 +03004566 }
4567}
4568
Chris Wilson20a8a742017-02-08 14:30:31 +00004569static int __i915_gem_restart_engines(void *data)
Ben Widawsky4fc7c972013-02-08 11:49:24 -08004570{
Chris Wilson20a8a742017-02-08 14:30:31 +00004571 struct drm_i915_private *i915 = data;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00004572 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05304573 enum intel_engine_id id;
Chris Wilson20a8a742017-02-08 14:30:31 +00004574 int err;
4575
4576 for_each_engine(engine, i915, id) {
4577 err = engine->init_hw(engine);
Chris Wilson8177e112018-02-07 11:15:45 +00004578 if (err) {
4579 DRM_ERROR("Failed to restart %s (%d)\n",
4580 engine->name, err);
Chris Wilson20a8a742017-02-08 14:30:31 +00004581 return err;
Chris Wilson8177e112018-02-07 11:15:45 +00004582 }
Chris Wilson20a8a742017-02-08 14:30:31 +00004583 }
4584
Chris Wilson2d5eaad2019-02-26 10:24:00 +00004585 intel_engines_set_scheduler_caps(i915);
4586
Chris Wilson20a8a742017-02-08 14:30:31 +00004587 return 0;
4588}
4589
4590int i915_gem_init_hw(struct drm_i915_private *dev_priv)
4591{
Chris Wilsond200cda2016-04-28 09:56:44 +01004592 int ret;
Ben Widawsky4fc7c972013-02-08 11:49:24 -08004593
Chris Wilsonde867c22016-10-25 13:16:02 +01004594 dev_priv->gt.last_init_time = ktime_get();
4595
Chris Wilson5e4f5182015-02-13 14:35:59 +00004596 /* Double layer security blanket, see i915_gem_init() */
Daniele Ceraolo Spurio3ceea6a2019-03-19 11:35:36 -07004597 intel_uncore_forcewake_get(&dev_priv->uncore, FORCEWAKE_ALL);
Chris Wilson5e4f5182015-02-13 14:35:59 +00004598
Tvrtko Ursulin0031fb92016-11-04 14:42:44 +00004599 if (HAS_EDRAM(dev_priv) && INTEL_GEN(dev_priv) < 9)
Ben Widawsky05e21cc2013-07-04 11:02:04 -07004600 I915_WRITE(HSW_IDICR, I915_READ(HSW_IDICR) | IDIHASHMSK(0xf));
Ben Widawsky4fc7c972013-02-08 11:49:24 -08004601
Tvrtko Ursulin772c2a52016-10-13 11:03:01 +01004602 if (IS_HASWELL(dev_priv))
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01004603 I915_WRITE(MI_PREDICATE_RESULT_2, IS_HSW_GT3(dev_priv) ?
Ville Syrjälä0bf21342013-11-29 14:56:12 +02004604 LOWER_SLICE_ENABLED : LOWER_SLICE_DISABLED);
Rodrigo Vivi94353732013-08-28 16:45:46 -03004605
Tvrtko Ursulin094304b2018-12-03 12:50:10 +00004606 /* Apply the GT workarounds... */
Tvrtko Ursulin25d140f2018-12-03 13:33:19 +00004607 intel_gt_apply_workarounds(dev_priv);
Tvrtko Ursulin094304b2018-12-03 12:50:10 +00004608 /* ...and determine whether they are sticking. */
4609 intel_gt_verify_workarounds(dev_priv, "init");
Oscar Mateo59b449d2018-04-10 09:12:47 -07004610
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00004611 i915_gem_init_swizzling(dev_priv);
Ben Widawsky4fc7c972013-02-08 11:49:24 -08004612
Daniel Vetterd5abdfd2014-11-20 09:45:19 +01004613 /*
4614 * At least 830 can leave some of the unused rings
4615 * "active" (ie. head != tail) after resume which
4616 * will prevent c3 entry. Makes sure all unused rings
4617 * are totally idle.
4618 */
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01004619 init_unused_rings(dev_priv);
Daniel Vetterd5abdfd2014-11-20 09:45:19 +01004620
Dave Gordoned54c1a2016-01-19 19:02:54 +00004621 BUG_ON(!dev_priv->kernel_context);
Chris Wilsonc41166f2019-02-20 14:56:37 +00004622 ret = i915_terminally_wedged(dev_priv);
4623 if (ret)
Chris Wilson6f74b362017-10-15 15:37:25 +01004624 goto out;
John Harrison90638cc2015-05-29 17:43:37 +01004625
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00004626 ret = i915_ppgtt_init_hw(dev_priv);
John Harrison4ad2fd82015-06-18 13:11:20 +01004627 if (ret) {
Chris Wilson8177e112018-02-07 11:15:45 +00004628 DRM_ERROR("Enabling PPGTT failed (%d)\n", ret);
John Harrison4ad2fd82015-06-18 13:11:20 +01004629 goto out;
4630 }
4631
Jackie Lif08e2032018-03-13 17:32:53 -07004632 ret = intel_wopcm_init_hw(&dev_priv->wopcm);
4633 if (ret) {
4634 DRM_ERROR("Enabling WOPCM failed (%d)\n", ret);
4635 goto out;
4636 }
4637
Michał Winiarski9bdc3572017-10-25 18:25:19 +01004638 /* We can't enable contexts until all firmware is loaded */
4639 ret = intel_uc_init_hw(dev_priv);
Chris Wilson8177e112018-02-07 11:15:45 +00004640 if (ret) {
4641 DRM_ERROR("Enabling uc failed (%d)\n", ret);
Michał Winiarski9bdc3572017-10-25 18:25:19 +01004642 goto out;
Chris Wilson8177e112018-02-07 11:15:45 +00004643 }
Michał Winiarski9bdc3572017-10-25 18:25:19 +01004644
Tvrtko Ursulinbf9e8422016-12-01 14:16:38 +00004645 intel_mocs_init_l3cc_table(dev_priv);
Peter Antoine0ccdacf2016-04-13 15:03:25 +01004646
Chris Wilson136109c2017-11-02 13:14:30 +00004647 /* Only when the HW is re-initialised, can we replay the requests */
4648 ret = __i915_gem_restart_engines(dev_priv);
Michal Wajdeczkob96f6eb2018-06-05 12:24:43 +00004649 if (ret)
4650 goto cleanup_uc;
Michał Winiarski60c0a662018-07-12 14:48:10 +02004651
Daniele Ceraolo Spurio3ceea6a2019-03-19 11:35:36 -07004652 intel_uncore_forcewake_put(&dev_priv->uncore, FORCEWAKE_ALL);
Michał Winiarski60c0a662018-07-12 14:48:10 +02004653
4654 return 0;
Michal Wajdeczkob96f6eb2018-06-05 12:24:43 +00004655
4656cleanup_uc:
4657 intel_uc_fini_hw(dev_priv);
Michał Winiarski60c0a662018-07-12 14:48:10 +02004658out:
Daniele Ceraolo Spurio3ceea6a2019-03-19 11:35:36 -07004659 intel_uncore_forcewake_put(&dev_priv->uncore, FORCEWAKE_ALL);
Michał Winiarski60c0a662018-07-12 14:48:10 +02004660
4661 return ret;
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004662}
4663
Chris Wilsond2b4b972017-11-10 14:26:33 +00004664static int __intel_engines_record_defaults(struct drm_i915_private *i915)
4665{
4666 struct i915_gem_context *ctx;
4667 struct intel_engine_cs *engine;
4668 enum intel_engine_id id;
Chris Wilson604c37d2019-03-08 09:36:55 +00004669 int err = 0;
Chris Wilsond2b4b972017-11-10 14:26:33 +00004670
4671 /*
4672 * As we reset the gpu during very early sanitisation, the current
4673 * register state on the GPU should reflect its defaults values.
4674 * We load a context onto the hw (with restore-inhibit), then switch
4675 * over to a second context to save that default register state. We
4676 * can then prime every new context with that state so they all start
4677 * from the same default HW values.
4678 */
4679
4680 ctx = i915_gem_context_create_kernel(i915, 0);
4681 if (IS_ERR(ctx))
4682 return PTR_ERR(ctx);
4683
4684 for_each_engine(engine, i915, id) {
Chris Wilsone61e0f52018-02-21 09:56:36 +00004685 struct i915_request *rq;
Chris Wilsond2b4b972017-11-10 14:26:33 +00004686
Chris Wilsone61e0f52018-02-21 09:56:36 +00004687 rq = i915_request_alloc(engine, ctx);
Chris Wilsond2b4b972017-11-10 14:26:33 +00004688 if (IS_ERR(rq)) {
4689 err = PTR_ERR(rq);
4690 goto out_ctx;
4691 }
4692
Chris Wilson3fef5cd2017-11-20 10:20:02 +00004693 err = 0;
Chris Wilsond2b4b972017-11-10 14:26:33 +00004694 if (engine->init_context)
4695 err = engine->init_context(rq);
4696
Chris Wilson697b9a82018-06-12 11:51:35 +01004697 i915_request_add(rq);
Chris Wilsond2b4b972017-11-10 14:26:33 +00004698 if (err)
4699 goto err_active;
4700 }
4701
Chris Wilson604c37d2019-03-08 09:36:55 +00004702 /* Flush the default context image to memory, and enable powersaving. */
4703 if (!load_power_context(i915)) {
4704 err = -EIO;
Chris Wilsond2b4b972017-11-10 14:26:33 +00004705 goto err_active;
Chris Wilson2621cef2018-07-09 13:20:43 +01004706 }
Chris Wilsond2b4b972017-11-10 14:26:33 +00004707
Chris Wilsond2b4b972017-11-10 14:26:33 +00004708 for_each_engine(engine, i915, id) {
Chris Wilsonc4d52fe2019-03-08 13:25:19 +00004709 struct intel_context *ce;
Chris Wilsond2b4b972017-11-10 14:26:33 +00004710 struct i915_vma *state;
Chris Wilson37d7c9c2018-09-14 13:35:03 +01004711 void *vaddr;
Chris Wilsond2b4b972017-11-10 14:26:33 +00004712
Chris Wilsonc4d52fe2019-03-08 13:25:19 +00004713 ce = intel_context_lookup(ctx, engine);
4714 if (!ce)
4715 continue;
Chris Wilson666424a2018-09-14 13:35:04 +01004716
Chris Wilsonc4d52fe2019-03-08 13:25:19 +00004717 state = ce->state;
Chris Wilsond2b4b972017-11-10 14:26:33 +00004718 if (!state)
4719 continue;
4720
Chris Wilson08819542019-03-08 13:25:22 +00004721 GEM_BUG_ON(intel_context_is_pinned(ce));
Chris Wilsonc4d52fe2019-03-08 13:25:19 +00004722
Chris Wilsond2b4b972017-11-10 14:26:33 +00004723 /*
4724 * As we will hold a reference to the logical state, it will
4725 * not be torn down with the context, and importantly the
4726 * object will hold onto its vma (making it possible for a
4727 * stray GTT write to corrupt our defaults). Unmap the vma
4728 * from the GTT to prevent such accidents and reclaim the
4729 * space.
4730 */
4731 err = i915_vma_unbind(state);
4732 if (err)
4733 goto err_active;
4734
4735 err = i915_gem_object_set_to_cpu_domain(state->obj, false);
4736 if (err)
4737 goto err_active;
4738
4739 engine->default_state = i915_gem_object_get(state->obj);
Chris Wilsona679f582019-03-21 16:19:07 +00004740 i915_gem_object_set_cache_coherency(engine->default_state,
4741 I915_CACHE_LLC);
Chris Wilson37d7c9c2018-09-14 13:35:03 +01004742
4743 /* Check we can acquire the image of the context state */
4744 vaddr = i915_gem_object_pin_map(engine->default_state,
Chris Wilson666424a2018-09-14 13:35:04 +01004745 I915_MAP_FORCE_WB);
Chris Wilson37d7c9c2018-09-14 13:35:03 +01004746 if (IS_ERR(vaddr)) {
4747 err = PTR_ERR(vaddr);
4748 goto err_active;
4749 }
4750
4751 i915_gem_object_unpin_map(engine->default_state);
Chris Wilsond2b4b972017-11-10 14:26:33 +00004752 }
4753
4754 if (IS_ENABLED(CONFIG_DRM_I915_DEBUG_GEM)) {
4755 unsigned int found = intel_engines_has_context_isolation(i915);
4756
4757 /*
4758 * Make sure that classes with multiple engine instances all
4759 * share the same basic configuration.
4760 */
4761 for_each_engine(engine, i915, id) {
4762 unsigned int bit = BIT(engine->uabi_class);
4763 unsigned int expected = engine->default_state ? bit : 0;
4764
4765 if ((found & bit) != expected) {
4766 DRM_ERROR("mismatching default context state for class %d on engine %s\n",
4767 engine->uabi_class, engine->name);
4768 }
4769 }
4770 }
4771
4772out_ctx:
4773 i915_gem_context_set_closed(ctx);
4774 i915_gem_context_put(ctx);
4775 return err;
4776
4777err_active:
4778 /*
4779 * If we have to abandon now, we expect the engines to be idle
Chris Wilson604c37d2019-03-08 09:36:55 +00004780 * and ready to be torn-down. The quickest way we can accomplish
4781 * this is by declaring ourselves wedged.
Chris Wilsond2b4b972017-11-10 14:26:33 +00004782 */
Chris Wilson604c37d2019-03-08 09:36:55 +00004783 i915_gem_set_wedged(i915);
Chris Wilsond2b4b972017-11-10 14:26:33 +00004784 goto out_ctx;
4785}
4786
Chris Wilson51797492018-12-04 14:15:16 +00004787static int
4788i915_gem_init_scratch(struct drm_i915_private *i915, unsigned int size)
4789{
4790 struct drm_i915_gem_object *obj;
4791 struct i915_vma *vma;
4792 int ret;
4793
4794 obj = i915_gem_object_create_stolen(i915, size);
4795 if (!obj)
4796 obj = i915_gem_object_create_internal(i915, size);
4797 if (IS_ERR(obj)) {
4798 DRM_ERROR("Failed to allocate scratch page\n");
4799 return PTR_ERR(obj);
4800 }
4801
4802 vma = i915_vma_instance(obj, &i915->ggtt.vm, NULL);
4803 if (IS_ERR(vma)) {
4804 ret = PTR_ERR(vma);
4805 goto err_unref;
4806 }
4807
4808 ret = i915_vma_pin(vma, 0, 0, PIN_GLOBAL | PIN_HIGH);
4809 if (ret)
4810 goto err_unref;
4811
4812 i915->gt.scratch = vma;
4813 return 0;
4814
4815err_unref:
4816 i915_gem_object_put(obj);
4817 return ret;
4818}
4819
4820static void i915_gem_fini_scratch(struct drm_i915_private *i915)
4821{
4822 i915_vma_unpin_and_release(&i915->gt.scratch, 0);
4823}
4824
Tvrtko Ursulinbf9e8422016-12-01 14:16:38 +00004825int i915_gem_init(struct drm_i915_private *dev_priv)
Chris Wilson1070a422012-04-24 15:47:41 +01004826{
Chris Wilson1070a422012-04-24 15:47:41 +01004827 int ret;
4828
Changbin Du52b24162018-05-08 17:07:05 +08004829 /* We need to fallback to 4K pages if host doesn't support huge gtt. */
4830 if (intel_vgpu_active(dev_priv) && !intel_vgpu_has_huge_gtt(dev_priv))
Matthew Auldda9fe3f32017-10-06 23:18:31 +01004831 mkwrite_device_info(dev_priv)->page_sizes =
4832 I915_GTT_PAGE_SIZE_4K;
4833
Chris Wilson94312822017-05-03 10:39:18 +01004834 dev_priv->mm.unordered_timeline = dma_fence_context_alloc(1);
Chris Wilson57822dc2017-02-22 11:40:48 +00004835
Chris Wilsonfb5c5512017-11-20 20:55:00 +00004836 if (HAS_LOGICAL_RING_CONTEXTS(dev_priv)) {
Chris Wilson821ed7d2016-09-09 14:11:53 +01004837 dev_priv->gt.resume = intel_lr_context_resume;
Tvrtko Ursulin117897f2016-03-16 11:00:40 +00004838 dev_priv->gt.cleanup_engine = intel_logical_ring_cleanup;
Chris Wilsonfb5c5512017-11-20 20:55:00 +00004839 } else {
4840 dev_priv->gt.resume = intel_legacy_submission_resume;
4841 dev_priv->gt.cleanup_engine = intel_engine_cleanup;
Oscar Mateoa83014d2014-07-24 17:04:21 +01004842 }
4843
Chris Wilson1e345562019-01-28 10:23:56 +00004844 i915_timelines_init(dev_priv);
4845
Chris Wilsonee487002017-11-22 17:26:21 +00004846 ret = i915_gem_init_userptr(dev_priv);
4847 if (ret)
4848 return ret;
4849
Sagar Arun Kamble70deead2018-01-24 21:16:58 +05304850 ret = intel_uc_init_misc(dev_priv);
Michał Winiarski3176ff42017-12-13 23:13:47 +01004851 if (ret)
4852 return ret;
4853
Michal Wajdeczkof7dc0152018-06-28 14:15:21 +00004854 ret = intel_wopcm_init(&dev_priv->wopcm);
4855 if (ret)
4856 goto err_uc_misc;
4857
Chris Wilson5e4f5182015-02-13 14:35:59 +00004858 /* This is just a security blanket to placate dragons.
4859 * On some systems, we very sporadically observe that the first TLBs
4860 * used by the CS may be stale, despite us poking the TLB reset. If
4861 * we hold the forcewake during initialisation these problems
4862 * just magically go away.
4863 */
Chris Wilsonee487002017-11-22 17:26:21 +00004864 mutex_lock(&dev_priv->drm.struct_mutex);
Daniele Ceraolo Spurio3ceea6a2019-03-19 11:35:36 -07004865 intel_uncore_forcewake_get(&dev_priv->uncore, FORCEWAKE_ALL);
Chris Wilson5e4f5182015-02-13 14:35:59 +00004866
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01004867 ret = i915_gem_init_ggtt(dev_priv);
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00004868 if (ret) {
4869 GEM_BUG_ON(ret == -EIO);
4870 goto err_unlock;
4871 }
Jesse Barnesd62b4892013-03-08 10:45:53 -08004872
Chris Wilson51797492018-12-04 14:15:16 +00004873 ret = i915_gem_init_scratch(dev_priv,
Lucas De Marchicf819ef2018-12-12 10:10:43 -08004874 IS_GEN(dev_priv, 2) ? SZ_256K : PAGE_SIZE);
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00004875 if (ret) {
4876 GEM_BUG_ON(ret == -EIO);
4877 goto err_ggtt;
4878 }
Ben Widawsky2fa48d82013-12-06 14:11:04 -08004879
Chris Wilson51797492018-12-04 14:15:16 +00004880 ret = i915_gem_contexts_init(dev_priv);
4881 if (ret) {
4882 GEM_BUG_ON(ret == -EIO);
4883 goto err_scratch;
4884 }
4885
Tvrtko Ursulinbf9e8422016-12-01 14:16:38 +00004886 ret = intel_engines_init(dev_priv);
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00004887 if (ret) {
4888 GEM_BUG_ON(ret == -EIO);
4889 goto err_context;
4890 }
Daniel Vetter53ca26c2012-04-26 23:28:03 +02004891
Chris Wilsonf58d13d2017-11-10 14:26:29 +00004892 intel_init_gt_powersave(dev_priv);
4893
Michał Winiarski61b5c152017-12-13 23:13:48 +01004894 ret = intel_uc_init(dev_priv);
Chris Wilsoncc6a8182017-11-10 14:26:30 +00004895 if (ret)
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00004896 goto err_pm;
Chris Wilsoncc6a8182017-11-10 14:26:30 +00004897
Michał Winiarski61b5c152017-12-13 23:13:48 +01004898 ret = i915_gem_init_hw(dev_priv);
4899 if (ret)
4900 goto err_uc_init;
4901
Chris Wilsoncc6a8182017-11-10 14:26:30 +00004902 /*
4903 * Despite its name intel_init_clock_gating applies both display
4904 * clock gating workarounds; GT mmio workarounds and the occasional
4905 * GT power context workaround. Worse, sometimes it includes a context
4906 * register workaround which we need to apply before we record the
4907 * default HW state for all contexts.
4908 *
4909 * FIXME: break up the workarounds and apply them at the right time!
4910 */
4911 intel_init_clock_gating(dev_priv);
4912
Chris Wilsond2b4b972017-11-10 14:26:33 +00004913 ret = __intel_engines_record_defaults(dev_priv);
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00004914 if (ret)
4915 goto err_init_hw;
4916
4917 if (i915_inject_load_failure()) {
4918 ret = -ENODEV;
4919 goto err_init_hw;
4920 }
4921
4922 if (i915_inject_load_failure()) {
4923 ret = -EIO;
4924 goto err_init_hw;
4925 }
4926
Daniele Ceraolo Spurio3ceea6a2019-03-19 11:35:36 -07004927 intel_uncore_forcewake_put(&dev_priv->uncore, FORCEWAKE_ALL);
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00004928 mutex_unlock(&dev_priv->drm.struct_mutex);
4929
4930 return 0;
4931
4932 /*
4933 * Unwinding is complicated by that we want to handle -EIO to mean
4934 * disable GPU submission but keep KMS alive. We want to mark the
4935 * HW as irrevisibly wedged, but keep enough state around that the
4936 * driver doesn't explode during runtime.
4937 */
4938err_init_hw:
Chris Wilson8571a052018-06-06 15:54:41 +01004939 mutex_unlock(&dev_priv->drm.struct_mutex);
4940
Chris Wilson5861b012019-03-08 09:36:54 +00004941 i915_gem_suspend(dev_priv);
Chris Wilson8571a052018-06-06 15:54:41 +01004942 i915_gem_suspend_late(dev_priv);
4943
Chris Wilson8bcf9f72018-07-10 10:44:20 +01004944 i915_gem_drain_workqueue(dev_priv);
4945
Chris Wilson8571a052018-06-06 15:54:41 +01004946 mutex_lock(&dev_priv->drm.struct_mutex);
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00004947 intel_uc_fini_hw(dev_priv);
Michał Winiarski61b5c152017-12-13 23:13:48 +01004948err_uc_init:
4949 intel_uc_fini(dev_priv);
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00004950err_pm:
4951 if (ret != -EIO) {
4952 intel_cleanup_gt_powersave(dev_priv);
4953 i915_gem_cleanup_engines(dev_priv);
4954 }
4955err_context:
4956 if (ret != -EIO)
4957 i915_gem_contexts_fini(dev_priv);
Chris Wilson51797492018-12-04 14:15:16 +00004958err_scratch:
4959 i915_gem_fini_scratch(dev_priv);
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00004960err_ggtt:
4961err_unlock:
Daniele Ceraolo Spurio3ceea6a2019-03-19 11:35:36 -07004962 intel_uncore_forcewake_put(&dev_priv->uncore, FORCEWAKE_ALL);
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00004963 mutex_unlock(&dev_priv->drm.struct_mutex);
4964
Michal Wajdeczkof7dc0152018-06-28 14:15:21 +00004965err_uc_misc:
Sagar Arun Kamble70deead2018-01-24 21:16:58 +05304966 intel_uc_fini_misc(dev_priv);
Sagar Arun Kambleda943b52018-01-10 18:24:16 +05304967
Chris Wilson1e345562019-01-28 10:23:56 +00004968 if (ret != -EIO) {
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00004969 i915_gem_cleanup_userptr(dev_priv);
Chris Wilson1e345562019-01-28 10:23:56 +00004970 i915_timelines_fini(dev_priv);
4971 }
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00004972
Chris Wilson60990322014-04-09 09:19:42 +01004973 if (ret == -EIO) {
Chris Wilson7ed43df2018-07-26 09:50:32 +01004974 mutex_lock(&dev_priv->drm.struct_mutex);
4975
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00004976 /*
4977 * Allow engine initialisation to fail by marking the GPU as
Chris Wilson60990322014-04-09 09:19:42 +01004978 * wedged. But we only want to do this where the GPU is angry,
4979 * for all other failure, such as an allocation failure, bail.
4980 */
Chris Wilsonc41166f2019-02-20 14:56:37 +00004981 if (!i915_reset_failed(dev_priv)) {
Chris Wilson51c18bf2018-06-09 12:10:58 +01004982 i915_load_error(dev_priv,
4983 "Failed to initialize GPU, declaring it wedged!\n");
Chris Wilson6f74b362017-10-15 15:37:25 +01004984 i915_gem_set_wedged(dev_priv);
4985 }
Chris Wilson7ed43df2018-07-26 09:50:32 +01004986
4987 /* Minimal basic recovery for KMS */
4988 ret = i915_ggtt_enable_hw(dev_priv);
4989 i915_gem_restore_gtt_mappings(dev_priv);
4990 i915_gem_restore_fences(dev_priv);
4991 intel_init_clock_gating(dev_priv);
4992
4993 mutex_unlock(&dev_priv->drm.struct_mutex);
Chris Wilson1070a422012-04-24 15:47:41 +01004994 }
4995
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00004996 i915_gem_drain_freed_objects(dev_priv);
Chris Wilson60990322014-04-09 09:19:42 +01004997 return ret;
Chris Wilson1070a422012-04-24 15:47:41 +01004998}
4999
Michal Wajdeczko8979187a2018-06-04 09:00:32 +00005000void i915_gem_fini(struct drm_i915_private *dev_priv)
5001{
5002 i915_gem_suspend_late(dev_priv);
Chris Wilson30b710842018-08-12 23:36:29 +01005003 intel_disable_gt_powersave(dev_priv);
Michal Wajdeczko8979187a2018-06-04 09:00:32 +00005004
5005 /* Flush any outstanding unpin_work. */
5006 i915_gem_drain_workqueue(dev_priv);
5007
5008 mutex_lock(&dev_priv->drm.struct_mutex);
5009 intel_uc_fini_hw(dev_priv);
5010 intel_uc_fini(dev_priv);
5011 i915_gem_cleanup_engines(dev_priv);
5012 i915_gem_contexts_fini(dev_priv);
Chris Wilson51797492018-12-04 14:15:16 +00005013 i915_gem_fini_scratch(dev_priv);
Michal Wajdeczko8979187a2018-06-04 09:00:32 +00005014 mutex_unlock(&dev_priv->drm.struct_mutex);
5015
Tvrtko Ursulin25d140f2018-12-03 13:33:19 +00005016 intel_wa_list_free(&dev_priv->gt_wa_list);
5017
Chris Wilson30b710842018-08-12 23:36:29 +01005018 intel_cleanup_gt_powersave(dev_priv);
5019
Michal Wajdeczko8979187a2018-06-04 09:00:32 +00005020 intel_uc_fini_misc(dev_priv);
5021 i915_gem_cleanup_userptr(dev_priv);
Chris Wilson1e345562019-01-28 10:23:56 +00005022 i915_timelines_fini(dev_priv);
Michal Wajdeczko8979187a2018-06-04 09:00:32 +00005023
5024 i915_gem_drain_freed_objects(dev_priv);
5025
5026 WARN_ON(!list_empty(&dev_priv->contexts.list));
5027}
5028
Chris Wilson24145512017-01-24 11:01:35 +00005029void i915_gem_init_mmio(struct drm_i915_private *i915)
5030{
5031 i915_gem_sanitize(i915);
5032}
5033
Zou Nan hai8187a2b2010-05-21 09:08:55 +08005034void
Tvrtko Ursulincb15d9f2016-12-01 14:16:39 +00005035i915_gem_cleanup_engines(struct drm_i915_private *dev_priv)
Zou Nan hai8187a2b2010-05-21 09:08:55 +08005036{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00005037 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05305038 enum intel_engine_id id;
Zou Nan hai8187a2b2010-05-21 09:08:55 +08005039
Akash Goel3b3f1652016-10-13 22:44:48 +05305040 for_each_engine(engine, dev_priv, id)
Tvrtko Ursulin117897f2016-03-16 11:00:40 +00005041 dev_priv->gt.cleanup_engine(engine);
Zou Nan hai8187a2b2010-05-21 09:08:55 +08005042}
5043
Eric Anholt673a3942008-07-30 12:06:12 -07005044void
Imre Deak40ae4e12016-03-16 14:54:03 +02005045i915_gem_load_init_fences(struct drm_i915_private *dev_priv)
5046{
Chris Wilson49ef5292016-08-18 17:17:00 +01005047 int i;
Imre Deak40ae4e12016-03-16 14:54:03 +02005048
Tvrtko Ursulinc56b89f2018-02-09 21:58:46 +00005049 if (INTEL_GEN(dev_priv) >= 7 && !IS_VALLEYVIEW(dev_priv) &&
Imre Deak40ae4e12016-03-16 14:54:03 +02005050 !IS_CHERRYVIEW(dev_priv))
5051 dev_priv->num_fence_regs = 32;
Tvrtko Ursulinc56b89f2018-02-09 21:58:46 +00005052 else if (INTEL_GEN(dev_priv) >= 4 ||
Jani Nikula73f67aa2016-12-07 22:48:09 +02005053 IS_I945G(dev_priv) || IS_I945GM(dev_priv) ||
5054 IS_G33(dev_priv) || IS_PINEVIEW(dev_priv))
Imre Deak40ae4e12016-03-16 14:54:03 +02005055 dev_priv->num_fence_regs = 16;
5056 else
5057 dev_priv->num_fence_regs = 8;
5058
Chris Wilsonc0336662016-05-06 15:40:21 +01005059 if (intel_vgpu_active(dev_priv))
Imre Deak40ae4e12016-03-16 14:54:03 +02005060 dev_priv->num_fence_regs =
5061 I915_READ(vgtif_reg(avail_rs.fence_num));
5062
5063 /* Initialize fence registers to zero */
Chris Wilson49ef5292016-08-18 17:17:00 +01005064 for (i = 0; i < dev_priv->num_fence_regs; i++) {
5065 struct drm_i915_fence_reg *fence = &dev_priv->fence_regs[i];
5066
5067 fence->i915 = dev_priv;
5068 fence->id = i;
5069 list_add_tail(&fence->link, &dev_priv->mm.fence_list);
5070 }
Tvrtko Ursulin4362f4f2016-11-16 08:55:33 +00005071 i915_gem_restore_fences(dev_priv);
Imre Deak40ae4e12016-03-16 14:54:03 +02005072
Tvrtko Ursulin4362f4f2016-11-16 08:55:33 +00005073 i915_gem_detect_bit_6_swizzle(dev_priv);
Imre Deak40ae4e12016-03-16 14:54:03 +02005074}
5075
Chris Wilson9c52d1c2017-11-10 23:24:47 +00005076static void i915_gem_init__mm(struct drm_i915_private *i915)
5077{
5078 spin_lock_init(&i915->mm.object_stat_lock);
5079 spin_lock_init(&i915->mm.obj_lock);
5080 spin_lock_init(&i915->mm.free_lock);
5081
5082 init_llist_head(&i915->mm.free_list);
5083
5084 INIT_LIST_HEAD(&i915->mm.unbound_list);
5085 INIT_LIST_HEAD(&i915->mm.bound_list);
5086 INIT_LIST_HEAD(&i915->mm.fence_list);
5087 INIT_LIST_HEAD(&i915->mm.userfault_list);
5088
5089 INIT_WORK(&i915->mm.free_work, __i915_gem_free_work);
5090}
5091
Michal Wajdeczkoa0de9082018-03-23 12:34:49 +00005092int i915_gem_init_early(struct drm_i915_private *dev_priv)
Eric Anholt673a3942008-07-30 12:06:12 -07005093{
Chris Wilson13f1bfd2019-02-28 10:20:34 +00005094 int err;
Chris Wilsond1b48c12017-08-16 09:52:08 +01005095
Chris Wilson643b4502018-04-30 14:15:03 +01005096 INIT_LIST_HEAD(&dev_priv->gt.active_rings);
Chris Wilson3365e222018-05-03 20:51:14 +01005097 INIT_LIST_HEAD(&dev_priv->gt.closed_vma);
Chris Wilson643b4502018-04-30 14:15:03 +01005098
Chris Wilson9c52d1c2017-11-10 23:24:47 +00005099 i915_gem_init__mm(dev_priv);
Chris Wilsonf2123812017-10-16 12:40:37 +01005100
Chris Wilson67d97da2016-07-04 08:08:31 +01005101 INIT_DELAYED_WORK(&dev_priv->gt.retire_work,
Eric Anholt673a3942008-07-30 12:06:12 -07005102 i915_gem_retire_work_handler);
Chris Wilson67d97da2016-07-04 08:08:31 +01005103 INIT_DELAYED_WORK(&dev_priv->gt.idle_work,
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005104 i915_gem_idle_work_handler);
Chris Wilson1f15b762016-07-01 17:23:14 +01005105 init_waitqueue_head(&dev_priv->gpu_error.wait_queue);
Daniel Vetter1f83fee2012-11-15 17:17:22 +01005106 init_waitqueue_head(&dev_priv->gpu_error.reset_queue);
Chris Wilson18bb2bc2019-01-14 21:04:01 +00005107 mutex_init(&dev_priv->gpu_error.wedge_mutex);
Chris Wilson2caffbf2019-02-08 15:37:03 +00005108 init_srcu_struct(&dev_priv->gpu_error.reset_backoff_srcu);
Chris Wilson31169712009-09-14 16:50:28 +01005109
Joonas Lahtinen6f633402016-09-01 14:58:21 +03005110 atomic_set(&dev_priv->mm.bsd_engine_dispatch_index, 0);
5111
Chris Wilsonb5add952016-08-04 16:32:36 +01005112 spin_lock_init(&dev_priv->fb_tracking.lock);
Chris Wilson73cb9702016-10-28 13:58:46 +01005113
Matthew Auld465c4032017-10-06 23:18:14 +01005114 err = i915_gemfs_init(dev_priv);
5115 if (err)
5116 DRM_NOTE("Unable to create a private tmpfs mount, hugepage support will be disabled(%d).\n", err);
5117
Chris Wilson73cb9702016-10-28 13:58:46 +01005118 return 0;
Eric Anholt673a3942008-07-30 12:06:12 -07005119}
Dave Airlie71acb5e2008-12-30 20:31:46 +10005120
Michal Wajdeczkoa0de9082018-03-23 12:34:49 +00005121void i915_gem_cleanup_early(struct drm_i915_private *dev_priv)
Imre Deakd64aa092016-01-19 15:26:29 +02005122{
Chris Wilsonc4d4c1c2017-02-10 16:35:23 +00005123 i915_gem_drain_freed_objects(dev_priv);
Chris Wilsonc9c704712018-02-19 22:06:31 +00005124 GEM_BUG_ON(!llist_empty(&dev_priv->mm.free_list));
5125 GEM_BUG_ON(atomic_read(&dev_priv->mm.free_count));
Chris Wilsonc4d4c1c2017-02-10 16:35:23 +00005126 WARN_ON(dev_priv->mm.object_count);
Matthew Auldea84aa72016-11-17 21:04:11 +00005127
Chris Wilson2caffbf2019-02-08 15:37:03 +00005128 cleanup_srcu_struct(&dev_priv->gpu_error.reset_backoff_srcu);
5129
Matthew Auld465c4032017-10-06 23:18:14 +01005130 i915_gemfs_fini(dev_priv);
Imre Deakd64aa092016-01-19 15:26:29 +02005131}
5132
Chris Wilson6a800ea2016-09-21 14:51:07 +01005133int i915_gem_freeze(struct drm_i915_private *dev_priv)
5134{
Chris Wilsond0aa3012017-04-07 11:25:49 +01005135 /* Discard all purgeable objects, let userspace recover those as
5136 * required after resuming.
5137 */
Chris Wilson6a800ea2016-09-21 14:51:07 +01005138 i915_gem_shrink_all(dev_priv);
Chris Wilson6a800ea2016-09-21 14:51:07 +01005139
Chris Wilson6a800ea2016-09-21 14:51:07 +01005140 return 0;
5141}
5142
Chris Wilson95c778d2018-06-01 15:41:25 +01005143int i915_gem_freeze_late(struct drm_i915_private *i915)
Chris Wilson461fb992016-05-14 07:26:33 +01005144{
5145 struct drm_i915_gem_object *obj;
Chris Wilson7aab2d52016-09-09 20:02:18 +01005146 struct list_head *phases[] = {
Chris Wilson95c778d2018-06-01 15:41:25 +01005147 &i915->mm.unbound_list,
5148 &i915->mm.bound_list,
Chris Wilson7aab2d52016-09-09 20:02:18 +01005149 NULL
Chris Wilson95c778d2018-06-01 15:41:25 +01005150 }, **phase;
Chris Wilson461fb992016-05-14 07:26:33 +01005151
Chris Wilson95c778d2018-06-01 15:41:25 +01005152 /*
5153 * Called just before we write the hibernation image.
Chris Wilson461fb992016-05-14 07:26:33 +01005154 *
5155 * We need to update the domain tracking to reflect that the CPU
5156 * will be accessing all the pages to create and restore from the
5157 * hibernation, and so upon restoration those pages will be in the
5158 * CPU domain.
5159 *
5160 * To make sure the hibernation image contains the latest state,
5161 * we update that state just before writing out the image.
Chris Wilson7aab2d52016-09-09 20:02:18 +01005162 *
5163 * To try and reduce the hibernation image, we manually shrink
Chris Wilsond0aa3012017-04-07 11:25:49 +01005164 * the objects as well, see i915_gem_freeze()
Chris Wilson461fb992016-05-14 07:26:33 +01005165 */
5166
Chris Wilson95c778d2018-06-01 15:41:25 +01005167 i915_gem_shrink(i915, -1UL, NULL, I915_SHRINK_UNBOUND);
5168 i915_gem_drain_freed_objects(i915);
Chris Wilson461fb992016-05-14 07:26:33 +01005169
Chris Wilson95c778d2018-06-01 15:41:25 +01005170 mutex_lock(&i915->drm.struct_mutex);
5171 for (phase = phases; *phase; phase++) {
5172 list_for_each_entry(obj, *phase, mm.link)
5173 WARN_ON(i915_gem_object_set_to_cpu_domain(obj, true));
Chris Wilson461fb992016-05-14 07:26:33 +01005174 }
Chris Wilson95c778d2018-06-01 15:41:25 +01005175 mutex_unlock(&i915->drm.struct_mutex);
Chris Wilson461fb992016-05-14 07:26:33 +01005176
5177 return 0;
5178}
5179
Chris Wilsonf787a5f2010-09-24 16:02:42 +01005180void i915_gem_release(struct drm_device *dev, struct drm_file *file)
Eric Anholtb9624422009-06-03 07:27:35 +00005181{
Chris Wilsonf787a5f2010-09-24 16:02:42 +01005182 struct drm_i915_file_private *file_priv = file->driver_priv;
Chris Wilsone61e0f52018-02-21 09:56:36 +00005183 struct i915_request *request;
Eric Anholtb9624422009-06-03 07:27:35 +00005184
5185 /* Clean up our request list when the client is going away, so that
5186 * later retire_requests won't dereference our soon-to-be-gone
5187 * file_priv.
5188 */
Chris Wilson1c255952010-09-26 11:03:27 +01005189 spin_lock(&file_priv->mm.lock);
Chris Wilsonc8659ef2017-03-02 12:25:25 +00005190 list_for_each_entry(request, &file_priv->mm.request_list, client_link)
Chris Wilsonf787a5f2010-09-24 16:02:42 +01005191 request->file_priv = NULL;
Chris Wilson1c255952010-09-26 11:03:27 +01005192 spin_unlock(&file_priv->mm.lock);
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005193}
5194
Chris Wilson829a0af2017-06-20 12:05:45 +01005195int i915_gem_open(struct drm_i915_private *i915, struct drm_file *file)
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005196{
5197 struct drm_i915_file_private *file_priv;
Ben Widawskye422b882013-12-06 14:10:58 -08005198 int ret;
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005199
Chris Wilsonc4c29d72016-11-09 10:45:07 +00005200 DRM_DEBUG("\n");
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005201
5202 file_priv = kzalloc(sizeof(*file_priv), GFP_KERNEL);
5203 if (!file_priv)
5204 return -ENOMEM;
5205
5206 file->driver_priv = file_priv;
Chris Wilson829a0af2017-06-20 12:05:45 +01005207 file_priv->dev_priv = i915;
Chris Wilsonab0e7ff2014-02-25 17:11:24 +02005208 file_priv->file = file;
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005209
5210 spin_lock_init(&file_priv->mm.lock);
5211 INIT_LIST_HEAD(&file_priv->mm.request_list);
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005212
Chris Wilsonc80ff162016-07-27 09:07:27 +01005213 file_priv->bsd_engine = -1;
Mika Kuoppala14921f32018-06-15 13:44:29 +03005214 file_priv->hang_timestamp = jiffies;
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00005215
Chris Wilson829a0af2017-06-20 12:05:45 +01005216 ret = i915_gem_context_open(i915, file);
Ben Widawskye422b882013-12-06 14:10:58 -08005217 if (ret)
5218 kfree(file_priv);
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005219
Ben Widawskye422b882013-12-06 14:10:58 -08005220 return ret;
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005221}
5222
Daniel Vetterb680c372014-09-19 18:27:27 +02005223/**
5224 * i915_gem_track_fb - update frontbuffer tracking
Geliang Tangd9072a32015-09-15 05:58:44 -07005225 * @old: current GEM buffer for the frontbuffer slots
5226 * @new: new GEM buffer for the frontbuffer slots
5227 * @frontbuffer_bits: bitmask of frontbuffer slots
Daniel Vetterb680c372014-09-19 18:27:27 +02005228 *
5229 * This updates the frontbuffer tracking bits @frontbuffer_bits by clearing them
5230 * from @old and setting them in @new. Both @old and @new can be NULL.
5231 */
Daniel Vettera071fa02014-06-18 23:28:09 +02005232void i915_gem_track_fb(struct drm_i915_gem_object *old,
5233 struct drm_i915_gem_object *new,
5234 unsigned frontbuffer_bits)
5235{
Chris Wilsonfaf5bf02016-08-04 16:32:37 +01005236 /* Control of individual bits within the mask are guarded by
5237 * the owning plane->mutex, i.e. we can never see concurrent
5238 * manipulation of individual bits. But since the bitfield as a whole
5239 * is updated using RMW, we need to use atomics in order to update
5240 * the bits.
5241 */
5242 BUILD_BUG_ON(INTEL_FRONTBUFFER_BITS_PER_PIPE * I915_MAX_PIPES >
Chris Wilson74f6e182018-09-26 11:47:07 +01005243 BITS_PER_TYPE(atomic_t));
Chris Wilsonfaf5bf02016-08-04 16:32:37 +01005244
Daniel Vettera071fa02014-06-18 23:28:09 +02005245 if (old) {
Chris Wilsonfaf5bf02016-08-04 16:32:37 +01005246 WARN_ON(!(atomic_read(&old->frontbuffer_bits) & frontbuffer_bits));
5247 atomic_andnot(frontbuffer_bits, &old->frontbuffer_bits);
Daniel Vettera071fa02014-06-18 23:28:09 +02005248 }
5249
5250 if (new) {
Chris Wilsonfaf5bf02016-08-04 16:32:37 +01005251 WARN_ON(atomic_read(&new->frontbuffer_bits) & frontbuffer_bits);
5252 atomic_or(frontbuffer_bits, &new->frontbuffer_bits);
Daniel Vettera071fa02014-06-18 23:28:09 +02005253 }
5254}
5255
Dave Gordonea702992015-07-09 19:29:02 +01005256/* Allocate a new GEM object and fill it with the supplied data */
5257struct drm_i915_gem_object *
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +00005258i915_gem_object_create_from_data(struct drm_i915_private *dev_priv,
Dave Gordonea702992015-07-09 19:29:02 +01005259 const void *data, size_t size)
5260{
5261 struct drm_i915_gem_object *obj;
Chris Wilsonbe062fa2017-03-17 19:46:48 +00005262 struct file *file;
5263 size_t offset;
5264 int err;
Dave Gordonea702992015-07-09 19:29:02 +01005265
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +00005266 obj = i915_gem_object_create(dev_priv, round_up(size, PAGE_SIZE));
Chris Wilsonfe3db792016-04-25 13:32:13 +01005267 if (IS_ERR(obj))
Dave Gordonea702992015-07-09 19:29:02 +01005268 return obj;
5269
Christian Königc0a51fd2018-02-16 13:43:38 +01005270 GEM_BUG_ON(obj->write_domain != I915_GEM_DOMAIN_CPU);
Dave Gordonea702992015-07-09 19:29:02 +01005271
Chris Wilsonbe062fa2017-03-17 19:46:48 +00005272 file = obj->base.filp;
5273 offset = 0;
5274 do {
5275 unsigned int len = min_t(typeof(size), size, PAGE_SIZE);
5276 struct page *page;
5277 void *pgdata, *vaddr;
Dave Gordonea702992015-07-09 19:29:02 +01005278
Chris Wilsonbe062fa2017-03-17 19:46:48 +00005279 err = pagecache_write_begin(file, file->f_mapping,
5280 offset, len, 0,
5281 &page, &pgdata);
5282 if (err < 0)
5283 goto fail;
Dave Gordonea702992015-07-09 19:29:02 +01005284
Chris Wilsonbe062fa2017-03-17 19:46:48 +00005285 vaddr = kmap(page);
5286 memcpy(vaddr, data, len);
5287 kunmap(page);
5288
5289 err = pagecache_write_end(file, file->f_mapping,
5290 offset, len, len,
5291 page, pgdata);
5292 if (err < 0)
5293 goto fail;
5294
5295 size -= len;
5296 data += len;
5297 offset += len;
5298 } while (size);
Dave Gordonea702992015-07-09 19:29:02 +01005299
5300 return obj;
5301
5302fail:
Chris Wilsonf8c417c2016-07-20 13:31:53 +01005303 i915_gem_object_put(obj);
Chris Wilsonbe062fa2017-03-17 19:46:48 +00005304 return ERR_PTR(err);
Dave Gordonea702992015-07-09 19:29:02 +01005305}
Chris Wilson96d77632016-10-28 13:58:33 +01005306
5307struct scatterlist *
5308i915_gem_object_get_sg(struct drm_i915_gem_object *obj,
5309 unsigned int n,
5310 unsigned int *offset)
5311{
Chris Wilsona4f5ea62016-10-28 13:58:35 +01005312 struct i915_gem_object_page_iter *iter = &obj->mm.get_page;
Chris Wilson96d77632016-10-28 13:58:33 +01005313 struct scatterlist *sg;
5314 unsigned int idx, count;
5315
5316 might_sleep();
5317 GEM_BUG_ON(n >= obj->base.size >> PAGE_SHIFT);
Chris Wilsona4f5ea62016-10-28 13:58:35 +01005318 GEM_BUG_ON(!i915_gem_object_has_pinned_pages(obj));
Chris Wilson96d77632016-10-28 13:58:33 +01005319
5320 /* As we iterate forward through the sg, we record each entry in a
5321 * radixtree for quick repeated (backwards) lookups. If we have seen
5322 * this index previously, we will have an entry for it.
5323 *
5324 * Initial lookup is O(N), but this is amortized to O(1) for
5325 * sequential page access (where each new request is consecutive
5326 * to the previous one). Repeated lookups are O(lg(obj->base.size)),
5327 * i.e. O(1) with a large constant!
5328 */
5329 if (n < READ_ONCE(iter->sg_idx))
5330 goto lookup;
5331
5332 mutex_lock(&iter->lock);
5333
5334 /* We prefer to reuse the last sg so that repeated lookup of this
5335 * (or the subsequent) sg are fast - comparing against the last
5336 * sg is faster than going through the radixtree.
5337 */
5338
5339 sg = iter->sg_pos;
5340 idx = iter->sg_idx;
5341 count = __sg_page_count(sg);
5342
5343 while (idx + count <= n) {
Matthew Wilcox3159f942017-11-03 13:30:42 -04005344 void *entry;
5345 unsigned long i;
Chris Wilson96d77632016-10-28 13:58:33 +01005346 int ret;
5347
5348 /* If we cannot allocate and insert this entry, or the
5349 * individual pages from this range, cancel updating the
5350 * sg_idx so that on this lookup we are forced to linearly
5351 * scan onwards, but on future lookups we will try the
5352 * insertion again (in which case we need to be careful of
5353 * the error return reporting that we have already inserted
5354 * this index).
5355 */
5356 ret = radix_tree_insert(&iter->radix, idx, sg);
5357 if (ret && ret != -EEXIST)
5358 goto scan;
5359
Matthew Wilcox3159f942017-11-03 13:30:42 -04005360 entry = xa_mk_value(idx);
Chris Wilson96d77632016-10-28 13:58:33 +01005361 for (i = 1; i < count; i++) {
Matthew Wilcox3159f942017-11-03 13:30:42 -04005362 ret = radix_tree_insert(&iter->radix, idx + i, entry);
Chris Wilson96d77632016-10-28 13:58:33 +01005363 if (ret && ret != -EEXIST)
5364 goto scan;
5365 }
5366
5367 idx += count;
5368 sg = ____sg_next(sg);
5369 count = __sg_page_count(sg);
5370 }
5371
5372scan:
5373 iter->sg_pos = sg;
5374 iter->sg_idx = idx;
5375
5376 mutex_unlock(&iter->lock);
5377
5378 if (unlikely(n < idx)) /* insertion completed by another thread */
5379 goto lookup;
5380
5381 /* In case we failed to insert the entry into the radixtree, we need
5382 * to look beyond the current sg.
5383 */
5384 while (idx + count <= n) {
5385 idx += count;
5386 sg = ____sg_next(sg);
5387 count = __sg_page_count(sg);
5388 }
5389
5390 *offset = n - idx;
5391 return sg;
5392
5393lookup:
5394 rcu_read_lock();
5395
5396 sg = radix_tree_lookup(&iter->radix, n);
5397 GEM_BUG_ON(!sg);
5398
5399 /* If this index is in the middle of multi-page sg entry,
Matthew Wilcox3159f942017-11-03 13:30:42 -04005400 * the radix tree will contain a value entry that points
Chris Wilson96d77632016-10-28 13:58:33 +01005401 * to the start of that range. We will return the pointer to
5402 * the base page and the offset of this page within the
5403 * sg entry's range.
5404 */
5405 *offset = 0;
Matthew Wilcox3159f942017-11-03 13:30:42 -04005406 if (unlikely(xa_is_value(sg))) {
5407 unsigned long base = xa_to_value(sg);
Chris Wilson96d77632016-10-28 13:58:33 +01005408
5409 sg = radix_tree_lookup(&iter->radix, base);
5410 GEM_BUG_ON(!sg);
5411
5412 *offset = n - base;
5413 }
5414
5415 rcu_read_unlock();
5416
5417 return sg;
5418}
5419
5420struct page *
5421i915_gem_object_get_page(struct drm_i915_gem_object *obj, unsigned int n)
5422{
5423 struct scatterlist *sg;
5424 unsigned int offset;
5425
5426 GEM_BUG_ON(!i915_gem_object_has_struct_page(obj));
5427
5428 sg = i915_gem_object_get_sg(obj, n, &offset);
5429 return nth_page(sg_page(sg), offset);
5430}
5431
5432/* Like i915_gem_object_get_page(), but mark the returned page dirty */
5433struct page *
5434i915_gem_object_get_dirty_page(struct drm_i915_gem_object *obj,
5435 unsigned int n)
5436{
5437 struct page *page;
5438
5439 page = i915_gem_object_get_page(obj, n);
Chris Wilsona4f5ea62016-10-28 13:58:35 +01005440 if (!obj->mm.dirty)
Chris Wilson96d77632016-10-28 13:58:33 +01005441 set_page_dirty(page);
5442
5443 return page;
5444}
5445
5446dma_addr_t
5447i915_gem_object_get_dma_address(struct drm_i915_gem_object *obj,
5448 unsigned long n)
5449{
5450 struct scatterlist *sg;
5451 unsigned int offset;
5452
5453 sg = i915_gem_object_get_sg(obj, n, &offset);
5454 return sg_dma_address(sg) + (offset << PAGE_SHIFT);
5455}
Chris Wilson935a2f72017-02-13 17:15:13 +00005456
Chris Wilson8eeb7902017-07-26 19:16:01 +01005457int i915_gem_object_attach_phys(struct drm_i915_gem_object *obj, int align)
5458{
5459 struct sg_table *pages;
5460 int err;
5461
5462 if (align > obj->base.size)
5463 return -EINVAL;
5464
5465 if (obj->ops == &i915_gem_phys_ops)
5466 return 0;
5467
5468 if (obj->ops != &i915_gem_object_ops)
5469 return -EINVAL;
5470
5471 err = i915_gem_object_unbind(obj);
5472 if (err)
5473 return err;
5474
5475 mutex_lock(&obj->mm.lock);
5476
5477 if (obj->mm.madv != I915_MADV_WILLNEED) {
5478 err = -EFAULT;
5479 goto err_unlock;
5480 }
5481
5482 if (obj->mm.quirked) {
5483 err = -EFAULT;
5484 goto err_unlock;
5485 }
5486
5487 if (obj->mm.mapping) {
5488 err = -EBUSY;
5489 goto err_unlock;
5490 }
5491
Chris Wilsonacd1c1e2018-06-11 08:55:32 +01005492 pages = __i915_gem_object_unset_pages(obj);
Chris Wilsonf2123812017-10-16 12:40:37 +01005493
Chris Wilson8eeb7902017-07-26 19:16:01 +01005494 obj->ops = &i915_gem_phys_ops;
5495
Chris Wilson8fb6a5d2017-07-26 19:16:02 +01005496 err = ____i915_gem_object_get_pages(obj);
Chris Wilson8eeb7902017-07-26 19:16:01 +01005497 if (err)
5498 goto err_xfer;
5499
5500 /* Perma-pin (until release) the physical set of pages */
5501 __i915_gem_object_pin_pages(obj);
5502
5503 if (!IS_ERR_OR_NULL(pages))
5504 i915_gem_object_ops.put_pages(obj, pages);
5505 mutex_unlock(&obj->mm.lock);
5506 return 0;
5507
5508err_xfer:
5509 obj->ops = &i915_gem_object_ops;
Chris Wilsonacd1c1e2018-06-11 08:55:32 +01005510 if (!IS_ERR_OR_NULL(pages)) {
5511 unsigned int sg_page_sizes = i915_sg_page_sizes(pages->sgl);
5512
5513 __i915_gem_object_set_pages(obj, pages, sg_page_sizes);
5514 }
Chris Wilson8eeb7902017-07-26 19:16:01 +01005515err_unlock:
5516 mutex_unlock(&obj->mm.lock);
5517 return err;
5518}
5519
Chris Wilson935a2f72017-02-13 17:15:13 +00005520#if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
5521#include "selftests/scatterlist.c"
Chris Wilson66d9cb52017-02-13 17:15:17 +00005522#include "selftests/mock_gem_device.c"
Chris Wilson44653982017-02-13 17:15:20 +00005523#include "selftests/huge_gem_object.c"
Matthew Auld40498662017-10-06 23:18:29 +01005524#include "selftests/huge_pages.c"
Chris Wilson8335fd62017-02-13 17:15:28 +00005525#include "selftests/i915_gem_object.c"
Chris Wilson17059452017-02-13 17:15:32 +00005526#include "selftests/i915_gem_coherency.c"
Chris Wilson3f51b7e12018-08-30 14:48:06 +01005527#include "selftests/i915_gem.c"
Chris Wilson935a2f72017-02-13 17:15:13 +00005528#endif