blob: 537f502123ea2c3dd7d54999a27a73bdc0f360bf [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 Howells760285e2012-10-02 18:01:07 +010028#include <drm/drmP.h>
David Herrmann0de23972013-07-24 21:07:52 +020029#include <drm/drm_vma_manager.h>
David Howells760285e2012-10-02 18:01:07 +010030#include <drm/i915_drm.h>
Eric Anholt673a3942008-07-30 12:06:12 -070031#include "i915_drv.h"
Chris Wilsonc13d87e2016-07-20 09:21:15 +010032#include "i915_gem_dmabuf.h"
Yu Zhangeb822892015-02-10 19:05:49 +080033#include "i915_vgpu.h"
Chris Wilson1c5d22f2009-08-25 11:15:50 +010034#include "i915_trace.h"
Jesse Barnes652c3932009-08-17 13:31:43 -070035#include "intel_drv.h"
Chris Wilson5d723d72016-08-04 16:32:35 +010036#include "intel_frontbuffer.h"
Peter Antoine0ccdacf2016-04-13 15:03:25 +010037#include "intel_mocs.h"
Chris Wilsonc13d87e2016-07-20 09:21:15 +010038#include <linux/reservation.h>
Hugh Dickins5949eac2011-06-27 16:18:18 -070039#include <linux/shmem_fs.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090040#include <linux/slab.h>
Eric Anholt673a3942008-07-30 12:06:12 -070041#include <linux/swap.h>
Jesse Barnes79e53942008-11-07 14:24:08 -080042#include <linux/pci.h>
Daniel Vetter1286ff72012-05-10 15:25:09 +020043#include <linux/dma-buf.h>
Eric Anholt673a3942008-07-30 12:06:12 -070044
Chris Wilson05394f32010-11-08 19:18:58 +000045static void i915_gem_object_flush_gtt_write_domain(struct drm_i915_gem_object *obj);
Daniel Vettere62b59e2015-01-21 14:53:48 +010046static void i915_gem_object_flush_cpu_write_domain(struct drm_i915_gem_object *obj);
Chris Wilson61050802012-04-17 15:31:31 +010047
Chris Wilsonc76ce032013-08-08 14:41:03 +010048static bool cpu_cache_is_coherent(struct drm_device *dev,
49 enum i915_cache_level level)
50{
51 return HAS_LLC(dev) || level != I915_CACHE_NONE;
52}
53
Chris Wilson2c225692013-08-09 12:26:45 +010054static bool cpu_write_needs_clflush(struct drm_i915_gem_object *obj)
55{
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +053056 if (obj->base.write_domain == I915_GEM_DOMAIN_CPU)
57 return false;
58
Chris Wilson2c225692013-08-09 12:26:45 +010059 if (!cpu_cache_is_coherent(obj->base.dev, obj->cache_level))
60 return true;
61
62 return obj->pin_display;
63}
64
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +053065static int
66insert_mappable_node(struct drm_i915_private *i915,
67 struct drm_mm_node *node, u32 size)
68{
69 memset(node, 0, sizeof(*node));
70 return drm_mm_insert_node_in_range_generic(&i915->ggtt.base.mm, node,
71 size, 0, 0, 0,
72 i915->ggtt.mappable_end,
73 DRM_MM_SEARCH_DEFAULT,
74 DRM_MM_CREATE_DEFAULT);
75}
76
77static void
78remove_mappable_node(struct drm_mm_node *node)
79{
80 drm_mm_remove_node(node);
81}
82
Chris Wilson73aa8082010-09-30 11:46:12 +010083/* some bookkeeping */
84static void i915_gem_info_add_obj(struct drm_i915_private *dev_priv,
Chris Wilson3ef7f222016-10-18 13:02:48 +010085 u64 size)
Chris Wilson73aa8082010-09-30 11:46:12 +010086{
Daniel Vetterc20e8352013-07-24 22:40:23 +020087 spin_lock(&dev_priv->mm.object_stat_lock);
Chris Wilson73aa8082010-09-30 11:46:12 +010088 dev_priv->mm.object_count++;
89 dev_priv->mm.object_memory += size;
Daniel Vetterc20e8352013-07-24 22:40:23 +020090 spin_unlock(&dev_priv->mm.object_stat_lock);
Chris Wilson73aa8082010-09-30 11:46:12 +010091}
92
93static void i915_gem_info_remove_obj(struct drm_i915_private *dev_priv,
Chris Wilson3ef7f222016-10-18 13:02:48 +010094 u64 size)
Chris Wilson73aa8082010-09-30 11:46:12 +010095{
Daniel Vetterc20e8352013-07-24 22:40:23 +020096 spin_lock(&dev_priv->mm.object_stat_lock);
Chris Wilson73aa8082010-09-30 11:46:12 +010097 dev_priv->mm.object_count--;
98 dev_priv->mm.object_memory -= size;
Daniel Vetterc20e8352013-07-24 22:40:23 +020099 spin_unlock(&dev_priv->mm.object_stat_lock);
Chris Wilson73aa8082010-09-30 11:46:12 +0100100}
101
Chris Wilson21dd3732011-01-26 15:55:56 +0000102static int
Daniel Vetter33196de2012-11-14 17:14:05 +0100103i915_gem_wait_for_error(struct i915_gpu_error *error)
Chris Wilson30dbf0c2010-09-25 10:19:17 +0100104{
Chris Wilson30dbf0c2010-09-25 10:19:17 +0100105 int ret;
106
Chris Wilsond98c52c2016-04-13 17:35:05 +0100107 if (!i915_reset_in_progress(error))
Chris Wilson30dbf0c2010-09-25 10:19:17 +0100108 return 0;
109
Daniel Vetter0a6759c2012-07-04 22:18:41 +0200110 /*
111 * Only wait 10 seconds for the gpu reset to complete to avoid hanging
112 * userspace. If it takes that long something really bad is going on and
113 * we should simply try to bail out and fail as gracefully as possible.
114 */
Daniel Vetter1f83fee2012-11-15 17:17:22 +0100115 ret = wait_event_interruptible_timeout(error->reset_queue,
Chris Wilsond98c52c2016-04-13 17:35:05 +0100116 !i915_reset_in_progress(error),
Chris Wilsonb52992c2016-10-28 13:58:24 +0100117 I915_RESET_TIMEOUT);
Daniel Vetter0a6759c2012-07-04 22:18:41 +0200118 if (ret == 0) {
119 DRM_ERROR("Timed out waiting for the gpu reset to complete\n");
120 return -EIO;
121 } else if (ret < 0) {
Chris Wilson30dbf0c2010-09-25 10:19:17 +0100122 return ret;
Chris Wilsond98c52c2016-04-13 17:35:05 +0100123 } else {
124 return 0;
Daniel Vetter0a6759c2012-07-04 22:18:41 +0200125 }
Chris Wilson30dbf0c2010-09-25 10:19:17 +0100126}
127
Chris Wilson54cf91d2010-11-25 18:00:26 +0000128int i915_mutex_lock_interruptible(struct drm_device *dev)
Chris Wilson76c1dec2010-09-25 11:22:51 +0100129{
Chris Wilsonfac5e232016-07-04 11:34:36 +0100130 struct drm_i915_private *dev_priv = to_i915(dev);
Chris Wilson76c1dec2010-09-25 11:22:51 +0100131 int ret;
132
Daniel Vetter33196de2012-11-14 17:14:05 +0100133 ret = i915_gem_wait_for_error(&dev_priv->gpu_error);
Chris Wilson76c1dec2010-09-25 11:22:51 +0100134 if (ret)
135 return ret;
136
137 ret = mutex_lock_interruptible(&dev->struct_mutex);
138 if (ret)
139 return ret;
140
Chris Wilson76c1dec2010-09-25 11:22:51 +0100141 return 0;
142}
Chris Wilson30dbf0c2010-09-25 10:19:17 +0100143
Eric Anholt673a3942008-07-30 12:06:12 -0700144int
Eric Anholt5a125c32008-10-22 21:40:13 -0700145i915_gem_get_aperture_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +0000146 struct drm_file *file)
Eric Anholt5a125c32008-10-22 21:40:13 -0700147{
Joonas Lahtinen72e96d62016-03-30 16:57:10 +0300148 struct drm_i915_private *dev_priv = to_i915(dev);
Joonas Lahtinen62106b42016-03-18 10:42:57 +0200149 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Joonas Lahtinen72e96d62016-03-30 16:57:10 +0300150 struct drm_i915_gem_get_aperture *args = data;
Tvrtko Ursulinca1543b2015-07-01 11:51:10 +0100151 struct i915_vma *vma;
Chris Wilson6299f992010-11-24 12:23:44 +0000152 size_t pinned;
Eric Anholt5a125c32008-10-22 21:40:13 -0700153
Chris Wilson6299f992010-11-24 12:23:44 +0000154 pinned = 0;
Chris Wilson73aa8082010-09-30 11:46:12 +0100155 mutex_lock(&dev->struct_mutex);
Chris Wilson1c7f4bc2016-02-26 11:03:19 +0000156 list_for_each_entry(vma, &ggtt->base.active_list, vm_link)
Chris Wilson20dfbde2016-08-04 16:32:30 +0100157 if (i915_vma_is_pinned(vma))
Tvrtko Ursulinca1543b2015-07-01 11:51:10 +0100158 pinned += vma->node.size;
Chris Wilson1c7f4bc2016-02-26 11:03:19 +0000159 list_for_each_entry(vma, &ggtt->base.inactive_list, vm_link)
Chris Wilson20dfbde2016-08-04 16:32:30 +0100160 if (i915_vma_is_pinned(vma))
Tvrtko Ursulinca1543b2015-07-01 11:51:10 +0100161 pinned += vma->node.size;
Chris Wilson73aa8082010-09-30 11:46:12 +0100162 mutex_unlock(&dev->struct_mutex);
Eric Anholt5a125c32008-10-22 21:40:13 -0700163
Joonas Lahtinen72e96d62016-03-30 16:57:10 +0300164 args->aper_size = ggtt->base.total;
Akshay Joshi0206e352011-08-16 15:34:10 -0400165 args->aper_available_size = args->aper_size - pinned;
Chris Wilson6299f992010-11-24 12:23:44 +0000166
Eric Anholt5a125c32008-10-22 21:40:13 -0700167 return 0;
168}
169
Chris Wilson6a2c4232014-11-04 04:51:40 -0800170static int
171i915_gem_object_get_pages_phys(struct drm_i915_gem_object *obj)
Chris Wilson00731152014-05-21 12:42:56 +0100172{
Al Viro93c76a32015-12-04 23:45:44 -0500173 struct address_space *mapping = obj->base.filp->f_mapping;
Chris Wilson6a2c4232014-11-04 04:51:40 -0800174 char *vaddr = obj->phys_handle->vaddr;
175 struct sg_table *st;
176 struct scatterlist *sg;
177 int i;
Chris Wilson00731152014-05-21 12:42:56 +0100178
Chris Wilson6a2c4232014-11-04 04:51:40 -0800179 if (WARN_ON(i915_gem_object_needs_bit17_swizzle(obj)))
180 return -EINVAL;
Chris Wilson00731152014-05-21 12:42:56 +0100181
Chris Wilson6a2c4232014-11-04 04:51:40 -0800182 for (i = 0; i < obj->base.size / PAGE_SIZE; i++) {
183 struct page *page;
184 char *src;
185
186 page = shmem_read_mapping_page(mapping, i);
187 if (IS_ERR(page))
188 return PTR_ERR(page);
189
190 src = kmap_atomic(page);
191 memcpy(vaddr, src, PAGE_SIZE);
192 drm_clflush_virt_range(vaddr, PAGE_SIZE);
193 kunmap_atomic(src);
194
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300195 put_page(page);
Chris Wilson6a2c4232014-11-04 04:51:40 -0800196 vaddr += PAGE_SIZE;
197 }
198
Chris Wilsonc0336662016-05-06 15:40:21 +0100199 i915_gem_chipset_flush(to_i915(obj->base.dev));
Chris Wilson6a2c4232014-11-04 04:51:40 -0800200
201 st = kmalloc(sizeof(*st), GFP_KERNEL);
202 if (st == NULL)
203 return -ENOMEM;
204
205 if (sg_alloc_table(st, 1, GFP_KERNEL)) {
206 kfree(st);
207 return -ENOMEM;
208 }
209
210 sg = st->sgl;
211 sg->offset = 0;
212 sg->length = obj->base.size;
213
214 sg_dma_address(sg) = obj->phys_handle->busaddr;
215 sg_dma_len(sg) = obj->base.size;
216
217 obj->pages = st;
Chris Wilson6a2c4232014-11-04 04:51:40 -0800218 return 0;
219}
220
221static void
222i915_gem_object_put_pages_phys(struct drm_i915_gem_object *obj)
223{
224 int ret;
225
226 BUG_ON(obj->madv == __I915_MADV_PURGED);
227
228 ret = i915_gem_object_set_to_cpu_domain(obj, true);
Chris Wilsonf4457ae2016-04-13 17:35:08 +0100229 if (WARN_ON(ret)) {
Chris Wilson6a2c4232014-11-04 04:51:40 -0800230 /* In the event of a disaster, abandon all caches and
231 * hope for the best.
232 */
Chris Wilson6a2c4232014-11-04 04:51:40 -0800233 obj->base.read_domains = obj->base.write_domain = I915_GEM_DOMAIN_CPU;
234 }
235
236 if (obj->madv == I915_MADV_DONTNEED)
237 obj->dirty = 0;
238
239 if (obj->dirty) {
Al Viro93c76a32015-12-04 23:45:44 -0500240 struct address_space *mapping = obj->base.filp->f_mapping;
Chris Wilson6a2c4232014-11-04 04:51:40 -0800241 char *vaddr = obj->phys_handle->vaddr;
Chris Wilson00731152014-05-21 12:42:56 +0100242 int i;
243
244 for (i = 0; i < obj->base.size / PAGE_SIZE; i++) {
Chris Wilson6a2c4232014-11-04 04:51:40 -0800245 struct page *page;
246 char *dst;
Chris Wilson00731152014-05-21 12:42:56 +0100247
Chris Wilson6a2c4232014-11-04 04:51:40 -0800248 page = shmem_read_mapping_page(mapping, i);
249 if (IS_ERR(page))
250 continue;
251
252 dst = kmap_atomic(page);
253 drm_clflush_virt_range(vaddr, PAGE_SIZE);
254 memcpy(dst, vaddr, PAGE_SIZE);
255 kunmap_atomic(dst);
256
257 set_page_dirty(page);
258 if (obj->madv == I915_MADV_WILLNEED)
Chris Wilson00731152014-05-21 12:42:56 +0100259 mark_page_accessed(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300260 put_page(page);
Chris Wilson00731152014-05-21 12:42:56 +0100261 vaddr += PAGE_SIZE;
262 }
Chris Wilson6a2c4232014-11-04 04:51:40 -0800263 obj->dirty = 0;
Chris Wilson00731152014-05-21 12:42:56 +0100264 }
265
Chris Wilson6a2c4232014-11-04 04:51:40 -0800266 sg_free_table(obj->pages);
267 kfree(obj->pages);
Chris Wilson6a2c4232014-11-04 04:51:40 -0800268}
269
270static void
271i915_gem_object_release_phys(struct drm_i915_gem_object *obj)
272{
273 drm_pci_free(obj->base.dev, obj->phys_handle);
274}
275
276static const struct drm_i915_gem_object_ops i915_gem_phys_ops = {
277 .get_pages = i915_gem_object_get_pages_phys,
278 .put_pages = i915_gem_object_put_pages_phys,
279 .release = i915_gem_object_release_phys,
280};
281
Chris Wilson35a96112016-08-14 18:44:40 +0100282int i915_gem_object_unbind(struct drm_i915_gem_object *obj)
Chris Wilsonaa653a62016-08-04 07:52:27 +0100283{
284 struct i915_vma *vma;
285 LIST_HEAD(still_in_list);
Chris Wilson02bef8f2016-08-14 18:44:41 +0100286 int ret;
Chris Wilsonaa653a62016-08-04 07:52:27 +0100287
Chris Wilson02bef8f2016-08-14 18:44:41 +0100288 lockdep_assert_held(&obj->base.dev->struct_mutex);
289
290 /* Closed vma are removed from the obj->vma_list - but they may
291 * still have an active binding on the object. To remove those we
292 * must wait for all rendering to complete to the object (as unbinding
293 * must anyway), and retire the requests.
Chris Wilsonaa653a62016-08-04 07:52:27 +0100294 */
Chris Wilsone95433c2016-10-28 13:58:27 +0100295 ret = i915_gem_object_wait(obj,
296 I915_WAIT_INTERRUPTIBLE |
297 I915_WAIT_LOCKED |
298 I915_WAIT_ALL,
299 MAX_SCHEDULE_TIMEOUT,
300 NULL);
Chris Wilson02bef8f2016-08-14 18:44:41 +0100301 if (ret)
302 return ret;
303
304 i915_gem_retire_requests(to_i915(obj->base.dev));
305
Chris Wilsonaa653a62016-08-04 07:52:27 +0100306 while ((vma = list_first_entry_or_null(&obj->vma_list,
307 struct i915_vma,
308 obj_link))) {
309 list_move_tail(&vma->obj_link, &still_in_list);
310 ret = i915_vma_unbind(vma);
311 if (ret)
312 break;
313 }
314 list_splice(&still_in_list, &obj->vma_list);
315
316 return ret;
317}
318
Chris Wilsone95433c2016-10-28 13:58:27 +0100319static long
320i915_gem_object_wait_fence(struct dma_fence *fence,
321 unsigned int flags,
322 long timeout,
323 struct intel_rps_client *rps)
324{
325 struct drm_i915_gem_request *rq;
326
327 BUILD_BUG_ON(I915_WAIT_INTERRUPTIBLE != 0x1);
328
329 if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags))
330 return timeout;
331
332 if (!dma_fence_is_i915(fence))
333 return dma_fence_wait_timeout(fence,
334 flags & I915_WAIT_INTERRUPTIBLE,
335 timeout);
336
337 rq = to_request(fence);
338 if (i915_gem_request_completed(rq))
339 goto out;
340
341 /* This client is about to stall waiting for the GPU. In many cases
342 * this is undesirable and limits the throughput of the system, as
343 * many clients cannot continue processing user input/output whilst
344 * blocked. RPS autotuning may take tens of milliseconds to respond
345 * to the GPU load and thus incurs additional latency for the client.
346 * We can circumvent that by promoting the GPU frequency to maximum
347 * before we wait. This makes the GPU throttle up much more quickly
348 * (good for benchmarks and user experience, e.g. window animations),
349 * but at a cost of spending more power processing the workload
350 * (bad for battery). Not all clients even want their results
351 * immediately and for them we should just let the GPU select its own
352 * frequency to maximise efficiency. To prevent a single client from
353 * forcing the clocks too high for the whole system, we only allow
354 * each client to waitboost once in a busy period.
355 */
356 if (rps) {
357 if (INTEL_GEN(rq->i915) >= 6)
358 gen6_rps_boost(rq->i915, rps, rq->emitted_jiffies);
359 else
360 rps = NULL;
361 }
362
363 timeout = i915_wait_request(rq, flags, timeout);
364
365out:
366 if (flags & I915_WAIT_LOCKED && i915_gem_request_completed(rq))
367 i915_gem_request_retire_upto(rq);
368
369 if (rps && rq->fence.seqno == rq->engine->last_submitted_seqno) {
370 /* The GPU is now idle and this client has stalled.
371 * Since no other client has submitted a request in the
372 * meantime, assume that this client is the only one
373 * supplying work to the GPU but is unable to keep that
374 * work supplied because it is waiting. Since the GPU is
375 * then never kept fully busy, RPS autoclocking will
376 * keep the clocks relatively low, causing further delays.
377 * Compensate by giving the synchronous client credit for
378 * a waitboost next time.
379 */
380 spin_lock(&rq->i915->rps.client_lock);
381 list_del_init(&rps->link);
382 spin_unlock(&rq->i915->rps.client_lock);
383 }
384
385 return timeout;
386}
387
388static long
389i915_gem_object_wait_reservation(struct reservation_object *resv,
390 unsigned int flags,
391 long timeout,
392 struct intel_rps_client *rps)
393{
394 struct dma_fence *excl;
395
396 if (flags & I915_WAIT_ALL) {
397 struct dma_fence **shared;
398 unsigned int count, i;
399 int ret;
400
401 ret = reservation_object_get_fences_rcu(resv,
402 &excl, &count, &shared);
403 if (ret)
404 return ret;
405
406 for (i = 0; i < count; i++) {
407 timeout = i915_gem_object_wait_fence(shared[i],
408 flags, timeout,
409 rps);
410 if (timeout <= 0)
411 break;
412
413 dma_fence_put(shared[i]);
414 }
415
416 for (; i < count; i++)
417 dma_fence_put(shared[i]);
418 kfree(shared);
419 } else {
420 excl = reservation_object_get_excl_rcu(resv);
421 }
422
423 if (excl && timeout > 0)
424 timeout = i915_gem_object_wait_fence(excl, flags, timeout, rps);
425
426 dma_fence_put(excl);
427
428 return timeout;
429}
430
Chris Wilson00e60f22016-08-04 16:32:40 +0100431/**
Chris Wilsone95433c2016-10-28 13:58:27 +0100432 * Waits for rendering to the object to be completed
Chris Wilson00e60f22016-08-04 16:32:40 +0100433 * @obj: i915 gem object
Chris Wilsone95433c2016-10-28 13:58:27 +0100434 * @flags: how to wait (under a lock, for all rendering or just for writes etc)
435 * @timeout: how long to wait
436 * @rps: client (user process) to charge for any waitboosting
Chris Wilson00e60f22016-08-04 16:32:40 +0100437 */
438int
Chris Wilsone95433c2016-10-28 13:58:27 +0100439i915_gem_object_wait(struct drm_i915_gem_object *obj,
440 unsigned int flags,
441 long timeout,
442 struct intel_rps_client *rps)
Chris Wilson00e60f22016-08-04 16:32:40 +0100443{
444 struct reservation_object *resv;
445 struct i915_gem_active *active;
446 unsigned long active_mask;
447 int idx;
448
Chris Wilsone95433c2016-10-28 13:58:27 +0100449 might_sleep();
450#if IS_ENABLED(CONFIG_LOCKDEP)
451 GEM_BUG_ON(debug_locks &&
452 !!lockdep_is_held(&obj->base.dev->struct_mutex) !=
453 !!(flags & I915_WAIT_LOCKED));
454#endif
455 GEM_BUG_ON(timeout < 0);
Chris Wilson00e60f22016-08-04 16:32:40 +0100456
Chris Wilsone95433c2016-10-28 13:58:27 +0100457 if (flags & I915_WAIT_ALL) {
Chris Wilson00e60f22016-08-04 16:32:40 +0100458 active = obj->last_read;
459 active_mask = i915_gem_object_get_active(obj);
460 } else {
461 active_mask = 1;
462 active = &obj->last_write;
463 }
464
465 for_each_active(active_mask, idx) {
Chris Wilsone95433c2016-10-28 13:58:27 +0100466 struct drm_i915_gem_request *request;
Chris Wilson00e60f22016-08-04 16:32:40 +0100467
Chris Wilsone95433c2016-10-28 13:58:27 +0100468 request = i915_gem_active_get_unlocked(&active[idx]);
469 if (request) {
470 timeout = i915_gem_object_wait_fence(&request->fence,
471 flags, timeout,
472 rps);
473 i915_gem_request_put(request);
474 }
475 if (timeout < 0)
476 return timeout;
Chris Wilson00e60f22016-08-04 16:32:40 +0100477 }
478
479 resv = i915_gem_object_get_dmabuf_resv(obj);
Chris Wilsone95433c2016-10-28 13:58:27 +0100480 if (resv)
481 timeout = i915_gem_object_wait_reservation(resv,
482 flags, timeout,
483 rps);
484 return timeout < 0 ? timeout : 0;
Chris Wilson00e60f22016-08-04 16:32:40 +0100485}
486
487static struct intel_rps_client *to_rps_client(struct drm_file *file)
488{
489 struct drm_i915_file_private *fpriv = file->driver_priv;
490
491 return &fpriv->rps;
492}
493
Chris Wilson00731152014-05-21 12:42:56 +0100494int
495i915_gem_object_attach_phys(struct drm_i915_gem_object *obj,
496 int align)
497{
498 drm_dma_handle_t *phys;
Chris Wilson6a2c4232014-11-04 04:51:40 -0800499 int ret;
Chris Wilson00731152014-05-21 12:42:56 +0100500
501 if (obj->phys_handle) {
502 if ((unsigned long)obj->phys_handle->vaddr & (align -1))
503 return -EBUSY;
504
505 return 0;
506 }
507
508 if (obj->madv != I915_MADV_WILLNEED)
509 return -EFAULT;
510
511 if (obj->base.filp == NULL)
512 return -EINVAL;
513
Chris Wilson4717ca92016-08-04 07:52:28 +0100514 ret = i915_gem_object_unbind(obj);
515 if (ret)
516 return ret;
517
518 ret = i915_gem_object_put_pages(obj);
Chris Wilson6a2c4232014-11-04 04:51:40 -0800519 if (ret)
520 return ret;
521
Chris Wilson00731152014-05-21 12:42:56 +0100522 /* create a new object */
523 phys = drm_pci_alloc(obj->base.dev, obj->base.size, align);
524 if (!phys)
525 return -ENOMEM;
526
Chris Wilson00731152014-05-21 12:42:56 +0100527 obj->phys_handle = phys;
Chris Wilson6a2c4232014-11-04 04:51:40 -0800528 obj->ops = &i915_gem_phys_ops;
529
530 return i915_gem_object_get_pages(obj);
Chris Wilson00731152014-05-21 12:42:56 +0100531}
532
533static int
534i915_gem_phys_pwrite(struct drm_i915_gem_object *obj,
535 struct drm_i915_gem_pwrite *args,
536 struct drm_file *file_priv)
537{
538 struct drm_device *dev = obj->base.dev;
539 void *vaddr = obj->phys_handle->vaddr + args->offset;
Gustavo Padovan3ed605b2016-04-26 12:32:27 -0300540 char __user *user_data = u64_to_user_ptr(args->data_ptr);
Chris Wilsone95433c2016-10-28 13:58:27 +0100541 int ret;
Chris Wilson6a2c4232014-11-04 04:51:40 -0800542
543 /* We manually control the domain here and pretend that it
544 * remains coherent i.e. in the GTT domain, like shmem_pwrite.
545 */
Chris Wilsone95433c2016-10-28 13:58:27 +0100546 lockdep_assert_held(&obj->base.dev->struct_mutex);
547 ret = i915_gem_object_wait(obj,
548 I915_WAIT_INTERRUPTIBLE |
549 I915_WAIT_LOCKED |
550 I915_WAIT_ALL,
551 MAX_SCHEDULE_TIMEOUT,
552 to_rps_client(file_priv));
Chris Wilson6a2c4232014-11-04 04:51:40 -0800553 if (ret)
554 return ret;
Chris Wilson00731152014-05-21 12:42:56 +0100555
Rodrigo Vivi77a0d1c2015-06-18 11:43:24 -0700556 intel_fb_obj_invalidate(obj, ORIGIN_CPU);
Chris Wilson00731152014-05-21 12:42:56 +0100557 if (__copy_from_user_inatomic_nocache(vaddr, user_data, args->size)) {
558 unsigned long unwritten;
559
560 /* The physical object once assigned is fixed for the lifetime
561 * of the obj, so we can safely drop the lock and continue
562 * to access vaddr.
563 */
564 mutex_unlock(&dev->struct_mutex);
565 unwritten = copy_from_user(vaddr, user_data, args->size);
566 mutex_lock(&dev->struct_mutex);
Paulo Zanoni063e4e62015-02-13 17:23:45 -0200567 if (unwritten) {
568 ret = -EFAULT;
569 goto out;
570 }
Chris Wilson00731152014-05-21 12:42:56 +0100571 }
572
Chris Wilson6a2c4232014-11-04 04:51:40 -0800573 drm_clflush_virt_range(vaddr, args->size);
Chris Wilsonc0336662016-05-06 15:40:21 +0100574 i915_gem_chipset_flush(to_i915(dev));
Paulo Zanoni063e4e62015-02-13 17:23:45 -0200575
576out:
Rodrigo Vivide152b62015-07-07 16:28:51 -0700577 intel_fb_obj_flush(obj, false, ORIGIN_CPU);
Paulo Zanoni063e4e62015-02-13 17:23:45 -0200578 return ret;
Chris Wilson00731152014-05-21 12:42:56 +0100579}
580
Chris Wilson42dcedd2012-11-15 11:32:30 +0000581void *i915_gem_object_alloc(struct drm_device *dev)
582{
Chris Wilsonfac5e232016-07-04 11:34:36 +0100583 struct drm_i915_private *dev_priv = to_i915(dev);
Chris Wilsonefab6d82015-04-07 16:20:57 +0100584 return kmem_cache_zalloc(dev_priv->objects, GFP_KERNEL);
Chris Wilson42dcedd2012-11-15 11:32:30 +0000585}
586
587void i915_gem_object_free(struct drm_i915_gem_object *obj)
588{
Chris Wilsonfac5e232016-07-04 11:34:36 +0100589 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
Chris Wilsonefab6d82015-04-07 16:20:57 +0100590 kmem_cache_free(dev_priv->objects, obj);
Chris Wilson42dcedd2012-11-15 11:32:30 +0000591}
592
Dave Airlieff72145b2011-02-07 12:16:14 +1000593static int
594i915_gem_create(struct drm_file *file,
595 struct drm_device *dev,
596 uint64_t size,
597 uint32_t *handle_p)
Eric Anholt673a3942008-07-30 12:06:12 -0700598{
Chris Wilson05394f32010-11-08 19:18:58 +0000599 struct drm_i915_gem_object *obj;
Pekka Paalanena1a2d1d2009-08-23 12:40:55 +0300600 int ret;
601 u32 handle;
Eric Anholt673a3942008-07-30 12:06:12 -0700602
Dave Airlieff72145b2011-02-07 12:16:14 +1000603 size = roundup(size, PAGE_SIZE);
Chris Wilson8ffc0242011-09-14 14:14:28 +0200604 if (size == 0)
605 return -EINVAL;
Eric Anholt673a3942008-07-30 12:06:12 -0700606
607 /* Allocate the new object */
Dave Gordond37cd8a2016-04-22 19:14:32 +0100608 obj = i915_gem_object_create(dev, size);
Chris Wilsonfe3db792016-04-25 13:32:13 +0100609 if (IS_ERR(obj))
610 return PTR_ERR(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700611
Chris Wilson05394f32010-11-08 19:18:58 +0000612 ret = drm_gem_handle_create(file, &obj->base, &handle);
Chris Wilson202f2fe2010-10-14 13:20:40 +0100613 /* drop reference from allocate - handle holds it now */
Chris Wilson34911fd2016-07-20 13:31:54 +0100614 i915_gem_object_put_unlocked(obj);
Daniel Vetterd861e332013-07-24 23:25:03 +0200615 if (ret)
616 return ret;
Chris Wilson202f2fe2010-10-14 13:20:40 +0100617
Dave Airlieff72145b2011-02-07 12:16:14 +1000618 *handle_p = handle;
Eric Anholt673a3942008-07-30 12:06:12 -0700619 return 0;
620}
621
Dave Airlieff72145b2011-02-07 12:16:14 +1000622int
623i915_gem_dumb_create(struct drm_file *file,
624 struct drm_device *dev,
625 struct drm_mode_create_dumb *args)
626{
627 /* have to work out size/pitch and return them */
Paulo Zanonide45eaf2013-10-18 18:48:24 -0300628 args->pitch = ALIGN(args->width * DIV_ROUND_UP(args->bpp, 8), 64);
Dave Airlieff72145b2011-02-07 12:16:14 +1000629 args->size = args->pitch * args->height;
630 return i915_gem_create(file, dev,
Dave Airlieda6b51d2014-12-24 13:11:17 +1000631 args->size, &args->handle);
Dave Airlieff72145b2011-02-07 12:16:14 +1000632}
633
Dave Airlieff72145b2011-02-07 12:16:14 +1000634/**
635 * Creates a new mm object and returns a handle to it.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +0100636 * @dev: drm device pointer
637 * @data: ioctl data blob
638 * @file: drm file pointer
Dave Airlieff72145b2011-02-07 12:16:14 +1000639 */
640int
641i915_gem_create_ioctl(struct drm_device *dev, void *data,
642 struct drm_file *file)
643{
644 struct drm_i915_gem_create *args = data;
Daniel Vetter63ed2cb2012-04-23 16:50:50 +0200645
Dave Airlieff72145b2011-02-07 12:16:14 +1000646 return i915_gem_create(file, dev,
Dave Airlieda6b51d2014-12-24 13:11:17 +1000647 args->size, &args->handle);
Dave Airlieff72145b2011-02-07 12:16:14 +1000648}
649
Daniel Vetter8c599672011-12-14 13:57:31 +0100650static inline int
Daniel Vetter8461d222011-12-14 13:57:32 +0100651__copy_to_user_swizzled(char __user *cpu_vaddr,
652 const char *gpu_vaddr, int gpu_offset,
653 int length)
654{
655 int ret, cpu_offset = 0;
656
657 while (length > 0) {
658 int cacheline_end = ALIGN(gpu_offset + 1, 64);
659 int this_length = min(cacheline_end - gpu_offset, length);
660 int swizzled_gpu_offset = gpu_offset ^ 64;
661
662 ret = __copy_to_user(cpu_vaddr + cpu_offset,
663 gpu_vaddr + swizzled_gpu_offset,
664 this_length);
665 if (ret)
666 return ret + length;
667
668 cpu_offset += this_length;
669 gpu_offset += this_length;
670 length -= this_length;
671 }
672
673 return 0;
674}
675
676static inline int
Ben Widawsky4f0c7cf2012-04-16 14:07:47 -0700677__copy_from_user_swizzled(char *gpu_vaddr, int gpu_offset,
678 const char __user *cpu_vaddr,
Daniel Vetter8c599672011-12-14 13:57:31 +0100679 int length)
680{
681 int ret, cpu_offset = 0;
682
683 while (length > 0) {
684 int cacheline_end = ALIGN(gpu_offset + 1, 64);
685 int this_length = min(cacheline_end - gpu_offset, length);
686 int swizzled_gpu_offset = gpu_offset ^ 64;
687
688 ret = __copy_from_user(gpu_vaddr + swizzled_gpu_offset,
689 cpu_vaddr + cpu_offset,
690 this_length);
691 if (ret)
692 return ret + length;
693
694 cpu_offset += this_length;
695 gpu_offset += this_length;
696 length -= this_length;
697 }
698
699 return 0;
700}
701
Brad Volkin4c914c02014-02-18 10:15:45 -0800702/*
703 * Pins the specified object's pages and synchronizes the object with
704 * GPU accesses. Sets needs_clflush to non-zero if the caller should
705 * flush the object from the CPU cache.
706 */
707int i915_gem_obj_prepare_shmem_read(struct drm_i915_gem_object *obj,
Chris Wilson43394c72016-08-18 17:16:47 +0100708 unsigned int *needs_clflush)
Brad Volkin4c914c02014-02-18 10:15:45 -0800709{
710 int ret;
711
Chris Wilsone95433c2016-10-28 13:58:27 +0100712 lockdep_assert_held(&obj->base.dev->struct_mutex);
Brad Volkin4c914c02014-02-18 10:15:45 -0800713
Chris Wilsone95433c2016-10-28 13:58:27 +0100714 *needs_clflush = 0;
Chris Wilson43394c72016-08-18 17:16:47 +0100715 if (!i915_gem_object_has_struct_page(obj))
716 return -ENODEV;
Brad Volkin4c914c02014-02-18 10:15:45 -0800717
Chris Wilsone95433c2016-10-28 13:58:27 +0100718 ret = i915_gem_object_wait(obj,
719 I915_WAIT_INTERRUPTIBLE |
720 I915_WAIT_LOCKED,
721 MAX_SCHEDULE_TIMEOUT,
722 NULL);
Chris Wilsonc13d87e2016-07-20 09:21:15 +0100723 if (ret)
724 return ret;
725
Chris Wilson97649512016-08-18 17:16:50 +0100726 ret = i915_gem_object_get_pages(obj);
727 if (ret)
728 return ret;
729
730 i915_gem_object_pin_pages(obj);
731
Chris Wilsona314d5c2016-08-18 17:16:48 +0100732 i915_gem_object_flush_gtt_write_domain(obj);
733
Chris Wilson43394c72016-08-18 17:16:47 +0100734 /* If we're not in the cpu read domain, set ourself into the gtt
735 * read domain and manually flush cachelines (if required). This
736 * optimizes for the case when the gpu will dirty the data
737 * anyway again before the next pread happens.
738 */
739 if (!(obj->base.read_domains & I915_GEM_DOMAIN_CPU))
Brad Volkin4c914c02014-02-18 10:15:45 -0800740 *needs_clflush = !cpu_cache_is_coherent(obj->base.dev,
741 obj->cache_level);
Brad Volkin4c914c02014-02-18 10:15:45 -0800742
Chris Wilson43394c72016-08-18 17:16:47 +0100743 if (*needs_clflush && !static_cpu_has(X86_FEATURE_CLFLUSH)) {
744 ret = i915_gem_object_set_to_cpu_domain(obj, false);
Chris Wilson97649512016-08-18 17:16:50 +0100745 if (ret)
746 goto err_unpin;
747
Chris Wilson43394c72016-08-18 17:16:47 +0100748 *needs_clflush = 0;
749 }
750
Chris Wilson97649512016-08-18 17:16:50 +0100751 /* return with the pages pinned */
Chris Wilson43394c72016-08-18 17:16:47 +0100752 return 0;
Chris Wilson97649512016-08-18 17:16:50 +0100753
754err_unpin:
755 i915_gem_object_unpin_pages(obj);
756 return ret;
Chris Wilson43394c72016-08-18 17:16:47 +0100757}
758
759int i915_gem_obj_prepare_shmem_write(struct drm_i915_gem_object *obj,
760 unsigned int *needs_clflush)
761{
762 int ret;
763
Chris Wilsone95433c2016-10-28 13:58:27 +0100764 lockdep_assert_held(&obj->base.dev->struct_mutex);
765
Chris Wilson43394c72016-08-18 17:16:47 +0100766 *needs_clflush = 0;
767 if (!i915_gem_object_has_struct_page(obj))
768 return -ENODEV;
769
Chris Wilsone95433c2016-10-28 13:58:27 +0100770 ret = i915_gem_object_wait(obj,
771 I915_WAIT_INTERRUPTIBLE |
772 I915_WAIT_LOCKED |
773 I915_WAIT_ALL,
774 MAX_SCHEDULE_TIMEOUT,
775 NULL);
Chris Wilson43394c72016-08-18 17:16:47 +0100776 if (ret)
777 return ret;
778
Chris Wilson97649512016-08-18 17:16:50 +0100779 ret = i915_gem_object_get_pages(obj);
780 if (ret)
781 return ret;
782
783 i915_gem_object_pin_pages(obj);
784
Chris Wilsona314d5c2016-08-18 17:16:48 +0100785 i915_gem_object_flush_gtt_write_domain(obj);
786
Chris Wilson43394c72016-08-18 17:16:47 +0100787 /* If we're not in the cpu write domain, set ourself into the
788 * gtt write domain and manually flush cachelines (as required).
789 * This optimizes for the case when the gpu will use the data
790 * right away and we therefore have to clflush anyway.
791 */
792 if (obj->base.write_domain != I915_GEM_DOMAIN_CPU)
793 *needs_clflush |= cpu_write_needs_clflush(obj) << 1;
794
795 /* Same trick applies to invalidate partially written cachelines read
796 * before writing.
797 */
798 if (!(obj->base.read_domains & I915_GEM_DOMAIN_CPU))
799 *needs_clflush |= !cpu_cache_is_coherent(obj->base.dev,
800 obj->cache_level);
801
Chris Wilson43394c72016-08-18 17:16:47 +0100802 if (*needs_clflush && !static_cpu_has(X86_FEATURE_CLFLUSH)) {
803 ret = i915_gem_object_set_to_cpu_domain(obj, true);
Chris Wilson97649512016-08-18 17:16:50 +0100804 if (ret)
805 goto err_unpin;
806
Chris Wilson43394c72016-08-18 17:16:47 +0100807 *needs_clflush = 0;
808 }
809
810 if ((*needs_clflush & CLFLUSH_AFTER) == 0)
811 obj->cache_dirty = true;
812
813 intel_fb_obj_invalidate(obj, ORIGIN_CPU);
814 obj->dirty = 1;
Chris Wilson97649512016-08-18 17:16:50 +0100815 /* return with the pages pinned */
Chris Wilson43394c72016-08-18 17:16:47 +0100816 return 0;
Chris Wilson97649512016-08-18 17:16:50 +0100817
818err_unpin:
819 i915_gem_object_unpin_pages(obj);
820 return ret;
Brad Volkin4c914c02014-02-18 10:15:45 -0800821}
822
Daniel Vetterd174bd62012-03-25 19:47:40 +0200823/* Per-page copy function for the shmem pread fastpath.
824 * Flushes invalid cachelines before reading the target if
825 * needs_clflush is set. */
Eric Anholteb014592009-03-10 11:44:52 -0700826static int
Daniel Vetterd174bd62012-03-25 19:47:40 +0200827shmem_pread_fast(struct page *page, int shmem_page_offset, int page_length,
828 char __user *user_data,
829 bool page_do_bit17_swizzling, bool needs_clflush)
830{
831 char *vaddr;
832 int ret;
833
Daniel Vettere7e58eb2012-03-25 19:47:43 +0200834 if (unlikely(page_do_bit17_swizzling))
Daniel Vetterd174bd62012-03-25 19:47:40 +0200835 return -EINVAL;
836
837 vaddr = kmap_atomic(page);
838 if (needs_clflush)
839 drm_clflush_virt_range(vaddr + shmem_page_offset,
840 page_length);
841 ret = __copy_to_user_inatomic(user_data,
842 vaddr + shmem_page_offset,
843 page_length);
844 kunmap_atomic(vaddr);
845
Chris Wilsonf60d7f02012-09-04 21:02:56 +0100846 return ret ? -EFAULT : 0;
Daniel Vetterd174bd62012-03-25 19:47:40 +0200847}
848
Daniel Vetter23c18c72012-03-25 19:47:42 +0200849static void
850shmem_clflush_swizzled_range(char *addr, unsigned long length,
851 bool swizzled)
852{
Daniel Vettere7e58eb2012-03-25 19:47:43 +0200853 if (unlikely(swizzled)) {
Daniel Vetter23c18c72012-03-25 19:47:42 +0200854 unsigned long start = (unsigned long) addr;
855 unsigned long end = (unsigned long) addr + length;
856
857 /* For swizzling simply ensure that we always flush both
858 * channels. Lame, but simple and it works. Swizzled
859 * pwrite/pread is far from a hotpath - current userspace
860 * doesn't use it at all. */
861 start = round_down(start, 128);
862 end = round_up(end, 128);
863
864 drm_clflush_virt_range((void *)start, end - start);
865 } else {
866 drm_clflush_virt_range(addr, length);
867 }
868
869}
870
Daniel Vetterd174bd62012-03-25 19:47:40 +0200871/* Only difference to the fast-path function is that this can handle bit17
872 * and uses non-atomic copy and kmap functions. */
873static int
874shmem_pread_slow(struct page *page, int shmem_page_offset, int page_length,
875 char __user *user_data,
876 bool page_do_bit17_swizzling, bool needs_clflush)
877{
878 char *vaddr;
879 int ret;
880
881 vaddr = kmap(page);
882 if (needs_clflush)
Daniel Vetter23c18c72012-03-25 19:47:42 +0200883 shmem_clflush_swizzled_range(vaddr + shmem_page_offset,
884 page_length,
885 page_do_bit17_swizzling);
Daniel Vetterd174bd62012-03-25 19:47:40 +0200886
887 if (page_do_bit17_swizzling)
888 ret = __copy_to_user_swizzled(user_data,
889 vaddr, shmem_page_offset,
890 page_length);
891 else
892 ret = __copy_to_user(user_data,
893 vaddr + shmem_page_offset,
894 page_length);
895 kunmap(page);
896
Chris Wilsonf60d7f02012-09-04 21:02:56 +0100897 return ret ? - EFAULT : 0;
Daniel Vetterd174bd62012-03-25 19:47:40 +0200898}
899
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +0530900static inline unsigned long
901slow_user_access(struct io_mapping *mapping,
902 uint64_t page_base, int page_offset,
903 char __user *user_data,
904 unsigned long length, bool pwrite)
905{
906 void __iomem *ioaddr;
907 void *vaddr;
908 uint64_t unwritten;
909
910 ioaddr = io_mapping_map_wc(mapping, page_base, PAGE_SIZE);
911 /* We can use the cpu mem copy function because this is X86. */
912 vaddr = (void __force *)ioaddr + page_offset;
913 if (pwrite)
914 unwritten = __copy_from_user(vaddr, user_data, length);
915 else
916 unwritten = __copy_to_user(user_data, vaddr, length);
917
918 io_mapping_unmap(ioaddr);
919 return unwritten;
920}
921
922static int
923i915_gem_gtt_pread(struct drm_device *dev,
924 struct drm_i915_gem_object *obj, uint64_t size,
925 uint64_t data_offset, uint64_t data_ptr)
926{
Chris Wilsonfac5e232016-07-04 11:34:36 +0100927 struct drm_i915_private *dev_priv = to_i915(dev);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +0530928 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Chris Wilson058d88c2016-08-15 10:49:06 +0100929 struct i915_vma *vma;
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +0530930 struct drm_mm_node node;
931 char __user *user_data;
932 uint64_t remain;
933 uint64_t offset;
934 int ret;
935
Chris Wilson9c870d02016-10-24 13:42:15 +0100936 intel_runtime_pm_get(to_i915(dev));
Chris Wilson058d88c2016-08-15 10:49:06 +0100937 vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0, PIN_MAPPABLE);
Chris Wilson18034582016-08-18 17:16:45 +0100938 if (!IS_ERR(vma)) {
939 node.start = i915_ggtt_offset(vma);
940 node.allocated = false;
Chris Wilson49ef5292016-08-18 17:17:00 +0100941 ret = i915_vma_put_fence(vma);
Chris Wilson18034582016-08-18 17:16:45 +0100942 if (ret) {
943 i915_vma_unpin(vma);
944 vma = ERR_PTR(ret);
945 }
946 }
Chris Wilson058d88c2016-08-15 10:49:06 +0100947 if (IS_ERR(vma)) {
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +0530948 ret = insert_mappable_node(dev_priv, &node, PAGE_SIZE);
949 if (ret)
950 goto out;
951
952 ret = i915_gem_object_get_pages(obj);
953 if (ret) {
954 remove_mappable_node(&node);
955 goto out;
956 }
957
958 i915_gem_object_pin_pages(obj);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +0530959 }
960
961 ret = i915_gem_object_set_to_gtt_domain(obj, false);
962 if (ret)
963 goto out_unpin;
964
965 user_data = u64_to_user_ptr(data_ptr);
966 remain = size;
967 offset = data_offset;
968
969 mutex_unlock(&dev->struct_mutex);
970 if (likely(!i915.prefault_disable)) {
Al Viro4bce9f62016-09-17 18:02:44 -0400971 ret = fault_in_pages_writeable(user_data, remain);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +0530972 if (ret) {
973 mutex_lock(&dev->struct_mutex);
974 goto out_unpin;
975 }
976 }
977
978 while (remain > 0) {
979 /* Operation in this page
980 *
981 * page_base = page offset within aperture
982 * page_offset = offset within page
983 * page_length = bytes to copy for this page
984 */
985 u32 page_base = node.start;
986 unsigned page_offset = offset_in_page(offset);
987 unsigned page_length = PAGE_SIZE - page_offset;
988 page_length = remain < page_length ? remain : page_length;
989 if (node.allocated) {
990 wmb();
991 ggtt->base.insert_page(&ggtt->base,
992 i915_gem_object_get_dma_address(obj, offset >> PAGE_SHIFT),
993 node.start,
994 I915_CACHE_NONE, 0);
995 wmb();
996 } else {
997 page_base += offset & PAGE_MASK;
998 }
999 /* This is a slow read/write as it tries to read from
1000 * and write to user memory which may result into page
1001 * faults, and so we cannot perform this under struct_mutex.
1002 */
Chris Wilsonf7bbe782016-08-19 16:54:27 +01001003 if (slow_user_access(&ggtt->mappable, page_base,
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301004 page_offset, user_data,
1005 page_length, false)) {
1006 ret = -EFAULT;
1007 break;
1008 }
1009
1010 remain -= page_length;
1011 user_data += page_length;
1012 offset += page_length;
1013 }
1014
1015 mutex_lock(&dev->struct_mutex);
1016 if (ret == 0 && (obj->base.read_domains & I915_GEM_DOMAIN_GTT) == 0) {
1017 /* The user has modified the object whilst we tried
1018 * reading from it, and we now have no idea what domain
1019 * the pages should be in. As we have just been touching
1020 * them directly, flush everything back to the GTT
1021 * domain.
1022 */
1023 ret = i915_gem_object_set_to_gtt_domain(obj, false);
1024 }
1025
1026out_unpin:
1027 if (node.allocated) {
1028 wmb();
1029 ggtt->base.clear_range(&ggtt->base,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02001030 node.start, node.size);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301031 i915_gem_object_unpin_pages(obj);
1032 remove_mappable_node(&node);
1033 } else {
Chris Wilson058d88c2016-08-15 10:49:06 +01001034 i915_vma_unpin(vma);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301035 }
1036out:
Chris Wilson9c870d02016-10-24 13:42:15 +01001037 intel_runtime_pm_put(to_i915(dev));
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301038 return ret;
1039}
1040
Eric Anholteb014592009-03-10 11:44:52 -07001041static int
Daniel Vetterdbf7bff2012-03-25 19:47:29 +02001042i915_gem_shmem_pread(struct drm_device *dev,
1043 struct drm_i915_gem_object *obj,
1044 struct drm_i915_gem_pread *args,
1045 struct drm_file *file)
Eric Anholteb014592009-03-10 11:44:52 -07001046{
Daniel Vetter8461d222011-12-14 13:57:32 +01001047 char __user *user_data;
Eric Anholteb014592009-03-10 11:44:52 -07001048 ssize_t remain;
Daniel Vetter8461d222011-12-14 13:57:32 +01001049 loff_t offset;
Ben Widawskyeb2c0c82012-02-15 14:42:43 +01001050 int shmem_page_offset, page_length, ret = 0;
Daniel Vetter8461d222011-12-14 13:57:32 +01001051 int obj_do_bit17_swizzling, page_do_bit17_swizzling;
Daniel Vetter96d79b52012-03-25 19:47:36 +02001052 int prefaulted = 0;
Daniel Vetter84897312012-03-25 19:47:31 +02001053 int needs_clflush = 0;
Imre Deak67d5a502013-02-18 19:28:02 +02001054 struct sg_page_iter sg_iter;
Eric Anholteb014592009-03-10 11:44:52 -07001055
Brad Volkin4c914c02014-02-18 10:15:45 -08001056 ret = i915_gem_obj_prepare_shmem_read(obj, &needs_clflush);
Chris Wilsonf60d7f02012-09-04 21:02:56 +01001057 if (ret)
1058 return ret;
1059
Chris Wilson43394c72016-08-18 17:16:47 +01001060 obj_do_bit17_swizzling = i915_gem_object_needs_bit17_swizzle(obj);
1061 user_data = u64_to_user_ptr(args->data_ptr);
Eric Anholteb014592009-03-10 11:44:52 -07001062 offset = args->offset;
Chris Wilson43394c72016-08-18 17:16:47 +01001063 remain = args->size;
Daniel Vetter8461d222011-12-14 13:57:32 +01001064
Imre Deak67d5a502013-02-18 19:28:02 +02001065 for_each_sg_page(obj->pages->sgl, &sg_iter, obj->pages->nents,
1066 offset >> PAGE_SHIFT) {
Imre Deak2db76d72013-03-26 15:14:18 +02001067 struct page *page = sg_page_iter_page(&sg_iter);
Chris Wilson9da3da62012-06-01 15:20:22 +01001068
1069 if (remain <= 0)
1070 break;
1071
Eric Anholteb014592009-03-10 11:44:52 -07001072 /* Operation in this page
1073 *
Eric Anholteb014592009-03-10 11:44:52 -07001074 * shmem_page_offset = offset within page in shmem file
Eric Anholteb014592009-03-10 11:44:52 -07001075 * page_length = bytes to copy for this page
1076 */
Chris Wilsonc8cbbb82011-05-12 22:17:11 +01001077 shmem_page_offset = offset_in_page(offset);
Eric Anholteb014592009-03-10 11:44:52 -07001078 page_length = remain;
1079 if ((shmem_page_offset + page_length) > PAGE_SIZE)
1080 page_length = PAGE_SIZE - shmem_page_offset;
Eric Anholteb014592009-03-10 11:44:52 -07001081
Daniel Vetter8461d222011-12-14 13:57:32 +01001082 page_do_bit17_swizzling = obj_do_bit17_swizzling &&
1083 (page_to_phys(page) & (1 << 17)) != 0;
1084
Daniel Vetterd174bd62012-03-25 19:47:40 +02001085 ret = shmem_pread_fast(page, shmem_page_offset, page_length,
1086 user_data, page_do_bit17_swizzling,
1087 needs_clflush);
1088 if (ret == 0)
1089 goto next_page;
Eric Anholteb014592009-03-10 11:44:52 -07001090
Daniel Vetterdbf7bff2012-03-25 19:47:29 +02001091 mutex_unlock(&dev->struct_mutex);
1092
Jani Nikulad330a952014-01-21 11:24:25 +02001093 if (likely(!i915.prefault_disable) && !prefaulted) {
Al Viro4bce9f62016-09-17 18:02:44 -04001094 ret = fault_in_pages_writeable(user_data, remain);
Daniel Vetter96d79b52012-03-25 19:47:36 +02001095 /* Userspace is tricking us, but we've already clobbered
1096 * its pages with the prefault and promised to write the
1097 * data up to the first fault. Hence ignore any errors
1098 * and just continue. */
1099 (void)ret;
1100 prefaulted = 1;
1101 }
1102
Daniel Vetterd174bd62012-03-25 19:47:40 +02001103 ret = shmem_pread_slow(page, shmem_page_offset, page_length,
1104 user_data, page_do_bit17_swizzling,
1105 needs_clflush);
Eric Anholteb014592009-03-10 11:44:52 -07001106
Daniel Vetterdbf7bff2012-03-25 19:47:29 +02001107 mutex_lock(&dev->struct_mutex);
Chris Wilsonf60d7f02012-09-04 21:02:56 +01001108
Chris Wilsonf60d7f02012-09-04 21:02:56 +01001109 if (ret)
Daniel Vetter8461d222011-12-14 13:57:32 +01001110 goto out;
Daniel Vetter8461d222011-12-14 13:57:32 +01001111
Chris Wilson17793c92014-03-07 08:30:36 +00001112next_page:
Eric Anholteb014592009-03-10 11:44:52 -07001113 remain -= page_length;
Daniel Vetter8461d222011-12-14 13:57:32 +01001114 user_data += page_length;
Eric Anholteb014592009-03-10 11:44:52 -07001115 offset += page_length;
1116 }
1117
Chris Wilson4f27b752010-10-14 15:26:45 +01001118out:
Chris Wilson43394c72016-08-18 17:16:47 +01001119 i915_gem_obj_finish_shmem_access(obj);
Chris Wilsonf60d7f02012-09-04 21:02:56 +01001120
Eric Anholteb014592009-03-10 11:44:52 -07001121 return ret;
1122}
1123
Eric Anholt673a3942008-07-30 12:06:12 -07001124/**
1125 * Reads data from the object referenced by handle.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01001126 * @dev: drm device pointer
1127 * @data: ioctl data blob
1128 * @file: drm file pointer
Eric Anholt673a3942008-07-30 12:06:12 -07001129 *
1130 * On error, the contents of *data are undefined.
1131 */
1132int
1133i915_gem_pread_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00001134 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001135{
1136 struct drm_i915_gem_pread *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00001137 struct drm_i915_gem_object *obj;
Chris Wilson35b62a82010-09-26 20:23:38 +01001138 int ret = 0;
Eric Anholt673a3942008-07-30 12:06:12 -07001139
Chris Wilson51311d02010-11-17 09:10:42 +00001140 if (args->size == 0)
1141 return 0;
1142
1143 if (!access_ok(VERIFY_WRITE,
Gustavo Padovan3ed605b2016-04-26 12:32:27 -03001144 u64_to_user_ptr(args->data_ptr),
Chris Wilson51311d02010-11-17 09:10:42 +00001145 args->size))
1146 return -EFAULT;
1147
Chris Wilson03ac0642016-07-20 13:31:51 +01001148 obj = i915_gem_object_lookup(file, args->handle);
Chris Wilson258a5ed2016-08-05 10:14:16 +01001149 if (!obj)
1150 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07001151
Chris Wilson7dcd2492010-09-26 20:21:44 +01001152 /* Bounds check source. */
Chris Wilson05394f32010-11-08 19:18:58 +00001153 if (args->offset > obj->base.size ||
1154 args->size > obj->base.size - args->offset) {
Chris Wilsonce9d4192010-09-26 20:50:05 +01001155 ret = -EINVAL;
Chris Wilson258a5ed2016-08-05 10:14:16 +01001156 goto err;
Chris Wilsonce9d4192010-09-26 20:50:05 +01001157 }
1158
Chris Wilsondb53a302011-02-03 11:57:46 +00001159 trace_i915_gem_object_pread(obj, args->offset, args->size);
1160
Chris Wilsone95433c2016-10-28 13:58:27 +01001161 ret = i915_gem_object_wait(obj,
1162 I915_WAIT_INTERRUPTIBLE,
1163 MAX_SCHEDULE_TIMEOUT,
1164 to_rps_client(file));
Chris Wilson258a5ed2016-08-05 10:14:16 +01001165 if (ret)
1166 goto err;
1167
1168 ret = i915_mutex_lock_interruptible(dev);
1169 if (ret)
1170 goto err;
1171
Daniel Vetterdbf7bff2012-03-25 19:47:29 +02001172 ret = i915_gem_shmem_pread(dev, obj, args, file);
Eric Anholt673a3942008-07-30 12:06:12 -07001173
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301174 /* pread for non shmem backed objects */
Chris Wilson9c870d02016-10-24 13:42:15 +01001175 if (ret == -EFAULT || ret == -ENODEV)
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301176 ret = i915_gem_gtt_pread(dev, obj, args->size,
1177 args->offset, args->data_ptr);
1178
Chris Wilsonf8c417c2016-07-20 13:31:53 +01001179 i915_gem_object_put(obj);
Chris Wilson4f27b752010-10-14 15:26:45 +01001180 mutex_unlock(&dev->struct_mutex);
Chris Wilson258a5ed2016-08-05 10:14:16 +01001181
1182 return ret;
1183
1184err:
1185 i915_gem_object_put_unlocked(obj);
Eric Anholteb014592009-03-10 11:44:52 -07001186 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07001187}
1188
Keith Packard0839ccb2008-10-30 19:38:48 -07001189/* This is the fast write path which cannot handle
1190 * page faults in the source data
Linus Torvalds9b7530cc2008-10-20 14:16:43 -07001191 */
Linus Torvalds9b7530cc2008-10-20 14:16:43 -07001192
Keith Packard0839ccb2008-10-30 19:38:48 -07001193static inline int
1194fast_user_write(struct io_mapping *mapping,
1195 loff_t page_base, int page_offset,
1196 char __user *user_data,
1197 int length)
1198{
Ben Widawsky4f0c7cf2012-04-16 14:07:47 -07001199 void __iomem *vaddr_atomic;
1200 void *vaddr;
Keith Packard0839ccb2008-10-30 19:38:48 -07001201 unsigned long unwritten;
1202
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -07001203 vaddr_atomic = io_mapping_map_atomic_wc(mapping, page_base);
Ben Widawsky4f0c7cf2012-04-16 14:07:47 -07001204 /* We can use the cpu mem copy function because this is X86. */
1205 vaddr = (void __force*)vaddr_atomic + page_offset;
1206 unwritten = __copy_from_user_inatomic_nocache(vaddr,
Keith Packard0839ccb2008-10-30 19:38:48 -07001207 user_data, length);
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -07001208 io_mapping_unmap_atomic(vaddr_atomic);
Chris Wilsonfbd5a262010-10-14 15:03:58 +01001209 return unwritten;
Keith Packard0839ccb2008-10-30 19:38:48 -07001210}
1211
Eric Anholt3de09aa2009-03-09 09:42:23 -07001212/**
1213 * This is the fast pwrite path, where we copy the data directly from the
1214 * user into the GTT, uncached.
Daniel Vetter62f90b32016-07-15 21:48:07 +02001215 * @i915: i915 device private data
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01001216 * @obj: i915 gem object
1217 * @args: pwrite arguments structure
1218 * @file: drm file pointer
Eric Anholt3de09aa2009-03-09 09:42:23 -07001219 */
Eric Anholt673a3942008-07-30 12:06:12 -07001220static int
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301221i915_gem_gtt_pwrite_fast(struct drm_i915_private *i915,
Chris Wilson05394f32010-11-08 19:18:58 +00001222 struct drm_i915_gem_object *obj,
Eric Anholt3de09aa2009-03-09 09:42:23 -07001223 struct drm_i915_gem_pwrite *args,
Chris Wilson05394f32010-11-08 19:18:58 +00001224 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001225{
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301226 struct i915_ggtt *ggtt = &i915->ggtt;
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301227 struct drm_device *dev = obj->base.dev;
Chris Wilson058d88c2016-08-15 10:49:06 +01001228 struct i915_vma *vma;
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301229 struct drm_mm_node node;
1230 uint64_t remain, offset;
Eric Anholt673a3942008-07-30 12:06:12 -07001231 char __user *user_data;
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301232 int ret;
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301233 bool hit_slow_path = false;
1234
Chris Wilson3e510a82016-08-05 10:14:23 +01001235 if (i915_gem_object_is_tiled(obj))
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301236 return -EFAULT;
Daniel Vetter935aaa62012-03-25 19:47:35 +02001237
Chris Wilson9c870d02016-10-24 13:42:15 +01001238 intel_runtime_pm_get(i915);
Chris Wilson058d88c2016-08-15 10:49:06 +01001239 vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0,
Chris Wilsonde895082016-08-04 16:32:34 +01001240 PIN_MAPPABLE | PIN_NONBLOCK);
Chris Wilson18034582016-08-18 17:16:45 +01001241 if (!IS_ERR(vma)) {
1242 node.start = i915_ggtt_offset(vma);
1243 node.allocated = false;
Chris Wilson49ef5292016-08-18 17:17:00 +01001244 ret = i915_vma_put_fence(vma);
Chris Wilson18034582016-08-18 17:16:45 +01001245 if (ret) {
1246 i915_vma_unpin(vma);
1247 vma = ERR_PTR(ret);
1248 }
1249 }
Chris Wilson058d88c2016-08-15 10:49:06 +01001250 if (IS_ERR(vma)) {
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301251 ret = insert_mappable_node(i915, &node, PAGE_SIZE);
1252 if (ret)
1253 goto out;
1254
1255 ret = i915_gem_object_get_pages(obj);
1256 if (ret) {
1257 remove_mappable_node(&node);
1258 goto out;
1259 }
1260
1261 i915_gem_object_pin_pages(obj);
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301262 }
Daniel Vetter935aaa62012-03-25 19:47:35 +02001263
1264 ret = i915_gem_object_set_to_gtt_domain(obj, true);
1265 if (ret)
1266 goto out_unpin;
1267
Chris Wilsonb19482d2016-08-18 17:16:43 +01001268 intel_fb_obj_invalidate(obj, ORIGIN_CPU);
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301269 obj->dirty = true;
Paulo Zanoni063e4e62015-02-13 17:23:45 -02001270
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301271 user_data = u64_to_user_ptr(args->data_ptr);
1272 offset = args->offset;
1273 remain = args->size;
1274 while (remain) {
Eric Anholt673a3942008-07-30 12:06:12 -07001275 /* Operation in this page
1276 *
Keith Packard0839ccb2008-10-30 19:38:48 -07001277 * page_base = page offset within aperture
1278 * page_offset = offset within page
1279 * page_length = bytes to copy for this page
Eric Anholt673a3942008-07-30 12:06:12 -07001280 */
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301281 u32 page_base = node.start;
1282 unsigned page_offset = offset_in_page(offset);
1283 unsigned page_length = PAGE_SIZE - page_offset;
1284 page_length = remain < page_length ? remain : page_length;
1285 if (node.allocated) {
1286 wmb(); /* flush the write before we modify the GGTT */
1287 ggtt->base.insert_page(&ggtt->base,
1288 i915_gem_object_get_dma_address(obj, offset >> PAGE_SHIFT),
1289 node.start, I915_CACHE_NONE, 0);
1290 wmb(); /* flush modifications to the GGTT (insert_page) */
1291 } else {
1292 page_base += offset & PAGE_MASK;
1293 }
Keith Packard0839ccb2008-10-30 19:38:48 -07001294 /* If we get a fault while copying data, then (presumably) our
Eric Anholt3de09aa2009-03-09 09:42:23 -07001295 * source page isn't available. Return the error and we'll
1296 * retry in the slow path.
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301297 * If the object is non-shmem backed, we retry again with the
1298 * path that handles page fault.
Keith Packard0839ccb2008-10-30 19:38:48 -07001299 */
Chris Wilsonf7bbe782016-08-19 16:54:27 +01001300 if (fast_user_write(&ggtt->mappable, page_base,
Daniel Vetter935aaa62012-03-25 19:47:35 +02001301 page_offset, user_data, page_length)) {
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301302 hit_slow_path = true;
1303 mutex_unlock(&dev->struct_mutex);
Chris Wilsonf7bbe782016-08-19 16:54:27 +01001304 if (slow_user_access(&ggtt->mappable,
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301305 page_base,
1306 page_offset, user_data,
1307 page_length, true)) {
1308 ret = -EFAULT;
1309 mutex_lock(&dev->struct_mutex);
1310 goto out_flush;
1311 }
1312
1313 mutex_lock(&dev->struct_mutex);
Daniel Vetter935aaa62012-03-25 19:47:35 +02001314 }
Eric Anholt673a3942008-07-30 12:06:12 -07001315
Keith Packard0839ccb2008-10-30 19:38:48 -07001316 remain -= page_length;
1317 user_data += page_length;
1318 offset += page_length;
Eric Anholt673a3942008-07-30 12:06:12 -07001319 }
Eric Anholt673a3942008-07-30 12:06:12 -07001320
Paulo Zanoni063e4e62015-02-13 17:23:45 -02001321out_flush:
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301322 if (hit_slow_path) {
1323 if (ret == 0 &&
1324 (obj->base.read_domains & I915_GEM_DOMAIN_GTT) == 0) {
1325 /* The user has modified the object whilst we tried
1326 * reading from it, and we now have no idea what domain
1327 * the pages should be in. As we have just been touching
1328 * them directly, flush everything back to the GTT
1329 * domain.
1330 */
1331 ret = i915_gem_object_set_to_gtt_domain(obj, false);
1332 }
1333 }
1334
Chris Wilsonb19482d2016-08-18 17:16:43 +01001335 intel_fb_obj_flush(obj, false, ORIGIN_CPU);
Daniel Vetter935aaa62012-03-25 19:47:35 +02001336out_unpin:
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301337 if (node.allocated) {
1338 wmb();
1339 ggtt->base.clear_range(&ggtt->base,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02001340 node.start, node.size);
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301341 i915_gem_object_unpin_pages(obj);
1342 remove_mappable_node(&node);
1343 } else {
Chris Wilson058d88c2016-08-15 10:49:06 +01001344 i915_vma_unpin(vma);
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301345 }
Daniel Vetter935aaa62012-03-25 19:47:35 +02001346out:
Chris Wilson9c870d02016-10-24 13:42:15 +01001347 intel_runtime_pm_put(i915);
Eric Anholt3de09aa2009-03-09 09:42:23 -07001348 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07001349}
1350
Daniel Vetterd174bd62012-03-25 19:47:40 +02001351/* Per-page copy function for the shmem pwrite fastpath.
1352 * Flushes invalid cachelines before writing to the target if
1353 * needs_clflush_before is set and flushes out any written cachelines after
1354 * writing if needs_clflush is set. */
Eric Anholt673a3942008-07-30 12:06:12 -07001355static int
Daniel Vetterd174bd62012-03-25 19:47:40 +02001356shmem_pwrite_fast(struct page *page, int shmem_page_offset, int page_length,
1357 char __user *user_data,
1358 bool page_do_bit17_swizzling,
1359 bool needs_clflush_before,
1360 bool needs_clflush_after)
Eric Anholt673a3942008-07-30 12:06:12 -07001361{
Daniel Vetterd174bd62012-03-25 19:47:40 +02001362 char *vaddr;
Eric Anholt3de09aa2009-03-09 09:42:23 -07001363 int ret;
Eric Anholt3de09aa2009-03-09 09:42:23 -07001364
Daniel Vettere7e58eb2012-03-25 19:47:43 +02001365 if (unlikely(page_do_bit17_swizzling))
Daniel Vetterd174bd62012-03-25 19:47:40 +02001366 return -EINVAL;
Eric Anholt3de09aa2009-03-09 09:42:23 -07001367
Daniel Vetterd174bd62012-03-25 19:47:40 +02001368 vaddr = kmap_atomic(page);
1369 if (needs_clflush_before)
1370 drm_clflush_virt_range(vaddr + shmem_page_offset,
1371 page_length);
Chris Wilsonc2831a92014-03-07 08:30:37 +00001372 ret = __copy_from_user_inatomic(vaddr + shmem_page_offset,
1373 user_data, page_length);
Daniel Vetterd174bd62012-03-25 19:47:40 +02001374 if (needs_clflush_after)
1375 drm_clflush_virt_range(vaddr + shmem_page_offset,
1376 page_length);
1377 kunmap_atomic(vaddr);
Eric Anholt3de09aa2009-03-09 09:42:23 -07001378
Chris Wilson755d2212012-09-04 21:02:55 +01001379 return ret ? -EFAULT : 0;
Eric Anholt3de09aa2009-03-09 09:42:23 -07001380}
1381
Daniel Vetterd174bd62012-03-25 19:47:40 +02001382/* Only difference to the fast-path function is that this can handle bit17
1383 * and uses non-atomic copy and kmap functions. */
Eric Anholt3043c602008-10-02 12:24:47 -07001384static int
Daniel Vetterd174bd62012-03-25 19:47:40 +02001385shmem_pwrite_slow(struct page *page, int shmem_page_offset, int page_length,
1386 char __user *user_data,
1387 bool page_do_bit17_swizzling,
1388 bool needs_clflush_before,
1389 bool needs_clflush_after)
Eric Anholt673a3942008-07-30 12:06:12 -07001390{
Daniel Vetterd174bd62012-03-25 19:47:40 +02001391 char *vaddr;
1392 int ret;
Eric Anholt40123c12009-03-09 13:42:30 -07001393
Daniel Vetterd174bd62012-03-25 19:47:40 +02001394 vaddr = kmap(page);
Daniel Vettere7e58eb2012-03-25 19:47:43 +02001395 if (unlikely(needs_clflush_before || page_do_bit17_swizzling))
Daniel Vetter23c18c72012-03-25 19:47:42 +02001396 shmem_clflush_swizzled_range(vaddr + shmem_page_offset,
1397 page_length,
1398 page_do_bit17_swizzling);
Daniel Vetterd174bd62012-03-25 19:47:40 +02001399 if (page_do_bit17_swizzling)
1400 ret = __copy_from_user_swizzled(vaddr, shmem_page_offset,
Chris Wilsone5281cc2010-10-28 13:45:36 +01001401 user_data,
1402 page_length);
Daniel Vetterd174bd62012-03-25 19:47:40 +02001403 else
1404 ret = __copy_from_user(vaddr + shmem_page_offset,
1405 user_data,
1406 page_length);
1407 if (needs_clflush_after)
Daniel Vetter23c18c72012-03-25 19:47:42 +02001408 shmem_clflush_swizzled_range(vaddr + shmem_page_offset,
1409 page_length,
1410 page_do_bit17_swizzling);
Daniel Vetterd174bd62012-03-25 19:47:40 +02001411 kunmap(page);
Chris Wilsone5281cc2010-10-28 13:45:36 +01001412
Chris Wilson755d2212012-09-04 21:02:55 +01001413 return ret ? -EFAULT : 0;
Eric Anholt40123c12009-03-09 13:42:30 -07001414}
1415
Eric Anholt40123c12009-03-09 13:42:30 -07001416static int
Daniel Vettere244a442012-03-25 19:47:28 +02001417i915_gem_shmem_pwrite(struct drm_device *dev,
1418 struct drm_i915_gem_object *obj,
1419 struct drm_i915_gem_pwrite *args,
1420 struct drm_file *file)
Eric Anholt40123c12009-03-09 13:42:30 -07001421{
Eric Anholt40123c12009-03-09 13:42:30 -07001422 ssize_t remain;
Daniel Vetter8c599672011-12-14 13:57:31 +01001423 loff_t offset;
1424 char __user *user_data;
Ben Widawskyeb2c0c82012-02-15 14:42:43 +01001425 int shmem_page_offset, page_length, ret = 0;
Daniel Vetter8c599672011-12-14 13:57:31 +01001426 int obj_do_bit17_swizzling, page_do_bit17_swizzling;
Daniel Vettere244a442012-03-25 19:47:28 +02001427 int hit_slowpath = 0;
Chris Wilson43394c72016-08-18 17:16:47 +01001428 unsigned int needs_clflush;
Imre Deak67d5a502013-02-18 19:28:02 +02001429 struct sg_page_iter sg_iter;
Eric Anholt40123c12009-03-09 13:42:30 -07001430
Chris Wilson43394c72016-08-18 17:16:47 +01001431 ret = i915_gem_obj_prepare_shmem_write(obj, &needs_clflush);
1432 if (ret)
1433 return ret;
Eric Anholt40123c12009-03-09 13:42:30 -07001434
Daniel Vetter8c599672011-12-14 13:57:31 +01001435 obj_do_bit17_swizzling = i915_gem_object_needs_bit17_swizzle(obj);
Chris Wilson43394c72016-08-18 17:16:47 +01001436 user_data = u64_to_user_ptr(args->data_ptr);
Eric Anholt40123c12009-03-09 13:42:30 -07001437 offset = args->offset;
Chris Wilson43394c72016-08-18 17:16:47 +01001438 remain = args->size;
Eric Anholt40123c12009-03-09 13:42:30 -07001439
Imre Deak67d5a502013-02-18 19:28:02 +02001440 for_each_sg_page(obj->pages->sgl, &sg_iter, obj->pages->nents,
1441 offset >> PAGE_SHIFT) {
Imre Deak2db76d72013-03-26 15:14:18 +02001442 struct page *page = sg_page_iter_page(&sg_iter);
Daniel Vetter58642882012-03-25 19:47:37 +02001443 int partial_cacheline_write;
Chris Wilsone5281cc2010-10-28 13:45:36 +01001444
Chris Wilson9da3da62012-06-01 15:20:22 +01001445 if (remain <= 0)
1446 break;
1447
Eric Anholt40123c12009-03-09 13:42:30 -07001448 /* Operation in this page
1449 *
Eric Anholt40123c12009-03-09 13:42:30 -07001450 * shmem_page_offset = offset within page in shmem file
Eric Anholt40123c12009-03-09 13:42:30 -07001451 * page_length = bytes to copy for this page
1452 */
Chris Wilsonc8cbbb82011-05-12 22:17:11 +01001453 shmem_page_offset = offset_in_page(offset);
Eric Anholt40123c12009-03-09 13:42:30 -07001454
1455 page_length = remain;
1456 if ((shmem_page_offset + page_length) > PAGE_SIZE)
1457 page_length = PAGE_SIZE - shmem_page_offset;
Eric Anholt40123c12009-03-09 13:42:30 -07001458
Daniel Vetter58642882012-03-25 19:47:37 +02001459 /* If we don't overwrite a cacheline completely we need to be
1460 * careful to have up-to-date data by first clflushing. Don't
1461 * overcomplicate things and flush the entire patch. */
Chris Wilson43394c72016-08-18 17:16:47 +01001462 partial_cacheline_write = needs_clflush & CLFLUSH_BEFORE &&
Daniel Vetter58642882012-03-25 19:47:37 +02001463 ((shmem_page_offset | page_length)
1464 & (boot_cpu_data.x86_clflush_size - 1));
1465
Daniel Vetter8c599672011-12-14 13:57:31 +01001466 page_do_bit17_swizzling = obj_do_bit17_swizzling &&
1467 (page_to_phys(page) & (1 << 17)) != 0;
1468
Daniel Vetterd174bd62012-03-25 19:47:40 +02001469 ret = shmem_pwrite_fast(page, shmem_page_offset, page_length,
1470 user_data, page_do_bit17_swizzling,
1471 partial_cacheline_write,
Chris Wilson43394c72016-08-18 17:16:47 +01001472 needs_clflush & CLFLUSH_AFTER);
Daniel Vetterd174bd62012-03-25 19:47:40 +02001473 if (ret == 0)
1474 goto next_page;
Eric Anholt40123c12009-03-09 13:42:30 -07001475
Daniel Vettere244a442012-03-25 19:47:28 +02001476 hit_slowpath = 1;
Daniel Vettere244a442012-03-25 19:47:28 +02001477 mutex_unlock(&dev->struct_mutex);
Daniel Vetterd174bd62012-03-25 19:47:40 +02001478 ret = shmem_pwrite_slow(page, shmem_page_offset, page_length,
1479 user_data, page_do_bit17_swizzling,
1480 partial_cacheline_write,
Chris Wilson43394c72016-08-18 17:16:47 +01001481 needs_clflush & CLFLUSH_AFTER);
Eric Anholt40123c12009-03-09 13:42:30 -07001482
Daniel Vettere244a442012-03-25 19:47:28 +02001483 mutex_lock(&dev->struct_mutex);
Chris Wilson755d2212012-09-04 21:02:55 +01001484
Chris Wilson755d2212012-09-04 21:02:55 +01001485 if (ret)
Daniel Vetter8c599672011-12-14 13:57:31 +01001486 goto out;
Daniel Vetter8c599672011-12-14 13:57:31 +01001487
Chris Wilson17793c92014-03-07 08:30:36 +00001488next_page:
Eric Anholt40123c12009-03-09 13:42:30 -07001489 remain -= page_length;
Daniel Vetter8c599672011-12-14 13:57:31 +01001490 user_data += page_length;
Eric Anholt40123c12009-03-09 13:42:30 -07001491 offset += page_length;
1492 }
1493
Chris Wilsonfbd5a262010-10-14 15:03:58 +01001494out:
Chris Wilson43394c72016-08-18 17:16:47 +01001495 i915_gem_obj_finish_shmem_access(obj);
Chris Wilson755d2212012-09-04 21:02:55 +01001496
Daniel Vettere244a442012-03-25 19:47:28 +02001497 if (hit_slowpath) {
Daniel Vetter8dcf0152012-11-15 16:53:58 +01001498 /*
1499 * Fixup: Flush cpu caches in case we didn't flush the dirty
1500 * cachelines in-line while writing and the object moved
1501 * out of the cpu write domain while we've dropped the lock.
1502 */
Chris Wilson43394c72016-08-18 17:16:47 +01001503 if (!(needs_clflush & CLFLUSH_AFTER) &&
Daniel Vetter8dcf0152012-11-15 16:53:58 +01001504 obj->base.write_domain != I915_GEM_DOMAIN_CPU) {
Chris Wilson000433b2013-08-08 14:41:09 +01001505 if (i915_gem_clflush_object(obj, obj->pin_display))
Chris Wilson43394c72016-08-18 17:16:47 +01001506 needs_clflush |= CLFLUSH_AFTER;
Daniel Vettere244a442012-03-25 19:47:28 +02001507 }
Daniel Vetter8c599672011-12-14 13:57:31 +01001508 }
Eric Anholt40123c12009-03-09 13:42:30 -07001509
Chris Wilson43394c72016-08-18 17:16:47 +01001510 if (needs_clflush & CLFLUSH_AFTER)
Chris Wilsonc0336662016-05-06 15:40:21 +01001511 i915_gem_chipset_flush(to_i915(dev));
Daniel Vetter58642882012-03-25 19:47:37 +02001512
Rodrigo Vivide152b62015-07-07 16:28:51 -07001513 intel_fb_obj_flush(obj, false, ORIGIN_CPU);
Eric Anholt40123c12009-03-09 13:42:30 -07001514 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07001515}
1516
1517/**
1518 * Writes data to the object referenced by handle.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01001519 * @dev: drm device
1520 * @data: ioctl data blob
1521 * @file: drm file
Eric Anholt673a3942008-07-30 12:06:12 -07001522 *
1523 * On error, the contents of the buffer that were to be modified are undefined.
1524 */
1525int
1526i915_gem_pwrite_ioctl(struct drm_device *dev, void *data,
Chris Wilsonfbd5a262010-10-14 15:03:58 +01001527 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001528{
Chris Wilsonfac5e232016-07-04 11:34:36 +01001529 struct drm_i915_private *dev_priv = to_i915(dev);
Eric Anholt673a3942008-07-30 12:06:12 -07001530 struct drm_i915_gem_pwrite *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00001531 struct drm_i915_gem_object *obj;
Chris Wilson51311d02010-11-17 09:10:42 +00001532 int ret;
1533
1534 if (args->size == 0)
1535 return 0;
1536
1537 if (!access_ok(VERIFY_READ,
Gustavo Padovan3ed605b2016-04-26 12:32:27 -03001538 u64_to_user_ptr(args->data_ptr),
Chris Wilson51311d02010-11-17 09:10:42 +00001539 args->size))
1540 return -EFAULT;
1541
Jani Nikulad330a952014-01-21 11:24:25 +02001542 if (likely(!i915.prefault_disable)) {
Al Viro4bce9f62016-09-17 18:02:44 -04001543 ret = fault_in_pages_readable(u64_to_user_ptr(args->data_ptr),
Xiong Zhang0b74b502013-07-19 13:51:24 +08001544 args->size);
1545 if (ret)
1546 return -EFAULT;
1547 }
Eric Anholt673a3942008-07-30 12:06:12 -07001548
Chris Wilson03ac0642016-07-20 13:31:51 +01001549 obj = i915_gem_object_lookup(file, args->handle);
Chris Wilson258a5ed2016-08-05 10:14:16 +01001550 if (!obj)
1551 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07001552
Chris Wilson7dcd2492010-09-26 20:21:44 +01001553 /* Bounds check destination. */
Chris Wilson05394f32010-11-08 19:18:58 +00001554 if (args->offset > obj->base.size ||
1555 args->size > obj->base.size - args->offset) {
Chris Wilsonce9d4192010-09-26 20:50:05 +01001556 ret = -EINVAL;
Chris Wilson258a5ed2016-08-05 10:14:16 +01001557 goto err;
Chris Wilsonce9d4192010-09-26 20:50:05 +01001558 }
1559
Chris Wilsondb53a302011-02-03 11:57:46 +00001560 trace_i915_gem_object_pwrite(obj, args->offset, args->size);
1561
Chris Wilsone95433c2016-10-28 13:58:27 +01001562 ret = i915_gem_object_wait(obj,
1563 I915_WAIT_INTERRUPTIBLE |
1564 I915_WAIT_ALL,
1565 MAX_SCHEDULE_TIMEOUT,
1566 to_rps_client(file));
Chris Wilson258a5ed2016-08-05 10:14:16 +01001567 if (ret)
1568 goto err;
1569
1570 intel_runtime_pm_get(dev_priv);
1571
1572 ret = i915_mutex_lock_interruptible(dev);
1573 if (ret)
1574 goto err_rpm;
1575
Daniel Vetter935aaa62012-03-25 19:47:35 +02001576 ret = -EFAULT;
Eric Anholt673a3942008-07-30 12:06:12 -07001577 /* We can only do the GTT pwrite on untiled buffers, as otherwise
1578 * it would end up going through the fenced access, and we'll get
1579 * different detiling behavior between reading and writing.
1580 * pread/pwrite currently are reading and writing from the CPU
1581 * perspective, requiring manual detiling by the client.
1582 */
Chris Wilson6eae0052016-06-20 15:05:52 +01001583 if (!i915_gem_object_has_struct_page(obj) ||
Chris Wilson9c870d02016-10-24 13:42:15 +01001584 cpu_write_needs_clflush(obj))
Daniel Vetter935aaa62012-03-25 19:47:35 +02001585 /* Note that the gtt paths might fail with non-page-backed user
1586 * pointers (e.g. gtt mappings when moving data between
Chris Wilson9c870d02016-10-24 13:42:15 +01001587 * textures). Fallback to the shmem path in that case.
1588 */
1589 ret = i915_gem_gtt_pwrite_fast(dev_priv, obj, args, file);
Eric Anholt673a3942008-07-30 12:06:12 -07001590
Chris Wilsond1054ee2016-07-16 18:42:36 +01001591 if (ret == -EFAULT || ret == -ENOSPC) {
Chris Wilson6a2c4232014-11-04 04:51:40 -08001592 if (obj->phys_handle)
1593 ret = i915_gem_phys_pwrite(obj, args, file);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301594 else
Chris Wilson43394c72016-08-18 17:16:47 +01001595 ret = i915_gem_shmem_pwrite(dev, obj, args, file);
Chris Wilson6a2c4232014-11-04 04:51:40 -08001596 }
Daniel Vetter5c0480f2011-12-14 13:57:30 +01001597
Chris Wilsonf8c417c2016-07-20 13:31:53 +01001598 i915_gem_object_put(obj);
Chris Wilsonfbd5a262010-10-14 15:03:58 +01001599 mutex_unlock(&dev->struct_mutex);
Imre Deak5d77d9c2014-11-12 16:40:35 +02001600 intel_runtime_pm_put(dev_priv);
1601
Eric Anholt673a3942008-07-30 12:06:12 -07001602 return ret;
Chris Wilson258a5ed2016-08-05 10:14:16 +01001603
1604err_rpm:
1605 intel_runtime_pm_put(dev_priv);
1606err:
1607 i915_gem_object_put_unlocked(obj);
1608 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07001609}
1610
Chris Wilsond243ad82016-08-18 17:16:44 +01001611static inline enum fb_op_origin
Chris Wilsonaeecc962016-06-17 14:46:39 -03001612write_origin(struct drm_i915_gem_object *obj, unsigned domain)
1613{
Chris Wilson50349242016-08-18 17:17:04 +01001614 return (domain == I915_GEM_DOMAIN_GTT ?
1615 obj->frontbuffer_ggtt_origin : ORIGIN_CPU);
Chris Wilsonaeecc962016-06-17 14:46:39 -03001616}
1617
Eric Anholt673a3942008-07-30 12:06:12 -07001618/**
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001619 * Called when user space prepares to use an object with the CPU, either
1620 * through the mmap ioctl's mapping or a GTT mapping.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01001621 * @dev: drm device
1622 * @data: ioctl data blob
1623 * @file: drm file
Eric Anholt673a3942008-07-30 12:06:12 -07001624 */
1625int
1626i915_gem_set_domain_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00001627 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001628{
1629 struct drm_i915_gem_set_domain *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00001630 struct drm_i915_gem_object *obj;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001631 uint32_t read_domains = args->read_domains;
1632 uint32_t write_domain = args->write_domain;
Eric Anholt673a3942008-07-30 12:06:12 -07001633 int ret;
1634
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001635 /* Only handle setting domains to types used by the CPU. */
Chris Wilsonb8f90962016-08-05 10:14:07 +01001636 if ((write_domain | read_domains) & I915_GEM_GPU_DOMAINS)
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001637 return -EINVAL;
1638
1639 /* Having something in the write domain implies it's in the read
1640 * domain, and only that read domain. Enforce that in the request.
1641 */
1642 if (write_domain != 0 && read_domains != write_domain)
1643 return -EINVAL;
1644
Chris Wilson03ac0642016-07-20 13:31:51 +01001645 obj = i915_gem_object_lookup(file, args->handle);
Chris Wilsonb8f90962016-08-05 10:14:07 +01001646 if (!obj)
1647 return -ENOENT;
Jesse Barnes652c3932009-08-17 13:31:43 -07001648
Chris Wilson3236f572012-08-24 09:35:09 +01001649 /* Try to flush the object off the GPU without holding the lock.
1650 * We will repeat the flush holding the lock in the normal manner
1651 * to catch cases where we are gazumped.
1652 */
Chris Wilsone95433c2016-10-28 13:58:27 +01001653 ret = i915_gem_object_wait(obj,
1654 I915_WAIT_INTERRUPTIBLE |
1655 (write_domain ? I915_WAIT_ALL : 0),
1656 MAX_SCHEDULE_TIMEOUT,
1657 to_rps_client(file));
Chris Wilson3236f572012-08-24 09:35:09 +01001658 if (ret)
Chris Wilsonb8f90962016-08-05 10:14:07 +01001659 goto err;
1660
1661 ret = i915_mutex_lock_interruptible(dev);
1662 if (ret)
1663 goto err;
Chris Wilson3236f572012-08-24 09:35:09 +01001664
Chris Wilson43566de2015-01-02 16:29:29 +05301665 if (read_domains & I915_GEM_DOMAIN_GTT)
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001666 ret = i915_gem_object_set_to_gtt_domain(obj, write_domain != 0);
Chris Wilson43566de2015-01-02 16:29:29 +05301667 else
Eric Anholte47c68e2008-11-14 13:35:19 -08001668 ret = i915_gem_object_set_to_cpu_domain(obj, write_domain != 0);
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001669
Daniel Vetter031b6982015-06-26 19:35:16 +02001670 if (write_domain != 0)
Chris Wilsonaeecc962016-06-17 14:46:39 -03001671 intel_fb_obj_invalidate(obj, write_origin(obj, write_domain));
Daniel Vetter031b6982015-06-26 19:35:16 +02001672
Chris Wilsonf8c417c2016-07-20 13:31:53 +01001673 i915_gem_object_put(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001674 mutex_unlock(&dev->struct_mutex);
1675 return ret;
Chris Wilsonb8f90962016-08-05 10:14:07 +01001676
1677err:
1678 i915_gem_object_put_unlocked(obj);
1679 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07001680}
1681
1682/**
1683 * Called when user space has done writes to this buffer
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01001684 * @dev: drm device
1685 * @data: ioctl data blob
1686 * @file: drm file
Eric Anholt673a3942008-07-30 12:06:12 -07001687 */
1688int
1689i915_gem_sw_finish_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00001690 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001691{
1692 struct drm_i915_gem_sw_finish *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00001693 struct drm_i915_gem_object *obj;
Chris Wilsonc21724c2016-08-05 10:14:19 +01001694 int err = 0;
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001695
Chris Wilson03ac0642016-07-20 13:31:51 +01001696 obj = i915_gem_object_lookup(file, args->handle);
Chris Wilsonc21724c2016-08-05 10:14:19 +01001697 if (!obj)
1698 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07001699
Eric Anholt673a3942008-07-30 12:06:12 -07001700 /* Pinned buffers may be scanout, so flush the cache */
Chris Wilsonc21724c2016-08-05 10:14:19 +01001701 if (READ_ONCE(obj->pin_display)) {
1702 err = i915_mutex_lock_interruptible(dev);
1703 if (!err) {
1704 i915_gem_object_flush_cpu_write_domain(obj);
1705 mutex_unlock(&dev->struct_mutex);
1706 }
1707 }
Eric Anholte47c68e2008-11-14 13:35:19 -08001708
Chris Wilsonc21724c2016-08-05 10:14:19 +01001709 i915_gem_object_put_unlocked(obj);
1710 return err;
Eric Anholt673a3942008-07-30 12:06:12 -07001711}
1712
1713/**
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01001714 * i915_gem_mmap_ioctl - Maps the contents of an object, returning the address
1715 * it is mapped to.
1716 * @dev: drm device
1717 * @data: ioctl data blob
1718 * @file: drm file
Eric Anholt673a3942008-07-30 12:06:12 -07001719 *
1720 * While the mapping holds a reference on the contents of the object, it doesn't
1721 * imply a ref on the object itself.
Daniel Vetter34367382014-10-16 12:28:18 +02001722 *
1723 * IMPORTANT:
1724 *
1725 * DRM driver writers who look a this function as an example for how to do GEM
1726 * mmap support, please don't implement mmap support like here. The modern way
1727 * to implement DRM mmap support is with an mmap offset ioctl (like
1728 * i915_gem_mmap_gtt) and then using the mmap syscall on the DRM fd directly.
1729 * That way debug tooling like valgrind will understand what's going on, hiding
1730 * the mmap call in a driver private ioctl will break that. The i915 driver only
1731 * does cpu mmaps this way because we didn't know better.
Eric Anholt673a3942008-07-30 12:06:12 -07001732 */
1733int
1734i915_gem_mmap_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00001735 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001736{
1737 struct drm_i915_gem_mmap *args = data;
Chris Wilson03ac0642016-07-20 13:31:51 +01001738 struct drm_i915_gem_object *obj;
Eric Anholt673a3942008-07-30 12:06:12 -07001739 unsigned long addr;
1740
Akash Goel1816f922015-01-02 16:29:30 +05301741 if (args->flags & ~(I915_MMAP_WC))
1742 return -EINVAL;
1743
Borislav Petkov568a58e2016-03-29 17:42:01 +02001744 if (args->flags & I915_MMAP_WC && !boot_cpu_has(X86_FEATURE_PAT))
Akash Goel1816f922015-01-02 16:29:30 +05301745 return -ENODEV;
1746
Chris Wilson03ac0642016-07-20 13:31:51 +01001747 obj = i915_gem_object_lookup(file, args->handle);
1748 if (!obj)
Chris Wilsonbf79cb92010-08-04 14:19:46 +01001749 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07001750
Daniel Vetter1286ff72012-05-10 15:25:09 +02001751 /* prime objects have no backing filp to GEM mmap
1752 * pages from.
1753 */
Chris Wilson03ac0642016-07-20 13:31:51 +01001754 if (!obj->base.filp) {
Chris Wilson34911fd2016-07-20 13:31:54 +01001755 i915_gem_object_put_unlocked(obj);
Daniel Vetter1286ff72012-05-10 15:25:09 +02001756 return -EINVAL;
1757 }
1758
Chris Wilson03ac0642016-07-20 13:31:51 +01001759 addr = vm_mmap(obj->base.filp, 0, args->size,
Eric Anholt673a3942008-07-30 12:06:12 -07001760 PROT_READ | PROT_WRITE, MAP_SHARED,
1761 args->offset);
Akash Goel1816f922015-01-02 16:29:30 +05301762 if (args->flags & I915_MMAP_WC) {
1763 struct mm_struct *mm = current->mm;
1764 struct vm_area_struct *vma;
1765
Michal Hocko80a89a52016-05-23 16:26:11 -07001766 if (down_write_killable(&mm->mmap_sem)) {
Chris Wilson34911fd2016-07-20 13:31:54 +01001767 i915_gem_object_put_unlocked(obj);
Michal Hocko80a89a52016-05-23 16:26:11 -07001768 return -EINTR;
1769 }
Akash Goel1816f922015-01-02 16:29:30 +05301770 vma = find_vma(mm, addr);
1771 if (vma)
1772 vma->vm_page_prot =
1773 pgprot_writecombine(vm_get_page_prot(vma->vm_flags));
1774 else
1775 addr = -ENOMEM;
1776 up_write(&mm->mmap_sem);
Chris Wilsonaeecc962016-06-17 14:46:39 -03001777
1778 /* This may race, but that's ok, it only gets set */
Chris Wilson50349242016-08-18 17:17:04 +01001779 WRITE_ONCE(obj->frontbuffer_ggtt_origin, ORIGIN_CPU);
Akash Goel1816f922015-01-02 16:29:30 +05301780 }
Chris Wilson34911fd2016-07-20 13:31:54 +01001781 i915_gem_object_put_unlocked(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001782 if (IS_ERR((void *)addr))
1783 return addr;
1784
1785 args->addr_ptr = (uint64_t) addr;
1786
1787 return 0;
1788}
1789
Chris Wilson03af84f2016-08-18 17:17:01 +01001790static unsigned int tile_row_pages(struct drm_i915_gem_object *obj)
1791{
1792 u64 size;
1793
1794 size = i915_gem_object_get_stride(obj);
1795 size *= i915_gem_object_get_tiling(obj) == I915_TILING_Y ? 32 : 8;
1796
1797 return size >> PAGE_SHIFT;
1798}
1799
Jesse Barnesde151cf2008-11-12 10:03:55 -08001800/**
Chris Wilson4cc69072016-08-25 19:05:19 +01001801 * i915_gem_mmap_gtt_version - report the current feature set for GTT mmaps
1802 *
1803 * A history of the GTT mmap interface:
1804 *
1805 * 0 - Everything had to fit into the GTT. Both parties of a memcpy had to
1806 * aligned and suitable for fencing, and still fit into the available
1807 * mappable space left by the pinned display objects. A classic problem
1808 * we called the page-fault-of-doom where we would ping-pong between
1809 * two objects that could not fit inside the GTT and so the memcpy
1810 * would page one object in at the expense of the other between every
1811 * single byte.
1812 *
1813 * 1 - Objects can be any size, and have any compatible fencing (X Y, or none
1814 * as set via i915_gem_set_tiling() [DRM_I915_GEM_SET_TILING]). If the
1815 * object is too large for the available space (or simply too large
1816 * for the mappable aperture!), a view is created instead and faulted
1817 * into userspace. (This view is aligned and sized appropriately for
1818 * fenced access.)
1819 *
1820 * Restrictions:
1821 *
1822 * * snoopable objects cannot be accessed via the GTT. It can cause machine
1823 * hangs on some architectures, corruption on others. An attempt to service
1824 * a GTT page fault from a snoopable object will generate a SIGBUS.
1825 *
1826 * * the object must be able to fit into RAM (physical memory, though no
1827 * limited to the mappable aperture).
1828 *
1829 *
1830 * Caveats:
1831 *
1832 * * a new GTT page fault will synchronize rendering from the GPU and flush
1833 * all data to system memory. Subsequent access will not be synchronized.
1834 *
1835 * * all mappings are revoked on runtime device suspend.
1836 *
1837 * * there are only 8, 16 or 32 fence registers to share between all users
1838 * (older machines require fence register for display and blitter access
1839 * as well). Contention of the fence registers will cause the previous users
1840 * to be unmapped and any new access will generate new page faults.
1841 *
1842 * * running out of memory while servicing a fault may generate a SIGBUS,
1843 * rather than the expected SIGSEGV.
1844 */
1845int i915_gem_mmap_gtt_version(void)
1846{
1847 return 1;
1848}
1849
1850/**
Jesse Barnesde151cf2008-11-12 10:03:55 -08001851 * i915_gem_fault - fault a page into the GTT
Chris Wilson058d88c2016-08-15 10:49:06 +01001852 * @area: CPU VMA in question
Geliang Tangd9072a32015-09-15 05:58:44 -07001853 * @vmf: fault info
Jesse Barnesde151cf2008-11-12 10:03:55 -08001854 *
1855 * The fault handler is set up by drm_gem_mmap() when a object is GTT mapped
1856 * from userspace. The fault handler takes care of binding the object to
1857 * the GTT (if needed), allocating and programming a fence register (again,
1858 * only if needed based on whether the old reg is still valid or the object
1859 * is tiled) and inserting a new PTE into the faulting process.
1860 *
1861 * Note that the faulting process may involve evicting existing objects
1862 * from the GTT and/or fence registers to make room. So performance may
1863 * suffer if the GTT working set is large or there are few fence registers
1864 * left.
Chris Wilson4cc69072016-08-25 19:05:19 +01001865 *
1866 * The current feature set supported by i915_gem_fault() and thus GTT mmaps
1867 * is exposed via I915_PARAM_MMAP_GTT_VERSION (see i915_gem_mmap_gtt_version).
Jesse Barnesde151cf2008-11-12 10:03:55 -08001868 */
Chris Wilson058d88c2016-08-15 10:49:06 +01001869int i915_gem_fault(struct vm_area_struct *area, struct vm_fault *vmf)
Jesse Barnesde151cf2008-11-12 10:03:55 -08001870{
Chris Wilson03af84f2016-08-18 17:17:01 +01001871#define MIN_CHUNK_PAGES ((1 << 20) >> PAGE_SHIFT) /* 1 MiB */
Chris Wilson058d88c2016-08-15 10:49:06 +01001872 struct drm_i915_gem_object *obj = to_intel_bo(area->vm_private_data);
Chris Wilson05394f32010-11-08 19:18:58 +00001873 struct drm_device *dev = obj->base.dev;
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03001874 struct drm_i915_private *dev_priv = to_i915(dev);
1875 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Chris Wilsonb8f90962016-08-05 10:14:07 +01001876 bool write = !!(vmf->flags & FAULT_FLAG_WRITE);
Chris Wilson058d88c2016-08-15 10:49:06 +01001877 struct i915_vma *vma;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001878 pgoff_t page_offset;
Chris Wilson82118872016-08-18 17:17:05 +01001879 unsigned int flags;
Chris Wilsonb8f90962016-08-05 10:14:07 +01001880 int ret;
Paulo Zanonif65c9162013-11-27 18:20:34 -02001881
Jesse Barnesde151cf2008-11-12 10:03:55 -08001882 /* We don't use vmf->pgoff since that has the fake offset */
Chris Wilson058d88c2016-08-15 10:49:06 +01001883 page_offset = ((unsigned long)vmf->virtual_address - area->vm_start) >>
Jesse Barnesde151cf2008-11-12 10:03:55 -08001884 PAGE_SHIFT;
1885
Chris Wilsondb53a302011-02-03 11:57:46 +00001886 trace_i915_gem_object_fault(obj, page_offset, true, write);
1887
Chris Wilson6e4930f2014-02-07 18:37:06 -02001888 /* Try to flush the object off the GPU first without holding the lock.
Chris Wilsonb8f90962016-08-05 10:14:07 +01001889 * Upon acquiring the lock, we will perform our sanity checks and then
Chris Wilson6e4930f2014-02-07 18:37:06 -02001890 * repeat the flush holding the lock in the normal manner to catch cases
1891 * where we are gazumped.
1892 */
Chris Wilsone95433c2016-10-28 13:58:27 +01001893 ret = i915_gem_object_wait(obj,
1894 I915_WAIT_INTERRUPTIBLE,
1895 MAX_SCHEDULE_TIMEOUT,
1896 NULL);
Chris Wilson6e4930f2014-02-07 18:37:06 -02001897 if (ret)
Chris Wilsonb8f90962016-08-05 10:14:07 +01001898 goto err;
1899
1900 intel_runtime_pm_get(dev_priv);
1901
1902 ret = i915_mutex_lock_interruptible(dev);
1903 if (ret)
1904 goto err_rpm;
Chris Wilson6e4930f2014-02-07 18:37:06 -02001905
Chris Wilsoneb119bd2012-12-16 12:43:36 +00001906 /* Access to snoopable pages through the GTT is incoherent. */
1907 if (obj->cache_level != I915_CACHE_NONE && !HAS_LLC(dev)) {
Chris Wilsonddeff6e2014-05-28 16:16:41 +01001908 ret = -EFAULT;
Chris Wilsonb8f90962016-08-05 10:14:07 +01001909 goto err_unlock;
Chris Wilsoneb119bd2012-12-16 12:43:36 +00001910 }
1911
Chris Wilson82118872016-08-18 17:17:05 +01001912 /* If the object is smaller than a couple of partial vma, it is
1913 * not worth only creating a single partial vma - we may as well
1914 * clear enough space for the full object.
1915 */
1916 flags = PIN_MAPPABLE;
1917 if (obj->base.size > 2 * MIN_CHUNK_PAGES << PAGE_SHIFT)
1918 flags |= PIN_NONBLOCK | PIN_NONFAULT;
1919
Chris Wilsona61007a2016-08-18 17:17:02 +01001920 /* Now pin it into the GTT as needed */
Chris Wilson82118872016-08-18 17:17:05 +01001921 vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0, flags);
Chris Wilsona61007a2016-08-18 17:17:02 +01001922 if (IS_ERR(vma)) {
1923 struct i915_ggtt_view view;
Chris Wilson03af84f2016-08-18 17:17:01 +01001924 unsigned int chunk_size;
1925
Chris Wilsona61007a2016-08-18 17:17:02 +01001926 /* Use a partial view if it is bigger than available space */
Chris Wilson03af84f2016-08-18 17:17:01 +01001927 chunk_size = MIN_CHUNK_PAGES;
1928 if (i915_gem_object_is_tiled(obj))
1929 chunk_size = max(chunk_size, tile_row_pages(obj));
Joonas Lahtinene7ded2d2015-05-08 14:37:39 +03001930
Joonas Lahtinenc5ad54c2015-05-06 14:36:09 +03001931 memset(&view, 0, sizeof(view));
1932 view.type = I915_GGTT_VIEW_PARTIAL;
1933 view.params.partial.offset = rounddown(page_offset, chunk_size);
1934 view.params.partial.size =
Chris Wilsona61007a2016-08-18 17:17:02 +01001935 min_t(unsigned int, chunk_size,
Chris Wilson908b1232016-10-11 10:06:56 +01001936 vma_pages(area) - view.params.partial.offset);
Joonas Lahtinenc5ad54c2015-05-06 14:36:09 +03001937
Chris Wilsonaa136d92016-08-18 17:17:03 +01001938 /* If the partial covers the entire object, just create a
1939 * normal VMA.
1940 */
1941 if (chunk_size >= obj->base.size >> PAGE_SHIFT)
1942 view.type = I915_GGTT_VIEW_NORMAL;
1943
Chris Wilson50349242016-08-18 17:17:04 +01001944 /* Userspace is now writing through an untracked VMA, abandon
1945 * all hope that the hardware is able to track future writes.
1946 */
1947 obj->frontbuffer_ggtt_origin = ORIGIN_CPU;
1948
Chris Wilsona61007a2016-08-18 17:17:02 +01001949 vma = i915_gem_object_ggtt_pin(obj, &view, 0, 0, PIN_MAPPABLE);
1950 }
Chris Wilson058d88c2016-08-15 10:49:06 +01001951 if (IS_ERR(vma)) {
1952 ret = PTR_ERR(vma);
Chris Wilsonb8f90962016-08-05 10:14:07 +01001953 goto err_unlock;
Chris Wilson058d88c2016-08-15 10:49:06 +01001954 }
Jesse Barnesde151cf2008-11-12 10:03:55 -08001955
Chris Wilsonc9839302012-11-20 10:45:17 +00001956 ret = i915_gem_object_set_to_gtt_domain(obj, write);
1957 if (ret)
Chris Wilsonb8f90962016-08-05 10:14:07 +01001958 goto err_unpin;
Chris Wilsonc9839302012-11-20 10:45:17 +00001959
Chris Wilson49ef5292016-08-18 17:17:00 +01001960 ret = i915_vma_get_fence(vma);
Chris Wilsonc9839302012-11-20 10:45:17 +00001961 if (ret)
Chris Wilsonb8f90962016-08-05 10:14:07 +01001962 goto err_unpin;
Chris Wilson7d1c4802010-08-07 21:45:03 +01001963
Chris Wilson275f0392016-10-24 13:42:14 +01001964 /* Mark as being mmapped into userspace for later revocation */
Chris Wilson9c870d02016-10-24 13:42:15 +01001965 assert_rpm_wakelock_held(dev_priv);
Chris Wilson275f0392016-10-24 13:42:14 +01001966 if (list_empty(&obj->userfault_link))
1967 list_add(&obj->userfault_link, &dev_priv->mm.userfault_list);
Chris Wilson275f0392016-10-24 13:42:14 +01001968
Chris Wilsonb90b91d2014-06-10 12:14:40 +01001969 /* Finally, remap it using the new GTT offset */
Chris Wilsonc58305a2016-08-19 16:54:28 +01001970 ret = remap_io_mapping(area,
1971 area->vm_start + (vma->ggtt_view.params.partial.offset << PAGE_SHIFT),
1972 (ggtt->mappable_base + vma->node.start) >> PAGE_SHIFT,
1973 min_t(u64, vma->size, area->vm_end - area->vm_start),
1974 &ggtt->mappable);
Chris Wilsona61007a2016-08-18 17:17:02 +01001975
Chris Wilsonb8f90962016-08-05 10:14:07 +01001976err_unpin:
Chris Wilson058d88c2016-08-15 10:49:06 +01001977 __i915_vma_unpin(vma);
Chris Wilsonb8f90962016-08-05 10:14:07 +01001978err_unlock:
Jesse Barnesde151cf2008-11-12 10:03:55 -08001979 mutex_unlock(&dev->struct_mutex);
Chris Wilsonb8f90962016-08-05 10:14:07 +01001980err_rpm:
1981 intel_runtime_pm_put(dev_priv);
1982err:
Jesse Barnesde151cf2008-11-12 10:03:55 -08001983 switch (ret) {
Chris Wilsond9bc7e92011-02-07 13:09:31 +00001984 case -EIO:
Daniel Vetter2232f032014-09-04 09:36:18 +02001985 /*
1986 * We eat errors when the gpu is terminally wedged to avoid
1987 * userspace unduly crashing (gl has no provisions for mmaps to
1988 * fail). But any other -EIO isn't ours (e.g. swap in failure)
1989 * and so needs to be reported.
1990 */
1991 if (!i915_terminally_wedged(&dev_priv->gpu_error)) {
Paulo Zanonif65c9162013-11-27 18:20:34 -02001992 ret = VM_FAULT_SIGBUS;
1993 break;
1994 }
Chris Wilson045e7692010-11-07 09:18:22 +00001995 case -EAGAIN:
Daniel Vetter571c6082013-09-12 17:57:28 +02001996 /*
1997 * EAGAIN means the gpu is hung and we'll wait for the error
1998 * handler to reset everything when re-faulting in
1999 * i915_mutex_lock_interruptible.
Chris Wilsond9bc7e92011-02-07 13:09:31 +00002000 */
Chris Wilsonc7150892009-09-23 00:43:56 +01002001 case 0:
2002 case -ERESTARTSYS:
Chris Wilsonbed636a2011-02-11 20:31:19 +00002003 case -EINTR:
Dmitry Rogozhkine79e0fe2012-10-03 17:15:26 +03002004 case -EBUSY:
2005 /*
2006 * EBUSY is ok: this just means that another thread
2007 * already did the job.
2008 */
Paulo Zanonif65c9162013-11-27 18:20:34 -02002009 ret = VM_FAULT_NOPAGE;
2010 break;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002011 case -ENOMEM:
Paulo Zanonif65c9162013-11-27 18:20:34 -02002012 ret = VM_FAULT_OOM;
2013 break;
Daniel Vettera7c2e1a2012-10-17 11:17:16 +02002014 case -ENOSPC:
Chris Wilson45d67812014-01-31 11:34:57 +00002015 case -EFAULT:
Paulo Zanonif65c9162013-11-27 18:20:34 -02002016 ret = VM_FAULT_SIGBUS;
2017 break;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002018 default:
Daniel Vettera7c2e1a2012-10-17 11:17:16 +02002019 WARN_ONCE(ret, "unhandled error in i915_gem_fault: %i\n", ret);
Paulo Zanonif65c9162013-11-27 18:20:34 -02002020 ret = VM_FAULT_SIGBUS;
2021 break;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002022 }
Paulo Zanonif65c9162013-11-27 18:20:34 -02002023 return ret;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002024}
2025
2026/**
Chris Wilson901782b2009-07-10 08:18:50 +01002027 * i915_gem_release_mmap - remove physical page mappings
2028 * @obj: obj in question
2029 *
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02002030 * Preserve the reservation of the mmapping with the DRM core code, but
Chris Wilson901782b2009-07-10 08:18:50 +01002031 * relinquish ownership of the pages back to the system.
2032 *
2033 * It is vital that we remove the page mapping if we have mapped a tiled
2034 * object through the GTT and then lose the fence register due to
2035 * resource pressure. Similarly if the object has been moved out of the
2036 * aperture, than pages mapped into userspace must be revoked. Removing the
2037 * mapping will then trigger a page fault on the next user access, allowing
2038 * fixup by i915_gem_fault().
2039 */
Eric Anholtd05ca302009-07-10 13:02:26 -07002040void
Chris Wilson05394f32010-11-08 19:18:58 +00002041i915_gem_release_mmap(struct drm_i915_gem_object *obj)
Chris Wilson901782b2009-07-10 08:18:50 +01002042{
Chris Wilson275f0392016-10-24 13:42:14 +01002043 struct drm_i915_private *i915 = to_i915(obj->base.dev);
Chris Wilson275f0392016-10-24 13:42:14 +01002044
Chris Wilson349f2cc2016-04-13 17:35:12 +01002045 /* Serialisation between user GTT access and our code depends upon
2046 * revoking the CPU's PTE whilst the mutex is held. The next user
2047 * pagefault then has to wait until we release the mutex.
Chris Wilson9c870d02016-10-24 13:42:15 +01002048 *
2049 * Note that RPM complicates somewhat by adding an additional
2050 * requirement that operations to the GGTT be made holding the RPM
2051 * wakeref.
Chris Wilson349f2cc2016-04-13 17:35:12 +01002052 */
Chris Wilson275f0392016-10-24 13:42:14 +01002053 lockdep_assert_held(&i915->drm.struct_mutex);
Chris Wilson9c870d02016-10-24 13:42:15 +01002054 intel_runtime_pm_get(i915);
Chris Wilson349f2cc2016-04-13 17:35:12 +01002055
Chris Wilson3594a3e2016-10-24 13:42:16 +01002056 if (list_empty(&obj->userfault_link))
Chris Wilson9c870d02016-10-24 13:42:15 +01002057 goto out;
Chris Wilson901782b2009-07-10 08:18:50 +01002058
Chris Wilson3594a3e2016-10-24 13:42:16 +01002059 list_del_init(&obj->userfault_link);
David Herrmann6796cb12014-01-03 14:24:19 +01002060 drm_vma_node_unmap(&obj->base.vma_node,
2061 obj->base.dev->anon_inode->i_mapping);
Chris Wilson349f2cc2016-04-13 17:35:12 +01002062
2063 /* Ensure that the CPU's PTE are revoked and there are not outstanding
2064 * memory transactions from userspace before we return. The TLB
2065 * flushing implied above by changing the PTE above *should* be
2066 * sufficient, an extra barrier here just provides us with a bit
2067 * of paranoid documentation about our requirement to serialise
2068 * memory writes before touching registers / GSM.
2069 */
2070 wmb();
Chris Wilson9c870d02016-10-24 13:42:15 +01002071
2072out:
2073 intel_runtime_pm_put(i915);
Chris Wilson901782b2009-07-10 08:18:50 +01002074}
2075
Chris Wilson7c108fd2016-10-24 13:42:18 +01002076void i915_gem_runtime_suspend(struct drm_i915_private *dev_priv)
Chris Wilsoneedd10f2014-06-16 08:57:44 +01002077{
Chris Wilson3594a3e2016-10-24 13:42:16 +01002078 struct drm_i915_gem_object *obj, *on;
Chris Wilson7c108fd2016-10-24 13:42:18 +01002079 int i;
Chris Wilsoneedd10f2014-06-16 08:57:44 +01002080
Chris Wilson3594a3e2016-10-24 13:42:16 +01002081 /*
2082 * Only called during RPM suspend. All users of the userfault_list
2083 * must be holding an RPM wakeref to ensure that this can not
2084 * run concurrently with themselves (and use the struct_mutex for
2085 * protection between themselves).
2086 */
2087
2088 list_for_each_entry_safe(obj, on,
2089 &dev_priv->mm.userfault_list, userfault_link) {
Chris Wilson275f0392016-10-24 13:42:14 +01002090 list_del_init(&obj->userfault_link);
Chris Wilson275f0392016-10-24 13:42:14 +01002091 drm_vma_node_unmap(&obj->base.vma_node,
2092 obj->base.dev->anon_inode->i_mapping);
Chris Wilson275f0392016-10-24 13:42:14 +01002093 }
Chris Wilson7c108fd2016-10-24 13:42:18 +01002094
2095 /* The fence will be lost when the device powers down. If any were
2096 * in use by hardware (i.e. they are pinned), we should not be powering
2097 * down! All other fences will be reacquired by the user upon waking.
2098 */
2099 for (i = 0; i < dev_priv->num_fence_regs; i++) {
2100 struct drm_i915_fence_reg *reg = &dev_priv->fence_regs[i];
2101
2102 if (WARN_ON(reg->pin_count))
2103 continue;
2104
2105 if (!reg->vma)
2106 continue;
2107
2108 GEM_BUG_ON(!list_empty(&reg->vma->obj->userfault_link));
2109 reg->dirty = true;
2110 }
Chris Wilsoneedd10f2014-06-16 08:57:44 +01002111}
2112
Chris Wilsonad1a7d22016-08-04 16:32:27 +01002113/**
2114 * i915_gem_get_ggtt_size - return required global GTT size for an object
Chris Wilsona9f14812016-08-04 16:32:28 +01002115 * @dev_priv: i915 device
Chris Wilsonad1a7d22016-08-04 16:32:27 +01002116 * @size: object size
2117 * @tiling_mode: tiling mode
2118 *
2119 * Return the required global GTT size for an object, taking into account
2120 * potential fence register mapping.
2121 */
Chris Wilsona9f14812016-08-04 16:32:28 +01002122u64 i915_gem_get_ggtt_size(struct drm_i915_private *dev_priv,
2123 u64 size, int tiling_mode)
Chris Wilson92b88ae2010-11-09 11:47:32 +00002124{
Chris Wilsonad1a7d22016-08-04 16:32:27 +01002125 u64 ggtt_size;
Chris Wilson92b88ae2010-11-09 11:47:32 +00002126
Chris Wilsonad1a7d22016-08-04 16:32:27 +01002127 GEM_BUG_ON(size == 0);
2128
Chris Wilsona9f14812016-08-04 16:32:28 +01002129 if (INTEL_GEN(dev_priv) >= 4 ||
Chris Wilsone28f8712011-07-18 13:11:49 -07002130 tiling_mode == I915_TILING_NONE)
2131 return size;
Chris Wilson92b88ae2010-11-09 11:47:32 +00002132
2133 /* Previous chips need a power-of-two fence region when tiling */
Chris Wilsona9f14812016-08-04 16:32:28 +01002134 if (IS_GEN3(dev_priv))
Chris Wilsonad1a7d22016-08-04 16:32:27 +01002135 ggtt_size = 1024*1024;
Chris Wilson92b88ae2010-11-09 11:47:32 +00002136 else
Chris Wilsonad1a7d22016-08-04 16:32:27 +01002137 ggtt_size = 512*1024;
Chris Wilson92b88ae2010-11-09 11:47:32 +00002138
Chris Wilsonad1a7d22016-08-04 16:32:27 +01002139 while (ggtt_size < size)
2140 ggtt_size <<= 1;
Chris Wilson92b88ae2010-11-09 11:47:32 +00002141
Chris Wilsonad1a7d22016-08-04 16:32:27 +01002142 return ggtt_size;
Chris Wilson92b88ae2010-11-09 11:47:32 +00002143}
2144
Jesse Barnesde151cf2008-11-12 10:03:55 -08002145/**
Chris Wilsonad1a7d22016-08-04 16:32:27 +01002146 * i915_gem_get_ggtt_alignment - return required global GTT alignment
Chris Wilsona9f14812016-08-04 16:32:28 +01002147 * @dev_priv: i915 device
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01002148 * @size: object size
2149 * @tiling_mode: tiling mode
Chris Wilsonad1a7d22016-08-04 16:32:27 +01002150 * @fenced: is fenced alignment required or not
Jesse Barnesde151cf2008-11-12 10:03:55 -08002151 *
Chris Wilsonad1a7d22016-08-04 16:32:27 +01002152 * Return the required global GTT alignment for an object, taking into account
Daniel Vetter5e783302010-11-14 22:32:36 +01002153 * potential fence register mapping.
Jesse Barnesde151cf2008-11-12 10:03:55 -08002154 */
Chris Wilsona9f14812016-08-04 16:32:28 +01002155u64 i915_gem_get_ggtt_alignment(struct drm_i915_private *dev_priv, u64 size,
Chris Wilsonad1a7d22016-08-04 16:32:27 +01002156 int tiling_mode, bool fenced)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002157{
Chris Wilsonad1a7d22016-08-04 16:32:27 +01002158 GEM_BUG_ON(size == 0);
2159
Jesse Barnesde151cf2008-11-12 10:03:55 -08002160 /*
2161 * Minimum alignment is 4k (GTT page size), but might be greater
2162 * if a fence register is needed for the object.
2163 */
Chris Wilsona9f14812016-08-04 16:32:28 +01002164 if (INTEL_GEN(dev_priv) >= 4 || (!fenced && IS_G33(dev_priv)) ||
Chris Wilsone28f8712011-07-18 13:11:49 -07002165 tiling_mode == I915_TILING_NONE)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002166 return 4096;
2167
2168 /*
2169 * Previous chips need to be aligned to the size of the smallest
2170 * fence register that can contain the object.
2171 */
Chris Wilsona9f14812016-08-04 16:32:28 +01002172 return i915_gem_get_ggtt_size(dev_priv, size, tiling_mode);
Chris Wilsona00b10c2010-09-24 21:15:47 +01002173}
2174
Chris Wilsond8cb5082012-08-11 15:41:03 +01002175static int i915_gem_object_create_mmap_offset(struct drm_i915_gem_object *obj)
2176{
Chris Wilsonfac5e232016-07-04 11:34:36 +01002177 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
Chris Wilsonf3f61842016-08-05 10:14:14 +01002178 int err;
Chris Wilsond8cb5082012-08-11 15:41:03 +01002179
Chris Wilsonf3f61842016-08-05 10:14:14 +01002180 err = drm_gem_create_mmap_offset(&obj->base);
2181 if (!err)
2182 return 0;
Daniel Vetterda494d72012-12-20 15:11:16 +01002183
Chris Wilsonf3f61842016-08-05 10:14:14 +01002184 /* We can idle the GPU locklessly to flush stale objects, but in order
2185 * to claim that space for ourselves, we need to take the big
2186 * struct_mutex to free the requests+objects and allocate our slot.
Chris Wilsond8cb5082012-08-11 15:41:03 +01002187 */
Chris Wilsonea746f32016-09-09 14:11:49 +01002188 err = i915_gem_wait_for_idle(dev_priv, I915_WAIT_INTERRUPTIBLE);
Chris Wilsonf3f61842016-08-05 10:14:14 +01002189 if (err)
2190 return err;
Chris Wilsond8cb5082012-08-11 15:41:03 +01002191
Chris Wilsonf3f61842016-08-05 10:14:14 +01002192 err = i915_mutex_lock_interruptible(&dev_priv->drm);
2193 if (!err) {
2194 i915_gem_retire_requests(dev_priv);
2195 err = drm_gem_create_mmap_offset(&obj->base);
2196 mutex_unlock(&dev_priv->drm.struct_mutex);
2197 }
Daniel Vetterda494d72012-12-20 15:11:16 +01002198
Chris Wilsonf3f61842016-08-05 10:14:14 +01002199 return err;
Chris Wilsond8cb5082012-08-11 15:41:03 +01002200}
2201
2202static void i915_gem_object_free_mmap_offset(struct drm_i915_gem_object *obj)
2203{
Chris Wilsond8cb5082012-08-11 15:41:03 +01002204 drm_gem_free_mmap_offset(&obj->base);
2205}
2206
Dave Airlieda6b51d2014-12-24 13:11:17 +10002207int
Dave Airlieff72145b2011-02-07 12:16:14 +10002208i915_gem_mmap_gtt(struct drm_file *file,
2209 struct drm_device *dev,
Dave Airlieda6b51d2014-12-24 13:11:17 +10002210 uint32_t handle,
Dave Airlieff72145b2011-02-07 12:16:14 +10002211 uint64_t *offset)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002212{
Chris Wilson05394f32010-11-08 19:18:58 +00002213 struct drm_i915_gem_object *obj;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002214 int ret;
2215
Chris Wilson03ac0642016-07-20 13:31:51 +01002216 obj = i915_gem_object_lookup(file, handle);
Chris Wilsonf3f61842016-08-05 10:14:14 +01002217 if (!obj)
2218 return -ENOENT;
Chris Wilsonab182822009-09-22 18:46:17 +01002219
Chris Wilsond8cb5082012-08-11 15:41:03 +01002220 ret = i915_gem_object_create_mmap_offset(obj);
Chris Wilsonf3f61842016-08-05 10:14:14 +01002221 if (ret == 0)
2222 *offset = drm_vma_node_offset_addr(&obj->base.vma_node);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002223
Chris Wilsonf3f61842016-08-05 10:14:14 +01002224 i915_gem_object_put_unlocked(obj);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01002225 return ret;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002226}
2227
Dave Airlieff72145b2011-02-07 12:16:14 +10002228/**
2229 * i915_gem_mmap_gtt_ioctl - prepare an object for GTT mmap'ing
2230 * @dev: DRM device
2231 * @data: GTT mapping ioctl data
2232 * @file: GEM object info
2233 *
2234 * Simply returns the fake offset to userspace so it can mmap it.
2235 * The mmap call will end up in drm_gem_mmap(), which will set things
2236 * up so we can get faults in the handler above.
2237 *
2238 * The fault handler will take care of binding the object into the GTT
2239 * (since it may have been evicted to make room for something), allocating
2240 * a fence register, and mapping the appropriate aperture address into
2241 * userspace.
2242 */
2243int
2244i915_gem_mmap_gtt_ioctl(struct drm_device *dev, void *data,
2245 struct drm_file *file)
2246{
2247 struct drm_i915_gem_mmap_gtt *args = data;
2248
Dave Airlieda6b51d2014-12-24 13:11:17 +10002249 return i915_gem_mmap_gtt(file, dev, args->handle, &args->offset);
Dave Airlieff72145b2011-02-07 12:16:14 +10002250}
2251
Daniel Vetter225067e2012-08-20 10:23:20 +02002252/* Immediately discard the backing storage */
2253static void
2254i915_gem_object_truncate(struct drm_i915_gem_object *obj)
Chris Wilsone5281cc2010-10-28 13:45:36 +01002255{
Chris Wilson4d6294bf2012-08-11 15:41:05 +01002256 i915_gem_object_free_mmap_offset(obj);
Daniel Vetter1286ff72012-05-10 15:25:09 +02002257
Chris Wilson4d6294bf2012-08-11 15:41:05 +01002258 if (obj->base.filp == NULL)
2259 return;
2260
Daniel Vetter225067e2012-08-20 10:23:20 +02002261 /* Our goal here is to return as much of the memory as
2262 * is possible back to the system as we are called from OOM.
2263 * To do this we must instruct the shmfs to drop all of its
2264 * backing pages, *now*.
Chris Wilsone5281cc2010-10-28 13:45:36 +01002265 */
Chris Wilson55372522014-03-25 13:23:06 +00002266 shmem_truncate_range(file_inode(obj->base.filp), 0, (loff_t)-1);
Daniel Vetter225067e2012-08-20 10:23:20 +02002267 obj->madv = __I915_MADV_PURGED;
Chris Wilsone5281cc2010-10-28 13:45:36 +01002268}
Chris Wilsone5281cc2010-10-28 13:45:36 +01002269
Chris Wilson55372522014-03-25 13:23:06 +00002270/* Try to discard unwanted pages */
2271static void
2272i915_gem_object_invalidate(struct drm_i915_gem_object *obj)
Daniel Vetter225067e2012-08-20 10:23:20 +02002273{
Chris Wilson55372522014-03-25 13:23:06 +00002274 struct address_space *mapping;
2275
2276 switch (obj->madv) {
2277 case I915_MADV_DONTNEED:
2278 i915_gem_object_truncate(obj);
2279 case __I915_MADV_PURGED:
2280 return;
2281 }
2282
2283 if (obj->base.filp == NULL)
2284 return;
2285
Al Viro93c76a32015-12-04 23:45:44 -05002286 mapping = obj->base.filp->f_mapping,
Chris Wilson55372522014-03-25 13:23:06 +00002287 invalidate_mapping_pages(mapping, 0, (loff_t)-1);
Chris Wilsone5281cc2010-10-28 13:45:36 +01002288}
2289
Chris Wilson5cdf5882010-09-27 15:51:07 +01002290static void
Chris Wilson05394f32010-11-08 19:18:58 +00002291i915_gem_object_put_pages_gtt(struct drm_i915_gem_object *obj)
Eric Anholt673a3942008-07-30 12:06:12 -07002292{
Dave Gordon85d12252016-05-20 11:54:06 +01002293 struct sgt_iter sgt_iter;
2294 struct page *page;
Imre Deak90797e62013-02-18 19:28:03 +02002295 int ret;
Daniel Vetter1286ff72012-05-10 15:25:09 +02002296
Chris Wilson05394f32010-11-08 19:18:58 +00002297 BUG_ON(obj->madv == __I915_MADV_PURGED);
Eric Anholt856fa192009-03-19 14:10:50 -07002298
Chris Wilson6c085a72012-08-20 11:40:46 +02002299 ret = i915_gem_object_set_to_cpu_domain(obj, true);
Chris Wilsonf4457ae2016-04-13 17:35:08 +01002300 if (WARN_ON(ret)) {
Chris Wilson6c085a72012-08-20 11:40:46 +02002301 /* In the event of a disaster, abandon all caches and
2302 * hope for the best.
2303 */
Chris Wilson2c225692013-08-09 12:26:45 +01002304 i915_gem_clflush_object(obj, true);
Chris Wilson6c085a72012-08-20 11:40:46 +02002305 obj->base.read_domains = obj->base.write_domain = I915_GEM_DOMAIN_CPU;
2306 }
2307
Imre Deake2273302015-07-09 12:59:05 +03002308 i915_gem_gtt_finish_object(obj);
2309
Daniel Vetter6dacfd22011-09-12 21:30:02 +02002310 if (i915_gem_object_needs_bit17_swizzle(obj))
Eric Anholt280b7132009-03-12 16:56:27 -07002311 i915_gem_object_save_bit_17_swizzle(obj);
2312
Chris Wilson05394f32010-11-08 19:18:58 +00002313 if (obj->madv == I915_MADV_DONTNEED)
2314 obj->dirty = 0;
Chris Wilson3ef94da2009-09-14 16:50:29 +01002315
Dave Gordon85d12252016-05-20 11:54:06 +01002316 for_each_sgt_page(page, sgt_iter, obj->pages) {
Chris Wilson05394f32010-11-08 19:18:58 +00002317 if (obj->dirty)
Chris Wilson9da3da62012-06-01 15:20:22 +01002318 set_page_dirty(page);
Chris Wilson3ef94da2009-09-14 16:50:29 +01002319
Chris Wilson05394f32010-11-08 19:18:58 +00002320 if (obj->madv == I915_MADV_WILLNEED)
Chris Wilson9da3da62012-06-01 15:20:22 +01002321 mark_page_accessed(page);
Chris Wilson3ef94da2009-09-14 16:50:29 +01002322
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002323 put_page(page);
Chris Wilson3ef94da2009-09-14 16:50:29 +01002324 }
Chris Wilson05394f32010-11-08 19:18:58 +00002325 obj->dirty = 0;
Eric Anholt673a3942008-07-30 12:06:12 -07002326
Chris Wilson9da3da62012-06-01 15:20:22 +01002327 sg_free_table(obj->pages);
2328 kfree(obj->pages);
Chris Wilson37e680a2012-06-07 15:38:42 +01002329}
2330
Chris Wilsondd624af2013-01-15 12:39:35 +00002331int
Chris Wilson37e680a2012-06-07 15:38:42 +01002332i915_gem_object_put_pages(struct drm_i915_gem_object *obj)
2333{
2334 const struct drm_i915_gem_object_ops *ops = obj->ops;
2335
Chris Wilson2f745ad2012-09-04 21:02:58 +01002336 if (obj->pages == NULL)
Chris Wilson37e680a2012-06-07 15:38:42 +01002337 return 0;
2338
Chris Wilsona5570172012-09-04 21:02:54 +01002339 if (obj->pages_pin_count)
2340 return -EBUSY;
2341
Chris Wilson15717de2016-08-04 07:52:26 +01002342 GEM_BUG_ON(obj->bind_count);
Ben Widawsky3e123022013-07-31 17:00:04 -07002343
Chris Wilsona2165e32012-12-03 11:49:00 +00002344 /* ->put_pages might need to allocate memory for the bit17 swizzle
2345 * array, hence protect them from being reaped by removing them from gtt
2346 * lists early. */
Ben Widawsky35c20a62013-05-31 11:28:48 -07002347 list_del(&obj->global_list);
Chris Wilsona2165e32012-12-03 11:49:00 +00002348
Chris Wilson0a798eb2016-04-08 12:11:11 +01002349 if (obj->mapping) {
Chris Wilson4b30cb22016-08-18 17:16:42 +01002350 void *ptr;
2351
2352 ptr = ptr_mask_bits(obj->mapping);
2353 if (is_vmalloc_addr(ptr))
2354 vunmap(ptr);
Chris Wilsonfb8621d2016-04-08 12:11:14 +01002355 else
Chris Wilson4b30cb22016-08-18 17:16:42 +01002356 kunmap(kmap_to_page(ptr));
2357
Chris Wilson0a798eb2016-04-08 12:11:11 +01002358 obj->mapping = NULL;
2359 }
2360
Chris Wilson37e680a2012-06-07 15:38:42 +01002361 ops->put_pages(obj);
Chris Wilson05394f32010-11-08 19:18:58 +00002362 obj->pages = NULL;
Chris Wilson6c085a72012-08-20 11:40:46 +02002363
Chris Wilson55372522014-03-25 13:23:06 +00002364 i915_gem_object_invalidate(obj);
Chris Wilson6c085a72012-08-20 11:40:46 +02002365
2366 return 0;
2367}
2368
Chris Wilson4ff340f02016-10-18 13:02:50 +01002369static unsigned int swiotlb_max_size(void)
Chris Wilson871dfbd2016-10-11 09:20:21 +01002370{
2371#if IS_ENABLED(CONFIG_SWIOTLB)
2372 return rounddown(swiotlb_nr_tbl() << IO_TLB_SHIFT, PAGE_SIZE);
2373#else
2374 return 0;
2375#endif
2376}
2377
Chris Wilson37e680a2012-06-07 15:38:42 +01002378static int
Chris Wilson6c085a72012-08-20 11:40:46 +02002379i915_gem_object_get_pages_gtt(struct drm_i915_gem_object *obj)
Eric Anholt673a3942008-07-30 12:06:12 -07002380{
Chris Wilsonfac5e232016-07-04 11:34:36 +01002381 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
Eric Anholt673a3942008-07-30 12:06:12 -07002382 int page_count, i;
2383 struct address_space *mapping;
Chris Wilson9da3da62012-06-01 15:20:22 +01002384 struct sg_table *st;
2385 struct scatterlist *sg;
Dave Gordon85d12252016-05-20 11:54:06 +01002386 struct sgt_iter sgt_iter;
Eric Anholt673a3942008-07-30 12:06:12 -07002387 struct page *page;
Imre Deak90797e62013-02-18 19:28:03 +02002388 unsigned long last_pfn = 0; /* suppress gcc warning */
Chris Wilson4ff340f02016-10-18 13:02:50 +01002389 unsigned int max_segment;
Imre Deake2273302015-07-09 12:59:05 +03002390 int ret;
Chris Wilson6c085a72012-08-20 11:40:46 +02002391 gfp_t gfp;
Eric Anholt673a3942008-07-30 12:06:12 -07002392
Chris Wilson6c085a72012-08-20 11:40:46 +02002393 /* Assert that the object is not currently in any GPU domain. As it
2394 * wasn't in the GTT, there shouldn't be any way it could have been in
2395 * a GPU cache
2396 */
2397 BUG_ON(obj->base.read_domains & I915_GEM_GPU_DOMAINS);
2398 BUG_ON(obj->base.write_domain & I915_GEM_GPU_DOMAINS);
2399
Chris Wilson871dfbd2016-10-11 09:20:21 +01002400 max_segment = swiotlb_max_size();
2401 if (!max_segment)
Chris Wilson4ff340f02016-10-18 13:02:50 +01002402 max_segment = rounddown(UINT_MAX, PAGE_SIZE);
Chris Wilson871dfbd2016-10-11 09:20:21 +01002403
Chris Wilson9da3da62012-06-01 15:20:22 +01002404 st = kmalloc(sizeof(*st), GFP_KERNEL);
2405 if (st == NULL)
Eric Anholt673a3942008-07-30 12:06:12 -07002406 return -ENOMEM;
2407
Chris Wilson9da3da62012-06-01 15:20:22 +01002408 page_count = obj->base.size / PAGE_SIZE;
2409 if (sg_alloc_table(st, page_count, GFP_KERNEL)) {
Chris Wilson9da3da62012-06-01 15:20:22 +01002410 kfree(st);
2411 return -ENOMEM;
2412 }
2413
2414 /* Get the list of pages out of our struct file. They'll be pinned
2415 * at this point until we release them.
2416 *
2417 * Fail silently without starting the shrinker
2418 */
Al Viro93c76a32015-12-04 23:45:44 -05002419 mapping = obj->base.filp->f_mapping;
Michal Hockoc62d2552015-11-06 16:28:49 -08002420 gfp = mapping_gfp_constraint(mapping, ~(__GFP_IO | __GFP_RECLAIM));
Mel Gormand0164ad2015-11-06 16:28:21 -08002421 gfp |= __GFP_NORETRY | __GFP_NOWARN;
Imre Deak90797e62013-02-18 19:28:03 +02002422 sg = st->sgl;
2423 st->nents = 0;
2424 for (i = 0; i < page_count; i++) {
Chris Wilson6c085a72012-08-20 11:40:46 +02002425 page = shmem_read_mapping_page_gfp(mapping, i, gfp);
2426 if (IS_ERR(page)) {
Chris Wilson21ab4e72014-09-09 11:16:08 +01002427 i915_gem_shrink(dev_priv,
2428 page_count,
2429 I915_SHRINK_BOUND |
2430 I915_SHRINK_UNBOUND |
2431 I915_SHRINK_PURGEABLE);
Chris Wilson6c085a72012-08-20 11:40:46 +02002432 page = shmem_read_mapping_page_gfp(mapping, i, gfp);
2433 }
2434 if (IS_ERR(page)) {
2435 /* We've tried hard to allocate the memory by reaping
2436 * our own buffer, now let the real VM do its job and
2437 * go down in flames if truly OOM.
2438 */
David Herrmannf461d1b2014-05-25 14:34:10 +02002439 page = shmem_read_mapping_page(mapping, i);
Imre Deake2273302015-07-09 12:59:05 +03002440 if (IS_ERR(page)) {
2441 ret = PTR_ERR(page);
Chris Wilson6c085a72012-08-20 11:40:46 +02002442 goto err_pages;
Imre Deake2273302015-07-09 12:59:05 +03002443 }
Chris Wilson6c085a72012-08-20 11:40:46 +02002444 }
Chris Wilson871dfbd2016-10-11 09:20:21 +01002445 if (!i ||
2446 sg->length >= max_segment ||
2447 page_to_pfn(page) != last_pfn + 1) {
Imre Deak90797e62013-02-18 19:28:03 +02002448 if (i)
2449 sg = sg_next(sg);
2450 st->nents++;
2451 sg_set_page(sg, page, PAGE_SIZE, 0);
2452 } else {
2453 sg->length += PAGE_SIZE;
2454 }
2455 last_pfn = page_to_pfn(page);
Daniel Vetter3bbbe702013-10-07 17:15:45 -03002456
2457 /* Check that the i965g/gm workaround works. */
2458 WARN_ON((gfp & __GFP_DMA32) && (last_pfn >= 0x00100000UL));
Eric Anholt673a3942008-07-30 12:06:12 -07002459 }
Chris Wilson871dfbd2016-10-11 09:20:21 +01002460 if (sg) /* loop terminated early; short sg table */
Konrad Rzeszutek Wilk426729d2013-06-24 11:47:48 -04002461 sg_mark_end(sg);
Chris Wilson74ce6b62012-10-19 15:51:06 +01002462 obj->pages = st;
2463
Imre Deake2273302015-07-09 12:59:05 +03002464 ret = i915_gem_gtt_prepare_object(obj);
2465 if (ret)
2466 goto err_pages;
2467
Eric Anholt673a3942008-07-30 12:06:12 -07002468 if (i915_gem_object_needs_bit17_swizzle(obj))
2469 i915_gem_object_do_bit_17_swizzle(obj);
2470
Chris Wilson3e510a82016-08-05 10:14:23 +01002471 if (i915_gem_object_is_tiled(obj) &&
Daniel Vetter656bfa32014-11-20 09:26:30 +01002472 dev_priv->quirks & QUIRK_PIN_SWIZZLED_PAGES)
2473 i915_gem_object_pin_pages(obj);
2474
Eric Anholt673a3942008-07-30 12:06:12 -07002475 return 0;
2476
2477err_pages:
Imre Deak90797e62013-02-18 19:28:03 +02002478 sg_mark_end(sg);
Dave Gordon85d12252016-05-20 11:54:06 +01002479 for_each_sgt_page(page, sgt_iter, st)
2480 put_page(page);
Chris Wilson9da3da62012-06-01 15:20:22 +01002481 sg_free_table(st);
2482 kfree(st);
Chris Wilson0820baf2014-03-25 13:23:03 +00002483
2484 /* shmemfs first checks if there is enough memory to allocate the page
2485 * and reports ENOSPC should there be insufficient, along with the usual
2486 * ENOMEM for a genuine allocation failure.
2487 *
2488 * We use ENOSPC in our driver to mean that we have run out of aperture
2489 * space and so want to translate the error from shmemfs back to our
2490 * usual understanding of ENOMEM.
2491 */
Imre Deake2273302015-07-09 12:59:05 +03002492 if (ret == -ENOSPC)
2493 ret = -ENOMEM;
2494
2495 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07002496}
2497
Chris Wilson37e680a2012-06-07 15:38:42 +01002498/* Ensure that the associated pages are gathered from the backing storage
2499 * and pinned into our object. i915_gem_object_get_pages() may be called
2500 * multiple times before they are released by a single call to
2501 * i915_gem_object_put_pages() - once the pages are no longer referenced
2502 * either as a result of memory pressure (reaping pages under the shrinker)
2503 * or as the object is itself released.
2504 */
2505int
2506i915_gem_object_get_pages(struct drm_i915_gem_object *obj)
2507{
Chris Wilsonfac5e232016-07-04 11:34:36 +01002508 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
Chris Wilson37e680a2012-06-07 15:38:42 +01002509 const struct drm_i915_gem_object_ops *ops = obj->ops;
2510 int ret;
2511
Chris Wilson2f745ad2012-09-04 21:02:58 +01002512 if (obj->pages)
Chris Wilson37e680a2012-06-07 15:38:42 +01002513 return 0;
2514
Chris Wilson43e28f02013-01-08 10:53:09 +00002515 if (obj->madv != I915_MADV_WILLNEED) {
Chris Wilsonbd9b6a42014-02-10 09:03:50 +00002516 DRM_DEBUG("Attempting to obtain a purgeable object\n");
Chris Wilson8c99e572014-01-31 11:34:58 +00002517 return -EFAULT;
Chris Wilson43e28f02013-01-08 10:53:09 +00002518 }
2519
Chris Wilsona5570172012-09-04 21:02:54 +01002520 BUG_ON(obj->pages_pin_count);
2521
Chris Wilson37e680a2012-06-07 15:38:42 +01002522 ret = ops->get_pages(obj);
2523 if (ret)
2524 return ret;
2525
Ben Widawsky35c20a62013-05-31 11:28:48 -07002526 list_add_tail(&obj->global_list, &dev_priv->mm.unbound_list);
Chris Wilsonee286372015-04-07 16:20:25 +01002527
2528 obj->get_page.sg = obj->pages->sgl;
2529 obj->get_page.last = 0;
2530
Chris Wilson37e680a2012-06-07 15:38:42 +01002531 return 0;
Eric Anholt673a3942008-07-30 12:06:12 -07002532}
2533
Dave Gordondd6034c2016-05-20 11:54:04 +01002534/* The 'mapping' part of i915_gem_object_pin_map() below */
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002535static void *i915_gem_object_map(const struct drm_i915_gem_object *obj,
2536 enum i915_map_type type)
Dave Gordondd6034c2016-05-20 11:54:04 +01002537{
2538 unsigned long n_pages = obj->base.size >> PAGE_SHIFT;
2539 struct sg_table *sgt = obj->pages;
Dave Gordon85d12252016-05-20 11:54:06 +01002540 struct sgt_iter sgt_iter;
2541 struct page *page;
Dave Gordonb338fa42016-05-20 11:54:05 +01002542 struct page *stack_pages[32];
2543 struct page **pages = stack_pages;
Dave Gordondd6034c2016-05-20 11:54:04 +01002544 unsigned long i = 0;
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002545 pgprot_t pgprot;
Dave Gordondd6034c2016-05-20 11:54:04 +01002546 void *addr;
2547
2548 /* A single page can always be kmapped */
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002549 if (n_pages == 1 && type == I915_MAP_WB)
Dave Gordondd6034c2016-05-20 11:54:04 +01002550 return kmap(sg_page(sgt->sgl));
2551
Dave Gordonb338fa42016-05-20 11:54:05 +01002552 if (n_pages > ARRAY_SIZE(stack_pages)) {
2553 /* Too big for stack -- allocate temporary array instead */
2554 pages = drm_malloc_gfp(n_pages, sizeof(*pages), GFP_TEMPORARY);
2555 if (!pages)
2556 return NULL;
2557 }
Dave Gordondd6034c2016-05-20 11:54:04 +01002558
Dave Gordon85d12252016-05-20 11:54:06 +01002559 for_each_sgt_page(page, sgt_iter, sgt)
2560 pages[i++] = page;
Dave Gordondd6034c2016-05-20 11:54:04 +01002561
2562 /* Check that we have the expected number of pages */
2563 GEM_BUG_ON(i != n_pages);
2564
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002565 switch (type) {
2566 case I915_MAP_WB:
2567 pgprot = PAGE_KERNEL;
2568 break;
2569 case I915_MAP_WC:
2570 pgprot = pgprot_writecombine(PAGE_KERNEL_IO);
2571 break;
2572 }
2573 addr = vmap(pages, n_pages, 0, pgprot);
Dave Gordondd6034c2016-05-20 11:54:04 +01002574
Dave Gordonb338fa42016-05-20 11:54:05 +01002575 if (pages != stack_pages)
2576 drm_free_large(pages);
Dave Gordondd6034c2016-05-20 11:54:04 +01002577
2578 return addr;
2579}
2580
2581/* get, pin, and map the pages of the object into kernel space */
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002582void *i915_gem_object_pin_map(struct drm_i915_gem_object *obj,
2583 enum i915_map_type type)
Chris Wilson0a798eb2016-04-08 12:11:11 +01002584{
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002585 enum i915_map_type has_type;
2586 bool pinned;
2587 void *ptr;
Chris Wilson0a798eb2016-04-08 12:11:11 +01002588 int ret;
2589
2590 lockdep_assert_held(&obj->base.dev->struct_mutex);
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002591 GEM_BUG_ON(!i915_gem_object_has_struct_page(obj));
Chris Wilson0a798eb2016-04-08 12:11:11 +01002592
2593 ret = i915_gem_object_get_pages(obj);
2594 if (ret)
2595 return ERR_PTR(ret);
2596
2597 i915_gem_object_pin_pages(obj);
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002598 pinned = obj->pages_pin_count > 1;
Chris Wilson0a798eb2016-04-08 12:11:11 +01002599
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002600 ptr = ptr_unpack_bits(obj->mapping, has_type);
2601 if (ptr && has_type != type) {
2602 if (pinned) {
2603 ret = -EBUSY;
2604 goto err;
Chris Wilson0a798eb2016-04-08 12:11:11 +01002605 }
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002606
2607 if (is_vmalloc_addr(ptr))
2608 vunmap(ptr);
2609 else
2610 kunmap(kmap_to_page(ptr));
2611
2612 ptr = obj->mapping = NULL;
Chris Wilson0a798eb2016-04-08 12:11:11 +01002613 }
2614
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002615 if (!ptr) {
2616 ptr = i915_gem_object_map(obj, type);
2617 if (!ptr) {
2618 ret = -ENOMEM;
2619 goto err;
2620 }
2621
2622 obj->mapping = ptr_pack_bits(ptr, type);
2623 }
2624
2625 return ptr;
2626
2627err:
2628 i915_gem_object_unpin_pages(obj);
2629 return ERR_PTR(ret);
Chris Wilson0a798eb2016-04-08 12:11:11 +01002630}
2631
Chris Wilsoncaea7472010-11-12 13:53:37 +00002632static void
Chris Wilsonfa545cb2016-08-04 07:52:35 +01002633i915_gem_object_retire__write(struct i915_gem_active *active,
2634 struct drm_i915_gem_request *request)
Chris Wilsonb4716182015-04-27 13:41:17 +01002635{
Chris Wilsonfa545cb2016-08-04 07:52:35 +01002636 struct drm_i915_gem_object *obj =
2637 container_of(active, struct drm_i915_gem_object, last_write);
Chris Wilsonb4716182015-04-27 13:41:17 +01002638
Rodrigo Vivide152b62015-07-07 16:28:51 -07002639 intel_fb_obj_flush(obj, true, ORIGIN_CS);
Chris Wilsonb4716182015-04-27 13:41:17 +01002640}
2641
2642static void
Chris Wilsonfa545cb2016-08-04 07:52:35 +01002643i915_gem_object_retire__read(struct i915_gem_active *active,
2644 struct drm_i915_gem_request *request)
Chris Wilsoncaea7472010-11-12 13:53:37 +00002645{
Chris Wilsonfa545cb2016-08-04 07:52:35 +01002646 int idx = request->engine->id;
2647 struct drm_i915_gem_object *obj =
2648 container_of(active, struct drm_i915_gem_object, last_read[idx]);
Chris Wilsoncaea7472010-11-12 13:53:37 +00002649
Chris Wilson573adb32016-08-04 16:32:39 +01002650 GEM_BUG_ON(!i915_gem_object_has_active_engine(obj, idx));
Chris Wilsonb4716182015-04-27 13:41:17 +01002651
Chris Wilson573adb32016-08-04 16:32:39 +01002652 i915_gem_object_clear_active(obj, idx);
2653 if (i915_gem_object_is_active(obj))
Chris Wilsonb4716182015-04-27 13:41:17 +01002654 return;
Chris Wilson65ce3022012-07-20 12:41:02 +01002655
Chris Wilson6c246952015-07-27 10:26:26 +01002656 /* Bump our place on the bound list to keep it roughly in LRU order
2657 * so that we don't steal from recently used but inactive objects
2658 * (unless we are forced to ofc!)
2659 */
Chris Wilsonb0decaf2016-08-04 07:52:44 +01002660 if (obj->bind_count)
2661 list_move_tail(&obj->global_list,
2662 &request->i915->mm.bound_list);
Chris Wilsoncaea7472010-11-12 13:53:37 +00002663
Chris Wilsonf8c417c2016-07-20 13:31:53 +01002664 i915_gem_object_put(obj);
Chris Wilsonc8725f32014-03-17 12:21:55 +00002665}
2666
Chris Wilson7b4d3a12016-07-04 08:08:37 +01002667static bool i915_context_is_banned(const struct i915_gem_context *ctx)
Mika Kuoppalabe62acb2013-08-30 16:19:28 +03002668{
Mika Kuoppala44e2c072014-01-30 16:01:15 +02002669 unsigned long elapsed;
Mika Kuoppalabe62acb2013-08-30 16:19:28 +03002670
Mika Kuoppala44e2c072014-01-30 16:01:15 +02002671 if (ctx->hang_stats.banned)
Mika Kuoppalabe62acb2013-08-30 16:19:28 +03002672 return true;
2673
Chris Wilson7b4d3a12016-07-04 08:08:37 +01002674 elapsed = get_seconds() - ctx->hang_stats.guilty_ts;
Chris Wilson676fa572014-12-24 08:13:39 -08002675 if (ctx->hang_stats.ban_period_seconds &&
2676 elapsed <= ctx->hang_stats.ban_period_seconds) {
Chris Wilson7b4d3a12016-07-04 08:08:37 +01002677 DRM_DEBUG("context hanging too fast, banning!\n");
2678 return true;
Mika Kuoppalabe62acb2013-08-30 16:19:28 +03002679 }
2680
2681 return false;
2682}
2683
Chris Wilson7b4d3a12016-07-04 08:08:37 +01002684static void i915_set_reset_status(struct i915_gem_context *ctx,
Mika Kuoppalab6b0fac2014-01-30 19:04:43 +02002685 const bool guilty)
Mika Kuoppalaaa60c662013-06-12 15:13:20 +03002686{
Chris Wilson7b4d3a12016-07-04 08:08:37 +01002687 struct i915_ctx_hang_stats *hs = &ctx->hang_stats;
Mika Kuoppala44e2c072014-01-30 16:01:15 +02002688
2689 if (guilty) {
Chris Wilson7b4d3a12016-07-04 08:08:37 +01002690 hs->banned = i915_context_is_banned(ctx);
Mika Kuoppala44e2c072014-01-30 16:01:15 +02002691 hs->batch_active++;
2692 hs->guilty_ts = get_seconds();
2693 } else {
2694 hs->batch_pending++;
Mika Kuoppalaaa60c662013-06-12 15:13:20 +03002695 }
2696}
2697
Chris Wilson8d9fc7f2014-02-25 17:11:23 +02002698struct drm_i915_gem_request *
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00002699i915_gem_find_active_request(struct intel_engine_cs *engine)
Chris Wilson9375e442010-09-19 12:21:28 +01002700{
Chris Wilson4db080f2013-12-04 11:37:09 +00002701 struct drm_i915_gem_request *request;
Mika Kuoppalaaa60c662013-06-12 15:13:20 +03002702
Chris Wilsonf69a02c2016-07-01 17:23:16 +01002703 /* We are called by the error capture and reset at a random
2704 * point in time. In particular, note that neither is crucially
2705 * ordered with an interrupt. After a hang, the GPU is dead and we
2706 * assume that no more writes can happen (we waited long enough for
2707 * all writes that were in transaction to be flushed) - adding an
2708 * extra delay for a recent interrupt is pointless. Hence, we do
2709 * not need an engine->irq_seqno_barrier() before the seqno reads.
2710 */
Chris Wilsonefdf7c02016-08-04 07:52:33 +01002711 list_for_each_entry(request, &engine->request_list, link) {
Chris Wilsonf69a02c2016-07-01 17:23:16 +01002712 if (i915_gem_request_completed(request))
Chris Wilson4db080f2013-12-04 11:37:09 +00002713 continue;
Mika Kuoppalaaa60c662013-06-12 15:13:20 +03002714
Chris Wilson5590af32016-09-09 14:11:54 +01002715 if (!i915_sw_fence_done(&request->submit))
2716 break;
2717
Mika Kuoppalab6b0fac2014-01-30 19:04:43 +02002718 return request;
Chris Wilson4db080f2013-12-04 11:37:09 +00002719 }
Mika Kuoppalab6b0fac2014-01-30 19:04:43 +02002720
2721 return NULL;
2722}
2723
Chris Wilson821ed7d2016-09-09 14:11:53 +01002724static void reset_request(struct drm_i915_gem_request *request)
Mika Kuoppalab6b0fac2014-01-30 19:04:43 +02002725{
Chris Wilson821ed7d2016-09-09 14:11:53 +01002726 void *vaddr = request->ring->vaddr;
2727 u32 head;
Mika Kuoppalab6b0fac2014-01-30 19:04:43 +02002728
Chris Wilson821ed7d2016-09-09 14:11:53 +01002729 /* As this request likely depends on state from the lost
2730 * context, clear out all the user operations leaving the
2731 * breadcrumb at the end (so we get the fence notifications).
2732 */
2733 head = request->head;
2734 if (request->postfix < head) {
2735 memset(vaddr + head, 0, request->ring->size - head);
2736 head = 0;
2737 }
2738 memset(vaddr + head, 0, request->postfix - head);
Chris Wilson4db080f2013-12-04 11:37:09 +00002739}
2740
Chris Wilson821ed7d2016-09-09 14:11:53 +01002741static void i915_gem_reset_engine(struct intel_engine_cs *engine)
Chris Wilson4db080f2013-12-04 11:37:09 +00002742{
Chris Wilsondcff85c2016-08-05 10:14:11 +01002743 struct drm_i915_gem_request *request;
Chris Wilson821ed7d2016-09-09 14:11:53 +01002744 struct i915_gem_context *incomplete_ctx;
2745 bool ring_hung;
Chris Wilson608c1a52015-09-03 13:01:40 +01002746
Chris Wilson821ed7d2016-09-09 14:11:53 +01002747 if (engine->irq_seqno_barrier)
2748 engine->irq_seqno_barrier(engine);
2749
2750 request = i915_gem_find_active_request(engine);
2751 if (!request)
2752 return;
2753
2754 ring_hung = engine->hangcheck.score >= HANGCHECK_SCORE_RING_HUNG;
Chris Wilson77c60702016-10-04 21:11:29 +01002755 if (engine->hangcheck.seqno != intel_engine_get_seqno(engine))
2756 ring_hung = false;
2757
Chris Wilson821ed7d2016-09-09 14:11:53 +01002758 i915_set_reset_status(request->ctx, ring_hung);
2759 if (!ring_hung)
2760 return;
2761
2762 DRM_DEBUG_DRIVER("resetting %s to restart from tail of request 0x%x\n",
2763 engine->name, request->fence.seqno);
2764
2765 /* Setup the CS to resume from the breadcrumb of the hung request */
2766 engine->reset_hw(engine, request);
2767
2768 /* Users of the default context do not rely on logical state
2769 * preserved between batches. They have to emit full state on
2770 * every batch and so it is safe to execute queued requests following
2771 * the hang.
2772 *
2773 * Other contexts preserve state, now corrupt. We want to skip all
2774 * queued requests that reference the corrupt context.
2775 */
2776 incomplete_ctx = request->ctx;
2777 if (i915_gem_context_is_default(incomplete_ctx))
2778 return;
2779
2780 list_for_each_entry_continue(request, &engine->request_list, link)
2781 if (request->ctx == incomplete_ctx)
2782 reset_request(request);
2783}
2784
2785void i915_gem_reset(struct drm_i915_private *dev_priv)
2786{
2787 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05302788 enum intel_engine_id id;
Chris Wilson821ed7d2016-09-09 14:11:53 +01002789
2790 i915_gem_retire_requests(dev_priv);
2791
Akash Goel3b3f1652016-10-13 22:44:48 +05302792 for_each_engine(engine, dev_priv, id)
Chris Wilson821ed7d2016-09-09 14:11:53 +01002793 i915_gem_reset_engine(engine);
2794
2795 i915_gem_restore_fences(&dev_priv->drm);
Chris Wilsonf2a91d12016-09-21 14:51:06 +01002796
2797 if (dev_priv->gt.awake) {
2798 intel_sanitize_gt_powersave(dev_priv);
2799 intel_enable_gt_powersave(dev_priv);
2800 if (INTEL_GEN(dev_priv) >= 6)
2801 gen6_rps_busy(dev_priv);
2802 }
Chris Wilson821ed7d2016-09-09 14:11:53 +01002803}
2804
2805static void nop_submit_request(struct drm_i915_gem_request *request)
2806{
2807}
2808
2809static void i915_gem_cleanup_engine(struct intel_engine_cs *engine)
2810{
2811 engine->submit_request = nop_submit_request;
Chris Wilson70c2a242016-09-09 14:11:46 +01002812
Chris Wilsonc4b09302016-07-20 09:21:10 +01002813 /* Mark all pending requests as complete so that any concurrent
2814 * (lockless) lookup doesn't try and wait upon the request as we
2815 * reset it.
2816 */
Chris Wilson87b723a2016-08-09 08:37:02 +01002817 intel_engine_init_seqno(engine, engine->last_submitted_seqno);
Chris Wilsonc4b09302016-07-20 09:21:10 +01002818
Ben Widawsky1d62bee2014-01-01 10:15:13 -08002819 /*
Oscar Mateodcb4c122014-11-13 10:28:10 +00002820 * Clear the execlists queue up before freeing the requests, as those
2821 * are the ones that keep the context and ringbuffer backing objects
2822 * pinned in place.
2823 */
Oscar Mateodcb4c122014-11-13 10:28:10 +00002824
Tomas Elf7de1691a2015-10-19 16:32:32 +01002825 if (i915.enable_execlists) {
Chris Wilson70c2a242016-09-09 14:11:46 +01002826 spin_lock(&engine->execlist_lock);
2827 INIT_LIST_HEAD(&engine->execlist_queue);
2828 i915_gem_request_put(engine->execlist_port[0].request);
2829 i915_gem_request_put(engine->execlist_port[1].request);
2830 memset(engine->execlist_port, 0, sizeof(engine->execlist_port));
2831 spin_unlock(&engine->execlist_lock);
Oscar Mateodcb4c122014-11-13 10:28:10 +00002832 }
2833
Chris Wilsonb913b332016-07-13 09:10:31 +01002834 engine->i915->gt.active_engines &= ~intel_engine_flag(engine);
Eric Anholt673a3942008-07-30 12:06:12 -07002835}
2836
Chris Wilson821ed7d2016-09-09 14:11:53 +01002837void i915_gem_set_wedged(struct drm_i915_private *dev_priv)
Eric Anholt673a3942008-07-30 12:06:12 -07002838{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002839 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05302840 enum intel_engine_id id;
Eric Anholt673a3942008-07-30 12:06:12 -07002841
Chris Wilson821ed7d2016-09-09 14:11:53 +01002842 lockdep_assert_held(&dev_priv->drm.struct_mutex);
2843 set_bit(I915_WEDGED, &dev_priv->gpu_error.flags);
Chris Wilson4db080f2013-12-04 11:37:09 +00002844
Chris Wilson821ed7d2016-09-09 14:11:53 +01002845 i915_gem_context_lost(dev_priv);
Akash Goel3b3f1652016-10-13 22:44:48 +05302846 for_each_engine(engine, dev_priv, id)
Chris Wilson821ed7d2016-09-09 14:11:53 +01002847 i915_gem_cleanup_engine(engine);
Chris Wilsonb913b332016-07-13 09:10:31 +01002848 mod_delayed_work(dev_priv->wq, &dev_priv->gt.idle_work, 0);
Chris Wilsondfaae392010-09-22 10:31:52 +01002849
Chris Wilson821ed7d2016-09-09 14:11:53 +01002850 i915_gem_retire_requests(dev_priv);
Eric Anholt673a3942008-07-30 12:06:12 -07002851}
2852
Daniel Vetter75ef9da2010-08-21 00:25:16 +02002853static void
Eric Anholt673a3942008-07-30 12:06:12 -07002854i915_gem_retire_work_handler(struct work_struct *work)
2855{
Chris Wilsonb29c19b2013-09-25 17:34:56 +01002856 struct drm_i915_private *dev_priv =
Chris Wilson67d97da2016-07-04 08:08:31 +01002857 container_of(work, typeof(*dev_priv), gt.retire_work.work);
Chris Wilson91c8a322016-07-05 10:40:23 +01002858 struct drm_device *dev = &dev_priv->drm;
Eric Anholt673a3942008-07-30 12:06:12 -07002859
Chris Wilson891b48c2010-09-29 12:26:37 +01002860 /* Come back later if the device is busy... */
Chris Wilsonb29c19b2013-09-25 17:34:56 +01002861 if (mutex_trylock(&dev->struct_mutex)) {
Chris Wilson67d97da2016-07-04 08:08:31 +01002862 i915_gem_retire_requests(dev_priv);
Chris Wilsonb29c19b2013-09-25 17:34:56 +01002863 mutex_unlock(&dev->struct_mutex);
2864 }
Chris Wilson67d97da2016-07-04 08:08:31 +01002865
2866 /* Keep the retire handler running until we are finally idle.
2867 * We do not need to do this test under locking as in the worst-case
2868 * we queue the retire worker once too often.
2869 */
Chris Wilsonc9615612016-07-09 10:12:06 +01002870 if (READ_ONCE(dev_priv->gt.awake)) {
2871 i915_queue_hangcheck(dev_priv);
Chris Wilson67d97da2016-07-04 08:08:31 +01002872 queue_delayed_work(dev_priv->wq,
2873 &dev_priv->gt.retire_work,
Chris Wilsonbcb45082012-10-05 17:02:57 +01002874 round_jiffies_up_relative(HZ));
Chris Wilsonc9615612016-07-09 10:12:06 +01002875 }
Chris Wilsonb29c19b2013-09-25 17:34:56 +01002876}
Chris Wilson891b48c2010-09-29 12:26:37 +01002877
Chris Wilsonb29c19b2013-09-25 17:34:56 +01002878static void
2879i915_gem_idle_work_handler(struct work_struct *work)
2880{
2881 struct drm_i915_private *dev_priv =
Chris Wilson67d97da2016-07-04 08:08:31 +01002882 container_of(work, typeof(*dev_priv), gt.idle_work.work);
Chris Wilson91c8a322016-07-05 10:40:23 +01002883 struct drm_device *dev = &dev_priv->drm;
Dave Gordonb4ac5af2016-03-24 11:20:38 +00002884 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05302885 enum intel_engine_id id;
Chris Wilson67d97da2016-07-04 08:08:31 +01002886 bool rearm_hangcheck;
2887
2888 if (!READ_ONCE(dev_priv->gt.awake))
2889 return;
2890
2891 if (READ_ONCE(dev_priv->gt.active_engines))
2892 return;
2893
2894 rearm_hangcheck =
2895 cancel_delayed_work_sync(&dev_priv->gpu_error.hangcheck_work);
2896
2897 if (!mutex_trylock(&dev->struct_mutex)) {
2898 /* Currently busy, come back later */
2899 mod_delayed_work(dev_priv->wq,
2900 &dev_priv->gt.idle_work,
2901 msecs_to_jiffies(50));
2902 goto out_rearm;
2903 }
2904
2905 if (dev_priv->gt.active_engines)
2906 goto out_unlock;
Zou Nan haid1b851f2010-05-21 09:08:57 +08002907
Akash Goel3b3f1652016-10-13 22:44:48 +05302908 for_each_engine(engine, dev_priv, id)
Chris Wilson67d97da2016-07-04 08:08:31 +01002909 i915_gem_batch_pool_fini(&engine->batch_pool);
Zou Nan hai852835f2010-05-21 09:08:56 +08002910
Chris Wilson67d97da2016-07-04 08:08:31 +01002911 GEM_BUG_ON(!dev_priv->gt.awake);
2912 dev_priv->gt.awake = false;
2913 rearm_hangcheck = false;
Daniel Vetter30ecad72015-12-09 09:29:36 +01002914
Chris Wilson67d97da2016-07-04 08:08:31 +01002915 if (INTEL_GEN(dev_priv) >= 6)
2916 gen6_rps_idle(dev_priv);
2917 intel_runtime_pm_put(dev_priv);
2918out_unlock:
2919 mutex_unlock(&dev->struct_mutex);
Chris Wilson35c94182015-04-07 16:20:37 +01002920
Chris Wilson67d97da2016-07-04 08:08:31 +01002921out_rearm:
2922 if (rearm_hangcheck) {
2923 GEM_BUG_ON(!dev_priv->gt.awake);
2924 i915_queue_hangcheck(dev_priv);
Chris Wilson35c94182015-04-07 16:20:37 +01002925 }
Eric Anholt673a3942008-07-30 12:06:12 -07002926}
2927
Chris Wilsonb1f788c2016-08-04 07:52:45 +01002928void i915_gem_close_object(struct drm_gem_object *gem, struct drm_file *file)
2929{
2930 struct drm_i915_gem_object *obj = to_intel_bo(gem);
2931 struct drm_i915_file_private *fpriv = file->driver_priv;
2932 struct i915_vma *vma, *vn;
2933
2934 mutex_lock(&obj->base.dev->struct_mutex);
2935 list_for_each_entry_safe(vma, vn, &obj->vma_list, obj_link)
2936 if (vma->vm->file == fpriv)
2937 i915_vma_close(vma);
2938 mutex_unlock(&obj->base.dev->struct_mutex);
2939}
2940
Chris Wilsone95433c2016-10-28 13:58:27 +01002941static unsigned long to_wait_timeout(s64 timeout_ns)
2942{
2943 if (timeout_ns < 0)
2944 return MAX_SCHEDULE_TIMEOUT;
2945
2946 if (timeout_ns == 0)
2947 return 0;
2948
2949 return nsecs_to_jiffies_timeout(timeout_ns);
2950}
2951
Ben Widawsky5816d642012-04-11 11:18:19 -07002952/**
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07002953 * i915_gem_wait_ioctl - implements DRM_IOCTL_I915_GEM_WAIT
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01002954 * @dev: drm device pointer
2955 * @data: ioctl data blob
2956 * @file: drm file pointer
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07002957 *
2958 * Returns 0 if successful, else an error is returned with the remaining time in
2959 * the timeout parameter.
2960 * -ETIME: object is still busy after timeout
2961 * -ERESTARTSYS: signal interrupted the wait
2962 * -ENONENT: object doesn't exist
2963 * Also possible, but rare:
2964 * -EAGAIN: GPU wedged
2965 * -ENOMEM: damn
2966 * -ENODEV: Internal IRQ fail
2967 * -E?: The add request failed
2968 *
2969 * The wait ioctl with a timeout of 0 reimplements the busy ioctl. With any
2970 * non-zero timeout parameter the wait ioctl will wait for the given number of
2971 * nanoseconds on an object becoming unbusy. Since the wait itself does so
2972 * without holding struct_mutex the object may become re-busied before this
2973 * function completes. A similar but shorter * race condition exists in the busy
2974 * ioctl
2975 */
2976int
2977i915_gem_wait_ioctl(struct drm_device *dev, void *data, struct drm_file *file)
2978{
2979 struct drm_i915_gem_wait *args = data;
2980 struct drm_i915_gem_object *obj;
Chris Wilsone95433c2016-10-28 13:58:27 +01002981 ktime_t start;
2982 long ret;
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07002983
Daniel Vetter11b5d512014-09-29 15:31:26 +02002984 if (args->flags != 0)
2985 return -EINVAL;
2986
Chris Wilson03ac0642016-07-20 13:31:51 +01002987 obj = i915_gem_object_lookup(file, args->bo_handle);
Chris Wilson033d5492016-08-05 10:14:17 +01002988 if (!obj)
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07002989 return -ENOENT;
Chris Wilson033d5492016-08-05 10:14:17 +01002990
Chris Wilsone95433c2016-10-28 13:58:27 +01002991 start = ktime_get();
2992
2993 ret = i915_gem_object_wait(obj,
2994 I915_WAIT_INTERRUPTIBLE | I915_WAIT_ALL,
2995 to_wait_timeout(args->timeout_ns),
2996 to_rps_client(file));
2997
2998 if (args->timeout_ns > 0) {
2999 args->timeout_ns -= ktime_to_ns(ktime_sub(ktime_get(), start));
3000 if (args->timeout_ns < 0)
3001 args->timeout_ns = 0;
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07003002 }
3003
Chris Wilson033d5492016-08-05 10:14:17 +01003004 i915_gem_object_put_unlocked(obj);
John Harrisonff865882014-11-24 18:49:28 +00003005 return ret;
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07003006}
3007
Chris Wilson8ef85612016-04-28 09:56:39 +01003008static void __i915_vma_iounmap(struct i915_vma *vma)
3009{
Chris Wilson20dfbde2016-08-04 16:32:30 +01003010 GEM_BUG_ON(i915_vma_is_pinned(vma));
Chris Wilson8ef85612016-04-28 09:56:39 +01003011
3012 if (vma->iomap == NULL)
3013 return;
3014
3015 io_mapping_unmap(vma->iomap);
3016 vma->iomap = NULL;
3017}
3018
Chris Wilsondf0e9a22016-08-04 07:52:47 +01003019int i915_vma_unbind(struct i915_vma *vma)
Eric Anholt673a3942008-07-30 12:06:12 -07003020{
Ben Widawsky07fe0b12013-07-31 17:00:10 -07003021 struct drm_i915_gem_object *obj = vma->obj;
Chris Wilsonb0decaf2016-08-04 07:52:44 +01003022 unsigned long active;
Chris Wilson43e28f02013-01-08 10:53:09 +00003023 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003024
Chris Wilsonb0decaf2016-08-04 07:52:44 +01003025 /* First wait upon any activity as retiring the request may
3026 * have side-effects such as unpinning or even unbinding this vma.
3027 */
3028 active = i915_vma_get_active(vma);
Chris Wilsondf0e9a22016-08-04 07:52:47 +01003029 if (active) {
Chris Wilsonb0decaf2016-08-04 07:52:44 +01003030 int idx;
3031
Chris Wilsonb1f788c2016-08-04 07:52:45 +01003032 /* When a closed VMA is retired, it is unbound - eek.
3033 * In order to prevent it from being recursively closed,
3034 * take a pin on the vma so that the second unbind is
3035 * aborted.
3036 */
Chris Wilson20dfbde2016-08-04 16:32:30 +01003037 __i915_vma_pin(vma);
Chris Wilsonb1f788c2016-08-04 07:52:45 +01003038
Chris Wilsonb0decaf2016-08-04 07:52:44 +01003039 for_each_active(active, idx) {
3040 ret = i915_gem_active_retire(&vma->last_read[idx],
3041 &vma->vm->dev->struct_mutex);
3042 if (ret)
Chris Wilsonb1f788c2016-08-04 07:52:45 +01003043 break;
Chris Wilsonb0decaf2016-08-04 07:52:44 +01003044 }
3045
Chris Wilson20dfbde2016-08-04 16:32:30 +01003046 __i915_vma_unpin(vma);
Chris Wilsonb1f788c2016-08-04 07:52:45 +01003047 if (ret)
3048 return ret;
3049
Chris Wilsonb0decaf2016-08-04 07:52:44 +01003050 GEM_BUG_ON(i915_vma_is_active(vma));
3051 }
3052
Chris Wilson20dfbde2016-08-04 16:32:30 +01003053 if (i915_vma_is_pinned(vma))
Chris Wilsonb0decaf2016-08-04 07:52:44 +01003054 return -EBUSY;
3055
Chris Wilsonb1f788c2016-08-04 07:52:45 +01003056 if (!drm_mm_node_allocated(&vma->node))
3057 goto destroy;
Ben Widawsky433544b2013-08-13 18:09:06 -07003058
Chris Wilson15717de2016-08-04 07:52:26 +01003059 GEM_BUG_ON(obj->bind_count == 0);
3060 GEM_BUG_ON(!obj->pages);
Chris Wilsonc4670ad2012-08-20 10:23:27 +01003061
Chris Wilson05a20d02016-08-18 17:16:55 +01003062 if (i915_vma_is_map_and_fenceable(vma)) {
Daniel Vetter8b1bc9b2014-02-14 14:06:07 +01003063 /* release the fence reg _after_ flushing */
Chris Wilson49ef5292016-08-18 17:17:00 +01003064 ret = i915_vma_put_fence(vma);
Daniel Vetter8b1bc9b2014-02-14 14:06:07 +01003065 if (ret)
3066 return ret;
Chris Wilson8ef85612016-04-28 09:56:39 +01003067
Chris Wilsoncd3127d2016-08-18 17:17:09 +01003068 /* Force a pagefault for domain tracking on next user access */
3069 i915_gem_release_mmap(obj);
3070
Chris Wilson8ef85612016-04-28 09:56:39 +01003071 __i915_vma_iounmap(vma);
Chris Wilson05a20d02016-08-18 17:16:55 +01003072 vma->flags &= ~I915_VMA_CAN_FENCE;
Daniel Vetter8b1bc9b2014-02-14 14:06:07 +01003073 }
Daniel Vetter96b47b62009-12-15 17:50:00 +01003074
Chris Wilson50e046b2016-08-04 07:52:46 +01003075 if (likely(!vma->vm->closed)) {
3076 trace_i915_vma_unbind(vma);
3077 vma->vm->unbind_vma(vma);
3078 }
Chris Wilson3272db52016-08-04 16:32:32 +01003079 vma->flags &= ~(I915_VMA_GLOBAL_BIND | I915_VMA_LOCAL_BIND);
Ben Widawsky6f65e292013-12-06 14:10:56 -08003080
Chris Wilson50e046b2016-08-04 07:52:46 +01003081 drm_mm_remove_node(&vma->node);
3082 list_move_tail(&vma->vm_link, &vma->vm->unbound_list);
3083
Chris Wilson05a20d02016-08-18 17:16:55 +01003084 if (vma->pages != obj->pages) {
3085 GEM_BUG_ON(!vma->pages);
3086 sg_free_table(vma->pages);
3087 kfree(vma->pages);
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00003088 }
Chris Wilson247177d2016-08-15 10:48:47 +01003089 vma->pages = NULL;
Eric Anholt673a3942008-07-30 12:06:12 -07003090
Ben Widawsky2f633152013-07-17 12:19:03 -07003091 /* Since the unbound list is global, only move to that list if
Daniel Vetterb93dab62013-08-26 11:23:47 +02003092 * no more VMAs exist. */
Chris Wilson15717de2016-08-04 07:52:26 +01003093 if (--obj->bind_count == 0)
3094 list_move_tail(&obj->global_list,
3095 &to_i915(obj->base.dev)->mm.unbound_list);
Eric Anholt673a3942008-07-30 12:06:12 -07003096
Chris Wilson70903c32013-12-04 09:59:09 +00003097 /* And finally now the object is completely decoupled from this vma,
3098 * we can drop its hold on the backing storage and allow it to be
3099 * reaped by the shrinker.
3100 */
3101 i915_gem_object_unpin_pages(obj);
3102
Chris Wilsonb1f788c2016-08-04 07:52:45 +01003103destroy:
Chris Wilson3272db52016-08-04 16:32:32 +01003104 if (unlikely(i915_vma_is_closed(vma)))
Chris Wilsonb1f788c2016-08-04 07:52:45 +01003105 i915_vma_destroy(vma);
3106
Chris Wilson88241782011-01-07 17:09:48 +00003107 return 0;
Chris Wilson54cf91d2010-11-25 18:00:26 +00003108}
3109
Chris Wilsondcff85c2016-08-05 10:14:11 +01003110int i915_gem_wait_for_idle(struct drm_i915_private *dev_priv,
Chris Wilsonea746f32016-09-09 14:11:49 +01003111 unsigned int flags)
Daniel Vetter4df2faf2010-02-19 11:52:00 +01003112{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00003113 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05303114 enum intel_engine_id id;
Dave Gordonb4ac5af2016-03-24 11:20:38 +00003115 int ret;
Daniel Vetter4df2faf2010-02-19 11:52:00 +01003116
Akash Goel3b3f1652016-10-13 22:44:48 +05303117 for_each_engine(engine, dev_priv, id) {
Chris Wilson62e63002016-06-24 14:55:52 +01003118 if (engine->last_context == NULL)
3119 continue;
3120
Chris Wilsonea746f32016-09-09 14:11:49 +01003121 ret = intel_engine_idle(engine, flags);
Chris Wilson1ec14ad2010-12-04 11:30:53 +00003122 if (ret)
3123 return ret;
3124 }
Zou Nan haid1b851f2010-05-21 09:08:57 +08003125
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01003126 return 0;
Daniel Vetter4df2faf2010-02-19 11:52:00 +01003127}
3128
Chris Wilson4144f9b2014-09-11 08:43:48 +01003129static bool i915_gem_valid_gtt_space(struct i915_vma *vma,
Chris Wilson42d6ab42012-07-26 11:49:32 +01003130 unsigned long cache_level)
3131{
Chris Wilson4144f9b2014-09-11 08:43:48 +01003132 struct drm_mm_node *gtt_space = &vma->node;
Chris Wilson42d6ab42012-07-26 11:49:32 +01003133 struct drm_mm_node *other;
3134
Chris Wilson4144f9b2014-09-11 08:43:48 +01003135 /*
3136 * On some machines we have to be careful when putting differing types
3137 * of snoopable memory together to avoid the prefetcher crossing memory
3138 * domains and dying. During vm initialisation, we decide whether or not
3139 * these constraints apply and set the drm_mm.color_adjust
3140 * appropriately.
Chris Wilson42d6ab42012-07-26 11:49:32 +01003141 */
Chris Wilson4144f9b2014-09-11 08:43:48 +01003142 if (vma->vm->mm.color_adjust == NULL)
Chris Wilson42d6ab42012-07-26 11:49:32 +01003143 return true;
3144
Ben Widawskyc6cfb322013-07-05 14:41:06 -07003145 if (!drm_mm_node_allocated(gtt_space))
Chris Wilson42d6ab42012-07-26 11:49:32 +01003146 return true;
3147
3148 if (list_empty(&gtt_space->node_list))
3149 return true;
3150
3151 other = list_entry(gtt_space->node_list.prev, struct drm_mm_node, node_list);
3152 if (other->allocated && !other->hole_follows && other->color != cache_level)
3153 return false;
3154
3155 other = list_entry(gtt_space->node_list.next, struct drm_mm_node, node_list);
3156 if (other->allocated && !gtt_space->hole_follows && other->color != cache_level)
3157 return false;
3158
3159 return true;
3160}
3161
Jesse Barnesde151cf2008-11-12 10:03:55 -08003162/**
Chris Wilson59bfa122016-08-04 16:32:31 +01003163 * i915_vma_insert - finds a slot for the vma in its address space
3164 * @vma: the vma
Chris Wilson91b2db62016-08-04 16:32:23 +01003165 * @size: requested size in bytes (can be larger than the VMA)
Chris Wilson59bfa122016-08-04 16:32:31 +01003166 * @alignment: required alignment
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01003167 * @flags: mask of PIN_* flags to use
Chris Wilson59bfa122016-08-04 16:32:31 +01003168 *
3169 * First we try to allocate some free space that meets the requirements for
3170 * the VMA. Failiing that, if the flags permit, it will evict an old VMA,
3171 * preferrably the oldest idle entry to make room for the new VMA.
3172 *
3173 * Returns:
3174 * 0 on success, negative error code otherwise.
Eric Anholt673a3942008-07-30 12:06:12 -07003175 */
Chris Wilson59bfa122016-08-04 16:32:31 +01003176static int
3177i915_vma_insert(struct i915_vma *vma, u64 size, u64 alignment, u64 flags)
Eric Anholt673a3942008-07-30 12:06:12 -07003178{
Chris Wilson59bfa122016-08-04 16:32:31 +01003179 struct drm_i915_private *dev_priv = to_i915(vma->vm->dev);
3180 struct drm_i915_gem_object *obj = vma->obj;
Chris Wilsonde180032016-08-04 16:32:29 +01003181 u64 start, end;
Chris Wilson07f73f62009-09-14 16:50:30 +01003182 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003183
Chris Wilson3272db52016-08-04 16:32:32 +01003184 GEM_BUG_ON(vma->flags & (I915_VMA_GLOBAL_BIND | I915_VMA_LOCAL_BIND));
Chris Wilson59bfa122016-08-04 16:32:31 +01003185 GEM_BUG_ON(drm_mm_node_allocated(&vma->node));
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02003186
Chris Wilsonde180032016-08-04 16:32:29 +01003187 size = max(size, vma->size);
3188 if (flags & PIN_MAPPABLE)
Chris Wilson3e510a82016-08-05 10:14:23 +01003189 size = i915_gem_get_ggtt_size(dev_priv, size,
3190 i915_gem_object_get_tiling(obj));
Joonas Lahtinen91e67112015-05-06 14:33:58 +03003191
Chris Wilsond8923dc2016-08-18 17:17:07 +01003192 alignment = max(max(alignment, vma->display_alignment),
3193 i915_gem_get_ggtt_alignment(dev_priv, size,
3194 i915_gem_object_get_tiling(obj),
3195 flags & PIN_MAPPABLE));
Chris Wilsona00b10c2010-09-24 21:15:47 +01003196
Michel Thierry101b5062015-10-01 13:33:57 +01003197 start = flags & PIN_OFFSET_BIAS ? flags & PIN_OFFSET_MASK : 0;
Chris Wilsonde180032016-08-04 16:32:29 +01003198
3199 end = vma->vm->total;
Michel Thierry101b5062015-10-01 13:33:57 +01003200 if (flags & PIN_MAPPABLE)
Chris Wilson91b2db62016-08-04 16:32:23 +01003201 end = min_t(u64, end, dev_priv->ggtt.mappable_end);
Michel Thierry101b5062015-10-01 13:33:57 +01003202 if (flags & PIN_ZONE_4G)
Michel Thierry48ea1e32016-01-11 11:39:27 +00003203 end = min_t(u64, end, (1ULL << 32) - PAGE_SIZE);
Michel Thierry101b5062015-10-01 13:33:57 +01003204
Joonas Lahtinen91e67112015-05-06 14:33:58 +03003205 /* If binding the object/GGTT view requires more space than the entire
3206 * aperture has, reject it early before evicting everything in a vain
3207 * attempt to find space.
Chris Wilson654fc602010-05-27 13:18:21 +01003208 */
Joonas Lahtinen91e67112015-05-06 14:33:58 +03003209 if (size > end) {
Chris Wilsonde180032016-08-04 16:32:29 +01003210 DRM_DEBUG("Attempting to bind an object larger than the aperture: request=%llu [object=%zd] > %s aperture=%llu\n",
Chris Wilson91b2db62016-08-04 16:32:23 +01003211 size, obj->base.size,
Daniel Vetter1ec9e262014-02-14 14:01:11 +01003212 flags & PIN_MAPPABLE ? "mappable" : "total",
Chris Wilsond23db882014-05-23 08:48:08 +02003213 end);
Chris Wilson59bfa122016-08-04 16:32:31 +01003214 return -E2BIG;
Chris Wilson654fc602010-05-27 13:18:21 +01003215 }
3216
Chris Wilson37e680a2012-06-07 15:38:42 +01003217 ret = i915_gem_object_get_pages(obj);
Chris Wilson6c085a72012-08-20 11:40:46 +02003218 if (ret)
Chris Wilson59bfa122016-08-04 16:32:31 +01003219 return ret;
Chris Wilson6c085a72012-08-20 11:40:46 +02003220
Chris Wilsonfbdda6f2012-11-20 10:45:16 +00003221 i915_gem_object_pin_pages(obj);
3222
Chris Wilson506a8e82015-12-08 11:55:07 +00003223 if (flags & PIN_OFFSET_FIXED) {
Chris Wilson59bfa122016-08-04 16:32:31 +01003224 u64 offset = flags & PIN_OFFSET_MASK;
Chris Wilsonde180032016-08-04 16:32:29 +01003225 if (offset & (alignment - 1) || offset > end - size) {
Chris Wilson506a8e82015-12-08 11:55:07 +00003226 ret = -EINVAL;
Chris Wilsonde180032016-08-04 16:32:29 +01003227 goto err_unpin;
Chris Wilson506a8e82015-12-08 11:55:07 +00003228 }
Chris Wilsonde180032016-08-04 16:32:29 +01003229
Chris Wilson506a8e82015-12-08 11:55:07 +00003230 vma->node.start = offset;
3231 vma->node.size = size;
3232 vma->node.color = obj->cache_level;
Chris Wilsonde180032016-08-04 16:32:29 +01003233 ret = drm_mm_reserve_node(&vma->vm->mm, &vma->node);
Chris Wilson506a8e82015-12-08 11:55:07 +00003234 if (ret) {
3235 ret = i915_gem_evict_for_vma(vma);
3236 if (ret == 0)
Chris Wilsonde180032016-08-04 16:32:29 +01003237 ret = drm_mm_reserve_node(&vma->vm->mm, &vma->node);
3238 if (ret)
3239 goto err_unpin;
Chris Wilson506a8e82015-12-08 11:55:07 +00003240 }
Michel Thierry101b5062015-10-01 13:33:57 +01003241 } else {
Chris Wilsonde180032016-08-04 16:32:29 +01003242 u32 search_flag, alloc_flag;
3243
Chris Wilson506a8e82015-12-08 11:55:07 +00003244 if (flags & PIN_HIGH) {
3245 search_flag = DRM_MM_SEARCH_BELOW;
3246 alloc_flag = DRM_MM_CREATE_TOP;
3247 } else {
3248 search_flag = DRM_MM_SEARCH_DEFAULT;
3249 alloc_flag = DRM_MM_CREATE_DEFAULT;
3250 }
Michel Thierry101b5062015-10-01 13:33:57 +01003251
Chris Wilson954c4692016-08-04 16:32:26 +01003252 /* We only allocate in PAGE_SIZE/GTT_PAGE_SIZE (4096) chunks,
3253 * so we know that we always have a minimum alignment of 4096.
3254 * The drm_mm range manager is optimised to return results
3255 * with zero alignment, so where possible use the optimal
3256 * path.
3257 */
3258 if (alignment <= 4096)
3259 alignment = 0;
3260
Ben Widawsky0a9ae0d2013-05-25 12:26:35 -07003261search_free:
Chris Wilsonde180032016-08-04 16:32:29 +01003262 ret = drm_mm_insert_node_in_range_generic(&vma->vm->mm,
3263 &vma->node,
Chris Wilson506a8e82015-12-08 11:55:07 +00003264 size, alignment,
3265 obj->cache_level,
3266 start, end,
3267 search_flag,
3268 alloc_flag);
3269 if (ret) {
Chris Wilsonde180032016-08-04 16:32:29 +01003270 ret = i915_gem_evict_something(vma->vm, size, alignment,
Chris Wilson506a8e82015-12-08 11:55:07 +00003271 obj->cache_level,
3272 start, end,
3273 flags);
3274 if (ret == 0)
3275 goto search_free;
Chris Wilson97311292009-09-21 00:22:34 +01003276
Chris Wilsonde180032016-08-04 16:32:29 +01003277 goto err_unpin;
Chris Wilson506a8e82015-12-08 11:55:07 +00003278 }
Chris Wilsonad16d2e2016-10-13 09:55:04 +01003279
3280 GEM_BUG_ON(vma->node.start < start);
3281 GEM_BUG_ON(vma->node.start + vma->node.size > end);
Chris Wilsondc9dd7a2012-12-07 20:37:07 +00003282 }
Chris Wilson37508582016-08-04 16:32:24 +01003283 GEM_BUG_ON(!i915_gem_valid_gtt_space(vma, obj->cache_level));
Eric Anholt673a3942008-07-30 12:06:12 -07003284
Ben Widawsky35c20a62013-05-31 11:28:48 -07003285 list_move_tail(&obj->global_list, &dev_priv->mm.bound_list);
Chris Wilsonde180032016-08-04 16:32:29 +01003286 list_move_tail(&vma->vm_link, &vma->vm->inactive_list);
Chris Wilson15717de2016-08-04 07:52:26 +01003287 obj->bind_count++;
Chris Wilsonbf1a1092010-08-07 11:01:20 +01003288
Chris Wilson59bfa122016-08-04 16:32:31 +01003289 return 0;
Ben Widawsky2f633152013-07-17 12:19:03 -07003290
Daniel Vetterbc6bc152013-07-22 12:12:38 +02003291err_unpin:
Ben Widawsky2f633152013-07-17 12:19:03 -07003292 i915_gem_object_unpin_pages(obj);
Chris Wilson59bfa122016-08-04 16:32:31 +01003293 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003294}
3295
Chris Wilson000433b2013-08-08 14:41:09 +01003296bool
Chris Wilson2c225692013-08-09 12:26:45 +01003297i915_gem_clflush_object(struct drm_i915_gem_object *obj,
3298 bool force)
Eric Anholt673a3942008-07-30 12:06:12 -07003299{
Eric Anholt673a3942008-07-30 12:06:12 -07003300 /* If we don't have a page list set up, then we're not pinned
3301 * to GPU, and we can ignore the cache flush because it'll happen
3302 * again at bind time.
3303 */
Chris Wilson05394f32010-11-08 19:18:58 +00003304 if (obj->pages == NULL)
Chris Wilson000433b2013-08-08 14:41:09 +01003305 return false;
Eric Anholt673a3942008-07-30 12:06:12 -07003306
Imre Deak769ce462013-02-13 21:56:05 +02003307 /*
3308 * Stolen memory is always coherent with the GPU as it is explicitly
3309 * marked as wc by the system, or the system is cache-coherent.
3310 */
Chris Wilson6a2c4232014-11-04 04:51:40 -08003311 if (obj->stolen || obj->phys_handle)
Chris Wilson000433b2013-08-08 14:41:09 +01003312 return false;
Imre Deak769ce462013-02-13 21:56:05 +02003313
Chris Wilson9c23f7f2011-03-29 16:59:52 -07003314 /* If the GPU is snooping the contents of the CPU cache,
3315 * we do not need to manually clear the CPU cache lines. However,
3316 * the caches are only snooped when the render cache is
3317 * flushed/invalidated. As we always have to emit invalidations
3318 * and flushes when moving into and out of the RENDER domain, correct
3319 * snooping behaviour occurs naturally as the result of our domain
3320 * tracking.
3321 */
Chris Wilson0f719792015-01-13 13:32:52 +00003322 if (!force && cpu_cache_is_coherent(obj->base.dev, obj->cache_level)) {
3323 obj->cache_dirty = true;
Chris Wilson000433b2013-08-08 14:41:09 +01003324 return false;
Chris Wilson0f719792015-01-13 13:32:52 +00003325 }
Chris Wilson9c23f7f2011-03-29 16:59:52 -07003326
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003327 trace_i915_gem_object_clflush(obj);
Chris Wilson9da3da62012-06-01 15:20:22 +01003328 drm_clflush_sg(obj->pages);
Chris Wilson0f719792015-01-13 13:32:52 +00003329 obj->cache_dirty = false;
Chris Wilson000433b2013-08-08 14:41:09 +01003330
3331 return true;
Eric Anholte47c68e2008-11-14 13:35:19 -08003332}
3333
3334/** Flushes the GTT write domain for the object if it's dirty. */
3335static void
Chris Wilson05394f32010-11-08 19:18:58 +00003336i915_gem_object_flush_gtt_write_domain(struct drm_i915_gem_object *obj)
Eric Anholte47c68e2008-11-14 13:35:19 -08003337{
Chris Wilson3b5724d2016-08-18 17:16:49 +01003338 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003339
Chris Wilson05394f32010-11-08 19:18:58 +00003340 if (obj->base.write_domain != I915_GEM_DOMAIN_GTT)
Eric Anholte47c68e2008-11-14 13:35:19 -08003341 return;
3342
Chris Wilson63256ec2011-01-04 18:42:07 +00003343 /* No actual flushing is required for the GTT write domain. Writes
Chris Wilson3b5724d2016-08-18 17:16:49 +01003344 * to it "immediately" go to main memory as far as we know, so there's
Eric Anholte47c68e2008-11-14 13:35:19 -08003345 * no chipset flush. It also doesn't land in render cache.
Chris Wilson63256ec2011-01-04 18:42:07 +00003346 *
3347 * However, we do have to enforce the order so that all writes through
3348 * the GTT land before any writes to the device, such as updates to
3349 * the GATT itself.
Chris Wilson3b5724d2016-08-18 17:16:49 +01003350 *
3351 * We also have to wait a bit for the writes to land from the GTT.
3352 * An uncached read (i.e. mmio) seems to be ideal for the round-trip
3353 * timing. This issue has only been observed when switching quickly
3354 * between GTT writes and CPU reads from inside the kernel on recent hw,
3355 * and it appears to only affect discrete GTT blocks (i.e. on LLC
3356 * system agents we cannot reproduce this behaviour).
Eric Anholte47c68e2008-11-14 13:35:19 -08003357 */
Chris Wilson63256ec2011-01-04 18:42:07 +00003358 wmb();
Chris Wilson3b5724d2016-08-18 17:16:49 +01003359 if (INTEL_GEN(dev_priv) >= 6 && !HAS_LLC(dev_priv))
Akash Goel3b3f1652016-10-13 22:44:48 +05303360 POSTING_READ(RING_ACTHD(dev_priv->engine[RCS]->mmio_base));
Chris Wilson63256ec2011-01-04 18:42:07 +00003361
Chris Wilsond243ad82016-08-18 17:16:44 +01003362 intel_fb_obj_flush(obj, false, write_origin(obj, I915_GEM_DOMAIN_GTT));
Daniel Vetterf99d7062014-06-19 16:01:59 +02003363
Chris Wilsonb0dc4652016-08-18 17:16:51 +01003364 obj->base.write_domain = 0;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003365 trace_i915_gem_object_change_domain(obj,
Chris Wilson05394f32010-11-08 19:18:58 +00003366 obj->base.read_domains,
Chris Wilsonb0dc4652016-08-18 17:16:51 +01003367 I915_GEM_DOMAIN_GTT);
Eric Anholte47c68e2008-11-14 13:35:19 -08003368}
3369
3370/** Flushes the CPU write domain for the object if it's dirty. */
3371static void
Daniel Vettere62b59e2015-01-21 14:53:48 +01003372i915_gem_object_flush_cpu_write_domain(struct drm_i915_gem_object *obj)
Eric Anholte47c68e2008-11-14 13:35:19 -08003373{
Chris Wilson05394f32010-11-08 19:18:58 +00003374 if (obj->base.write_domain != I915_GEM_DOMAIN_CPU)
Eric Anholte47c68e2008-11-14 13:35:19 -08003375 return;
3376
Daniel Vettere62b59e2015-01-21 14:53:48 +01003377 if (i915_gem_clflush_object(obj, obj->pin_display))
Chris Wilsonc0336662016-05-06 15:40:21 +01003378 i915_gem_chipset_flush(to_i915(obj->base.dev));
Chris Wilson000433b2013-08-08 14:41:09 +01003379
Rodrigo Vivide152b62015-07-07 16:28:51 -07003380 intel_fb_obj_flush(obj, false, ORIGIN_CPU);
Daniel Vetterf99d7062014-06-19 16:01:59 +02003381
Chris Wilsonb0dc4652016-08-18 17:16:51 +01003382 obj->base.write_domain = 0;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003383 trace_i915_gem_object_change_domain(obj,
Chris Wilson05394f32010-11-08 19:18:58 +00003384 obj->base.read_domains,
Chris Wilsonb0dc4652016-08-18 17:16:51 +01003385 I915_GEM_DOMAIN_CPU);
Eric Anholte47c68e2008-11-14 13:35:19 -08003386}
3387
Chris Wilson383d5822016-08-18 17:17:08 +01003388static void i915_gem_object_bump_inactive_ggtt(struct drm_i915_gem_object *obj)
3389{
3390 struct i915_vma *vma;
3391
3392 list_for_each_entry(vma, &obj->vma_list, obj_link) {
3393 if (!i915_vma_is_ggtt(vma))
3394 continue;
3395
3396 if (i915_vma_is_active(vma))
3397 continue;
3398
3399 if (!drm_mm_node_allocated(&vma->node))
3400 continue;
3401
3402 list_move_tail(&vma->vm_link, &vma->vm->inactive_list);
3403 }
3404}
3405
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003406/**
3407 * Moves a single object to the GTT read, and possibly write domain.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01003408 * @obj: object to act on
3409 * @write: ask for write access or read only
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003410 *
3411 * This function returns when the move is complete, including waiting on
3412 * flushes to occur.
3413 */
Jesse Barnes79e53942008-11-07 14:24:08 -08003414int
Chris Wilson20217462010-11-23 15:26:33 +00003415i915_gem_object_set_to_gtt_domain(struct drm_i915_gem_object *obj, bool write)
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003416{
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003417 uint32_t old_write_domain, old_read_domains;
Eric Anholte47c68e2008-11-14 13:35:19 -08003418 int ret;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003419
Chris Wilsone95433c2016-10-28 13:58:27 +01003420 lockdep_assert_held(&obj->base.dev->struct_mutex);
3421 ret = i915_gem_object_wait(obj,
3422 I915_WAIT_INTERRUPTIBLE |
3423 I915_WAIT_LOCKED |
3424 (write ? I915_WAIT_ALL : 0),
3425 MAX_SCHEDULE_TIMEOUT,
3426 NULL);
Chris Wilson88241782011-01-07 17:09:48 +00003427 if (ret)
3428 return ret;
3429
Chris Wilsonc13d87e2016-07-20 09:21:15 +01003430 if (obj->base.write_domain == I915_GEM_DOMAIN_GTT)
3431 return 0;
3432
Chris Wilson43566de2015-01-02 16:29:29 +05303433 /* Flush and acquire obj->pages so that we are coherent through
3434 * direct access in memory with previous cached writes through
3435 * shmemfs and that our cache domain tracking remains valid.
3436 * For example, if the obj->filp was moved to swap without us
3437 * being notified and releasing the pages, we would mistakenly
3438 * continue to assume that the obj remained out of the CPU cached
3439 * domain.
3440 */
3441 ret = i915_gem_object_get_pages(obj);
3442 if (ret)
3443 return ret;
3444
Daniel Vettere62b59e2015-01-21 14:53:48 +01003445 i915_gem_object_flush_cpu_write_domain(obj);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003446
Chris Wilsond0a57782012-10-09 19:24:37 +01003447 /* Serialise direct access to this object with the barriers for
3448 * coherent writes from the GPU, by effectively invalidating the
3449 * GTT domain upon first access.
3450 */
3451 if ((obj->base.read_domains & I915_GEM_DOMAIN_GTT) == 0)
3452 mb();
3453
Chris Wilson05394f32010-11-08 19:18:58 +00003454 old_write_domain = obj->base.write_domain;
3455 old_read_domains = obj->base.read_domains;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003456
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003457 /* It should now be out of any other write domains, and we can update
3458 * the domain values for our changes.
3459 */
Chris Wilson05394f32010-11-08 19:18:58 +00003460 BUG_ON((obj->base.write_domain & ~I915_GEM_DOMAIN_GTT) != 0);
3461 obj->base.read_domains |= I915_GEM_DOMAIN_GTT;
Eric Anholte47c68e2008-11-14 13:35:19 -08003462 if (write) {
Chris Wilson05394f32010-11-08 19:18:58 +00003463 obj->base.read_domains = I915_GEM_DOMAIN_GTT;
3464 obj->base.write_domain = I915_GEM_DOMAIN_GTT;
3465 obj->dirty = 1;
Eric Anholte47c68e2008-11-14 13:35:19 -08003466 }
3467
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003468 trace_i915_gem_object_change_domain(obj,
3469 old_read_domains,
3470 old_write_domain);
3471
Chris Wilson8325a092012-04-24 15:52:35 +01003472 /* And bump the LRU for this access */
Chris Wilson383d5822016-08-18 17:17:08 +01003473 i915_gem_object_bump_inactive_ggtt(obj);
Chris Wilson8325a092012-04-24 15:52:35 +01003474
Eric Anholte47c68e2008-11-14 13:35:19 -08003475 return 0;
3476}
3477
Chris Wilsonef55f922015-10-09 14:11:27 +01003478/**
3479 * Changes the cache-level of an object across all VMA.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01003480 * @obj: object to act on
3481 * @cache_level: new cache level to set for the object
Chris Wilsonef55f922015-10-09 14:11:27 +01003482 *
3483 * After this function returns, the object will be in the new cache-level
3484 * across all GTT and the contents of the backing storage will be coherent,
3485 * with respect to the new cache-level. In order to keep the backing storage
3486 * coherent for all users, we only allow a single cache level to be set
3487 * globally on the object and prevent it from being changed whilst the
3488 * hardware is reading from the object. That is if the object is currently
3489 * on the scanout it will be set to uncached (or equivalent display
3490 * cache coherency) and all non-MOCS GPU access will also be uncached so
3491 * that all direct access to the scanout remains coherent.
3492 */
Chris Wilsone4ffd172011-04-04 09:44:39 +01003493int i915_gem_object_set_cache_level(struct drm_i915_gem_object *obj,
3494 enum i915_cache_level cache_level)
3495{
Chris Wilsonaa653a62016-08-04 07:52:27 +01003496 struct i915_vma *vma;
Ville Syrjäläed75a552015-08-11 19:47:10 +03003497 int ret = 0;
Chris Wilsone4ffd172011-04-04 09:44:39 +01003498
3499 if (obj->cache_level == cache_level)
Ville Syrjäläed75a552015-08-11 19:47:10 +03003500 goto out;
Chris Wilsone4ffd172011-04-04 09:44:39 +01003501
Chris Wilsonef55f922015-10-09 14:11:27 +01003502 /* Inspect the list of currently bound VMA and unbind any that would
3503 * be invalid given the new cache-level. This is principally to
3504 * catch the issue of the CS prefetch crossing page boundaries and
3505 * reading an invalid PTE on older architectures.
3506 */
Chris Wilsonaa653a62016-08-04 07:52:27 +01003507restart:
3508 list_for_each_entry(vma, &obj->vma_list, obj_link) {
Chris Wilsonef55f922015-10-09 14:11:27 +01003509 if (!drm_mm_node_allocated(&vma->node))
3510 continue;
3511
Chris Wilson20dfbde2016-08-04 16:32:30 +01003512 if (i915_vma_is_pinned(vma)) {
Chris Wilsonef55f922015-10-09 14:11:27 +01003513 DRM_DEBUG("can not change the cache level of pinned objects\n");
3514 return -EBUSY;
3515 }
3516
Chris Wilsonaa653a62016-08-04 07:52:27 +01003517 if (i915_gem_valid_gtt_space(vma, cache_level))
3518 continue;
3519
3520 ret = i915_vma_unbind(vma);
3521 if (ret)
3522 return ret;
3523
3524 /* As unbinding may affect other elements in the
3525 * obj->vma_list (due to side-effects from retiring
3526 * an active vma), play safe and restart the iterator.
3527 */
3528 goto restart;
Chris Wilson42d6ab42012-07-26 11:49:32 +01003529 }
3530
Chris Wilsonef55f922015-10-09 14:11:27 +01003531 /* We can reuse the existing drm_mm nodes but need to change the
3532 * cache-level on the PTE. We could simply unbind them all and
3533 * rebind with the correct cache-level on next use. However since
3534 * we already have a valid slot, dma mapping, pages etc, we may as
3535 * rewrite the PTE in the belief that doing so tramples upon less
3536 * state and so involves less work.
3537 */
Chris Wilson15717de2016-08-04 07:52:26 +01003538 if (obj->bind_count) {
Chris Wilsonef55f922015-10-09 14:11:27 +01003539 /* Before we change the PTE, the GPU must not be accessing it.
3540 * If we wait upon the object, we know that all the bound
3541 * VMA are no longer active.
3542 */
Chris Wilsone95433c2016-10-28 13:58:27 +01003543 ret = i915_gem_object_wait(obj,
3544 I915_WAIT_INTERRUPTIBLE |
3545 I915_WAIT_LOCKED |
3546 I915_WAIT_ALL,
3547 MAX_SCHEDULE_TIMEOUT,
3548 NULL);
Chris Wilsone4ffd172011-04-04 09:44:39 +01003549 if (ret)
3550 return ret;
3551
Chris Wilsonaa653a62016-08-04 07:52:27 +01003552 if (!HAS_LLC(obj->base.dev) && cache_level != I915_CACHE_NONE) {
Chris Wilsonef55f922015-10-09 14:11:27 +01003553 /* Access to snoopable pages through the GTT is
3554 * incoherent and on some machines causes a hard
3555 * lockup. Relinquish the CPU mmaping to force
3556 * userspace to refault in the pages and we can
3557 * then double check if the GTT mapping is still
3558 * valid for that pointer access.
3559 */
3560 i915_gem_release_mmap(obj);
Chris Wilsone4ffd172011-04-04 09:44:39 +01003561
Chris Wilsonef55f922015-10-09 14:11:27 +01003562 /* As we no longer need a fence for GTT access,
3563 * we can relinquish it now (and so prevent having
3564 * to steal a fence from someone else on the next
3565 * fence request). Note GPU activity would have
3566 * dropped the fence as all snoopable access is
3567 * supposed to be linear.
3568 */
Chris Wilson49ef5292016-08-18 17:17:00 +01003569 list_for_each_entry(vma, &obj->vma_list, obj_link) {
3570 ret = i915_vma_put_fence(vma);
3571 if (ret)
3572 return ret;
3573 }
Chris Wilsonef55f922015-10-09 14:11:27 +01003574 } else {
3575 /* We either have incoherent backing store and
3576 * so no GTT access or the architecture is fully
3577 * coherent. In such cases, existing GTT mmaps
3578 * ignore the cache bit in the PTE and we can
3579 * rewrite it without confusing the GPU or having
3580 * to force userspace to fault back in its mmaps.
3581 */
Chris Wilsone4ffd172011-04-04 09:44:39 +01003582 }
3583
Chris Wilson1c7f4bc2016-02-26 11:03:19 +00003584 list_for_each_entry(vma, &obj->vma_list, obj_link) {
Chris Wilsonef55f922015-10-09 14:11:27 +01003585 if (!drm_mm_node_allocated(&vma->node))
3586 continue;
3587
3588 ret = i915_vma_bind(vma, cache_level, PIN_UPDATE);
3589 if (ret)
3590 return ret;
3591 }
Chris Wilsone4ffd172011-04-04 09:44:39 +01003592 }
3593
Chris Wilson1c7f4bc2016-02-26 11:03:19 +00003594 list_for_each_entry(vma, &obj->vma_list, obj_link)
Chris Wilson2c225692013-08-09 12:26:45 +01003595 vma->node.color = cache_level;
3596 obj->cache_level = cache_level;
3597
Ville Syrjäläed75a552015-08-11 19:47:10 +03003598out:
Chris Wilsonef55f922015-10-09 14:11:27 +01003599 /* Flush the dirty CPU caches to the backing storage so that the
3600 * object is now coherent at its new cache level (with respect
3601 * to the access domain).
3602 */
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05303603 if (obj->cache_dirty && cpu_write_needs_clflush(obj)) {
Chris Wilson0f719792015-01-13 13:32:52 +00003604 if (i915_gem_clflush_object(obj, true))
Chris Wilsonc0336662016-05-06 15:40:21 +01003605 i915_gem_chipset_flush(to_i915(obj->base.dev));
Chris Wilsone4ffd172011-04-04 09:44:39 +01003606 }
3607
Chris Wilsone4ffd172011-04-04 09:44:39 +01003608 return 0;
3609}
3610
Ben Widawsky199adf42012-09-21 17:01:20 -07003611int i915_gem_get_caching_ioctl(struct drm_device *dev, void *data,
3612 struct drm_file *file)
Chris Wilsone6994ae2012-07-10 10:27:08 +01003613{
Ben Widawsky199adf42012-09-21 17:01:20 -07003614 struct drm_i915_gem_caching *args = data;
Chris Wilsone6994ae2012-07-10 10:27:08 +01003615 struct drm_i915_gem_object *obj;
Chris Wilsone6994ae2012-07-10 10:27:08 +01003616
Chris Wilson03ac0642016-07-20 13:31:51 +01003617 obj = i915_gem_object_lookup(file, args->handle);
3618 if (!obj)
Chris Wilson432be692015-05-07 12:14:55 +01003619 return -ENOENT;
Chris Wilsone6994ae2012-07-10 10:27:08 +01003620
Chris Wilson651d7942013-08-08 14:41:10 +01003621 switch (obj->cache_level) {
3622 case I915_CACHE_LLC:
3623 case I915_CACHE_L3_LLC:
3624 args->caching = I915_CACHING_CACHED;
3625 break;
3626
Chris Wilson4257d3b2013-08-08 14:41:11 +01003627 case I915_CACHE_WT:
3628 args->caching = I915_CACHING_DISPLAY;
3629 break;
3630
Chris Wilson651d7942013-08-08 14:41:10 +01003631 default:
3632 args->caching = I915_CACHING_NONE;
3633 break;
3634 }
Chris Wilsone6994ae2012-07-10 10:27:08 +01003635
Chris Wilson34911fd2016-07-20 13:31:54 +01003636 i915_gem_object_put_unlocked(obj);
Chris Wilson432be692015-05-07 12:14:55 +01003637 return 0;
Chris Wilsone6994ae2012-07-10 10:27:08 +01003638}
3639
Ben Widawsky199adf42012-09-21 17:01:20 -07003640int i915_gem_set_caching_ioctl(struct drm_device *dev, void *data,
3641 struct drm_file *file)
Chris Wilsone6994ae2012-07-10 10:27:08 +01003642{
Chris Wilson9c870d02016-10-24 13:42:15 +01003643 struct drm_i915_private *i915 = to_i915(dev);
Ben Widawsky199adf42012-09-21 17:01:20 -07003644 struct drm_i915_gem_caching *args = data;
Chris Wilsone6994ae2012-07-10 10:27:08 +01003645 struct drm_i915_gem_object *obj;
3646 enum i915_cache_level level;
3647 int ret;
3648
Ben Widawsky199adf42012-09-21 17:01:20 -07003649 switch (args->caching) {
3650 case I915_CACHING_NONE:
Chris Wilsone6994ae2012-07-10 10:27:08 +01003651 level = I915_CACHE_NONE;
3652 break;
Ben Widawsky199adf42012-09-21 17:01:20 -07003653 case I915_CACHING_CACHED:
Imre Deake5756c12015-08-14 18:43:30 +03003654 /*
3655 * Due to a HW issue on BXT A stepping, GPU stores via a
3656 * snooped mapping may leave stale data in a corresponding CPU
3657 * cacheline, whereas normally such cachelines would get
3658 * invalidated.
3659 */
Chris Wilson9c870d02016-10-24 13:42:15 +01003660 if (!HAS_LLC(i915) && !HAS_SNOOP(i915))
Imre Deake5756c12015-08-14 18:43:30 +03003661 return -ENODEV;
3662
Chris Wilsone6994ae2012-07-10 10:27:08 +01003663 level = I915_CACHE_LLC;
3664 break;
Chris Wilson4257d3b2013-08-08 14:41:11 +01003665 case I915_CACHING_DISPLAY:
Chris Wilson9c870d02016-10-24 13:42:15 +01003666 level = HAS_WT(i915) ? I915_CACHE_WT : I915_CACHE_NONE;
Chris Wilson4257d3b2013-08-08 14:41:11 +01003667 break;
Chris Wilsone6994ae2012-07-10 10:27:08 +01003668 default:
3669 return -EINVAL;
3670 }
3671
Ben Widawsky3bc29132012-09-26 16:15:20 -07003672 ret = i915_mutex_lock_interruptible(dev);
3673 if (ret)
Chris Wilson9c870d02016-10-24 13:42:15 +01003674 return ret;
Ben Widawsky3bc29132012-09-26 16:15:20 -07003675
Chris Wilson03ac0642016-07-20 13:31:51 +01003676 obj = i915_gem_object_lookup(file, args->handle);
3677 if (!obj) {
Chris Wilsone6994ae2012-07-10 10:27:08 +01003678 ret = -ENOENT;
3679 goto unlock;
3680 }
3681
3682 ret = i915_gem_object_set_cache_level(obj, level);
Chris Wilsonf8c417c2016-07-20 13:31:53 +01003683 i915_gem_object_put(obj);
Chris Wilsone6994ae2012-07-10 10:27:08 +01003684unlock:
3685 mutex_unlock(&dev->struct_mutex);
3686 return ret;
3687}
3688
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003689/*
Chris Wilson2da3b9b2011-04-14 09:41:17 +01003690 * Prepare buffer for display plane (scanout, cursors, etc).
3691 * Can be called from an uninterruptible phase (modesetting) and allows
3692 * any flushes to be pipelined (for pageflips).
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003693 */
Chris Wilson058d88c2016-08-15 10:49:06 +01003694struct i915_vma *
Chris Wilson2da3b9b2011-04-14 09:41:17 +01003695i915_gem_object_pin_to_display_plane(struct drm_i915_gem_object *obj,
3696 u32 alignment,
Tvrtko Ursuline6617332015-03-23 11:10:33 +00003697 const struct i915_ggtt_view *view)
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003698{
Chris Wilson058d88c2016-08-15 10:49:06 +01003699 struct i915_vma *vma;
Chris Wilson2da3b9b2011-04-14 09:41:17 +01003700 u32 old_read_domains, old_write_domain;
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003701 int ret;
3702
Chris Wilsoncc98b412013-08-09 12:25:09 +01003703 /* Mark the pin_display early so that we account for the
3704 * display coherency whilst setting up the cache domains.
3705 */
Tvrtko Ursulin8a0c39b2015-04-13 11:50:09 +01003706 obj->pin_display++;
Chris Wilsoncc98b412013-08-09 12:25:09 +01003707
Eric Anholta7ef0642011-03-29 16:59:54 -07003708 /* The display engine is not coherent with the LLC cache on gen6. As
3709 * a result, we make sure that the pinning that is about to occur is
3710 * done with uncached PTEs. This is lowest common denominator for all
3711 * chipsets.
3712 *
3713 * However for gen6+, we could do better by using the GFDT bit instead
3714 * of uncaching, which would allow us to flush all the LLC-cached data
3715 * with that bit in the PTE to main memory with just one PIPE_CONTROL.
3716 */
Chris Wilson651d7942013-08-08 14:41:10 +01003717 ret = i915_gem_object_set_cache_level(obj,
Tvrtko Ursulin86527442016-10-13 11:03:00 +01003718 HAS_WT(to_i915(obj->base.dev)) ?
3719 I915_CACHE_WT : I915_CACHE_NONE);
Chris Wilson058d88c2016-08-15 10:49:06 +01003720 if (ret) {
3721 vma = ERR_PTR(ret);
Chris Wilsoncc98b412013-08-09 12:25:09 +01003722 goto err_unpin_display;
Chris Wilson058d88c2016-08-15 10:49:06 +01003723 }
Eric Anholta7ef0642011-03-29 16:59:54 -07003724
Chris Wilson2da3b9b2011-04-14 09:41:17 +01003725 /* As the user may map the buffer once pinned in the display plane
3726 * (e.g. libkms for the bootup splash), we have to ensure that we
Chris Wilson2efb8132016-08-18 17:17:06 +01003727 * always use map_and_fenceable for all scanout buffers. However,
3728 * it may simply be too big to fit into mappable, in which case
3729 * put it anyway and hope that userspace can cope (but always first
3730 * try to preserve the existing ABI).
Chris Wilson2da3b9b2011-04-14 09:41:17 +01003731 */
Chris Wilson2efb8132016-08-18 17:17:06 +01003732 vma = ERR_PTR(-ENOSPC);
3733 if (view->type == I915_GGTT_VIEW_NORMAL)
3734 vma = i915_gem_object_ggtt_pin(obj, view, 0, alignment,
3735 PIN_MAPPABLE | PIN_NONBLOCK);
3736 if (IS_ERR(vma))
3737 vma = i915_gem_object_ggtt_pin(obj, view, 0, alignment, 0);
Chris Wilson058d88c2016-08-15 10:49:06 +01003738 if (IS_ERR(vma))
Chris Wilsoncc98b412013-08-09 12:25:09 +01003739 goto err_unpin_display;
Chris Wilson2da3b9b2011-04-14 09:41:17 +01003740
Chris Wilsond8923dc2016-08-18 17:17:07 +01003741 vma->display_alignment = max_t(u64, vma->display_alignment, alignment);
3742
Daniel Vettere62b59e2015-01-21 14:53:48 +01003743 i915_gem_object_flush_cpu_write_domain(obj);
Chris Wilsonb118c1e2010-05-27 13:18:14 +01003744
Chris Wilson2da3b9b2011-04-14 09:41:17 +01003745 old_write_domain = obj->base.write_domain;
Chris Wilson05394f32010-11-08 19:18:58 +00003746 old_read_domains = obj->base.read_domains;
Chris Wilson2da3b9b2011-04-14 09:41:17 +01003747
3748 /* It should now be out of any other write domains, and we can update
3749 * the domain values for our changes.
3750 */
Chris Wilsone5f1d962012-07-20 12:41:00 +01003751 obj->base.write_domain = 0;
Chris Wilson05394f32010-11-08 19:18:58 +00003752 obj->base.read_domains |= I915_GEM_DOMAIN_GTT;
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003753
3754 trace_i915_gem_object_change_domain(obj,
3755 old_read_domains,
Chris Wilson2da3b9b2011-04-14 09:41:17 +01003756 old_write_domain);
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003757
Chris Wilson058d88c2016-08-15 10:49:06 +01003758 return vma;
Chris Wilsoncc98b412013-08-09 12:25:09 +01003759
3760err_unpin_display:
Tvrtko Ursulin8a0c39b2015-04-13 11:50:09 +01003761 obj->pin_display--;
Chris Wilson058d88c2016-08-15 10:49:06 +01003762 return vma;
Chris Wilsoncc98b412013-08-09 12:25:09 +01003763}
3764
3765void
Chris Wilson058d88c2016-08-15 10:49:06 +01003766i915_gem_object_unpin_from_display_plane(struct i915_vma *vma)
Chris Wilsoncc98b412013-08-09 12:25:09 +01003767{
Chris Wilson058d88c2016-08-15 10:49:06 +01003768 if (WARN_ON(vma->obj->pin_display == 0))
Tvrtko Ursulin8a0c39b2015-04-13 11:50:09 +01003769 return;
3770
Chris Wilsond8923dc2016-08-18 17:17:07 +01003771 if (--vma->obj->pin_display == 0)
3772 vma->display_alignment = 0;
Tvrtko Ursuline6617332015-03-23 11:10:33 +00003773
Chris Wilson383d5822016-08-18 17:17:08 +01003774 /* Bump the LRU to try and avoid premature eviction whilst flipping */
3775 if (!i915_vma_is_active(vma))
3776 list_move_tail(&vma->vm_link, &vma->vm->inactive_list);
3777
Chris Wilson058d88c2016-08-15 10:49:06 +01003778 i915_vma_unpin(vma);
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003779}
3780
Eric Anholte47c68e2008-11-14 13:35:19 -08003781/**
3782 * Moves a single object to the CPU read, and possibly write domain.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01003783 * @obj: object to act on
3784 * @write: requesting write or read-only access
Eric Anholte47c68e2008-11-14 13:35:19 -08003785 *
3786 * This function returns when the move is complete, including waiting on
3787 * flushes to occur.
3788 */
Chris Wilsondabdfe02012-03-26 10:10:27 +02003789int
Chris Wilson919926a2010-11-12 13:42:53 +00003790i915_gem_object_set_to_cpu_domain(struct drm_i915_gem_object *obj, bool write)
Eric Anholte47c68e2008-11-14 13:35:19 -08003791{
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003792 uint32_t old_write_domain, old_read_domains;
Eric Anholte47c68e2008-11-14 13:35:19 -08003793 int ret;
3794
Chris Wilsone95433c2016-10-28 13:58:27 +01003795 lockdep_assert_held(&obj->base.dev->struct_mutex);
3796 ret = i915_gem_object_wait(obj,
3797 I915_WAIT_INTERRUPTIBLE |
3798 I915_WAIT_LOCKED |
3799 (write ? I915_WAIT_ALL : 0),
3800 MAX_SCHEDULE_TIMEOUT,
3801 NULL);
Chris Wilson88241782011-01-07 17:09:48 +00003802 if (ret)
3803 return ret;
3804
Chris Wilsonc13d87e2016-07-20 09:21:15 +01003805 if (obj->base.write_domain == I915_GEM_DOMAIN_CPU)
3806 return 0;
3807
Eric Anholte47c68e2008-11-14 13:35:19 -08003808 i915_gem_object_flush_gtt_write_domain(obj);
3809
Chris Wilson05394f32010-11-08 19:18:58 +00003810 old_write_domain = obj->base.write_domain;
3811 old_read_domains = obj->base.read_domains;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003812
Eric Anholte47c68e2008-11-14 13:35:19 -08003813 /* Flush the CPU cache if it's still invalid. */
Chris Wilson05394f32010-11-08 19:18:58 +00003814 if ((obj->base.read_domains & I915_GEM_DOMAIN_CPU) == 0) {
Chris Wilson2c225692013-08-09 12:26:45 +01003815 i915_gem_clflush_object(obj, false);
Eric Anholte47c68e2008-11-14 13:35:19 -08003816
Chris Wilson05394f32010-11-08 19:18:58 +00003817 obj->base.read_domains |= I915_GEM_DOMAIN_CPU;
Eric Anholte47c68e2008-11-14 13:35:19 -08003818 }
3819
3820 /* It should now be out of any other write domains, and we can update
3821 * the domain values for our changes.
3822 */
Chris Wilson05394f32010-11-08 19:18:58 +00003823 BUG_ON((obj->base.write_domain & ~I915_GEM_DOMAIN_CPU) != 0);
Eric Anholte47c68e2008-11-14 13:35:19 -08003824
3825 /* If we're writing through the CPU, then the GPU read domains will
3826 * need to be invalidated at next use.
3827 */
3828 if (write) {
Chris Wilson05394f32010-11-08 19:18:58 +00003829 obj->base.read_domains = I915_GEM_DOMAIN_CPU;
3830 obj->base.write_domain = I915_GEM_DOMAIN_CPU;
Eric Anholte47c68e2008-11-14 13:35:19 -08003831 }
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003832
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003833 trace_i915_gem_object_change_domain(obj,
3834 old_read_domains,
3835 old_write_domain);
3836
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003837 return 0;
3838}
3839
Eric Anholt673a3942008-07-30 12:06:12 -07003840/* Throttle our rendering by waiting until the ring has completed our requests
3841 * emitted over 20 msec ago.
3842 *
Eric Anholtb9624422009-06-03 07:27:35 +00003843 * Note that if we were to use the current jiffies each time around the loop,
3844 * we wouldn't escape the function with any frames outstanding if the time to
3845 * render a frame was over 20ms.
3846 *
Eric Anholt673a3942008-07-30 12:06:12 -07003847 * This should get us reasonable parallelism between CPU and GPU but also
3848 * relatively low latency when blocking on a particular request to finish.
3849 */
3850static int
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003851i915_gem_ring_throttle(struct drm_device *dev, struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07003852{
Chris Wilsonfac5e232016-07-04 11:34:36 +01003853 struct drm_i915_private *dev_priv = to_i915(dev);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003854 struct drm_i915_file_private *file_priv = file->driver_priv;
Chris Wilsond0bc54f2015-05-21 21:01:48 +01003855 unsigned long recent_enough = jiffies - DRM_I915_THROTTLE_JIFFIES;
John Harrison54fb2412014-11-24 18:49:27 +00003856 struct drm_i915_gem_request *request, *target = NULL;
Chris Wilsone95433c2016-10-28 13:58:27 +01003857 long ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003858
Chris Wilsonf4457ae2016-04-13 17:35:08 +01003859 /* ABI: return -EIO if already wedged */
3860 if (i915_terminally_wedged(&dev_priv->gpu_error))
3861 return -EIO;
Chris Wilsone110e8d2011-01-26 15:39:14 +00003862
Chris Wilson1c255952010-09-26 11:03:27 +01003863 spin_lock(&file_priv->mm.lock);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003864 list_for_each_entry(request, &file_priv->mm.request_list, client_list) {
Eric Anholtb9624422009-06-03 07:27:35 +00003865 if (time_after_eq(request->emitted_jiffies, recent_enough))
3866 break;
3867
John Harrisonfcfa423c2015-05-29 17:44:12 +01003868 /*
3869 * Note that the request might not have been submitted yet.
3870 * In which case emitted_jiffies will be zero.
3871 */
3872 if (!request->emitted_jiffies)
3873 continue;
3874
John Harrison54fb2412014-11-24 18:49:27 +00003875 target = request;
Eric Anholtb9624422009-06-03 07:27:35 +00003876 }
John Harrisonff865882014-11-24 18:49:28 +00003877 if (target)
Chris Wilsone8a261e2016-07-20 13:31:49 +01003878 i915_gem_request_get(target);
Chris Wilson1c255952010-09-26 11:03:27 +01003879 spin_unlock(&file_priv->mm.lock);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003880
John Harrison54fb2412014-11-24 18:49:27 +00003881 if (target == NULL)
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003882 return 0;
3883
Chris Wilsone95433c2016-10-28 13:58:27 +01003884 ret = i915_wait_request(target,
3885 I915_WAIT_INTERRUPTIBLE,
3886 MAX_SCHEDULE_TIMEOUT);
Chris Wilsone8a261e2016-07-20 13:31:49 +01003887 i915_gem_request_put(target);
John Harrisonff865882014-11-24 18:49:28 +00003888
Chris Wilsone95433c2016-10-28 13:58:27 +01003889 return ret < 0 ? ret : 0;
Eric Anholt673a3942008-07-30 12:06:12 -07003890}
3891
Chris Wilsond23db882014-05-23 08:48:08 +02003892static bool
Chris Wilson91b2db62016-08-04 16:32:23 +01003893i915_vma_misplaced(struct i915_vma *vma, u64 size, u64 alignment, u64 flags)
Chris Wilsond23db882014-05-23 08:48:08 +02003894{
Chris Wilson59bfa122016-08-04 16:32:31 +01003895 if (!drm_mm_node_allocated(&vma->node))
3896 return false;
3897
Chris Wilson91b2db62016-08-04 16:32:23 +01003898 if (vma->node.size < size)
3899 return true;
3900
3901 if (alignment && vma->node.start & (alignment - 1))
Chris Wilsond23db882014-05-23 08:48:08 +02003902 return true;
3903
Chris Wilson05a20d02016-08-18 17:16:55 +01003904 if (flags & PIN_MAPPABLE && !i915_vma_is_map_and_fenceable(vma))
Chris Wilsond23db882014-05-23 08:48:08 +02003905 return true;
3906
3907 if (flags & PIN_OFFSET_BIAS &&
3908 vma->node.start < (flags & PIN_OFFSET_MASK))
3909 return true;
3910
Chris Wilson506a8e82015-12-08 11:55:07 +00003911 if (flags & PIN_OFFSET_FIXED &&
3912 vma->node.start != (flags & PIN_OFFSET_MASK))
3913 return true;
3914
Chris Wilsond23db882014-05-23 08:48:08 +02003915 return false;
3916}
3917
Chris Wilsond0710ab2015-11-20 14:16:39 +00003918void __i915_vma_set_map_and_fenceable(struct i915_vma *vma)
3919{
3920 struct drm_i915_gem_object *obj = vma->obj;
Chris Wilsona9f14812016-08-04 16:32:28 +01003921 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
Chris Wilsond0710ab2015-11-20 14:16:39 +00003922 bool mappable, fenceable;
3923 u32 fence_size, fence_alignment;
3924
Chris Wilsona9f14812016-08-04 16:32:28 +01003925 fence_size = i915_gem_get_ggtt_size(dev_priv,
Chris Wilson05a20d02016-08-18 17:16:55 +01003926 vma->size,
Chris Wilson3e510a82016-08-05 10:14:23 +01003927 i915_gem_object_get_tiling(obj));
Chris Wilsona9f14812016-08-04 16:32:28 +01003928 fence_alignment = i915_gem_get_ggtt_alignment(dev_priv,
Chris Wilson05a20d02016-08-18 17:16:55 +01003929 vma->size,
Chris Wilson3e510a82016-08-05 10:14:23 +01003930 i915_gem_object_get_tiling(obj),
Chris Wilsonad1a7d22016-08-04 16:32:27 +01003931 true);
Chris Wilsond0710ab2015-11-20 14:16:39 +00003932
3933 fenceable = (vma->node.size == fence_size &&
3934 (vma->node.start & (fence_alignment - 1)) == 0);
3935
3936 mappable = (vma->node.start + fence_size <=
Chris Wilsona9f14812016-08-04 16:32:28 +01003937 dev_priv->ggtt.mappable_end);
Chris Wilsond0710ab2015-11-20 14:16:39 +00003938
Tvrtko Ursulin07ee2bc2016-10-25 17:40:35 +01003939 /*
3940 * Explicitly disable for rotated VMA since the display does not
3941 * need the fence and the VMA is not accessible to other users.
3942 */
3943 if (mappable && fenceable &&
3944 vma->ggtt_view.type != I915_GGTT_VIEW_ROTATED)
Chris Wilson05a20d02016-08-18 17:16:55 +01003945 vma->flags |= I915_VMA_CAN_FENCE;
3946 else
3947 vma->flags &= ~I915_VMA_CAN_FENCE;
Chris Wilsond0710ab2015-11-20 14:16:39 +00003948}
3949
Chris Wilson305bc232016-08-04 16:32:33 +01003950int __i915_vma_do_pin(struct i915_vma *vma,
3951 u64 size, u64 alignment, u64 flags)
Eric Anholt673a3942008-07-30 12:06:12 -07003952{
Chris Wilson305bc232016-08-04 16:32:33 +01003953 unsigned int bound = vma->flags;
Eric Anholt673a3942008-07-30 12:06:12 -07003954 int ret;
3955
Chris Wilson59bfa122016-08-04 16:32:31 +01003956 GEM_BUG_ON((flags & (PIN_GLOBAL | PIN_USER)) == 0);
Chris Wilson3272db52016-08-04 16:32:32 +01003957 GEM_BUG_ON((flags & PIN_GLOBAL) && !i915_vma_is_ggtt(vma));
Ben Widawsky6e7186a2014-05-06 22:21:36 -07003958
Chris Wilson305bc232016-08-04 16:32:33 +01003959 if (WARN_ON(bound & I915_VMA_PIN_OVERFLOW)) {
3960 ret = -EBUSY;
3961 goto err;
3962 }
Chris Wilsonc826c442014-10-31 13:53:53 +00003963
Chris Wilsonde895082016-08-04 16:32:34 +01003964 if ((bound & I915_VMA_BIND_MASK) == 0) {
Chris Wilson59bfa122016-08-04 16:32:31 +01003965 ret = i915_vma_insert(vma, size, alignment, flags);
3966 if (ret)
3967 goto err;
Chris Wilsonac0c6b52010-05-27 13:18:18 +01003968 }
3969
Chris Wilson59bfa122016-08-04 16:32:31 +01003970 ret = i915_vma_bind(vma, vma->obj->cache_level, flags);
Chris Wilson3b165252016-08-04 16:32:25 +01003971 if (ret)
Chris Wilson59bfa122016-08-04 16:32:31 +01003972 goto err;
Chris Wilson3b165252016-08-04 16:32:25 +01003973
Chris Wilson3272db52016-08-04 16:32:32 +01003974 if ((bound ^ vma->flags) & I915_VMA_GLOBAL_BIND)
Chris Wilsond0710ab2015-11-20 14:16:39 +00003975 __i915_vma_set_map_and_fenceable(vma);
Chris Wilsonef79e172014-10-31 13:53:52 +00003976
Chris Wilson3b165252016-08-04 16:32:25 +01003977 GEM_BUG_ON(i915_vma_misplaced(vma, size, alignment, flags));
Eric Anholt673a3942008-07-30 12:06:12 -07003978 return 0;
Eric Anholt673a3942008-07-30 12:06:12 -07003979
Chris Wilson59bfa122016-08-04 16:32:31 +01003980err:
3981 __i915_vma_unpin(vma);
3982 return ret;
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02003983}
3984
Chris Wilson058d88c2016-08-15 10:49:06 +01003985struct i915_vma *
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02003986i915_gem_object_ggtt_pin(struct drm_i915_gem_object *obj,
3987 const struct i915_ggtt_view *view,
Chris Wilson91b2db62016-08-04 16:32:23 +01003988 u64 size,
Chris Wilson2ffffd02016-08-04 16:32:22 +01003989 u64 alignment,
3990 u64 flags)
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02003991{
Chris Wilsonad16d2e2016-10-13 09:55:04 +01003992 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
3993 struct i915_address_space *vm = &dev_priv->ggtt.base;
Chris Wilson59bfa122016-08-04 16:32:31 +01003994 struct i915_vma *vma;
3995 int ret;
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03003996
Chris Wilson058d88c2016-08-15 10:49:06 +01003997 vma = i915_gem_obj_lookup_or_create_vma(obj, vm, view);
Chris Wilson59bfa122016-08-04 16:32:31 +01003998 if (IS_ERR(vma))
Chris Wilson058d88c2016-08-15 10:49:06 +01003999 return vma;
Chris Wilson59bfa122016-08-04 16:32:31 +01004000
4001 if (i915_vma_misplaced(vma, size, alignment, flags)) {
4002 if (flags & PIN_NONBLOCK &&
4003 (i915_vma_is_pinned(vma) || i915_vma_is_active(vma)))
Chris Wilson058d88c2016-08-15 10:49:06 +01004004 return ERR_PTR(-ENOSPC);
Chris Wilson59bfa122016-08-04 16:32:31 +01004005
Chris Wilsonad16d2e2016-10-13 09:55:04 +01004006 if (flags & PIN_MAPPABLE) {
4007 u32 fence_size;
4008
4009 fence_size = i915_gem_get_ggtt_size(dev_priv, vma->size,
4010 i915_gem_object_get_tiling(obj));
4011 /* If the required space is larger than the available
4012 * aperture, we will not able to find a slot for the
4013 * object and unbinding the object now will be in
4014 * vain. Worse, doing so may cause us to ping-pong
4015 * the object in and out of the Global GTT and
4016 * waste a lot of cycles under the mutex.
4017 */
4018 if (fence_size > dev_priv->ggtt.mappable_end)
4019 return ERR_PTR(-E2BIG);
4020
4021 /* If NONBLOCK is set the caller is optimistically
4022 * trying to cache the full object within the mappable
4023 * aperture, and *must* have a fallback in place for
4024 * situations where we cannot bind the object. We
4025 * can be a little more lax here and use the fallback
4026 * more often to avoid costly migrations of ourselves
4027 * and other objects within the aperture.
4028 *
4029 * Half-the-aperture is used as a simple heuristic.
4030 * More interesting would to do search for a free
4031 * block prior to making the commitment to unbind.
4032 * That caters for the self-harm case, and with a
4033 * little more heuristics (e.g. NOFAULT, NOEVICT)
4034 * we could try to minimise harm to others.
4035 */
4036 if (flags & PIN_NONBLOCK &&
4037 fence_size > dev_priv->ggtt.mappable_end / 2)
4038 return ERR_PTR(-ENOSPC);
4039 }
4040
Chris Wilson59bfa122016-08-04 16:32:31 +01004041 WARN(i915_vma_is_pinned(vma),
4042 "bo is already pinned in ggtt with incorrect alignment:"
Chris Wilson05a20d02016-08-18 17:16:55 +01004043 " offset=%08x, req.alignment=%llx,"
4044 " req.map_and_fenceable=%d, vma->map_and_fenceable=%d\n",
4045 i915_ggtt_offset(vma), alignment,
Chris Wilson59bfa122016-08-04 16:32:31 +01004046 !!(flags & PIN_MAPPABLE),
Chris Wilson05a20d02016-08-18 17:16:55 +01004047 i915_vma_is_map_and_fenceable(vma));
Chris Wilson59bfa122016-08-04 16:32:31 +01004048 ret = i915_vma_unbind(vma);
4049 if (ret)
Chris Wilson058d88c2016-08-15 10:49:06 +01004050 return ERR_PTR(ret);
Chris Wilson59bfa122016-08-04 16:32:31 +01004051 }
4052
Chris Wilson058d88c2016-08-15 10:49:06 +01004053 ret = i915_vma_pin(vma, size, alignment, flags | PIN_GLOBAL);
4054 if (ret)
4055 return ERR_PTR(ret);
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02004056
Chris Wilson058d88c2016-08-15 10:49:06 +01004057 return vma;
Eric Anholt673a3942008-07-30 12:06:12 -07004058}
4059
Chris Wilsonedf6b762016-08-09 09:23:33 +01004060static __always_inline unsigned int __busy_read_flag(unsigned int id)
Chris Wilson3fdc13c2016-08-05 10:14:18 +01004061{
4062 /* Note that we could alias engines in the execbuf API, but
4063 * that would be very unwise as it prevents userspace from
4064 * fine control over engine selection. Ahem.
4065 *
4066 * This should be something like EXEC_MAX_ENGINE instead of
4067 * I915_NUM_ENGINES.
4068 */
4069 BUILD_BUG_ON(I915_NUM_ENGINES > 16);
4070 return 0x10000 << id;
4071}
4072
4073static __always_inline unsigned int __busy_write_id(unsigned int id)
4074{
Chris Wilson70cb4722016-08-09 18:08:25 +01004075 /* The uABI guarantees an active writer is also amongst the read
4076 * engines. This would be true if we accessed the activity tracking
4077 * under the lock, but as we perform the lookup of the object and
4078 * its activity locklessly we can not guarantee that the last_write
4079 * being active implies that we have set the same engine flag from
4080 * last_read - hence we always set both read and write busy for
4081 * last_write.
4082 */
4083 return id | __busy_read_flag(id);
Chris Wilson3fdc13c2016-08-05 10:14:18 +01004084}
4085
Chris Wilsonedf6b762016-08-09 09:23:33 +01004086static __always_inline unsigned int
Chris Wilson3fdc13c2016-08-05 10:14:18 +01004087__busy_set_if_active(const struct i915_gem_active *active,
4088 unsigned int (*flag)(unsigned int id))
4089{
Chris Wilson12555012016-08-16 09:50:40 +01004090 struct drm_i915_gem_request *request;
4091
4092 request = rcu_dereference(active->request);
4093 if (!request || i915_gem_request_completed(request))
4094 return 0;
4095
4096 /* This is racy. See __i915_gem_active_get_rcu() for an in detail
4097 * discussion of how to handle the race correctly, but for reporting
4098 * the busy state we err on the side of potentially reporting the
4099 * wrong engine as being busy (but we guarantee that the result
4100 * is at least self-consistent).
4101 *
4102 * As we use SLAB_DESTROY_BY_RCU, the request may be reallocated
4103 * whilst we are inspecting it, even under the RCU read lock as we are.
4104 * This means that there is a small window for the engine and/or the
4105 * seqno to have been overwritten. The seqno will always be in the
4106 * future compared to the intended, and so we know that if that
4107 * seqno is idle (on whatever engine) our request is idle and the
4108 * return 0 above is correct.
4109 *
4110 * The issue is that if the engine is switched, it is just as likely
4111 * to report that it is busy (but since the switch happened, we know
4112 * the request should be idle). So there is a small chance that a busy
4113 * result is actually the wrong engine.
4114 *
4115 * So why don't we care?
4116 *
4117 * For starters, the busy ioctl is a heuristic that is by definition
4118 * racy. Even with perfect serialisation in the driver, the hardware
4119 * state is constantly advancing - the state we report to the user
4120 * is stale.
4121 *
4122 * The critical information for the busy-ioctl is whether the object
4123 * is idle as userspace relies on that to detect whether its next
4124 * access will stall, or if it has missed submitting commands to
4125 * the hardware allowing the GPU to stall. We never generate a
4126 * false-positive for idleness, thus busy-ioctl is reliable at the
4127 * most fundamental level, and we maintain the guarantee that a
4128 * busy object left to itself will eventually become idle (and stay
4129 * idle!).
4130 *
4131 * We allow ourselves the leeway of potentially misreporting the busy
4132 * state because that is an optimisation heuristic that is constantly
4133 * in flux. Being quickly able to detect the busy/idle state is much
4134 * more important than accurate logging of exactly which engines were
4135 * busy.
4136 *
4137 * For accuracy in reporting the engine, we could use
4138 *
4139 * result = 0;
4140 * request = __i915_gem_active_get_rcu(active);
4141 * if (request) {
4142 * if (!i915_gem_request_completed(request))
4143 * result = flag(request->engine->exec_id);
4144 * i915_gem_request_put(request);
4145 * }
4146 *
4147 * but that still remains susceptible to both hardware and userspace
4148 * races. So we accept making the result of that race slightly worse,
4149 * given the rarity of the race and its low impact on the result.
Chris Wilson3fdc13c2016-08-05 10:14:18 +01004150 */
Chris Wilson12555012016-08-16 09:50:40 +01004151 return flag(READ_ONCE(request->engine->exec_id));
Chris Wilson3fdc13c2016-08-05 10:14:18 +01004152}
4153
Chris Wilsonedf6b762016-08-09 09:23:33 +01004154static __always_inline unsigned int
Chris Wilson3fdc13c2016-08-05 10:14:18 +01004155busy_check_reader(const struct i915_gem_active *active)
4156{
4157 return __busy_set_if_active(active, __busy_read_flag);
4158}
4159
Chris Wilsonedf6b762016-08-09 09:23:33 +01004160static __always_inline unsigned int
Chris Wilson3fdc13c2016-08-05 10:14:18 +01004161busy_check_writer(const struct i915_gem_active *active)
4162{
4163 return __busy_set_if_active(active, __busy_write_id);
4164}
4165
Eric Anholt673a3942008-07-30 12:06:12 -07004166int
Eric Anholt673a3942008-07-30 12:06:12 -07004167i915_gem_busy_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00004168 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07004169{
4170 struct drm_i915_gem_busy *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00004171 struct drm_i915_gem_object *obj;
Chris Wilson3fdc13c2016-08-05 10:14:18 +01004172 unsigned long active;
Eric Anholt673a3942008-07-30 12:06:12 -07004173
Chris Wilson03ac0642016-07-20 13:31:51 +01004174 obj = i915_gem_object_lookup(file, args->handle);
Chris Wilson3fdc13c2016-08-05 10:14:18 +01004175 if (!obj)
4176 return -ENOENT;
Zou Nan haid1b851f2010-05-21 09:08:57 +08004177
Chris Wilson426960b2016-01-15 16:51:46 +00004178 args->busy = 0;
Chris Wilson3fdc13c2016-08-05 10:14:18 +01004179 active = __I915_BO_ACTIVE(obj);
4180 if (active) {
4181 int idx;
Chris Wilson426960b2016-01-15 16:51:46 +00004182
Chris Wilson3fdc13c2016-08-05 10:14:18 +01004183 /* Yes, the lookups are intentionally racy.
4184 *
4185 * First, we cannot simply rely on __I915_BO_ACTIVE. We have
4186 * to regard the value as stale and as our ABI guarantees
4187 * forward progress, we confirm the status of each active
4188 * request with the hardware.
4189 *
4190 * Even though we guard the pointer lookup by RCU, that only
4191 * guarantees that the pointer and its contents remain
4192 * dereferencable and does *not* mean that the request we
4193 * have is the same as the one being tracked by the object.
4194 *
4195 * Consider that we lookup the request just as it is being
4196 * retired and freed. We take a local copy of the pointer,
4197 * but before we add its engine into the busy set, the other
4198 * thread reallocates it and assigns it to a task on another
Chris Wilson12555012016-08-16 09:50:40 +01004199 * engine with a fresh and incomplete seqno. Guarding against
4200 * that requires careful serialisation and reference counting,
4201 * i.e. using __i915_gem_active_get_request_rcu(). We don't,
4202 * instead we expect that if the result is busy, which engines
4203 * are busy is not completely reliable - we only guarantee
4204 * that the object was busy.
Chris Wilson3fdc13c2016-08-05 10:14:18 +01004205 */
4206 rcu_read_lock();
4207
4208 for_each_active(active, idx)
4209 args->busy |= busy_check_reader(&obj->last_read[idx]);
4210
4211 /* For ABI sanity, we only care that the write engine is in
Chris Wilson70cb4722016-08-09 18:08:25 +01004212 * the set of read engines. This should be ensured by the
4213 * ordering of setting last_read/last_write in
4214 * i915_vma_move_to_active(), and then in reverse in retire.
4215 * However, for good measure, we always report the last_write
4216 * request as a busy read as well as being a busy write.
Chris Wilson3fdc13c2016-08-05 10:14:18 +01004217 *
4218 * We don't care that the set of active read/write engines
4219 * may change during construction of the result, as it is
4220 * equally liable to change before userspace can inspect
4221 * the result.
4222 */
4223 args->busy |= busy_check_writer(&obj->last_write);
4224
4225 rcu_read_unlock();
Chris Wilson426960b2016-01-15 16:51:46 +00004226 }
Eric Anholt673a3942008-07-30 12:06:12 -07004227
Chris Wilson3fdc13c2016-08-05 10:14:18 +01004228 i915_gem_object_put_unlocked(obj);
4229 return 0;
Eric Anholt673a3942008-07-30 12:06:12 -07004230}
4231
4232int
4233i915_gem_throttle_ioctl(struct drm_device *dev, void *data,
4234 struct drm_file *file_priv)
4235{
Akshay Joshi0206e352011-08-16 15:34:10 -04004236 return i915_gem_ring_throttle(dev, file_priv);
Eric Anholt673a3942008-07-30 12:06:12 -07004237}
4238
Chris Wilson3ef94da2009-09-14 16:50:29 +01004239int
4240i915_gem_madvise_ioctl(struct drm_device *dev, void *data,
4241 struct drm_file *file_priv)
4242{
Chris Wilsonfac5e232016-07-04 11:34:36 +01004243 struct drm_i915_private *dev_priv = to_i915(dev);
Chris Wilson3ef94da2009-09-14 16:50:29 +01004244 struct drm_i915_gem_madvise *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00004245 struct drm_i915_gem_object *obj;
Chris Wilson76c1dec2010-09-25 11:22:51 +01004246 int ret;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004247
4248 switch (args->madv) {
4249 case I915_MADV_DONTNEED:
4250 case I915_MADV_WILLNEED:
4251 break;
4252 default:
4253 return -EINVAL;
4254 }
4255
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004256 ret = i915_mutex_lock_interruptible(dev);
4257 if (ret)
4258 return ret;
4259
Chris Wilson03ac0642016-07-20 13:31:51 +01004260 obj = i915_gem_object_lookup(file_priv, args->handle);
4261 if (!obj) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004262 ret = -ENOENT;
4263 goto unlock;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004264 }
Chris Wilson3ef94da2009-09-14 16:50:29 +01004265
Daniel Vetter656bfa32014-11-20 09:26:30 +01004266 if (obj->pages &&
Chris Wilson3e510a82016-08-05 10:14:23 +01004267 i915_gem_object_is_tiled(obj) &&
Daniel Vetter656bfa32014-11-20 09:26:30 +01004268 dev_priv->quirks & QUIRK_PIN_SWIZZLED_PAGES) {
4269 if (obj->madv == I915_MADV_WILLNEED)
4270 i915_gem_object_unpin_pages(obj);
4271 if (args->madv == I915_MADV_WILLNEED)
4272 i915_gem_object_pin_pages(obj);
4273 }
4274
Chris Wilson05394f32010-11-08 19:18:58 +00004275 if (obj->madv != __I915_MADV_PURGED)
4276 obj->madv = args->madv;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004277
Chris Wilson6c085a72012-08-20 11:40:46 +02004278 /* if the object is no longer attached, discard its backing storage */
Daniel Vetterbe6a0372015-03-18 10:46:04 +01004279 if (obj->madv == I915_MADV_DONTNEED && obj->pages == NULL)
Chris Wilson2d7ef392009-09-20 23:13:10 +01004280 i915_gem_object_truncate(obj);
4281
Chris Wilson05394f32010-11-08 19:18:58 +00004282 args->retained = obj->madv != __I915_MADV_PURGED;
Chris Wilsonbb6baf72009-09-22 14:24:13 +01004283
Chris Wilsonf8c417c2016-07-20 13:31:53 +01004284 i915_gem_object_put(obj);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004285unlock:
Chris Wilson3ef94da2009-09-14 16:50:29 +01004286 mutex_unlock(&dev->struct_mutex);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004287 return ret;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004288}
4289
Chris Wilson37e680a2012-06-07 15:38:42 +01004290void i915_gem_object_init(struct drm_i915_gem_object *obj,
4291 const struct drm_i915_gem_object_ops *ops)
Chris Wilson0327d6b2012-08-11 15:41:06 +01004292{
Chris Wilsonb4716182015-04-27 13:41:17 +01004293 int i;
4294
Ben Widawsky35c20a62013-05-31 11:28:48 -07004295 INIT_LIST_HEAD(&obj->global_list);
Chris Wilson275f0392016-10-24 13:42:14 +01004296 INIT_LIST_HEAD(&obj->userfault_link);
Tvrtko Ursulin666796d2016-03-16 11:00:39 +00004297 for (i = 0; i < I915_NUM_ENGINES; i++)
Chris Wilsonfa545cb2016-08-04 07:52:35 +01004298 init_request_active(&obj->last_read[i],
4299 i915_gem_object_retire__read);
4300 init_request_active(&obj->last_write,
4301 i915_gem_object_retire__write);
Ben Widawskyb25cb2f2013-08-14 11:38:33 +02004302 INIT_LIST_HEAD(&obj->obj_exec_link);
Ben Widawsky2f633152013-07-17 12:19:03 -07004303 INIT_LIST_HEAD(&obj->vma_list);
Chris Wilson8d9d5742015-04-07 16:20:38 +01004304 INIT_LIST_HEAD(&obj->batch_pool_link);
Chris Wilson0327d6b2012-08-11 15:41:06 +01004305
Chris Wilson37e680a2012-06-07 15:38:42 +01004306 obj->ops = ops;
4307
Chris Wilson50349242016-08-18 17:17:04 +01004308 obj->frontbuffer_ggtt_origin = ORIGIN_GTT;
Chris Wilson0327d6b2012-08-11 15:41:06 +01004309 obj->madv = I915_MADV_WILLNEED;
Chris Wilson0327d6b2012-08-11 15:41:06 +01004310
Dave Gordonf19ec8c2016-07-04 11:34:37 +01004311 i915_gem_info_add_obj(to_i915(obj->base.dev), obj->base.size);
Chris Wilson0327d6b2012-08-11 15:41:06 +01004312}
4313
Chris Wilson37e680a2012-06-07 15:38:42 +01004314static const struct drm_i915_gem_object_ops i915_gem_object_ops = {
Chris Wilsonde472662016-01-22 18:32:31 +00004315 .flags = I915_GEM_OBJECT_HAS_STRUCT_PAGE,
Chris Wilson37e680a2012-06-07 15:38:42 +01004316 .get_pages = i915_gem_object_get_pages_gtt,
4317 .put_pages = i915_gem_object_put_pages_gtt,
4318};
4319
Chris Wilsonb4bcbe22016-10-18 13:02:49 +01004320/* Note we don't consider signbits :| */
4321#define overflows_type(x, T) \
4322 (sizeof(x) > sizeof(T) && (x) >> (sizeof(T) * BITS_PER_BYTE))
4323
4324struct drm_i915_gem_object *
4325i915_gem_object_create(struct drm_device *dev, u64 size)
Daniel Vetterac52bc52010-04-09 19:05:06 +00004326{
Daniel Vetterc397b902010-04-09 19:05:07 +00004327 struct drm_i915_gem_object *obj;
Hugh Dickins5949eac2011-06-27 16:18:18 -07004328 struct address_space *mapping;
Daniel Vetter1a240d42012-11-29 22:18:51 +01004329 gfp_t mask;
Chris Wilsonfe3db792016-04-25 13:32:13 +01004330 int ret;
Daniel Vetterc397b902010-04-09 19:05:07 +00004331
Chris Wilsonb4bcbe22016-10-18 13:02:49 +01004332 /* There is a prevalence of the assumption that we fit the object's
4333 * page count inside a 32bit _signed_ variable. Let's document this and
4334 * catch if we ever need to fix it. In the meantime, if you do spot
4335 * such a local variable, please consider fixing!
4336 */
4337 if (WARN_ON(size >> PAGE_SHIFT > INT_MAX))
4338 return ERR_PTR(-E2BIG);
4339
4340 if (overflows_type(size, obj->base.size))
4341 return ERR_PTR(-E2BIG);
4342
Chris Wilson42dcedd2012-11-15 11:32:30 +00004343 obj = i915_gem_object_alloc(dev);
Daniel Vetterc397b902010-04-09 19:05:07 +00004344 if (obj == NULL)
Chris Wilsonfe3db792016-04-25 13:32:13 +01004345 return ERR_PTR(-ENOMEM);
Daniel Vetterc397b902010-04-09 19:05:07 +00004346
Chris Wilsonfe3db792016-04-25 13:32:13 +01004347 ret = drm_gem_object_init(dev, &obj->base, size);
4348 if (ret)
4349 goto fail;
Daniel Vetterc397b902010-04-09 19:05:07 +00004350
Chris Wilsonbed1ea92012-05-24 20:48:12 +01004351 mask = GFP_HIGHUSER | __GFP_RECLAIMABLE;
4352 if (IS_CRESTLINE(dev) || IS_BROADWATER(dev)) {
4353 /* 965gm cannot relocate objects above 4GiB. */
4354 mask &= ~__GFP_HIGHMEM;
4355 mask |= __GFP_DMA32;
4356 }
4357
Al Viro93c76a32015-12-04 23:45:44 -05004358 mapping = obj->base.filp->f_mapping;
Chris Wilsonbed1ea92012-05-24 20:48:12 +01004359 mapping_set_gfp_mask(mapping, mask);
Hugh Dickins5949eac2011-06-27 16:18:18 -07004360
Chris Wilson37e680a2012-06-07 15:38:42 +01004361 i915_gem_object_init(obj, &i915_gem_object_ops);
Chris Wilson73aa8082010-09-30 11:46:12 +01004362
Daniel Vetterc397b902010-04-09 19:05:07 +00004363 obj->base.write_domain = I915_GEM_DOMAIN_CPU;
4364 obj->base.read_domains = I915_GEM_DOMAIN_CPU;
4365
Eugeni Dodonov3d29b842012-01-17 14:43:53 -02004366 if (HAS_LLC(dev)) {
4367 /* On some devices, we can have the GPU use the LLC (the CPU
Eric Anholta1871112011-03-29 16:59:55 -07004368 * cache) for about a 10% performance improvement
4369 * compared to uncached. Graphics requests other than
4370 * display scanout are coherent with the CPU in
4371 * accessing this cache. This means in this mode we
4372 * don't need to clflush on the CPU side, and on the
4373 * GPU side we only need to flush internal caches to
4374 * get data visible to the CPU.
4375 *
4376 * However, we maintain the display planes as UC, and so
4377 * need to rebind when first used as such.
4378 */
4379 obj->cache_level = I915_CACHE_LLC;
4380 } else
4381 obj->cache_level = I915_CACHE_NONE;
4382
Daniel Vetterd861e332013-07-24 23:25:03 +02004383 trace_i915_gem_object_create(obj);
4384
Chris Wilson05394f32010-11-08 19:18:58 +00004385 return obj;
Chris Wilsonfe3db792016-04-25 13:32:13 +01004386
4387fail:
4388 i915_gem_object_free(obj);
4389
4390 return ERR_PTR(ret);
Daniel Vetterac52bc52010-04-09 19:05:06 +00004391}
4392
Chris Wilson340fbd82014-05-22 09:16:52 +01004393static bool discard_backing_storage(struct drm_i915_gem_object *obj)
4394{
4395 /* If we are the last user of the backing storage (be it shmemfs
4396 * pages or stolen etc), we know that the pages are going to be
4397 * immediately released. In this case, we can then skip copying
4398 * back the contents from the GPU.
4399 */
4400
4401 if (obj->madv != I915_MADV_WILLNEED)
4402 return false;
4403
4404 if (obj->base.filp == NULL)
4405 return true;
4406
4407 /* At first glance, this looks racy, but then again so would be
4408 * userspace racing mmap against close. However, the first external
4409 * reference to the filp can only be obtained through the
4410 * i915_gem_mmap_ioctl() which safeguards us against the user
4411 * acquiring such a reference whilst we are in the middle of
4412 * freeing the object.
4413 */
4414 return atomic_long_read(&obj->base.filp->f_count) == 1;
4415}
4416
Chris Wilson1488fc02012-04-24 15:47:31 +01004417void i915_gem_free_object(struct drm_gem_object *gem_obj)
Chris Wilsonbe726152010-07-23 23:18:50 +01004418{
Chris Wilson1488fc02012-04-24 15:47:31 +01004419 struct drm_i915_gem_object *obj = to_intel_bo(gem_obj);
Chris Wilson05394f32010-11-08 19:18:58 +00004420 struct drm_device *dev = obj->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01004421 struct drm_i915_private *dev_priv = to_i915(dev);
Ben Widawsky07fe0b12013-07-31 17:00:10 -07004422 struct i915_vma *vma, *next;
Chris Wilsonbe726152010-07-23 23:18:50 +01004423
Paulo Zanonif65c9162013-11-27 18:20:34 -02004424 intel_runtime_pm_get(dev_priv);
4425
Chris Wilson26e12f82011-03-20 11:20:19 +00004426 trace_i915_gem_object_destroy(obj);
4427
Chris Wilsonb1f788c2016-08-04 07:52:45 +01004428 /* All file-owned VMA should have been released by this point through
4429 * i915_gem_close_object(), or earlier by i915_gem_context_close().
4430 * However, the object may also be bound into the global GTT (e.g.
4431 * older GPUs without per-process support, or for direct access through
4432 * the GTT either for the user or for scanout). Those VMA still need to
4433 * unbound now.
4434 */
Chris Wilson1c7f4bc2016-02-26 11:03:19 +00004435 list_for_each_entry_safe(vma, next, &obj->vma_list, obj_link) {
Chris Wilson3272db52016-08-04 16:32:32 +01004436 GEM_BUG_ON(!i915_vma_is_ggtt(vma));
Chris Wilsonb1f788c2016-08-04 07:52:45 +01004437 GEM_BUG_ON(i915_vma_is_active(vma));
Chris Wilson3272db52016-08-04 16:32:32 +01004438 vma->flags &= ~I915_VMA_PIN_MASK;
Chris Wilsonb1f788c2016-08-04 07:52:45 +01004439 i915_vma_close(vma);
Chris Wilson1488fc02012-04-24 15:47:31 +01004440 }
Chris Wilson15717de2016-08-04 07:52:26 +01004441 GEM_BUG_ON(obj->bind_count);
Chris Wilson1488fc02012-04-24 15:47:31 +01004442
Ben Widawsky1d64ae72013-05-31 14:46:20 -07004443 /* Stolen objects don't hold a ref, but do hold pin count. Fix that up
4444 * before progressing. */
4445 if (obj->stolen)
4446 i915_gem_object_unpin_pages(obj);
4447
Chris Wilsonfaf5bf02016-08-04 16:32:37 +01004448 WARN_ON(atomic_read(&obj->frontbuffer_bits));
Daniel Vettera071fa02014-06-18 23:28:09 +02004449
Daniel Vetter656bfa32014-11-20 09:26:30 +01004450 if (obj->pages && obj->madv == I915_MADV_WILLNEED &&
4451 dev_priv->quirks & QUIRK_PIN_SWIZZLED_PAGES &&
Chris Wilson3e510a82016-08-05 10:14:23 +01004452 i915_gem_object_is_tiled(obj))
Daniel Vetter656bfa32014-11-20 09:26:30 +01004453 i915_gem_object_unpin_pages(obj);
4454
Ben Widawsky401c29f2013-05-31 11:28:47 -07004455 if (WARN_ON(obj->pages_pin_count))
4456 obj->pages_pin_count = 0;
Chris Wilson340fbd82014-05-22 09:16:52 +01004457 if (discard_backing_storage(obj))
Chris Wilson55372522014-03-25 13:23:06 +00004458 obj->madv = I915_MADV_DONTNEED;
Chris Wilson37e680a2012-06-07 15:38:42 +01004459 i915_gem_object_put_pages(obj);
Chris Wilsonbe726152010-07-23 23:18:50 +01004460
Chris Wilson9da3da62012-06-01 15:20:22 +01004461 BUG_ON(obj->pages);
4462
Chris Wilson2f745ad2012-09-04 21:02:58 +01004463 if (obj->base.import_attach)
4464 drm_prime_gem_destroy(&obj->base, NULL);
Chris Wilsonbe726152010-07-23 23:18:50 +01004465
Chris Wilson5cc9ed42014-05-16 14:22:37 +01004466 if (obj->ops->release)
4467 obj->ops->release(obj);
4468
Chris Wilson05394f32010-11-08 19:18:58 +00004469 drm_gem_object_release(&obj->base);
4470 i915_gem_info_remove_obj(dev_priv, obj->base.size);
Chris Wilsonbe726152010-07-23 23:18:50 +01004471
Chris Wilson05394f32010-11-08 19:18:58 +00004472 kfree(obj->bit_17);
Chris Wilson42dcedd2012-11-15 11:32:30 +00004473 i915_gem_object_free(obj);
Paulo Zanonif65c9162013-11-27 18:20:34 -02004474
4475 intel_runtime_pm_put(dev_priv);
Chris Wilsonbe726152010-07-23 23:18:50 +01004476}
4477
Chris Wilsondcff85c2016-08-05 10:14:11 +01004478int i915_gem_suspend(struct drm_device *dev)
Eric Anholt673a3942008-07-30 12:06:12 -07004479{
Chris Wilsonfac5e232016-07-04 11:34:36 +01004480 struct drm_i915_private *dev_priv = to_i915(dev);
Chris Wilsondcff85c2016-08-05 10:14:11 +01004481 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004482
Chris Wilson54b4f682016-07-21 21:16:19 +01004483 intel_suspend_gt_powersave(dev_priv);
4484
Chris Wilson45c5f202013-10-16 11:50:01 +01004485 mutex_lock(&dev->struct_mutex);
Chris Wilson5ab57c72016-07-15 14:56:20 +01004486
4487 /* We have to flush all the executing contexts to main memory so
4488 * that they can saved in the hibernation image. To ensure the last
4489 * context image is coherent, we have to switch away from it. That
4490 * leaves the dev_priv->kernel_context still active when
4491 * we actually suspend, and its image in memory may not match the GPU
4492 * state. Fortunately, the kernel_context is disposable and we do
4493 * not rely on its state.
4494 */
4495 ret = i915_gem_switch_to_kernel_context(dev_priv);
4496 if (ret)
4497 goto err;
4498
Chris Wilson22dd3bb2016-09-09 14:11:50 +01004499 ret = i915_gem_wait_for_idle(dev_priv,
4500 I915_WAIT_INTERRUPTIBLE |
4501 I915_WAIT_LOCKED);
Chris Wilsonf7403342013-09-13 23:57:04 +01004502 if (ret)
Chris Wilson45c5f202013-10-16 11:50:01 +01004503 goto err;
Chris Wilsonf7403342013-09-13 23:57:04 +01004504
Chris Wilsonc0336662016-05-06 15:40:21 +01004505 i915_gem_retire_requests(dev_priv);
Eric Anholt673a3942008-07-30 12:06:12 -07004506
Chris Wilsonb2e862d2016-04-28 09:56:41 +01004507 i915_gem_context_lost(dev_priv);
Chris Wilson45c5f202013-10-16 11:50:01 +01004508 mutex_unlock(&dev->struct_mutex);
4509
Chris Wilson737b1502015-01-26 18:03:03 +02004510 cancel_delayed_work_sync(&dev_priv->gpu_error.hangcheck_work);
Chris Wilson67d97da2016-07-04 08:08:31 +01004511 cancel_delayed_work_sync(&dev_priv->gt.retire_work);
4512 flush_delayed_work(&dev_priv->gt.idle_work);
Chris Wilson29105cc2010-01-07 10:39:13 +00004513
Chris Wilsonbdcf1202014-11-25 11:56:33 +00004514 /* Assert that we sucessfully flushed all the work and
4515 * reset the GPU back to its idle, low power state.
4516 */
Chris Wilson67d97da2016-07-04 08:08:31 +01004517 WARN_ON(dev_priv->gt.awake);
Chris Wilsonbdcf1202014-11-25 11:56:33 +00004518
Imre Deak1c777c52016-10-12 17:46:37 +03004519 /*
4520 * Neither the BIOS, ourselves or any other kernel
4521 * expects the system to be in execlists mode on startup,
4522 * so we need to reset the GPU back to legacy mode. And the only
4523 * known way to disable logical contexts is through a GPU reset.
4524 *
4525 * So in order to leave the system in a known default configuration,
4526 * always reset the GPU upon unload and suspend. Afterwards we then
4527 * clean up the GEM state tracking, flushing off the requests and
4528 * leaving the system in a known idle state.
4529 *
4530 * Note that is of the upmost importance that the GPU is idle and
4531 * all stray writes are flushed *before* we dismantle the backing
4532 * storage for the pinned objects.
4533 *
4534 * However, since we are uncertain that resetting the GPU on older
4535 * machines is a good idea, we don't - just in case it leaves the
4536 * machine in an unusable condition.
4537 */
4538 if (HAS_HW_CONTEXTS(dev)) {
4539 int reset = intel_gpu_reset(dev_priv, ALL_ENGINES);
4540 WARN_ON(reset && reset != -ENODEV);
4541 }
4542
Eric Anholt673a3942008-07-30 12:06:12 -07004543 return 0;
Chris Wilson45c5f202013-10-16 11:50:01 +01004544
4545err:
4546 mutex_unlock(&dev->struct_mutex);
4547 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004548}
4549
Chris Wilson5ab57c72016-07-15 14:56:20 +01004550void i915_gem_resume(struct drm_device *dev)
4551{
4552 struct drm_i915_private *dev_priv = to_i915(dev);
4553
4554 mutex_lock(&dev->struct_mutex);
4555 i915_gem_restore_gtt_mappings(dev);
4556
4557 /* As we didn't flush the kernel context before suspend, we cannot
4558 * guarantee that the context image is complete. So let's just reset
4559 * it and start again.
4560 */
Chris Wilson821ed7d2016-09-09 14:11:53 +01004561 dev_priv->gt.resume(dev_priv);
Chris Wilson5ab57c72016-07-15 14:56:20 +01004562
4563 mutex_unlock(&dev->struct_mutex);
4564}
4565
Daniel Vetterf691e2f2012-02-02 09:58:12 +01004566void i915_gem_init_swizzling(struct drm_device *dev)
4567{
Chris Wilsonfac5e232016-07-04 11:34:36 +01004568 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetterf691e2f2012-02-02 09:58:12 +01004569
Daniel Vetter11782b02012-01-31 16:47:55 +01004570 if (INTEL_INFO(dev)->gen < 5 ||
Daniel Vetterf691e2f2012-02-02 09:58:12 +01004571 dev_priv->mm.bit_6_swizzle_x == I915_BIT_6_SWIZZLE_NONE)
4572 return;
4573
4574 I915_WRITE(DISP_ARB_CTL, I915_READ(DISP_ARB_CTL) |
4575 DISP_TILE_SURFACE_SWIZZLING);
4576
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01004577 if (IS_GEN5(dev_priv))
Daniel Vetter11782b02012-01-31 16:47:55 +01004578 return;
4579
Daniel Vetterf691e2f2012-02-02 09:58:12 +01004580 I915_WRITE(TILECTL, I915_READ(TILECTL) | TILECTL_SWZCTL);
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01004581 if (IS_GEN6(dev_priv))
Daniel Vetter6b26c862012-04-24 14:04:12 +02004582 I915_WRITE(ARB_MODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_SNB));
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01004583 else if (IS_GEN7(dev_priv))
Daniel Vetter6b26c862012-04-24 14:04:12 +02004584 I915_WRITE(ARB_MODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_IVB));
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01004585 else if (IS_GEN8(dev_priv))
Ben Widawsky31a53362013-11-02 21:07:04 -07004586 I915_WRITE(GAMTARBMODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_BDW));
Ben Widawsky8782e262012-12-18 10:31:23 -08004587 else
4588 BUG();
Daniel Vetterf691e2f2012-02-02 09:58:12 +01004589}
Daniel Vettere21af882012-02-09 20:53:27 +01004590
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01004591static void init_unused_ring(struct drm_i915_private *dev_priv, u32 base)
Ville Syrjälä81e7f202014-08-15 01:21:55 +03004592{
Ville Syrjälä81e7f202014-08-15 01:21:55 +03004593 I915_WRITE(RING_CTL(base), 0);
4594 I915_WRITE(RING_HEAD(base), 0);
4595 I915_WRITE(RING_TAIL(base), 0);
4596 I915_WRITE(RING_START(base), 0);
4597}
4598
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01004599static void init_unused_rings(struct drm_i915_private *dev_priv)
Ville Syrjälä81e7f202014-08-15 01:21:55 +03004600{
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01004601 if (IS_I830(dev_priv)) {
4602 init_unused_ring(dev_priv, PRB1_BASE);
4603 init_unused_ring(dev_priv, SRB0_BASE);
4604 init_unused_ring(dev_priv, SRB1_BASE);
4605 init_unused_ring(dev_priv, SRB2_BASE);
4606 init_unused_ring(dev_priv, SRB3_BASE);
4607 } else if (IS_GEN2(dev_priv)) {
4608 init_unused_ring(dev_priv, SRB0_BASE);
4609 init_unused_ring(dev_priv, SRB1_BASE);
4610 } else if (IS_GEN3(dev_priv)) {
4611 init_unused_ring(dev_priv, PRB1_BASE);
4612 init_unused_ring(dev_priv, PRB2_BASE);
Ville Syrjälä81e7f202014-08-15 01:21:55 +03004613 }
4614}
4615
Ben Widawsky4fc7c972013-02-08 11:49:24 -08004616int
4617i915_gem_init_hw(struct drm_device *dev)
4618{
Chris Wilsonfac5e232016-07-04 11:34:36 +01004619 struct drm_i915_private *dev_priv = to_i915(dev);
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00004620 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05304621 enum intel_engine_id id;
Chris Wilsond200cda2016-04-28 09:56:44 +01004622 int ret;
Ben Widawsky4fc7c972013-02-08 11:49:24 -08004623
Chris Wilsonde867c22016-10-25 13:16:02 +01004624 dev_priv->gt.last_init_time = ktime_get();
4625
Chris Wilson5e4f5182015-02-13 14:35:59 +00004626 /* Double layer security blanket, see i915_gem_init() */
4627 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
4628
Mika Kuoppala3accaf72016-04-13 17:26:43 +03004629 if (HAS_EDRAM(dev) && INTEL_GEN(dev_priv) < 9)
Ben Widawsky05e21cc2013-07-04 11:02:04 -07004630 I915_WRITE(HSW_IDICR, I915_READ(HSW_IDICR) | IDIHASHMSK(0xf));
Ben Widawsky4fc7c972013-02-08 11:49:24 -08004631
Tvrtko Ursulin772c2a52016-10-13 11:03:01 +01004632 if (IS_HASWELL(dev_priv))
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01004633 I915_WRITE(MI_PREDICATE_RESULT_2, IS_HSW_GT3(dev_priv) ?
Ville Syrjälä0bf21342013-11-29 14:56:12 +02004634 LOWER_SLICE_ENABLED : LOWER_SLICE_DISABLED);
Rodrigo Vivi94353732013-08-28 16:45:46 -03004635
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01004636 if (HAS_PCH_NOP(dev_priv)) {
Tvrtko Ursulinfd6b8f42016-10-14 10:13:06 +01004637 if (IS_IVYBRIDGE(dev_priv)) {
Daniel Vetter6ba844b2014-01-22 23:39:30 +01004638 u32 temp = I915_READ(GEN7_MSG_CTL);
4639 temp &= ~(WAIT_FOR_PCH_FLR_ACK | WAIT_FOR_PCH_RESET_ACK);
4640 I915_WRITE(GEN7_MSG_CTL, temp);
4641 } else if (INTEL_INFO(dev)->gen >= 7) {
4642 u32 temp = I915_READ(HSW_NDE_RSTWRN_OPT);
4643 temp &= ~RESET_PCH_HANDSHAKE_ENABLE;
4644 I915_WRITE(HSW_NDE_RSTWRN_OPT, temp);
4645 }
Ben Widawsky88a2b2a2013-04-05 13:12:43 -07004646 }
4647
Ben Widawsky4fc7c972013-02-08 11:49:24 -08004648 i915_gem_init_swizzling(dev);
4649
Daniel Vetterd5abdfd2014-11-20 09:45:19 +01004650 /*
4651 * At least 830 can leave some of the unused rings
4652 * "active" (ie. head != tail) after resume which
4653 * will prevent c3 entry. Makes sure all unused rings
4654 * are totally idle.
4655 */
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01004656 init_unused_rings(dev_priv);
Daniel Vetterd5abdfd2014-11-20 09:45:19 +01004657
Dave Gordoned54c1a2016-01-19 19:02:54 +00004658 BUG_ON(!dev_priv->kernel_context);
John Harrison90638cc2015-05-29 17:43:37 +01004659
John Harrison4ad2fd82015-06-18 13:11:20 +01004660 ret = i915_ppgtt_init_hw(dev);
4661 if (ret) {
4662 DRM_ERROR("PPGTT enable HW failed %d\n", ret);
4663 goto out;
4664 }
4665
4666 /* Need to do basic initialisation of all rings first: */
Akash Goel3b3f1652016-10-13 22:44:48 +05304667 for_each_engine(engine, dev_priv, id) {
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00004668 ret = engine->init_hw(engine);
Daniel Vetter35a57ff2014-11-20 00:33:07 +01004669 if (ret)
Chris Wilson5e4f5182015-02-13 14:35:59 +00004670 goto out;
Daniel Vetter35a57ff2014-11-20 00:33:07 +01004671 }
Mika Kuoppala99433932013-01-22 14:12:17 +02004672
Peter Antoine0ccdacf2016-04-13 15:03:25 +01004673 intel_mocs_init_l3cc_table(dev);
4674
Alex Dai33a732f2015-08-12 15:43:36 +01004675 /* We can't enable contexts until all firmware is loaded */
Dave Gordone556f7c2016-06-07 09:14:49 +01004676 ret = intel_guc_setup(dev);
4677 if (ret)
4678 goto out;
Alex Dai33a732f2015-08-12 15:43:36 +01004679
Chris Wilson5e4f5182015-02-13 14:35:59 +00004680out:
4681 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Ben Widawsky2fa48d82013-12-06 14:11:04 -08004682 return ret;
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004683}
4684
Chris Wilson39df9192016-07-20 13:31:57 +01004685bool intel_sanitize_semaphores(struct drm_i915_private *dev_priv, int value)
4686{
4687 if (INTEL_INFO(dev_priv)->gen < 6)
4688 return false;
4689
4690 /* TODO: make semaphores and Execlists play nicely together */
4691 if (i915.enable_execlists)
4692 return false;
4693
4694 if (value >= 0)
4695 return value;
4696
4697#ifdef CONFIG_INTEL_IOMMU
4698 /* Enable semaphores on SNB when IO remapping is off */
4699 if (INTEL_INFO(dev_priv)->gen == 6 && intel_iommu_gfx_mapped)
4700 return false;
4701#endif
4702
4703 return true;
4704}
4705
Chris Wilson1070a422012-04-24 15:47:41 +01004706int i915_gem_init(struct drm_device *dev)
4707{
Chris Wilsonfac5e232016-07-04 11:34:36 +01004708 struct drm_i915_private *dev_priv = to_i915(dev);
Chris Wilson1070a422012-04-24 15:47:41 +01004709 int ret;
4710
Chris Wilson1070a422012-04-24 15:47:41 +01004711 mutex_lock(&dev->struct_mutex);
Jesse Barnesd62b4892013-03-08 10:45:53 -08004712
Oscar Mateoa83014d2014-07-24 17:04:21 +01004713 if (!i915.enable_execlists) {
Chris Wilson821ed7d2016-09-09 14:11:53 +01004714 dev_priv->gt.resume = intel_legacy_submission_resume;
Chris Wilson7e37f882016-08-02 22:50:21 +01004715 dev_priv->gt.cleanup_engine = intel_engine_cleanup;
Oscar Mateo454afeb2014-07-24 17:04:22 +01004716 } else {
Chris Wilson821ed7d2016-09-09 14:11:53 +01004717 dev_priv->gt.resume = intel_lr_context_resume;
Tvrtko Ursulin117897f2016-03-16 11:00:40 +00004718 dev_priv->gt.cleanup_engine = intel_logical_ring_cleanup;
Oscar Mateoa83014d2014-07-24 17:04:21 +01004719 }
4720
Chris Wilson5e4f5182015-02-13 14:35:59 +00004721 /* This is just a security blanket to placate dragons.
4722 * On some systems, we very sporadically observe that the first TLBs
4723 * used by the CS may be stale, despite us poking the TLB reset. If
4724 * we hold the forcewake during initialisation these problems
4725 * just magically go away.
4726 */
4727 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
4728
Chris Wilson72778cb2016-05-19 16:17:16 +01004729 i915_gem_init_userptr(dev_priv);
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01004730
4731 ret = i915_gem_init_ggtt(dev_priv);
4732 if (ret)
4733 goto out_unlock;
Jesse Barnesd62b4892013-03-08 10:45:53 -08004734
Ben Widawsky2fa48d82013-12-06 14:11:04 -08004735 ret = i915_gem_context_init(dev);
Jani Nikula7bcc3772014-12-05 14:17:42 +02004736 if (ret)
4737 goto out_unlock;
Ben Widawsky2fa48d82013-12-06 14:11:04 -08004738
Tvrtko Ursulin8b3e2d32016-07-13 16:03:37 +01004739 ret = intel_engines_init(dev);
Daniel Vetter35a57ff2014-11-20 00:33:07 +01004740 if (ret)
Jani Nikula7bcc3772014-12-05 14:17:42 +02004741 goto out_unlock;
Daniel Vetter53ca26c2012-04-26 23:28:03 +02004742
4743 ret = i915_gem_init_hw(dev);
Chris Wilson60990322014-04-09 09:19:42 +01004744 if (ret == -EIO) {
Chris Wilson7e21d642016-07-27 09:07:29 +01004745 /* Allow engine initialisation to fail by marking the GPU as
Chris Wilson60990322014-04-09 09:19:42 +01004746 * wedged. But we only want to do this where the GPU is angry,
4747 * for all other failure, such as an allocation failure, bail.
4748 */
4749 DRM_ERROR("Failed to initialize GPU, declaring it wedged\n");
Chris Wilson821ed7d2016-09-09 14:11:53 +01004750 i915_gem_set_wedged(dev_priv);
Chris Wilson60990322014-04-09 09:19:42 +01004751 ret = 0;
Chris Wilson1070a422012-04-24 15:47:41 +01004752 }
Jani Nikula7bcc3772014-12-05 14:17:42 +02004753
4754out_unlock:
Chris Wilson5e4f5182015-02-13 14:35:59 +00004755 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Chris Wilson60990322014-04-09 09:19:42 +01004756 mutex_unlock(&dev->struct_mutex);
Chris Wilson1070a422012-04-24 15:47:41 +01004757
Chris Wilson60990322014-04-09 09:19:42 +01004758 return ret;
Chris Wilson1070a422012-04-24 15:47:41 +01004759}
4760
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004761void
Tvrtko Ursulin117897f2016-03-16 11:00:40 +00004762i915_gem_cleanup_engines(struct drm_device *dev)
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004763{
Chris Wilsonfac5e232016-07-04 11:34:36 +01004764 struct drm_i915_private *dev_priv = to_i915(dev);
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00004765 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05304766 enum intel_engine_id id;
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004767
Akash Goel3b3f1652016-10-13 22:44:48 +05304768 for_each_engine(engine, dev_priv, id)
Tvrtko Ursulin117897f2016-03-16 11:00:40 +00004769 dev_priv->gt.cleanup_engine(engine);
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004770}
4771
Eric Anholt673a3942008-07-30 12:06:12 -07004772void
Imre Deak40ae4e12016-03-16 14:54:03 +02004773i915_gem_load_init_fences(struct drm_i915_private *dev_priv)
4774{
Chris Wilson91c8a322016-07-05 10:40:23 +01004775 struct drm_device *dev = &dev_priv->drm;
Chris Wilson49ef5292016-08-18 17:17:00 +01004776 int i;
Imre Deak40ae4e12016-03-16 14:54:03 +02004777
4778 if (INTEL_INFO(dev_priv)->gen >= 7 && !IS_VALLEYVIEW(dev_priv) &&
4779 !IS_CHERRYVIEW(dev_priv))
4780 dev_priv->num_fence_regs = 32;
4781 else if (INTEL_INFO(dev_priv)->gen >= 4 || IS_I945G(dev_priv) ||
4782 IS_I945GM(dev_priv) || IS_G33(dev_priv))
4783 dev_priv->num_fence_regs = 16;
4784 else
4785 dev_priv->num_fence_regs = 8;
4786
Chris Wilsonc0336662016-05-06 15:40:21 +01004787 if (intel_vgpu_active(dev_priv))
Imre Deak40ae4e12016-03-16 14:54:03 +02004788 dev_priv->num_fence_regs =
4789 I915_READ(vgtif_reg(avail_rs.fence_num));
4790
4791 /* Initialize fence registers to zero */
Chris Wilson49ef5292016-08-18 17:17:00 +01004792 for (i = 0; i < dev_priv->num_fence_regs; i++) {
4793 struct drm_i915_fence_reg *fence = &dev_priv->fence_regs[i];
4794
4795 fence->i915 = dev_priv;
4796 fence->id = i;
4797 list_add_tail(&fence->link, &dev_priv->mm.fence_list);
4798 }
Imre Deak40ae4e12016-03-16 14:54:03 +02004799 i915_gem_restore_fences(dev);
4800
4801 i915_gem_detect_bit_6_swizzle(dev);
4802}
4803
4804void
Imre Deakd64aa092016-01-19 15:26:29 +02004805i915_gem_load_init(struct drm_device *dev)
Eric Anholt673a3942008-07-30 12:06:12 -07004806{
Chris Wilsonfac5e232016-07-04 11:34:36 +01004807 struct drm_i915_private *dev_priv = to_i915(dev);
Chris Wilson42dcedd2012-11-15 11:32:30 +00004808
Chris Wilsonefab6d82015-04-07 16:20:57 +01004809 dev_priv->objects =
Chris Wilson42dcedd2012-11-15 11:32:30 +00004810 kmem_cache_create("i915_gem_object",
4811 sizeof(struct drm_i915_gem_object), 0,
4812 SLAB_HWCACHE_ALIGN,
4813 NULL);
Chris Wilsone20d2ab2015-04-07 16:20:58 +01004814 dev_priv->vmas =
4815 kmem_cache_create("i915_gem_vma",
4816 sizeof(struct i915_vma), 0,
4817 SLAB_HWCACHE_ALIGN,
4818 NULL);
Chris Wilsonefab6d82015-04-07 16:20:57 +01004819 dev_priv->requests =
4820 kmem_cache_create("i915_gem_request",
4821 sizeof(struct drm_i915_gem_request), 0,
Chris Wilson0eafec62016-08-04 16:32:41 +01004822 SLAB_HWCACHE_ALIGN |
4823 SLAB_RECLAIM_ACCOUNT |
4824 SLAB_DESTROY_BY_RCU,
Chris Wilsonefab6d82015-04-07 16:20:57 +01004825 NULL);
Eric Anholt673a3942008-07-30 12:06:12 -07004826
Ben Widawskya33afea2013-09-17 21:12:45 -07004827 INIT_LIST_HEAD(&dev_priv->context_list);
Chris Wilson6c085a72012-08-20 11:40:46 +02004828 INIT_LIST_HEAD(&dev_priv->mm.unbound_list);
4829 INIT_LIST_HEAD(&dev_priv->mm.bound_list);
Eric Anholta09ba7f2009-08-29 12:49:51 -07004830 INIT_LIST_HEAD(&dev_priv->mm.fence_list);
Chris Wilson275f0392016-10-24 13:42:14 +01004831 INIT_LIST_HEAD(&dev_priv->mm.userfault_list);
Chris Wilson67d97da2016-07-04 08:08:31 +01004832 INIT_DELAYED_WORK(&dev_priv->gt.retire_work,
Eric Anholt673a3942008-07-30 12:06:12 -07004833 i915_gem_retire_work_handler);
Chris Wilson67d97da2016-07-04 08:08:31 +01004834 INIT_DELAYED_WORK(&dev_priv->gt.idle_work,
Chris Wilsonb29c19b2013-09-25 17:34:56 +01004835 i915_gem_idle_work_handler);
Chris Wilson1f15b762016-07-01 17:23:14 +01004836 init_waitqueue_head(&dev_priv->gpu_error.wait_queue);
Daniel Vetter1f83fee2012-11-15 17:17:22 +01004837 init_waitqueue_head(&dev_priv->gpu_error.reset_queue);
Chris Wilson31169712009-09-14 16:50:28 +01004838
Chris Wilson72bfa192010-12-19 11:42:05 +00004839 dev_priv->relative_constants_mode = I915_EXEC_CONSTANTS_REL_GENERAL;
4840
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05004841 init_waitqueue_head(&dev_priv->pending_flip_queue);
Chris Wilson17250b72010-10-28 12:51:39 +01004842
Chris Wilsonce453d82011-02-21 14:43:56 +00004843 dev_priv->mm.interruptible = true;
4844
Joonas Lahtinen6f633402016-09-01 14:58:21 +03004845 atomic_set(&dev_priv->mm.bsd_engine_dispatch_index, 0);
4846
Chris Wilsonb5add952016-08-04 16:32:36 +01004847 spin_lock_init(&dev_priv->fb_tracking.lock);
Eric Anholt673a3942008-07-30 12:06:12 -07004848}
Dave Airlie71acb5e2008-12-30 20:31:46 +10004849
Imre Deakd64aa092016-01-19 15:26:29 +02004850void i915_gem_load_cleanup(struct drm_device *dev)
4851{
4852 struct drm_i915_private *dev_priv = to_i915(dev);
4853
4854 kmem_cache_destroy(dev_priv->requests);
4855 kmem_cache_destroy(dev_priv->vmas);
4856 kmem_cache_destroy(dev_priv->objects);
Chris Wilson0eafec62016-08-04 16:32:41 +01004857
4858 /* And ensure that our DESTROY_BY_RCU slabs are truly destroyed */
4859 rcu_barrier();
Imre Deakd64aa092016-01-19 15:26:29 +02004860}
4861
Chris Wilson6a800ea2016-09-21 14:51:07 +01004862int i915_gem_freeze(struct drm_i915_private *dev_priv)
4863{
4864 intel_runtime_pm_get(dev_priv);
4865
4866 mutex_lock(&dev_priv->drm.struct_mutex);
4867 i915_gem_shrink_all(dev_priv);
4868 mutex_unlock(&dev_priv->drm.struct_mutex);
4869
4870 intel_runtime_pm_put(dev_priv);
4871
4872 return 0;
4873}
4874
Chris Wilson461fb992016-05-14 07:26:33 +01004875int i915_gem_freeze_late(struct drm_i915_private *dev_priv)
4876{
4877 struct drm_i915_gem_object *obj;
Chris Wilson7aab2d52016-09-09 20:02:18 +01004878 struct list_head *phases[] = {
4879 &dev_priv->mm.unbound_list,
4880 &dev_priv->mm.bound_list,
4881 NULL
4882 }, **p;
Chris Wilson461fb992016-05-14 07:26:33 +01004883
4884 /* Called just before we write the hibernation image.
4885 *
4886 * We need to update the domain tracking to reflect that the CPU
4887 * will be accessing all the pages to create and restore from the
4888 * hibernation, and so upon restoration those pages will be in the
4889 * CPU domain.
4890 *
4891 * To make sure the hibernation image contains the latest state,
4892 * we update that state just before writing out the image.
Chris Wilson7aab2d52016-09-09 20:02:18 +01004893 *
4894 * To try and reduce the hibernation image, we manually shrink
4895 * the objects as well.
Chris Wilson461fb992016-05-14 07:26:33 +01004896 */
4897
Chris Wilson6a800ea2016-09-21 14:51:07 +01004898 mutex_lock(&dev_priv->drm.struct_mutex);
4899 i915_gem_shrink(dev_priv, -1UL, I915_SHRINK_UNBOUND);
Chris Wilson461fb992016-05-14 07:26:33 +01004900
Chris Wilson7aab2d52016-09-09 20:02:18 +01004901 for (p = phases; *p; p++) {
4902 list_for_each_entry(obj, *p, global_list) {
4903 obj->base.read_domains = I915_GEM_DOMAIN_CPU;
4904 obj->base.write_domain = I915_GEM_DOMAIN_CPU;
4905 }
Chris Wilson461fb992016-05-14 07:26:33 +01004906 }
Chris Wilson6a800ea2016-09-21 14:51:07 +01004907 mutex_unlock(&dev_priv->drm.struct_mutex);
Chris Wilson461fb992016-05-14 07:26:33 +01004908
4909 return 0;
4910}
4911
Chris Wilsonf787a5f2010-09-24 16:02:42 +01004912void i915_gem_release(struct drm_device *dev, struct drm_file *file)
Eric Anholtb9624422009-06-03 07:27:35 +00004913{
Chris Wilsonf787a5f2010-09-24 16:02:42 +01004914 struct drm_i915_file_private *file_priv = file->driver_priv;
Chris Wilson15f7bbc2016-07-26 12:01:52 +01004915 struct drm_i915_gem_request *request;
Eric Anholtb9624422009-06-03 07:27:35 +00004916
4917 /* Clean up our request list when the client is going away, so that
4918 * later retire_requests won't dereference our soon-to-be-gone
4919 * file_priv.
4920 */
Chris Wilson1c255952010-09-26 11:03:27 +01004921 spin_lock(&file_priv->mm.lock);
Chris Wilson15f7bbc2016-07-26 12:01:52 +01004922 list_for_each_entry(request, &file_priv->mm.request_list, client_list)
Chris Wilsonf787a5f2010-09-24 16:02:42 +01004923 request->file_priv = NULL;
Chris Wilson1c255952010-09-26 11:03:27 +01004924 spin_unlock(&file_priv->mm.lock);
Chris Wilson31169712009-09-14 16:50:28 +01004925
Chris Wilson2e1b8732015-04-27 13:41:22 +01004926 if (!list_empty(&file_priv->rps.link)) {
Chris Wilson8d3afd72015-05-21 21:01:47 +01004927 spin_lock(&to_i915(dev)->rps.client_lock);
Chris Wilson2e1b8732015-04-27 13:41:22 +01004928 list_del(&file_priv->rps.link);
Chris Wilson8d3afd72015-05-21 21:01:47 +01004929 spin_unlock(&to_i915(dev)->rps.client_lock);
Chris Wilson1854d5c2015-04-07 16:20:32 +01004930 }
Chris Wilsonb29c19b2013-09-25 17:34:56 +01004931}
4932
4933int i915_gem_open(struct drm_device *dev, struct drm_file *file)
4934{
4935 struct drm_i915_file_private *file_priv;
Ben Widawskye422b882013-12-06 14:10:58 -08004936 int ret;
Chris Wilsonb29c19b2013-09-25 17:34:56 +01004937
4938 DRM_DEBUG_DRIVER("\n");
4939
4940 file_priv = kzalloc(sizeof(*file_priv), GFP_KERNEL);
4941 if (!file_priv)
4942 return -ENOMEM;
4943
4944 file->driver_priv = file_priv;
Dave Gordonf19ec8c2016-07-04 11:34:37 +01004945 file_priv->dev_priv = to_i915(dev);
Chris Wilsonab0e7ff2014-02-25 17:11:24 +02004946 file_priv->file = file;
Chris Wilson2e1b8732015-04-27 13:41:22 +01004947 INIT_LIST_HEAD(&file_priv->rps.link);
Chris Wilsonb29c19b2013-09-25 17:34:56 +01004948
4949 spin_lock_init(&file_priv->mm.lock);
4950 INIT_LIST_HEAD(&file_priv->mm.request_list);
Chris Wilsonb29c19b2013-09-25 17:34:56 +01004951
Chris Wilsonc80ff162016-07-27 09:07:27 +01004952 file_priv->bsd_engine = -1;
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00004953
Ben Widawskye422b882013-12-06 14:10:58 -08004954 ret = i915_gem_context_open(dev, file);
4955 if (ret)
4956 kfree(file_priv);
Chris Wilsonb29c19b2013-09-25 17:34:56 +01004957
Ben Widawskye422b882013-12-06 14:10:58 -08004958 return ret;
Chris Wilsonb29c19b2013-09-25 17:34:56 +01004959}
4960
Daniel Vetterb680c372014-09-19 18:27:27 +02004961/**
4962 * i915_gem_track_fb - update frontbuffer tracking
Geliang Tangd9072a32015-09-15 05:58:44 -07004963 * @old: current GEM buffer for the frontbuffer slots
4964 * @new: new GEM buffer for the frontbuffer slots
4965 * @frontbuffer_bits: bitmask of frontbuffer slots
Daniel Vetterb680c372014-09-19 18:27:27 +02004966 *
4967 * This updates the frontbuffer tracking bits @frontbuffer_bits by clearing them
4968 * from @old and setting them in @new. Both @old and @new can be NULL.
4969 */
Daniel Vettera071fa02014-06-18 23:28:09 +02004970void i915_gem_track_fb(struct drm_i915_gem_object *old,
4971 struct drm_i915_gem_object *new,
4972 unsigned frontbuffer_bits)
4973{
Chris Wilsonfaf5bf02016-08-04 16:32:37 +01004974 /* Control of individual bits within the mask are guarded by
4975 * the owning plane->mutex, i.e. we can never see concurrent
4976 * manipulation of individual bits. But since the bitfield as a whole
4977 * is updated using RMW, we need to use atomics in order to update
4978 * the bits.
4979 */
4980 BUILD_BUG_ON(INTEL_FRONTBUFFER_BITS_PER_PIPE * I915_MAX_PIPES >
4981 sizeof(atomic_t) * BITS_PER_BYTE);
4982
Daniel Vettera071fa02014-06-18 23:28:09 +02004983 if (old) {
Chris Wilsonfaf5bf02016-08-04 16:32:37 +01004984 WARN_ON(!(atomic_read(&old->frontbuffer_bits) & frontbuffer_bits));
4985 atomic_andnot(frontbuffer_bits, &old->frontbuffer_bits);
Daniel Vettera071fa02014-06-18 23:28:09 +02004986 }
4987
4988 if (new) {
Chris Wilsonfaf5bf02016-08-04 16:32:37 +01004989 WARN_ON(atomic_read(&new->frontbuffer_bits) & frontbuffer_bits);
4990 atomic_or(frontbuffer_bits, &new->frontbuffer_bits);
Daniel Vettera071fa02014-06-18 23:28:09 +02004991 }
4992}
4993
Dave Gordon033908a2015-12-10 18:51:23 +00004994/* Like i915_gem_object_get_page(), but mark the returned page dirty */
4995struct page *
4996i915_gem_object_get_dirty_page(struct drm_i915_gem_object *obj, int n)
4997{
4998 struct page *page;
4999
5000 /* Only default objects have per-page dirty tracking */
Chris Wilsonb9bcd142016-06-20 15:05:51 +01005001 if (WARN_ON(!i915_gem_object_has_struct_page(obj)))
Dave Gordon033908a2015-12-10 18:51:23 +00005002 return NULL;
5003
5004 page = i915_gem_object_get_page(obj, n);
5005 set_page_dirty(page);
5006 return page;
5007}
5008
Dave Gordonea702992015-07-09 19:29:02 +01005009/* Allocate a new GEM object and fill it with the supplied data */
5010struct drm_i915_gem_object *
5011i915_gem_object_create_from_data(struct drm_device *dev,
5012 const void *data, size_t size)
5013{
5014 struct drm_i915_gem_object *obj;
5015 struct sg_table *sg;
5016 size_t bytes;
5017 int ret;
5018
Dave Gordond37cd8a2016-04-22 19:14:32 +01005019 obj = i915_gem_object_create(dev, round_up(size, PAGE_SIZE));
Chris Wilsonfe3db792016-04-25 13:32:13 +01005020 if (IS_ERR(obj))
Dave Gordonea702992015-07-09 19:29:02 +01005021 return obj;
5022
5023 ret = i915_gem_object_set_to_cpu_domain(obj, true);
5024 if (ret)
5025 goto fail;
5026
5027 ret = i915_gem_object_get_pages(obj);
5028 if (ret)
5029 goto fail;
5030
5031 i915_gem_object_pin_pages(obj);
5032 sg = obj->pages;
5033 bytes = sg_copy_from_buffer(sg->sgl, sg->nents, (void *)data, size);
Dave Gordon9e7d18c2015-12-10 18:51:24 +00005034 obj->dirty = 1; /* Backing store is now out of date */
Dave Gordonea702992015-07-09 19:29:02 +01005035 i915_gem_object_unpin_pages(obj);
5036
5037 if (WARN_ON(bytes != size)) {
5038 DRM_ERROR("Incomplete copy, wrote %zu of %zu", bytes, size);
5039 ret = -EFAULT;
5040 goto fail;
5041 }
5042
5043 return obj;
5044
5045fail:
Chris Wilsonf8c417c2016-07-20 13:31:53 +01005046 i915_gem_object_put(obj);
Dave Gordonea702992015-07-09 19:29:02 +01005047 return ERR_PTR(ret);
5048}