blob: b7086c8d472639b2f8de608d37a501b8643c2f19 [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 Wilson6a2c4232014-11-04 04:51:40 -0800311static void
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,
Jani Nikula739f3ab2019-01-16 11:15:19 +0200625 u64 size,
626 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 int ret;
630 u32 handle;
Eric Anholt673a3942008-07-30 12:06:12 -0700631
Dave Airlieff72145b2011-02-07 12:16:14 +1000632 size = roundup(size, PAGE_SIZE);
Chris Wilson8ffc0242011-09-14 14:14:28 +0200633 if (size == 0)
634 return -EINVAL;
Eric Anholt673a3942008-07-30 12:06:12 -0700635
636 /* Allocate the new object */
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +0000637 obj = i915_gem_object_create(dev_priv, size);
Chris Wilsonfe3db792016-04-25 13:32:13 +0100638 if (IS_ERR(obj))
639 return PTR_ERR(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700640
Chris Wilson05394f32010-11-08 19:18:58 +0000641 ret = drm_gem_handle_create(file, &obj->base, &handle);
Chris Wilson202f2fe2010-10-14 13:20:40 +0100642 /* drop reference from allocate - handle holds it now */
Chris Wilsonf0cd5182016-10-28 13:58:43 +0100643 i915_gem_object_put(obj);
Daniel Vetterd861e332013-07-24 23:25:03 +0200644 if (ret)
645 return ret;
Chris Wilson202f2fe2010-10-14 13:20:40 +0100646
Dave Airlieff72145b2011-02-07 12:16:14 +1000647 *handle_p = handle;
Eric Anholt673a3942008-07-30 12:06:12 -0700648 return 0;
649}
650
Dave Airlieff72145b2011-02-07 12:16:14 +1000651int
652i915_gem_dumb_create(struct drm_file *file,
653 struct drm_device *dev,
654 struct drm_mode_create_dumb *args)
655{
656 /* have to work out size/pitch and return them */
Paulo Zanonide45eaf2013-10-18 18:48:24 -0300657 args->pitch = ALIGN(args->width * DIV_ROUND_UP(args->bpp, 8), 64);
Dave Airlieff72145b2011-02-07 12:16:14 +1000658 args->size = args->pitch * args->height;
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +0000659 return i915_gem_create(file, to_i915(dev),
Dave Airlieda6b51d2014-12-24 13:11:17 +1000660 args->size, &args->handle);
Dave Airlieff72145b2011-02-07 12:16:14 +1000661}
662
Chris Wilsone27ab732017-06-15 13:38:49 +0100663static bool gpu_write_needs_clflush(struct drm_i915_gem_object *obj)
664{
665 return !(obj->cache_level == I915_CACHE_NONE ||
666 obj->cache_level == I915_CACHE_WT);
667}
668
Dave Airlieff72145b2011-02-07 12:16:14 +1000669/**
670 * Creates a new mm object and returns a handle to it.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +0100671 * @dev: drm device pointer
672 * @data: ioctl data blob
673 * @file: drm file pointer
Dave Airlieff72145b2011-02-07 12:16:14 +1000674 */
675int
676i915_gem_create_ioctl(struct drm_device *dev, void *data,
677 struct drm_file *file)
678{
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +0000679 struct drm_i915_private *dev_priv = to_i915(dev);
Dave Airlieff72145b2011-02-07 12:16:14 +1000680 struct drm_i915_gem_create *args = data;
Daniel Vetter63ed2cb2012-04-23 16:50:50 +0200681
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +0000682 i915_gem_flush_free_objects(dev_priv);
Chris Wilsonfbbd37b2016-10-28 13:58:42 +0100683
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +0000684 return i915_gem_create(file, dev_priv,
Dave Airlieda6b51d2014-12-24 13:11:17 +1000685 args->size, &args->handle);
Dave Airlieff72145b2011-02-07 12:16:14 +1000686}
687
Chris Wilsonef749212017-04-12 12:01:10 +0100688static inline enum fb_op_origin
689fb_write_origin(struct drm_i915_gem_object *obj, unsigned int domain)
690{
691 return (domain == I915_GEM_DOMAIN_GTT ?
692 obj->frontbuffer_ggtt_origin : ORIGIN_CPU);
693}
694
Chris Wilson7125397b2017-12-06 12:49:14 +0000695void i915_gem_flush_ggtt_writes(struct drm_i915_private *dev_priv)
Chris Wilsonef749212017-04-12 12:01:10 +0100696{
Chris Wilson538ef962019-01-14 14:21:18 +0000697 intel_wakeref_t wakeref;
698
Chris Wilson7125397b2017-12-06 12:49:14 +0000699 /*
700 * No actual flushing is required for the GTT write domain for reads
701 * from the GTT domain. Writes to it "immediately" go to main memory
702 * as far as we know, so there's no chipset flush. It also doesn't
703 * land in the GPU render cache.
Chris Wilsonef749212017-04-12 12:01:10 +0100704 *
705 * However, we do have to enforce the order so that all writes through
706 * the GTT land before any writes to the device, such as updates to
707 * the GATT itself.
708 *
709 * We also have to wait a bit for the writes to land from the GTT.
710 * An uncached read (i.e. mmio) seems to be ideal for the round-trip
711 * timing. This issue has only been observed when switching quickly
712 * between GTT writes and CPU reads from inside the kernel on recent hw,
713 * and it appears to only affect discrete GTT blocks (i.e. on LLC
Chris Wilson7125397b2017-12-06 12:49:14 +0000714 * system agents we cannot reproduce this behaviour, until Cannonlake
715 * that was!).
Chris Wilsonef749212017-04-12 12:01:10 +0100716 */
Chris Wilson7125397b2017-12-06 12:49:14 +0000717
Chris Wilson900ccf32018-07-20 11:19:10 +0100718 wmb();
719
720 if (INTEL_INFO(dev_priv)->has_coherent_ggtt)
721 return;
722
Chris Wilsona8bd3b82018-07-17 10:26:55 +0100723 i915_gem_chipset_flush(dev_priv);
Chris Wilsonef749212017-04-12 12:01:10 +0100724
Chris Wilsond4225a52019-01-14 14:21:23 +0000725 with_intel_runtime_pm(dev_priv, wakeref) {
726 spin_lock_irq(&dev_priv->uncore.lock);
Chris Wilson7125397b2017-12-06 12:49:14 +0000727
Chris Wilsond4225a52019-01-14 14:21:23 +0000728 POSTING_READ_FW(RING_HEAD(RENDER_RING_BASE));
Chris Wilson7125397b2017-12-06 12:49:14 +0000729
Chris Wilsond4225a52019-01-14 14:21:23 +0000730 spin_unlock_irq(&dev_priv->uncore.lock);
731 }
Chris Wilson7125397b2017-12-06 12:49:14 +0000732}
733
734static void
735flush_write_domain(struct drm_i915_gem_object *obj, unsigned int flush_domains)
736{
737 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
738 struct i915_vma *vma;
739
Christian Königc0a51fd2018-02-16 13:43:38 +0100740 if (!(obj->write_domain & flush_domains))
Chris Wilson7125397b2017-12-06 12:49:14 +0000741 return;
742
Christian Königc0a51fd2018-02-16 13:43:38 +0100743 switch (obj->write_domain) {
Chris Wilsonef749212017-04-12 12:01:10 +0100744 case I915_GEM_DOMAIN_GTT:
Chris Wilson7125397b2017-12-06 12:49:14 +0000745 i915_gem_flush_ggtt_writes(dev_priv);
Chris Wilsonef749212017-04-12 12:01:10 +0100746
747 intel_fb_obj_flush(obj,
748 fb_write_origin(obj, I915_GEM_DOMAIN_GTT));
Chris Wilson7125397b2017-12-06 12:49:14 +0000749
Chris Wilsone2189dd2017-12-07 21:14:07 +0000750 for_each_ggtt_vma(vma, obj) {
Chris Wilson7125397b2017-12-06 12:49:14 +0000751 if (vma->iomap)
752 continue;
753
754 i915_vma_unset_ggtt_write(vma);
755 }
Chris Wilsonef749212017-04-12 12:01:10 +0100756 break;
757
Chris Wilsonadd00e62018-07-06 12:54:02 +0100758 case I915_GEM_DOMAIN_WC:
759 wmb();
760 break;
761
Chris Wilsonef749212017-04-12 12:01:10 +0100762 case I915_GEM_DOMAIN_CPU:
763 i915_gem_clflush_object(obj, I915_CLFLUSH_SYNC);
764 break;
Chris Wilsone27ab732017-06-15 13:38:49 +0100765
766 case I915_GEM_DOMAIN_RENDER:
767 if (gpu_write_needs_clflush(obj))
768 obj->cache_dirty = true;
769 break;
Chris Wilsonef749212017-04-12 12:01:10 +0100770 }
771
Christian Königc0a51fd2018-02-16 13:43:38 +0100772 obj->write_domain = 0;
Chris Wilsonef749212017-04-12 12:01:10 +0100773}
774
Brad Volkin4c914c02014-02-18 10:15:45 -0800775/*
776 * Pins the specified object's pages and synchronizes the object with
777 * GPU accesses. Sets needs_clflush to non-zero if the caller should
778 * flush the object from the CPU cache.
779 */
780int i915_gem_obj_prepare_shmem_read(struct drm_i915_gem_object *obj,
Chris Wilson43394c72016-08-18 17:16:47 +0100781 unsigned int *needs_clflush)
Brad Volkin4c914c02014-02-18 10:15:45 -0800782{
783 int ret;
784
Chris Wilsone95433c2016-10-28 13:58:27 +0100785 lockdep_assert_held(&obj->base.dev->struct_mutex);
Brad Volkin4c914c02014-02-18 10:15:45 -0800786
Chris Wilsone95433c2016-10-28 13:58:27 +0100787 *needs_clflush = 0;
Chris Wilson43394c72016-08-18 17:16:47 +0100788 if (!i915_gem_object_has_struct_page(obj))
789 return -ENODEV;
Brad Volkin4c914c02014-02-18 10:15:45 -0800790
Chris Wilsone95433c2016-10-28 13:58:27 +0100791 ret = i915_gem_object_wait(obj,
792 I915_WAIT_INTERRUPTIBLE |
793 I915_WAIT_LOCKED,
Chris Wilson62eb3c22019-02-13 09:25:04 +0000794 MAX_SCHEDULE_TIMEOUT);
Chris Wilsonc13d87e2016-07-20 09:21:15 +0100795 if (ret)
796 return ret;
797
Chris Wilsona4f5ea62016-10-28 13:58:35 +0100798 ret = i915_gem_object_pin_pages(obj);
Chris Wilson97649512016-08-18 17:16:50 +0100799 if (ret)
800 return ret;
801
Chris Wilsonb8f55be2017-08-11 12:11:16 +0100802 if (obj->cache_coherent & I915_BO_CACHE_COHERENT_FOR_READ ||
803 !static_cpu_has(X86_FEATURE_CLFLUSH)) {
Chris Wilson7f5f95d2017-03-10 00:09:42 +0000804 ret = i915_gem_object_set_to_cpu_domain(obj, false);
805 if (ret)
806 goto err_unpin;
807 else
808 goto out;
809 }
810
Chris Wilsonef749212017-04-12 12:01:10 +0100811 flush_write_domain(obj, ~I915_GEM_DOMAIN_CPU);
Chris Wilsona314d5c2016-08-18 17:16:48 +0100812
Chris Wilson43394c72016-08-18 17:16:47 +0100813 /* If we're not in the cpu read domain, set ourself into the gtt
814 * read domain and manually flush cachelines (if required). This
815 * optimizes for the case when the gpu will dirty the data
816 * anyway again before the next pread happens.
817 */
Chris Wilsone27ab732017-06-15 13:38:49 +0100818 if (!obj->cache_dirty &&
Christian Königc0a51fd2018-02-16 13:43:38 +0100819 !(obj->read_domains & I915_GEM_DOMAIN_CPU))
Chris Wilson7f5f95d2017-03-10 00:09:42 +0000820 *needs_clflush = CLFLUSH_BEFORE;
Brad Volkin4c914c02014-02-18 10:15:45 -0800821
Chris Wilson7f5f95d2017-03-10 00:09:42 +0000822out:
Chris Wilson97649512016-08-18 17:16:50 +0100823 /* return with the pages pinned */
Chris Wilson43394c72016-08-18 17:16:47 +0100824 return 0;
Chris Wilson97649512016-08-18 17:16:50 +0100825
826err_unpin:
827 i915_gem_object_unpin_pages(obj);
828 return ret;
Chris Wilson43394c72016-08-18 17:16:47 +0100829}
830
831int i915_gem_obj_prepare_shmem_write(struct drm_i915_gem_object *obj,
832 unsigned int *needs_clflush)
833{
834 int ret;
835
Chris Wilsone95433c2016-10-28 13:58:27 +0100836 lockdep_assert_held(&obj->base.dev->struct_mutex);
837
Chris Wilson43394c72016-08-18 17:16:47 +0100838 *needs_clflush = 0;
839 if (!i915_gem_object_has_struct_page(obj))
840 return -ENODEV;
841
Chris Wilsone95433c2016-10-28 13:58:27 +0100842 ret = i915_gem_object_wait(obj,
843 I915_WAIT_INTERRUPTIBLE |
844 I915_WAIT_LOCKED |
845 I915_WAIT_ALL,
Chris Wilson62eb3c22019-02-13 09:25:04 +0000846 MAX_SCHEDULE_TIMEOUT);
Chris Wilson43394c72016-08-18 17:16:47 +0100847 if (ret)
848 return ret;
849
Chris Wilsona4f5ea62016-10-28 13:58:35 +0100850 ret = i915_gem_object_pin_pages(obj);
Chris Wilson97649512016-08-18 17:16:50 +0100851 if (ret)
852 return ret;
853
Chris Wilsonb8f55be2017-08-11 12:11:16 +0100854 if (obj->cache_coherent & I915_BO_CACHE_COHERENT_FOR_WRITE ||
855 !static_cpu_has(X86_FEATURE_CLFLUSH)) {
Chris Wilson7f5f95d2017-03-10 00:09:42 +0000856 ret = i915_gem_object_set_to_cpu_domain(obj, true);
857 if (ret)
858 goto err_unpin;
859 else
860 goto out;
861 }
862
Chris Wilsonef749212017-04-12 12:01:10 +0100863 flush_write_domain(obj, ~I915_GEM_DOMAIN_CPU);
Chris Wilsona314d5c2016-08-18 17:16:48 +0100864
Chris Wilson43394c72016-08-18 17:16:47 +0100865 /* If we're not in the cpu write domain, set ourself into the
866 * gtt write domain and manually flush cachelines (as required).
867 * This optimizes for the case when the gpu will use the data
868 * right away and we therefore have to clflush anyway.
869 */
Chris Wilsone27ab732017-06-15 13:38:49 +0100870 if (!obj->cache_dirty) {
Chris Wilson7f5f95d2017-03-10 00:09:42 +0000871 *needs_clflush |= CLFLUSH_AFTER;
Chris Wilson43394c72016-08-18 17:16:47 +0100872
Chris Wilsone27ab732017-06-15 13:38:49 +0100873 /*
874 * Same trick applies to invalidate partially written
875 * cachelines read before writing.
876 */
Christian Königc0a51fd2018-02-16 13:43:38 +0100877 if (!(obj->read_domains & I915_GEM_DOMAIN_CPU))
Chris Wilsone27ab732017-06-15 13:38:49 +0100878 *needs_clflush |= CLFLUSH_BEFORE;
879 }
Chris Wilson43394c72016-08-18 17:16:47 +0100880
Chris Wilson7f5f95d2017-03-10 00:09:42 +0000881out:
Chris Wilson43394c72016-08-18 17:16:47 +0100882 intel_fb_obj_invalidate(obj, ORIGIN_CPU);
Chris Wilsona4f5ea62016-10-28 13:58:35 +0100883 obj->mm.dirty = true;
Chris Wilson97649512016-08-18 17:16:50 +0100884 /* return with the pages pinned */
Chris Wilson43394c72016-08-18 17:16:47 +0100885 return 0;
Chris Wilson97649512016-08-18 17:16:50 +0100886
887err_unpin:
888 i915_gem_object_unpin_pages(obj);
889 return ret;
Brad Volkin4c914c02014-02-18 10:15:45 -0800890}
891
Daniel Vetterd174bd62012-03-25 19:47:40 +0200892static int
Chris Wilsonb9d126e2019-01-05 12:07:58 +0000893shmem_pread(struct page *page, int offset, int len, char __user *user_data,
894 bool needs_clflush)
Daniel Vetterd174bd62012-03-25 19:47:40 +0200895{
896 char *vaddr;
897 int ret;
898
899 vaddr = kmap(page);
Daniel Vetterd174bd62012-03-25 19:47:40 +0200900
Chris Wilsonb9d126e2019-01-05 12:07:58 +0000901 if (needs_clflush)
902 drm_clflush_virt_range(vaddr + offset, len);
903
904 ret = __copy_to_user(user_data, vaddr + offset, len);
905
Daniel Vetterd174bd62012-03-25 19:47:40 +0200906 kunmap(page);
907
Chris Wilsonb9d126e2019-01-05 12:07:58 +0000908 return ret ? -EFAULT : 0;
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100909}
910
911static int
912i915_gem_shmem_pread(struct drm_i915_gem_object *obj,
913 struct drm_i915_gem_pread *args)
914{
915 char __user *user_data;
916 u64 remain;
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100917 unsigned int needs_clflush;
918 unsigned int idx, offset;
919 int ret;
920
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100921 ret = mutex_lock_interruptible(&obj->base.dev->struct_mutex);
922 if (ret)
923 return ret;
924
925 ret = i915_gem_obj_prepare_shmem_read(obj, &needs_clflush);
926 mutex_unlock(&obj->base.dev->struct_mutex);
927 if (ret)
928 return ret;
929
930 remain = args->size;
931 user_data = u64_to_user_ptr(args->data_ptr);
932 offset = offset_in_page(args->offset);
933 for (idx = args->offset >> PAGE_SHIFT; remain; idx++) {
934 struct page *page = i915_gem_object_get_page(obj, idx);
Chris Wilsona5e856a52018-10-12 15:02:28 +0100935 unsigned int length = min_t(u64, remain, PAGE_SIZE - offset);
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100936
937 ret = shmem_pread(page, offset, length, user_data,
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100938 needs_clflush);
939 if (ret)
940 break;
941
942 remain -= length;
943 user_data += length;
944 offset = 0;
945 }
946
947 i915_gem_obj_finish_shmem_access(obj);
948 return ret;
949}
950
951static inline bool
952gtt_user_read(struct io_mapping *mapping,
953 loff_t base, int offset,
954 char __user *user_data, int length)
955{
Ville Syrjäläafe722b2017-09-01 20:12:52 +0300956 void __iomem *vaddr;
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100957 unsigned long unwritten;
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +0530958
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +0530959 /* We can use the cpu mem copy function because this is X86. */
Ville Syrjäläafe722b2017-09-01 20:12:52 +0300960 vaddr = io_mapping_map_atomic_wc(mapping, base);
961 unwritten = __copy_to_user_inatomic(user_data,
962 (void __force *)vaddr + offset,
963 length);
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100964 io_mapping_unmap_atomic(vaddr);
965 if (unwritten) {
Ville Syrjäläafe722b2017-09-01 20:12:52 +0300966 vaddr = io_mapping_map_wc(mapping, base, PAGE_SIZE);
967 unwritten = copy_to_user(user_data,
968 (void __force *)vaddr + offset,
969 length);
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100970 io_mapping_unmap(vaddr);
971 }
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +0530972 return unwritten;
973}
974
975static int
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100976i915_gem_gtt_pread(struct drm_i915_gem_object *obj,
977 const struct drm_i915_gem_pread *args)
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +0530978{
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100979 struct drm_i915_private *i915 = to_i915(obj->base.dev);
980 struct i915_ggtt *ggtt = &i915->ggtt;
Chris Wilson538ef962019-01-14 14:21:18 +0000981 intel_wakeref_t wakeref;
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +0530982 struct drm_mm_node node;
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100983 struct i915_vma *vma;
984 void __user *user_data;
985 u64 remain, offset;
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +0530986 int ret;
987
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100988 ret = mutex_lock_interruptible(&i915->drm.struct_mutex);
989 if (ret)
990 return ret;
991
Chris Wilson538ef962019-01-14 14:21:18 +0000992 wakeref = intel_runtime_pm_get(i915);
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100993 vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0,
Chris Wilsona3259ca2017-10-09 09:44:00 +0100994 PIN_MAPPABLE |
995 PIN_NONFAULT |
996 PIN_NONBLOCK);
Chris Wilson18034582016-08-18 17:16:45 +0100997 if (!IS_ERR(vma)) {
998 node.start = i915_ggtt_offset(vma);
999 node.allocated = false;
Chris Wilson49ef5292016-08-18 17:17:00 +01001000 ret = i915_vma_put_fence(vma);
Chris Wilson18034582016-08-18 17:16:45 +01001001 if (ret) {
1002 i915_vma_unpin(vma);
1003 vma = ERR_PTR(ret);
1004 }
1005 }
Chris Wilson058d88c2016-08-15 10:49:06 +01001006 if (IS_ERR(vma)) {
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001007 ret = insert_mappable_node(ggtt, &node, PAGE_SIZE);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301008 if (ret)
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001009 goto out_unlock;
1010 GEM_BUG_ON(!node.allocated);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301011 }
1012
1013 ret = i915_gem_object_set_to_gtt_domain(obj, false);
1014 if (ret)
1015 goto out_unpin;
1016
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001017 mutex_unlock(&i915->drm.struct_mutex);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301018
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001019 user_data = u64_to_user_ptr(args->data_ptr);
1020 remain = args->size;
1021 offset = args->offset;
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301022
1023 while (remain > 0) {
1024 /* Operation in this page
1025 *
1026 * page_base = page offset within aperture
1027 * page_offset = offset within page
1028 * page_length = bytes to copy for this page
1029 */
1030 u32 page_base = node.start;
1031 unsigned page_offset = offset_in_page(offset);
1032 unsigned page_length = PAGE_SIZE - page_offset;
1033 page_length = remain < page_length ? remain : page_length;
1034 if (node.allocated) {
1035 wmb();
Chris Wilson82ad6442018-06-05 16:37:58 +01001036 ggtt->vm.insert_page(&ggtt->vm,
1037 i915_gem_object_get_dma_address(obj, offset >> PAGE_SHIFT),
1038 node.start, I915_CACHE_NONE, 0);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301039 wmb();
1040 } else {
1041 page_base += offset & PAGE_MASK;
1042 }
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001043
Matthew Auld73ebd502017-12-11 15:18:20 +00001044 if (gtt_user_read(&ggtt->iomap, page_base, page_offset,
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001045 user_data, page_length)) {
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301046 ret = -EFAULT;
1047 break;
1048 }
1049
1050 remain -= page_length;
1051 user_data += page_length;
1052 offset += page_length;
1053 }
1054
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001055 mutex_lock(&i915->drm.struct_mutex);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301056out_unpin:
1057 if (node.allocated) {
1058 wmb();
Chris Wilson82ad6442018-06-05 16:37:58 +01001059 ggtt->vm.clear_range(&ggtt->vm, node.start, node.size);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301060 remove_mappable_node(&node);
1061 } else {
Chris Wilson058d88c2016-08-15 10:49:06 +01001062 i915_vma_unpin(vma);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301063 }
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001064out_unlock:
Chris Wilson538ef962019-01-14 14:21:18 +00001065 intel_runtime_pm_put(i915, wakeref);
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001066 mutex_unlock(&i915->drm.struct_mutex);
Chris Wilsonf60d7f02012-09-04 21:02:56 +01001067
Eric Anholteb014592009-03-10 11:44:52 -07001068 return ret;
1069}
1070
Eric Anholt673a3942008-07-30 12:06:12 -07001071/**
1072 * Reads data from the object referenced by handle.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01001073 * @dev: drm device pointer
1074 * @data: ioctl data blob
1075 * @file: drm file pointer
Eric Anholt673a3942008-07-30 12:06:12 -07001076 *
1077 * On error, the contents of *data are undefined.
1078 */
1079int
1080i915_gem_pread_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00001081 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001082{
1083 struct drm_i915_gem_pread *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00001084 struct drm_i915_gem_object *obj;
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001085 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07001086
Chris Wilson51311d02010-11-17 09:10:42 +00001087 if (args->size == 0)
1088 return 0;
1089
Linus Torvalds96d4f262019-01-03 18:57:57 -08001090 if (!access_ok(u64_to_user_ptr(args->data_ptr),
Chris Wilson51311d02010-11-17 09:10:42 +00001091 args->size))
1092 return -EFAULT;
1093
Chris Wilson03ac0642016-07-20 13:31:51 +01001094 obj = i915_gem_object_lookup(file, args->handle);
Chris Wilson258a5ed2016-08-05 10:14:16 +01001095 if (!obj)
1096 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07001097
Chris Wilson7dcd2492010-09-26 20:21:44 +01001098 /* Bounds check source. */
Matthew Auld966d5bf2016-12-13 20:32:22 +00001099 if (range_overflows_t(u64, args->offset, args->size, obj->base.size)) {
Chris Wilsonce9d4192010-09-26 20:50:05 +01001100 ret = -EINVAL;
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001101 goto out;
Chris Wilsonce9d4192010-09-26 20:50:05 +01001102 }
1103
Chris Wilsondb53a302011-02-03 11:57:46 +00001104 trace_i915_gem_object_pread(obj, args->offset, args->size);
1105
Chris Wilsone95433c2016-10-28 13:58:27 +01001106 ret = i915_gem_object_wait(obj,
1107 I915_WAIT_INTERRUPTIBLE,
Chris Wilson62eb3c22019-02-13 09:25:04 +00001108 MAX_SCHEDULE_TIMEOUT);
Chris Wilson258a5ed2016-08-05 10:14:16 +01001109 if (ret)
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001110 goto out;
Chris Wilson258a5ed2016-08-05 10:14:16 +01001111
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001112 ret = i915_gem_object_pin_pages(obj);
Chris Wilson258a5ed2016-08-05 10:14:16 +01001113 if (ret)
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001114 goto out;
Chris Wilson258a5ed2016-08-05 10:14:16 +01001115
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001116 ret = i915_gem_shmem_pread(obj, args);
Chris Wilson9c870d02016-10-24 13:42:15 +01001117 if (ret == -EFAULT || ret == -ENODEV)
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001118 ret = i915_gem_gtt_pread(obj, args);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301119
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001120 i915_gem_object_unpin_pages(obj);
1121out:
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001122 i915_gem_object_put(obj);
Eric Anholteb014592009-03-10 11:44:52 -07001123 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07001124}
1125
Keith Packard0839ccb2008-10-30 19:38:48 -07001126/* This is the fast write path which cannot handle
1127 * page faults in the source data
Linus Torvalds9b7530cc2008-10-20 14:16:43 -07001128 */
Linus Torvalds9b7530cc2008-10-20 14:16:43 -07001129
Chris Wilsonfe115622016-10-28 13:58:40 +01001130static inline bool
1131ggtt_write(struct io_mapping *mapping,
1132 loff_t base, int offset,
1133 char __user *user_data, int length)
Keith Packard0839ccb2008-10-30 19:38:48 -07001134{
Ville Syrjäläafe722b2017-09-01 20:12:52 +03001135 void __iomem *vaddr;
Keith Packard0839ccb2008-10-30 19:38:48 -07001136 unsigned long unwritten;
1137
Ben Widawsky4f0c7cf2012-04-16 14:07:47 -07001138 /* We can use the cpu mem copy function because this is X86. */
Ville Syrjäläafe722b2017-09-01 20:12:52 +03001139 vaddr = io_mapping_map_atomic_wc(mapping, base);
1140 unwritten = __copy_from_user_inatomic_nocache((void __force *)vaddr + offset,
Keith Packard0839ccb2008-10-30 19:38:48 -07001141 user_data, length);
Chris Wilsonfe115622016-10-28 13:58:40 +01001142 io_mapping_unmap_atomic(vaddr);
1143 if (unwritten) {
Ville Syrjäläafe722b2017-09-01 20:12:52 +03001144 vaddr = io_mapping_map_wc(mapping, base, PAGE_SIZE);
1145 unwritten = copy_from_user((void __force *)vaddr + offset,
1146 user_data, length);
Chris Wilsonfe115622016-10-28 13:58:40 +01001147 io_mapping_unmap(vaddr);
1148 }
Keith Packard0839ccb2008-10-30 19:38:48 -07001149
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001150 return unwritten;
1151}
1152
Eric Anholt3de09aa2009-03-09 09:42:23 -07001153/**
1154 * This is the fast pwrite path, where we copy the data directly from the
1155 * user into the GTT, uncached.
Chris Wilsonfe115622016-10-28 13:58:40 +01001156 * @obj: i915 GEM object
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01001157 * @args: pwrite arguments structure
Eric Anholt3de09aa2009-03-09 09:42:23 -07001158 */
Eric Anholt673a3942008-07-30 12:06:12 -07001159static int
Chris Wilsonfe115622016-10-28 13:58:40 +01001160i915_gem_gtt_pwrite_fast(struct drm_i915_gem_object *obj,
1161 const struct drm_i915_gem_pwrite *args)
Eric Anholt673a3942008-07-30 12:06:12 -07001162{
Chris Wilsonfe115622016-10-28 13:58:40 +01001163 struct drm_i915_private *i915 = to_i915(obj->base.dev);
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301164 struct i915_ggtt *ggtt = &i915->ggtt;
Chris Wilson538ef962019-01-14 14:21:18 +00001165 intel_wakeref_t wakeref;
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301166 struct drm_mm_node node;
Chris Wilsonfe115622016-10-28 13:58:40 +01001167 struct i915_vma *vma;
1168 u64 remain, offset;
1169 void __user *user_data;
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301170 int ret;
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301171
Chris Wilsonfe115622016-10-28 13:58:40 +01001172 ret = mutex_lock_interruptible(&i915->drm.struct_mutex);
1173 if (ret)
1174 return ret;
Daniel Vetter935aaa62012-03-25 19:47:35 +02001175
Chris Wilson8bd818152017-10-19 07:37:33 +01001176 if (i915_gem_object_has_struct_page(obj)) {
1177 /*
1178 * Avoid waking the device up if we can fallback, as
1179 * waking/resuming is very slow (worst-case 10-100 ms
1180 * depending on PCI sleeps and our own resume time).
1181 * This easily dwarfs any performance advantage from
1182 * using the cache bypass of indirect GGTT access.
1183 */
Chris Wilson538ef962019-01-14 14:21:18 +00001184 wakeref = intel_runtime_pm_get_if_in_use(i915);
1185 if (!wakeref) {
Chris Wilson8bd818152017-10-19 07:37:33 +01001186 ret = -EFAULT;
1187 goto out_unlock;
1188 }
1189 } else {
1190 /* No backing pages, no fallback, we must force GGTT access */
Chris Wilson538ef962019-01-14 14:21:18 +00001191 wakeref = intel_runtime_pm_get(i915);
Chris Wilson8bd818152017-10-19 07:37:33 +01001192 }
1193
Chris Wilson058d88c2016-08-15 10:49:06 +01001194 vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0,
Chris Wilsona3259ca2017-10-09 09:44:00 +01001195 PIN_MAPPABLE |
1196 PIN_NONFAULT |
1197 PIN_NONBLOCK);
Chris Wilson18034582016-08-18 17:16:45 +01001198 if (!IS_ERR(vma)) {
1199 node.start = i915_ggtt_offset(vma);
1200 node.allocated = false;
Chris Wilson49ef5292016-08-18 17:17:00 +01001201 ret = i915_vma_put_fence(vma);
Chris Wilson18034582016-08-18 17:16:45 +01001202 if (ret) {
1203 i915_vma_unpin(vma);
1204 vma = ERR_PTR(ret);
1205 }
1206 }
Chris Wilson058d88c2016-08-15 10:49:06 +01001207 if (IS_ERR(vma)) {
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001208 ret = insert_mappable_node(ggtt, &node, PAGE_SIZE);
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301209 if (ret)
Chris Wilson8bd818152017-10-19 07:37:33 +01001210 goto out_rpm;
Chris Wilsonfe115622016-10-28 13:58:40 +01001211 GEM_BUG_ON(!node.allocated);
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301212 }
Daniel Vetter935aaa62012-03-25 19:47:35 +02001213
1214 ret = i915_gem_object_set_to_gtt_domain(obj, true);
1215 if (ret)
1216 goto out_unpin;
1217
Chris Wilsonfe115622016-10-28 13:58:40 +01001218 mutex_unlock(&i915->drm.struct_mutex);
1219
Chris Wilsonb19482d2016-08-18 17:16:43 +01001220 intel_fb_obj_invalidate(obj, ORIGIN_CPU);
Paulo Zanoni063e4e62015-02-13 17:23:45 -02001221
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301222 user_data = u64_to_user_ptr(args->data_ptr);
1223 offset = args->offset;
1224 remain = args->size;
1225 while (remain) {
Eric Anholt673a3942008-07-30 12:06:12 -07001226 /* Operation in this page
1227 *
Keith Packard0839ccb2008-10-30 19:38:48 -07001228 * page_base = page offset within aperture
1229 * page_offset = offset within page
1230 * page_length = bytes to copy for this page
Eric Anholt673a3942008-07-30 12:06:12 -07001231 */
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301232 u32 page_base = node.start;
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001233 unsigned int page_offset = offset_in_page(offset);
1234 unsigned int page_length = PAGE_SIZE - page_offset;
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301235 page_length = remain < page_length ? remain : page_length;
1236 if (node.allocated) {
1237 wmb(); /* flush the write before we modify the GGTT */
Chris Wilson82ad6442018-06-05 16:37:58 +01001238 ggtt->vm.insert_page(&ggtt->vm,
1239 i915_gem_object_get_dma_address(obj, offset >> PAGE_SHIFT),
1240 node.start, I915_CACHE_NONE, 0);
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301241 wmb(); /* flush modifications to the GGTT (insert_page) */
1242 } else {
1243 page_base += offset & PAGE_MASK;
1244 }
Keith Packard0839ccb2008-10-30 19:38:48 -07001245 /* If we get a fault while copying data, then (presumably) our
Eric Anholt3de09aa2009-03-09 09:42:23 -07001246 * source page isn't available. Return the error and we'll
1247 * retry in the slow path.
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301248 * If the object is non-shmem backed, we retry again with the
1249 * path that handles page fault.
Keith Packard0839ccb2008-10-30 19:38:48 -07001250 */
Matthew Auld73ebd502017-12-11 15:18:20 +00001251 if (ggtt_write(&ggtt->iomap, page_base, page_offset,
Chris Wilsonfe115622016-10-28 13:58:40 +01001252 user_data, page_length)) {
1253 ret = -EFAULT;
1254 break;
Daniel Vetter935aaa62012-03-25 19:47:35 +02001255 }
Eric Anholt673a3942008-07-30 12:06:12 -07001256
Keith Packard0839ccb2008-10-30 19:38:48 -07001257 remain -= page_length;
1258 user_data += page_length;
1259 offset += page_length;
Eric Anholt673a3942008-07-30 12:06:12 -07001260 }
Chris Wilsond59b21e2017-02-22 11:40:49 +00001261 intel_fb_obj_flush(obj, ORIGIN_CPU);
Chris Wilsonfe115622016-10-28 13:58:40 +01001262
1263 mutex_lock(&i915->drm.struct_mutex);
Daniel Vetter935aaa62012-03-25 19:47:35 +02001264out_unpin:
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301265 if (node.allocated) {
1266 wmb();
Chris Wilson82ad6442018-06-05 16:37:58 +01001267 ggtt->vm.clear_range(&ggtt->vm, node.start, node.size);
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301268 remove_mappable_node(&node);
1269 } else {
Chris Wilson058d88c2016-08-15 10:49:06 +01001270 i915_vma_unpin(vma);
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301271 }
Chris Wilson8bd818152017-10-19 07:37:33 +01001272out_rpm:
Chris Wilson538ef962019-01-14 14:21:18 +00001273 intel_runtime_pm_put(i915, wakeref);
Chris Wilson8bd818152017-10-19 07:37:33 +01001274out_unlock:
Chris Wilsonfe115622016-10-28 13:58:40 +01001275 mutex_unlock(&i915->drm.struct_mutex);
Eric Anholt3de09aa2009-03-09 09:42:23 -07001276 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07001277}
1278
Chris Wilsonfe115622016-10-28 13:58:40 +01001279/* Per-page copy function for the shmem pwrite fastpath.
1280 * Flushes invalid cachelines before writing to the target if
1281 * needs_clflush_before is set and flushes out any written cachelines after
1282 * writing if needs_clflush is set.
1283 */
Eric Anholt40123c12009-03-09 13:42:30 -07001284static int
Chris Wilsonfe115622016-10-28 13:58:40 +01001285shmem_pwrite(struct page *page, int offset, int len, char __user *user_data,
Chris Wilsonfe115622016-10-28 13:58:40 +01001286 bool needs_clflush_before,
1287 bool needs_clflush_after)
Eric Anholt40123c12009-03-09 13:42:30 -07001288{
Chris Wilsonb9d126e2019-01-05 12:07:58 +00001289 char *vaddr;
Chris Wilsonfe115622016-10-28 13:58:40 +01001290 int ret;
Eric Anholt40123c12009-03-09 13:42:30 -07001291
Chris Wilsonb9d126e2019-01-05 12:07:58 +00001292 vaddr = kmap(page);
Chris Wilsonfe115622016-10-28 13:58:40 +01001293
Chris Wilsonb9d126e2019-01-05 12:07:58 +00001294 if (needs_clflush_before)
1295 drm_clflush_virt_range(vaddr + offset, len);
Chris Wilsonfe115622016-10-28 13:58:40 +01001296
Chris Wilsonb9d126e2019-01-05 12:07:58 +00001297 ret = __copy_from_user(vaddr + offset, user_data, len);
1298 if (!ret && needs_clflush_after)
1299 drm_clflush_virt_range(vaddr + offset, len);
Chris Wilsonfe115622016-10-28 13:58:40 +01001300
Chris Wilsonb9d126e2019-01-05 12:07:58 +00001301 kunmap(page);
1302
1303 return ret ? -EFAULT : 0;
Chris Wilsonfe115622016-10-28 13:58:40 +01001304}
1305
1306static int
1307i915_gem_shmem_pwrite(struct drm_i915_gem_object *obj,
1308 const struct drm_i915_gem_pwrite *args)
1309{
1310 struct drm_i915_private *i915 = to_i915(obj->base.dev);
1311 void __user *user_data;
1312 u64 remain;
Chris Wilsonfe115622016-10-28 13:58:40 +01001313 unsigned int partial_cacheline_write;
1314 unsigned int needs_clflush;
1315 unsigned int offset, idx;
1316 int ret;
1317
1318 ret = mutex_lock_interruptible(&i915->drm.struct_mutex);
Chris Wilson43394c72016-08-18 17:16:47 +01001319 if (ret)
1320 return ret;
Eric Anholt40123c12009-03-09 13:42:30 -07001321
Chris Wilsonfe115622016-10-28 13:58:40 +01001322 ret = i915_gem_obj_prepare_shmem_write(obj, &needs_clflush);
1323 mutex_unlock(&i915->drm.struct_mutex);
1324 if (ret)
1325 return ret;
1326
Chris Wilsonfe115622016-10-28 13:58:40 +01001327 /* If we don't overwrite a cacheline completely we need to be
1328 * careful to have up-to-date data by first clflushing. Don't
1329 * overcomplicate things and flush the entire patch.
1330 */
1331 partial_cacheline_write = 0;
1332 if (needs_clflush & CLFLUSH_BEFORE)
1333 partial_cacheline_write = boot_cpu_data.x86_clflush_size - 1;
1334
Chris Wilson43394c72016-08-18 17:16:47 +01001335 user_data = u64_to_user_ptr(args->data_ptr);
Chris Wilson43394c72016-08-18 17:16:47 +01001336 remain = args->size;
Chris Wilsonfe115622016-10-28 13:58:40 +01001337 offset = offset_in_page(args->offset);
1338 for (idx = args->offset >> PAGE_SHIFT; remain; idx++) {
1339 struct page *page = i915_gem_object_get_page(obj, idx);
Chris Wilsona5e856a52018-10-12 15:02:28 +01001340 unsigned int length = min_t(u64, remain, PAGE_SIZE - offset);
Chris Wilsone5281cc2010-10-28 13:45:36 +01001341
Chris Wilsonfe115622016-10-28 13:58:40 +01001342 ret = shmem_pwrite(page, offset, length, user_data,
Chris Wilsonfe115622016-10-28 13:58:40 +01001343 (offset | length) & partial_cacheline_write,
1344 needs_clflush & CLFLUSH_AFTER);
1345 if (ret)
Chris Wilson9da3da62012-06-01 15:20:22 +01001346 break;
1347
Chris Wilsonfe115622016-10-28 13:58:40 +01001348 remain -= length;
1349 user_data += length;
1350 offset = 0;
Eric Anholt40123c12009-03-09 13:42:30 -07001351 }
1352
Chris Wilsond59b21e2017-02-22 11:40:49 +00001353 intel_fb_obj_flush(obj, ORIGIN_CPU);
Chris Wilsonfe115622016-10-28 13:58:40 +01001354 i915_gem_obj_finish_shmem_access(obj);
Eric Anholt40123c12009-03-09 13:42:30 -07001355 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07001356}
1357
1358/**
1359 * Writes data to the object referenced by handle.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01001360 * @dev: drm device
1361 * @data: ioctl data blob
1362 * @file: drm file
Eric Anholt673a3942008-07-30 12:06:12 -07001363 *
1364 * On error, the contents of the buffer that were to be modified are undefined.
1365 */
1366int
1367i915_gem_pwrite_ioctl(struct drm_device *dev, void *data,
Chris Wilsonfbd5a262010-10-14 15:03:58 +01001368 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001369{
1370 struct drm_i915_gem_pwrite *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00001371 struct drm_i915_gem_object *obj;
Chris Wilson51311d02010-11-17 09:10:42 +00001372 int ret;
1373
1374 if (args->size == 0)
1375 return 0;
1376
Linus Torvalds96d4f262019-01-03 18:57:57 -08001377 if (!access_ok(u64_to_user_ptr(args->data_ptr), args->size))
Chris Wilson51311d02010-11-17 09:10:42 +00001378 return -EFAULT;
1379
Chris Wilson03ac0642016-07-20 13:31:51 +01001380 obj = i915_gem_object_lookup(file, args->handle);
Chris Wilson258a5ed2016-08-05 10:14:16 +01001381 if (!obj)
1382 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07001383
Chris Wilson7dcd2492010-09-26 20:21:44 +01001384 /* Bounds check destination. */
Matthew Auld966d5bf2016-12-13 20:32:22 +00001385 if (range_overflows_t(u64, args->offset, args->size, obj->base.size)) {
Chris Wilsonce9d4192010-09-26 20:50:05 +01001386 ret = -EINVAL;
Chris Wilson258a5ed2016-08-05 10:14:16 +01001387 goto err;
Chris Wilsonce9d4192010-09-26 20:50:05 +01001388 }
1389
Chris Wilsonf8c1cce2018-07-12 19:53:14 +01001390 /* Writes not allowed into this read-only object */
1391 if (i915_gem_object_is_readonly(obj)) {
1392 ret = -EINVAL;
1393 goto err;
1394 }
1395
Chris Wilsondb53a302011-02-03 11:57:46 +00001396 trace_i915_gem_object_pwrite(obj, args->offset, args->size);
1397
Chris Wilson7c55e2c2017-03-07 12:03:38 +00001398 ret = -ENODEV;
1399 if (obj->ops->pwrite)
1400 ret = obj->ops->pwrite(obj, args);
1401 if (ret != -ENODEV)
1402 goto err;
1403
Chris Wilsone95433c2016-10-28 13:58:27 +01001404 ret = i915_gem_object_wait(obj,
1405 I915_WAIT_INTERRUPTIBLE |
1406 I915_WAIT_ALL,
Chris Wilson62eb3c22019-02-13 09:25:04 +00001407 MAX_SCHEDULE_TIMEOUT);
Chris Wilson258a5ed2016-08-05 10:14:16 +01001408 if (ret)
1409 goto err;
1410
Chris Wilsonfe115622016-10-28 13:58:40 +01001411 ret = i915_gem_object_pin_pages(obj);
Chris Wilson258a5ed2016-08-05 10:14:16 +01001412 if (ret)
Chris Wilsonfe115622016-10-28 13:58:40 +01001413 goto err;
Chris Wilson258a5ed2016-08-05 10:14:16 +01001414
Daniel Vetter935aaa62012-03-25 19:47:35 +02001415 ret = -EFAULT;
Eric Anholt673a3942008-07-30 12:06:12 -07001416 /* We can only do the GTT pwrite on untiled buffers, as otherwise
1417 * it would end up going through the fenced access, and we'll get
1418 * different detiling behavior between reading and writing.
1419 * pread/pwrite currently are reading and writing from the CPU
1420 * perspective, requiring manual detiling by the client.
1421 */
Chris Wilson6eae0052016-06-20 15:05:52 +01001422 if (!i915_gem_object_has_struct_page(obj) ||
Chris Wilson9c870d02016-10-24 13:42:15 +01001423 cpu_write_needs_clflush(obj))
Daniel Vetter935aaa62012-03-25 19:47:35 +02001424 /* Note that the gtt paths might fail with non-page-backed user
1425 * pointers (e.g. gtt mappings when moving data between
Chris Wilson9c870d02016-10-24 13:42:15 +01001426 * textures). Fallback to the shmem path in that case.
1427 */
Chris Wilsonfe115622016-10-28 13:58:40 +01001428 ret = i915_gem_gtt_pwrite_fast(obj, args);
Eric Anholt673a3942008-07-30 12:06:12 -07001429
Chris Wilsond1054ee2016-07-16 18:42:36 +01001430 if (ret == -EFAULT || ret == -ENOSPC) {
Chris Wilson6a2c4232014-11-04 04:51:40 -08001431 if (obj->phys_handle)
1432 ret = i915_gem_phys_pwrite(obj, args, file);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301433 else
Chris Wilsonfe115622016-10-28 13:58:40 +01001434 ret = i915_gem_shmem_pwrite(obj, args);
Chris Wilson6a2c4232014-11-04 04:51:40 -08001435 }
Daniel Vetter5c0480f2011-12-14 13:57:30 +01001436
Chris Wilsonfe115622016-10-28 13:58:40 +01001437 i915_gem_object_unpin_pages(obj);
Chris Wilson258a5ed2016-08-05 10:14:16 +01001438err:
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001439 i915_gem_object_put(obj);
Chris Wilson258a5ed2016-08-05 10:14:16 +01001440 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07001441}
1442
Chris Wilson40e62d52016-10-28 13:58:41 +01001443static void i915_gem_object_bump_inactive_ggtt(struct drm_i915_gem_object *obj)
1444{
Chris Wilson09d7e462019-01-28 10:23:53 +00001445 struct drm_i915_private *i915 = to_i915(obj->base.dev);
Chris Wilson40e62d52016-10-28 13:58:41 +01001446 struct list_head *list;
1447 struct i915_vma *vma;
1448
Chris Wilsonf2123812017-10-16 12:40:37 +01001449 GEM_BUG_ON(!i915_gem_object_has_pinned_pages(obj));
1450
Chris Wilson09d7e462019-01-28 10:23:53 +00001451 mutex_lock(&i915->ggtt.vm.mutex);
Chris Wilsone2189dd2017-12-07 21:14:07 +00001452 for_each_ggtt_vma(vma, obj) {
Chris Wilson40e62d52016-10-28 13:58:41 +01001453 if (!drm_mm_node_allocated(&vma->node))
1454 continue;
1455
Chris Wilson499197d2019-01-28 10:23:52 +00001456 list_move_tail(&vma->vm_link, &vma->vm->bound_list);
Chris Wilson40e62d52016-10-28 13:58:41 +01001457 }
Chris Wilson09d7e462019-01-28 10:23:53 +00001458 mutex_unlock(&i915->ggtt.vm.mutex);
Chris Wilson40e62d52016-10-28 13:58:41 +01001459
Chris Wilsonf2123812017-10-16 12:40:37 +01001460 spin_lock(&i915->mm.obj_lock);
Chris Wilson40e62d52016-10-28 13:58:41 +01001461 list = obj->bind_count ? &i915->mm.bound_list : &i915->mm.unbound_list;
Chris Wilsonf2123812017-10-16 12:40:37 +01001462 list_move_tail(&obj->mm.link, list);
1463 spin_unlock(&i915->mm.obj_lock);
Chris Wilson40e62d52016-10-28 13:58:41 +01001464}
1465
Eric Anholt673a3942008-07-30 12:06:12 -07001466/**
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001467 * Called when user space prepares to use an object with the CPU, either
1468 * through the mmap ioctl's mapping or a GTT mapping.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01001469 * @dev: drm device
1470 * @data: ioctl data blob
1471 * @file: drm file
Eric Anholt673a3942008-07-30 12:06:12 -07001472 */
1473int
1474i915_gem_set_domain_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00001475 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001476{
1477 struct drm_i915_gem_set_domain *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00001478 struct drm_i915_gem_object *obj;
Jani Nikula739f3ab2019-01-16 11:15:19 +02001479 u32 read_domains = args->read_domains;
1480 u32 write_domain = args->write_domain;
Chris Wilson40e62d52016-10-28 13:58:41 +01001481 int err;
Eric Anholt673a3942008-07-30 12:06:12 -07001482
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001483 /* Only handle setting domains to types used by the CPU. */
Chris Wilsonb8f90962016-08-05 10:14:07 +01001484 if ((write_domain | read_domains) & I915_GEM_GPU_DOMAINS)
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001485 return -EINVAL;
1486
1487 /* Having something in the write domain implies it's in the read
1488 * domain, and only that read domain. Enforce that in the request.
1489 */
1490 if (write_domain != 0 && read_domains != write_domain)
1491 return -EINVAL;
1492
Chris Wilson03ac0642016-07-20 13:31:51 +01001493 obj = i915_gem_object_lookup(file, args->handle);
Chris Wilsonb8f90962016-08-05 10:14:07 +01001494 if (!obj)
1495 return -ENOENT;
Jesse Barnes652c3932009-08-17 13:31:43 -07001496
Chris Wilson3236f572012-08-24 09:35:09 +01001497 /* Try to flush the object off the GPU without holding the lock.
1498 * We will repeat the flush holding the lock in the normal manner
1499 * to catch cases where we are gazumped.
1500 */
Chris Wilson40e62d52016-10-28 13:58:41 +01001501 err = i915_gem_object_wait(obj,
Chris Wilsone95433c2016-10-28 13:58:27 +01001502 I915_WAIT_INTERRUPTIBLE |
Chris Wilsone9eaf822018-10-01 15:47:55 +01001503 I915_WAIT_PRIORITY |
Chris Wilsone95433c2016-10-28 13:58:27 +01001504 (write_domain ? I915_WAIT_ALL : 0),
Chris Wilson62eb3c22019-02-13 09:25:04 +00001505 MAX_SCHEDULE_TIMEOUT);
Chris Wilson40e62d52016-10-28 13:58:41 +01001506 if (err)
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001507 goto out;
Chris Wilsonb8f90962016-08-05 10:14:07 +01001508
Tina Zhanga03f3952017-11-14 10:25:13 +00001509 /*
1510 * Proxy objects do not control access to the backing storage, ergo
1511 * they cannot be used as a means to manipulate the cache domain
1512 * tracking for that backing storage. The proxy object is always
1513 * considered to be outside of any cache domain.
1514 */
1515 if (i915_gem_object_is_proxy(obj)) {
1516 err = -ENXIO;
1517 goto out;
1518 }
1519
1520 /*
1521 * Flush and acquire obj->pages so that we are coherent through
Chris Wilson40e62d52016-10-28 13:58:41 +01001522 * direct access in memory with previous cached writes through
1523 * shmemfs and that our cache domain tracking remains valid.
1524 * For example, if the obj->filp was moved to swap without us
1525 * being notified and releasing the pages, we would mistakenly
1526 * continue to assume that the obj remained out of the CPU cached
1527 * domain.
1528 */
1529 err = i915_gem_object_pin_pages(obj);
1530 if (err)
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001531 goto out;
Chris Wilson40e62d52016-10-28 13:58:41 +01001532
1533 err = i915_mutex_lock_interruptible(dev);
1534 if (err)
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001535 goto out_unpin;
Chris Wilson3236f572012-08-24 09:35:09 +01001536
Chris Wilsone22d8e32017-04-12 12:01:11 +01001537 if (read_domains & I915_GEM_DOMAIN_WC)
1538 err = i915_gem_object_set_to_wc_domain(obj, write_domain);
1539 else if (read_domains & I915_GEM_DOMAIN_GTT)
1540 err = i915_gem_object_set_to_gtt_domain(obj, write_domain);
Chris Wilson43566de2015-01-02 16:29:29 +05301541 else
Chris Wilsone22d8e32017-04-12 12:01:11 +01001542 err = i915_gem_object_set_to_cpu_domain(obj, write_domain);
Chris Wilson40e62d52016-10-28 13:58:41 +01001543
1544 /* And bump the LRU for this access */
1545 i915_gem_object_bump_inactive_ggtt(obj);
1546
1547 mutex_unlock(&dev->struct_mutex);
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001548
Daniel Vetter031b6982015-06-26 19:35:16 +02001549 if (write_domain != 0)
Chris Wilsonef749212017-04-12 12:01:10 +01001550 intel_fb_obj_invalidate(obj,
1551 fb_write_origin(obj, write_domain));
Daniel Vetter031b6982015-06-26 19:35:16 +02001552
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001553out_unpin:
Chris Wilson40e62d52016-10-28 13:58:41 +01001554 i915_gem_object_unpin_pages(obj);
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001555out:
1556 i915_gem_object_put(obj);
Chris Wilson40e62d52016-10-28 13:58:41 +01001557 return err;
Eric Anholt673a3942008-07-30 12:06:12 -07001558}
1559
1560/**
1561 * Called when user space has done writes to this buffer
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01001562 * @dev: drm device
1563 * @data: ioctl data blob
1564 * @file: drm file
Eric Anholt673a3942008-07-30 12:06:12 -07001565 */
1566int
1567i915_gem_sw_finish_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00001568 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001569{
1570 struct drm_i915_gem_sw_finish *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00001571 struct drm_i915_gem_object *obj;
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001572
Chris Wilson03ac0642016-07-20 13:31:51 +01001573 obj = i915_gem_object_lookup(file, args->handle);
Chris Wilsonc21724c2016-08-05 10:14:19 +01001574 if (!obj)
1575 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07001576
Tina Zhanga03f3952017-11-14 10:25:13 +00001577 /*
1578 * Proxy objects are barred from CPU access, so there is no
1579 * need to ban sw_finish as it is a nop.
1580 */
1581
Eric Anholt673a3942008-07-30 12:06:12 -07001582 /* Pinned buffers may be scanout, so flush the cache */
Chris Wilson5a97bcc2017-02-22 11:40:46 +00001583 i915_gem_object_flush_if_display(obj);
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001584 i915_gem_object_put(obj);
Chris Wilson5a97bcc2017-02-22 11:40:46 +00001585
1586 return 0;
Eric Anholt673a3942008-07-30 12:06:12 -07001587}
1588
Joonas Lahtinen5c4604e2019-02-07 10:54:53 +02001589static inline bool
1590__vma_matches(struct vm_area_struct *vma, struct file *filp,
1591 unsigned long addr, unsigned long size)
1592{
1593 if (vma->vm_file != filp)
1594 return false;
1595
Tvrtko Ursulina90e1942019-03-05 11:04:08 +00001596 return vma->vm_start == addr &&
1597 (vma->vm_end - vma->vm_start) == PAGE_ALIGN(size);
Joonas Lahtinen5c4604e2019-02-07 10:54:53 +02001598}
1599
Eric Anholt673a3942008-07-30 12:06:12 -07001600/**
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01001601 * i915_gem_mmap_ioctl - Maps the contents of an object, returning the address
1602 * it is mapped to.
1603 * @dev: drm device
1604 * @data: ioctl data blob
1605 * @file: drm file
Eric Anholt673a3942008-07-30 12:06:12 -07001606 *
1607 * While the mapping holds a reference on the contents of the object, it doesn't
1608 * imply a ref on the object itself.
Daniel Vetter34367382014-10-16 12:28:18 +02001609 *
1610 * IMPORTANT:
1611 *
1612 * DRM driver writers who look a this function as an example for how to do GEM
1613 * mmap support, please don't implement mmap support like here. The modern way
1614 * to implement DRM mmap support is with an mmap offset ioctl (like
1615 * i915_gem_mmap_gtt) and then using the mmap syscall on the DRM fd directly.
1616 * That way debug tooling like valgrind will understand what's going on, hiding
1617 * the mmap call in a driver private ioctl will break that. The i915 driver only
1618 * does cpu mmaps this way because we didn't know better.
Eric Anholt673a3942008-07-30 12:06:12 -07001619 */
1620int
1621i915_gem_mmap_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00001622 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001623{
1624 struct drm_i915_gem_mmap *args = data;
Chris Wilson03ac0642016-07-20 13:31:51 +01001625 struct drm_i915_gem_object *obj;
Eric Anholt673a3942008-07-30 12:06:12 -07001626 unsigned long addr;
1627
Akash Goel1816f922015-01-02 16:29:30 +05301628 if (args->flags & ~(I915_MMAP_WC))
1629 return -EINVAL;
1630
Borislav Petkov568a58e2016-03-29 17:42:01 +02001631 if (args->flags & I915_MMAP_WC && !boot_cpu_has(X86_FEATURE_PAT))
Akash Goel1816f922015-01-02 16:29:30 +05301632 return -ENODEV;
1633
Chris Wilson03ac0642016-07-20 13:31:51 +01001634 obj = i915_gem_object_lookup(file, args->handle);
1635 if (!obj)
Chris Wilsonbf79cb92010-08-04 14:19:46 +01001636 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07001637
Daniel Vetter1286ff72012-05-10 15:25:09 +02001638 /* prime objects have no backing filp to GEM mmap
1639 * pages from.
1640 */
Chris Wilson03ac0642016-07-20 13:31:51 +01001641 if (!obj->base.filp) {
Chris Wilson794a11c2019-03-14 07:58:29 +00001642 addr = -ENXIO;
1643 goto err;
1644 }
1645
1646 if (range_overflows(args->offset, args->size, (u64)obj->base.size)) {
1647 addr = -EINVAL;
1648 goto err;
Daniel Vetter1286ff72012-05-10 15:25:09 +02001649 }
1650
Chris Wilson03ac0642016-07-20 13:31:51 +01001651 addr = vm_mmap(obj->base.filp, 0, args->size,
Eric Anholt673a3942008-07-30 12:06:12 -07001652 PROT_READ | PROT_WRITE, MAP_SHARED,
1653 args->offset);
Joonas Lahtinenebfb6972019-02-07 10:54:54 +02001654 if (IS_ERR_VALUE(addr))
1655 goto err;
1656
Akash Goel1816f922015-01-02 16:29:30 +05301657 if (args->flags & I915_MMAP_WC) {
1658 struct mm_struct *mm = current->mm;
1659 struct vm_area_struct *vma;
1660
Michal Hocko80a89a52016-05-23 16:26:11 -07001661 if (down_write_killable(&mm->mmap_sem)) {
Chris Wilson794a11c2019-03-14 07:58:29 +00001662 addr = -EINTR;
1663 goto err;
Michal Hocko80a89a52016-05-23 16:26:11 -07001664 }
Akash Goel1816f922015-01-02 16:29:30 +05301665 vma = find_vma(mm, addr);
Joonas Lahtinen5c4604e2019-02-07 10:54:53 +02001666 if (vma && __vma_matches(vma, obj->base.filp, addr, args->size))
Akash Goel1816f922015-01-02 16:29:30 +05301667 vma->vm_page_prot =
1668 pgprot_writecombine(vm_get_page_prot(vma->vm_flags));
1669 else
1670 addr = -ENOMEM;
1671 up_write(&mm->mmap_sem);
Joonas Lahtinenebfb6972019-02-07 10:54:54 +02001672 if (IS_ERR_VALUE(addr))
1673 goto err;
Chris Wilsonaeecc962016-06-17 14:46:39 -03001674
1675 /* This may race, but that's ok, it only gets set */
Chris Wilson50349242016-08-18 17:17:04 +01001676 WRITE_ONCE(obj->frontbuffer_ggtt_origin, ORIGIN_CPU);
Akash Goel1816f922015-01-02 16:29:30 +05301677 }
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001678 i915_gem_object_put(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001679
Jani Nikula739f3ab2019-01-16 11:15:19 +02001680 args->addr_ptr = (u64)addr;
Eric Anholt673a3942008-07-30 12:06:12 -07001681 return 0;
Joonas Lahtinenebfb6972019-02-07 10:54:54 +02001682
1683err:
1684 i915_gem_object_put(obj);
Joonas Lahtinenebfb6972019-02-07 10:54:54 +02001685 return addr;
Eric Anholt673a3942008-07-30 12:06:12 -07001686}
1687
Chris Wilsond899ace2018-07-25 16:54:47 +01001688static unsigned int tile_row_pages(const struct drm_i915_gem_object *obj)
Chris Wilson03af84f2016-08-18 17:17:01 +01001689{
Chris Wilson6649a0b2017-01-09 16:16:08 +00001690 return i915_gem_object_get_tile_row_size(obj) >> PAGE_SHIFT;
Chris Wilson03af84f2016-08-18 17:17:01 +01001691}
1692
Jesse Barnesde151cf2008-11-12 10:03:55 -08001693/**
Chris Wilson4cc69072016-08-25 19:05:19 +01001694 * i915_gem_mmap_gtt_version - report the current feature set for GTT mmaps
1695 *
1696 * A history of the GTT mmap interface:
1697 *
1698 * 0 - Everything had to fit into the GTT. Both parties of a memcpy had to
1699 * aligned and suitable for fencing, and still fit into the available
1700 * mappable space left by the pinned display objects. A classic problem
1701 * we called the page-fault-of-doom where we would ping-pong between
1702 * two objects that could not fit inside the GTT and so the memcpy
1703 * would page one object in at the expense of the other between every
1704 * single byte.
1705 *
1706 * 1 - Objects can be any size, and have any compatible fencing (X Y, or none
1707 * as set via i915_gem_set_tiling() [DRM_I915_GEM_SET_TILING]). If the
1708 * object is too large for the available space (or simply too large
1709 * for the mappable aperture!), a view is created instead and faulted
1710 * into userspace. (This view is aligned and sized appropriately for
1711 * fenced access.)
1712 *
Chris Wilsone22d8e32017-04-12 12:01:11 +01001713 * 2 - Recognise WC as a separate cache domain so that we can flush the
1714 * delayed writes via GTT before performing direct access via WC.
1715 *
Chris Wilson4cc69072016-08-25 19:05:19 +01001716 * Restrictions:
1717 *
1718 * * snoopable objects cannot be accessed via the GTT. It can cause machine
1719 * hangs on some architectures, corruption on others. An attempt to service
1720 * a GTT page fault from a snoopable object will generate a SIGBUS.
1721 *
1722 * * the object must be able to fit into RAM (physical memory, though no
1723 * limited to the mappable aperture).
1724 *
1725 *
1726 * Caveats:
1727 *
1728 * * a new GTT page fault will synchronize rendering from the GPU and flush
1729 * all data to system memory. Subsequent access will not be synchronized.
1730 *
1731 * * all mappings are revoked on runtime device suspend.
1732 *
1733 * * there are only 8, 16 or 32 fence registers to share between all users
1734 * (older machines require fence register for display and blitter access
1735 * as well). Contention of the fence registers will cause the previous users
1736 * to be unmapped and any new access will generate new page faults.
1737 *
1738 * * running out of memory while servicing a fault may generate a SIGBUS,
1739 * rather than the expected SIGSEGV.
1740 */
1741int i915_gem_mmap_gtt_version(void)
1742{
Chris Wilsone22d8e32017-04-12 12:01:11 +01001743 return 2;
Chris Wilson4cc69072016-08-25 19:05:19 +01001744}
1745
Chris Wilson2d4281b2017-01-10 09:56:32 +00001746static inline struct i915_ggtt_view
Chris Wilsond899ace2018-07-25 16:54:47 +01001747compute_partial_view(const struct drm_i915_gem_object *obj,
Chris Wilson2d4281b2017-01-10 09:56:32 +00001748 pgoff_t page_offset,
1749 unsigned int chunk)
1750{
1751 struct i915_ggtt_view view;
1752
1753 if (i915_gem_object_is_tiled(obj))
1754 chunk = roundup(chunk, tile_row_pages(obj));
1755
Chris Wilson2d4281b2017-01-10 09:56:32 +00001756 view.type = I915_GGTT_VIEW_PARTIAL;
Chris Wilson8bab11932017-01-14 00:28:25 +00001757 view.partial.offset = rounddown(page_offset, chunk);
1758 view.partial.size =
Chris Wilson2d4281b2017-01-10 09:56:32 +00001759 min_t(unsigned int, chunk,
Chris Wilson8bab11932017-01-14 00:28:25 +00001760 (obj->base.size >> PAGE_SHIFT) - view.partial.offset);
Chris Wilson2d4281b2017-01-10 09:56:32 +00001761
1762 /* If the partial covers the entire object, just create a normal VMA. */
1763 if (chunk >= obj->base.size >> PAGE_SHIFT)
1764 view.type = I915_GGTT_VIEW_NORMAL;
1765
1766 return view;
1767}
1768
Chris Wilson4cc69072016-08-25 19:05:19 +01001769/**
Jesse Barnesde151cf2008-11-12 10:03:55 -08001770 * i915_gem_fault - fault a page into the GTT
Geliang Tangd9072a32015-09-15 05:58:44 -07001771 * @vmf: fault info
Jesse Barnesde151cf2008-11-12 10:03:55 -08001772 *
1773 * The fault handler is set up by drm_gem_mmap() when a object is GTT mapped
1774 * from userspace. The fault handler takes care of binding the object to
1775 * the GTT (if needed), allocating and programming a fence register (again,
1776 * only if needed based on whether the old reg is still valid or the object
1777 * is tiled) and inserting a new PTE into the faulting process.
1778 *
1779 * Note that the faulting process may involve evicting existing objects
1780 * from the GTT and/or fence registers to make room. So performance may
1781 * suffer if the GTT working set is large or there are few fence registers
1782 * left.
Chris Wilson4cc69072016-08-25 19:05:19 +01001783 *
1784 * The current feature set supported by i915_gem_fault() and thus GTT mmaps
1785 * is exposed via I915_PARAM_MMAP_GTT_VERSION (see i915_gem_mmap_gtt_version).
Jesse Barnesde151cf2008-11-12 10:03:55 -08001786 */
Chris Wilson52137012018-06-06 22:45:20 +01001787vm_fault_t i915_gem_fault(struct vm_fault *vmf)
Jesse Barnesde151cf2008-11-12 10:03:55 -08001788{
Chris Wilson420980c2018-06-05 14:57:46 +01001789#define MIN_CHUNK_PAGES (SZ_1M >> PAGE_SHIFT)
Dave Jiang11bac802017-02-24 14:56:41 -08001790 struct vm_area_struct *area = vmf->vma;
Chris Wilson058d88c2016-08-15 10:49:06 +01001791 struct drm_i915_gem_object *obj = to_intel_bo(area->vm_private_data);
Chris Wilson05394f32010-11-08 19:18:58 +00001792 struct drm_device *dev = obj->base.dev;
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03001793 struct drm_i915_private *dev_priv = to_i915(dev);
1794 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Chris Wilsonaae7c062018-09-03 09:33:34 +01001795 bool write = area->vm_flags & VM_WRITE;
Chris Wilson538ef962019-01-14 14:21:18 +00001796 intel_wakeref_t wakeref;
Chris Wilson058d88c2016-08-15 10:49:06 +01001797 struct i915_vma *vma;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001798 pgoff_t page_offset;
Chris Wilson2caffbf2019-02-08 15:37:03 +00001799 int srcu;
Chris Wilsonb8f90962016-08-05 10:14:07 +01001800 int ret;
Paulo Zanonif65c9162013-11-27 18:20:34 -02001801
Chris Wilson3e977ac2018-07-12 19:53:13 +01001802 /* Sanity check that we allow writing into this object */
1803 if (i915_gem_object_is_readonly(obj) && write)
1804 return VM_FAULT_SIGBUS;
1805
Jesse Barnesde151cf2008-11-12 10:03:55 -08001806 /* We don't use vmf->pgoff since that has the fake offset */
Jan Kara1a29d852016-12-14 15:07:01 -08001807 page_offset = (vmf->address - area->vm_start) >> PAGE_SHIFT;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001808
Chris Wilsondb53a302011-02-03 11:57:46 +00001809 trace_i915_gem_object_fault(obj, page_offset, true, write);
1810
Chris Wilson6e4930f2014-02-07 18:37:06 -02001811 /* Try to flush the object off the GPU first without holding the lock.
Chris Wilsonb8f90962016-08-05 10:14:07 +01001812 * Upon acquiring the lock, we will perform our sanity checks and then
Chris Wilson6e4930f2014-02-07 18:37:06 -02001813 * repeat the flush holding the lock in the normal manner to catch cases
1814 * where we are gazumped.
1815 */
Chris Wilsone95433c2016-10-28 13:58:27 +01001816 ret = i915_gem_object_wait(obj,
1817 I915_WAIT_INTERRUPTIBLE,
Chris Wilson62eb3c22019-02-13 09:25:04 +00001818 MAX_SCHEDULE_TIMEOUT);
Chris Wilson6e4930f2014-02-07 18:37:06 -02001819 if (ret)
Chris Wilsonb8f90962016-08-05 10:14:07 +01001820 goto err;
1821
Chris Wilson40e62d52016-10-28 13:58:41 +01001822 ret = i915_gem_object_pin_pages(obj);
1823 if (ret)
1824 goto err;
1825
Chris Wilson538ef962019-01-14 14:21:18 +00001826 wakeref = intel_runtime_pm_get(dev_priv);
Chris Wilsonb8f90962016-08-05 10:14:07 +01001827
Chris Wilson43a8f682019-02-21 10:29:19 +00001828 srcu = i915_reset_trylock(dev_priv);
1829 if (srcu < 0) {
1830 ret = srcu;
1831 goto err_rpm;
1832 }
1833
Chris Wilsonb8f90962016-08-05 10:14:07 +01001834 ret = i915_mutex_lock_interruptible(dev);
1835 if (ret)
Chris Wilson43a8f682019-02-21 10:29:19 +00001836 goto err_reset;
Chris Wilson6e4930f2014-02-07 18:37:06 -02001837
Chris Wilsoneb119bd2012-12-16 12:43:36 +00001838 /* Access to snoopable pages through the GTT is incoherent. */
Tvrtko Ursulin0031fb92016-11-04 14:42:44 +00001839 if (obj->cache_level != I915_CACHE_NONE && !HAS_LLC(dev_priv)) {
Chris Wilsonddeff6e2014-05-28 16:16:41 +01001840 ret = -EFAULT;
Chris Wilsonb8f90962016-08-05 10:14:07 +01001841 goto err_unlock;
Chris Wilsoneb119bd2012-12-16 12:43:36 +00001842 }
1843
Chris Wilsona61007a2016-08-18 17:17:02 +01001844 /* Now pin it into the GTT as needed */
Chris Wilson7e7367d2018-06-30 10:05:09 +01001845 vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0,
1846 PIN_MAPPABLE |
1847 PIN_NONBLOCK |
1848 PIN_NONFAULT);
Chris Wilsona61007a2016-08-18 17:17:02 +01001849 if (IS_ERR(vma)) {
Chris Wilsona61007a2016-08-18 17:17:02 +01001850 /* Use a partial view if it is bigger than available space */
Chris Wilson2d4281b2017-01-10 09:56:32 +00001851 struct i915_ggtt_view view =
Chris Wilson8201c1f2017-01-10 09:56:33 +00001852 compute_partial_view(obj, page_offset, MIN_CHUNK_PAGES);
Chris Wilson7e7367d2018-06-30 10:05:09 +01001853 unsigned int flags;
Chris Wilsonaa136d92016-08-18 17:17:03 +01001854
Chris Wilson7e7367d2018-06-30 10:05:09 +01001855 flags = PIN_MAPPABLE;
1856 if (view.type == I915_GGTT_VIEW_NORMAL)
1857 flags |= PIN_NONBLOCK; /* avoid warnings for pinned */
1858
1859 /*
1860 * Userspace is now writing through an untracked VMA, abandon
Chris Wilson50349242016-08-18 17:17:04 +01001861 * all hope that the hardware is able to track future writes.
1862 */
1863 obj->frontbuffer_ggtt_origin = ORIGIN_CPU;
1864
Chris Wilson7e7367d2018-06-30 10:05:09 +01001865 vma = i915_gem_object_ggtt_pin(obj, &view, 0, 0, flags);
1866 if (IS_ERR(vma) && !view.type) {
1867 flags = PIN_MAPPABLE;
1868 view.type = I915_GGTT_VIEW_PARTIAL;
1869 vma = i915_gem_object_ggtt_pin(obj, &view, 0, 0, flags);
1870 }
Chris Wilsona61007a2016-08-18 17:17:02 +01001871 }
Chris Wilson058d88c2016-08-15 10:49:06 +01001872 if (IS_ERR(vma)) {
1873 ret = PTR_ERR(vma);
Chris Wilsonb8f90962016-08-05 10:14:07 +01001874 goto err_unlock;
Chris Wilson058d88c2016-08-15 10:49:06 +01001875 }
Jesse Barnesde151cf2008-11-12 10:03:55 -08001876
Chris Wilsonc9839302012-11-20 10:45:17 +00001877 ret = i915_gem_object_set_to_gtt_domain(obj, write);
1878 if (ret)
Chris Wilsonb8f90962016-08-05 10:14:07 +01001879 goto err_unpin;
Chris Wilsonc9839302012-11-20 10:45:17 +00001880
Chris Wilsonaeaaa552019-02-12 13:08:30 +00001881 ret = i915_vma_pin_fence(vma);
1882 if (ret)
1883 goto err_unpin;
1884
Chris Wilsonb90b91d2014-06-10 12:14:40 +01001885 /* Finally, remap it using the new GTT offset */
Chris Wilsonc58305a2016-08-19 16:54:28 +01001886 ret = remap_io_mapping(area,
Chris Wilson8bab11932017-01-14 00:28:25 +00001887 area->vm_start + (vma->ggtt_view.partial.offset << PAGE_SHIFT),
Matthew Auld73ebd502017-12-11 15:18:20 +00001888 (ggtt->gmadr.start + vma->node.start) >> PAGE_SHIFT,
Chris Wilsonc58305a2016-08-19 16:54:28 +01001889 min_t(u64, vma->size, area->vm_end - area->vm_start),
Matthew Auld73ebd502017-12-11 15:18:20 +00001890 &ggtt->iomap);
Chris Wilsona65adaf2017-10-09 09:43:57 +01001891 if (ret)
Chris Wilson43a8f682019-02-21 10:29:19 +00001892 goto err_fence;
Chris Wilsona61007a2016-08-18 17:17:02 +01001893
Chris Wilsona65adaf2017-10-09 09:43:57 +01001894 /* Mark as being mmapped into userspace for later revocation */
1895 assert_rpm_wakelock_held(dev_priv);
1896 if (!i915_vma_set_userfault(vma) && !obj->userfault_count++)
1897 list_add(&obj->userfault_link, &dev_priv->mm.userfault_list);
1898 GEM_BUG_ON(!obj->userfault_count);
1899
Chris Wilson7125397b2017-12-06 12:49:14 +00001900 i915_vma_set_ggtt_write(vma);
1901
Chris Wilsonaeaaa552019-02-12 13:08:30 +00001902err_fence:
1903 i915_vma_unpin_fence(vma);
Chris Wilsonb8f90962016-08-05 10:14:07 +01001904err_unpin:
Chris Wilson058d88c2016-08-15 10:49:06 +01001905 __i915_vma_unpin(vma);
Chris Wilsonb8f90962016-08-05 10:14:07 +01001906err_unlock:
Jesse Barnesde151cf2008-11-12 10:03:55 -08001907 mutex_unlock(&dev->struct_mutex);
Chris Wilson43a8f682019-02-21 10:29:19 +00001908err_reset:
1909 i915_reset_unlock(dev_priv, srcu);
Chris Wilsonb8f90962016-08-05 10:14:07 +01001910err_rpm:
Chris Wilson538ef962019-01-14 14:21:18 +00001911 intel_runtime_pm_put(dev_priv, wakeref);
Chris Wilson40e62d52016-10-28 13:58:41 +01001912 i915_gem_object_unpin_pages(obj);
Chris Wilsonb8f90962016-08-05 10:14:07 +01001913err:
Jesse Barnesde151cf2008-11-12 10:03:55 -08001914 switch (ret) {
Chris Wilsond9bc7e92011-02-07 13:09:31 +00001915 case -EIO:
Daniel Vetter2232f032014-09-04 09:36:18 +02001916 /*
1917 * We eat errors when the gpu is terminally wedged to avoid
1918 * userspace unduly crashing (gl has no provisions for mmaps to
1919 * fail). But any other -EIO isn't ours (e.g. swap in failure)
1920 * and so needs to be reported.
1921 */
Chris Wilsonc41166f2019-02-20 14:56:37 +00001922 if (!i915_terminally_wedged(dev_priv))
Chris Wilson52137012018-06-06 22:45:20 +01001923 return VM_FAULT_SIGBUS;
Gustavo A. R. Silvaf0d759f2018-06-28 17:35:41 -05001924 /* else: fall through */
Chris Wilson045e7692010-11-07 09:18:22 +00001925 case -EAGAIN:
Daniel Vetter571c6082013-09-12 17:57:28 +02001926 /*
1927 * EAGAIN means the gpu is hung and we'll wait for the error
1928 * handler to reset everything when re-faulting in
1929 * i915_mutex_lock_interruptible.
Chris Wilsond9bc7e92011-02-07 13:09:31 +00001930 */
Chris Wilsonc7150892009-09-23 00:43:56 +01001931 case 0:
1932 case -ERESTARTSYS:
Chris Wilsonbed636a2011-02-11 20:31:19 +00001933 case -EINTR:
Dmitry Rogozhkine79e0fe2012-10-03 17:15:26 +03001934 case -EBUSY:
1935 /*
1936 * EBUSY is ok: this just means that another thread
1937 * already did the job.
1938 */
Chris Wilson52137012018-06-06 22:45:20 +01001939 return VM_FAULT_NOPAGE;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001940 case -ENOMEM:
Chris Wilson52137012018-06-06 22:45:20 +01001941 return VM_FAULT_OOM;
Daniel Vettera7c2e1a2012-10-17 11:17:16 +02001942 case -ENOSPC:
Chris Wilson45d67812014-01-31 11:34:57 +00001943 case -EFAULT:
Chris Wilson52137012018-06-06 22:45:20 +01001944 return VM_FAULT_SIGBUS;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001945 default:
Daniel Vettera7c2e1a2012-10-17 11:17:16 +02001946 WARN_ONCE(ret, "unhandled error in i915_gem_fault: %i\n", ret);
Chris Wilson52137012018-06-06 22:45:20 +01001947 return VM_FAULT_SIGBUS;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001948 }
1949}
1950
Chris Wilsona65adaf2017-10-09 09:43:57 +01001951static void __i915_gem_object_release_mmap(struct drm_i915_gem_object *obj)
1952{
1953 struct i915_vma *vma;
1954
1955 GEM_BUG_ON(!obj->userfault_count);
1956
1957 obj->userfault_count = 0;
1958 list_del(&obj->userfault_link);
1959 drm_vma_node_unmap(&obj->base.vma_node,
1960 obj->base.dev->anon_inode->i_mapping);
1961
Chris Wilsone2189dd2017-12-07 21:14:07 +00001962 for_each_ggtt_vma(vma, obj)
Chris Wilsona65adaf2017-10-09 09:43:57 +01001963 i915_vma_unset_userfault(vma);
Chris Wilsona65adaf2017-10-09 09:43:57 +01001964}
1965
Jesse Barnesde151cf2008-11-12 10:03:55 -08001966/**
Chris Wilson901782b2009-07-10 08:18:50 +01001967 * i915_gem_release_mmap - remove physical page mappings
1968 * @obj: obj in question
1969 *
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02001970 * Preserve the reservation of the mmapping with the DRM core code, but
Chris Wilson901782b2009-07-10 08:18:50 +01001971 * relinquish ownership of the pages back to the system.
1972 *
1973 * It is vital that we remove the page mapping if we have mapped a tiled
1974 * object through the GTT and then lose the fence register due to
1975 * resource pressure. Similarly if the object has been moved out of the
1976 * aperture, than pages mapped into userspace must be revoked. Removing the
1977 * mapping will then trigger a page fault on the next user access, allowing
1978 * fixup by i915_gem_fault().
1979 */
Eric Anholtd05ca302009-07-10 13:02:26 -07001980void
Chris Wilson05394f32010-11-08 19:18:58 +00001981i915_gem_release_mmap(struct drm_i915_gem_object *obj)
Chris Wilson901782b2009-07-10 08:18:50 +01001982{
Chris Wilson275f0392016-10-24 13:42:14 +01001983 struct drm_i915_private *i915 = to_i915(obj->base.dev);
Chris Wilson538ef962019-01-14 14:21:18 +00001984 intel_wakeref_t wakeref;
Chris Wilson275f0392016-10-24 13:42:14 +01001985
Chris Wilson349f2cc2016-04-13 17:35:12 +01001986 /* Serialisation between user GTT access and our code depends upon
1987 * revoking the CPU's PTE whilst the mutex is held. The next user
1988 * pagefault then has to wait until we release the mutex.
Chris Wilson9c870d02016-10-24 13:42:15 +01001989 *
1990 * Note that RPM complicates somewhat by adding an additional
1991 * requirement that operations to the GGTT be made holding the RPM
1992 * wakeref.
Chris Wilson349f2cc2016-04-13 17:35:12 +01001993 */
Chris Wilson275f0392016-10-24 13:42:14 +01001994 lockdep_assert_held(&i915->drm.struct_mutex);
Chris Wilson538ef962019-01-14 14:21:18 +00001995 wakeref = intel_runtime_pm_get(i915);
Chris Wilson349f2cc2016-04-13 17:35:12 +01001996
Chris Wilsona65adaf2017-10-09 09:43:57 +01001997 if (!obj->userfault_count)
Chris Wilson9c870d02016-10-24 13:42:15 +01001998 goto out;
Chris Wilson901782b2009-07-10 08:18:50 +01001999
Chris Wilsona65adaf2017-10-09 09:43:57 +01002000 __i915_gem_object_release_mmap(obj);
Chris Wilson349f2cc2016-04-13 17:35:12 +01002001
2002 /* Ensure that the CPU's PTE are revoked and there are not outstanding
2003 * memory transactions from userspace before we return. The TLB
2004 * flushing implied above by changing the PTE above *should* be
2005 * sufficient, an extra barrier here just provides us with a bit
2006 * of paranoid documentation about our requirement to serialise
2007 * memory writes before touching registers / GSM.
2008 */
2009 wmb();
Chris Wilson9c870d02016-10-24 13:42:15 +01002010
2011out:
Chris Wilson538ef962019-01-14 14:21:18 +00002012 intel_runtime_pm_put(i915, wakeref);
Chris Wilson901782b2009-07-10 08:18:50 +01002013}
2014
Chris Wilson7c108fd2016-10-24 13:42:18 +01002015void i915_gem_runtime_suspend(struct drm_i915_private *dev_priv)
Chris Wilsoneedd10f2014-06-16 08:57:44 +01002016{
Chris Wilson3594a3e2016-10-24 13:42:16 +01002017 struct drm_i915_gem_object *obj, *on;
Chris Wilson7c108fd2016-10-24 13:42:18 +01002018 int i;
Chris Wilsoneedd10f2014-06-16 08:57:44 +01002019
Chris Wilson3594a3e2016-10-24 13:42:16 +01002020 /*
2021 * Only called during RPM suspend. All users of the userfault_list
2022 * must be holding an RPM wakeref to ensure that this can not
2023 * run concurrently with themselves (and use the struct_mutex for
2024 * protection between themselves).
2025 */
2026
2027 list_for_each_entry_safe(obj, on,
Chris Wilsona65adaf2017-10-09 09:43:57 +01002028 &dev_priv->mm.userfault_list, userfault_link)
2029 __i915_gem_object_release_mmap(obj);
Chris Wilson7c108fd2016-10-24 13:42:18 +01002030
2031 /* The fence will be lost when the device powers down. If any were
2032 * in use by hardware (i.e. they are pinned), we should not be powering
2033 * down! All other fences will be reacquired by the user upon waking.
2034 */
2035 for (i = 0; i < dev_priv->num_fence_regs; i++) {
2036 struct drm_i915_fence_reg *reg = &dev_priv->fence_regs[i];
2037
Chris Wilsone0ec3ec2017-02-03 12:57:17 +00002038 /* Ideally we want to assert that the fence register is not
2039 * live at this point (i.e. that no piece of code will be
2040 * trying to write through fence + GTT, as that both violates
2041 * our tracking of activity and associated locking/barriers,
2042 * but also is illegal given that the hw is powered down).
2043 *
2044 * Previously we used reg->pin_count as a "liveness" indicator.
2045 * That is not sufficient, and we need a more fine-grained
2046 * tool if we want to have a sanity check here.
2047 */
Chris Wilson7c108fd2016-10-24 13:42:18 +01002048
2049 if (!reg->vma)
2050 continue;
2051
Chris Wilsona65adaf2017-10-09 09:43:57 +01002052 GEM_BUG_ON(i915_vma_has_userfault(reg->vma));
Chris Wilson7c108fd2016-10-24 13:42:18 +01002053 reg->dirty = true;
2054 }
Chris Wilsoneedd10f2014-06-16 08:57:44 +01002055}
2056
Chris Wilsond8cb5082012-08-11 15:41:03 +01002057static int i915_gem_object_create_mmap_offset(struct drm_i915_gem_object *obj)
2058{
Chris Wilsonfac5e232016-07-04 11:34:36 +01002059 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
Chris Wilsonf3f61842016-08-05 10:14:14 +01002060 int err;
Chris Wilsond8cb5082012-08-11 15:41:03 +01002061
Chris Wilsonf3f61842016-08-05 10:14:14 +01002062 err = drm_gem_create_mmap_offset(&obj->base);
Chris Wilsonb42a13d2017-01-06 15:22:40 +00002063 if (likely(!err))
Chris Wilsonf3f61842016-08-05 10:14:14 +01002064 return 0;
Daniel Vetterda494d72012-12-20 15:11:16 +01002065
Chris Wilsonb42a13d2017-01-06 15:22:40 +00002066 /* Attempt to reap some mmap space from dead objects */
2067 do {
Chris Wilsonec625fb2018-07-09 13:20:42 +01002068 err = i915_gem_wait_for_idle(dev_priv,
2069 I915_WAIT_INTERRUPTIBLE,
2070 MAX_SCHEDULE_TIMEOUT);
Chris Wilsonb42a13d2017-01-06 15:22:40 +00002071 if (err)
2072 break;
Chris Wilsond8cb5082012-08-11 15:41:03 +01002073
Chris Wilsonb42a13d2017-01-06 15:22:40 +00002074 i915_gem_drain_freed_objects(dev_priv);
Chris Wilsonf3f61842016-08-05 10:14:14 +01002075 err = drm_gem_create_mmap_offset(&obj->base);
Chris Wilsonb42a13d2017-01-06 15:22:40 +00002076 if (!err)
2077 break;
2078
2079 } while (flush_delayed_work(&dev_priv->gt.retire_work));
Daniel Vetterda494d72012-12-20 15:11:16 +01002080
Chris Wilsonf3f61842016-08-05 10:14:14 +01002081 return err;
Chris Wilsond8cb5082012-08-11 15:41:03 +01002082}
2083
2084static void i915_gem_object_free_mmap_offset(struct drm_i915_gem_object *obj)
2085{
Chris Wilsond8cb5082012-08-11 15:41:03 +01002086 drm_gem_free_mmap_offset(&obj->base);
2087}
2088
Dave Airlieda6b51d2014-12-24 13:11:17 +10002089int
Dave Airlieff72145b2011-02-07 12:16:14 +10002090i915_gem_mmap_gtt(struct drm_file *file,
2091 struct drm_device *dev,
Jani Nikula739f3ab2019-01-16 11:15:19 +02002092 u32 handle,
2093 u64 *offset)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002094{
Chris Wilson05394f32010-11-08 19:18:58 +00002095 struct drm_i915_gem_object *obj;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002096 int ret;
2097
Chris Wilson03ac0642016-07-20 13:31:51 +01002098 obj = i915_gem_object_lookup(file, handle);
Chris Wilsonf3f61842016-08-05 10:14:14 +01002099 if (!obj)
2100 return -ENOENT;
Chris Wilsonab182822009-09-22 18:46:17 +01002101
Chris Wilsond8cb5082012-08-11 15:41:03 +01002102 ret = i915_gem_object_create_mmap_offset(obj);
Chris Wilsonf3f61842016-08-05 10:14:14 +01002103 if (ret == 0)
2104 *offset = drm_vma_node_offset_addr(&obj->base.vma_node);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002105
Chris Wilsonf0cd5182016-10-28 13:58:43 +01002106 i915_gem_object_put(obj);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01002107 return ret;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002108}
2109
Dave Airlieff72145b2011-02-07 12:16:14 +10002110/**
2111 * i915_gem_mmap_gtt_ioctl - prepare an object for GTT mmap'ing
2112 * @dev: DRM device
2113 * @data: GTT mapping ioctl data
2114 * @file: GEM object info
2115 *
2116 * Simply returns the fake offset to userspace so it can mmap it.
2117 * The mmap call will end up in drm_gem_mmap(), which will set things
2118 * up so we can get faults in the handler above.
2119 *
2120 * The fault handler will take care of binding the object into the GTT
2121 * (since it may have been evicted to make room for something), allocating
2122 * a fence register, and mapping the appropriate aperture address into
2123 * userspace.
2124 */
2125int
2126i915_gem_mmap_gtt_ioctl(struct drm_device *dev, void *data,
2127 struct drm_file *file)
2128{
2129 struct drm_i915_gem_mmap_gtt *args = data;
2130
Dave Airlieda6b51d2014-12-24 13:11:17 +10002131 return i915_gem_mmap_gtt(file, dev, args->handle, &args->offset);
Dave Airlieff72145b2011-02-07 12:16:14 +10002132}
2133
Daniel Vetter225067e2012-08-20 10:23:20 +02002134/* Immediately discard the backing storage */
2135static void
2136i915_gem_object_truncate(struct drm_i915_gem_object *obj)
Chris Wilsone5281cc2010-10-28 13:45:36 +01002137{
Chris Wilson4d6294bf2012-08-11 15:41:05 +01002138 i915_gem_object_free_mmap_offset(obj);
Daniel Vetter1286ff72012-05-10 15:25:09 +02002139
Chris Wilson4d6294bf2012-08-11 15:41:05 +01002140 if (obj->base.filp == NULL)
2141 return;
2142
Daniel Vetter225067e2012-08-20 10:23:20 +02002143 /* Our goal here is to return as much of the memory as
2144 * is possible back to the system as we are called from OOM.
2145 * To do this we must instruct the shmfs to drop all of its
2146 * backing pages, *now*.
Chris Wilsone5281cc2010-10-28 13:45:36 +01002147 */
Chris Wilson55372522014-03-25 13:23:06 +00002148 shmem_truncate_range(file_inode(obj->base.filp), 0, (loff_t)-1);
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002149 obj->mm.madv = __I915_MADV_PURGED;
Chris Wilson4e5462e2017-03-07 13:20:31 +00002150 obj->mm.pages = ERR_PTR(-EFAULT);
Chris Wilsone5281cc2010-10-28 13:45:36 +01002151}
Chris Wilsone5281cc2010-10-28 13:45:36 +01002152
Chris Wilson55372522014-03-25 13:23:06 +00002153/* Try to discard unwanted pages */
Chris Wilson03ac84f2016-10-28 13:58:36 +01002154void __i915_gem_object_invalidate(struct drm_i915_gem_object *obj)
Daniel Vetter225067e2012-08-20 10:23:20 +02002155{
Chris Wilson55372522014-03-25 13:23:06 +00002156 struct address_space *mapping;
2157
Chris Wilson1233e2d2016-10-28 13:58:37 +01002158 lockdep_assert_held(&obj->mm.lock);
Chris Wilsonf1fa4f42017-10-13 21:26:13 +01002159 GEM_BUG_ON(i915_gem_object_has_pages(obj));
Chris Wilson1233e2d2016-10-28 13:58:37 +01002160
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002161 switch (obj->mm.madv) {
Chris Wilson55372522014-03-25 13:23:06 +00002162 case I915_MADV_DONTNEED:
2163 i915_gem_object_truncate(obj);
2164 case __I915_MADV_PURGED:
2165 return;
2166 }
2167
2168 if (obj->base.filp == NULL)
2169 return;
2170
Al Viro93c76a32015-12-04 23:45:44 -05002171 mapping = obj->base.filp->f_mapping,
Chris Wilson55372522014-03-25 13:23:06 +00002172 invalidate_mapping_pages(mapping, 0, (loff_t)-1);
Chris Wilsone5281cc2010-10-28 13:45:36 +01002173}
2174
Kuo-Hsin Yang64e3d122018-11-06 13:23:24 +00002175/*
2176 * Move pages to appropriate lru and release the pagevec, decrementing the
2177 * ref count of those pages.
2178 */
2179static void check_release_pagevec(struct pagevec *pvec)
2180{
2181 check_move_unevictable_pages(pvec);
2182 __pagevec_release(pvec);
2183 cond_resched();
2184}
2185
Chris Wilson5cdf5882010-09-27 15:51:07 +01002186static void
Chris Wilson03ac84f2016-10-28 13:58:36 +01002187i915_gem_object_put_pages_gtt(struct drm_i915_gem_object *obj,
2188 struct sg_table *pages)
Eric Anholt673a3942008-07-30 12:06:12 -07002189{
Dave Gordon85d12252016-05-20 11:54:06 +01002190 struct sgt_iter sgt_iter;
Kuo-Hsin Yang64e3d122018-11-06 13:23:24 +00002191 struct pagevec pvec;
Dave Gordon85d12252016-05-20 11:54:06 +01002192 struct page *page;
Daniel Vetter1286ff72012-05-10 15:25:09 +02002193
Chris Wilsone5facdf2016-12-23 14:57:57 +00002194 __i915_gem_object_release_shmem(obj, pages, true);
Eric Anholt856fa192009-03-19 14:10:50 -07002195
Chris Wilson03ac84f2016-10-28 13:58:36 +01002196 i915_gem_gtt_finish_pages(obj, pages);
Imre Deake2273302015-07-09 12:59:05 +03002197
Daniel Vetter6dacfd22011-09-12 21:30:02 +02002198 if (i915_gem_object_needs_bit17_swizzle(obj))
Chris Wilson03ac84f2016-10-28 13:58:36 +01002199 i915_gem_object_save_bit_17_swizzle(obj, pages);
Eric Anholt280b7132009-03-12 16:56:27 -07002200
Kuo-Hsin Yang64e3d122018-11-06 13:23:24 +00002201 mapping_clear_unevictable(file_inode(obj->base.filp)->i_mapping);
2202
2203 pagevec_init(&pvec);
Chris Wilson03ac84f2016-10-28 13:58:36 +01002204 for_each_sgt_page(page, sgt_iter, pages) {
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002205 if (obj->mm.dirty)
Chris Wilson9da3da62012-06-01 15:20:22 +01002206 set_page_dirty(page);
Chris Wilson3ef94da2009-09-14 16:50:29 +01002207
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002208 if (obj->mm.madv == I915_MADV_WILLNEED)
Chris Wilson9da3da62012-06-01 15:20:22 +01002209 mark_page_accessed(page);
Chris Wilson3ef94da2009-09-14 16:50:29 +01002210
Kuo-Hsin Yang64e3d122018-11-06 13:23:24 +00002211 if (!pagevec_add(&pvec, page))
2212 check_release_pagevec(&pvec);
Chris Wilson3ef94da2009-09-14 16:50:29 +01002213 }
Kuo-Hsin Yang64e3d122018-11-06 13:23:24 +00002214 if (pagevec_count(&pvec))
2215 check_release_pagevec(&pvec);
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002216 obj->mm.dirty = false;
Eric Anholt673a3942008-07-30 12:06:12 -07002217
Chris Wilson03ac84f2016-10-28 13:58:36 +01002218 sg_free_table(pages);
2219 kfree(pages);
Chris Wilson37e680a2012-06-07 15:38:42 +01002220}
2221
Chris Wilson96d77632016-10-28 13:58:33 +01002222static void __i915_gem_object_reset_page_iter(struct drm_i915_gem_object *obj)
2223{
2224 struct radix_tree_iter iter;
Ville Syrjäläc23aa712017-09-01 20:12:51 +03002225 void __rcu **slot;
Chris Wilson96d77632016-10-28 13:58:33 +01002226
Chris Wilsonbea6e982017-10-26 14:00:31 +01002227 rcu_read_lock();
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002228 radix_tree_for_each_slot(slot, &obj->mm.get_page.radix, &iter, 0)
2229 radix_tree_delete(&obj->mm.get_page.radix, iter.index);
Chris Wilsonbea6e982017-10-26 14:00:31 +01002230 rcu_read_unlock();
Chris Wilson96d77632016-10-28 13:58:33 +01002231}
2232
Chris Wilsonacd1c1e2018-06-11 08:55:32 +01002233static struct sg_table *
2234__i915_gem_object_unset_pages(struct drm_i915_gem_object *obj)
Chris Wilson37e680a2012-06-07 15:38:42 +01002235{
Chris Wilsonf2123812017-10-16 12:40:37 +01002236 struct drm_i915_private *i915 = to_i915(obj->base.dev);
Chris Wilson03ac84f2016-10-28 13:58:36 +01002237 struct sg_table *pages;
Chris Wilson37e680a2012-06-07 15:38:42 +01002238
Chris Wilson03ac84f2016-10-28 13:58:36 +01002239 pages = fetch_and_zero(&obj->mm.pages);
Chris Wilson484d9a82019-01-15 12:44:42 +00002240 if (IS_ERR_OR_NULL(pages))
2241 return pages;
Chris Wilsona2165e32012-12-03 11:49:00 +00002242
Chris Wilsonf2123812017-10-16 12:40:37 +01002243 spin_lock(&i915->mm.obj_lock);
2244 list_del(&obj->mm.link);
2245 spin_unlock(&i915->mm.obj_lock);
2246
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002247 if (obj->mm.mapping) {
Chris Wilson4b30cb22016-08-18 17:16:42 +01002248 void *ptr;
2249
Chris Wilson0ce81782017-05-17 13:09:59 +01002250 ptr = page_mask_bits(obj->mm.mapping);
Chris Wilson4b30cb22016-08-18 17:16:42 +01002251 if (is_vmalloc_addr(ptr))
2252 vunmap(ptr);
Chris Wilsonfb8621d2016-04-08 12:11:14 +01002253 else
Chris Wilson4b30cb22016-08-18 17:16:42 +01002254 kunmap(kmap_to_page(ptr));
2255
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002256 obj->mm.mapping = NULL;
Chris Wilson0a798eb2016-04-08 12:11:11 +01002257 }
2258
Chris Wilson96d77632016-10-28 13:58:33 +01002259 __i915_gem_object_reset_page_iter(obj);
Chris Wilsonacd1c1e2018-06-11 08:55:32 +01002260 obj->mm.page_sizes.phys = obj->mm.page_sizes.sg = 0;
Chris Wilson96d77632016-10-28 13:58:33 +01002261
Chris Wilsonacd1c1e2018-06-11 08:55:32 +01002262 return pages;
2263}
2264
Chris Wilson484d9a82019-01-15 12:44:42 +00002265int __i915_gem_object_put_pages(struct drm_i915_gem_object *obj,
2266 enum i915_mm_subclass subclass)
Chris Wilsonacd1c1e2018-06-11 08:55:32 +01002267{
2268 struct sg_table *pages;
Chris Wilson484d9a82019-01-15 12:44:42 +00002269 int ret;
Chris Wilsonacd1c1e2018-06-11 08:55:32 +01002270
2271 if (i915_gem_object_has_pinned_pages(obj))
Chris Wilson484d9a82019-01-15 12:44:42 +00002272 return -EBUSY;
Chris Wilsonacd1c1e2018-06-11 08:55:32 +01002273
2274 GEM_BUG_ON(obj->bind_count);
Chris Wilsonacd1c1e2018-06-11 08:55:32 +01002275
2276 /* May be called by shrinker from within get_pages() (on another bo) */
2277 mutex_lock_nested(&obj->mm.lock, subclass);
Chris Wilson484d9a82019-01-15 12:44:42 +00002278 if (unlikely(atomic_read(&obj->mm.pages_pin_count))) {
2279 ret = -EBUSY;
Chris Wilsonacd1c1e2018-06-11 08:55:32 +01002280 goto unlock;
Chris Wilson484d9a82019-01-15 12:44:42 +00002281 }
Chris Wilsonacd1c1e2018-06-11 08:55:32 +01002282
2283 /*
2284 * ->put_pages might need to allocate memory for the bit17 swizzle
2285 * array, hence protect them from being reaped by removing them from gtt
2286 * lists early.
2287 */
2288 pages = __i915_gem_object_unset_pages(obj);
Chris Wilson484d9a82019-01-15 12:44:42 +00002289
2290 /*
2291 * XXX Temporary hijinx to avoid updating all backends to handle
2292 * NULL pages. In the future, when we have more asynchronous
2293 * get_pages backends we should be better able to handle the
2294 * cancellation of the async task in a more uniform manner.
2295 */
2296 if (!pages && !i915_gem_object_needs_async_cancel(obj))
2297 pages = ERR_PTR(-EINVAL);
2298
Chris Wilson4e5462e2017-03-07 13:20:31 +00002299 if (!IS_ERR(pages))
2300 obj->ops->put_pages(obj, pages);
2301
Chris Wilson484d9a82019-01-15 12:44:42 +00002302 ret = 0;
Chris Wilson1233e2d2016-10-28 13:58:37 +01002303unlock:
2304 mutex_unlock(&obj->mm.lock);
Chris Wilson484d9a82019-01-15 12:44:42 +00002305
2306 return ret;
Chris Wilson6c085a72012-08-20 11:40:46 +02002307}
2308
Tvrtko Ursulinf8e57862018-09-26 09:03:53 +01002309bool i915_sg_trim(struct sg_table *orig_st)
Tvrtko Ursulin0c40ce12016-11-09 15:13:43 +00002310{
2311 struct sg_table new_st;
2312 struct scatterlist *sg, *new_sg;
2313 unsigned int i;
2314
2315 if (orig_st->nents == orig_st->orig_nents)
Chris Wilson935a2f72017-02-13 17:15:13 +00002316 return false;
Tvrtko Ursulin0c40ce12016-11-09 15:13:43 +00002317
Chris Wilson8bfc478f2016-12-23 14:57:58 +00002318 if (sg_alloc_table(&new_st, orig_st->nents, GFP_KERNEL | __GFP_NOWARN))
Chris Wilson935a2f72017-02-13 17:15:13 +00002319 return false;
Tvrtko Ursulin0c40ce12016-11-09 15:13:43 +00002320
2321 new_sg = new_st.sgl;
2322 for_each_sg(orig_st->sgl, sg, orig_st->nents, i) {
2323 sg_set_page(new_sg, sg_page(sg), sg->length, 0);
Matthew Auldc6d22ab2018-09-20 15:27:06 +01002324 sg_dma_address(new_sg) = sg_dma_address(sg);
2325 sg_dma_len(new_sg) = sg_dma_len(sg);
2326
Tvrtko Ursulin0c40ce12016-11-09 15:13:43 +00002327 new_sg = sg_next(new_sg);
2328 }
Chris Wilsonc2dc6cc2016-12-19 12:43:46 +00002329 GEM_BUG_ON(new_sg); /* Should walk exactly nents and hit the end */
Tvrtko Ursulin0c40ce12016-11-09 15:13:43 +00002330
2331 sg_free_table(orig_st);
2332
2333 *orig_st = new_st;
Chris Wilson935a2f72017-02-13 17:15:13 +00002334 return true;
Tvrtko Ursulin0c40ce12016-11-09 15:13:43 +00002335}
2336
Matthew Auldb91b09e2017-10-06 23:18:17 +01002337static int i915_gem_object_get_pages_gtt(struct drm_i915_gem_object *obj)
Eric Anholt673a3942008-07-30 12:06:12 -07002338{
Chris Wilsonfac5e232016-07-04 11:34:36 +01002339 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
Chris Wilsond766ef52016-12-19 12:43:45 +00002340 const unsigned long page_count = obj->base.size / PAGE_SIZE;
2341 unsigned long i;
Eric Anholt673a3942008-07-30 12:06:12 -07002342 struct address_space *mapping;
Chris Wilson9da3da62012-06-01 15:20:22 +01002343 struct sg_table *st;
2344 struct scatterlist *sg;
Dave Gordon85d12252016-05-20 11:54:06 +01002345 struct sgt_iter sgt_iter;
Eric Anholt673a3942008-07-30 12:06:12 -07002346 struct page *page;
Imre Deak90797e62013-02-18 19:28:03 +02002347 unsigned long last_pfn = 0; /* suppress gcc warning */
Tvrtko Ursulin56024522017-08-03 10:14:17 +01002348 unsigned int max_segment = i915_sg_segment_size();
Matthew Auld84e89782017-10-09 12:00:24 +01002349 unsigned int sg_page_sizes;
Kuo-Hsin Yang64e3d122018-11-06 13:23:24 +00002350 struct pagevec pvec;
Chris Wilson4846bf02017-06-09 12:03:46 +01002351 gfp_t noreclaim;
Imre Deake2273302015-07-09 12:59:05 +03002352 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07002353
Chris Wilsone0ff7a72018-09-03 09:33:36 +01002354 /*
2355 * Assert that the object is not currently in any GPU domain. As it
Chris Wilson6c085a72012-08-20 11:40:46 +02002356 * wasn't in the GTT, there shouldn't be any way it could have been in
2357 * a GPU cache
2358 */
Christian Königc0a51fd2018-02-16 13:43:38 +01002359 GEM_BUG_ON(obj->read_domains & I915_GEM_GPU_DOMAINS);
2360 GEM_BUG_ON(obj->write_domain & I915_GEM_GPU_DOMAINS);
Chris Wilson6c085a72012-08-20 11:40:46 +02002361
Chris Wilsone0ff7a72018-09-03 09:33:36 +01002362 /*
2363 * If there's no chance of allocating enough pages for the whole
2364 * object, bail early.
2365 */
Arun KSca79b0c2018-12-28 00:34:29 -08002366 if (page_count > totalram_pages())
Chris Wilsone0ff7a72018-09-03 09:33:36 +01002367 return -ENOMEM;
2368
Chris Wilson9da3da62012-06-01 15:20:22 +01002369 st = kmalloc(sizeof(*st), GFP_KERNEL);
2370 if (st == NULL)
Matthew Auldb91b09e2017-10-06 23:18:17 +01002371 return -ENOMEM;
Eric Anholt673a3942008-07-30 12:06:12 -07002372
Chris Wilsond766ef52016-12-19 12:43:45 +00002373rebuild_st:
Chris Wilson9da3da62012-06-01 15:20:22 +01002374 if (sg_alloc_table(st, page_count, GFP_KERNEL)) {
Chris Wilson9da3da62012-06-01 15:20:22 +01002375 kfree(st);
Matthew Auldb91b09e2017-10-06 23:18:17 +01002376 return -ENOMEM;
Chris Wilson9da3da62012-06-01 15:20:22 +01002377 }
2378
Chris Wilsone0ff7a72018-09-03 09:33:36 +01002379 /*
2380 * Get the list of pages out of our struct file. They'll be pinned
Chris Wilson9da3da62012-06-01 15:20:22 +01002381 * at this point until we release them.
2382 *
2383 * Fail silently without starting the shrinker
2384 */
Al Viro93c76a32015-12-04 23:45:44 -05002385 mapping = obj->base.filp->f_mapping;
Kuo-Hsin Yang64e3d122018-11-06 13:23:24 +00002386 mapping_set_unevictable(mapping);
Chris Wilson0f6ab552017-06-09 12:03:48 +01002387 noreclaim = mapping_gfp_constraint(mapping, ~__GFP_RECLAIM);
Chris Wilson4846bf02017-06-09 12:03:46 +01002388 noreclaim |= __GFP_NORETRY | __GFP_NOWARN;
2389
Imre Deak90797e62013-02-18 19:28:03 +02002390 sg = st->sgl;
2391 st->nents = 0;
Matthew Auld84e89782017-10-09 12:00:24 +01002392 sg_page_sizes = 0;
Imre Deak90797e62013-02-18 19:28:03 +02002393 for (i = 0; i < page_count; i++) {
Chris Wilson4846bf02017-06-09 12:03:46 +01002394 const unsigned int shrink[] = {
2395 I915_SHRINK_BOUND | I915_SHRINK_UNBOUND | I915_SHRINK_PURGEABLE,
2396 0,
2397 }, *s = shrink;
2398 gfp_t gfp = noreclaim;
2399
2400 do {
Chris Wilsone6db7f42018-11-05 17:06:40 +00002401 cond_resched();
Chris Wilson6c085a72012-08-20 11:40:46 +02002402 page = shmem_read_mapping_page_gfp(mapping, i, gfp);
Chengguang Xu772b5402019-02-21 10:08:19 +08002403 if (!IS_ERR(page))
Chris Wilson4846bf02017-06-09 12:03:46 +01002404 break;
2405
2406 if (!*s) {
2407 ret = PTR_ERR(page);
2408 goto err_sg;
2409 }
2410
Chris Wilson912d5722017-09-06 16:19:30 -07002411 i915_gem_shrink(dev_priv, 2 * page_count, NULL, *s++);
Chris Wilson24f8e002017-03-22 11:05:21 +00002412
Chris Wilsone0ff7a72018-09-03 09:33:36 +01002413 /*
2414 * We've tried hard to allocate the memory by reaping
Chris Wilson6c085a72012-08-20 11:40:46 +02002415 * our own buffer, now let the real VM do its job and
2416 * go down in flames if truly OOM.
Chris Wilson24f8e002017-03-22 11:05:21 +00002417 *
2418 * However, since graphics tend to be disposable,
2419 * defer the oom here by reporting the ENOMEM back
2420 * to userspace.
Chris Wilson6c085a72012-08-20 11:40:46 +02002421 */
Chris Wilson4846bf02017-06-09 12:03:46 +01002422 if (!*s) {
2423 /* reclaim and warn, but no oom */
2424 gfp = mapping_gfp_mask(mapping);
Chris Wilsoneaf41802017-06-09 12:03:47 +01002425
Chris Wilsone0ff7a72018-09-03 09:33:36 +01002426 /*
2427 * Our bo are always dirty and so we require
Chris Wilsoneaf41802017-06-09 12:03:47 +01002428 * kswapd to reclaim our pages (direct reclaim
2429 * does not effectively begin pageout of our
2430 * buffers on its own). However, direct reclaim
2431 * only waits for kswapd when under allocation
2432 * congestion. So as a result __GFP_RECLAIM is
2433 * unreliable and fails to actually reclaim our
2434 * dirty pages -- unless you try over and over
2435 * again with !__GFP_NORETRY. However, we still
2436 * want to fail this allocation rather than
2437 * trigger the out-of-memory killer and for
Michal Hockodbb32952017-07-12 14:36:55 -07002438 * this we want __GFP_RETRY_MAYFAIL.
Chris Wilsoneaf41802017-06-09 12:03:47 +01002439 */
Michal Hockodbb32952017-07-12 14:36:55 -07002440 gfp |= __GFP_RETRY_MAYFAIL;
Imre Deake2273302015-07-09 12:59:05 +03002441 }
Chris Wilson4846bf02017-06-09 12:03:46 +01002442 } while (1);
2443
Chris Wilson871dfbd2016-10-11 09:20:21 +01002444 if (!i ||
2445 sg->length >= max_segment ||
2446 page_to_pfn(page) != last_pfn + 1) {
Matthew Aulda5c081662017-10-06 23:18:18 +01002447 if (i) {
Matthew Auld84e89782017-10-09 12:00:24 +01002448 sg_page_sizes |= sg->length;
Imre Deak90797e62013-02-18 19:28:03 +02002449 sg = sg_next(sg);
Matthew Aulda5c081662017-10-06 23:18:18 +01002450 }
Imre Deak90797e62013-02-18 19:28:03 +02002451 st->nents++;
2452 sg_set_page(sg, page, PAGE_SIZE, 0);
2453 } else {
2454 sg->length += PAGE_SIZE;
2455 }
2456 last_pfn = page_to_pfn(page);
Daniel Vetter3bbbe702013-10-07 17:15:45 -03002457
2458 /* Check that the i965g/gm workaround works. */
2459 WARN_ON((gfp & __GFP_DMA32) && (last_pfn >= 0x00100000UL));
Eric Anholt673a3942008-07-30 12:06:12 -07002460 }
Matthew Aulda5c081662017-10-06 23:18:18 +01002461 if (sg) { /* loop terminated early; short sg table */
Matthew Auld84e89782017-10-09 12:00:24 +01002462 sg_page_sizes |= sg->length;
Konrad Rzeszutek Wilk426729d2013-06-24 11:47:48 -04002463 sg_mark_end(sg);
Matthew Aulda5c081662017-10-06 23:18:18 +01002464 }
Chris Wilson74ce6b62012-10-19 15:51:06 +01002465
Tvrtko Ursulin0c40ce12016-11-09 15:13:43 +00002466 /* Trim unused sg entries to avoid wasting memory. */
2467 i915_sg_trim(st);
2468
Chris Wilson03ac84f2016-10-28 13:58:36 +01002469 ret = i915_gem_gtt_prepare_pages(obj, st);
Chris Wilsond766ef52016-12-19 12:43:45 +00002470 if (ret) {
Chris Wilsone0ff7a72018-09-03 09:33:36 +01002471 /*
2472 * DMA remapping failed? One possible cause is that
Chris Wilsond766ef52016-12-19 12:43:45 +00002473 * it could not reserve enough large entries, asking
2474 * for PAGE_SIZE chunks instead may be helpful.
2475 */
2476 if (max_segment > PAGE_SIZE) {
2477 for_each_sgt_page(page, sgt_iter, st)
2478 put_page(page);
2479 sg_free_table(st);
2480
2481 max_segment = PAGE_SIZE;
2482 goto rebuild_st;
2483 } else {
2484 dev_warn(&dev_priv->drm.pdev->dev,
2485 "Failed to DMA remap %lu pages\n",
2486 page_count);
2487 goto err_pages;
2488 }
2489 }
Imre Deake2273302015-07-09 12:59:05 +03002490
Eric Anholt673a3942008-07-30 12:06:12 -07002491 if (i915_gem_object_needs_bit17_swizzle(obj))
Chris Wilson03ac84f2016-10-28 13:58:36 +01002492 i915_gem_object_do_bit_17_swizzle(obj, st);
Eric Anholt673a3942008-07-30 12:06:12 -07002493
Matthew Auld84e89782017-10-09 12:00:24 +01002494 __i915_gem_object_set_pages(obj, st, sg_page_sizes);
Matthew Auldb91b09e2017-10-06 23:18:17 +01002495
2496 return 0;
Eric Anholt673a3942008-07-30 12:06:12 -07002497
Chris Wilsonb17993b2016-11-14 11:29:30 +00002498err_sg:
Imre Deak90797e62013-02-18 19:28:03 +02002499 sg_mark_end(sg);
Chris Wilsonb17993b2016-11-14 11:29:30 +00002500err_pages:
Kuo-Hsin Yang64e3d122018-11-06 13:23:24 +00002501 mapping_clear_unevictable(mapping);
2502 pagevec_init(&pvec);
2503 for_each_sgt_page(page, sgt_iter, st) {
2504 if (!pagevec_add(&pvec, page))
2505 check_release_pagevec(&pvec);
2506 }
2507 if (pagevec_count(&pvec))
2508 check_release_pagevec(&pvec);
Chris Wilson9da3da62012-06-01 15:20:22 +01002509 sg_free_table(st);
2510 kfree(st);
Chris Wilson0820baf2014-03-25 13:23:03 +00002511
Chris Wilsone0ff7a72018-09-03 09:33:36 +01002512 /*
2513 * shmemfs first checks if there is enough memory to allocate the page
Chris Wilson0820baf2014-03-25 13:23:03 +00002514 * and reports ENOSPC should there be insufficient, along with the usual
2515 * ENOMEM for a genuine allocation failure.
2516 *
2517 * We use ENOSPC in our driver to mean that we have run out of aperture
2518 * space and so want to translate the error from shmemfs back to our
2519 * usual understanding of ENOMEM.
2520 */
Imre Deake2273302015-07-09 12:59:05 +03002521 if (ret == -ENOSPC)
2522 ret = -ENOMEM;
2523
Matthew Auldb91b09e2017-10-06 23:18:17 +01002524 return ret;
Chris Wilson03ac84f2016-10-28 13:58:36 +01002525}
2526
2527void __i915_gem_object_set_pages(struct drm_i915_gem_object *obj,
Matthew Aulda5c081662017-10-06 23:18:18 +01002528 struct sg_table *pages,
Matthew Auld84e89782017-10-09 12:00:24 +01002529 unsigned int sg_page_sizes)
Chris Wilson03ac84f2016-10-28 13:58:36 +01002530{
Matthew Aulda5c081662017-10-06 23:18:18 +01002531 struct drm_i915_private *i915 = to_i915(obj->base.dev);
2532 unsigned long supported = INTEL_INFO(i915)->page_sizes;
2533 int i;
2534
Chris Wilson1233e2d2016-10-28 13:58:37 +01002535 lockdep_assert_held(&obj->mm.lock);
Chris Wilson03ac84f2016-10-28 13:58:36 +01002536
2537 obj->mm.get_page.sg_pos = pages->sgl;
2538 obj->mm.get_page.sg_idx = 0;
2539
2540 obj->mm.pages = pages;
Chris Wilson2c3a3f42016-11-04 10:30:01 +00002541
2542 if (i915_gem_object_is_tiled(obj) &&
Chris Wilsonf2123812017-10-16 12:40:37 +01002543 i915->quirks & QUIRK_PIN_SWIZZLED_PAGES) {
Chris Wilson2c3a3f42016-11-04 10:30:01 +00002544 GEM_BUG_ON(obj->mm.quirked);
2545 __i915_gem_object_pin_pages(obj);
2546 obj->mm.quirked = true;
2547 }
Matthew Aulda5c081662017-10-06 23:18:18 +01002548
Matthew Auld84e89782017-10-09 12:00:24 +01002549 GEM_BUG_ON(!sg_page_sizes);
2550 obj->mm.page_sizes.phys = sg_page_sizes;
Matthew Aulda5c081662017-10-06 23:18:18 +01002551
2552 /*
Matthew Auld84e89782017-10-09 12:00:24 +01002553 * Calculate the supported page-sizes which fit into the given
2554 * sg_page_sizes. This will give us the page-sizes which we may be able
2555 * to use opportunistically when later inserting into the GTT. For
2556 * example if phys=2G, then in theory we should be able to use 1G, 2M,
2557 * 64K or 4K pages, although in practice this will depend on a number of
2558 * other factors.
Matthew Aulda5c081662017-10-06 23:18:18 +01002559 */
2560 obj->mm.page_sizes.sg = 0;
2561 for_each_set_bit(i, &supported, ilog2(I915_GTT_MAX_PAGE_SIZE) + 1) {
2562 if (obj->mm.page_sizes.phys & ~0u << i)
2563 obj->mm.page_sizes.sg |= BIT(i);
2564 }
Matthew Aulda5c081662017-10-06 23:18:18 +01002565 GEM_BUG_ON(!HAS_PAGE_SIZES(i915, obj->mm.page_sizes.sg));
Chris Wilsonf2123812017-10-16 12:40:37 +01002566
2567 spin_lock(&i915->mm.obj_lock);
2568 list_add(&obj->mm.link, &i915->mm.unbound_list);
2569 spin_unlock(&i915->mm.obj_lock);
Chris Wilson03ac84f2016-10-28 13:58:36 +01002570}
2571
2572static int ____i915_gem_object_get_pages(struct drm_i915_gem_object *obj)
2573{
Matthew Auldb91b09e2017-10-06 23:18:17 +01002574 int err;
Chris Wilson03ac84f2016-10-28 13:58:36 +01002575
2576 if (unlikely(obj->mm.madv != I915_MADV_WILLNEED)) {
2577 DRM_DEBUG("Attempting to obtain a purgeable object\n");
2578 return -EFAULT;
2579 }
2580
Matthew Auldb91b09e2017-10-06 23:18:17 +01002581 err = obj->ops->get_pages(obj);
Matthew Auldb65a9b92017-12-18 10:38:55 +00002582 GEM_BUG_ON(!err && !i915_gem_object_has_pages(obj));
Chris Wilson03ac84f2016-10-28 13:58:36 +01002583
Matthew Auldb91b09e2017-10-06 23:18:17 +01002584 return err;
Eric Anholt673a3942008-07-30 12:06:12 -07002585}
2586
Chris Wilson37e680a2012-06-07 15:38:42 +01002587/* Ensure that the associated pages are gathered from the backing storage
Chris Wilson1233e2d2016-10-28 13:58:37 +01002588 * and pinned into our object. i915_gem_object_pin_pages() may be called
Chris Wilson37e680a2012-06-07 15:38:42 +01002589 * multiple times before they are released by a single call to
Chris Wilson1233e2d2016-10-28 13:58:37 +01002590 * i915_gem_object_unpin_pages() - once the pages are no longer referenced
Chris Wilson37e680a2012-06-07 15:38:42 +01002591 * either as a result of memory pressure (reaping pages under the shrinker)
2592 * or as the object is itself released.
2593 */
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002594int __i915_gem_object_get_pages(struct drm_i915_gem_object *obj)
Chris Wilson37e680a2012-06-07 15:38:42 +01002595{
Chris Wilson03ac84f2016-10-28 13:58:36 +01002596 int err;
Chris Wilson37e680a2012-06-07 15:38:42 +01002597
Chris Wilson1233e2d2016-10-28 13:58:37 +01002598 err = mutex_lock_interruptible(&obj->mm.lock);
2599 if (err)
2600 return err;
Chris Wilson4c7d62c2016-10-28 13:58:32 +01002601
Chris Wilsonf1fa4f42017-10-13 21:26:13 +01002602 if (unlikely(!i915_gem_object_has_pages(obj))) {
Chris Wilson88c880b2017-09-06 14:52:20 +01002603 GEM_BUG_ON(i915_gem_object_has_pinned_pages(obj));
2604
Chris Wilson2c3a3f42016-11-04 10:30:01 +00002605 err = ____i915_gem_object_get_pages(obj);
2606 if (err)
2607 goto unlock;
2608
2609 smp_mb__before_atomic();
Chris Wilson1233e2d2016-10-28 13:58:37 +01002610 }
Chris Wilson2c3a3f42016-11-04 10:30:01 +00002611 atomic_inc(&obj->mm.pages_pin_count);
Chris Wilson43e28f02013-01-08 10:53:09 +00002612
Chris Wilson1233e2d2016-10-28 13:58:37 +01002613unlock:
2614 mutex_unlock(&obj->mm.lock);
Chris Wilson03ac84f2016-10-28 13:58:36 +01002615 return err;
Eric Anholt673a3942008-07-30 12:06:12 -07002616}
2617
Dave Gordondd6034c2016-05-20 11:54:04 +01002618/* The 'mapping' part of i915_gem_object_pin_map() below */
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002619static void *i915_gem_object_map(const struct drm_i915_gem_object *obj,
2620 enum i915_map_type type)
Dave Gordondd6034c2016-05-20 11:54:04 +01002621{
2622 unsigned long n_pages = obj->base.size >> PAGE_SHIFT;
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002623 struct sg_table *sgt = obj->mm.pages;
Dave Gordon85d12252016-05-20 11:54:06 +01002624 struct sgt_iter sgt_iter;
2625 struct page *page;
Dave Gordonb338fa42016-05-20 11:54:05 +01002626 struct page *stack_pages[32];
2627 struct page **pages = stack_pages;
Dave Gordondd6034c2016-05-20 11:54:04 +01002628 unsigned long i = 0;
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002629 pgprot_t pgprot;
Dave Gordondd6034c2016-05-20 11:54:04 +01002630 void *addr;
2631
2632 /* A single page can always be kmapped */
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002633 if (n_pages == 1 && type == I915_MAP_WB)
Dave Gordondd6034c2016-05-20 11:54:04 +01002634 return kmap(sg_page(sgt->sgl));
2635
Dave Gordonb338fa42016-05-20 11:54:05 +01002636 if (n_pages > ARRAY_SIZE(stack_pages)) {
2637 /* Too big for stack -- allocate temporary array instead */
Michal Hocko0ee931c2017-09-13 16:28:29 -07002638 pages = kvmalloc_array(n_pages, sizeof(*pages), GFP_KERNEL);
Dave Gordonb338fa42016-05-20 11:54:05 +01002639 if (!pages)
2640 return NULL;
2641 }
Dave Gordondd6034c2016-05-20 11:54:04 +01002642
Dave Gordon85d12252016-05-20 11:54:06 +01002643 for_each_sgt_page(page, sgt_iter, sgt)
2644 pages[i++] = page;
Dave Gordondd6034c2016-05-20 11:54:04 +01002645
2646 /* Check that we have the expected number of pages */
2647 GEM_BUG_ON(i != n_pages);
2648
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002649 switch (type) {
Chris Wilsona575c672017-08-28 11:46:31 +01002650 default:
2651 MISSING_CASE(type);
2652 /* fallthrough to use PAGE_KERNEL anyway */
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002653 case I915_MAP_WB:
2654 pgprot = PAGE_KERNEL;
2655 break;
2656 case I915_MAP_WC:
2657 pgprot = pgprot_writecombine(PAGE_KERNEL_IO);
2658 break;
2659 }
2660 addr = vmap(pages, n_pages, 0, pgprot);
Dave Gordondd6034c2016-05-20 11:54:04 +01002661
Dave Gordonb338fa42016-05-20 11:54:05 +01002662 if (pages != stack_pages)
Michal Hocko20981052017-05-17 14:23:12 +02002663 kvfree(pages);
Dave Gordondd6034c2016-05-20 11:54:04 +01002664
2665 return addr;
2666}
2667
2668/* get, pin, and map the pages of the object into kernel space */
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002669void *i915_gem_object_pin_map(struct drm_i915_gem_object *obj,
2670 enum i915_map_type type)
Chris Wilson0a798eb2016-04-08 12:11:11 +01002671{
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002672 enum i915_map_type has_type;
2673 bool pinned;
2674 void *ptr;
Chris Wilson0a798eb2016-04-08 12:11:11 +01002675 int ret;
2676
Tina Zhanga03f3952017-11-14 10:25:13 +00002677 if (unlikely(!i915_gem_object_has_struct_page(obj)))
2678 return ERR_PTR(-ENXIO);
Chris Wilson0a798eb2016-04-08 12:11:11 +01002679
Chris Wilson1233e2d2016-10-28 13:58:37 +01002680 ret = mutex_lock_interruptible(&obj->mm.lock);
Chris Wilson0a798eb2016-04-08 12:11:11 +01002681 if (ret)
2682 return ERR_PTR(ret);
2683
Chris Wilsona575c672017-08-28 11:46:31 +01002684 pinned = !(type & I915_MAP_OVERRIDE);
2685 type &= ~I915_MAP_OVERRIDE;
2686
Chris Wilson1233e2d2016-10-28 13:58:37 +01002687 if (!atomic_inc_not_zero(&obj->mm.pages_pin_count)) {
Chris Wilsonf1fa4f42017-10-13 21:26:13 +01002688 if (unlikely(!i915_gem_object_has_pages(obj))) {
Chris Wilson88c880b2017-09-06 14:52:20 +01002689 GEM_BUG_ON(i915_gem_object_has_pinned_pages(obj));
2690
Chris Wilson2c3a3f42016-11-04 10:30:01 +00002691 ret = ____i915_gem_object_get_pages(obj);
2692 if (ret)
2693 goto err_unlock;
Chris Wilson1233e2d2016-10-28 13:58:37 +01002694
Chris Wilson2c3a3f42016-11-04 10:30:01 +00002695 smp_mb__before_atomic();
2696 }
2697 atomic_inc(&obj->mm.pages_pin_count);
Chris Wilson1233e2d2016-10-28 13:58:37 +01002698 pinned = false;
2699 }
Chris Wilsonf1fa4f42017-10-13 21:26:13 +01002700 GEM_BUG_ON(!i915_gem_object_has_pages(obj));
Chris Wilson0a798eb2016-04-08 12:11:11 +01002701
Chris Wilson0ce81782017-05-17 13:09:59 +01002702 ptr = page_unpack_bits(obj->mm.mapping, &has_type);
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002703 if (ptr && has_type != type) {
2704 if (pinned) {
2705 ret = -EBUSY;
Chris Wilson1233e2d2016-10-28 13:58:37 +01002706 goto err_unpin;
Chris Wilson0a798eb2016-04-08 12:11:11 +01002707 }
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002708
2709 if (is_vmalloc_addr(ptr))
2710 vunmap(ptr);
2711 else
2712 kunmap(kmap_to_page(ptr));
2713
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002714 ptr = obj->mm.mapping = NULL;
Chris Wilson0a798eb2016-04-08 12:11:11 +01002715 }
2716
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002717 if (!ptr) {
2718 ptr = i915_gem_object_map(obj, type);
2719 if (!ptr) {
2720 ret = -ENOMEM;
Chris Wilson1233e2d2016-10-28 13:58:37 +01002721 goto err_unpin;
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002722 }
2723
Chris Wilson0ce81782017-05-17 13:09:59 +01002724 obj->mm.mapping = page_pack_bits(ptr, type);
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002725 }
2726
Chris Wilson1233e2d2016-10-28 13:58:37 +01002727out_unlock:
2728 mutex_unlock(&obj->mm.lock);
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002729 return ptr;
2730
Chris Wilson1233e2d2016-10-28 13:58:37 +01002731err_unpin:
2732 atomic_dec(&obj->mm.pages_pin_count);
2733err_unlock:
2734 ptr = ERR_PTR(ret);
2735 goto out_unlock;
Chris Wilson0a798eb2016-04-08 12:11:11 +01002736}
2737
Chris Wilson7c55e2c2017-03-07 12:03:38 +00002738static int
2739i915_gem_object_pwrite_gtt(struct drm_i915_gem_object *obj,
2740 const struct drm_i915_gem_pwrite *arg)
2741{
2742 struct address_space *mapping = obj->base.filp->f_mapping;
2743 char __user *user_data = u64_to_user_ptr(arg->data_ptr);
2744 u64 remain, offset;
2745 unsigned int pg;
2746
2747 /* Before we instantiate/pin the backing store for our use, we
2748 * can prepopulate the shmemfs filp efficiently using a write into
2749 * the pagecache. We avoid the penalty of instantiating all the
2750 * pages, important if the user is just writing to a few and never
2751 * uses the object on the GPU, and using a direct write into shmemfs
2752 * allows it to avoid the cost of retrieving a page (either swapin
2753 * or clearing-before-use) before it is overwritten.
2754 */
Chris Wilsonf1fa4f42017-10-13 21:26:13 +01002755 if (i915_gem_object_has_pages(obj))
Chris Wilson7c55e2c2017-03-07 12:03:38 +00002756 return -ENODEV;
2757
Chris Wilsona6d65e42017-10-16 21:27:32 +01002758 if (obj->mm.madv != I915_MADV_WILLNEED)
2759 return -EFAULT;
2760
Chris Wilson7c55e2c2017-03-07 12:03:38 +00002761 /* Before the pages are instantiated the object is treated as being
2762 * in the CPU domain. The pages will be clflushed as required before
2763 * use, and we can freely write into the pages directly. If userspace
2764 * races pwrite with any other operation; corruption will ensue -
2765 * that is userspace's prerogative!
2766 */
2767
2768 remain = arg->size;
2769 offset = arg->offset;
2770 pg = offset_in_page(offset);
2771
2772 do {
2773 unsigned int len, unwritten;
2774 struct page *page;
2775 void *data, *vaddr;
2776 int err;
2777
2778 len = PAGE_SIZE - pg;
2779 if (len > remain)
2780 len = remain;
2781
2782 err = pagecache_write_begin(obj->base.filp, mapping,
2783 offset, len, 0,
2784 &page, &data);
2785 if (err < 0)
2786 return err;
2787
2788 vaddr = kmap(page);
2789 unwritten = copy_from_user(vaddr + pg, user_data, len);
2790 kunmap(page);
2791
2792 err = pagecache_write_end(obj->base.filp, mapping,
2793 offset, len, len - unwritten,
2794 page, data);
2795 if (err < 0)
2796 return err;
2797
2798 if (unwritten)
2799 return -EFAULT;
2800
2801 remain -= len;
2802 user_data += len;
2803 offset += len;
2804 pg = 0;
2805 } while (remain);
2806
2807 return 0;
2808}
2809
Daniel Vetter75ef9da2010-08-21 00:25:16 +02002810static void
Eric Anholt673a3942008-07-30 12:06:12 -07002811i915_gem_retire_work_handler(struct work_struct *work)
2812{
Chris Wilsonb29c19b2013-09-25 17:34:56 +01002813 struct drm_i915_private *dev_priv =
Chris Wilson67d97da2016-07-04 08:08:31 +01002814 container_of(work, typeof(*dev_priv), gt.retire_work.work);
Chris Wilson91c8a322016-07-05 10:40:23 +01002815 struct drm_device *dev = &dev_priv->drm;
Eric Anholt673a3942008-07-30 12:06:12 -07002816
Chris Wilson891b48c2010-09-29 12:26:37 +01002817 /* Come back later if the device is busy... */
Chris Wilsonb29c19b2013-09-25 17:34:56 +01002818 if (mutex_trylock(&dev->struct_mutex)) {
Chris Wilsone61e0f52018-02-21 09:56:36 +00002819 i915_retire_requests(dev_priv);
Chris Wilsonb29c19b2013-09-25 17:34:56 +01002820 mutex_unlock(&dev->struct_mutex);
2821 }
Chris Wilson67d97da2016-07-04 08:08:31 +01002822
Chris Wilson88923042018-01-29 14:41:04 +00002823 /*
2824 * Keep the retire handler running until we are finally idle.
Chris Wilson67d97da2016-07-04 08:08:31 +01002825 * We do not need to do this test under locking as in the worst-case
2826 * we queue the retire worker once too often.
2827 */
Chris Wilson88923042018-01-29 14:41:04 +00002828 if (READ_ONCE(dev_priv->gt.awake))
Chris Wilson67d97da2016-07-04 08:08:31 +01002829 queue_delayed_work(dev_priv->wq,
2830 &dev_priv->gt.retire_work,
Chris Wilsonbcb45082012-10-05 17:02:57 +01002831 round_jiffies_up_relative(HZ));
Chris Wilsonb29c19b2013-09-25 17:34:56 +01002832}
Chris Wilson891b48c2010-09-29 12:26:37 +01002833
Chris Wilsonc6eeb472019-03-08 09:36:56 +00002834static bool switch_to_kernel_context_sync(struct drm_i915_private *i915,
2835 unsigned long mask)
Chris Wilson5861b012019-03-08 09:36:54 +00002836{
2837 bool result = true;
2838
2839 /*
2840 * Even if we fail to switch, give whatever is running a small chance
2841 * to save itself before we report the failure. Yes, this may be a
2842 * false positive due to e.g. ENOMEM, caveat emptor!
2843 */
Chris Wilsonc6eeb472019-03-08 09:36:56 +00002844 if (i915_gem_switch_to_kernel_context(i915, mask))
Chris Wilson5861b012019-03-08 09:36:54 +00002845 result = false;
2846
2847 if (i915_gem_wait_for_idle(i915,
2848 I915_WAIT_LOCKED |
2849 I915_WAIT_FOR_IDLE_BOOST,
2850 I915_GEM_IDLE_TIMEOUT))
2851 result = false;
2852
Chris Wilson7d6ce552019-03-08 09:36:57 +00002853 if (!result) {
Chris Wilson831ebf12019-03-08 13:45:12 +00002854 if (i915_modparams.reset) { /* XXX hide warning from gem_eio */
2855 dev_err(i915->drm.dev,
2856 "Failed to idle engines, declaring wedged!\n");
2857 GEM_TRACE_DUMP();
2858 }
2859
Chris Wilson5861b012019-03-08 09:36:54 +00002860 /* Forcibly cancel outstanding work and leave the gpu quiet. */
Chris Wilson5861b012019-03-08 09:36:54 +00002861 i915_gem_set_wedged(i915);
2862 }
2863
2864 i915_retire_requests(i915); /* ensure we flush after wedging */
2865 return result;
2866}
2867
Chris Wilson604c37d2019-03-08 09:36:55 +00002868static bool load_power_context(struct drm_i915_private *i915)
2869{
Chris Wilsonc6eeb472019-03-08 09:36:56 +00002870 /* Force loading the kernel context on all engines */
2871 if (!switch_to_kernel_context_sync(i915, ALL_ENGINES))
Chris Wilson604c37d2019-03-08 09:36:55 +00002872 return false;
2873
2874 /*
2875 * Immediately park the GPU so that we enable powersaving and
2876 * treat it as idle. The next time we issue a request, we will
2877 * unpark and start using the engine->pinned_default_state, otherwise
2878 * it is in limbo and an early reset may fail.
2879 */
2880 __i915_gem_park(i915);
2881
2882 return true;
2883}
2884
Chris Wilsonb29c19b2013-09-25 17:34:56 +01002885static void
2886i915_gem_idle_work_handler(struct work_struct *work)
2887{
Chris Wilson5861b012019-03-08 09:36:54 +00002888 struct drm_i915_private *i915 =
2889 container_of(work, typeof(*i915), gt.idle_work.work);
Chris Wilson67d97da2016-07-04 08:08:31 +01002890 bool rearm_hangcheck;
2891
Chris Wilson5861b012019-03-08 09:36:54 +00002892 if (!READ_ONCE(i915->gt.awake))
Chris Wilson67d97da2016-07-04 08:08:31 +01002893 return;
2894
Chris Wilson5861b012019-03-08 09:36:54 +00002895 if (READ_ONCE(i915->gt.active_requests))
Chris Wilson4dfacb02018-05-31 09:22:43 +01002896 return;
2897
Chris Wilson67d97da2016-07-04 08:08:31 +01002898 rearm_hangcheck =
Chris Wilson5861b012019-03-08 09:36:54 +00002899 cancel_delayed_work_sync(&i915->gpu_error.hangcheck_work);
Chris Wilson67d97da2016-07-04 08:08:31 +01002900
Chris Wilson5861b012019-03-08 09:36:54 +00002901 if (!mutex_trylock(&i915->drm.struct_mutex)) {
Chris Wilson67d97da2016-07-04 08:08:31 +01002902 /* Currently busy, come back later */
Chris Wilson5861b012019-03-08 09:36:54 +00002903 mod_delayed_work(i915->wq,
2904 &i915->gt.idle_work,
Chris Wilson67d97da2016-07-04 08:08:31 +01002905 msecs_to_jiffies(50));
2906 goto out_rearm;
2907 }
2908
Imre Deak93c97dc2016-11-07 11:20:03 +02002909 /*
Chris Wilson5861b012019-03-08 09:36:54 +00002910 * Flush out the last user context, leaving only the pinned
2911 * kernel context resident. Should anything unfortunate happen
2912 * while we are idle (such as the GPU being power cycled), no users
2913 * will be harmed.
Imre Deak93c97dc2016-11-07 11:20:03 +02002914 */
Chris Wilson5861b012019-03-08 09:36:54 +00002915 if (!work_pending(&i915->gt.idle_work.work) &&
2916 !i915->gt.active_requests) {
2917 ++i915->gt.active_requests; /* don't requeue idle */
Imre Deak93c97dc2016-11-07 11:20:03 +02002918
Chris Wilsonc6eeb472019-03-08 09:36:56 +00002919 switch_to_kernel_context_sync(i915, i915->gt.active_engines);
Chris Wilsonff320d62017-10-23 22:32:35 +01002920
Chris Wilson5861b012019-03-08 09:36:54 +00002921 if (!--i915->gt.active_requests) {
2922 __i915_gem_park(i915);
2923 rearm_hangcheck = false;
2924 }
2925 }
Chris Wilson1934f5de2018-05-31 23:40:57 +01002926
Chris Wilson5861b012019-03-08 09:36:54 +00002927 mutex_unlock(&i915->drm.struct_mutex);
Chris Wilson35c94182015-04-07 16:20:37 +01002928
Chris Wilson67d97da2016-07-04 08:08:31 +01002929out_rearm:
2930 if (rearm_hangcheck) {
Chris Wilson5861b012019-03-08 09:36:54 +00002931 GEM_BUG_ON(!i915->gt.awake);
2932 i915_queue_hangcheck(i915);
Chris Wilson35c94182015-04-07 16:20:37 +01002933 }
Eric Anholt673a3942008-07-30 12:06:12 -07002934}
2935
Chris Wilsonb1f788c2016-08-04 07:52:45 +01002936void i915_gem_close_object(struct drm_gem_object *gem, struct drm_file *file)
2937{
Chris Wilsond1b48c12017-08-16 09:52:08 +01002938 struct drm_i915_private *i915 = to_i915(gem->dev);
Chris Wilsonb1f788c2016-08-04 07:52:45 +01002939 struct drm_i915_gem_object *obj = to_intel_bo(gem);
2940 struct drm_i915_file_private *fpriv = file->driver_priv;
Chris Wilsond1b48c12017-08-16 09:52:08 +01002941 struct i915_lut_handle *lut, *ln;
Chris Wilsonb1f788c2016-08-04 07:52:45 +01002942
Chris Wilsond1b48c12017-08-16 09:52:08 +01002943 mutex_lock(&i915->drm.struct_mutex);
2944
2945 list_for_each_entry_safe(lut, ln, &obj->lut_list, obj_link) {
2946 struct i915_gem_context *ctx = lut->ctx;
2947 struct i915_vma *vma;
2948
Chris Wilson432295d2017-08-22 12:05:15 +01002949 GEM_BUG_ON(ctx->file_priv == ERR_PTR(-EBADF));
Chris Wilsond1b48c12017-08-16 09:52:08 +01002950 if (ctx->file_priv != fpriv)
2951 continue;
2952
2953 vma = radix_tree_delete(&ctx->handles_vma, lut->handle);
Chris Wilson3ffff012017-08-22 12:05:17 +01002954 GEM_BUG_ON(vma->obj != obj);
2955
2956 /* We allow the process to have multiple handles to the same
2957 * vma, in the same fd namespace, by virtue of flink/open.
2958 */
2959 GEM_BUG_ON(!vma->open_count);
2960 if (!--vma->open_count && !i915_vma_is_ggtt(vma))
Chris Wilsonb1f788c2016-08-04 07:52:45 +01002961 i915_vma_close(vma);
Chris Wilsonf8a7fde2016-10-28 13:58:29 +01002962
Chris Wilsond1b48c12017-08-16 09:52:08 +01002963 list_del(&lut->obj_link);
2964 list_del(&lut->ctx_link);
Chris Wilson4ff4b442017-06-16 15:05:16 +01002965
Chris Wilson13f1bfd2019-02-28 10:20:34 +00002966 i915_lut_handle_free(lut);
Chris Wilsond1b48c12017-08-16 09:52:08 +01002967 __i915_gem_object_release_unless_active(obj);
Chris Wilsonf8a7fde2016-10-28 13:58:29 +01002968 }
Chris Wilsond1b48c12017-08-16 09:52:08 +01002969
2970 mutex_unlock(&i915->drm.struct_mutex);
Chris Wilsonb1f788c2016-08-04 07:52:45 +01002971}
2972
Chris Wilsone95433c2016-10-28 13:58:27 +01002973static unsigned long to_wait_timeout(s64 timeout_ns)
2974{
2975 if (timeout_ns < 0)
2976 return MAX_SCHEDULE_TIMEOUT;
2977
2978 if (timeout_ns == 0)
2979 return 0;
2980
2981 return nsecs_to_jiffies_timeout(timeout_ns);
2982}
2983
Ben Widawsky5816d642012-04-11 11:18:19 -07002984/**
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07002985 * i915_gem_wait_ioctl - implements DRM_IOCTL_I915_GEM_WAIT
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01002986 * @dev: drm device pointer
2987 * @data: ioctl data blob
2988 * @file: drm file pointer
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07002989 *
2990 * Returns 0 if successful, else an error is returned with the remaining time in
2991 * the timeout parameter.
2992 * -ETIME: object is still busy after timeout
2993 * -ERESTARTSYS: signal interrupted the wait
2994 * -ENONENT: object doesn't exist
2995 * Also possible, but rare:
Chris Wilsonb8050142017-08-11 11:57:31 +01002996 * -EAGAIN: incomplete, restart syscall
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07002997 * -ENOMEM: damn
2998 * -ENODEV: Internal IRQ fail
2999 * -E?: The add request failed
3000 *
3001 * The wait ioctl with a timeout of 0 reimplements the busy ioctl. With any
3002 * non-zero timeout parameter the wait ioctl will wait for the given number of
3003 * nanoseconds on an object becoming unbusy. Since the wait itself does so
3004 * without holding struct_mutex the object may become re-busied before this
3005 * function completes. A similar but shorter * race condition exists in the busy
3006 * ioctl
3007 */
3008int
3009i915_gem_wait_ioctl(struct drm_device *dev, void *data, struct drm_file *file)
3010{
3011 struct drm_i915_gem_wait *args = data;
3012 struct drm_i915_gem_object *obj;
Chris Wilsone95433c2016-10-28 13:58:27 +01003013 ktime_t start;
3014 long ret;
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07003015
Daniel Vetter11b5d512014-09-29 15:31:26 +02003016 if (args->flags != 0)
3017 return -EINVAL;
3018
Chris Wilson03ac0642016-07-20 13:31:51 +01003019 obj = i915_gem_object_lookup(file, args->bo_handle);
Chris Wilson033d5492016-08-05 10:14:17 +01003020 if (!obj)
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07003021 return -ENOENT;
Chris Wilson033d5492016-08-05 10:14:17 +01003022
Chris Wilsone95433c2016-10-28 13:58:27 +01003023 start = ktime_get();
3024
3025 ret = i915_gem_object_wait(obj,
Chris Wilsone9eaf822018-10-01 15:47:55 +01003026 I915_WAIT_INTERRUPTIBLE |
3027 I915_WAIT_PRIORITY |
3028 I915_WAIT_ALL,
Chris Wilson62eb3c22019-02-13 09:25:04 +00003029 to_wait_timeout(args->timeout_ns));
Chris Wilsone95433c2016-10-28 13:58:27 +01003030
3031 if (args->timeout_ns > 0) {
3032 args->timeout_ns -= ktime_to_ns(ktime_sub(ktime_get(), start));
3033 if (args->timeout_ns < 0)
3034 args->timeout_ns = 0;
Chris Wilsonc1d20612017-02-16 12:54:41 +00003035
3036 /*
3037 * Apparently ktime isn't accurate enough and occasionally has a
3038 * bit of mismatch in the jiffies<->nsecs<->ktime loop. So patch
3039 * things up to make the test happy. We allow up to 1 jiffy.
3040 *
3041 * This is a regression from the timespec->ktime conversion.
3042 */
3043 if (ret == -ETIME && !nsecs_to_jiffies(args->timeout_ns))
3044 args->timeout_ns = 0;
Chris Wilsonb8050142017-08-11 11:57:31 +01003045
3046 /* Asked to wait beyond the jiffie/scheduler precision? */
3047 if (ret == -ETIME && args->timeout_ns)
3048 ret = -EAGAIN;
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07003049 }
3050
Chris Wilsonf0cd5182016-10-28 13:58:43 +01003051 i915_gem_object_put(obj);
John Harrisonff865882014-11-24 18:49:28 +00003052 return ret;
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07003053}
3054
Chris Wilson25112b62017-03-30 15:50:39 +01003055static int wait_for_engines(struct drm_i915_private *i915)
3056{
Chris Wilsonee42c002017-12-11 19:41:34 +00003057 if (wait_for(intel_engines_are_idle(i915), I915_IDLE_ENGINES_TIMEOUT)) {
Chris Wilson59e4b192017-12-11 19:41:35 +00003058 dev_err(i915->drm.dev,
3059 "Failed to idle engines, declaring wedged!\n");
Chris Wilson629820f2018-03-09 10:11:14 +00003060 GEM_TRACE_DUMP();
Chris Wilsoncad99462017-08-26 12:09:33 +01003061 i915_gem_set_wedged(i915);
3062 return -EIO;
Chris Wilson25112b62017-03-30 15:50:39 +01003063 }
3064
3065 return 0;
3066}
3067
Chris Wilson1e345562019-01-28 10:23:56 +00003068static long
3069wait_for_timelines(struct drm_i915_private *i915,
3070 unsigned int flags, long timeout)
3071{
3072 struct i915_gt_timelines *gt = &i915->gt.timelines;
3073 struct i915_timeline *tl;
3074
3075 if (!READ_ONCE(i915->gt.active_requests))
3076 return timeout;
3077
3078 mutex_lock(&gt->mutex);
Chris Wilson9407d3b2019-01-28 18:18:12 +00003079 list_for_each_entry(tl, &gt->active_list, link) {
Chris Wilson1e345562019-01-28 10:23:56 +00003080 struct i915_request *rq;
3081
Chris Wilson21950ee2019-02-05 13:00:05 +00003082 rq = i915_active_request_get_unlocked(&tl->last_request);
Chris Wilson1e345562019-01-28 10:23:56 +00003083 if (!rq)
3084 continue;
3085
3086 mutex_unlock(&gt->mutex);
3087
3088 /*
3089 * "Race-to-idle".
3090 *
3091 * Switching to the kernel context is often used a synchronous
3092 * step prior to idling, e.g. in suspend for flushing all
3093 * current operations to memory before sleeping. These we
3094 * want to complete as quickly as possible to avoid prolonged
3095 * stalls, so allow the gpu to boost to maximum clocks.
3096 */
3097 if (flags & I915_WAIT_FOR_IDLE_BOOST)
Chris Wilson62eb3c22019-02-13 09:25:04 +00003098 gen6_rps_boost(rq);
Chris Wilson1e345562019-01-28 10:23:56 +00003099
3100 timeout = i915_request_wait(rq, flags, timeout);
3101 i915_request_put(rq);
3102 if (timeout < 0)
3103 return timeout;
3104
3105 /* restart after reacquiring the lock */
3106 mutex_lock(&gt->mutex);
Chris Wilson9407d3b2019-01-28 18:18:12 +00003107 tl = list_entry(&gt->active_list, typeof(*tl), link);
Chris Wilson1e345562019-01-28 10:23:56 +00003108 }
3109 mutex_unlock(&gt->mutex);
3110
3111 return timeout;
3112}
3113
Chris Wilsonec625fb2018-07-09 13:20:42 +01003114int i915_gem_wait_for_idle(struct drm_i915_private *i915,
3115 unsigned int flags, long timeout)
Chris Wilson73cb9702016-10-28 13:58:46 +01003116{
Chris Wilsonec625fb2018-07-09 13:20:42 +01003117 GEM_TRACE("flags=%x (%s), timeout=%ld%s\n",
3118 flags, flags & I915_WAIT_LOCKED ? "locked" : "unlocked",
3119 timeout, timeout == MAX_SCHEDULE_TIMEOUT ? " (forever)" : "");
Chris Wilson09a4c022018-05-24 09:11:35 +01003120
Chris Wilson863e9fd2017-05-30 13:13:32 +01003121 /* If the device is asleep, we have no requests outstanding */
3122 if (!READ_ONCE(i915->gt.awake))
3123 return 0;
3124
Chris Wilson1e345562019-01-28 10:23:56 +00003125 timeout = wait_for_timelines(i915, flags, timeout);
3126 if (timeout < 0)
3127 return timeout;
3128
Chris Wilson9caa34a2016-11-11 14:58:08 +00003129 if (flags & I915_WAIT_LOCKED) {
Chris Wilsona89d1f92018-05-02 17:38:39 +01003130 int err;
Chris Wilson9caa34a2016-11-11 14:58:08 +00003131
3132 lockdep_assert_held(&i915->drm.struct_mutex);
3133
Chris Wilsona61b47f2018-06-27 12:53:34 +01003134 err = wait_for_engines(i915);
3135 if (err)
3136 return err;
3137
Chris Wilsone61e0f52018-02-21 09:56:36 +00003138 i915_retire_requests(i915);
Chris Wilsona89d1f92018-05-02 17:38:39 +01003139 }
Chris Wilsona61b47f2018-06-27 12:53:34 +01003140
3141 return 0;
Daniel Vetter4df2faf2010-02-19 11:52:00 +01003142}
3143
Chris Wilson5a97bcc2017-02-22 11:40:46 +00003144static void __i915_gem_object_flush_for_display(struct drm_i915_gem_object *obj)
3145{
Chris Wilsone27ab732017-06-15 13:38:49 +01003146 /*
3147 * We manually flush the CPU domain so that we can override and
3148 * force the flush for the display, and perform it asyncrhonously.
3149 */
3150 flush_write_domain(obj, ~I915_GEM_DOMAIN_CPU);
3151 if (obj->cache_dirty)
3152 i915_gem_clflush_object(obj, I915_CLFLUSH_FORCE);
Christian Königc0a51fd2018-02-16 13:43:38 +01003153 obj->write_domain = 0;
Chris Wilson5a97bcc2017-02-22 11:40:46 +00003154}
3155
3156void i915_gem_object_flush_if_display(struct drm_i915_gem_object *obj)
3157{
Chris Wilsonbd3d2252017-10-13 21:26:14 +01003158 if (!READ_ONCE(obj->pin_global))
Chris Wilson5a97bcc2017-02-22 11:40:46 +00003159 return;
3160
3161 mutex_lock(&obj->base.dev->struct_mutex);
3162 __i915_gem_object_flush_for_display(obj);
3163 mutex_unlock(&obj->base.dev->struct_mutex);
3164}
3165
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003166/**
Chris Wilsone22d8e32017-04-12 12:01:11 +01003167 * Moves a single object to the WC read, and possibly write domain.
3168 * @obj: object to act on
3169 * @write: ask for write access or read only
3170 *
3171 * This function returns when the move is complete, including waiting on
3172 * flushes to occur.
3173 */
3174int
3175i915_gem_object_set_to_wc_domain(struct drm_i915_gem_object *obj, bool write)
3176{
3177 int ret;
3178
3179 lockdep_assert_held(&obj->base.dev->struct_mutex);
3180
3181 ret = i915_gem_object_wait(obj,
3182 I915_WAIT_INTERRUPTIBLE |
3183 I915_WAIT_LOCKED |
3184 (write ? I915_WAIT_ALL : 0),
Chris Wilson62eb3c22019-02-13 09:25:04 +00003185 MAX_SCHEDULE_TIMEOUT);
Chris Wilsone22d8e32017-04-12 12:01:11 +01003186 if (ret)
3187 return ret;
3188
Christian Königc0a51fd2018-02-16 13:43:38 +01003189 if (obj->write_domain == I915_GEM_DOMAIN_WC)
Chris Wilsone22d8e32017-04-12 12:01:11 +01003190 return 0;
3191
3192 /* Flush and acquire obj->pages so that we are coherent through
3193 * direct access in memory with previous cached writes through
3194 * shmemfs and that our cache domain tracking remains valid.
3195 * For example, if the obj->filp was moved to swap without us
3196 * being notified and releasing the pages, we would mistakenly
3197 * continue to assume that the obj remained out of the CPU cached
3198 * domain.
3199 */
3200 ret = i915_gem_object_pin_pages(obj);
3201 if (ret)
3202 return ret;
3203
3204 flush_write_domain(obj, ~I915_GEM_DOMAIN_WC);
3205
3206 /* Serialise direct access to this object with the barriers for
3207 * coherent writes from the GPU, by effectively invalidating the
3208 * WC domain upon first access.
3209 */
Christian Königc0a51fd2018-02-16 13:43:38 +01003210 if ((obj->read_domains & I915_GEM_DOMAIN_WC) == 0)
Chris Wilsone22d8e32017-04-12 12:01:11 +01003211 mb();
3212
3213 /* It should now be out of any other write domains, and we can update
3214 * the domain values for our changes.
3215 */
Christian Königc0a51fd2018-02-16 13:43:38 +01003216 GEM_BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_WC) != 0);
3217 obj->read_domains |= I915_GEM_DOMAIN_WC;
Chris Wilsone22d8e32017-04-12 12:01:11 +01003218 if (write) {
Christian Königc0a51fd2018-02-16 13:43:38 +01003219 obj->read_domains = I915_GEM_DOMAIN_WC;
3220 obj->write_domain = I915_GEM_DOMAIN_WC;
Chris Wilsone22d8e32017-04-12 12:01:11 +01003221 obj->mm.dirty = true;
3222 }
3223
3224 i915_gem_object_unpin_pages(obj);
3225 return 0;
3226}
3227
3228/**
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003229 * Moves a single object to the GTT read, and possibly write domain.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01003230 * @obj: object to act on
3231 * @write: ask for write access or read only
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003232 *
3233 * This function returns when the move is complete, including waiting on
3234 * flushes to occur.
3235 */
Jesse Barnes79e53942008-11-07 14:24:08 -08003236int
Chris Wilson20217462010-11-23 15:26:33 +00003237i915_gem_object_set_to_gtt_domain(struct drm_i915_gem_object *obj, bool write)
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003238{
Eric Anholte47c68e2008-11-14 13:35:19 -08003239 int ret;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003240
Chris Wilsone95433c2016-10-28 13:58:27 +01003241 lockdep_assert_held(&obj->base.dev->struct_mutex);
Chris Wilson4c7d62c2016-10-28 13:58:32 +01003242
Chris Wilsone95433c2016-10-28 13:58:27 +01003243 ret = i915_gem_object_wait(obj,
3244 I915_WAIT_INTERRUPTIBLE |
3245 I915_WAIT_LOCKED |
3246 (write ? I915_WAIT_ALL : 0),
Chris Wilson62eb3c22019-02-13 09:25:04 +00003247 MAX_SCHEDULE_TIMEOUT);
Chris Wilson88241782011-01-07 17:09:48 +00003248 if (ret)
3249 return ret;
3250
Christian Königc0a51fd2018-02-16 13:43:38 +01003251 if (obj->write_domain == I915_GEM_DOMAIN_GTT)
Chris Wilsonc13d87e2016-07-20 09:21:15 +01003252 return 0;
3253
Chris Wilson43566de2015-01-02 16:29:29 +05303254 /* Flush and acquire obj->pages so that we are coherent through
3255 * direct access in memory with previous cached writes through
3256 * shmemfs and that our cache domain tracking remains valid.
3257 * For example, if the obj->filp was moved to swap without us
3258 * being notified and releasing the pages, we would mistakenly
3259 * continue to assume that the obj remained out of the CPU cached
3260 * domain.
3261 */
Chris Wilsona4f5ea62016-10-28 13:58:35 +01003262 ret = i915_gem_object_pin_pages(obj);
Chris Wilson43566de2015-01-02 16:29:29 +05303263 if (ret)
3264 return ret;
3265
Chris Wilsonef749212017-04-12 12:01:10 +01003266 flush_write_domain(obj, ~I915_GEM_DOMAIN_GTT);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003267
Chris Wilsond0a57782012-10-09 19:24:37 +01003268 /* Serialise direct access to this object with the barriers for
3269 * coherent writes from the GPU, by effectively invalidating the
3270 * GTT domain upon first access.
3271 */
Christian Königc0a51fd2018-02-16 13:43:38 +01003272 if ((obj->read_domains & I915_GEM_DOMAIN_GTT) == 0)
Chris Wilsond0a57782012-10-09 19:24:37 +01003273 mb();
3274
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003275 /* It should now be out of any other write domains, and we can update
3276 * the domain values for our changes.
3277 */
Christian Königc0a51fd2018-02-16 13:43:38 +01003278 GEM_BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_GTT) != 0);
3279 obj->read_domains |= I915_GEM_DOMAIN_GTT;
Eric Anholte47c68e2008-11-14 13:35:19 -08003280 if (write) {
Christian Königc0a51fd2018-02-16 13:43:38 +01003281 obj->read_domains = I915_GEM_DOMAIN_GTT;
3282 obj->write_domain = I915_GEM_DOMAIN_GTT;
Chris Wilsona4f5ea62016-10-28 13:58:35 +01003283 obj->mm.dirty = true;
Eric Anholte47c68e2008-11-14 13:35:19 -08003284 }
3285
Chris Wilsona4f5ea62016-10-28 13:58:35 +01003286 i915_gem_object_unpin_pages(obj);
Eric Anholte47c68e2008-11-14 13:35:19 -08003287 return 0;
3288}
3289
Chris Wilsonef55f922015-10-09 14:11:27 +01003290/**
3291 * Changes the cache-level of an object across all VMA.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01003292 * @obj: object to act on
3293 * @cache_level: new cache level to set for the object
Chris Wilsonef55f922015-10-09 14:11:27 +01003294 *
3295 * After this function returns, the object will be in the new cache-level
3296 * across all GTT and the contents of the backing storage will be coherent,
3297 * with respect to the new cache-level. In order to keep the backing storage
3298 * coherent for all users, we only allow a single cache level to be set
3299 * globally on the object and prevent it from being changed whilst the
3300 * hardware is reading from the object. That is if the object is currently
3301 * on the scanout it will be set to uncached (or equivalent display
3302 * cache coherency) and all non-MOCS GPU access will also be uncached so
3303 * that all direct access to the scanout remains coherent.
3304 */
Chris Wilsone4ffd172011-04-04 09:44:39 +01003305int i915_gem_object_set_cache_level(struct drm_i915_gem_object *obj,
3306 enum i915_cache_level cache_level)
3307{
Chris Wilsonaa653a62016-08-04 07:52:27 +01003308 struct i915_vma *vma;
Chris Wilsona6a7cc42016-11-18 21:17:46 +00003309 int ret;
Chris Wilsone4ffd172011-04-04 09:44:39 +01003310
Chris Wilson4c7d62c2016-10-28 13:58:32 +01003311 lockdep_assert_held(&obj->base.dev->struct_mutex);
3312
Chris Wilsone4ffd172011-04-04 09:44:39 +01003313 if (obj->cache_level == cache_level)
Chris Wilsona6a7cc42016-11-18 21:17:46 +00003314 return 0;
Chris Wilsone4ffd172011-04-04 09:44:39 +01003315
Chris Wilsonef55f922015-10-09 14:11:27 +01003316 /* Inspect the list of currently bound VMA and unbind any that would
3317 * be invalid given the new cache-level. This is principally to
3318 * catch the issue of the CS prefetch crossing page boundaries and
3319 * reading an invalid PTE on older architectures.
3320 */
Chris Wilsonaa653a62016-08-04 07:52:27 +01003321restart:
Chris Wilson528cbd12019-01-28 10:23:54 +00003322 list_for_each_entry(vma, &obj->vma.list, obj_link) {
Chris Wilsonef55f922015-10-09 14:11:27 +01003323 if (!drm_mm_node_allocated(&vma->node))
3324 continue;
3325
Chris Wilson20dfbde2016-08-04 16:32:30 +01003326 if (i915_vma_is_pinned(vma)) {
Chris Wilsonef55f922015-10-09 14:11:27 +01003327 DRM_DEBUG("can not change the cache level of pinned objects\n");
3328 return -EBUSY;
3329 }
3330
Chris Wilson010e3e62017-12-06 12:49:13 +00003331 if (!i915_vma_is_closed(vma) &&
3332 i915_gem_valid_gtt_space(vma, cache_level))
Chris Wilsonaa653a62016-08-04 07:52:27 +01003333 continue;
3334
3335 ret = i915_vma_unbind(vma);
3336 if (ret)
3337 return ret;
3338
3339 /* As unbinding may affect other elements in the
3340 * obj->vma_list (due to side-effects from retiring
3341 * an active vma), play safe and restart the iterator.
3342 */
3343 goto restart;
Chris Wilson42d6ab42012-07-26 11:49:32 +01003344 }
3345
Chris Wilsonef55f922015-10-09 14:11:27 +01003346 /* We can reuse the existing drm_mm nodes but need to change the
3347 * cache-level on the PTE. We could simply unbind them all and
3348 * rebind with the correct cache-level on next use. However since
3349 * we already have a valid slot, dma mapping, pages etc, we may as
3350 * rewrite the PTE in the belief that doing so tramples upon less
3351 * state and so involves less work.
3352 */
Chris Wilson15717de2016-08-04 07:52:26 +01003353 if (obj->bind_count) {
Chris Wilsonef55f922015-10-09 14:11:27 +01003354 /* Before we change the PTE, the GPU must not be accessing it.
3355 * If we wait upon the object, we know that all the bound
3356 * VMA are no longer active.
3357 */
Chris Wilsone95433c2016-10-28 13:58:27 +01003358 ret = i915_gem_object_wait(obj,
3359 I915_WAIT_INTERRUPTIBLE |
3360 I915_WAIT_LOCKED |
3361 I915_WAIT_ALL,
Chris Wilson62eb3c22019-02-13 09:25:04 +00003362 MAX_SCHEDULE_TIMEOUT);
Chris Wilsone4ffd172011-04-04 09:44:39 +01003363 if (ret)
3364 return ret;
3365
Tvrtko Ursulin0031fb92016-11-04 14:42:44 +00003366 if (!HAS_LLC(to_i915(obj->base.dev)) &&
3367 cache_level != I915_CACHE_NONE) {
Chris Wilsonef55f922015-10-09 14:11:27 +01003368 /* Access to snoopable pages through the GTT is
3369 * incoherent and on some machines causes a hard
3370 * lockup. Relinquish the CPU mmaping to force
3371 * userspace to refault in the pages and we can
3372 * then double check if the GTT mapping is still
3373 * valid for that pointer access.
3374 */
3375 i915_gem_release_mmap(obj);
Chris Wilsone4ffd172011-04-04 09:44:39 +01003376
Chris Wilsonef55f922015-10-09 14:11:27 +01003377 /* As we no longer need a fence for GTT access,
3378 * we can relinquish it now (and so prevent having
3379 * to steal a fence from someone else on the next
3380 * fence request). Note GPU activity would have
3381 * dropped the fence as all snoopable access is
3382 * supposed to be linear.
3383 */
Chris Wilsone2189dd2017-12-07 21:14:07 +00003384 for_each_ggtt_vma(vma, obj) {
Chris Wilson49ef5292016-08-18 17:17:00 +01003385 ret = i915_vma_put_fence(vma);
3386 if (ret)
3387 return ret;
3388 }
Chris Wilsonef55f922015-10-09 14:11:27 +01003389 } else {
3390 /* We either have incoherent backing store and
3391 * so no GTT access or the architecture is fully
3392 * coherent. In such cases, existing GTT mmaps
3393 * ignore the cache bit in the PTE and we can
3394 * rewrite it without confusing the GPU or having
3395 * to force userspace to fault back in its mmaps.
3396 */
Chris Wilsone4ffd172011-04-04 09:44:39 +01003397 }
3398
Chris Wilson528cbd12019-01-28 10:23:54 +00003399 list_for_each_entry(vma, &obj->vma.list, obj_link) {
Chris Wilsonef55f922015-10-09 14:11:27 +01003400 if (!drm_mm_node_allocated(&vma->node))
3401 continue;
3402
3403 ret = i915_vma_bind(vma, cache_level, PIN_UPDATE);
3404 if (ret)
3405 return ret;
3406 }
Chris Wilsone4ffd172011-04-04 09:44:39 +01003407 }
3408
Chris Wilson528cbd12019-01-28 10:23:54 +00003409 list_for_each_entry(vma, &obj->vma.list, obj_link)
Chris Wilson2c225692013-08-09 12:26:45 +01003410 vma->node.color = cache_level;
Chris Wilsonb8f55be2017-08-11 12:11:16 +01003411 i915_gem_object_set_cache_coherency(obj, cache_level);
Chris Wilsone27ab732017-06-15 13:38:49 +01003412 obj->cache_dirty = true; /* Always invalidate stale cachelines */
Chris Wilson2c225692013-08-09 12:26:45 +01003413
Chris Wilsone4ffd172011-04-04 09:44:39 +01003414 return 0;
3415}
3416
Ben Widawsky199adf42012-09-21 17:01:20 -07003417int i915_gem_get_caching_ioctl(struct drm_device *dev, void *data,
3418 struct drm_file *file)
Chris Wilsone6994ae2012-07-10 10:27:08 +01003419{
Ben Widawsky199adf42012-09-21 17:01:20 -07003420 struct drm_i915_gem_caching *args = data;
Chris Wilsone6994ae2012-07-10 10:27:08 +01003421 struct drm_i915_gem_object *obj;
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01003422 int err = 0;
Chris Wilsone6994ae2012-07-10 10:27:08 +01003423
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01003424 rcu_read_lock();
3425 obj = i915_gem_object_lookup_rcu(file, args->handle);
3426 if (!obj) {
3427 err = -ENOENT;
3428 goto out;
3429 }
Chris Wilsone6994ae2012-07-10 10:27:08 +01003430
Chris Wilson651d7942013-08-08 14:41:10 +01003431 switch (obj->cache_level) {
3432 case I915_CACHE_LLC:
3433 case I915_CACHE_L3_LLC:
3434 args->caching = I915_CACHING_CACHED;
3435 break;
3436
Chris Wilson4257d3b2013-08-08 14:41:11 +01003437 case I915_CACHE_WT:
3438 args->caching = I915_CACHING_DISPLAY;
3439 break;
3440
Chris Wilson651d7942013-08-08 14:41:10 +01003441 default:
3442 args->caching = I915_CACHING_NONE;
3443 break;
3444 }
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01003445out:
3446 rcu_read_unlock();
3447 return err;
Chris Wilsone6994ae2012-07-10 10:27:08 +01003448}
3449
Ben Widawsky199adf42012-09-21 17:01:20 -07003450int i915_gem_set_caching_ioctl(struct drm_device *dev, void *data,
3451 struct drm_file *file)
Chris Wilsone6994ae2012-07-10 10:27:08 +01003452{
Chris Wilson9c870d02016-10-24 13:42:15 +01003453 struct drm_i915_private *i915 = to_i915(dev);
Ben Widawsky199adf42012-09-21 17:01:20 -07003454 struct drm_i915_gem_caching *args = data;
Chris Wilsone6994ae2012-07-10 10:27:08 +01003455 struct drm_i915_gem_object *obj;
3456 enum i915_cache_level level;
Chris Wilsond65415d2017-01-19 08:22:10 +00003457 int ret = 0;
Chris Wilsone6994ae2012-07-10 10:27:08 +01003458
Ben Widawsky199adf42012-09-21 17:01:20 -07003459 switch (args->caching) {
3460 case I915_CACHING_NONE:
Chris Wilsone6994ae2012-07-10 10:27:08 +01003461 level = I915_CACHE_NONE;
3462 break;
Ben Widawsky199adf42012-09-21 17:01:20 -07003463 case I915_CACHING_CACHED:
Imre Deake5756c12015-08-14 18:43:30 +03003464 /*
3465 * Due to a HW issue on BXT A stepping, GPU stores via a
3466 * snooped mapping may leave stale data in a corresponding CPU
3467 * cacheline, whereas normally such cachelines would get
3468 * invalidated.
3469 */
Chris Wilson9c870d02016-10-24 13:42:15 +01003470 if (!HAS_LLC(i915) && !HAS_SNOOP(i915))
Imre Deake5756c12015-08-14 18:43:30 +03003471 return -ENODEV;
3472
Chris Wilsone6994ae2012-07-10 10:27:08 +01003473 level = I915_CACHE_LLC;
3474 break;
Chris Wilson4257d3b2013-08-08 14:41:11 +01003475 case I915_CACHING_DISPLAY:
Chris Wilson9c870d02016-10-24 13:42:15 +01003476 level = HAS_WT(i915) ? I915_CACHE_WT : I915_CACHE_NONE;
Chris Wilson4257d3b2013-08-08 14:41:11 +01003477 break;
Chris Wilsone6994ae2012-07-10 10:27:08 +01003478 default:
3479 return -EINVAL;
3480 }
3481
Chris Wilsond65415d2017-01-19 08:22:10 +00003482 obj = i915_gem_object_lookup(file, args->handle);
3483 if (!obj)
3484 return -ENOENT;
3485
Tina Zhanga03f3952017-11-14 10:25:13 +00003486 /*
3487 * The caching mode of proxy object is handled by its generator, and
3488 * not allowed to be changed by userspace.
3489 */
3490 if (i915_gem_object_is_proxy(obj)) {
3491 ret = -ENXIO;
3492 goto out;
3493 }
3494
Chris Wilsond65415d2017-01-19 08:22:10 +00003495 if (obj->cache_level == level)
3496 goto out;
3497
3498 ret = i915_gem_object_wait(obj,
3499 I915_WAIT_INTERRUPTIBLE,
Chris Wilson62eb3c22019-02-13 09:25:04 +00003500 MAX_SCHEDULE_TIMEOUT);
Chris Wilsond65415d2017-01-19 08:22:10 +00003501 if (ret)
3502 goto out;
3503
Ben Widawsky3bc29132012-09-26 16:15:20 -07003504 ret = i915_mutex_lock_interruptible(dev);
3505 if (ret)
Chris Wilsond65415d2017-01-19 08:22:10 +00003506 goto out;
Chris Wilsone6994ae2012-07-10 10:27:08 +01003507
3508 ret = i915_gem_object_set_cache_level(obj, level);
Chris Wilsone6994ae2012-07-10 10:27:08 +01003509 mutex_unlock(&dev->struct_mutex);
Chris Wilsond65415d2017-01-19 08:22:10 +00003510
3511out:
3512 i915_gem_object_put(obj);
Chris Wilsone6994ae2012-07-10 10:27:08 +01003513 return ret;
3514}
3515
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003516/*
Dhinakaran Pandiyan07bcd992018-03-06 19:34:18 -08003517 * Prepare buffer for display plane (scanout, cursors, etc). Can be called from
3518 * an uninterruptible phase (modesetting) and allows any flushes to be pipelined
3519 * (for pageflips). We only flush the caches while preparing the buffer for
3520 * display, the callers are responsible for frontbuffer flush.
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003521 */
Chris Wilson058d88c2016-08-15 10:49:06 +01003522struct i915_vma *
Chris Wilson2da3b9b2011-04-14 09:41:17 +01003523i915_gem_object_pin_to_display_plane(struct drm_i915_gem_object *obj,
3524 u32 alignment,
Chris Wilson59354852018-02-20 13:42:06 +00003525 const struct i915_ggtt_view *view,
3526 unsigned int flags)
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003527{
Chris Wilson058d88c2016-08-15 10:49:06 +01003528 struct i915_vma *vma;
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003529 int ret;
3530
Chris Wilson4c7d62c2016-10-28 13:58:32 +01003531 lockdep_assert_held(&obj->base.dev->struct_mutex);
3532
Chris Wilsonbd3d2252017-10-13 21:26:14 +01003533 /* Mark the global pin early so that we account for the
Chris Wilsoncc98b412013-08-09 12:25:09 +01003534 * display coherency whilst setting up the cache domains.
3535 */
Chris Wilsonbd3d2252017-10-13 21:26:14 +01003536 obj->pin_global++;
Chris Wilsoncc98b412013-08-09 12:25:09 +01003537
Eric Anholta7ef0642011-03-29 16:59:54 -07003538 /* The display engine is not coherent with the LLC cache on gen6. As
3539 * a result, we make sure that the pinning that is about to occur is
3540 * done with uncached PTEs. This is lowest common denominator for all
3541 * chipsets.
3542 *
3543 * However for gen6+, we could do better by using the GFDT bit instead
3544 * of uncaching, which would allow us to flush all the LLC-cached data
3545 * with that bit in the PTE to main memory with just one PIPE_CONTROL.
3546 */
Chris Wilson651d7942013-08-08 14:41:10 +01003547 ret = i915_gem_object_set_cache_level(obj,
Tvrtko Ursulin86527442016-10-13 11:03:00 +01003548 HAS_WT(to_i915(obj->base.dev)) ?
3549 I915_CACHE_WT : I915_CACHE_NONE);
Chris Wilson058d88c2016-08-15 10:49:06 +01003550 if (ret) {
3551 vma = ERR_PTR(ret);
Chris Wilsonbd3d2252017-10-13 21:26:14 +01003552 goto err_unpin_global;
Chris Wilson058d88c2016-08-15 10:49:06 +01003553 }
Eric Anholta7ef0642011-03-29 16:59:54 -07003554
Chris Wilson2da3b9b2011-04-14 09:41:17 +01003555 /* As the user may map the buffer once pinned in the display plane
3556 * (e.g. libkms for the bootup splash), we have to ensure that we
Chris Wilson2efb8132016-08-18 17:17:06 +01003557 * always use map_and_fenceable for all scanout buffers. However,
3558 * it may simply be too big to fit into mappable, in which case
3559 * put it anyway and hope that userspace can cope (but always first
3560 * try to preserve the existing ABI).
Chris Wilson2da3b9b2011-04-14 09:41:17 +01003561 */
Chris Wilson2efb8132016-08-18 17:17:06 +01003562 vma = ERR_PTR(-ENOSPC);
Chris Wilson59354852018-02-20 13:42:06 +00003563 if ((flags & PIN_MAPPABLE) == 0 &&
3564 (!view || view->type == I915_GGTT_VIEW_NORMAL))
Chris Wilson2efb8132016-08-18 17:17:06 +01003565 vma = i915_gem_object_ggtt_pin(obj, view, 0, alignment,
Chris Wilson59354852018-02-20 13:42:06 +00003566 flags |
3567 PIN_MAPPABLE |
3568 PIN_NONBLOCK);
3569 if (IS_ERR(vma))
Chris Wilson767a2222016-11-07 11:01:28 +00003570 vma = i915_gem_object_ggtt_pin(obj, view, 0, alignment, flags);
Chris Wilson058d88c2016-08-15 10:49:06 +01003571 if (IS_ERR(vma))
Chris Wilsonbd3d2252017-10-13 21:26:14 +01003572 goto err_unpin_global;
Chris Wilson2da3b9b2011-04-14 09:41:17 +01003573
Chris Wilsond8923dc2016-08-18 17:17:07 +01003574 vma->display_alignment = max_t(u64, vma->display_alignment, alignment);
3575
Chris Wilson5a97bcc2017-02-22 11:40:46 +00003576 __i915_gem_object_flush_for_display(obj);
Chris Wilsonb118c1e2010-05-27 13:18:14 +01003577
Chris Wilson2da3b9b2011-04-14 09:41:17 +01003578 /* It should now be out of any other write domains, and we can update
3579 * the domain values for our changes.
3580 */
Christian Königc0a51fd2018-02-16 13:43:38 +01003581 obj->read_domains |= I915_GEM_DOMAIN_GTT;
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003582
Chris Wilson058d88c2016-08-15 10:49:06 +01003583 return vma;
Chris Wilsoncc98b412013-08-09 12:25:09 +01003584
Chris Wilsonbd3d2252017-10-13 21:26:14 +01003585err_unpin_global:
3586 obj->pin_global--;
Chris Wilson058d88c2016-08-15 10:49:06 +01003587 return vma;
Chris Wilsoncc98b412013-08-09 12:25:09 +01003588}
3589
3590void
Chris Wilson058d88c2016-08-15 10:49:06 +01003591i915_gem_object_unpin_from_display_plane(struct i915_vma *vma)
Chris Wilsoncc98b412013-08-09 12:25:09 +01003592{
Chris Wilson49d73912016-11-29 09:50:08 +00003593 lockdep_assert_held(&vma->vm->i915->drm.struct_mutex);
Chris Wilson4c7d62c2016-10-28 13:58:32 +01003594
Chris Wilsonbd3d2252017-10-13 21:26:14 +01003595 if (WARN_ON(vma->obj->pin_global == 0))
Tvrtko Ursulin8a0c39b2015-04-13 11:50:09 +01003596 return;
3597
Chris Wilsonbd3d2252017-10-13 21:26:14 +01003598 if (--vma->obj->pin_global == 0)
Chris Wilsonf51455d2017-01-10 14:47:34 +00003599 vma->display_alignment = I915_GTT_MIN_ALIGNMENT;
Tvrtko Ursuline6617332015-03-23 11:10:33 +00003600
Chris Wilson383d5822016-08-18 17:17:08 +01003601 /* Bump the LRU to try and avoid premature eviction whilst flipping */
Chris Wilsonbefedbb2017-01-19 19:26:55 +00003602 i915_gem_object_bump_inactive_ggtt(vma->obj);
Chris Wilson383d5822016-08-18 17:17:08 +01003603
Chris Wilson058d88c2016-08-15 10:49:06 +01003604 i915_vma_unpin(vma);
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003605}
3606
Eric Anholte47c68e2008-11-14 13:35:19 -08003607/**
3608 * Moves a single object to the CPU read, and possibly write domain.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01003609 * @obj: object to act on
3610 * @write: requesting write or read-only access
Eric Anholte47c68e2008-11-14 13:35:19 -08003611 *
3612 * This function returns when the move is complete, including waiting on
3613 * flushes to occur.
3614 */
Chris Wilsondabdfe02012-03-26 10:10:27 +02003615int
Chris Wilson919926a2010-11-12 13:42:53 +00003616i915_gem_object_set_to_cpu_domain(struct drm_i915_gem_object *obj, bool write)
Eric Anholte47c68e2008-11-14 13:35:19 -08003617{
Eric Anholte47c68e2008-11-14 13:35:19 -08003618 int ret;
3619
Chris Wilsone95433c2016-10-28 13:58:27 +01003620 lockdep_assert_held(&obj->base.dev->struct_mutex);
Chris Wilson4c7d62c2016-10-28 13:58:32 +01003621
Chris Wilsone95433c2016-10-28 13:58:27 +01003622 ret = i915_gem_object_wait(obj,
3623 I915_WAIT_INTERRUPTIBLE |
3624 I915_WAIT_LOCKED |
3625 (write ? I915_WAIT_ALL : 0),
Chris Wilson62eb3c22019-02-13 09:25:04 +00003626 MAX_SCHEDULE_TIMEOUT);
Chris Wilson88241782011-01-07 17:09:48 +00003627 if (ret)
3628 return ret;
3629
Chris Wilsonef749212017-04-12 12:01:10 +01003630 flush_write_domain(obj, ~I915_GEM_DOMAIN_CPU);
Eric Anholte47c68e2008-11-14 13:35:19 -08003631
Eric Anholte47c68e2008-11-14 13:35:19 -08003632 /* Flush the CPU cache if it's still invalid. */
Christian Königc0a51fd2018-02-16 13:43:38 +01003633 if ((obj->read_domains & I915_GEM_DOMAIN_CPU) == 0) {
Chris Wilson57822dc2017-02-22 11:40:48 +00003634 i915_gem_clflush_object(obj, I915_CLFLUSH_SYNC);
Christian Königc0a51fd2018-02-16 13:43:38 +01003635 obj->read_domains |= I915_GEM_DOMAIN_CPU;
Eric Anholte47c68e2008-11-14 13:35:19 -08003636 }
3637
3638 /* It should now be out of any other write domains, and we can update
3639 * the domain values for our changes.
3640 */
Christian Königc0a51fd2018-02-16 13:43:38 +01003641 GEM_BUG_ON(obj->write_domain & ~I915_GEM_DOMAIN_CPU);
Eric Anholte47c68e2008-11-14 13:35:19 -08003642
3643 /* If we're writing through the CPU, then the GPU read domains will
3644 * need to be invalidated at next use.
3645 */
Chris Wilsone27ab732017-06-15 13:38:49 +01003646 if (write)
3647 __start_cpu_write(obj);
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003648
3649 return 0;
3650}
3651
Eric Anholt673a3942008-07-30 12:06:12 -07003652/* Throttle our rendering by waiting until the ring has completed our requests
3653 * emitted over 20 msec ago.
3654 *
Eric Anholtb9624422009-06-03 07:27:35 +00003655 * Note that if we were to use the current jiffies each time around the loop,
3656 * we wouldn't escape the function with any frames outstanding if the time to
3657 * render a frame was over 20ms.
3658 *
Eric Anholt673a3942008-07-30 12:06:12 -07003659 * This should get us reasonable parallelism between CPU and GPU but also
3660 * relatively low latency when blocking on a particular request to finish.
3661 */
3662static int
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003663i915_gem_ring_throttle(struct drm_device *dev, struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07003664{
Chris Wilsonfac5e232016-07-04 11:34:36 +01003665 struct drm_i915_private *dev_priv = to_i915(dev);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003666 struct drm_i915_file_private *file_priv = file->driver_priv;
Chris Wilsond0bc54f2015-05-21 21:01:48 +01003667 unsigned long recent_enough = jiffies - DRM_I915_THROTTLE_JIFFIES;
Chris Wilsone61e0f52018-02-21 09:56:36 +00003668 struct i915_request *request, *target = NULL;
Chris Wilsone95433c2016-10-28 13:58:27 +01003669 long ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003670
Chris Wilsonf4457ae2016-04-13 17:35:08 +01003671 /* ABI: return -EIO if already wedged */
Chris Wilsonc41166f2019-02-20 14:56:37 +00003672 ret = i915_terminally_wedged(dev_priv);
3673 if (ret)
3674 return ret;
Chris Wilsone110e8d2011-01-26 15:39:14 +00003675
Chris Wilson1c255952010-09-26 11:03:27 +01003676 spin_lock(&file_priv->mm.lock);
Chris Wilsonc8659ef2017-03-02 12:25:25 +00003677 list_for_each_entry(request, &file_priv->mm.request_list, client_link) {
Eric Anholtb9624422009-06-03 07:27:35 +00003678 if (time_after_eq(request->emitted_jiffies, recent_enough))
3679 break;
3680
Chris Wilsonc8659ef2017-03-02 12:25:25 +00003681 if (target) {
3682 list_del(&target->client_link);
3683 target->file_priv = NULL;
3684 }
John Harrisonfcfa423c2015-05-29 17:44:12 +01003685
John Harrison54fb2412014-11-24 18:49:27 +00003686 target = request;
Eric Anholtb9624422009-06-03 07:27:35 +00003687 }
John Harrisonff865882014-11-24 18:49:28 +00003688 if (target)
Chris Wilsone61e0f52018-02-21 09:56:36 +00003689 i915_request_get(target);
Chris Wilson1c255952010-09-26 11:03:27 +01003690 spin_unlock(&file_priv->mm.lock);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003691
John Harrison54fb2412014-11-24 18:49:27 +00003692 if (target == NULL)
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003693 return 0;
3694
Chris Wilsone61e0f52018-02-21 09:56:36 +00003695 ret = i915_request_wait(target,
Chris Wilsone95433c2016-10-28 13:58:27 +01003696 I915_WAIT_INTERRUPTIBLE,
3697 MAX_SCHEDULE_TIMEOUT);
Chris Wilsone61e0f52018-02-21 09:56:36 +00003698 i915_request_put(target);
John Harrisonff865882014-11-24 18:49:28 +00003699
Chris Wilsone95433c2016-10-28 13:58:27 +01003700 return ret < 0 ? ret : 0;
Eric Anholt673a3942008-07-30 12:06:12 -07003701}
3702
Chris Wilson058d88c2016-08-15 10:49:06 +01003703struct i915_vma *
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02003704i915_gem_object_ggtt_pin(struct drm_i915_gem_object *obj,
3705 const struct i915_ggtt_view *view,
Chris Wilson91b2db62016-08-04 16:32:23 +01003706 u64 size,
Chris Wilson2ffffd02016-08-04 16:32:22 +01003707 u64 alignment,
3708 u64 flags)
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02003709{
Chris Wilsonad16d2e2016-10-13 09:55:04 +01003710 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
Chris Wilson82ad6442018-06-05 16:37:58 +01003711 struct i915_address_space *vm = &dev_priv->ggtt.vm;
Chris Wilson59bfa122016-08-04 16:32:31 +01003712 struct i915_vma *vma;
3713 int ret;
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03003714
Chris Wilson4c7d62c2016-10-28 13:58:32 +01003715 lockdep_assert_held(&obj->base.dev->struct_mutex);
3716
Chris Wilsonac87a6fd2018-02-20 13:42:05 +00003717 if (flags & PIN_MAPPABLE &&
3718 (!view || view->type == I915_GGTT_VIEW_NORMAL)) {
Chris Wilson43ae70d92017-10-09 09:44:01 +01003719 /* If the required space is larger than the available
3720 * aperture, we will not able to find a slot for the
3721 * object and unbinding the object now will be in
3722 * vain. Worse, doing so may cause us to ping-pong
3723 * the object in and out of the Global GTT and
3724 * waste a lot of cycles under the mutex.
3725 */
3726 if (obj->base.size > dev_priv->ggtt.mappable_end)
3727 return ERR_PTR(-E2BIG);
3728
3729 /* If NONBLOCK is set the caller is optimistically
3730 * trying to cache the full object within the mappable
3731 * aperture, and *must* have a fallback in place for
3732 * situations where we cannot bind the object. We
3733 * can be a little more lax here and use the fallback
3734 * more often to avoid costly migrations of ourselves
3735 * and other objects within the aperture.
3736 *
3737 * Half-the-aperture is used as a simple heuristic.
3738 * More interesting would to do search for a free
3739 * block prior to making the commitment to unbind.
3740 * That caters for the self-harm case, and with a
3741 * little more heuristics (e.g. NOFAULT, NOEVICT)
3742 * we could try to minimise harm to others.
3743 */
3744 if (flags & PIN_NONBLOCK &&
3745 obj->base.size > dev_priv->ggtt.mappable_end / 2)
3746 return ERR_PTR(-ENOSPC);
3747 }
3748
Chris Wilson718659a2017-01-16 15:21:28 +00003749 vma = i915_vma_instance(obj, vm, view);
Chengguang Xu772b5402019-02-21 10:08:19 +08003750 if (IS_ERR(vma))
Chris Wilson058d88c2016-08-15 10:49:06 +01003751 return vma;
Chris Wilson59bfa122016-08-04 16:32:31 +01003752
3753 if (i915_vma_misplaced(vma, size, alignment, flags)) {
Chris Wilson43ae70d92017-10-09 09:44:01 +01003754 if (flags & PIN_NONBLOCK) {
3755 if (i915_vma_is_pinned(vma) || i915_vma_is_active(vma))
3756 return ERR_PTR(-ENOSPC);
Chris Wilson59bfa122016-08-04 16:32:31 +01003757
Chris Wilson43ae70d92017-10-09 09:44:01 +01003758 if (flags & PIN_MAPPABLE &&
Chris Wilson944397f2017-01-09 16:16:11 +00003759 vma->fence_size > dev_priv->ggtt.mappable_end / 2)
Chris Wilsonad16d2e2016-10-13 09:55:04 +01003760 return ERR_PTR(-ENOSPC);
3761 }
3762
Chris Wilson59bfa122016-08-04 16:32:31 +01003763 WARN(i915_vma_is_pinned(vma),
3764 "bo is already pinned in ggtt with incorrect alignment:"
Chris Wilson05a20d02016-08-18 17:16:55 +01003765 " offset=%08x, req.alignment=%llx,"
3766 " req.map_and_fenceable=%d, vma->map_and_fenceable=%d\n",
3767 i915_ggtt_offset(vma), alignment,
Chris Wilson59bfa122016-08-04 16:32:31 +01003768 !!(flags & PIN_MAPPABLE),
Chris Wilson05a20d02016-08-18 17:16:55 +01003769 i915_vma_is_map_and_fenceable(vma));
Chris Wilson59bfa122016-08-04 16:32:31 +01003770 ret = i915_vma_unbind(vma);
3771 if (ret)
Chris Wilson058d88c2016-08-15 10:49:06 +01003772 return ERR_PTR(ret);
Chris Wilson59bfa122016-08-04 16:32:31 +01003773 }
3774
Chris Wilson058d88c2016-08-15 10:49:06 +01003775 ret = i915_vma_pin(vma, size, alignment, flags | PIN_GLOBAL);
3776 if (ret)
3777 return ERR_PTR(ret);
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02003778
Chris Wilson058d88c2016-08-15 10:49:06 +01003779 return vma;
Eric Anholt673a3942008-07-30 12:06:12 -07003780}
3781
Chris Wilsonedf6b762016-08-09 09:23:33 +01003782static __always_inline unsigned int __busy_read_flag(unsigned int id)
Chris Wilson3fdc13c2016-08-05 10:14:18 +01003783{
Chris Wilsonc8b50242019-03-05 16:26:43 +00003784 if (id == I915_ENGINE_CLASS_INVALID)
3785 return 0xffff0000;
3786
3787 GEM_BUG_ON(id >= 16);
Chris Wilson3fdc13c2016-08-05 10:14:18 +01003788 return 0x10000 << id;
3789}
3790
3791static __always_inline unsigned int __busy_write_id(unsigned int id)
3792{
Chris Wilsonc8b50242019-03-05 16:26:43 +00003793 /*
3794 * The uABI guarantees an active writer is also amongst the read
Chris Wilson70cb4722016-08-09 18:08:25 +01003795 * engines. This would be true if we accessed the activity tracking
3796 * under the lock, but as we perform the lookup of the object and
3797 * its activity locklessly we can not guarantee that the last_write
3798 * being active implies that we have set the same engine flag from
3799 * last_read - hence we always set both read and write busy for
3800 * last_write.
3801 */
Chris Wilsonc8b50242019-03-05 16:26:43 +00003802 if (id == I915_ENGINE_CLASS_INVALID)
3803 return 0xffffffff;
3804
3805 return (id + 1) | __busy_read_flag(id);
Chris Wilson3fdc13c2016-08-05 10:14:18 +01003806}
3807
Chris Wilsonedf6b762016-08-09 09:23:33 +01003808static __always_inline unsigned int
Chris Wilsond07f0e52016-10-28 13:58:44 +01003809__busy_set_if_active(const struct dma_fence *fence,
Chris Wilson3fdc13c2016-08-05 10:14:18 +01003810 unsigned int (*flag)(unsigned int id))
3811{
Chris Wilsonc8b50242019-03-05 16:26:43 +00003812 const struct i915_request *rq;
Chris Wilson12555012016-08-16 09:50:40 +01003813
Chris Wilsonc8b50242019-03-05 16:26:43 +00003814 /*
3815 * We have to check the current hw status of the fence as the uABI
Chris Wilsond07f0e52016-10-28 13:58:44 +01003816 * guarantees forward progress. We could rely on the idle worker
3817 * to eventually flush us, but to minimise latency just ask the
3818 * hardware.
3819 *
3820 * Note we only report on the status of native fences.
3821 */
3822 if (!dma_fence_is_i915(fence))
Chris Wilson12555012016-08-16 09:50:40 +01003823 return 0;
3824
Chris Wilsond07f0e52016-10-28 13:58:44 +01003825 /* opencode to_request() in order to avoid const warnings */
Chris Wilsonc8b50242019-03-05 16:26:43 +00003826 rq = container_of(fence, const struct i915_request, fence);
Chris Wilsone61e0f52018-02-21 09:56:36 +00003827 if (i915_request_completed(rq))
Chris Wilsond07f0e52016-10-28 13:58:44 +01003828 return 0;
3829
Chris Wilsonc8b50242019-03-05 16:26:43 +00003830 return flag(rq->engine->uabi_class);
Chris Wilson3fdc13c2016-08-05 10:14:18 +01003831}
3832
Chris Wilsonedf6b762016-08-09 09:23:33 +01003833static __always_inline unsigned int
Chris Wilsond07f0e52016-10-28 13:58:44 +01003834busy_check_reader(const struct dma_fence *fence)
Chris Wilson3fdc13c2016-08-05 10:14:18 +01003835{
Chris Wilsond07f0e52016-10-28 13:58:44 +01003836 return __busy_set_if_active(fence, __busy_read_flag);
Chris Wilson3fdc13c2016-08-05 10:14:18 +01003837}
3838
Chris Wilsonedf6b762016-08-09 09:23:33 +01003839static __always_inline unsigned int
Chris Wilsond07f0e52016-10-28 13:58:44 +01003840busy_check_writer(const struct dma_fence *fence)
Chris Wilson3fdc13c2016-08-05 10:14:18 +01003841{
Chris Wilsond07f0e52016-10-28 13:58:44 +01003842 if (!fence)
3843 return 0;
3844
3845 return __busy_set_if_active(fence, __busy_write_id);
Chris Wilson3fdc13c2016-08-05 10:14:18 +01003846}
3847
Eric Anholt673a3942008-07-30 12:06:12 -07003848int
Eric Anholt673a3942008-07-30 12:06:12 -07003849i915_gem_busy_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00003850 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07003851{
3852 struct drm_i915_gem_busy *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00003853 struct drm_i915_gem_object *obj;
Chris Wilsond07f0e52016-10-28 13:58:44 +01003854 struct reservation_object_list *list;
3855 unsigned int seq;
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01003856 int err;
Eric Anholt673a3942008-07-30 12:06:12 -07003857
Chris Wilsond07f0e52016-10-28 13:58:44 +01003858 err = -ENOENT;
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01003859 rcu_read_lock();
3860 obj = i915_gem_object_lookup_rcu(file, args->handle);
Chris Wilsond07f0e52016-10-28 13:58:44 +01003861 if (!obj)
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01003862 goto out;
Chris Wilsond07f0e52016-10-28 13:58:44 +01003863
Chris Wilsonc8b50242019-03-05 16:26:43 +00003864 /*
3865 * A discrepancy here is that we do not report the status of
Chris Wilsond07f0e52016-10-28 13:58:44 +01003866 * non-i915 fences, i.e. even though we may report the object as idle,
3867 * a call to set-domain may still stall waiting for foreign rendering.
3868 * This also means that wait-ioctl may report an object as busy,
3869 * where busy-ioctl considers it idle.
3870 *
3871 * We trade the ability to warn of foreign fences to report on which
3872 * i915 engines are active for the object.
3873 *
3874 * Alternatively, we can trade that extra information on read/write
3875 * activity with
3876 * args->busy =
3877 * !reservation_object_test_signaled_rcu(obj->resv, true);
3878 * to report the overall busyness. This is what the wait-ioctl does.
3879 *
3880 */
3881retry:
3882 seq = raw_read_seqcount(&obj->resv->seq);
3883
3884 /* Translate the exclusive fence to the READ *and* WRITE engine */
3885 args->busy = busy_check_writer(rcu_dereference(obj->resv->fence_excl));
3886
3887 /* Translate shared fences to READ set of engines */
3888 list = rcu_dereference(obj->resv->fence);
3889 if (list) {
3890 unsigned int shared_count = list->shared_count, i;
3891
3892 for (i = 0; i < shared_count; ++i) {
3893 struct dma_fence *fence =
3894 rcu_dereference(list->shared[i]);
3895
3896 args->busy |= busy_check_reader(fence);
3897 }
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01003898 }
Zou Nan haid1b851f2010-05-21 09:08:57 +08003899
Chris Wilsond07f0e52016-10-28 13:58:44 +01003900 if (args->busy && read_seqcount_retry(&obj->resv->seq, seq))
3901 goto retry;
Chris Wilson426960b2016-01-15 16:51:46 +00003902
Chris Wilsond07f0e52016-10-28 13:58:44 +01003903 err = 0;
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01003904out:
3905 rcu_read_unlock();
3906 return err;
Eric Anholt673a3942008-07-30 12:06:12 -07003907}
3908
3909int
3910i915_gem_throttle_ioctl(struct drm_device *dev, void *data,
3911 struct drm_file *file_priv)
3912{
Akshay Joshi0206e352011-08-16 15:34:10 -04003913 return i915_gem_ring_throttle(dev, file_priv);
Eric Anholt673a3942008-07-30 12:06:12 -07003914}
3915
Chris Wilson3ef94da2009-09-14 16:50:29 +01003916int
3917i915_gem_madvise_ioctl(struct drm_device *dev, void *data,
3918 struct drm_file *file_priv)
3919{
Chris Wilsonfac5e232016-07-04 11:34:36 +01003920 struct drm_i915_private *dev_priv = to_i915(dev);
Chris Wilson3ef94da2009-09-14 16:50:29 +01003921 struct drm_i915_gem_madvise *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00003922 struct drm_i915_gem_object *obj;
Chris Wilson1233e2d2016-10-28 13:58:37 +01003923 int err;
Chris Wilson3ef94da2009-09-14 16:50:29 +01003924
3925 switch (args->madv) {
3926 case I915_MADV_DONTNEED:
3927 case I915_MADV_WILLNEED:
3928 break;
3929 default:
3930 return -EINVAL;
3931 }
3932
Chris Wilson03ac0642016-07-20 13:31:51 +01003933 obj = i915_gem_object_lookup(file_priv, args->handle);
Chris Wilson1233e2d2016-10-28 13:58:37 +01003934 if (!obj)
3935 return -ENOENT;
3936
3937 err = mutex_lock_interruptible(&obj->mm.lock);
3938 if (err)
3939 goto out;
Chris Wilson3ef94da2009-09-14 16:50:29 +01003940
Chris Wilsonf1fa4f42017-10-13 21:26:13 +01003941 if (i915_gem_object_has_pages(obj) &&
Chris Wilson3e510a82016-08-05 10:14:23 +01003942 i915_gem_object_is_tiled(obj) &&
Daniel Vetter656bfa32014-11-20 09:26:30 +01003943 dev_priv->quirks & QUIRK_PIN_SWIZZLED_PAGES) {
Chris Wilsonbc0629a2016-11-01 10:03:17 +00003944 if (obj->mm.madv == I915_MADV_WILLNEED) {
3945 GEM_BUG_ON(!obj->mm.quirked);
Chris Wilsona4f5ea62016-10-28 13:58:35 +01003946 __i915_gem_object_unpin_pages(obj);
Chris Wilsonbc0629a2016-11-01 10:03:17 +00003947 obj->mm.quirked = false;
3948 }
3949 if (args->madv == I915_MADV_WILLNEED) {
Chris Wilson2c3a3f42016-11-04 10:30:01 +00003950 GEM_BUG_ON(obj->mm.quirked);
Chris Wilsona4f5ea62016-10-28 13:58:35 +01003951 __i915_gem_object_pin_pages(obj);
Chris Wilsonbc0629a2016-11-01 10:03:17 +00003952 obj->mm.quirked = true;
3953 }
Daniel Vetter656bfa32014-11-20 09:26:30 +01003954 }
3955
Chris Wilsona4f5ea62016-10-28 13:58:35 +01003956 if (obj->mm.madv != __I915_MADV_PURGED)
3957 obj->mm.madv = args->madv;
Chris Wilson3ef94da2009-09-14 16:50:29 +01003958
Chris Wilson6c085a72012-08-20 11:40:46 +02003959 /* if the object is no longer attached, discard its backing storage */
Chris Wilsonf1fa4f42017-10-13 21:26:13 +01003960 if (obj->mm.madv == I915_MADV_DONTNEED &&
3961 !i915_gem_object_has_pages(obj))
Chris Wilson2d7ef392009-09-20 23:13:10 +01003962 i915_gem_object_truncate(obj);
3963
Chris Wilsona4f5ea62016-10-28 13:58:35 +01003964 args->retained = obj->mm.madv != __I915_MADV_PURGED;
Chris Wilson1233e2d2016-10-28 13:58:37 +01003965 mutex_unlock(&obj->mm.lock);
Chris Wilsonbb6baf72009-09-22 14:24:13 +01003966
Chris Wilson1233e2d2016-10-28 13:58:37 +01003967out:
Chris Wilsonf8c417c2016-07-20 13:31:53 +01003968 i915_gem_object_put(obj);
Chris Wilson1233e2d2016-10-28 13:58:37 +01003969 return err;
Chris Wilson3ef94da2009-09-14 16:50:29 +01003970}
3971
Chris Wilson5b8c8ae2016-11-16 19:07:04 +00003972static void
Chris Wilson21950ee2019-02-05 13:00:05 +00003973frontbuffer_retire(struct i915_active_request *active,
3974 struct i915_request *request)
Chris Wilson5b8c8ae2016-11-16 19:07:04 +00003975{
3976 struct drm_i915_gem_object *obj =
3977 container_of(active, typeof(*obj), frontbuffer_write);
3978
Chris Wilsond59b21e2017-02-22 11:40:49 +00003979 intel_fb_obj_flush(obj, ORIGIN_CS);
Chris Wilson5b8c8ae2016-11-16 19:07:04 +00003980}
3981
Chris Wilson37e680a2012-06-07 15:38:42 +01003982void i915_gem_object_init(struct drm_i915_gem_object *obj,
3983 const struct drm_i915_gem_object_ops *ops)
Chris Wilson0327d6b2012-08-11 15:41:06 +01003984{
Chris Wilson1233e2d2016-10-28 13:58:37 +01003985 mutex_init(&obj->mm.lock);
3986
Chris Wilson528cbd12019-01-28 10:23:54 +00003987 spin_lock_init(&obj->vma.lock);
3988 INIT_LIST_HEAD(&obj->vma.list);
3989
Chris Wilsond1b48c12017-08-16 09:52:08 +01003990 INIT_LIST_HEAD(&obj->lut_list);
Chris Wilson8d9d5742015-04-07 16:20:38 +01003991 INIT_LIST_HEAD(&obj->batch_pool_link);
Chris Wilson0327d6b2012-08-11 15:41:06 +01003992
Chris Wilson8811d612018-11-09 09:03:11 +00003993 init_rcu_head(&obj->rcu);
3994
Chris Wilson37e680a2012-06-07 15:38:42 +01003995 obj->ops = ops;
3996
Chris Wilsond07f0e52016-10-28 13:58:44 +01003997 reservation_object_init(&obj->__builtin_resv);
3998 obj->resv = &obj->__builtin_resv;
3999
Chris Wilson50349242016-08-18 17:17:04 +01004000 obj->frontbuffer_ggtt_origin = ORIGIN_GTT;
Chris Wilson21950ee2019-02-05 13:00:05 +00004001 i915_active_request_init(&obj->frontbuffer_write,
4002 NULL, frontbuffer_retire);
Chris Wilsona4f5ea62016-10-28 13:58:35 +01004003
4004 obj->mm.madv = I915_MADV_WILLNEED;
4005 INIT_RADIX_TREE(&obj->mm.get_page.radix, GFP_KERNEL | __GFP_NOWARN);
4006 mutex_init(&obj->mm.get_page.lock);
Chris Wilson0327d6b2012-08-11 15:41:06 +01004007
Dave Gordonf19ec8c2016-07-04 11:34:37 +01004008 i915_gem_info_add_obj(to_i915(obj->base.dev), obj->base.size);
Chris Wilson0327d6b2012-08-11 15:41:06 +01004009}
4010
Chris Wilson37e680a2012-06-07 15:38:42 +01004011static const struct drm_i915_gem_object_ops i915_gem_object_ops = {
Tvrtko Ursulin3599a912016-11-01 14:44:10 +00004012 .flags = I915_GEM_OBJECT_HAS_STRUCT_PAGE |
4013 I915_GEM_OBJECT_IS_SHRINKABLE,
Chris Wilson7c55e2c2017-03-07 12:03:38 +00004014
Chris Wilson37e680a2012-06-07 15:38:42 +01004015 .get_pages = i915_gem_object_get_pages_gtt,
4016 .put_pages = i915_gem_object_put_pages_gtt,
Chris Wilson7c55e2c2017-03-07 12:03:38 +00004017
4018 .pwrite = i915_gem_object_pwrite_gtt,
Chris Wilson37e680a2012-06-07 15:38:42 +01004019};
4020
Matthew Auld465c4032017-10-06 23:18:14 +01004021static int i915_gem_object_create_shmem(struct drm_device *dev,
4022 struct drm_gem_object *obj,
4023 size_t size)
4024{
4025 struct drm_i915_private *i915 = to_i915(dev);
4026 unsigned long flags = VM_NORESERVE;
4027 struct file *filp;
4028
4029 drm_gem_private_object_init(dev, obj, size);
4030
4031 if (i915->mm.gemfs)
4032 filp = shmem_file_setup_with_mnt(i915->mm.gemfs, "i915", size,
4033 flags);
4034 else
4035 filp = shmem_file_setup("i915", size, flags);
4036
4037 if (IS_ERR(filp))
4038 return PTR_ERR(filp);
4039
4040 obj->filp = filp;
4041
4042 return 0;
4043}
4044
Chris Wilsonb4bcbe22016-10-18 13:02:49 +01004045struct drm_i915_gem_object *
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +00004046i915_gem_object_create(struct drm_i915_private *dev_priv, u64 size)
Daniel Vetterac52bc52010-04-09 19:05:06 +00004047{
Daniel Vetterc397b902010-04-09 19:05:07 +00004048 struct drm_i915_gem_object *obj;
Hugh Dickins5949eac2011-06-27 16:18:18 -07004049 struct address_space *mapping;
Chris Wilsonb8f55be2017-08-11 12:11:16 +01004050 unsigned int cache_level;
Daniel Vetter1a240d42012-11-29 22:18:51 +01004051 gfp_t mask;
Chris Wilsonfe3db792016-04-25 13:32:13 +01004052 int ret;
Daniel Vetterc397b902010-04-09 19:05:07 +00004053
Chris Wilsonb4bcbe22016-10-18 13:02:49 +01004054 /* There is a prevalence of the assumption that we fit the object's
4055 * page count inside a 32bit _signed_ variable. Let's document this and
4056 * catch if we ever need to fix it. In the meantime, if you do spot
4057 * such a local variable, please consider fixing!
4058 */
Tvrtko Ursulin7a3ee5d2017-03-30 17:31:30 +01004059 if (size >> PAGE_SHIFT > INT_MAX)
Chris Wilsonb4bcbe22016-10-18 13:02:49 +01004060 return ERR_PTR(-E2BIG);
4061
4062 if (overflows_type(size, obj->base.size))
4063 return ERR_PTR(-E2BIG);
4064
Chris Wilson13f1bfd2019-02-28 10:20:34 +00004065 obj = i915_gem_object_alloc();
Daniel Vetterc397b902010-04-09 19:05:07 +00004066 if (obj == NULL)
Chris Wilsonfe3db792016-04-25 13:32:13 +01004067 return ERR_PTR(-ENOMEM);
Daniel Vetterc397b902010-04-09 19:05:07 +00004068
Matthew Auld465c4032017-10-06 23:18:14 +01004069 ret = i915_gem_object_create_shmem(&dev_priv->drm, &obj->base, size);
Chris Wilsonfe3db792016-04-25 13:32:13 +01004070 if (ret)
4071 goto fail;
Daniel Vetterc397b902010-04-09 19:05:07 +00004072
Chris Wilsonbed1ea92012-05-24 20:48:12 +01004073 mask = GFP_HIGHUSER | __GFP_RECLAIMABLE;
Jani Nikulac0f86832016-12-07 12:13:04 +02004074 if (IS_I965GM(dev_priv) || IS_I965G(dev_priv)) {
Chris Wilsonbed1ea92012-05-24 20:48:12 +01004075 /* 965gm cannot relocate objects above 4GiB. */
4076 mask &= ~__GFP_HIGHMEM;
4077 mask |= __GFP_DMA32;
4078 }
4079
Al Viro93c76a32015-12-04 23:45:44 -05004080 mapping = obj->base.filp->f_mapping;
Chris Wilsonbed1ea92012-05-24 20:48:12 +01004081 mapping_set_gfp_mask(mapping, mask);
Chris Wilson4846bf02017-06-09 12:03:46 +01004082 GEM_BUG_ON(!(mapping_gfp_mask(mapping) & __GFP_RECLAIM));
Hugh Dickins5949eac2011-06-27 16:18:18 -07004083
Chris Wilson37e680a2012-06-07 15:38:42 +01004084 i915_gem_object_init(obj, &i915_gem_object_ops);
Chris Wilson73aa8082010-09-30 11:46:12 +01004085
Christian Königc0a51fd2018-02-16 13:43:38 +01004086 obj->write_domain = I915_GEM_DOMAIN_CPU;
4087 obj->read_domains = I915_GEM_DOMAIN_CPU;
Daniel Vetterc397b902010-04-09 19:05:07 +00004088
Chris Wilsonb8f55be2017-08-11 12:11:16 +01004089 if (HAS_LLC(dev_priv))
Eugeni Dodonov3d29b842012-01-17 14:43:53 -02004090 /* On some devices, we can have the GPU use the LLC (the CPU
Eric Anholta1871112011-03-29 16:59:55 -07004091 * cache) for about a 10% performance improvement
4092 * compared to uncached. Graphics requests other than
4093 * display scanout are coherent with the CPU in
4094 * accessing this cache. This means in this mode we
4095 * don't need to clflush on the CPU side, and on the
4096 * GPU side we only need to flush internal caches to
4097 * get data visible to the CPU.
4098 *
4099 * However, we maintain the display planes as UC, and so
4100 * need to rebind when first used as such.
4101 */
Chris Wilsonb8f55be2017-08-11 12:11:16 +01004102 cache_level = I915_CACHE_LLC;
4103 else
4104 cache_level = I915_CACHE_NONE;
Eric Anholta1871112011-03-29 16:59:55 -07004105
Chris Wilsonb8f55be2017-08-11 12:11:16 +01004106 i915_gem_object_set_cache_coherency(obj, cache_level);
Chris Wilsone27ab732017-06-15 13:38:49 +01004107
Daniel Vetterd861e332013-07-24 23:25:03 +02004108 trace_i915_gem_object_create(obj);
4109
Chris Wilson05394f32010-11-08 19:18:58 +00004110 return obj;
Chris Wilsonfe3db792016-04-25 13:32:13 +01004111
4112fail:
4113 i915_gem_object_free(obj);
Chris Wilsonfe3db792016-04-25 13:32:13 +01004114 return ERR_PTR(ret);
Daniel Vetterac52bc52010-04-09 19:05:06 +00004115}
4116
Chris Wilson340fbd82014-05-22 09:16:52 +01004117static bool discard_backing_storage(struct drm_i915_gem_object *obj)
4118{
4119 /* If we are the last user of the backing storage (be it shmemfs
4120 * pages or stolen etc), we know that the pages are going to be
4121 * immediately released. In this case, we can then skip copying
4122 * back the contents from the GPU.
4123 */
4124
Chris Wilsona4f5ea62016-10-28 13:58:35 +01004125 if (obj->mm.madv != I915_MADV_WILLNEED)
Chris Wilson340fbd82014-05-22 09:16:52 +01004126 return false;
4127
4128 if (obj->base.filp == NULL)
4129 return true;
4130
4131 /* At first glance, this looks racy, but then again so would be
4132 * userspace racing mmap against close. However, the first external
4133 * reference to the filp can only be obtained through the
4134 * i915_gem_mmap_ioctl() which safeguards us against the user
4135 * acquiring such a reference whilst we are in the middle of
4136 * freeing the object.
4137 */
4138 return atomic_long_read(&obj->base.filp->f_count) == 1;
4139}
4140
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004141static void __i915_gem_free_objects(struct drm_i915_private *i915,
4142 struct llist_node *freed)
Chris Wilsonbe726152010-07-23 23:18:50 +01004143{
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004144 struct drm_i915_gem_object *obj, *on;
Chris Wilson538ef962019-01-14 14:21:18 +00004145 intel_wakeref_t wakeref;
Chris Wilsonbe726152010-07-23 23:18:50 +01004146
Chris Wilson538ef962019-01-14 14:21:18 +00004147 wakeref = intel_runtime_pm_get(i915);
Chris Wilsoncc731f52017-10-13 21:26:21 +01004148 llist_for_each_entry_safe(obj, on, freed, freed) {
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004149 struct i915_vma *vma, *vn;
Paulo Zanonif65c9162013-11-27 18:20:34 -02004150
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004151 trace_i915_gem_object_destroy(obj);
4152
Chris Wilsoncc731f52017-10-13 21:26:21 +01004153 mutex_lock(&i915->drm.struct_mutex);
4154
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004155 GEM_BUG_ON(i915_gem_object_is_active(obj));
Chris Wilson528cbd12019-01-28 10:23:54 +00004156 list_for_each_entry_safe(vma, vn, &obj->vma.list, obj_link) {
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004157 GEM_BUG_ON(i915_vma_is_active(vma));
4158 vma->flags &= ~I915_VMA_PIN_MASK;
Chris Wilson3365e222018-05-03 20:51:14 +01004159 i915_vma_destroy(vma);
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004160 }
Chris Wilson528cbd12019-01-28 10:23:54 +00004161 GEM_BUG_ON(!list_empty(&obj->vma.list));
4162 GEM_BUG_ON(!RB_EMPTY_ROOT(&obj->vma.tree));
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004163
Chris Wilsonf2123812017-10-16 12:40:37 +01004164 /* This serializes freeing with the shrinker. Since the free
4165 * is delayed, first by RCU then by the workqueue, we want the
4166 * shrinker to be able to free pages of unreferenced objects,
4167 * or else we may oom whilst there are plenty of deferred
4168 * freed objects.
4169 */
4170 if (i915_gem_object_has_pages(obj)) {
4171 spin_lock(&i915->mm.obj_lock);
4172 list_del_init(&obj->mm.link);
4173 spin_unlock(&i915->mm.obj_lock);
4174 }
4175
Chris Wilsoncc731f52017-10-13 21:26:21 +01004176 mutex_unlock(&i915->drm.struct_mutex);
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004177
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004178 GEM_BUG_ON(obj->bind_count);
Chris Wilsona65adaf2017-10-09 09:43:57 +01004179 GEM_BUG_ON(obj->userfault_count);
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004180 GEM_BUG_ON(atomic_read(&obj->frontbuffer_bits));
Chris Wilson67b48042017-08-22 12:05:16 +01004181 GEM_BUG_ON(!list_empty(&obj->lut_list));
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004182
4183 if (obj->ops->release)
4184 obj->ops->release(obj);
4185
4186 if (WARN_ON(i915_gem_object_has_pinned_pages(obj)))
4187 atomic_set(&obj->mm.pages_pin_count, 0);
Chris Wilson548625e2016-11-01 12:11:34 +00004188 __i915_gem_object_put_pages(obj, I915_MM_NORMAL);
Chris Wilsonf1fa4f42017-10-13 21:26:13 +01004189 GEM_BUG_ON(i915_gem_object_has_pages(obj));
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004190
4191 if (obj->base.import_attach)
4192 drm_prime_gem_destroy(&obj->base, NULL);
4193
Chris Wilsond07f0e52016-10-28 13:58:44 +01004194 reservation_object_fini(&obj->__builtin_resv);
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004195 drm_gem_object_release(&obj->base);
4196 i915_gem_info_remove_obj(i915, obj->base.size);
4197
4198 kfree(obj->bit_17);
4199 i915_gem_object_free(obj);
Chris Wilsoncc731f52017-10-13 21:26:21 +01004200
Chris Wilsonc9c704712018-02-19 22:06:31 +00004201 GEM_BUG_ON(!atomic_read(&i915->mm.free_count));
4202 atomic_dec(&i915->mm.free_count);
4203
Chris Wilsoncc731f52017-10-13 21:26:21 +01004204 if (on)
4205 cond_resched();
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004206 }
Chris Wilson538ef962019-01-14 14:21:18 +00004207 intel_runtime_pm_put(i915, wakeref);
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004208}
4209
4210static void i915_gem_flush_free_objects(struct drm_i915_private *i915)
4211{
4212 struct llist_node *freed;
4213
Chris Wilson87701b42017-10-13 21:26:20 +01004214 /* Free the oldest, most stale object to keep the free_list short */
4215 freed = NULL;
4216 if (!llist_empty(&i915->mm.free_list)) { /* quick test for hotpath */
4217 /* Only one consumer of llist_del_first() allowed */
4218 spin_lock(&i915->mm.free_lock);
4219 freed = llist_del_first(&i915->mm.free_list);
4220 spin_unlock(&i915->mm.free_lock);
4221 }
4222 if (unlikely(freed)) {
4223 freed->next = NULL;
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004224 __i915_gem_free_objects(i915, freed);
Chris Wilson87701b42017-10-13 21:26:20 +01004225 }
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004226}
4227
4228static void __i915_gem_free_work(struct work_struct *work)
4229{
4230 struct drm_i915_private *i915 =
4231 container_of(work, struct drm_i915_private, mm.free_work);
4232 struct llist_node *freed;
Chris Wilson26e12f82011-03-20 11:20:19 +00004233
Chris Wilson2ef1e722018-01-15 20:57:59 +00004234 /*
4235 * All file-owned VMA should have been released by this point through
Chris Wilsonb1f788c2016-08-04 07:52:45 +01004236 * i915_gem_close_object(), or earlier by i915_gem_context_close().
4237 * However, the object may also be bound into the global GTT (e.g.
4238 * older GPUs without per-process support, or for direct access through
4239 * the GTT either for the user or for scanout). Those VMA still need to
4240 * unbound now.
4241 */
Chris Wilson1488fc02012-04-24 15:47:31 +01004242
Chris Wilsonf991c492017-11-06 11:15:08 +00004243 spin_lock(&i915->mm.free_lock);
Chris Wilson5ad08be2017-04-07 11:25:51 +01004244 while ((freed = llist_del_all(&i915->mm.free_list))) {
Chris Wilsonf991c492017-11-06 11:15:08 +00004245 spin_unlock(&i915->mm.free_lock);
4246
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004247 __i915_gem_free_objects(i915, freed);
Chris Wilson5ad08be2017-04-07 11:25:51 +01004248 if (need_resched())
Chris Wilsonf991c492017-11-06 11:15:08 +00004249 return;
4250
4251 spin_lock(&i915->mm.free_lock);
Chris Wilson5ad08be2017-04-07 11:25:51 +01004252 }
Chris Wilsonf991c492017-11-06 11:15:08 +00004253 spin_unlock(&i915->mm.free_lock);
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004254}
4255
4256static void __i915_gem_free_object_rcu(struct rcu_head *head)
4257{
4258 struct drm_i915_gem_object *obj =
4259 container_of(head, typeof(*obj), rcu);
4260 struct drm_i915_private *i915 = to_i915(obj->base.dev);
4261
Chris Wilson2ef1e722018-01-15 20:57:59 +00004262 /*
Chris Wilson8811d612018-11-09 09:03:11 +00004263 * We reuse obj->rcu for the freed list, so we had better not treat
4264 * it like a rcu_head from this point forwards. And we expect all
4265 * objects to be freed via this path.
4266 */
4267 destroy_rcu_head(&obj->rcu);
4268
4269 /*
Chris Wilson2ef1e722018-01-15 20:57:59 +00004270 * Since we require blocking on struct_mutex to unbind the freed
4271 * object from the GPU before releasing resources back to the
4272 * system, we can not do that directly from the RCU callback (which may
4273 * be a softirq context), but must instead then defer that work onto a
4274 * kthread. We use the RCU callback rather than move the freed object
4275 * directly onto the work queue so that we can mix between using the
4276 * worker and performing frees directly from subsequent allocations for
4277 * crude but effective memory throttling.
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004278 */
4279 if (llist_add(&obj->freed, &i915->mm.free_list))
Chris Wilsonbeacbd12018-01-15 12:28:45 +00004280 queue_work(i915->wq, &i915->mm.free_work);
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004281}
4282
4283void i915_gem_free_object(struct drm_gem_object *gem_obj)
4284{
4285 struct drm_i915_gem_object *obj = to_intel_bo(gem_obj);
4286
Chris Wilsonbc0629a2016-11-01 10:03:17 +00004287 if (obj->mm.quirked)
4288 __i915_gem_object_unpin_pages(obj);
4289
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004290 if (discard_backing_storage(obj))
4291 obj->mm.madv = I915_MADV_DONTNEED;
Daniel Vettera071fa02014-06-18 23:28:09 +02004292
Chris Wilson2ef1e722018-01-15 20:57:59 +00004293 /*
4294 * Before we free the object, make sure any pure RCU-only
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004295 * read-side critical sections are complete, e.g.
4296 * i915_gem_busy_ioctl(). For the corresponding synchronized
4297 * lookup see i915_gem_object_lookup_rcu().
4298 */
Chris Wilsonc9c704712018-02-19 22:06:31 +00004299 atomic_inc(&to_i915(obj->base.dev)->mm.free_count);
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004300 call_rcu(&obj->rcu, __i915_gem_free_object_rcu);
Chris Wilsonbe726152010-07-23 23:18:50 +01004301}
4302
Chris Wilsonf8a7fde2016-10-28 13:58:29 +01004303void __i915_gem_object_release_unless_active(struct drm_i915_gem_object *obj)
4304{
4305 lockdep_assert_held(&obj->base.dev->struct_mutex);
4306
Chris Wilsond1b48c12017-08-16 09:52:08 +01004307 if (!i915_gem_object_has_active_reference(obj) &&
4308 i915_gem_object_is_active(obj))
Chris Wilsonf8a7fde2016-10-28 13:58:29 +01004309 i915_gem_object_set_active_reference(obj);
4310 else
4311 i915_gem_object_put(obj);
4312}
4313
Chris Wilson24145512017-01-24 11:01:35 +00004314void i915_gem_sanitize(struct drm_i915_private *i915)
4315{
Chris Wilson538ef962019-01-14 14:21:18 +00004316 intel_wakeref_t wakeref;
4317
Chris Wilsonc3160da2018-05-31 09:22:45 +01004318 GEM_TRACE("\n");
4319
Chris Wilson538ef962019-01-14 14:21:18 +00004320 wakeref = intel_runtime_pm_get(i915);
Chris Wilsonc3160da2018-05-31 09:22:45 +01004321 intel_uncore_forcewake_get(i915, FORCEWAKE_ALL);
4322
4323 /*
4324 * As we have just resumed the machine and woken the device up from
4325 * deep PCI sleep (presumably D3_cold), assume the HW has been reset
4326 * back to defaults, recovering from whatever wedged state we left it
4327 * in and so worth trying to use the device once more.
4328 */
Chris Wilsonc41166f2019-02-20 14:56:37 +00004329 if (i915_terminally_wedged(i915))
Chris Wilsonf36325f2017-08-26 12:09:34 +01004330 i915_gem_unset_wedged(i915);
Chris Wilsonf36325f2017-08-26 12:09:34 +01004331
Chris Wilson24145512017-01-24 11:01:35 +00004332 /*
4333 * If we inherit context state from the BIOS or earlier occupants
4334 * of the GPU, the GPU may be in an inconsistent state when we
4335 * try to take over. The only way to remove the earlier state
4336 * is by resetting. However, resetting on earlier gen is tricky as
4337 * it may impact the display and we are uncertain about the stability
Joonas Lahtinenea117b82017-04-28 10:53:38 +03004338 * of the reset, so this could be applied to even earlier gen.
Chris Wilson24145512017-01-24 11:01:35 +00004339 */
Chris Wilson55277e12019-01-03 11:21:04 +00004340 intel_engines_sanitize(i915, false);
Chris Wilsonc3160da2018-05-31 09:22:45 +01004341
4342 intel_uncore_forcewake_put(i915, FORCEWAKE_ALL);
Chris Wilson538ef962019-01-14 14:21:18 +00004343 intel_runtime_pm_put(i915, wakeref);
Chris Wilsonc3160da2018-05-31 09:22:45 +01004344
Chris Wilsoneb8d0f52019-01-25 13:22:28 +00004345 mutex_lock(&i915->drm.struct_mutex);
Chris Wilson4dfacb02018-05-31 09:22:43 +01004346 i915_gem_contexts_lost(i915);
4347 mutex_unlock(&i915->drm.struct_mutex);
Chris Wilson24145512017-01-24 11:01:35 +00004348}
4349
Chris Wilson5861b012019-03-08 09:36:54 +00004350void i915_gem_suspend(struct drm_i915_private *i915)
Eric Anholt673a3942008-07-30 12:06:12 -07004351{
Chris Wilson538ef962019-01-14 14:21:18 +00004352 intel_wakeref_t wakeref;
Eric Anholt673a3942008-07-30 12:06:12 -07004353
Chris Wilson09a4c022018-05-24 09:11:35 +01004354 GEM_TRACE("\n");
4355
Chris Wilson538ef962019-01-14 14:21:18 +00004356 wakeref = intel_runtime_pm_get(i915);
Chris Wilsonbf061122018-07-09 14:02:04 +01004357 intel_suspend_gt_powersave(i915);
Chris Wilson54b4f682016-07-21 21:16:19 +01004358
Chris Wilsoneb8d0f52019-01-25 13:22:28 +00004359 flush_workqueue(i915->wq);
4360
Chris Wilsonbf061122018-07-09 14:02:04 +01004361 mutex_lock(&i915->drm.struct_mutex);
Chris Wilson5ab57c72016-07-15 14:56:20 +01004362
Chris Wilsonbf061122018-07-09 14:02:04 +01004363 /*
4364 * We have to flush all the executing contexts to main memory so
Chris Wilson5ab57c72016-07-15 14:56:20 +01004365 * that they can saved in the hibernation image. To ensure the last
4366 * context image is coherent, we have to switch away from it. That
Chris Wilsonbf061122018-07-09 14:02:04 +01004367 * leaves the i915->kernel_context still active when
Chris Wilson5ab57c72016-07-15 14:56:20 +01004368 * we actually suspend, and its image in memory may not match the GPU
4369 * state. Fortunately, the kernel_context is disposable and we do
4370 * not rely on its state.
4371 */
Chris Wilsonc6eeb472019-03-08 09:36:56 +00004372 switch_to_kernel_context_sync(i915, i915->gt.active_engines);
Chris Wilson01f8f332018-07-17 09:41:21 +01004373
Chris Wilsonbf061122018-07-09 14:02:04 +01004374 mutex_unlock(&i915->drm.struct_mutex);
Chris Wilsoneb8d0f52019-01-25 13:22:28 +00004375 i915_reset_flush(i915);
Chris Wilson45c5f202013-10-16 11:50:01 +01004376
Chris Wilsoneb8d0f52019-01-25 13:22:28 +00004377 drain_delayed_work(&i915->gt.retire_work);
Chris Wilsonbdeb9782016-12-23 14:57:56 +00004378
Chris Wilsonbf061122018-07-09 14:02:04 +01004379 /*
4380 * As the idle_work is rearming if it detects a race, play safe and
Chris Wilsonbdeb9782016-12-23 14:57:56 +00004381 * repeat the flush until it is definitely idle.
4382 */
Chris Wilsonbf061122018-07-09 14:02:04 +01004383 drain_delayed_work(&i915->gt.idle_work);
Chris Wilsonbdeb9782016-12-23 14:57:56 +00004384
Chris Wilsonbf061122018-07-09 14:02:04 +01004385 /*
4386 * Assert that we successfully flushed all the work and
Chris Wilsonbdcf1202014-11-25 11:56:33 +00004387 * reset the GPU back to its idle, low power state.
4388 */
Chris Wilson50b022a2019-03-07 10:45:30 +00004389 GEM_BUG_ON(i915->gt.awake);
Chris Wilsonbdcf1202014-11-25 11:56:33 +00004390
Chris Wilson538ef962019-01-14 14:21:18 +00004391 intel_runtime_pm_put(i915, wakeref);
Chris Wilsonec92ad02018-05-31 09:22:46 +01004392}
4393
4394void i915_gem_suspend_late(struct drm_i915_private *i915)
4395{
Chris Wilson9776f472018-06-01 15:41:24 +01004396 struct drm_i915_gem_object *obj;
4397 struct list_head *phases[] = {
4398 &i915->mm.unbound_list,
4399 &i915->mm.bound_list,
4400 NULL
4401 }, **phase;
4402
Imre Deak1c777c52016-10-12 17:46:37 +03004403 /*
4404 * Neither the BIOS, ourselves or any other kernel
4405 * expects the system to be in execlists mode on startup,
4406 * so we need to reset the GPU back to legacy mode. And the only
4407 * known way to disable logical contexts is through a GPU reset.
4408 *
4409 * So in order to leave the system in a known default configuration,
4410 * always reset the GPU upon unload and suspend. Afterwards we then
4411 * clean up the GEM state tracking, flushing off the requests and
4412 * leaving the system in a known idle state.
4413 *
4414 * Note that is of the upmost importance that the GPU is idle and
4415 * all stray writes are flushed *before* we dismantle the backing
4416 * storage for the pinned objects.
4417 *
4418 * However, since we are uncertain that resetting the GPU on older
4419 * machines is a good idea, we don't - just in case it leaves the
4420 * machine in an unusable condition.
4421 */
Chris Wilsoncad99462017-08-26 12:09:33 +01004422
Chris Wilson9776f472018-06-01 15:41:24 +01004423 mutex_lock(&i915->drm.struct_mutex);
4424 for (phase = phases; *phase; phase++) {
4425 list_for_each_entry(obj, *phase, mm.link)
4426 WARN_ON(i915_gem_object_set_to_gtt_domain(obj, false));
4427 }
4428 mutex_unlock(&i915->drm.struct_mutex);
4429
Chris Wilsonec92ad02018-05-31 09:22:46 +01004430 intel_uc_sanitize(i915);
4431 i915_gem_sanitize(i915);
Eric Anholt673a3942008-07-30 12:06:12 -07004432}
4433
Chris Wilson37cd3302017-11-12 11:27:38 +00004434void i915_gem_resume(struct drm_i915_private *i915)
Chris Wilson5ab57c72016-07-15 14:56:20 +01004435{
Chris Wilson4dfacb02018-05-31 09:22:43 +01004436 GEM_TRACE("\n");
4437
Chris Wilson37cd3302017-11-12 11:27:38 +00004438 WARN_ON(i915->gt.awake);
Chris Wilson5ab57c72016-07-15 14:56:20 +01004439
Chris Wilson37cd3302017-11-12 11:27:38 +00004440 mutex_lock(&i915->drm.struct_mutex);
4441 intel_uncore_forcewake_get(i915, FORCEWAKE_ALL);
Imre Deak31ab49a2016-11-07 11:20:05 +02004442
Chris Wilson37cd3302017-11-12 11:27:38 +00004443 i915_gem_restore_gtt_mappings(i915);
4444 i915_gem_restore_fences(i915);
Chris Wilson5ab57c72016-07-15 14:56:20 +01004445
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00004446 /*
4447 * As we didn't flush the kernel context before suspend, we cannot
Chris Wilson5ab57c72016-07-15 14:56:20 +01004448 * guarantee that the context image is complete. So let's just reset
4449 * it and start again.
4450 */
Chris Wilson37cd3302017-11-12 11:27:38 +00004451 i915->gt.resume(i915);
Chris Wilson5ab57c72016-07-15 14:56:20 +01004452
Chris Wilson37cd3302017-11-12 11:27:38 +00004453 if (i915_gem_init_hw(i915))
4454 goto err_wedged;
4455
Michal Wajdeczko7cfca4a2018-03-02 11:15:49 +00004456 intel_uc_resume(i915);
Chris Wilson7469c622017-11-14 13:03:00 +00004457
Chris Wilson37cd3302017-11-12 11:27:38 +00004458 /* Always reload a context for powersaving. */
Chris Wilson604c37d2019-03-08 09:36:55 +00004459 if (!load_power_context(i915))
Chris Wilson37cd3302017-11-12 11:27:38 +00004460 goto err_wedged;
4461
4462out_unlock:
4463 intel_uncore_forcewake_put(i915, FORCEWAKE_ALL);
4464 mutex_unlock(&i915->drm.struct_mutex);
4465 return;
4466
4467err_wedged:
Chris Wilsonc41166f2019-02-20 14:56:37 +00004468 if (!i915_reset_failed(i915)) {
4469 dev_err(i915->drm.dev,
4470 "Failed to re-initialize GPU, declaring it wedged!\n");
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00004471 i915_gem_set_wedged(i915);
4472 }
Chris Wilson37cd3302017-11-12 11:27:38 +00004473 goto out_unlock;
Chris Wilson5ab57c72016-07-15 14:56:20 +01004474}
4475
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00004476void i915_gem_init_swizzling(struct drm_i915_private *dev_priv)
Daniel Vetterf691e2f2012-02-02 09:58:12 +01004477{
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00004478 if (INTEL_GEN(dev_priv) < 5 ||
Daniel Vetterf691e2f2012-02-02 09:58:12 +01004479 dev_priv->mm.bit_6_swizzle_x == I915_BIT_6_SWIZZLE_NONE)
4480 return;
4481
4482 I915_WRITE(DISP_ARB_CTL, I915_READ(DISP_ARB_CTL) |
4483 DISP_TILE_SURFACE_SWIZZLING);
4484
Lucas De Marchicf819ef2018-12-12 10:10:43 -08004485 if (IS_GEN(dev_priv, 5))
Daniel Vetter11782b02012-01-31 16:47:55 +01004486 return;
4487
Daniel Vetterf691e2f2012-02-02 09:58:12 +01004488 I915_WRITE(TILECTL, I915_READ(TILECTL) | TILECTL_SWZCTL);
Lucas De Marchicf819ef2018-12-12 10:10:43 -08004489 if (IS_GEN(dev_priv, 6))
Daniel Vetter6b26c862012-04-24 14:04:12 +02004490 I915_WRITE(ARB_MODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_SNB));
Lucas De Marchicf819ef2018-12-12 10:10:43 -08004491 else if (IS_GEN(dev_priv, 7))
Daniel Vetter6b26c862012-04-24 14:04:12 +02004492 I915_WRITE(ARB_MODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_IVB));
Lucas De Marchicf819ef2018-12-12 10:10:43 -08004493 else if (IS_GEN(dev_priv, 8))
Ben Widawsky31a53362013-11-02 21:07:04 -07004494 I915_WRITE(GAMTARBMODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_BDW));
Ben Widawsky8782e262012-12-18 10:31:23 -08004495 else
4496 BUG();
Daniel Vetterf691e2f2012-02-02 09:58:12 +01004497}
Daniel Vettere21af882012-02-09 20:53:27 +01004498
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01004499static void init_unused_ring(struct drm_i915_private *dev_priv, u32 base)
Ville Syrjälä81e7f202014-08-15 01:21:55 +03004500{
Ville Syrjälä81e7f202014-08-15 01:21:55 +03004501 I915_WRITE(RING_CTL(base), 0);
4502 I915_WRITE(RING_HEAD(base), 0);
4503 I915_WRITE(RING_TAIL(base), 0);
4504 I915_WRITE(RING_START(base), 0);
4505}
4506
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01004507static void init_unused_rings(struct drm_i915_private *dev_priv)
Ville Syrjälä81e7f202014-08-15 01:21:55 +03004508{
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01004509 if (IS_I830(dev_priv)) {
4510 init_unused_ring(dev_priv, PRB1_BASE);
4511 init_unused_ring(dev_priv, SRB0_BASE);
4512 init_unused_ring(dev_priv, SRB1_BASE);
4513 init_unused_ring(dev_priv, SRB2_BASE);
4514 init_unused_ring(dev_priv, SRB3_BASE);
Lucas De Marchicf819ef2018-12-12 10:10:43 -08004515 } else if (IS_GEN(dev_priv, 2)) {
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01004516 init_unused_ring(dev_priv, SRB0_BASE);
4517 init_unused_ring(dev_priv, SRB1_BASE);
Lucas De Marchicf819ef2018-12-12 10:10:43 -08004518 } else if (IS_GEN(dev_priv, 3)) {
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01004519 init_unused_ring(dev_priv, PRB1_BASE);
4520 init_unused_ring(dev_priv, PRB2_BASE);
Ville Syrjälä81e7f202014-08-15 01:21:55 +03004521 }
4522}
4523
Chris Wilson20a8a742017-02-08 14:30:31 +00004524static int __i915_gem_restart_engines(void *data)
Ben Widawsky4fc7c972013-02-08 11:49:24 -08004525{
Chris Wilson20a8a742017-02-08 14:30:31 +00004526 struct drm_i915_private *i915 = data;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00004527 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05304528 enum intel_engine_id id;
Chris Wilson20a8a742017-02-08 14:30:31 +00004529 int err;
4530
4531 for_each_engine(engine, i915, id) {
4532 err = engine->init_hw(engine);
Chris Wilson8177e112018-02-07 11:15:45 +00004533 if (err) {
4534 DRM_ERROR("Failed to restart %s (%d)\n",
4535 engine->name, err);
Chris Wilson20a8a742017-02-08 14:30:31 +00004536 return err;
Chris Wilson8177e112018-02-07 11:15:45 +00004537 }
Chris Wilson20a8a742017-02-08 14:30:31 +00004538 }
4539
Chris Wilson2d5eaad2019-02-26 10:24:00 +00004540 intel_engines_set_scheduler_caps(i915);
4541
Chris Wilson20a8a742017-02-08 14:30:31 +00004542 return 0;
4543}
4544
4545int i915_gem_init_hw(struct drm_i915_private *dev_priv)
4546{
Chris Wilsond200cda2016-04-28 09:56:44 +01004547 int ret;
Ben Widawsky4fc7c972013-02-08 11:49:24 -08004548
Chris Wilsonde867c22016-10-25 13:16:02 +01004549 dev_priv->gt.last_init_time = ktime_get();
4550
Chris Wilson5e4f5182015-02-13 14:35:59 +00004551 /* Double layer security blanket, see i915_gem_init() */
4552 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
4553
Tvrtko Ursulin0031fb92016-11-04 14:42:44 +00004554 if (HAS_EDRAM(dev_priv) && INTEL_GEN(dev_priv) < 9)
Ben Widawsky05e21cc2013-07-04 11:02:04 -07004555 I915_WRITE(HSW_IDICR, I915_READ(HSW_IDICR) | IDIHASHMSK(0xf));
Ben Widawsky4fc7c972013-02-08 11:49:24 -08004556
Tvrtko Ursulin772c2a52016-10-13 11:03:01 +01004557 if (IS_HASWELL(dev_priv))
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01004558 I915_WRITE(MI_PREDICATE_RESULT_2, IS_HSW_GT3(dev_priv) ?
Ville Syrjälä0bf21342013-11-29 14:56:12 +02004559 LOWER_SLICE_ENABLED : LOWER_SLICE_DISABLED);
Rodrigo Vivi94353732013-08-28 16:45:46 -03004560
Tvrtko Ursulin094304b2018-12-03 12:50:10 +00004561 /* Apply the GT workarounds... */
Tvrtko Ursulin25d140f2018-12-03 13:33:19 +00004562 intel_gt_apply_workarounds(dev_priv);
Tvrtko Ursulin094304b2018-12-03 12:50:10 +00004563 /* ...and determine whether they are sticking. */
4564 intel_gt_verify_workarounds(dev_priv, "init");
Oscar Mateo59b449d2018-04-10 09:12:47 -07004565
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00004566 i915_gem_init_swizzling(dev_priv);
Ben Widawsky4fc7c972013-02-08 11:49:24 -08004567
Daniel Vetterd5abdfd2014-11-20 09:45:19 +01004568 /*
4569 * At least 830 can leave some of the unused rings
4570 * "active" (ie. head != tail) after resume which
4571 * will prevent c3 entry. Makes sure all unused rings
4572 * are totally idle.
4573 */
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01004574 init_unused_rings(dev_priv);
Daniel Vetterd5abdfd2014-11-20 09:45:19 +01004575
Dave Gordoned54c1a2016-01-19 19:02:54 +00004576 BUG_ON(!dev_priv->kernel_context);
Chris Wilsonc41166f2019-02-20 14:56:37 +00004577 ret = i915_terminally_wedged(dev_priv);
4578 if (ret)
Chris Wilson6f74b362017-10-15 15:37:25 +01004579 goto out;
John Harrison90638cc2015-05-29 17:43:37 +01004580
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00004581 ret = i915_ppgtt_init_hw(dev_priv);
John Harrison4ad2fd82015-06-18 13:11:20 +01004582 if (ret) {
Chris Wilson8177e112018-02-07 11:15:45 +00004583 DRM_ERROR("Enabling PPGTT failed (%d)\n", ret);
John Harrison4ad2fd82015-06-18 13:11:20 +01004584 goto out;
4585 }
4586
Jackie Lif08e2032018-03-13 17:32:53 -07004587 ret = intel_wopcm_init_hw(&dev_priv->wopcm);
4588 if (ret) {
4589 DRM_ERROR("Enabling WOPCM failed (%d)\n", ret);
4590 goto out;
4591 }
4592
Michał Winiarski9bdc3572017-10-25 18:25:19 +01004593 /* We can't enable contexts until all firmware is loaded */
4594 ret = intel_uc_init_hw(dev_priv);
Chris Wilson8177e112018-02-07 11:15:45 +00004595 if (ret) {
4596 DRM_ERROR("Enabling uc failed (%d)\n", ret);
Michał Winiarski9bdc3572017-10-25 18:25:19 +01004597 goto out;
Chris Wilson8177e112018-02-07 11:15:45 +00004598 }
Michał Winiarski9bdc3572017-10-25 18:25:19 +01004599
Tvrtko Ursulinbf9e8422016-12-01 14:16:38 +00004600 intel_mocs_init_l3cc_table(dev_priv);
Peter Antoine0ccdacf2016-04-13 15:03:25 +01004601
Chris Wilson136109c2017-11-02 13:14:30 +00004602 /* Only when the HW is re-initialised, can we replay the requests */
4603 ret = __i915_gem_restart_engines(dev_priv);
Michal Wajdeczkob96f6eb2018-06-05 12:24:43 +00004604 if (ret)
4605 goto cleanup_uc;
Michał Winiarski60c0a662018-07-12 14:48:10 +02004606
Chris Wilson5e4f5182015-02-13 14:35:59 +00004607 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Michał Winiarski60c0a662018-07-12 14:48:10 +02004608
4609 return 0;
Michal Wajdeczkob96f6eb2018-06-05 12:24:43 +00004610
4611cleanup_uc:
4612 intel_uc_fini_hw(dev_priv);
Michał Winiarski60c0a662018-07-12 14:48:10 +02004613out:
4614 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
4615
4616 return ret;
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004617}
4618
Chris Wilsond2b4b972017-11-10 14:26:33 +00004619static int __intel_engines_record_defaults(struct drm_i915_private *i915)
4620{
4621 struct i915_gem_context *ctx;
4622 struct intel_engine_cs *engine;
4623 enum intel_engine_id id;
Chris Wilson604c37d2019-03-08 09:36:55 +00004624 int err = 0;
Chris Wilsond2b4b972017-11-10 14:26:33 +00004625
4626 /*
4627 * As we reset the gpu during very early sanitisation, the current
4628 * register state on the GPU should reflect its defaults values.
4629 * We load a context onto the hw (with restore-inhibit), then switch
4630 * over to a second context to save that default register state. We
4631 * can then prime every new context with that state so they all start
4632 * from the same default HW values.
4633 */
4634
4635 ctx = i915_gem_context_create_kernel(i915, 0);
4636 if (IS_ERR(ctx))
4637 return PTR_ERR(ctx);
4638
4639 for_each_engine(engine, i915, id) {
Chris Wilsone61e0f52018-02-21 09:56:36 +00004640 struct i915_request *rq;
Chris Wilsond2b4b972017-11-10 14:26:33 +00004641
Chris Wilsone61e0f52018-02-21 09:56:36 +00004642 rq = i915_request_alloc(engine, ctx);
Chris Wilsond2b4b972017-11-10 14:26:33 +00004643 if (IS_ERR(rq)) {
4644 err = PTR_ERR(rq);
4645 goto out_ctx;
4646 }
4647
Chris Wilson3fef5cd2017-11-20 10:20:02 +00004648 err = 0;
Chris Wilsond2b4b972017-11-10 14:26:33 +00004649 if (engine->init_context)
4650 err = engine->init_context(rq);
4651
Chris Wilson697b9a82018-06-12 11:51:35 +01004652 i915_request_add(rq);
Chris Wilsond2b4b972017-11-10 14:26:33 +00004653 if (err)
4654 goto err_active;
4655 }
4656
Chris Wilson604c37d2019-03-08 09:36:55 +00004657 /* Flush the default context image to memory, and enable powersaving. */
4658 if (!load_power_context(i915)) {
4659 err = -EIO;
Chris Wilsond2b4b972017-11-10 14:26:33 +00004660 goto err_active;
Chris Wilson2621cef2018-07-09 13:20:43 +01004661 }
Chris Wilsond2b4b972017-11-10 14:26:33 +00004662
Chris Wilsond2b4b972017-11-10 14:26:33 +00004663 for_each_engine(engine, i915, id) {
Chris Wilsonc4d52fe2019-03-08 13:25:19 +00004664 struct intel_context *ce;
Chris Wilsond2b4b972017-11-10 14:26:33 +00004665 struct i915_vma *state;
Chris Wilson37d7c9c2018-09-14 13:35:03 +01004666 void *vaddr;
Chris Wilsond2b4b972017-11-10 14:26:33 +00004667
Chris Wilsonc4d52fe2019-03-08 13:25:19 +00004668 ce = intel_context_lookup(ctx, engine);
4669 if (!ce)
4670 continue;
Chris Wilson666424a2018-09-14 13:35:04 +01004671
Chris Wilsonc4d52fe2019-03-08 13:25:19 +00004672 state = ce->state;
Chris Wilsond2b4b972017-11-10 14:26:33 +00004673 if (!state)
4674 continue;
4675
Chris Wilson08819542019-03-08 13:25:22 +00004676 GEM_BUG_ON(intel_context_is_pinned(ce));
Chris Wilsonc4d52fe2019-03-08 13:25:19 +00004677
Chris Wilsond2b4b972017-11-10 14:26:33 +00004678 /*
4679 * As we will hold a reference to the logical state, it will
4680 * not be torn down with the context, and importantly the
4681 * object will hold onto its vma (making it possible for a
4682 * stray GTT write to corrupt our defaults). Unmap the vma
4683 * from the GTT to prevent such accidents and reclaim the
4684 * space.
4685 */
4686 err = i915_vma_unbind(state);
4687 if (err)
4688 goto err_active;
4689
4690 err = i915_gem_object_set_to_cpu_domain(state->obj, false);
4691 if (err)
4692 goto err_active;
4693
4694 engine->default_state = i915_gem_object_get(state->obj);
Chris Wilson37d7c9c2018-09-14 13:35:03 +01004695
4696 /* Check we can acquire the image of the context state */
4697 vaddr = i915_gem_object_pin_map(engine->default_state,
Chris Wilson666424a2018-09-14 13:35:04 +01004698 I915_MAP_FORCE_WB);
Chris Wilson37d7c9c2018-09-14 13:35:03 +01004699 if (IS_ERR(vaddr)) {
4700 err = PTR_ERR(vaddr);
4701 goto err_active;
4702 }
4703
4704 i915_gem_object_unpin_map(engine->default_state);
Chris Wilsond2b4b972017-11-10 14:26:33 +00004705 }
4706
4707 if (IS_ENABLED(CONFIG_DRM_I915_DEBUG_GEM)) {
4708 unsigned int found = intel_engines_has_context_isolation(i915);
4709
4710 /*
4711 * Make sure that classes with multiple engine instances all
4712 * share the same basic configuration.
4713 */
4714 for_each_engine(engine, i915, id) {
4715 unsigned int bit = BIT(engine->uabi_class);
4716 unsigned int expected = engine->default_state ? bit : 0;
4717
4718 if ((found & bit) != expected) {
4719 DRM_ERROR("mismatching default context state for class %d on engine %s\n",
4720 engine->uabi_class, engine->name);
4721 }
4722 }
4723 }
4724
4725out_ctx:
4726 i915_gem_context_set_closed(ctx);
4727 i915_gem_context_put(ctx);
4728 return err;
4729
4730err_active:
4731 /*
4732 * If we have to abandon now, we expect the engines to be idle
Chris Wilson604c37d2019-03-08 09:36:55 +00004733 * and ready to be torn-down. The quickest way we can accomplish
4734 * this is by declaring ourselves wedged.
Chris Wilsond2b4b972017-11-10 14:26:33 +00004735 */
Chris Wilson604c37d2019-03-08 09:36:55 +00004736 i915_gem_set_wedged(i915);
Chris Wilsond2b4b972017-11-10 14:26:33 +00004737 goto out_ctx;
4738}
4739
Chris Wilson51797492018-12-04 14:15:16 +00004740static int
4741i915_gem_init_scratch(struct drm_i915_private *i915, unsigned int size)
4742{
4743 struct drm_i915_gem_object *obj;
4744 struct i915_vma *vma;
4745 int ret;
4746
4747 obj = i915_gem_object_create_stolen(i915, size);
4748 if (!obj)
4749 obj = i915_gem_object_create_internal(i915, size);
4750 if (IS_ERR(obj)) {
4751 DRM_ERROR("Failed to allocate scratch page\n");
4752 return PTR_ERR(obj);
4753 }
4754
4755 vma = i915_vma_instance(obj, &i915->ggtt.vm, NULL);
4756 if (IS_ERR(vma)) {
4757 ret = PTR_ERR(vma);
4758 goto err_unref;
4759 }
4760
4761 ret = i915_vma_pin(vma, 0, 0, PIN_GLOBAL | PIN_HIGH);
4762 if (ret)
4763 goto err_unref;
4764
4765 i915->gt.scratch = vma;
4766 return 0;
4767
4768err_unref:
4769 i915_gem_object_put(obj);
4770 return ret;
4771}
4772
4773static void i915_gem_fini_scratch(struct drm_i915_private *i915)
4774{
4775 i915_vma_unpin_and_release(&i915->gt.scratch, 0);
4776}
4777
Tvrtko Ursulinbf9e8422016-12-01 14:16:38 +00004778int i915_gem_init(struct drm_i915_private *dev_priv)
Chris Wilson1070a422012-04-24 15:47:41 +01004779{
Chris Wilson1070a422012-04-24 15:47:41 +01004780 int ret;
4781
Changbin Du52b24162018-05-08 17:07:05 +08004782 /* We need to fallback to 4K pages if host doesn't support huge gtt. */
4783 if (intel_vgpu_active(dev_priv) && !intel_vgpu_has_huge_gtt(dev_priv))
Matthew Auldda9fe3f32017-10-06 23:18:31 +01004784 mkwrite_device_info(dev_priv)->page_sizes =
4785 I915_GTT_PAGE_SIZE_4K;
4786
Chris Wilson94312822017-05-03 10:39:18 +01004787 dev_priv->mm.unordered_timeline = dma_fence_context_alloc(1);
Chris Wilson57822dc2017-02-22 11:40:48 +00004788
Chris Wilsonfb5c5512017-11-20 20:55:00 +00004789 if (HAS_LOGICAL_RING_CONTEXTS(dev_priv)) {
Chris Wilson821ed7d2016-09-09 14:11:53 +01004790 dev_priv->gt.resume = intel_lr_context_resume;
Tvrtko Ursulin117897f2016-03-16 11:00:40 +00004791 dev_priv->gt.cleanup_engine = intel_logical_ring_cleanup;
Chris Wilsonfb5c5512017-11-20 20:55:00 +00004792 } else {
4793 dev_priv->gt.resume = intel_legacy_submission_resume;
4794 dev_priv->gt.cleanup_engine = intel_engine_cleanup;
Oscar Mateoa83014d2014-07-24 17:04:21 +01004795 }
4796
Chris Wilson1e345562019-01-28 10:23:56 +00004797 i915_timelines_init(dev_priv);
4798
Chris Wilsonee487002017-11-22 17:26:21 +00004799 ret = i915_gem_init_userptr(dev_priv);
4800 if (ret)
4801 return ret;
4802
Sagar Arun Kamble70deead2018-01-24 21:16:58 +05304803 ret = intel_uc_init_misc(dev_priv);
Michał Winiarski3176ff42017-12-13 23:13:47 +01004804 if (ret)
4805 return ret;
4806
Michal Wajdeczkof7dc0152018-06-28 14:15:21 +00004807 ret = intel_wopcm_init(&dev_priv->wopcm);
4808 if (ret)
4809 goto err_uc_misc;
4810
Chris Wilson5e4f5182015-02-13 14:35:59 +00004811 /* This is just a security blanket to placate dragons.
4812 * On some systems, we very sporadically observe that the first TLBs
4813 * used by the CS may be stale, despite us poking the TLB reset. If
4814 * we hold the forcewake during initialisation these problems
4815 * just magically go away.
4816 */
Chris Wilsonee487002017-11-22 17:26:21 +00004817 mutex_lock(&dev_priv->drm.struct_mutex);
Chris Wilson5e4f5182015-02-13 14:35:59 +00004818 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
4819
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01004820 ret = i915_gem_init_ggtt(dev_priv);
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00004821 if (ret) {
4822 GEM_BUG_ON(ret == -EIO);
4823 goto err_unlock;
4824 }
Jesse Barnesd62b4892013-03-08 10:45:53 -08004825
Chris Wilson51797492018-12-04 14:15:16 +00004826 ret = i915_gem_init_scratch(dev_priv,
Lucas De Marchicf819ef2018-12-12 10:10:43 -08004827 IS_GEN(dev_priv, 2) ? SZ_256K : PAGE_SIZE);
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00004828 if (ret) {
4829 GEM_BUG_ON(ret == -EIO);
4830 goto err_ggtt;
4831 }
Ben Widawsky2fa48d82013-12-06 14:11:04 -08004832
Chris Wilson51797492018-12-04 14:15:16 +00004833 ret = i915_gem_contexts_init(dev_priv);
4834 if (ret) {
4835 GEM_BUG_ON(ret == -EIO);
4836 goto err_scratch;
4837 }
4838
Tvrtko Ursulinbf9e8422016-12-01 14:16:38 +00004839 ret = intel_engines_init(dev_priv);
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00004840 if (ret) {
4841 GEM_BUG_ON(ret == -EIO);
4842 goto err_context;
4843 }
Daniel Vetter53ca26c2012-04-26 23:28:03 +02004844
Chris Wilsonf58d13d2017-11-10 14:26:29 +00004845 intel_init_gt_powersave(dev_priv);
4846
Michał Winiarski61b5c152017-12-13 23:13:48 +01004847 ret = intel_uc_init(dev_priv);
Chris Wilsoncc6a8182017-11-10 14:26:30 +00004848 if (ret)
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00004849 goto err_pm;
Chris Wilsoncc6a8182017-11-10 14:26:30 +00004850
Michał Winiarski61b5c152017-12-13 23:13:48 +01004851 ret = i915_gem_init_hw(dev_priv);
4852 if (ret)
4853 goto err_uc_init;
4854
Chris Wilsoncc6a8182017-11-10 14:26:30 +00004855 /*
4856 * Despite its name intel_init_clock_gating applies both display
4857 * clock gating workarounds; GT mmio workarounds and the occasional
4858 * GT power context workaround. Worse, sometimes it includes a context
4859 * register workaround which we need to apply before we record the
4860 * default HW state for all contexts.
4861 *
4862 * FIXME: break up the workarounds and apply them at the right time!
4863 */
4864 intel_init_clock_gating(dev_priv);
4865
Chris Wilsond2b4b972017-11-10 14:26:33 +00004866 ret = __intel_engines_record_defaults(dev_priv);
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00004867 if (ret)
4868 goto err_init_hw;
4869
4870 if (i915_inject_load_failure()) {
4871 ret = -ENODEV;
4872 goto err_init_hw;
4873 }
4874
4875 if (i915_inject_load_failure()) {
4876 ret = -EIO;
4877 goto err_init_hw;
4878 }
4879
4880 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
4881 mutex_unlock(&dev_priv->drm.struct_mutex);
4882
4883 return 0;
4884
4885 /*
4886 * Unwinding is complicated by that we want to handle -EIO to mean
4887 * disable GPU submission but keep KMS alive. We want to mark the
4888 * HW as irrevisibly wedged, but keep enough state around that the
4889 * driver doesn't explode during runtime.
4890 */
4891err_init_hw:
Chris Wilson8571a052018-06-06 15:54:41 +01004892 mutex_unlock(&dev_priv->drm.struct_mutex);
4893
Chris Wilson5861b012019-03-08 09:36:54 +00004894 i915_gem_suspend(dev_priv);
Chris Wilson8571a052018-06-06 15:54:41 +01004895 i915_gem_suspend_late(dev_priv);
4896
Chris Wilson8bcf9f72018-07-10 10:44:20 +01004897 i915_gem_drain_workqueue(dev_priv);
4898
Chris Wilson8571a052018-06-06 15:54:41 +01004899 mutex_lock(&dev_priv->drm.struct_mutex);
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00004900 intel_uc_fini_hw(dev_priv);
Michał Winiarski61b5c152017-12-13 23:13:48 +01004901err_uc_init:
4902 intel_uc_fini(dev_priv);
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00004903err_pm:
4904 if (ret != -EIO) {
4905 intel_cleanup_gt_powersave(dev_priv);
4906 i915_gem_cleanup_engines(dev_priv);
4907 }
4908err_context:
4909 if (ret != -EIO)
4910 i915_gem_contexts_fini(dev_priv);
Chris Wilson51797492018-12-04 14:15:16 +00004911err_scratch:
4912 i915_gem_fini_scratch(dev_priv);
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00004913err_ggtt:
4914err_unlock:
4915 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
4916 mutex_unlock(&dev_priv->drm.struct_mutex);
4917
Michal Wajdeczkof7dc0152018-06-28 14:15:21 +00004918err_uc_misc:
Sagar Arun Kamble70deead2018-01-24 21:16:58 +05304919 intel_uc_fini_misc(dev_priv);
Sagar Arun Kambleda943b52018-01-10 18:24:16 +05304920
Chris Wilson1e345562019-01-28 10:23:56 +00004921 if (ret != -EIO) {
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00004922 i915_gem_cleanup_userptr(dev_priv);
Chris Wilson1e345562019-01-28 10:23:56 +00004923 i915_timelines_fini(dev_priv);
4924 }
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00004925
Chris Wilson60990322014-04-09 09:19:42 +01004926 if (ret == -EIO) {
Chris Wilson7ed43df2018-07-26 09:50:32 +01004927 mutex_lock(&dev_priv->drm.struct_mutex);
4928
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00004929 /*
4930 * Allow engine initialisation to fail by marking the GPU as
Chris Wilson60990322014-04-09 09:19:42 +01004931 * wedged. But we only want to do this where the GPU is angry,
4932 * for all other failure, such as an allocation failure, bail.
4933 */
Chris Wilsonc41166f2019-02-20 14:56:37 +00004934 if (!i915_reset_failed(dev_priv)) {
Chris Wilson51c18bf2018-06-09 12:10:58 +01004935 i915_load_error(dev_priv,
4936 "Failed to initialize GPU, declaring it wedged!\n");
Chris Wilson6f74b362017-10-15 15:37:25 +01004937 i915_gem_set_wedged(dev_priv);
4938 }
Chris Wilson7ed43df2018-07-26 09:50:32 +01004939
4940 /* Minimal basic recovery for KMS */
4941 ret = i915_ggtt_enable_hw(dev_priv);
4942 i915_gem_restore_gtt_mappings(dev_priv);
4943 i915_gem_restore_fences(dev_priv);
4944 intel_init_clock_gating(dev_priv);
4945
4946 mutex_unlock(&dev_priv->drm.struct_mutex);
Chris Wilson1070a422012-04-24 15:47:41 +01004947 }
4948
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00004949 i915_gem_drain_freed_objects(dev_priv);
Chris Wilson60990322014-04-09 09:19:42 +01004950 return ret;
Chris Wilson1070a422012-04-24 15:47:41 +01004951}
4952
Michal Wajdeczko8979187a2018-06-04 09:00:32 +00004953void i915_gem_fini(struct drm_i915_private *dev_priv)
4954{
4955 i915_gem_suspend_late(dev_priv);
Chris Wilson30b710842018-08-12 23:36:29 +01004956 intel_disable_gt_powersave(dev_priv);
Michal Wajdeczko8979187a2018-06-04 09:00:32 +00004957
4958 /* Flush any outstanding unpin_work. */
4959 i915_gem_drain_workqueue(dev_priv);
4960
4961 mutex_lock(&dev_priv->drm.struct_mutex);
4962 intel_uc_fini_hw(dev_priv);
4963 intel_uc_fini(dev_priv);
4964 i915_gem_cleanup_engines(dev_priv);
4965 i915_gem_contexts_fini(dev_priv);
Chris Wilson51797492018-12-04 14:15:16 +00004966 i915_gem_fini_scratch(dev_priv);
Michal Wajdeczko8979187a2018-06-04 09:00:32 +00004967 mutex_unlock(&dev_priv->drm.struct_mutex);
4968
Tvrtko Ursulin25d140f2018-12-03 13:33:19 +00004969 intel_wa_list_free(&dev_priv->gt_wa_list);
4970
Chris Wilson30b710842018-08-12 23:36:29 +01004971 intel_cleanup_gt_powersave(dev_priv);
4972
Michal Wajdeczko8979187a2018-06-04 09:00:32 +00004973 intel_uc_fini_misc(dev_priv);
4974 i915_gem_cleanup_userptr(dev_priv);
Chris Wilson1e345562019-01-28 10:23:56 +00004975 i915_timelines_fini(dev_priv);
Michal Wajdeczko8979187a2018-06-04 09:00:32 +00004976
4977 i915_gem_drain_freed_objects(dev_priv);
4978
4979 WARN_ON(!list_empty(&dev_priv->contexts.list));
4980}
4981
Chris Wilson24145512017-01-24 11:01:35 +00004982void i915_gem_init_mmio(struct drm_i915_private *i915)
4983{
4984 i915_gem_sanitize(i915);
4985}
4986
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004987void
Tvrtko Ursulincb15d9f2016-12-01 14:16:39 +00004988i915_gem_cleanup_engines(struct drm_i915_private *dev_priv)
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004989{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00004990 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05304991 enum intel_engine_id id;
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004992
Akash Goel3b3f1652016-10-13 22:44:48 +05304993 for_each_engine(engine, dev_priv, id)
Tvrtko Ursulin117897f2016-03-16 11:00:40 +00004994 dev_priv->gt.cleanup_engine(engine);
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004995}
4996
Eric Anholt673a3942008-07-30 12:06:12 -07004997void
Imre Deak40ae4e12016-03-16 14:54:03 +02004998i915_gem_load_init_fences(struct drm_i915_private *dev_priv)
4999{
Chris Wilson49ef5292016-08-18 17:17:00 +01005000 int i;
Imre Deak40ae4e12016-03-16 14:54:03 +02005001
Tvrtko Ursulinc56b89f2018-02-09 21:58:46 +00005002 if (INTEL_GEN(dev_priv) >= 7 && !IS_VALLEYVIEW(dev_priv) &&
Imre Deak40ae4e12016-03-16 14:54:03 +02005003 !IS_CHERRYVIEW(dev_priv))
5004 dev_priv->num_fence_regs = 32;
Tvrtko Ursulinc56b89f2018-02-09 21:58:46 +00005005 else if (INTEL_GEN(dev_priv) >= 4 ||
Jani Nikula73f67aa2016-12-07 22:48:09 +02005006 IS_I945G(dev_priv) || IS_I945GM(dev_priv) ||
5007 IS_G33(dev_priv) || IS_PINEVIEW(dev_priv))
Imre Deak40ae4e12016-03-16 14:54:03 +02005008 dev_priv->num_fence_regs = 16;
5009 else
5010 dev_priv->num_fence_regs = 8;
5011
Chris Wilsonc0336662016-05-06 15:40:21 +01005012 if (intel_vgpu_active(dev_priv))
Imre Deak40ae4e12016-03-16 14:54:03 +02005013 dev_priv->num_fence_regs =
5014 I915_READ(vgtif_reg(avail_rs.fence_num));
5015
5016 /* Initialize fence registers to zero */
Chris Wilson49ef5292016-08-18 17:17:00 +01005017 for (i = 0; i < dev_priv->num_fence_regs; i++) {
5018 struct drm_i915_fence_reg *fence = &dev_priv->fence_regs[i];
5019
5020 fence->i915 = dev_priv;
5021 fence->id = i;
5022 list_add_tail(&fence->link, &dev_priv->mm.fence_list);
5023 }
Tvrtko Ursulin4362f4f2016-11-16 08:55:33 +00005024 i915_gem_restore_fences(dev_priv);
Imre Deak40ae4e12016-03-16 14:54:03 +02005025
Tvrtko Ursulin4362f4f2016-11-16 08:55:33 +00005026 i915_gem_detect_bit_6_swizzle(dev_priv);
Imre Deak40ae4e12016-03-16 14:54:03 +02005027}
5028
Chris Wilson9c52d1c2017-11-10 23:24:47 +00005029static void i915_gem_init__mm(struct drm_i915_private *i915)
5030{
5031 spin_lock_init(&i915->mm.object_stat_lock);
5032 spin_lock_init(&i915->mm.obj_lock);
5033 spin_lock_init(&i915->mm.free_lock);
5034
5035 init_llist_head(&i915->mm.free_list);
5036
5037 INIT_LIST_HEAD(&i915->mm.unbound_list);
5038 INIT_LIST_HEAD(&i915->mm.bound_list);
5039 INIT_LIST_HEAD(&i915->mm.fence_list);
5040 INIT_LIST_HEAD(&i915->mm.userfault_list);
5041
5042 INIT_WORK(&i915->mm.free_work, __i915_gem_free_work);
5043}
5044
Michal Wajdeczkoa0de9082018-03-23 12:34:49 +00005045int i915_gem_init_early(struct drm_i915_private *dev_priv)
Eric Anholt673a3942008-07-30 12:06:12 -07005046{
Chris Wilson13f1bfd2019-02-28 10:20:34 +00005047 int err;
Chris Wilsond1b48c12017-08-16 09:52:08 +01005048
Chris Wilson643b4502018-04-30 14:15:03 +01005049 INIT_LIST_HEAD(&dev_priv->gt.active_rings);
Chris Wilson3365e222018-05-03 20:51:14 +01005050 INIT_LIST_HEAD(&dev_priv->gt.closed_vma);
Chris Wilson643b4502018-04-30 14:15:03 +01005051
Chris Wilson9c52d1c2017-11-10 23:24:47 +00005052 i915_gem_init__mm(dev_priv);
Chris Wilsonf2123812017-10-16 12:40:37 +01005053
Chris Wilson67d97da2016-07-04 08:08:31 +01005054 INIT_DELAYED_WORK(&dev_priv->gt.retire_work,
Eric Anholt673a3942008-07-30 12:06:12 -07005055 i915_gem_retire_work_handler);
Chris Wilson67d97da2016-07-04 08:08:31 +01005056 INIT_DELAYED_WORK(&dev_priv->gt.idle_work,
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005057 i915_gem_idle_work_handler);
Chris Wilson1f15b762016-07-01 17:23:14 +01005058 init_waitqueue_head(&dev_priv->gpu_error.wait_queue);
Daniel Vetter1f83fee2012-11-15 17:17:22 +01005059 init_waitqueue_head(&dev_priv->gpu_error.reset_queue);
Chris Wilson18bb2bc2019-01-14 21:04:01 +00005060 mutex_init(&dev_priv->gpu_error.wedge_mutex);
Chris Wilson2caffbf2019-02-08 15:37:03 +00005061 init_srcu_struct(&dev_priv->gpu_error.reset_backoff_srcu);
Chris Wilson31169712009-09-14 16:50:28 +01005062
Joonas Lahtinen6f633402016-09-01 14:58:21 +03005063 atomic_set(&dev_priv->mm.bsd_engine_dispatch_index, 0);
5064
Chris Wilsonb5add952016-08-04 16:32:36 +01005065 spin_lock_init(&dev_priv->fb_tracking.lock);
Chris Wilson73cb9702016-10-28 13:58:46 +01005066
Matthew Auld465c4032017-10-06 23:18:14 +01005067 err = i915_gemfs_init(dev_priv);
5068 if (err)
5069 DRM_NOTE("Unable to create a private tmpfs mount, hugepage support will be disabled(%d).\n", err);
5070
Chris Wilson73cb9702016-10-28 13:58:46 +01005071 return 0;
Eric Anholt673a3942008-07-30 12:06:12 -07005072}
Dave Airlie71acb5e2008-12-30 20:31:46 +10005073
Michal Wajdeczkoa0de9082018-03-23 12:34:49 +00005074void i915_gem_cleanup_early(struct drm_i915_private *dev_priv)
Imre Deakd64aa092016-01-19 15:26:29 +02005075{
Chris Wilsonc4d4c1c2017-02-10 16:35:23 +00005076 i915_gem_drain_freed_objects(dev_priv);
Chris Wilsonc9c704712018-02-19 22:06:31 +00005077 GEM_BUG_ON(!llist_empty(&dev_priv->mm.free_list));
5078 GEM_BUG_ON(atomic_read(&dev_priv->mm.free_count));
Chris Wilsonc4d4c1c2017-02-10 16:35:23 +00005079 WARN_ON(dev_priv->mm.object_count);
Matthew Auldea84aa72016-11-17 21:04:11 +00005080
Chris Wilson2caffbf2019-02-08 15:37:03 +00005081 cleanup_srcu_struct(&dev_priv->gpu_error.reset_backoff_srcu);
5082
Matthew Auld465c4032017-10-06 23:18:14 +01005083 i915_gemfs_fini(dev_priv);
Imre Deakd64aa092016-01-19 15:26:29 +02005084}
5085
Chris Wilson6a800ea2016-09-21 14:51:07 +01005086int i915_gem_freeze(struct drm_i915_private *dev_priv)
5087{
Chris Wilsond0aa3012017-04-07 11:25:49 +01005088 /* Discard all purgeable objects, let userspace recover those as
5089 * required after resuming.
5090 */
Chris Wilson6a800ea2016-09-21 14:51:07 +01005091 i915_gem_shrink_all(dev_priv);
Chris Wilson6a800ea2016-09-21 14:51:07 +01005092
Chris Wilson6a800ea2016-09-21 14:51:07 +01005093 return 0;
5094}
5095
Chris Wilson95c778d2018-06-01 15:41:25 +01005096int i915_gem_freeze_late(struct drm_i915_private *i915)
Chris Wilson461fb992016-05-14 07:26:33 +01005097{
5098 struct drm_i915_gem_object *obj;
Chris Wilson7aab2d52016-09-09 20:02:18 +01005099 struct list_head *phases[] = {
Chris Wilson95c778d2018-06-01 15:41:25 +01005100 &i915->mm.unbound_list,
5101 &i915->mm.bound_list,
Chris Wilson7aab2d52016-09-09 20:02:18 +01005102 NULL
Chris Wilson95c778d2018-06-01 15:41:25 +01005103 }, **phase;
Chris Wilson461fb992016-05-14 07:26:33 +01005104
Chris Wilson95c778d2018-06-01 15:41:25 +01005105 /*
5106 * Called just before we write the hibernation image.
Chris Wilson461fb992016-05-14 07:26:33 +01005107 *
5108 * We need to update the domain tracking to reflect that the CPU
5109 * will be accessing all the pages to create and restore from the
5110 * hibernation, and so upon restoration those pages will be in the
5111 * CPU domain.
5112 *
5113 * To make sure the hibernation image contains the latest state,
5114 * we update that state just before writing out the image.
Chris Wilson7aab2d52016-09-09 20:02:18 +01005115 *
5116 * To try and reduce the hibernation image, we manually shrink
Chris Wilsond0aa3012017-04-07 11:25:49 +01005117 * the objects as well, see i915_gem_freeze()
Chris Wilson461fb992016-05-14 07:26:33 +01005118 */
5119
Chris Wilson95c778d2018-06-01 15:41:25 +01005120 i915_gem_shrink(i915, -1UL, NULL, I915_SHRINK_UNBOUND);
5121 i915_gem_drain_freed_objects(i915);
Chris Wilson461fb992016-05-14 07:26:33 +01005122
Chris Wilson95c778d2018-06-01 15:41:25 +01005123 mutex_lock(&i915->drm.struct_mutex);
5124 for (phase = phases; *phase; phase++) {
5125 list_for_each_entry(obj, *phase, mm.link)
5126 WARN_ON(i915_gem_object_set_to_cpu_domain(obj, true));
Chris Wilson461fb992016-05-14 07:26:33 +01005127 }
Chris Wilson95c778d2018-06-01 15:41:25 +01005128 mutex_unlock(&i915->drm.struct_mutex);
Chris Wilson461fb992016-05-14 07:26:33 +01005129
5130 return 0;
5131}
5132
Chris Wilsonf787a5f2010-09-24 16:02:42 +01005133void i915_gem_release(struct drm_device *dev, struct drm_file *file)
Eric Anholtb9624422009-06-03 07:27:35 +00005134{
Chris Wilsonf787a5f2010-09-24 16:02:42 +01005135 struct drm_i915_file_private *file_priv = file->driver_priv;
Chris Wilsone61e0f52018-02-21 09:56:36 +00005136 struct i915_request *request;
Eric Anholtb9624422009-06-03 07:27:35 +00005137
5138 /* Clean up our request list when the client is going away, so that
5139 * later retire_requests won't dereference our soon-to-be-gone
5140 * file_priv.
5141 */
Chris Wilson1c255952010-09-26 11:03:27 +01005142 spin_lock(&file_priv->mm.lock);
Chris Wilsonc8659ef2017-03-02 12:25:25 +00005143 list_for_each_entry(request, &file_priv->mm.request_list, client_link)
Chris Wilsonf787a5f2010-09-24 16:02:42 +01005144 request->file_priv = NULL;
Chris Wilson1c255952010-09-26 11:03:27 +01005145 spin_unlock(&file_priv->mm.lock);
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005146}
5147
Chris Wilson829a0af2017-06-20 12:05:45 +01005148int i915_gem_open(struct drm_i915_private *i915, struct drm_file *file)
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005149{
5150 struct drm_i915_file_private *file_priv;
Ben Widawskye422b882013-12-06 14:10:58 -08005151 int ret;
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005152
Chris Wilsonc4c29d72016-11-09 10:45:07 +00005153 DRM_DEBUG("\n");
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005154
5155 file_priv = kzalloc(sizeof(*file_priv), GFP_KERNEL);
5156 if (!file_priv)
5157 return -ENOMEM;
5158
5159 file->driver_priv = file_priv;
Chris Wilson829a0af2017-06-20 12:05:45 +01005160 file_priv->dev_priv = i915;
Chris Wilsonab0e7ff2014-02-25 17:11:24 +02005161 file_priv->file = file;
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005162
5163 spin_lock_init(&file_priv->mm.lock);
5164 INIT_LIST_HEAD(&file_priv->mm.request_list);
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005165
Chris Wilsonc80ff162016-07-27 09:07:27 +01005166 file_priv->bsd_engine = -1;
Mika Kuoppala14921f32018-06-15 13:44:29 +03005167 file_priv->hang_timestamp = jiffies;
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00005168
Chris Wilson829a0af2017-06-20 12:05:45 +01005169 ret = i915_gem_context_open(i915, file);
Ben Widawskye422b882013-12-06 14:10:58 -08005170 if (ret)
5171 kfree(file_priv);
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005172
Ben Widawskye422b882013-12-06 14:10:58 -08005173 return ret;
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005174}
5175
Daniel Vetterb680c372014-09-19 18:27:27 +02005176/**
5177 * i915_gem_track_fb - update frontbuffer tracking
Geliang Tangd9072a32015-09-15 05:58:44 -07005178 * @old: current GEM buffer for the frontbuffer slots
5179 * @new: new GEM buffer for the frontbuffer slots
5180 * @frontbuffer_bits: bitmask of frontbuffer slots
Daniel Vetterb680c372014-09-19 18:27:27 +02005181 *
5182 * This updates the frontbuffer tracking bits @frontbuffer_bits by clearing them
5183 * from @old and setting them in @new. Both @old and @new can be NULL.
5184 */
Daniel Vettera071fa02014-06-18 23:28:09 +02005185void i915_gem_track_fb(struct drm_i915_gem_object *old,
5186 struct drm_i915_gem_object *new,
5187 unsigned frontbuffer_bits)
5188{
Chris Wilsonfaf5bf02016-08-04 16:32:37 +01005189 /* Control of individual bits within the mask are guarded by
5190 * the owning plane->mutex, i.e. we can never see concurrent
5191 * manipulation of individual bits. But since the bitfield as a whole
5192 * is updated using RMW, we need to use atomics in order to update
5193 * the bits.
5194 */
5195 BUILD_BUG_ON(INTEL_FRONTBUFFER_BITS_PER_PIPE * I915_MAX_PIPES >
Chris Wilson74f6e182018-09-26 11:47:07 +01005196 BITS_PER_TYPE(atomic_t));
Chris Wilsonfaf5bf02016-08-04 16:32:37 +01005197
Daniel Vettera071fa02014-06-18 23:28:09 +02005198 if (old) {
Chris Wilsonfaf5bf02016-08-04 16:32:37 +01005199 WARN_ON(!(atomic_read(&old->frontbuffer_bits) & frontbuffer_bits));
5200 atomic_andnot(frontbuffer_bits, &old->frontbuffer_bits);
Daniel Vettera071fa02014-06-18 23:28:09 +02005201 }
5202
5203 if (new) {
Chris Wilsonfaf5bf02016-08-04 16:32:37 +01005204 WARN_ON(atomic_read(&new->frontbuffer_bits) & frontbuffer_bits);
5205 atomic_or(frontbuffer_bits, &new->frontbuffer_bits);
Daniel Vettera071fa02014-06-18 23:28:09 +02005206 }
5207}
5208
Dave Gordonea702992015-07-09 19:29:02 +01005209/* Allocate a new GEM object and fill it with the supplied data */
5210struct drm_i915_gem_object *
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +00005211i915_gem_object_create_from_data(struct drm_i915_private *dev_priv,
Dave Gordonea702992015-07-09 19:29:02 +01005212 const void *data, size_t size)
5213{
5214 struct drm_i915_gem_object *obj;
Chris Wilsonbe062fa2017-03-17 19:46:48 +00005215 struct file *file;
5216 size_t offset;
5217 int err;
Dave Gordonea702992015-07-09 19:29:02 +01005218
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +00005219 obj = i915_gem_object_create(dev_priv, round_up(size, PAGE_SIZE));
Chris Wilsonfe3db792016-04-25 13:32:13 +01005220 if (IS_ERR(obj))
Dave Gordonea702992015-07-09 19:29:02 +01005221 return obj;
5222
Christian Königc0a51fd2018-02-16 13:43:38 +01005223 GEM_BUG_ON(obj->write_domain != I915_GEM_DOMAIN_CPU);
Dave Gordonea702992015-07-09 19:29:02 +01005224
Chris Wilsonbe062fa2017-03-17 19:46:48 +00005225 file = obj->base.filp;
5226 offset = 0;
5227 do {
5228 unsigned int len = min_t(typeof(size), size, PAGE_SIZE);
5229 struct page *page;
5230 void *pgdata, *vaddr;
Dave Gordonea702992015-07-09 19:29:02 +01005231
Chris Wilsonbe062fa2017-03-17 19:46:48 +00005232 err = pagecache_write_begin(file, file->f_mapping,
5233 offset, len, 0,
5234 &page, &pgdata);
5235 if (err < 0)
5236 goto fail;
Dave Gordonea702992015-07-09 19:29:02 +01005237
Chris Wilsonbe062fa2017-03-17 19:46:48 +00005238 vaddr = kmap(page);
5239 memcpy(vaddr, data, len);
5240 kunmap(page);
5241
5242 err = pagecache_write_end(file, file->f_mapping,
5243 offset, len, len,
5244 page, pgdata);
5245 if (err < 0)
5246 goto fail;
5247
5248 size -= len;
5249 data += len;
5250 offset += len;
5251 } while (size);
Dave Gordonea702992015-07-09 19:29:02 +01005252
5253 return obj;
5254
5255fail:
Chris Wilsonf8c417c2016-07-20 13:31:53 +01005256 i915_gem_object_put(obj);
Chris Wilsonbe062fa2017-03-17 19:46:48 +00005257 return ERR_PTR(err);
Dave Gordonea702992015-07-09 19:29:02 +01005258}
Chris Wilson96d77632016-10-28 13:58:33 +01005259
5260struct scatterlist *
5261i915_gem_object_get_sg(struct drm_i915_gem_object *obj,
5262 unsigned int n,
5263 unsigned int *offset)
5264{
Chris Wilsona4f5ea62016-10-28 13:58:35 +01005265 struct i915_gem_object_page_iter *iter = &obj->mm.get_page;
Chris Wilson96d77632016-10-28 13:58:33 +01005266 struct scatterlist *sg;
5267 unsigned int idx, count;
5268
5269 might_sleep();
5270 GEM_BUG_ON(n >= obj->base.size >> PAGE_SHIFT);
Chris Wilsona4f5ea62016-10-28 13:58:35 +01005271 GEM_BUG_ON(!i915_gem_object_has_pinned_pages(obj));
Chris Wilson96d77632016-10-28 13:58:33 +01005272
5273 /* As we iterate forward through the sg, we record each entry in a
5274 * radixtree for quick repeated (backwards) lookups. If we have seen
5275 * this index previously, we will have an entry for it.
5276 *
5277 * Initial lookup is O(N), but this is amortized to O(1) for
5278 * sequential page access (where each new request is consecutive
5279 * to the previous one). Repeated lookups are O(lg(obj->base.size)),
5280 * i.e. O(1) with a large constant!
5281 */
5282 if (n < READ_ONCE(iter->sg_idx))
5283 goto lookup;
5284
5285 mutex_lock(&iter->lock);
5286
5287 /* We prefer to reuse the last sg so that repeated lookup of this
5288 * (or the subsequent) sg are fast - comparing against the last
5289 * sg is faster than going through the radixtree.
5290 */
5291
5292 sg = iter->sg_pos;
5293 idx = iter->sg_idx;
5294 count = __sg_page_count(sg);
5295
5296 while (idx + count <= n) {
Matthew Wilcox3159f942017-11-03 13:30:42 -04005297 void *entry;
5298 unsigned long i;
Chris Wilson96d77632016-10-28 13:58:33 +01005299 int ret;
5300
5301 /* If we cannot allocate and insert this entry, or the
5302 * individual pages from this range, cancel updating the
5303 * sg_idx so that on this lookup we are forced to linearly
5304 * scan onwards, but on future lookups we will try the
5305 * insertion again (in which case we need to be careful of
5306 * the error return reporting that we have already inserted
5307 * this index).
5308 */
5309 ret = radix_tree_insert(&iter->radix, idx, sg);
5310 if (ret && ret != -EEXIST)
5311 goto scan;
5312
Matthew Wilcox3159f942017-11-03 13:30:42 -04005313 entry = xa_mk_value(idx);
Chris Wilson96d77632016-10-28 13:58:33 +01005314 for (i = 1; i < count; i++) {
Matthew Wilcox3159f942017-11-03 13:30:42 -04005315 ret = radix_tree_insert(&iter->radix, idx + i, entry);
Chris Wilson96d77632016-10-28 13:58:33 +01005316 if (ret && ret != -EEXIST)
5317 goto scan;
5318 }
5319
5320 idx += count;
5321 sg = ____sg_next(sg);
5322 count = __sg_page_count(sg);
5323 }
5324
5325scan:
5326 iter->sg_pos = sg;
5327 iter->sg_idx = idx;
5328
5329 mutex_unlock(&iter->lock);
5330
5331 if (unlikely(n < idx)) /* insertion completed by another thread */
5332 goto lookup;
5333
5334 /* In case we failed to insert the entry into the radixtree, we need
5335 * to look beyond the current sg.
5336 */
5337 while (idx + count <= n) {
5338 idx += count;
5339 sg = ____sg_next(sg);
5340 count = __sg_page_count(sg);
5341 }
5342
5343 *offset = n - idx;
5344 return sg;
5345
5346lookup:
5347 rcu_read_lock();
5348
5349 sg = radix_tree_lookup(&iter->radix, n);
5350 GEM_BUG_ON(!sg);
5351
5352 /* If this index is in the middle of multi-page sg entry,
Matthew Wilcox3159f942017-11-03 13:30:42 -04005353 * the radix tree will contain a value entry that points
Chris Wilson96d77632016-10-28 13:58:33 +01005354 * to the start of that range. We will return the pointer to
5355 * the base page and the offset of this page within the
5356 * sg entry's range.
5357 */
5358 *offset = 0;
Matthew Wilcox3159f942017-11-03 13:30:42 -04005359 if (unlikely(xa_is_value(sg))) {
5360 unsigned long base = xa_to_value(sg);
Chris Wilson96d77632016-10-28 13:58:33 +01005361
5362 sg = radix_tree_lookup(&iter->radix, base);
5363 GEM_BUG_ON(!sg);
5364
5365 *offset = n - base;
5366 }
5367
5368 rcu_read_unlock();
5369
5370 return sg;
5371}
5372
5373struct page *
5374i915_gem_object_get_page(struct drm_i915_gem_object *obj, unsigned int n)
5375{
5376 struct scatterlist *sg;
5377 unsigned int offset;
5378
5379 GEM_BUG_ON(!i915_gem_object_has_struct_page(obj));
5380
5381 sg = i915_gem_object_get_sg(obj, n, &offset);
5382 return nth_page(sg_page(sg), offset);
5383}
5384
5385/* Like i915_gem_object_get_page(), but mark the returned page dirty */
5386struct page *
5387i915_gem_object_get_dirty_page(struct drm_i915_gem_object *obj,
5388 unsigned int n)
5389{
5390 struct page *page;
5391
5392 page = i915_gem_object_get_page(obj, n);
Chris Wilsona4f5ea62016-10-28 13:58:35 +01005393 if (!obj->mm.dirty)
Chris Wilson96d77632016-10-28 13:58:33 +01005394 set_page_dirty(page);
5395
5396 return page;
5397}
5398
5399dma_addr_t
5400i915_gem_object_get_dma_address(struct drm_i915_gem_object *obj,
5401 unsigned long n)
5402{
5403 struct scatterlist *sg;
5404 unsigned int offset;
5405
5406 sg = i915_gem_object_get_sg(obj, n, &offset);
5407 return sg_dma_address(sg) + (offset << PAGE_SHIFT);
5408}
Chris Wilson935a2f72017-02-13 17:15:13 +00005409
Chris Wilson8eeb7902017-07-26 19:16:01 +01005410int i915_gem_object_attach_phys(struct drm_i915_gem_object *obj, int align)
5411{
5412 struct sg_table *pages;
5413 int err;
5414
5415 if (align > obj->base.size)
5416 return -EINVAL;
5417
5418 if (obj->ops == &i915_gem_phys_ops)
5419 return 0;
5420
5421 if (obj->ops != &i915_gem_object_ops)
5422 return -EINVAL;
5423
5424 err = i915_gem_object_unbind(obj);
5425 if (err)
5426 return err;
5427
5428 mutex_lock(&obj->mm.lock);
5429
5430 if (obj->mm.madv != I915_MADV_WILLNEED) {
5431 err = -EFAULT;
5432 goto err_unlock;
5433 }
5434
5435 if (obj->mm.quirked) {
5436 err = -EFAULT;
5437 goto err_unlock;
5438 }
5439
5440 if (obj->mm.mapping) {
5441 err = -EBUSY;
5442 goto err_unlock;
5443 }
5444
Chris Wilsonacd1c1e2018-06-11 08:55:32 +01005445 pages = __i915_gem_object_unset_pages(obj);
Chris Wilsonf2123812017-10-16 12:40:37 +01005446
Chris Wilson8eeb7902017-07-26 19:16:01 +01005447 obj->ops = &i915_gem_phys_ops;
5448
Chris Wilson8fb6a5d2017-07-26 19:16:02 +01005449 err = ____i915_gem_object_get_pages(obj);
Chris Wilson8eeb7902017-07-26 19:16:01 +01005450 if (err)
5451 goto err_xfer;
5452
5453 /* Perma-pin (until release) the physical set of pages */
5454 __i915_gem_object_pin_pages(obj);
5455
5456 if (!IS_ERR_OR_NULL(pages))
5457 i915_gem_object_ops.put_pages(obj, pages);
5458 mutex_unlock(&obj->mm.lock);
5459 return 0;
5460
5461err_xfer:
5462 obj->ops = &i915_gem_object_ops;
Chris Wilsonacd1c1e2018-06-11 08:55:32 +01005463 if (!IS_ERR_OR_NULL(pages)) {
5464 unsigned int sg_page_sizes = i915_sg_page_sizes(pages->sgl);
5465
5466 __i915_gem_object_set_pages(obj, pages, sg_page_sizes);
5467 }
Chris Wilson8eeb7902017-07-26 19:16:01 +01005468err_unlock:
5469 mutex_unlock(&obj->mm.lock);
5470 return err;
5471}
5472
Chris Wilson935a2f72017-02-13 17:15:13 +00005473#if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
5474#include "selftests/scatterlist.c"
Chris Wilson66d9cb52017-02-13 17:15:17 +00005475#include "selftests/mock_gem_device.c"
Chris Wilson44653982017-02-13 17:15:20 +00005476#include "selftests/huge_gem_object.c"
Matthew Auld40498662017-10-06 23:18:29 +01005477#include "selftests/huge_pages.c"
Chris Wilson8335fd62017-02-13 17:15:28 +00005478#include "selftests/i915_gem_object.c"
Chris Wilson17059452017-02-13 17:15:32 +00005479#include "selftests/i915_gem_coherency.c"
Chris Wilson3f51b7e12018-08-30 14:48:06 +01005480#include "selftests/i915_gem.c"
Chris Wilson935a2f72017-02-13 17:15:13 +00005481#endif