blob: 4752a3bf963611e45a180d927f77ae85fabfc1e7 [file] [log] [blame]
Eric Anholt673a3942008-07-30 12:06:12 -07001/*
Daniel Vetterbe6a0372015-03-18 10:46:04 +01002 * Copyright © 2008-2015 Intel Corporation
Eric Anholt673a3942008-07-30 12:06:12 -07003 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 *
23 * Authors:
24 * Eric Anholt <eric@anholt.net>
25 *
26 */
27
David Herrmann0de23972013-07-24 21:07:52 +020028#include <drm/drm_vma_manager.h>
David Howells760285e2012-10-02 18:01:07 +010029#include <drm/i915_drm.h>
Chris Wilson6b5e90f2016-11-14 20:41:05 +000030#include <linux/dma-fence-array.h>
Chris Wilsonfe3288b2017-02-12 17:20:01 +000031#include <linux/kthread.h>
Chris Wilsonc13d87e2016-07-20 09:21:15 +010032#include <linux/reservation.h>
Hugh Dickins5949eac2011-06-27 16:18:18 -070033#include <linux/shmem_fs.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090034#include <linux/slab.h>
Chris Wilson20e49332016-11-22 14:41:21 +000035#include <linux/stop_machine.h>
Eric Anholt673a3942008-07-30 12:06:12 -070036#include <linux/swap.h>
Jesse Barnes79e53942008-11-07 14:24:08 -080037#include <linux/pci.h>
Daniel Vetter1286ff72012-05-10 15:25:09 +020038#include <linux/dma-buf.h>
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
142 /* We manually control the domain here and pretend that it
143 * remains coherent i.e. in the GTT domain, like shmem_pwrite.
144 */
Rodrigo Vivi77a0d1c2015-06-18 11:43:24 -0700145 intel_fb_obj_invalidate(obj, ORIGIN_CPU);
Chris Wilson10466d22017-01-06 15:22:38 +0000146 if (copy_from_user(vaddr, user_data, args->size))
147 return -EFAULT;
Chris Wilson00731152014-05-21 12:42:56 +0100148
Chris Wilson6a2c4232014-11-04 04:51:40 -0800149 drm_clflush_virt_range(vaddr, args->size);
Tvrtko Ursulinbaea4292019-06-21 08:08:02 +0100150 intel_gt_chipset_flush(&to_i915(obj->base.dev)->gt);
Paulo Zanoni063e4e62015-02-13 17:23:45 -0200151
Chris Wilsond59b21e2017-02-22 11:40:49 +0000152 intel_fb_obj_flush(obj, ORIGIN_CPU);
Chris Wilson10466d22017-01-06 15:22:38 +0000153 return 0;
Chris Wilson00731152014-05-21 12:42:56 +0100154}
155
Dave Airlieff72145b2011-02-07 12:16:14 +1000156static int
157i915_gem_create(struct drm_file *file,
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +0000158 struct drm_i915_private *dev_priv,
Michał Winiarskie1634842019-03-26 18:02:18 +0100159 u64 *size_p,
Jani Nikula739f3ab2019-01-16 11:15:19 +0200160 u32 *handle_p)
Eric Anholt673a3942008-07-30 12:06:12 -0700161{
Chris Wilson05394f32010-11-08 19:18:58 +0000162 struct drm_i915_gem_object *obj;
Pekka Paalanena1a2d1d2009-08-23 12:40:55 +0300163 u32 handle;
Michał Winiarskie1634842019-03-26 18:02:18 +0100164 u64 size;
165 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700166
Michał Winiarskie1634842019-03-26 18:02:18 +0100167 size = round_up(*size_p, PAGE_SIZE);
Chris Wilson8ffc0242011-09-14 14:14:28 +0200168 if (size == 0)
169 return -EINVAL;
Eric Anholt673a3942008-07-30 12:06:12 -0700170
171 /* Allocate the new object */
Chris Wilson84753552019-05-28 10:29:45 +0100172 obj = i915_gem_object_create_shmem(dev_priv, size);
Chris Wilsonfe3db792016-04-25 13:32:13 +0100173 if (IS_ERR(obj))
174 return PTR_ERR(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700175
Chris Wilson05394f32010-11-08 19:18:58 +0000176 ret = drm_gem_handle_create(file, &obj->base, &handle);
Chris Wilson202f2fe2010-10-14 13:20:40 +0100177 /* drop reference from allocate - handle holds it now */
Chris Wilsonf0cd5182016-10-28 13:58:43 +0100178 i915_gem_object_put(obj);
Daniel Vetterd861e332013-07-24 23:25:03 +0200179 if (ret)
180 return ret;
Chris Wilson202f2fe2010-10-14 13:20:40 +0100181
Dave Airlieff72145b2011-02-07 12:16:14 +1000182 *handle_p = handle;
Chris Wilson99534022019-04-17 14:25:07 +0100183 *size_p = size;
Eric Anholt673a3942008-07-30 12:06:12 -0700184 return 0;
185}
186
Dave Airlieff72145b2011-02-07 12:16:14 +1000187int
188i915_gem_dumb_create(struct drm_file *file,
189 struct drm_device *dev,
190 struct drm_mode_create_dumb *args)
191{
Ville Syrjäläaa5ca8b2019-05-09 15:21:57 +0300192 int cpp = DIV_ROUND_UP(args->bpp, 8);
193 u32 format;
194
195 switch (cpp) {
196 case 1:
197 format = DRM_FORMAT_C8;
198 break;
199 case 2:
200 format = DRM_FORMAT_RGB565;
201 break;
202 case 4:
203 format = DRM_FORMAT_XRGB8888;
204 break;
205 default:
206 return -EINVAL;
207 }
208
Dave Airlieff72145b2011-02-07 12:16:14 +1000209 /* have to work out size/pitch and return them */
Ville Syrjäläaa5ca8b2019-05-09 15:21:57 +0300210 args->pitch = ALIGN(args->width * cpp, 64);
211
212 /* align stride to page size so that we can remap */
213 if (args->pitch > intel_plane_fb_max_stride(to_i915(dev), format,
214 DRM_FORMAT_MOD_LINEAR))
215 args->pitch = ALIGN(args->pitch, 4096);
216
Dave Airlieff72145b2011-02-07 12:16:14 +1000217 args->size = args->pitch * args->height;
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +0000218 return i915_gem_create(file, to_i915(dev),
Michał Winiarskie1634842019-03-26 18:02:18 +0100219 &args->size, &args->handle);
Dave Airlieff72145b2011-02-07 12:16:14 +1000220}
221
Dave Airlieff72145b2011-02-07 12:16:14 +1000222/**
223 * Creates a new mm object and returns a handle to it.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +0100224 * @dev: drm device pointer
225 * @data: ioctl data blob
226 * @file: drm file pointer
Dave Airlieff72145b2011-02-07 12:16:14 +1000227 */
228int
229i915_gem_create_ioctl(struct drm_device *dev, void *data,
230 struct drm_file *file)
231{
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +0000232 struct drm_i915_private *dev_priv = to_i915(dev);
Dave Airlieff72145b2011-02-07 12:16:14 +1000233 struct drm_i915_gem_create *args = data;
Daniel Vetter63ed2cb2012-04-23 16:50:50 +0200234
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +0000235 i915_gem_flush_free_objects(dev_priv);
Chris Wilsonfbbd37b2016-10-28 13:58:42 +0100236
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +0000237 return i915_gem_create(file, dev_priv,
Michał Winiarskie1634842019-03-26 18:02:18 +0100238 &args->size, &args->handle);
Dave Airlieff72145b2011-02-07 12:16:14 +1000239}
240
Daniel Vetterd174bd62012-03-25 19:47:40 +0200241static int
Chris Wilsonb9d126e2019-01-05 12:07:58 +0000242shmem_pread(struct page *page, int offset, int len, char __user *user_data,
243 bool needs_clflush)
Daniel Vetterd174bd62012-03-25 19:47:40 +0200244{
245 char *vaddr;
246 int ret;
247
248 vaddr = kmap(page);
Daniel Vetterd174bd62012-03-25 19:47:40 +0200249
Chris Wilsonb9d126e2019-01-05 12:07:58 +0000250 if (needs_clflush)
251 drm_clflush_virt_range(vaddr + offset, len);
252
253 ret = __copy_to_user(user_data, vaddr + offset, len);
254
Daniel Vetterd174bd62012-03-25 19:47:40 +0200255 kunmap(page);
256
Chris Wilsonb9d126e2019-01-05 12:07:58 +0000257 return ret ? -EFAULT : 0;
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100258}
259
260static int
261i915_gem_shmem_pread(struct drm_i915_gem_object *obj,
262 struct drm_i915_gem_pread *args)
263{
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100264 unsigned int needs_clflush;
265 unsigned int idx, offset;
Chris Wilson6951e582019-05-28 10:29:51 +0100266 struct dma_fence *fence;
267 char __user *user_data;
268 u64 remain;
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100269 int ret;
270
Chris Wilson6951e582019-05-28 10:29:51 +0100271 ret = i915_gem_object_prepare_read(obj, &needs_clflush);
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100272 if (ret)
273 return ret;
274
Chris Wilson6951e582019-05-28 10:29:51 +0100275 fence = i915_gem_object_lock_fence(obj);
276 i915_gem_object_finish_access(obj);
277 if (!fence)
278 return -ENOMEM;
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100279
280 remain = args->size;
281 user_data = u64_to_user_ptr(args->data_ptr);
282 offset = offset_in_page(args->offset);
283 for (idx = args->offset >> PAGE_SHIFT; remain; idx++) {
284 struct page *page = i915_gem_object_get_page(obj, idx);
Chris Wilsona5e856a52018-10-12 15:02:28 +0100285 unsigned int length = min_t(u64, remain, PAGE_SIZE - offset);
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100286
287 ret = shmem_pread(page, offset, length, user_data,
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100288 needs_clflush);
289 if (ret)
290 break;
291
292 remain -= length;
293 user_data += length;
294 offset = 0;
295 }
296
Chris Wilson6951e582019-05-28 10:29:51 +0100297 i915_gem_object_unlock_fence(obj, fence);
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100298 return ret;
299}
300
301static inline bool
302gtt_user_read(struct io_mapping *mapping,
303 loff_t base, int offset,
304 char __user *user_data, int length)
305{
Ville Syrjäläafe722b2017-09-01 20:12:52 +0300306 void __iomem *vaddr;
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100307 unsigned long unwritten;
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +0530308
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +0530309 /* We can use the cpu mem copy function because this is X86. */
Ville Syrjäläafe722b2017-09-01 20:12:52 +0300310 vaddr = io_mapping_map_atomic_wc(mapping, base);
311 unwritten = __copy_to_user_inatomic(user_data,
312 (void __force *)vaddr + offset,
313 length);
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100314 io_mapping_unmap_atomic(vaddr);
315 if (unwritten) {
Ville Syrjäläafe722b2017-09-01 20:12:52 +0300316 vaddr = io_mapping_map_wc(mapping, base, PAGE_SIZE);
317 unwritten = copy_to_user(user_data,
318 (void __force *)vaddr + offset,
319 length);
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100320 io_mapping_unmap(vaddr);
321 }
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +0530322 return unwritten;
323}
324
325static int
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100326i915_gem_gtt_pread(struct drm_i915_gem_object *obj,
327 const struct drm_i915_gem_pread *args)
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +0530328{
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100329 struct drm_i915_private *i915 = to_i915(obj->base.dev);
330 struct i915_ggtt *ggtt = &i915->ggtt;
Chris Wilson538ef962019-01-14 14:21:18 +0000331 intel_wakeref_t wakeref;
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +0530332 struct drm_mm_node node;
Chris Wilson6951e582019-05-28 10:29:51 +0100333 struct dma_fence *fence;
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100334 void __user *user_data;
Chris Wilson6951e582019-05-28 10:29:51 +0100335 struct i915_vma *vma;
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100336 u64 remain, offset;
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +0530337 int ret;
338
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100339 ret = mutex_lock_interruptible(&i915->drm.struct_mutex);
340 if (ret)
341 return ret;
342
Daniele Ceraolo Spuriod858d562019-06-13 16:21:54 -0700343 wakeref = intel_runtime_pm_get(&i915->runtime_pm);
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100344 vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0,
Chris Wilsona3259ca2017-10-09 09:44:00 +0100345 PIN_MAPPABLE |
346 PIN_NONFAULT |
347 PIN_NONBLOCK);
Chris Wilson18034582016-08-18 17:16:45 +0100348 if (!IS_ERR(vma)) {
349 node.start = i915_ggtt_offset(vma);
350 node.allocated = false;
Chris Wilson49ef5292016-08-18 17:17:00 +0100351 ret = i915_vma_put_fence(vma);
Chris Wilson18034582016-08-18 17:16:45 +0100352 if (ret) {
353 i915_vma_unpin(vma);
354 vma = ERR_PTR(ret);
355 }
356 }
Chris Wilson058d88c2016-08-15 10:49:06 +0100357 if (IS_ERR(vma)) {
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100358 ret = insert_mappable_node(ggtt, &node, PAGE_SIZE);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +0530359 if (ret)
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100360 goto out_unlock;
361 GEM_BUG_ON(!node.allocated);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +0530362 }
363
Chris Wilson6951e582019-05-28 10:29:51 +0100364 mutex_unlock(&i915->drm.struct_mutex);
365
366 ret = i915_gem_object_lock_interruptible(obj);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +0530367 if (ret)
368 goto out_unpin;
369
Chris Wilson6951e582019-05-28 10:29:51 +0100370 ret = i915_gem_object_set_to_gtt_domain(obj, false);
371 if (ret) {
372 i915_gem_object_unlock(obj);
373 goto out_unpin;
374 }
375
376 fence = i915_gem_object_lock_fence(obj);
377 i915_gem_object_unlock(obj);
378 if (!fence) {
379 ret = -ENOMEM;
380 goto out_unpin;
381 }
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +0530382
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100383 user_data = u64_to_user_ptr(args->data_ptr);
384 remain = args->size;
385 offset = args->offset;
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +0530386
387 while (remain > 0) {
388 /* Operation in this page
389 *
390 * page_base = page offset within aperture
391 * page_offset = offset within page
392 * page_length = bytes to copy for this page
393 */
394 u32 page_base = node.start;
395 unsigned page_offset = offset_in_page(offset);
396 unsigned page_length = PAGE_SIZE - page_offset;
397 page_length = remain < page_length ? remain : page_length;
398 if (node.allocated) {
Chris Wilson82ad6442018-06-05 16:37:58 +0100399 ggtt->vm.insert_page(&ggtt->vm,
400 i915_gem_object_get_dma_address(obj, offset >> PAGE_SHIFT),
401 node.start, I915_CACHE_NONE, 0);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +0530402 } else {
403 page_base += offset & PAGE_MASK;
404 }
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100405
Matthew Auld73ebd502017-12-11 15:18:20 +0000406 if (gtt_user_read(&ggtt->iomap, page_base, page_offset,
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100407 user_data, page_length)) {
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +0530408 ret = -EFAULT;
409 break;
410 }
411
412 remain -= page_length;
413 user_data += page_length;
414 offset += page_length;
415 }
416
Chris Wilson6951e582019-05-28 10:29:51 +0100417 i915_gem_object_unlock_fence(obj, fence);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +0530418out_unpin:
Chris Wilson6951e582019-05-28 10:29:51 +0100419 mutex_lock(&i915->drm.struct_mutex);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +0530420 if (node.allocated) {
Chris Wilson82ad6442018-06-05 16:37:58 +0100421 ggtt->vm.clear_range(&ggtt->vm, node.start, node.size);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +0530422 remove_mappable_node(&node);
423 } else {
Chris Wilson058d88c2016-08-15 10:49:06 +0100424 i915_vma_unpin(vma);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +0530425 }
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100426out_unlock:
Daniele Ceraolo Spuriod858d562019-06-13 16:21:54 -0700427 intel_runtime_pm_put(&i915->runtime_pm, wakeref);
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100428 mutex_unlock(&i915->drm.struct_mutex);
Chris Wilsonf60d7f02012-09-04 21:02:56 +0100429
Eric Anholteb014592009-03-10 11:44:52 -0700430 return ret;
431}
432
Eric Anholt673a3942008-07-30 12:06:12 -0700433/**
434 * Reads data from the object referenced by handle.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +0100435 * @dev: drm device pointer
436 * @data: ioctl data blob
437 * @file: drm file pointer
Eric Anholt673a3942008-07-30 12:06:12 -0700438 *
439 * On error, the contents of *data are undefined.
440 */
441int
442i915_gem_pread_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +0000443 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -0700444{
445 struct drm_i915_gem_pread *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +0000446 struct drm_i915_gem_object *obj;
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100447 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700448
Chris Wilson51311d02010-11-17 09:10:42 +0000449 if (args->size == 0)
450 return 0;
451
Linus Torvalds96d4f262019-01-03 18:57:57 -0800452 if (!access_ok(u64_to_user_ptr(args->data_ptr),
Chris Wilson51311d02010-11-17 09:10:42 +0000453 args->size))
454 return -EFAULT;
455
Chris Wilson03ac0642016-07-20 13:31:51 +0100456 obj = i915_gem_object_lookup(file, args->handle);
Chris Wilson258a5ed2016-08-05 10:14:16 +0100457 if (!obj)
458 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -0700459
Chris Wilson7dcd2492010-09-26 20:21:44 +0100460 /* Bounds check source. */
Matthew Auld966d5bf2016-12-13 20:32:22 +0000461 if (range_overflows_t(u64, args->offset, args->size, obj->base.size)) {
Chris Wilsonce9d4192010-09-26 20:50:05 +0100462 ret = -EINVAL;
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100463 goto out;
Chris Wilsonce9d4192010-09-26 20:50:05 +0100464 }
465
Chris Wilsondb53a302011-02-03 11:57:46 +0000466 trace_i915_gem_object_pread(obj, args->offset, args->size);
467
Chris Wilsone95433c2016-10-28 13:58:27 +0100468 ret = i915_gem_object_wait(obj,
469 I915_WAIT_INTERRUPTIBLE,
Chris Wilson62eb3c22019-02-13 09:25:04 +0000470 MAX_SCHEDULE_TIMEOUT);
Chris Wilson258a5ed2016-08-05 10:14:16 +0100471 if (ret)
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100472 goto out;
Chris Wilson258a5ed2016-08-05 10:14:16 +0100473
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100474 ret = i915_gem_object_pin_pages(obj);
Chris Wilson258a5ed2016-08-05 10:14:16 +0100475 if (ret)
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100476 goto out;
Chris Wilson258a5ed2016-08-05 10:14:16 +0100477
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100478 ret = i915_gem_shmem_pread(obj, args);
Chris Wilson9c870d02016-10-24 13:42:15 +0100479 if (ret == -EFAULT || ret == -ENODEV)
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100480 ret = i915_gem_gtt_pread(obj, args);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +0530481
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100482 i915_gem_object_unpin_pages(obj);
483out:
Chris Wilsonf0cd5182016-10-28 13:58:43 +0100484 i915_gem_object_put(obj);
Eric Anholteb014592009-03-10 11:44:52 -0700485 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700486}
487
Keith Packard0839ccb2008-10-30 19:38:48 -0700488/* This is the fast write path which cannot handle
489 * page faults in the source data
Linus Torvalds9b7530cc2008-10-20 14:16:43 -0700490 */
Linus Torvalds9b7530cc2008-10-20 14:16:43 -0700491
Chris Wilsonfe115622016-10-28 13:58:40 +0100492static inline bool
493ggtt_write(struct io_mapping *mapping,
494 loff_t base, int offset,
495 char __user *user_data, int length)
Keith Packard0839ccb2008-10-30 19:38:48 -0700496{
Ville Syrjäläafe722b2017-09-01 20:12:52 +0300497 void __iomem *vaddr;
Keith Packard0839ccb2008-10-30 19:38:48 -0700498 unsigned long unwritten;
499
Ben Widawsky4f0c7cf2012-04-16 14:07:47 -0700500 /* We can use the cpu mem copy function because this is X86. */
Ville Syrjäläafe722b2017-09-01 20:12:52 +0300501 vaddr = io_mapping_map_atomic_wc(mapping, base);
502 unwritten = __copy_from_user_inatomic_nocache((void __force *)vaddr + offset,
Keith Packard0839ccb2008-10-30 19:38:48 -0700503 user_data, length);
Chris Wilsonfe115622016-10-28 13:58:40 +0100504 io_mapping_unmap_atomic(vaddr);
505 if (unwritten) {
Ville Syrjäläafe722b2017-09-01 20:12:52 +0300506 vaddr = io_mapping_map_wc(mapping, base, PAGE_SIZE);
507 unwritten = copy_from_user((void __force *)vaddr + offset,
508 user_data, length);
Chris Wilsonfe115622016-10-28 13:58:40 +0100509 io_mapping_unmap(vaddr);
510 }
Keith Packard0839ccb2008-10-30 19:38:48 -0700511
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100512 return unwritten;
513}
514
Eric Anholt3de09aa2009-03-09 09:42:23 -0700515/**
516 * This is the fast pwrite path, where we copy the data directly from the
517 * user into the GTT, uncached.
Chris Wilsonfe115622016-10-28 13:58:40 +0100518 * @obj: i915 GEM object
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +0100519 * @args: pwrite arguments structure
Eric Anholt3de09aa2009-03-09 09:42:23 -0700520 */
Eric Anholt673a3942008-07-30 12:06:12 -0700521static int
Chris Wilsonfe115622016-10-28 13:58:40 +0100522i915_gem_gtt_pwrite_fast(struct drm_i915_gem_object *obj,
523 const struct drm_i915_gem_pwrite *args)
Eric Anholt673a3942008-07-30 12:06:12 -0700524{
Chris Wilsonfe115622016-10-28 13:58:40 +0100525 struct drm_i915_private *i915 = to_i915(obj->base.dev);
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +0530526 struct i915_ggtt *ggtt = &i915->ggtt;
Daniele Ceraolo Spuriod858d562019-06-13 16:21:54 -0700527 struct intel_runtime_pm *rpm = &i915->runtime_pm;
Chris Wilson538ef962019-01-14 14:21:18 +0000528 intel_wakeref_t wakeref;
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +0530529 struct drm_mm_node node;
Chris Wilson6951e582019-05-28 10:29:51 +0100530 struct dma_fence *fence;
Chris Wilsonfe115622016-10-28 13:58:40 +0100531 struct i915_vma *vma;
532 u64 remain, offset;
533 void __user *user_data;
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +0530534 int ret;
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +0530535
Chris Wilsonfe115622016-10-28 13:58:40 +0100536 ret = mutex_lock_interruptible(&i915->drm.struct_mutex);
537 if (ret)
538 return ret;
Daniel Vetter935aaa62012-03-25 19:47:35 +0200539
Chris Wilson8bd818152017-10-19 07:37:33 +0100540 if (i915_gem_object_has_struct_page(obj)) {
541 /*
542 * Avoid waking the device up if we can fallback, as
543 * waking/resuming is very slow (worst-case 10-100 ms
544 * depending on PCI sleeps and our own resume time).
545 * This easily dwarfs any performance advantage from
546 * using the cache bypass of indirect GGTT access.
547 */
Daniele Ceraolo Spuriod858d562019-06-13 16:21:54 -0700548 wakeref = intel_runtime_pm_get_if_in_use(rpm);
Chris Wilson538ef962019-01-14 14:21:18 +0000549 if (!wakeref) {
Chris Wilson8bd818152017-10-19 07:37:33 +0100550 ret = -EFAULT;
551 goto out_unlock;
552 }
553 } else {
554 /* No backing pages, no fallback, we must force GGTT access */
Daniele Ceraolo Spuriod858d562019-06-13 16:21:54 -0700555 wakeref = intel_runtime_pm_get(rpm);
Chris Wilson8bd818152017-10-19 07:37:33 +0100556 }
557
Chris Wilson058d88c2016-08-15 10:49:06 +0100558 vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0,
Chris Wilsona3259ca2017-10-09 09:44:00 +0100559 PIN_MAPPABLE |
560 PIN_NONFAULT |
561 PIN_NONBLOCK);
Chris Wilson18034582016-08-18 17:16:45 +0100562 if (!IS_ERR(vma)) {
563 node.start = i915_ggtt_offset(vma);
564 node.allocated = false;
Chris Wilson49ef5292016-08-18 17:17:00 +0100565 ret = i915_vma_put_fence(vma);
Chris Wilson18034582016-08-18 17:16:45 +0100566 if (ret) {
567 i915_vma_unpin(vma);
568 vma = ERR_PTR(ret);
569 }
570 }
Chris Wilson058d88c2016-08-15 10:49:06 +0100571 if (IS_ERR(vma)) {
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100572 ret = insert_mappable_node(ggtt, &node, PAGE_SIZE);
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +0530573 if (ret)
Chris Wilson8bd818152017-10-19 07:37:33 +0100574 goto out_rpm;
Chris Wilsonfe115622016-10-28 13:58:40 +0100575 GEM_BUG_ON(!node.allocated);
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +0530576 }
Daniel Vetter935aaa62012-03-25 19:47:35 +0200577
Chris Wilson6951e582019-05-28 10:29:51 +0100578 mutex_unlock(&i915->drm.struct_mutex);
579
580 ret = i915_gem_object_lock_interruptible(obj);
Daniel Vetter935aaa62012-03-25 19:47:35 +0200581 if (ret)
582 goto out_unpin;
583
Chris Wilson6951e582019-05-28 10:29:51 +0100584 ret = i915_gem_object_set_to_gtt_domain(obj, true);
585 if (ret) {
586 i915_gem_object_unlock(obj);
587 goto out_unpin;
588 }
589
590 fence = i915_gem_object_lock_fence(obj);
591 i915_gem_object_unlock(obj);
592 if (!fence) {
593 ret = -ENOMEM;
594 goto out_unpin;
595 }
Chris Wilsonfe115622016-10-28 13:58:40 +0100596
Chris Wilsonb19482d2016-08-18 17:16:43 +0100597 intel_fb_obj_invalidate(obj, ORIGIN_CPU);
Paulo Zanoni063e4e62015-02-13 17:23:45 -0200598
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +0530599 user_data = u64_to_user_ptr(args->data_ptr);
600 offset = args->offset;
601 remain = args->size;
602 while (remain) {
Eric Anholt673a3942008-07-30 12:06:12 -0700603 /* Operation in this page
604 *
Keith Packard0839ccb2008-10-30 19:38:48 -0700605 * page_base = page offset within aperture
606 * page_offset = offset within page
607 * page_length = bytes to copy for this page
Eric Anholt673a3942008-07-30 12:06:12 -0700608 */
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +0530609 u32 page_base = node.start;
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100610 unsigned int page_offset = offset_in_page(offset);
611 unsigned int page_length = PAGE_SIZE - page_offset;
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +0530612 page_length = remain < page_length ? remain : page_length;
613 if (node.allocated) {
Chris Wilsonbdae33b2019-07-18 15:54:05 +0100614 /* flush the write before we modify the GGTT */
615 intel_gt_flush_ggtt_writes(ggtt->vm.gt);
Chris Wilson82ad6442018-06-05 16:37:58 +0100616 ggtt->vm.insert_page(&ggtt->vm,
617 i915_gem_object_get_dma_address(obj, offset >> PAGE_SHIFT),
618 node.start, I915_CACHE_NONE, 0);
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +0530619 wmb(); /* flush modifications to the GGTT (insert_page) */
620 } else {
621 page_base += offset & PAGE_MASK;
622 }
Keith Packard0839ccb2008-10-30 19:38:48 -0700623 /* If we get a fault while copying data, then (presumably) our
Eric Anholt3de09aa2009-03-09 09:42:23 -0700624 * source page isn't available. Return the error and we'll
625 * retry in the slow path.
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +0530626 * If the object is non-shmem backed, we retry again with the
627 * path that handles page fault.
Keith Packard0839ccb2008-10-30 19:38:48 -0700628 */
Matthew Auld73ebd502017-12-11 15:18:20 +0000629 if (ggtt_write(&ggtt->iomap, page_base, page_offset,
Chris Wilsonfe115622016-10-28 13:58:40 +0100630 user_data, page_length)) {
631 ret = -EFAULT;
632 break;
Daniel Vetter935aaa62012-03-25 19:47:35 +0200633 }
Eric Anholt673a3942008-07-30 12:06:12 -0700634
Keith Packard0839ccb2008-10-30 19:38:48 -0700635 remain -= page_length;
636 user_data += page_length;
637 offset += page_length;
Eric Anholt673a3942008-07-30 12:06:12 -0700638 }
Chris Wilsond59b21e2017-02-22 11:40:49 +0000639 intel_fb_obj_flush(obj, ORIGIN_CPU);
Chris Wilsonfe115622016-10-28 13:58:40 +0100640
Chris Wilson6951e582019-05-28 10:29:51 +0100641 i915_gem_object_unlock_fence(obj, fence);
Daniel Vetter935aaa62012-03-25 19:47:35 +0200642out_unpin:
Chris Wilson6951e582019-05-28 10:29:51 +0100643 mutex_lock(&i915->drm.struct_mutex);
Chris Wilsonbdae33b2019-07-18 15:54:05 +0100644 intel_gt_flush_ggtt_writes(ggtt->vm.gt);
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +0530645 if (node.allocated) {
Chris Wilson82ad6442018-06-05 16:37:58 +0100646 ggtt->vm.clear_range(&ggtt->vm, node.start, node.size);
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +0530647 remove_mappable_node(&node);
648 } else {
Chris Wilson058d88c2016-08-15 10:49:06 +0100649 i915_vma_unpin(vma);
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +0530650 }
Chris Wilson8bd818152017-10-19 07:37:33 +0100651out_rpm:
Daniele Ceraolo Spuriod858d562019-06-13 16:21:54 -0700652 intel_runtime_pm_put(rpm, wakeref);
Chris Wilson8bd818152017-10-19 07:37:33 +0100653out_unlock:
Chris Wilsonfe115622016-10-28 13:58:40 +0100654 mutex_unlock(&i915->drm.struct_mutex);
Eric Anholt3de09aa2009-03-09 09:42:23 -0700655 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700656}
657
Chris Wilsonfe115622016-10-28 13:58:40 +0100658/* Per-page copy function for the shmem pwrite fastpath.
659 * Flushes invalid cachelines before writing to the target if
660 * needs_clflush_before is set and flushes out any written cachelines after
661 * writing if needs_clflush is set.
662 */
Eric Anholt40123c12009-03-09 13:42:30 -0700663static int
Chris Wilsonfe115622016-10-28 13:58:40 +0100664shmem_pwrite(struct page *page, int offset, int len, char __user *user_data,
Chris Wilsonfe115622016-10-28 13:58:40 +0100665 bool needs_clflush_before,
666 bool needs_clflush_after)
Eric Anholt40123c12009-03-09 13:42:30 -0700667{
Chris Wilsonb9d126e2019-01-05 12:07:58 +0000668 char *vaddr;
Chris Wilsonfe115622016-10-28 13:58:40 +0100669 int ret;
Eric Anholt40123c12009-03-09 13:42:30 -0700670
Chris Wilsonb9d126e2019-01-05 12:07:58 +0000671 vaddr = kmap(page);
Chris Wilsonfe115622016-10-28 13:58:40 +0100672
Chris Wilsonb9d126e2019-01-05 12:07:58 +0000673 if (needs_clflush_before)
674 drm_clflush_virt_range(vaddr + offset, len);
Chris Wilsonfe115622016-10-28 13:58:40 +0100675
Chris Wilsonb9d126e2019-01-05 12:07:58 +0000676 ret = __copy_from_user(vaddr + offset, user_data, len);
677 if (!ret && needs_clflush_after)
678 drm_clflush_virt_range(vaddr + offset, len);
Chris Wilsonfe115622016-10-28 13:58:40 +0100679
Chris Wilsonb9d126e2019-01-05 12:07:58 +0000680 kunmap(page);
681
682 return ret ? -EFAULT : 0;
Chris Wilsonfe115622016-10-28 13:58:40 +0100683}
684
685static int
686i915_gem_shmem_pwrite(struct drm_i915_gem_object *obj,
687 const struct drm_i915_gem_pwrite *args)
688{
Chris Wilsonfe115622016-10-28 13:58:40 +0100689 unsigned int partial_cacheline_write;
690 unsigned int needs_clflush;
691 unsigned int offset, idx;
Chris Wilson6951e582019-05-28 10:29:51 +0100692 struct dma_fence *fence;
693 void __user *user_data;
694 u64 remain;
Chris Wilsonfe115622016-10-28 13:58:40 +0100695 int ret;
696
Chris Wilson6951e582019-05-28 10:29:51 +0100697 ret = i915_gem_object_prepare_write(obj, &needs_clflush);
Chris Wilson43394c72016-08-18 17:16:47 +0100698 if (ret)
699 return ret;
Eric Anholt40123c12009-03-09 13:42:30 -0700700
Chris Wilson6951e582019-05-28 10:29:51 +0100701 fence = i915_gem_object_lock_fence(obj);
702 i915_gem_object_finish_access(obj);
703 if (!fence)
704 return -ENOMEM;
Chris Wilsonfe115622016-10-28 13:58:40 +0100705
Chris Wilsonfe115622016-10-28 13:58:40 +0100706 /* If we don't overwrite a cacheline completely we need to be
707 * careful to have up-to-date data by first clflushing. Don't
708 * overcomplicate things and flush the entire patch.
709 */
710 partial_cacheline_write = 0;
711 if (needs_clflush & CLFLUSH_BEFORE)
712 partial_cacheline_write = boot_cpu_data.x86_clflush_size - 1;
713
Chris Wilson43394c72016-08-18 17:16:47 +0100714 user_data = u64_to_user_ptr(args->data_ptr);
Chris Wilson43394c72016-08-18 17:16:47 +0100715 remain = args->size;
Chris Wilsonfe115622016-10-28 13:58:40 +0100716 offset = offset_in_page(args->offset);
717 for (idx = args->offset >> PAGE_SHIFT; remain; idx++) {
718 struct page *page = i915_gem_object_get_page(obj, idx);
Chris Wilsona5e856a52018-10-12 15:02:28 +0100719 unsigned int length = min_t(u64, remain, PAGE_SIZE - offset);
Chris Wilsone5281cc2010-10-28 13:45:36 +0100720
Chris Wilsonfe115622016-10-28 13:58:40 +0100721 ret = shmem_pwrite(page, offset, length, user_data,
Chris Wilsonfe115622016-10-28 13:58:40 +0100722 (offset | length) & partial_cacheline_write,
723 needs_clflush & CLFLUSH_AFTER);
724 if (ret)
Chris Wilson9da3da62012-06-01 15:20:22 +0100725 break;
726
Chris Wilsonfe115622016-10-28 13:58:40 +0100727 remain -= length;
728 user_data += length;
729 offset = 0;
Eric Anholt40123c12009-03-09 13:42:30 -0700730 }
731
Chris Wilsond59b21e2017-02-22 11:40:49 +0000732 intel_fb_obj_flush(obj, ORIGIN_CPU);
Chris Wilson6951e582019-05-28 10:29:51 +0100733 i915_gem_object_unlock_fence(obj, fence);
734
Eric Anholt40123c12009-03-09 13:42:30 -0700735 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700736}
737
738/**
739 * Writes data to the object referenced by handle.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +0100740 * @dev: drm device
741 * @data: ioctl data blob
742 * @file: drm file
Eric Anholt673a3942008-07-30 12:06:12 -0700743 *
744 * On error, the contents of the buffer that were to be modified are undefined.
745 */
746int
747i915_gem_pwrite_ioctl(struct drm_device *dev, void *data,
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100748 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -0700749{
750 struct drm_i915_gem_pwrite *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +0000751 struct drm_i915_gem_object *obj;
Chris Wilson51311d02010-11-17 09:10:42 +0000752 int ret;
753
754 if (args->size == 0)
755 return 0;
756
Linus Torvalds96d4f262019-01-03 18:57:57 -0800757 if (!access_ok(u64_to_user_ptr(args->data_ptr), args->size))
Chris Wilson51311d02010-11-17 09:10:42 +0000758 return -EFAULT;
759
Chris Wilson03ac0642016-07-20 13:31:51 +0100760 obj = i915_gem_object_lookup(file, args->handle);
Chris Wilson258a5ed2016-08-05 10:14:16 +0100761 if (!obj)
762 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -0700763
Chris Wilson7dcd2492010-09-26 20:21:44 +0100764 /* Bounds check destination. */
Matthew Auld966d5bf2016-12-13 20:32:22 +0000765 if (range_overflows_t(u64, args->offset, args->size, obj->base.size)) {
Chris Wilsonce9d4192010-09-26 20:50:05 +0100766 ret = -EINVAL;
Chris Wilson258a5ed2016-08-05 10:14:16 +0100767 goto err;
Chris Wilsonce9d4192010-09-26 20:50:05 +0100768 }
769
Chris Wilsonf8c1cce2018-07-12 19:53:14 +0100770 /* Writes not allowed into this read-only object */
771 if (i915_gem_object_is_readonly(obj)) {
772 ret = -EINVAL;
773 goto err;
774 }
775
Chris Wilsondb53a302011-02-03 11:57:46 +0000776 trace_i915_gem_object_pwrite(obj, args->offset, args->size);
777
Chris Wilson7c55e2c2017-03-07 12:03:38 +0000778 ret = -ENODEV;
779 if (obj->ops->pwrite)
780 ret = obj->ops->pwrite(obj, args);
781 if (ret != -ENODEV)
782 goto err;
783
Chris Wilsone95433c2016-10-28 13:58:27 +0100784 ret = i915_gem_object_wait(obj,
785 I915_WAIT_INTERRUPTIBLE |
786 I915_WAIT_ALL,
Chris Wilson62eb3c22019-02-13 09:25:04 +0000787 MAX_SCHEDULE_TIMEOUT);
Chris Wilson258a5ed2016-08-05 10:14:16 +0100788 if (ret)
789 goto err;
790
Chris Wilsonfe115622016-10-28 13:58:40 +0100791 ret = i915_gem_object_pin_pages(obj);
Chris Wilson258a5ed2016-08-05 10:14:16 +0100792 if (ret)
Chris Wilsonfe115622016-10-28 13:58:40 +0100793 goto err;
Chris Wilson258a5ed2016-08-05 10:14:16 +0100794
Daniel Vetter935aaa62012-03-25 19:47:35 +0200795 ret = -EFAULT;
Eric Anholt673a3942008-07-30 12:06:12 -0700796 /* We can only do the GTT pwrite on untiled buffers, as otherwise
797 * it would end up going through the fenced access, and we'll get
798 * different detiling behavior between reading and writing.
799 * pread/pwrite currently are reading and writing from the CPU
800 * perspective, requiring manual detiling by the client.
801 */
Chris Wilson6eae0052016-06-20 15:05:52 +0100802 if (!i915_gem_object_has_struct_page(obj) ||
Chris Wilson9c870d02016-10-24 13:42:15 +0100803 cpu_write_needs_clflush(obj))
Daniel Vetter935aaa62012-03-25 19:47:35 +0200804 /* Note that the gtt paths might fail with non-page-backed user
805 * pointers (e.g. gtt mappings when moving data between
Chris Wilson9c870d02016-10-24 13:42:15 +0100806 * textures). Fallback to the shmem path in that case.
807 */
Chris Wilsonfe115622016-10-28 13:58:40 +0100808 ret = i915_gem_gtt_pwrite_fast(obj, args);
Eric Anholt673a3942008-07-30 12:06:12 -0700809
Chris Wilsond1054ee2016-07-16 18:42:36 +0100810 if (ret == -EFAULT || ret == -ENOSPC) {
Chris Wilson6a2c4232014-11-04 04:51:40 -0800811 if (obj->phys_handle)
812 ret = i915_gem_phys_pwrite(obj, args, file);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +0530813 else
Chris Wilsonfe115622016-10-28 13:58:40 +0100814 ret = i915_gem_shmem_pwrite(obj, args);
Chris Wilson6a2c4232014-11-04 04:51:40 -0800815 }
Daniel Vetter5c0480f2011-12-14 13:57:30 +0100816
Chris Wilsonfe115622016-10-28 13:58:40 +0100817 i915_gem_object_unpin_pages(obj);
Chris Wilson258a5ed2016-08-05 10:14:16 +0100818err:
Chris Wilsonf0cd5182016-10-28 13:58:43 +0100819 i915_gem_object_put(obj);
Chris Wilson258a5ed2016-08-05 10:14:16 +0100820 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700821}
822
Eric Anholt673a3942008-07-30 12:06:12 -0700823/**
824 * Called when user space has done writes to this buffer
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +0100825 * @dev: drm device
826 * @data: ioctl data blob
827 * @file: drm file
Eric Anholt673a3942008-07-30 12:06:12 -0700828 */
829int
830i915_gem_sw_finish_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +0000831 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -0700832{
833 struct drm_i915_gem_sw_finish *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +0000834 struct drm_i915_gem_object *obj;
Chris Wilson1d7cfea2010-10-17 09:45:41 +0100835
Chris Wilson03ac0642016-07-20 13:31:51 +0100836 obj = i915_gem_object_lookup(file, args->handle);
Chris Wilsonc21724c2016-08-05 10:14:19 +0100837 if (!obj)
838 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -0700839
Tina Zhanga03f3952017-11-14 10:25:13 +0000840 /*
841 * Proxy objects are barred from CPU access, so there is no
842 * need to ban sw_finish as it is a nop.
843 */
844
Eric Anholt673a3942008-07-30 12:06:12 -0700845 /* Pinned buffers may be scanout, so flush the cache */
Chris Wilson5a97bcc2017-02-22 11:40:46 +0000846 i915_gem_object_flush_if_display(obj);
Chris Wilsonf0cd5182016-10-28 13:58:43 +0100847 i915_gem_object_put(obj);
Chris Wilson5a97bcc2017-02-22 11:40:46 +0000848
849 return 0;
Eric Anholt673a3942008-07-30 12:06:12 -0700850}
851
Chris Wilson0cf289b2019-06-13 08:32:54 +0100852void i915_gem_runtime_suspend(struct drm_i915_private *i915)
Chris Wilsoneedd10f2014-06-16 08:57:44 +0100853{
Chris Wilson3594a3e2016-10-24 13:42:16 +0100854 struct drm_i915_gem_object *obj, *on;
Chris Wilson7c108fd2016-10-24 13:42:18 +0100855 int i;
Chris Wilsoneedd10f2014-06-16 08:57:44 +0100856
Chris Wilson3594a3e2016-10-24 13:42:16 +0100857 /*
858 * Only called during RPM suspend. All users of the userfault_list
859 * must be holding an RPM wakeref to ensure that this can not
860 * run concurrently with themselves (and use the struct_mutex for
861 * protection between themselves).
862 */
863
864 list_for_each_entry_safe(obj, on,
Chris Wilson0cf289b2019-06-13 08:32:54 +0100865 &i915->ggtt.userfault_list, userfault_link)
Chris Wilsona65adaf2017-10-09 09:43:57 +0100866 __i915_gem_object_release_mmap(obj);
Chris Wilson7c108fd2016-10-24 13:42:18 +0100867
Chris Wilson0cf289b2019-06-13 08:32:54 +0100868 /*
869 * The fence will be lost when the device powers down. If any were
Chris Wilson7c108fd2016-10-24 13:42:18 +0100870 * in use by hardware (i.e. they are pinned), we should not be powering
871 * down! All other fences will be reacquired by the user upon waking.
872 */
Chris Wilson0cf289b2019-06-13 08:32:54 +0100873 for (i = 0; i < i915->ggtt.num_fences; i++) {
874 struct i915_fence_reg *reg = &i915->ggtt.fence_regs[i];
Chris Wilson7c108fd2016-10-24 13:42:18 +0100875
Chris Wilson0cf289b2019-06-13 08:32:54 +0100876 /*
877 * Ideally we want to assert that the fence register is not
Chris Wilsone0ec3ec2017-02-03 12:57:17 +0000878 * live at this point (i.e. that no piece of code will be
879 * trying to write through fence + GTT, as that both violates
880 * our tracking of activity and associated locking/barriers,
881 * but also is illegal given that the hw is powered down).
882 *
883 * Previously we used reg->pin_count as a "liveness" indicator.
884 * That is not sufficient, and we need a more fine-grained
885 * tool if we want to have a sanity check here.
886 */
Chris Wilson7c108fd2016-10-24 13:42:18 +0100887
888 if (!reg->vma)
889 continue;
890
Chris Wilsona65adaf2017-10-09 09:43:57 +0100891 GEM_BUG_ON(i915_vma_has_userfault(reg->vma));
Chris Wilson7c108fd2016-10-24 13:42:18 +0100892 reg->dirty = true;
893 }
Chris Wilsoneedd10f2014-06-16 08:57:44 +0100894}
895
Chris Wilson1e345562019-01-28 10:23:56 +0000896static long
897wait_for_timelines(struct drm_i915_private *i915,
898 unsigned int flags, long timeout)
899{
Chris Wilsonc6fe28b2019-06-21 14:16:39 +0100900 struct intel_gt_timelines *gt = &i915->gt.timelines;
Tvrtko Ursulinf0c02c12019-06-21 08:08:10 +0100901 struct intel_timeline *tl;
Chris Wilson1e345562019-01-28 10:23:56 +0000902
Chris Wilson1e345562019-01-28 10:23:56 +0000903 mutex_lock(&gt->mutex);
Chris Wilson9407d3b2019-01-28 18:18:12 +0000904 list_for_each_entry(tl, &gt->active_list, link) {
Chris Wilson1e345562019-01-28 10:23:56 +0000905 struct i915_request *rq;
906
Chris Wilson21950ee2019-02-05 13:00:05 +0000907 rq = i915_active_request_get_unlocked(&tl->last_request);
Chris Wilson1e345562019-01-28 10:23:56 +0000908 if (!rq)
909 continue;
910
911 mutex_unlock(&gt->mutex);
912
913 /*
914 * "Race-to-idle".
915 *
916 * Switching to the kernel context is often used a synchronous
917 * step prior to idling, e.g. in suspend for flushing all
918 * current operations to memory before sleeping. These we
919 * want to complete as quickly as possible to avoid prolonged
920 * stalls, so allow the gpu to boost to maximum clocks.
921 */
922 if (flags & I915_WAIT_FOR_IDLE_BOOST)
Chris Wilson62eb3c22019-02-13 09:25:04 +0000923 gen6_rps_boost(rq);
Chris Wilson1e345562019-01-28 10:23:56 +0000924
925 timeout = i915_request_wait(rq, flags, timeout);
926 i915_request_put(rq);
927 if (timeout < 0)
928 return timeout;
929
930 /* restart after reacquiring the lock */
931 mutex_lock(&gt->mutex);
Chris Wilson9407d3b2019-01-28 18:18:12 +0000932 tl = list_entry(&gt->active_list, typeof(*tl), link);
Chris Wilson1e345562019-01-28 10:23:56 +0000933 }
934 mutex_unlock(&gt->mutex);
935
936 return timeout;
937}
938
Chris Wilsonec625fb2018-07-09 13:20:42 +0100939int i915_gem_wait_for_idle(struct drm_i915_private *i915,
940 unsigned int flags, long timeout)
Chris Wilson73cb9702016-10-28 13:58:46 +0100941{
Chris Wilson14f8a0e2019-07-23 10:12:18 +0100942 /* If the device is asleep, we have no requests outstanding */
Chris Wilsonc7302f22019-08-08 21:27:58 +0100943 if (!intel_gt_pm_is_awake(&i915->gt))
Chris Wilson14f8a0e2019-07-23 10:12:18 +0100944 return 0;
945
Chris Wilsonc7302f22019-08-08 21:27:58 +0100946 GEM_TRACE("flags=%x (%s), timeout=%ld%s\n",
Chris Wilsonec625fb2018-07-09 13:20:42 +0100947 flags, flags & I915_WAIT_LOCKED ? "locked" : "unlocked",
Chris Wilsonc7302f22019-08-08 21:27:58 +0100948 timeout, timeout == MAX_SCHEDULE_TIMEOUT ? " (forever)" : "");
Chris Wilson09a4c022018-05-24 09:11:35 +0100949
Chris Wilson1e345562019-01-28 10:23:56 +0000950 timeout = wait_for_timelines(i915, flags, timeout);
951 if (timeout < 0)
952 return timeout;
953
Chris Wilson9caa34a2016-11-11 14:58:08 +0000954 if (flags & I915_WAIT_LOCKED) {
Chris Wilson9caa34a2016-11-11 14:58:08 +0000955 lockdep_assert_held(&i915->drm.struct_mutex);
956
Chris Wilsone61e0f52018-02-21 09:56:36 +0000957 i915_retire_requests(i915);
Chris Wilsona89d1f92018-05-02 17:38:39 +0100958 }
Chris Wilsona61b47f2018-06-27 12:53:34 +0100959
960 return 0;
Daniel Vetter4df2faf2010-02-19 11:52:00 +0100961}
962
Chris Wilson058d88c2016-08-15 10:49:06 +0100963struct i915_vma *
Joonas Lahtinenec7adb62015-03-16 14:11:13 +0200964i915_gem_object_ggtt_pin(struct drm_i915_gem_object *obj,
965 const struct i915_ggtt_view *view,
Chris Wilson91b2db62016-08-04 16:32:23 +0100966 u64 size,
Chris Wilson2ffffd02016-08-04 16:32:22 +0100967 u64 alignment,
968 u64 flags)
Joonas Lahtinenec7adb62015-03-16 14:11:13 +0200969{
Chris Wilsonad16d2e2016-10-13 09:55:04 +0100970 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
Chris Wilson82ad6442018-06-05 16:37:58 +0100971 struct i915_address_space *vm = &dev_priv->ggtt.vm;
Chris Wilson59bfa122016-08-04 16:32:31 +0100972 struct i915_vma *vma;
973 int ret;
Joonas Lahtinen72e96d62016-03-30 16:57:10 +0300974
Chris Wilson4c7d62c2016-10-28 13:58:32 +0100975 lockdep_assert_held(&obj->base.dev->struct_mutex);
976
Chris Wilsonac87a6fd2018-02-20 13:42:05 +0000977 if (flags & PIN_MAPPABLE &&
978 (!view || view->type == I915_GGTT_VIEW_NORMAL)) {
Chris Wilson43ae70d92017-10-09 09:44:01 +0100979 /* If the required space is larger than the available
980 * aperture, we will not able to find a slot for the
981 * object and unbinding the object now will be in
982 * vain. Worse, doing so may cause us to ping-pong
983 * the object in and out of the Global GTT and
984 * waste a lot of cycles under the mutex.
985 */
986 if (obj->base.size > dev_priv->ggtt.mappable_end)
987 return ERR_PTR(-E2BIG);
988
989 /* If NONBLOCK is set the caller is optimistically
990 * trying to cache the full object within the mappable
991 * aperture, and *must* have a fallback in place for
992 * situations where we cannot bind the object. We
993 * can be a little more lax here and use the fallback
994 * more often to avoid costly migrations of ourselves
995 * and other objects within the aperture.
996 *
997 * Half-the-aperture is used as a simple heuristic.
998 * More interesting would to do search for a free
999 * block prior to making the commitment to unbind.
1000 * That caters for the self-harm case, and with a
1001 * little more heuristics (e.g. NOFAULT, NOEVICT)
1002 * we could try to minimise harm to others.
1003 */
1004 if (flags & PIN_NONBLOCK &&
1005 obj->base.size > dev_priv->ggtt.mappable_end / 2)
1006 return ERR_PTR(-ENOSPC);
1007 }
1008
Chris Wilson718659a2017-01-16 15:21:28 +00001009 vma = i915_vma_instance(obj, vm, view);
Chengguang Xu772b5402019-02-21 10:08:19 +08001010 if (IS_ERR(vma))
Chris Wilson058d88c2016-08-15 10:49:06 +01001011 return vma;
Chris Wilson59bfa122016-08-04 16:32:31 +01001012
1013 if (i915_vma_misplaced(vma, size, alignment, flags)) {
Chris Wilson43ae70d92017-10-09 09:44:01 +01001014 if (flags & PIN_NONBLOCK) {
1015 if (i915_vma_is_pinned(vma) || i915_vma_is_active(vma))
1016 return ERR_PTR(-ENOSPC);
Chris Wilson59bfa122016-08-04 16:32:31 +01001017
Chris Wilson43ae70d92017-10-09 09:44:01 +01001018 if (flags & PIN_MAPPABLE &&
Chris Wilson944397f2017-01-09 16:16:11 +00001019 vma->fence_size > dev_priv->ggtt.mappable_end / 2)
Chris Wilsonad16d2e2016-10-13 09:55:04 +01001020 return ERR_PTR(-ENOSPC);
1021 }
1022
Chris Wilson59bfa122016-08-04 16:32:31 +01001023 WARN(i915_vma_is_pinned(vma),
1024 "bo is already pinned in ggtt with incorrect alignment:"
Chris Wilson05a20d02016-08-18 17:16:55 +01001025 " offset=%08x, req.alignment=%llx,"
1026 " req.map_and_fenceable=%d, vma->map_and_fenceable=%d\n",
1027 i915_ggtt_offset(vma), alignment,
Chris Wilson59bfa122016-08-04 16:32:31 +01001028 !!(flags & PIN_MAPPABLE),
Chris Wilson05a20d02016-08-18 17:16:55 +01001029 i915_vma_is_map_and_fenceable(vma));
Chris Wilson59bfa122016-08-04 16:32:31 +01001030 ret = i915_vma_unbind(vma);
1031 if (ret)
Chris Wilson058d88c2016-08-15 10:49:06 +01001032 return ERR_PTR(ret);
Chris Wilson59bfa122016-08-04 16:32:31 +01001033 }
1034
Chris Wilson058d88c2016-08-15 10:49:06 +01001035 ret = i915_vma_pin(vma, size, alignment, flags | PIN_GLOBAL);
1036 if (ret)
1037 return ERR_PTR(ret);
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02001038
Chris Wilson058d88c2016-08-15 10:49:06 +01001039 return vma;
Eric Anholt673a3942008-07-30 12:06:12 -07001040}
1041
Eric Anholt673a3942008-07-30 12:06:12 -07001042int
Chris Wilson3ef94da2009-09-14 16:50:29 +01001043i915_gem_madvise_ioctl(struct drm_device *dev, void *data,
1044 struct drm_file *file_priv)
1045{
Chris Wilson3b4fa962019-05-30 21:34:59 +01001046 struct drm_i915_private *i915 = to_i915(dev);
Chris Wilson3ef94da2009-09-14 16:50:29 +01001047 struct drm_i915_gem_madvise *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00001048 struct drm_i915_gem_object *obj;
Chris Wilson1233e2d2016-10-28 13:58:37 +01001049 int err;
Chris Wilson3ef94da2009-09-14 16:50:29 +01001050
1051 switch (args->madv) {
1052 case I915_MADV_DONTNEED:
1053 case I915_MADV_WILLNEED:
1054 break;
1055 default:
1056 return -EINVAL;
1057 }
1058
Chris Wilson03ac0642016-07-20 13:31:51 +01001059 obj = i915_gem_object_lookup(file_priv, args->handle);
Chris Wilson1233e2d2016-10-28 13:58:37 +01001060 if (!obj)
1061 return -ENOENT;
1062
1063 err = mutex_lock_interruptible(&obj->mm.lock);
1064 if (err)
1065 goto out;
Chris Wilson3ef94da2009-09-14 16:50:29 +01001066
Chris Wilsonf1fa4f42017-10-13 21:26:13 +01001067 if (i915_gem_object_has_pages(obj) &&
Chris Wilson3e510a82016-08-05 10:14:23 +01001068 i915_gem_object_is_tiled(obj) &&
Chris Wilson3b4fa962019-05-30 21:34:59 +01001069 i915->quirks & QUIRK_PIN_SWIZZLED_PAGES) {
Chris Wilsonbc0629a2016-11-01 10:03:17 +00001070 if (obj->mm.madv == I915_MADV_WILLNEED) {
1071 GEM_BUG_ON(!obj->mm.quirked);
Chris Wilsona4f5ea62016-10-28 13:58:35 +01001072 __i915_gem_object_unpin_pages(obj);
Chris Wilsonbc0629a2016-11-01 10:03:17 +00001073 obj->mm.quirked = false;
1074 }
1075 if (args->madv == I915_MADV_WILLNEED) {
Chris Wilson2c3a3f42016-11-04 10:30:01 +00001076 GEM_BUG_ON(obj->mm.quirked);
Chris Wilsona4f5ea62016-10-28 13:58:35 +01001077 __i915_gem_object_pin_pages(obj);
Chris Wilsonbc0629a2016-11-01 10:03:17 +00001078 obj->mm.quirked = true;
1079 }
Daniel Vetter656bfa32014-11-20 09:26:30 +01001080 }
1081
Chris Wilsona4f5ea62016-10-28 13:58:35 +01001082 if (obj->mm.madv != __I915_MADV_PURGED)
1083 obj->mm.madv = args->madv;
Chris Wilson3ef94da2009-09-14 16:50:29 +01001084
Chris Wilson3b4fa962019-05-30 21:34:59 +01001085 if (i915_gem_object_has_pages(obj)) {
1086 struct list_head *list;
1087
Chris Wilsond82b4b22019-05-30 21:35:00 +01001088 if (i915_gem_object_is_shrinkable(obj)) {
Chris Wilsona8cff4c82019-06-10 15:54:30 +01001089 unsigned long flags;
1090
1091 spin_lock_irqsave(&i915->mm.obj_lock, flags);
1092
Chris Wilsond82b4b22019-05-30 21:35:00 +01001093 if (obj->mm.madv != I915_MADV_WILLNEED)
1094 list = &i915->mm.purge_list;
Chris Wilsond82b4b22019-05-30 21:35:00 +01001095 else
Chris Wilsonecab9be2019-06-12 11:57:20 +01001096 list = &i915->mm.shrink_list;
Chris Wilsond82b4b22019-05-30 21:35:00 +01001097 list_move_tail(&obj->mm.link, list);
Chris Wilsona8cff4c82019-06-10 15:54:30 +01001098
1099 spin_unlock_irqrestore(&i915->mm.obj_lock, flags);
Chris Wilsond82b4b22019-05-30 21:35:00 +01001100 }
Chris Wilson3b4fa962019-05-30 21:34:59 +01001101 }
1102
Chris Wilson6c085a72012-08-20 11:40:46 +02001103 /* if the object is no longer attached, discard its backing storage */
Chris Wilsonf1fa4f42017-10-13 21:26:13 +01001104 if (obj->mm.madv == I915_MADV_DONTNEED &&
1105 !i915_gem_object_has_pages(obj))
Chris Wilsonf0334282019-05-28 10:29:46 +01001106 i915_gem_object_truncate(obj);
Chris Wilson2d7ef392009-09-20 23:13:10 +01001107
Chris Wilsona4f5ea62016-10-28 13:58:35 +01001108 args->retained = obj->mm.madv != __I915_MADV_PURGED;
Chris Wilson1233e2d2016-10-28 13:58:37 +01001109 mutex_unlock(&obj->mm.lock);
Chris Wilsonbb6baf72009-09-22 14:24:13 +01001110
Chris Wilson1233e2d2016-10-28 13:58:37 +01001111out:
Chris Wilsonf8c417c2016-07-20 13:31:53 +01001112 i915_gem_object_put(obj);
Chris Wilson1233e2d2016-10-28 13:58:37 +01001113 return err;
Chris Wilson3ef94da2009-09-14 16:50:29 +01001114}
1115
Chris Wilson24145512017-01-24 11:01:35 +00001116void i915_gem_sanitize(struct drm_i915_private *i915)
1117{
Chris Wilson538ef962019-01-14 14:21:18 +00001118 intel_wakeref_t wakeref;
1119
Chris Wilsonc3160da2018-05-31 09:22:45 +01001120 GEM_TRACE("\n");
1121
Daniele Ceraolo Spuriod858d562019-06-13 16:21:54 -07001122 wakeref = intel_runtime_pm_get(&i915->runtime_pm);
Daniele Ceraolo Spurio3ceea6a2019-03-19 11:35:36 -07001123 intel_uncore_forcewake_get(&i915->uncore, FORCEWAKE_ALL);
Chris Wilsonc3160da2018-05-31 09:22:45 +01001124
1125 /*
1126 * As we have just resumed the machine and woken the device up from
1127 * deep PCI sleep (presumably D3_cold), assume the HW has been reset
1128 * back to defaults, recovering from whatever wedged state we left it
1129 * in and so worth trying to use the device once more.
1130 */
Chris Wilsoncb823ed2019-07-12 20:29:53 +01001131 if (intel_gt_is_wedged(&i915->gt))
1132 intel_gt_unset_wedged(&i915->gt);
Chris Wilsonf36325f2017-08-26 12:09:34 +01001133
Chris Wilson24145512017-01-24 11:01:35 +00001134 /*
1135 * If we inherit context state from the BIOS or earlier occupants
1136 * of the GPU, the GPU may be in an inconsistent state when we
1137 * try to take over. The only way to remove the earlier state
1138 * is by resetting. However, resetting on earlier gen is tricky as
1139 * it may impact the display and we are uncertain about the stability
Joonas Lahtinenea117b82017-04-28 10:53:38 +03001140 * of the reset, so this could be applied to even earlier gen.
Chris Wilson24145512017-01-24 11:01:35 +00001141 */
Chris Wilson0c916212019-06-25 14:01:10 +01001142 intel_gt_sanitize(&i915->gt, false);
Chris Wilsonc3160da2018-05-31 09:22:45 +01001143
Daniele Ceraolo Spurio3ceea6a2019-03-19 11:35:36 -07001144 intel_uncore_forcewake_put(&i915->uncore, FORCEWAKE_ALL);
Daniele Ceraolo Spuriod858d562019-06-13 16:21:54 -07001145 intel_runtime_pm_put(&i915->runtime_pm, wakeref);
Chris Wilson24145512017-01-24 11:01:35 +00001146}
1147
Tvrtko Ursulincf6844b2019-06-21 08:07:47 +01001148static void init_unused_ring(struct intel_gt *gt, u32 base)
Ville Syrjälä81e7f202014-08-15 01:21:55 +03001149{
Tvrtko Ursulincf6844b2019-06-21 08:07:47 +01001150 struct intel_uncore *uncore = gt->uncore;
1151
1152 intel_uncore_write(uncore, RING_CTL(base), 0);
1153 intel_uncore_write(uncore, RING_HEAD(base), 0);
1154 intel_uncore_write(uncore, RING_TAIL(base), 0);
1155 intel_uncore_write(uncore, RING_START(base), 0);
Ville Syrjälä81e7f202014-08-15 01:21:55 +03001156}
1157
Tvrtko Ursulincf6844b2019-06-21 08:07:47 +01001158static void init_unused_rings(struct intel_gt *gt)
Ville Syrjälä81e7f202014-08-15 01:21:55 +03001159{
Tvrtko Ursulincf6844b2019-06-21 08:07:47 +01001160 struct drm_i915_private *i915 = gt->i915;
1161
1162 if (IS_I830(i915)) {
1163 init_unused_ring(gt, PRB1_BASE);
1164 init_unused_ring(gt, SRB0_BASE);
1165 init_unused_ring(gt, SRB1_BASE);
1166 init_unused_ring(gt, SRB2_BASE);
1167 init_unused_ring(gt, SRB3_BASE);
1168 } else if (IS_GEN(i915, 2)) {
1169 init_unused_ring(gt, SRB0_BASE);
1170 init_unused_ring(gt, SRB1_BASE);
1171 } else if (IS_GEN(i915, 3)) {
1172 init_unused_ring(gt, PRB1_BASE);
1173 init_unused_ring(gt, PRB2_BASE);
Ville Syrjälä81e7f202014-08-15 01:21:55 +03001174 }
1175}
1176
Chris Wilson092be382019-06-26 16:45:49 +01001177int i915_gem_init_hw(struct drm_i915_private *i915)
Chris Wilson20a8a742017-02-08 14:30:31 +00001178{
Chris Wilson092be382019-06-26 16:45:49 +01001179 struct intel_uncore *uncore = &i915->uncore;
1180 struct intel_gt *gt = &i915->gt;
Chris Wilsond200cda2016-04-28 09:56:44 +01001181 int ret;
Ben Widawsky4fc7c972013-02-08 11:49:24 -08001182
Chris Wilson092be382019-06-26 16:45:49 +01001183 BUG_ON(!i915->kernel_context);
Chris Wilsoncb823ed2019-07-12 20:29:53 +01001184 ret = intel_gt_terminally_wedged(gt);
Chris Wilson092be382019-06-26 16:45:49 +01001185 if (ret)
1186 return ret;
1187
Tvrtko Ursulinabc584f2019-06-21 08:07:53 +01001188 gt->last_init_time = ktime_get();
Chris Wilsonde867c22016-10-25 13:16:02 +01001189
Chris Wilson5e4f5182015-02-13 14:35:59 +00001190 /* Double layer security blanket, see i915_gem_init() */
Tvrtko Ursulinabc584f2019-06-21 08:07:53 +01001191 intel_uncore_forcewake_get(uncore, FORCEWAKE_ALL);
Chris Wilson5e4f5182015-02-13 14:35:59 +00001192
Tvrtko Ursulinabc584f2019-06-21 08:07:53 +01001193 if (HAS_EDRAM(i915) && INTEL_GEN(i915) < 9)
1194 intel_uncore_rmw(uncore, HSW_IDICR, 0, IDIHASHMSK(0xf));
Ben Widawsky4fc7c972013-02-08 11:49:24 -08001195
Tvrtko Ursulinabc584f2019-06-21 08:07:53 +01001196 if (IS_HASWELL(i915))
1197 intel_uncore_write(uncore,
1198 MI_PREDICATE_RESULT_2,
1199 IS_HSW_GT3(i915) ?
1200 LOWER_SLICE_ENABLED : LOWER_SLICE_DISABLED);
Rodrigo Vivi94353732013-08-28 16:45:46 -03001201
Tvrtko Ursulin094304b2018-12-03 12:50:10 +00001202 /* Apply the GT workarounds... */
Tvrtko Ursulinabc584f2019-06-21 08:07:53 +01001203 intel_gt_apply_workarounds(gt);
Tvrtko Ursulin094304b2018-12-03 12:50:10 +00001204 /* ...and determine whether they are sticking. */
Tvrtko Ursulinabc584f2019-06-21 08:07:53 +01001205 intel_gt_verify_workarounds(gt, "init");
Oscar Mateo59b449d2018-04-10 09:12:47 -07001206
Tvrtko Ursulinabc584f2019-06-21 08:07:53 +01001207 intel_gt_init_swizzling(gt);
Ben Widawsky4fc7c972013-02-08 11:49:24 -08001208
Daniel Vetterd5abdfd2014-11-20 09:45:19 +01001209 /*
1210 * At least 830 can leave some of the unused rings
1211 * "active" (ie. head != tail) after resume which
1212 * will prevent c3 entry. Makes sure all unused rings
1213 * are totally idle.
1214 */
Tvrtko Ursulinabc584f2019-06-21 08:07:53 +01001215 init_unused_rings(gt);
Daniel Vetterd5abdfd2014-11-20 09:45:19 +01001216
Tvrtko Ursulinabc584f2019-06-21 08:07:53 +01001217 ret = i915_ppgtt_init_hw(gt);
John Harrison4ad2fd82015-06-18 13:11:20 +01001218 if (ret) {
Chris Wilson8177e112018-02-07 11:15:45 +00001219 DRM_ERROR("Enabling PPGTT failed (%d)\n", ret);
John Harrison4ad2fd82015-06-18 13:11:20 +01001220 goto out;
1221 }
1222
Michał Winiarski9bdc3572017-10-25 18:25:19 +01001223 /* We can't enable contexts until all firmware is loaded */
Daniele Ceraolo Spurio63064d82019-07-30 16:07:40 -07001224 ret = intel_uc_init_hw(&gt->uc);
Chris Wilson8177e112018-02-07 11:15:45 +00001225 if (ret) {
Michal Wajdeczko5d1ef2b2019-08-02 18:40:54 +00001226 i915_probe_error(i915, "Enabling uc failed (%d)\n", ret);
Michał Winiarski9bdc3572017-10-25 18:25:19 +01001227 goto out;
Chris Wilson8177e112018-02-07 11:15:45 +00001228 }
Michał Winiarski9bdc3572017-10-25 18:25:19 +01001229
Tvrtko Ursulin1b6c3c62019-07-30 11:04:07 -07001230 intel_mocs_init(gt);
Peter Antoine0ccdacf2016-04-13 15:03:25 +01001231
Michał Winiarski60c0a662018-07-12 14:48:10 +02001232out:
Tvrtko Ursulinabc584f2019-06-21 08:07:53 +01001233 intel_uncore_forcewake_put(uncore, FORCEWAKE_ALL);
Michał Winiarski60c0a662018-07-12 14:48:10 +02001234 return ret;
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001235}
1236
Chris Wilsond2b4b972017-11-10 14:26:33 +00001237static int __intel_engines_record_defaults(struct drm_i915_private *i915)
1238{
Chris Wilson38775822019-08-08 12:06:11 +01001239 struct i915_request *requests[I915_NUM_ENGINES] = {};
Chris Wilsond2b4b972017-11-10 14:26:33 +00001240 struct intel_engine_cs *engine;
1241 enum intel_engine_id id;
Chris Wilson604c37d2019-03-08 09:36:55 +00001242 int err = 0;
Chris Wilsond2b4b972017-11-10 14:26:33 +00001243
1244 /*
1245 * As we reset the gpu during very early sanitisation, the current
1246 * register state on the GPU should reflect its defaults values.
1247 * We load a context onto the hw (with restore-inhibit), then switch
1248 * over to a second context to save that default register state. We
1249 * can then prime every new context with that state so they all start
1250 * from the same default HW values.
1251 */
1252
Chris Wilsond2b4b972017-11-10 14:26:33 +00001253 for_each_engine(engine, i915, id) {
Chris Wilson38775822019-08-08 12:06:11 +01001254 struct intel_context *ce;
Chris Wilsone61e0f52018-02-21 09:56:36 +00001255 struct i915_request *rq;
Chris Wilsond2b4b972017-11-10 14:26:33 +00001256
Chris Wilson38775822019-08-08 12:06:11 +01001257 /* We must be able to switch to something! */
1258 GEM_BUG_ON(!engine->kernel_context);
1259 engine->serial++; /* force the kernel context switch */
1260
1261 ce = intel_context_create(i915->kernel_context, engine);
1262 if (IS_ERR(ce)) {
1263 err = PTR_ERR(ce);
1264 goto out;
1265 }
1266
Chris Wilson5e2a0412019-04-26 17:33:34 +01001267 rq = intel_context_create_request(ce);
Chris Wilsond2b4b972017-11-10 14:26:33 +00001268 if (IS_ERR(rq)) {
1269 err = PTR_ERR(rq);
Chris Wilson38775822019-08-08 12:06:11 +01001270 intel_context_put(ce);
1271 goto out;
Chris Wilsond2b4b972017-11-10 14:26:33 +00001272 }
1273
Chris Wilsona5627722019-07-29 12:37:20 +01001274 err = intel_engine_emit_ctx_wa(rq);
1275 if (err)
1276 goto err_rq;
Chris Wilsond2b4b972017-11-10 14:26:33 +00001277
Chris Wilsona5627722019-07-29 12:37:20 +01001278 /*
1279 * Failing to program the MOCS is non-fatal.The system will not
1280 * run at peak performance. So warn the user and carry on.
1281 */
1282 err = intel_mocs_emit(rq);
1283 if (err)
1284 dev_notice(i915->drm.dev,
1285 "Failed to program MOCS registers; expect performance issues.\n");
1286
1287 err = intel_renderstate_emit(rq);
1288 if (err)
1289 goto err_rq;
1290
1291err_rq:
Chris Wilson38775822019-08-08 12:06:11 +01001292 requests[id] = i915_request_get(rq);
Chris Wilson697b9a82018-06-12 11:51:35 +01001293 i915_request_add(rq);
Chris Wilsond2b4b972017-11-10 14:26:33 +00001294 if (err)
Chris Wilson38775822019-08-08 12:06:11 +01001295 goto out;
Chris Wilsond2b4b972017-11-10 14:26:33 +00001296 }
1297
Chris Wilson604c37d2019-03-08 09:36:55 +00001298 /* Flush the default context image to memory, and enable powersaving. */
Chris Wilson23c3c3d2019-04-24 21:07:14 +01001299 if (!i915_gem_load_power_context(i915)) {
Chris Wilson604c37d2019-03-08 09:36:55 +00001300 err = -EIO;
Chris Wilson38775822019-08-08 12:06:11 +01001301 goto out;
Chris Wilson2621cef2018-07-09 13:20:43 +01001302 }
Chris Wilsond2b4b972017-11-10 14:26:33 +00001303
Chris Wilson38775822019-08-08 12:06:11 +01001304 for (id = 0; id < ARRAY_SIZE(requests); id++) {
1305 struct i915_request *rq;
1306 struct i915_vma *state;
Chris Wilson37d7c9c2018-09-14 13:35:03 +01001307 void *vaddr;
Chris Wilsond2b4b972017-11-10 14:26:33 +00001308
Chris Wilson38775822019-08-08 12:06:11 +01001309 rq = requests[id];
1310 if (!rq)
Chris Wilsond2b4b972017-11-10 14:26:33 +00001311 continue;
1312
Chris Wilson38775822019-08-08 12:06:11 +01001313 /* We want to be able to unbind the state from the GGTT */
1314 GEM_BUG_ON(intel_context_is_pinned(rq->hw_context));
1315
1316 state = rq->hw_context->state;
1317 if (!state)
1318 continue;
Chris Wilsonc4d52fe2019-03-08 13:25:19 +00001319
Chris Wilsond2b4b972017-11-10 14:26:33 +00001320 /*
1321 * As we will hold a reference to the logical state, it will
1322 * not be torn down with the context, and importantly the
1323 * object will hold onto its vma (making it possible for a
1324 * stray GTT write to corrupt our defaults). Unmap the vma
1325 * from the GTT to prevent such accidents and reclaim the
1326 * space.
1327 */
1328 err = i915_vma_unbind(state);
1329 if (err)
Chris Wilson38775822019-08-08 12:06:11 +01001330 goto out;
Chris Wilsond2b4b972017-11-10 14:26:33 +00001331
Chris Wilson6951e582019-05-28 10:29:51 +01001332 i915_gem_object_lock(state->obj);
Chris Wilsond2b4b972017-11-10 14:26:33 +00001333 err = i915_gem_object_set_to_cpu_domain(state->obj, false);
Chris Wilson6951e582019-05-28 10:29:51 +01001334 i915_gem_object_unlock(state->obj);
Chris Wilsond2b4b972017-11-10 14:26:33 +00001335 if (err)
Chris Wilson38775822019-08-08 12:06:11 +01001336 goto out;
Chris Wilsond2b4b972017-11-10 14:26:33 +00001337
Chris Wilson38775822019-08-08 12:06:11 +01001338 i915_gem_object_set_cache_coherency(state->obj, I915_CACHE_LLC);
Chris Wilson37d7c9c2018-09-14 13:35:03 +01001339
1340 /* Check we can acquire the image of the context state */
Chris Wilson38775822019-08-08 12:06:11 +01001341 vaddr = i915_gem_object_pin_map(state->obj, I915_MAP_FORCE_WB);
Chris Wilson37d7c9c2018-09-14 13:35:03 +01001342 if (IS_ERR(vaddr)) {
1343 err = PTR_ERR(vaddr);
Chris Wilson38775822019-08-08 12:06:11 +01001344 goto out;
Chris Wilson37d7c9c2018-09-14 13:35:03 +01001345 }
1346
Chris Wilson38775822019-08-08 12:06:11 +01001347 rq->engine->default_state = i915_gem_object_get(state->obj);
1348 i915_gem_object_unpin_map(state->obj);
Chris Wilsond2b4b972017-11-10 14:26:33 +00001349 }
1350
Chris Wilson38775822019-08-08 12:06:11 +01001351out:
Chris Wilsond2b4b972017-11-10 14:26:33 +00001352 /*
1353 * If we have to abandon now, we expect the engines to be idle
Chris Wilson604c37d2019-03-08 09:36:55 +00001354 * and ready to be torn-down. The quickest way we can accomplish
1355 * this is by declaring ourselves wedged.
Chris Wilsond2b4b972017-11-10 14:26:33 +00001356 */
Chris Wilson38775822019-08-08 12:06:11 +01001357 if (err)
1358 intel_gt_set_wedged(&i915->gt);
1359
1360 for (id = 0; id < ARRAY_SIZE(requests); id++) {
1361 struct intel_context *ce;
1362 struct i915_request *rq;
1363
1364 rq = requests[id];
1365 if (!rq)
1366 continue;
1367
1368 ce = rq->hw_context;
1369 i915_request_put(rq);
1370 intel_context_put(ce);
1371 }
1372 return err;
Chris Wilsond2b4b972017-11-10 14:26:33 +00001373}
1374
Chris Wilson51797492018-12-04 14:15:16 +00001375static int
1376i915_gem_init_scratch(struct drm_i915_private *i915, unsigned int size)
1377{
Tvrtko Ursulindb56f972019-06-21 08:08:11 +01001378 return intel_gt_init_scratch(&i915->gt, size);
Chris Wilson51797492018-12-04 14:15:16 +00001379}
1380
1381static void i915_gem_fini_scratch(struct drm_i915_private *i915)
1382{
Tvrtko Ursulindb56f972019-06-21 08:08:11 +01001383 intel_gt_fini_scratch(&i915->gt);
Chris Wilson51797492018-12-04 14:15:16 +00001384}
1385
Chris Wilson254e1182019-04-17 08:56:28 +01001386static int intel_engines_verify_workarounds(struct drm_i915_private *i915)
1387{
1388 struct intel_engine_cs *engine;
1389 enum intel_engine_id id;
1390 int err = 0;
1391
1392 if (!IS_ENABLED(CONFIG_DRM_I915_DEBUG_GEM))
1393 return 0;
1394
1395 for_each_engine(engine, i915, id) {
1396 if (intel_engine_verify_workarounds(engine, "load"))
1397 err = -EIO;
1398 }
1399
1400 return err;
1401}
1402
Tvrtko Ursulinbf9e8422016-12-01 14:16:38 +00001403int i915_gem_init(struct drm_i915_private *dev_priv)
Chris Wilson1070a422012-04-24 15:47:41 +01001404{
Chris Wilson1070a422012-04-24 15:47:41 +01001405 int ret;
1406
Changbin Du52b24162018-05-08 17:07:05 +08001407 /* We need to fallback to 4K pages if host doesn't support huge gtt. */
1408 if (intel_vgpu_active(dev_priv) && !intel_vgpu_has_huge_gtt(dev_priv))
Matthew Auldda9fe3f32017-10-06 23:18:31 +01001409 mkwrite_device_info(dev_priv)->page_sizes =
1410 I915_GTT_PAGE_SIZE_4K;
1411
Chris Wilson94312822017-05-03 10:39:18 +01001412 dev_priv->mm.unordered_timeline = dma_fence_context_alloc(1);
Chris Wilson57822dc2017-02-22 11:40:48 +00001413
Tvrtko Ursulinf0c02c12019-06-21 08:08:10 +01001414 intel_timelines_init(dev_priv);
Chris Wilson1e345562019-01-28 10:23:56 +00001415
Chris Wilsonee487002017-11-22 17:26:21 +00001416 ret = i915_gem_init_userptr(dev_priv);
1417 if (ret)
1418 return ret;
1419
Daniele Ceraolo Spurioca7b2c12019-07-13 11:00:13 +01001420 intel_uc_fetch_firmwares(&dev_priv->gt.uc);
Michal Wajdeczko6bd0fbe2019-08-02 18:40:55 +00001421 intel_wopcm_init(&dev_priv->wopcm);
Michal Wajdeczkof7dc0152018-06-28 14:15:21 +00001422
Chris Wilson5e4f5182015-02-13 14:35:59 +00001423 /* This is just a security blanket to placate dragons.
1424 * On some systems, we very sporadically observe that the first TLBs
1425 * used by the CS may be stale, despite us poking the TLB reset. If
1426 * we hold the forcewake during initialisation these problems
1427 * just magically go away.
1428 */
Chris Wilsonee487002017-11-22 17:26:21 +00001429 mutex_lock(&dev_priv->drm.struct_mutex);
Daniele Ceraolo Spurio3ceea6a2019-03-19 11:35:36 -07001430 intel_uncore_forcewake_get(&dev_priv->uncore, FORCEWAKE_ALL);
Chris Wilson5e4f5182015-02-13 14:35:59 +00001431
Tvrtko Ursulin1d66377a2019-06-21 08:08:05 +01001432 ret = i915_init_ggtt(dev_priv);
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00001433 if (ret) {
1434 GEM_BUG_ON(ret == -EIO);
1435 goto err_unlock;
1436 }
Jesse Barnesd62b4892013-03-08 10:45:53 -08001437
Chris Wilson51797492018-12-04 14:15:16 +00001438 ret = i915_gem_init_scratch(dev_priv,
Lucas De Marchicf819ef2018-12-12 10:10:43 -08001439 IS_GEN(dev_priv, 2) ? SZ_256K : PAGE_SIZE);
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00001440 if (ret) {
1441 GEM_BUG_ON(ret == -EIO);
1442 goto err_ggtt;
1443 }
Ben Widawsky2fa48d82013-12-06 14:11:04 -08001444
Chris Wilson11334c62019-04-26 17:33:33 +01001445 ret = intel_engines_setup(dev_priv);
1446 if (ret) {
1447 GEM_BUG_ON(ret == -EIO);
1448 goto err_unlock;
1449 }
1450
Chris Wilson51797492018-12-04 14:15:16 +00001451 ret = i915_gem_contexts_init(dev_priv);
1452 if (ret) {
1453 GEM_BUG_ON(ret == -EIO);
1454 goto err_scratch;
1455 }
1456
Tvrtko Ursulinbf9e8422016-12-01 14:16:38 +00001457 ret = intel_engines_init(dev_priv);
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00001458 if (ret) {
1459 GEM_BUG_ON(ret == -EIO);
1460 goto err_context;
1461 }
Daniel Vetter53ca26c2012-04-26 23:28:03 +02001462
Chris Wilsonf58d13d2017-11-10 14:26:29 +00001463 intel_init_gt_powersave(dev_priv);
1464
Daniele Ceraolo Spurioca7b2c12019-07-13 11:00:13 +01001465 ret = intel_uc_init(&dev_priv->gt.uc);
Michal Wajdeczkoa5f978c2019-08-11 19:51:32 +00001466 if (ret) {
1467 GEM_BUG_ON(ret == -EIO);
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00001468 goto err_pm;
Michal Wajdeczkoa5f978c2019-08-11 19:51:32 +00001469 }
Chris Wilsoncc6a8182017-11-10 14:26:30 +00001470
Michał Winiarski61b5c152017-12-13 23:13:48 +01001471 ret = i915_gem_init_hw(dev_priv);
1472 if (ret)
1473 goto err_uc_init;
1474
Chris Wilson092be382019-06-26 16:45:49 +01001475 /* Only when the HW is re-initialised, can we replay the requests */
1476 ret = intel_gt_resume(&dev_priv->gt);
1477 if (ret)
1478 goto err_init_hw;
1479
Chris Wilsoncc6a8182017-11-10 14:26:30 +00001480 /*
1481 * Despite its name intel_init_clock_gating applies both display
1482 * clock gating workarounds; GT mmio workarounds and the occasional
1483 * GT power context workaround. Worse, sometimes it includes a context
1484 * register workaround which we need to apply before we record the
1485 * default HW state for all contexts.
1486 *
1487 * FIXME: break up the workarounds and apply them at the right time!
1488 */
1489 intel_init_clock_gating(dev_priv);
1490
Chris Wilson254e1182019-04-17 08:56:28 +01001491 ret = intel_engines_verify_workarounds(dev_priv);
1492 if (ret)
Chris Wilson092be382019-06-26 16:45:49 +01001493 goto err_gt;
Chris Wilson254e1182019-04-17 08:56:28 +01001494
Chris Wilsond2b4b972017-11-10 14:26:33 +00001495 ret = __intel_engines_record_defaults(dev_priv);
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00001496 if (ret)
Chris Wilson092be382019-06-26 16:45:49 +01001497 goto err_gt;
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00001498
Michal Wajdeczko50d84412019-08-02 18:40:50 +00001499 ret = i915_inject_load_error(dev_priv, -ENODEV);
1500 if (ret)
Chris Wilson092be382019-06-26 16:45:49 +01001501 goto err_gt;
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00001502
Michal Wajdeczko50d84412019-08-02 18:40:50 +00001503 ret = i915_inject_load_error(dev_priv, -EIO);
1504 if (ret)
Chris Wilson092be382019-06-26 16:45:49 +01001505 goto err_gt;
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00001506
Daniele Ceraolo Spurio3ceea6a2019-03-19 11:35:36 -07001507 intel_uncore_forcewake_put(&dev_priv->uncore, FORCEWAKE_ALL);
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00001508 mutex_unlock(&dev_priv->drm.struct_mutex);
1509
1510 return 0;
1511
1512 /*
1513 * Unwinding is complicated by that we want to handle -EIO to mean
1514 * disable GPU submission but keep KMS alive. We want to mark the
1515 * HW as irrevisibly wedged, but keep enough state around that the
1516 * driver doesn't explode during runtime.
1517 */
Chris Wilson092be382019-06-26 16:45:49 +01001518err_gt:
Chris Wilson8571a052018-06-06 15:54:41 +01001519 mutex_unlock(&dev_priv->drm.struct_mutex);
1520
Chris Wilsoncb823ed2019-07-12 20:29:53 +01001521 intel_gt_set_wedged(&dev_priv->gt);
Chris Wilson5861b012019-03-08 09:36:54 +00001522 i915_gem_suspend(dev_priv);
Chris Wilson8571a052018-06-06 15:54:41 +01001523 i915_gem_suspend_late(dev_priv);
1524
Chris Wilson8bcf9f72018-07-10 10:44:20 +01001525 i915_gem_drain_workqueue(dev_priv);
1526
Chris Wilson8571a052018-06-06 15:54:41 +01001527 mutex_lock(&dev_priv->drm.struct_mutex);
Chris Wilson092be382019-06-26 16:45:49 +01001528err_init_hw:
Daniele Ceraolo Spurioca7b2c12019-07-13 11:00:13 +01001529 intel_uc_fini_hw(&dev_priv->gt.uc);
Michał Winiarski61b5c152017-12-13 23:13:48 +01001530err_uc_init:
Michal Wajdeczkoa5f978c2019-08-11 19:51:32 +00001531 if (ret != -EIO)
1532 intel_uc_fini(&dev_priv->gt.uc);
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00001533err_pm:
1534 if (ret != -EIO) {
1535 intel_cleanup_gt_powersave(dev_priv);
Chris Wilson45b9c962019-05-01 11:32:04 +01001536 intel_engines_cleanup(dev_priv);
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00001537 }
1538err_context:
1539 if (ret != -EIO)
1540 i915_gem_contexts_fini(dev_priv);
Chris Wilson51797492018-12-04 14:15:16 +00001541err_scratch:
1542 i915_gem_fini_scratch(dev_priv);
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00001543err_ggtt:
1544err_unlock:
Daniele Ceraolo Spurio3ceea6a2019-03-19 11:35:36 -07001545 intel_uncore_forcewake_put(&dev_priv->uncore, FORCEWAKE_ALL);
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00001546 mutex_unlock(&dev_priv->drm.struct_mutex);
1547
Chris Wilson1e345562019-01-28 10:23:56 +00001548 if (ret != -EIO) {
Michal Wajdeczkoa5f978c2019-08-11 19:51:32 +00001549 intel_uc_cleanup_firmwares(&dev_priv->gt.uc);
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00001550 i915_gem_cleanup_userptr(dev_priv);
Tvrtko Ursulinf0c02c12019-06-21 08:08:10 +01001551 intel_timelines_fini(dev_priv);
Chris Wilson1e345562019-01-28 10:23:56 +00001552 }
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00001553
Chris Wilson60990322014-04-09 09:19:42 +01001554 if (ret == -EIO) {
Chris Wilson7ed43df2018-07-26 09:50:32 +01001555 mutex_lock(&dev_priv->drm.struct_mutex);
1556
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00001557 /*
Michal Wajdeczkoa5f978c2019-08-11 19:51:32 +00001558 * Allow engines or uC initialisation to fail by marking the GPU
1559 * as wedged. But we only want to do this when the GPU is angry,
Chris Wilson60990322014-04-09 09:19:42 +01001560 * for all other failure, such as an allocation failure, bail.
1561 */
Chris Wilsoncb823ed2019-07-12 20:29:53 +01001562 if (!intel_gt_is_wedged(&dev_priv->gt)) {
Janusz Krzysztofikf2db53f2019-07-12 13:24:27 +02001563 i915_probe_error(dev_priv,
1564 "Failed to initialize GPU, declaring it wedged!\n");
Chris Wilsoncb823ed2019-07-12 20:29:53 +01001565 intel_gt_set_wedged(&dev_priv->gt);
Chris Wilson6f74b362017-10-15 15:37:25 +01001566 }
Chris Wilson7ed43df2018-07-26 09:50:32 +01001567
1568 /* Minimal basic recovery for KMS */
1569 ret = i915_ggtt_enable_hw(dev_priv);
1570 i915_gem_restore_gtt_mappings(dev_priv);
1571 i915_gem_restore_fences(dev_priv);
1572 intel_init_clock_gating(dev_priv);
1573
1574 mutex_unlock(&dev_priv->drm.struct_mutex);
Chris Wilson1070a422012-04-24 15:47:41 +01001575 }
1576
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00001577 i915_gem_drain_freed_objects(dev_priv);
Chris Wilson60990322014-04-09 09:19:42 +01001578 return ret;
Chris Wilson1070a422012-04-24 15:47:41 +01001579}
1580
Chris Wilsonc29579d2019-08-06 13:42:59 +01001581void i915_gem_driver_register(struct drm_i915_private *i915)
1582{
1583 i915_gem_driver_register__shrinker(i915);
Chris Wilson750e76b2019-08-06 13:43:00 +01001584
1585 intel_engines_driver_register(i915);
Chris Wilsonc29579d2019-08-06 13:42:59 +01001586}
1587
1588void i915_gem_driver_unregister(struct drm_i915_private *i915)
1589{
1590 i915_gem_driver_unregister__shrinker(i915);
1591}
1592
Janusz Krzysztofik78dae1a2019-07-12 13:24:29 +02001593void i915_gem_driver_remove(struct drm_i915_private *dev_priv)
Michal Wajdeczko8979187a2018-06-04 09:00:32 +00001594{
Chris Wilson79ffac852019-04-24 21:07:17 +01001595 GEM_BUG_ON(dev_priv->gt.awake);
1596
Chris Wilson0cf289b2019-06-13 08:32:54 +01001597 intel_wakeref_auto_fini(&dev_priv->ggtt.userfault_wakeref);
Chris Wilsonb27e35a2019-05-27 12:51:14 +01001598
Michal Wajdeczko8979187a2018-06-04 09:00:32 +00001599 i915_gem_suspend_late(dev_priv);
Chris Wilson30b710842018-08-12 23:36:29 +01001600 intel_disable_gt_powersave(dev_priv);
Michal Wajdeczko8979187a2018-06-04 09:00:32 +00001601
1602 /* Flush any outstanding unpin_work. */
1603 i915_gem_drain_workqueue(dev_priv);
1604
1605 mutex_lock(&dev_priv->drm.struct_mutex);
Daniele Ceraolo Spurioca7b2c12019-07-13 11:00:13 +01001606 intel_uc_fini_hw(&dev_priv->gt.uc);
1607 intel_uc_fini(&dev_priv->gt.uc);
Janusz Krzysztofik47bc28d2019-05-30 15:31:05 +02001608 mutex_unlock(&dev_priv->drm.struct_mutex);
1609
1610 i915_gem_drain_freed_objects(dev_priv);
1611}
1612
Janusz Krzysztofik3b58a942019-07-12 13:24:28 +02001613void i915_gem_driver_release(struct drm_i915_private *dev_priv)
Janusz Krzysztofik47bc28d2019-05-30 15:31:05 +02001614{
1615 mutex_lock(&dev_priv->drm.struct_mutex);
Chris Wilson45b9c962019-05-01 11:32:04 +01001616 intel_engines_cleanup(dev_priv);
Michal Wajdeczko8979187a2018-06-04 09:00:32 +00001617 i915_gem_contexts_fini(dev_priv);
Chris Wilson51797492018-12-04 14:15:16 +00001618 i915_gem_fini_scratch(dev_priv);
Michal Wajdeczko8979187a2018-06-04 09:00:32 +00001619 mutex_unlock(&dev_priv->drm.struct_mutex);
1620
Tvrtko Ursulin25d140f2018-12-03 13:33:19 +00001621 intel_wa_list_free(&dev_priv->gt_wa_list);
1622
Chris Wilson30b710842018-08-12 23:36:29 +01001623 intel_cleanup_gt_powersave(dev_priv);
1624
Daniele Ceraolo Spurioca7b2c12019-07-13 11:00:13 +01001625 intel_uc_cleanup_firmwares(&dev_priv->gt.uc);
Michal Wajdeczko8979187a2018-06-04 09:00:32 +00001626 i915_gem_cleanup_userptr(dev_priv);
Tvrtko Ursulinf0c02c12019-06-21 08:08:10 +01001627 intel_timelines_fini(dev_priv);
Michal Wajdeczko8979187a2018-06-04 09:00:32 +00001628
1629 i915_gem_drain_freed_objects(dev_priv);
1630
1631 WARN_ON(!list_empty(&dev_priv->contexts.list));
1632}
1633
Chris Wilson24145512017-01-24 11:01:35 +00001634void i915_gem_init_mmio(struct drm_i915_private *i915)
1635{
1636 i915_gem_sanitize(i915);
1637}
1638
Chris Wilson9c52d1c2017-11-10 23:24:47 +00001639static void i915_gem_init__mm(struct drm_i915_private *i915)
1640{
Chris Wilson9c52d1c2017-11-10 23:24:47 +00001641 spin_lock_init(&i915->mm.obj_lock);
Chris Wilson9c52d1c2017-11-10 23:24:47 +00001642
1643 init_llist_head(&i915->mm.free_list);
1644
Chris Wilson3b4fa962019-05-30 21:34:59 +01001645 INIT_LIST_HEAD(&i915->mm.purge_list);
Chris Wilsonecab9be2019-06-12 11:57:20 +01001646 INIT_LIST_HEAD(&i915->mm.shrink_list);
Chris Wilson9c52d1c2017-11-10 23:24:47 +00001647
Chris Wilson84753552019-05-28 10:29:45 +01001648 i915_gem_init__objects(i915);
Chris Wilson9c52d1c2017-11-10 23:24:47 +00001649}
1650
Michal Wajdeczkoa0de9082018-03-23 12:34:49 +00001651int i915_gem_init_early(struct drm_i915_private *dev_priv)
Eric Anholt673a3942008-07-30 12:06:12 -07001652{
Chris Wilson13f1bfd2019-02-28 10:20:34 +00001653 int err;
Chris Wilsond1b48c12017-08-16 09:52:08 +01001654
Chris Wilson9c52d1c2017-11-10 23:24:47 +00001655 i915_gem_init__mm(dev_priv);
Chris Wilson23c3c3d2019-04-24 21:07:14 +01001656 i915_gem_init__pm(dev_priv);
Chris Wilsonf2123812017-10-16 12:40:37 +01001657
Chris Wilsonb5add952016-08-04 16:32:36 +01001658 spin_lock_init(&dev_priv->fb_tracking.lock);
Chris Wilson73cb9702016-10-28 13:58:46 +01001659
Matthew Auld465c4032017-10-06 23:18:14 +01001660 err = i915_gemfs_init(dev_priv);
1661 if (err)
1662 DRM_NOTE("Unable to create a private tmpfs mount, hugepage support will be disabled(%d).\n", err);
1663
Chris Wilson73cb9702016-10-28 13:58:46 +01001664 return 0;
Eric Anholt673a3942008-07-30 12:06:12 -07001665}
Dave Airlie71acb5e2008-12-30 20:31:46 +10001666
Michal Wajdeczkoa0de9082018-03-23 12:34:49 +00001667void i915_gem_cleanup_early(struct drm_i915_private *dev_priv)
Imre Deakd64aa092016-01-19 15:26:29 +02001668{
Chris Wilsonc4d4c1c2017-02-10 16:35:23 +00001669 i915_gem_drain_freed_objects(dev_priv);
Chris Wilsonc9c704712018-02-19 22:06:31 +00001670 GEM_BUG_ON(!llist_empty(&dev_priv->mm.free_list));
1671 GEM_BUG_ON(atomic_read(&dev_priv->mm.free_count));
Chris Wilsond82b4b22019-05-30 21:35:00 +01001672 WARN_ON(dev_priv->mm.shrink_count);
Matthew Auldea84aa72016-11-17 21:04:11 +00001673
Matthew Auld465c4032017-10-06 23:18:14 +01001674 i915_gemfs_fini(dev_priv);
Imre Deakd64aa092016-01-19 15:26:29 +02001675}
1676
Chris Wilson6a800ea2016-09-21 14:51:07 +01001677int i915_gem_freeze(struct drm_i915_private *dev_priv)
1678{
Chris Wilsond0aa3012017-04-07 11:25:49 +01001679 /* Discard all purgeable objects, let userspace recover those as
1680 * required after resuming.
1681 */
Chris Wilson6a800ea2016-09-21 14:51:07 +01001682 i915_gem_shrink_all(dev_priv);
Chris Wilson6a800ea2016-09-21 14:51:07 +01001683
Chris Wilson6a800ea2016-09-21 14:51:07 +01001684 return 0;
1685}
1686
Chris Wilson95c778d2018-06-01 15:41:25 +01001687int i915_gem_freeze_late(struct drm_i915_private *i915)
Chris Wilson461fb992016-05-14 07:26:33 +01001688{
1689 struct drm_i915_gem_object *obj;
Chris Wilsonecab9be2019-06-12 11:57:20 +01001690 intel_wakeref_t wakeref;
Chris Wilson461fb992016-05-14 07:26:33 +01001691
Chris Wilson95c778d2018-06-01 15:41:25 +01001692 /*
1693 * Called just before we write the hibernation image.
Chris Wilson461fb992016-05-14 07:26:33 +01001694 *
1695 * We need to update the domain tracking to reflect that the CPU
1696 * will be accessing all the pages to create and restore from the
1697 * hibernation, and so upon restoration those pages will be in the
1698 * CPU domain.
1699 *
1700 * To make sure the hibernation image contains the latest state,
1701 * we update that state just before writing out the image.
Chris Wilson7aab2d52016-09-09 20:02:18 +01001702 *
1703 * To try and reduce the hibernation image, we manually shrink
Chris Wilsond0aa3012017-04-07 11:25:49 +01001704 * the objects as well, see i915_gem_freeze()
Chris Wilson461fb992016-05-14 07:26:33 +01001705 */
1706
Daniele Ceraolo Spuriod858d562019-06-13 16:21:54 -07001707 wakeref = intel_runtime_pm_get(&i915->runtime_pm);
Chris Wilsonecab9be2019-06-12 11:57:20 +01001708
1709 i915_gem_shrink(i915, -1UL, NULL, ~0);
Chris Wilson95c778d2018-06-01 15:41:25 +01001710 i915_gem_drain_freed_objects(i915);
Chris Wilson461fb992016-05-14 07:26:33 +01001711
Chris Wilsonecab9be2019-06-12 11:57:20 +01001712 list_for_each_entry(obj, &i915->mm.shrink_list, mm.link) {
1713 i915_gem_object_lock(obj);
1714 WARN_ON(i915_gem_object_set_to_cpu_domain(obj, true));
1715 i915_gem_object_unlock(obj);
Chris Wilson461fb992016-05-14 07:26:33 +01001716 }
Chris Wilsonecab9be2019-06-12 11:57:20 +01001717
Daniele Ceraolo Spuriod858d562019-06-13 16:21:54 -07001718 intel_runtime_pm_put(&i915->runtime_pm, wakeref);
Chris Wilson461fb992016-05-14 07:26:33 +01001719
1720 return 0;
1721}
1722
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001723void i915_gem_release(struct drm_device *dev, struct drm_file *file)
Eric Anholtb9624422009-06-03 07:27:35 +00001724{
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001725 struct drm_i915_file_private *file_priv = file->driver_priv;
Chris Wilsone61e0f52018-02-21 09:56:36 +00001726 struct i915_request *request;
Eric Anholtb9624422009-06-03 07:27:35 +00001727
1728 /* Clean up our request list when the client is going away, so that
1729 * later retire_requests won't dereference our soon-to-be-gone
1730 * file_priv.
1731 */
Chris Wilson1c255952010-09-26 11:03:27 +01001732 spin_lock(&file_priv->mm.lock);
Chris Wilsonc8659ef2017-03-02 12:25:25 +00001733 list_for_each_entry(request, &file_priv->mm.request_list, client_link)
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001734 request->file_priv = NULL;
Chris Wilson1c255952010-09-26 11:03:27 +01001735 spin_unlock(&file_priv->mm.lock);
Chris Wilsonb29c19b2013-09-25 17:34:56 +01001736}
1737
Chris Wilson829a0af2017-06-20 12:05:45 +01001738int i915_gem_open(struct drm_i915_private *i915, struct drm_file *file)
Chris Wilsonb29c19b2013-09-25 17:34:56 +01001739{
1740 struct drm_i915_file_private *file_priv;
Ben Widawskye422b882013-12-06 14:10:58 -08001741 int ret;
Chris Wilsonb29c19b2013-09-25 17:34:56 +01001742
Chris Wilsonc4c29d72016-11-09 10:45:07 +00001743 DRM_DEBUG("\n");
Chris Wilsonb29c19b2013-09-25 17:34:56 +01001744
1745 file_priv = kzalloc(sizeof(*file_priv), GFP_KERNEL);
1746 if (!file_priv)
1747 return -ENOMEM;
1748
1749 file->driver_priv = file_priv;
Chris Wilson829a0af2017-06-20 12:05:45 +01001750 file_priv->dev_priv = i915;
Chris Wilsonab0e7ff2014-02-25 17:11:24 +02001751 file_priv->file = file;
Chris Wilsonb29c19b2013-09-25 17:34:56 +01001752
1753 spin_lock_init(&file_priv->mm.lock);
1754 INIT_LIST_HEAD(&file_priv->mm.request_list);
Chris Wilsonb29c19b2013-09-25 17:34:56 +01001755
Chris Wilsonc80ff162016-07-27 09:07:27 +01001756 file_priv->bsd_engine = -1;
Mika Kuoppala14921f32018-06-15 13:44:29 +03001757 file_priv->hang_timestamp = jiffies;
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00001758
Chris Wilson829a0af2017-06-20 12:05:45 +01001759 ret = i915_gem_context_open(i915, file);
Ben Widawskye422b882013-12-06 14:10:58 -08001760 if (ret)
1761 kfree(file_priv);
Chris Wilsonb29c19b2013-09-25 17:34:56 +01001762
Ben Widawskye422b882013-12-06 14:10:58 -08001763 return ret;
Chris Wilsonb29c19b2013-09-25 17:34:56 +01001764}
1765
Daniel Vetterb680c372014-09-19 18:27:27 +02001766/**
1767 * i915_gem_track_fb - update frontbuffer tracking
Geliang Tangd9072a32015-09-15 05:58:44 -07001768 * @old: current GEM buffer for the frontbuffer slots
1769 * @new: new GEM buffer for the frontbuffer slots
1770 * @frontbuffer_bits: bitmask of frontbuffer slots
Daniel Vetterb680c372014-09-19 18:27:27 +02001771 *
1772 * This updates the frontbuffer tracking bits @frontbuffer_bits by clearing them
1773 * from @old and setting them in @new. Both @old and @new can be NULL.
1774 */
Daniel Vettera071fa02014-06-18 23:28:09 +02001775void i915_gem_track_fb(struct drm_i915_gem_object *old,
1776 struct drm_i915_gem_object *new,
1777 unsigned frontbuffer_bits)
1778{
Chris Wilsonfaf5bf02016-08-04 16:32:37 +01001779 /* Control of individual bits within the mask are guarded by
1780 * the owning plane->mutex, i.e. we can never see concurrent
1781 * manipulation of individual bits. But since the bitfield as a whole
1782 * is updated using RMW, we need to use atomics in order to update
1783 * the bits.
1784 */
1785 BUILD_BUG_ON(INTEL_FRONTBUFFER_BITS_PER_PIPE * I915_MAX_PIPES >
Chris Wilson74f6e182018-09-26 11:47:07 +01001786 BITS_PER_TYPE(atomic_t));
Chris Wilsonfaf5bf02016-08-04 16:32:37 +01001787
Daniel Vettera071fa02014-06-18 23:28:09 +02001788 if (old) {
Chris Wilsonfaf5bf02016-08-04 16:32:37 +01001789 WARN_ON(!(atomic_read(&old->frontbuffer_bits) & frontbuffer_bits));
1790 atomic_andnot(frontbuffer_bits, &old->frontbuffer_bits);
Daniel Vettera071fa02014-06-18 23:28:09 +02001791 }
1792
1793 if (new) {
Chris Wilsonfaf5bf02016-08-04 16:32:37 +01001794 WARN_ON(atomic_read(&new->frontbuffer_bits) & frontbuffer_bits);
1795 atomic_or(frontbuffer_bits, &new->frontbuffer_bits);
Daniel Vettera071fa02014-06-18 23:28:09 +02001796 }
1797}
1798
Chris Wilson935a2f72017-02-13 17:15:13 +00001799#if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
Chris Wilson66d9cb52017-02-13 17:15:17 +00001800#include "selftests/mock_gem_device.c"
Chris Wilson3f51b7e12018-08-30 14:48:06 +01001801#include "selftests/i915_gem.c"
Chris Wilson935a2f72017-02-13 17:15:13 +00001802#endif