blob: eb31b69a316a933f34e7d2ec5d0c43d890de59bb [file] [log] [blame]
Eric Anholt673a3942008-07-30 12:06:12 -07001/*
Daniel Vetterbe6a0372015-03-18 10:46:04 +01002 * Copyright © 2008-2015 Intel Corporation
Eric Anholt673a3942008-07-30 12:06:12 -07003 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 *
23 * Authors:
24 * Eric Anholt <eric@anholt.net>
25 *
26 */
27
David Herrmann0de23972013-07-24 21:07:52 +020028#include <drm/drm_vma_manager.h>
David Howells760285e2012-10-02 18:01:07 +010029#include <drm/i915_drm.h>
Chris Wilson6b5e90f2016-11-14 20:41:05 +000030#include <linux/dma-fence-array.h>
Chris Wilsonfe3288b2017-02-12 17:20:01 +000031#include <linux/kthread.h>
Christian König52791ee2019-08-11 10:06:32 +020032#include <linux/dma-resv.h>
Hugh Dickins5949eac2011-06-27 16:18:18 -070033#include <linux/shmem_fs.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090034#include <linux/slab.h>
Chris Wilson20e49332016-11-22 14:41:21 +000035#include <linux/stop_machine.h>
Eric Anholt673a3942008-07-30 12:06:12 -070036#include <linux/swap.h>
Jesse Barnes79e53942008-11-07 14:24:08 -080037#include <linux/pci.h>
Daniel Vetter1286ff72012-05-10 15:25:09 +020038#include <linux/dma-buf.h>
Daniel Vetterfcd70cd2019-01-17 22:03:34 +010039#include <linux/mman.h>
Eric Anholt673a3942008-07-30 12:06:12 -070040
Jani Nikuladf0566a2019-06-13 11:44:16 +030041#include "display/intel_display.h"
42#include "display/intel_frontbuffer.h"
43
Chris Wilson10be98a2019-05-28 10:29:49 +010044#include "gem/i915_gem_clflush.h"
45#include "gem/i915_gem_context.h"
Chris Wilsonafa13082019-05-28 10:29:43 +010046#include "gem/i915_gem_ioctls.h"
Chris Wilson10be98a2019-05-28 10:29:49 +010047#include "gem/i915_gem_pm.h"
48#include "gem/i915_gemfs.h"
Chris Wilson750e76b2019-08-06 13:43:00 +010049#include "gt/intel_engine_user.h"
Tvrtko Ursulinbaea4292019-06-21 08:08:02 +010050#include "gt/intel_gt.h"
Chris Wilson79ffac852019-04-24 21:07:17 +010051#include "gt/intel_gt_pm.h"
Chris Wilson112ed2d2019-04-24 18:48:39 +010052#include "gt/intel_mocs.h"
53#include "gt/intel_reset.h"
Chris Wilsona5627722019-07-29 12:37:20 +010054#include "gt/intel_renderstate.h"
Chris Wilson112ed2d2019-04-24 18:48:39 +010055#include "gt/intel_workarounds.h"
56
Chris Wilson9f588922019-01-16 15:33:04 +000057#include "i915_drv.h"
Chris Wilson37d63f82019-05-28 10:29:50 +010058#include "i915_scatterlist.h"
Chris Wilson9f588922019-01-16 15:33:04 +000059#include "i915_trace.h"
60#include "i915_vgpu.h"
61
Jani Nikula696173b2019-04-05 14:00:15 +030062#include "intel_pm.h"
Chris Wilson9f588922019-01-16 15:33:04 +000063
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +053064static int
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +010065insert_mappable_node(struct i915_ggtt *ggtt,
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +053066 struct drm_mm_node *node, u32 size)
67{
68 memset(node, 0, sizeof(*node));
Chris Wilson82ad6442018-06-05 16:37:58 +010069 return drm_mm_insert_node_in_range(&ggtt->vm.mm, node,
Chris Wilson4e64e552017-02-02 21:04:38 +000070 size, 0, I915_COLOR_UNEVICTABLE,
71 0, ggtt->mappable_end,
72 DRM_MM_INSERT_LOW);
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +053073}
74
75static void
76remove_mappable_node(struct drm_mm_node *node)
77{
78 drm_mm_remove_node(node);
79}
80
Eric Anholt673a3942008-07-30 12:06:12 -070081int
Eric Anholt5a125c32008-10-22 21:40:13 -070082i915_gem_get_aperture_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +000083 struct drm_file *file)
Eric Anholt5a125c32008-10-22 21:40:13 -070084{
Chris Wilson09d7e462019-01-28 10:23:53 +000085 struct i915_ggtt *ggtt = &to_i915(dev)->ggtt;
Joonas Lahtinen72e96d62016-03-30 16:57:10 +030086 struct drm_i915_gem_get_aperture *args = data;
Tvrtko Ursulinca1543b2015-07-01 11:51:10 +010087 struct i915_vma *vma;
Weinan Liff8f7972017-05-31 10:35:52 +080088 u64 pinned;
Eric Anholt5a125c32008-10-22 21:40:13 -070089
Chris Wilson09d7e462019-01-28 10:23:53 +000090 mutex_lock(&ggtt->vm.mutex);
91
Chris Wilson82ad6442018-06-05 16:37:58 +010092 pinned = ggtt->vm.reserved;
Chris Wilson499197d2019-01-28 10:23:52 +000093 list_for_each_entry(vma, &ggtt->vm.bound_list, vm_link)
Chris Wilson20dfbde2016-08-04 16:32:30 +010094 if (i915_vma_is_pinned(vma))
Tvrtko Ursulinca1543b2015-07-01 11:51:10 +010095 pinned += vma->node.size;
Chris Wilson09d7e462019-01-28 10:23:53 +000096
97 mutex_unlock(&ggtt->vm.mutex);
Eric Anholt5a125c32008-10-22 21:40:13 -070098
Chris Wilson82ad6442018-06-05 16:37:58 +010099 args->aper_size = ggtt->vm.total;
Akshay Joshi0206e352011-08-16 15:34:10 -0400100 args->aper_available_size = args->aper_size - pinned;
Chris Wilson6299f992010-11-24 12:23:44 +0000101
Eric Anholt5a125c32008-10-22 21:40:13 -0700102 return 0;
103}
104
Chris Wilsonc03467b2019-07-03 10:17:17 +0100105int i915_gem_object_unbind(struct drm_i915_gem_object *obj,
106 unsigned long flags)
Chris Wilsonaa653a62016-08-04 07:52:27 +0100107{
108 struct i915_vma *vma;
109 LIST_HEAD(still_in_list);
Chris Wilson6951e582019-05-28 10:29:51 +0100110 int ret = 0;
Chris Wilsonaa653a62016-08-04 07:52:27 +0100111
Chris Wilson02bef8f2016-08-14 18:44:41 +0100112 lockdep_assert_held(&obj->base.dev->struct_mutex);
113
Chris Wilson528cbd12019-01-28 10:23:54 +0000114 spin_lock(&obj->vma.lock);
115 while (!ret && (vma = list_first_entry_or_null(&obj->vma.list,
116 struct i915_vma,
117 obj_link))) {
Chris Wilsonaa653a62016-08-04 07:52:27 +0100118 list_move_tail(&vma->obj_link, &still_in_list);
Chris Wilson528cbd12019-01-28 10:23:54 +0000119 spin_unlock(&obj->vma.lock);
120
Chris Wilsonc03467b2019-07-03 10:17:17 +0100121 ret = -EBUSY;
122 if (flags & I915_GEM_OBJECT_UNBIND_ACTIVE ||
123 !i915_vma_is_active(vma))
124 ret = i915_vma_unbind(vma);
Chris Wilson528cbd12019-01-28 10:23:54 +0000125
126 spin_lock(&obj->vma.lock);
Chris Wilsonaa653a62016-08-04 07:52:27 +0100127 }
Chris Wilson528cbd12019-01-28 10:23:54 +0000128 list_splice(&still_in_list, &obj->vma.list);
129 spin_unlock(&obj->vma.lock);
Chris Wilsonaa653a62016-08-04 07:52:27 +0100130
131 return ret;
132}
133
Chris Wilson00731152014-05-21 12:42:56 +0100134static int
135i915_gem_phys_pwrite(struct drm_i915_gem_object *obj,
136 struct drm_i915_gem_pwrite *args,
Chris Wilson03ac84f2016-10-28 13:58:36 +0100137 struct drm_file *file)
Chris Wilson00731152014-05-21 12:42:56 +0100138{
Chris Wilson00731152014-05-21 12:42:56 +0100139 void *vaddr = obj->phys_handle->vaddr + args->offset;
Gustavo Padovan3ed605b2016-04-26 12:32:27 -0300140 char __user *user_data = u64_to_user_ptr(args->data_ptr);
Chris Wilson6a2c4232014-11-04 04:51:40 -0800141
Chris Wilson8e7cb172019-08-16 08:46:35 +0100142 /*
143 * We manually control the domain here and pretend that it
Chris Wilson6a2c4232014-11-04 04:51:40 -0800144 * remains coherent i.e. in the GTT domain, like shmem_pwrite.
145 */
Chris Wilson8e7cb172019-08-16 08:46:35 +0100146 intel_frontbuffer_invalidate(obj->frontbuffer, ORIGIN_CPU);
147
Chris Wilson10466d22017-01-06 15:22:38 +0000148 if (copy_from_user(vaddr, user_data, args->size))
149 return -EFAULT;
Chris Wilson00731152014-05-21 12:42:56 +0100150
Chris Wilson6a2c4232014-11-04 04:51:40 -0800151 drm_clflush_virt_range(vaddr, args->size);
Tvrtko Ursulinbaea4292019-06-21 08:08:02 +0100152 intel_gt_chipset_flush(&to_i915(obj->base.dev)->gt);
Paulo Zanoni063e4e62015-02-13 17:23:45 -0200153
Chris Wilson8e7cb172019-08-16 08:46:35 +0100154 intel_frontbuffer_flush(obj->frontbuffer, ORIGIN_CPU);
Chris Wilson10466d22017-01-06 15:22:38 +0000155 return 0;
Chris Wilson00731152014-05-21 12:42:56 +0100156}
157
Dave Airlieff72145b2011-02-07 12:16:14 +1000158static int
159i915_gem_create(struct drm_file *file,
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +0000160 struct drm_i915_private *dev_priv,
Michał Winiarskie1634842019-03-26 18:02:18 +0100161 u64 *size_p,
Jani Nikula739f3ab2019-01-16 11:15:19 +0200162 u32 *handle_p)
Eric Anholt673a3942008-07-30 12:06:12 -0700163{
Chris Wilson05394f32010-11-08 19:18:58 +0000164 struct drm_i915_gem_object *obj;
Pekka Paalanena1a2d1d2009-08-23 12:40:55 +0300165 u32 handle;
Michał Winiarskie1634842019-03-26 18:02:18 +0100166 u64 size;
167 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700168
Michał Winiarskie1634842019-03-26 18:02:18 +0100169 size = round_up(*size_p, PAGE_SIZE);
Chris Wilson8ffc0242011-09-14 14:14:28 +0200170 if (size == 0)
171 return -EINVAL;
Eric Anholt673a3942008-07-30 12:06:12 -0700172
173 /* Allocate the new object */
Chris Wilson84753552019-05-28 10:29:45 +0100174 obj = i915_gem_object_create_shmem(dev_priv, size);
Chris Wilsonfe3db792016-04-25 13:32:13 +0100175 if (IS_ERR(obj))
176 return PTR_ERR(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700177
Chris Wilson05394f32010-11-08 19:18:58 +0000178 ret = drm_gem_handle_create(file, &obj->base, &handle);
Chris Wilson202f2fe2010-10-14 13:20:40 +0100179 /* drop reference from allocate - handle holds it now */
Chris Wilsonf0cd5182016-10-28 13:58:43 +0100180 i915_gem_object_put(obj);
Daniel Vetterd861e332013-07-24 23:25:03 +0200181 if (ret)
182 return ret;
Chris Wilson202f2fe2010-10-14 13:20:40 +0100183
Dave Airlieff72145b2011-02-07 12:16:14 +1000184 *handle_p = handle;
Chris Wilson99534022019-04-17 14:25:07 +0100185 *size_p = size;
Eric Anholt673a3942008-07-30 12:06:12 -0700186 return 0;
187}
188
Dave Airlieff72145b2011-02-07 12:16:14 +1000189int
190i915_gem_dumb_create(struct drm_file *file,
191 struct drm_device *dev,
192 struct drm_mode_create_dumb *args)
193{
Ville Syrjäläaa5ca8b2019-05-09 15:21:57 +0300194 int cpp = DIV_ROUND_UP(args->bpp, 8);
195 u32 format;
196
197 switch (cpp) {
198 case 1:
199 format = DRM_FORMAT_C8;
200 break;
201 case 2:
202 format = DRM_FORMAT_RGB565;
203 break;
204 case 4:
205 format = DRM_FORMAT_XRGB8888;
206 break;
207 default:
208 return -EINVAL;
209 }
210
Dave Airlieff72145b2011-02-07 12:16:14 +1000211 /* have to work out size/pitch and return them */
Ville Syrjäläaa5ca8b2019-05-09 15:21:57 +0300212 args->pitch = ALIGN(args->width * cpp, 64);
213
214 /* align stride to page size so that we can remap */
215 if (args->pitch > intel_plane_fb_max_stride(to_i915(dev), format,
216 DRM_FORMAT_MOD_LINEAR))
217 args->pitch = ALIGN(args->pitch, 4096);
218
Dave Airlieff72145b2011-02-07 12:16:14 +1000219 args->size = args->pitch * args->height;
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +0000220 return i915_gem_create(file, to_i915(dev),
Michał Winiarskie1634842019-03-26 18:02:18 +0100221 &args->size, &args->handle);
Dave Airlieff72145b2011-02-07 12:16:14 +1000222}
223
Dave Airlieff72145b2011-02-07 12:16:14 +1000224/**
225 * Creates a new mm object and returns a handle to it.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +0100226 * @dev: drm device pointer
227 * @data: ioctl data blob
228 * @file: drm file pointer
Dave Airlieff72145b2011-02-07 12:16:14 +1000229 */
230int
231i915_gem_create_ioctl(struct drm_device *dev, void *data,
232 struct drm_file *file)
233{
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +0000234 struct drm_i915_private *dev_priv = to_i915(dev);
Dave Airlieff72145b2011-02-07 12:16:14 +1000235 struct drm_i915_gem_create *args = data;
Daniel Vetter63ed2cb2012-04-23 16:50:50 +0200236
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +0000237 i915_gem_flush_free_objects(dev_priv);
Chris Wilsonfbbd37b2016-10-28 13:58:42 +0100238
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +0000239 return i915_gem_create(file, dev_priv,
Michał Winiarskie1634842019-03-26 18:02:18 +0100240 &args->size, &args->handle);
Dave Airlieff72145b2011-02-07 12:16:14 +1000241}
242
Daniel Vetterd174bd62012-03-25 19:47:40 +0200243static int
Chris Wilsonb9d126e2019-01-05 12:07:58 +0000244shmem_pread(struct page *page, int offset, int len, char __user *user_data,
245 bool needs_clflush)
Daniel Vetterd174bd62012-03-25 19:47:40 +0200246{
247 char *vaddr;
248 int ret;
249
250 vaddr = kmap(page);
Daniel Vetterd174bd62012-03-25 19:47:40 +0200251
Chris Wilsonb9d126e2019-01-05 12:07:58 +0000252 if (needs_clflush)
253 drm_clflush_virt_range(vaddr + offset, len);
254
255 ret = __copy_to_user(user_data, vaddr + offset, len);
256
Daniel Vetterd174bd62012-03-25 19:47:40 +0200257 kunmap(page);
258
Chris Wilsonb9d126e2019-01-05 12:07:58 +0000259 return ret ? -EFAULT : 0;
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100260}
261
262static int
263i915_gem_shmem_pread(struct drm_i915_gem_object *obj,
264 struct drm_i915_gem_pread *args)
265{
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100266 unsigned int needs_clflush;
267 unsigned int idx, offset;
Chris Wilson6951e582019-05-28 10:29:51 +0100268 struct dma_fence *fence;
269 char __user *user_data;
270 u64 remain;
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100271 int ret;
272
Chris Wilson6951e582019-05-28 10:29:51 +0100273 ret = i915_gem_object_prepare_read(obj, &needs_clflush);
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100274 if (ret)
275 return ret;
276
Chris Wilson6951e582019-05-28 10:29:51 +0100277 fence = i915_gem_object_lock_fence(obj);
278 i915_gem_object_finish_access(obj);
279 if (!fence)
280 return -ENOMEM;
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100281
282 remain = args->size;
283 user_data = u64_to_user_ptr(args->data_ptr);
284 offset = offset_in_page(args->offset);
285 for (idx = args->offset >> PAGE_SHIFT; remain; idx++) {
286 struct page *page = i915_gem_object_get_page(obj, idx);
Chris Wilsona5e856a52018-10-12 15:02:28 +0100287 unsigned int length = min_t(u64, remain, PAGE_SIZE - offset);
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100288
289 ret = shmem_pread(page, offset, length, user_data,
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100290 needs_clflush);
291 if (ret)
292 break;
293
294 remain -= length;
295 user_data += length;
296 offset = 0;
297 }
298
Chris Wilson6951e582019-05-28 10:29:51 +0100299 i915_gem_object_unlock_fence(obj, fence);
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100300 return ret;
301}
302
303static inline bool
304gtt_user_read(struct io_mapping *mapping,
305 loff_t base, int offset,
306 char __user *user_data, int length)
307{
Ville Syrjäläafe722b2017-09-01 20:12:52 +0300308 void __iomem *vaddr;
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100309 unsigned long unwritten;
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +0530310
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +0530311 /* We can use the cpu mem copy function because this is X86. */
Ville Syrjäläafe722b2017-09-01 20:12:52 +0300312 vaddr = io_mapping_map_atomic_wc(mapping, base);
313 unwritten = __copy_to_user_inatomic(user_data,
314 (void __force *)vaddr + offset,
315 length);
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100316 io_mapping_unmap_atomic(vaddr);
317 if (unwritten) {
Ville Syrjäläafe722b2017-09-01 20:12:52 +0300318 vaddr = io_mapping_map_wc(mapping, base, PAGE_SIZE);
319 unwritten = copy_to_user(user_data,
320 (void __force *)vaddr + offset,
321 length);
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100322 io_mapping_unmap(vaddr);
323 }
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +0530324 return unwritten;
325}
326
327static int
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100328i915_gem_gtt_pread(struct drm_i915_gem_object *obj,
329 const struct drm_i915_gem_pread *args)
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +0530330{
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100331 struct drm_i915_private *i915 = to_i915(obj->base.dev);
332 struct i915_ggtt *ggtt = &i915->ggtt;
Chris Wilson538ef962019-01-14 14:21:18 +0000333 intel_wakeref_t wakeref;
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +0530334 struct drm_mm_node node;
Chris Wilson6951e582019-05-28 10:29:51 +0100335 struct dma_fence *fence;
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100336 void __user *user_data;
Chris Wilson6951e582019-05-28 10:29:51 +0100337 struct i915_vma *vma;
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100338 u64 remain, offset;
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +0530339 int ret;
340
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100341 ret = mutex_lock_interruptible(&i915->drm.struct_mutex);
342 if (ret)
343 return ret;
344
Daniele Ceraolo Spuriod858d562019-06-13 16:21:54 -0700345 wakeref = intel_runtime_pm_get(&i915->runtime_pm);
Chris Wilson1f7fd482019-08-22 07:15:57 +0100346 vma = ERR_PTR(-ENODEV);
347 if (!i915_gem_object_is_tiled(obj))
348 vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0,
349 PIN_MAPPABLE |
350 PIN_NONBLOCK /* NOWARN */ |
351 PIN_NOEVICT);
Chris Wilson18034582016-08-18 17:16:45 +0100352 if (!IS_ERR(vma)) {
353 node.start = i915_ggtt_offset(vma);
354 node.allocated = false;
Chris Wilson1f7fd482019-08-22 07:15:57 +0100355 } else {
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100356 ret = insert_mappable_node(ggtt, &node, PAGE_SIZE);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +0530357 if (ret)
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100358 goto out_unlock;
359 GEM_BUG_ON(!node.allocated);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +0530360 }
361
Chris Wilson6951e582019-05-28 10:29:51 +0100362 mutex_unlock(&i915->drm.struct_mutex);
363
364 ret = i915_gem_object_lock_interruptible(obj);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +0530365 if (ret)
366 goto out_unpin;
367
Chris Wilson6951e582019-05-28 10:29:51 +0100368 ret = i915_gem_object_set_to_gtt_domain(obj, false);
369 if (ret) {
370 i915_gem_object_unlock(obj);
371 goto out_unpin;
372 }
373
374 fence = i915_gem_object_lock_fence(obj);
375 i915_gem_object_unlock(obj);
376 if (!fence) {
377 ret = -ENOMEM;
378 goto out_unpin;
379 }
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +0530380
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100381 user_data = u64_to_user_ptr(args->data_ptr);
382 remain = args->size;
383 offset = args->offset;
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +0530384
385 while (remain > 0) {
386 /* Operation in this page
387 *
388 * page_base = page offset within aperture
389 * page_offset = offset within page
390 * page_length = bytes to copy for this page
391 */
392 u32 page_base = node.start;
393 unsigned page_offset = offset_in_page(offset);
394 unsigned page_length = PAGE_SIZE - page_offset;
395 page_length = remain < page_length ? remain : page_length;
396 if (node.allocated) {
Chris Wilson82ad6442018-06-05 16:37:58 +0100397 ggtt->vm.insert_page(&ggtt->vm,
398 i915_gem_object_get_dma_address(obj, offset >> PAGE_SHIFT),
399 node.start, I915_CACHE_NONE, 0);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +0530400 } else {
401 page_base += offset & PAGE_MASK;
402 }
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100403
Matthew Auld73ebd502017-12-11 15:18:20 +0000404 if (gtt_user_read(&ggtt->iomap, page_base, page_offset,
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100405 user_data, page_length)) {
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +0530406 ret = -EFAULT;
407 break;
408 }
409
410 remain -= page_length;
411 user_data += page_length;
412 offset += page_length;
413 }
414
Chris Wilson6951e582019-05-28 10:29:51 +0100415 i915_gem_object_unlock_fence(obj, fence);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +0530416out_unpin:
Chris Wilson6951e582019-05-28 10:29:51 +0100417 mutex_lock(&i915->drm.struct_mutex);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +0530418 if (node.allocated) {
Chris Wilson82ad6442018-06-05 16:37:58 +0100419 ggtt->vm.clear_range(&ggtt->vm, node.start, node.size);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +0530420 remove_mappable_node(&node);
421 } else {
Chris Wilson058d88c2016-08-15 10:49:06 +0100422 i915_vma_unpin(vma);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +0530423 }
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100424out_unlock:
Daniele Ceraolo Spuriod858d562019-06-13 16:21:54 -0700425 intel_runtime_pm_put(&i915->runtime_pm, wakeref);
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100426 mutex_unlock(&i915->drm.struct_mutex);
Chris Wilsonf60d7f02012-09-04 21:02:56 +0100427
Eric Anholteb014592009-03-10 11:44:52 -0700428 return ret;
429}
430
Eric Anholt673a3942008-07-30 12:06:12 -0700431/**
432 * Reads data from the object referenced by handle.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +0100433 * @dev: drm device pointer
434 * @data: ioctl data blob
435 * @file: drm file pointer
Eric Anholt673a3942008-07-30 12:06:12 -0700436 *
437 * On error, the contents of *data are undefined.
438 */
439int
440i915_gem_pread_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +0000441 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -0700442{
443 struct drm_i915_gem_pread *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +0000444 struct drm_i915_gem_object *obj;
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100445 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700446
Chris Wilson51311d02010-11-17 09:10:42 +0000447 if (args->size == 0)
448 return 0;
449
Linus Torvalds96d4f262019-01-03 18:57:57 -0800450 if (!access_ok(u64_to_user_ptr(args->data_ptr),
Chris Wilson51311d02010-11-17 09:10:42 +0000451 args->size))
452 return -EFAULT;
453
Chris Wilson03ac0642016-07-20 13:31:51 +0100454 obj = i915_gem_object_lookup(file, args->handle);
Chris Wilson258a5ed2016-08-05 10:14:16 +0100455 if (!obj)
456 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -0700457
Chris Wilson7dcd2492010-09-26 20:21:44 +0100458 /* Bounds check source. */
Matthew Auld966d5bf2016-12-13 20:32:22 +0000459 if (range_overflows_t(u64, args->offset, args->size, obj->base.size)) {
Chris Wilsonce9d4192010-09-26 20:50:05 +0100460 ret = -EINVAL;
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100461 goto out;
Chris Wilsonce9d4192010-09-26 20:50:05 +0100462 }
463
Chris Wilsondb53a302011-02-03 11:57:46 +0000464 trace_i915_gem_object_pread(obj, args->offset, args->size);
465
Chris Wilsone95433c2016-10-28 13:58:27 +0100466 ret = i915_gem_object_wait(obj,
467 I915_WAIT_INTERRUPTIBLE,
Chris Wilson62eb3c22019-02-13 09:25:04 +0000468 MAX_SCHEDULE_TIMEOUT);
Chris Wilson258a5ed2016-08-05 10:14:16 +0100469 if (ret)
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100470 goto out;
Chris Wilson258a5ed2016-08-05 10:14:16 +0100471
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100472 ret = i915_gem_object_pin_pages(obj);
Chris Wilson258a5ed2016-08-05 10:14:16 +0100473 if (ret)
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100474 goto out;
Chris Wilson258a5ed2016-08-05 10:14:16 +0100475
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100476 ret = i915_gem_shmem_pread(obj, args);
Chris Wilson9c870d02016-10-24 13:42:15 +0100477 if (ret == -EFAULT || ret == -ENODEV)
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100478 ret = i915_gem_gtt_pread(obj, args);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +0530479
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100480 i915_gem_object_unpin_pages(obj);
481out:
Chris Wilsonf0cd5182016-10-28 13:58:43 +0100482 i915_gem_object_put(obj);
Eric Anholteb014592009-03-10 11:44:52 -0700483 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700484}
485
Keith Packard0839ccb2008-10-30 19:38:48 -0700486/* This is the fast write path which cannot handle
487 * page faults in the source data
Linus Torvalds9b7530cc2008-10-20 14:16:43 -0700488 */
Linus Torvalds9b7530cc2008-10-20 14:16:43 -0700489
Chris Wilsonfe115622016-10-28 13:58:40 +0100490static inline bool
491ggtt_write(struct io_mapping *mapping,
492 loff_t base, int offset,
493 char __user *user_data, int length)
Keith Packard0839ccb2008-10-30 19:38:48 -0700494{
Ville Syrjäläafe722b2017-09-01 20:12:52 +0300495 void __iomem *vaddr;
Keith Packard0839ccb2008-10-30 19:38:48 -0700496 unsigned long unwritten;
497
Ben Widawsky4f0c7cf2012-04-16 14:07:47 -0700498 /* We can use the cpu mem copy function because this is X86. */
Ville Syrjäläafe722b2017-09-01 20:12:52 +0300499 vaddr = io_mapping_map_atomic_wc(mapping, base);
500 unwritten = __copy_from_user_inatomic_nocache((void __force *)vaddr + offset,
Keith Packard0839ccb2008-10-30 19:38:48 -0700501 user_data, length);
Chris Wilsonfe115622016-10-28 13:58:40 +0100502 io_mapping_unmap_atomic(vaddr);
503 if (unwritten) {
Ville Syrjäläafe722b2017-09-01 20:12:52 +0300504 vaddr = io_mapping_map_wc(mapping, base, PAGE_SIZE);
505 unwritten = copy_from_user((void __force *)vaddr + offset,
506 user_data, length);
Chris Wilsonfe115622016-10-28 13:58:40 +0100507 io_mapping_unmap(vaddr);
508 }
Keith Packard0839ccb2008-10-30 19:38:48 -0700509
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100510 return unwritten;
511}
512
Eric Anholt3de09aa2009-03-09 09:42:23 -0700513/**
514 * This is the fast pwrite path, where we copy the data directly from the
515 * user into the GTT, uncached.
Chris Wilsonfe115622016-10-28 13:58:40 +0100516 * @obj: i915 GEM object
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +0100517 * @args: pwrite arguments structure
Eric Anholt3de09aa2009-03-09 09:42:23 -0700518 */
Eric Anholt673a3942008-07-30 12:06:12 -0700519static int
Chris Wilsonfe115622016-10-28 13:58:40 +0100520i915_gem_gtt_pwrite_fast(struct drm_i915_gem_object *obj,
521 const struct drm_i915_gem_pwrite *args)
Eric Anholt673a3942008-07-30 12:06:12 -0700522{
Chris Wilsonfe115622016-10-28 13:58:40 +0100523 struct drm_i915_private *i915 = to_i915(obj->base.dev);
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +0530524 struct i915_ggtt *ggtt = &i915->ggtt;
Daniele Ceraolo Spuriod858d562019-06-13 16:21:54 -0700525 struct intel_runtime_pm *rpm = &i915->runtime_pm;
Chris Wilson538ef962019-01-14 14:21:18 +0000526 intel_wakeref_t wakeref;
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +0530527 struct drm_mm_node node;
Chris Wilson6951e582019-05-28 10:29:51 +0100528 struct dma_fence *fence;
Chris Wilsonfe115622016-10-28 13:58:40 +0100529 struct i915_vma *vma;
530 u64 remain, offset;
531 void __user *user_data;
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +0530532 int ret;
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +0530533
Chris Wilsonfe115622016-10-28 13:58:40 +0100534 ret = mutex_lock_interruptible(&i915->drm.struct_mutex);
535 if (ret)
536 return ret;
Daniel Vetter935aaa62012-03-25 19:47:35 +0200537
Chris Wilson8bd818152017-10-19 07:37:33 +0100538 if (i915_gem_object_has_struct_page(obj)) {
539 /*
540 * Avoid waking the device up if we can fallback, as
541 * waking/resuming is very slow (worst-case 10-100 ms
542 * depending on PCI sleeps and our own resume time).
543 * This easily dwarfs any performance advantage from
544 * using the cache bypass of indirect GGTT access.
545 */
Daniele Ceraolo Spuriod858d562019-06-13 16:21:54 -0700546 wakeref = intel_runtime_pm_get_if_in_use(rpm);
Chris Wilson538ef962019-01-14 14:21:18 +0000547 if (!wakeref) {
Chris Wilson8bd818152017-10-19 07:37:33 +0100548 ret = -EFAULT;
549 goto out_unlock;
550 }
551 } else {
552 /* No backing pages, no fallback, we must force GGTT access */
Daniele Ceraolo Spuriod858d562019-06-13 16:21:54 -0700553 wakeref = intel_runtime_pm_get(rpm);
Chris Wilson8bd818152017-10-19 07:37:33 +0100554 }
555
Chris Wilson1f7fd482019-08-22 07:15:57 +0100556 vma = ERR_PTR(-ENODEV);
557 if (!i915_gem_object_is_tiled(obj))
558 vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0,
559 PIN_MAPPABLE |
560 PIN_NONBLOCK /* NOWARN */ |
561 PIN_NOEVICT);
Chris Wilson18034582016-08-18 17:16:45 +0100562 if (!IS_ERR(vma)) {
563 node.start = i915_ggtt_offset(vma);
564 node.allocated = false;
Chris Wilson1f7fd482019-08-22 07:15:57 +0100565 } else {
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100566 ret = insert_mappable_node(ggtt, &node, PAGE_SIZE);
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +0530567 if (ret)
Chris Wilson8bd818152017-10-19 07:37:33 +0100568 goto out_rpm;
Chris Wilsonfe115622016-10-28 13:58:40 +0100569 GEM_BUG_ON(!node.allocated);
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +0530570 }
Daniel Vetter935aaa62012-03-25 19:47:35 +0200571
Chris Wilson6951e582019-05-28 10:29:51 +0100572 mutex_unlock(&i915->drm.struct_mutex);
573
574 ret = i915_gem_object_lock_interruptible(obj);
Daniel Vetter935aaa62012-03-25 19:47:35 +0200575 if (ret)
576 goto out_unpin;
577
Chris Wilson6951e582019-05-28 10:29:51 +0100578 ret = i915_gem_object_set_to_gtt_domain(obj, true);
579 if (ret) {
580 i915_gem_object_unlock(obj);
581 goto out_unpin;
582 }
583
584 fence = i915_gem_object_lock_fence(obj);
585 i915_gem_object_unlock(obj);
586 if (!fence) {
587 ret = -ENOMEM;
588 goto out_unpin;
589 }
Chris Wilsonfe115622016-10-28 13:58:40 +0100590
Chris Wilson8e7cb172019-08-16 08:46:35 +0100591 intel_frontbuffer_invalidate(obj->frontbuffer, ORIGIN_CPU);
Paulo Zanoni063e4e62015-02-13 17:23:45 -0200592
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +0530593 user_data = u64_to_user_ptr(args->data_ptr);
594 offset = args->offset;
595 remain = args->size;
596 while (remain) {
Eric Anholt673a3942008-07-30 12:06:12 -0700597 /* Operation in this page
598 *
Keith Packard0839ccb2008-10-30 19:38:48 -0700599 * page_base = page offset within aperture
600 * page_offset = offset within page
601 * page_length = bytes to copy for this page
Eric Anholt673a3942008-07-30 12:06:12 -0700602 */
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +0530603 u32 page_base = node.start;
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100604 unsigned int page_offset = offset_in_page(offset);
605 unsigned int page_length = PAGE_SIZE - page_offset;
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +0530606 page_length = remain < page_length ? remain : page_length;
607 if (node.allocated) {
Chris Wilsonbdae33b2019-07-18 15:54:05 +0100608 /* flush the write before we modify the GGTT */
609 intel_gt_flush_ggtt_writes(ggtt->vm.gt);
Chris Wilson82ad6442018-06-05 16:37:58 +0100610 ggtt->vm.insert_page(&ggtt->vm,
611 i915_gem_object_get_dma_address(obj, offset >> PAGE_SHIFT),
612 node.start, I915_CACHE_NONE, 0);
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +0530613 wmb(); /* flush modifications to the GGTT (insert_page) */
614 } else {
615 page_base += offset & PAGE_MASK;
616 }
Keith Packard0839ccb2008-10-30 19:38:48 -0700617 /* If we get a fault while copying data, then (presumably) our
Eric Anholt3de09aa2009-03-09 09:42:23 -0700618 * source page isn't available. Return the error and we'll
619 * retry in the slow path.
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +0530620 * If the object is non-shmem backed, we retry again with the
621 * path that handles page fault.
Keith Packard0839ccb2008-10-30 19:38:48 -0700622 */
Matthew Auld73ebd502017-12-11 15:18:20 +0000623 if (ggtt_write(&ggtt->iomap, page_base, page_offset,
Chris Wilsonfe115622016-10-28 13:58:40 +0100624 user_data, page_length)) {
625 ret = -EFAULT;
626 break;
Daniel Vetter935aaa62012-03-25 19:47:35 +0200627 }
Eric Anholt673a3942008-07-30 12:06:12 -0700628
Keith Packard0839ccb2008-10-30 19:38:48 -0700629 remain -= page_length;
630 user_data += page_length;
631 offset += page_length;
Eric Anholt673a3942008-07-30 12:06:12 -0700632 }
Chris Wilson8e7cb172019-08-16 08:46:35 +0100633 intel_frontbuffer_flush(obj->frontbuffer, ORIGIN_CPU);
Chris Wilsonfe115622016-10-28 13:58:40 +0100634
Chris Wilson6951e582019-05-28 10:29:51 +0100635 i915_gem_object_unlock_fence(obj, fence);
Daniel Vetter935aaa62012-03-25 19:47:35 +0200636out_unpin:
Chris Wilson6951e582019-05-28 10:29:51 +0100637 mutex_lock(&i915->drm.struct_mutex);
Chris Wilsonbdae33b2019-07-18 15:54:05 +0100638 intel_gt_flush_ggtt_writes(ggtt->vm.gt);
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +0530639 if (node.allocated) {
Chris Wilson82ad6442018-06-05 16:37:58 +0100640 ggtt->vm.clear_range(&ggtt->vm, node.start, node.size);
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +0530641 remove_mappable_node(&node);
642 } else {
Chris Wilson058d88c2016-08-15 10:49:06 +0100643 i915_vma_unpin(vma);
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +0530644 }
Chris Wilson8bd818152017-10-19 07:37:33 +0100645out_rpm:
Daniele Ceraolo Spuriod858d562019-06-13 16:21:54 -0700646 intel_runtime_pm_put(rpm, wakeref);
Chris Wilson8bd818152017-10-19 07:37:33 +0100647out_unlock:
Chris Wilsonfe115622016-10-28 13:58:40 +0100648 mutex_unlock(&i915->drm.struct_mutex);
Eric Anholt3de09aa2009-03-09 09:42:23 -0700649 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700650}
651
Chris Wilsonfe115622016-10-28 13:58:40 +0100652/* Per-page copy function for the shmem pwrite fastpath.
653 * Flushes invalid cachelines before writing to the target if
654 * needs_clflush_before is set and flushes out any written cachelines after
655 * writing if needs_clflush is set.
656 */
Eric Anholt40123c12009-03-09 13:42:30 -0700657static int
Chris Wilsonfe115622016-10-28 13:58:40 +0100658shmem_pwrite(struct page *page, int offset, int len, char __user *user_data,
Chris Wilsonfe115622016-10-28 13:58:40 +0100659 bool needs_clflush_before,
660 bool needs_clflush_after)
Eric Anholt40123c12009-03-09 13:42:30 -0700661{
Chris Wilsonb9d126e2019-01-05 12:07:58 +0000662 char *vaddr;
Chris Wilsonfe115622016-10-28 13:58:40 +0100663 int ret;
Eric Anholt40123c12009-03-09 13:42:30 -0700664
Chris Wilsonb9d126e2019-01-05 12:07:58 +0000665 vaddr = kmap(page);
Chris Wilsonfe115622016-10-28 13:58:40 +0100666
Chris Wilsonb9d126e2019-01-05 12:07:58 +0000667 if (needs_clflush_before)
668 drm_clflush_virt_range(vaddr + offset, len);
Chris Wilsonfe115622016-10-28 13:58:40 +0100669
Chris Wilsonb9d126e2019-01-05 12:07:58 +0000670 ret = __copy_from_user(vaddr + offset, user_data, len);
671 if (!ret && needs_clflush_after)
672 drm_clflush_virt_range(vaddr + offset, len);
Chris Wilsonfe115622016-10-28 13:58:40 +0100673
Chris Wilsonb9d126e2019-01-05 12:07:58 +0000674 kunmap(page);
675
676 return ret ? -EFAULT : 0;
Chris Wilsonfe115622016-10-28 13:58:40 +0100677}
678
679static int
680i915_gem_shmem_pwrite(struct drm_i915_gem_object *obj,
681 const struct drm_i915_gem_pwrite *args)
682{
Chris Wilsonfe115622016-10-28 13:58:40 +0100683 unsigned int partial_cacheline_write;
684 unsigned int needs_clflush;
685 unsigned int offset, idx;
Chris Wilson6951e582019-05-28 10:29:51 +0100686 struct dma_fence *fence;
687 void __user *user_data;
688 u64 remain;
Chris Wilsonfe115622016-10-28 13:58:40 +0100689 int ret;
690
Chris Wilson6951e582019-05-28 10:29:51 +0100691 ret = i915_gem_object_prepare_write(obj, &needs_clflush);
Chris Wilson43394c72016-08-18 17:16:47 +0100692 if (ret)
693 return ret;
Eric Anholt40123c12009-03-09 13:42:30 -0700694
Chris Wilson6951e582019-05-28 10:29:51 +0100695 fence = i915_gem_object_lock_fence(obj);
696 i915_gem_object_finish_access(obj);
697 if (!fence)
698 return -ENOMEM;
Chris Wilsonfe115622016-10-28 13:58:40 +0100699
Chris Wilsonfe115622016-10-28 13:58:40 +0100700 /* If we don't overwrite a cacheline completely we need to be
701 * careful to have up-to-date data by first clflushing. Don't
702 * overcomplicate things and flush the entire patch.
703 */
704 partial_cacheline_write = 0;
705 if (needs_clflush & CLFLUSH_BEFORE)
706 partial_cacheline_write = boot_cpu_data.x86_clflush_size - 1;
707
Chris Wilson43394c72016-08-18 17:16:47 +0100708 user_data = u64_to_user_ptr(args->data_ptr);
Chris Wilson43394c72016-08-18 17:16:47 +0100709 remain = args->size;
Chris Wilsonfe115622016-10-28 13:58:40 +0100710 offset = offset_in_page(args->offset);
711 for (idx = args->offset >> PAGE_SHIFT; remain; idx++) {
712 struct page *page = i915_gem_object_get_page(obj, idx);
Chris Wilsona5e856a52018-10-12 15:02:28 +0100713 unsigned int length = min_t(u64, remain, PAGE_SIZE - offset);
Chris Wilsone5281cc2010-10-28 13:45:36 +0100714
Chris Wilsonfe115622016-10-28 13:58:40 +0100715 ret = shmem_pwrite(page, offset, length, user_data,
Chris Wilsonfe115622016-10-28 13:58:40 +0100716 (offset | length) & partial_cacheline_write,
717 needs_clflush & CLFLUSH_AFTER);
718 if (ret)
Chris Wilson9da3da62012-06-01 15:20:22 +0100719 break;
720
Chris Wilsonfe115622016-10-28 13:58:40 +0100721 remain -= length;
722 user_data += length;
723 offset = 0;
Eric Anholt40123c12009-03-09 13:42:30 -0700724 }
725
Chris Wilson8e7cb172019-08-16 08:46:35 +0100726 intel_frontbuffer_flush(obj->frontbuffer, ORIGIN_CPU);
Chris Wilson6951e582019-05-28 10:29:51 +0100727 i915_gem_object_unlock_fence(obj, fence);
728
Eric Anholt40123c12009-03-09 13:42:30 -0700729 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700730}
731
732/**
733 * Writes data to the object referenced by handle.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +0100734 * @dev: drm device
735 * @data: ioctl data blob
736 * @file: drm file
Eric Anholt673a3942008-07-30 12:06:12 -0700737 *
738 * On error, the contents of the buffer that were to be modified are undefined.
739 */
740int
741i915_gem_pwrite_ioctl(struct drm_device *dev, void *data,
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100742 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -0700743{
744 struct drm_i915_gem_pwrite *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +0000745 struct drm_i915_gem_object *obj;
Chris Wilson51311d02010-11-17 09:10:42 +0000746 int ret;
747
748 if (args->size == 0)
749 return 0;
750
Linus Torvalds96d4f262019-01-03 18:57:57 -0800751 if (!access_ok(u64_to_user_ptr(args->data_ptr), args->size))
Chris Wilson51311d02010-11-17 09:10:42 +0000752 return -EFAULT;
753
Chris Wilson03ac0642016-07-20 13:31:51 +0100754 obj = i915_gem_object_lookup(file, args->handle);
Chris Wilson258a5ed2016-08-05 10:14:16 +0100755 if (!obj)
756 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -0700757
Chris Wilson7dcd2492010-09-26 20:21:44 +0100758 /* Bounds check destination. */
Matthew Auld966d5bf2016-12-13 20:32:22 +0000759 if (range_overflows_t(u64, args->offset, args->size, obj->base.size)) {
Chris Wilsonce9d4192010-09-26 20:50:05 +0100760 ret = -EINVAL;
Chris Wilson258a5ed2016-08-05 10:14:16 +0100761 goto err;
Chris Wilsonce9d4192010-09-26 20:50:05 +0100762 }
763
Chris Wilsonf8c1cce2018-07-12 19:53:14 +0100764 /* Writes not allowed into this read-only object */
765 if (i915_gem_object_is_readonly(obj)) {
766 ret = -EINVAL;
767 goto err;
768 }
769
Chris Wilsondb53a302011-02-03 11:57:46 +0000770 trace_i915_gem_object_pwrite(obj, args->offset, args->size);
771
Chris Wilson7c55e2c2017-03-07 12:03:38 +0000772 ret = -ENODEV;
773 if (obj->ops->pwrite)
774 ret = obj->ops->pwrite(obj, args);
775 if (ret != -ENODEV)
776 goto err;
777
Chris Wilsone95433c2016-10-28 13:58:27 +0100778 ret = i915_gem_object_wait(obj,
779 I915_WAIT_INTERRUPTIBLE |
780 I915_WAIT_ALL,
Chris Wilson62eb3c22019-02-13 09:25:04 +0000781 MAX_SCHEDULE_TIMEOUT);
Chris Wilson258a5ed2016-08-05 10:14:16 +0100782 if (ret)
783 goto err;
784
Chris Wilsonfe115622016-10-28 13:58:40 +0100785 ret = i915_gem_object_pin_pages(obj);
Chris Wilson258a5ed2016-08-05 10:14:16 +0100786 if (ret)
Chris Wilsonfe115622016-10-28 13:58:40 +0100787 goto err;
Chris Wilson258a5ed2016-08-05 10:14:16 +0100788
Daniel Vetter935aaa62012-03-25 19:47:35 +0200789 ret = -EFAULT;
Eric Anholt673a3942008-07-30 12:06:12 -0700790 /* We can only do the GTT pwrite on untiled buffers, as otherwise
791 * it would end up going through the fenced access, and we'll get
792 * different detiling behavior between reading and writing.
793 * pread/pwrite currently are reading and writing from the CPU
794 * perspective, requiring manual detiling by the client.
795 */
Chris Wilson6eae0052016-06-20 15:05:52 +0100796 if (!i915_gem_object_has_struct_page(obj) ||
Chris Wilson9c870d02016-10-24 13:42:15 +0100797 cpu_write_needs_clflush(obj))
Daniel Vetter935aaa62012-03-25 19:47:35 +0200798 /* Note that the gtt paths might fail with non-page-backed user
799 * pointers (e.g. gtt mappings when moving data between
Chris Wilson9c870d02016-10-24 13:42:15 +0100800 * textures). Fallback to the shmem path in that case.
801 */
Chris Wilsonfe115622016-10-28 13:58:40 +0100802 ret = i915_gem_gtt_pwrite_fast(obj, args);
Eric Anholt673a3942008-07-30 12:06:12 -0700803
Chris Wilsond1054ee2016-07-16 18:42:36 +0100804 if (ret == -EFAULT || ret == -ENOSPC) {
Chris Wilson6a2c4232014-11-04 04:51:40 -0800805 if (obj->phys_handle)
806 ret = i915_gem_phys_pwrite(obj, args, file);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +0530807 else
Chris Wilsonfe115622016-10-28 13:58:40 +0100808 ret = i915_gem_shmem_pwrite(obj, args);
Chris Wilson6a2c4232014-11-04 04:51:40 -0800809 }
Daniel Vetter5c0480f2011-12-14 13:57:30 +0100810
Chris Wilsonfe115622016-10-28 13:58:40 +0100811 i915_gem_object_unpin_pages(obj);
Chris Wilson258a5ed2016-08-05 10:14:16 +0100812err:
Chris Wilsonf0cd5182016-10-28 13:58:43 +0100813 i915_gem_object_put(obj);
Chris Wilson258a5ed2016-08-05 10:14:16 +0100814 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700815}
816
Eric Anholt673a3942008-07-30 12:06:12 -0700817/**
818 * Called when user space has done writes to this buffer
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +0100819 * @dev: drm device
820 * @data: ioctl data blob
821 * @file: drm file
Eric Anholt673a3942008-07-30 12:06:12 -0700822 */
823int
824i915_gem_sw_finish_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +0000825 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -0700826{
827 struct drm_i915_gem_sw_finish *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +0000828 struct drm_i915_gem_object *obj;
Chris Wilson1d7cfea2010-10-17 09:45:41 +0100829
Chris Wilson03ac0642016-07-20 13:31:51 +0100830 obj = i915_gem_object_lookup(file, args->handle);
Chris Wilsonc21724c2016-08-05 10:14:19 +0100831 if (!obj)
832 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -0700833
Tina Zhanga03f3952017-11-14 10:25:13 +0000834 /*
835 * Proxy objects are barred from CPU access, so there is no
836 * need to ban sw_finish as it is a nop.
837 */
838
Eric Anholt673a3942008-07-30 12:06:12 -0700839 /* Pinned buffers may be scanout, so flush the cache */
Chris Wilson5a97bcc2017-02-22 11:40:46 +0000840 i915_gem_object_flush_if_display(obj);
Chris Wilsonf0cd5182016-10-28 13:58:43 +0100841 i915_gem_object_put(obj);
Chris Wilson5a97bcc2017-02-22 11:40:46 +0000842
843 return 0;
Eric Anholt673a3942008-07-30 12:06:12 -0700844}
845
Chris Wilson0cf289b2019-06-13 08:32:54 +0100846void i915_gem_runtime_suspend(struct drm_i915_private *i915)
Chris Wilsoneedd10f2014-06-16 08:57:44 +0100847{
Chris Wilson3594a3e2016-10-24 13:42:16 +0100848 struct drm_i915_gem_object *obj, *on;
Chris Wilson7c108fd2016-10-24 13:42:18 +0100849 int i;
Chris Wilsoneedd10f2014-06-16 08:57:44 +0100850
Chris Wilson3594a3e2016-10-24 13:42:16 +0100851 /*
852 * Only called during RPM suspend. All users of the userfault_list
853 * must be holding an RPM wakeref to ensure that this can not
854 * run concurrently with themselves (and use the struct_mutex for
855 * protection between themselves).
856 */
857
858 list_for_each_entry_safe(obj, on,
Chris Wilson0cf289b2019-06-13 08:32:54 +0100859 &i915->ggtt.userfault_list, userfault_link)
Chris Wilsona65adaf2017-10-09 09:43:57 +0100860 __i915_gem_object_release_mmap(obj);
Chris Wilson7c108fd2016-10-24 13:42:18 +0100861
Chris Wilson0cf289b2019-06-13 08:32:54 +0100862 /*
863 * The fence will be lost when the device powers down. If any were
Chris Wilson7c108fd2016-10-24 13:42:18 +0100864 * in use by hardware (i.e. they are pinned), we should not be powering
865 * down! All other fences will be reacquired by the user upon waking.
866 */
Chris Wilson0cf289b2019-06-13 08:32:54 +0100867 for (i = 0; i < i915->ggtt.num_fences; i++) {
868 struct i915_fence_reg *reg = &i915->ggtt.fence_regs[i];
Chris Wilson7c108fd2016-10-24 13:42:18 +0100869
Chris Wilson0cf289b2019-06-13 08:32:54 +0100870 /*
871 * Ideally we want to assert that the fence register is not
Chris Wilsone0ec3ec2017-02-03 12:57:17 +0000872 * live at this point (i.e. that no piece of code will be
873 * trying to write through fence + GTT, as that both violates
874 * our tracking of activity and associated locking/barriers,
875 * but also is illegal given that the hw is powered down).
876 *
877 * Previously we used reg->pin_count as a "liveness" indicator.
878 * That is not sufficient, and we need a more fine-grained
879 * tool if we want to have a sanity check here.
880 */
Chris Wilson7c108fd2016-10-24 13:42:18 +0100881
882 if (!reg->vma)
883 continue;
884
Chris Wilsona65adaf2017-10-09 09:43:57 +0100885 GEM_BUG_ON(i915_vma_has_userfault(reg->vma));
Chris Wilson7c108fd2016-10-24 13:42:18 +0100886 reg->dirty = true;
887 }
Chris Wilsoneedd10f2014-06-16 08:57:44 +0100888}
889
Chris Wilson1e345562019-01-28 10:23:56 +0000890static long
891wait_for_timelines(struct drm_i915_private *i915,
892 unsigned int flags, long timeout)
893{
Chris Wilson338aade2019-08-15 21:57:07 +0100894 struct intel_gt_timelines *timelines = &i915->gt.timelines;
Tvrtko Ursulinf0c02c12019-06-21 08:08:10 +0100895 struct intel_timeline *tl;
Chris Wilson1e345562019-01-28 10:23:56 +0000896
Chris Wilson338aade2019-08-15 21:57:07 +0100897 spin_lock(&timelines->lock);
898 list_for_each_entry(tl, &timelines->active_list, link) {
Chris Wilson1e345562019-01-28 10:23:56 +0000899 struct i915_request *rq;
900
Chris Wilson21950ee2019-02-05 13:00:05 +0000901 rq = i915_active_request_get_unlocked(&tl->last_request);
Chris Wilson1e345562019-01-28 10:23:56 +0000902 if (!rq)
903 continue;
904
Chris Wilson338aade2019-08-15 21:57:07 +0100905 spin_unlock(&timelines->lock);
Chris Wilson1e345562019-01-28 10:23:56 +0000906
907 /*
908 * "Race-to-idle".
909 *
910 * Switching to the kernel context is often used a synchronous
911 * step prior to idling, e.g. in suspend for flushing all
912 * current operations to memory before sleeping. These we
913 * want to complete as quickly as possible to avoid prolonged
914 * stalls, so allow the gpu to boost to maximum clocks.
915 */
916 if (flags & I915_WAIT_FOR_IDLE_BOOST)
Chris Wilson62eb3c22019-02-13 09:25:04 +0000917 gen6_rps_boost(rq);
Chris Wilson1e345562019-01-28 10:23:56 +0000918
919 timeout = i915_request_wait(rq, flags, timeout);
920 i915_request_put(rq);
921 if (timeout < 0)
922 return timeout;
923
924 /* restart after reacquiring the lock */
Chris Wilson338aade2019-08-15 21:57:07 +0100925 spin_lock(&timelines->lock);
926 tl = list_entry(&timelines->active_list, typeof(*tl), link);
Chris Wilson1e345562019-01-28 10:23:56 +0000927 }
Chris Wilson338aade2019-08-15 21:57:07 +0100928 spin_unlock(&timelines->lock);
Chris Wilson1e345562019-01-28 10:23:56 +0000929
930 return timeout;
931}
932
Chris Wilsonec625fb2018-07-09 13:20:42 +0100933int i915_gem_wait_for_idle(struct drm_i915_private *i915,
934 unsigned int flags, long timeout)
Chris Wilson73cb9702016-10-28 13:58:46 +0100935{
Chris Wilson14f8a0e2019-07-23 10:12:18 +0100936 /* If the device is asleep, we have no requests outstanding */
Chris Wilsonc7302f22019-08-08 21:27:58 +0100937 if (!intel_gt_pm_is_awake(&i915->gt))
Chris Wilson14f8a0e2019-07-23 10:12:18 +0100938 return 0;
939
Chris Wilsonc7302f22019-08-08 21:27:58 +0100940 GEM_TRACE("flags=%x (%s), timeout=%ld%s\n",
Chris Wilsonec625fb2018-07-09 13:20:42 +0100941 flags, flags & I915_WAIT_LOCKED ? "locked" : "unlocked",
Chris Wilsonc7302f22019-08-08 21:27:58 +0100942 timeout, timeout == MAX_SCHEDULE_TIMEOUT ? " (forever)" : "");
Chris Wilson09a4c022018-05-24 09:11:35 +0100943
Chris Wilson1e345562019-01-28 10:23:56 +0000944 timeout = wait_for_timelines(i915, flags, timeout);
945 if (timeout < 0)
946 return timeout;
947
Chris Wilson9caa34a2016-11-11 14:58:08 +0000948 if (flags & I915_WAIT_LOCKED) {
Chris Wilson9caa34a2016-11-11 14:58:08 +0000949 lockdep_assert_held(&i915->drm.struct_mutex);
950
Chris Wilsone61e0f52018-02-21 09:56:36 +0000951 i915_retire_requests(i915);
Chris Wilsona89d1f92018-05-02 17:38:39 +0100952 }
Chris Wilsona61b47f2018-06-27 12:53:34 +0100953
954 return 0;
Daniel Vetter4df2faf2010-02-19 11:52:00 +0100955}
956
Chris Wilson058d88c2016-08-15 10:49:06 +0100957struct i915_vma *
Joonas Lahtinenec7adb62015-03-16 14:11:13 +0200958i915_gem_object_ggtt_pin(struct drm_i915_gem_object *obj,
959 const struct i915_ggtt_view *view,
Chris Wilson91b2db62016-08-04 16:32:23 +0100960 u64 size,
Chris Wilson2ffffd02016-08-04 16:32:22 +0100961 u64 alignment,
962 u64 flags)
Joonas Lahtinenec7adb62015-03-16 14:11:13 +0200963{
Chris Wilsonad16d2e2016-10-13 09:55:04 +0100964 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
Chris Wilson82ad6442018-06-05 16:37:58 +0100965 struct i915_address_space *vm = &dev_priv->ggtt.vm;
Chris Wilson59bfa122016-08-04 16:32:31 +0100966 struct i915_vma *vma;
967 int ret;
Joonas Lahtinen72e96d62016-03-30 16:57:10 +0300968
Chris Wilson4c7d62c2016-10-28 13:58:32 +0100969 lockdep_assert_held(&obj->base.dev->struct_mutex);
970
Chris Wilsonac87a6fd2018-02-20 13:42:05 +0000971 if (flags & PIN_MAPPABLE &&
972 (!view || view->type == I915_GGTT_VIEW_NORMAL)) {
Chris Wilson43ae70d92017-10-09 09:44:01 +0100973 /* If the required space is larger than the available
974 * aperture, we will not able to find a slot for the
975 * object and unbinding the object now will be in
976 * vain. Worse, doing so may cause us to ping-pong
977 * the object in and out of the Global GTT and
978 * waste a lot of cycles under the mutex.
979 */
980 if (obj->base.size > dev_priv->ggtt.mappable_end)
981 return ERR_PTR(-E2BIG);
982
983 /* If NONBLOCK is set the caller is optimistically
984 * trying to cache the full object within the mappable
985 * aperture, and *must* have a fallback in place for
986 * situations where we cannot bind the object. We
987 * can be a little more lax here and use the fallback
988 * more often to avoid costly migrations of ourselves
989 * and other objects within the aperture.
990 *
991 * Half-the-aperture is used as a simple heuristic.
992 * More interesting would to do search for a free
993 * block prior to making the commitment to unbind.
994 * That caters for the self-harm case, and with a
995 * little more heuristics (e.g. NOFAULT, NOEVICT)
996 * we could try to minimise harm to others.
997 */
998 if (flags & PIN_NONBLOCK &&
999 obj->base.size > dev_priv->ggtt.mappable_end / 2)
1000 return ERR_PTR(-ENOSPC);
1001 }
1002
Chris Wilson718659a2017-01-16 15:21:28 +00001003 vma = i915_vma_instance(obj, vm, view);
Chengguang Xu772b5402019-02-21 10:08:19 +08001004 if (IS_ERR(vma))
Chris Wilson058d88c2016-08-15 10:49:06 +01001005 return vma;
Chris Wilson59bfa122016-08-04 16:32:31 +01001006
1007 if (i915_vma_misplaced(vma, size, alignment, flags)) {
Chris Wilson43ae70d92017-10-09 09:44:01 +01001008 if (flags & PIN_NONBLOCK) {
1009 if (i915_vma_is_pinned(vma) || i915_vma_is_active(vma))
1010 return ERR_PTR(-ENOSPC);
Chris Wilson59bfa122016-08-04 16:32:31 +01001011
Chris Wilson43ae70d92017-10-09 09:44:01 +01001012 if (flags & PIN_MAPPABLE &&
Chris Wilson944397f2017-01-09 16:16:11 +00001013 vma->fence_size > dev_priv->ggtt.mappable_end / 2)
Chris Wilsonad16d2e2016-10-13 09:55:04 +01001014 return ERR_PTR(-ENOSPC);
1015 }
1016
Chris Wilson59bfa122016-08-04 16:32:31 +01001017 WARN(i915_vma_is_pinned(vma),
1018 "bo is already pinned in ggtt with incorrect alignment:"
Chris Wilson05a20d02016-08-18 17:16:55 +01001019 " offset=%08x, req.alignment=%llx,"
1020 " req.map_and_fenceable=%d, vma->map_and_fenceable=%d\n",
1021 i915_ggtt_offset(vma), alignment,
Chris Wilson59bfa122016-08-04 16:32:31 +01001022 !!(flags & PIN_MAPPABLE),
Chris Wilson05a20d02016-08-18 17:16:55 +01001023 i915_vma_is_map_and_fenceable(vma));
Chris Wilson59bfa122016-08-04 16:32:31 +01001024 ret = i915_vma_unbind(vma);
1025 if (ret)
Chris Wilson058d88c2016-08-15 10:49:06 +01001026 return ERR_PTR(ret);
Chris Wilson59bfa122016-08-04 16:32:31 +01001027 }
1028
Chris Wilson058d88c2016-08-15 10:49:06 +01001029 ret = i915_vma_pin(vma, size, alignment, flags | PIN_GLOBAL);
1030 if (ret)
1031 return ERR_PTR(ret);
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02001032
Chris Wilson058d88c2016-08-15 10:49:06 +01001033 return vma;
Eric Anholt673a3942008-07-30 12:06:12 -07001034}
1035
Eric Anholt673a3942008-07-30 12:06:12 -07001036int
Chris Wilson3ef94da2009-09-14 16:50:29 +01001037i915_gem_madvise_ioctl(struct drm_device *dev, void *data,
1038 struct drm_file *file_priv)
1039{
Chris Wilson3b4fa962019-05-30 21:34:59 +01001040 struct drm_i915_private *i915 = to_i915(dev);
Chris Wilson3ef94da2009-09-14 16:50:29 +01001041 struct drm_i915_gem_madvise *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00001042 struct drm_i915_gem_object *obj;
Chris Wilson1233e2d2016-10-28 13:58:37 +01001043 int err;
Chris Wilson3ef94da2009-09-14 16:50:29 +01001044
1045 switch (args->madv) {
1046 case I915_MADV_DONTNEED:
1047 case I915_MADV_WILLNEED:
1048 break;
1049 default:
1050 return -EINVAL;
1051 }
1052
Chris Wilson03ac0642016-07-20 13:31:51 +01001053 obj = i915_gem_object_lookup(file_priv, args->handle);
Chris Wilson1233e2d2016-10-28 13:58:37 +01001054 if (!obj)
1055 return -ENOENT;
1056
1057 err = mutex_lock_interruptible(&obj->mm.lock);
1058 if (err)
1059 goto out;
Chris Wilson3ef94da2009-09-14 16:50:29 +01001060
Chris Wilsonf1fa4f42017-10-13 21:26:13 +01001061 if (i915_gem_object_has_pages(obj) &&
Chris Wilson3e510a82016-08-05 10:14:23 +01001062 i915_gem_object_is_tiled(obj) &&
Chris Wilson3b4fa962019-05-30 21:34:59 +01001063 i915->quirks & QUIRK_PIN_SWIZZLED_PAGES) {
Chris Wilsonbc0629a2016-11-01 10:03:17 +00001064 if (obj->mm.madv == I915_MADV_WILLNEED) {
1065 GEM_BUG_ON(!obj->mm.quirked);
Chris Wilsona4f5ea62016-10-28 13:58:35 +01001066 __i915_gem_object_unpin_pages(obj);
Chris Wilsonbc0629a2016-11-01 10:03:17 +00001067 obj->mm.quirked = false;
1068 }
1069 if (args->madv == I915_MADV_WILLNEED) {
Chris Wilson2c3a3f42016-11-04 10:30:01 +00001070 GEM_BUG_ON(obj->mm.quirked);
Chris Wilsona4f5ea62016-10-28 13:58:35 +01001071 __i915_gem_object_pin_pages(obj);
Chris Wilsonbc0629a2016-11-01 10:03:17 +00001072 obj->mm.quirked = true;
1073 }
Daniel Vetter656bfa32014-11-20 09:26:30 +01001074 }
1075
Chris Wilsona4f5ea62016-10-28 13:58:35 +01001076 if (obj->mm.madv != __I915_MADV_PURGED)
1077 obj->mm.madv = args->madv;
Chris Wilson3ef94da2009-09-14 16:50:29 +01001078
Chris Wilson3b4fa962019-05-30 21:34:59 +01001079 if (i915_gem_object_has_pages(obj)) {
1080 struct list_head *list;
1081
Chris Wilsond82b4b22019-05-30 21:35:00 +01001082 if (i915_gem_object_is_shrinkable(obj)) {
Chris Wilsona8cff4c82019-06-10 15:54:30 +01001083 unsigned long flags;
1084
1085 spin_lock_irqsave(&i915->mm.obj_lock, flags);
1086
Chris Wilsond82b4b22019-05-30 21:35:00 +01001087 if (obj->mm.madv != I915_MADV_WILLNEED)
1088 list = &i915->mm.purge_list;
Chris Wilsond82b4b22019-05-30 21:35:00 +01001089 else
Chris Wilsonecab9be2019-06-12 11:57:20 +01001090 list = &i915->mm.shrink_list;
Chris Wilsond82b4b22019-05-30 21:35:00 +01001091 list_move_tail(&obj->mm.link, list);
Chris Wilsona8cff4c82019-06-10 15:54:30 +01001092
1093 spin_unlock_irqrestore(&i915->mm.obj_lock, flags);
Chris Wilsond82b4b22019-05-30 21:35:00 +01001094 }
Chris Wilson3b4fa962019-05-30 21:34:59 +01001095 }
1096
Chris Wilson6c085a72012-08-20 11:40:46 +02001097 /* if the object is no longer attached, discard its backing storage */
Chris Wilsonf1fa4f42017-10-13 21:26:13 +01001098 if (obj->mm.madv == I915_MADV_DONTNEED &&
1099 !i915_gem_object_has_pages(obj))
Chris Wilsonf0334282019-05-28 10:29:46 +01001100 i915_gem_object_truncate(obj);
Chris Wilson2d7ef392009-09-20 23:13:10 +01001101
Chris Wilsona4f5ea62016-10-28 13:58:35 +01001102 args->retained = obj->mm.madv != __I915_MADV_PURGED;
Chris Wilson1233e2d2016-10-28 13:58:37 +01001103 mutex_unlock(&obj->mm.lock);
Chris Wilsonbb6baf72009-09-22 14:24:13 +01001104
Chris Wilson1233e2d2016-10-28 13:58:37 +01001105out:
Chris Wilsonf8c417c2016-07-20 13:31:53 +01001106 i915_gem_object_put(obj);
Chris Wilson1233e2d2016-10-28 13:58:37 +01001107 return err;
Chris Wilson3ef94da2009-09-14 16:50:29 +01001108}
1109
Chris Wilson24145512017-01-24 11:01:35 +00001110void i915_gem_sanitize(struct drm_i915_private *i915)
1111{
Chris Wilson538ef962019-01-14 14:21:18 +00001112 intel_wakeref_t wakeref;
1113
Chris Wilsonc3160da2018-05-31 09:22:45 +01001114 GEM_TRACE("\n");
1115
Daniele Ceraolo Spuriod858d562019-06-13 16:21:54 -07001116 wakeref = intel_runtime_pm_get(&i915->runtime_pm);
Daniele Ceraolo Spurio3ceea6a2019-03-19 11:35:36 -07001117 intel_uncore_forcewake_get(&i915->uncore, FORCEWAKE_ALL);
Chris Wilsonc3160da2018-05-31 09:22:45 +01001118
1119 /*
1120 * As we have just resumed the machine and woken the device up from
1121 * deep PCI sleep (presumably D3_cold), assume the HW has been reset
1122 * back to defaults, recovering from whatever wedged state we left it
1123 * in and so worth trying to use the device once more.
1124 */
Chris Wilsoncb823ed2019-07-12 20:29:53 +01001125 if (intel_gt_is_wedged(&i915->gt))
1126 intel_gt_unset_wedged(&i915->gt);
Chris Wilsonf36325f2017-08-26 12:09:34 +01001127
Chris Wilson24145512017-01-24 11:01:35 +00001128 /*
1129 * If we inherit context state from the BIOS or earlier occupants
1130 * of the GPU, the GPU may be in an inconsistent state when we
1131 * try to take over. The only way to remove the earlier state
1132 * is by resetting. However, resetting on earlier gen is tricky as
1133 * it may impact the display and we are uncertain about the stability
Joonas Lahtinenea117b82017-04-28 10:53:38 +03001134 * of the reset, so this could be applied to even earlier gen.
Chris Wilson24145512017-01-24 11:01:35 +00001135 */
Chris Wilson0c916212019-06-25 14:01:10 +01001136 intel_gt_sanitize(&i915->gt, false);
Chris Wilsonc3160da2018-05-31 09:22:45 +01001137
Daniele Ceraolo Spurio3ceea6a2019-03-19 11:35:36 -07001138 intel_uncore_forcewake_put(&i915->uncore, FORCEWAKE_ALL);
Daniele Ceraolo Spuriod858d562019-06-13 16:21:54 -07001139 intel_runtime_pm_put(&i915->runtime_pm, wakeref);
Chris Wilson24145512017-01-24 11:01:35 +00001140}
1141
Tvrtko Ursulincf6844b2019-06-21 08:07:47 +01001142static void init_unused_ring(struct intel_gt *gt, u32 base)
Ville Syrjälä81e7f202014-08-15 01:21:55 +03001143{
Tvrtko Ursulincf6844b2019-06-21 08:07:47 +01001144 struct intel_uncore *uncore = gt->uncore;
1145
1146 intel_uncore_write(uncore, RING_CTL(base), 0);
1147 intel_uncore_write(uncore, RING_HEAD(base), 0);
1148 intel_uncore_write(uncore, RING_TAIL(base), 0);
1149 intel_uncore_write(uncore, RING_START(base), 0);
Ville Syrjälä81e7f202014-08-15 01:21:55 +03001150}
1151
Tvrtko Ursulincf6844b2019-06-21 08:07:47 +01001152static void init_unused_rings(struct intel_gt *gt)
Ville Syrjälä81e7f202014-08-15 01:21:55 +03001153{
Tvrtko Ursulincf6844b2019-06-21 08:07:47 +01001154 struct drm_i915_private *i915 = gt->i915;
1155
1156 if (IS_I830(i915)) {
1157 init_unused_ring(gt, PRB1_BASE);
1158 init_unused_ring(gt, SRB0_BASE);
1159 init_unused_ring(gt, SRB1_BASE);
1160 init_unused_ring(gt, SRB2_BASE);
1161 init_unused_ring(gt, SRB3_BASE);
1162 } else if (IS_GEN(i915, 2)) {
1163 init_unused_ring(gt, SRB0_BASE);
1164 init_unused_ring(gt, SRB1_BASE);
1165 } else if (IS_GEN(i915, 3)) {
1166 init_unused_ring(gt, PRB1_BASE);
1167 init_unused_ring(gt, PRB2_BASE);
Ville Syrjälä81e7f202014-08-15 01:21:55 +03001168 }
1169}
1170
Chris Wilson092be382019-06-26 16:45:49 +01001171int i915_gem_init_hw(struct drm_i915_private *i915)
Chris Wilson20a8a742017-02-08 14:30:31 +00001172{
Chris Wilson092be382019-06-26 16:45:49 +01001173 struct intel_uncore *uncore = &i915->uncore;
1174 struct intel_gt *gt = &i915->gt;
Chris Wilsond200cda2016-04-28 09:56:44 +01001175 int ret;
Ben Widawsky4fc7c972013-02-08 11:49:24 -08001176
Chris Wilson092be382019-06-26 16:45:49 +01001177 BUG_ON(!i915->kernel_context);
Chris Wilsoncb823ed2019-07-12 20:29:53 +01001178 ret = intel_gt_terminally_wedged(gt);
Chris Wilson092be382019-06-26 16:45:49 +01001179 if (ret)
1180 return ret;
1181
Tvrtko Ursulinabc584f2019-06-21 08:07:53 +01001182 gt->last_init_time = ktime_get();
Chris Wilsonde867c22016-10-25 13:16:02 +01001183
Chris Wilson5e4f5182015-02-13 14:35:59 +00001184 /* Double layer security blanket, see i915_gem_init() */
Tvrtko Ursulinabc584f2019-06-21 08:07:53 +01001185 intel_uncore_forcewake_get(uncore, FORCEWAKE_ALL);
Chris Wilson5e4f5182015-02-13 14:35:59 +00001186
Tvrtko Ursulinabc584f2019-06-21 08:07:53 +01001187 if (HAS_EDRAM(i915) && INTEL_GEN(i915) < 9)
1188 intel_uncore_rmw(uncore, HSW_IDICR, 0, IDIHASHMSK(0xf));
Ben Widawsky4fc7c972013-02-08 11:49:24 -08001189
Tvrtko Ursulinabc584f2019-06-21 08:07:53 +01001190 if (IS_HASWELL(i915))
1191 intel_uncore_write(uncore,
1192 MI_PREDICATE_RESULT_2,
1193 IS_HSW_GT3(i915) ?
1194 LOWER_SLICE_ENABLED : LOWER_SLICE_DISABLED);
Rodrigo Vivi94353732013-08-28 16:45:46 -03001195
Tvrtko Ursulin094304b2018-12-03 12:50:10 +00001196 /* Apply the GT workarounds... */
Tvrtko Ursulinabc584f2019-06-21 08:07:53 +01001197 intel_gt_apply_workarounds(gt);
Tvrtko Ursulin094304b2018-12-03 12:50:10 +00001198 /* ...and determine whether they are sticking. */
Tvrtko Ursulinabc584f2019-06-21 08:07:53 +01001199 intel_gt_verify_workarounds(gt, "init");
Oscar Mateo59b449d2018-04-10 09:12:47 -07001200
Tvrtko Ursulinabc584f2019-06-21 08:07:53 +01001201 intel_gt_init_swizzling(gt);
Ben Widawsky4fc7c972013-02-08 11:49:24 -08001202
Daniel Vetterd5abdfd2014-11-20 09:45:19 +01001203 /*
1204 * At least 830 can leave some of the unused rings
1205 * "active" (ie. head != tail) after resume which
1206 * will prevent c3 entry. Makes sure all unused rings
1207 * are totally idle.
1208 */
Tvrtko Ursulinabc584f2019-06-21 08:07:53 +01001209 init_unused_rings(gt);
Daniel Vetterd5abdfd2014-11-20 09:45:19 +01001210
Tvrtko Ursulinabc584f2019-06-21 08:07:53 +01001211 ret = i915_ppgtt_init_hw(gt);
John Harrison4ad2fd82015-06-18 13:11:20 +01001212 if (ret) {
Chris Wilson8177e112018-02-07 11:15:45 +00001213 DRM_ERROR("Enabling PPGTT failed (%d)\n", ret);
John Harrison4ad2fd82015-06-18 13:11:20 +01001214 goto out;
1215 }
1216
Michał Winiarski9bdc3572017-10-25 18:25:19 +01001217 /* We can't enable contexts until all firmware is loaded */
Daniele Ceraolo Spurio63064d82019-07-30 16:07:40 -07001218 ret = intel_uc_init_hw(&gt->uc);
Chris Wilson8177e112018-02-07 11:15:45 +00001219 if (ret) {
Michal Wajdeczko5d1ef2b2019-08-02 18:40:54 +00001220 i915_probe_error(i915, "Enabling uc failed (%d)\n", ret);
Michał Winiarski9bdc3572017-10-25 18:25:19 +01001221 goto out;
Chris Wilson8177e112018-02-07 11:15:45 +00001222 }
Michał Winiarski9bdc3572017-10-25 18:25:19 +01001223
Tvrtko Ursulin1b6c3c62019-07-30 11:04:07 -07001224 intel_mocs_init(gt);
Peter Antoine0ccdacf2016-04-13 15:03:25 +01001225
Michał Winiarski60c0a662018-07-12 14:48:10 +02001226out:
Tvrtko Ursulinabc584f2019-06-21 08:07:53 +01001227 intel_uncore_forcewake_put(uncore, FORCEWAKE_ALL);
Michał Winiarski60c0a662018-07-12 14:48:10 +02001228 return ret;
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001229}
1230
Chris Wilsond2b4b972017-11-10 14:26:33 +00001231static int __intel_engines_record_defaults(struct drm_i915_private *i915)
1232{
Chris Wilson38775822019-08-08 12:06:11 +01001233 struct i915_request *requests[I915_NUM_ENGINES] = {};
Chris Wilsond2b4b972017-11-10 14:26:33 +00001234 struct intel_engine_cs *engine;
1235 enum intel_engine_id id;
Chris Wilson604c37d2019-03-08 09:36:55 +00001236 int err = 0;
Chris Wilsond2b4b972017-11-10 14:26:33 +00001237
1238 /*
1239 * As we reset the gpu during very early sanitisation, the current
1240 * register state on the GPU should reflect its defaults values.
1241 * We load a context onto the hw (with restore-inhibit), then switch
1242 * over to a second context to save that default register state. We
1243 * can then prime every new context with that state so they all start
1244 * from the same default HW values.
1245 */
1246
Chris Wilsond2b4b972017-11-10 14:26:33 +00001247 for_each_engine(engine, i915, id) {
Chris Wilson38775822019-08-08 12:06:11 +01001248 struct intel_context *ce;
Chris Wilsone61e0f52018-02-21 09:56:36 +00001249 struct i915_request *rq;
Chris Wilsond2b4b972017-11-10 14:26:33 +00001250
Chris Wilson38775822019-08-08 12:06:11 +01001251 /* We must be able to switch to something! */
1252 GEM_BUG_ON(!engine->kernel_context);
1253 engine->serial++; /* force the kernel context switch */
1254
1255 ce = intel_context_create(i915->kernel_context, engine);
1256 if (IS_ERR(ce)) {
1257 err = PTR_ERR(ce);
1258 goto out;
1259 }
1260
Chris Wilson5e2a0412019-04-26 17:33:34 +01001261 rq = intel_context_create_request(ce);
Chris Wilsond2b4b972017-11-10 14:26:33 +00001262 if (IS_ERR(rq)) {
1263 err = PTR_ERR(rq);
Chris Wilson38775822019-08-08 12:06:11 +01001264 intel_context_put(ce);
1265 goto out;
Chris Wilsond2b4b972017-11-10 14:26:33 +00001266 }
1267
Chris Wilsona5627722019-07-29 12:37:20 +01001268 err = intel_engine_emit_ctx_wa(rq);
1269 if (err)
1270 goto err_rq;
Chris Wilsond2b4b972017-11-10 14:26:33 +00001271
Chris Wilsona5627722019-07-29 12:37:20 +01001272 /*
1273 * Failing to program the MOCS is non-fatal.The system will not
1274 * run at peak performance. So warn the user and carry on.
1275 */
1276 err = intel_mocs_emit(rq);
1277 if (err)
1278 dev_notice(i915->drm.dev,
1279 "Failed to program MOCS registers; expect performance issues.\n");
1280
1281 err = intel_renderstate_emit(rq);
1282 if (err)
1283 goto err_rq;
1284
1285err_rq:
Chris Wilson38775822019-08-08 12:06:11 +01001286 requests[id] = i915_request_get(rq);
Chris Wilson697b9a82018-06-12 11:51:35 +01001287 i915_request_add(rq);
Chris Wilsond2b4b972017-11-10 14:26:33 +00001288 if (err)
Chris Wilson38775822019-08-08 12:06:11 +01001289 goto out;
Chris Wilsond2b4b972017-11-10 14:26:33 +00001290 }
1291
Chris Wilson604c37d2019-03-08 09:36:55 +00001292 /* Flush the default context image to memory, and enable powersaving. */
Chris Wilson23c3c3d2019-04-24 21:07:14 +01001293 if (!i915_gem_load_power_context(i915)) {
Chris Wilson604c37d2019-03-08 09:36:55 +00001294 err = -EIO;
Chris Wilson38775822019-08-08 12:06:11 +01001295 goto out;
Chris Wilson2621cef2018-07-09 13:20:43 +01001296 }
Chris Wilsond2b4b972017-11-10 14:26:33 +00001297
Chris Wilson38775822019-08-08 12:06:11 +01001298 for (id = 0; id < ARRAY_SIZE(requests); id++) {
1299 struct i915_request *rq;
1300 struct i915_vma *state;
Chris Wilson37d7c9c2018-09-14 13:35:03 +01001301 void *vaddr;
Chris Wilsond2b4b972017-11-10 14:26:33 +00001302
Chris Wilson38775822019-08-08 12:06:11 +01001303 rq = requests[id];
1304 if (!rq)
Chris Wilsond2b4b972017-11-10 14:26:33 +00001305 continue;
1306
Chris Wilson38775822019-08-08 12:06:11 +01001307 /* We want to be able to unbind the state from the GGTT */
1308 GEM_BUG_ON(intel_context_is_pinned(rq->hw_context));
1309
1310 state = rq->hw_context->state;
1311 if (!state)
1312 continue;
Chris Wilsonc4d52fe2019-03-08 13:25:19 +00001313
Chris Wilsond2b4b972017-11-10 14:26:33 +00001314 /*
1315 * As we will hold a reference to the logical state, it will
1316 * not be torn down with the context, and importantly the
1317 * object will hold onto its vma (making it possible for a
1318 * stray GTT write to corrupt our defaults). Unmap the vma
1319 * from the GTT to prevent such accidents and reclaim the
1320 * space.
1321 */
1322 err = i915_vma_unbind(state);
1323 if (err)
Chris Wilson38775822019-08-08 12:06:11 +01001324 goto out;
Chris Wilsond2b4b972017-11-10 14:26:33 +00001325
Chris Wilson6951e582019-05-28 10:29:51 +01001326 i915_gem_object_lock(state->obj);
Chris Wilsond2b4b972017-11-10 14:26:33 +00001327 err = i915_gem_object_set_to_cpu_domain(state->obj, false);
Chris Wilson6951e582019-05-28 10:29:51 +01001328 i915_gem_object_unlock(state->obj);
Chris Wilsond2b4b972017-11-10 14:26:33 +00001329 if (err)
Chris Wilson38775822019-08-08 12:06:11 +01001330 goto out;
Chris Wilsond2b4b972017-11-10 14:26:33 +00001331
Chris Wilson38775822019-08-08 12:06:11 +01001332 i915_gem_object_set_cache_coherency(state->obj, I915_CACHE_LLC);
Chris Wilson37d7c9c2018-09-14 13:35:03 +01001333
1334 /* Check we can acquire the image of the context state */
Chris Wilson38775822019-08-08 12:06:11 +01001335 vaddr = i915_gem_object_pin_map(state->obj, I915_MAP_FORCE_WB);
Chris Wilson37d7c9c2018-09-14 13:35:03 +01001336 if (IS_ERR(vaddr)) {
1337 err = PTR_ERR(vaddr);
Chris Wilson38775822019-08-08 12:06:11 +01001338 goto out;
Chris Wilson37d7c9c2018-09-14 13:35:03 +01001339 }
1340
Chris Wilson38775822019-08-08 12:06:11 +01001341 rq->engine->default_state = i915_gem_object_get(state->obj);
1342 i915_gem_object_unpin_map(state->obj);
Chris Wilsond2b4b972017-11-10 14:26:33 +00001343 }
1344
Chris Wilson38775822019-08-08 12:06:11 +01001345out:
Chris Wilsond2b4b972017-11-10 14:26:33 +00001346 /*
1347 * If we have to abandon now, we expect the engines to be idle
Chris Wilson604c37d2019-03-08 09:36:55 +00001348 * and ready to be torn-down. The quickest way we can accomplish
1349 * this is by declaring ourselves wedged.
Chris Wilsond2b4b972017-11-10 14:26:33 +00001350 */
Chris Wilson38775822019-08-08 12:06:11 +01001351 if (err)
1352 intel_gt_set_wedged(&i915->gt);
1353
1354 for (id = 0; id < ARRAY_SIZE(requests); id++) {
1355 struct intel_context *ce;
1356 struct i915_request *rq;
1357
1358 rq = requests[id];
1359 if (!rq)
1360 continue;
1361
1362 ce = rq->hw_context;
1363 i915_request_put(rq);
1364 intel_context_put(ce);
1365 }
1366 return err;
Chris Wilsond2b4b972017-11-10 14:26:33 +00001367}
1368
Chris Wilson51797492018-12-04 14:15:16 +00001369static int
1370i915_gem_init_scratch(struct drm_i915_private *i915, unsigned int size)
1371{
Tvrtko Ursulindb56f972019-06-21 08:08:11 +01001372 return intel_gt_init_scratch(&i915->gt, size);
Chris Wilson51797492018-12-04 14:15:16 +00001373}
1374
1375static void i915_gem_fini_scratch(struct drm_i915_private *i915)
1376{
Tvrtko Ursulindb56f972019-06-21 08:08:11 +01001377 intel_gt_fini_scratch(&i915->gt);
Chris Wilson51797492018-12-04 14:15:16 +00001378}
1379
Chris Wilson254e1182019-04-17 08:56:28 +01001380static int intel_engines_verify_workarounds(struct drm_i915_private *i915)
1381{
1382 struct intel_engine_cs *engine;
1383 enum intel_engine_id id;
1384 int err = 0;
1385
1386 if (!IS_ENABLED(CONFIG_DRM_I915_DEBUG_GEM))
1387 return 0;
1388
1389 for_each_engine(engine, i915, id) {
1390 if (intel_engine_verify_workarounds(engine, "load"))
1391 err = -EIO;
1392 }
1393
1394 return err;
1395}
1396
Tvrtko Ursulinbf9e8422016-12-01 14:16:38 +00001397int i915_gem_init(struct drm_i915_private *dev_priv)
Chris Wilson1070a422012-04-24 15:47:41 +01001398{
Chris Wilson1070a422012-04-24 15:47:41 +01001399 int ret;
1400
Changbin Du52b24162018-05-08 17:07:05 +08001401 /* We need to fallback to 4K pages if host doesn't support huge gtt. */
1402 if (intel_vgpu_active(dev_priv) && !intel_vgpu_has_huge_gtt(dev_priv))
Matthew Auldda9fe3f32017-10-06 23:18:31 +01001403 mkwrite_device_info(dev_priv)->page_sizes =
1404 I915_GTT_PAGE_SIZE_4K;
1405
Tvrtko Ursulinf0c02c12019-06-21 08:08:10 +01001406 intel_timelines_init(dev_priv);
Chris Wilson1e345562019-01-28 10:23:56 +00001407
Chris Wilsonee487002017-11-22 17:26:21 +00001408 ret = i915_gem_init_userptr(dev_priv);
1409 if (ret)
1410 return ret;
1411
Daniele Ceraolo Spurioca7b2c12019-07-13 11:00:13 +01001412 intel_uc_fetch_firmwares(&dev_priv->gt.uc);
Michal Wajdeczko6bd0fbe2019-08-02 18:40:55 +00001413 intel_wopcm_init(&dev_priv->wopcm);
Michal Wajdeczkof7dc0152018-06-28 14:15:21 +00001414
Chris Wilson5e4f5182015-02-13 14:35:59 +00001415 /* This is just a security blanket to placate dragons.
1416 * On some systems, we very sporadically observe that the first TLBs
1417 * used by the CS may be stale, despite us poking the TLB reset. If
1418 * we hold the forcewake during initialisation these problems
1419 * just magically go away.
1420 */
Chris Wilsonee487002017-11-22 17:26:21 +00001421 mutex_lock(&dev_priv->drm.struct_mutex);
Daniele Ceraolo Spurio3ceea6a2019-03-19 11:35:36 -07001422 intel_uncore_forcewake_get(&dev_priv->uncore, FORCEWAKE_ALL);
Chris Wilson5e4f5182015-02-13 14:35:59 +00001423
Tvrtko Ursulin1d66377a2019-06-21 08:08:05 +01001424 ret = i915_init_ggtt(dev_priv);
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00001425 if (ret) {
1426 GEM_BUG_ON(ret == -EIO);
1427 goto err_unlock;
1428 }
Jesse Barnesd62b4892013-03-08 10:45:53 -08001429
Chris Wilson51797492018-12-04 14:15:16 +00001430 ret = i915_gem_init_scratch(dev_priv,
Lucas De Marchicf819ef2018-12-12 10:10:43 -08001431 IS_GEN(dev_priv, 2) ? SZ_256K : PAGE_SIZE);
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00001432 if (ret) {
1433 GEM_BUG_ON(ret == -EIO);
1434 goto err_ggtt;
1435 }
Ben Widawsky2fa48d82013-12-06 14:11:04 -08001436
Chris Wilson11334c62019-04-26 17:33:33 +01001437 ret = intel_engines_setup(dev_priv);
1438 if (ret) {
1439 GEM_BUG_ON(ret == -EIO);
1440 goto err_unlock;
1441 }
1442
Chris Wilson51797492018-12-04 14:15:16 +00001443 ret = i915_gem_contexts_init(dev_priv);
1444 if (ret) {
1445 GEM_BUG_ON(ret == -EIO);
1446 goto err_scratch;
1447 }
1448
Tvrtko Ursulinbf9e8422016-12-01 14:16:38 +00001449 ret = intel_engines_init(dev_priv);
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00001450 if (ret) {
1451 GEM_BUG_ON(ret == -EIO);
1452 goto err_context;
1453 }
Daniel Vetter53ca26c2012-04-26 23:28:03 +02001454
Chris Wilsonf58d13d2017-11-10 14:26:29 +00001455 intel_init_gt_powersave(dev_priv);
1456
Michal Wajdeczko0075a202019-08-17 13:11:44 +00001457 intel_uc_init(&dev_priv->gt.uc);
Chris Wilsoncc6a8182017-11-10 14:26:30 +00001458
Michał Winiarski61b5c152017-12-13 23:13:48 +01001459 ret = i915_gem_init_hw(dev_priv);
1460 if (ret)
1461 goto err_uc_init;
1462
Chris Wilson092be382019-06-26 16:45:49 +01001463 /* Only when the HW is re-initialised, can we replay the requests */
1464 ret = intel_gt_resume(&dev_priv->gt);
1465 if (ret)
1466 goto err_init_hw;
1467
Chris Wilsoncc6a8182017-11-10 14:26:30 +00001468 /*
1469 * Despite its name intel_init_clock_gating applies both display
1470 * clock gating workarounds; GT mmio workarounds and the occasional
1471 * GT power context workaround. Worse, sometimes it includes a context
1472 * register workaround which we need to apply before we record the
1473 * default HW state for all contexts.
1474 *
1475 * FIXME: break up the workarounds and apply them at the right time!
1476 */
1477 intel_init_clock_gating(dev_priv);
1478
Chris Wilson254e1182019-04-17 08:56:28 +01001479 ret = intel_engines_verify_workarounds(dev_priv);
1480 if (ret)
Chris Wilson092be382019-06-26 16:45:49 +01001481 goto err_gt;
Chris Wilson254e1182019-04-17 08:56:28 +01001482
Chris Wilsond2b4b972017-11-10 14:26:33 +00001483 ret = __intel_engines_record_defaults(dev_priv);
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00001484 if (ret)
Chris Wilson092be382019-06-26 16:45:49 +01001485 goto err_gt;
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00001486
Michal Wajdeczko50d84412019-08-02 18:40:50 +00001487 ret = i915_inject_load_error(dev_priv, -ENODEV);
1488 if (ret)
Chris Wilson092be382019-06-26 16:45:49 +01001489 goto err_gt;
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00001490
Michal Wajdeczko50d84412019-08-02 18:40:50 +00001491 ret = i915_inject_load_error(dev_priv, -EIO);
1492 if (ret)
Chris Wilson092be382019-06-26 16:45:49 +01001493 goto err_gt;
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00001494
Daniele Ceraolo Spurio3ceea6a2019-03-19 11:35:36 -07001495 intel_uncore_forcewake_put(&dev_priv->uncore, FORCEWAKE_ALL);
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00001496 mutex_unlock(&dev_priv->drm.struct_mutex);
1497
1498 return 0;
1499
1500 /*
1501 * Unwinding is complicated by that we want to handle -EIO to mean
1502 * disable GPU submission but keep KMS alive. We want to mark the
1503 * HW as irrevisibly wedged, but keep enough state around that the
1504 * driver doesn't explode during runtime.
1505 */
Chris Wilson092be382019-06-26 16:45:49 +01001506err_gt:
Chris Wilson8571a052018-06-06 15:54:41 +01001507 mutex_unlock(&dev_priv->drm.struct_mutex);
1508
Chris Wilsoncb823ed2019-07-12 20:29:53 +01001509 intel_gt_set_wedged(&dev_priv->gt);
Chris Wilson5861b012019-03-08 09:36:54 +00001510 i915_gem_suspend(dev_priv);
Chris Wilson8571a052018-06-06 15:54:41 +01001511 i915_gem_suspend_late(dev_priv);
1512
Chris Wilson8bcf9f72018-07-10 10:44:20 +01001513 i915_gem_drain_workqueue(dev_priv);
1514
Chris Wilson8571a052018-06-06 15:54:41 +01001515 mutex_lock(&dev_priv->drm.struct_mutex);
Chris Wilson092be382019-06-26 16:45:49 +01001516err_init_hw:
Daniele Ceraolo Spurioca7b2c12019-07-13 11:00:13 +01001517 intel_uc_fini_hw(&dev_priv->gt.uc);
Michał Winiarski61b5c152017-12-13 23:13:48 +01001518err_uc_init:
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00001519 if (ret != -EIO) {
Michal Wajdeczko0075a202019-08-17 13:11:44 +00001520 intel_uc_fini(&dev_priv->gt.uc);
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00001521 intel_cleanup_gt_powersave(dev_priv);
Chris Wilson45b9c962019-05-01 11:32:04 +01001522 intel_engines_cleanup(dev_priv);
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00001523 }
1524err_context:
1525 if (ret != -EIO)
1526 i915_gem_contexts_fini(dev_priv);
Chris Wilson51797492018-12-04 14:15:16 +00001527err_scratch:
1528 i915_gem_fini_scratch(dev_priv);
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00001529err_ggtt:
1530err_unlock:
Daniele Ceraolo Spurio3ceea6a2019-03-19 11:35:36 -07001531 intel_uncore_forcewake_put(&dev_priv->uncore, FORCEWAKE_ALL);
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00001532 mutex_unlock(&dev_priv->drm.struct_mutex);
1533
Chris Wilson1e345562019-01-28 10:23:56 +00001534 if (ret != -EIO) {
Michal Wajdeczkoa5f978c2019-08-11 19:51:32 +00001535 intel_uc_cleanup_firmwares(&dev_priv->gt.uc);
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00001536 i915_gem_cleanup_userptr(dev_priv);
Tvrtko Ursulinf0c02c12019-06-21 08:08:10 +01001537 intel_timelines_fini(dev_priv);
Chris Wilson1e345562019-01-28 10:23:56 +00001538 }
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00001539
Chris Wilson60990322014-04-09 09:19:42 +01001540 if (ret == -EIO) {
Chris Wilson7ed43df2018-07-26 09:50:32 +01001541 mutex_lock(&dev_priv->drm.struct_mutex);
1542
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00001543 /*
Michal Wajdeczkoa5f978c2019-08-11 19:51:32 +00001544 * Allow engines or uC initialisation to fail by marking the GPU
1545 * as wedged. But we only want to do this when the GPU is angry,
Chris Wilson60990322014-04-09 09:19:42 +01001546 * for all other failure, such as an allocation failure, bail.
1547 */
Chris Wilsoncb823ed2019-07-12 20:29:53 +01001548 if (!intel_gt_is_wedged(&dev_priv->gt)) {
Janusz Krzysztofikf2db53f2019-07-12 13:24:27 +02001549 i915_probe_error(dev_priv,
1550 "Failed to initialize GPU, declaring it wedged!\n");
Chris Wilsoncb823ed2019-07-12 20:29:53 +01001551 intel_gt_set_wedged(&dev_priv->gt);
Chris Wilson6f74b362017-10-15 15:37:25 +01001552 }
Chris Wilson7ed43df2018-07-26 09:50:32 +01001553
1554 /* Minimal basic recovery for KMS */
1555 ret = i915_ggtt_enable_hw(dev_priv);
1556 i915_gem_restore_gtt_mappings(dev_priv);
1557 i915_gem_restore_fences(dev_priv);
1558 intel_init_clock_gating(dev_priv);
1559
1560 mutex_unlock(&dev_priv->drm.struct_mutex);
Chris Wilson1070a422012-04-24 15:47:41 +01001561 }
1562
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00001563 i915_gem_drain_freed_objects(dev_priv);
Chris Wilson60990322014-04-09 09:19:42 +01001564 return ret;
Chris Wilson1070a422012-04-24 15:47:41 +01001565}
1566
Chris Wilsonc29579d2019-08-06 13:42:59 +01001567void i915_gem_driver_register(struct drm_i915_private *i915)
1568{
1569 i915_gem_driver_register__shrinker(i915);
Chris Wilson750e76b2019-08-06 13:43:00 +01001570
1571 intel_engines_driver_register(i915);
Chris Wilsonc29579d2019-08-06 13:42:59 +01001572}
1573
1574void i915_gem_driver_unregister(struct drm_i915_private *i915)
1575{
1576 i915_gem_driver_unregister__shrinker(i915);
1577}
1578
Janusz Krzysztofik78dae1a2019-07-12 13:24:29 +02001579void i915_gem_driver_remove(struct drm_i915_private *dev_priv)
Michal Wajdeczko8979187a2018-06-04 09:00:32 +00001580{
Chris Wilson79ffac852019-04-24 21:07:17 +01001581 GEM_BUG_ON(dev_priv->gt.awake);
1582
Chris Wilson0cf289b2019-06-13 08:32:54 +01001583 intel_wakeref_auto_fini(&dev_priv->ggtt.userfault_wakeref);
Chris Wilsonb27e35a2019-05-27 12:51:14 +01001584
Michal Wajdeczko8979187a2018-06-04 09:00:32 +00001585 i915_gem_suspend_late(dev_priv);
Chris Wilson30b710842018-08-12 23:36:29 +01001586 intel_disable_gt_powersave(dev_priv);
Michal Wajdeczko8979187a2018-06-04 09:00:32 +00001587
1588 /* Flush any outstanding unpin_work. */
1589 i915_gem_drain_workqueue(dev_priv);
1590
1591 mutex_lock(&dev_priv->drm.struct_mutex);
Daniele Ceraolo Spurioca7b2c12019-07-13 11:00:13 +01001592 intel_uc_fini_hw(&dev_priv->gt.uc);
1593 intel_uc_fini(&dev_priv->gt.uc);
Janusz Krzysztofik47bc28d2019-05-30 15:31:05 +02001594 mutex_unlock(&dev_priv->drm.struct_mutex);
1595
1596 i915_gem_drain_freed_objects(dev_priv);
1597}
1598
Janusz Krzysztofik3b58a942019-07-12 13:24:28 +02001599void i915_gem_driver_release(struct drm_i915_private *dev_priv)
Janusz Krzysztofik47bc28d2019-05-30 15:31:05 +02001600{
1601 mutex_lock(&dev_priv->drm.struct_mutex);
Chris Wilson45b9c962019-05-01 11:32:04 +01001602 intel_engines_cleanup(dev_priv);
Michal Wajdeczko8979187a2018-06-04 09:00:32 +00001603 i915_gem_contexts_fini(dev_priv);
Chris Wilson51797492018-12-04 14:15:16 +00001604 i915_gem_fini_scratch(dev_priv);
Michal Wajdeczko8979187a2018-06-04 09:00:32 +00001605 mutex_unlock(&dev_priv->drm.struct_mutex);
1606
Tvrtko Ursulin25d140f2018-12-03 13:33:19 +00001607 intel_wa_list_free(&dev_priv->gt_wa_list);
1608
Chris Wilson30b710842018-08-12 23:36:29 +01001609 intel_cleanup_gt_powersave(dev_priv);
1610
Daniele Ceraolo Spurioca7b2c12019-07-13 11:00:13 +01001611 intel_uc_cleanup_firmwares(&dev_priv->gt.uc);
Michal Wajdeczko8979187a2018-06-04 09:00:32 +00001612 i915_gem_cleanup_userptr(dev_priv);
Tvrtko Ursulinf0c02c12019-06-21 08:08:10 +01001613 intel_timelines_fini(dev_priv);
Michal Wajdeczko8979187a2018-06-04 09:00:32 +00001614
1615 i915_gem_drain_freed_objects(dev_priv);
1616
1617 WARN_ON(!list_empty(&dev_priv->contexts.list));
1618}
1619
Chris Wilson24145512017-01-24 11:01:35 +00001620void i915_gem_init_mmio(struct drm_i915_private *i915)
1621{
1622 i915_gem_sanitize(i915);
1623}
1624
Chris Wilson9c52d1c2017-11-10 23:24:47 +00001625static void i915_gem_init__mm(struct drm_i915_private *i915)
1626{
Chris Wilson9c52d1c2017-11-10 23:24:47 +00001627 spin_lock_init(&i915->mm.obj_lock);
Chris Wilson9c52d1c2017-11-10 23:24:47 +00001628
1629 init_llist_head(&i915->mm.free_list);
1630
Chris Wilson3b4fa962019-05-30 21:34:59 +01001631 INIT_LIST_HEAD(&i915->mm.purge_list);
Chris Wilsonecab9be2019-06-12 11:57:20 +01001632 INIT_LIST_HEAD(&i915->mm.shrink_list);
Chris Wilson9c52d1c2017-11-10 23:24:47 +00001633
Chris Wilson84753552019-05-28 10:29:45 +01001634 i915_gem_init__objects(i915);
Chris Wilson9c52d1c2017-11-10 23:24:47 +00001635}
1636
Michal Wajdeczkoa0de9082018-03-23 12:34:49 +00001637int i915_gem_init_early(struct drm_i915_private *dev_priv)
Eric Anholt673a3942008-07-30 12:06:12 -07001638{
Chris Wilson13f1bfd2019-02-28 10:20:34 +00001639 int err;
Chris Wilsond1b48c12017-08-16 09:52:08 +01001640
Chris Wilson9c52d1c2017-11-10 23:24:47 +00001641 i915_gem_init__mm(dev_priv);
Chris Wilson23c3c3d2019-04-24 21:07:14 +01001642 i915_gem_init__pm(dev_priv);
Chris Wilsonf2123812017-10-16 12:40:37 +01001643
Chris Wilsonb5add952016-08-04 16:32:36 +01001644 spin_lock_init(&dev_priv->fb_tracking.lock);
Chris Wilson73cb9702016-10-28 13:58:46 +01001645
Matthew Auld465c4032017-10-06 23:18:14 +01001646 err = i915_gemfs_init(dev_priv);
1647 if (err)
1648 DRM_NOTE("Unable to create a private tmpfs mount, hugepage support will be disabled(%d).\n", err);
1649
Chris Wilson73cb9702016-10-28 13:58:46 +01001650 return 0;
Eric Anholt673a3942008-07-30 12:06:12 -07001651}
Dave Airlie71acb5e2008-12-30 20:31:46 +10001652
Michal Wajdeczkoa0de9082018-03-23 12:34:49 +00001653void i915_gem_cleanup_early(struct drm_i915_private *dev_priv)
Imre Deakd64aa092016-01-19 15:26:29 +02001654{
Chris Wilsonc4d4c1c2017-02-10 16:35:23 +00001655 i915_gem_drain_freed_objects(dev_priv);
Chris Wilsonc9c704712018-02-19 22:06:31 +00001656 GEM_BUG_ON(!llist_empty(&dev_priv->mm.free_list));
1657 GEM_BUG_ON(atomic_read(&dev_priv->mm.free_count));
Chris Wilsond82b4b22019-05-30 21:35:00 +01001658 WARN_ON(dev_priv->mm.shrink_count);
Matthew Auldea84aa72016-11-17 21:04:11 +00001659
Matthew Auld465c4032017-10-06 23:18:14 +01001660 i915_gemfs_fini(dev_priv);
Imre Deakd64aa092016-01-19 15:26:29 +02001661}
1662
Chris Wilson6a800ea2016-09-21 14:51:07 +01001663int i915_gem_freeze(struct drm_i915_private *dev_priv)
1664{
Chris Wilsond0aa3012017-04-07 11:25:49 +01001665 /* Discard all purgeable objects, let userspace recover those as
1666 * required after resuming.
1667 */
Chris Wilson6a800ea2016-09-21 14:51:07 +01001668 i915_gem_shrink_all(dev_priv);
Chris Wilson6a800ea2016-09-21 14:51:07 +01001669
Chris Wilson6a800ea2016-09-21 14:51:07 +01001670 return 0;
1671}
1672
Chris Wilson95c778d2018-06-01 15:41:25 +01001673int i915_gem_freeze_late(struct drm_i915_private *i915)
Chris Wilson461fb992016-05-14 07:26:33 +01001674{
1675 struct drm_i915_gem_object *obj;
Chris Wilsonecab9be2019-06-12 11:57:20 +01001676 intel_wakeref_t wakeref;
Chris Wilson461fb992016-05-14 07:26:33 +01001677
Chris Wilson95c778d2018-06-01 15:41:25 +01001678 /*
1679 * Called just before we write the hibernation image.
Chris Wilson461fb992016-05-14 07:26:33 +01001680 *
1681 * We need to update the domain tracking to reflect that the CPU
1682 * will be accessing all the pages to create and restore from the
1683 * hibernation, and so upon restoration those pages will be in the
1684 * CPU domain.
1685 *
1686 * To make sure the hibernation image contains the latest state,
1687 * we update that state just before writing out the image.
Chris Wilson7aab2d52016-09-09 20:02:18 +01001688 *
1689 * To try and reduce the hibernation image, we manually shrink
Chris Wilsond0aa3012017-04-07 11:25:49 +01001690 * the objects as well, see i915_gem_freeze()
Chris Wilson461fb992016-05-14 07:26:33 +01001691 */
1692
Daniele Ceraolo Spuriod858d562019-06-13 16:21:54 -07001693 wakeref = intel_runtime_pm_get(&i915->runtime_pm);
Chris Wilsonecab9be2019-06-12 11:57:20 +01001694
1695 i915_gem_shrink(i915, -1UL, NULL, ~0);
Chris Wilson95c778d2018-06-01 15:41:25 +01001696 i915_gem_drain_freed_objects(i915);
Chris Wilson461fb992016-05-14 07:26:33 +01001697
Chris Wilsonecab9be2019-06-12 11:57:20 +01001698 list_for_each_entry(obj, &i915->mm.shrink_list, mm.link) {
1699 i915_gem_object_lock(obj);
1700 WARN_ON(i915_gem_object_set_to_cpu_domain(obj, true));
1701 i915_gem_object_unlock(obj);
Chris Wilson461fb992016-05-14 07:26:33 +01001702 }
Chris Wilsonecab9be2019-06-12 11:57:20 +01001703
Daniele Ceraolo Spuriod858d562019-06-13 16:21:54 -07001704 intel_runtime_pm_put(&i915->runtime_pm, wakeref);
Chris Wilson461fb992016-05-14 07:26:33 +01001705
1706 return 0;
1707}
1708
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001709void i915_gem_release(struct drm_device *dev, struct drm_file *file)
Eric Anholtb9624422009-06-03 07:27:35 +00001710{
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001711 struct drm_i915_file_private *file_priv = file->driver_priv;
Chris Wilsone61e0f52018-02-21 09:56:36 +00001712 struct i915_request *request;
Eric Anholtb9624422009-06-03 07:27:35 +00001713
1714 /* Clean up our request list when the client is going away, so that
1715 * later retire_requests won't dereference our soon-to-be-gone
1716 * file_priv.
1717 */
Chris Wilson1c255952010-09-26 11:03:27 +01001718 spin_lock(&file_priv->mm.lock);
Chris Wilsonc8659ef2017-03-02 12:25:25 +00001719 list_for_each_entry(request, &file_priv->mm.request_list, client_link)
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001720 request->file_priv = NULL;
Chris Wilson1c255952010-09-26 11:03:27 +01001721 spin_unlock(&file_priv->mm.lock);
Chris Wilsonb29c19b2013-09-25 17:34:56 +01001722}
1723
Chris Wilson829a0af2017-06-20 12:05:45 +01001724int i915_gem_open(struct drm_i915_private *i915, struct drm_file *file)
Chris Wilsonb29c19b2013-09-25 17:34:56 +01001725{
1726 struct drm_i915_file_private *file_priv;
Ben Widawskye422b882013-12-06 14:10:58 -08001727 int ret;
Chris Wilsonb29c19b2013-09-25 17:34:56 +01001728
Chris Wilsonc4c29d72016-11-09 10:45:07 +00001729 DRM_DEBUG("\n");
Chris Wilsonb29c19b2013-09-25 17:34:56 +01001730
1731 file_priv = kzalloc(sizeof(*file_priv), GFP_KERNEL);
1732 if (!file_priv)
1733 return -ENOMEM;
1734
1735 file->driver_priv = file_priv;
Chris Wilson829a0af2017-06-20 12:05:45 +01001736 file_priv->dev_priv = i915;
Chris Wilsonab0e7ff2014-02-25 17:11:24 +02001737 file_priv->file = file;
Chris Wilsonb29c19b2013-09-25 17:34:56 +01001738
1739 spin_lock_init(&file_priv->mm.lock);
1740 INIT_LIST_HEAD(&file_priv->mm.request_list);
Chris Wilsonb29c19b2013-09-25 17:34:56 +01001741
Chris Wilsonc80ff162016-07-27 09:07:27 +01001742 file_priv->bsd_engine = -1;
Mika Kuoppala14921f32018-06-15 13:44:29 +03001743 file_priv->hang_timestamp = jiffies;
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00001744
Chris Wilson829a0af2017-06-20 12:05:45 +01001745 ret = i915_gem_context_open(i915, file);
Ben Widawskye422b882013-12-06 14:10:58 -08001746 if (ret)
1747 kfree(file_priv);
Chris Wilsonb29c19b2013-09-25 17:34:56 +01001748
Ben Widawskye422b882013-12-06 14:10:58 -08001749 return ret;
Chris Wilsonb29c19b2013-09-25 17:34:56 +01001750}
1751
Chris Wilson935a2f72017-02-13 17:15:13 +00001752#if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
Chris Wilson66d9cb52017-02-13 17:15:17 +00001753#include "selftests/mock_gem_device.c"
Chris Wilson3f51b7e12018-08-30 14:48:06 +01001754#include "selftests/i915_gem.c"
Chris Wilson935a2f72017-02-13 17:15:13 +00001755#endif