blob: 096e31e3df92b55a6a4df5dc05f17b58262a1cf4 [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
Chris Wilson10be98a2019-05-28 10:29:49 +010041#include "gem/i915_gem_clflush.h"
42#include "gem/i915_gem_context.h"
Chris Wilsonafa13082019-05-28 10:29:43 +010043#include "gem/i915_gem_ioctls.h"
Chris Wilson10be98a2019-05-28 10:29:49 +010044#include "gem/i915_gem_pm.h"
45#include "gem/i915_gemfs.h"
Chris Wilson79ffac852019-04-24 21:07:17 +010046#include "gt/intel_engine_pm.h"
47#include "gt/intel_gt_pm.h"
Chris Wilson112ed2d2019-04-24 18:48:39 +010048#include "gt/intel_mocs.h"
49#include "gt/intel_reset.h"
50#include "gt/intel_workarounds.h"
51
Chris Wilson9f588922019-01-16 15:33:04 +000052#include "i915_drv.h"
Chris Wilson9f588922019-01-16 15:33:04 +000053#include "i915_trace.h"
54#include "i915_vgpu.h"
55
Ville Syrjäläaa5ca8b2019-05-09 15:21:57 +030056#include "intel_display.h"
Chris Wilson9f588922019-01-16 15:33:04 +000057#include "intel_drv.h"
58#include "intel_frontbuffer.h"
Jani Nikula696173b2019-04-05 14:00:15 +030059#include "intel_pm.h"
Chris Wilson9f588922019-01-16 15:33:04 +000060
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +053061static int
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +010062insert_mappable_node(struct i915_ggtt *ggtt,
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +053063 struct drm_mm_node *node, u32 size)
64{
65 memset(node, 0, sizeof(*node));
Chris Wilson82ad6442018-06-05 16:37:58 +010066 return drm_mm_insert_node_in_range(&ggtt->vm.mm, node,
Chris Wilson4e64e552017-02-02 21:04:38 +000067 size, 0, I915_COLOR_UNEVICTABLE,
68 0, ggtt->mappable_end,
69 DRM_MM_INSERT_LOW);
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +053070}
71
72static void
73remove_mappable_node(struct drm_mm_node *node)
74{
75 drm_mm_remove_node(node);
76}
77
Eric Anholt673a3942008-07-30 12:06:12 -070078int
Eric Anholt5a125c32008-10-22 21:40:13 -070079i915_gem_get_aperture_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +000080 struct drm_file *file)
Eric Anholt5a125c32008-10-22 21:40:13 -070081{
Chris Wilson09d7e462019-01-28 10:23:53 +000082 struct i915_ggtt *ggtt = &to_i915(dev)->ggtt;
Joonas Lahtinen72e96d62016-03-30 16:57:10 +030083 struct drm_i915_gem_get_aperture *args = data;
Tvrtko Ursulinca1543b2015-07-01 11:51:10 +010084 struct i915_vma *vma;
Weinan Liff8f7972017-05-31 10:35:52 +080085 u64 pinned;
Eric Anholt5a125c32008-10-22 21:40:13 -070086
Chris Wilson09d7e462019-01-28 10:23:53 +000087 mutex_lock(&ggtt->vm.mutex);
88
Chris Wilson82ad6442018-06-05 16:37:58 +010089 pinned = ggtt->vm.reserved;
Chris Wilson499197d2019-01-28 10:23:52 +000090 list_for_each_entry(vma, &ggtt->vm.bound_list, vm_link)
Chris Wilson20dfbde2016-08-04 16:32:30 +010091 if (i915_vma_is_pinned(vma))
Tvrtko Ursulinca1543b2015-07-01 11:51:10 +010092 pinned += vma->node.size;
Chris Wilson09d7e462019-01-28 10:23:53 +000093
94 mutex_unlock(&ggtt->vm.mutex);
Eric Anholt5a125c32008-10-22 21:40:13 -070095
Chris Wilson82ad6442018-06-05 16:37:58 +010096 args->aper_size = ggtt->vm.total;
Akshay Joshi0206e352011-08-16 15:34:10 -040097 args->aper_available_size = args->aper_size - pinned;
Chris Wilson6299f992010-11-24 12:23:44 +000098
Eric Anholt5a125c32008-10-22 21:40:13 -070099 return 0;
100}
101
Chris Wilson35a96112016-08-14 18:44:40 +0100102int i915_gem_object_unbind(struct drm_i915_gem_object *obj)
Chris Wilsonaa653a62016-08-04 07:52:27 +0100103{
104 struct i915_vma *vma;
105 LIST_HEAD(still_in_list);
Chris Wilson02bef8f2016-08-14 18:44:41 +0100106 int ret;
Chris Wilsonaa653a62016-08-04 07:52:27 +0100107
Chris Wilson02bef8f2016-08-14 18:44:41 +0100108 lockdep_assert_held(&obj->base.dev->struct_mutex);
109
110 /* Closed vma are removed from the obj->vma_list - but they may
111 * still have an active binding on the object. To remove those we
112 * must wait for all rendering to complete to the object (as unbinding
113 * must anyway), and retire the requests.
Chris Wilsonaa653a62016-08-04 07:52:27 +0100114 */
Chris Wilson5888fc92017-12-04 13:25:13 +0000115 ret = i915_gem_object_set_to_cpu_domain(obj, false);
Chris Wilson02bef8f2016-08-14 18:44:41 +0100116 if (ret)
117 return ret;
118
Chris Wilson528cbd12019-01-28 10:23:54 +0000119 spin_lock(&obj->vma.lock);
120 while (!ret && (vma = list_first_entry_or_null(&obj->vma.list,
121 struct i915_vma,
122 obj_link))) {
Chris Wilsonaa653a62016-08-04 07:52:27 +0100123 list_move_tail(&vma->obj_link, &still_in_list);
Chris Wilson528cbd12019-01-28 10:23:54 +0000124 spin_unlock(&obj->vma.lock);
125
Chris Wilsonaa653a62016-08-04 07:52:27 +0100126 ret = i915_vma_unbind(vma);
Chris Wilson528cbd12019-01-28 10:23:54 +0000127
128 spin_lock(&obj->vma.lock);
Chris Wilsonaa653a62016-08-04 07:52:27 +0100129 }
Chris Wilson528cbd12019-01-28 10:23:54 +0000130 list_splice(&still_in_list, &obj->vma.list);
131 spin_unlock(&obj->vma.lock);
Chris Wilsonaa653a62016-08-04 07:52:27 +0100132
133 return ret;
134}
135
Chris Wilsone95433c2016-10-28 13:58:27 +0100136static long
137i915_gem_object_wait_fence(struct dma_fence *fence,
138 unsigned int flags,
Chris Wilson62eb3c22019-02-13 09:25:04 +0000139 long timeout)
Chris Wilsone95433c2016-10-28 13:58:27 +0100140{
Chris Wilsone61e0f52018-02-21 09:56:36 +0000141 struct i915_request *rq;
Chris Wilsone95433c2016-10-28 13:58:27 +0100142
143 BUILD_BUG_ON(I915_WAIT_INTERRUPTIBLE != 0x1);
144
145 if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags))
146 return timeout;
147
148 if (!dma_fence_is_i915(fence))
149 return dma_fence_wait_timeout(fence,
150 flags & I915_WAIT_INTERRUPTIBLE,
151 timeout);
152
153 rq = to_request(fence);
Chris Wilsone61e0f52018-02-21 09:56:36 +0000154 if (i915_request_completed(rq))
Chris Wilsone95433c2016-10-28 13:58:27 +0100155 goto out;
156
Chris Wilsone61e0f52018-02-21 09:56:36 +0000157 timeout = i915_request_wait(rq, flags, timeout);
Chris Wilsone95433c2016-10-28 13:58:27 +0100158
159out:
Chris Wilsone61e0f52018-02-21 09:56:36 +0000160 if (flags & I915_WAIT_LOCKED && i915_request_completed(rq))
161 i915_request_retire_upto(rq);
Chris Wilsone95433c2016-10-28 13:58:27 +0100162
Chris Wilsone95433c2016-10-28 13:58:27 +0100163 return timeout;
164}
165
166static long
167i915_gem_object_wait_reservation(struct reservation_object *resv,
168 unsigned int flags,
Chris Wilson62eb3c22019-02-13 09:25:04 +0000169 long timeout)
Chris Wilsone95433c2016-10-28 13:58:27 +0100170{
Chris Wilsone54ca972017-02-17 15:13:04 +0000171 unsigned int seq = __read_seqcount_begin(&resv->seq);
Chris Wilsone95433c2016-10-28 13:58:27 +0100172 struct dma_fence *excl;
Chris Wilsone54ca972017-02-17 15:13:04 +0000173 bool prune_fences = false;
Chris Wilsone95433c2016-10-28 13:58:27 +0100174
175 if (flags & I915_WAIT_ALL) {
176 struct dma_fence **shared;
177 unsigned int count, i;
178 int ret;
179
180 ret = reservation_object_get_fences_rcu(resv,
181 &excl, &count, &shared);
182 if (ret)
183 return ret;
184
185 for (i = 0; i < count; i++) {
186 timeout = i915_gem_object_wait_fence(shared[i],
Chris Wilson62eb3c22019-02-13 09:25:04 +0000187 flags, timeout);
Chris Wilsond892e932017-02-12 21:53:43 +0000188 if (timeout < 0)
Chris Wilsone95433c2016-10-28 13:58:27 +0100189 break;
190
191 dma_fence_put(shared[i]);
192 }
193
194 for (; i < count; i++)
195 dma_fence_put(shared[i]);
196 kfree(shared);
Chris Wilsone54ca972017-02-17 15:13:04 +0000197
Chris Wilsonfa730552018-03-07 17:13:03 +0000198 /*
199 * If both shared fences and an exclusive fence exist,
200 * then by construction the shared fences must be later
201 * than the exclusive fence. If we successfully wait for
202 * all the shared fences, we know that the exclusive fence
203 * must all be signaled. If all the shared fences are
204 * signaled, we can prune the array and recover the
205 * floating references on the fences/requests.
206 */
Chris Wilsone54ca972017-02-17 15:13:04 +0000207 prune_fences = count && timeout >= 0;
Chris Wilsone95433c2016-10-28 13:58:27 +0100208 } else {
209 excl = reservation_object_get_excl_rcu(resv);
210 }
211
Chris Wilsonfa730552018-03-07 17:13:03 +0000212 if (excl && timeout >= 0)
Chris Wilson62eb3c22019-02-13 09:25:04 +0000213 timeout = i915_gem_object_wait_fence(excl, flags, timeout);
Chris Wilsone95433c2016-10-28 13:58:27 +0100214
215 dma_fence_put(excl);
216
Chris Wilsonfa730552018-03-07 17:13:03 +0000217 /*
218 * Opportunistically prune the fences iff we know they have *all* been
Chris Wilson03d1cac2017-03-08 13:26:28 +0000219 * signaled and that the reservation object has not been changed (i.e.
220 * no new fences have been added).
221 */
Chris Wilsone54ca972017-02-17 15:13:04 +0000222 if (prune_fences && !__read_seqcount_retry(&resv->seq, seq)) {
Chris Wilson03d1cac2017-03-08 13:26:28 +0000223 if (reservation_object_trylock(resv)) {
224 if (!__read_seqcount_retry(&resv->seq, seq))
225 reservation_object_add_excl_fence(resv, NULL);
226 reservation_object_unlock(resv);
227 }
Chris Wilsone54ca972017-02-17 15:13:04 +0000228 }
229
Chris Wilsone95433c2016-10-28 13:58:27 +0100230 return timeout;
231}
232
Chris Wilsonb7268c52018-04-18 19:40:52 +0100233static void __fence_set_priority(struct dma_fence *fence,
234 const struct i915_sched_attr *attr)
Chris Wilson6b5e90f2016-11-14 20:41:05 +0000235{
Chris Wilsone61e0f52018-02-21 09:56:36 +0000236 struct i915_request *rq;
Chris Wilson6b5e90f2016-11-14 20:41:05 +0000237 struct intel_engine_cs *engine;
238
Chris Wilsonc218ee02018-01-06 10:56:18 +0000239 if (dma_fence_is_signaled(fence) || !dma_fence_is_i915(fence))
Chris Wilson6b5e90f2016-11-14 20:41:05 +0000240 return;
241
242 rq = to_request(fence);
243 engine = rq->engine;
Chris Wilson6b5e90f2016-11-14 20:41:05 +0000244
Chris Wilson4f6d8fc2018-05-07 14:57:25 +0100245 local_bh_disable();
246 rcu_read_lock(); /* RCU serialisation for set-wedged protection */
Chris Wilson47650db2018-03-07 13:42:25 +0000247 if (engine->schedule)
Chris Wilsonb7268c52018-04-18 19:40:52 +0100248 engine->schedule(rq, attr);
Chris Wilson47650db2018-03-07 13:42:25 +0000249 rcu_read_unlock();
Chris Wilson4f6d8fc2018-05-07 14:57:25 +0100250 local_bh_enable(); /* kick the tasklets if queues were reprioritised */
Chris Wilson6b5e90f2016-11-14 20:41:05 +0000251}
252
Chris Wilsonb7268c52018-04-18 19:40:52 +0100253static void fence_set_priority(struct dma_fence *fence,
254 const struct i915_sched_attr *attr)
Chris Wilson6b5e90f2016-11-14 20:41:05 +0000255{
256 /* Recurse once into a fence-array */
257 if (dma_fence_is_array(fence)) {
258 struct dma_fence_array *array = to_dma_fence_array(fence);
259 int i;
260
261 for (i = 0; i < array->num_fences; i++)
Chris Wilsonb7268c52018-04-18 19:40:52 +0100262 __fence_set_priority(array->fences[i], attr);
Chris Wilson6b5e90f2016-11-14 20:41:05 +0000263 } else {
Chris Wilsonb7268c52018-04-18 19:40:52 +0100264 __fence_set_priority(fence, attr);
Chris Wilson6b5e90f2016-11-14 20:41:05 +0000265 }
266}
267
268int
269i915_gem_object_wait_priority(struct drm_i915_gem_object *obj,
270 unsigned int flags,
Chris Wilsonb7268c52018-04-18 19:40:52 +0100271 const struct i915_sched_attr *attr)
Chris Wilson6b5e90f2016-11-14 20:41:05 +0000272{
273 struct dma_fence *excl;
274
275 if (flags & I915_WAIT_ALL) {
276 struct dma_fence **shared;
277 unsigned int count, i;
278 int ret;
279
280 ret = reservation_object_get_fences_rcu(obj->resv,
281 &excl, &count, &shared);
282 if (ret)
283 return ret;
284
285 for (i = 0; i < count; i++) {
Chris Wilsonb7268c52018-04-18 19:40:52 +0100286 fence_set_priority(shared[i], attr);
Chris Wilson6b5e90f2016-11-14 20:41:05 +0000287 dma_fence_put(shared[i]);
288 }
289
290 kfree(shared);
291 } else {
292 excl = reservation_object_get_excl_rcu(obj->resv);
293 }
294
295 if (excl) {
Chris Wilsonb7268c52018-04-18 19:40:52 +0100296 fence_set_priority(excl, attr);
Chris Wilson6b5e90f2016-11-14 20:41:05 +0000297 dma_fence_put(excl);
298 }
299 return 0;
300}
301
Chris Wilson00e60f22016-08-04 16:32:40 +0100302/**
Chris Wilsone95433c2016-10-28 13:58:27 +0100303 * Waits for rendering to the object to be completed
Chris Wilson00e60f22016-08-04 16:32:40 +0100304 * @obj: i915 gem object
Chris Wilsone95433c2016-10-28 13:58:27 +0100305 * @flags: how to wait (under a lock, for all rendering or just for writes etc)
306 * @timeout: how long to wait
Chris Wilson00e60f22016-08-04 16:32:40 +0100307 */
308int
Chris Wilsone95433c2016-10-28 13:58:27 +0100309i915_gem_object_wait(struct drm_i915_gem_object *obj,
310 unsigned int flags,
Chris Wilson62eb3c22019-02-13 09:25:04 +0000311 long timeout)
Chris Wilson00e60f22016-08-04 16:32:40 +0100312{
Chris Wilsone95433c2016-10-28 13:58:27 +0100313 might_sleep();
Chris Wilsone95433c2016-10-28 13:58:27 +0100314 GEM_BUG_ON(timeout < 0);
Chris Wilson00e60f22016-08-04 16:32:40 +0100315
Chris Wilson62eb3c22019-02-13 09:25:04 +0000316 timeout = i915_gem_object_wait_reservation(obj->resv, flags, timeout);
Chris Wilsone95433c2016-10-28 13:58:27 +0100317 return timeout < 0 ? timeout : 0;
Chris Wilson00e60f22016-08-04 16:32:40 +0100318}
319
Chris Wilson00731152014-05-21 12:42:56 +0100320static int
321i915_gem_phys_pwrite(struct drm_i915_gem_object *obj,
322 struct drm_i915_gem_pwrite *args,
Chris Wilson03ac84f2016-10-28 13:58:36 +0100323 struct drm_file *file)
Chris Wilson00731152014-05-21 12:42:56 +0100324{
Chris Wilson00731152014-05-21 12:42:56 +0100325 void *vaddr = obj->phys_handle->vaddr + args->offset;
Gustavo Padovan3ed605b2016-04-26 12:32:27 -0300326 char __user *user_data = u64_to_user_ptr(args->data_ptr);
Chris Wilson6a2c4232014-11-04 04:51:40 -0800327
328 /* We manually control the domain here and pretend that it
329 * remains coherent i.e. in the GTT domain, like shmem_pwrite.
330 */
Rodrigo Vivi77a0d1c2015-06-18 11:43:24 -0700331 intel_fb_obj_invalidate(obj, ORIGIN_CPU);
Chris Wilson10466d22017-01-06 15:22:38 +0000332 if (copy_from_user(vaddr, user_data, args->size))
333 return -EFAULT;
Chris Wilson00731152014-05-21 12:42:56 +0100334
Chris Wilson6a2c4232014-11-04 04:51:40 -0800335 drm_clflush_virt_range(vaddr, args->size);
Chris Wilson10466d22017-01-06 15:22:38 +0000336 i915_gem_chipset_flush(to_i915(obj->base.dev));
Paulo Zanoni063e4e62015-02-13 17:23:45 -0200337
Chris Wilsond59b21e2017-02-22 11:40:49 +0000338 intel_fb_obj_flush(obj, ORIGIN_CPU);
Chris Wilson10466d22017-01-06 15:22:38 +0000339 return 0;
Chris Wilson00731152014-05-21 12:42:56 +0100340}
341
Dave Airlieff72145b2011-02-07 12:16:14 +1000342static int
343i915_gem_create(struct drm_file *file,
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +0000344 struct drm_i915_private *dev_priv,
Michał Winiarskie1634842019-03-26 18:02:18 +0100345 u64 *size_p,
Jani Nikula739f3ab2019-01-16 11:15:19 +0200346 u32 *handle_p)
Eric Anholt673a3942008-07-30 12:06:12 -0700347{
Chris Wilson05394f32010-11-08 19:18:58 +0000348 struct drm_i915_gem_object *obj;
Pekka Paalanena1a2d1d2009-08-23 12:40:55 +0300349 u32 handle;
Michał Winiarskie1634842019-03-26 18:02:18 +0100350 u64 size;
351 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700352
Michał Winiarskie1634842019-03-26 18:02:18 +0100353 size = round_up(*size_p, PAGE_SIZE);
Chris Wilson8ffc0242011-09-14 14:14:28 +0200354 if (size == 0)
355 return -EINVAL;
Eric Anholt673a3942008-07-30 12:06:12 -0700356
357 /* Allocate the new object */
Chris Wilson84753552019-05-28 10:29:45 +0100358 obj = i915_gem_object_create_shmem(dev_priv, size);
Chris Wilsonfe3db792016-04-25 13:32:13 +0100359 if (IS_ERR(obj))
360 return PTR_ERR(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700361
Chris Wilson05394f32010-11-08 19:18:58 +0000362 ret = drm_gem_handle_create(file, &obj->base, &handle);
Chris Wilson202f2fe2010-10-14 13:20:40 +0100363 /* drop reference from allocate - handle holds it now */
Chris Wilsonf0cd5182016-10-28 13:58:43 +0100364 i915_gem_object_put(obj);
Daniel Vetterd861e332013-07-24 23:25:03 +0200365 if (ret)
366 return ret;
Chris Wilson202f2fe2010-10-14 13:20:40 +0100367
Dave Airlieff72145b2011-02-07 12:16:14 +1000368 *handle_p = handle;
Chris Wilson99534022019-04-17 14:25:07 +0100369 *size_p = size;
Eric Anholt673a3942008-07-30 12:06:12 -0700370 return 0;
371}
372
Dave Airlieff72145b2011-02-07 12:16:14 +1000373int
374i915_gem_dumb_create(struct drm_file *file,
375 struct drm_device *dev,
376 struct drm_mode_create_dumb *args)
377{
Ville Syrjäläaa5ca8b2019-05-09 15:21:57 +0300378 int cpp = DIV_ROUND_UP(args->bpp, 8);
379 u32 format;
380
381 switch (cpp) {
382 case 1:
383 format = DRM_FORMAT_C8;
384 break;
385 case 2:
386 format = DRM_FORMAT_RGB565;
387 break;
388 case 4:
389 format = DRM_FORMAT_XRGB8888;
390 break;
391 default:
392 return -EINVAL;
393 }
394
Dave Airlieff72145b2011-02-07 12:16:14 +1000395 /* have to work out size/pitch and return them */
Ville Syrjäläaa5ca8b2019-05-09 15:21:57 +0300396 args->pitch = ALIGN(args->width * cpp, 64);
397
398 /* align stride to page size so that we can remap */
399 if (args->pitch > intel_plane_fb_max_stride(to_i915(dev), format,
400 DRM_FORMAT_MOD_LINEAR))
401 args->pitch = ALIGN(args->pitch, 4096);
402
Dave Airlieff72145b2011-02-07 12:16:14 +1000403 args->size = args->pitch * args->height;
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +0000404 return i915_gem_create(file, to_i915(dev),
Michał Winiarskie1634842019-03-26 18:02:18 +0100405 &args->size, &args->handle);
Dave Airlieff72145b2011-02-07 12:16:14 +1000406}
407
Dave Airlieff72145b2011-02-07 12:16:14 +1000408/**
409 * Creates a new mm object and returns a handle to it.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +0100410 * @dev: drm device pointer
411 * @data: ioctl data blob
412 * @file: drm file pointer
Dave Airlieff72145b2011-02-07 12:16:14 +1000413 */
414int
415i915_gem_create_ioctl(struct drm_device *dev, void *data,
416 struct drm_file *file)
417{
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +0000418 struct drm_i915_private *dev_priv = to_i915(dev);
Dave Airlieff72145b2011-02-07 12:16:14 +1000419 struct drm_i915_gem_create *args = data;
Daniel Vetter63ed2cb2012-04-23 16:50:50 +0200420
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +0000421 i915_gem_flush_free_objects(dev_priv);
Chris Wilsonfbbd37b2016-10-28 13:58:42 +0100422
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +0000423 return i915_gem_create(file, dev_priv,
Michał Winiarskie1634842019-03-26 18:02:18 +0100424 &args->size, &args->handle);
Dave Airlieff72145b2011-02-07 12:16:14 +1000425}
426
Chris Wilson7125397b2017-12-06 12:49:14 +0000427void i915_gem_flush_ggtt_writes(struct drm_i915_private *dev_priv)
Chris Wilsonef749212017-04-12 12:01:10 +0100428{
Chris Wilson538ef962019-01-14 14:21:18 +0000429 intel_wakeref_t wakeref;
430
Chris Wilson7125397b2017-12-06 12:49:14 +0000431 /*
432 * No actual flushing is required for the GTT write domain for reads
433 * from the GTT domain. Writes to it "immediately" go to main memory
434 * as far as we know, so there's no chipset flush. It also doesn't
435 * land in the GPU render cache.
Chris Wilsonef749212017-04-12 12:01:10 +0100436 *
437 * However, we do have to enforce the order so that all writes through
438 * the GTT land before any writes to the device, such as updates to
439 * the GATT itself.
440 *
441 * We also have to wait a bit for the writes to land from the GTT.
442 * An uncached read (i.e. mmio) seems to be ideal for the round-trip
443 * timing. This issue has only been observed when switching quickly
444 * between GTT writes and CPU reads from inside the kernel on recent hw,
445 * and it appears to only affect discrete GTT blocks (i.e. on LLC
Chris Wilson7125397b2017-12-06 12:49:14 +0000446 * system agents we cannot reproduce this behaviour, until Cannonlake
447 * that was!).
Chris Wilsonef749212017-04-12 12:01:10 +0100448 */
Chris Wilson7125397b2017-12-06 12:49:14 +0000449
Chris Wilson900ccf32018-07-20 11:19:10 +0100450 wmb();
451
452 if (INTEL_INFO(dev_priv)->has_coherent_ggtt)
453 return;
454
Chris Wilsona8bd3b82018-07-17 10:26:55 +0100455 i915_gem_chipset_flush(dev_priv);
Chris Wilsonef749212017-04-12 12:01:10 +0100456
Chris Wilsond4225a52019-01-14 14:21:23 +0000457 with_intel_runtime_pm(dev_priv, wakeref) {
458 spin_lock_irq(&dev_priv->uncore.lock);
Chris Wilson7125397b2017-12-06 12:49:14 +0000459
Chris Wilsond4225a52019-01-14 14:21:23 +0000460 POSTING_READ_FW(RING_HEAD(RENDER_RING_BASE));
Chris Wilson7125397b2017-12-06 12:49:14 +0000461
Chris Wilsond4225a52019-01-14 14:21:23 +0000462 spin_unlock_irq(&dev_priv->uncore.lock);
463 }
Chris Wilson7125397b2017-12-06 12:49:14 +0000464}
465
Daniel Vetterd174bd62012-03-25 19:47:40 +0200466static int
Chris Wilsonb9d126e2019-01-05 12:07:58 +0000467shmem_pread(struct page *page, int offset, int len, char __user *user_data,
468 bool needs_clflush)
Daniel Vetterd174bd62012-03-25 19:47:40 +0200469{
470 char *vaddr;
471 int ret;
472
473 vaddr = kmap(page);
Daniel Vetterd174bd62012-03-25 19:47:40 +0200474
Chris Wilsonb9d126e2019-01-05 12:07:58 +0000475 if (needs_clflush)
476 drm_clflush_virt_range(vaddr + offset, len);
477
478 ret = __copy_to_user(user_data, vaddr + offset, len);
479
Daniel Vetterd174bd62012-03-25 19:47:40 +0200480 kunmap(page);
481
Chris Wilsonb9d126e2019-01-05 12:07:58 +0000482 return ret ? -EFAULT : 0;
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100483}
484
485static int
486i915_gem_shmem_pread(struct drm_i915_gem_object *obj,
487 struct drm_i915_gem_pread *args)
488{
489 char __user *user_data;
490 u64 remain;
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100491 unsigned int needs_clflush;
492 unsigned int idx, offset;
493 int ret;
494
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100495 ret = mutex_lock_interruptible(&obj->base.dev->struct_mutex);
496 if (ret)
497 return ret;
498
Chris Wilsonf0e4a062019-05-28 10:29:48 +0100499 ret = i915_gem_object_prepare_read(obj, &needs_clflush);
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100500 mutex_unlock(&obj->base.dev->struct_mutex);
501 if (ret)
502 return ret;
503
504 remain = args->size;
505 user_data = u64_to_user_ptr(args->data_ptr);
506 offset = offset_in_page(args->offset);
507 for (idx = args->offset >> PAGE_SHIFT; remain; idx++) {
508 struct page *page = i915_gem_object_get_page(obj, idx);
Chris Wilsona5e856a52018-10-12 15:02:28 +0100509 unsigned int length = min_t(u64, remain, PAGE_SIZE - offset);
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100510
511 ret = shmem_pread(page, offset, length, user_data,
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100512 needs_clflush);
513 if (ret)
514 break;
515
516 remain -= length;
517 user_data += length;
518 offset = 0;
519 }
520
Chris Wilsonf0e4a062019-05-28 10:29:48 +0100521 i915_gem_object_finish_access(obj);
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100522 return ret;
523}
524
525static inline bool
526gtt_user_read(struct io_mapping *mapping,
527 loff_t base, int offset,
528 char __user *user_data, int length)
529{
Ville Syrjäläafe722b2017-09-01 20:12:52 +0300530 void __iomem *vaddr;
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100531 unsigned long unwritten;
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +0530532
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +0530533 /* We can use the cpu mem copy function because this is X86. */
Ville Syrjäläafe722b2017-09-01 20:12:52 +0300534 vaddr = io_mapping_map_atomic_wc(mapping, base);
535 unwritten = __copy_to_user_inatomic(user_data,
536 (void __force *)vaddr + offset,
537 length);
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100538 io_mapping_unmap_atomic(vaddr);
539 if (unwritten) {
Ville Syrjäläafe722b2017-09-01 20:12:52 +0300540 vaddr = io_mapping_map_wc(mapping, base, PAGE_SIZE);
541 unwritten = copy_to_user(user_data,
542 (void __force *)vaddr + offset,
543 length);
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100544 io_mapping_unmap(vaddr);
545 }
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +0530546 return unwritten;
547}
548
549static int
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100550i915_gem_gtt_pread(struct drm_i915_gem_object *obj,
551 const struct drm_i915_gem_pread *args)
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +0530552{
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100553 struct drm_i915_private *i915 = to_i915(obj->base.dev);
554 struct i915_ggtt *ggtt = &i915->ggtt;
Chris Wilson538ef962019-01-14 14:21:18 +0000555 intel_wakeref_t wakeref;
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +0530556 struct drm_mm_node node;
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100557 struct i915_vma *vma;
558 void __user *user_data;
559 u64 remain, offset;
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +0530560 int ret;
561
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100562 ret = mutex_lock_interruptible(&i915->drm.struct_mutex);
563 if (ret)
564 return ret;
565
Chris Wilson538ef962019-01-14 14:21:18 +0000566 wakeref = intel_runtime_pm_get(i915);
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100567 vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0,
Chris Wilsona3259ca2017-10-09 09:44:00 +0100568 PIN_MAPPABLE |
569 PIN_NONFAULT |
570 PIN_NONBLOCK);
Chris Wilson18034582016-08-18 17:16:45 +0100571 if (!IS_ERR(vma)) {
572 node.start = i915_ggtt_offset(vma);
573 node.allocated = false;
Chris Wilson49ef5292016-08-18 17:17:00 +0100574 ret = i915_vma_put_fence(vma);
Chris Wilson18034582016-08-18 17:16:45 +0100575 if (ret) {
576 i915_vma_unpin(vma);
577 vma = ERR_PTR(ret);
578 }
579 }
Chris Wilson058d88c2016-08-15 10:49:06 +0100580 if (IS_ERR(vma)) {
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100581 ret = insert_mappable_node(ggtt, &node, PAGE_SIZE);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +0530582 if (ret)
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100583 goto out_unlock;
584 GEM_BUG_ON(!node.allocated);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +0530585 }
586
587 ret = i915_gem_object_set_to_gtt_domain(obj, false);
588 if (ret)
589 goto out_unpin;
590
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100591 mutex_unlock(&i915->drm.struct_mutex);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +0530592
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100593 user_data = u64_to_user_ptr(args->data_ptr);
594 remain = args->size;
595 offset = args->offset;
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +0530596
597 while (remain > 0) {
598 /* Operation in this page
599 *
600 * page_base = page offset within aperture
601 * page_offset = offset within page
602 * page_length = bytes to copy for this page
603 */
604 u32 page_base = node.start;
605 unsigned page_offset = offset_in_page(offset);
606 unsigned page_length = PAGE_SIZE - page_offset;
607 page_length = remain < page_length ? remain : page_length;
608 if (node.allocated) {
609 wmb();
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 Sharmab50a5372016-06-10 14:23:03 +0530613 wmb();
614 } else {
615 page_base += offset & PAGE_MASK;
616 }
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100617
Matthew Auld73ebd502017-12-11 15:18:20 +0000618 if (gtt_user_read(&ggtt->iomap, page_base, page_offset,
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100619 user_data, page_length)) {
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +0530620 ret = -EFAULT;
621 break;
622 }
623
624 remain -= page_length;
625 user_data += page_length;
626 offset += page_length;
627 }
628
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100629 mutex_lock(&i915->drm.struct_mutex);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +0530630out_unpin:
631 if (node.allocated) {
632 wmb();
Chris Wilson82ad6442018-06-05 16:37:58 +0100633 ggtt->vm.clear_range(&ggtt->vm, node.start, node.size);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +0530634 remove_mappable_node(&node);
635 } else {
Chris Wilson058d88c2016-08-15 10:49:06 +0100636 i915_vma_unpin(vma);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +0530637 }
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100638out_unlock:
Chris Wilson538ef962019-01-14 14:21:18 +0000639 intel_runtime_pm_put(i915, wakeref);
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100640 mutex_unlock(&i915->drm.struct_mutex);
Chris Wilsonf60d7f02012-09-04 21:02:56 +0100641
Eric Anholteb014592009-03-10 11:44:52 -0700642 return ret;
643}
644
Eric Anholt673a3942008-07-30 12:06:12 -0700645/**
646 * Reads data from the object referenced by handle.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +0100647 * @dev: drm device pointer
648 * @data: ioctl data blob
649 * @file: drm file pointer
Eric Anholt673a3942008-07-30 12:06:12 -0700650 *
651 * On error, the contents of *data are undefined.
652 */
653int
654i915_gem_pread_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +0000655 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -0700656{
657 struct drm_i915_gem_pread *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +0000658 struct drm_i915_gem_object *obj;
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100659 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700660
Chris Wilson51311d02010-11-17 09:10:42 +0000661 if (args->size == 0)
662 return 0;
663
Linus Torvalds96d4f262019-01-03 18:57:57 -0800664 if (!access_ok(u64_to_user_ptr(args->data_ptr),
Chris Wilson51311d02010-11-17 09:10:42 +0000665 args->size))
666 return -EFAULT;
667
Chris Wilson03ac0642016-07-20 13:31:51 +0100668 obj = i915_gem_object_lookup(file, args->handle);
Chris Wilson258a5ed2016-08-05 10:14:16 +0100669 if (!obj)
670 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -0700671
Chris Wilson7dcd2492010-09-26 20:21:44 +0100672 /* Bounds check source. */
Matthew Auld966d5bf2016-12-13 20:32:22 +0000673 if (range_overflows_t(u64, args->offset, args->size, obj->base.size)) {
Chris Wilsonce9d4192010-09-26 20:50:05 +0100674 ret = -EINVAL;
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100675 goto out;
Chris Wilsonce9d4192010-09-26 20:50:05 +0100676 }
677
Chris Wilsondb53a302011-02-03 11:57:46 +0000678 trace_i915_gem_object_pread(obj, args->offset, args->size);
679
Chris Wilsone95433c2016-10-28 13:58:27 +0100680 ret = i915_gem_object_wait(obj,
681 I915_WAIT_INTERRUPTIBLE,
Chris Wilson62eb3c22019-02-13 09:25:04 +0000682 MAX_SCHEDULE_TIMEOUT);
Chris Wilson258a5ed2016-08-05 10:14:16 +0100683 if (ret)
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100684 goto out;
Chris Wilson258a5ed2016-08-05 10:14:16 +0100685
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100686 ret = i915_gem_object_pin_pages(obj);
Chris Wilson258a5ed2016-08-05 10:14:16 +0100687 if (ret)
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100688 goto out;
Chris Wilson258a5ed2016-08-05 10:14:16 +0100689
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100690 ret = i915_gem_shmem_pread(obj, args);
Chris Wilson9c870d02016-10-24 13:42:15 +0100691 if (ret == -EFAULT || ret == -ENODEV)
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100692 ret = i915_gem_gtt_pread(obj, args);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +0530693
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100694 i915_gem_object_unpin_pages(obj);
695out:
Chris Wilsonf0cd5182016-10-28 13:58:43 +0100696 i915_gem_object_put(obj);
Eric Anholteb014592009-03-10 11:44:52 -0700697 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700698}
699
Keith Packard0839ccb2008-10-30 19:38:48 -0700700/* This is the fast write path which cannot handle
701 * page faults in the source data
Linus Torvalds9b7530cc2008-10-20 14:16:43 -0700702 */
Linus Torvalds9b7530cc2008-10-20 14:16:43 -0700703
Chris Wilsonfe115622016-10-28 13:58:40 +0100704static inline bool
705ggtt_write(struct io_mapping *mapping,
706 loff_t base, int offset,
707 char __user *user_data, int length)
Keith Packard0839ccb2008-10-30 19:38:48 -0700708{
Ville Syrjäläafe722b2017-09-01 20:12:52 +0300709 void __iomem *vaddr;
Keith Packard0839ccb2008-10-30 19:38:48 -0700710 unsigned long unwritten;
711
Ben Widawsky4f0c7cf2012-04-16 14:07:47 -0700712 /* We can use the cpu mem copy function because this is X86. */
Ville Syrjäläafe722b2017-09-01 20:12:52 +0300713 vaddr = io_mapping_map_atomic_wc(mapping, base);
714 unwritten = __copy_from_user_inatomic_nocache((void __force *)vaddr + offset,
Keith Packard0839ccb2008-10-30 19:38:48 -0700715 user_data, length);
Chris Wilsonfe115622016-10-28 13:58:40 +0100716 io_mapping_unmap_atomic(vaddr);
717 if (unwritten) {
Ville Syrjäläafe722b2017-09-01 20:12:52 +0300718 vaddr = io_mapping_map_wc(mapping, base, PAGE_SIZE);
719 unwritten = copy_from_user((void __force *)vaddr + offset,
720 user_data, length);
Chris Wilsonfe115622016-10-28 13:58:40 +0100721 io_mapping_unmap(vaddr);
722 }
Keith Packard0839ccb2008-10-30 19:38:48 -0700723
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100724 return unwritten;
725}
726
Eric Anholt3de09aa2009-03-09 09:42:23 -0700727/**
728 * This is the fast pwrite path, where we copy the data directly from the
729 * user into the GTT, uncached.
Chris Wilsonfe115622016-10-28 13:58:40 +0100730 * @obj: i915 GEM object
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +0100731 * @args: pwrite arguments structure
Eric Anholt3de09aa2009-03-09 09:42:23 -0700732 */
Eric Anholt673a3942008-07-30 12:06:12 -0700733static int
Chris Wilsonfe115622016-10-28 13:58:40 +0100734i915_gem_gtt_pwrite_fast(struct drm_i915_gem_object *obj,
735 const struct drm_i915_gem_pwrite *args)
Eric Anholt673a3942008-07-30 12:06:12 -0700736{
Chris Wilsonfe115622016-10-28 13:58:40 +0100737 struct drm_i915_private *i915 = to_i915(obj->base.dev);
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +0530738 struct i915_ggtt *ggtt = &i915->ggtt;
Chris Wilson538ef962019-01-14 14:21:18 +0000739 intel_wakeref_t wakeref;
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +0530740 struct drm_mm_node node;
Chris Wilsonfe115622016-10-28 13:58:40 +0100741 struct i915_vma *vma;
742 u64 remain, offset;
743 void __user *user_data;
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +0530744 int ret;
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +0530745
Chris Wilsonfe115622016-10-28 13:58:40 +0100746 ret = mutex_lock_interruptible(&i915->drm.struct_mutex);
747 if (ret)
748 return ret;
Daniel Vetter935aaa62012-03-25 19:47:35 +0200749
Chris Wilson8bd818152017-10-19 07:37:33 +0100750 if (i915_gem_object_has_struct_page(obj)) {
751 /*
752 * Avoid waking the device up if we can fallback, as
753 * waking/resuming is very slow (worst-case 10-100 ms
754 * depending on PCI sleeps and our own resume time).
755 * This easily dwarfs any performance advantage from
756 * using the cache bypass of indirect GGTT access.
757 */
Chris Wilson538ef962019-01-14 14:21:18 +0000758 wakeref = intel_runtime_pm_get_if_in_use(i915);
759 if (!wakeref) {
Chris Wilson8bd818152017-10-19 07:37:33 +0100760 ret = -EFAULT;
761 goto out_unlock;
762 }
763 } else {
764 /* No backing pages, no fallback, we must force GGTT access */
Chris Wilson538ef962019-01-14 14:21:18 +0000765 wakeref = intel_runtime_pm_get(i915);
Chris Wilson8bd818152017-10-19 07:37:33 +0100766 }
767
Chris Wilson058d88c2016-08-15 10:49:06 +0100768 vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0,
Chris Wilsona3259ca2017-10-09 09:44:00 +0100769 PIN_MAPPABLE |
770 PIN_NONFAULT |
771 PIN_NONBLOCK);
Chris Wilson18034582016-08-18 17:16:45 +0100772 if (!IS_ERR(vma)) {
773 node.start = i915_ggtt_offset(vma);
774 node.allocated = false;
Chris Wilson49ef5292016-08-18 17:17:00 +0100775 ret = i915_vma_put_fence(vma);
Chris Wilson18034582016-08-18 17:16:45 +0100776 if (ret) {
777 i915_vma_unpin(vma);
778 vma = ERR_PTR(ret);
779 }
780 }
Chris Wilson058d88c2016-08-15 10:49:06 +0100781 if (IS_ERR(vma)) {
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100782 ret = insert_mappable_node(ggtt, &node, PAGE_SIZE);
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +0530783 if (ret)
Chris Wilson8bd818152017-10-19 07:37:33 +0100784 goto out_rpm;
Chris Wilsonfe115622016-10-28 13:58:40 +0100785 GEM_BUG_ON(!node.allocated);
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +0530786 }
Daniel Vetter935aaa62012-03-25 19:47:35 +0200787
788 ret = i915_gem_object_set_to_gtt_domain(obj, true);
789 if (ret)
790 goto out_unpin;
791
Chris Wilsonfe115622016-10-28 13:58:40 +0100792 mutex_unlock(&i915->drm.struct_mutex);
793
Chris Wilsonb19482d2016-08-18 17:16:43 +0100794 intel_fb_obj_invalidate(obj, ORIGIN_CPU);
Paulo Zanoni063e4e62015-02-13 17:23:45 -0200795
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +0530796 user_data = u64_to_user_ptr(args->data_ptr);
797 offset = args->offset;
798 remain = args->size;
799 while (remain) {
Eric Anholt673a3942008-07-30 12:06:12 -0700800 /* Operation in this page
801 *
Keith Packard0839ccb2008-10-30 19:38:48 -0700802 * page_base = page offset within aperture
803 * page_offset = offset within page
804 * page_length = bytes to copy for this page
Eric Anholt673a3942008-07-30 12:06:12 -0700805 */
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +0530806 u32 page_base = node.start;
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100807 unsigned int page_offset = offset_in_page(offset);
808 unsigned int page_length = PAGE_SIZE - page_offset;
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +0530809 page_length = remain < page_length ? remain : page_length;
810 if (node.allocated) {
811 wmb(); /* flush the write before we modify the GGTT */
Chris Wilson82ad6442018-06-05 16:37:58 +0100812 ggtt->vm.insert_page(&ggtt->vm,
813 i915_gem_object_get_dma_address(obj, offset >> PAGE_SHIFT),
814 node.start, I915_CACHE_NONE, 0);
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +0530815 wmb(); /* flush modifications to the GGTT (insert_page) */
816 } else {
817 page_base += offset & PAGE_MASK;
818 }
Keith Packard0839ccb2008-10-30 19:38:48 -0700819 /* If we get a fault while copying data, then (presumably) our
Eric Anholt3de09aa2009-03-09 09:42:23 -0700820 * source page isn't available. Return the error and we'll
821 * retry in the slow path.
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +0530822 * If the object is non-shmem backed, we retry again with the
823 * path that handles page fault.
Keith Packard0839ccb2008-10-30 19:38:48 -0700824 */
Matthew Auld73ebd502017-12-11 15:18:20 +0000825 if (ggtt_write(&ggtt->iomap, page_base, page_offset,
Chris Wilsonfe115622016-10-28 13:58:40 +0100826 user_data, page_length)) {
827 ret = -EFAULT;
828 break;
Daniel Vetter935aaa62012-03-25 19:47:35 +0200829 }
Eric Anholt673a3942008-07-30 12:06:12 -0700830
Keith Packard0839ccb2008-10-30 19:38:48 -0700831 remain -= page_length;
832 user_data += page_length;
833 offset += page_length;
Eric Anholt673a3942008-07-30 12:06:12 -0700834 }
Chris Wilsond59b21e2017-02-22 11:40:49 +0000835 intel_fb_obj_flush(obj, ORIGIN_CPU);
Chris Wilsonfe115622016-10-28 13:58:40 +0100836
837 mutex_lock(&i915->drm.struct_mutex);
Daniel Vetter935aaa62012-03-25 19:47:35 +0200838out_unpin:
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +0530839 if (node.allocated) {
840 wmb();
Chris Wilson82ad6442018-06-05 16:37:58 +0100841 ggtt->vm.clear_range(&ggtt->vm, node.start, node.size);
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +0530842 remove_mappable_node(&node);
843 } else {
Chris Wilson058d88c2016-08-15 10:49:06 +0100844 i915_vma_unpin(vma);
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +0530845 }
Chris Wilson8bd818152017-10-19 07:37:33 +0100846out_rpm:
Chris Wilson538ef962019-01-14 14:21:18 +0000847 intel_runtime_pm_put(i915, wakeref);
Chris Wilson8bd818152017-10-19 07:37:33 +0100848out_unlock:
Chris Wilsonfe115622016-10-28 13:58:40 +0100849 mutex_unlock(&i915->drm.struct_mutex);
Eric Anholt3de09aa2009-03-09 09:42:23 -0700850 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700851}
852
Chris Wilsonfe115622016-10-28 13:58:40 +0100853/* Per-page copy function for the shmem pwrite fastpath.
854 * Flushes invalid cachelines before writing to the target if
855 * needs_clflush_before is set and flushes out any written cachelines after
856 * writing if needs_clflush is set.
857 */
Eric Anholt40123c12009-03-09 13:42:30 -0700858static int
Chris Wilsonfe115622016-10-28 13:58:40 +0100859shmem_pwrite(struct page *page, int offset, int len, char __user *user_data,
Chris Wilsonfe115622016-10-28 13:58:40 +0100860 bool needs_clflush_before,
861 bool needs_clflush_after)
Eric Anholt40123c12009-03-09 13:42:30 -0700862{
Chris Wilsonb9d126e2019-01-05 12:07:58 +0000863 char *vaddr;
Chris Wilsonfe115622016-10-28 13:58:40 +0100864 int ret;
Eric Anholt40123c12009-03-09 13:42:30 -0700865
Chris Wilsonb9d126e2019-01-05 12:07:58 +0000866 vaddr = kmap(page);
Chris Wilsonfe115622016-10-28 13:58:40 +0100867
Chris Wilsonb9d126e2019-01-05 12:07:58 +0000868 if (needs_clflush_before)
869 drm_clflush_virt_range(vaddr + offset, len);
Chris Wilsonfe115622016-10-28 13:58:40 +0100870
Chris Wilsonb9d126e2019-01-05 12:07:58 +0000871 ret = __copy_from_user(vaddr + offset, user_data, len);
872 if (!ret && needs_clflush_after)
873 drm_clflush_virt_range(vaddr + offset, len);
Chris Wilsonfe115622016-10-28 13:58:40 +0100874
Chris Wilsonb9d126e2019-01-05 12:07:58 +0000875 kunmap(page);
876
877 return ret ? -EFAULT : 0;
Chris Wilsonfe115622016-10-28 13:58:40 +0100878}
879
880static int
881i915_gem_shmem_pwrite(struct drm_i915_gem_object *obj,
882 const struct drm_i915_gem_pwrite *args)
883{
884 struct drm_i915_private *i915 = to_i915(obj->base.dev);
885 void __user *user_data;
886 u64 remain;
Chris Wilsonfe115622016-10-28 13:58:40 +0100887 unsigned int partial_cacheline_write;
888 unsigned int needs_clflush;
889 unsigned int offset, idx;
890 int ret;
891
892 ret = mutex_lock_interruptible(&i915->drm.struct_mutex);
Chris Wilson43394c72016-08-18 17:16:47 +0100893 if (ret)
894 return ret;
Eric Anholt40123c12009-03-09 13:42:30 -0700895
Chris Wilsonf0e4a062019-05-28 10:29:48 +0100896 ret = i915_gem_object_prepare_write(obj, &needs_clflush);
Chris Wilsonfe115622016-10-28 13:58:40 +0100897 mutex_unlock(&i915->drm.struct_mutex);
898 if (ret)
899 return ret;
900
Chris Wilsonfe115622016-10-28 13:58:40 +0100901 /* If we don't overwrite a cacheline completely we need to be
902 * careful to have up-to-date data by first clflushing. Don't
903 * overcomplicate things and flush the entire patch.
904 */
905 partial_cacheline_write = 0;
906 if (needs_clflush & CLFLUSH_BEFORE)
907 partial_cacheline_write = boot_cpu_data.x86_clflush_size - 1;
908
Chris Wilson43394c72016-08-18 17:16:47 +0100909 user_data = u64_to_user_ptr(args->data_ptr);
Chris Wilson43394c72016-08-18 17:16:47 +0100910 remain = args->size;
Chris Wilsonfe115622016-10-28 13:58:40 +0100911 offset = offset_in_page(args->offset);
912 for (idx = args->offset >> PAGE_SHIFT; remain; idx++) {
913 struct page *page = i915_gem_object_get_page(obj, idx);
Chris Wilsona5e856a52018-10-12 15:02:28 +0100914 unsigned int length = min_t(u64, remain, PAGE_SIZE - offset);
Chris Wilsone5281cc2010-10-28 13:45:36 +0100915
Chris Wilsonfe115622016-10-28 13:58:40 +0100916 ret = shmem_pwrite(page, offset, length, user_data,
Chris Wilsonfe115622016-10-28 13:58:40 +0100917 (offset | length) & partial_cacheline_write,
918 needs_clflush & CLFLUSH_AFTER);
919 if (ret)
Chris Wilson9da3da62012-06-01 15:20:22 +0100920 break;
921
Chris Wilsonfe115622016-10-28 13:58:40 +0100922 remain -= length;
923 user_data += length;
924 offset = 0;
Eric Anholt40123c12009-03-09 13:42:30 -0700925 }
926
Chris Wilsond59b21e2017-02-22 11:40:49 +0000927 intel_fb_obj_flush(obj, ORIGIN_CPU);
Chris Wilsonf0e4a062019-05-28 10:29:48 +0100928 i915_gem_object_finish_access(obj);
Eric Anholt40123c12009-03-09 13:42:30 -0700929 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700930}
931
932/**
933 * Writes data to the object referenced by handle.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +0100934 * @dev: drm device
935 * @data: ioctl data blob
936 * @file: drm file
Eric Anholt673a3942008-07-30 12:06:12 -0700937 *
938 * On error, the contents of the buffer that were to be modified are undefined.
939 */
940int
941i915_gem_pwrite_ioctl(struct drm_device *dev, void *data,
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100942 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -0700943{
944 struct drm_i915_gem_pwrite *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +0000945 struct drm_i915_gem_object *obj;
Chris Wilson51311d02010-11-17 09:10:42 +0000946 int ret;
947
948 if (args->size == 0)
949 return 0;
950
Linus Torvalds96d4f262019-01-03 18:57:57 -0800951 if (!access_ok(u64_to_user_ptr(args->data_ptr), args->size))
Chris Wilson51311d02010-11-17 09:10:42 +0000952 return -EFAULT;
953
Chris Wilson03ac0642016-07-20 13:31:51 +0100954 obj = i915_gem_object_lookup(file, args->handle);
Chris Wilson258a5ed2016-08-05 10:14:16 +0100955 if (!obj)
956 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -0700957
Chris Wilson7dcd2492010-09-26 20:21:44 +0100958 /* Bounds check destination. */
Matthew Auld966d5bf2016-12-13 20:32:22 +0000959 if (range_overflows_t(u64, args->offset, args->size, obj->base.size)) {
Chris Wilsonce9d4192010-09-26 20:50:05 +0100960 ret = -EINVAL;
Chris Wilson258a5ed2016-08-05 10:14:16 +0100961 goto err;
Chris Wilsonce9d4192010-09-26 20:50:05 +0100962 }
963
Chris Wilsonf8c1cce2018-07-12 19:53:14 +0100964 /* Writes not allowed into this read-only object */
965 if (i915_gem_object_is_readonly(obj)) {
966 ret = -EINVAL;
967 goto err;
968 }
969
Chris Wilsondb53a302011-02-03 11:57:46 +0000970 trace_i915_gem_object_pwrite(obj, args->offset, args->size);
971
Chris Wilson7c55e2c2017-03-07 12:03:38 +0000972 ret = -ENODEV;
973 if (obj->ops->pwrite)
974 ret = obj->ops->pwrite(obj, args);
975 if (ret != -ENODEV)
976 goto err;
977
Chris Wilsone95433c2016-10-28 13:58:27 +0100978 ret = i915_gem_object_wait(obj,
979 I915_WAIT_INTERRUPTIBLE |
980 I915_WAIT_ALL,
Chris Wilson62eb3c22019-02-13 09:25:04 +0000981 MAX_SCHEDULE_TIMEOUT);
Chris Wilson258a5ed2016-08-05 10:14:16 +0100982 if (ret)
983 goto err;
984
Chris Wilsonfe115622016-10-28 13:58:40 +0100985 ret = i915_gem_object_pin_pages(obj);
Chris Wilson258a5ed2016-08-05 10:14:16 +0100986 if (ret)
Chris Wilsonfe115622016-10-28 13:58:40 +0100987 goto err;
Chris Wilson258a5ed2016-08-05 10:14:16 +0100988
Daniel Vetter935aaa62012-03-25 19:47:35 +0200989 ret = -EFAULT;
Eric Anholt673a3942008-07-30 12:06:12 -0700990 /* We can only do the GTT pwrite on untiled buffers, as otherwise
991 * it would end up going through the fenced access, and we'll get
992 * different detiling behavior between reading and writing.
993 * pread/pwrite currently are reading and writing from the CPU
994 * perspective, requiring manual detiling by the client.
995 */
Chris Wilson6eae0052016-06-20 15:05:52 +0100996 if (!i915_gem_object_has_struct_page(obj) ||
Chris Wilson9c870d02016-10-24 13:42:15 +0100997 cpu_write_needs_clflush(obj))
Daniel Vetter935aaa62012-03-25 19:47:35 +0200998 /* Note that the gtt paths might fail with non-page-backed user
999 * pointers (e.g. gtt mappings when moving data between
Chris Wilson9c870d02016-10-24 13:42:15 +01001000 * textures). Fallback to the shmem path in that case.
1001 */
Chris Wilsonfe115622016-10-28 13:58:40 +01001002 ret = i915_gem_gtt_pwrite_fast(obj, args);
Eric Anholt673a3942008-07-30 12:06:12 -07001003
Chris Wilsond1054ee2016-07-16 18:42:36 +01001004 if (ret == -EFAULT || ret == -ENOSPC) {
Chris Wilson6a2c4232014-11-04 04:51:40 -08001005 if (obj->phys_handle)
1006 ret = i915_gem_phys_pwrite(obj, args, file);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301007 else
Chris Wilsonfe115622016-10-28 13:58:40 +01001008 ret = i915_gem_shmem_pwrite(obj, args);
Chris Wilson6a2c4232014-11-04 04:51:40 -08001009 }
Daniel Vetter5c0480f2011-12-14 13:57:30 +01001010
Chris Wilsonfe115622016-10-28 13:58:40 +01001011 i915_gem_object_unpin_pages(obj);
Chris Wilson258a5ed2016-08-05 10:14:16 +01001012err:
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001013 i915_gem_object_put(obj);
Chris Wilson258a5ed2016-08-05 10:14:16 +01001014 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07001015}
1016
Eric Anholt673a3942008-07-30 12:06:12 -07001017/**
1018 * Called when user space has done writes to this buffer
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01001019 * @dev: drm device
1020 * @data: ioctl data blob
1021 * @file: drm file
Eric Anholt673a3942008-07-30 12:06:12 -07001022 */
1023int
1024i915_gem_sw_finish_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00001025 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001026{
1027 struct drm_i915_gem_sw_finish *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00001028 struct drm_i915_gem_object *obj;
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001029
Chris Wilson03ac0642016-07-20 13:31:51 +01001030 obj = i915_gem_object_lookup(file, args->handle);
Chris Wilsonc21724c2016-08-05 10:14:19 +01001031 if (!obj)
1032 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07001033
Tina Zhanga03f3952017-11-14 10:25:13 +00001034 /*
1035 * Proxy objects are barred from CPU access, so there is no
1036 * need to ban sw_finish as it is a nop.
1037 */
1038
Eric Anholt673a3942008-07-30 12:06:12 -07001039 /* Pinned buffers may be scanout, so flush the cache */
Chris Wilson5a97bcc2017-02-22 11:40:46 +00001040 i915_gem_object_flush_if_display(obj);
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001041 i915_gem_object_put(obj);
Chris Wilson5a97bcc2017-02-22 11:40:46 +00001042
1043 return 0;
Eric Anholt673a3942008-07-30 12:06:12 -07001044}
1045
Chris Wilson7c108fd2016-10-24 13:42:18 +01001046void i915_gem_runtime_suspend(struct drm_i915_private *dev_priv)
Chris Wilsoneedd10f2014-06-16 08:57:44 +01001047{
Chris Wilson3594a3e2016-10-24 13:42:16 +01001048 struct drm_i915_gem_object *obj, *on;
Chris Wilson7c108fd2016-10-24 13:42:18 +01001049 int i;
Chris Wilsoneedd10f2014-06-16 08:57:44 +01001050
Chris Wilson3594a3e2016-10-24 13:42:16 +01001051 /*
1052 * Only called during RPM suspend. All users of the userfault_list
1053 * must be holding an RPM wakeref to ensure that this can not
1054 * run concurrently with themselves (and use the struct_mutex for
1055 * protection between themselves).
1056 */
1057
1058 list_for_each_entry_safe(obj, on,
Chris Wilsona65adaf2017-10-09 09:43:57 +01001059 &dev_priv->mm.userfault_list, userfault_link)
1060 __i915_gem_object_release_mmap(obj);
Chris Wilson7c108fd2016-10-24 13:42:18 +01001061
1062 /* The fence will be lost when the device powers down. If any were
1063 * in use by hardware (i.e. they are pinned), we should not be powering
1064 * down! All other fences will be reacquired by the user upon waking.
1065 */
1066 for (i = 0; i < dev_priv->num_fence_regs; i++) {
1067 struct drm_i915_fence_reg *reg = &dev_priv->fence_regs[i];
1068
Chris Wilsone0ec3ec2017-02-03 12:57:17 +00001069 /* Ideally we want to assert that the fence register is not
1070 * live at this point (i.e. that no piece of code will be
1071 * trying to write through fence + GTT, as that both violates
1072 * our tracking of activity and associated locking/barriers,
1073 * but also is illegal given that the hw is powered down).
1074 *
1075 * Previously we used reg->pin_count as a "liveness" indicator.
1076 * That is not sufficient, and we need a more fine-grained
1077 * tool if we want to have a sanity check here.
1078 */
Chris Wilson7c108fd2016-10-24 13:42:18 +01001079
1080 if (!reg->vma)
1081 continue;
1082
Chris Wilsona65adaf2017-10-09 09:43:57 +01001083 GEM_BUG_ON(i915_vma_has_userfault(reg->vma));
Chris Wilson7c108fd2016-10-24 13:42:18 +01001084 reg->dirty = true;
1085 }
Chris Wilsoneedd10f2014-06-16 08:57:44 +01001086}
1087
Tvrtko Ursulinf8e57862018-09-26 09:03:53 +01001088bool i915_sg_trim(struct sg_table *orig_st)
Tvrtko Ursulin0c40ce12016-11-09 15:13:43 +00001089{
1090 struct sg_table new_st;
1091 struct scatterlist *sg, *new_sg;
1092 unsigned int i;
1093
1094 if (orig_st->nents == orig_st->orig_nents)
Chris Wilson935a2f72017-02-13 17:15:13 +00001095 return false;
Tvrtko Ursulin0c40ce12016-11-09 15:13:43 +00001096
Chris Wilson8bfc478f2016-12-23 14:57:58 +00001097 if (sg_alloc_table(&new_st, orig_st->nents, GFP_KERNEL | __GFP_NOWARN))
Chris Wilson935a2f72017-02-13 17:15:13 +00001098 return false;
Tvrtko Ursulin0c40ce12016-11-09 15:13:43 +00001099
1100 new_sg = new_st.sgl;
1101 for_each_sg(orig_st->sgl, sg, orig_st->nents, i) {
1102 sg_set_page(new_sg, sg_page(sg), sg->length, 0);
Matthew Auldc6d22ab2018-09-20 15:27:06 +01001103 sg_dma_address(new_sg) = sg_dma_address(sg);
1104 sg_dma_len(new_sg) = sg_dma_len(sg);
1105
Tvrtko Ursulin0c40ce12016-11-09 15:13:43 +00001106 new_sg = sg_next(new_sg);
1107 }
Chris Wilsonc2dc6cc2016-12-19 12:43:46 +00001108 GEM_BUG_ON(new_sg); /* Should walk exactly nents and hit the end */
Tvrtko Ursulin0c40ce12016-11-09 15:13:43 +00001109
1110 sg_free_table(orig_st);
1111
1112 *orig_st = new_st;
Chris Wilson935a2f72017-02-13 17:15:13 +00001113 return true;
Tvrtko Ursulin0c40ce12016-11-09 15:13:43 +00001114}
1115
Chris Wilsone95433c2016-10-28 13:58:27 +01001116static unsigned long to_wait_timeout(s64 timeout_ns)
1117{
1118 if (timeout_ns < 0)
1119 return MAX_SCHEDULE_TIMEOUT;
1120
1121 if (timeout_ns == 0)
1122 return 0;
1123
1124 return nsecs_to_jiffies_timeout(timeout_ns);
1125}
1126
Ben Widawsky5816d642012-04-11 11:18:19 -07001127/**
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07001128 * i915_gem_wait_ioctl - implements DRM_IOCTL_I915_GEM_WAIT
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01001129 * @dev: drm device pointer
1130 * @data: ioctl data blob
1131 * @file: drm file pointer
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07001132 *
1133 * Returns 0 if successful, else an error is returned with the remaining time in
1134 * the timeout parameter.
1135 * -ETIME: object is still busy after timeout
1136 * -ERESTARTSYS: signal interrupted the wait
1137 * -ENONENT: object doesn't exist
1138 * Also possible, but rare:
Chris Wilsonb8050142017-08-11 11:57:31 +01001139 * -EAGAIN: incomplete, restart syscall
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07001140 * -ENOMEM: damn
1141 * -ENODEV: Internal IRQ fail
1142 * -E?: The add request failed
1143 *
1144 * The wait ioctl with a timeout of 0 reimplements the busy ioctl. With any
1145 * non-zero timeout parameter the wait ioctl will wait for the given number of
1146 * nanoseconds on an object becoming unbusy. Since the wait itself does so
1147 * without holding struct_mutex the object may become re-busied before this
1148 * function completes. A similar but shorter * race condition exists in the busy
1149 * ioctl
1150 */
1151int
1152i915_gem_wait_ioctl(struct drm_device *dev, void *data, struct drm_file *file)
1153{
1154 struct drm_i915_gem_wait *args = data;
1155 struct drm_i915_gem_object *obj;
Chris Wilsone95433c2016-10-28 13:58:27 +01001156 ktime_t start;
1157 long ret;
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07001158
Daniel Vetter11b5d512014-09-29 15:31:26 +02001159 if (args->flags != 0)
1160 return -EINVAL;
1161
Chris Wilson03ac0642016-07-20 13:31:51 +01001162 obj = i915_gem_object_lookup(file, args->bo_handle);
Chris Wilson033d5492016-08-05 10:14:17 +01001163 if (!obj)
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07001164 return -ENOENT;
Chris Wilson033d5492016-08-05 10:14:17 +01001165
Chris Wilsone95433c2016-10-28 13:58:27 +01001166 start = ktime_get();
1167
1168 ret = i915_gem_object_wait(obj,
Chris Wilsone9eaf822018-10-01 15:47:55 +01001169 I915_WAIT_INTERRUPTIBLE |
1170 I915_WAIT_PRIORITY |
1171 I915_WAIT_ALL,
Chris Wilson62eb3c22019-02-13 09:25:04 +00001172 to_wait_timeout(args->timeout_ns));
Chris Wilsone95433c2016-10-28 13:58:27 +01001173
1174 if (args->timeout_ns > 0) {
1175 args->timeout_ns -= ktime_to_ns(ktime_sub(ktime_get(), start));
1176 if (args->timeout_ns < 0)
1177 args->timeout_ns = 0;
Chris Wilsonc1d20612017-02-16 12:54:41 +00001178
1179 /*
1180 * Apparently ktime isn't accurate enough and occasionally has a
1181 * bit of mismatch in the jiffies<->nsecs<->ktime loop. So patch
1182 * things up to make the test happy. We allow up to 1 jiffy.
1183 *
1184 * This is a regression from the timespec->ktime conversion.
1185 */
1186 if (ret == -ETIME && !nsecs_to_jiffies(args->timeout_ns))
1187 args->timeout_ns = 0;
Chris Wilsonb8050142017-08-11 11:57:31 +01001188
1189 /* Asked to wait beyond the jiffie/scheduler precision? */
1190 if (ret == -ETIME && args->timeout_ns)
1191 ret = -EAGAIN;
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07001192 }
1193
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001194 i915_gem_object_put(obj);
John Harrisonff865882014-11-24 18:49:28 +00001195 return ret;
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07001196}
1197
Chris Wilson25112b62017-03-30 15:50:39 +01001198static int wait_for_engines(struct drm_i915_private *i915)
1199{
Chris Wilsonee42c002017-12-11 19:41:34 +00001200 if (wait_for(intel_engines_are_idle(i915), I915_IDLE_ENGINES_TIMEOUT)) {
Chris Wilson59e4b192017-12-11 19:41:35 +00001201 dev_err(i915->drm.dev,
1202 "Failed to idle engines, declaring wedged!\n");
Chris Wilson629820f2018-03-09 10:11:14 +00001203 GEM_TRACE_DUMP();
Chris Wilsoncad99462017-08-26 12:09:33 +01001204 i915_gem_set_wedged(i915);
1205 return -EIO;
Chris Wilson25112b62017-03-30 15:50:39 +01001206 }
1207
1208 return 0;
1209}
1210
Chris Wilson1e345562019-01-28 10:23:56 +00001211static long
1212wait_for_timelines(struct drm_i915_private *i915,
1213 unsigned int flags, long timeout)
1214{
1215 struct i915_gt_timelines *gt = &i915->gt.timelines;
1216 struct i915_timeline *tl;
1217
Chris Wilson1e345562019-01-28 10:23:56 +00001218 mutex_lock(&gt->mutex);
Chris Wilson9407d3b2019-01-28 18:18:12 +00001219 list_for_each_entry(tl, &gt->active_list, link) {
Chris Wilson1e345562019-01-28 10:23:56 +00001220 struct i915_request *rq;
1221
Chris Wilson21950ee2019-02-05 13:00:05 +00001222 rq = i915_active_request_get_unlocked(&tl->last_request);
Chris Wilson1e345562019-01-28 10:23:56 +00001223 if (!rq)
1224 continue;
1225
1226 mutex_unlock(&gt->mutex);
1227
1228 /*
1229 * "Race-to-idle".
1230 *
1231 * Switching to the kernel context is often used a synchronous
1232 * step prior to idling, e.g. in suspend for flushing all
1233 * current operations to memory before sleeping. These we
1234 * want to complete as quickly as possible to avoid prolonged
1235 * stalls, so allow the gpu to boost to maximum clocks.
1236 */
1237 if (flags & I915_WAIT_FOR_IDLE_BOOST)
Chris Wilson62eb3c22019-02-13 09:25:04 +00001238 gen6_rps_boost(rq);
Chris Wilson1e345562019-01-28 10:23:56 +00001239
1240 timeout = i915_request_wait(rq, flags, timeout);
1241 i915_request_put(rq);
1242 if (timeout < 0)
1243 return timeout;
1244
1245 /* restart after reacquiring the lock */
1246 mutex_lock(&gt->mutex);
Chris Wilson9407d3b2019-01-28 18:18:12 +00001247 tl = list_entry(&gt->active_list, typeof(*tl), link);
Chris Wilson1e345562019-01-28 10:23:56 +00001248 }
1249 mutex_unlock(&gt->mutex);
1250
1251 return timeout;
1252}
1253
Chris Wilsonec625fb2018-07-09 13:20:42 +01001254int i915_gem_wait_for_idle(struct drm_i915_private *i915,
1255 unsigned int flags, long timeout)
Chris Wilson73cb9702016-10-28 13:58:46 +01001256{
Chris Wilson79ffac852019-04-24 21:07:17 +01001257 GEM_TRACE("flags=%x (%s), timeout=%ld%s, awake?=%s\n",
Chris Wilsonec625fb2018-07-09 13:20:42 +01001258 flags, flags & I915_WAIT_LOCKED ? "locked" : "unlocked",
Chris Wilson79ffac852019-04-24 21:07:17 +01001259 timeout, timeout == MAX_SCHEDULE_TIMEOUT ? " (forever)" : "",
1260 yesno(i915->gt.awake));
Chris Wilson09a4c022018-05-24 09:11:35 +01001261
Chris Wilson863e9fd2017-05-30 13:13:32 +01001262 /* If the device is asleep, we have no requests outstanding */
1263 if (!READ_ONCE(i915->gt.awake))
1264 return 0;
1265
Chris Wilson1e345562019-01-28 10:23:56 +00001266 timeout = wait_for_timelines(i915, flags, timeout);
1267 if (timeout < 0)
1268 return timeout;
1269
Chris Wilson9caa34a2016-11-11 14:58:08 +00001270 if (flags & I915_WAIT_LOCKED) {
Chris Wilsona89d1f92018-05-02 17:38:39 +01001271 int err;
Chris Wilson9caa34a2016-11-11 14:58:08 +00001272
1273 lockdep_assert_held(&i915->drm.struct_mutex);
1274
Chris Wilsona61b47f2018-06-27 12:53:34 +01001275 err = wait_for_engines(i915);
1276 if (err)
1277 return err;
1278
Chris Wilsone61e0f52018-02-21 09:56:36 +00001279 i915_retire_requests(i915);
Chris Wilsona89d1f92018-05-02 17:38:39 +01001280 }
Chris Wilsona61b47f2018-06-27 12:53:34 +01001281
1282 return 0;
Daniel Vetter4df2faf2010-02-19 11:52:00 +01001283}
1284
Eric Anholt673a3942008-07-30 12:06:12 -07001285/* Throttle our rendering by waiting until the ring has completed our requests
1286 * emitted over 20 msec ago.
1287 *
Eric Anholtb9624422009-06-03 07:27:35 +00001288 * Note that if we were to use the current jiffies each time around the loop,
1289 * we wouldn't escape the function with any frames outstanding if the time to
1290 * render a frame was over 20ms.
1291 *
Eric Anholt673a3942008-07-30 12:06:12 -07001292 * This should get us reasonable parallelism between CPU and GPU but also
1293 * relatively low latency when blocking on a particular request to finish.
1294 */
1295static int
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001296i915_gem_ring_throttle(struct drm_device *dev, struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001297{
Chris Wilsonfac5e232016-07-04 11:34:36 +01001298 struct drm_i915_private *dev_priv = to_i915(dev);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001299 struct drm_i915_file_private *file_priv = file->driver_priv;
Chris Wilsond0bc54f2015-05-21 21:01:48 +01001300 unsigned long recent_enough = jiffies - DRM_I915_THROTTLE_JIFFIES;
Chris Wilsone61e0f52018-02-21 09:56:36 +00001301 struct i915_request *request, *target = NULL;
Chris Wilsone95433c2016-10-28 13:58:27 +01001302 long ret;
Eric Anholt673a3942008-07-30 12:06:12 -07001303
Chris Wilsonf4457ae2016-04-13 17:35:08 +01001304 /* ABI: return -EIO if already wedged */
Chris Wilsonc41166f2019-02-20 14:56:37 +00001305 ret = i915_terminally_wedged(dev_priv);
1306 if (ret)
1307 return ret;
Chris Wilsone110e8d2011-01-26 15:39:14 +00001308
Chris Wilson1c255952010-09-26 11:03:27 +01001309 spin_lock(&file_priv->mm.lock);
Chris Wilsonc8659ef2017-03-02 12:25:25 +00001310 list_for_each_entry(request, &file_priv->mm.request_list, client_link) {
Eric Anholtb9624422009-06-03 07:27:35 +00001311 if (time_after_eq(request->emitted_jiffies, recent_enough))
1312 break;
1313
Chris Wilsonc8659ef2017-03-02 12:25:25 +00001314 if (target) {
1315 list_del(&target->client_link);
1316 target->file_priv = NULL;
1317 }
John Harrisonfcfa423c2015-05-29 17:44:12 +01001318
John Harrison54fb2412014-11-24 18:49:27 +00001319 target = request;
Eric Anholtb9624422009-06-03 07:27:35 +00001320 }
John Harrisonff865882014-11-24 18:49:28 +00001321 if (target)
Chris Wilsone61e0f52018-02-21 09:56:36 +00001322 i915_request_get(target);
Chris Wilson1c255952010-09-26 11:03:27 +01001323 spin_unlock(&file_priv->mm.lock);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001324
John Harrison54fb2412014-11-24 18:49:27 +00001325 if (target == NULL)
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001326 return 0;
1327
Chris Wilsone61e0f52018-02-21 09:56:36 +00001328 ret = i915_request_wait(target,
Chris Wilsone95433c2016-10-28 13:58:27 +01001329 I915_WAIT_INTERRUPTIBLE,
1330 MAX_SCHEDULE_TIMEOUT);
Chris Wilsone61e0f52018-02-21 09:56:36 +00001331 i915_request_put(target);
John Harrisonff865882014-11-24 18:49:28 +00001332
Chris Wilsone95433c2016-10-28 13:58:27 +01001333 return ret < 0 ? ret : 0;
Eric Anholt673a3942008-07-30 12:06:12 -07001334}
1335
Chris Wilson058d88c2016-08-15 10:49:06 +01001336struct i915_vma *
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02001337i915_gem_object_ggtt_pin(struct drm_i915_gem_object *obj,
1338 const struct i915_ggtt_view *view,
Chris Wilson91b2db62016-08-04 16:32:23 +01001339 u64 size,
Chris Wilson2ffffd02016-08-04 16:32:22 +01001340 u64 alignment,
1341 u64 flags)
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02001342{
Chris Wilsonad16d2e2016-10-13 09:55:04 +01001343 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
Chris Wilson82ad6442018-06-05 16:37:58 +01001344 struct i915_address_space *vm = &dev_priv->ggtt.vm;
Chris Wilson59bfa122016-08-04 16:32:31 +01001345 struct i915_vma *vma;
1346 int ret;
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03001347
Chris Wilson4c7d62c2016-10-28 13:58:32 +01001348 lockdep_assert_held(&obj->base.dev->struct_mutex);
1349
Chris Wilsonac87a6fd2018-02-20 13:42:05 +00001350 if (flags & PIN_MAPPABLE &&
1351 (!view || view->type == I915_GGTT_VIEW_NORMAL)) {
Chris Wilson43ae70d92017-10-09 09:44:01 +01001352 /* If the required space is larger than the available
1353 * aperture, we will not able to find a slot for the
1354 * object and unbinding the object now will be in
1355 * vain. Worse, doing so may cause us to ping-pong
1356 * the object in and out of the Global GTT and
1357 * waste a lot of cycles under the mutex.
1358 */
1359 if (obj->base.size > dev_priv->ggtt.mappable_end)
1360 return ERR_PTR(-E2BIG);
1361
1362 /* If NONBLOCK is set the caller is optimistically
1363 * trying to cache the full object within the mappable
1364 * aperture, and *must* have a fallback in place for
1365 * situations where we cannot bind the object. We
1366 * can be a little more lax here and use the fallback
1367 * more often to avoid costly migrations of ourselves
1368 * and other objects within the aperture.
1369 *
1370 * Half-the-aperture is used as a simple heuristic.
1371 * More interesting would to do search for a free
1372 * block prior to making the commitment to unbind.
1373 * That caters for the self-harm case, and with a
1374 * little more heuristics (e.g. NOFAULT, NOEVICT)
1375 * we could try to minimise harm to others.
1376 */
1377 if (flags & PIN_NONBLOCK &&
1378 obj->base.size > dev_priv->ggtt.mappable_end / 2)
1379 return ERR_PTR(-ENOSPC);
1380 }
1381
Chris Wilson718659a2017-01-16 15:21:28 +00001382 vma = i915_vma_instance(obj, vm, view);
Chengguang Xu772b5402019-02-21 10:08:19 +08001383 if (IS_ERR(vma))
Chris Wilson058d88c2016-08-15 10:49:06 +01001384 return vma;
Chris Wilson59bfa122016-08-04 16:32:31 +01001385
1386 if (i915_vma_misplaced(vma, size, alignment, flags)) {
Chris Wilson43ae70d92017-10-09 09:44:01 +01001387 if (flags & PIN_NONBLOCK) {
1388 if (i915_vma_is_pinned(vma) || i915_vma_is_active(vma))
1389 return ERR_PTR(-ENOSPC);
Chris Wilson59bfa122016-08-04 16:32:31 +01001390
Chris Wilson43ae70d92017-10-09 09:44:01 +01001391 if (flags & PIN_MAPPABLE &&
Chris Wilson944397f2017-01-09 16:16:11 +00001392 vma->fence_size > dev_priv->ggtt.mappable_end / 2)
Chris Wilsonad16d2e2016-10-13 09:55:04 +01001393 return ERR_PTR(-ENOSPC);
1394 }
1395
Chris Wilson59bfa122016-08-04 16:32:31 +01001396 WARN(i915_vma_is_pinned(vma),
1397 "bo is already pinned in ggtt with incorrect alignment:"
Chris Wilson05a20d02016-08-18 17:16:55 +01001398 " offset=%08x, req.alignment=%llx,"
1399 " req.map_and_fenceable=%d, vma->map_and_fenceable=%d\n",
1400 i915_ggtt_offset(vma), alignment,
Chris Wilson59bfa122016-08-04 16:32:31 +01001401 !!(flags & PIN_MAPPABLE),
Chris Wilson05a20d02016-08-18 17:16:55 +01001402 i915_vma_is_map_and_fenceable(vma));
Chris Wilson59bfa122016-08-04 16:32:31 +01001403 ret = i915_vma_unbind(vma);
1404 if (ret)
Chris Wilson058d88c2016-08-15 10:49:06 +01001405 return ERR_PTR(ret);
Chris Wilson59bfa122016-08-04 16:32:31 +01001406 }
1407
Chris Wilson058d88c2016-08-15 10:49:06 +01001408 ret = i915_vma_pin(vma, size, alignment, flags | PIN_GLOBAL);
1409 if (ret)
1410 return ERR_PTR(ret);
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02001411
Chris Wilson058d88c2016-08-15 10:49:06 +01001412 return vma;
Eric Anholt673a3942008-07-30 12:06:12 -07001413}
1414
Chris Wilson6960d9c2019-04-04 11:19:14 +01001415static __always_inline u32 __busy_read_flag(u8 id)
Chris Wilson3fdc13c2016-08-05 10:14:18 +01001416{
Chris Wilson6960d9c2019-04-04 11:19:14 +01001417 if (id == (u8)I915_ENGINE_CLASS_INVALID)
1418 return 0xffff0000u;
Chris Wilsonc8b50242019-03-05 16:26:43 +00001419
1420 GEM_BUG_ON(id >= 16);
Chris Wilson6960d9c2019-04-04 11:19:14 +01001421 return 0x10000u << id;
Chris Wilson3fdc13c2016-08-05 10:14:18 +01001422}
1423
Chris Wilson6960d9c2019-04-04 11:19:14 +01001424static __always_inline u32 __busy_write_id(u8 id)
Chris Wilson3fdc13c2016-08-05 10:14:18 +01001425{
Chris Wilsonc8b50242019-03-05 16:26:43 +00001426 /*
1427 * The uABI guarantees an active writer is also amongst the read
Chris Wilson70cb4722016-08-09 18:08:25 +01001428 * engines. This would be true if we accessed the activity tracking
1429 * under the lock, but as we perform the lookup of the object and
1430 * its activity locklessly we can not guarantee that the last_write
1431 * being active implies that we have set the same engine flag from
1432 * last_read - hence we always set both read and write busy for
1433 * last_write.
1434 */
Chris Wilson6960d9c2019-04-04 11:19:14 +01001435 if (id == (u8)I915_ENGINE_CLASS_INVALID)
1436 return 0xffffffffu;
Chris Wilsonc8b50242019-03-05 16:26:43 +00001437
1438 return (id + 1) | __busy_read_flag(id);
Chris Wilson3fdc13c2016-08-05 10:14:18 +01001439}
1440
Chris Wilsonedf6b762016-08-09 09:23:33 +01001441static __always_inline unsigned int
Chris Wilson6960d9c2019-04-04 11:19:14 +01001442__busy_set_if_active(const struct dma_fence *fence, u32 (*flag)(u8 id))
Chris Wilson3fdc13c2016-08-05 10:14:18 +01001443{
Chris Wilsonc8b50242019-03-05 16:26:43 +00001444 const struct i915_request *rq;
Chris Wilson12555012016-08-16 09:50:40 +01001445
Chris Wilsonc8b50242019-03-05 16:26:43 +00001446 /*
1447 * We have to check the current hw status of the fence as the uABI
Chris Wilsond07f0e52016-10-28 13:58:44 +01001448 * guarantees forward progress. We could rely on the idle worker
1449 * to eventually flush us, but to minimise latency just ask the
1450 * hardware.
1451 *
1452 * Note we only report on the status of native fences.
1453 */
1454 if (!dma_fence_is_i915(fence))
Chris Wilson12555012016-08-16 09:50:40 +01001455 return 0;
1456
Chris Wilsond07f0e52016-10-28 13:58:44 +01001457 /* opencode to_request() in order to avoid const warnings */
Chris Wilsonc8b50242019-03-05 16:26:43 +00001458 rq = container_of(fence, const struct i915_request, fence);
Chris Wilsone61e0f52018-02-21 09:56:36 +00001459 if (i915_request_completed(rq))
Chris Wilsond07f0e52016-10-28 13:58:44 +01001460 return 0;
1461
Chris Wilson6960d9c2019-04-04 11:19:14 +01001462 /* Beware type-expansion follies! */
1463 BUILD_BUG_ON(!typecheck(u8, rq->engine->uabi_class));
Chris Wilsonc8b50242019-03-05 16:26:43 +00001464 return flag(rq->engine->uabi_class);
Chris Wilson3fdc13c2016-08-05 10:14:18 +01001465}
1466
Chris Wilsonedf6b762016-08-09 09:23:33 +01001467static __always_inline unsigned int
Chris Wilsond07f0e52016-10-28 13:58:44 +01001468busy_check_reader(const struct dma_fence *fence)
Chris Wilson3fdc13c2016-08-05 10:14:18 +01001469{
Chris Wilsond07f0e52016-10-28 13:58:44 +01001470 return __busy_set_if_active(fence, __busy_read_flag);
Chris Wilson3fdc13c2016-08-05 10:14:18 +01001471}
1472
Chris Wilsonedf6b762016-08-09 09:23:33 +01001473static __always_inline unsigned int
Chris Wilsond07f0e52016-10-28 13:58:44 +01001474busy_check_writer(const struct dma_fence *fence)
Chris Wilson3fdc13c2016-08-05 10:14:18 +01001475{
Chris Wilsond07f0e52016-10-28 13:58:44 +01001476 if (!fence)
1477 return 0;
1478
1479 return __busy_set_if_active(fence, __busy_write_id);
Chris Wilson3fdc13c2016-08-05 10:14:18 +01001480}
1481
Eric Anholt673a3942008-07-30 12:06:12 -07001482int
Eric Anholt673a3942008-07-30 12:06:12 -07001483i915_gem_busy_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00001484 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001485{
1486 struct drm_i915_gem_busy *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00001487 struct drm_i915_gem_object *obj;
Chris Wilsond07f0e52016-10-28 13:58:44 +01001488 struct reservation_object_list *list;
1489 unsigned int seq;
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01001490 int err;
Eric Anholt673a3942008-07-30 12:06:12 -07001491
Chris Wilsond07f0e52016-10-28 13:58:44 +01001492 err = -ENOENT;
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01001493 rcu_read_lock();
1494 obj = i915_gem_object_lookup_rcu(file, args->handle);
Chris Wilsond07f0e52016-10-28 13:58:44 +01001495 if (!obj)
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01001496 goto out;
Chris Wilsond07f0e52016-10-28 13:58:44 +01001497
Chris Wilsonc8b50242019-03-05 16:26:43 +00001498 /*
1499 * A discrepancy here is that we do not report the status of
Chris Wilsond07f0e52016-10-28 13:58:44 +01001500 * non-i915 fences, i.e. even though we may report the object as idle,
1501 * a call to set-domain may still stall waiting for foreign rendering.
1502 * This also means that wait-ioctl may report an object as busy,
1503 * where busy-ioctl considers it idle.
1504 *
1505 * We trade the ability to warn of foreign fences to report on which
1506 * i915 engines are active for the object.
1507 *
1508 * Alternatively, we can trade that extra information on read/write
1509 * activity with
1510 * args->busy =
1511 * !reservation_object_test_signaled_rcu(obj->resv, true);
1512 * to report the overall busyness. This is what the wait-ioctl does.
1513 *
1514 */
1515retry:
1516 seq = raw_read_seqcount(&obj->resv->seq);
1517
1518 /* Translate the exclusive fence to the READ *and* WRITE engine */
1519 args->busy = busy_check_writer(rcu_dereference(obj->resv->fence_excl));
1520
1521 /* Translate shared fences to READ set of engines */
1522 list = rcu_dereference(obj->resv->fence);
1523 if (list) {
1524 unsigned int shared_count = list->shared_count, i;
1525
1526 for (i = 0; i < shared_count; ++i) {
1527 struct dma_fence *fence =
1528 rcu_dereference(list->shared[i]);
1529
1530 args->busy |= busy_check_reader(fence);
1531 }
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01001532 }
Zou Nan haid1b851f2010-05-21 09:08:57 +08001533
Chris Wilsond07f0e52016-10-28 13:58:44 +01001534 if (args->busy && read_seqcount_retry(&obj->resv->seq, seq))
1535 goto retry;
Chris Wilson426960b2016-01-15 16:51:46 +00001536
Chris Wilsond07f0e52016-10-28 13:58:44 +01001537 err = 0;
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01001538out:
1539 rcu_read_unlock();
1540 return err;
Eric Anholt673a3942008-07-30 12:06:12 -07001541}
1542
1543int
1544i915_gem_throttle_ioctl(struct drm_device *dev, void *data,
1545 struct drm_file *file_priv)
1546{
Akshay Joshi0206e352011-08-16 15:34:10 -04001547 return i915_gem_ring_throttle(dev, file_priv);
Eric Anholt673a3942008-07-30 12:06:12 -07001548}
1549
Chris Wilson3ef94da2009-09-14 16:50:29 +01001550int
1551i915_gem_madvise_ioctl(struct drm_device *dev, void *data,
1552 struct drm_file *file_priv)
1553{
Chris Wilsonfac5e232016-07-04 11:34:36 +01001554 struct drm_i915_private *dev_priv = to_i915(dev);
Chris Wilson3ef94da2009-09-14 16:50:29 +01001555 struct drm_i915_gem_madvise *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00001556 struct drm_i915_gem_object *obj;
Chris Wilson1233e2d2016-10-28 13:58:37 +01001557 int err;
Chris Wilson3ef94da2009-09-14 16:50:29 +01001558
1559 switch (args->madv) {
1560 case I915_MADV_DONTNEED:
1561 case I915_MADV_WILLNEED:
1562 break;
1563 default:
1564 return -EINVAL;
1565 }
1566
Chris Wilson03ac0642016-07-20 13:31:51 +01001567 obj = i915_gem_object_lookup(file_priv, args->handle);
Chris Wilson1233e2d2016-10-28 13:58:37 +01001568 if (!obj)
1569 return -ENOENT;
1570
1571 err = mutex_lock_interruptible(&obj->mm.lock);
1572 if (err)
1573 goto out;
Chris Wilson3ef94da2009-09-14 16:50:29 +01001574
Chris Wilsonf1fa4f42017-10-13 21:26:13 +01001575 if (i915_gem_object_has_pages(obj) &&
Chris Wilson3e510a82016-08-05 10:14:23 +01001576 i915_gem_object_is_tiled(obj) &&
Daniel Vetter656bfa32014-11-20 09:26:30 +01001577 dev_priv->quirks & QUIRK_PIN_SWIZZLED_PAGES) {
Chris Wilsonbc0629a2016-11-01 10:03:17 +00001578 if (obj->mm.madv == I915_MADV_WILLNEED) {
1579 GEM_BUG_ON(!obj->mm.quirked);
Chris Wilsona4f5ea62016-10-28 13:58:35 +01001580 __i915_gem_object_unpin_pages(obj);
Chris Wilsonbc0629a2016-11-01 10:03:17 +00001581 obj->mm.quirked = false;
1582 }
1583 if (args->madv == I915_MADV_WILLNEED) {
Chris Wilson2c3a3f42016-11-04 10:30:01 +00001584 GEM_BUG_ON(obj->mm.quirked);
Chris Wilsona4f5ea62016-10-28 13:58:35 +01001585 __i915_gem_object_pin_pages(obj);
Chris Wilsonbc0629a2016-11-01 10:03:17 +00001586 obj->mm.quirked = true;
1587 }
Daniel Vetter656bfa32014-11-20 09:26:30 +01001588 }
1589
Chris Wilsona4f5ea62016-10-28 13:58:35 +01001590 if (obj->mm.madv != __I915_MADV_PURGED)
1591 obj->mm.madv = args->madv;
Chris Wilson3ef94da2009-09-14 16:50:29 +01001592
Chris Wilson6c085a72012-08-20 11:40:46 +02001593 /* if the object is no longer attached, discard its backing storage */
Chris Wilsonf1fa4f42017-10-13 21:26:13 +01001594 if (obj->mm.madv == I915_MADV_DONTNEED &&
1595 !i915_gem_object_has_pages(obj))
Chris Wilsonf0334282019-05-28 10:29:46 +01001596 i915_gem_object_truncate(obj);
Chris Wilson2d7ef392009-09-20 23:13:10 +01001597
Chris Wilsona4f5ea62016-10-28 13:58:35 +01001598 args->retained = obj->mm.madv != __I915_MADV_PURGED;
Chris Wilson1233e2d2016-10-28 13:58:37 +01001599 mutex_unlock(&obj->mm.lock);
Chris Wilsonbb6baf72009-09-22 14:24:13 +01001600
Chris Wilson1233e2d2016-10-28 13:58:37 +01001601out:
Chris Wilsonf8c417c2016-07-20 13:31:53 +01001602 i915_gem_object_put(obj);
Chris Wilson1233e2d2016-10-28 13:58:37 +01001603 return err;
Chris Wilson3ef94da2009-09-14 16:50:29 +01001604}
1605
Chris Wilson24145512017-01-24 11:01:35 +00001606void i915_gem_sanitize(struct drm_i915_private *i915)
1607{
Chris Wilson538ef962019-01-14 14:21:18 +00001608 intel_wakeref_t wakeref;
1609
Chris Wilsonc3160da2018-05-31 09:22:45 +01001610 GEM_TRACE("\n");
1611
Chris Wilson538ef962019-01-14 14:21:18 +00001612 wakeref = intel_runtime_pm_get(i915);
Daniele Ceraolo Spurio3ceea6a2019-03-19 11:35:36 -07001613 intel_uncore_forcewake_get(&i915->uncore, FORCEWAKE_ALL);
Chris Wilsonc3160da2018-05-31 09:22:45 +01001614
1615 /*
1616 * As we have just resumed the machine and woken the device up from
1617 * deep PCI sleep (presumably D3_cold), assume the HW has been reset
1618 * back to defaults, recovering from whatever wedged state we left it
1619 * in and so worth trying to use the device once more.
1620 */
Chris Wilsonc41166f2019-02-20 14:56:37 +00001621 if (i915_terminally_wedged(i915))
Chris Wilsonf36325f2017-08-26 12:09:34 +01001622 i915_gem_unset_wedged(i915);
Chris Wilsonf36325f2017-08-26 12:09:34 +01001623
Chris Wilson24145512017-01-24 11:01:35 +00001624 /*
1625 * If we inherit context state from the BIOS or earlier occupants
1626 * of the GPU, the GPU may be in an inconsistent state when we
1627 * try to take over. The only way to remove the earlier state
1628 * is by resetting. However, resetting on earlier gen is tricky as
1629 * it may impact the display and we are uncertain about the stability
Joonas Lahtinenea117b82017-04-28 10:53:38 +03001630 * of the reset, so this could be applied to even earlier gen.
Chris Wilson24145512017-01-24 11:01:35 +00001631 */
Chris Wilson79ffac852019-04-24 21:07:17 +01001632 intel_gt_sanitize(i915, false);
Chris Wilsonc3160da2018-05-31 09:22:45 +01001633
Daniele Ceraolo Spurio3ceea6a2019-03-19 11:35:36 -07001634 intel_uncore_forcewake_put(&i915->uncore, FORCEWAKE_ALL);
Chris Wilson538ef962019-01-14 14:21:18 +00001635 intel_runtime_pm_put(i915, wakeref);
Chris Wilsonc3160da2018-05-31 09:22:45 +01001636
Chris Wilsoneb8d0f52019-01-25 13:22:28 +00001637 mutex_lock(&i915->drm.struct_mutex);
Chris Wilson4dfacb02018-05-31 09:22:43 +01001638 i915_gem_contexts_lost(i915);
1639 mutex_unlock(&i915->drm.struct_mutex);
Chris Wilson24145512017-01-24 11:01:35 +00001640}
1641
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00001642void i915_gem_init_swizzling(struct drm_i915_private *dev_priv)
Daniel Vetterf691e2f2012-02-02 09:58:12 +01001643{
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00001644 if (INTEL_GEN(dev_priv) < 5 ||
Daniel Vetterf691e2f2012-02-02 09:58:12 +01001645 dev_priv->mm.bit_6_swizzle_x == I915_BIT_6_SWIZZLE_NONE)
1646 return;
1647
1648 I915_WRITE(DISP_ARB_CTL, I915_READ(DISP_ARB_CTL) |
1649 DISP_TILE_SURFACE_SWIZZLING);
1650
Lucas De Marchicf819ef2018-12-12 10:10:43 -08001651 if (IS_GEN(dev_priv, 5))
Daniel Vetter11782b02012-01-31 16:47:55 +01001652 return;
1653
Daniel Vetterf691e2f2012-02-02 09:58:12 +01001654 I915_WRITE(TILECTL, I915_READ(TILECTL) | TILECTL_SWZCTL);
Lucas De Marchicf819ef2018-12-12 10:10:43 -08001655 if (IS_GEN(dev_priv, 6))
Daniel Vetter6b26c862012-04-24 14:04:12 +02001656 I915_WRITE(ARB_MODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_SNB));
Lucas De Marchicf819ef2018-12-12 10:10:43 -08001657 else if (IS_GEN(dev_priv, 7))
Daniel Vetter6b26c862012-04-24 14:04:12 +02001658 I915_WRITE(ARB_MODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_IVB));
Lucas De Marchicf819ef2018-12-12 10:10:43 -08001659 else if (IS_GEN(dev_priv, 8))
Ben Widawsky31a53362013-11-02 21:07:04 -07001660 I915_WRITE(GAMTARBMODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_BDW));
Ben Widawsky8782e262012-12-18 10:31:23 -08001661 else
1662 BUG();
Daniel Vetterf691e2f2012-02-02 09:58:12 +01001663}
Daniel Vettere21af882012-02-09 20:53:27 +01001664
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01001665static void init_unused_ring(struct drm_i915_private *dev_priv, u32 base)
Ville Syrjälä81e7f202014-08-15 01:21:55 +03001666{
Ville Syrjälä81e7f202014-08-15 01:21:55 +03001667 I915_WRITE(RING_CTL(base), 0);
1668 I915_WRITE(RING_HEAD(base), 0);
1669 I915_WRITE(RING_TAIL(base), 0);
1670 I915_WRITE(RING_START(base), 0);
1671}
1672
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01001673static void init_unused_rings(struct drm_i915_private *dev_priv)
Ville Syrjälä81e7f202014-08-15 01:21:55 +03001674{
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01001675 if (IS_I830(dev_priv)) {
1676 init_unused_ring(dev_priv, PRB1_BASE);
1677 init_unused_ring(dev_priv, SRB0_BASE);
1678 init_unused_ring(dev_priv, SRB1_BASE);
1679 init_unused_ring(dev_priv, SRB2_BASE);
1680 init_unused_ring(dev_priv, SRB3_BASE);
Lucas De Marchicf819ef2018-12-12 10:10:43 -08001681 } else if (IS_GEN(dev_priv, 2)) {
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01001682 init_unused_ring(dev_priv, SRB0_BASE);
1683 init_unused_ring(dev_priv, SRB1_BASE);
Lucas De Marchicf819ef2018-12-12 10:10:43 -08001684 } else if (IS_GEN(dev_priv, 3)) {
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01001685 init_unused_ring(dev_priv, PRB1_BASE);
1686 init_unused_ring(dev_priv, PRB2_BASE);
Ville Syrjälä81e7f202014-08-15 01:21:55 +03001687 }
1688}
1689
Chris Wilson20a8a742017-02-08 14:30:31 +00001690int i915_gem_init_hw(struct drm_i915_private *dev_priv)
1691{
Chris Wilsond200cda2016-04-28 09:56:44 +01001692 int ret;
Ben Widawsky4fc7c972013-02-08 11:49:24 -08001693
Chris Wilsonde867c22016-10-25 13:16:02 +01001694 dev_priv->gt.last_init_time = ktime_get();
1695
Chris Wilson5e4f5182015-02-13 14:35:59 +00001696 /* Double layer security blanket, see i915_gem_init() */
Daniele Ceraolo Spurio3ceea6a2019-03-19 11:35:36 -07001697 intel_uncore_forcewake_get(&dev_priv->uncore, FORCEWAKE_ALL);
Chris Wilson5e4f5182015-02-13 14:35:59 +00001698
Tvrtko Ursulin0031fb92016-11-04 14:42:44 +00001699 if (HAS_EDRAM(dev_priv) && INTEL_GEN(dev_priv) < 9)
Ben Widawsky05e21cc2013-07-04 11:02:04 -07001700 I915_WRITE(HSW_IDICR, I915_READ(HSW_IDICR) | IDIHASHMSK(0xf));
Ben Widawsky4fc7c972013-02-08 11:49:24 -08001701
Tvrtko Ursulin772c2a52016-10-13 11:03:01 +01001702 if (IS_HASWELL(dev_priv))
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01001703 I915_WRITE(MI_PREDICATE_RESULT_2, IS_HSW_GT3(dev_priv) ?
Ville Syrjälä0bf21342013-11-29 14:56:12 +02001704 LOWER_SLICE_ENABLED : LOWER_SLICE_DISABLED);
Rodrigo Vivi94353732013-08-28 16:45:46 -03001705
Tvrtko Ursulin094304b2018-12-03 12:50:10 +00001706 /* Apply the GT workarounds... */
Tvrtko Ursulin25d140f2018-12-03 13:33:19 +00001707 intel_gt_apply_workarounds(dev_priv);
Tvrtko Ursulin094304b2018-12-03 12:50:10 +00001708 /* ...and determine whether they are sticking. */
1709 intel_gt_verify_workarounds(dev_priv, "init");
Oscar Mateo59b449d2018-04-10 09:12:47 -07001710
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00001711 i915_gem_init_swizzling(dev_priv);
Ben Widawsky4fc7c972013-02-08 11:49:24 -08001712
Daniel Vetterd5abdfd2014-11-20 09:45:19 +01001713 /*
1714 * At least 830 can leave some of the unused rings
1715 * "active" (ie. head != tail) after resume which
1716 * will prevent c3 entry. Makes sure all unused rings
1717 * are totally idle.
1718 */
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01001719 init_unused_rings(dev_priv);
Daniel Vetterd5abdfd2014-11-20 09:45:19 +01001720
Dave Gordoned54c1a2016-01-19 19:02:54 +00001721 BUG_ON(!dev_priv->kernel_context);
Chris Wilsonc41166f2019-02-20 14:56:37 +00001722 ret = i915_terminally_wedged(dev_priv);
1723 if (ret)
Chris Wilson6f74b362017-10-15 15:37:25 +01001724 goto out;
John Harrison90638cc2015-05-29 17:43:37 +01001725
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00001726 ret = i915_ppgtt_init_hw(dev_priv);
John Harrison4ad2fd82015-06-18 13:11:20 +01001727 if (ret) {
Chris Wilson8177e112018-02-07 11:15:45 +00001728 DRM_ERROR("Enabling PPGTT failed (%d)\n", ret);
John Harrison4ad2fd82015-06-18 13:11:20 +01001729 goto out;
1730 }
1731
Jackie Lif08e2032018-03-13 17:32:53 -07001732 ret = intel_wopcm_init_hw(&dev_priv->wopcm);
1733 if (ret) {
1734 DRM_ERROR("Enabling WOPCM failed (%d)\n", ret);
1735 goto out;
1736 }
1737
Michał Winiarski9bdc3572017-10-25 18:25:19 +01001738 /* We can't enable contexts until all firmware is loaded */
1739 ret = intel_uc_init_hw(dev_priv);
Chris Wilson8177e112018-02-07 11:15:45 +00001740 if (ret) {
1741 DRM_ERROR("Enabling uc failed (%d)\n", ret);
Michał Winiarski9bdc3572017-10-25 18:25:19 +01001742 goto out;
Chris Wilson8177e112018-02-07 11:15:45 +00001743 }
Michał Winiarski9bdc3572017-10-25 18:25:19 +01001744
Tvrtko Ursulinbf9e8422016-12-01 14:16:38 +00001745 intel_mocs_init_l3cc_table(dev_priv);
Peter Antoine0ccdacf2016-04-13 15:03:25 +01001746
Chris Wilson136109c2017-11-02 13:14:30 +00001747 /* Only when the HW is re-initialised, can we replay the requests */
Chris Wilson79ffac852019-04-24 21:07:17 +01001748 ret = intel_engines_resume(dev_priv);
Michal Wajdeczkob96f6eb2018-06-05 12:24:43 +00001749 if (ret)
1750 goto cleanup_uc;
Michał Winiarski60c0a662018-07-12 14:48:10 +02001751
Daniele Ceraolo Spurio3ceea6a2019-03-19 11:35:36 -07001752 intel_uncore_forcewake_put(&dev_priv->uncore, FORCEWAKE_ALL);
Michał Winiarski60c0a662018-07-12 14:48:10 +02001753
Chris Wilson79ffac852019-04-24 21:07:17 +01001754 intel_engines_set_scheduler_caps(dev_priv);
Michał Winiarski60c0a662018-07-12 14:48:10 +02001755 return 0;
Michal Wajdeczkob96f6eb2018-06-05 12:24:43 +00001756
1757cleanup_uc:
1758 intel_uc_fini_hw(dev_priv);
Michał Winiarski60c0a662018-07-12 14:48:10 +02001759out:
Daniele Ceraolo Spurio3ceea6a2019-03-19 11:35:36 -07001760 intel_uncore_forcewake_put(&dev_priv->uncore, FORCEWAKE_ALL);
Michał Winiarski60c0a662018-07-12 14:48:10 +02001761
1762 return ret;
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001763}
1764
Chris Wilsond2b4b972017-11-10 14:26:33 +00001765static int __intel_engines_record_defaults(struct drm_i915_private *i915)
1766{
Chris Wilsond2b4b972017-11-10 14:26:33 +00001767 struct intel_engine_cs *engine;
Chris Wilson5e2a0412019-04-26 17:33:34 +01001768 struct i915_gem_context *ctx;
1769 struct i915_gem_engines *e;
Chris Wilsond2b4b972017-11-10 14:26:33 +00001770 enum intel_engine_id id;
Chris Wilson604c37d2019-03-08 09:36:55 +00001771 int err = 0;
Chris Wilsond2b4b972017-11-10 14:26:33 +00001772
1773 /*
1774 * As we reset the gpu during very early sanitisation, the current
1775 * register state on the GPU should reflect its defaults values.
1776 * We load a context onto the hw (with restore-inhibit), then switch
1777 * over to a second context to save that default register state. We
1778 * can then prime every new context with that state so they all start
1779 * from the same default HW values.
1780 */
1781
1782 ctx = i915_gem_context_create_kernel(i915, 0);
1783 if (IS_ERR(ctx))
1784 return PTR_ERR(ctx);
1785
Chris Wilson5e2a0412019-04-26 17:33:34 +01001786 e = i915_gem_context_lock_engines(ctx);
1787
Chris Wilsond2b4b972017-11-10 14:26:33 +00001788 for_each_engine(engine, i915, id) {
Chris Wilson5e2a0412019-04-26 17:33:34 +01001789 struct intel_context *ce = e->engines[id];
Chris Wilsone61e0f52018-02-21 09:56:36 +00001790 struct i915_request *rq;
Chris Wilsond2b4b972017-11-10 14:26:33 +00001791
Chris Wilson5e2a0412019-04-26 17:33:34 +01001792 rq = intel_context_create_request(ce);
Chris Wilsond2b4b972017-11-10 14:26:33 +00001793 if (IS_ERR(rq)) {
1794 err = PTR_ERR(rq);
Chris Wilson5e2a0412019-04-26 17:33:34 +01001795 goto err_active;
Chris Wilsond2b4b972017-11-10 14:26:33 +00001796 }
1797
Chris Wilson3fef5cd2017-11-20 10:20:02 +00001798 err = 0;
Chris Wilson5e2a0412019-04-26 17:33:34 +01001799 if (rq->engine->init_context)
1800 err = rq->engine->init_context(rq);
Chris Wilsond2b4b972017-11-10 14:26:33 +00001801
Chris Wilson697b9a82018-06-12 11:51:35 +01001802 i915_request_add(rq);
Chris Wilsond2b4b972017-11-10 14:26:33 +00001803 if (err)
1804 goto err_active;
1805 }
1806
Chris Wilson604c37d2019-03-08 09:36:55 +00001807 /* Flush the default context image to memory, and enable powersaving. */
Chris Wilson23c3c3d2019-04-24 21:07:14 +01001808 if (!i915_gem_load_power_context(i915)) {
Chris Wilson604c37d2019-03-08 09:36:55 +00001809 err = -EIO;
Chris Wilsond2b4b972017-11-10 14:26:33 +00001810 goto err_active;
Chris Wilson2621cef2018-07-09 13:20:43 +01001811 }
Chris Wilsond2b4b972017-11-10 14:26:33 +00001812
Chris Wilsond2b4b972017-11-10 14:26:33 +00001813 for_each_engine(engine, i915, id) {
Chris Wilson5e2a0412019-04-26 17:33:34 +01001814 struct intel_context *ce = e->engines[id];
1815 struct i915_vma *state = ce->state;
Chris Wilson37d7c9c2018-09-14 13:35:03 +01001816 void *vaddr;
Chris Wilsond2b4b972017-11-10 14:26:33 +00001817
Chris Wilsond2b4b972017-11-10 14:26:33 +00001818 if (!state)
1819 continue;
1820
Chris Wilson08819542019-03-08 13:25:22 +00001821 GEM_BUG_ON(intel_context_is_pinned(ce));
Chris Wilsonc4d52fe2019-03-08 13:25:19 +00001822
Chris Wilsond2b4b972017-11-10 14:26:33 +00001823 /*
1824 * As we will hold a reference to the logical state, it will
1825 * not be torn down with the context, and importantly the
1826 * object will hold onto its vma (making it possible for a
1827 * stray GTT write to corrupt our defaults). Unmap the vma
1828 * from the GTT to prevent such accidents and reclaim the
1829 * space.
1830 */
1831 err = i915_vma_unbind(state);
1832 if (err)
1833 goto err_active;
1834
1835 err = i915_gem_object_set_to_cpu_domain(state->obj, false);
1836 if (err)
1837 goto err_active;
1838
1839 engine->default_state = i915_gem_object_get(state->obj);
Chris Wilsona679f582019-03-21 16:19:07 +00001840 i915_gem_object_set_cache_coherency(engine->default_state,
1841 I915_CACHE_LLC);
Chris Wilson37d7c9c2018-09-14 13:35:03 +01001842
1843 /* Check we can acquire the image of the context state */
1844 vaddr = i915_gem_object_pin_map(engine->default_state,
Chris Wilson666424a2018-09-14 13:35:04 +01001845 I915_MAP_FORCE_WB);
Chris Wilson37d7c9c2018-09-14 13:35:03 +01001846 if (IS_ERR(vaddr)) {
1847 err = PTR_ERR(vaddr);
1848 goto err_active;
1849 }
1850
1851 i915_gem_object_unpin_map(engine->default_state);
Chris Wilsond2b4b972017-11-10 14:26:33 +00001852 }
1853
1854 if (IS_ENABLED(CONFIG_DRM_I915_DEBUG_GEM)) {
1855 unsigned int found = intel_engines_has_context_isolation(i915);
1856
1857 /*
1858 * Make sure that classes with multiple engine instances all
1859 * share the same basic configuration.
1860 */
1861 for_each_engine(engine, i915, id) {
1862 unsigned int bit = BIT(engine->uabi_class);
1863 unsigned int expected = engine->default_state ? bit : 0;
1864
1865 if ((found & bit) != expected) {
1866 DRM_ERROR("mismatching default context state for class %d on engine %s\n",
1867 engine->uabi_class, engine->name);
1868 }
1869 }
1870 }
1871
1872out_ctx:
Chris Wilson5e2a0412019-04-26 17:33:34 +01001873 i915_gem_context_unlock_engines(ctx);
Chris Wilsond2b4b972017-11-10 14:26:33 +00001874 i915_gem_context_set_closed(ctx);
1875 i915_gem_context_put(ctx);
1876 return err;
1877
1878err_active:
1879 /*
1880 * If we have to abandon now, we expect the engines to be idle
Chris Wilson604c37d2019-03-08 09:36:55 +00001881 * and ready to be torn-down. The quickest way we can accomplish
1882 * this is by declaring ourselves wedged.
Chris Wilsond2b4b972017-11-10 14:26:33 +00001883 */
Chris Wilson604c37d2019-03-08 09:36:55 +00001884 i915_gem_set_wedged(i915);
Chris Wilsond2b4b972017-11-10 14:26:33 +00001885 goto out_ctx;
1886}
1887
Chris Wilson51797492018-12-04 14:15:16 +00001888static int
1889i915_gem_init_scratch(struct drm_i915_private *i915, unsigned int size)
1890{
1891 struct drm_i915_gem_object *obj;
1892 struct i915_vma *vma;
1893 int ret;
1894
1895 obj = i915_gem_object_create_stolen(i915, size);
1896 if (!obj)
1897 obj = i915_gem_object_create_internal(i915, size);
1898 if (IS_ERR(obj)) {
1899 DRM_ERROR("Failed to allocate scratch page\n");
1900 return PTR_ERR(obj);
1901 }
1902
1903 vma = i915_vma_instance(obj, &i915->ggtt.vm, NULL);
1904 if (IS_ERR(vma)) {
1905 ret = PTR_ERR(vma);
1906 goto err_unref;
1907 }
1908
1909 ret = i915_vma_pin(vma, 0, 0, PIN_GLOBAL | PIN_HIGH);
1910 if (ret)
1911 goto err_unref;
1912
1913 i915->gt.scratch = vma;
1914 return 0;
1915
1916err_unref:
1917 i915_gem_object_put(obj);
1918 return ret;
1919}
1920
1921static void i915_gem_fini_scratch(struct drm_i915_private *i915)
1922{
1923 i915_vma_unpin_and_release(&i915->gt.scratch, 0);
1924}
1925
Chris Wilson254e1182019-04-17 08:56:28 +01001926static int intel_engines_verify_workarounds(struct drm_i915_private *i915)
1927{
1928 struct intel_engine_cs *engine;
1929 enum intel_engine_id id;
1930 int err = 0;
1931
1932 if (!IS_ENABLED(CONFIG_DRM_I915_DEBUG_GEM))
1933 return 0;
1934
1935 for_each_engine(engine, i915, id) {
1936 if (intel_engine_verify_workarounds(engine, "load"))
1937 err = -EIO;
1938 }
1939
1940 return err;
1941}
1942
Tvrtko Ursulinbf9e8422016-12-01 14:16:38 +00001943int i915_gem_init(struct drm_i915_private *dev_priv)
Chris Wilson1070a422012-04-24 15:47:41 +01001944{
Chris Wilson1070a422012-04-24 15:47:41 +01001945 int ret;
1946
Changbin Du52b24162018-05-08 17:07:05 +08001947 /* We need to fallback to 4K pages if host doesn't support huge gtt. */
1948 if (intel_vgpu_active(dev_priv) && !intel_vgpu_has_huge_gtt(dev_priv))
Matthew Auldda9fe3f32017-10-06 23:18:31 +01001949 mkwrite_device_info(dev_priv)->page_sizes =
1950 I915_GTT_PAGE_SIZE_4K;
1951
Chris Wilson94312822017-05-03 10:39:18 +01001952 dev_priv->mm.unordered_timeline = dma_fence_context_alloc(1);
Chris Wilson57822dc2017-02-22 11:40:48 +00001953
Chris Wilson1e345562019-01-28 10:23:56 +00001954 i915_timelines_init(dev_priv);
1955
Chris Wilsonee487002017-11-22 17:26:21 +00001956 ret = i915_gem_init_userptr(dev_priv);
1957 if (ret)
1958 return ret;
1959
Sagar Arun Kamble70deead2018-01-24 21:16:58 +05301960 ret = intel_uc_init_misc(dev_priv);
Michał Winiarski3176ff42017-12-13 23:13:47 +01001961 if (ret)
1962 return ret;
1963
Michal Wajdeczkof7dc0152018-06-28 14:15:21 +00001964 ret = intel_wopcm_init(&dev_priv->wopcm);
1965 if (ret)
1966 goto err_uc_misc;
1967
Chris Wilson5e4f5182015-02-13 14:35:59 +00001968 /* This is just a security blanket to placate dragons.
1969 * On some systems, we very sporadically observe that the first TLBs
1970 * used by the CS may be stale, despite us poking the TLB reset. If
1971 * we hold the forcewake during initialisation these problems
1972 * just magically go away.
1973 */
Chris Wilsonee487002017-11-22 17:26:21 +00001974 mutex_lock(&dev_priv->drm.struct_mutex);
Daniele Ceraolo Spurio3ceea6a2019-03-19 11:35:36 -07001975 intel_uncore_forcewake_get(&dev_priv->uncore, FORCEWAKE_ALL);
Chris Wilson5e4f5182015-02-13 14:35:59 +00001976
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01001977 ret = i915_gem_init_ggtt(dev_priv);
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00001978 if (ret) {
1979 GEM_BUG_ON(ret == -EIO);
1980 goto err_unlock;
1981 }
Jesse Barnesd62b4892013-03-08 10:45:53 -08001982
Chris Wilson51797492018-12-04 14:15:16 +00001983 ret = i915_gem_init_scratch(dev_priv,
Lucas De Marchicf819ef2018-12-12 10:10:43 -08001984 IS_GEN(dev_priv, 2) ? SZ_256K : PAGE_SIZE);
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00001985 if (ret) {
1986 GEM_BUG_ON(ret == -EIO);
1987 goto err_ggtt;
1988 }
Ben Widawsky2fa48d82013-12-06 14:11:04 -08001989
Chris Wilson11334c62019-04-26 17:33:33 +01001990 ret = intel_engines_setup(dev_priv);
1991 if (ret) {
1992 GEM_BUG_ON(ret == -EIO);
1993 goto err_unlock;
1994 }
1995
Chris Wilson51797492018-12-04 14:15:16 +00001996 ret = i915_gem_contexts_init(dev_priv);
1997 if (ret) {
1998 GEM_BUG_ON(ret == -EIO);
1999 goto err_scratch;
2000 }
2001
Tvrtko Ursulinbf9e8422016-12-01 14:16:38 +00002002 ret = intel_engines_init(dev_priv);
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00002003 if (ret) {
2004 GEM_BUG_ON(ret == -EIO);
2005 goto err_context;
2006 }
Daniel Vetter53ca26c2012-04-26 23:28:03 +02002007
Chris Wilsonf58d13d2017-11-10 14:26:29 +00002008 intel_init_gt_powersave(dev_priv);
2009
Michał Winiarski61b5c152017-12-13 23:13:48 +01002010 ret = intel_uc_init(dev_priv);
Chris Wilsoncc6a8182017-11-10 14:26:30 +00002011 if (ret)
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00002012 goto err_pm;
Chris Wilsoncc6a8182017-11-10 14:26:30 +00002013
Michał Winiarski61b5c152017-12-13 23:13:48 +01002014 ret = i915_gem_init_hw(dev_priv);
2015 if (ret)
2016 goto err_uc_init;
2017
Chris Wilsoncc6a8182017-11-10 14:26:30 +00002018 /*
2019 * Despite its name intel_init_clock_gating applies both display
2020 * clock gating workarounds; GT mmio workarounds and the occasional
2021 * GT power context workaround. Worse, sometimes it includes a context
2022 * register workaround which we need to apply before we record the
2023 * default HW state for all contexts.
2024 *
2025 * FIXME: break up the workarounds and apply them at the right time!
2026 */
2027 intel_init_clock_gating(dev_priv);
2028
Chris Wilson254e1182019-04-17 08:56:28 +01002029 ret = intel_engines_verify_workarounds(dev_priv);
2030 if (ret)
2031 goto err_init_hw;
2032
Chris Wilsond2b4b972017-11-10 14:26:33 +00002033 ret = __intel_engines_record_defaults(dev_priv);
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00002034 if (ret)
2035 goto err_init_hw;
2036
2037 if (i915_inject_load_failure()) {
2038 ret = -ENODEV;
2039 goto err_init_hw;
2040 }
2041
2042 if (i915_inject_load_failure()) {
2043 ret = -EIO;
2044 goto err_init_hw;
2045 }
2046
Daniele Ceraolo Spurio3ceea6a2019-03-19 11:35:36 -07002047 intel_uncore_forcewake_put(&dev_priv->uncore, FORCEWAKE_ALL);
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00002048 mutex_unlock(&dev_priv->drm.struct_mutex);
2049
2050 return 0;
2051
2052 /*
2053 * Unwinding is complicated by that we want to handle -EIO to mean
2054 * disable GPU submission but keep KMS alive. We want to mark the
2055 * HW as irrevisibly wedged, but keep enough state around that the
2056 * driver doesn't explode during runtime.
2057 */
2058err_init_hw:
Chris Wilson8571a052018-06-06 15:54:41 +01002059 mutex_unlock(&dev_priv->drm.struct_mutex);
2060
Chris Wilson79ffac852019-04-24 21:07:17 +01002061 i915_gem_set_wedged(dev_priv);
Chris Wilson5861b012019-03-08 09:36:54 +00002062 i915_gem_suspend(dev_priv);
Chris Wilson8571a052018-06-06 15:54:41 +01002063 i915_gem_suspend_late(dev_priv);
2064
Chris Wilson8bcf9f72018-07-10 10:44:20 +01002065 i915_gem_drain_workqueue(dev_priv);
2066
Chris Wilson8571a052018-06-06 15:54:41 +01002067 mutex_lock(&dev_priv->drm.struct_mutex);
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00002068 intel_uc_fini_hw(dev_priv);
Michał Winiarski61b5c152017-12-13 23:13:48 +01002069err_uc_init:
2070 intel_uc_fini(dev_priv);
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00002071err_pm:
2072 if (ret != -EIO) {
2073 intel_cleanup_gt_powersave(dev_priv);
Chris Wilson45b9c962019-05-01 11:32:04 +01002074 intel_engines_cleanup(dev_priv);
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00002075 }
2076err_context:
2077 if (ret != -EIO)
2078 i915_gem_contexts_fini(dev_priv);
Chris Wilson51797492018-12-04 14:15:16 +00002079err_scratch:
2080 i915_gem_fini_scratch(dev_priv);
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00002081err_ggtt:
2082err_unlock:
Daniele Ceraolo Spurio3ceea6a2019-03-19 11:35:36 -07002083 intel_uncore_forcewake_put(&dev_priv->uncore, FORCEWAKE_ALL);
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00002084 mutex_unlock(&dev_priv->drm.struct_mutex);
2085
Michal Wajdeczkof7dc0152018-06-28 14:15:21 +00002086err_uc_misc:
Sagar Arun Kamble70deead2018-01-24 21:16:58 +05302087 intel_uc_fini_misc(dev_priv);
Sagar Arun Kambleda943b52018-01-10 18:24:16 +05302088
Chris Wilson1e345562019-01-28 10:23:56 +00002089 if (ret != -EIO) {
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00002090 i915_gem_cleanup_userptr(dev_priv);
Chris Wilson1e345562019-01-28 10:23:56 +00002091 i915_timelines_fini(dev_priv);
2092 }
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00002093
Chris Wilson60990322014-04-09 09:19:42 +01002094 if (ret == -EIO) {
Chris Wilson7ed43df2018-07-26 09:50:32 +01002095 mutex_lock(&dev_priv->drm.struct_mutex);
2096
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00002097 /*
2098 * Allow engine initialisation to fail by marking the GPU as
Chris Wilson60990322014-04-09 09:19:42 +01002099 * wedged. But we only want to do this where the GPU is angry,
2100 * for all other failure, such as an allocation failure, bail.
2101 */
Chris Wilsonc41166f2019-02-20 14:56:37 +00002102 if (!i915_reset_failed(dev_priv)) {
Chris Wilson51c18bf2018-06-09 12:10:58 +01002103 i915_load_error(dev_priv,
2104 "Failed to initialize GPU, declaring it wedged!\n");
Chris Wilson6f74b362017-10-15 15:37:25 +01002105 i915_gem_set_wedged(dev_priv);
2106 }
Chris Wilson7ed43df2018-07-26 09:50:32 +01002107
2108 /* Minimal basic recovery for KMS */
2109 ret = i915_ggtt_enable_hw(dev_priv);
2110 i915_gem_restore_gtt_mappings(dev_priv);
2111 i915_gem_restore_fences(dev_priv);
2112 intel_init_clock_gating(dev_priv);
2113
2114 mutex_unlock(&dev_priv->drm.struct_mutex);
Chris Wilson1070a422012-04-24 15:47:41 +01002115 }
2116
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00002117 i915_gem_drain_freed_objects(dev_priv);
Chris Wilson60990322014-04-09 09:19:42 +01002118 return ret;
Chris Wilson1070a422012-04-24 15:47:41 +01002119}
2120
Michal Wajdeczko8979187a2018-06-04 09:00:32 +00002121void i915_gem_fini(struct drm_i915_private *dev_priv)
2122{
Chris Wilson79ffac852019-04-24 21:07:17 +01002123 GEM_BUG_ON(dev_priv->gt.awake);
2124
Chris Wilsonb27e35a2019-05-27 12:51:14 +01002125 intel_wakeref_auto_fini(&dev_priv->mm.userfault_wakeref);
2126
Michal Wajdeczko8979187a2018-06-04 09:00:32 +00002127 i915_gem_suspend_late(dev_priv);
Chris Wilson30b710842018-08-12 23:36:29 +01002128 intel_disable_gt_powersave(dev_priv);
Michal Wajdeczko8979187a2018-06-04 09:00:32 +00002129
2130 /* Flush any outstanding unpin_work. */
2131 i915_gem_drain_workqueue(dev_priv);
2132
2133 mutex_lock(&dev_priv->drm.struct_mutex);
2134 intel_uc_fini_hw(dev_priv);
2135 intel_uc_fini(dev_priv);
Chris Wilson45b9c962019-05-01 11:32:04 +01002136 intel_engines_cleanup(dev_priv);
Michal Wajdeczko8979187a2018-06-04 09:00:32 +00002137 i915_gem_contexts_fini(dev_priv);
Chris Wilson51797492018-12-04 14:15:16 +00002138 i915_gem_fini_scratch(dev_priv);
Michal Wajdeczko8979187a2018-06-04 09:00:32 +00002139 mutex_unlock(&dev_priv->drm.struct_mutex);
2140
Tvrtko Ursulin25d140f2018-12-03 13:33:19 +00002141 intel_wa_list_free(&dev_priv->gt_wa_list);
2142
Chris Wilson30b710842018-08-12 23:36:29 +01002143 intel_cleanup_gt_powersave(dev_priv);
2144
Michal Wajdeczko8979187a2018-06-04 09:00:32 +00002145 intel_uc_fini_misc(dev_priv);
2146 i915_gem_cleanup_userptr(dev_priv);
Chris Wilson1e345562019-01-28 10:23:56 +00002147 i915_timelines_fini(dev_priv);
Michal Wajdeczko8979187a2018-06-04 09:00:32 +00002148
2149 i915_gem_drain_freed_objects(dev_priv);
2150
2151 WARN_ON(!list_empty(&dev_priv->contexts.list));
2152}
2153
Chris Wilson24145512017-01-24 11:01:35 +00002154void i915_gem_init_mmio(struct drm_i915_private *i915)
2155{
2156 i915_gem_sanitize(i915);
2157}
2158
Zou Nan hai8187a2b2010-05-21 09:08:55 +08002159void
Imre Deak40ae4e12016-03-16 14:54:03 +02002160i915_gem_load_init_fences(struct drm_i915_private *dev_priv)
2161{
Chris Wilson49ef5292016-08-18 17:17:00 +01002162 int i;
Imre Deak40ae4e12016-03-16 14:54:03 +02002163
Tvrtko Ursulinc56b89f2018-02-09 21:58:46 +00002164 if (INTEL_GEN(dev_priv) >= 7 && !IS_VALLEYVIEW(dev_priv) &&
Imre Deak40ae4e12016-03-16 14:54:03 +02002165 !IS_CHERRYVIEW(dev_priv))
2166 dev_priv->num_fence_regs = 32;
Tvrtko Ursulinc56b89f2018-02-09 21:58:46 +00002167 else if (INTEL_GEN(dev_priv) >= 4 ||
Jani Nikula73f67aa2016-12-07 22:48:09 +02002168 IS_I945G(dev_priv) || IS_I945GM(dev_priv) ||
2169 IS_G33(dev_priv) || IS_PINEVIEW(dev_priv))
Imre Deak40ae4e12016-03-16 14:54:03 +02002170 dev_priv->num_fence_regs = 16;
2171 else
2172 dev_priv->num_fence_regs = 8;
2173
Chris Wilsonc0336662016-05-06 15:40:21 +01002174 if (intel_vgpu_active(dev_priv))
Imre Deak40ae4e12016-03-16 14:54:03 +02002175 dev_priv->num_fence_regs =
2176 I915_READ(vgtif_reg(avail_rs.fence_num));
2177
2178 /* Initialize fence registers to zero */
Chris Wilson49ef5292016-08-18 17:17:00 +01002179 for (i = 0; i < dev_priv->num_fence_regs; i++) {
2180 struct drm_i915_fence_reg *fence = &dev_priv->fence_regs[i];
2181
2182 fence->i915 = dev_priv;
2183 fence->id = i;
2184 list_add_tail(&fence->link, &dev_priv->mm.fence_list);
2185 }
Tvrtko Ursulin4362f4f2016-11-16 08:55:33 +00002186 i915_gem_restore_fences(dev_priv);
Imre Deak40ae4e12016-03-16 14:54:03 +02002187
Tvrtko Ursulin4362f4f2016-11-16 08:55:33 +00002188 i915_gem_detect_bit_6_swizzle(dev_priv);
Imre Deak40ae4e12016-03-16 14:54:03 +02002189}
2190
Chris Wilson9c52d1c2017-11-10 23:24:47 +00002191static void i915_gem_init__mm(struct drm_i915_private *i915)
2192{
2193 spin_lock_init(&i915->mm.object_stat_lock);
2194 spin_lock_init(&i915->mm.obj_lock);
2195 spin_lock_init(&i915->mm.free_lock);
2196
2197 init_llist_head(&i915->mm.free_list);
2198
2199 INIT_LIST_HEAD(&i915->mm.unbound_list);
2200 INIT_LIST_HEAD(&i915->mm.bound_list);
2201 INIT_LIST_HEAD(&i915->mm.fence_list);
Chris Wilsonb27e35a2019-05-27 12:51:14 +01002202
Chris Wilson9c52d1c2017-11-10 23:24:47 +00002203 INIT_LIST_HEAD(&i915->mm.userfault_list);
Chris Wilsonb27e35a2019-05-27 12:51:14 +01002204 intel_wakeref_auto_init(&i915->mm.userfault_wakeref, i915);
Chris Wilson9c52d1c2017-11-10 23:24:47 +00002205
Chris Wilson84753552019-05-28 10:29:45 +01002206 i915_gem_init__objects(i915);
Chris Wilson9c52d1c2017-11-10 23:24:47 +00002207}
2208
Michal Wajdeczkoa0de9082018-03-23 12:34:49 +00002209int i915_gem_init_early(struct drm_i915_private *dev_priv)
Eric Anholt673a3942008-07-30 12:06:12 -07002210{
Chris Wilson13f1bfd2019-02-28 10:20:34 +00002211 int err;
Chris Wilsond1b48c12017-08-16 09:52:08 +01002212
Chris Wilson79ffac852019-04-24 21:07:17 +01002213 intel_gt_pm_init(dev_priv);
2214
Chris Wilson643b4502018-04-30 14:15:03 +01002215 INIT_LIST_HEAD(&dev_priv->gt.active_rings);
Chris Wilson3365e222018-05-03 20:51:14 +01002216 INIT_LIST_HEAD(&dev_priv->gt.closed_vma);
Chris Wilson643b4502018-04-30 14:15:03 +01002217
Chris Wilson9c52d1c2017-11-10 23:24:47 +00002218 i915_gem_init__mm(dev_priv);
Chris Wilson23c3c3d2019-04-24 21:07:14 +01002219 i915_gem_init__pm(dev_priv);
Chris Wilsonf2123812017-10-16 12:40:37 +01002220
Chris Wilson1f15b762016-07-01 17:23:14 +01002221 init_waitqueue_head(&dev_priv->gpu_error.wait_queue);
Daniel Vetter1f83fee2012-11-15 17:17:22 +01002222 init_waitqueue_head(&dev_priv->gpu_error.reset_queue);
Chris Wilson18bb2bc2019-01-14 21:04:01 +00002223 mutex_init(&dev_priv->gpu_error.wedge_mutex);
Chris Wilson2caffbf2019-02-08 15:37:03 +00002224 init_srcu_struct(&dev_priv->gpu_error.reset_backoff_srcu);
Chris Wilson31169712009-09-14 16:50:28 +01002225
Joonas Lahtinen6f633402016-09-01 14:58:21 +03002226 atomic_set(&dev_priv->mm.bsd_engine_dispatch_index, 0);
2227
Chris Wilsonb5add952016-08-04 16:32:36 +01002228 spin_lock_init(&dev_priv->fb_tracking.lock);
Chris Wilson73cb9702016-10-28 13:58:46 +01002229
Matthew Auld465c4032017-10-06 23:18:14 +01002230 err = i915_gemfs_init(dev_priv);
2231 if (err)
2232 DRM_NOTE("Unable to create a private tmpfs mount, hugepage support will be disabled(%d).\n", err);
2233
Chris Wilson73cb9702016-10-28 13:58:46 +01002234 return 0;
Eric Anholt673a3942008-07-30 12:06:12 -07002235}
Dave Airlie71acb5e2008-12-30 20:31:46 +10002236
Michal Wajdeczkoa0de9082018-03-23 12:34:49 +00002237void i915_gem_cleanup_early(struct drm_i915_private *dev_priv)
Imre Deakd64aa092016-01-19 15:26:29 +02002238{
Chris Wilsonc4d4c1c2017-02-10 16:35:23 +00002239 i915_gem_drain_freed_objects(dev_priv);
Chris Wilsonc9c704712018-02-19 22:06:31 +00002240 GEM_BUG_ON(!llist_empty(&dev_priv->mm.free_list));
2241 GEM_BUG_ON(atomic_read(&dev_priv->mm.free_count));
Chris Wilsonc4d4c1c2017-02-10 16:35:23 +00002242 WARN_ON(dev_priv->mm.object_count);
Matthew Auldea84aa72016-11-17 21:04:11 +00002243
Chris Wilson2caffbf2019-02-08 15:37:03 +00002244 cleanup_srcu_struct(&dev_priv->gpu_error.reset_backoff_srcu);
2245
Matthew Auld465c4032017-10-06 23:18:14 +01002246 i915_gemfs_fini(dev_priv);
Imre Deakd64aa092016-01-19 15:26:29 +02002247}
2248
Chris Wilson6a800ea2016-09-21 14:51:07 +01002249int i915_gem_freeze(struct drm_i915_private *dev_priv)
2250{
Chris Wilsond0aa3012017-04-07 11:25:49 +01002251 /* Discard all purgeable objects, let userspace recover those as
2252 * required after resuming.
2253 */
Chris Wilson6a800ea2016-09-21 14:51:07 +01002254 i915_gem_shrink_all(dev_priv);
Chris Wilson6a800ea2016-09-21 14:51:07 +01002255
Chris Wilson6a800ea2016-09-21 14:51:07 +01002256 return 0;
2257}
2258
Chris Wilson95c778d2018-06-01 15:41:25 +01002259int i915_gem_freeze_late(struct drm_i915_private *i915)
Chris Wilson461fb992016-05-14 07:26:33 +01002260{
2261 struct drm_i915_gem_object *obj;
Chris Wilson7aab2d52016-09-09 20:02:18 +01002262 struct list_head *phases[] = {
Chris Wilson95c778d2018-06-01 15:41:25 +01002263 &i915->mm.unbound_list,
2264 &i915->mm.bound_list,
Chris Wilson7aab2d52016-09-09 20:02:18 +01002265 NULL
Chris Wilson95c778d2018-06-01 15:41:25 +01002266 }, **phase;
Chris Wilson461fb992016-05-14 07:26:33 +01002267
Chris Wilson95c778d2018-06-01 15:41:25 +01002268 /*
2269 * Called just before we write the hibernation image.
Chris Wilson461fb992016-05-14 07:26:33 +01002270 *
2271 * We need to update the domain tracking to reflect that the CPU
2272 * will be accessing all the pages to create and restore from the
2273 * hibernation, and so upon restoration those pages will be in the
2274 * CPU domain.
2275 *
2276 * To make sure the hibernation image contains the latest state,
2277 * we update that state just before writing out the image.
Chris Wilson7aab2d52016-09-09 20:02:18 +01002278 *
2279 * To try and reduce the hibernation image, we manually shrink
Chris Wilsond0aa3012017-04-07 11:25:49 +01002280 * the objects as well, see i915_gem_freeze()
Chris Wilson461fb992016-05-14 07:26:33 +01002281 */
2282
Chris Wilson95c778d2018-06-01 15:41:25 +01002283 i915_gem_shrink(i915, -1UL, NULL, I915_SHRINK_UNBOUND);
2284 i915_gem_drain_freed_objects(i915);
Chris Wilson461fb992016-05-14 07:26:33 +01002285
Chris Wilson95c778d2018-06-01 15:41:25 +01002286 mutex_lock(&i915->drm.struct_mutex);
2287 for (phase = phases; *phase; phase++) {
2288 list_for_each_entry(obj, *phase, mm.link)
2289 WARN_ON(i915_gem_object_set_to_cpu_domain(obj, true));
Chris Wilson461fb992016-05-14 07:26:33 +01002290 }
Chris Wilson95c778d2018-06-01 15:41:25 +01002291 mutex_unlock(&i915->drm.struct_mutex);
Chris Wilson461fb992016-05-14 07:26:33 +01002292
2293 return 0;
2294}
2295
Chris Wilsonf787a5f2010-09-24 16:02:42 +01002296void i915_gem_release(struct drm_device *dev, struct drm_file *file)
Eric Anholtb9624422009-06-03 07:27:35 +00002297{
Chris Wilsonf787a5f2010-09-24 16:02:42 +01002298 struct drm_i915_file_private *file_priv = file->driver_priv;
Chris Wilsone61e0f52018-02-21 09:56:36 +00002299 struct i915_request *request;
Eric Anholtb9624422009-06-03 07:27:35 +00002300
2301 /* Clean up our request list when the client is going away, so that
2302 * later retire_requests won't dereference our soon-to-be-gone
2303 * file_priv.
2304 */
Chris Wilson1c255952010-09-26 11:03:27 +01002305 spin_lock(&file_priv->mm.lock);
Chris Wilsonc8659ef2017-03-02 12:25:25 +00002306 list_for_each_entry(request, &file_priv->mm.request_list, client_link)
Chris Wilsonf787a5f2010-09-24 16:02:42 +01002307 request->file_priv = NULL;
Chris Wilson1c255952010-09-26 11:03:27 +01002308 spin_unlock(&file_priv->mm.lock);
Chris Wilsonb29c19b2013-09-25 17:34:56 +01002309}
2310
Chris Wilson829a0af2017-06-20 12:05:45 +01002311int i915_gem_open(struct drm_i915_private *i915, struct drm_file *file)
Chris Wilsonb29c19b2013-09-25 17:34:56 +01002312{
2313 struct drm_i915_file_private *file_priv;
Ben Widawskye422b882013-12-06 14:10:58 -08002314 int ret;
Chris Wilsonb29c19b2013-09-25 17:34:56 +01002315
Chris Wilsonc4c29d72016-11-09 10:45:07 +00002316 DRM_DEBUG("\n");
Chris Wilsonb29c19b2013-09-25 17:34:56 +01002317
2318 file_priv = kzalloc(sizeof(*file_priv), GFP_KERNEL);
2319 if (!file_priv)
2320 return -ENOMEM;
2321
2322 file->driver_priv = file_priv;
Chris Wilson829a0af2017-06-20 12:05:45 +01002323 file_priv->dev_priv = i915;
Chris Wilsonab0e7ff2014-02-25 17:11:24 +02002324 file_priv->file = file;
Chris Wilsonb29c19b2013-09-25 17:34:56 +01002325
2326 spin_lock_init(&file_priv->mm.lock);
2327 INIT_LIST_HEAD(&file_priv->mm.request_list);
Chris Wilsonb29c19b2013-09-25 17:34:56 +01002328
Chris Wilsonc80ff162016-07-27 09:07:27 +01002329 file_priv->bsd_engine = -1;
Mika Kuoppala14921f32018-06-15 13:44:29 +03002330 file_priv->hang_timestamp = jiffies;
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00002331
Chris Wilson829a0af2017-06-20 12:05:45 +01002332 ret = i915_gem_context_open(i915, file);
Ben Widawskye422b882013-12-06 14:10:58 -08002333 if (ret)
2334 kfree(file_priv);
Chris Wilsonb29c19b2013-09-25 17:34:56 +01002335
Ben Widawskye422b882013-12-06 14:10:58 -08002336 return ret;
Chris Wilsonb29c19b2013-09-25 17:34:56 +01002337}
2338
Daniel Vetterb680c372014-09-19 18:27:27 +02002339/**
2340 * i915_gem_track_fb - update frontbuffer tracking
Geliang Tangd9072a32015-09-15 05:58:44 -07002341 * @old: current GEM buffer for the frontbuffer slots
2342 * @new: new GEM buffer for the frontbuffer slots
2343 * @frontbuffer_bits: bitmask of frontbuffer slots
Daniel Vetterb680c372014-09-19 18:27:27 +02002344 *
2345 * This updates the frontbuffer tracking bits @frontbuffer_bits by clearing them
2346 * from @old and setting them in @new. Both @old and @new can be NULL.
2347 */
Daniel Vettera071fa02014-06-18 23:28:09 +02002348void i915_gem_track_fb(struct drm_i915_gem_object *old,
2349 struct drm_i915_gem_object *new,
2350 unsigned frontbuffer_bits)
2351{
Chris Wilsonfaf5bf02016-08-04 16:32:37 +01002352 /* Control of individual bits within the mask are guarded by
2353 * the owning plane->mutex, i.e. we can never see concurrent
2354 * manipulation of individual bits. But since the bitfield as a whole
2355 * is updated using RMW, we need to use atomics in order to update
2356 * the bits.
2357 */
2358 BUILD_BUG_ON(INTEL_FRONTBUFFER_BITS_PER_PIPE * I915_MAX_PIPES >
Chris Wilson74f6e182018-09-26 11:47:07 +01002359 BITS_PER_TYPE(atomic_t));
Chris Wilsonfaf5bf02016-08-04 16:32:37 +01002360
Daniel Vettera071fa02014-06-18 23:28:09 +02002361 if (old) {
Chris Wilsonfaf5bf02016-08-04 16:32:37 +01002362 WARN_ON(!(atomic_read(&old->frontbuffer_bits) & frontbuffer_bits));
2363 atomic_andnot(frontbuffer_bits, &old->frontbuffer_bits);
Daniel Vettera071fa02014-06-18 23:28:09 +02002364 }
2365
2366 if (new) {
Chris Wilsonfaf5bf02016-08-04 16:32:37 +01002367 WARN_ON(atomic_read(&new->frontbuffer_bits) & frontbuffer_bits);
2368 atomic_or(frontbuffer_bits, &new->frontbuffer_bits);
Daniel Vettera071fa02014-06-18 23:28:09 +02002369 }
2370}
2371
Chris Wilson935a2f72017-02-13 17:15:13 +00002372#if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
2373#include "selftests/scatterlist.c"
Chris Wilson66d9cb52017-02-13 17:15:17 +00002374#include "selftests/mock_gem_device.c"
Chris Wilson3f51b7e12018-08-30 14:48:06 +01002375#include "selftests/i915_gem.c"
Chris Wilson935a2f72017-02-13 17:15:13 +00002376#endif