blob: c0103044deded68faeae3d08ed0fb82d56aab954 [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 Wilsonf8a7fde2016-10-28 13:58:29 +01002664 if (i915_gem_object_has_active_reference(obj)) {
2665 i915_gem_object_clear_active_reference(obj);
2666 i915_gem_object_put(obj);
2667 }
Chris Wilsonc8725f32014-03-17 12:21:55 +00002668}
2669
Chris Wilson7b4d3a12016-07-04 08:08:37 +01002670static bool i915_context_is_banned(const struct i915_gem_context *ctx)
Mika Kuoppalabe62acb2013-08-30 16:19:28 +03002671{
Mika Kuoppala44e2c072014-01-30 16:01:15 +02002672 unsigned long elapsed;
Mika Kuoppalabe62acb2013-08-30 16:19:28 +03002673
Mika Kuoppala44e2c072014-01-30 16:01:15 +02002674 if (ctx->hang_stats.banned)
Mika Kuoppalabe62acb2013-08-30 16:19:28 +03002675 return true;
2676
Chris Wilson7b4d3a12016-07-04 08:08:37 +01002677 elapsed = get_seconds() - ctx->hang_stats.guilty_ts;
Chris Wilson676fa572014-12-24 08:13:39 -08002678 if (ctx->hang_stats.ban_period_seconds &&
2679 elapsed <= ctx->hang_stats.ban_period_seconds) {
Chris Wilson7b4d3a12016-07-04 08:08:37 +01002680 DRM_DEBUG("context hanging too fast, banning!\n");
2681 return true;
Mika Kuoppalabe62acb2013-08-30 16:19:28 +03002682 }
2683
2684 return false;
2685}
2686
Chris Wilson7b4d3a12016-07-04 08:08:37 +01002687static void i915_set_reset_status(struct i915_gem_context *ctx,
Mika Kuoppalab6b0fac2014-01-30 19:04:43 +02002688 const bool guilty)
Mika Kuoppalaaa60c662013-06-12 15:13:20 +03002689{
Chris Wilson7b4d3a12016-07-04 08:08:37 +01002690 struct i915_ctx_hang_stats *hs = &ctx->hang_stats;
Mika Kuoppala44e2c072014-01-30 16:01:15 +02002691
2692 if (guilty) {
Chris Wilson7b4d3a12016-07-04 08:08:37 +01002693 hs->banned = i915_context_is_banned(ctx);
Mika Kuoppala44e2c072014-01-30 16:01:15 +02002694 hs->batch_active++;
2695 hs->guilty_ts = get_seconds();
2696 } else {
2697 hs->batch_pending++;
Mika Kuoppalaaa60c662013-06-12 15:13:20 +03002698 }
2699}
2700
Chris Wilson8d9fc7f2014-02-25 17:11:23 +02002701struct drm_i915_gem_request *
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00002702i915_gem_find_active_request(struct intel_engine_cs *engine)
Chris Wilson9375e442010-09-19 12:21:28 +01002703{
Chris Wilson4db080f2013-12-04 11:37:09 +00002704 struct drm_i915_gem_request *request;
Mika Kuoppalaaa60c662013-06-12 15:13:20 +03002705
Chris Wilsonf69a02c2016-07-01 17:23:16 +01002706 /* We are called by the error capture and reset at a random
2707 * point in time. In particular, note that neither is crucially
2708 * ordered with an interrupt. After a hang, the GPU is dead and we
2709 * assume that no more writes can happen (we waited long enough for
2710 * all writes that were in transaction to be flushed) - adding an
2711 * extra delay for a recent interrupt is pointless. Hence, we do
2712 * not need an engine->irq_seqno_barrier() before the seqno reads.
2713 */
Chris Wilsonefdf7c02016-08-04 07:52:33 +01002714 list_for_each_entry(request, &engine->request_list, link) {
Chris Wilsonf69a02c2016-07-01 17:23:16 +01002715 if (i915_gem_request_completed(request))
Chris Wilson4db080f2013-12-04 11:37:09 +00002716 continue;
Mika Kuoppalaaa60c662013-06-12 15:13:20 +03002717
Chris Wilson5590af32016-09-09 14:11:54 +01002718 if (!i915_sw_fence_done(&request->submit))
2719 break;
2720
Mika Kuoppalab6b0fac2014-01-30 19:04:43 +02002721 return request;
Chris Wilson4db080f2013-12-04 11:37:09 +00002722 }
Mika Kuoppalab6b0fac2014-01-30 19:04:43 +02002723
2724 return NULL;
2725}
2726
Chris Wilson821ed7d2016-09-09 14:11:53 +01002727static void reset_request(struct drm_i915_gem_request *request)
Mika Kuoppalab6b0fac2014-01-30 19:04:43 +02002728{
Chris Wilson821ed7d2016-09-09 14:11:53 +01002729 void *vaddr = request->ring->vaddr;
2730 u32 head;
Mika Kuoppalab6b0fac2014-01-30 19:04:43 +02002731
Chris Wilson821ed7d2016-09-09 14:11:53 +01002732 /* As this request likely depends on state from the lost
2733 * context, clear out all the user operations leaving the
2734 * breadcrumb at the end (so we get the fence notifications).
2735 */
2736 head = request->head;
2737 if (request->postfix < head) {
2738 memset(vaddr + head, 0, request->ring->size - head);
2739 head = 0;
2740 }
2741 memset(vaddr + head, 0, request->postfix - head);
Chris Wilson4db080f2013-12-04 11:37:09 +00002742}
2743
Chris Wilson821ed7d2016-09-09 14:11:53 +01002744static void i915_gem_reset_engine(struct intel_engine_cs *engine)
Chris Wilson4db080f2013-12-04 11:37:09 +00002745{
Chris Wilsondcff85c2016-08-05 10:14:11 +01002746 struct drm_i915_gem_request *request;
Chris Wilson821ed7d2016-09-09 14:11:53 +01002747 struct i915_gem_context *incomplete_ctx;
2748 bool ring_hung;
Chris Wilson608c1a52015-09-03 13:01:40 +01002749
Chris Wilson821ed7d2016-09-09 14:11:53 +01002750 if (engine->irq_seqno_barrier)
2751 engine->irq_seqno_barrier(engine);
2752
2753 request = i915_gem_find_active_request(engine);
2754 if (!request)
2755 return;
2756
2757 ring_hung = engine->hangcheck.score >= HANGCHECK_SCORE_RING_HUNG;
Chris Wilson77c60702016-10-04 21:11:29 +01002758 if (engine->hangcheck.seqno != intel_engine_get_seqno(engine))
2759 ring_hung = false;
2760
Chris Wilson821ed7d2016-09-09 14:11:53 +01002761 i915_set_reset_status(request->ctx, ring_hung);
2762 if (!ring_hung)
2763 return;
2764
2765 DRM_DEBUG_DRIVER("resetting %s to restart from tail of request 0x%x\n",
2766 engine->name, request->fence.seqno);
2767
2768 /* Setup the CS to resume from the breadcrumb of the hung request */
2769 engine->reset_hw(engine, request);
2770
2771 /* Users of the default context do not rely on logical state
2772 * preserved between batches. They have to emit full state on
2773 * every batch and so it is safe to execute queued requests following
2774 * the hang.
2775 *
2776 * Other contexts preserve state, now corrupt. We want to skip all
2777 * queued requests that reference the corrupt context.
2778 */
2779 incomplete_ctx = request->ctx;
2780 if (i915_gem_context_is_default(incomplete_ctx))
2781 return;
2782
2783 list_for_each_entry_continue(request, &engine->request_list, link)
2784 if (request->ctx == incomplete_ctx)
2785 reset_request(request);
2786}
2787
2788void i915_gem_reset(struct drm_i915_private *dev_priv)
2789{
2790 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05302791 enum intel_engine_id id;
Chris Wilson821ed7d2016-09-09 14:11:53 +01002792
2793 i915_gem_retire_requests(dev_priv);
2794
Akash Goel3b3f1652016-10-13 22:44:48 +05302795 for_each_engine(engine, dev_priv, id)
Chris Wilson821ed7d2016-09-09 14:11:53 +01002796 i915_gem_reset_engine(engine);
2797
2798 i915_gem_restore_fences(&dev_priv->drm);
Chris Wilsonf2a91d12016-09-21 14:51:06 +01002799
2800 if (dev_priv->gt.awake) {
2801 intel_sanitize_gt_powersave(dev_priv);
2802 intel_enable_gt_powersave(dev_priv);
2803 if (INTEL_GEN(dev_priv) >= 6)
2804 gen6_rps_busy(dev_priv);
2805 }
Chris Wilson821ed7d2016-09-09 14:11:53 +01002806}
2807
2808static void nop_submit_request(struct drm_i915_gem_request *request)
2809{
2810}
2811
2812static void i915_gem_cleanup_engine(struct intel_engine_cs *engine)
2813{
2814 engine->submit_request = nop_submit_request;
Chris Wilson70c2a242016-09-09 14:11:46 +01002815
Chris Wilsonc4b09302016-07-20 09:21:10 +01002816 /* Mark all pending requests as complete so that any concurrent
2817 * (lockless) lookup doesn't try and wait upon the request as we
2818 * reset it.
2819 */
Chris Wilson87b723a2016-08-09 08:37:02 +01002820 intel_engine_init_seqno(engine, engine->last_submitted_seqno);
Chris Wilsonc4b09302016-07-20 09:21:10 +01002821
Ben Widawsky1d62bee2014-01-01 10:15:13 -08002822 /*
Oscar Mateodcb4c122014-11-13 10:28:10 +00002823 * Clear the execlists queue up before freeing the requests, as those
2824 * are the ones that keep the context and ringbuffer backing objects
2825 * pinned in place.
2826 */
Oscar Mateodcb4c122014-11-13 10:28:10 +00002827
Tomas Elf7de1691a2015-10-19 16:32:32 +01002828 if (i915.enable_execlists) {
Chris Wilson70c2a242016-09-09 14:11:46 +01002829 spin_lock(&engine->execlist_lock);
2830 INIT_LIST_HEAD(&engine->execlist_queue);
2831 i915_gem_request_put(engine->execlist_port[0].request);
2832 i915_gem_request_put(engine->execlist_port[1].request);
2833 memset(engine->execlist_port, 0, sizeof(engine->execlist_port));
2834 spin_unlock(&engine->execlist_lock);
Oscar Mateodcb4c122014-11-13 10:28:10 +00002835 }
2836
Chris Wilsonb913b332016-07-13 09:10:31 +01002837 engine->i915->gt.active_engines &= ~intel_engine_flag(engine);
Eric Anholt673a3942008-07-30 12:06:12 -07002838}
2839
Chris Wilson821ed7d2016-09-09 14:11:53 +01002840void i915_gem_set_wedged(struct drm_i915_private *dev_priv)
Eric Anholt673a3942008-07-30 12:06:12 -07002841{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002842 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05302843 enum intel_engine_id id;
Eric Anholt673a3942008-07-30 12:06:12 -07002844
Chris Wilson821ed7d2016-09-09 14:11:53 +01002845 lockdep_assert_held(&dev_priv->drm.struct_mutex);
2846 set_bit(I915_WEDGED, &dev_priv->gpu_error.flags);
Chris Wilson4db080f2013-12-04 11:37:09 +00002847
Chris Wilson821ed7d2016-09-09 14:11:53 +01002848 i915_gem_context_lost(dev_priv);
Akash Goel3b3f1652016-10-13 22:44:48 +05302849 for_each_engine(engine, dev_priv, id)
Chris Wilson821ed7d2016-09-09 14:11:53 +01002850 i915_gem_cleanup_engine(engine);
Chris Wilsonb913b332016-07-13 09:10:31 +01002851 mod_delayed_work(dev_priv->wq, &dev_priv->gt.idle_work, 0);
Chris Wilsondfaae392010-09-22 10:31:52 +01002852
Chris Wilson821ed7d2016-09-09 14:11:53 +01002853 i915_gem_retire_requests(dev_priv);
Eric Anholt673a3942008-07-30 12:06:12 -07002854}
2855
Daniel Vetter75ef9da2010-08-21 00:25:16 +02002856static void
Eric Anholt673a3942008-07-30 12:06:12 -07002857i915_gem_retire_work_handler(struct work_struct *work)
2858{
Chris Wilsonb29c19b2013-09-25 17:34:56 +01002859 struct drm_i915_private *dev_priv =
Chris Wilson67d97da2016-07-04 08:08:31 +01002860 container_of(work, typeof(*dev_priv), gt.retire_work.work);
Chris Wilson91c8a322016-07-05 10:40:23 +01002861 struct drm_device *dev = &dev_priv->drm;
Eric Anholt673a3942008-07-30 12:06:12 -07002862
Chris Wilson891b48c2010-09-29 12:26:37 +01002863 /* Come back later if the device is busy... */
Chris Wilsonb29c19b2013-09-25 17:34:56 +01002864 if (mutex_trylock(&dev->struct_mutex)) {
Chris Wilson67d97da2016-07-04 08:08:31 +01002865 i915_gem_retire_requests(dev_priv);
Chris Wilsonb29c19b2013-09-25 17:34:56 +01002866 mutex_unlock(&dev->struct_mutex);
2867 }
Chris Wilson67d97da2016-07-04 08:08:31 +01002868
2869 /* Keep the retire handler running until we are finally idle.
2870 * We do not need to do this test under locking as in the worst-case
2871 * we queue the retire worker once too often.
2872 */
Chris Wilsonc9615612016-07-09 10:12:06 +01002873 if (READ_ONCE(dev_priv->gt.awake)) {
2874 i915_queue_hangcheck(dev_priv);
Chris Wilson67d97da2016-07-04 08:08:31 +01002875 queue_delayed_work(dev_priv->wq,
2876 &dev_priv->gt.retire_work,
Chris Wilsonbcb45082012-10-05 17:02:57 +01002877 round_jiffies_up_relative(HZ));
Chris Wilsonc9615612016-07-09 10:12:06 +01002878 }
Chris Wilsonb29c19b2013-09-25 17:34:56 +01002879}
Chris Wilson891b48c2010-09-29 12:26:37 +01002880
Chris Wilsonb29c19b2013-09-25 17:34:56 +01002881static void
2882i915_gem_idle_work_handler(struct work_struct *work)
2883{
2884 struct drm_i915_private *dev_priv =
Chris Wilson67d97da2016-07-04 08:08:31 +01002885 container_of(work, typeof(*dev_priv), gt.idle_work.work);
Chris Wilson91c8a322016-07-05 10:40:23 +01002886 struct drm_device *dev = &dev_priv->drm;
Dave Gordonb4ac5af2016-03-24 11:20:38 +00002887 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05302888 enum intel_engine_id id;
Chris Wilson67d97da2016-07-04 08:08:31 +01002889 bool rearm_hangcheck;
2890
2891 if (!READ_ONCE(dev_priv->gt.awake))
2892 return;
2893
2894 if (READ_ONCE(dev_priv->gt.active_engines))
2895 return;
2896
2897 rearm_hangcheck =
2898 cancel_delayed_work_sync(&dev_priv->gpu_error.hangcheck_work);
2899
2900 if (!mutex_trylock(&dev->struct_mutex)) {
2901 /* Currently busy, come back later */
2902 mod_delayed_work(dev_priv->wq,
2903 &dev_priv->gt.idle_work,
2904 msecs_to_jiffies(50));
2905 goto out_rearm;
2906 }
2907
2908 if (dev_priv->gt.active_engines)
2909 goto out_unlock;
Zou Nan haid1b851f2010-05-21 09:08:57 +08002910
Akash Goel3b3f1652016-10-13 22:44:48 +05302911 for_each_engine(engine, dev_priv, id)
Chris Wilson67d97da2016-07-04 08:08:31 +01002912 i915_gem_batch_pool_fini(&engine->batch_pool);
Zou Nan hai852835f2010-05-21 09:08:56 +08002913
Chris Wilson67d97da2016-07-04 08:08:31 +01002914 GEM_BUG_ON(!dev_priv->gt.awake);
2915 dev_priv->gt.awake = false;
2916 rearm_hangcheck = false;
Daniel Vetter30ecad72015-12-09 09:29:36 +01002917
Chris Wilson67d97da2016-07-04 08:08:31 +01002918 if (INTEL_GEN(dev_priv) >= 6)
2919 gen6_rps_idle(dev_priv);
2920 intel_runtime_pm_put(dev_priv);
2921out_unlock:
2922 mutex_unlock(&dev->struct_mutex);
Chris Wilson35c94182015-04-07 16:20:37 +01002923
Chris Wilson67d97da2016-07-04 08:08:31 +01002924out_rearm:
2925 if (rearm_hangcheck) {
2926 GEM_BUG_ON(!dev_priv->gt.awake);
2927 i915_queue_hangcheck(dev_priv);
Chris Wilson35c94182015-04-07 16:20:37 +01002928 }
Eric Anholt673a3942008-07-30 12:06:12 -07002929}
2930
Chris Wilsonb1f788c2016-08-04 07:52:45 +01002931void i915_gem_close_object(struct drm_gem_object *gem, struct drm_file *file)
2932{
2933 struct drm_i915_gem_object *obj = to_intel_bo(gem);
2934 struct drm_i915_file_private *fpriv = file->driver_priv;
2935 struct i915_vma *vma, *vn;
2936
2937 mutex_lock(&obj->base.dev->struct_mutex);
2938 list_for_each_entry_safe(vma, vn, &obj->vma_list, obj_link)
2939 if (vma->vm->file == fpriv)
2940 i915_vma_close(vma);
Chris Wilsonf8a7fde2016-10-28 13:58:29 +01002941
2942 if (i915_gem_object_is_active(obj) &&
2943 !i915_gem_object_has_active_reference(obj)) {
2944 i915_gem_object_set_active_reference(obj);
2945 i915_gem_object_get(obj);
2946 }
Chris Wilsonb1f788c2016-08-04 07:52:45 +01002947 mutex_unlock(&obj->base.dev->struct_mutex);
2948}
2949
Chris Wilsone95433c2016-10-28 13:58:27 +01002950static unsigned long to_wait_timeout(s64 timeout_ns)
2951{
2952 if (timeout_ns < 0)
2953 return MAX_SCHEDULE_TIMEOUT;
2954
2955 if (timeout_ns == 0)
2956 return 0;
2957
2958 return nsecs_to_jiffies_timeout(timeout_ns);
2959}
2960
Ben Widawsky5816d642012-04-11 11:18:19 -07002961/**
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07002962 * i915_gem_wait_ioctl - implements DRM_IOCTL_I915_GEM_WAIT
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01002963 * @dev: drm device pointer
2964 * @data: ioctl data blob
2965 * @file: drm file pointer
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07002966 *
2967 * Returns 0 if successful, else an error is returned with the remaining time in
2968 * the timeout parameter.
2969 * -ETIME: object is still busy after timeout
2970 * -ERESTARTSYS: signal interrupted the wait
2971 * -ENONENT: object doesn't exist
2972 * Also possible, but rare:
2973 * -EAGAIN: GPU wedged
2974 * -ENOMEM: damn
2975 * -ENODEV: Internal IRQ fail
2976 * -E?: The add request failed
2977 *
2978 * The wait ioctl with a timeout of 0 reimplements the busy ioctl. With any
2979 * non-zero timeout parameter the wait ioctl will wait for the given number of
2980 * nanoseconds on an object becoming unbusy. Since the wait itself does so
2981 * without holding struct_mutex the object may become re-busied before this
2982 * function completes. A similar but shorter * race condition exists in the busy
2983 * ioctl
2984 */
2985int
2986i915_gem_wait_ioctl(struct drm_device *dev, void *data, struct drm_file *file)
2987{
2988 struct drm_i915_gem_wait *args = data;
2989 struct drm_i915_gem_object *obj;
Chris Wilsone95433c2016-10-28 13:58:27 +01002990 ktime_t start;
2991 long ret;
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07002992
Daniel Vetter11b5d512014-09-29 15:31:26 +02002993 if (args->flags != 0)
2994 return -EINVAL;
2995
Chris Wilson03ac0642016-07-20 13:31:51 +01002996 obj = i915_gem_object_lookup(file, args->bo_handle);
Chris Wilson033d5492016-08-05 10:14:17 +01002997 if (!obj)
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07002998 return -ENOENT;
Chris Wilson033d5492016-08-05 10:14:17 +01002999
Chris Wilsone95433c2016-10-28 13:58:27 +01003000 start = ktime_get();
3001
3002 ret = i915_gem_object_wait(obj,
3003 I915_WAIT_INTERRUPTIBLE | I915_WAIT_ALL,
3004 to_wait_timeout(args->timeout_ns),
3005 to_rps_client(file));
3006
3007 if (args->timeout_ns > 0) {
3008 args->timeout_ns -= ktime_to_ns(ktime_sub(ktime_get(), start));
3009 if (args->timeout_ns < 0)
3010 args->timeout_ns = 0;
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07003011 }
3012
Chris Wilson033d5492016-08-05 10:14:17 +01003013 i915_gem_object_put_unlocked(obj);
John Harrisonff865882014-11-24 18:49:28 +00003014 return ret;
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07003015}
3016
Chris Wilson8ef85612016-04-28 09:56:39 +01003017static void __i915_vma_iounmap(struct i915_vma *vma)
3018{
Chris Wilson20dfbde2016-08-04 16:32:30 +01003019 GEM_BUG_ON(i915_vma_is_pinned(vma));
Chris Wilson8ef85612016-04-28 09:56:39 +01003020
3021 if (vma->iomap == NULL)
3022 return;
3023
3024 io_mapping_unmap(vma->iomap);
3025 vma->iomap = NULL;
3026}
3027
Chris Wilsondf0e9a22016-08-04 07:52:47 +01003028int i915_vma_unbind(struct i915_vma *vma)
Eric Anholt673a3942008-07-30 12:06:12 -07003029{
Ben Widawsky07fe0b12013-07-31 17:00:10 -07003030 struct drm_i915_gem_object *obj = vma->obj;
Chris Wilsonb0decaf2016-08-04 07:52:44 +01003031 unsigned long active;
Chris Wilson43e28f02013-01-08 10:53:09 +00003032 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003033
Chris Wilsonb0decaf2016-08-04 07:52:44 +01003034 /* First wait upon any activity as retiring the request may
3035 * have side-effects such as unpinning or even unbinding this vma.
3036 */
3037 active = i915_vma_get_active(vma);
Chris Wilsondf0e9a22016-08-04 07:52:47 +01003038 if (active) {
Chris Wilsonb0decaf2016-08-04 07:52:44 +01003039 int idx;
3040
Chris Wilsonb1f788c2016-08-04 07:52:45 +01003041 /* When a closed VMA is retired, it is unbound - eek.
3042 * In order to prevent it from being recursively closed,
3043 * take a pin on the vma so that the second unbind is
3044 * aborted.
3045 */
Chris Wilson20dfbde2016-08-04 16:32:30 +01003046 __i915_vma_pin(vma);
Chris Wilsonb1f788c2016-08-04 07:52:45 +01003047
Chris Wilsonb0decaf2016-08-04 07:52:44 +01003048 for_each_active(active, idx) {
3049 ret = i915_gem_active_retire(&vma->last_read[idx],
3050 &vma->vm->dev->struct_mutex);
3051 if (ret)
Chris Wilsonb1f788c2016-08-04 07:52:45 +01003052 break;
Chris Wilsonb0decaf2016-08-04 07:52:44 +01003053 }
3054
Chris Wilson20dfbde2016-08-04 16:32:30 +01003055 __i915_vma_unpin(vma);
Chris Wilsonb1f788c2016-08-04 07:52:45 +01003056 if (ret)
3057 return ret;
3058
Chris Wilsonb0decaf2016-08-04 07:52:44 +01003059 GEM_BUG_ON(i915_vma_is_active(vma));
3060 }
3061
Chris Wilson20dfbde2016-08-04 16:32:30 +01003062 if (i915_vma_is_pinned(vma))
Chris Wilsonb0decaf2016-08-04 07:52:44 +01003063 return -EBUSY;
3064
Chris Wilsonb1f788c2016-08-04 07:52:45 +01003065 if (!drm_mm_node_allocated(&vma->node))
3066 goto destroy;
Ben Widawsky433544b2013-08-13 18:09:06 -07003067
Chris Wilson15717de2016-08-04 07:52:26 +01003068 GEM_BUG_ON(obj->bind_count == 0);
3069 GEM_BUG_ON(!obj->pages);
Chris Wilsonc4670ad2012-08-20 10:23:27 +01003070
Chris Wilson05a20d02016-08-18 17:16:55 +01003071 if (i915_vma_is_map_and_fenceable(vma)) {
Daniel Vetter8b1bc9b2014-02-14 14:06:07 +01003072 /* release the fence reg _after_ flushing */
Chris Wilson49ef5292016-08-18 17:17:00 +01003073 ret = i915_vma_put_fence(vma);
Daniel Vetter8b1bc9b2014-02-14 14:06:07 +01003074 if (ret)
3075 return ret;
Chris Wilson8ef85612016-04-28 09:56:39 +01003076
Chris Wilsoncd3127d2016-08-18 17:17:09 +01003077 /* Force a pagefault for domain tracking on next user access */
3078 i915_gem_release_mmap(obj);
3079
Chris Wilson8ef85612016-04-28 09:56:39 +01003080 __i915_vma_iounmap(vma);
Chris Wilson05a20d02016-08-18 17:16:55 +01003081 vma->flags &= ~I915_VMA_CAN_FENCE;
Daniel Vetter8b1bc9b2014-02-14 14:06:07 +01003082 }
Daniel Vetter96b47b62009-12-15 17:50:00 +01003083
Chris Wilson50e046b2016-08-04 07:52:46 +01003084 if (likely(!vma->vm->closed)) {
3085 trace_i915_vma_unbind(vma);
3086 vma->vm->unbind_vma(vma);
3087 }
Chris Wilson3272db52016-08-04 16:32:32 +01003088 vma->flags &= ~(I915_VMA_GLOBAL_BIND | I915_VMA_LOCAL_BIND);
Ben Widawsky6f65e292013-12-06 14:10:56 -08003089
Chris Wilson50e046b2016-08-04 07:52:46 +01003090 drm_mm_remove_node(&vma->node);
3091 list_move_tail(&vma->vm_link, &vma->vm->unbound_list);
3092
Chris Wilson05a20d02016-08-18 17:16:55 +01003093 if (vma->pages != obj->pages) {
3094 GEM_BUG_ON(!vma->pages);
3095 sg_free_table(vma->pages);
3096 kfree(vma->pages);
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00003097 }
Chris Wilson247177d2016-08-15 10:48:47 +01003098 vma->pages = NULL;
Eric Anholt673a3942008-07-30 12:06:12 -07003099
Ben Widawsky2f633152013-07-17 12:19:03 -07003100 /* Since the unbound list is global, only move to that list if
Daniel Vetterb93dab62013-08-26 11:23:47 +02003101 * no more VMAs exist. */
Chris Wilson15717de2016-08-04 07:52:26 +01003102 if (--obj->bind_count == 0)
3103 list_move_tail(&obj->global_list,
3104 &to_i915(obj->base.dev)->mm.unbound_list);
Eric Anholt673a3942008-07-30 12:06:12 -07003105
Chris Wilson70903c32013-12-04 09:59:09 +00003106 /* And finally now the object is completely decoupled from this vma,
3107 * we can drop its hold on the backing storage and allow it to be
3108 * reaped by the shrinker.
3109 */
3110 i915_gem_object_unpin_pages(obj);
3111
Chris Wilsonb1f788c2016-08-04 07:52:45 +01003112destroy:
Chris Wilson3272db52016-08-04 16:32:32 +01003113 if (unlikely(i915_vma_is_closed(vma)))
Chris Wilsonb1f788c2016-08-04 07:52:45 +01003114 i915_vma_destroy(vma);
3115
Chris Wilson88241782011-01-07 17:09:48 +00003116 return 0;
Chris Wilson54cf91d2010-11-25 18:00:26 +00003117}
3118
Chris Wilsondcff85c2016-08-05 10:14:11 +01003119int i915_gem_wait_for_idle(struct drm_i915_private *dev_priv,
Chris Wilsonea746f32016-09-09 14:11:49 +01003120 unsigned int flags)
Daniel Vetter4df2faf2010-02-19 11:52:00 +01003121{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00003122 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05303123 enum intel_engine_id id;
Dave Gordonb4ac5af2016-03-24 11:20:38 +00003124 int ret;
Daniel Vetter4df2faf2010-02-19 11:52:00 +01003125
Akash Goel3b3f1652016-10-13 22:44:48 +05303126 for_each_engine(engine, dev_priv, id) {
Chris Wilson62e63002016-06-24 14:55:52 +01003127 if (engine->last_context == NULL)
3128 continue;
3129
Chris Wilsonea746f32016-09-09 14:11:49 +01003130 ret = intel_engine_idle(engine, flags);
Chris Wilson1ec14ad2010-12-04 11:30:53 +00003131 if (ret)
3132 return ret;
3133 }
Zou Nan haid1b851f2010-05-21 09:08:57 +08003134
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01003135 return 0;
Daniel Vetter4df2faf2010-02-19 11:52:00 +01003136}
3137
Chris Wilson4144f9b2014-09-11 08:43:48 +01003138static bool i915_gem_valid_gtt_space(struct i915_vma *vma,
Chris Wilson42d6ab42012-07-26 11:49:32 +01003139 unsigned long cache_level)
3140{
Chris Wilson4144f9b2014-09-11 08:43:48 +01003141 struct drm_mm_node *gtt_space = &vma->node;
Chris Wilson42d6ab42012-07-26 11:49:32 +01003142 struct drm_mm_node *other;
3143
Chris Wilson4144f9b2014-09-11 08:43:48 +01003144 /*
3145 * On some machines we have to be careful when putting differing types
3146 * of snoopable memory together to avoid the prefetcher crossing memory
3147 * domains and dying. During vm initialisation, we decide whether or not
3148 * these constraints apply and set the drm_mm.color_adjust
3149 * appropriately.
Chris Wilson42d6ab42012-07-26 11:49:32 +01003150 */
Chris Wilson4144f9b2014-09-11 08:43:48 +01003151 if (vma->vm->mm.color_adjust == NULL)
Chris Wilson42d6ab42012-07-26 11:49:32 +01003152 return true;
3153
Ben Widawskyc6cfb322013-07-05 14:41:06 -07003154 if (!drm_mm_node_allocated(gtt_space))
Chris Wilson42d6ab42012-07-26 11:49:32 +01003155 return true;
3156
3157 if (list_empty(&gtt_space->node_list))
3158 return true;
3159
3160 other = list_entry(gtt_space->node_list.prev, struct drm_mm_node, node_list);
3161 if (other->allocated && !other->hole_follows && other->color != cache_level)
3162 return false;
3163
3164 other = list_entry(gtt_space->node_list.next, struct drm_mm_node, node_list);
3165 if (other->allocated && !gtt_space->hole_follows && other->color != cache_level)
3166 return false;
3167
3168 return true;
3169}
3170
Jesse Barnesde151cf2008-11-12 10:03:55 -08003171/**
Chris Wilson59bfa122016-08-04 16:32:31 +01003172 * i915_vma_insert - finds a slot for the vma in its address space
3173 * @vma: the vma
Chris Wilson91b2db62016-08-04 16:32:23 +01003174 * @size: requested size in bytes (can be larger than the VMA)
Chris Wilson59bfa122016-08-04 16:32:31 +01003175 * @alignment: required alignment
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01003176 * @flags: mask of PIN_* flags to use
Chris Wilson59bfa122016-08-04 16:32:31 +01003177 *
3178 * First we try to allocate some free space that meets the requirements for
3179 * the VMA. Failiing that, if the flags permit, it will evict an old VMA,
3180 * preferrably the oldest idle entry to make room for the new VMA.
3181 *
3182 * Returns:
3183 * 0 on success, negative error code otherwise.
Eric Anholt673a3942008-07-30 12:06:12 -07003184 */
Chris Wilson59bfa122016-08-04 16:32:31 +01003185static int
3186i915_vma_insert(struct i915_vma *vma, u64 size, u64 alignment, u64 flags)
Eric Anholt673a3942008-07-30 12:06:12 -07003187{
Chris Wilson59bfa122016-08-04 16:32:31 +01003188 struct drm_i915_private *dev_priv = to_i915(vma->vm->dev);
3189 struct drm_i915_gem_object *obj = vma->obj;
Chris Wilsonde180032016-08-04 16:32:29 +01003190 u64 start, end;
Chris Wilson07f73f62009-09-14 16:50:30 +01003191 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003192
Chris Wilson3272db52016-08-04 16:32:32 +01003193 GEM_BUG_ON(vma->flags & (I915_VMA_GLOBAL_BIND | I915_VMA_LOCAL_BIND));
Chris Wilson59bfa122016-08-04 16:32:31 +01003194 GEM_BUG_ON(drm_mm_node_allocated(&vma->node));
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02003195
Chris Wilsonde180032016-08-04 16:32:29 +01003196 size = max(size, vma->size);
3197 if (flags & PIN_MAPPABLE)
Chris Wilson3e510a82016-08-05 10:14:23 +01003198 size = i915_gem_get_ggtt_size(dev_priv, size,
3199 i915_gem_object_get_tiling(obj));
Joonas Lahtinen91e67112015-05-06 14:33:58 +03003200
Chris Wilsond8923dc2016-08-18 17:17:07 +01003201 alignment = max(max(alignment, vma->display_alignment),
3202 i915_gem_get_ggtt_alignment(dev_priv, size,
3203 i915_gem_object_get_tiling(obj),
3204 flags & PIN_MAPPABLE));
Chris Wilsona00b10c2010-09-24 21:15:47 +01003205
Michel Thierry101b5062015-10-01 13:33:57 +01003206 start = flags & PIN_OFFSET_BIAS ? flags & PIN_OFFSET_MASK : 0;
Chris Wilsonde180032016-08-04 16:32:29 +01003207
3208 end = vma->vm->total;
Michel Thierry101b5062015-10-01 13:33:57 +01003209 if (flags & PIN_MAPPABLE)
Chris Wilson91b2db62016-08-04 16:32:23 +01003210 end = min_t(u64, end, dev_priv->ggtt.mappable_end);
Michel Thierry101b5062015-10-01 13:33:57 +01003211 if (flags & PIN_ZONE_4G)
Michel Thierry48ea1e32016-01-11 11:39:27 +00003212 end = min_t(u64, end, (1ULL << 32) - PAGE_SIZE);
Michel Thierry101b5062015-10-01 13:33:57 +01003213
Joonas Lahtinen91e67112015-05-06 14:33:58 +03003214 /* If binding the object/GGTT view requires more space than the entire
3215 * aperture has, reject it early before evicting everything in a vain
3216 * attempt to find space.
Chris Wilson654fc602010-05-27 13:18:21 +01003217 */
Joonas Lahtinen91e67112015-05-06 14:33:58 +03003218 if (size > end) {
Chris Wilsonde180032016-08-04 16:32:29 +01003219 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 +01003220 size, obj->base.size,
Daniel Vetter1ec9e262014-02-14 14:01:11 +01003221 flags & PIN_MAPPABLE ? "mappable" : "total",
Chris Wilsond23db882014-05-23 08:48:08 +02003222 end);
Chris Wilson59bfa122016-08-04 16:32:31 +01003223 return -E2BIG;
Chris Wilson654fc602010-05-27 13:18:21 +01003224 }
3225
Chris Wilson37e680a2012-06-07 15:38:42 +01003226 ret = i915_gem_object_get_pages(obj);
Chris Wilson6c085a72012-08-20 11:40:46 +02003227 if (ret)
Chris Wilson59bfa122016-08-04 16:32:31 +01003228 return ret;
Chris Wilson6c085a72012-08-20 11:40:46 +02003229
Chris Wilsonfbdda6f2012-11-20 10:45:16 +00003230 i915_gem_object_pin_pages(obj);
3231
Chris Wilson506a8e82015-12-08 11:55:07 +00003232 if (flags & PIN_OFFSET_FIXED) {
Chris Wilson59bfa122016-08-04 16:32:31 +01003233 u64 offset = flags & PIN_OFFSET_MASK;
Chris Wilsonde180032016-08-04 16:32:29 +01003234 if (offset & (alignment - 1) || offset > end - size) {
Chris Wilson506a8e82015-12-08 11:55:07 +00003235 ret = -EINVAL;
Chris Wilsonde180032016-08-04 16:32:29 +01003236 goto err_unpin;
Chris Wilson506a8e82015-12-08 11:55:07 +00003237 }
Chris Wilsonde180032016-08-04 16:32:29 +01003238
Chris Wilson506a8e82015-12-08 11:55:07 +00003239 vma->node.start = offset;
3240 vma->node.size = size;
3241 vma->node.color = obj->cache_level;
Chris Wilsonde180032016-08-04 16:32:29 +01003242 ret = drm_mm_reserve_node(&vma->vm->mm, &vma->node);
Chris Wilson506a8e82015-12-08 11:55:07 +00003243 if (ret) {
3244 ret = i915_gem_evict_for_vma(vma);
3245 if (ret == 0)
Chris Wilsonde180032016-08-04 16:32:29 +01003246 ret = drm_mm_reserve_node(&vma->vm->mm, &vma->node);
3247 if (ret)
3248 goto err_unpin;
Chris Wilson506a8e82015-12-08 11:55:07 +00003249 }
Michel Thierry101b5062015-10-01 13:33:57 +01003250 } else {
Chris Wilsonde180032016-08-04 16:32:29 +01003251 u32 search_flag, alloc_flag;
3252
Chris Wilson506a8e82015-12-08 11:55:07 +00003253 if (flags & PIN_HIGH) {
3254 search_flag = DRM_MM_SEARCH_BELOW;
3255 alloc_flag = DRM_MM_CREATE_TOP;
3256 } else {
3257 search_flag = DRM_MM_SEARCH_DEFAULT;
3258 alloc_flag = DRM_MM_CREATE_DEFAULT;
3259 }
Michel Thierry101b5062015-10-01 13:33:57 +01003260
Chris Wilson954c4692016-08-04 16:32:26 +01003261 /* We only allocate in PAGE_SIZE/GTT_PAGE_SIZE (4096) chunks,
3262 * so we know that we always have a minimum alignment of 4096.
3263 * The drm_mm range manager is optimised to return results
3264 * with zero alignment, so where possible use the optimal
3265 * path.
3266 */
3267 if (alignment <= 4096)
3268 alignment = 0;
3269
Ben Widawsky0a9ae0d2013-05-25 12:26:35 -07003270search_free:
Chris Wilsonde180032016-08-04 16:32:29 +01003271 ret = drm_mm_insert_node_in_range_generic(&vma->vm->mm,
3272 &vma->node,
Chris Wilson506a8e82015-12-08 11:55:07 +00003273 size, alignment,
3274 obj->cache_level,
3275 start, end,
3276 search_flag,
3277 alloc_flag);
3278 if (ret) {
Chris Wilsonde180032016-08-04 16:32:29 +01003279 ret = i915_gem_evict_something(vma->vm, size, alignment,
Chris Wilson506a8e82015-12-08 11:55:07 +00003280 obj->cache_level,
3281 start, end,
3282 flags);
3283 if (ret == 0)
3284 goto search_free;
Chris Wilson97311292009-09-21 00:22:34 +01003285
Chris Wilsonde180032016-08-04 16:32:29 +01003286 goto err_unpin;
Chris Wilson506a8e82015-12-08 11:55:07 +00003287 }
Chris Wilsonad16d2e2016-10-13 09:55:04 +01003288
3289 GEM_BUG_ON(vma->node.start < start);
3290 GEM_BUG_ON(vma->node.start + vma->node.size > end);
Chris Wilsondc9dd7a2012-12-07 20:37:07 +00003291 }
Chris Wilson37508582016-08-04 16:32:24 +01003292 GEM_BUG_ON(!i915_gem_valid_gtt_space(vma, obj->cache_level));
Eric Anholt673a3942008-07-30 12:06:12 -07003293
Ben Widawsky35c20a62013-05-31 11:28:48 -07003294 list_move_tail(&obj->global_list, &dev_priv->mm.bound_list);
Chris Wilsonde180032016-08-04 16:32:29 +01003295 list_move_tail(&vma->vm_link, &vma->vm->inactive_list);
Chris Wilson15717de2016-08-04 07:52:26 +01003296 obj->bind_count++;
Chris Wilsonbf1a1092010-08-07 11:01:20 +01003297
Chris Wilson59bfa122016-08-04 16:32:31 +01003298 return 0;
Ben Widawsky2f633152013-07-17 12:19:03 -07003299
Daniel Vetterbc6bc152013-07-22 12:12:38 +02003300err_unpin:
Ben Widawsky2f633152013-07-17 12:19:03 -07003301 i915_gem_object_unpin_pages(obj);
Chris Wilson59bfa122016-08-04 16:32:31 +01003302 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003303}
3304
Chris Wilson000433b2013-08-08 14:41:09 +01003305bool
Chris Wilson2c225692013-08-09 12:26:45 +01003306i915_gem_clflush_object(struct drm_i915_gem_object *obj,
3307 bool force)
Eric Anholt673a3942008-07-30 12:06:12 -07003308{
Eric Anholt673a3942008-07-30 12:06:12 -07003309 /* If we don't have a page list set up, then we're not pinned
3310 * to GPU, and we can ignore the cache flush because it'll happen
3311 * again at bind time.
3312 */
Chris Wilson05394f32010-11-08 19:18:58 +00003313 if (obj->pages == NULL)
Chris Wilson000433b2013-08-08 14:41:09 +01003314 return false;
Eric Anholt673a3942008-07-30 12:06:12 -07003315
Imre Deak769ce462013-02-13 21:56:05 +02003316 /*
3317 * Stolen memory is always coherent with the GPU as it is explicitly
3318 * marked as wc by the system, or the system is cache-coherent.
3319 */
Chris Wilson6a2c4232014-11-04 04:51:40 -08003320 if (obj->stolen || obj->phys_handle)
Chris Wilson000433b2013-08-08 14:41:09 +01003321 return false;
Imre Deak769ce462013-02-13 21:56:05 +02003322
Chris Wilson9c23f7f2011-03-29 16:59:52 -07003323 /* If the GPU is snooping the contents of the CPU cache,
3324 * we do not need to manually clear the CPU cache lines. However,
3325 * the caches are only snooped when the render cache is
3326 * flushed/invalidated. As we always have to emit invalidations
3327 * and flushes when moving into and out of the RENDER domain, correct
3328 * snooping behaviour occurs naturally as the result of our domain
3329 * tracking.
3330 */
Chris Wilson0f719792015-01-13 13:32:52 +00003331 if (!force && cpu_cache_is_coherent(obj->base.dev, obj->cache_level)) {
3332 obj->cache_dirty = true;
Chris Wilson000433b2013-08-08 14:41:09 +01003333 return false;
Chris Wilson0f719792015-01-13 13:32:52 +00003334 }
Chris Wilson9c23f7f2011-03-29 16:59:52 -07003335
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003336 trace_i915_gem_object_clflush(obj);
Chris Wilson9da3da62012-06-01 15:20:22 +01003337 drm_clflush_sg(obj->pages);
Chris Wilson0f719792015-01-13 13:32:52 +00003338 obj->cache_dirty = false;
Chris Wilson000433b2013-08-08 14:41:09 +01003339
3340 return true;
Eric Anholte47c68e2008-11-14 13:35:19 -08003341}
3342
3343/** Flushes the GTT write domain for the object if it's dirty. */
3344static void
Chris Wilson05394f32010-11-08 19:18:58 +00003345i915_gem_object_flush_gtt_write_domain(struct drm_i915_gem_object *obj)
Eric Anholte47c68e2008-11-14 13:35:19 -08003346{
Chris Wilson3b5724d2016-08-18 17:16:49 +01003347 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003348
Chris Wilson05394f32010-11-08 19:18:58 +00003349 if (obj->base.write_domain != I915_GEM_DOMAIN_GTT)
Eric Anholte47c68e2008-11-14 13:35:19 -08003350 return;
3351
Chris Wilson63256ec2011-01-04 18:42:07 +00003352 /* No actual flushing is required for the GTT write domain. Writes
Chris Wilson3b5724d2016-08-18 17:16:49 +01003353 * to it "immediately" go to main memory as far as we know, so there's
Eric Anholte47c68e2008-11-14 13:35:19 -08003354 * no chipset flush. It also doesn't land in render cache.
Chris Wilson63256ec2011-01-04 18:42:07 +00003355 *
3356 * However, we do have to enforce the order so that all writes through
3357 * the GTT land before any writes to the device, such as updates to
3358 * the GATT itself.
Chris Wilson3b5724d2016-08-18 17:16:49 +01003359 *
3360 * We also have to wait a bit for the writes to land from the GTT.
3361 * An uncached read (i.e. mmio) seems to be ideal for the round-trip
3362 * timing. This issue has only been observed when switching quickly
3363 * between GTT writes and CPU reads from inside the kernel on recent hw,
3364 * and it appears to only affect discrete GTT blocks (i.e. on LLC
3365 * system agents we cannot reproduce this behaviour).
Eric Anholte47c68e2008-11-14 13:35:19 -08003366 */
Chris Wilson63256ec2011-01-04 18:42:07 +00003367 wmb();
Chris Wilson3b5724d2016-08-18 17:16:49 +01003368 if (INTEL_GEN(dev_priv) >= 6 && !HAS_LLC(dev_priv))
Akash Goel3b3f1652016-10-13 22:44:48 +05303369 POSTING_READ(RING_ACTHD(dev_priv->engine[RCS]->mmio_base));
Chris Wilson63256ec2011-01-04 18:42:07 +00003370
Chris Wilsond243ad82016-08-18 17:16:44 +01003371 intel_fb_obj_flush(obj, false, write_origin(obj, I915_GEM_DOMAIN_GTT));
Daniel Vetterf99d7062014-06-19 16:01:59 +02003372
Chris Wilsonb0dc4652016-08-18 17:16:51 +01003373 obj->base.write_domain = 0;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003374 trace_i915_gem_object_change_domain(obj,
Chris Wilson05394f32010-11-08 19:18:58 +00003375 obj->base.read_domains,
Chris Wilsonb0dc4652016-08-18 17:16:51 +01003376 I915_GEM_DOMAIN_GTT);
Eric Anholte47c68e2008-11-14 13:35:19 -08003377}
3378
3379/** Flushes the CPU write domain for the object if it's dirty. */
3380static void
Daniel Vettere62b59e2015-01-21 14:53:48 +01003381i915_gem_object_flush_cpu_write_domain(struct drm_i915_gem_object *obj)
Eric Anholte47c68e2008-11-14 13:35:19 -08003382{
Chris Wilson05394f32010-11-08 19:18:58 +00003383 if (obj->base.write_domain != I915_GEM_DOMAIN_CPU)
Eric Anholte47c68e2008-11-14 13:35:19 -08003384 return;
3385
Daniel Vettere62b59e2015-01-21 14:53:48 +01003386 if (i915_gem_clflush_object(obj, obj->pin_display))
Chris Wilsonc0336662016-05-06 15:40:21 +01003387 i915_gem_chipset_flush(to_i915(obj->base.dev));
Chris Wilson000433b2013-08-08 14:41:09 +01003388
Rodrigo Vivide152b62015-07-07 16:28:51 -07003389 intel_fb_obj_flush(obj, false, ORIGIN_CPU);
Daniel Vetterf99d7062014-06-19 16:01:59 +02003390
Chris Wilsonb0dc4652016-08-18 17:16:51 +01003391 obj->base.write_domain = 0;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003392 trace_i915_gem_object_change_domain(obj,
Chris Wilson05394f32010-11-08 19:18:58 +00003393 obj->base.read_domains,
Chris Wilsonb0dc4652016-08-18 17:16:51 +01003394 I915_GEM_DOMAIN_CPU);
Eric Anholte47c68e2008-11-14 13:35:19 -08003395}
3396
Chris Wilson383d5822016-08-18 17:17:08 +01003397static void i915_gem_object_bump_inactive_ggtt(struct drm_i915_gem_object *obj)
3398{
3399 struct i915_vma *vma;
3400
3401 list_for_each_entry(vma, &obj->vma_list, obj_link) {
3402 if (!i915_vma_is_ggtt(vma))
3403 continue;
3404
3405 if (i915_vma_is_active(vma))
3406 continue;
3407
3408 if (!drm_mm_node_allocated(&vma->node))
3409 continue;
3410
3411 list_move_tail(&vma->vm_link, &vma->vm->inactive_list);
3412 }
3413}
3414
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003415/**
3416 * Moves a single object to the GTT read, and possibly write domain.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01003417 * @obj: object to act on
3418 * @write: ask for write access or read only
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003419 *
3420 * This function returns when the move is complete, including waiting on
3421 * flushes to occur.
3422 */
Jesse Barnes79e53942008-11-07 14:24:08 -08003423int
Chris Wilson20217462010-11-23 15:26:33 +00003424i915_gem_object_set_to_gtt_domain(struct drm_i915_gem_object *obj, bool write)
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003425{
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003426 uint32_t old_write_domain, old_read_domains;
Eric Anholte47c68e2008-11-14 13:35:19 -08003427 int ret;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003428
Chris Wilsone95433c2016-10-28 13:58:27 +01003429 lockdep_assert_held(&obj->base.dev->struct_mutex);
3430 ret = i915_gem_object_wait(obj,
3431 I915_WAIT_INTERRUPTIBLE |
3432 I915_WAIT_LOCKED |
3433 (write ? I915_WAIT_ALL : 0),
3434 MAX_SCHEDULE_TIMEOUT,
3435 NULL);
Chris Wilson88241782011-01-07 17:09:48 +00003436 if (ret)
3437 return ret;
3438
Chris Wilsonc13d87e2016-07-20 09:21:15 +01003439 if (obj->base.write_domain == I915_GEM_DOMAIN_GTT)
3440 return 0;
3441
Chris Wilson43566de2015-01-02 16:29:29 +05303442 /* Flush and acquire obj->pages so that we are coherent through
3443 * direct access in memory with previous cached writes through
3444 * shmemfs and that our cache domain tracking remains valid.
3445 * For example, if the obj->filp was moved to swap without us
3446 * being notified and releasing the pages, we would mistakenly
3447 * continue to assume that the obj remained out of the CPU cached
3448 * domain.
3449 */
3450 ret = i915_gem_object_get_pages(obj);
3451 if (ret)
3452 return ret;
3453
Daniel Vettere62b59e2015-01-21 14:53:48 +01003454 i915_gem_object_flush_cpu_write_domain(obj);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003455
Chris Wilsond0a57782012-10-09 19:24:37 +01003456 /* Serialise direct access to this object with the barriers for
3457 * coherent writes from the GPU, by effectively invalidating the
3458 * GTT domain upon first access.
3459 */
3460 if ((obj->base.read_domains & I915_GEM_DOMAIN_GTT) == 0)
3461 mb();
3462
Chris Wilson05394f32010-11-08 19:18:58 +00003463 old_write_domain = obj->base.write_domain;
3464 old_read_domains = obj->base.read_domains;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003465
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003466 /* It should now be out of any other write domains, and we can update
3467 * the domain values for our changes.
3468 */
Chris Wilson05394f32010-11-08 19:18:58 +00003469 BUG_ON((obj->base.write_domain & ~I915_GEM_DOMAIN_GTT) != 0);
3470 obj->base.read_domains |= I915_GEM_DOMAIN_GTT;
Eric Anholte47c68e2008-11-14 13:35:19 -08003471 if (write) {
Chris Wilson05394f32010-11-08 19:18:58 +00003472 obj->base.read_domains = I915_GEM_DOMAIN_GTT;
3473 obj->base.write_domain = I915_GEM_DOMAIN_GTT;
3474 obj->dirty = 1;
Eric Anholte47c68e2008-11-14 13:35:19 -08003475 }
3476
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003477 trace_i915_gem_object_change_domain(obj,
3478 old_read_domains,
3479 old_write_domain);
3480
Chris Wilson8325a092012-04-24 15:52:35 +01003481 /* And bump the LRU for this access */
Chris Wilson383d5822016-08-18 17:17:08 +01003482 i915_gem_object_bump_inactive_ggtt(obj);
Chris Wilson8325a092012-04-24 15:52:35 +01003483
Eric Anholte47c68e2008-11-14 13:35:19 -08003484 return 0;
3485}
3486
Chris Wilsonef55f922015-10-09 14:11:27 +01003487/**
3488 * Changes the cache-level of an object across all VMA.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01003489 * @obj: object to act on
3490 * @cache_level: new cache level to set for the object
Chris Wilsonef55f922015-10-09 14:11:27 +01003491 *
3492 * After this function returns, the object will be in the new cache-level
3493 * across all GTT and the contents of the backing storage will be coherent,
3494 * with respect to the new cache-level. In order to keep the backing storage
3495 * coherent for all users, we only allow a single cache level to be set
3496 * globally on the object and prevent it from being changed whilst the
3497 * hardware is reading from the object. That is if the object is currently
3498 * on the scanout it will be set to uncached (or equivalent display
3499 * cache coherency) and all non-MOCS GPU access will also be uncached so
3500 * that all direct access to the scanout remains coherent.
3501 */
Chris Wilsone4ffd172011-04-04 09:44:39 +01003502int i915_gem_object_set_cache_level(struct drm_i915_gem_object *obj,
3503 enum i915_cache_level cache_level)
3504{
Chris Wilsonaa653a62016-08-04 07:52:27 +01003505 struct i915_vma *vma;
Ville Syrjäläed75a552015-08-11 19:47:10 +03003506 int ret = 0;
Chris Wilsone4ffd172011-04-04 09:44:39 +01003507
3508 if (obj->cache_level == cache_level)
Ville Syrjäläed75a552015-08-11 19:47:10 +03003509 goto out;
Chris Wilsone4ffd172011-04-04 09:44:39 +01003510
Chris Wilsonef55f922015-10-09 14:11:27 +01003511 /* Inspect the list of currently bound VMA and unbind any that would
3512 * be invalid given the new cache-level. This is principally to
3513 * catch the issue of the CS prefetch crossing page boundaries and
3514 * reading an invalid PTE on older architectures.
3515 */
Chris Wilsonaa653a62016-08-04 07:52:27 +01003516restart:
3517 list_for_each_entry(vma, &obj->vma_list, obj_link) {
Chris Wilsonef55f922015-10-09 14:11:27 +01003518 if (!drm_mm_node_allocated(&vma->node))
3519 continue;
3520
Chris Wilson20dfbde2016-08-04 16:32:30 +01003521 if (i915_vma_is_pinned(vma)) {
Chris Wilsonef55f922015-10-09 14:11:27 +01003522 DRM_DEBUG("can not change the cache level of pinned objects\n");
3523 return -EBUSY;
3524 }
3525
Chris Wilsonaa653a62016-08-04 07:52:27 +01003526 if (i915_gem_valid_gtt_space(vma, cache_level))
3527 continue;
3528
3529 ret = i915_vma_unbind(vma);
3530 if (ret)
3531 return ret;
3532
3533 /* As unbinding may affect other elements in the
3534 * obj->vma_list (due to side-effects from retiring
3535 * an active vma), play safe and restart the iterator.
3536 */
3537 goto restart;
Chris Wilson42d6ab42012-07-26 11:49:32 +01003538 }
3539
Chris Wilsonef55f922015-10-09 14:11:27 +01003540 /* We can reuse the existing drm_mm nodes but need to change the
3541 * cache-level on the PTE. We could simply unbind them all and
3542 * rebind with the correct cache-level on next use. However since
3543 * we already have a valid slot, dma mapping, pages etc, we may as
3544 * rewrite the PTE in the belief that doing so tramples upon less
3545 * state and so involves less work.
3546 */
Chris Wilson15717de2016-08-04 07:52:26 +01003547 if (obj->bind_count) {
Chris Wilsonef55f922015-10-09 14:11:27 +01003548 /* Before we change the PTE, the GPU must not be accessing it.
3549 * If we wait upon the object, we know that all the bound
3550 * VMA are no longer active.
3551 */
Chris Wilsone95433c2016-10-28 13:58:27 +01003552 ret = i915_gem_object_wait(obj,
3553 I915_WAIT_INTERRUPTIBLE |
3554 I915_WAIT_LOCKED |
3555 I915_WAIT_ALL,
3556 MAX_SCHEDULE_TIMEOUT,
3557 NULL);
Chris Wilsone4ffd172011-04-04 09:44:39 +01003558 if (ret)
3559 return ret;
3560
Chris Wilsonaa653a62016-08-04 07:52:27 +01003561 if (!HAS_LLC(obj->base.dev) && cache_level != I915_CACHE_NONE) {
Chris Wilsonef55f922015-10-09 14:11:27 +01003562 /* Access to snoopable pages through the GTT is
3563 * incoherent and on some machines causes a hard
3564 * lockup. Relinquish the CPU mmaping to force
3565 * userspace to refault in the pages and we can
3566 * then double check if the GTT mapping is still
3567 * valid for that pointer access.
3568 */
3569 i915_gem_release_mmap(obj);
Chris Wilsone4ffd172011-04-04 09:44:39 +01003570
Chris Wilsonef55f922015-10-09 14:11:27 +01003571 /* As we no longer need a fence for GTT access,
3572 * we can relinquish it now (and so prevent having
3573 * to steal a fence from someone else on the next
3574 * fence request). Note GPU activity would have
3575 * dropped the fence as all snoopable access is
3576 * supposed to be linear.
3577 */
Chris Wilson49ef5292016-08-18 17:17:00 +01003578 list_for_each_entry(vma, &obj->vma_list, obj_link) {
3579 ret = i915_vma_put_fence(vma);
3580 if (ret)
3581 return ret;
3582 }
Chris Wilsonef55f922015-10-09 14:11:27 +01003583 } else {
3584 /* We either have incoherent backing store and
3585 * so no GTT access or the architecture is fully
3586 * coherent. In such cases, existing GTT mmaps
3587 * ignore the cache bit in the PTE and we can
3588 * rewrite it without confusing the GPU or having
3589 * to force userspace to fault back in its mmaps.
3590 */
Chris Wilsone4ffd172011-04-04 09:44:39 +01003591 }
3592
Chris Wilson1c7f4bc2016-02-26 11:03:19 +00003593 list_for_each_entry(vma, &obj->vma_list, obj_link) {
Chris Wilsonef55f922015-10-09 14:11:27 +01003594 if (!drm_mm_node_allocated(&vma->node))
3595 continue;
3596
3597 ret = i915_vma_bind(vma, cache_level, PIN_UPDATE);
3598 if (ret)
3599 return ret;
3600 }
Chris Wilsone4ffd172011-04-04 09:44:39 +01003601 }
3602
Chris Wilson1c7f4bc2016-02-26 11:03:19 +00003603 list_for_each_entry(vma, &obj->vma_list, obj_link)
Chris Wilson2c225692013-08-09 12:26:45 +01003604 vma->node.color = cache_level;
3605 obj->cache_level = cache_level;
3606
Ville Syrjäläed75a552015-08-11 19:47:10 +03003607out:
Chris Wilsonef55f922015-10-09 14:11:27 +01003608 /* Flush the dirty CPU caches to the backing storage so that the
3609 * object is now coherent at its new cache level (with respect
3610 * to the access domain).
3611 */
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05303612 if (obj->cache_dirty && cpu_write_needs_clflush(obj)) {
Chris Wilson0f719792015-01-13 13:32:52 +00003613 if (i915_gem_clflush_object(obj, true))
Chris Wilsonc0336662016-05-06 15:40:21 +01003614 i915_gem_chipset_flush(to_i915(obj->base.dev));
Chris Wilsone4ffd172011-04-04 09:44:39 +01003615 }
3616
Chris Wilsone4ffd172011-04-04 09:44:39 +01003617 return 0;
3618}
3619
Ben Widawsky199adf42012-09-21 17:01:20 -07003620int i915_gem_get_caching_ioctl(struct drm_device *dev, void *data,
3621 struct drm_file *file)
Chris Wilsone6994ae2012-07-10 10:27:08 +01003622{
Ben Widawsky199adf42012-09-21 17:01:20 -07003623 struct drm_i915_gem_caching *args = data;
Chris Wilsone6994ae2012-07-10 10:27:08 +01003624 struct drm_i915_gem_object *obj;
Chris Wilsone6994ae2012-07-10 10:27:08 +01003625
Chris Wilson03ac0642016-07-20 13:31:51 +01003626 obj = i915_gem_object_lookup(file, args->handle);
3627 if (!obj)
Chris Wilson432be692015-05-07 12:14:55 +01003628 return -ENOENT;
Chris Wilsone6994ae2012-07-10 10:27:08 +01003629
Chris Wilson651d7942013-08-08 14:41:10 +01003630 switch (obj->cache_level) {
3631 case I915_CACHE_LLC:
3632 case I915_CACHE_L3_LLC:
3633 args->caching = I915_CACHING_CACHED;
3634 break;
3635
Chris Wilson4257d3b2013-08-08 14:41:11 +01003636 case I915_CACHE_WT:
3637 args->caching = I915_CACHING_DISPLAY;
3638 break;
3639
Chris Wilson651d7942013-08-08 14:41:10 +01003640 default:
3641 args->caching = I915_CACHING_NONE;
3642 break;
3643 }
Chris Wilsone6994ae2012-07-10 10:27:08 +01003644
Chris Wilson34911fd2016-07-20 13:31:54 +01003645 i915_gem_object_put_unlocked(obj);
Chris Wilson432be692015-05-07 12:14:55 +01003646 return 0;
Chris Wilsone6994ae2012-07-10 10:27:08 +01003647}
3648
Ben Widawsky199adf42012-09-21 17:01:20 -07003649int i915_gem_set_caching_ioctl(struct drm_device *dev, void *data,
3650 struct drm_file *file)
Chris Wilsone6994ae2012-07-10 10:27:08 +01003651{
Chris Wilson9c870d02016-10-24 13:42:15 +01003652 struct drm_i915_private *i915 = to_i915(dev);
Ben Widawsky199adf42012-09-21 17:01:20 -07003653 struct drm_i915_gem_caching *args = data;
Chris Wilsone6994ae2012-07-10 10:27:08 +01003654 struct drm_i915_gem_object *obj;
3655 enum i915_cache_level level;
3656 int ret;
3657
Ben Widawsky199adf42012-09-21 17:01:20 -07003658 switch (args->caching) {
3659 case I915_CACHING_NONE:
Chris Wilsone6994ae2012-07-10 10:27:08 +01003660 level = I915_CACHE_NONE;
3661 break;
Ben Widawsky199adf42012-09-21 17:01:20 -07003662 case I915_CACHING_CACHED:
Imre Deake5756c12015-08-14 18:43:30 +03003663 /*
3664 * Due to a HW issue on BXT A stepping, GPU stores via a
3665 * snooped mapping may leave stale data in a corresponding CPU
3666 * cacheline, whereas normally such cachelines would get
3667 * invalidated.
3668 */
Chris Wilson9c870d02016-10-24 13:42:15 +01003669 if (!HAS_LLC(i915) && !HAS_SNOOP(i915))
Imre Deake5756c12015-08-14 18:43:30 +03003670 return -ENODEV;
3671
Chris Wilsone6994ae2012-07-10 10:27:08 +01003672 level = I915_CACHE_LLC;
3673 break;
Chris Wilson4257d3b2013-08-08 14:41:11 +01003674 case I915_CACHING_DISPLAY:
Chris Wilson9c870d02016-10-24 13:42:15 +01003675 level = HAS_WT(i915) ? I915_CACHE_WT : I915_CACHE_NONE;
Chris Wilson4257d3b2013-08-08 14:41:11 +01003676 break;
Chris Wilsone6994ae2012-07-10 10:27:08 +01003677 default:
3678 return -EINVAL;
3679 }
3680
Ben Widawsky3bc29132012-09-26 16:15:20 -07003681 ret = i915_mutex_lock_interruptible(dev);
3682 if (ret)
Chris Wilson9c870d02016-10-24 13:42:15 +01003683 return ret;
Ben Widawsky3bc29132012-09-26 16:15:20 -07003684
Chris Wilson03ac0642016-07-20 13:31:51 +01003685 obj = i915_gem_object_lookup(file, args->handle);
3686 if (!obj) {
Chris Wilsone6994ae2012-07-10 10:27:08 +01003687 ret = -ENOENT;
3688 goto unlock;
3689 }
3690
3691 ret = i915_gem_object_set_cache_level(obj, level);
Chris Wilsonf8c417c2016-07-20 13:31:53 +01003692 i915_gem_object_put(obj);
Chris Wilsone6994ae2012-07-10 10:27:08 +01003693unlock:
3694 mutex_unlock(&dev->struct_mutex);
3695 return ret;
3696}
3697
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003698/*
Chris Wilson2da3b9b2011-04-14 09:41:17 +01003699 * Prepare buffer for display plane (scanout, cursors, etc).
3700 * Can be called from an uninterruptible phase (modesetting) and allows
3701 * any flushes to be pipelined (for pageflips).
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003702 */
Chris Wilson058d88c2016-08-15 10:49:06 +01003703struct i915_vma *
Chris Wilson2da3b9b2011-04-14 09:41:17 +01003704i915_gem_object_pin_to_display_plane(struct drm_i915_gem_object *obj,
3705 u32 alignment,
Tvrtko Ursuline6617332015-03-23 11:10:33 +00003706 const struct i915_ggtt_view *view)
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003707{
Chris Wilson058d88c2016-08-15 10:49:06 +01003708 struct i915_vma *vma;
Chris Wilson2da3b9b2011-04-14 09:41:17 +01003709 u32 old_read_domains, old_write_domain;
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003710 int ret;
3711
Chris Wilsoncc98b412013-08-09 12:25:09 +01003712 /* Mark the pin_display early so that we account for the
3713 * display coherency whilst setting up the cache domains.
3714 */
Tvrtko Ursulin8a0c39b2015-04-13 11:50:09 +01003715 obj->pin_display++;
Chris Wilsoncc98b412013-08-09 12:25:09 +01003716
Eric Anholta7ef0642011-03-29 16:59:54 -07003717 /* The display engine is not coherent with the LLC cache on gen6. As
3718 * a result, we make sure that the pinning that is about to occur is
3719 * done with uncached PTEs. This is lowest common denominator for all
3720 * chipsets.
3721 *
3722 * However for gen6+, we could do better by using the GFDT bit instead
3723 * of uncaching, which would allow us to flush all the LLC-cached data
3724 * with that bit in the PTE to main memory with just one PIPE_CONTROL.
3725 */
Chris Wilson651d7942013-08-08 14:41:10 +01003726 ret = i915_gem_object_set_cache_level(obj,
Tvrtko Ursulin86527442016-10-13 11:03:00 +01003727 HAS_WT(to_i915(obj->base.dev)) ?
3728 I915_CACHE_WT : I915_CACHE_NONE);
Chris Wilson058d88c2016-08-15 10:49:06 +01003729 if (ret) {
3730 vma = ERR_PTR(ret);
Chris Wilsoncc98b412013-08-09 12:25:09 +01003731 goto err_unpin_display;
Chris Wilson058d88c2016-08-15 10:49:06 +01003732 }
Eric Anholta7ef0642011-03-29 16:59:54 -07003733
Chris Wilson2da3b9b2011-04-14 09:41:17 +01003734 /* As the user may map the buffer once pinned in the display plane
3735 * (e.g. libkms for the bootup splash), we have to ensure that we
Chris Wilson2efb8132016-08-18 17:17:06 +01003736 * always use map_and_fenceable for all scanout buffers. However,
3737 * it may simply be too big to fit into mappable, in which case
3738 * put it anyway and hope that userspace can cope (but always first
3739 * try to preserve the existing ABI).
Chris Wilson2da3b9b2011-04-14 09:41:17 +01003740 */
Chris Wilson2efb8132016-08-18 17:17:06 +01003741 vma = ERR_PTR(-ENOSPC);
3742 if (view->type == I915_GGTT_VIEW_NORMAL)
3743 vma = i915_gem_object_ggtt_pin(obj, view, 0, alignment,
3744 PIN_MAPPABLE | PIN_NONBLOCK);
3745 if (IS_ERR(vma))
3746 vma = i915_gem_object_ggtt_pin(obj, view, 0, alignment, 0);
Chris Wilson058d88c2016-08-15 10:49:06 +01003747 if (IS_ERR(vma))
Chris Wilsoncc98b412013-08-09 12:25:09 +01003748 goto err_unpin_display;
Chris Wilson2da3b9b2011-04-14 09:41:17 +01003749
Chris Wilsond8923dc2016-08-18 17:17:07 +01003750 vma->display_alignment = max_t(u64, vma->display_alignment, alignment);
3751
Daniel Vettere62b59e2015-01-21 14:53:48 +01003752 i915_gem_object_flush_cpu_write_domain(obj);
Chris Wilsonb118c1e2010-05-27 13:18:14 +01003753
Chris Wilson2da3b9b2011-04-14 09:41:17 +01003754 old_write_domain = obj->base.write_domain;
Chris Wilson05394f32010-11-08 19:18:58 +00003755 old_read_domains = obj->base.read_domains;
Chris Wilson2da3b9b2011-04-14 09:41:17 +01003756
3757 /* It should now be out of any other write domains, and we can update
3758 * the domain values for our changes.
3759 */
Chris Wilsone5f1d962012-07-20 12:41:00 +01003760 obj->base.write_domain = 0;
Chris Wilson05394f32010-11-08 19:18:58 +00003761 obj->base.read_domains |= I915_GEM_DOMAIN_GTT;
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003762
3763 trace_i915_gem_object_change_domain(obj,
3764 old_read_domains,
Chris Wilson2da3b9b2011-04-14 09:41:17 +01003765 old_write_domain);
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003766
Chris Wilson058d88c2016-08-15 10:49:06 +01003767 return vma;
Chris Wilsoncc98b412013-08-09 12:25:09 +01003768
3769err_unpin_display:
Tvrtko Ursulin8a0c39b2015-04-13 11:50:09 +01003770 obj->pin_display--;
Chris Wilson058d88c2016-08-15 10:49:06 +01003771 return vma;
Chris Wilsoncc98b412013-08-09 12:25:09 +01003772}
3773
3774void
Chris Wilson058d88c2016-08-15 10:49:06 +01003775i915_gem_object_unpin_from_display_plane(struct i915_vma *vma)
Chris Wilsoncc98b412013-08-09 12:25:09 +01003776{
Chris Wilson058d88c2016-08-15 10:49:06 +01003777 if (WARN_ON(vma->obj->pin_display == 0))
Tvrtko Ursulin8a0c39b2015-04-13 11:50:09 +01003778 return;
3779
Chris Wilsond8923dc2016-08-18 17:17:07 +01003780 if (--vma->obj->pin_display == 0)
3781 vma->display_alignment = 0;
Tvrtko Ursuline6617332015-03-23 11:10:33 +00003782
Chris Wilson383d5822016-08-18 17:17:08 +01003783 /* Bump the LRU to try and avoid premature eviction whilst flipping */
3784 if (!i915_vma_is_active(vma))
3785 list_move_tail(&vma->vm_link, &vma->vm->inactive_list);
3786
Chris Wilson058d88c2016-08-15 10:49:06 +01003787 i915_vma_unpin(vma);
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003788}
3789
Eric Anholte47c68e2008-11-14 13:35:19 -08003790/**
3791 * Moves a single object to the CPU read, and possibly write domain.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01003792 * @obj: object to act on
3793 * @write: requesting write or read-only access
Eric Anholte47c68e2008-11-14 13:35:19 -08003794 *
3795 * This function returns when the move is complete, including waiting on
3796 * flushes to occur.
3797 */
Chris Wilsondabdfe02012-03-26 10:10:27 +02003798int
Chris Wilson919926a2010-11-12 13:42:53 +00003799i915_gem_object_set_to_cpu_domain(struct drm_i915_gem_object *obj, bool write)
Eric Anholte47c68e2008-11-14 13:35:19 -08003800{
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003801 uint32_t old_write_domain, old_read_domains;
Eric Anholte47c68e2008-11-14 13:35:19 -08003802 int ret;
3803
Chris Wilsone95433c2016-10-28 13:58:27 +01003804 lockdep_assert_held(&obj->base.dev->struct_mutex);
3805 ret = i915_gem_object_wait(obj,
3806 I915_WAIT_INTERRUPTIBLE |
3807 I915_WAIT_LOCKED |
3808 (write ? I915_WAIT_ALL : 0),
3809 MAX_SCHEDULE_TIMEOUT,
3810 NULL);
Chris Wilson88241782011-01-07 17:09:48 +00003811 if (ret)
3812 return ret;
3813
Chris Wilsonc13d87e2016-07-20 09:21:15 +01003814 if (obj->base.write_domain == I915_GEM_DOMAIN_CPU)
3815 return 0;
3816
Eric Anholte47c68e2008-11-14 13:35:19 -08003817 i915_gem_object_flush_gtt_write_domain(obj);
3818
Chris Wilson05394f32010-11-08 19:18:58 +00003819 old_write_domain = obj->base.write_domain;
3820 old_read_domains = obj->base.read_domains;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003821
Eric Anholte47c68e2008-11-14 13:35:19 -08003822 /* Flush the CPU cache if it's still invalid. */
Chris Wilson05394f32010-11-08 19:18:58 +00003823 if ((obj->base.read_domains & I915_GEM_DOMAIN_CPU) == 0) {
Chris Wilson2c225692013-08-09 12:26:45 +01003824 i915_gem_clflush_object(obj, false);
Eric Anholte47c68e2008-11-14 13:35:19 -08003825
Chris Wilson05394f32010-11-08 19:18:58 +00003826 obj->base.read_domains |= I915_GEM_DOMAIN_CPU;
Eric Anholte47c68e2008-11-14 13:35:19 -08003827 }
3828
3829 /* It should now be out of any other write domains, and we can update
3830 * the domain values for our changes.
3831 */
Chris Wilson05394f32010-11-08 19:18:58 +00003832 BUG_ON((obj->base.write_domain & ~I915_GEM_DOMAIN_CPU) != 0);
Eric Anholte47c68e2008-11-14 13:35:19 -08003833
3834 /* If we're writing through the CPU, then the GPU read domains will
3835 * need to be invalidated at next use.
3836 */
3837 if (write) {
Chris Wilson05394f32010-11-08 19:18:58 +00003838 obj->base.read_domains = I915_GEM_DOMAIN_CPU;
3839 obj->base.write_domain = I915_GEM_DOMAIN_CPU;
Eric Anholte47c68e2008-11-14 13:35:19 -08003840 }
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003841
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003842 trace_i915_gem_object_change_domain(obj,
3843 old_read_domains,
3844 old_write_domain);
3845
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003846 return 0;
3847}
3848
Eric Anholt673a3942008-07-30 12:06:12 -07003849/* Throttle our rendering by waiting until the ring has completed our requests
3850 * emitted over 20 msec ago.
3851 *
Eric Anholtb9624422009-06-03 07:27:35 +00003852 * Note that if we were to use the current jiffies each time around the loop,
3853 * we wouldn't escape the function with any frames outstanding if the time to
3854 * render a frame was over 20ms.
3855 *
Eric Anholt673a3942008-07-30 12:06:12 -07003856 * This should get us reasonable parallelism between CPU and GPU but also
3857 * relatively low latency when blocking on a particular request to finish.
3858 */
3859static int
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003860i915_gem_ring_throttle(struct drm_device *dev, struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07003861{
Chris Wilsonfac5e232016-07-04 11:34:36 +01003862 struct drm_i915_private *dev_priv = to_i915(dev);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003863 struct drm_i915_file_private *file_priv = file->driver_priv;
Chris Wilsond0bc54f2015-05-21 21:01:48 +01003864 unsigned long recent_enough = jiffies - DRM_I915_THROTTLE_JIFFIES;
John Harrison54fb2412014-11-24 18:49:27 +00003865 struct drm_i915_gem_request *request, *target = NULL;
Chris Wilsone95433c2016-10-28 13:58:27 +01003866 long ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003867
Chris Wilsonf4457ae2016-04-13 17:35:08 +01003868 /* ABI: return -EIO if already wedged */
3869 if (i915_terminally_wedged(&dev_priv->gpu_error))
3870 return -EIO;
Chris Wilsone110e8d2011-01-26 15:39:14 +00003871
Chris Wilson1c255952010-09-26 11:03:27 +01003872 spin_lock(&file_priv->mm.lock);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003873 list_for_each_entry(request, &file_priv->mm.request_list, client_list) {
Eric Anholtb9624422009-06-03 07:27:35 +00003874 if (time_after_eq(request->emitted_jiffies, recent_enough))
3875 break;
3876
John Harrisonfcfa423c2015-05-29 17:44:12 +01003877 /*
3878 * Note that the request might not have been submitted yet.
3879 * In which case emitted_jiffies will be zero.
3880 */
3881 if (!request->emitted_jiffies)
3882 continue;
3883
John Harrison54fb2412014-11-24 18:49:27 +00003884 target = request;
Eric Anholtb9624422009-06-03 07:27:35 +00003885 }
John Harrisonff865882014-11-24 18:49:28 +00003886 if (target)
Chris Wilsone8a261e2016-07-20 13:31:49 +01003887 i915_gem_request_get(target);
Chris Wilson1c255952010-09-26 11:03:27 +01003888 spin_unlock(&file_priv->mm.lock);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003889
John Harrison54fb2412014-11-24 18:49:27 +00003890 if (target == NULL)
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003891 return 0;
3892
Chris Wilsone95433c2016-10-28 13:58:27 +01003893 ret = i915_wait_request(target,
3894 I915_WAIT_INTERRUPTIBLE,
3895 MAX_SCHEDULE_TIMEOUT);
Chris Wilsone8a261e2016-07-20 13:31:49 +01003896 i915_gem_request_put(target);
John Harrisonff865882014-11-24 18:49:28 +00003897
Chris Wilsone95433c2016-10-28 13:58:27 +01003898 return ret < 0 ? ret : 0;
Eric Anholt673a3942008-07-30 12:06:12 -07003899}
3900
Chris Wilsond23db882014-05-23 08:48:08 +02003901static bool
Chris Wilson91b2db62016-08-04 16:32:23 +01003902i915_vma_misplaced(struct i915_vma *vma, u64 size, u64 alignment, u64 flags)
Chris Wilsond23db882014-05-23 08:48:08 +02003903{
Chris Wilson59bfa122016-08-04 16:32:31 +01003904 if (!drm_mm_node_allocated(&vma->node))
3905 return false;
3906
Chris Wilson91b2db62016-08-04 16:32:23 +01003907 if (vma->node.size < size)
3908 return true;
3909
3910 if (alignment && vma->node.start & (alignment - 1))
Chris Wilsond23db882014-05-23 08:48:08 +02003911 return true;
3912
Chris Wilson05a20d02016-08-18 17:16:55 +01003913 if (flags & PIN_MAPPABLE && !i915_vma_is_map_and_fenceable(vma))
Chris Wilsond23db882014-05-23 08:48:08 +02003914 return true;
3915
3916 if (flags & PIN_OFFSET_BIAS &&
3917 vma->node.start < (flags & PIN_OFFSET_MASK))
3918 return true;
3919
Chris Wilson506a8e82015-12-08 11:55:07 +00003920 if (flags & PIN_OFFSET_FIXED &&
3921 vma->node.start != (flags & PIN_OFFSET_MASK))
3922 return true;
3923
Chris Wilsond23db882014-05-23 08:48:08 +02003924 return false;
3925}
3926
Chris Wilsond0710ab2015-11-20 14:16:39 +00003927void __i915_vma_set_map_and_fenceable(struct i915_vma *vma)
3928{
3929 struct drm_i915_gem_object *obj = vma->obj;
Chris Wilsona9f14812016-08-04 16:32:28 +01003930 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
Chris Wilsond0710ab2015-11-20 14:16:39 +00003931 bool mappable, fenceable;
3932 u32 fence_size, fence_alignment;
3933
Chris Wilsona9f14812016-08-04 16:32:28 +01003934 fence_size = i915_gem_get_ggtt_size(dev_priv,
Chris Wilson05a20d02016-08-18 17:16:55 +01003935 vma->size,
Chris Wilson3e510a82016-08-05 10:14:23 +01003936 i915_gem_object_get_tiling(obj));
Chris Wilsona9f14812016-08-04 16:32:28 +01003937 fence_alignment = i915_gem_get_ggtt_alignment(dev_priv,
Chris Wilson05a20d02016-08-18 17:16:55 +01003938 vma->size,
Chris Wilson3e510a82016-08-05 10:14:23 +01003939 i915_gem_object_get_tiling(obj),
Chris Wilsonad1a7d22016-08-04 16:32:27 +01003940 true);
Chris Wilsond0710ab2015-11-20 14:16:39 +00003941
3942 fenceable = (vma->node.size == fence_size &&
3943 (vma->node.start & (fence_alignment - 1)) == 0);
3944
3945 mappable = (vma->node.start + fence_size <=
Chris Wilsona9f14812016-08-04 16:32:28 +01003946 dev_priv->ggtt.mappable_end);
Chris Wilsond0710ab2015-11-20 14:16:39 +00003947
Tvrtko Ursulin07ee2bc2016-10-25 17:40:35 +01003948 /*
3949 * Explicitly disable for rotated VMA since the display does not
3950 * need the fence and the VMA is not accessible to other users.
3951 */
3952 if (mappable && fenceable &&
3953 vma->ggtt_view.type != I915_GGTT_VIEW_ROTATED)
Chris Wilson05a20d02016-08-18 17:16:55 +01003954 vma->flags |= I915_VMA_CAN_FENCE;
3955 else
3956 vma->flags &= ~I915_VMA_CAN_FENCE;
Chris Wilsond0710ab2015-11-20 14:16:39 +00003957}
3958
Chris Wilson305bc232016-08-04 16:32:33 +01003959int __i915_vma_do_pin(struct i915_vma *vma,
3960 u64 size, u64 alignment, u64 flags)
Eric Anholt673a3942008-07-30 12:06:12 -07003961{
Chris Wilson305bc232016-08-04 16:32:33 +01003962 unsigned int bound = vma->flags;
Eric Anholt673a3942008-07-30 12:06:12 -07003963 int ret;
3964
Chris Wilson59bfa122016-08-04 16:32:31 +01003965 GEM_BUG_ON((flags & (PIN_GLOBAL | PIN_USER)) == 0);
Chris Wilson3272db52016-08-04 16:32:32 +01003966 GEM_BUG_ON((flags & PIN_GLOBAL) && !i915_vma_is_ggtt(vma));
Ben Widawsky6e7186a2014-05-06 22:21:36 -07003967
Chris Wilson305bc232016-08-04 16:32:33 +01003968 if (WARN_ON(bound & I915_VMA_PIN_OVERFLOW)) {
3969 ret = -EBUSY;
3970 goto err;
3971 }
Chris Wilsonc826c442014-10-31 13:53:53 +00003972
Chris Wilsonde895082016-08-04 16:32:34 +01003973 if ((bound & I915_VMA_BIND_MASK) == 0) {
Chris Wilson59bfa122016-08-04 16:32:31 +01003974 ret = i915_vma_insert(vma, size, alignment, flags);
3975 if (ret)
3976 goto err;
Chris Wilsonac0c6b52010-05-27 13:18:18 +01003977 }
3978
Chris Wilson59bfa122016-08-04 16:32:31 +01003979 ret = i915_vma_bind(vma, vma->obj->cache_level, flags);
Chris Wilson3b165252016-08-04 16:32:25 +01003980 if (ret)
Chris Wilson59bfa122016-08-04 16:32:31 +01003981 goto err;
Chris Wilson3b165252016-08-04 16:32:25 +01003982
Chris Wilson3272db52016-08-04 16:32:32 +01003983 if ((bound ^ vma->flags) & I915_VMA_GLOBAL_BIND)
Chris Wilsond0710ab2015-11-20 14:16:39 +00003984 __i915_vma_set_map_and_fenceable(vma);
Chris Wilsonef79e172014-10-31 13:53:52 +00003985
Chris Wilson3b165252016-08-04 16:32:25 +01003986 GEM_BUG_ON(i915_vma_misplaced(vma, size, alignment, flags));
Eric Anholt673a3942008-07-30 12:06:12 -07003987 return 0;
Eric Anholt673a3942008-07-30 12:06:12 -07003988
Chris Wilson59bfa122016-08-04 16:32:31 +01003989err:
3990 __i915_vma_unpin(vma);
3991 return ret;
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02003992}
3993
Chris Wilson058d88c2016-08-15 10:49:06 +01003994struct i915_vma *
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02003995i915_gem_object_ggtt_pin(struct drm_i915_gem_object *obj,
3996 const struct i915_ggtt_view *view,
Chris Wilson91b2db62016-08-04 16:32:23 +01003997 u64 size,
Chris Wilson2ffffd02016-08-04 16:32:22 +01003998 u64 alignment,
3999 u64 flags)
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02004000{
Chris Wilsonad16d2e2016-10-13 09:55:04 +01004001 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
4002 struct i915_address_space *vm = &dev_priv->ggtt.base;
Chris Wilson59bfa122016-08-04 16:32:31 +01004003 struct i915_vma *vma;
4004 int ret;
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03004005
Chris Wilson058d88c2016-08-15 10:49:06 +01004006 vma = i915_gem_obj_lookup_or_create_vma(obj, vm, view);
Chris Wilson59bfa122016-08-04 16:32:31 +01004007 if (IS_ERR(vma))
Chris Wilson058d88c2016-08-15 10:49:06 +01004008 return vma;
Chris Wilson59bfa122016-08-04 16:32:31 +01004009
4010 if (i915_vma_misplaced(vma, size, alignment, flags)) {
4011 if (flags & PIN_NONBLOCK &&
4012 (i915_vma_is_pinned(vma) || i915_vma_is_active(vma)))
Chris Wilson058d88c2016-08-15 10:49:06 +01004013 return ERR_PTR(-ENOSPC);
Chris Wilson59bfa122016-08-04 16:32:31 +01004014
Chris Wilsonad16d2e2016-10-13 09:55:04 +01004015 if (flags & PIN_MAPPABLE) {
4016 u32 fence_size;
4017
4018 fence_size = i915_gem_get_ggtt_size(dev_priv, vma->size,
4019 i915_gem_object_get_tiling(obj));
4020 /* If the required space is larger than the available
4021 * aperture, we will not able to find a slot for the
4022 * object and unbinding the object now will be in
4023 * vain. Worse, doing so may cause us to ping-pong
4024 * the object in and out of the Global GTT and
4025 * waste a lot of cycles under the mutex.
4026 */
4027 if (fence_size > dev_priv->ggtt.mappable_end)
4028 return ERR_PTR(-E2BIG);
4029
4030 /* If NONBLOCK is set the caller is optimistically
4031 * trying to cache the full object within the mappable
4032 * aperture, and *must* have a fallback in place for
4033 * situations where we cannot bind the object. We
4034 * can be a little more lax here and use the fallback
4035 * more often to avoid costly migrations of ourselves
4036 * and other objects within the aperture.
4037 *
4038 * Half-the-aperture is used as a simple heuristic.
4039 * More interesting would to do search for a free
4040 * block prior to making the commitment to unbind.
4041 * That caters for the self-harm case, and with a
4042 * little more heuristics (e.g. NOFAULT, NOEVICT)
4043 * we could try to minimise harm to others.
4044 */
4045 if (flags & PIN_NONBLOCK &&
4046 fence_size > dev_priv->ggtt.mappable_end / 2)
4047 return ERR_PTR(-ENOSPC);
4048 }
4049
Chris Wilson59bfa122016-08-04 16:32:31 +01004050 WARN(i915_vma_is_pinned(vma),
4051 "bo is already pinned in ggtt with incorrect alignment:"
Chris Wilson05a20d02016-08-18 17:16:55 +01004052 " offset=%08x, req.alignment=%llx,"
4053 " req.map_and_fenceable=%d, vma->map_and_fenceable=%d\n",
4054 i915_ggtt_offset(vma), alignment,
Chris Wilson59bfa122016-08-04 16:32:31 +01004055 !!(flags & PIN_MAPPABLE),
Chris Wilson05a20d02016-08-18 17:16:55 +01004056 i915_vma_is_map_and_fenceable(vma));
Chris Wilson59bfa122016-08-04 16:32:31 +01004057 ret = i915_vma_unbind(vma);
4058 if (ret)
Chris Wilson058d88c2016-08-15 10:49:06 +01004059 return ERR_PTR(ret);
Chris Wilson59bfa122016-08-04 16:32:31 +01004060 }
4061
Chris Wilson058d88c2016-08-15 10:49:06 +01004062 ret = i915_vma_pin(vma, size, alignment, flags | PIN_GLOBAL);
4063 if (ret)
4064 return ERR_PTR(ret);
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02004065
Chris Wilson058d88c2016-08-15 10:49:06 +01004066 return vma;
Eric Anholt673a3942008-07-30 12:06:12 -07004067}
4068
Chris Wilsonedf6b762016-08-09 09:23:33 +01004069static __always_inline unsigned int __busy_read_flag(unsigned int id)
Chris Wilson3fdc13c2016-08-05 10:14:18 +01004070{
4071 /* Note that we could alias engines in the execbuf API, but
4072 * that would be very unwise as it prevents userspace from
4073 * fine control over engine selection. Ahem.
4074 *
4075 * This should be something like EXEC_MAX_ENGINE instead of
4076 * I915_NUM_ENGINES.
4077 */
4078 BUILD_BUG_ON(I915_NUM_ENGINES > 16);
4079 return 0x10000 << id;
4080}
4081
4082static __always_inline unsigned int __busy_write_id(unsigned int id)
4083{
Chris Wilson70cb4722016-08-09 18:08:25 +01004084 /* The uABI guarantees an active writer is also amongst the read
4085 * engines. This would be true if we accessed the activity tracking
4086 * under the lock, but as we perform the lookup of the object and
4087 * its activity locklessly we can not guarantee that the last_write
4088 * being active implies that we have set the same engine flag from
4089 * last_read - hence we always set both read and write busy for
4090 * last_write.
4091 */
4092 return id | __busy_read_flag(id);
Chris Wilson3fdc13c2016-08-05 10:14:18 +01004093}
4094
Chris Wilsonedf6b762016-08-09 09:23:33 +01004095static __always_inline unsigned int
Chris Wilson3fdc13c2016-08-05 10:14:18 +01004096__busy_set_if_active(const struct i915_gem_active *active,
4097 unsigned int (*flag)(unsigned int id))
4098{
Chris Wilson12555012016-08-16 09:50:40 +01004099 struct drm_i915_gem_request *request;
4100
4101 request = rcu_dereference(active->request);
4102 if (!request || i915_gem_request_completed(request))
4103 return 0;
4104
4105 /* This is racy. See __i915_gem_active_get_rcu() for an in detail
4106 * discussion of how to handle the race correctly, but for reporting
4107 * the busy state we err on the side of potentially reporting the
4108 * wrong engine as being busy (but we guarantee that the result
4109 * is at least self-consistent).
4110 *
4111 * As we use SLAB_DESTROY_BY_RCU, the request may be reallocated
4112 * whilst we are inspecting it, even under the RCU read lock as we are.
4113 * This means that there is a small window for the engine and/or the
4114 * seqno to have been overwritten. The seqno will always be in the
4115 * future compared to the intended, and so we know that if that
4116 * seqno is idle (on whatever engine) our request is idle and the
4117 * return 0 above is correct.
4118 *
4119 * The issue is that if the engine is switched, it is just as likely
4120 * to report that it is busy (but since the switch happened, we know
4121 * the request should be idle). So there is a small chance that a busy
4122 * result is actually the wrong engine.
4123 *
4124 * So why don't we care?
4125 *
4126 * For starters, the busy ioctl is a heuristic that is by definition
4127 * racy. Even with perfect serialisation in the driver, the hardware
4128 * state is constantly advancing - the state we report to the user
4129 * is stale.
4130 *
4131 * The critical information for the busy-ioctl is whether the object
4132 * is idle as userspace relies on that to detect whether its next
4133 * access will stall, or if it has missed submitting commands to
4134 * the hardware allowing the GPU to stall. We never generate a
4135 * false-positive for idleness, thus busy-ioctl is reliable at the
4136 * most fundamental level, and we maintain the guarantee that a
4137 * busy object left to itself will eventually become idle (and stay
4138 * idle!).
4139 *
4140 * We allow ourselves the leeway of potentially misreporting the busy
4141 * state because that is an optimisation heuristic that is constantly
4142 * in flux. Being quickly able to detect the busy/idle state is much
4143 * more important than accurate logging of exactly which engines were
4144 * busy.
4145 *
4146 * For accuracy in reporting the engine, we could use
4147 *
4148 * result = 0;
4149 * request = __i915_gem_active_get_rcu(active);
4150 * if (request) {
4151 * if (!i915_gem_request_completed(request))
4152 * result = flag(request->engine->exec_id);
4153 * i915_gem_request_put(request);
4154 * }
4155 *
4156 * but that still remains susceptible to both hardware and userspace
4157 * races. So we accept making the result of that race slightly worse,
4158 * given the rarity of the race and its low impact on the result.
Chris Wilson3fdc13c2016-08-05 10:14:18 +01004159 */
Chris Wilson12555012016-08-16 09:50:40 +01004160 return flag(READ_ONCE(request->engine->exec_id));
Chris Wilson3fdc13c2016-08-05 10:14:18 +01004161}
4162
Chris Wilsonedf6b762016-08-09 09:23:33 +01004163static __always_inline unsigned int
Chris Wilson3fdc13c2016-08-05 10:14:18 +01004164busy_check_reader(const struct i915_gem_active *active)
4165{
4166 return __busy_set_if_active(active, __busy_read_flag);
4167}
4168
Chris Wilsonedf6b762016-08-09 09:23:33 +01004169static __always_inline unsigned int
Chris Wilson3fdc13c2016-08-05 10:14:18 +01004170busy_check_writer(const struct i915_gem_active *active)
4171{
4172 return __busy_set_if_active(active, __busy_write_id);
4173}
4174
Eric Anholt673a3942008-07-30 12:06:12 -07004175int
Eric Anholt673a3942008-07-30 12:06:12 -07004176i915_gem_busy_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00004177 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07004178{
4179 struct drm_i915_gem_busy *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00004180 struct drm_i915_gem_object *obj;
Chris Wilson3fdc13c2016-08-05 10:14:18 +01004181 unsigned long active;
Eric Anholt673a3942008-07-30 12:06:12 -07004182
Chris Wilson03ac0642016-07-20 13:31:51 +01004183 obj = i915_gem_object_lookup(file, args->handle);
Chris Wilson3fdc13c2016-08-05 10:14:18 +01004184 if (!obj)
4185 return -ENOENT;
Zou Nan haid1b851f2010-05-21 09:08:57 +08004186
Chris Wilson426960b2016-01-15 16:51:46 +00004187 args->busy = 0;
Chris Wilson3fdc13c2016-08-05 10:14:18 +01004188 active = __I915_BO_ACTIVE(obj);
4189 if (active) {
4190 int idx;
Chris Wilson426960b2016-01-15 16:51:46 +00004191
Chris Wilson3fdc13c2016-08-05 10:14:18 +01004192 /* Yes, the lookups are intentionally racy.
4193 *
4194 * First, we cannot simply rely on __I915_BO_ACTIVE. We have
4195 * to regard the value as stale and as our ABI guarantees
4196 * forward progress, we confirm the status of each active
4197 * request with the hardware.
4198 *
4199 * Even though we guard the pointer lookup by RCU, that only
4200 * guarantees that the pointer and its contents remain
4201 * dereferencable and does *not* mean that the request we
4202 * have is the same as the one being tracked by the object.
4203 *
4204 * Consider that we lookup the request just as it is being
4205 * retired and freed. We take a local copy of the pointer,
4206 * but before we add its engine into the busy set, the other
4207 * thread reallocates it and assigns it to a task on another
Chris Wilson12555012016-08-16 09:50:40 +01004208 * engine with a fresh and incomplete seqno. Guarding against
4209 * that requires careful serialisation and reference counting,
4210 * i.e. using __i915_gem_active_get_request_rcu(). We don't,
4211 * instead we expect that if the result is busy, which engines
4212 * are busy is not completely reliable - we only guarantee
4213 * that the object was busy.
Chris Wilson3fdc13c2016-08-05 10:14:18 +01004214 */
4215 rcu_read_lock();
4216
4217 for_each_active(active, idx)
4218 args->busy |= busy_check_reader(&obj->last_read[idx]);
4219
4220 /* For ABI sanity, we only care that the write engine is in
Chris Wilson70cb4722016-08-09 18:08:25 +01004221 * the set of read engines. This should be ensured by the
4222 * ordering of setting last_read/last_write in
4223 * i915_vma_move_to_active(), and then in reverse in retire.
4224 * However, for good measure, we always report the last_write
4225 * request as a busy read as well as being a busy write.
Chris Wilson3fdc13c2016-08-05 10:14:18 +01004226 *
4227 * We don't care that the set of active read/write engines
4228 * may change during construction of the result, as it is
4229 * equally liable to change before userspace can inspect
4230 * the result.
4231 */
4232 args->busy |= busy_check_writer(&obj->last_write);
4233
4234 rcu_read_unlock();
Chris Wilson426960b2016-01-15 16:51:46 +00004235 }
Eric Anholt673a3942008-07-30 12:06:12 -07004236
Chris Wilson3fdc13c2016-08-05 10:14:18 +01004237 i915_gem_object_put_unlocked(obj);
4238 return 0;
Eric Anholt673a3942008-07-30 12:06:12 -07004239}
4240
4241int
4242i915_gem_throttle_ioctl(struct drm_device *dev, void *data,
4243 struct drm_file *file_priv)
4244{
Akshay Joshi0206e352011-08-16 15:34:10 -04004245 return i915_gem_ring_throttle(dev, file_priv);
Eric Anholt673a3942008-07-30 12:06:12 -07004246}
4247
Chris Wilson3ef94da2009-09-14 16:50:29 +01004248int
4249i915_gem_madvise_ioctl(struct drm_device *dev, void *data,
4250 struct drm_file *file_priv)
4251{
Chris Wilsonfac5e232016-07-04 11:34:36 +01004252 struct drm_i915_private *dev_priv = to_i915(dev);
Chris Wilson3ef94da2009-09-14 16:50:29 +01004253 struct drm_i915_gem_madvise *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00004254 struct drm_i915_gem_object *obj;
Chris Wilson76c1dec2010-09-25 11:22:51 +01004255 int ret;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004256
4257 switch (args->madv) {
4258 case I915_MADV_DONTNEED:
4259 case I915_MADV_WILLNEED:
4260 break;
4261 default:
4262 return -EINVAL;
4263 }
4264
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004265 ret = i915_mutex_lock_interruptible(dev);
4266 if (ret)
4267 return ret;
4268
Chris Wilson03ac0642016-07-20 13:31:51 +01004269 obj = i915_gem_object_lookup(file_priv, args->handle);
4270 if (!obj) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004271 ret = -ENOENT;
4272 goto unlock;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004273 }
Chris Wilson3ef94da2009-09-14 16:50:29 +01004274
Daniel Vetter656bfa32014-11-20 09:26:30 +01004275 if (obj->pages &&
Chris Wilson3e510a82016-08-05 10:14:23 +01004276 i915_gem_object_is_tiled(obj) &&
Daniel Vetter656bfa32014-11-20 09:26:30 +01004277 dev_priv->quirks & QUIRK_PIN_SWIZZLED_PAGES) {
4278 if (obj->madv == I915_MADV_WILLNEED)
4279 i915_gem_object_unpin_pages(obj);
4280 if (args->madv == I915_MADV_WILLNEED)
4281 i915_gem_object_pin_pages(obj);
4282 }
4283
Chris Wilson05394f32010-11-08 19:18:58 +00004284 if (obj->madv != __I915_MADV_PURGED)
4285 obj->madv = args->madv;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004286
Chris Wilson6c085a72012-08-20 11:40:46 +02004287 /* if the object is no longer attached, discard its backing storage */
Daniel Vetterbe6a0372015-03-18 10:46:04 +01004288 if (obj->madv == I915_MADV_DONTNEED && obj->pages == NULL)
Chris Wilson2d7ef392009-09-20 23:13:10 +01004289 i915_gem_object_truncate(obj);
4290
Chris Wilson05394f32010-11-08 19:18:58 +00004291 args->retained = obj->madv != __I915_MADV_PURGED;
Chris Wilsonbb6baf72009-09-22 14:24:13 +01004292
Chris Wilsonf8c417c2016-07-20 13:31:53 +01004293 i915_gem_object_put(obj);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004294unlock:
Chris Wilson3ef94da2009-09-14 16:50:29 +01004295 mutex_unlock(&dev->struct_mutex);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004296 return ret;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004297}
4298
Chris Wilson37e680a2012-06-07 15:38:42 +01004299void i915_gem_object_init(struct drm_i915_gem_object *obj,
4300 const struct drm_i915_gem_object_ops *ops)
Chris Wilson0327d6b2012-08-11 15:41:06 +01004301{
Chris Wilsonb4716182015-04-27 13:41:17 +01004302 int i;
4303
Ben Widawsky35c20a62013-05-31 11:28:48 -07004304 INIT_LIST_HEAD(&obj->global_list);
Chris Wilson275f0392016-10-24 13:42:14 +01004305 INIT_LIST_HEAD(&obj->userfault_link);
Tvrtko Ursulin666796d2016-03-16 11:00:39 +00004306 for (i = 0; i < I915_NUM_ENGINES; i++)
Chris Wilsonfa545cb2016-08-04 07:52:35 +01004307 init_request_active(&obj->last_read[i],
4308 i915_gem_object_retire__read);
4309 init_request_active(&obj->last_write,
4310 i915_gem_object_retire__write);
Ben Widawskyb25cb2f2013-08-14 11:38:33 +02004311 INIT_LIST_HEAD(&obj->obj_exec_link);
Ben Widawsky2f633152013-07-17 12:19:03 -07004312 INIT_LIST_HEAD(&obj->vma_list);
Chris Wilson8d9d5742015-04-07 16:20:38 +01004313 INIT_LIST_HEAD(&obj->batch_pool_link);
Chris Wilson0327d6b2012-08-11 15:41:06 +01004314
Chris Wilson37e680a2012-06-07 15:38:42 +01004315 obj->ops = ops;
4316
Chris Wilson50349242016-08-18 17:17:04 +01004317 obj->frontbuffer_ggtt_origin = ORIGIN_GTT;
Chris Wilson0327d6b2012-08-11 15:41:06 +01004318 obj->madv = I915_MADV_WILLNEED;
Chris Wilson0327d6b2012-08-11 15:41:06 +01004319
Dave Gordonf19ec8c2016-07-04 11:34:37 +01004320 i915_gem_info_add_obj(to_i915(obj->base.dev), obj->base.size);
Chris Wilson0327d6b2012-08-11 15:41:06 +01004321}
4322
Chris Wilson37e680a2012-06-07 15:38:42 +01004323static const struct drm_i915_gem_object_ops i915_gem_object_ops = {
Chris Wilsonde472662016-01-22 18:32:31 +00004324 .flags = I915_GEM_OBJECT_HAS_STRUCT_PAGE,
Chris Wilson37e680a2012-06-07 15:38:42 +01004325 .get_pages = i915_gem_object_get_pages_gtt,
4326 .put_pages = i915_gem_object_put_pages_gtt,
4327};
4328
Chris Wilsonb4bcbe22016-10-18 13:02:49 +01004329/* Note we don't consider signbits :| */
4330#define overflows_type(x, T) \
4331 (sizeof(x) > sizeof(T) && (x) >> (sizeof(T) * BITS_PER_BYTE))
4332
4333struct drm_i915_gem_object *
4334i915_gem_object_create(struct drm_device *dev, u64 size)
Daniel Vetterac52bc52010-04-09 19:05:06 +00004335{
Daniel Vetterc397b902010-04-09 19:05:07 +00004336 struct drm_i915_gem_object *obj;
Hugh Dickins5949eac2011-06-27 16:18:18 -07004337 struct address_space *mapping;
Daniel Vetter1a240d42012-11-29 22:18:51 +01004338 gfp_t mask;
Chris Wilsonfe3db792016-04-25 13:32:13 +01004339 int ret;
Daniel Vetterc397b902010-04-09 19:05:07 +00004340
Chris Wilsonb4bcbe22016-10-18 13:02:49 +01004341 /* There is a prevalence of the assumption that we fit the object's
4342 * page count inside a 32bit _signed_ variable. Let's document this and
4343 * catch if we ever need to fix it. In the meantime, if you do spot
4344 * such a local variable, please consider fixing!
4345 */
4346 if (WARN_ON(size >> PAGE_SHIFT > INT_MAX))
4347 return ERR_PTR(-E2BIG);
4348
4349 if (overflows_type(size, obj->base.size))
4350 return ERR_PTR(-E2BIG);
4351
Chris Wilson42dcedd2012-11-15 11:32:30 +00004352 obj = i915_gem_object_alloc(dev);
Daniel Vetterc397b902010-04-09 19:05:07 +00004353 if (obj == NULL)
Chris Wilsonfe3db792016-04-25 13:32:13 +01004354 return ERR_PTR(-ENOMEM);
Daniel Vetterc397b902010-04-09 19:05:07 +00004355
Chris Wilsonfe3db792016-04-25 13:32:13 +01004356 ret = drm_gem_object_init(dev, &obj->base, size);
4357 if (ret)
4358 goto fail;
Daniel Vetterc397b902010-04-09 19:05:07 +00004359
Chris Wilsonbed1ea92012-05-24 20:48:12 +01004360 mask = GFP_HIGHUSER | __GFP_RECLAIMABLE;
4361 if (IS_CRESTLINE(dev) || IS_BROADWATER(dev)) {
4362 /* 965gm cannot relocate objects above 4GiB. */
4363 mask &= ~__GFP_HIGHMEM;
4364 mask |= __GFP_DMA32;
4365 }
4366
Al Viro93c76a32015-12-04 23:45:44 -05004367 mapping = obj->base.filp->f_mapping;
Chris Wilsonbed1ea92012-05-24 20:48:12 +01004368 mapping_set_gfp_mask(mapping, mask);
Hugh Dickins5949eac2011-06-27 16:18:18 -07004369
Chris Wilson37e680a2012-06-07 15:38:42 +01004370 i915_gem_object_init(obj, &i915_gem_object_ops);
Chris Wilson73aa8082010-09-30 11:46:12 +01004371
Daniel Vetterc397b902010-04-09 19:05:07 +00004372 obj->base.write_domain = I915_GEM_DOMAIN_CPU;
4373 obj->base.read_domains = I915_GEM_DOMAIN_CPU;
4374
Eugeni Dodonov3d29b842012-01-17 14:43:53 -02004375 if (HAS_LLC(dev)) {
4376 /* On some devices, we can have the GPU use the LLC (the CPU
Eric Anholta1871112011-03-29 16:59:55 -07004377 * cache) for about a 10% performance improvement
4378 * compared to uncached. Graphics requests other than
4379 * display scanout are coherent with the CPU in
4380 * accessing this cache. This means in this mode we
4381 * don't need to clflush on the CPU side, and on the
4382 * GPU side we only need to flush internal caches to
4383 * get data visible to the CPU.
4384 *
4385 * However, we maintain the display planes as UC, and so
4386 * need to rebind when first used as such.
4387 */
4388 obj->cache_level = I915_CACHE_LLC;
4389 } else
4390 obj->cache_level = I915_CACHE_NONE;
4391
Daniel Vetterd861e332013-07-24 23:25:03 +02004392 trace_i915_gem_object_create(obj);
4393
Chris Wilson05394f32010-11-08 19:18:58 +00004394 return obj;
Chris Wilsonfe3db792016-04-25 13:32:13 +01004395
4396fail:
4397 i915_gem_object_free(obj);
4398
4399 return ERR_PTR(ret);
Daniel Vetterac52bc52010-04-09 19:05:06 +00004400}
4401
Chris Wilson340fbd82014-05-22 09:16:52 +01004402static bool discard_backing_storage(struct drm_i915_gem_object *obj)
4403{
4404 /* If we are the last user of the backing storage (be it shmemfs
4405 * pages or stolen etc), we know that the pages are going to be
4406 * immediately released. In this case, we can then skip copying
4407 * back the contents from the GPU.
4408 */
4409
4410 if (obj->madv != I915_MADV_WILLNEED)
4411 return false;
4412
4413 if (obj->base.filp == NULL)
4414 return true;
4415
4416 /* At first glance, this looks racy, but then again so would be
4417 * userspace racing mmap against close. However, the first external
4418 * reference to the filp can only be obtained through the
4419 * i915_gem_mmap_ioctl() which safeguards us against the user
4420 * acquiring such a reference whilst we are in the middle of
4421 * freeing the object.
4422 */
4423 return atomic_long_read(&obj->base.filp->f_count) == 1;
4424}
4425
Chris Wilson1488fc02012-04-24 15:47:31 +01004426void i915_gem_free_object(struct drm_gem_object *gem_obj)
Chris Wilsonbe726152010-07-23 23:18:50 +01004427{
Chris Wilson1488fc02012-04-24 15:47:31 +01004428 struct drm_i915_gem_object *obj = to_intel_bo(gem_obj);
Chris Wilson05394f32010-11-08 19:18:58 +00004429 struct drm_device *dev = obj->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01004430 struct drm_i915_private *dev_priv = to_i915(dev);
Ben Widawsky07fe0b12013-07-31 17:00:10 -07004431 struct i915_vma *vma, *next;
Chris Wilsonbe726152010-07-23 23:18:50 +01004432
Paulo Zanonif65c9162013-11-27 18:20:34 -02004433 intel_runtime_pm_get(dev_priv);
4434
Chris Wilson26e12f82011-03-20 11:20:19 +00004435 trace_i915_gem_object_destroy(obj);
4436
Chris Wilsonb1f788c2016-08-04 07:52:45 +01004437 /* All file-owned VMA should have been released by this point through
4438 * i915_gem_close_object(), or earlier by i915_gem_context_close().
4439 * However, the object may also be bound into the global GTT (e.g.
4440 * older GPUs without per-process support, or for direct access through
4441 * the GTT either for the user or for scanout). Those VMA still need to
4442 * unbound now.
4443 */
Chris Wilson1c7f4bc2016-02-26 11:03:19 +00004444 list_for_each_entry_safe(vma, next, &obj->vma_list, obj_link) {
Chris Wilson3272db52016-08-04 16:32:32 +01004445 GEM_BUG_ON(!i915_vma_is_ggtt(vma));
Chris Wilsonb1f788c2016-08-04 07:52:45 +01004446 GEM_BUG_ON(i915_vma_is_active(vma));
Chris Wilson3272db52016-08-04 16:32:32 +01004447 vma->flags &= ~I915_VMA_PIN_MASK;
Chris Wilsonb1f788c2016-08-04 07:52:45 +01004448 i915_vma_close(vma);
Chris Wilson1488fc02012-04-24 15:47:31 +01004449 }
Chris Wilson15717de2016-08-04 07:52:26 +01004450 GEM_BUG_ON(obj->bind_count);
Chris Wilson1488fc02012-04-24 15:47:31 +01004451
Ben Widawsky1d64ae72013-05-31 14:46:20 -07004452 /* Stolen objects don't hold a ref, but do hold pin count. Fix that up
4453 * before progressing. */
4454 if (obj->stolen)
4455 i915_gem_object_unpin_pages(obj);
4456
Chris Wilsonfaf5bf02016-08-04 16:32:37 +01004457 WARN_ON(atomic_read(&obj->frontbuffer_bits));
Daniel Vettera071fa02014-06-18 23:28:09 +02004458
Daniel Vetter656bfa32014-11-20 09:26:30 +01004459 if (obj->pages && obj->madv == I915_MADV_WILLNEED &&
4460 dev_priv->quirks & QUIRK_PIN_SWIZZLED_PAGES &&
Chris Wilson3e510a82016-08-05 10:14:23 +01004461 i915_gem_object_is_tiled(obj))
Daniel Vetter656bfa32014-11-20 09:26:30 +01004462 i915_gem_object_unpin_pages(obj);
4463
Ben Widawsky401c29f2013-05-31 11:28:47 -07004464 if (WARN_ON(obj->pages_pin_count))
4465 obj->pages_pin_count = 0;
Chris Wilson340fbd82014-05-22 09:16:52 +01004466 if (discard_backing_storage(obj))
Chris Wilson55372522014-03-25 13:23:06 +00004467 obj->madv = I915_MADV_DONTNEED;
Chris Wilson37e680a2012-06-07 15:38:42 +01004468 i915_gem_object_put_pages(obj);
Chris Wilsonbe726152010-07-23 23:18:50 +01004469
Chris Wilson9da3da62012-06-01 15:20:22 +01004470 BUG_ON(obj->pages);
4471
Chris Wilson2f745ad2012-09-04 21:02:58 +01004472 if (obj->base.import_attach)
4473 drm_prime_gem_destroy(&obj->base, NULL);
Chris Wilsonbe726152010-07-23 23:18:50 +01004474
Chris Wilson5cc9ed42014-05-16 14:22:37 +01004475 if (obj->ops->release)
4476 obj->ops->release(obj);
4477
Chris Wilson05394f32010-11-08 19:18:58 +00004478 drm_gem_object_release(&obj->base);
4479 i915_gem_info_remove_obj(dev_priv, obj->base.size);
Chris Wilsonbe726152010-07-23 23:18:50 +01004480
Chris Wilson05394f32010-11-08 19:18:58 +00004481 kfree(obj->bit_17);
Chris Wilson42dcedd2012-11-15 11:32:30 +00004482 i915_gem_object_free(obj);
Paulo Zanonif65c9162013-11-27 18:20:34 -02004483
4484 intel_runtime_pm_put(dev_priv);
Chris Wilsonbe726152010-07-23 23:18:50 +01004485}
4486
Chris Wilsonf8a7fde2016-10-28 13:58:29 +01004487void __i915_gem_object_release_unless_active(struct drm_i915_gem_object *obj)
4488{
4489 lockdep_assert_held(&obj->base.dev->struct_mutex);
4490
4491 GEM_BUG_ON(i915_gem_object_has_active_reference(obj));
4492 if (i915_gem_object_is_active(obj))
4493 i915_gem_object_set_active_reference(obj);
4494 else
4495 i915_gem_object_put(obj);
4496}
4497
Chris Wilsondcff85c2016-08-05 10:14:11 +01004498int i915_gem_suspend(struct drm_device *dev)
Eric Anholt673a3942008-07-30 12:06:12 -07004499{
Chris Wilsonfac5e232016-07-04 11:34:36 +01004500 struct drm_i915_private *dev_priv = to_i915(dev);
Chris Wilsondcff85c2016-08-05 10:14:11 +01004501 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004502
Chris Wilson54b4f682016-07-21 21:16:19 +01004503 intel_suspend_gt_powersave(dev_priv);
4504
Chris Wilson45c5f202013-10-16 11:50:01 +01004505 mutex_lock(&dev->struct_mutex);
Chris Wilson5ab57c72016-07-15 14:56:20 +01004506
4507 /* We have to flush all the executing contexts to main memory so
4508 * that they can saved in the hibernation image. To ensure the last
4509 * context image is coherent, we have to switch away from it. That
4510 * leaves the dev_priv->kernel_context still active when
4511 * we actually suspend, and its image in memory may not match the GPU
4512 * state. Fortunately, the kernel_context is disposable and we do
4513 * not rely on its state.
4514 */
4515 ret = i915_gem_switch_to_kernel_context(dev_priv);
4516 if (ret)
4517 goto err;
4518
Chris Wilson22dd3bb2016-09-09 14:11:50 +01004519 ret = i915_gem_wait_for_idle(dev_priv,
4520 I915_WAIT_INTERRUPTIBLE |
4521 I915_WAIT_LOCKED);
Chris Wilsonf7403342013-09-13 23:57:04 +01004522 if (ret)
Chris Wilson45c5f202013-10-16 11:50:01 +01004523 goto err;
Chris Wilsonf7403342013-09-13 23:57:04 +01004524
Chris Wilsonc0336662016-05-06 15:40:21 +01004525 i915_gem_retire_requests(dev_priv);
Eric Anholt673a3942008-07-30 12:06:12 -07004526
Chris Wilsonb2e862d2016-04-28 09:56:41 +01004527 i915_gem_context_lost(dev_priv);
Chris Wilson45c5f202013-10-16 11:50:01 +01004528 mutex_unlock(&dev->struct_mutex);
4529
Chris Wilson737b1502015-01-26 18:03:03 +02004530 cancel_delayed_work_sync(&dev_priv->gpu_error.hangcheck_work);
Chris Wilson67d97da2016-07-04 08:08:31 +01004531 cancel_delayed_work_sync(&dev_priv->gt.retire_work);
4532 flush_delayed_work(&dev_priv->gt.idle_work);
Chris Wilson29105cc2010-01-07 10:39:13 +00004533
Chris Wilsonbdcf1202014-11-25 11:56:33 +00004534 /* Assert that we sucessfully flushed all the work and
4535 * reset the GPU back to its idle, low power state.
4536 */
Chris Wilson67d97da2016-07-04 08:08:31 +01004537 WARN_ON(dev_priv->gt.awake);
Chris Wilsonbdcf1202014-11-25 11:56:33 +00004538
Imre Deak1c777c52016-10-12 17:46:37 +03004539 /*
4540 * Neither the BIOS, ourselves or any other kernel
4541 * expects the system to be in execlists mode on startup,
4542 * so we need to reset the GPU back to legacy mode. And the only
4543 * known way to disable logical contexts is through a GPU reset.
4544 *
4545 * So in order to leave the system in a known default configuration,
4546 * always reset the GPU upon unload and suspend. Afterwards we then
4547 * clean up the GEM state tracking, flushing off the requests and
4548 * leaving the system in a known idle state.
4549 *
4550 * Note that is of the upmost importance that the GPU is idle and
4551 * all stray writes are flushed *before* we dismantle the backing
4552 * storage for the pinned objects.
4553 *
4554 * However, since we are uncertain that resetting the GPU on older
4555 * machines is a good idea, we don't - just in case it leaves the
4556 * machine in an unusable condition.
4557 */
4558 if (HAS_HW_CONTEXTS(dev)) {
4559 int reset = intel_gpu_reset(dev_priv, ALL_ENGINES);
4560 WARN_ON(reset && reset != -ENODEV);
4561 }
4562
Eric Anholt673a3942008-07-30 12:06:12 -07004563 return 0;
Chris Wilson45c5f202013-10-16 11:50:01 +01004564
4565err:
4566 mutex_unlock(&dev->struct_mutex);
4567 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004568}
4569
Chris Wilson5ab57c72016-07-15 14:56:20 +01004570void i915_gem_resume(struct drm_device *dev)
4571{
4572 struct drm_i915_private *dev_priv = to_i915(dev);
4573
4574 mutex_lock(&dev->struct_mutex);
4575 i915_gem_restore_gtt_mappings(dev);
4576
4577 /* As we didn't flush the kernel context before suspend, we cannot
4578 * guarantee that the context image is complete. So let's just reset
4579 * it and start again.
4580 */
Chris Wilson821ed7d2016-09-09 14:11:53 +01004581 dev_priv->gt.resume(dev_priv);
Chris Wilson5ab57c72016-07-15 14:56:20 +01004582
4583 mutex_unlock(&dev->struct_mutex);
4584}
4585
Daniel Vetterf691e2f2012-02-02 09:58:12 +01004586void i915_gem_init_swizzling(struct drm_device *dev)
4587{
Chris Wilsonfac5e232016-07-04 11:34:36 +01004588 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetterf691e2f2012-02-02 09:58:12 +01004589
Daniel Vetter11782b02012-01-31 16:47:55 +01004590 if (INTEL_INFO(dev)->gen < 5 ||
Daniel Vetterf691e2f2012-02-02 09:58:12 +01004591 dev_priv->mm.bit_6_swizzle_x == I915_BIT_6_SWIZZLE_NONE)
4592 return;
4593
4594 I915_WRITE(DISP_ARB_CTL, I915_READ(DISP_ARB_CTL) |
4595 DISP_TILE_SURFACE_SWIZZLING);
4596
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01004597 if (IS_GEN5(dev_priv))
Daniel Vetter11782b02012-01-31 16:47:55 +01004598 return;
4599
Daniel Vetterf691e2f2012-02-02 09:58:12 +01004600 I915_WRITE(TILECTL, I915_READ(TILECTL) | TILECTL_SWZCTL);
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01004601 if (IS_GEN6(dev_priv))
Daniel Vetter6b26c862012-04-24 14:04:12 +02004602 I915_WRITE(ARB_MODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_SNB));
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01004603 else if (IS_GEN7(dev_priv))
Daniel Vetter6b26c862012-04-24 14:04:12 +02004604 I915_WRITE(ARB_MODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_IVB));
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01004605 else if (IS_GEN8(dev_priv))
Ben Widawsky31a53362013-11-02 21:07:04 -07004606 I915_WRITE(GAMTARBMODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_BDW));
Ben Widawsky8782e262012-12-18 10:31:23 -08004607 else
4608 BUG();
Daniel Vetterf691e2f2012-02-02 09:58:12 +01004609}
Daniel Vettere21af882012-02-09 20:53:27 +01004610
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01004611static void init_unused_ring(struct drm_i915_private *dev_priv, u32 base)
Ville Syrjälä81e7f202014-08-15 01:21:55 +03004612{
Ville Syrjälä81e7f202014-08-15 01:21:55 +03004613 I915_WRITE(RING_CTL(base), 0);
4614 I915_WRITE(RING_HEAD(base), 0);
4615 I915_WRITE(RING_TAIL(base), 0);
4616 I915_WRITE(RING_START(base), 0);
4617}
4618
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01004619static void init_unused_rings(struct drm_i915_private *dev_priv)
Ville Syrjälä81e7f202014-08-15 01:21:55 +03004620{
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01004621 if (IS_I830(dev_priv)) {
4622 init_unused_ring(dev_priv, PRB1_BASE);
4623 init_unused_ring(dev_priv, SRB0_BASE);
4624 init_unused_ring(dev_priv, SRB1_BASE);
4625 init_unused_ring(dev_priv, SRB2_BASE);
4626 init_unused_ring(dev_priv, SRB3_BASE);
4627 } else if (IS_GEN2(dev_priv)) {
4628 init_unused_ring(dev_priv, SRB0_BASE);
4629 init_unused_ring(dev_priv, SRB1_BASE);
4630 } else if (IS_GEN3(dev_priv)) {
4631 init_unused_ring(dev_priv, PRB1_BASE);
4632 init_unused_ring(dev_priv, PRB2_BASE);
Ville Syrjälä81e7f202014-08-15 01:21:55 +03004633 }
4634}
4635
Ben Widawsky4fc7c972013-02-08 11:49:24 -08004636int
4637i915_gem_init_hw(struct drm_device *dev)
4638{
Chris Wilsonfac5e232016-07-04 11:34:36 +01004639 struct drm_i915_private *dev_priv = to_i915(dev);
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00004640 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05304641 enum intel_engine_id id;
Chris Wilsond200cda2016-04-28 09:56:44 +01004642 int ret;
Ben Widawsky4fc7c972013-02-08 11:49:24 -08004643
Chris Wilsonde867c22016-10-25 13:16:02 +01004644 dev_priv->gt.last_init_time = ktime_get();
4645
Chris Wilson5e4f5182015-02-13 14:35:59 +00004646 /* Double layer security blanket, see i915_gem_init() */
4647 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
4648
Mika Kuoppala3accaf72016-04-13 17:26:43 +03004649 if (HAS_EDRAM(dev) && INTEL_GEN(dev_priv) < 9)
Ben Widawsky05e21cc2013-07-04 11:02:04 -07004650 I915_WRITE(HSW_IDICR, I915_READ(HSW_IDICR) | IDIHASHMSK(0xf));
Ben Widawsky4fc7c972013-02-08 11:49:24 -08004651
Tvrtko Ursulin772c2a52016-10-13 11:03:01 +01004652 if (IS_HASWELL(dev_priv))
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01004653 I915_WRITE(MI_PREDICATE_RESULT_2, IS_HSW_GT3(dev_priv) ?
Ville Syrjälä0bf21342013-11-29 14:56:12 +02004654 LOWER_SLICE_ENABLED : LOWER_SLICE_DISABLED);
Rodrigo Vivi94353732013-08-28 16:45:46 -03004655
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01004656 if (HAS_PCH_NOP(dev_priv)) {
Tvrtko Ursulinfd6b8f42016-10-14 10:13:06 +01004657 if (IS_IVYBRIDGE(dev_priv)) {
Daniel Vetter6ba844b2014-01-22 23:39:30 +01004658 u32 temp = I915_READ(GEN7_MSG_CTL);
4659 temp &= ~(WAIT_FOR_PCH_FLR_ACK | WAIT_FOR_PCH_RESET_ACK);
4660 I915_WRITE(GEN7_MSG_CTL, temp);
4661 } else if (INTEL_INFO(dev)->gen >= 7) {
4662 u32 temp = I915_READ(HSW_NDE_RSTWRN_OPT);
4663 temp &= ~RESET_PCH_HANDSHAKE_ENABLE;
4664 I915_WRITE(HSW_NDE_RSTWRN_OPT, temp);
4665 }
Ben Widawsky88a2b2a2013-04-05 13:12:43 -07004666 }
4667
Ben Widawsky4fc7c972013-02-08 11:49:24 -08004668 i915_gem_init_swizzling(dev);
4669
Daniel Vetterd5abdfd2014-11-20 09:45:19 +01004670 /*
4671 * At least 830 can leave some of the unused rings
4672 * "active" (ie. head != tail) after resume which
4673 * will prevent c3 entry. Makes sure all unused rings
4674 * are totally idle.
4675 */
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01004676 init_unused_rings(dev_priv);
Daniel Vetterd5abdfd2014-11-20 09:45:19 +01004677
Dave Gordoned54c1a2016-01-19 19:02:54 +00004678 BUG_ON(!dev_priv->kernel_context);
John Harrison90638cc2015-05-29 17:43:37 +01004679
John Harrison4ad2fd82015-06-18 13:11:20 +01004680 ret = i915_ppgtt_init_hw(dev);
4681 if (ret) {
4682 DRM_ERROR("PPGTT enable HW failed %d\n", ret);
4683 goto out;
4684 }
4685
4686 /* Need to do basic initialisation of all rings first: */
Akash Goel3b3f1652016-10-13 22:44:48 +05304687 for_each_engine(engine, dev_priv, id) {
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00004688 ret = engine->init_hw(engine);
Daniel Vetter35a57ff2014-11-20 00:33:07 +01004689 if (ret)
Chris Wilson5e4f5182015-02-13 14:35:59 +00004690 goto out;
Daniel Vetter35a57ff2014-11-20 00:33:07 +01004691 }
Mika Kuoppala99433932013-01-22 14:12:17 +02004692
Peter Antoine0ccdacf2016-04-13 15:03:25 +01004693 intel_mocs_init_l3cc_table(dev);
4694
Alex Dai33a732f2015-08-12 15:43:36 +01004695 /* We can't enable contexts until all firmware is loaded */
Dave Gordone556f7c2016-06-07 09:14:49 +01004696 ret = intel_guc_setup(dev);
4697 if (ret)
4698 goto out;
Alex Dai33a732f2015-08-12 15:43:36 +01004699
Chris Wilson5e4f5182015-02-13 14:35:59 +00004700out:
4701 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Ben Widawsky2fa48d82013-12-06 14:11:04 -08004702 return ret;
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004703}
4704
Chris Wilson39df9192016-07-20 13:31:57 +01004705bool intel_sanitize_semaphores(struct drm_i915_private *dev_priv, int value)
4706{
4707 if (INTEL_INFO(dev_priv)->gen < 6)
4708 return false;
4709
4710 /* TODO: make semaphores and Execlists play nicely together */
4711 if (i915.enable_execlists)
4712 return false;
4713
4714 if (value >= 0)
4715 return value;
4716
4717#ifdef CONFIG_INTEL_IOMMU
4718 /* Enable semaphores on SNB when IO remapping is off */
4719 if (INTEL_INFO(dev_priv)->gen == 6 && intel_iommu_gfx_mapped)
4720 return false;
4721#endif
4722
4723 return true;
4724}
4725
Chris Wilson1070a422012-04-24 15:47:41 +01004726int i915_gem_init(struct drm_device *dev)
4727{
Chris Wilsonfac5e232016-07-04 11:34:36 +01004728 struct drm_i915_private *dev_priv = to_i915(dev);
Chris Wilson1070a422012-04-24 15:47:41 +01004729 int ret;
4730
Chris Wilson1070a422012-04-24 15:47:41 +01004731 mutex_lock(&dev->struct_mutex);
Jesse Barnesd62b4892013-03-08 10:45:53 -08004732
Oscar Mateoa83014d2014-07-24 17:04:21 +01004733 if (!i915.enable_execlists) {
Chris Wilson821ed7d2016-09-09 14:11:53 +01004734 dev_priv->gt.resume = intel_legacy_submission_resume;
Chris Wilson7e37f882016-08-02 22:50:21 +01004735 dev_priv->gt.cleanup_engine = intel_engine_cleanup;
Oscar Mateo454afeb2014-07-24 17:04:22 +01004736 } else {
Chris Wilson821ed7d2016-09-09 14:11:53 +01004737 dev_priv->gt.resume = intel_lr_context_resume;
Tvrtko Ursulin117897f2016-03-16 11:00:40 +00004738 dev_priv->gt.cleanup_engine = intel_logical_ring_cleanup;
Oscar Mateoa83014d2014-07-24 17:04:21 +01004739 }
4740
Chris Wilson5e4f5182015-02-13 14:35:59 +00004741 /* This is just a security blanket to placate dragons.
4742 * On some systems, we very sporadically observe that the first TLBs
4743 * used by the CS may be stale, despite us poking the TLB reset. If
4744 * we hold the forcewake during initialisation these problems
4745 * just magically go away.
4746 */
4747 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
4748
Chris Wilson72778cb2016-05-19 16:17:16 +01004749 i915_gem_init_userptr(dev_priv);
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01004750
4751 ret = i915_gem_init_ggtt(dev_priv);
4752 if (ret)
4753 goto out_unlock;
Jesse Barnesd62b4892013-03-08 10:45:53 -08004754
Ben Widawsky2fa48d82013-12-06 14:11:04 -08004755 ret = i915_gem_context_init(dev);
Jani Nikula7bcc3772014-12-05 14:17:42 +02004756 if (ret)
4757 goto out_unlock;
Ben Widawsky2fa48d82013-12-06 14:11:04 -08004758
Tvrtko Ursulin8b3e2d32016-07-13 16:03:37 +01004759 ret = intel_engines_init(dev);
Daniel Vetter35a57ff2014-11-20 00:33:07 +01004760 if (ret)
Jani Nikula7bcc3772014-12-05 14:17:42 +02004761 goto out_unlock;
Daniel Vetter53ca26c2012-04-26 23:28:03 +02004762
4763 ret = i915_gem_init_hw(dev);
Chris Wilson60990322014-04-09 09:19:42 +01004764 if (ret == -EIO) {
Chris Wilson7e21d642016-07-27 09:07:29 +01004765 /* Allow engine initialisation to fail by marking the GPU as
Chris Wilson60990322014-04-09 09:19:42 +01004766 * wedged. But we only want to do this where the GPU is angry,
4767 * for all other failure, such as an allocation failure, bail.
4768 */
4769 DRM_ERROR("Failed to initialize GPU, declaring it wedged\n");
Chris Wilson821ed7d2016-09-09 14:11:53 +01004770 i915_gem_set_wedged(dev_priv);
Chris Wilson60990322014-04-09 09:19:42 +01004771 ret = 0;
Chris Wilson1070a422012-04-24 15:47:41 +01004772 }
Jani Nikula7bcc3772014-12-05 14:17:42 +02004773
4774out_unlock:
Chris Wilson5e4f5182015-02-13 14:35:59 +00004775 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Chris Wilson60990322014-04-09 09:19:42 +01004776 mutex_unlock(&dev->struct_mutex);
Chris Wilson1070a422012-04-24 15:47:41 +01004777
Chris Wilson60990322014-04-09 09:19:42 +01004778 return ret;
Chris Wilson1070a422012-04-24 15:47:41 +01004779}
4780
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004781void
Tvrtko Ursulin117897f2016-03-16 11:00:40 +00004782i915_gem_cleanup_engines(struct drm_device *dev)
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004783{
Chris Wilsonfac5e232016-07-04 11:34:36 +01004784 struct drm_i915_private *dev_priv = to_i915(dev);
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00004785 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05304786 enum intel_engine_id id;
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004787
Akash Goel3b3f1652016-10-13 22:44:48 +05304788 for_each_engine(engine, dev_priv, id)
Tvrtko Ursulin117897f2016-03-16 11:00:40 +00004789 dev_priv->gt.cleanup_engine(engine);
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004790}
4791
Eric Anholt673a3942008-07-30 12:06:12 -07004792void
Imre Deak40ae4e12016-03-16 14:54:03 +02004793i915_gem_load_init_fences(struct drm_i915_private *dev_priv)
4794{
Chris Wilson91c8a322016-07-05 10:40:23 +01004795 struct drm_device *dev = &dev_priv->drm;
Chris Wilson49ef5292016-08-18 17:17:00 +01004796 int i;
Imre Deak40ae4e12016-03-16 14:54:03 +02004797
4798 if (INTEL_INFO(dev_priv)->gen >= 7 && !IS_VALLEYVIEW(dev_priv) &&
4799 !IS_CHERRYVIEW(dev_priv))
4800 dev_priv->num_fence_regs = 32;
4801 else if (INTEL_INFO(dev_priv)->gen >= 4 || IS_I945G(dev_priv) ||
4802 IS_I945GM(dev_priv) || IS_G33(dev_priv))
4803 dev_priv->num_fence_regs = 16;
4804 else
4805 dev_priv->num_fence_regs = 8;
4806
Chris Wilsonc0336662016-05-06 15:40:21 +01004807 if (intel_vgpu_active(dev_priv))
Imre Deak40ae4e12016-03-16 14:54:03 +02004808 dev_priv->num_fence_regs =
4809 I915_READ(vgtif_reg(avail_rs.fence_num));
4810
4811 /* Initialize fence registers to zero */
Chris Wilson49ef5292016-08-18 17:17:00 +01004812 for (i = 0; i < dev_priv->num_fence_regs; i++) {
4813 struct drm_i915_fence_reg *fence = &dev_priv->fence_regs[i];
4814
4815 fence->i915 = dev_priv;
4816 fence->id = i;
4817 list_add_tail(&fence->link, &dev_priv->mm.fence_list);
4818 }
Imre Deak40ae4e12016-03-16 14:54:03 +02004819 i915_gem_restore_fences(dev);
4820
4821 i915_gem_detect_bit_6_swizzle(dev);
4822}
4823
4824void
Imre Deakd64aa092016-01-19 15:26:29 +02004825i915_gem_load_init(struct drm_device *dev)
Eric Anholt673a3942008-07-30 12:06:12 -07004826{
Chris Wilsonfac5e232016-07-04 11:34:36 +01004827 struct drm_i915_private *dev_priv = to_i915(dev);
Chris Wilson42dcedd2012-11-15 11:32:30 +00004828
Chris Wilsonefab6d82015-04-07 16:20:57 +01004829 dev_priv->objects =
Chris Wilson42dcedd2012-11-15 11:32:30 +00004830 kmem_cache_create("i915_gem_object",
4831 sizeof(struct drm_i915_gem_object), 0,
4832 SLAB_HWCACHE_ALIGN,
4833 NULL);
Chris Wilsone20d2ab2015-04-07 16:20:58 +01004834 dev_priv->vmas =
4835 kmem_cache_create("i915_gem_vma",
4836 sizeof(struct i915_vma), 0,
4837 SLAB_HWCACHE_ALIGN,
4838 NULL);
Chris Wilsonefab6d82015-04-07 16:20:57 +01004839 dev_priv->requests =
4840 kmem_cache_create("i915_gem_request",
4841 sizeof(struct drm_i915_gem_request), 0,
Chris Wilson0eafec62016-08-04 16:32:41 +01004842 SLAB_HWCACHE_ALIGN |
4843 SLAB_RECLAIM_ACCOUNT |
4844 SLAB_DESTROY_BY_RCU,
Chris Wilsonefab6d82015-04-07 16:20:57 +01004845 NULL);
Eric Anholt673a3942008-07-30 12:06:12 -07004846
Ben Widawskya33afea2013-09-17 21:12:45 -07004847 INIT_LIST_HEAD(&dev_priv->context_list);
Chris Wilson6c085a72012-08-20 11:40:46 +02004848 INIT_LIST_HEAD(&dev_priv->mm.unbound_list);
4849 INIT_LIST_HEAD(&dev_priv->mm.bound_list);
Eric Anholta09ba7f2009-08-29 12:49:51 -07004850 INIT_LIST_HEAD(&dev_priv->mm.fence_list);
Chris Wilson275f0392016-10-24 13:42:14 +01004851 INIT_LIST_HEAD(&dev_priv->mm.userfault_list);
Chris Wilson67d97da2016-07-04 08:08:31 +01004852 INIT_DELAYED_WORK(&dev_priv->gt.retire_work,
Eric Anholt673a3942008-07-30 12:06:12 -07004853 i915_gem_retire_work_handler);
Chris Wilson67d97da2016-07-04 08:08:31 +01004854 INIT_DELAYED_WORK(&dev_priv->gt.idle_work,
Chris Wilsonb29c19b2013-09-25 17:34:56 +01004855 i915_gem_idle_work_handler);
Chris Wilson1f15b762016-07-01 17:23:14 +01004856 init_waitqueue_head(&dev_priv->gpu_error.wait_queue);
Daniel Vetter1f83fee2012-11-15 17:17:22 +01004857 init_waitqueue_head(&dev_priv->gpu_error.reset_queue);
Chris Wilson31169712009-09-14 16:50:28 +01004858
Chris Wilson72bfa192010-12-19 11:42:05 +00004859 dev_priv->relative_constants_mode = I915_EXEC_CONSTANTS_REL_GENERAL;
4860
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05004861 init_waitqueue_head(&dev_priv->pending_flip_queue);
Chris Wilson17250b72010-10-28 12:51:39 +01004862
Chris Wilsonce453d82011-02-21 14:43:56 +00004863 dev_priv->mm.interruptible = true;
4864
Joonas Lahtinen6f633402016-09-01 14:58:21 +03004865 atomic_set(&dev_priv->mm.bsd_engine_dispatch_index, 0);
4866
Chris Wilsonb5add952016-08-04 16:32:36 +01004867 spin_lock_init(&dev_priv->fb_tracking.lock);
Eric Anholt673a3942008-07-30 12:06:12 -07004868}
Dave Airlie71acb5e2008-12-30 20:31:46 +10004869
Imre Deakd64aa092016-01-19 15:26:29 +02004870void i915_gem_load_cleanup(struct drm_device *dev)
4871{
4872 struct drm_i915_private *dev_priv = to_i915(dev);
4873
4874 kmem_cache_destroy(dev_priv->requests);
4875 kmem_cache_destroy(dev_priv->vmas);
4876 kmem_cache_destroy(dev_priv->objects);
Chris Wilson0eafec62016-08-04 16:32:41 +01004877
4878 /* And ensure that our DESTROY_BY_RCU slabs are truly destroyed */
4879 rcu_barrier();
Imre Deakd64aa092016-01-19 15:26:29 +02004880}
4881
Chris Wilson6a800ea2016-09-21 14:51:07 +01004882int i915_gem_freeze(struct drm_i915_private *dev_priv)
4883{
4884 intel_runtime_pm_get(dev_priv);
4885
4886 mutex_lock(&dev_priv->drm.struct_mutex);
4887 i915_gem_shrink_all(dev_priv);
4888 mutex_unlock(&dev_priv->drm.struct_mutex);
4889
4890 intel_runtime_pm_put(dev_priv);
4891
4892 return 0;
4893}
4894
Chris Wilson461fb992016-05-14 07:26:33 +01004895int i915_gem_freeze_late(struct drm_i915_private *dev_priv)
4896{
4897 struct drm_i915_gem_object *obj;
Chris Wilson7aab2d52016-09-09 20:02:18 +01004898 struct list_head *phases[] = {
4899 &dev_priv->mm.unbound_list,
4900 &dev_priv->mm.bound_list,
4901 NULL
4902 }, **p;
Chris Wilson461fb992016-05-14 07:26:33 +01004903
4904 /* Called just before we write the hibernation image.
4905 *
4906 * We need to update the domain tracking to reflect that the CPU
4907 * will be accessing all the pages to create and restore from the
4908 * hibernation, and so upon restoration those pages will be in the
4909 * CPU domain.
4910 *
4911 * To make sure the hibernation image contains the latest state,
4912 * we update that state just before writing out the image.
Chris Wilson7aab2d52016-09-09 20:02:18 +01004913 *
4914 * To try and reduce the hibernation image, we manually shrink
4915 * the objects as well.
Chris Wilson461fb992016-05-14 07:26:33 +01004916 */
4917
Chris Wilson6a800ea2016-09-21 14:51:07 +01004918 mutex_lock(&dev_priv->drm.struct_mutex);
4919 i915_gem_shrink(dev_priv, -1UL, I915_SHRINK_UNBOUND);
Chris Wilson461fb992016-05-14 07:26:33 +01004920
Chris Wilson7aab2d52016-09-09 20:02:18 +01004921 for (p = phases; *p; p++) {
4922 list_for_each_entry(obj, *p, global_list) {
4923 obj->base.read_domains = I915_GEM_DOMAIN_CPU;
4924 obj->base.write_domain = I915_GEM_DOMAIN_CPU;
4925 }
Chris Wilson461fb992016-05-14 07:26:33 +01004926 }
Chris Wilson6a800ea2016-09-21 14:51:07 +01004927 mutex_unlock(&dev_priv->drm.struct_mutex);
Chris Wilson461fb992016-05-14 07:26:33 +01004928
4929 return 0;
4930}
4931
Chris Wilsonf787a5f2010-09-24 16:02:42 +01004932void i915_gem_release(struct drm_device *dev, struct drm_file *file)
Eric Anholtb9624422009-06-03 07:27:35 +00004933{
Chris Wilsonf787a5f2010-09-24 16:02:42 +01004934 struct drm_i915_file_private *file_priv = file->driver_priv;
Chris Wilson15f7bbc2016-07-26 12:01:52 +01004935 struct drm_i915_gem_request *request;
Eric Anholtb9624422009-06-03 07:27:35 +00004936
4937 /* Clean up our request list when the client is going away, so that
4938 * later retire_requests won't dereference our soon-to-be-gone
4939 * file_priv.
4940 */
Chris Wilson1c255952010-09-26 11:03:27 +01004941 spin_lock(&file_priv->mm.lock);
Chris Wilson15f7bbc2016-07-26 12:01:52 +01004942 list_for_each_entry(request, &file_priv->mm.request_list, client_list)
Chris Wilsonf787a5f2010-09-24 16:02:42 +01004943 request->file_priv = NULL;
Chris Wilson1c255952010-09-26 11:03:27 +01004944 spin_unlock(&file_priv->mm.lock);
Chris Wilson31169712009-09-14 16:50:28 +01004945
Chris Wilson2e1b8732015-04-27 13:41:22 +01004946 if (!list_empty(&file_priv->rps.link)) {
Chris Wilson8d3afd72015-05-21 21:01:47 +01004947 spin_lock(&to_i915(dev)->rps.client_lock);
Chris Wilson2e1b8732015-04-27 13:41:22 +01004948 list_del(&file_priv->rps.link);
Chris Wilson8d3afd72015-05-21 21:01:47 +01004949 spin_unlock(&to_i915(dev)->rps.client_lock);
Chris Wilson1854d5c2015-04-07 16:20:32 +01004950 }
Chris Wilsonb29c19b2013-09-25 17:34:56 +01004951}
4952
4953int i915_gem_open(struct drm_device *dev, struct drm_file *file)
4954{
4955 struct drm_i915_file_private *file_priv;
Ben Widawskye422b882013-12-06 14:10:58 -08004956 int ret;
Chris Wilsonb29c19b2013-09-25 17:34:56 +01004957
4958 DRM_DEBUG_DRIVER("\n");
4959
4960 file_priv = kzalloc(sizeof(*file_priv), GFP_KERNEL);
4961 if (!file_priv)
4962 return -ENOMEM;
4963
4964 file->driver_priv = file_priv;
Dave Gordonf19ec8c2016-07-04 11:34:37 +01004965 file_priv->dev_priv = to_i915(dev);
Chris Wilsonab0e7ff2014-02-25 17:11:24 +02004966 file_priv->file = file;
Chris Wilson2e1b8732015-04-27 13:41:22 +01004967 INIT_LIST_HEAD(&file_priv->rps.link);
Chris Wilsonb29c19b2013-09-25 17:34:56 +01004968
4969 spin_lock_init(&file_priv->mm.lock);
4970 INIT_LIST_HEAD(&file_priv->mm.request_list);
Chris Wilsonb29c19b2013-09-25 17:34:56 +01004971
Chris Wilsonc80ff162016-07-27 09:07:27 +01004972 file_priv->bsd_engine = -1;
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00004973
Ben Widawskye422b882013-12-06 14:10:58 -08004974 ret = i915_gem_context_open(dev, file);
4975 if (ret)
4976 kfree(file_priv);
Chris Wilsonb29c19b2013-09-25 17:34:56 +01004977
Ben Widawskye422b882013-12-06 14:10:58 -08004978 return ret;
Chris Wilsonb29c19b2013-09-25 17:34:56 +01004979}
4980
Daniel Vetterb680c372014-09-19 18:27:27 +02004981/**
4982 * i915_gem_track_fb - update frontbuffer tracking
Geliang Tangd9072a32015-09-15 05:58:44 -07004983 * @old: current GEM buffer for the frontbuffer slots
4984 * @new: new GEM buffer for the frontbuffer slots
4985 * @frontbuffer_bits: bitmask of frontbuffer slots
Daniel Vetterb680c372014-09-19 18:27:27 +02004986 *
4987 * This updates the frontbuffer tracking bits @frontbuffer_bits by clearing them
4988 * from @old and setting them in @new. Both @old and @new can be NULL.
4989 */
Daniel Vettera071fa02014-06-18 23:28:09 +02004990void i915_gem_track_fb(struct drm_i915_gem_object *old,
4991 struct drm_i915_gem_object *new,
4992 unsigned frontbuffer_bits)
4993{
Chris Wilsonfaf5bf02016-08-04 16:32:37 +01004994 /* Control of individual bits within the mask are guarded by
4995 * the owning plane->mutex, i.e. we can never see concurrent
4996 * manipulation of individual bits. But since the bitfield as a whole
4997 * is updated using RMW, we need to use atomics in order to update
4998 * the bits.
4999 */
5000 BUILD_BUG_ON(INTEL_FRONTBUFFER_BITS_PER_PIPE * I915_MAX_PIPES >
5001 sizeof(atomic_t) * BITS_PER_BYTE);
5002
Daniel Vettera071fa02014-06-18 23:28:09 +02005003 if (old) {
Chris Wilsonfaf5bf02016-08-04 16:32:37 +01005004 WARN_ON(!(atomic_read(&old->frontbuffer_bits) & frontbuffer_bits));
5005 atomic_andnot(frontbuffer_bits, &old->frontbuffer_bits);
Daniel Vettera071fa02014-06-18 23:28:09 +02005006 }
5007
5008 if (new) {
Chris Wilsonfaf5bf02016-08-04 16:32:37 +01005009 WARN_ON(atomic_read(&new->frontbuffer_bits) & frontbuffer_bits);
5010 atomic_or(frontbuffer_bits, &new->frontbuffer_bits);
Daniel Vettera071fa02014-06-18 23:28:09 +02005011 }
5012}
5013
Dave Gordon033908a2015-12-10 18:51:23 +00005014/* Like i915_gem_object_get_page(), but mark the returned page dirty */
5015struct page *
5016i915_gem_object_get_dirty_page(struct drm_i915_gem_object *obj, int n)
5017{
5018 struct page *page;
5019
5020 /* Only default objects have per-page dirty tracking */
Chris Wilsonb9bcd142016-06-20 15:05:51 +01005021 if (WARN_ON(!i915_gem_object_has_struct_page(obj)))
Dave Gordon033908a2015-12-10 18:51:23 +00005022 return NULL;
5023
5024 page = i915_gem_object_get_page(obj, n);
5025 set_page_dirty(page);
5026 return page;
5027}
5028
Dave Gordonea702992015-07-09 19:29:02 +01005029/* Allocate a new GEM object and fill it with the supplied data */
5030struct drm_i915_gem_object *
5031i915_gem_object_create_from_data(struct drm_device *dev,
5032 const void *data, size_t size)
5033{
5034 struct drm_i915_gem_object *obj;
5035 struct sg_table *sg;
5036 size_t bytes;
5037 int ret;
5038
Dave Gordond37cd8a2016-04-22 19:14:32 +01005039 obj = i915_gem_object_create(dev, round_up(size, PAGE_SIZE));
Chris Wilsonfe3db792016-04-25 13:32:13 +01005040 if (IS_ERR(obj))
Dave Gordonea702992015-07-09 19:29:02 +01005041 return obj;
5042
5043 ret = i915_gem_object_set_to_cpu_domain(obj, true);
5044 if (ret)
5045 goto fail;
5046
5047 ret = i915_gem_object_get_pages(obj);
5048 if (ret)
5049 goto fail;
5050
5051 i915_gem_object_pin_pages(obj);
5052 sg = obj->pages;
5053 bytes = sg_copy_from_buffer(sg->sgl, sg->nents, (void *)data, size);
Dave Gordon9e7d18c2015-12-10 18:51:24 +00005054 obj->dirty = 1; /* Backing store is now out of date */
Dave Gordonea702992015-07-09 19:29:02 +01005055 i915_gem_object_unpin_pages(obj);
5056
5057 if (WARN_ON(bytes != size)) {
5058 DRM_ERROR("Incomplete copy, wrote %zu of %zu", bytes, size);
5059 ret = -EFAULT;
5060 goto fail;
5061 }
5062
5063 return obj;
5064
5065fail:
Chris Wilsonf8c417c2016-07-20 13:31:53 +01005066 i915_gem_object_put(obj);
Dave Gordonea702992015-07-09 19:29:02 +01005067 return ERR_PTR(ret);
5068}